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