Home | History | Annotate | Line # | Download | only in pxeboot
main.c revision 1.22
      1 /*	$NetBSD: main.c,v 1.22 2009/12/13 23:01:42 jakllsch 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 <bootmenu.h>
     49 #include <bootmod.h>
     50 #include "pxeboot.h"
     51 #include "vbe.h"
     52 
     53 extern struct x86_boot_params boot_params;
     54 
     55 int errno;
     56 int debug;
     57 
     58 extern char	bootprog_name[], bootprog_rev[], bootprog_kernrev[];
     59 
     60 int	main(void);
     61 
     62 void	command_help(char *);
     63 void	command_quit(char *);
     64 void	command_boot(char *);
     65 void	command_consdev(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",	module_add },
     76 	{ "vesa",	command_vesa },
     77 	{ NULL,		NULL },
     78 };
     79 
     80 static void
     81 clearit(void)
     82 {
     83 
     84 	if (bootconf.clear)
     85 		clear_pc_screen();
     86 }
     87 
     88 static void
     89 alldone(void)
     90 {
     91 	pxe_fini();
     92 	clearit();
     93 }
     94 
     95 static int
     96 bootit(const char *filename, int howto)
     97 {
     98 	if (exec_netbsd(filename, 0, howto, 0, alldone) < 0)
     99 		printf("boot: %s\n", strerror(errno));
    100 	else
    101 		printf("boot returned\n");
    102 	return (-1);
    103 }
    104 
    105 static void
    106 print_banner(void)
    107 {
    108 	int base = getbasemem();
    109 	int ext = getextmem();
    110 
    111 	clearit();
    112 	printf("\n"
    113 	       ">> NetBSD/x86 PXE boot, Revision %s (from NetBSD %s)\n"
    114 	       ">> Memory: %d/%d k\n",
    115 	       bootprog_rev, bootprog_kernrev,
    116 	       base, ext);
    117 }
    118 
    119 int
    120 main(void)
    121 {
    122 	extern char twiddle_toggle;
    123         char c;
    124 
    125 	twiddle_toggle = 1;	/* no twiddling until we're ready */
    126 
    127 #ifdef SUPPORT_SERIAL
    128 	initio(SUPPORT_SERIAL);
    129 #else
    130 	initio(CONSDEV_PC);
    131 #endif
    132 	gateA20();
    133 
    134 #ifndef SMALL
    135 	parsebootconf(BOOTCONF);
    136 
    137 	/*
    138 	 * If console set in boot.cfg, switch to it.
    139 	 * This will print the banner, so we don't need to explicitly do it
    140 	 */
    141 	if (bootconf.consdev)
    142 		command_consdev(bootconf.consdev);
    143 	else
    144 		print_banner();
    145 
    146 	/* Display the menu, if applicable */
    147 	twiddle_toggle = 0;
    148 	if (bootconf.nummenu > 0) {
    149 		/* Does not return */
    150 		doboottypemenu();
    151 	}
    152 #else
    153 	twiddle_toggle = 0;
    154 	print_banner();
    155 #endif
    156 
    157 	printf("Press return to boot now, any other key for boot menu\n");
    158 	printf("booting netbsd - starting in ");
    159 
    160 #ifdef SMALL
    161 	c = awaitkey(boot_params.bp_timeout, 1);
    162 #else
    163 	c = awaitkey((bootconf.timeout < 0) ? 0 : bootconf.timeout, 1);
    164 #endif
    165 	if ((c != '\r') && (c != '\n') && (c != '\0') &&
    166 	    ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0
    167 	     || check_password((char *)boot_params.bp_password))) {
    168 		printf("type \"?\" or \"help\" for help.\n");
    169 		bootmenu(); /* does not return */
    170 	}
    171 
    172 	/*
    173 	 * The file name provided here is just a default.  If the
    174 	 * DHCP server provides a file name, we'll use that instead.
    175 	 */
    176 	bootit("netbsd", 0);
    177 
    178 	/*
    179 	 * If that fails, let the BIOS try the next boot device.
    180 	 */
    181 	return (1);
    182 }
    183 
    184 /* ARGSUSED */
    185 void
    186 command_help(char *arg)
    187 {
    188 	printf("commands are:\n"
    189 	       "boot [filename] [-adsqv]\n"
    190 	       "     (ex. \"netbsd.old -s\"\n"
    191 	       "consdev {pc|com[0123]|com[0123]kbd|auto}\n"
    192 	       "vesa {enabled|disabled|list|modenum}\n"
    193 	       "modules {enabled|disabled}\n"
    194 	       "load {path_to_module}\n"
    195 	       "help|?\n"
    196 	       "quit\n");
    197 }
    198 
    199 /* ARGSUSED */
    200 void
    201 command_quit(char *arg)
    202 {
    203 
    204 	printf("Exiting...\n");
    205 	delay(1000000);
    206 	reboot();
    207 	/* Note: we shouldn't get to this point! */
    208 	panic("Could not reboot!");
    209 	exit(0);
    210 }
    211 
    212 void
    213 command_boot(char *arg)
    214 {
    215 	char *filename;
    216 	int howto;
    217 
    218 	if (parseboot(arg, &filename, &howto))
    219 		bootit(filename, howto);
    220 }
    221 
    222 static const struct cons_devs {
    223     const char	*name;
    224     u_int	tag;
    225 } cons_devs[] = {
    226 	{ "pc",		CONSDEV_PC },
    227 	{ "com0",	CONSDEV_COM0 },
    228 	{ "com1",	CONSDEV_COM1 },
    229 	{ "com2",	CONSDEV_COM2 },
    230 	{ "com3",	CONSDEV_COM3 },
    231 	{ "com0kbd",	CONSDEV_COM0KBD },
    232 	{ "com1kbd",	CONSDEV_COM1KBD },
    233 	{ "com2kbd",	CONSDEV_COM2KBD },
    234 	{ "com3kbd",	CONSDEV_COM3KBD },
    235 	{ "auto",	CONSDEV_AUTO },
    236 	{ 0, 0 } };
    237 
    238 void
    239 command_consdev(char *arg)
    240 {
    241 	const struct cons_devs *cdp;
    242 
    243 	for (cdp = cons_devs; cdp->name; cdp++) {
    244 		if (!strcmp(arg, cdp->name)) {
    245 			initio(cdp->tag);
    246 			print_banner();
    247 			return;
    248 		}
    249 	}
    250 	printf("invalid console device.\n");
    251 }
    252 void
    253 command_modules(char *arg)
    254 {
    255 	if (strcmp(arg, "enabled") == 0 ||
    256 			strcmp(arg, "on") == 0)
    257 		boot_modules_enabled = true;
    258 	else if (strcmp(arg, "disabled") == 0 ||
    259 			strcmp(arg, "off") == 0)
    260 		boot_modules_enabled = false;
    261 	else
    262 		printf("invalid flag, must be 'enabled' or 'disabled'.\n");
    263 }
    264