Home | History | Annotate | Line # | Download | only in pxeboot
main.c revision 1.12.10.3
      1  1.12.10.3     yamt /*	$NetBSD: main.c,v 1.12.10.3 2009/09/16 13:37:39 yamt Exp $	*/
      2        1.1  thorpej 
      3        1.1  thorpej /*
      4        1.1  thorpej  * Copyright (c) 1996
      5        1.1  thorpej  * 	Matthias Drochner.  All rights reserved.
      6        1.1  thorpej  * Copyright (c) 1996
      7        1.1  thorpej  * 	Perry E. Metzger.  All rights reserved.
      8        1.1  thorpej  *
      9        1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     10        1.1  thorpej  * modification, are permitted provided that the following conditions
     11        1.1  thorpej  * are met:
     12        1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     13        1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     14        1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15        1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     16        1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     17        1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     18        1.1  thorpej  *    must display the following acknowledgements:
     19        1.1  thorpej  *	This product includes software developed for the NetBSD Project
     20        1.1  thorpej  *	by Matthias Drochner.
     21        1.1  thorpej  *	This product includes software developed for the NetBSD Project
     22        1.1  thorpej  *	by Perry E. Metzger.
     23        1.1  thorpej  * 4. The names of the authors may not be used to endorse or promote products
     24        1.1  thorpej  *    derived from this software without specific prior written permission.
     25        1.1  thorpej  *
     26        1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     27        1.1  thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     28        1.1  thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     29        1.1  thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     30        1.1  thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     31        1.1  thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     32        1.1  thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     33        1.1  thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     34        1.1  thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     35        1.1  thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     36        1.1  thorpej  *
     37        1.1  thorpej  */
     38        1.1  thorpej 
     39       1.10      dsl #include <sys/types.h>
     40       1.10      dsl #include <sys/reboot.h>
     41       1.10      dsl #include <sys/bootblock.h>
     42       1.10      dsl 
     43        1.1  thorpej #include <lib/libkern/libkern.h>
     44        1.1  thorpej 
     45        1.1  thorpej #include <lib/libsa/stand.h>
     46        1.1  thorpej 
     47        1.1  thorpej #include <libi386.h>
     48  1.12.10.2     yamt #include <bootmenu.h>
     49  1.12.10.2     yamt #include <bootmod.h>
     50        1.1  thorpej #include "pxeboot.h"
     51  1.12.10.3     yamt #include "vbe.h"
     52        1.1  thorpej 
     53       1.10      dsl extern struct x86_boot_params boot_params;
     54       1.10      dsl 
     55        1.1  thorpej int errno;
     56        1.1  thorpej int debug;
     57        1.1  thorpej 
     58  1.12.10.2     yamt extern char	bootprog_name[], bootprog_rev[], bootprog_kernrev[];
     59        1.1  thorpej 
     60        1.1  thorpej int	main(void);
     61        1.1  thorpej 
     62  1.12.10.2     yamt void	command_help(char *);
     63  1.12.10.2     yamt void	command_quit(char *);
     64  1.12.10.2     yamt void	command_boot(char *);
     65        1.9      chs void	command_consdev(char *);
     66  1.12.10.1     yamt void	command_modules(char *);
     67        1.1  thorpej 
     68        1.1  thorpej const struct bootblk_command commands[] = {
     69        1.1  thorpej 	{ "help",	command_help },
     70        1.1  thorpej 	{ "?",		command_help },
     71        1.1  thorpej 	{ "quit",	command_quit },
     72        1.1  thorpej 	{ "boot",	command_boot },
     73        1.9      chs 	{ "consdev",	command_consdev },
     74  1.12.10.1     yamt 	{ "modules",    command_modules },
     75  1.12.10.2     yamt 	{ "load",	module_add },
     76  1.12.10.3     yamt 	{ "vesa",	command_vesa },
     77        1.1  thorpej 	{ NULL,		NULL },
     78        1.1  thorpej };
     79        1.1  thorpej 
     80  1.12.10.2     yamt static void
     81  1.12.10.2     yamt clearit(void)
     82  1.12.10.2     yamt {
     83  1.12.10.2     yamt 
     84  1.12.10.2     yamt 	if (bootconf.clear)
     85  1.12.10.2     yamt 		clear_pc_screen();
     86  1.12.10.2     yamt }
     87  1.12.10.2     yamt 
     88        1.1  thorpej static int
     89        1.1  thorpej bootit(const char *filename, int howto)
     90        1.1  thorpej {
     91  1.12.10.2     yamt 	if (exec_netbsd(filename, 0, howto, 0, clearit) < 0)
     92        1.1  thorpej 		printf("boot: %s\n", strerror(errno));
     93        1.1  thorpej 	else
     94        1.1  thorpej 		printf("boot returned\n");
     95        1.1  thorpej 	return (-1);
     96        1.1  thorpej }
     97        1.1  thorpej 
     98        1.1  thorpej static void
     99        1.1  thorpej print_banner(void)
    100        1.1  thorpej {
    101        1.1  thorpej 	int base = getbasemem();
    102        1.1  thorpej 	int ext = getextmem();
    103        1.1  thorpej 
    104  1.12.10.2     yamt 	clearit();
    105        1.1  thorpej 	printf("\n"
    106  1.12.10.2     yamt 	       ">> NetBSD/x86 PXE boot, Revision %s (from NetBSD %s)\n"
    107        1.1  thorpej 	       ">> Memory: %d/%d k\n",
    108  1.12.10.2     yamt 	       bootprog_rev, bootprog_kernrev,
    109        1.1  thorpej 	       base, ext);
    110        1.1  thorpej }
    111        1.1  thorpej 
    112        1.1  thorpej int
    113        1.1  thorpej main(void)
    114        1.1  thorpej {
    115  1.12.10.2     yamt 	extern char twiddle_toggle;
    116        1.1  thorpej         char c;
    117        1.1  thorpej 
    118  1.12.10.2     yamt 	twiddle_toggle = 1;	/* no twiddling until we're ready */
    119  1.12.10.2     yamt 
    120        1.4  thorpej #ifdef SUPPORT_SERIAL
    121        1.4  thorpej 	initio(SUPPORT_SERIAL);
    122        1.4  thorpej #else
    123        1.4  thorpej 	initio(CONSDEV_PC);
    124        1.4  thorpej #endif
    125        1.1  thorpej 	gateA20();
    126        1.1  thorpej 
    127  1.12.10.2     yamt #ifndef SMALL
    128  1.12.10.2     yamt 	parsebootconf(BOOTCONF);
    129  1.12.10.2     yamt 
    130  1.12.10.2     yamt 	/*
    131  1.12.10.2     yamt 	 * If console set in boot.cfg, switch to it.
    132  1.12.10.2     yamt 	 * This will print the banner, so we don't need to explicitly do it
    133  1.12.10.2     yamt 	 */
    134  1.12.10.2     yamt 	if (bootconf.consdev)
    135  1.12.10.2     yamt 		command_consdev(bootconf.consdev);
    136  1.12.10.2     yamt 	else
    137  1.12.10.2     yamt 		print_banner();
    138  1.12.10.2     yamt 
    139  1.12.10.2     yamt 	/* Display the menu, if applicable */
    140  1.12.10.2     yamt 	twiddle_toggle = 0;
    141  1.12.10.2     yamt 	if (bootconf.nummenu > 0) {
    142  1.12.10.2     yamt 		/* Does not return */
    143  1.12.10.2     yamt 		doboottypemenu();
    144  1.12.10.2     yamt 	}
    145  1.12.10.2     yamt #else
    146  1.12.10.2     yamt 	twiddle_toggle = 0;
    147        1.1  thorpej 	print_banner();
    148  1.12.10.2     yamt #endif
    149        1.1  thorpej 
    150        1.1  thorpej 	printf("Press return to boot now, any other key for boot menu\n");
    151  1.12.10.2     yamt 	printf("booting netbsd - starting in ");
    152        1.1  thorpej 
    153  1.12.10.2     yamt #ifdef SMALL
    154       1.11      dsl 	c = awaitkey(boot_params.bp_timeout, 1);
    155  1.12.10.2     yamt #else
    156  1.12.10.2     yamt 	c = awaitkey((bootconf.timeout < 0) ? 0 : bootconf.timeout, 1);
    157  1.12.10.2     yamt #endif
    158  1.12.10.2     yamt 	if ((c != '\r') && (c != '\n') && (c != '\0') &&
    159  1.12.10.2     yamt 	    ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0
    160  1.12.10.2     yamt 	     || check_password((char *)boot_params.bp_password))) {
    161        1.1  thorpej 		printf("type \"?\" or \"help\" for help.\n");
    162  1.12.10.2     yamt 		bootmenu(); /* does not return */
    163        1.1  thorpej 	}
    164        1.1  thorpej 
    165        1.3  thorpej 	/*
    166        1.3  thorpej 	 * The file name provided here is just a default.  If the
    167        1.3  thorpej 	 * DHCP server provides a file name, we'll use that instead.
    168        1.3  thorpej 	 */
    169        1.1  thorpej 	bootit("netbsd", 0);
    170        1.1  thorpej 
    171        1.1  thorpej 	/*
    172        1.1  thorpej 	 * If that fails, let the BIOS try the next boot device.
    173        1.1  thorpej 	 */
    174        1.1  thorpej 	return (1);
    175        1.1  thorpej }
    176        1.1  thorpej 
    177        1.1  thorpej /* ARGSUSED */
    178        1.1  thorpej void
    179        1.1  thorpej command_help(char *arg)
    180        1.1  thorpej {
    181        1.1  thorpej 	printf("commands are:\n"
    182        1.1  thorpej 	       "boot [filename] [-adsqv]\n"
    183        1.1  thorpej 	       "     (ex. \"netbsd.old -s\"\n"
    184        1.9      chs 	       "consdev {pc|com[0123]|com[0123]kbd|auto}\n"
    185  1.12.10.3     yamt 	       "vesa {enabled|disabled|list|modenum}\n"
    186  1.12.10.1     yamt 	       "modules {enabled|disabled}\n"
    187  1.12.10.1     yamt 	       "load {path_to_module}\n"
    188        1.1  thorpej 	       "help|?\n"
    189        1.1  thorpej 	       "quit\n");
    190        1.1  thorpej }
    191        1.1  thorpej 
    192        1.1  thorpej /* ARGSUSED */
    193        1.1  thorpej void
    194        1.1  thorpej command_quit(char *arg)
    195        1.1  thorpej {
    196        1.9      chs 
    197        1.9      chs 	printf("Exiting...\n");
    198        1.9      chs 	delay(1000000);
    199        1.9      chs 	reboot();
    200        1.9      chs 	/* Note: we shouldn't get to this point! */
    201        1.9      chs 	panic("Could not reboot!");
    202        1.1  thorpej 	exit(0);
    203        1.1  thorpej }
    204        1.1  thorpej 
    205        1.1  thorpej void
    206        1.1  thorpej command_boot(char *arg)
    207        1.1  thorpej {
    208        1.1  thorpej 	char *filename;
    209        1.1  thorpej 	int howto;
    210        1.1  thorpej 
    211        1.1  thorpej 	if (parseboot(arg, &filename, &howto))
    212        1.1  thorpej 		bootit(filename, howto);
    213        1.1  thorpej }
    214        1.9      chs 
    215        1.9      chs static const struct cons_devs {
    216        1.9      chs     const char	*name;
    217        1.9      chs     u_int	tag;
    218        1.9      chs } cons_devs[] = {
    219        1.9      chs 	{ "pc",		CONSDEV_PC },
    220        1.9      chs 	{ "com0",	CONSDEV_COM0 },
    221        1.9      chs 	{ "com1",	CONSDEV_COM1 },
    222        1.9      chs 	{ "com2",	CONSDEV_COM2 },
    223        1.9      chs 	{ "com3",	CONSDEV_COM3 },
    224        1.9      chs 	{ "com0kbd",	CONSDEV_COM0KBD },
    225        1.9      chs 	{ "com1kbd",	CONSDEV_COM1KBD },
    226        1.9      chs 	{ "com2kbd",	CONSDEV_COM2KBD },
    227        1.9      chs 	{ "com3kbd",	CONSDEV_COM3KBD },
    228        1.9      chs 	{ "auto",	CONSDEV_AUTO },
    229        1.9      chs 	{ 0, 0 } };
    230        1.9      chs 
    231        1.9      chs void
    232        1.9      chs command_consdev(char *arg)
    233        1.9      chs {
    234        1.9      chs 	const struct cons_devs *cdp;
    235        1.9      chs 
    236        1.9      chs 	for (cdp = cons_devs; cdp->name; cdp++) {
    237        1.9      chs 		if (!strcmp(arg, cdp->name)) {
    238        1.9      chs 			initio(cdp->tag);
    239        1.9      chs 			print_banner();
    240        1.9      chs 			return;
    241        1.9      chs 		}
    242        1.9      chs 	}
    243        1.9      chs 	printf("invalid console device.\n");
    244        1.9      chs }
    245  1.12.10.1     yamt void
    246  1.12.10.1     yamt command_modules(char *arg)
    247  1.12.10.1     yamt {
    248  1.12.10.1     yamt 	if (strcmp(arg, "enabled") == 0 ||
    249  1.12.10.1     yamt 			strcmp(arg, "on") == 0)
    250  1.12.10.1     yamt 		boot_modules_enabled = true;
    251  1.12.10.1     yamt 	else if (strcmp(arg, "disabled") == 0 ||
    252  1.12.10.1     yamt 			strcmp(arg, "off") == 0)
    253  1.12.10.1     yamt 		boot_modules_enabled = false;
    254  1.12.10.1     yamt 	else
    255  1.12.10.1     yamt 		printf("invalid flag, must be 'enabled' or 'disabled'.\n");
    256  1.12.10.1     yamt }
    257