xref: /netbsd-src/sys/arch/i386/stand/pxeboot/main.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: main.c,v 1.31 2014/06/28 09:16:18 rtr 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 static void
110 print_banner(void)
111 {
112 	int base = getbasemem();
113 	int ext = getextmem();
114 
115 	clearit();
116 	printf("\n"
117 	       ">> NetBSD/x86 PXE boot, Revision %s (from NetBSD %s)\n"
118 	       ">> Memory: %d/%d k\n",
119 	       bootprog_rev, bootprog_kernrev,
120 	       base, ext);
121 }
122 
123 int
124 main(void)
125 {
126 	extern char twiddle_toggle;
127         char c;
128 
129 	twiddle_toggle = 1;	/* no twiddling until we're ready */
130 
131 #ifdef SUPPORT_SERIAL
132 	initio(SUPPORT_SERIAL);
133 #else
134 	initio(CONSDEV_PC);
135 #endif
136 	gateA20();
137 	boot_modules_enabled = !(boot_params.bp_flags
138 				 & X86_BP_FLAGS_NOMODULES);
139 
140 #ifndef SMALL
141 	if (!(boot_params.bp_flags & X86_BP_FLAGS_NOBOOTCONF)) {
142 		parsebootconf(BOOTCFG_FILENAME);
143 	} else {
144 		bootcfg_info.timeout = boot_params.bp_timeout;
145 	}
146 
147 	/*
148 	 * If console set in boot.cfg, switch to it.
149 	 * This will print the banner, so we don't need to explicitly do it
150 	 */
151 	if (bootcfg_info.consdev)
152 		command_consdev(bootcfg_info.consdev);
153 	else
154 		print_banner();
155 
156 	/* Display the menu, if applicable */
157 	twiddle_toggle = 0;
158 	if (bootcfg_info.nummenu > 0) {
159 		/* Does not return */
160 		doboottypemenu();
161 	}
162 #else
163 	twiddle_toggle = 0;
164 	print_banner();
165 #endif
166 
167 	printf("Press return to boot now, any other key for boot menu\n");
168 	printf("booting netbsd - starting in ");
169 
170 #ifdef SMALL
171 	c = awaitkey(boot_params.bp_timeout, 1);
172 #else
173 	c = awaitkey((bootcfg_info.timeout < 0) ? 0 : bootcfg_info.timeout, 1);
174 #endif
175 	if ((c != '\r') && (c != '\n') && (c != '\0') &&
176 	    ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0
177 	     || check_password((char *)boot_params.bp_password))) {
178 		printf("type \"?\" or \"help\" for help.\n");
179 		bootmenu(); /* does not return */
180 	}
181 
182 	/*
183 	 * The file name provided here is just a default.  If the
184 	 * DHCP server provides a file name, we'll use that instead.
185 	 */
186 	bootit("netbsd", 0);
187 
188 	/*
189 	 * If that fails, let the BIOS try the next boot device.
190 	 */
191 	return (1);
192 }
193 
194 /* ARGSUSED */
195 void
196 command_help(char *arg)
197 {
198 	printf("commands are:\n"
199 	       "boot [filename] [-acdsqv]\n"
200 	       "     (ex. \"netbsd.old -s\"\n"
201 	       "consdev {pc|com[0123]|com[0123]kbd|auto}\n"
202 	       "vesa {modenum|on|off|enabled|disabled|list}\n"
203 	       "multiboot [filename] [<args>]\n"
204 	       "modules {on|off|enabled|disabled}\n"
205 	       "load {path_to_module}\n"
206 	       "userconf {command}\n"
207 	       "help|?\n"
208 	       "quit\n");
209 }
210 
211 /* ARGSUSED */
212 void
213 command_quit(char *arg)
214 {
215 
216 	printf("Exiting...\n");
217 	delay(1000000);
218 	reboot();
219 	/* Note: we shouldn't get to this point! */
220 	panic("Could not reboot!");
221 }
222 
223 void
224 command_boot(char *arg)
225 {
226 	char *filename;
227 	int howto;
228 
229 	if (parseboot(arg, &filename, &howto))
230 		bootit(filename, howto);
231 }
232 
233 static const struct cons_devs {
234     const char	*name;
235     u_int	tag;
236 } cons_devs[] = {
237 	{ "pc",		CONSDEV_PC },
238 	{ "com0",	CONSDEV_COM0 },
239 	{ "com1",	CONSDEV_COM1 },
240 	{ "com2",	CONSDEV_COM2 },
241 	{ "com3",	CONSDEV_COM3 },
242 	{ "com0kbd",	CONSDEV_COM0KBD },
243 	{ "com1kbd",	CONSDEV_COM1KBD },
244 	{ "com2kbd",	CONSDEV_COM2KBD },
245 	{ "com3kbd",	CONSDEV_COM3KBD },
246 	{ "auto",	CONSDEV_AUTO },
247 	{ 0, 0 } };
248 
249 void
250 command_consdev(char *arg)
251 {
252 	const struct cons_devs *cdp;
253 
254 	for (cdp = cons_devs; cdp->name; cdp++) {
255 		if (!strcmp(arg, cdp->name)) {
256 			initio(cdp->tag);
257 			print_banner();
258 			return;
259 		}
260 	}
261 	printf("invalid console device.\n");
262 }
263 void
264 command_modules(char *arg)
265 {
266 	if (strcmp(arg, "enabled") == 0 ||
267 			strcmp(arg, "on") == 0)
268 		boot_modules_enabled = true;
269 	else if (strcmp(arg, "disabled") == 0 ||
270 			strcmp(arg, "off") == 0)
271 		boot_modules_enabled = false;
272 	else
273 		printf("invalid flag, must be 'enabled' or 'disabled'.\n");
274 }
275 
276 void
277 command_multiboot(char *arg)
278 {
279 	char *filename;
280 
281 	filename = arg;
282 	if (exec_multiboot(filename, gettrailer(arg)) < 0)
283 		printf("multiboot: %s: %s\n", filename,
284 		  strerror(errno));
285 	else
286 		printf("boot returned\n");
287 }
288