Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / x68k / stand / loadbsd / loadbsd.c
blob180b0f43b9ce5131c63281fb424e3087767486e6
1 /*
2 * Load and boot NetBSD kernel on Human68k
4 * written by Yasha (ITOH Yasufumi)
5 * public domain
7 * loadbsd [-hvV] [-abDs] [-r root_device] netbsd
9 * loadbsd options:
10 * -h help
11 * -V print version and exit
13 * kernel options:
14 * -a auto boot, opposite of -s
15 * -s single user boot (default)
16 * -D enter kernel debugger
17 * -b ask root device
18 * -r specify root device
19 * -q quiet boot
20 * -v verbose boot (also turn on verbosity of loadbsd)
22 * $NetBSD: loadbsd.c,v 1.12 2009/03/14 21:04:17 dsl Exp $
25 #include <sys/cdefs.h>
27 __RCSID("$NetBSD: loadbsd.c,v 1.12 2009/03/14 21:04:17 dsl Exp $");
28 #define VERSION "$Revision: 1.13 $ $Date: 2009/03/18 10:22:38 $"
30 #include <sys/types.h> /* ntohl */
31 #include <sys/reboot.h>
32 #include <sys/param.h> /* ALIGN, ALIGNBYTES */
33 #include <a.out.h>
34 #include <sys/exec_elf.h>
35 #include <string.h>
36 #include <machine/bootinfo.h>
38 #include <dos.h>
39 #include <iocs.h>
40 #include "../common/xprintf.h"
41 #include "trampoline.h"
43 #define DEFAULT_ROOTDEVNAME "sd@0,0:a"
45 #define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
47 #define GETDECIMAL(var, str) \
48 do { var *= 10; var += *str++ - '0'; } while (ISDIGIT(*str))
50 static const char *lookupif(const char *name,
51 unsigned *pif, unsigned *punit);
52 static void get_current_scsi_interface(unsigned *pif, unsigned *punit);
53 static int bootdev(const char *devstr);
54 static struct tramparg *read_kernel(const char *fn);
55 static int chkmpu(void);
56 static __dead void usage(int status, const char *msg)
57 __attribute__((noreturn));
59 int main(int argc, char *argv[]);
61 int opt_v;
62 int opt_N;
63 const char *kernel_fn;
65 const struct hatbl {
66 char name[4];
67 unsigned short id;
68 } hatable[] = {
69 X68K_BOOT_SCSIIF_LIST
73 * parse interface name
74 * return the next position
76 static const char *
77 lookupif(const char *name, unsigned *pif, unsigned *punit)
79 unsigned u, unit;
80 const char *p;
82 for (u = 0; u < sizeof hatable / sizeof hatable[0]; u++) {
83 const char *n;
85 for (n = hatable[u].name, p = name; *n && *n == *p; n++, p++)
87 if (!*n)
88 goto found;
90 /* not found */
91 return (char *) 0;
93 found:
94 if (*p == '@')
95 p++;
97 /* get unit # */
98 if (!ISDIGIT(*p))
99 return (char *) 0;
101 unit = 0;
102 GETDECIMAL(unit, p);
104 *pif = hatable[u].id;
105 *punit = unit;
107 return p;
111 * if the SCSI interface is not specified, use the current one
113 static void
114 get_current_scsi_interface(unsigned *pif, unsigned *punit)
116 unsigned binf;
117 char *bootrom;
118 int bus_err_buf;
120 binf = (unsigned) IOCS_BOOTINF();
121 if (binf < 0x00fc0000)
122 return; /* not booted from SCSI */
124 bootrom = (char *) (binf & 0x00ffffe0);
125 if (IOCS_B_LPEEK(bootrom + 0x24) == 0x53435349 && /* 'SCSI' */
126 IOCS_B_WPEEK(bootrom + 0x28) == 0x494E) { /* 'IN' */
127 /* spc0 */
128 *pif = X68K_BOOT_SCSIIF_SPC;
129 *punit = 0;
130 } else if (DOS_BUS_ERR(&bus_err_buf, (void *)EXSPC_BDID, 1)) {
131 /* mha0 */
132 *pif = X68K_BOOT_SCSIIF_MHA;
133 *punit = 0;
134 } else {
135 /* spc1 */
136 *pif = X68K_BOOT_SCSIIF_SPC;
137 *punit = 1;
142 * parse device name
144 * [/<controller>@<unit>/]<device>@<unit>[,<lun>][:<partition>]
146 * <unit> must be target SCSI ID if <device> is a SCSI device
148 * full form:
149 * /spc@0/sd@1,2:e
151 * partial form:
152 * /mha@0/sd@1 = /mha@0/sd@1,0:a
153 * sd@1:e = /current_device/sd@1,0e
154 * sd@1,2:e = /current_device/sd@1,2:e
157 const struct devtbl {
158 char name[3];
159 u_char major;
160 } devtable[] = {
161 X68K_BOOT_DEV_LIST,
162 X68K_BOOT_NETIF_LIST
165 static int
166 bootdev(const char *devstr)
168 unsigned u;
169 unsigned major, unit, lun, partition;
170 int dev;
171 const char *s = devstr;
172 unsigned interface = 0, unit_if = 0;
174 if (*s == '/') {
176 * /<interface>/<device>"
177 * "/spc@1/sd@2,3:e"
179 while (*++s == '/') /* skip slashes */
181 if (!strchr(s, '/'))
182 xerrx(1, "%s: bad format", devstr);
184 if (!(s = lookupif(s, &interface, &unit_if)))
185 xerrx(1, "%s: unknown interface", devstr);
187 while (*s == '/') /* skip slashes */
188 s++;
189 } else {
190 /* make lint happy */
191 interface = 0;
192 unit_if = 0;
195 /* allow r at the top */
196 if (*s == 'r')
197 s++;
199 for (u = 0; u < sizeof devtable / sizeof devtable[0]; u++)
200 if (s[0] == devtable[u].name[0] && s[1] == devtable[u].name[1])
201 goto found;
203 /* not found */
204 xerrx(1, "%s: unknown device", devstr);
206 found: major = devtable[u].major;
209 * <type>@unit[,lun][:part]
210 * "sd@1,3:a"
213 /* get device unit # */
214 s += 2;
215 if (*s == '@')
216 s++;
217 if (!*s)
218 xerrx(1, "%s: missing unit number", devstr);
219 if (!ISDIGIT(*s))
220 xerrx(1, "%s: wrong device", devstr);
222 unit = 0;
223 GETDECIMAL(unit, s);
225 lun = 0;
226 if (*s == ',') {
227 s++;
228 if (!ISDIGIT(*s))
229 xerrx(1, "%s: wrong device", devstr);
230 GETDECIMAL(lun, s);
233 /* get device partition */
234 if (*s == ':')
235 s++;
236 if (!*s)
237 partition = 0; /* no partition letter -- assuming 'a' */
238 else if (!s[1])
239 partition = *s - 'a';
240 else
241 xerrx(1, "%s: wrong partition letter", devstr);
244 * sanity check
246 if (unit_if >= 16)
247 xerrx(1, "%s: interface unit # too large", devstr);
248 if (unit >= 16)
249 xerrx(1, "%s: device unit # too large", devstr);
250 if (lun >= 8)
251 xerrx(1, "%s: SCSI LUN >= 8 is not supported yet", devstr);
252 if (partition >= 16)
253 xerrx(1, "%s: unsupported partition", devstr);
256 * encode device to be passed to kernel
258 if (X68K_BOOT_DEV_IS_SCSI(major)) {
260 * encode SCSI device
262 if (interface == 0)
263 get_current_scsi_interface(&interface, &unit_if);
265 dev = X68K_MAKESCSIBOOTDEV(major, interface, unit_if,
266 unit, lun, partition);
267 } else {
268 /* encode non-SCSI device */
269 dev = X68K_MAKEBOOTDEV(major, unit, partition);
272 if (opt_v)
273 xwarnx("%s: major %u, if %u, un_if %u, unit %u, lun %u, partition %u; bootdev 0x%x",
274 devstr, major, interface, unit_if, unit, lun, partition, dev);
276 return dev;
279 #define LOADBSD
280 #include "../common/exec_sub.c"
283 * read kernel and create trampoline
285 * |----------------------| <- allocated buf addr
286 * | kernel image |
287 * ~ (exec file contents) ~
288 * | |
289 * |----------------------| <- return value (entry addr of trampoline)
290 * | struct tramparg |
291 * | (trampoline args) |
292 * |----------------------|
293 * | trampoline code |
294 * | (in assembly) |
295 * |----------------------|
297 static struct tramparg *
298 read_kernel(const char *fn)
300 int fd;
301 union dos_fcb *fcb;
302 size_t filesize, nread;
303 void *buf;
304 struct tramparg *arg;
305 size_t size_tramp = end_trampoline - trampoline;
307 kernel_fn = fn;
309 if ((fd = DOS_OPEN(fn, 0x00)) < 0) /* read only */
310 xerr(1, "%s: open", fn);
312 if ((int)(fcb = DOS_GET_FCB_ADR(fd)) < 0)
313 xerr(1, "%s: get_fcb_adr", fn);
316 * XXX FCB is in supervisor area
318 /*if (fcb->blk.mode != 0)*/
319 if (IOCS_B_BPEEK((char *)fcb + 1) & 0x80)
320 xerrx(1, "%s: Not a regular file", fn);
322 /*filesize = fcb->blk.size;*/
323 filesize = IOCS_B_LPEEK(&fcb->blk.size);
325 if (filesize < sizeof(Elf32_Ehdr))
326 xerrx(1, "%s: Unknown format", fn);
329 * read entire file
331 if ((int)(buf = DOS_MALLOC(filesize + ALIGNBYTES
332 + sizeof(struct tramparg)
333 + size_tramp + SIZE_TMPSTACK)) < 0)
334 xerr(1, "read_kernel");
336 if ((nread = DOS_READ(fd, buf, filesize)) != filesize) {
337 if ((int)nread < 0)
338 xerr(1, "%s: read", fn);
339 else
340 xerrx(1, "%s: short read", fn);
343 if (DOS_CLOSE(fd) < 0)
344 xerr(1, "%s: close", fn);
347 * address for argument for trampoline code
349 arg = (struct tramparg *) ALIGN((char *) buf + nread);
351 if (opt_v)
352 xwarnx("trampoline arg at %p", arg);
354 xk_load(&arg->xk, buf, 0 /* XXX load addr should not be fixed */);
357 * create argument for trampoline code
359 arg->bsr_inst = TRAMP_BSR + sizeof(struct tramparg) - 2;
360 arg->tmp_stack = (char *) arg + sizeof(struct tramparg)
361 + size_tramp + SIZE_TMPSTACK;
362 arg->mpu_type = IOCS_MPU_STAT() & 0xff;
364 arg->xk.d5 = IOCS_BOOTINF(); /* unused for now */
365 #if 0
366 /* filled afterwards */
367 arg->xk.rootdev =
368 arg->xk.boothowto =
369 #endif
371 if (opt_v)
372 xwarnx("args: mpu %d, image %p, load 0x%x, entry 0x%x",
373 arg->mpu_type, arg->xk.sec[0].sec_image,
374 arg->xk.load_addr, arg->xk.entry_addr);
377 * copy trampoline code
379 if (opt_v)
380 xwarnx("trampoline code at %p (%u bytes)",
381 (char *) arg + sizeof(struct tramparg), size_tramp);
383 memcpy((char *) arg + sizeof(struct tramparg), trampoline, size_tramp);
385 return arg;
389 * MC68000/010 -> return zero
390 * MC68020 and later -> return nonzero
392 static int
393 chkmpu(void)
395 register int ret __asm("%d0");
397 __asm("| %0 <- this must be %%d0\n\
398 moveq #1,%%d0\n\
399 .long 0x103B02FF | foo: moveb %%pc@((foo+1)-foo-2:B,d0:W:2),%%d0\n\
400 | ^ ^\n\
401 | %%d0.b = 0x02 (68000/010)\n\
402 | = 0xff (68020 and later)\n\
403 bmis 1f\n\
404 moveq #0,%%d0 | 68000/010\n\
405 1:" : "=d" (ret));
407 return ret;
410 static __dead void
411 usage(int status, const char *msg)
413 extern const char *const __progname;
415 if (msg)
416 xwarnx("%s", msg);
418 xerrprintf("\
419 %s [-hvV] [-abDs] [-r root_device] netbsd\n\
421 loadbsd options:\n\
422 \t-h help\n\
423 \t-v verbose\n\
424 \t-V print version and exit\n\
426 kernel options:\n\
427 \t-a auto boot, opposite of -s\n\
428 \t-s single user boot (default)\n\
429 \t-D enter kernel debugger\n\
430 \t-b ask root device\n\
431 \t-r specify root device (default %s)\n\
432 \t format: [/interface/]device@unit[,lun][:partition]\n\
433 \t interface: one of spc@0, spc@1, mha@0\n\
434 \t (current boot interface if omitted)\n\
435 \t device: one of fd, sd, cd, md, ne\n\
436 \t unit: device unit number (SCSI ID for SCSI device)\n\
437 \t lun: SCSI LUN # (0 if omitted)\n\
438 \t partition: partition letter ('a' if omitted)\n\
439 ", __progname, DEFAULT_ROOTDEVNAME);
441 DOS_EXIT2(status);
445 main(int argc, char *argv[])
447 char *rootdevname = 0;
448 int rootdev;
449 u_long boothowto = RB_SINGLE;
450 const char *kernel;
451 char *p, **flg, **arg;
452 struct tramparg *tramp;
453 struct dos_dregs regs; /* unused... */
454 int i;
456 /* parse options */
457 for (arg = flg = argv + 1; (p = *flg) && *p == '-'; ) {
458 int c;
460 while ((c = *++p))
461 switch (c) {
462 case 'h':
463 usage(0, (char *) 0);
464 /* NOTREACHED */
465 break;
466 case 'N': /* don't actually execute kernel */
467 opt_N = 1;
468 break;
469 case 'v':
470 opt_v = 1;
471 boothowto |= AB_VERBOSE; /* XXX */
472 break;
473 case 'V':
474 xprintf("loadbsd %s\n", VERSION);
475 return 0;
478 * kernel boot flags
480 case 'r':
481 if (rootdevname)
482 usage(1, "multiple -r flags");
483 else if (!*++arg)
484 usage(1, "-r requires device name");
485 else
486 rootdevname = *arg;
487 break;
488 case 'b':
489 boothowto |= RB_ASKNAME;
490 break;
491 case 'a':
492 boothowto &= ~RB_SINGLE;
493 break;
494 case 's':
495 boothowto |= RB_SINGLE;
496 break;
497 case 'D':
498 boothowto |= RB_KDB;
499 break;
500 case 'q':
501 boothowto |= AB_QUIET;
502 break;
504 default:
505 usage(1, (char *) 0);
506 /* NOTREACHED */
507 break;
509 flg = ++arg;
512 /* check MPU */
513 if (chkmpu() == 0)
514 xerrx(1, "Can't boot NetBSD on 68000/010");
516 argc -= arg - argv;
517 argv = arg;
519 if (argc != 1)
520 usage(1, (char *) 0);
522 kernel = *argv;
524 rootdev = bootdev(rootdevname ? rootdevname : DEFAULT_ROOTDEVNAME);
526 if (opt_v)
527 xwarnx("boothowto 0x%x", boothowto);
529 tramp = read_kernel(kernel);
531 tramp->xk.rootdev = rootdev;
532 tramp->xk.boothowto = boothowto;
535 * we never return, and make sure the disk cache
536 * be flushed (if write-back cache is enabled)
538 if (opt_v)
539 xwarnx("flush disk cache...");
541 i = DOS_FFLUSH_SET(1); /* enable fflush */
542 DOS_FFLUSH(); /* then, issue fflush */
543 (void) DOS_FFLUSH_SET(i); /* restore old mode just in case */
546 * the program assumes the MPU caches off
548 if (opt_v)
549 xwarnx("flush and disable MPU caches...");
551 IOCS_CACHE_MD(-1); /* flush */
552 if (!opt_N)
553 IOCS_CACHE_MD(0); /* disable both caches */
555 if (opt_v)
556 xwarnx("Jumping to the kernel. Good Luck!");
558 if (opt_N)
559 xerrx(0, "But don't actually do it.");
561 DOS_SUPER_JSR((void (*)(void)) tramp, &regs, &regs);
563 /* NOTREACHED */
565 xwarnx("??? return from kernel");
567 return 1;