Home | History | Annotate | Line # | Download | only in pxeboot
      1 /*	$NetBSD: main.c,v 1.35 2025/05/06 18:16:12 pgoyette 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 #include <lib/libsa/bootcfg.h>
     47 
     48 #include <libi386.h>
     49 #include <bootmenu.h>
     50 #include <bootmod.h>
     51 #include "pxeboot.h"
     52 #include "vbe.h"
     53 
     54 extern struct x86_boot_params boot_params;
     55 
     56 int errno;
     57 int debug;
     58 
     59 extern char	bootprog_name[], bootprog_rev[], bootprog_kernrev[];
     60 
     61 int	main(void);
     62 
     63 void	command_help(char *);
     64 void	command_quit(char *);
     65 void	command_boot(char *);
     66 void	command_consdev(char *);
     67 void	command_modules(char *);
     68 void	command_multiboot(char *);
     69 
     70 const struct bootblk_command commands[] = {
     71 	{ "help",	command_help },
     72 	{ "?",		command_help },
     73 	{ "quit",	command_quit },
     74 	{ "boot",	command_boot },
     75 	{ "consdev",	command_consdev },
     76 	{ "modules",    command_modules },
     77 	{ "multiboot",  command_multiboot },
     78 	{ "load",	module_add },
     79 	{ "vesa",	command_vesa },
     80 	{ "userconf",	userconf_add },
     81 	{ NULL,		NULL },
     82 };
     83 
     84 static void
     85 clearit(void)
     86 {
     87 
     88 	if (bootcfg_info.clear)
     89 		clear_pc_screen();
     90 }
     91 
     92 static void
     93 alldone(void)
     94 {
     95 	pxe_fini();
     96 	clearit();
     97 }
     98 
     99 static int
    100 bootit(const char *filename, int howto)
    101 {
    102 	if (exec_netbsd(filename, 0, howto, 0, alldone) < 0)
    103 		printf("boot: %s\n", strerror(errno));
    104 	else
    105 		printf("boot returned\n");
    106 	return (-1);
    107 }
    108 
    109 int
    110 main(void)
    111 {
    112 	extern char twiddle_toggle;
    113         char c;
    114 
    115 	twiddle_toggle = 1;	/* no twiddling until we're ready */
    116 
    117 #ifdef SUPPORT_SERIAL
    118 	initio(SUPPORT_SERIAL);
    119 #else
    120 	initio(CONSDEV_PC);
    121 #endif
    122 	gateA20();
    123 	boot_modules_enabled = !(boot_params.bp_flags
    124 				 & X86_BP_FLAGS_NOMODULES);
    125 
    126 #ifndef SMALL
    127 	if (!(boot_params.bp_flags & X86_BP_FLAGS_NOBOOTCONF)) {
    128 		parsebootconf(BOOTCFG_FILENAME);
    129 	} else {
    130 		bootcfg_info.timeout = boot_params.bp_timeout;
    131 	}
    132 
    133 	/*
    134 	 * If console set in boot.cfg, switch to it.
    135 	 * This will print the banner, so we don't need to explicitly do it
    136 	 */
    137 	if (bootcfg_info.consdev) {
    138 		command_consdev(bootcfg_info.consdev);
    139 	} else {
    140 		clearit();
    141 		print_bootcfg_banner(bootprog_name, bootprog_rev);
    142 	}
    143 
    144 	/* Display the menu, if applicable */
    145 	twiddle_toggle = 0;
    146 	if (bootcfg_info.nummenu > 0) {
    147 		/* Does not return */
    148 		doboottypemenu();
    149 	}
    150 #else
    151 	twiddle_toggle = 0;
    152 	clearit();
    153 	print_bootcfg_banner(bootprog_name, bootprog_rev);
    154 #endif
    155 
    156 	printf("Press return to boot now, any other key for boot menu\n");
    157 	printf("booting netbsd - starting in ");
    158 
    159 #ifdef SMALL
    160 	c = awaitkey(boot_params.bp_timeout, 1);
    161 #else
    162 	c = awaitkey((bootcfg_info.timeout < 0) ? 0 : bootcfg_info.timeout, 1);
    163 #endif
    164 	if ((c != '\r') && (c != '\n') && (c != '\0') &&
    165 	    ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0
    166 	     || check_password((char *)boot_params.bp_password))) {
    167 		printf("type \"?\" or \"help\" for help.\n");
    168 		bootmenu(); /* does not return */
    169 	}
    170 
    171 	/*
    172 	 * The file name provided here is just a default.  If the
    173 	 * DHCP server provides a file name, we'll use that instead.
    174 	 */
    175 	bootit("netbsd", 0);
    176 
    177 	/*
    178 	 * If that fails, let the BIOS try the next boot device.
    179 	 */
    180 	return (1);
    181 }
    182 
    183 /*
    184  * dummy function to satisfy link against call in
    185  * sys/arch/i386/stand/lib/bootmenu.c:do_bootcfg_command()
    186  */
    187 
    188 /* ARGSUSED */
    189 void
    190 command_dev(char *arg)
    191 {
    192 	(void)arg;
    193 	return;
    194 }
    195 
    196 /* ARGSUSED */
    197 void
    198 command_help(char *arg)
    199 {
    200 	printf("commands are:\n"
    201 	       "boot [filename] [-acdsqv]\n"
    202 	       "     (ex. \"netbsd.old -s\"\n"
    203 	       "consdev {pc|{com[0123]|com[0123]kbd|auto}[,{speed}]}\n"
    204 	       "vesa {modenum|on|off|enabled|disabled|list}\n"
    205 	       "multiboot [filename] [<args>]\n"
    206 	       "modules {on|off|enabled|disabled}\n"
    207 	       "load {path_to_module}\n"
    208 	       "userconf {command}\n"
    209 	       "help|?\n"
    210 	       "quit\n");
    211 }
    212 
    213 /* ARGSUSED */
    214 void
    215 command_quit(char *arg)
    216 {
    217 
    218 	printf("Exiting...\n");
    219 	delay(1000000);
    220 	reboot();
    221 	/* Note: we shouldn't get to this point! */
    222 	panic("Could not reboot!");
    223 }
    224 
    225 void
    226 command_boot(char *arg)
    227 {
    228 	char *filename;
    229 	int howto;
    230 
    231 	if (parseboot(arg, &filename, &howto))
    232 		bootit(filename, howto);
    233 }
    234 
    235 static const struct cons_devs {
    236     const char	*name;
    237     u_int	tag;
    238 } cons_devs[] = {
    239 	{ "pc",		CONSDEV_PC },
    240 	{ "com0",	CONSDEV_COM0 },
    241 	{ "com1",	CONSDEV_COM1 },
    242 	{ "com2",	CONSDEV_COM2 },
    243 	{ "com3",	CONSDEV_COM3 },
    244 	{ "com0kbd",	CONSDEV_COM0KBD },
    245 	{ "com1kbd",	CONSDEV_COM1KBD },
    246 	{ "com2kbd",	CONSDEV_COM2KBD },
    247 	{ "com3kbd",	CONSDEV_COM3KBD },
    248 	{ "auto",	CONSDEV_AUTO },
    249 	{ 0, 0 } };
    250 
    251 void
    252 command_consdev(char *arg)
    253 {
    254 	const struct cons_devs *cdp;
    255 	char *sep;
    256 	int speed;
    257 
    258 	sep = strchr(arg, ',');
    259 	if (sep != NULL)
    260 		*sep++ = '\0';
    261 
    262 	for (cdp = cons_devs; cdp->name; cdp++) {
    263 		if (strcmp(arg, cdp->name) != 0)
    264 			continue;
    265 
    266 		if (sep != NULL) {
    267 			if (cdp->tag == CONSDEV_PC)
    268 				goto error;
    269 
    270 			speed = atoi(sep);
    271 			if (speed < 0)
    272 				goto error;
    273 			boot_params.bp_conspeed = speed;
    274 		}
    275 
    276 		initio(cdp->tag);
    277 		clearit();
    278 		print_bootcfg_banner(bootprog_name, bootprog_rev);
    279 		return;
    280 	}
    281 error:
    282 	printf("invalid console device.\n");
    283 }
    284 
    285 void
    286 command_modules(char *arg)
    287 {
    288 	if (strcmp(arg, "enabled") == 0 ||
    289 			strcmp(arg, "on") == 0)
    290 		boot_modules_enabled = true;
    291 	else if (strcmp(arg, "disabled") == 0 ||
    292 			strcmp(arg, "off") == 0)
    293 		boot_modules_enabled = false;
    294 	else
    295 		printf("invalid flag, must be 'enabled' or 'disabled'.\n");
    296 }
    297 
    298 void
    299 command_multiboot(char *arg)
    300 {
    301 	char *filename;
    302 
    303 	filename = arg;
    304 	if (exec_multiboot(filename, gettrailer(arg)) < 0)
    305 		printf("multiboot: %s: %s\n", filename,
    306 		  strerror(errno));
    307 	else
    308 		printf("boot returned\n");
    309 }
    310