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