boot.c revision 1.18 1 /* $NetBSD: boot.c,v 1.18 2010/08/25 16:35:57 christos Exp $ */
2
3 /*-
4 * Copyright (C) 1999 Tsubai Masanari. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <lib/libkern/libkern.h>
30 #include <lib/libsa/stand.h>
31 #include <lib/libsa/loadfile.h>
32
33 #include <machine/apcall.h>
34 #include <machine/bootinfo.h>
35 #include <machine/cpu.h>
36 #include <machine/romcall.h>
37
38 void boot(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
39
40 void mips1_flushicache(void *, int);
41 extern char _edata[], _end[];
42
43 /* version strings in vers.c (generated by newvers.sh) */
44 extern const char bootprog_name[];
45 extern const char bootprog_rev[];
46 extern const char bootprog_date[];
47 extern const char bootprog_maker[];
48
49 struct apbus_sysinfo *_sip;
50 int apbus;
51
52 char *devs[] = { "sd", "fh", "fd", NULL, NULL, "rd", "st" };
53 char *kernels[] = { "/netbsd", "/netbsd.gz", NULL };
54
55 #ifdef BOOT_DEBUG
56 # define DPRINTF printf
57 #else
58 # define DPRINTF while (0) printf
59 #endif
60
61 void
62 boot(uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3, uint32_t a4,
63 uint32_t a5)
64 {
65 int fd, i;
66 char *netbsd = "";
67 int maxmem;
68 u_long marks[MARK_MAX];
69 char devname[32], file[32];
70 void (*entry)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t,
71 uint32_t);
72 struct btinfo_symtab bi_sym;
73 struct btinfo_bootarg bi_arg;
74 struct btinfo_bootpath bi_bpath;
75 struct btinfo_systype bi_sys;
76 int loadflag;
77
78 /* Clear BSS. */
79 memset(_edata, 0, _end - _edata);
80
81 /*
82 * XXX a3 contains:
83 * maxmem (nws-3xxx)
84 * argv (apbus-based machine)
85 */
86 if (a3 >= 0x80000000)
87 apbus = 1;
88 else
89 apbus = 0;
90
91 if (apbus)
92 _sip = (void *)a4;
93
94 printf("%s Secondary Boot, Revision %s\n",
95 bootprog_name, bootprog_rev);
96 printf("(%s, %s)\n", bootprog_maker, bootprog_date);
97
98 if (apbus) {
99 char *bootdev = (char *)a1;
100 int argc = a2;
101 char **argv = (char **)a3;
102
103 DPRINTF("APbus-based system\n");
104
105 DPRINTF("argc = %d\n", argc);
106 for (i = 0; i < argc; i++) {
107 DPRINTF("argv[%d] = %s\n", i, argv[i]);
108 if (argv[i][0] != '-' && *netbsd == 0)
109 netbsd = argv[i];
110 }
111 maxmem = _sip->apbsi_memsize;
112 maxmem -= 0x100000; /* reserve 1MB for ROM monitor */
113
114 DPRINTF("howto = 0x%x\n", a0);
115 DPRINTF("bootdev = %s\n", (char *)a1);
116 DPRINTF("bootname = %s\n", netbsd);
117 DPRINTF("maxmem = 0x%x\n", maxmem);
118
119 /* XXX use "sonic()" instead of "tftp()" */
120 if (strncmp(bootdev, "tftp", 4) == 0)
121 bootdev = "sonic";
122
123 strcpy(devname, bootdev);
124 if (strchr(devname, '(') == NULL)
125 strcat(devname, "()");
126 } else {
127 int bootdev = a1;
128 char *bootname = (char *)a2;
129 int ctlr, unit, part, type;
130
131 DPRINTF("HB system.\n");
132
133 /* bootname is "/boot" by default on HB system. */
134 if (bootname && strcmp(bootname, "/boot") != 0)
135 netbsd = bootname;
136 maxmem = a3;
137
138 DPRINTF("howto = 0x%x\n", a0);
139 DPRINTF("bootdev = 0x%x\n", a1);
140 DPRINTF("bootname = %s\n", netbsd);
141 DPRINTF("maxmem = 0x%x\n", maxmem);
142
143 ctlr = BOOTDEV_CTLR(bootdev);
144 unit = BOOTDEV_UNIT(bootdev);
145 part = BOOTDEV_PART(bootdev);
146 type = BOOTDEV_TYPE(bootdev);
147
148 if (devs[type] == NULL) {
149 printf("unknown bootdev (0x%x)\n", bootdev);
150 _rtt();
151 }
152
153 sprintf(devname, "%s(%d,%d,%d)", devs[type], ctlr, unit, part);
154 }
155
156 printf("Booting %s%s\n", devname, netbsd);
157
158 /* use user specified kernel name if exists */
159 if (*netbsd) {
160 kernels[0] = netbsd;
161 kernels[1] = NULL;
162 }
163
164 loadflag = LOAD_KERNEL;
165 if (devname[0] == 'f') /* XXX */
166 loadflag &= ~LOAD_BACKWARDS;
167
168 marks[MARK_START] = 0;
169
170 for (i = 0; kernels[i]; i++) {
171 sprintf(file, "%s%s", devname, kernels[i]);
172 DPRINTF("trying %s...\n", file);
173 fd = loadfile(file, marks, loadflag);
174 if (fd != -1)
175 break;
176 }
177 if (kernels[i] == NULL)
178 _rtt();
179
180 DPRINTF("entry = 0x%x\n", (int)marks[MARK_ENTRY]);
181 DPRINTF("ssym = 0x%x\n", (int)marks[MARK_SYM]);
182 DPRINTF("esym = 0x%x\n", (int)marks[MARK_END]);
183
184 bi_init(BOOTINFO_ADDR);
185
186 bi_sym.nsym = marks[MARK_NSYM];
187 bi_sym.ssym = marks[MARK_SYM];
188 bi_sym.esym = marks[MARK_END];
189 bi_add(&bi_sym, BTINFO_SYMTAB, sizeof(bi_sym));
190
191 bi_arg.howto = a0;
192 bi_arg.bootdev = a1;
193 bi_arg.maxmem = maxmem;
194 bi_arg.sip = (int)_sip;
195 bi_add(&bi_arg, BTINFO_BOOTARG, sizeof(bi_arg));
196
197 strcpy(bi_bpath.bootpath, file);
198 bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
199
200 bi_sys.type = apbus ? NEWS5000 : NEWS3400; /* XXX */
201 bi_add(&bi_sys, BTINFO_SYSTYPE, sizeof(bi_sys));
202
203 entry = (void *)marks[MARK_ENTRY];
204
205 if (apbus)
206 apcall_flushcache();
207 else
208 mips1_flushicache(entry, marks[MARK_SYM] - marks[MARK_ENTRY]);
209
210 printf("\n");
211 (*entry)(a0, a1, a2, a3, a4, a5);
212 }
213
214 void
215 putchar(int x)
216 {
217 char c = x;
218
219 if (apbus)
220 apcall_write(1, &c, 1);
221 else
222 rom_write(1, &c, 1);
223 }
224
225 int
226 getchar(void)
227 {
228 unsigned char c = '\0';
229 int i;
230
231 for (;;) {
232 i = apbus ? apcall_read(1, &c, 1) : rom_read(1, &c, 1);
233 if (i == 1)
234 break;
235 if (i != -2 && i != 0)
236 return -1;
237 }
238 return c;
239 }
240
241 void
242 _rtt(void)
243 {
244 if (apbus)
245 apcall_exit(8);
246 else
247 rom_halt();
248
249 for (;;);
250 }
251