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