Home | History | Annotate | Line # | Download | only in efiboot
boot.c revision 1.5.10.4
      1 /*	$NetBSD: boot.c,v 1.5.10.4 2018/04/07 04:12:14 pgoyette 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 #include <machine/limits.h>
     34 
     35 #include "bootcfg.h"
     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 #ifndef	EFIBOOTCFG_FILENAME
     59 #define	EFIBOOTCFG_FILENAME	"esp:/EFI/NetBSD/boot.cfg"
     60 #endif
     61 
     62 void	command_help(char *);
     63 void	command_quit(char *);
     64 void	command_boot(char *);
     65 void	command_consdev(char *);
     66 void	command_dev(char *);
     67 void	command_devpath(char *);
     68 void	command_efivar(char *);
     69 void	command_gop(char *);
     70 #if LIBSA_ENABLE_LS_OP
     71 void	command_ls(char *);
     72 #endif
     73 void	command_memmap(char *);
     74 #ifndef SMALL
     75 void	command_menu(char *);
     76 #endif
     77 void	command_modules(char *);
     78 void	command_multiboot(char *);
     79 void	command_text(char *);
     80 void	command_version(char *);
     81 
     82 const struct bootblk_command commands[] = {
     83 	{ "help",	command_help },
     84 	{ "?",		command_help },
     85 	{ "quit",	command_quit },
     86 	{ "boot",	command_boot },
     87 	{ "consdev",	command_consdev },
     88 	{ "dev",	command_dev },
     89 	{ "devpath",	command_devpath },
     90 	{ "efivar",	command_efivar },
     91 	{ "fs",		fs_add },
     92 	{ "gop",	command_gop },
     93 	{ "load",	module_add },
     94 #if LIBSA_ENABLE_LS_OP
     95 	{ "ls",		command_ls },
     96 #endif
     97 	{ "memmap",	command_memmap },
     98 #ifndef SMALL
     99 	{ "menu",	command_menu },
    100 #endif
    101 	{ "modules",	command_modules },
    102 	{ "multiboot",	command_multiboot },
    103 	{ "rndseed",	rnd_add },
    104 	{ "splash",	splash_add },
    105 	{ "text",	command_text },
    106 	{ "userconf",	userconf_add },
    107 	{ "version",	command_version },
    108 	{ NULL,		NULL },
    109 };
    110 
    111 static char *default_devname;
    112 static int default_unit, default_partition;
    113 static const char *default_filename;
    114 
    115 static char *sprint_bootsel(const char *);
    116 static void bootit(const char *, int);
    117 
    118 int
    119 parsebootfile(const char *fname, char **fsname, char **devname, int *unit,
    120     int *partition, const char **file)
    121 {
    122 	const char *col;
    123 
    124 	*fsname = "ufs";
    125 	*devname = default_devname;
    126 	*unit = default_unit;
    127 	*partition = default_partition;
    128 	*file = default_filename;
    129 
    130 	if (fname == NULL)
    131 		return 0;
    132 
    133 	if ((col = strchr(fname, ':')) != NULL) {	/* device given */
    134 		static char savedevname[MAXDEVNAME+1];
    135 		int devlen;
    136 		int u = 0, p = 0;
    137 		int i = 0;
    138 
    139 		devlen = col - fname;
    140 		if (devlen > MAXDEVNAME)
    141 			return EINVAL;
    142 
    143 #define isvalidname(c) ((c) >= 'a' && (c) <= 'z')
    144 		if (!isvalidname(fname[i]))
    145 			return EINVAL;
    146 		do {
    147 			savedevname[i] = fname[i];
    148 			i++;
    149 		} while (isvalidname(fname[i]));
    150 		savedevname[i] = '\0';
    151 
    152 #define isnum(c) ((c) >= '0' && (c) <= '9')
    153 		if (i < devlen) {
    154 			if (!isnum(fname[i]))
    155 				return EUNIT;
    156 			do {
    157 				u *= 10;
    158 				u += fname[i++] - '0';
    159 			} while (isnum(fname[i]));
    160 		}
    161 
    162 #define isvalidpart(c) ((c) >= 'a' && (c) <= 'z')
    163 		if (i < devlen) {
    164 			if (!isvalidpart(fname[i]))
    165 				return EPART;
    166 			p = fname[i++] - 'a';
    167 		}
    168 
    169 		if (i != devlen)
    170 			return ENXIO;
    171 
    172 		*devname = savedevname;
    173 		*unit = u;
    174 		*partition = p;
    175 		fname = col + 1;
    176 	}
    177 
    178 	if (*fname)
    179 		*file = fname;
    180 
    181 	return 0;
    182 }
    183 
    184 static char *
    185 sprint_bootsel(const char *filename)
    186 {
    187 	char *fsname, *devname;
    188 	int unit, partition;
    189 	const char *file;
    190 	static char buf[80];
    191 
    192 	if (parsebootfile(filename, &fsname, &devname, &unit,
    193 			  &partition, &file) == 0) {
    194 		snprintf(buf, sizeof(buf), "%s%d%c:%s", devname, unit,
    195 		    'a' + partition, file);
    196 		return buf;
    197 	}
    198 	return "(invalid)";
    199 }
    200 
    201 void
    202 clearit(void)
    203 {
    204 
    205 	if (bootcfg_info.clear)
    206 		clear_pc_screen();
    207 }
    208 
    209 static void
    210 bootit(const char *filename, int howto)
    211 {
    212 	EFI_STATUS status;
    213 	EFI_PHYSICAL_ADDRESS bouncebuf;
    214 	UINTN npages;
    215 	u_long allocsz;
    216 
    217 	if (howto & AB_VERBOSE)
    218 		printf("booting %s (howto 0x%x)\n", sprint_bootsel(filename),
    219 		    howto);
    220 
    221 	if (count_netbsd(filename, &allocsz) < 0) {
    222 		printf("boot: %s: %s\n", sprint_bootsel(filename),
    223 		       strerror(errno));
    224 		return;
    225 	}
    226 
    227 	bouncebuf = EFI_ALLOCATE_MAX_ADDRESS;
    228 	npages = EFI_SIZE_TO_PAGES(allocsz);
    229 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress,
    230 	    EfiLoaderData, npages, &bouncebuf);
    231 	if (EFI_ERROR(status)) {
    232 		printf("boot: %s: %s\n", sprint_bootsel(filename),
    233 		       strerror(ENOMEM));
    234 		return;
    235 	}
    236 
    237 	efi_loadaddr = bouncebuf;
    238 	if (exec_netbsd(filename, bouncebuf, howto, 0, efi_cleanup) < 0)
    239 		printf("boot: %s: %s\n", sprint_bootsel(filename),
    240 		       strerror(errno));
    241 	else
    242 		printf("boot returned\n");
    243 
    244 	(void) uefi_call_wrapper(BS->FreePages, 2, bouncebuf, npages);
    245 	efi_loadaddr = 0;
    246 }
    247 
    248 void
    249 print_banner(void)
    250 {
    251 	int n;
    252 
    253 	clearit();
    254 	if (bootcfg_info.banner[0]) {
    255 		for (n = 0; n < BOOTCFG_MAXBANNER && bootcfg_info.banner[n];
    256 		    n++)
    257 			printf("%s\n", bootcfg_info.banner[n]);
    258 	} else
    259 		command_version("short");
    260 }
    261 
    262 void
    263 boot(void)
    264 {
    265 	int currname;
    266 	int c;
    267 
    268 	boot_modules_enabled = !(boot_params.bp_flags & X86_BP_FLAGS_NOMODULES);
    269 
    270 	/* try to set default device to what BIOS tells us */
    271 	bios2dev(boot_biosdev, boot_biossector, &default_devname, &default_unit,
    272 	    &default_partition);
    273 
    274 	/* if the user types "boot" without filename */
    275 	default_filename = DEFFILENAME;
    276 
    277 	if (!(boot_params.bp_flags & X86_BP_FLAGS_NOBOOTCONF)) {
    278 #ifdef EFIBOOTCFG_FILENAME
    279 		int rv = parsebootconf(EFIBOOTCFG_FILENAME);
    280 		if (rv)
    281 #endif
    282 		parsebootconf(BOOTCFG_FILENAME);
    283 	} else {
    284 		bootcfg_info.timeout = boot_params.bp_timeout;
    285 	}
    286 
    287 	/*
    288 	 * If console set in boot.cfg, switch to it.
    289 	 * This will print the banner, so we don't need to explicitly do it
    290 	 */
    291 	if (bootcfg_info.consdev)
    292 		command_consdev(bootcfg_info.consdev);
    293 	else
    294 		print_banner();
    295 
    296 	/* Display the menu, if applicable */
    297 	twiddle_toggle = 0;
    298 	if (bootcfg_info.nummenu > 0) {
    299 		/* Does not return */
    300 		doboottypemenu();
    301 	}
    302 
    303 	printf("Press return to boot now, any other key for boot menu\n");
    304 	for (currname = 0; currname < NUMNAMES; currname++) {
    305 		printf("booting %s - starting in ",
    306 		       sprint_bootsel(names[currname][0]));
    307 
    308 		c = awaitkey((bootcfg_info.timeout < 0) ? 0
    309 		    : bootcfg_info.timeout, 1);
    310 		if ((c != '\r') && (c != '\n') && (c != '\0')) {
    311 		    if ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0) {
    312 			/* do NOT ask for password */
    313 			bootmenu(); /* does not return */
    314 		    } else {
    315 			/* DO ask for password */
    316 			if (check_password((char *)boot_params.bp_password)) {
    317 			    /* password ok */
    318 			    printf("type \"?\" or \"help\" for help.\n");
    319 			    bootmenu(); /* does not return */
    320 			} else {
    321 			    /* bad password */
    322 			    printf("Wrong password.\n");
    323 			    currname = 0;
    324 			    continue;
    325 			}
    326 		    }
    327 		}
    328 
    329 		/*
    330 		 * try pairs of names[] entries, foo and foo.gz
    331 		 */
    332 		/* don't print "booting..." again */
    333 		bootit(names[currname][0], 0);
    334 		/* since it failed, try compressed bootfile. */
    335 		bootit(names[currname][1], AB_VERBOSE);
    336 	}
    337 
    338 	bootmenu();	/* does not return */
    339 }
    340 
    341 /* ARGSUSED */
    342 void
    343 command_help(char *arg)
    344 {
    345 
    346 	printf("commands are:\n"
    347 	       "boot [xdNx:][filename] [-12acdqsvxz]\n"
    348 	       "     (ex. \"hd0a:netbsd.old -s\"\n"
    349 	       "dev [xd[N[x]]:]\n"
    350 	       "consdev {pc|com[0123][,{speed}]|com,{ioport}[,{speed}]}\n"
    351 	       "devpath\n"
    352 	       "efivar\n"
    353 	       "gop [{modenum|list}]\n"
    354 	       "load {path_to_module}\n"
    355 #if LIBSA_ENABLE_LS_OP
    356 	       "ls [path]\n"
    357 #endif
    358 	       "memmap [{sorted|unsorted}]\n"
    359 #ifndef SMALL
    360 	       "menu (reenters boot menu, if defined in boot.cfg)\n"
    361 #endif
    362 	       "modules {on|off|enabled|disabled}\n"
    363 	       "multiboot [xdNx:][filename] [<args>]\n"
    364 	       "rndseed {path_to_rndseed_file}\n"
    365 	       "splash {path_to_image_file}\n"
    366 	       "text [{modenum|list}]\n"
    367 	       "userconf {command}\n"
    368 	       "version\n"
    369 	       "help|?\n"
    370 	       "quit\n");
    371 }
    372 
    373 #if LIBSA_ENABLE_LS_OP
    374 void
    375 command_ls(char *arg)
    376 {
    377 	const char *save = default_filename;
    378 
    379 	default_filename = "/";
    380 	ls(arg);
    381 	default_filename = save;
    382 }
    383 #endif
    384 
    385 /* ARGSUSED */
    386 void
    387 command_quit(char *arg)
    388 {
    389 
    390 	printf("Exiting...\n");
    391 	delay(1 * 1000 * 1000);
    392 	reboot();
    393 	/* Note: we shouldn't get to this point! */
    394 	panic("Could not reboot!");
    395 }
    396 
    397 void
    398 command_boot(char *arg)
    399 {
    400 	char *filename;
    401 	int howto;
    402 
    403 	if (!parseboot(arg, &filename, &howto))
    404 		return;
    405 
    406 	if (filename != NULL) {
    407 		bootit(filename, howto);
    408 	} else {
    409 		int i;
    410 
    411 		if (howto == 0)
    412 			bootdefault();
    413 		for (i = 0; i < NUMNAMES; i++) {
    414 			bootit(names[i][0], howto);
    415 			bootit(names[i][1], howto);
    416 		}
    417 	}
    418 }
    419 
    420 void
    421 command_dev(char *arg)
    422 {
    423 	static char savedevname[MAXDEVNAME + 1];
    424 	char *fsname, *devname;
    425 	const char *file; /* dummy */
    426 
    427 	if (*arg == '\0') {
    428 		efi_disk_show();
    429 		printf("default %s%d%c\n", default_devname, default_unit,
    430 		       'a' + default_partition);
    431 		return;
    432 	}
    433 
    434 	if (strchr(arg, ':') == NULL ||
    435 	    parsebootfile(arg, &fsname, &devname, &default_unit,
    436 	      &default_partition, &file)) {
    437 		command_help(NULL);
    438 		return;
    439 	}
    440 
    441 	/* put to own static storage */
    442 	strncpy(savedevname, devname, MAXDEVNAME + 1);
    443 	default_devname = savedevname;
    444 }
    445 
    446 static const struct cons_devs {
    447 	const char	*name;
    448 	u_int		tag;
    449 	int		ioport;
    450 } cons_devs[] = {
    451 	{ "pc",		CONSDEV_PC,   0 },
    452 	{ "com0",	CONSDEV_COM0, 0 },
    453 	{ "com1",	CONSDEV_COM1, 0 },
    454 	{ "com2",	CONSDEV_COM2, 0 },
    455 	{ "com3",	CONSDEV_COM3, 0 },
    456 	{ "com0kbd",	CONSDEV_COM0KBD, 0 },
    457 	{ "com1kbd",	CONSDEV_COM1KBD, 0 },
    458 	{ "com2kbd",	CONSDEV_COM2KBD, 0 },
    459 	{ "com3kbd",	CONSDEV_COM3KBD, 0 },
    460 	{ "com",	CONSDEV_COM0, -1 },
    461 	{ "auto",	CONSDEV_AUTO, 0 },
    462 	{ NULL,		0 }
    463 };
    464 
    465 void
    466 command_consdev(char *arg)
    467 {
    468 	const struct cons_devs *cdp;
    469 	char *sep, *sep2 = NULL;
    470 	int ioport, speed = 0;
    471 
    472 	if (*arg == '\0') {
    473 		efi_cons_show();
    474 		return;
    475 	}
    476 
    477 	sep = strchr(arg, ',');
    478 	if (sep != NULL) {
    479 		*sep++ = '\0';
    480 		sep2 = strchr(sep, ',');
    481 		if (sep2 != NULL)
    482 			*sep2++ = '\0';
    483 	}
    484 
    485 	for (cdp = cons_devs; cdp->name; cdp++) {
    486 		if (strcmp(arg, cdp->name) == 0) {
    487 			ioport = cdp->ioport;
    488 			if (cdp->tag == CONSDEV_PC || cdp->tag == CONSDEV_AUTO) {
    489 				if (sep != NULL || sep2 != NULL)
    490 					goto error;
    491 			} else {
    492 				/* com? */
    493 				if (ioport == -1) {
    494 					if (sep != NULL) {
    495 						u_long t = strtoul(sep, NULL, 0);
    496 						if (t > INT_MAX)
    497 							goto error;
    498 						ioport = (int)t;
    499 					}
    500 					if (sep2 != NULL) {
    501 						speed = atoi(sep2);
    502 						if (speed < 0)
    503 							goto error;
    504 					}
    505 				} else {
    506 					if (sep != NULL) {
    507 						speed = atoi(sep);
    508 						if (speed < 0)
    509 							goto error;
    510 					}
    511 					if (sep2 != NULL)
    512 						goto error;
    513 				}
    514 			}
    515 			consinit(cdp->tag, ioport, speed);
    516 			print_banner();
    517 			return;
    518 		}
    519 	}
    520 error:
    521 	printf("invalid console device.\n");
    522 }
    523 
    524 #ifndef SMALL
    525 /* ARGSUSED */
    526 void
    527 command_menu(char *arg)
    528 {
    529 
    530 	if (bootcfg_info.nummenu > 0) {
    531 		/* Does not return */
    532 		doboottypemenu();
    533 	} else
    534 		printf("No menu defined in boot.cfg\n");
    535 }
    536 #endif /* !SMALL */
    537 
    538 void
    539 command_modules(char *arg)
    540 {
    541 
    542 	if (strcmp(arg, "enabled") == 0 ||
    543 	    strcmp(arg, "on") == 0)
    544 		boot_modules_enabled = true;
    545 	else if (strcmp(arg, "disabled") == 0 ||
    546 	    strcmp(arg, "off") == 0)
    547 		boot_modules_enabled = false;
    548 	else
    549 		printf("invalid flag, must be 'enabled' or 'disabled'.\n");
    550 }
    551 
    552 void
    553 command_multiboot(char *arg)
    554 {
    555 	char *filename;
    556 
    557 	filename = arg;
    558 	if (exec_multiboot(filename, gettrailer(arg)) < 0)
    559 		printf("multiboot: %s: %s\n", sprint_bootsel(filename),
    560 		       strerror(errno));
    561 	else
    562 		printf("boot returned\n");
    563 }
    564 
    565 void
    566 command_version(char *arg)
    567 {
    568 	CHAR16 *path;
    569 	char *upath, *ufirmware;
    570 	int rv;
    571 
    572 	if (strcmp(arg, "full") == 0) {
    573 		printf("ImageBase: 0x%" PRIxPTR "\n",
    574 		    (uintptr_t)efi_li->ImageBase);
    575 		printf("Stack: 0x%" PRIxPTR "\n", efi_main_sp);
    576 		printf("EFI version: %d.%02d\n",
    577 		    ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
    578 		ufirmware = NULL;
    579 		rv = ucs2_to_utf8(ST->FirmwareVendor, &ufirmware);
    580 		if (rv == 0) {
    581 			printf("EFI Firmware: %s (rev %d.%02d)\n", ufirmware,
    582 			    ST->FirmwareRevision >> 16,
    583 			    ST->FirmwareRevision & 0xffff);
    584 			FreePool(ufirmware);
    585 		}
    586 		path = DevicePathToStr(efi_bootdp);
    587 		upath = NULL;
    588 		rv = ucs2_to_utf8(path, &upath);
    589 		FreePool(path);
    590 		if (rv == 0) {
    591 			printf("Boot DevicePath: %d:%d:%s\n",
    592 			    DevicePathType(efi_bootdp),
    593 			    DevicePathSubType(efi_bootdp), upath);
    594 			FreePool(upath);
    595 		}
    596 	}
    597 
    598 	printf("\n"
    599 	    ">> %s, Revision %s (from NetBSD %s)\n"
    600 	    ">> Memory: %d/%d k\n",
    601 	    bootprog_name, bootprog_rev, bootprog_kernrev,
    602 	    getbasemem(), getextmem());
    603 }
    604 
    605 void
    606 command_memmap(char *arg)
    607 {
    608 	bool sorted = true;
    609 
    610 	if (*arg == '\0' || strcmp(arg, "sorted") == 0)
    611 		/* Already sorted is true. */;
    612 	else if (strcmp(arg, "unsorted") == 0)
    613 		sorted = false;
    614 	else {
    615 		printf("invalid flag, "
    616 		    "must be 'sorted' or 'unsorted'.\n");
    617 		return;
    618 	}
    619 
    620 	efi_memory_show_map(sorted);
    621 }
    622 
    623 void
    624 command_devpath(char *arg)
    625 {
    626 	EFI_STATUS status;
    627 	UINTN i, nhandles;
    628 	EFI_HANDLE *handles;
    629 	EFI_DEVICE_PATH *dp0, *dp;
    630 	CHAR16 *path;
    631 	char *upath;
    632 	UINTN cols, rows, row = 0;
    633 	int rv;
    634 
    635 	status = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut,
    636 	    ST->ConOut->Mode->Mode, &cols, &rows);
    637 	if (EFI_ERROR(status) || rows <= 2)
    638 		rows = 0;
    639 	else
    640 		rows -= 2;
    641 
    642 	/*
    643 	 * all devices.
    644 	 */
    645 	status = LibLocateHandle(ByProtocol, &DevicePathProtocol, NULL,
    646 	    &nhandles, &handles);
    647 	if (EFI_ERROR(status))
    648 		return;
    649 
    650 	for (i = 0; i < nhandles; i++) {
    651 		status = uefi_call_wrapper(BS->HandleProtocol, 3, handles[i],
    652 		    &DevicePathProtocol, (void **)&dp0);
    653 		if (EFI_ERROR(status))
    654 			break;
    655 
    656 		printf("DevicePathType %d\n", DevicePathType(dp0));
    657 		if (++row >= rows) {
    658 			row = 0;
    659 			printf("Press Any Key to continue :");
    660 			(void) awaitkey(-1, 0);
    661 			printf("\n");
    662 		}
    663 		for (dp = dp0;
    664 		     !IsDevicePathEnd(dp);
    665 		     dp = NextDevicePathNode(dp)) {
    666 
    667 			path = DevicePathToStr(dp);
    668 			upath = NULL;
    669 			rv = ucs2_to_utf8(path, &upath);
    670 			FreePool(path);
    671 			if (rv) {
    672 				printf("convert failed\n");
    673 				break;
    674 			}
    675 
    676 			printf("%d:%d:%s\n", DevicePathType(dp),
    677 			    DevicePathSubType(dp), upath);
    678 			FreePool(upath);
    679 
    680 			if (++row >= rows) {
    681 				row = 0;
    682 				printf("Press Any Key to continue :");
    683 				(void) awaitkey(-1, 0);
    684 				printf("\n");
    685 			}
    686 		}
    687 	}
    688 }
    689 
    690 
    691 void
    692 command_efivar(char *arg)
    693 {
    694 	static const char header[] =
    695 	 "GUID                                 Variable Name        Value\n"
    696 	 "==================================== ==================== ========\n";
    697 	EFI_STATUS status;
    698 	UINTN sz = 64, osz;
    699 	CHAR16 *name = NULL, *tmp, *val, guid[128];
    700 	char *uname, *uval, *uguid;
    701 	EFI_GUID vendor;
    702 	UINTN cols, rows, row = 0;
    703 	int rv;
    704 
    705 	status = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut,
    706 	    ST->ConOut->Mode->Mode, &cols, &rows);
    707 	if (EFI_ERROR(status) || rows <= 2)
    708 		rows = 0;
    709 	else
    710 		rows -= 2;
    711 
    712 	name = AllocatePool(sz);
    713 	if (name == NULL) {
    714 		printf("memory allocation failed: %" PRIuMAX" bytes\n",
    715 		    (uintmax_t)sz);
    716 		return;
    717 	}
    718 
    719 	SetMem(name, sz, 0);
    720 	vendor = NullGuid;
    721 
    722 	printf("%s", header);
    723 	for (;;) {
    724 		osz = sz;
    725 		status = uefi_call_wrapper(RT->GetNextVariableName, 3,
    726 		    &sz, name, &vendor);
    727 		if (EFI_ERROR(status)) {
    728 			if (status == EFI_NOT_FOUND)
    729 				break;
    730 			if (status != EFI_BUFFER_TOO_SMALL) {
    731 				printf("GetNextVariableName failed: %" PRIxMAX "\n",
    732 				    (uintmax_t)status);
    733 				break;
    734 			}
    735 
    736 			tmp = AllocatePool(sz);
    737 			if (tmp == NULL) {
    738 				printf("memory allocation failed: %" PRIuMAX
    739 				    "bytes\n", (uintmax_t)sz);
    740 				break;
    741 			}
    742 			SetMem(tmp, sz, 0);
    743 			CopyMem(tmp, name, osz);
    744 			FreePool(name);
    745 			name = tmp;
    746 			continue;
    747 		}
    748 
    749 		val = LibGetVariable(name, &vendor);
    750 		if (val != NULL) {
    751 			uval = NULL;
    752 			rv = ucs2_to_utf8(val, &uval);
    753 			FreePool(val);
    754 			if (rv) {
    755 				printf("value convert failed\n");
    756 				break;
    757 			}
    758 		} else
    759 			uval = NULL;
    760 		uname = NULL;
    761 		rv = ucs2_to_utf8(name, &uname);
    762 		if (rv) {
    763 			printf("name convert failed\n");
    764 			FreePool(uval);
    765 			break;
    766 		}
    767 		GuidToString(guid, &vendor);
    768 		uguid = NULL;
    769 		rv = ucs2_to_utf8(guid, &uguid);
    770 		if (rv) {
    771 			printf("GUID convert failed\n");
    772 			FreePool(uval);
    773 			FreePool(uname);
    774 			break;
    775 		}
    776 		printf("%-35s %-20s %s\n", uguid, uname, uval ? uval : "(null)");
    777 		FreePool(uguid);
    778 		FreePool(uname);
    779 		if (uval != NULL)
    780 			FreePool(uval);
    781 
    782 		if (++row >= rows) {
    783 			row = 0;
    784 			printf("Press Any Key to continue :");
    785 			(void) awaitkey(-1, 0);
    786 			printf("\n");
    787 		}
    788 	}
    789 
    790 	FreePool(name);
    791 }
    792