xref: /netbsd-src/sys/arch/i386/stand/pxeboot/main.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /*	$NetBSD: main.c,v 1.32 2021/09/07 11:41:32 nia 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 /* ARGSUSED */
184 void
185 command_help(char *arg)
186 {
187 	printf("commands are:\n"
188 	       "boot [filename] [-acdsqv]\n"
189 	       "     (ex. \"netbsd.old -s\"\n"
190 	       "consdev {pc|com[0123]|com[0123]kbd|auto}\n"
191 	       "vesa {modenum|on|off|enabled|disabled|list}\n"
192 	       "multiboot [filename] [<args>]\n"
193 	       "modules {on|off|enabled|disabled}\n"
194 	       "load {path_to_module}\n"
195 	       "userconf {command}\n"
196 	       "help|?\n"
197 	       "quit\n");
198 }
199 
200 /* ARGSUSED */
201 void
202 command_quit(char *arg)
203 {
204 
205 	printf("Exiting...\n");
206 	delay(1000000);
207 	reboot();
208 	/* Note: we shouldn't get to this point! */
209 	panic("Could not reboot!");
210 }
211 
212 void
213 command_boot(char *arg)
214 {
215 	char *filename;
216 	int howto;
217 
218 	if (parseboot(arg, &filename, &howto))
219 		bootit(filename, howto);
220 }
221 
222 static const struct cons_devs {
223     const char	*name;
224     u_int	tag;
225 } cons_devs[] = {
226 	{ "pc",		CONSDEV_PC },
227 	{ "com0",	CONSDEV_COM0 },
228 	{ "com1",	CONSDEV_COM1 },
229 	{ "com2",	CONSDEV_COM2 },
230 	{ "com3",	CONSDEV_COM3 },
231 	{ "com0kbd",	CONSDEV_COM0KBD },
232 	{ "com1kbd",	CONSDEV_COM1KBD },
233 	{ "com2kbd",	CONSDEV_COM2KBD },
234 	{ "com3kbd",	CONSDEV_COM3KBD },
235 	{ "auto",	CONSDEV_AUTO },
236 	{ 0, 0 } };
237 
238 void
239 command_consdev(char *arg)
240 {
241 	const struct cons_devs *cdp;
242 
243 	for (cdp = cons_devs; cdp->name; cdp++) {
244 		if (!strcmp(arg, cdp->name)) {
245 			initio(cdp->tag);
246 			clearit();
247 			print_bootcfg_banner(bootprog_name, bootprog_rev);
248 			return;
249 		}
250 	}
251 	printf("invalid console device.\n");
252 }
253 void
254 command_modules(char *arg)
255 {
256 	if (strcmp(arg, "enabled") == 0 ||
257 			strcmp(arg, "on") == 0)
258 		boot_modules_enabled = true;
259 	else if (strcmp(arg, "disabled") == 0 ||
260 			strcmp(arg, "off") == 0)
261 		boot_modules_enabled = false;
262 	else
263 		printf("invalid flag, must be 'enabled' or 'disabled'.\n");
264 }
265 
266 void
267 command_multiboot(char *arg)
268 {
269 	char *filename;
270 
271 	filename = arg;
272 	if (exec_multiboot(filename, gettrailer(arg)) < 0)
273 		printf("multiboot: %s: %s\n", filename,
274 		  strerror(errno));
275 	else
276 		printf("boot returned\n");
277 }
278