Home | History | Annotate | Line # | Download | only in pxeboot
main.c revision 1.13
      1 /*	$NetBSD: main.c,v 1.13 2008/05/11 11:42:03 chris Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996
      5  * 	Matthias Drochner.  All rights reserved.
      6  * Copyright (c) 1996
      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/types.h>
     40 #include <sys/reboot.h>
     41 #include <sys/bootblock.h>
     42 
     43 #include <lib/libkern/libkern.h>
     44 
     45 #include <lib/libsa/stand.h>
     46 
     47 #include <libi386.h>
     48 #include "pxeboot.h"
     49 #include "bootmod.h"
     50 
     51 extern struct x86_boot_params boot_params;
     52 
     53 int errno;
     54 int debug;
     55 
     56 extern char	bootprog_name[], bootprog_rev[], bootprog_date[],
     57 		bootprog_maker[];
     58 
     59 int	main(void);
     60 
     61 void	command_help __P((char *));
     62 void	command_quit __P((char *));
     63 void	command_boot __P((char *));
     64 void	command_consdev(char *);
     65 void	command_load(char *);
     66 void	command_modules(char *);
     67 
     68 const struct bootblk_command commands[] = {
     69 	{ "help",	command_help },
     70 	{ "?",		command_help },
     71 	{ "quit",	command_quit },
     72 	{ "boot",	command_boot },
     73 	{ "consdev",	command_consdev },
     74 	{ "modules",    command_modules },
     75 	{ "load",	command_load },
     76 	{ NULL,		NULL },
     77 };
     78 
     79 static int
     80 bootit(const char *filename, int howto)
     81 {
     82 	if (exec_netbsd(filename, 0, howto) < 0)
     83 		printf("boot: %s\n", strerror(errno));
     84 	else
     85 		printf("boot returned\n");
     86 	return (-1);
     87 }
     88 
     89 static void
     90 print_banner(void)
     91 {
     92 	int base = getbasemem();
     93 	int ext = getextmem();
     94 
     95 	printf("\n"
     96 	       ">> %s, Revision %s\n"
     97 	       ">> (%s, %s)\n"
     98 	       ">> Memory: %d/%d k\n",
     99 	       bootprog_name, bootprog_rev,
    100 	       bootprog_maker, bootprog_date,
    101 	       base, ext);
    102 }
    103 
    104 int
    105 main(void)
    106 {
    107         char c;
    108 
    109 #ifdef SUPPORT_SERIAL
    110 	initio(SUPPORT_SERIAL);
    111 #else
    112 	initio(CONSDEV_PC);
    113 #endif
    114 	gateA20();
    115 
    116 	print_banner();
    117 
    118 	printf("Press return to boot now, any other key for boot menu\n");
    119 	printf("Starting in ");
    120 
    121 	c = awaitkey(boot_params.bp_timeout, 1);
    122 	if ((c != '\r') && (c != '\n') && (c != '\0')) {
    123 		printf("type \"?\" or \"help\" for help.\n");
    124 		bootmenu();	/* does not return */
    125 	}
    126 
    127 	/*
    128 	 * The file name provided here is just a default.  If the
    129 	 * DHCP server provides a file name, we'll use that instead.
    130 	 */
    131 	bootit("netbsd", 0);
    132 
    133 	/*
    134 	 * If that fails, let the BIOS try the next boot device.
    135 	 */
    136 	return (1);
    137 }
    138 
    139 /* ARGSUSED */
    140 void
    141 command_help(char *arg)
    142 {
    143 	printf("commands are:\n"
    144 	       "boot [filename] [-adsqv]\n"
    145 	       "     (ex. \"netbsd.old -s\"\n"
    146 	       "consdev {pc|com[0123]|com[0123]kbd|auto}\n"
    147 	       "modules {enabled|disabled}\n"
    148 	       "load {path_to_module}\n"
    149 	       "help|?\n"
    150 	       "quit\n");
    151 }
    152 
    153 /* ARGSUSED */
    154 void
    155 command_quit(char *arg)
    156 {
    157 
    158 	printf("Exiting...\n");
    159 	delay(1000000);
    160 	reboot();
    161 	/* Note: we shouldn't get to this point! */
    162 	panic("Could not reboot!");
    163 	exit(0);
    164 }
    165 
    166 void
    167 command_boot(char *arg)
    168 {
    169 	char *filename;
    170 	int howto;
    171 
    172 	if (parseboot(arg, &filename, &howto))
    173 		bootit(filename, howto);
    174 }
    175 
    176 static const struct cons_devs {
    177     const char	*name;
    178     u_int	tag;
    179 } cons_devs[] = {
    180 	{ "pc",		CONSDEV_PC },
    181 	{ "com0",	CONSDEV_COM0 },
    182 	{ "com1",	CONSDEV_COM1 },
    183 	{ "com2",	CONSDEV_COM2 },
    184 	{ "com3",	CONSDEV_COM3 },
    185 	{ "com0kbd",	CONSDEV_COM0KBD },
    186 	{ "com1kbd",	CONSDEV_COM1KBD },
    187 	{ "com2kbd",	CONSDEV_COM2KBD },
    188 	{ "com3kbd",	CONSDEV_COM3KBD },
    189 	{ "auto",	CONSDEV_AUTO },
    190 	{ 0, 0 } };
    191 
    192 void
    193 command_consdev(char *arg)
    194 {
    195 	const struct cons_devs *cdp;
    196 
    197 	for (cdp = cons_devs; cdp->name; cdp++) {
    198 		if (!strcmp(arg, cdp->name)) {
    199 			initio(cdp->tag);
    200 			print_banner();
    201 			return;
    202 		}
    203 	}
    204 	printf("invalid console device.\n");
    205 }
    206 void
    207 command_modules(char *arg)
    208 {
    209 	if (strcmp(arg, "enabled") == 0 ||
    210 			strcmp(arg, "on") == 0)
    211 		boot_modules_enabled = true;
    212 	else if (strcmp(arg, "disabled") == 0 ||
    213 			strcmp(arg, "off") == 0)
    214 		boot_modules_enabled = false;
    215 	else
    216 		printf("invalid flag, must be 'enabled' or 'disabled'.\n");
    217 }
    218 
    219 void
    220 command_load(char *arg)
    221 {
    222 	boot_module_t *bm, *bmp;
    223 	size_t len;
    224 	char *str;
    225 
    226 	bm = alloc(sizeof(boot_module_t));
    227 	len = strlen(arg) + 1;
    228 	str = alloc(len);
    229 	if (bm == NULL || str == NULL) {
    230 		printf("couldn't allocate module\n");
    231 		return;
    232 	}
    233 	memcpy(str, arg, len);
    234 	bm->bm_path = str;
    235 	bm->bm_next = NULL;
    236 	if (boot_modules == NULL)
    237 		boot_modules = bm;
    238 	else {
    239 		for (bmp = boot_modules; bmp->bm_next;
    240 				bmp = bmp->bm_next)
    241 			;
    242 		bmp->bm_next = bm;
    243 	}
    244 }
    245