boot.c revision 1.21 1 1.21 tsutsui /* $NetBSD: boot.c,v 1.21 2014/07/06 08:10:21 tsutsui Exp $ */
2 1.1 minoura
3 1.1 minoura /*
4 1.1 minoura * Copyright (c) 2001 Minoura Makoto
5 1.1 minoura * All rights reserved.
6 1.1 minoura *
7 1.1 minoura * Redistribution and use in source and binary forms, with or without
8 1.1 minoura * modification, are permitted provided that the following conditions
9 1.1 minoura * are met:
10 1.1 minoura * 1. Redistributions of source code must retain the above copyright
11 1.1 minoura * notice, this list of conditions and the following disclaimer.
12 1.1 minoura * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 minoura * notice, this list of conditions and the following disclaimer in the
14 1.1 minoura * documentation and/or other materials provided with the distribution.
15 1.1 minoura *
16 1.1 minoura * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 minoura * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 minoura * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 minoura * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 minoura * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 minoura * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 minoura * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 minoura * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 minoura * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 minoura * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 minoura * SUCH DAMAGE.
27 1.1 minoura */
28 1.1 minoura
29 1.1 minoura #include <sys/param.h>
30 1.1 minoura #include <machine/bootinfo.h>
31 1.1 minoura
32 1.1 minoura #include <lib/libsa/stand.h>
33 1.1 minoura #include <lib/libsa/loadfile.h>
34 1.6 minoura #include <lib/libsa/ufs.h>
35 1.20 tsutsui #ifdef NETBOOT
36 1.20 tsutsui #include <lib/libsa/dev_net.h>
37 1.20 tsutsui #endif
38 1.1 minoura #include <lib/libkern/libkern.h>
39 1.1 minoura
40 1.1 minoura #include "libx68k.h"
41 1.1 minoura #include "iocs.h"
42 1.1 minoura
43 1.1 minoura #include "exec_image.h"
44 1.1 minoura
45 1.1 minoura
46 1.1 minoura #define HEAP_START ((void*) 0x00080000)
47 1.1 minoura #define HEAP_END ((void*) 0x000fffff)
48 1.1 minoura #define EXSCSI_BDID ((void*) 0x00ea0001)
49 1.1 minoura #define SRAM_MEMSIZE (*((long*) 0x00ed0008))
50 1.1 minoura
51 1.19 tsutsui char default_kernel[20] =
52 1.20 tsutsui #ifndef NETBOOT
53 1.19 tsutsui "sd0a:netbsd";
54 1.20 tsutsui #else
55 1.20 tsutsui "nfs:netbsd";
56 1.20 tsutsui #endif
57 1.19 tsutsui int mpu;
58 1.20 tsutsui #ifndef NETBOOT
59 1.19 tsutsui int hostadaptor;
60 1.20 tsutsui #endif
61 1.1 minoura int console_device = -1;
62 1.1 minoura
63 1.20 tsutsui #ifdef DEBUG
64 1.20 tsutsui #ifdef NETBOOT
65 1.20 tsutsui int debug = 1;
66 1.20 tsutsui #endif
67 1.20 tsutsui #endif
68 1.20 tsutsui
69 1.1 minoura static void help(void);
70 1.20 tsutsui #ifndef NETBOOT
71 1.1 minoura static int get_scsi_host_adapter(void);
72 1.20 tsutsui #endif
73 1.1 minoura static void doboot(const char *, int);
74 1.1 minoura static void boot(char *);
75 1.20 tsutsui #ifndef NETBOOT
76 1.17 tsutsui static void cmd_ls(char *);
77 1.20 tsutsui #endif
78 1.1 minoura int bootmenu(void);
79 1.1 minoura void bootmain(int);
80 1.1 minoura extern int detectmpu(void);
81 1.11 christos extern int badbaddr(void *);
82 1.1 minoura
83 1.20 tsutsui #ifndef NETBOOT
84 1.1 minoura /* from boot_ufs/bootmain.c */
85 1.1 minoura static int
86 1.1 minoura get_scsi_host_adapter(void)
87 1.1 minoura {
88 1.1 minoura char *bootrom;
89 1.1 minoura int ha;
90 1.1 minoura
91 1.1 minoura bootrom = (char *) (IOCS_BOOTINF() & 0x00ffffe0);
92 1.1 minoura /*
93 1.1 minoura * bootrom+0x24 "SCSIIN" ... Internal SCSI (spc@0)
94 1.1 minoura * "SCSIEX" ... External SCSI (spc@1 or mha@0)
95 1.1 minoura */
96 1.1 minoura if (*(u_short *)(bootrom + 0x24 + 4) == 0x494e) { /* "IN" */
97 1.1 minoura ha = (X68K_BOOT_SCSIIF_SPC << 4) | 0;
98 1.1 minoura } else if (badbaddr(EXSCSI_BDID)) {
99 1.1 minoura ha = (X68K_BOOT_SCSIIF_MHA << 4) | 0;
100 1.1 minoura } else {
101 1.1 minoura ha = (X68K_BOOT_SCSIIF_SPC << 4) | 1;
102 1.1 minoura }
103 1.1 minoura
104 1.1 minoura return ha;
105 1.1 minoura }
106 1.20 tsutsui #endif
107 1.1 minoura
108 1.1 minoura static void
109 1.1 minoura help(void)
110 1.1 minoura {
111 1.3 minoura printf("Usage:\n");
112 1.3 minoura printf("boot [dev:][file] -[flags]\n");
113 1.20 tsutsui #ifndef NETBOOT
114 1.3 minoura printf(" dev: sd<ID><PART>, ID=0-7, PART=a-p\n");
115 1.3 minoura printf(" cd<ID>a, ID=0-7\n");
116 1.3 minoura printf(" fd<UNIT>a, UNIT=0-3, format is detected.\n");
117 1.20 tsutsui #else
118 1.20 tsutsui printf(" dev: nfs, first probed NE2000 is used.\n");
119 1.20 tsutsui #endif
120 1.3 minoura printf(" file: netbsd, netbsd.gz, etc.\n");
121 1.3 minoura printf(" flags: abdqsv\n");
122 1.20 tsutsui #ifndef NETBOOT
123 1.4 minoura printf("ls [dev:][directory]\n");
124 1.20 tsutsui #endif
125 1.4 minoura printf("halt\nreboot\n");
126 1.1 minoura }
127 1.1 minoura
128 1.1 minoura static void
129 1.4 minoura doboot(const char *file, int flags)
130 1.1 minoura {
131 1.1 minoura u_long marks[MARK_MAX];
132 1.1 minoura int fd;
133 1.1 minoura int dev, unit, part;
134 1.1 minoura char *name;
135 1.12 isaki short *p;
136 1.14 tsutsui int loadflag;
137 1.1 minoura
138 1.3 minoura printf("Starting %s, flags 0x%x\n", file, flags);
139 1.14 tsutsui
140 1.14 tsutsui loadflag = LOAD_KERNEL;
141 1.14 tsutsui if (file[0] == 'f')
142 1.15 christos loadflag &= ~LOAD_BACKWARDS;
143 1.14 tsutsui
144 1.1 minoura marks[MARK_START] = 0x100000;
145 1.14 tsutsui if ((fd = loadfile(file, marks, loadflag)) == -1) {
146 1.9 nsmrtks printf("loadfile failed\n");
147 1.1 minoura return;
148 1.9 nsmrtks }
149 1.1 minoura close(fd);
150 1.1 minoura
151 1.1 minoura if (devparse(file, &dev, &unit, &part, &name) != 0) {
152 1.3 minoura printf("XXX: unknown corruption in /boot.\n");
153 1.1 minoura }
154 1.1 minoura
155 1.18 minoura #ifdef DEBUG
156 1.20 tsutsui #ifndef NETBOOT
157 1.3 minoura printf("dev = %x, unit = %d, part = %c, name = %s\n",
158 1.4 minoura dev, unit, part + 'a', name);
159 1.20 tsutsui #else
160 1.20 tsutsui printf("dev = %x, unit = %d, name = %s\n",
161 1.20 tsutsui dev, unit, name);
162 1.20 tsutsui #endif
163 1.18 minoura #endif
164 1.1 minoura
165 1.20 tsutsui #ifndef NETBOOT
166 1.1 minoura if (dev == 0) { /* SCSI */
167 1.1 minoura dev = X68K_MAKESCSIBOOTDEV(X68K_MAJOR_SD,
168 1.1 minoura hostadaptor >> 4,
169 1.1 minoura hostadaptor & 15,
170 1.1 minoura unit & 7, 0, 0);
171 1.1 minoura } else {
172 1.1 minoura dev = X68K_MAKEBOOTDEV(X68K_MAJOR_FD, unit & 3, 0);
173 1.1 minoura }
174 1.20 tsutsui #else
175 1.20 tsutsui dev = X68K_MAKEBOOTDEV(X68K_MAJOR_NE, unit, 0);
176 1.20 tsutsui #endif
177 1.18 minoura #ifdef DEBUG
178 1.3 minoura printf("boot device = %x\n", dev);
179 1.20 tsutsui #ifndef NETBOOT
180 1.3 minoura printf("if = %d, unit = %d, id = %d, lun = %d, part = %c\n",
181 1.3 minoura B_X68K_SCSI_IF(dev),
182 1.3 minoura B_X68K_SCSI_IF_UN(dev),
183 1.3 minoura B_X68K_SCSI_ID(dev),
184 1.3 minoura B_X68K_SCSI_LUN(dev),
185 1.3 minoura B_X68K_SCSI_PART(dev) + 'a');
186 1.20 tsutsui #else
187 1.20 tsutsui printf("if = %d, unit = %d\n",
188 1.20 tsutsui B_X68K_SCSI_IF(dev),
189 1.20 tsutsui B_X68K_SCSI_IF_UN(dev));
190 1.20 tsutsui #endif
191 1.18 minoura #endif
192 1.1 minoura
193 1.12 isaki p = ((short*) marks[MARK_ENTRY]) - 1;
194 1.18 minoura #ifdef DEBUG
195 1.12 isaki printf("Kernel Version: 0x%x\n", *p);
196 1.18 minoura #endif
197 1.12 isaki if (*p != 0x4e73 && *p != 0) {
198 1.12 isaki /*
199 1.12 isaki * XXX temporary solution; compatibility loader
200 1.12 isaki * must be written.
201 1.12 isaki */
202 1.12 isaki printf("This kernel is too new to be loaded by "
203 1.12 isaki "this version of /boot.\n");
204 1.12 isaki return;
205 1.1 minoura }
206 1.1 minoura
207 1.1 minoura exec_image(marks[MARK_START], 0, marks[MARK_ENTRY]-marks[MARK_START],
208 1.1 minoura marks[MARK_END]-marks[MARK_START], dev, flags);
209 1.1 minoura
210 1.1 minoura return;
211 1.1 minoura }
212 1.1 minoura
213 1.1 minoura static void
214 1.1 minoura boot(char *arg)
215 1.1 minoura {
216 1.1 minoura char filename[80];
217 1.1 minoura char *p;
218 1.1 minoura int flags = 0;
219 1.1 minoura
220 1.1 minoura if (*arg == 0 || *arg == '-') {
221 1.3 minoura strcpy(filename, default_kernel);
222 1.1 minoura if (*arg == '-')
223 1.1 minoura if (parseopts(arg, &flags) == 0) {
224 1.3 minoura help();
225 1.1 minoura return;
226 1.1 minoura }
227 1.3 minoura doboot(filename, flags);
228 1.1 minoura return;
229 1.1 minoura } else {
230 1.3 minoura p = gettrailer(arg);
231 1.3 minoura if (strchr(arg, ':')) {
232 1.3 minoura strcpy(filename, arg);
233 1.1 minoura if (arg[strlen(arg) - 1] == ':')
234 1.3 minoura strcat(filename, "netbsd");
235 1.1 minoura } else {
236 1.3 minoura strcpy(filename, default_kernel);
237 1.3 minoura strcpy(strchr(filename, ':') + 1, arg);
238 1.1 minoura }
239 1.1 minoura if (*p == '-') {
240 1.1 minoura if (parseopts(p, &flags) == 0)
241 1.1 minoura return;
242 1.1 minoura } else if (*p != 0) {
243 1.3 minoura help();
244 1.1 minoura return;
245 1.1 minoura }
246 1.1 minoura
247 1.3 minoura doboot(filename, flags);
248 1.1 minoura return;
249 1.1 minoura }
250 1.1 minoura }
251 1.4 minoura
252 1.20 tsutsui #ifndef NETBOOT
253 1.4 minoura static void
254 1.17 tsutsui cmd_ls(char *arg)
255 1.4 minoura {
256 1.4 minoura char filename[80];
257 1.4 minoura
258 1.4 minoura devopen_open_dir = 1;
259 1.4 minoura if (*arg == 0) {
260 1.4 minoura strcpy(filename, default_kernel);
261 1.4 minoura strcpy(strchr(filename, ':')+1, "/");
262 1.4 minoura } else if (strchr(arg, ':') == 0) {
263 1.4 minoura strcpy(filename, default_kernel);
264 1.4 minoura strcpy(strchr(filename, ':')+1, arg);
265 1.4 minoura } else {
266 1.4 minoura strcpy(filename, arg);
267 1.4 minoura if (*(strchr(arg, ':')+1) == 0)
268 1.4 minoura strcat(filename, "/");
269 1.4 minoura }
270 1.17 tsutsui ls(filename);
271 1.4 minoura devopen_open_dir = 0;
272 1.4 minoura }
273 1.20 tsutsui #endif
274 1.4 minoura
275 1.1 minoura int
276 1.1 minoura bootmenu(void)
277 1.1 minoura {
278 1.1 minoura char input[80];
279 1.1 minoura int n = 5, c;
280 1.1 minoura
281 1.5 minoura printf("Press return to boot now, any other key for boot menu\n");
282 1.1 minoura printf("booting %s - starting in %d seconds. ",
283 1.1 minoura default_kernel, n);
284 1.1 minoura while (n-- > 0 && (c = awaitkey_1sec()) == 0) {
285 1.1 minoura printf("\r");
286 1.1 minoura printf("booting %s - starting in %d seconds. ",
287 1.1 minoura default_kernel, n);
288 1.1 minoura }
289 1.1 minoura printf("\r");
290 1.1 minoura printf("booting %s - starting in %d seconds. ", default_kernel, 0);
291 1.1 minoura printf("\n");
292 1.1 minoura
293 1.1 minoura if (c == 0 || c == '\r') {
294 1.1 minoura doboot(default_kernel, 0);
295 1.1 minoura printf("Could not start %s; ", default_kernel);
296 1.1 minoura strcat(default_kernel, ".gz");
297 1.1 minoura printf("trying %s.\n", default_kernel);
298 1.1 minoura doboot(default_kernel, 0);
299 1.1 minoura printf("Could not start %s; ", default_kernel);
300 1.1 minoura }
301 1.1 minoura
302 1.4 minoura printf("Please use the absolute unit# (e.g. SCSI ID)"
303 1.8 minoura " instead of the NetBSD logical #.\n");
304 1.1 minoura for (;;) {
305 1.1 minoura char *p, *options;
306 1.1 minoura
307 1.1 minoura printf("> ");
308 1.1 minoura gets(input);
309 1.1 minoura
310 1.19 tsutsui for (p = &input[0]; p - &input[0] < 80 && *p == ' '; p++)
311 1.19 tsutsui ;
312 1.1 minoura options = gettrailer(p);
313 1.1 minoura if (strcmp("boot", p) == 0)
314 1.1 minoura boot(options);
315 1.1 minoura else if (strcmp("help", p) == 0 ||
316 1.1 minoura strcmp("?", p) == 0)
317 1.1 minoura help();
318 1.19 tsutsui else if (strcmp("halt", p) == 0 ||
319 1.19 tsutsui strcmp("reboot", p) == 0)
320 1.1 minoura exit(0);
321 1.20 tsutsui #ifndef NETBOOT
322 1.4 minoura else if (strcmp("ls", p) == 0)
323 1.17 tsutsui cmd_ls(options);
324 1.20 tsutsui #endif
325 1.1 minoura else
326 1.1 minoura printf("Unknown command %s\n", p);
327 1.1 minoura }
328 1.1 minoura }
329 1.1 minoura
330 1.21 tsutsui static u_int
331 1.21 tsutsui checkmemsize(void)
332 1.21 tsutsui {
333 1.21 tsutsui u_int m;
334 1.21 tsutsui
335 1.21 tsutsui #define MIN_MB 4
336 1.21 tsutsui #define MAX_MB 12
337 1.21 tsutsui
338 1.21 tsutsui for (m = MIN_MB; m <= MAX_MB; m++) {
339 1.21 tsutsui if (badbaddr((void *)(m * 1024 * 1024 - 1))) {
340 1.21 tsutsui /* no memory */
341 1.21 tsutsui break;
342 1.21 tsutsui }
343 1.21 tsutsui }
344 1.21 tsutsui
345 1.21 tsutsui return (m - 1) * 1024 * 1024;
346 1.21 tsutsui }
347 1.1 minoura
348 1.7 minoura extern const char bootprog_rev[];
349 1.7 minoura extern const char bootprog_name[];
350 1.7 minoura
351 1.1 minoura /*
352 1.1 minoura * Arguments from the boot block:
353 1.1 minoura * bootdev - specifies the device from which /boot was read, in
354 1.1 minoura * bootdev format.
355 1.1 minoura */
356 1.1 minoura void
357 1.1 minoura bootmain(int bootdev)
358 1.1 minoura {
359 1.21 tsutsui u_int sram_memsize;
360 1.21 tsutsui u_int probed_memsize;
361 1.19 tsutsui
362 1.20 tsutsui #ifndef NETBOOT
363 1.1 minoura hostadaptor = get_scsi_host_adapter();
364 1.20 tsutsui #else
365 1.20 tsutsui rtc_offset = RTC_OFFSET;
366 1.20 tsutsui try_bootp = 1;
367 1.20 tsutsui #endif
368 1.1 minoura mpu = detectmpu();
369 1.1 minoura
370 1.1 minoura if (mpu < 3) { /* not tested on 68020 */
371 1.1 minoura printf("This MPU cannot run NetBSD.\n");
372 1.1 minoura exit(1);
373 1.1 minoura }
374 1.21 tsutsui sram_memsize = SRAM_MEMSIZE;
375 1.21 tsutsui if (sram_memsize < 4*1024*1024) {
376 1.1 minoura printf("Main memory too small.\n");
377 1.1 minoura exit(1);
378 1.1 minoura }
379 1.1 minoura
380 1.1 minoura console_device = consio_init(console_device);
381 1.1 minoura setheap(HEAP_START, HEAP_END);
382 1.1 minoura
383 1.20 tsutsui #ifndef NETBOOT
384 1.4 minoura switch (B_TYPE(bootdev)) {
385 1.4 minoura case X68K_MAJOR_FD:
386 1.1 minoura default_kernel[0] = 'f';
387 1.1 minoura default_kernel[2] = '0' + B_UNIT(bootdev);
388 1.1 minoura default_kernel[3] = 'a';
389 1.4 minoura break;
390 1.4 minoura case X68K_MAJOR_SD:
391 1.4 minoura default_kernel[2] = '0' + B_X68K_SCSI_ID(bootdev);
392 1.4 minoura default_kernel[3] =
393 1.4 minoura 'a' + sd_getbsdpartition(B_X68K_SCSI_ID(bootdev),
394 1.4 minoura B_X68K_SCSI_PART(bootdev));
395 1.4 minoura break;
396 1.4 minoura case X68K_MAJOR_CD:
397 1.4 minoura default_kernel[0] = 'c';
398 1.1 minoura default_kernel[2] = '0' + B_X68K_SCSI_ID(bootdev);
399 1.1 minoura default_kernel[3] = 'a';
400 1.4 minoura break;
401 1.4 minoura default:
402 1.4 minoura printf("Warning: unknown boot device: %x\n", bootdev);
403 1.1 minoura }
404 1.20 tsutsui #endif
405 1.16 joerg print_title("%s, Revision %s\n", bootprog_name, bootprog_rev);
406 1.21 tsutsui
407 1.21 tsutsui /* check actual memory size for machines with a dead SRAM battery */
408 1.21 tsutsui probed_memsize = checkmemsize();
409 1.21 tsutsui if (sram_memsize != probed_memsize) {
410 1.21 tsutsui printf("\x1b[1mWarning: SRAM Memory Size (%d MB) "
411 1.21 tsutsui "is different from probed Memory Size (%d MB)\n"
412 1.21 tsutsui " Check and reset SRAM values.\x1b[m\n\n",
413 1.21 tsutsui sram_memsize / (1024 * 1024),
414 1.21 tsutsui probed_memsize / (1024 * 1024));
415 1.21 tsutsui }
416 1.21 tsutsui
417 1.1 minoura bootmenu();
418 1.1 minoura }
419