Home | History | Annotate | Line # | Download | only in efiboot
boot.c revision 1.2
      1 /*	$NetBSD: boot.c,v 1.2 2017/02/03 16:42:26 roy Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2016 Kimihiro Nonaka <nonaka (at) netbsd.org>
      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 REGENTS AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include "efiboot.h"
     30 
     31 #include <sys/bootblock.h>
     32 #include <sys/boot_flag.h>
     33 
     34 #include <lib/libsa/bootcfg.h>
     35 
     36 #include <bootmod.h>
     37 #include <bootmenu.h>
     38 #include "devopen.h"
     39 
     40 int errno;
     41 int boot_biosdev;
     42 daddr_t boot_biossector;
     43 
     44 extern const char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
     45 
     46 extern struct x86_boot_params boot_params;
     47 extern char twiddle_toggle;
     48 
     49 static const char * const names[][2] = {
     50 	{ "netbsd", "netbsd.gz" },
     51 	{ "onetbsd", "onetbsd.gz" },
     52 	{ "netbsd.old", "netbsd.old.gz" },
     53 };
     54 
     55 #define NUMNAMES	__arraycount(names)
     56 #define DEFFILENAME	names[0][0]
     57 
     58 #define	MAXDEVNAME	16
     59 
     60 void	command_help(char *);
     61 void	command_quit(char *);
     62 void	command_boot(char *);
     63 void	command_consdev(char *);
     64 void	command_dev(char *);
     65 void	command_devpath(char *);
     66 void	command_efivar(char *);
     67 void	command_gop(char *);
     68 #if LIBSA_ENABLE_LS_OP
     69 void	command_ls(char *);
     70 #endif
     71 void	command_memmap(char *);
     72 #ifndef SMALL
     73 void	command_menu(char *);
     74 #endif
     75 void	command_modules(char *);
     76 void	command_multiboot(char *);
     77 void	command_text(char *);
     78 void	command_version(char *);
     79 
     80 const struct bootblk_command commands[] = {
     81 	{ "help",	command_help },
     82 	{ "?",		command_help },
     83 	{ "quit",	command_quit },
     84 	{ "boot",	command_boot },
     85 	{ "consdev",	command_consdev },
     86 	{ "dev",	command_dev },
     87 	{ "devpath",	command_devpath },
     88 	{ "efivar",	command_efivar },
     89 	{ "fs",		fs_add },
     90 	{ "gop",	command_gop },
     91 	{ "load",	module_add },
     92 #if LIBSA_ENABLE_LS_OP
     93 	{ "ls",		command_ls },
     94 #endif
     95 	{ "memmap",	command_memmap },
     96 #ifndef SMALL
     97 	{ "menu",	command_menu },
     98 #endif
     99 	{ "modules",	command_modules },
    100 	{ "multiboot",	command_multiboot },
    101 	{ "rndseed",	rnd_add },
    102 	{ "splash",	splash_add },
    103 	{ "text",	command_text },
    104 	{ "userconf",	userconf_add },
    105 	{ "version",	command_version },
    106 	{ NULL,		NULL },
    107 };
    108 
    109 static char *default_devname;
    110 static int default_unit, default_partition;
    111 static const char *default_filename;
    112 
    113 static char *sprint_bootsel(const char *);
    114 static void bootit(const char *, int);
    115 
    116 int
    117 parsebootfile(const char *fname, char **fsname, char **devname, int *unit,
    118     int *partition, const char **file)
    119 {
    120 	const char *col;
    121 
    122 	*fsname = "ufs";
    123 	*devname = default_devname;
    124 	*unit = default_unit;
    125 	*partition = default_partition;
    126 	*file = default_filename;
    127 
    128 	if (fname == NULL)
    129 		return 0;
    130 
    131 	if ((col = strchr(fname, ':')) != NULL) {	/* device given */
    132 		static char savedevname[MAXDEVNAME+1];
    133 		int devlen;
    134 		int u = 0, p = 0;
    135 		int i = 0;
    136 
    137 		devlen = col - fname;
    138 		if (devlen > MAXDEVNAME)
    139 			return EINVAL;
    140 
    141 #define isvalidname(c) ((c) >= 'a' && (c) <= 'z')
    142 		if (!isvalidname(fname[i]))
    143 			return EINVAL;
    144 		do {
    145 			savedevname[i] = fname[i];
    146 			i++;
    147 		} while (isvalidname(fname[i]));
    148 		savedevname[i] = '\0';
    149 
    150 #define isnum(c) ((c) >= '0' && (c) <= '9')
    151 		if (i < devlen) {
    152 			if (!isnum(fname[i]))
    153 				return EUNIT;
    154 			do {
    155 				u *= 10;
    156 				u += fname[i++] - '0';
    157 			} while (isnum(fname[i]));
    158 		}
    159 
    160 #define isvalidpart(c) ((c) >= 'a' && (c) <= 'z')
    161 		if (i < devlen) {
    162 			if (!isvalidpart(fname[i]))
    163 				return EPART;
    164 			p = fname[i++] - 'a';
    165 		}
    166 
    167 		if (i != devlen)
    168 			return ENXIO;
    169 
    170 		*devname = savedevname;
    171 		*unit = u;
    172 		*partition = p;
    173 		fname = col + 1;
    174 	}
    175 
    176 	if (*fname)
    177 		*file = fname;
    178 
    179 	return 0;
    180 }
    181 
    182 static char *
    183 sprint_bootsel(const char *filename)
    184 {
    185 	char *fsname, *devname;
    186 	int unit, partition;
    187 	const char *file;
    188 	static char buf[80];
    189 
    190 	if (parsebootfile(filename, &fsname, &devname, &unit,
    191 			  &partition, &file) == 0) {
    192 		snprintf(buf, sizeof(buf), "%s%d%c:%s", devname, unit,
    193 		    'a' + partition, file);
    194 		return buf;
    195 	}
    196 	return "(invalid)";
    197 }
    198 
    199 void
    200 clearit(void)
    201 {
    202 
    203 	if (bootcfg_info.clear)
    204 		clear_pc_screen();
    205 }
    206 
    207 static void
    208 bootit(const char *filename, int howto)
    209 {
    210 
    211 	if (howto & AB_VERBOSE)
    212 		printf("booting %s (howto 0x%x)\n", sprint_bootsel(filename),
    213 		    howto);
    214 
    215 	if (exec_netbsd(filename, 0, howto, 0, efi_cleanup) < 0)
    216 		printf("boot: %s: %s\n", sprint_bootsel(filename),
    217 		       strerror(errno));
    218 	else
    219 		printf("boot returned\n");
    220 }
    221 
    222 void
    223 print_banner(void)
    224 {
    225 	int n;
    226 
    227 	clearit();
    228 	if (bootcfg_info.banner[0]) {
    229 		for (n = 0; n < BOOTCFG_MAXBANNER && bootcfg_info.banner[n];
    230 		    n++)
    231 			printf("%s\n", bootcfg_info.banner[n]);
    232 	} else
    233 		command_version("short");
    234 }
    235 
    236 void
    237 boot(void)
    238 {
    239 	int currname;
    240 	int c;
    241 
    242 	boot_modules_enabled = !(boot_params.bp_flags & X86_BP_FLAGS_NOMODULES);
    243 
    244 	/* try to set default device to what BIOS tells us */
    245 	bios2dev(boot_biosdev, boot_biossector, &default_devname, &default_unit,
    246 	    &default_partition);
    247 
    248 	/* if the user types "boot" without filename */
    249 	default_filename = DEFFILENAME;
    250 
    251 	if (!(boot_params.bp_flags & X86_BP_FLAGS_NOBOOTCONF)) {
    252 		parsebootconf(BOOTCFG_FILENAME);
    253 	} else {
    254 		bootcfg_info.timeout = boot_params.bp_timeout;
    255 	}
    256 
    257 	/*
    258 	 * If console set in boot.cfg, switch to it.
    259 	 * This will print the banner, so we don't need to explicitly do it
    260 	 */
    261 	if (bootcfg_info.consdev)
    262 		command_consdev(bootcfg_info.consdev);
    263 	else
    264 		print_banner();
    265 
    266 	/* Display the menu, if applicable */
    267 	twiddle_toggle = 0;
    268 	if (bootcfg_info.nummenu > 0) {
    269 		/* Does not return */
    270 		doboottypemenu();
    271 	}
    272 
    273 	printf("Press return to boot now, any other key for boot menu\n");
    274 	for (currname = 0; currname < NUMNAMES; currname++) {
    275 		printf("booting %s - starting in ",
    276 		       sprint_bootsel(names[currname][0]));
    277 
    278 		c = awaitkey((bootcfg_info.timeout < 0) ? 0
    279 		    : bootcfg_info.timeout, 1);
    280 		if ((c != '\r') && (c != '\n') && (c != '\0')) {
    281 		    if ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0) {
    282 			/* do NOT ask for password */
    283 			bootmenu(); /* does not return */
    284 		    } else {
    285 			/* DO ask for password */
    286 			if (check_password((char *)boot_params.bp_password)) {
    287 			    /* password ok */
    288 			    printf("type \"?\" or \"help\" for help.\n");
    289 			    bootmenu(); /* does not return */
    290 			} else {
    291 			    /* bad password */
    292 			    printf("Wrong password.\n");
    293 			    currname = 0;
    294 			    continue;
    295 			}
    296 		    }
    297 		}
    298 
    299 		/*
    300 		 * try pairs of names[] entries, foo and foo.gz
    301 		 */
    302 		/* don't print "booting..." again */
    303 		bootit(names[currname][0], 0);
    304 		/* since it failed, try compressed bootfile. */
    305 		bootit(names[currname][1], AB_VERBOSE);
    306 	}
    307 
    308 	bootmenu();	/* does not return */
    309 }
    310 
    311 /* ARGSUSED */
    312 void
    313 command_help(char *arg)
    314 {
    315 
    316 	printf("commands are:\n"
    317 	       "boot [xdNx:][filename] [-12acdqsvxz]\n"
    318 	       "     (ex. \"hd0a:netbsd.old -s\"\n"
    319 	       "dev [xd[N[x]]:]\n"
    320 	       "consdev {pc|com[0123]|com[0123]kbd|auto}\n"
    321 	       "devpath\n"
    322 	       "efivar\n"
    323 	       "gop [{modenum|list}]\n"
    324 	       "load {path_to_module}\n"
    325 #if LIBSA_ENABLE_LS_OP
    326 	       "ls [path]\n"
    327 #endif
    328 	       "memmap [{sorted|unsorted}]\n"
    329 #ifndef SMALL
    330 	       "menu (reenters boot menu, if defined in boot.cfg)\n"
    331 #endif
    332 	       "modules {on|off|enabled|disabled}\n"
    333 	       "multiboot [xdNx:][filename] [<args>]\n"
    334 	       "rndseed {path_to_rndseed_file}\n"
    335 	       "splash {path_to_image_file}\n"
    336 	       "text [{modenum|list}]\n"
    337 	       "userconf {command}\n"
    338 	       "version\n"
    339 	       "help|?\n"
    340 	       "quit\n");
    341 }
    342 
    343 #if LIBSA_ENABLE_LS_OP
    344 void
    345 command_ls(char *arg)
    346 {
    347 	const char *save = default_filename;
    348 
    349 	default_filename = "/";
    350 	ls(arg);
    351 	default_filename = save;
    352 }
    353 #endif
    354 
    355 /* ARGSUSED */
    356 void
    357 command_quit(char *arg)
    358 {
    359 
    360 	printf("Exiting...\n");
    361 	delay(1 * 1000 * 1000);
    362 	reboot();
    363 	/* Note: we shouldn't get to this point! */
    364 	panic("Could not reboot!");
    365 }
    366 
    367 void
    368 command_boot(char *arg)
    369 {
    370 	char *filename;
    371 	int howto;
    372 
    373 	if (!parseboot(arg, &filename, &howto))
    374 		return;
    375 
    376 	if (filename != NULL) {
    377 		bootit(filename, howto);
    378 	} else {
    379 		int i;
    380 
    381 		if (howto == 0)
    382 			bootdefault();
    383 		for (i = 0; i < NUMNAMES; i++) {
    384 			bootit(names[i][0], howto);
    385 			bootit(names[i][1], howto);
    386 		}
    387 	}
    388 }
    389 
    390 void
    391 command_dev(char *arg)
    392 {
    393 	static char savedevname[MAXDEVNAME + 1];
    394 	char *fsname, *devname;
    395 	const char *file; /* dummy */
    396 
    397 	if (*arg == '\0') {
    398 		biosdisk_probe();
    399 		printf("default %s%d%c\n", default_devname, default_unit,
    400 		       'a' + default_partition);
    401 		return;
    402 	}
    403 
    404 	if (strchr(arg, ':') == NULL ||
    405 	    parsebootfile(arg, &fsname, &devname, &default_unit,
    406 	      &default_partition, &file)) {
    407 		command_help(NULL);
    408 		return;
    409 	}
    410 
    411 	/* put to own static storage */
    412 	strncpy(savedevname, devname, MAXDEVNAME + 1);
    413 	default_devname = savedevname;
    414 }
    415 
    416 /* ARGSUSED */
    417 void
    418 command_consdev(char *arg)
    419 {
    420 
    421 	/* XXX not implemented yet */
    422 }
    423 
    424 #ifndef SMALL
    425 /* ARGSUSED */
    426 void
    427 command_menu(char *arg)
    428 {
    429 
    430 	if (bootcfg_info.nummenu > 0) {
    431 		/* Does not return */
    432 		doboottypemenu();
    433 	} else
    434 		printf("No menu defined in boot.cfg\n");
    435 }
    436 #endif /* !SMALL */
    437 
    438 void
    439 command_modules(char *arg)
    440 {
    441 
    442 	if (strcmp(arg, "enabled") == 0 ||
    443 	    strcmp(arg, "on") == 0)
    444 		boot_modules_enabled = true;
    445 	else if (strcmp(arg, "disabled") == 0 ||
    446 	    strcmp(arg, "off") == 0)
    447 		boot_modules_enabled = false;
    448 	else
    449 		printf("invalid flag, must be 'enabled' or 'disabled'.\n");
    450 }
    451 
    452 void
    453 command_multiboot(char *arg)
    454 {
    455 	char *filename;
    456 
    457 	filename = arg;
    458 	if (exec_multiboot(filename, gettrailer(arg)) < 0)
    459 		printf("multiboot: %s: %s\n", sprint_bootsel(filename),
    460 		       strerror(errno));
    461 	else
    462 		printf("boot returned\n");
    463 }
    464 
    465 void
    466 command_version(char *arg)
    467 {
    468 
    469 	if (strcmp(arg, "full") == 0) {
    470 		printf("ImageBase: 0x%" PRIxPTR "\n",
    471 		    (uintptr_t)efi_li->ImageBase);
    472 		printf("Stack: 0x%" PRIxPTR "\n", efi_main_sp);
    473 		printf("EFI version: %d.%02d\n",
    474 		    ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
    475 		Print(L"EFI Firmware: %s (rev %d.%02d)\n", ST->FirmwareVendor,
    476 		    ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
    477 	}
    478 
    479 	printf("\n"
    480 	    ">> %s, Revision %s (from NetBSD %s)\n"
    481 	    ">> Memory: %d/%d k\n",
    482 	    bootprog_name, bootprog_rev, bootprog_kernrev,
    483 	    getbasemem(), getextmem());
    484 }
    485 
    486 void
    487 command_memmap(char *arg)
    488 {
    489 	bool sorted = true;
    490 
    491 	if (*arg == '\0' || strcmp(arg, "sorted") == 0)
    492 		/* Already sorted is true. */;
    493 	else if (strcmp(arg, "unsorted") == 0)
    494 		sorted = false;
    495 	else {
    496 		printf("invalid flag, "
    497 		    "must be 'sorted' or 'unsorted'.\n");
    498 		return;
    499 	}
    500 
    501 	efi_memory_show_map(sorted);
    502 }
    503 
    504 void
    505 command_devpath(char *arg)
    506 {
    507 	EFI_STATUS status;
    508 	UINTN i, nhandles;
    509 	EFI_HANDLE *handles;
    510 	EFI_DEVICE_PATH *dp0, *dp;
    511 	CHAR16 *path;
    512 	UINTN cols, rows, row = 0;
    513 
    514 	status = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut,
    515 	    ST->ConOut->Mode->Mode, &cols, &rows);
    516 	if (EFI_ERROR(status) || rows <= 2)
    517 		rows = 0;
    518 	else
    519 		rows -= 2;
    520 
    521 	/*
    522 	 * all devices.
    523 	 */
    524 	status = LibLocateHandle(ByProtocol, &DevicePathProtocol, NULL,
    525 	    &nhandles, &handles);
    526 	if (EFI_ERROR(status))
    527 		return;
    528 
    529 	for (i = 0; i < nhandles; i++) {
    530 		status = uefi_call_wrapper(BS->HandleProtocol, 3, handles[i],
    531 		    &DevicePathProtocol, (void **)&dp0);
    532 		if (EFI_ERROR(status))
    533 			break;
    534 
    535 		Print(L"DevicePathType %d\n", DevicePathType(dp0));
    536 		for (dp = dp0;
    537 		     !IsDevicePathEnd(dp);
    538 		     dp = NextDevicePathNode(dp)) {
    539 			path = DevicePathToStr(dp);
    540 			Print(L"%d:%d:%s\n", DevicePathType(dp), DevicePathSubType(dp), path);
    541 			FreePool(path);
    542 
    543 			if (++row >= rows) {
    544 				row = 0;
    545 				Print(L"Press Any Key to continue :");
    546 				(void) awaitkey(-1, 0);
    547 				Print(L"\n");
    548 			}
    549 		}
    550 	}
    551 }
    552 
    553 void
    554 command_efivar(char *arg)
    555 {
    556 	static const CHAR16 header[] =
    557 	 L"GUID                                Variable Name        Value\n"
    558 	 L"=================================== ==================== ========\n";
    559 	EFI_STATUS status;
    560 	UINTN sz = 64;
    561 	CHAR16 *name = NULL, *tmp, *val;
    562 	EFI_GUID vendor;
    563 	UINTN cols, rows, row = 0;
    564 
    565 	status = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut,
    566 	    ST->ConOut->Mode->Mode, &cols, &rows);
    567 	if (EFI_ERROR(status) || rows <= 2)
    568 		rows = 0;
    569 	else
    570 		rows -= 2;
    571 
    572 	name = AllocatePool(sz);
    573 	if (name == NULL) {
    574 		Print(L"memory allocation failed: %ld bytes\n",
    575 		    (UINT64)sz);
    576 		return;
    577 	}
    578 
    579 	name[0] = 0;
    580 	vendor = NullGuid;
    581 
    582 	Print(L"%s", header);
    583 	for (;;) {
    584 		status = uefi_call_wrapper(RT->GetNextVariableName, 3,
    585 		    &sz, name, &vendor);
    586 		if (EFI_ERROR(status)) {
    587 			if (status == EFI_NOT_FOUND)
    588 				break;
    589 			if (status != EFI_BUFFER_TOO_SMALL) {
    590 				Print(L"GetNextVariableName failed: %r\n",
    591 				    status);
    592 				break;
    593 			}
    594 
    595 			tmp = AllocatePool(sz);
    596 			if (tmp == NULL) {
    597 				Print(L"memory allocation failed: %ld bytes\n",
    598 				    (UINT64)sz);
    599 				break;
    600 			}
    601 			FreePool(name);
    602 			name = tmp;
    603 		}
    604 
    605 		val = LibGetVariable(name, &vendor);
    606 		Print(L"%.-35g %.-20s %s\n", &vendor, name,
    607 		    val ? val : L"(null)");
    608 		FreePool(val);
    609 
    610 		if (++row >= rows) {
    611 			row = 0;
    612 			Print(L"Press Any Key to continue :");
    613 			(void) awaitkey(-1, 0);
    614 			Print(L"\n");
    615 		}
    616 	}
    617 
    618 	FreePool(name);
    619 }
    620