xref: /netbsd-src/sys/arch/i386/stand/boot/boot2.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: boot2.c,v 1.60 2013/08/30 16:42:17 jmcneill Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Copyright (c) 2003
31  *	David Laight.  All rights reserved
32  * Copyright (c) 1996, 1997, 1999
33  * 	Matthias Drochner.  All rights reserved.
34  * Copyright (c) 1996, 1997
35  * 	Perry E. Metzger.  All rights reserved.
36  * Copyright (c) 1997
37  *	Jason R. Thorpe.  All rights reserved
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgements:
49  *	This product includes software developed for the NetBSD Project
50  *	by Matthias Drochner.
51  *	This product includes software developed for the NetBSD Project
52  *	by Perry E. Metzger.
53  * 4. The names of the authors may not be used to endorse or promote products
54  *    derived from this software without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
57  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
58  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
59  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
60  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
61  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
62  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
63  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
64  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
65  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66  */
67 
68 /* Based on stand/biosboot/main.c */
69 
70 #include <sys/types.h>
71 #include <sys/reboot.h>
72 #include <sys/bootblock.h>
73 
74 #include <lib/libsa/stand.h>
75 #include <lib/libsa/ufs.h>
76 #include <lib/libkern/libkern.h>
77 
78 #include <libi386.h>
79 #include <bootmod.h>
80 #include <bootmenu.h>
81 #include <vbe.h>
82 #include "devopen.h"
83 
84 #ifdef SUPPORT_PS2
85 #include <biosmca.h>
86 #endif
87 
88 extern struct x86_boot_params boot_params;
89 
90 extern	const char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
91 
92 int errno;
93 
94 int boot_biosdev;
95 daddr_t boot_biossector;
96 
97 static const char * const names[][2] = {
98 	{ "netbsd", "netbsd.gz" },
99 	{ "onetbsd", "onetbsd.gz" },
100 	{ "netbsd.old", "netbsd.old.gz" },
101 };
102 
103 #define NUMNAMES (sizeof(names)/sizeof(names[0]))
104 #define DEFFILENAME names[0][0]
105 
106 #define MAXDEVNAME 16
107 
108 static char *default_devname;
109 static int default_unit, default_partition;
110 static const char *default_filename;
111 
112 char *sprint_bootsel(const char *);
113 void bootit(const char *, int, int);
114 void print_banner(void);
115 void boot2(int, uint64_t);
116 
117 void	command_help(char *);
118 void	command_ls(char *);
119 void	command_quit(char *);
120 void	command_boot(char *);
121 void	command_dev(char *);
122 void	command_consdev(char *);
123 #ifndef SMALL
124 void	command_menu(char *);
125 #endif
126 void	command_modules(char *);
127 void	command_multiboot(char *);
128 
129 const struct bootblk_command commands[] = {
130 	{ "help",	command_help },
131 	{ "?",		command_help },
132 	{ "ls",		command_ls },
133 	{ "quit",	command_quit },
134 	{ "boot",	command_boot },
135 	{ "dev",	command_dev },
136 	{ "consdev",	command_consdev },
137 #ifndef SMALL
138 	{ "menu",	command_menu },
139 #endif
140 	{ "modules",	command_modules },
141 	{ "load",	module_add },
142 	{ "multiboot",	command_multiboot },
143 	{ "vesa",	command_vesa },
144 	{ "splash",	splash_add },
145 	{ "rndseed",	rnd_add },
146 	{ "fs",		fs_add },
147 	{ "userconf",	userconf_add },
148 	{ NULL,		NULL },
149 };
150 
151 int
152 parsebootfile(const char *fname, char **fsname, char **devname,
153 	      int *unit, int *partition, const char **file)
154 {
155 	const char *col;
156 
157 	*fsname = "ufs";
158 	*devname = default_devname;
159 	*unit = default_unit;
160 	*partition = default_partition;
161 	*file = default_filename;
162 
163 	if (fname == NULL)
164 		return 0;
165 
166 	if ((col = strchr(fname, ':')) != NULL) {	/* device given */
167 		static char savedevname[MAXDEVNAME+1];
168 		int devlen;
169 		int u = 0, p = 0;
170 		int i = 0;
171 
172 		devlen = col - fname;
173 		if (devlen > MAXDEVNAME)
174 			return EINVAL;
175 
176 #define isvalidname(c) ((c) >= 'a' && (c) <= 'z')
177 		if (!isvalidname(fname[i]))
178 			return EINVAL;
179 		do {
180 			savedevname[i] = fname[i];
181 			i++;
182 		} while (isvalidname(fname[i]));
183 		savedevname[i] = '\0';
184 
185 #define isnum(c) ((c) >= '0' && (c) <= '9')
186 		if (i < devlen) {
187 			if (!isnum(fname[i]))
188 				return EUNIT;
189 			do {
190 				u *= 10;
191 				u += fname[i++] - '0';
192 			} while (isnum(fname[i]));
193 		}
194 
195 #define isvalidpart(c) ((c) >= 'a' && (c) <= 'z')
196 		if (i < devlen) {
197 			if (!isvalidpart(fname[i]))
198 				return EPART;
199 			p = fname[i++] - 'a';
200 		}
201 
202 		if (i != devlen)
203 			return ENXIO;
204 
205 		*devname = savedevname;
206 		*unit = u;
207 		*partition = p;
208 		fname = col + 1;
209 	}
210 
211 	if (*fname)
212 		*file = fname;
213 
214 	return 0;
215 }
216 
217 char *
218 sprint_bootsel(const char *filename)
219 {
220 	char *fsname, *devname;
221 	int unit, partition;
222 	const char *file;
223 	static char buf[80];
224 
225 	if (parsebootfile(filename, &fsname, &devname, &unit,
226 			  &partition, &file) == 0) {
227 		sprintf(buf, "%s%d%c:%s", devname, unit, 'a' + partition, file);
228 		return buf;
229 	}
230 	return "(invalid)";
231 }
232 
233 static void
234 clearit(void)
235 {
236 
237 	if (bootconf.clear)
238 		clear_pc_screen();
239 }
240 
241 void
242 bootit(const char *filename, int howto, int tell)
243 {
244 
245 	if (tell) {
246 		printf("booting %s", sprint_bootsel(filename));
247 		if (howto)
248 			printf(" (howto 0x%x)", howto);
249 		printf("\n");
250 	}
251 
252 	if (exec_netbsd(filename, 0, howto, boot_biosdev < 0x80, clearit) < 0)
253 		printf("boot: %s: %s\n", sprint_bootsel(filename),
254 		       strerror(errno));
255 	else
256 		printf("boot returned\n");
257 }
258 
259 void
260 print_banner(void)
261 {
262 
263 	clearit();
264 #ifndef SMALL
265 	int n;
266 	if (bootconf.banner[0]) {
267 		for (n = 0; bootconf.banner[n] && n < MAXBANNER; n++)
268 			printf("%s\n", bootconf.banner[n]);
269 	} else {
270 #endif /* !SMALL */
271 		printf("\n"
272 		       ">> %s, Revision %s (from NetBSD %s)\n"
273 		       ">> Memory: %d/%d k\n",
274 		       bootprog_name, bootprog_rev, bootprog_kernrev,
275 		       getbasemem(), getextmem());
276 
277 #ifndef SMALL
278 	}
279 #endif /* !SMALL */
280 }
281 
282 /*
283  * Called from the initial entry point boot_start in biosboot.S
284  *
285  * biosdev: BIOS drive number the system booted from
286  * biossector: Sector number of the NetBSD partition
287  */
288 void
289 boot2(int biosdev, uint64_t biossector)
290 {
291 	extern char twiddle_toggle;
292 	int currname;
293 	char c;
294 
295 	twiddle_toggle = 1;	/* no twiddling until we're ready */
296 
297 	initio(boot_params.bp_consdev);
298 
299 #ifdef SUPPORT_PS2
300 	biosmca();
301 #endif
302 	gateA20();
303 
304 	boot_modules_enabled = !(boot_params.bp_flags
305 				 & X86_BP_FLAGS_NOMODULES);
306 	if (boot_params.bp_flags & X86_BP_FLAGS_RESET_VIDEO)
307 		biosvideomode();
308 
309 	vbe_init();
310 
311 	/* need to remember these */
312 	boot_biosdev = biosdev;
313 	boot_biossector = biossector;
314 
315 	/* try to set default device to what BIOS tells us */
316 	bios2dev(biosdev, biossector, &default_devname, &default_unit,
317 		 &default_partition);
318 
319 	/* if the user types "boot" without filename */
320 	default_filename = DEFFILENAME;
321 
322 #ifndef SMALL
323 	if (!(boot_params.bp_flags & X86_BP_FLAGS_NOBOOTCONF)) {
324 		parsebootconf(BOOTCONF);
325 	} else {
326 		bootconf.timeout = boot_params.bp_timeout;
327 	}
328 
329 
330 	/*
331 	 * If console set in boot.cfg, switch to it.
332 	 * This will print the banner, so we don't need to explicitly do it
333 	 */
334 	if (bootconf.consdev)
335 		command_consdev(bootconf.consdev);
336 	else
337 		print_banner();
338 
339 	/* Display the menu, if applicable */
340 	twiddle_toggle = 0;
341 	if (bootconf.nummenu > 0) {
342 		/* Does not return */
343 		doboottypemenu();
344 	}
345 
346 #else
347 	twiddle_toggle = 0;
348 	print_banner();
349 #endif
350 
351 	printf("Press return to boot now, any other key for boot menu\n");
352 	for (currname = 0; currname < NUMNAMES; currname++) {
353 		printf("booting %s - starting in ",
354 		       sprint_bootsel(names[currname][0]));
355 
356 #ifdef SMALL
357 		c = awaitkey(boot_params.bp_timeout, 1);
358 #else
359 		c = awaitkey((bootconf.timeout < 0) ? 0 : bootconf.timeout, 1);
360 #endif
361 		if ((c != '\r') && (c != '\n') && (c != '\0')) {
362 		    if ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0) {
363 			/* do NOT ask for password */
364 			bootmenu(); /* does not return */
365 		    } else {
366 			/* DO ask for password */
367 			if (check_password((char *)boot_params.bp_password)) {
368 			    /* password ok */
369 			    printf("type \"?\" or \"help\" for help.\n");
370 			    bootmenu(); /* does not return */
371 			} else {
372 			    /* bad password */
373 			    printf("Wrong password.\n");
374 			    currname = 0;
375 			    continue;
376 			}
377 		    }
378 		}
379 
380 		/*
381 		 * try pairs of names[] entries, foo and foo.gz
382 		 */
383 		/* don't print "booting..." again */
384 		bootit(names[currname][0], 0, 0);
385 		/* since it failed, try compressed bootfile. */
386 		bootit(names[currname][1], 0, 1);
387 	}
388 
389 	bootmenu();	/* does not return */
390 }
391 
392 /* ARGSUSED */
393 void
394 command_help(char *arg)
395 {
396 
397 	printf("commands are:\n"
398 	       "boot [xdNx:][filename] [-12acdqsvxz]\n"
399 	       "     (ex. \"hd0a:netbsd.old -s\"\n"
400 	       "ls [path]\n"
401 	       "dev xd[N[x]]:\n"
402 	       "consdev {pc|com[0123]|com[0123]kbd|auto}\n"
403 	       "vesa {modenum|on|off|enabled|disabled|list}\n"
404 #ifndef SMALL
405 	       "menu (reenters boot menu, if defined in boot.cfg)\n"
406 #endif
407 	       "modules {on|off|enabled|disabled}\n"
408 	       "load {path_to_module}\n"
409 	       "multiboot [xdNx:][filename] [<args>]\n"
410 	       "userconf {command}\n"
411 	       "rndseed {path_to_rndseed_file}\n"
412 	       "help|?\n"
413 	       "quit\n");
414 }
415 
416 void
417 command_ls(char *arg)
418 {
419 	const char *save = default_filename;
420 
421 	default_filename = "/";
422 	ls(arg);
423 	default_filename = save;
424 }
425 
426 /* ARGSUSED */
427 void
428 command_quit(char *arg)
429 {
430 
431 	printf("Exiting...\n");
432 	delay(1000000);
433 	reboot();
434 	/* Note: we shouldn't get to this point! */
435 	panic("Could not reboot!");
436 }
437 
438 void
439 command_boot(char *arg)
440 {
441 	char *filename;
442 	int howto, tell;
443 
444 	if (!parseboot(arg, &filename, &howto))
445 		return;
446 
447 	tell = ((howto & AB_VERBOSE) != 0);
448 	if (filename != NULL) {
449 		bootit(filename, howto, tell);
450 	} else {
451 		int i;
452 
453 #ifndef SMALL
454 		bootdefault();
455 #endif
456 		for (i = 0; i < NUMNAMES; i++) {
457 			bootit(names[i][0], howto, tell);
458 			bootit(names[i][1], howto, tell);
459 		}
460 	}
461 }
462 
463 void
464 command_dev(char *arg)
465 {
466 	static char savedevname[MAXDEVNAME + 1];
467 	char *fsname, *devname;
468 	const char *file; /* dummy */
469 
470 	if (*arg == '\0') {
471 		biosdisk_probe();
472 		printf("default %s%d%c\n", default_devname, default_unit,
473 		       'a' + default_partition);
474 		return;
475 	}
476 
477 	if (strchr(arg, ':') == NULL ||
478 	    parsebootfile(arg, &fsname, &devname, &default_unit,
479 			  &default_partition, &file)) {
480 		command_help(NULL);
481 		return;
482 	}
483 
484 	/* put to own static storage */
485 	strncpy(savedevname, devname, MAXDEVNAME + 1);
486 	default_devname = savedevname;
487 }
488 
489 static const struct cons_devs {
490 	const char	*name;
491 	u_int		tag;
492 } cons_devs[] = {
493 	{ "pc",		CONSDEV_PC },
494 	{ "com0",	CONSDEV_COM0 },
495 	{ "com1",	CONSDEV_COM1 },
496 	{ "com2",	CONSDEV_COM2 },
497 	{ "com3",	CONSDEV_COM3 },
498 	{ "com0kbd",	CONSDEV_COM0KBD },
499 	{ "com1kbd",	CONSDEV_COM1KBD },
500 	{ "com2kbd",	CONSDEV_COM2KBD },
501 	{ "com3kbd",	CONSDEV_COM3KBD },
502 	{ "auto",	CONSDEV_AUTO },
503 	{ NULL,		0 }
504 };
505 
506 void
507 command_consdev(char *arg)
508 {
509 	const struct cons_devs *cdp;
510 
511 	for (cdp = cons_devs; cdp->name; cdp++) {
512 		if (strcmp(arg, cdp->name) == 0) {
513 			initio(cdp->tag);
514 			print_banner();
515 			return;
516 		}
517 	}
518 	printf("invalid console device.\n");
519 }
520 
521 #ifndef SMALL
522 /* ARGSUSED */
523 void
524 command_menu(char *arg)
525 {
526 
527 	if (bootconf.nummenu > 0) {
528 		/* Does not return */
529 		doboottypemenu();
530 	} else {
531 		printf("No menu defined in boot.cfg\n");
532 	}
533 }
534 #endif /* !SMALL */
535 
536 void
537 command_modules(char *arg)
538 {
539 
540 	if (strcmp(arg, "enabled") == 0 ||
541 	    strcmp(arg, "on") == 0)
542 		boot_modules_enabled = true;
543 	else if (strcmp(arg, "disabled") == 0 ||
544 	    strcmp(arg, "off") == 0)
545 		boot_modules_enabled = false;
546 	else
547 		printf("invalid flag, must be 'enabled' or 'disabled'.\n");
548 }
549 
550 void
551 command_multiboot(char *arg)
552 {
553 	char *filename;
554 
555 	filename = arg;
556 	if (exec_multiboot(filename, gettrailer(arg)) < 0)
557 		printf("multiboot: %s: %s\n", sprint_bootsel(filename),
558 		       strerror(errno));
559 	else
560 		printf("boot returned\n");
561 }
562 
563