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