Home | History | Annotate | Line # | Download | only in efiboot
boot.c revision 1.11
      1 /*	$NetBSD: boot.c,v 1.11 2019/06/20 17:33:31 maxv 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 	EFI_STATUS status;
    230 	EFI_PHYSICAL_ADDRESS bouncebuf;
    231 	UINTN npages;
    232 	u_long allocsz;
    233 
    234 	if (howto & AB_VERBOSE)
    235 		printf("booting %s (howto 0x%x)\n", sprint_bootsel(filename),
    236 		    howto);
    237 
    238 	if (count_netbsd(filename, &allocsz) < 0) {
    239 		printf("boot: %s: %s\n", sprint_bootsel(filename),
    240 		       strerror(errno));
    241 		return;
    242 	}
    243 
    244 	bouncebuf = EFI_ALLOCATE_MAX_ADDRESS;
    245 	npages = EFI_SIZE_TO_PAGES(allocsz);
    246 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress,
    247 	    EfiLoaderData, npages, &bouncebuf);
    248 	if (EFI_ERROR(status)) {
    249 		printf("boot: %s: %s\n", sprint_bootsel(filename),
    250 		       strerror(ENOMEM));
    251 		return;
    252 	}
    253 
    254 	efi_loadaddr = bouncebuf;
    255 	if (exec_netbsd(filename, bouncebuf, 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 	(void) uefi_call_wrapper(BS->FreePages, 2, bouncebuf, npages);
    262 	efi_loadaddr = 0;
    263 }
    264 
    265 void
    266 print_banner(void)
    267 {
    268 	int n;
    269 
    270 	clearit();
    271 	if (bootcfg_info.banner[0]) {
    272 		for (n = 0; n < BOOTCFG_MAXBANNER && bootcfg_info.banner[n];
    273 		    n++)
    274 			printf("%s\n", bootcfg_info.banner[n]);
    275 	} else
    276 		command_version("short");
    277 }
    278 
    279 void
    280 boot(void)
    281 {
    282 	int currname;
    283 	int c;
    284 
    285 	boot_modules_enabled = !(boot_params.bp_flags & X86_BP_FLAGS_NOMODULES);
    286 
    287 	/* try to set default device to what BIOS tells us */
    288 	bios2dev(boot_biosdev, boot_biossector, &default_devname, &default_unit,
    289 	    &default_partition);
    290 
    291 	/* if the user types "boot" without filename */
    292 	default_filename = DEFFILENAME;
    293 
    294 	if (!(boot_params.bp_flags & X86_BP_FLAGS_NOBOOTCONF)) {
    295 #ifdef EFIBOOTCFG_FILENAME
    296 		int rv = EINVAL;
    297 		if (efi_bootdp_type != BOOT_DEVICE_TYPE_NET)
    298 			rv = parsebootconf(EFIBOOTCFG_FILENAME);
    299 		if (rv)
    300 #endif
    301 		parsebootconf(BOOTCFG_FILENAME);
    302 	} else {
    303 		bootcfg_info.timeout = boot_params.bp_timeout;
    304 	}
    305 
    306 	/*
    307 	 * If console set in boot.cfg, switch to it.
    308 	 * This will print the banner, so we don't need to explicitly do it
    309 	 */
    310 	if (bootcfg_info.consdev)
    311 		command_consdev(bootcfg_info.consdev);
    312 	else
    313 		print_banner();
    314 
    315 	/* Display the menu, if applicable */
    316 	twiddle_toggle = 0;
    317 	if (bootcfg_info.nummenu > 0) {
    318 		/* Does not return */
    319 		doboottypemenu();
    320 	}
    321 
    322 	printf("Press return to boot now, any other key for boot menu\n");
    323 	for (currname = 0; currname < NUMNAMES; currname++) {
    324 		printf("booting %s - starting in ",
    325 		       sprint_bootsel(names[currname][0]));
    326 
    327 		c = awaitkey((bootcfg_info.timeout < 0) ? 0
    328 		    : bootcfg_info.timeout, 1);
    329 		if ((c != '\r') && (c != '\n') && (c != '\0')) {
    330 		    if ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0) {
    331 			/* do NOT ask for password */
    332 			bootmenu(); /* does not return */
    333 		    } else {
    334 			/* DO ask for password */
    335 			if (check_password((char *)boot_params.bp_password)) {
    336 			    /* password ok */
    337 			    printf("type \"?\" or \"help\" for help.\n");
    338 			    bootmenu(); /* does not return */
    339 			} else {
    340 			    /* bad password */
    341 			    printf("Wrong password.\n");
    342 			    currname = 0;
    343 			    continue;
    344 			}
    345 		    }
    346 		}
    347 
    348 		/*
    349 		 * try pairs of names[] entries, foo and foo.gz
    350 		 */
    351 		/* don't print "booting..." again */
    352 		bootit(names[currname][0], 0);
    353 		/* since it failed, try compressed bootfile. */
    354 		bootit(names[currname][1], AB_VERBOSE);
    355 	}
    356 
    357 	bootmenu();	/* does not return */
    358 }
    359 
    360 /* ARGSUSED */
    361 void
    362 command_help(char *arg)
    363 {
    364 
    365 	printf("commands are:\n"
    366 	       "boot [xdNx:][filename] [-12acdqsvxz]\n"
    367 	       "     (ex. \"hd0a:netbsd.old -s\")\n"
    368 	       "pkboot [xdNx:][filename] [-12acdqsvxz]\n"
    369 	       "dev [xd[N[x]]:]\n"
    370 	       "consdev {pc|com[0123][,{speed}]|com,{ioport}[,{speed}]}\n"
    371 	       "devpath\n"
    372 	       "efivar\n"
    373 	       "gop [{modenum|list}]\n"
    374 	       "load {path_to_module}\n"
    375 #if LIBSA_ENABLE_LS_OP
    376 	       "ls [path]\n"
    377 #endif
    378 	       "memmap [{sorted|unsorted}]\n"
    379 #ifndef SMALL
    380 	       "menu (reenters boot menu, if defined in boot.cfg)\n"
    381 #endif
    382 	       "modules {on|off|enabled|disabled}\n"
    383 	       "multiboot [xdNx:][filename] [<args>]\n"
    384 	       "rndseed {path_to_rndseed_file}\n"
    385 	       "splash {path_to_image_file}\n"
    386 	       "text [{modenum|list}]\n"
    387 	       "userconf {command}\n"
    388 	       "version\n"
    389 	       "help|?\n"
    390 	       "quit\n");
    391 }
    392 
    393 #if LIBSA_ENABLE_LS_OP
    394 void
    395 command_ls(char *arg)
    396 {
    397 	const char *save = default_filename;
    398 
    399 	default_filename = "/";
    400 	ls(arg);
    401 	default_filename = save;
    402 }
    403 #endif
    404 
    405 /* ARGSUSED */
    406 void
    407 command_quit(char *arg)
    408 {
    409 
    410 	printf("Exiting...\n");
    411 	delay(1 * 1000 * 1000);
    412 	reboot();
    413 	/* Note: we shouldn't get to this point! */
    414 	panic("Could not reboot!");
    415 }
    416 
    417 void
    418 command_boot(char *arg)
    419 {
    420 	char *filename;
    421 	int howto;
    422 
    423 	if (!parseboot(arg, &filename, &howto))
    424 		return;
    425 
    426 	if (filename != NULL) {
    427 		bootit(filename, howto);
    428 	} else {
    429 		int i;
    430 
    431 		if (howto == 0)
    432 			bootdefault();
    433 		for (i = 0; i < NUMNAMES; i++) {
    434 			bootit(names[i][0], howto);
    435 			bootit(names[i][1], howto);
    436 		}
    437 	}
    438 }
    439 
    440 void
    441 command_pkboot(char *arg)
    442 {
    443 	extern int has_prekern;
    444 	has_prekern = 1;
    445 	command_boot(arg);
    446 	has_prekern = 0;
    447 }
    448 
    449 void
    450 command_dev(char *arg)
    451 {
    452 	static char savedevname[MAXDEVNAME + 1];
    453 	char buf[80];
    454 	char *fsname, *devname;
    455 	const char *file; /* dummy */
    456 
    457 	if (*arg == '\0') {
    458 		efi_disk_show();
    459 		efi_net_show();
    460 		printf("default %s\n", snprint_bootdev(buf, sizeof(buf),
    461 		    default_devname, default_unit, default_partition));
    462 		return;
    463 	}
    464 
    465 	if (strchr(arg, ':') == NULL ||
    466 	    parsebootfile(arg, &fsname, &devname, &default_unit,
    467 	      &default_partition, &file)) {
    468 		command_help(NULL);
    469 		return;
    470 	}
    471 
    472 	/* put to own static storage */
    473 	strncpy(savedevname, devname, MAXDEVNAME + 1);
    474 	default_devname = savedevname;
    475 }
    476 
    477 static const struct cons_devs {
    478 	const char	*name;
    479 	u_int		tag;
    480 	int		ioport;
    481 } cons_devs[] = {
    482 	{ "pc",		CONSDEV_PC,   0 },
    483 	{ "com0",	CONSDEV_COM0, 0 },
    484 	{ "com1",	CONSDEV_COM1, 0 },
    485 	{ "com2",	CONSDEV_COM2, 0 },
    486 	{ "com3",	CONSDEV_COM3, 0 },
    487 	{ "com0kbd",	CONSDEV_COM0KBD, 0 },
    488 	{ "com1kbd",	CONSDEV_COM1KBD, 0 },
    489 	{ "com2kbd",	CONSDEV_COM2KBD, 0 },
    490 	{ "com3kbd",	CONSDEV_COM3KBD, 0 },
    491 	{ "com",	CONSDEV_COM0, -1 },
    492 	{ "auto",	CONSDEV_AUTO, 0 },
    493 	{ NULL,		0 }
    494 };
    495 
    496 void
    497 command_consdev(char *arg)
    498 {
    499 	const struct cons_devs *cdp;
    500 	char *sep, *sep2 = NULL;
    501 	int ioport, speed = 0;
    502 
    503 	if (*arg == '\0') {
    504 		efi_cons_show();
    505 		return;
    506 	}
    507 
    508 	sep = strchr(arg, ',');
    509 	if (sep != NULL) {
    510 		*sep++ = '\0';
    511 		sep2 = strchr(sep, ',');
    512 		if (sep2 != NULL)
    513 			*sep2++ = '\0';
    514 	}
    515 
    516 	for (cdp = cons_devs; cdp->name; cdp++) {
    517 		if (strcmp(arg, cdp->name) == 0) {
    518 			ioport = cdp->ioport;
    519 			if (cdp->tag == CONSDEV_PC || cdp->tag == CONSDEV_AUTO) {
    520 				if (sep != NULL || sep2 != NULL)
    521 					goto error;
    522 			} else {
    523 				/* com? */
    524 				if (ioport == -1) {
    525 					if (sep != NULL) {
    526 						u_long t = strtoul(sep, NULL, 0);
    527 						if (t > INT_MAX)
    528 							goto error;
    529 						ioport = (int)t;
    530 					}
    531 					if (sep2 != NULL) {
    532 						speed = atoi(sep2);
    533 						if (speed < 0)
    534 							goto error;
    535 					}
    536 				} else {
    537 					if (sep != NULL) {
    538 						speed = atoi(sep);
    539 						if (speed < 0)
    540 							goto error;
    541 					}
    542 					if (sep2 != NULL)
    543 						goto error;
    544 				}
    545 			}
    546 			consinit(cdp->tag, ioport, speed);
    547 			print_banner();
    548 			return;
    549 		}
    550 	}
    551 error:
    552 	printf("invalid console device.\n");
    553 }
    554 
    555 #ifndef SMALL
    556 /* ARGSUSED */
    557 void
    558 command_menu(char *arg)
    559 {
    560 
    561 	if (bootcfg_info.nummenu > 0) {
    562 		/* Does not return */
    563 		doboottypemenu();
    564 	} else
    565 		printf("No menu defined in boot.cfg\n");
    566 }
    567 #endif /* !SMALL */
    568 
    569 void
    570 command_modules(char *arg)
    571 {
    572 
    573 	if (strcmp(arg, "enabled") == 0 ||
    574 	    strcmp(arg, "on") == 0)
    575 		boot_modules_enabled = true;
    576 	else if (strcmp(arg, "disabled") == 0 ||
    577 	    strcmp(arg, "off") == 0)
    578 		boot_modules_enabled = false;
    579 	else
    580 		printf("invalid flag, must be 'enabled' or 'disabled'.\n");
    581 }
    582 
    583 void
    584 command_multiboot(char *arg)
    585 {
    586 	char *filename;
    587 
    588 	filename = arg;
    589 	if (exec_multiboot(filename, gettrailer(arg)) < 0)
    590 		printf("multiboot: %s: %s\n", sprint_bootsel(filename),
    591 		       strerror(errno));
    592 	else
    593 		printf("boot returned\n");
    594 }
    595 
    596 void
    597 command_version(char *arg)
    598 {
    599 	CHAR16 *path;
    600 	char *upath, *ufirmware;
    601 	int rv;
    602 
    603 	if (strcmp(arg, "full") == 0) {
    604 		printf("ImageBase: 0x%" PRIxPTR "\n",
    605 		    (uintptr_t)efi_li->ImageBase);
    606 		printf("Stack: 0x%" PRIxPTR "\n", efi_main_sp);
    607 		printf("EFI version: %d.%02d\n",
    608 		    ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
    609 		ufirmware = NULL;
    610 		rv = ucs2_to_utf8(ST->FirmwareVendor, &ufirmware);
    611 		if (rv == 0) {
    612 			printf("EFI Firmware: %s (rev %d.%02d)\n", ufirmware,
    613 			    ST->FirmwareRevision >> 16,
    614 			    ST->FirmwareRevision & 0xffff);
    615 			FreePool(ufirmware);
    616 		}
    617 		path = DevicePathToStr(efi_bootdp);
    618 		upath = NULL;
    619 		rv = ucs2_to_utf8(path, &upath);
    620 		FreePool(path);
    621 		if (rv == 0) {
    622 			printf("Boot DevicePath: %d:%d:%s\n",
    623 			    DevicePathType(efi_bootdp),
    624 			    DevicePathSubType(efi_bootdp), upath);
    625 			FreePool(upath);
    626 		}
    627 	}
    628 
    629 	printf("\n"
    630 	    ">> %s, Revision %s (from NetBSD %s)\n"
    631 	    ">> Memory: %d/%d k\n",
    632 	    bootprog_name, bootprog_rev, bootprog_kernrev,
    633 	    getbasemem(), getextmem());
    634 }
    635 
    636 void
    637 command_memmap(char *arg)
    638 {
    639 	bool sorted = true;
    640 
    641 	if (*arg == '\0' || strcmp(arg, "sorted") == 0)
    642 		/* Already sorted is true. */;
    643 	else if (strcmp(arg, "unsorted") == 0)
    644 		sorted = false;
    645 	else {
    646 		printf("invalid flag, "
    647 		    "must be 'sorted' or 'unsorted'.\n");
    648 		return;
    649 	}
    650 
    651 	efi_memory_show_map(sorted);
    652 }
    653 
    654 void
    655 command_devpath(char *arg)
    656 {
    657 	EFI_STATUS status;
    658 	UINTN i, nhandles;
    659 	EFI_HANDLE *handles;
    660 	EFI_DEVICE_PATH *dp0, *dp;
    661 	CHAR16 *path;
    662 	char *upath;
    663 	UINTN cols, rows, row = 0;
    664 	int rv;
    665 
    666 	status = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut,
    667 	    ST->ConOut->Mode->Mode, &cols, &rows);
    668 	if (EFI_ERROR(status) || rows <= 2)
    669 		rows = 0;
    670 	else
    671 		rows -= 2;
    672 
    673 	/*
    674 	 * all devices.
    675 	 */
    676 	status = LibLocateHandle(ByProtocol, &DevicePathProtocol, NULL,
    677 	    &nhandles, &handles);
    678 	if (EFI_ERROR(status))
    679 		return;
    680 
    681 	for (i = 0; i < nhandles; i++) {
    682 		status = uefi_call_wrapper(BS->HandleProtocol, 3, handles[i],
    683 		    &DevicePathProtocol, (void **)&dp0);
    684 		if (EFI_ERROR(status))
    685 			break;
    686 
    687 		printf("DevicePathType %d\n", DevicePathType(dp0));
    688 		if (++row >= rows) {
    689 			row = 0;
    690 			printf("Press Any Key to continue :");
    691 			(void) awaitkey(-1, 0);
    692 			printf("\n");
    693 		}
    694 		for (dp = dp0;
    695 		     !IsDevicePathEnd(dp);
    696 		     dp = NextDevicePathNode(dp)) {
    697 
    698 			path = DevicePathToStr(dp);
    699 			upath = NULL;
    700 			rv = ucs2_to_utf8(path, &upath);
    701 			FreePool(path);
    702 			if (rv) {
    703 				printf("convert failed\n");
    704 				break;
    705 			}
    706 
    707 			printf("%d:%d:%s\n", DevicePathType(dp),
    708 			    DevicePathSubType(dp), upath);
    709 			FreePool(upath);
    710 
    711 			if (++row >= rows) {
    712 				row = 0;
    713 				printf("Press Any Key to continue :");
    714 				(void) awaitkey(-1, 0);
    715 				printf("\n");
    716 			}
    717 		}
    718 	}
    719 }
    720 
    721 
    722 void
    723 command_efivar(char *arg)
    724 {
    725 	static const char header[] =
    726 	 "GUID                                 Variable Name        Value\n"
    727 	 "==================================== ==================== ========\n";
    728 	EFI_STATUS status;
    729 	UINTN sz = 64, osz;
    730 	CHAR16 *name = NULL, *tmp, *val, guid[128];
    731 	char *uname, *uval, *uguid;
    732 	EFI_GUID vendor;
    733 	UINTN cols, rows, row = 0;
    734 	int rv;
    735 
    736 	status = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut,
    737 	    ST->ConOut->Mode->Mode, &cols, &rows);
    738 	if (EFI_ERROR(status) || rows <= 2)
    739 		rows = 0;
    740 	else
    741 		rows -= 2;
    742 
    743 	name = AllocatePool(sz);
    744 	if (name == NULL) {
    745 		printf("memory allocation failed: %" PRIuMAX" bytes\n",
    746 		    (uintmax_t)sz);
    747 		return;
    748 	}
    749 
    750 	SetMem(name, sz, 0);
    751 	vendor = NullGuid;
    752 
    753 	printf("%s", header);
    754 	for (;;) {
    755 		osz = sz;
    756 		status = uefi_call_wrapper(RT->GetNextVariableName, 3,
    757 		    &sz, name, &vendor);
    758 		if (EFI_ERROR(status)) {
    759 			if (status == EFI_NOT_FOUND)
    760 				break;
    761 			if (status != EFI_BUFFER_TOO_SMALL) {
    762 				printf("GetNextVariableName failed: %" PRIxMAX "\n",
    763 				    (uintmax_t)status);
    764 				break;
    765 			}
    766 
    767 			tmp = AllocatePool(sz);
    768 			if (tmp == NULL) {
    769 				printf("memory allocation failed: %" PRIuMAX
    770 				    "bytes\n", (uintmax_t)sz);
    771 				break;
    772 			}
    773 			SetMem(tmp, sz, 0);
    774 			CopyMem(tmp, name, osz);
    775 			FreePool(name);
    776 			name = tmp;
    777 			continue;
    778 		}
    779 
    780 		val = LibGetVariable(name, &vendor);
    781 		if (val != NULL) {
    782 			uval = NULL;
    783 			rv = ucs2_to_utf8(val, &uval);
    784 			FreePool(val);
    785 			if (rv) {
    786 				printf("value convert failed\n");
    787 				break;
    788 			}
    789 		} else
    790 			uval = NULL;
    791 		uname = NULL;
    792 		rv = ucs2_to_utf8(name, &uname);
    793 		if (rv) {
    794 			printf("name convert failed\n");
    795 			FreePool(uval);
    796 			break;
    797 		}
    798 		GuidToString(guid, &vendor);
    799 		uguid = NULL;
    800 		rv = ucs2_to_utf8(guid, &uguid);
    801 		if (rv) {
    802 			printf("GUID convert failed\n");
    803 			FreePool(uval);
    804 			FreePool(uname);
    805 			break;
    806 		}
    807 		printf("%-35s %-20s %s\n", uguid, uname, uval ? uval : "(null)");
    808 		FreePool(uguid);
    809 		FreePool(uname);
    810 		if (uval != NULL)
    811 			FreePool(uval);
    812 
    813 		if (++row >= rows) {
    814 			row = 0;
    815 			printf("Press Any Key to continue :");
    816 			(void) awaitkey(-1, 0);
    817 			printf("\n");
    818 		}
    819 	}
    820 
    821 	FreePool(name);
    822 }
    823