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