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