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