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