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