Home | History | Annotate | Line # | Download | only in dosboot
main.c revision 1.26
      1 /*	$NetBSD: main.c,v 1.26 2009/03/14 15:36:07 dsl Exp $	 */
      2 
      3 /*
      4  * Copyright (c) 1996, 1997
      5  * 	Matthias Drochner.  All rights reserved.
      6  * Copyright (c) 1996, 1997
      7  * 	Perry E. Metzger.  All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgements:
     19  *	This product includes software developed for the NetBSD Project
     20  *	by Matthias Drochner.
     21  *	This product includes software developed for the NetBSD Project
     22  *	by Perry E. Metzger.
     23  * 4. The names of the authors may not be used to endorse or promote products
     24  *    derived from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     29  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     31  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 
     39 #include <sys/reboot.h>
     40 
     41 #include <lib/libkern/libkern.h>
     42 #include <lib/libsa/stand.h>
     43 #include <lib/libsa/ufs.h>
     44 
     45 #include <libi386.h>
     46 
     47 #ifdef SUPPORT_LYNX
     48 extern int exec_lynx(const char*, int);
     49 #endif
     50 
     51 int errno;
     52 
     53 extern	char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
     54 
     55 #define MAXDEVNAME 16
     56 
     57 static char    *current_fsmode;
     58 static char    *default_devname;
     59 static int      default_unit, default_partition;
     60 static char    *default_filename;
     61 
     62 char *sprint_bootsel(const char *);
     63 static void bootit(const char *, int, int);
     64 void usage(void);
     65 int main(int, char **);
     66 
     67 void	command_help(char *);
     68 void	command_ls(char *);
     69 void	command_quit(char *);
     70 void	command_boot(char *);
     71 void	command_mode(char *);
     72 void	command_dev(char *);
     73 
     74 const struct bootblk_command commands[] = {
     75 	{ "help",	command_help },
     76 	{ "?",		command_help },
     77 	{ "ls",		command_ls },
     78 	{ "quit",	command_quit },
     79 	{ "boot",	command_boot },
     80 	{ "mode",	command_mode },
     81 	{ "dev",	command_dev },
     82 	{ NULL,		NULL },
     83 };
     84 
     85 int
     86 parsebootfile(fname, fsmode, devname, unit, partition, file)
     87 	const char     *fname;
     88 	char          **fsmode; /* out */
     89 	char          **devname; /* out */
     90 	int            *unit, *partition; /* out */
     91 	const char    **file; /* out */
     92 {
     93 	const char     *col, *help;
     94 
     95 	*fsmode = current_fsmode;
     96 	*devname = default_devname;
     97 	*unit = default_unit;
     98 	*partition = default_partition;
     99 	*file = default_filename;
    100 
    101 	if (fname == NULL)
    102 		return (0);
    103 
    104 	if (strcmp(current_fsmode, "dos") && (col = strchr(fname, ':'))) {
    105 		/* no DOS, device given */
    106 		static char     savedevname[MAXDEVNAME + 1];
    107 		int             devlen;
    108 		unsigned int    u = 0, p = 0;
    109 		int             i = 0;
    110 
    111 		devlen = col - fname;
    112 		if (devlen > MAXDEVNAME)
    113 			return (EINVAL);
    114 
    115 #define isvalidname(c) ((c) >= 'a' && (c) <= 'z')
    116 		if (!isvalidname(fname[i]))
    117 			return (EINVAL);
    118 		do {
    119 			savedevname[i] = fname[i];
    120 			i++;
    121 		} while (isvalidname(fname[i]));
    122 		savedevname[i] = '\0';
    123 
    124 #define isnum(c) ((c) >= '0' && (c) <= '9')
    125 		if (i < devlen) {
    126 			if (!isnum(fname[i]))
    127 				return (EUNIT);
    128 			do {
    129 				u *= 10;
    130 				u += fname[i++] - '0';
    131 			} while (isnum(fname[i]));
    132 		}
    133 
    134 #define isvalidpart(c) ((c) >= 'a' && (c) <= 'z')
    135 		if (i < devlen) {
    136 			if (!isvalidpart(fname[i]))
    137 				return (EPART);
    138 			p = fname[i++] - 'a';
    139 		}
    140 		if (i != devlen)
    141 			return (ENXIO);
    142 
    143 		*devname = savedevname;
    144 		*unit = u;
    145 		*partition = p;
    146 		help = col + 1;
    147 	} else
    148 		help = fname;
    149 
    150 	if (*help)
    151 		*file = help;
    152 
    153 	return (0);
    154 }
    155 
    156 char *
    157 sprint_bootsel(const char *filename)
    158 {
    159 	char *fsname, *devname;
    160 	int unit, partition;
    161 	const char *file;
    162 	static char buf[80];
    163 
    164 	if (parsebootfile(filename, &fsname, &devname, &unit,
    165 			  &partition, &file) == 0) {
    166 		if (!strcmp(fsname, "dos"))
    167 			sprintf(buf, "dos:%s", file);
    168 		else if (!strcmp(fsname, "ufs"))
    169 			sprintf(buf, "%s%d%c:%s", devname, unit,
    170 				'a' + partition, file);
    171 		else goto bad;
    172 		return (buf);
    173 	}
    174 bad:
    175 	return ("(invalid)");
    176 }
    177 
    178 static void
    179 bootit(filename, howto, tell)
    180 	const char     *filename;
    181 	int             howto, tell;
    182 {
    183 	int floppy = strncmp(default_devname, "fd", 2) == 0;
    184 	if (tell) {
    185 		printf("booting %s", sprint_bootsel(filename));
    186 		if (howto)
    187 			printf(" (howto 0x%x)", howto);
    188 		printf("\n");
    189 	}
    190 #ifdef SUPPORT_LYNX
    191 	if(exec_netbsd(filename, 0, howto, floppy) < 0)
    192 		printf("boot netbsd: %s: %s\n", sprint_bootsel(filename),
    193 		       strerror(errno));
    194 	else {
    195 		printf("boot netbsd returned\n");
    196 		return;
    197 	}
    198 	if (exec_lynx(filename, 0) < 0)
    199 		printf("boot lynx: %s: %s\n", sprint_bootsel(filename),
    200 		       strerror(errno));
    201 	else
    202 		printf("boot lynx returned\n");
    203 #else
    204 	if (exec_netbsd(filename, 0, howto, floppy) < 0)
    205 		printf("boot: %s: %s\n", sprint_bootsel(filename),
    206 		       strerror(errno));
    207 	else
    208 		printf("boot returned\n");
    209 #endif
    210 }
    211 
    212 static void
    213 print_banner(void)
    214 {
    215 	int extmem = getextmem();
    216 	char *s = "";
    217 
    218 	clear_pc_screen();
    219 
    220 #ifdef XMS
    221 	u_long xmsmem;
    222 	if (getextmem1() == 0 && (xmsmem = checkxms()) != 0) {
    223 		/*
    224 		 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
    225 		 *  getextmem() is getextmem1(). Without, the "smart"
    226 		 *  methods could fail to report all memory as well.
    227 		 * xmsmem is a few kB less than the actual size, but
    228 		 *  better than nothing.
    229 		 */
    230 		if ((int)xmsmem > extmem)
    231 			extmem = xmsmem;
    232 		s = "(xms) ";
    233 	}
    234 #endif
    235 
    236 	printf("\n"
    237 	       ">> %s, Revision %s (from NetBSD %s)\n"
    238 	       ">> Memory: %d/%d %sk\n",
    239 	       bootprog_name, bootprog_rev, bootprog_kernrev,
    240 	       getbasemem(), extmem, s);
    241 }
    242 
    243 void
    244 usage()
    245 {
    246 	printf("dosboot [-u] [-c <commands>] [-i] [filename [-bootopts]]\n");
    247 }
    248 
    249 int
    250 main(int argc, char **argv)
    251 {
    252 	int             ch;
    253 	int             interactive = 0;
    254 	int             howto;
    255 	extern char    *optarg;
    256 	extern int      optind;
    257 
    258 #ifdef	SUPPORT_SERIAL
    259 	initio(SUPPORT_SERIAL);
    260 #else
    261 	initio(CONSDEV_PC);
    262 #endif
    263 	gateA20();
    264 
    265 	print_banner();
    266 
    267 	current_fsmode = "dos";
    268 	default_devname = "hd";
    269 	default_unit = 0;
    270 	default_partition = 0;
    271 	default_filename = "netbsd";
    272 
    273 	while ((ch = getopt(argc, argv, "c:iu")) != -1) {
    274 		switch (ch) {
    275 		case 'c':
    276 			docommand(optarg);
    277 			return (1);
    278 			break;
    279 		case 'i':
    280 			interactive = 1;
    281 			break;
    282 		case 'u':
    283 			current_fsmode = "ufs";
    284 			break;
    285 		default:
    286 			usage();
    287 			return (1);
    288 		}
    289 	}
    290 
    291 	if (interactive) {
    292 		printf("type \"?\" or \"help\" for help.\n");
    293 		bootmenu();
    294 	}
    295 
    296 	argc -= optind;
    297 	argv += optind;
    298 
    299 	if (argc > 2) {
    300 		usage();
    301 		return (1);
    302 	}
    303 	howto = 0;
    304 	if (argc > 1 && !parseopts(argv[1], &howto))
    305 		return (1);
    306 
    307 	bootit((argc > 0 ? argv[0] : "netbsd"), howto, 1);
    308 	return (1);
    309 }
    310 
    311 /* ARGSUSED */
    312 void
    313 command_help(char *arg)
    314 {
    315 	printf("commands are:\n"
    316 	       "boot [xdNx:][filename] [-acdqsv]\n"
    317 	       "     (ex. \"sd0a:netbsd.old -s\"\n"
    318 	       "ls [path]\n"
    319 	       "mode ufs|dos\n"
    320 	       "dev xd[N[x]]:\n"
    321 	       "help|?\n"
    322 	       "quit\n");
    323 }
    324 
    325 void
    326 command_ls(char *arg)
    327 {
    328 	char *help = default_filename;
    329 	if (strcmp(current_fsmode, "ufs")) {
    330 		printf("UFS only\n");
    331 		return;
    332 	}
    333 	default_filename = "/";
    334 	ufs_ls(arg);
    335 	default_filename = help;
    336 }
    337 
    338 /* ARGSUSED */
    339 void
    340 command_quit(char *arg)
    341 {
    342 	printf("Exiting... goodbye...\n");
    343 	exit(0);
    344 }
    345 
    346 void
    347 command_boot(char *arg)
    348 {
    349 	char *filename;
    350 	int howto;
    351 
    352 	if (parseboot(arg, &filename, &howto))
    353 		bootit(filename, howto, 1);
    354 }
    355 
    356 void
    357 command_mode(char *arg)
    358 {
    359 	if (!strcmp("dos", arg))
    360 		current_fsmode = "dos";
    361 	else if (!strcmp("ufs", arg))
    362 		current_fsmode = "ufs";
    363 	else
    364 		printf("invalid mode\n");
    365 }
    366 
    367 void
    368 command_dev(char *arg)
    369 {
    370 	static char savedevname[MAXDEVNAME + 1];
    371 	char *fsname, *devname;
    372 	const char *file; /* dummy */
    373 
    374 	if (!strcmp(current_fsmode, "dos")) {
    375 		printf("not available in DOS mode\n");
    376 		return;
    377 	}
    378 
    379 	if (*arg == '\0') {
    380 		printf("%s%d%c:\n", default_devname, default_unit,
    381 		       'a' + default_partition);
    382 		return;
    383 	}
    384 
    385 	if (!strchr(arg, ':') ||
    386 	    parsebootfile(arg, &fsname, &devname, &default_unit,
    387 			  &default_partition, &file)) {
    388 		command_help(NULL);
    389 		return;
    390 	}
    391 
    392 	/* put to own static storage */
    393 	strncpy(savedevname, devname, MAXDEVNAME + 1);
    394 	default_devname = savedevname;
    395 }
    396