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