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