Home | History | Annotate | Line # | Download | only in dosboot
main.c revision 1.9
      1  1.9  drochner /*	$NetBSD: main.c,v 1.9 1997/10/31 18:50:09 drochner 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 void ls  __P((char *));
     47  1.2   thorpej extern int getopt __P((int, char **, const char *));
     48  1.1     perry 
     49  1.7  drochner #ifdef SUPPORT_LYNX
     50  1.7  drochner extern int exec_lynx __P((const char*, int));
     51  1.7  drochner #endif
     52  1.7  drochner 
     53  1.2   thorpej int             errno;
     54  1.1     perry 
     55  1.4   thorpej extern	char bootprog_name[], bootprog_rev[], bootprog_date[],
     56  1.4   thorpej 	bootprog_maker[];
     57  1.1     perry 
     58  1.1     perry #define MAXDEVNAME 16
     59  1.1     perry 
     60  1.2   thorpej static char    *current_fsmode;
     61  1.2   thorpej static char    *default_devname;
     62  1.2   thorpej static int      default_unit, default_partition;
     63  1.2   thorpej static char    *default_filename;
     64  1.1     perry 
     65  1.7  drochner void	command_help __P((char *));
     66  1.7  drochner void	command_ls __P((char *));
     67  1.7  drochner void	command_quit __P((char *));
     68  1.7  drochner void	command_boot __P((char *));
     69  1.7  drochner void	command_mode __P((char *));
     70  1.9  drochner void	command_dev __P((char *));
     71  1.7  drochner 
     72  1.7  drochner struct bootblk_command commands[] = {
     73  1.7  drochner 	{ "help",	command_help },
     74  1.7  drochner 	{ "?",		command_help },
     75  1.7  drochner 	{ "ls",		command_ls },
     76  1.7  drochner 	{ "quit",	command_quit },
     77  1.7  drochner 	{ "boot",	command_boot },
     78  1.7  drochner 	{ "mode",	command_mode },
     79  1.9  drochner 	{ "dev",	command_dev },
     80  1.7  drochner 	{ NULL,		NULL },
     81  1.7  drochner };
     82  1.7  drochner 
     83  1.1     perry int
     84  1.1     perry parsebootfile(fname, fsmode, devname, unit, partition, file)
     85  1.2   thorpej 	const char     *fname;
     86  1.7  drochner 	char          **fsmode; /* out */
     87  1.7  drochner 	char          **devname; /* out */
     88  1.7  drochner 	unsigned int   *unit, *partition; /* out */
     89  1.7  drochner 	const char    **file; /* out */
     90  1.2   thorpej {
     91  1.2   thorpej 	const char     *col, *help;
     92  1.2   thorpej 
     93  1.2   thorpej 	*fsmode = current_fsmode;
     94  1.2   thorpej 	*devname = default_devname;
     95  1.2   thorpej 	*unit = default_unit;
     96  1.2   thorpej 	*partition = default_partition;
     97  1.2   thorpej 	*file = default_filename;
     98  1.2   thorpej 
     99  1.7  drochner 	if (fname == NULL)
    100  1.2   thorpej 		return (0);
    101  1.2   thorpej 
    102  1.9  drochner 	if (strcmp(current_fsmode, "dos") && (col = strchr(fname, ':'))) {
    103  1.9  drochner 		/* no DOS, device given */
    104  1.2   thorpej 		static char     savedevname[MAXDEVNAME + 1];
    105  1.2   thorpej 		int             devlen;
    106  1.2   thorpej 		unsigned int    u = 0, p = 0;
    107  1.2   thorpej 		int             i = 0;
    108  1.2   thorpej 
    109  1.2   thorpej 		devlen = col - fname;
    110  1.2   thorpej 		if (devlen > MAXDEVNAME)
    111  1.2   thorpej 			return (EINVAL);
    112  1.1     perry 
    113  1.1     perry #define isvalidname(c) ((c) >= 'a' && (c) <= 'z')
    114  1.2   thorpej 		if (!isvalidname(fname[i]))
    115  1.2   thorpej 			return (EINVAL);
    116  1.2   thorpej 		do {
    117  1.2   thorpej 			savedevname[i] = fname[i];
    118  1.2   thorpej 			i++;
    119  1.2   thorpej 		} while (isvalidname(fname[i]));
    120  1.2   thorpej 		savedevname[i] = '\0';
    121  1.1     perry 
    122  1.1     perry #define isnum(c) ((c) >= '0' && (c) <= '9')
    123  1.2   thorpej 		if (i < devlen) {
    124  1.2   thorpej 			if (!isnum(fname[i]))
    125  1.2   thorpej 				return (EUNIT);
    126  1.2   thorpej 			do {
    127  1.2   thorpej 				u *= 10;
    128  1.2   thorpej 				u += fname[i++] - '0';
    129  1.2   thorpej 			} while (isnum(fname[i]));
    130  1.2   thorpej 		}
    131  1.7  drochner 
    132  1.1     perry #define isvalidpart(c) ((c) >= 'a' && (c) <= 'z')
    133  1.2   thorpej 		if (i < devlen) {
    134  1.2   thorpej 			if (!isvalidpart(fname[i]))
    135  1.2   thorpej 				return (EPART);
    136  1.2   thorpej 			p = fname[i++] - 'a';
    137  1.2   thorpej 		}
    138  1.2   thorpej 		if (i != devlen)
    139  1.2   thorpej 			return (ENXIO);
    140  1.2   thorpej 
    141  1.2   thorpej 		*devname = savedevname;
    142  1.2   thorpej 		*unit = u;
    143  1.2   thorpej 		*partition = p;
    144  1.2   thorpej 		help = col + 1;
    145  1.2   thorpej 	} else
    146  1.2   thorpej 		help = fname;
    147  1.1     perry 
    148  1.2   thorpej 	if (*help)
    149  1.2   thorpej 		*file = help;
    150  1.1     perry 
    151  1.2   thorpej 	return (0);
    152  1.1     perry }
    153  1.1     perry 
    154  1.7  drochner char *sprint_bootsel(filename)
    155  1.7  drochner const char *filename;
    156  1.1     perry {
    157  1.7  drochner 	char *fsname, *devname;
    158  1.7  drochner 	int unit, partition;
    159  1.7  drochner 	const char *file;
    160  1.7  drochner 	static char buf[80];
    161  1.7  drochner 
    162  1.7  drochner 	if (parsebootfile(filename, &fsname, &devname, &unit,
    163  1.7  drochner 			  &partition, &file) == 0) {
    164  1.2   thorpej 		if (!strcmp(fsname, "dos"))
    165  1.7  drochner 			sprintf(buf, "dos:%s", file);
    166  1.2   thorpej 		else if (!strcmp(fsname, "ufs"))
    167  1.7  drochner 			sprintf(buf, "%s%d%c:%s", devname, unit, 'a' + partition, file);
    168  1.7  drochner 		else goto bad;
    169  1.7  drochner 		return(buf);
    170  1.2   thorpej 	}
    171  1.7  drochner bad:
    172  1.7  drochner 	return("(invalid)");
    173  1.1     perry }
    174  1.1     perry 
    175  1.1     perry static void
    176  1.1     perry bootit(filename, howto, tell)
    177  1.2   thorpej 	const char     *filename;
    178  1.2   thorpej 	int             howto, tell;
    179  1.1     perry {
    180  1.7  drochner 	if (tell) {
    181  1.7  drochner 		printf("booting %s", sprint_bootsel(filename));
    182  1.7  drochner 		if (howto)
    183  1.7  drochner 			printf(" (howto 0x%x)", howto);
    184  1.7  drochner 		printf("\n");
    185  1.7  drochner 	}
    186  1.7  drochner #ifdef SUPPORT_LYNX
    187  1.7  drochner 	if(exec_netbsd(filename, 0, howto) < 0)
    188  1.7  drochner 		printf("boot netbsd: %s: %s\n", sprint_bootsel(filename),
    189  1.7  drochner 		       strerror(errno));
    190  1.7  drochner 	else {
    191  1.7  drochner 		printf("boot netbsd returned\n");
    192  1.7  drochner 		return;
    193  1.7  drochner 	}
    194  1.7  drochner 	if (exec_lynx(filename, 0) < 0)
    195  1.7  drochner 		printf("boot lynx: %s: %s\n", sprint_bootsel(filename),
    196  1.7  drochner 		       strerror(errno));
    197  1.7  drochner 	else
    198  1.7  drochner 		printf("boot lynx returned\n");
    199  1.7  drochner #else
    200  1.7  drochner 	if (exec_netbsd(filename, 0, howto) < 0)
    201  1.7  drochner 		printf("boot: %s: %s\n", sprint_bootsel(filename),
    202  1.7  drochner 		       strerror(errno));
    203  1.2   thorpej 	else
    204  1.2   thorpej 		printf("boot returned\n");
    205  1.7  drochner #endif
    206  1.1     perry }
    207  1.1     perry 
    208  1.1     perry static void
    209  1.1     perry print_banner(void)
    210  1.1     perry {
    211  1.5  christos 	int extmem = getextmem();
    212  1.5  christos 	char *s = "";
    213  1.5  christos 
    214  1.5  christos #ifdef XMS
    215  1.6  drochner 	if (getextmem1() == 0) {
    216  1.5  christos 		if ((extmem = checkxms()) != 0)
    217  1.5  christos 			s = "(xms) ";
    218  1.5  christos 	}
    219  1.5  christos #endif
    220  1.4   thorpej 
    221  1.4   thorpej 	printf("\n");
    222  1.4   thorpej 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
    223  1.4   thorpej 	printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
    224  1.5  christos 	printf(">> Memory: %d/%d %sk\n", getbasemem(), extmem, s);
    225  1.2   thorpej }
    226  1.2   thorpej 
    227  1.2   thorpej void
    228  1.2   thorpej usage()
    229  1.2   thorpej {
    230  1.2   thorpej 	printf("dosboot [-u] [-c <commands>] [-i] [filename [-bootopts]]\n");
    231  1.2   thorpej }
    232  1.2   thorpej 
    233  1.2   thorpej int
    234  1.2   thorpej main(argc, argv)
    235  1.2   thorpej 	int             argc;
    236  1.2   thorpej 	char          **argv;
    237  1.2   thorpej {
    238  1.2   thorpej 	int             ch;
    239  1.2   thorpej 	int             interactive = 0;
    240  1.2   thorpej 	int             howto;
    241  1.2   thorpej 	extern char    *optarg;
    242  1.2   thorpej 	extern int      optind;
    243  1.2   thorpej 
    244  1.7  drochner 	initio(CONSDEV_PC);
    245  1.2   thorpej 	gateA20();
    246  1.2   thorpej 
    247  1.2   thorpej 	print_banner();
    248  1.2   thorpej 
    249  1.2   thorpej 	current_fsmode = "dos";
    250  1.2   thorpej 	default_devname = "hd";
    251  1.2   thorpej 	default_unit = 0;
    252  1.2   thorpej 	default_partition = 0;
    253  1.2   thorpej 	default_filename = "netbsd";
    254  1.2   thorpej 
    255  1.2   thorpej 	while ((ch = getopt(argc, argv, "c:iu")) != -1) {
    256  1.2   thorpej 		switch (ch) {
    257  1.2   thorpej 		case 'c':
    258  1.2   thorpej 			docommand(optarg);
    259  1.2   thorpej 			return (1);
    260  1.2   thorpej 			break;
    261  1.2   thorpej 		case 'i':
    262  1.2   thorpej 			interactive = 1;
    263  1.2   thorpej 			break;
    264  1.2   thorpej 		case 'u':
    265  1.2   thorpej 			current_fsmode = "ufs";
    266  1.2   thorpej 			break;
    267  1.2   thorpej 		default:
    268  1.2   thorpej 			usage();
    269  1.2   thorpej 			return (1);
    270  1.2   thorpej 		}
    271  1.2   thorpej 	}
    272  1.2   thorpej 
    273  1.7  drochner 	if (interactive) {
    274  1.8  drochner 		printf("type \"?\" or \"help\" for help.\n");
    275  1.2   thorpej 		bootmenu();
    276  1.7  drochner 	}
    277  1.2   thorpej 
    278  1.2   thorpej 	argc -= optind;
    279  1.2   thorpej 	argv += optind;
    280  1.2   thorpej 
    281  1.2   thorpej 	if (argc > 2) {
    282  1.2   thorpej 		usage();
    283  1.2   thorpej 		return (1);
    284  1.2   thorpej 	}
    285  1.2   thorpej 	howto = 0;
    286  1.2   thorpej 	if (argc > 1 && !parseopts(argv[1], &howto))
    287  1.2   thorpej 		return (1);
    288  1.1     perry 
    289  1.2   thorpej 	bootit((argc > 0 ? argv[0] : "netbsd"), howto, 1);
    290  1.2   thorpej 	return (1);
    291  1.7  drochner }
    292  1.7  drochner 
    293  1.7  drochner /* ARGSUSED */
    294  1.7  drochner void
    295  1.7  drochner command_help(arg)
    296  1.7  drochner 	char *arg;
    297  1.7  drochner {
    298  1.7  drochner 	printf("commands are:\n"
    299  1.7  drochner 	       "boot [xdNx:][filename] [-adrs]\n"
    300  1.7  drochner 	       "     (ex. \"sd0a:netbsd.old -s\"\n"
    301  1.7  drochner 	       "ls [path]\n"
    302  1.7  drochner 	       "mode ufs|dos\n"
    303  1.9  drochner 	       "dev xd[N[x]]:\n"
    304  1.7  drochner 	       "help|?\n"
    305  1.7  drochner 	       "quit\n");
    306  1.7  drochner }
    307  1.7  drochner 
    308  1.7  drochner void
    309  1.7  drochner command_ls(arg)
    310  1.7  drochner 	char *arg;
    311  1.7  drochner {
    312  1.7  drochner 	char *help = default_filename;
    313  1.7  drochner 	if (strcmp(current_fsmode, "ufs")) {
    314  1.7  drochner 		printf("UFS only\n");
    315  1.7  drochner 		return;
    316  1.7  drochner 	}
    317  1.7  drochner 	default_filename = "/";
    318  1.7  drochner 	ls(arg);
    319  1.7  drochner 	default_filename = help;
    320  1.7  drochner }
    321  1.7  drochner 
    322  1.7  drochner /* ARGSUSED */
    323  1.7  drochner void
    324  1.7  drochner command_quit(arg)
    325  1.7  drochner 	char *arg;
    326  1.7  drochner {
    327  1.7  drochner 	printf("Exiting... goodbye...\n");
    328  1.7  drochner 	exit(0);
    329  1.7  drochner }
    330  1.7  drochner 
    331  1.7  drochner void
    332  1.7  drochner command_boot(arg)
    333  1.7  drochner 	char *arg;
    334  1.7  drochner {
    335  1.7  drochner 	char *filename;
    336  1.7  drochner 	int howto;
    337  1.7  drochner 
    338  1.7  drochner 	if (parseboot(arg, &filename, &howto))
    339  1.7  drochner 		bootit(filename, howto, 1);
    340  1.7  drochner }
    341  1.7  drochner 
    342  1.7  drochner void
    343  1.7  drochner command_mode(arg)
    344  1.7  drochner 	char *arg;
    345  1.7  drochner {
    346  1.7  drochner 	if (!strcmp("dos", arg))
    347  1.7  drochner 		current_fsmode = "dos";
    348  1.7  drochner 	else if (!strcmp("ufs", arg))
    349  1.7  drochner 		current_fsmode = "ufs";
    350  1.7  drochner 	else
    351  1.7  drochner 		printf("invalid mode\n");
    352  1.9  drochner }
    353  1.9  drochner 
    354  1.9  drochner void
    355  1.9  drochner command_dev(arg)
    356  1.9  drochner 	char *arg;
    357  1.9  drochner {
    358  1.9  drochner 	static char savedevname[MAXDEVNAME + 1];
    359  1.9  drochner 	char *fsname, *devname;
    360  1.9  drochner 	const char *file; /* dummy */
    361  1.9  drochner 
    362  1.9  drochner 	if (!strcmp(current_fsmode, "dos")) {
    363  1.9  drochner 		printf("not available in DOS mode\n");
    364  1.9  drochner 		return;
    365  1.9  drochner 	}
    366  1.9  drochner 
    367  1.9  drochner 	if (*arg == '\0') {
    368  1.9  drochner 		printf("%s%d%c:\n", default_devname, default_unit,
    369  1.9  drochner 		       'a' + default_partition);
    370  1.9  drochner 		return;
    371  1.9  drochner 	}
    372  1.9  drochner 
    373  1.9  drochner 	if (!strchr(arg, ':') ||
    374  1.9  drochner 	    parsebootfile(arg, &fsname, &devname, &default_unit,
    375  1.9  drochner 			  &default_partition, &file)) {
    376  1.9  drochner 		command_help(NULL);
    377  1.9  drochner 		return;
    378  1.9  drochner 	}
    379  1.9  drochner 
    380  1.9  drochner 	/* put to own static storage */
    381  1.9  drochner 	strncpy(savedevname, devname, MAXDEVNAME + 1);
    382  1.9  drochner 	default_devname = savedevname;
    383  1.1     perry }
    384