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