boot.c revision 1.15 1 /* $NetBSD: boot.c,v 1.15 2005/02/08 08:36:22 he 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
77 /* Clear BSS. */
78 memset(_edata, 0, _end - _edata);
79
80 /*
81 * XXX a3 contains:
82 * maxmem (nws-3xxx)
83 * argv (apbus-based machine)
84 */
85 if (a3 >= 0x80000000)
86 apbus = 1;
87 else
88 apbus = 0;
89
90 if (apbus)
91 _sip = (void *)a4;
92
93 printf("%s Secondary Boot, Revision %s\n",
94 bootprog_name, bootprog_rev);
95 printf("(%s, %s)\n", bootprog_maker, bootprog_date);
96
97 if (apbus) {
98 char *bootdev = (char *)a1;
99 int argc = a2;
100 char **argv = (char **)a3;
101
102 DPRINTF("APbus-based system\n");
103
104 DPRINTF("argc = %d\n", argc);
105 for (i = 0; i < argc; i++) {
106 DPRINTF("argv[%d] = %s\n", i, argv[i]);
107 if (argv[i][0] != '-' && *netbsd == 0)
108 netbsd = argv[i];
109 }
110 maxmem = _sip->apbsi_memsize;
111 maxmem -= 0x100000; /* reserve 1MB for ROM monitor */
112
113 DPRINTF("howto = 0x%x\n", a0);
114 DPRINTF("bootdev = %s\n", (char *)a1);
115 DPRINTF("bootname = %s\n", netbsd);
116 DPRINTF("maxmem = 0x%x\n", maxmem);
117
118 /* XXX use "sonic()" instead of "tftp()" */
119 if (strncmp(bootdev, "tftp", 4) == 0)
120 bootdev = "sonic";
121
122 strcpy(devname, bootdev);
123 if (strchr(devname, '(') == NULL)
124 strcat(devname, "()");
125 } else {
126 int bootdev = a1;
127 char *bootname = (char *)a2;
128 int ctlr, unit, part, type;
129
130 DPRINTF("HB system.\n");
131
132 /* bootname is "/boot" by default on HB system. */
133 if (bootname && strcmp(bootname, "/boot") != 0)
134 netbsd = bootname;
135 maxmem = a3;
136
137 DPRINTF("howto = 0x%x\n", a0);
138 DPRINTF("bootdev = 0x%x\n", a1);
139 DPRINTF("bootname = %s\n", netbsd);
140 DPRINTF("maxmem = 0x%x\n", maxmem);
141
142 ctlr = BOOTDEV_CTLR(bootdev);
143 unit = BOOTDEV_UNIT(bootdev);
144 part = BOOTDEV_PART(bootdev);
145 type = BOOTDEV_TYPE(bootdev);
146
147 if (devs[type] == NULL) {
148 printf("unknown bootdev (0x%x)\n", bootdev);
149 _rtt();
150 }
151
152 sprintf(devname, "%s(%d,%d,%d)", devs[type], ctlr, unit, part);
153 }
154
155 printf("Booting %s%s\n", devname, netbsd);
156
157 /* use user specified kernel name if exists */
158 if (*netbsd) {
159 kernels[0] = netbsd;
160 kernels[1] = NULL;
161 }
162
163 marks[MARK_START] = 0;
164
165 for (i = 0; kernels[i]; i++) {
166 sprintf(file, "%s%s", devname, kernels[i]);
167 DPRINTF("trying %s...\n", file);
168 fd = loadfile(file, marks, LOAD_KERNEL);
169 if (fd != -1)
170 break;
171 }
172 if (kernels[i] == NULL)
173 _rtt();
174
175 DPRINTF("entry = 0x%x\n", (int)marks[MARK_ENTRY]);
176 DPRINTF("ssym = 0x%x\n", (int)marks[MARK_SYM]);
177 DPRINTF("esym = 0x%x\n", (int)marks[MARK_END]);
178
179 bi_init(BOOTINFO_ADDR);
180
181 bi_sym.nsym = marks[MARK_NSYM];
182 bi_sym.ssym = marks[MARK_SYM];
183 bi_sym.esym = marks[MARK_END];
184 bi_add(&bi_sym, BTINFO_SYMTAB, sizeof(bi_sym));
185
186 bi_arg.howto = a0;
187 bi_arg.bootdev = a1;
188 bi_arg.maxmem = maxmem;
189 bi_arg.sip = (int)_sip;
190 bi_add(&bi_arg, BTINFO_BOOTARG, sizeof(bi_arg));
191
192 strcpy(bi_bpath.bootpath, file);
193 bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
194
195 bi_sys.type = apbus ? NEWS5000 : NEWS3400; /* XXX */
196 bi_add(&bi_sys, BTINFO_SYSTYPE, sizeof(bi_sys));
197
198 entry = (void *)marks[MARK_ENTRY];
199
200 if (apbus)
201 apcall_flushcache();
202 else
203 mips1_flushicache(entry, marks[MARK_SYM] - marks[MARK_ENTRY]);
204
205 printf("\n");
206 (*entry)(a0, a1, a2, a3, a4, a5);
207 }
208
209 void
210 putchar(int x)
211 {
212 char c = x;
213
214 if (apbus)
215 apcall_write(1, &c, 1);
216 else
217 rom_write(1, &c, 1);
218 }
219
220 int
221 getchar(void)
222 {
223 unsigned char c = '\0';
224 int i;
225
226 for (;;) {
227 i = apbus ? apcall_read(1, &c, 1) : rom_read(1, &c, 1);
228 if (i == 1)
229 break;
230 if (i != -2 && i != 0)
231 return -1;
232 }
233 return c;
234 }
235
236 void
237 _rtt(void)
238 {
239 if (apbus)
240 apcall_exit(8);
241 else
242 rom_halt();
243
244 for (;;);
245 }
246