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