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