Home | History | Annotate | Line # | Download | only in loadbsd
loadbsd.c revision 1.2
      1 /*
      2  *	Load and boot NetBSD kernel on Human68k
      3  *
      4  *	written by Yasha (ITOH Yasufumi)
      5  *	public domain
      6  *
      7  *	loadbsd [-hvV] [-abDs] [-r root_device] netbsd
      8  *
      9  *	loadbsd options:
     10  *		-h	help
     11  *		-v	verbose
     12  *		-V	print version and exit
     13  *
     14  *	kernel options:
     15  *		-a	auto boot, opposite of -s
     16  *		-s	single user boot (default)
     17  *		-D	enter kernel debugger
     18  *		-b	ask root device
     19  *		-d	use compiled-in rootdev
     20  *		-r	specify root device
     21  *
     22  *	$NetBSD: loadbsd.c,v 1.2 1999/04/30 15:31:18 minoura Exp $
     23  */
     24 
     25 #include <sys/cdefs.h>
     26 
     27 __RCSID("$NetBSD: loadbsd.c,v 1.2 1999/04/30 15:31:18 minoura Exp $");
     28 #define VERSION	"$Revision: 1.2 $ $Date: 1999/04/30 15:31:18 $"
     29 
     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 <string.h>
     35 #include <machine/bootinfo.h>
     36 
     37 #include <dos.h>
     38 #include <iocs.h>
     39 #include "../common/xprintf.h"
     40 #include "trampoline.h"
     41 
     42 #define DEFAULT_ROOTDEVNAME	"sd@0,0:a"
     43 
     44 #define ISDIGIT(c)	((c) >= '0' && (c) <= '9')
     45 
     46 #define GETDECIMAL(var, str)	\
     47 	do {	var *= 10; var += *str++ - '0'; } while (ISDIGIT(*str))
     48 
     49 static const char *lookupif __P((const char *name,
     50 				 unsigned *pif, unsigned *punit));
     51 static void get_current_scsi_interface __P((unsigned *pif, unsigned *punit));
     52 static int bootdev __P((const char *devstr));
     53 static struct tramparg *read_kernel __P((const char *fn));
     54 static int chkmpu __P((void));
     55 static __dead void usage __P((int status, const char *msg))
     56 					__attribute__((noreturn));
     57 
     58 int main __P((int argc, char *argv[]));
     59 
     60 int opt_v;
     61 int opt_N;
     62 
     63 const struct hatbl {
     64 	char name[4];
     65 	unsigned short id;
     66 } hatable[] = {
     67 	X68K_BOOT_SCSIIF_LIST
     68 };
     69 
     70 /*
     71  * parse interface name
     72  * returns the next position
     73  */
     74 static const char *
     75 lookupif(name, pif, punit)
     76 	const char *name;
     77 	unsigned *pif, *punit;
     78 {
     79 	unsigned u, unit;
     80 	const char *p;
     81 
     82 	for (u = 0; u < sizeof hatable / sizeof hatable[0]; u++) {
     83 		const char *n;
     84 
     85 		for (n = hatable[u].name, p = name; *n && *n == *p; n++, p++)
     86 			;
     87 		if (!*n)
     88 			goto found;
     89 	}
     90 	/* not found */
     91 	return (char *) 0;
     92 
     93 found:
     94 	if (*p == '@')
     95 		p++;
     96 
     97 	/* get unit # */
     98 	if (!ISDIGIT(*p))
     99 		return (char *) 0;
    100 
    101 	unit = 0;
    102 	GETDECIMAL(unit, p);
    103 
    104 	*pif = hatable[u].id;
    105 	*punit = unit;
    106 
    107 	return p;
    108 }
    109 
    110 /*
    111  * if the SCSI interface is not specified, use the current one
    112  */
    113 static void
    114 get_current_scsi_interface(pif, punit)
    115 	unsigned *pif, *punit;
    116 {
    117 	unsigned binf;
    118 	char *bootrom;
    119 	int bus_err_buf;
    120 
    121 	binf = (unsigned) IOCS_BOOTINF();
    122 	if (binf < 0x00fc0000)
    123 		return;			/* not booted from SCSI */
    124 
    125 	bootrom = (char *) (binf & 0x00ffffe0);
    126 	if (IOCS_B_LPEEK(bootrom + 0x24) == 0x53435349 &&	/* 'SCSI' */
    127 	    IOCS_B_WPEEK(bootrom + 0x28) == 0x494E) {		/* 'IN' */
    128 		/* spc0 */
    129 		*pif = X68K_BOOT_SCSIIF_SPC;
    130 		*punit = 0;
    131 	} else if (DOS_BUS_ERR(&bus_err_buf, (void *)EXSPC_BDID, 1)) {
    132 		/* mha0 */
    133 		*pif = X68K_BOOT_SCSIIF_MHA;
    134 		*punit = 0;
    135 	} else {
    136 		/* spc1 */
    137 		*pif = X68K_BOOT_SCSIIF_SPC;
    138 		*punit = 1;
    139 	}
    140 }
    141 
    142 /*
    143  * parse device name
    144  *
    145  * [/<controller>@<unit>/]<device>@<unit>[,<lun>][:<partition>]
    146  *
    147  * <unit> must be target SCSI ID if <device> is a SCSI device
    148  *
    149  *	full form:
    150  *		/spc@0/sd@1,2:e
    151  *
    152  *	partial form:
    153  *		/mha@0/sd@1	= /mha@0/sd@1,0,:a
    154  *		sd@1:e		= /current_device/sd@1,0e
    155  *		sd@1,2:e	= /current_device/sd@1,2:e
    156  */
    157 
    158 const struct devtbl {
    159 	char name[3];
    160 	u_char major;
    161 } devtable[] = {
    162 	X68K_BOOT_DEV_LIST
    163 };
    164 
    165 static int
    166 bootdev(devstr)
    167 	const char *devstr;
    168 {
    169 	unsigned u;
    170 	unsigned major, unit, lun, partition;
    171 	int dev;
    172 	const char *s = devstr;
    173 	unsigned interface = 0, unit_if = 0;
    174 
    175 	if (*s == '/') {
    176 		/*
    177 		 * /<interface>/<device>"
    178 		 * "/spc@1/sd@2,3:e"
    179 		 */
    180 		while (*++s == '/')	/* skip slashes */
    181 			;
    182 		if (!strchr(s, '/'))
    183 			xerrx(1, "%s: bad format", devstr);
    184 
    185 		if (!(s = lookupif(s, &interface, &unit_if)))
    186 			xerrx(1, "%s: unknown interface", devstr);
    187 
    188 		while (*s == '/')	/* skip slashes */
    189 			s++;
    190 	} else {
    191 		/* make lint happy */
    192 		interface = 0;
    193 		unit_if = 0;
    194 	}
    195 
    196 	/* allow r at the top */
    197 	if (*s == 'r')
    198 		s++;
    199 
    200 	for (u = 0; u < sizeof devtable / sizeof devtable[0]; u++)
    201 		if (s[0] == devtable[u].name[0] && s[1] == devtable[u].name[1])
    202 			goto found;
    203 
    204 	/* not found */
    205 	xerrx(1, "%s: unknown device", devstr);
    206 
    207 found:	major = devtable[u].major;
    208 
    209 	/*
    210 	 * <type>@unit[,lun][:part]
    211 	 * "sd@1,3:a"
    212 	 */
    213 
    214 	/* get device unit # */
    215 	s += 2;
    216 	if (*s == '@')
    217 		s++;
    218 	if (!*s)
    219 		xerrx(1, "%s: missing unit number", devstr);
    220 	if (!ISDIGIT(*s))
    221 		xerrx(1, "%s: wrong device", devstr);
    222 
    223 	unit = 0;
    224 	GETDECIMAL(unit, s);
    225 
    226 	lun = 0;
    227 	if (*s == ',') {
    228 		s++;
    229 		if (!ISDIGIT(*s))
    230 			xerrx(1, "%s: wrong device", devstr);
    231 		GETDECIMAL(lun, s);
    232 	}
    233 
    234 	/* get device partition */
    235 	if (*s == ':')
    236 		s++;
    237 	if (!*s)
    238 		partition = 0;	/* no partition letter -- assuming 'a' */
    239 	else if (!s[1])
    240 		partition = *s - 'a';
    241 	else
    242 		xerrx(1, "%s: wrong partition letter", devstr);
    243 
    244 	/*
    245 	 * sanity check
    246 	 */
    247 	if (unit_if >= 16)
    248 		xerrx(1, "%s: interface unit # too large", devstr);
    249 	if (unit >= 16)
    250 		xerrx(1, "%s: device unit # too large", devstr);
    251 	if (lun >= 8)
    252 		xerrx(1, "%s: SCSI LUN >= 8 is not supported yet", devstr);
    253 	if (partition >= 16)
    254 		xerrx(1, "%s: unsupported partition", devstr);
    255 
    256 	/*
    257 	 * encode device to be passed to kernel
    258 	 */
    259 	if (X68K_BOOT_DEV_IS_SCSI(major)) {
    260 		/*
    261 		 * encode SCSI device
    262 		 */
    263 		if (interface == 0)
    264 			get_current_scsi_interface(&interface, &unit_if);
    265 
    266 		dev = X68K_MAKESCSIBOOTDEV(major, interface, unit_if,
    267 						unit, lun, partition);
    268 	} else {
    269 		/* encode non-SCSI device */
    270 		dev = X68K_MAKEBOOTDEV(major, unit, partition);
    271 	}
    272 
    273 	if (opt_v)
    274 		xwarnx("%s: major %u, if %u, un_if %u, unit %u, lun %u, partition %u; bootdev 0x%x",
    275 			devstr, major, interface, unit_if, unit, lun, partition, dev);
    276 
    277 	return dev;
    278 }
    279 
    280 /*
    281  * read kernel and create trampoline
    282  *
    283  *	|----------------------| <- allocated buf addr
    284  *	| kernel image         |
    285  *	~ (header is excluded) ~
    286  *	|                      |
    287  *	|----------------------| <- return value (entry addr of trampoline)
    288  *	| struct tramparg      |
    289  *	| (trampoline args)    |
    290  *	|----------------------|
    291  *	| trampoline code      |
    292  *	| (in assembly)        |
    293  *	|----------------------|
    294  */
    295 static struct tramparg *
    296 read_kernel(fn)
    297 	const char *fn;
    298 {
    299 	int fd;
    300 	union dos_fcb *fcb;
    301 	size_t filesize, nread;
    302 	void *buf;
    303 	struct exec hdr;
    304 	int i;
    305 	struct tramparg *arg;
    306 	size_t size_tramp = end_trampoline - trampoline;
    307 
    308 	if ((fd = DOS_OPEN(fn, 0x20)) < 0)	/* RO, share READ */
    309 		xerr(1, "%s: open", fn);
    310 
    311 	if ((int)(fcb = DOS_GET_FCB_ADR(fd)) < 0)
    312 		xerr(1, "%s: get_fcb_adr", fn);
    313 
    314 	/*
    315 	 * XXX FCB is in supervisor area
    316 	 */
    317 	/*if (fcb->blk.mode != 0)*/
    318 	if (IOCS_B_BPEEK((char *)fcb + 1) & 0x80)
    319 		xerrx(1, "%s: Not a regular file", fn);
    320 
    321 	/*filesize = fcb->blk.size;*/
    322 	filesize = IOCS_B_LPEEK(&fcb->blk.size);
    323 
    324 	/*
    325 	 * read a.out header
    326 	 */
    327 	if ((nread = DOS_READ(fd, (void *) &hdr, sizeof hdr)) != sizeof hdr) {
    328 		if ((int)nread < 0)
    329 			xerr(1, "%s: read header", fn);
    330 		else
    331 			xerrx(1, "%s: Not an a.out", fn);
    332 	}
    333 	/*
    334 	 * check header
    335 	 */
    336 	if (N_GETMAGIC(hdr) != NMAGIC)
    337 		xerrx(1, "%s: Bad magic number", fn);
    338 	if ((i = N_GETMID(hdr)) != MID_M68K)
    339 		xerrx(1, "%s: Wrong architecture (mid %d)", fn, i);
    340 
    341 	if (opt_v)
    342 		xwarnx("%s: %u bytes; text %u, data %u, bss %u, sym %u",
    343 			fn, filesize, hdr.a_text, hdr.a_data,
    344 			hdr.a_bss, hdr.a_syms);
    345 
    346 	/*
    347 	 * then, read entire body
    348 	 */
    349 	if ((int)(buf = DOS_MALLOC(filesize + ALIGNBYTES - sizeof hdr
    350 				   + sizeof(struct tramparg)
    351 				   + size_tramp + SIZE_TMPSTACK)) < 0)
    352 		xerr(1, "read_kernel");
    353 
    354 	if ((nread = DOS_READ(fd, buf, filesize - sizeof hdr))
    355 					!= filesize - sizeof hdr) {
    356 		if ((int)nread < 0)
    357 			xerr(1, "%s: read", fn);
    358 		else
    359 			xerrx(1, "%s: short read", fn);
    360 	}
    361 
    362 	if (DOS_CLOSE(fd) < 0)
    363 		xerr(1, "%s: close", fn);
    364 
    365 	/*
    366 	 * create argument for trampoline code
    367 	 */
    368 	arg = (struct tramparg *) ALIGN(buf + nread);
    369 
    370 	if (opt_v)
    371 		xwarnx("trampoline arg at %p", arg);
    372 
    373 	arg->bsr_inst = TRAMP_BSR + sizeof(struct tramparg) - 2;
    374 	arg->tmp_stack = (char *) arg + sizeof(struct tramparg)
    375 				+ size_tramp + SIZE_TMPSTACK;
    376 	arg->mpu_type = IOCS_MPU_STAT() & 0xff;
    377 	arg->xk.image_top = buf;
    378 	arg->xk.load_addr = 0x00000000;	/* XXX should not be a fixed addr */
    379 	arg->xk.text_size = hdr.a_text;
    380 	arg->xk.data_size = hdr.a_data;
    381 	arg->xk.bss_size = hdr.a_bss;
    382 	arg->xk.symbol_size = hdr.a_syms;
    383 	arg->xk.d5 = IOCS_BOOTINF();	/* unused for now */
    384 #if 0
    385 	/* filled afterwards */
    386 	arg->xk.rootdev =
    387 	arg->xk.boothowto =
    388 #endif
    389 	arg->xk.entry_addr = hdr.a_entry;
    390 
    391 	if (opt_v)
    392 		xwarnx("args: mpu %d, image %p, load 0x%x, entry 0x%x",
    393 			arg->mpu_type, arg->xk.image_top, arg->xk.load_addr,
    394 			arg->xk.entry_addr);
    395 
    396 	/*
    397 	 * copy trampoline code
    398 	 */
    399 	if (opt_v)
    400 		xwarnx("trampoline code at %p (%u bytes)",
    401 			(char *) arg + sizeof(struct tramparg), size_tramp);
    402 
    403 	memcpy((char *) arg + sizeof(struct tramparg), trampoline, size_tramp);
    404 
    405 	return arg;
    406 }
    407 
    408 /*
    409  * MC68000/010		-> return zero
    410  * MC68020 and later	-> return nonzero
    411  */
    412 static int
    413 chkmpu()
    414 {
    415 	register int ret asm("d0");
    416 
    417 	asm("| %0 <- this must be d0\n\
    418 	moveq	#1,d0\n\
    419 	.long	0x103B02FF	| foo: moveb pc@((foo+1)-foo-2:B,d0:W:2),d0\n\
    420 	|	      ^ ^\n\
    421 	| d0.b	= 0x02	(68000/010)\n\
    422 	|	= 0xff	(68020 and later)\n\
    423 	bmis	1f\n\
    424 	moveq	#0,d0		| 68000/010\n\
    425 1:"	: "=d" (ret));
    426 
    427 	return ret;
    428 }
    429 
    430 static __dead void
    431 usage(status, msg)
    432 	int status;
    433 	const char *msg;
    434 {
    435 	extern const char *const __progname;
    436 
    437 	if (msg)
    438 		xwarnx("%s", msg);
    439 
    440 	xerrprintf("\
    441 %s [-hvV] [-abDs] [-r root_device] netbsd\n\
    442 \n\
    443 loadbsd options:\n\
    444 \t-h	help\n\
    445 \t-v	verbose\n\
    446 \t-V	print version and exit\n\
    447 \n\
    448 kernel options:\n\
    449 \t-a	auto boot, opposite of -s\n\
    450 \t-s	single user boot (default)\n\
    451 \t-D	enter kernel debugger\n\
    452 \t-b	ask root device\n\
    453 \t-d	use compiled-in rootdev\n\
    454 \t-r	specify root device (default %s)\n\
    455 \t	format:  [/interface/]device@unit[,lun][:partition]\n\
    456 \t	    interface: one of  spc@0, spc@1, mha@0\n\
    457 \t		       (current boot interface if omitted)\n\
    458 \t	    device:    one of  fd, sd, cd, md\n\
    459 \t	    unit:      device unit number (SCSI ID for SCSI device)\n\
    460 \t	    lun:       SCSI LUN # (0 if omitted)\n\
    461 \t	    partition: partition letter ('a' if omitted)\n\
    462 ", __progname, DEFAULT_ROOTDEVNAME);
    463 
    464 	DOS_EXIT2(status);
    465 }
    466 
    467 int
    468 main(argc, argv)
    469 	int argc;
    470 	char *argv[];
    471 {
    472 	char *rootdevname = 0;
    473 	int rootdev;
    474 	u_long boothowto = RB_SINGLE;
    475 	const char *kernel;
    476 	char *p, **flg, **arg;
    477 	struct tramparg *tramp;
    478 	struct dos_dregs regs;	/* unused... */
    479 	int i;
    480 
    481 	/* parse options */
    482 	for (arg = flg = argv + 1; (p = *flg) && *p == '-'; ) {
    483 		int c;
    484 
    485 		while ((c = *++p))
    486 			switch (c) {
    487 			case 'h':
    488 				usage(0, (char *) 0);
    489 				/* NOTREACHED */
    490 				break;
    491 			case 'N':	/* don't actually execute kernel */
    492 				opt_N = 1;
    493 				break;
    494 			case 'v':
    495 				opt_v = 1;
    496 				break;
    497 			case 'V':
    498 				xprintf("loadbsd %s\n", VERSION);
    499 				return 0;
    500 
    501 			/*
    502 			 * kernel boot flags
    503 			 */
    504 			case 'r':
    505 				if (rootdevname)
    506 					usage(1, "multiple -r flags");
    507 				else if (!*++arg)
    508 					usage(1, "-r requires device name");
    509 				else
    510 					rootdevname = *arg;
    511 				break;
    512 			case 'd':
    513 				boothowto |= RB_DFLTROOT;
    514 				break;
    515 			case 'b':
    516 				boothowto |= RB_ASKNAME;
    517 				break;
    518 			case 'a':
    519 				boothowto &= ~RB_SINGLE;
    520 				break;
    521 			case 's':
    522 				boothowto |= RB_SINGLE;
    523 				break;
    524 			case 'D':
    525 				boothowto |= RB_KDB;
    526 				break;
    527 
    528 			default:
    529 				usage(1, (char *) 0);
    530 				/* NOTREACHED */
    531 				break;
    532 			}
    533 		flg = ++arg;
    534 	}
    535 
    536 	/* check MPU */
    537 	if (chkmpu() == 0)
    538 		xerrx(1, "Can't boot NetBSD on 68000/010");
    539 
    540 	argc -= arg - argv;
    541 	argv = arg;
    542 
    543 	if (argc != 1)
    544 		usage(1, (char *) 0);
    545 
    546 	kernel = *argv;
    547 
    548 	rootdev = bootdev(rootdevname ? rootdevname : DEFAULT_ROOTDEVNAME);
    549 
    550 	if (opt_v)
    551 		xwarnx("boothowto 0x%x", boothowto);
    552 
    553 	tramp = read_kernel(kernel);
    554 
    555 	tramp->xk.rootdev = rootdev;
    556 	tramp->xk.boothowto = boothowto;
    557 
    558 	/*
    559 	 * we never returns, and make sure the disk cache
    560 	 * be flushed (if write-back cache is enabled)
    561 	 */
    562 	if (opt_v)
    563 		xwarnx("flush disk cache...");
    564 
    565 	i = DOS_FFLUSH_SET(1);		/* enable fflush */
    566 	DOS_FFLUSH();			/* then, issue fflush */
    567 	(void) DOS_FFLUSH_SET(i);	/* restore old mode just in case */
    568 
    569 	/*
    570 	 * the program assumes the MPU caches off
    571 	 */
    572 	if (opt_v)
    573 		xwarnx("flush and disable MPU caches...");
    574 
    575 	IOCS_CACHE_MD(-1);		/* flush */
    576 	if (!opt_N)
    577 		IOCS_CACHE_MD(0);	/* disable both caches */
    578 
    579 	if (opt_v)
    580 		xwarnx("Jumping to the kernel.  Good Luck!");
    581 
    582 	if (opt_N)
    583 		xerrx(0, "But don't actually do it.");
    584 
    585 	DOS_SUPER_JSR((void (*) __P((void))) tramp, &regs, &regs);
    586 
    587 	/* NOTREACHED */
    588 
    589 	xwarnx("??? return from kernel");
    590 
    591 	return 1;
    592 }
    593