2 * Load and boot NetBSD kernel on Human68k
4 * written by Yasha (ITOH Yasufumi)
7 * loadbsd [-hvV] [-abDs] [-r root_device] netbsd
11 * -V print version and exit
14 * -a auto boot, opposite of -s
15 * -s single user boot (default)
16 * -D enter kernel debugger
18 * -r specify root device
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 */
34 #include <sys/exec_elf.h>
36 #include <machine/bootinfo.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
[]);
63 const char *kernel_fn
;
73 * parse interface name
74 * return the next position
77 lookupif(const char *name
, unsigned *pif
, unsigned *punit
)
82 for (u
= 0; u
< sizeof hatable
/ sizeof hatable
[0]; u
++) {
85 for (n
= hatable
[u
].name
, p
= name
; *n
&& *n
== *p
; n
++, p
++)
104 *pif
= hatable
[u
].id
;
111 * if the SCSI interface is not specified, use the current one
114 get_current_scsi_interface(unsigned *pif
, unsigned *punit
)
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' */
128 *pif
= X68K_BOOT_SCSIIF_SPC
;
130 } else if (DOS_BUS_ERR(&bus_err_buf
, (void *)EXSPC_BDID
, 1)) {
132 *pif
= X68K_BOOT_SCSIIF_MHA
;
136 *pif
= X68K_BOOT_SCSIIF_SPC
;
144 * [/<controller>@<unit>/]<device>@<unit>[,<lun>][:<partition>]
146 * <unit> must be target SCSI ID if <device> is a SCSI device
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
{
166 bootdev(const char *devstr
)
169 unsigned major
, unit
, lun
, partition
;
171 const char *s
= devstr
;
172 unsigned interface
= 0, unit_if
= 0;
176 * /<interface>/<device>"
179 while (*++s
== '/') /* skip slashes */
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 */
190 /* make lint happy */
195 /* allow r at the top */
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])
204 xerrx(1, "%s: unknown device", devstr
);
206 found
: major
= devtable
[u
].major
;
209 * <type>@unit[,lun][:part]
213 /* get device unit # */
218 xerrx(1, "%s: missing unit number", devstr
);
220 xerrx(1, "%s: wrong device", devstr
);
229 xerrx(1, "%s: wrong device", devstr
);
233 /* get device partition */
237 partition
= 0; /* no partition letter -- assuming 'a' */
239 partition
= *s
- 'a';
241 xerrx(1, "%s: wrong partition letter", devstr
);
247 xerrx(1, "%s: interface unit # too large", devstr
);
249 xerrx(1, "%s: device unit # too large", devstr
);
251 xerrx(1, "%s: SCSI LUN >= 8 is not supported yet", devstr
);
253 xerrx(1, "%s: unsupported partition", devstr
);
256 * encode device to be passed to kernel
258 if (X68K_BOOT_DEV_IS_SCSI(major
)) {
263 get_current_scsi_interface(&interface
, &unit_if
);
265 dev
= X68K_MAKESCSIBOOTDEV(major
, interface
, unit_if
,
266 unit
, lun
, partition
);
268 /* encode non-SCSI device */
269 dev
= X68K_MAKEBOOTDEV(major
, unit
, partition
);
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
);
280 #include "../common/exec_sub.c"
283 * read kernel and create trampoline
285 * |----------------------| <- allocated buf addr
287 * ~ (exec file contents) ~
289 * |----------------------| <- return value (entry addr of trampoline)
290 * | struct tramparg |
291 * | (trampoline args) |
292 * |----------------------|
293 * | trampoline code |
295 * |----------------------|
297 static struct tramparg
*
298 read_kernel(const char *fn
)
302 size_t filesize
, nread
;
304 struct tramparg
*arg
;
305 size_t size_tramp
= end_trampoline
- trampoline
;
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
);
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
) {
338 xerr(1, "%s: read", fn
);
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
);
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 */
366 /* filled afterwards */
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
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
);
389 * MC68000/010 -> return zero
390 * MC68020 and later -> return nonzero
395 register int ret
__asm("%d0");
397 __asm("| %0 <- this must be %%d0\n\
399 .long 0x103B02FF | foo: moveb %%pc@((foo+1)-foo-2:B,d0:W:2),%%d0\n\
401 | %%d0.b = 0x02 (68000/010)\n\
402 | = 0xff (68020 and later)\n\
404 moveq #0,%%d0 | 68000/010\n\
411 usage(int status
, const char *msg
)
413 extern const char *const __progname
;
419 %s [-hvV] [-abDs] [-r root_device] netbsd\n\
424 \t-V print version and exit\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
);
445 main(int argc
, char *argv
[])
447 char *rootdevname
= 0;
449 u_long boothowto
= RB_SINGLE
;
451 char *p
, **flg
, **arg
;
452 struct tramparg
*tramp
;
453 struct dos_dregs regs
; /* unused... */
457 for (arg
= flg
= argv
+ 1; (p
= *flg
) && *p
== '-'; ) {
463 usage(0, (char *) 0);
466 case 'N': /* don't actually execute kernel */
471 boothowto
|= AB_VERBOSE
; /* XXX */
474 xprintf("loadbsd %s\n", VERSION
);
482 usage(1, "multiple -r flags");
484 usage(1, "-r requires device name");
489 boothowto
|= RB_ASKNAME
;
492 boothowto
&= ~RB_SINGLE
;
495 boothowto
|= RB_SINGLE
;
501 boothowto
|= AB_QUIET
;
505 usage(1, (char *) 0);
514 xerrx(1, "Can't boot NetBSD on 68000/010");
520 usage(1, (char *) 0);
524 rootdev
= bootdev(rootdevname
? rootdevname
: DEFAULT_ROOTDEVNAME
);
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)
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
549 xwarnx("flush and disable MPU caches...");
551 IOCS_CACHE_MD(-1); /* flush */
553 IOCS_CACHE_MD(0); /* disable both caches */
556 xwarnx("Jumping to the kernel. Good Luck!");
559 xerrx(0, "But don't actually do it.");
561 DOS_SUPER_JSR((void (*)(void)) tramp
, ®s
, ®s
);
565 xwarnx("??? return from kernel");