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