blob: 5e333e39d4c7f87c7ef2a3ebeb183598f7a44bc9 [file] [log] [blame]
Toomas Soome199767f2015-10-25 00:06:51 +03001/*-
2 * Copyright (c) 2008-2010 Rui Paulo
3 * Copyright (c) 2006 Marcel Moolenaar
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
Toomas Soome199767f2015-10-25 00:06:51 +030029
30#include <sys/param.h>
31#include <sys/reboot.h>
32#include <sys/boot.h>
33#include <stand.h>
34#include <string.h>
35#include <setjmp.h>
36
37#include <efi.h>
38#include <efilib.h>
39#include <efigpt.h>
40
41#include <bootstrap.h>
42#include <smbios.h>
43
44#ifdef EFI_ZFS_BOOT
45#include <libzfs.h>
46#endif
47
48#include "loader_efi.h"
49
50extern char bootprog_name[];
51extern char bootprog_rev[];
52extern char bootprog_date[];
53extern char bootprog_maker[];
54
55struct arch_switch archsw; /* MI/MD interface boundary */
56
57EFI_GUID acpi = ACPI_TABLE_GUID;
58EFI_GUID acpi20 = ACPI_20_TABLE_GUID;
59EFI_GUID devid = DEVICE_PATH_PROTOCOL;
60EFI_GUID imgid = LOADED_IMAGE_PROTOCOL;
61EFI_GUID mps = MPS_TABLE_GUID;
62EFI_GUID netid = EFI_SIMPLE_NETWORK_PROTOCOL;
63EFI_GUID smbios = SMBIOS_TABLE_GUID;
64EFI_GUID smbios3 = SMBIOS3_TABLE_GUID;
65EFI_GUID dxe = DXE_SERVICES_TABLE_GUID;
66EFI_GUID hoblist = HOB_LIST_TABLE_GUID;
67EFI_GUID memtype = MEMORY_TYPE_INFORMATION_TABLE_GUID;
68EFI_GUID debugimg = DEBUG_IMAGE_INFO_TABLE_GUID;
69EFI_GUID fdtdtb = FDT_TABLE_GUID;
70EFI_GUID inputid = SIMPLE_TEXT_INPUT_PROTOCOL;
71EFI_GUID serial_io = SERIAL_IO_PROTOCOL;
72
Toomas Soomedcba96f2016-10-09 17:12:23 +030073extern void acpi_detect(void);
Toomas Soome199767f2015-10-25 00:06:51 +030074void efi_serial_init(void);
75#ifdef EFI_ZFS_BOOT
76static void efi_zfs_probe(void);
77#endif
78
79/*
80 * Need this because EFI uses UTF-16 unicode string constants, but we
Toomas Soome03502722016-11-11 22:35:51 +020081 * use UTF-8. We can't use printf due to the possibility of \0 and we
82 * don't support wide characters either.
Toomas Soome199767f2015-10-25 00:06:51 +030083 */
84static void
85print_str16(const CHAR16 *str)
86{
87 int i;
88
89 for (i = 0; str[i]; i++)
90 printf("%c", (char)str[i]);
91}
92
93static void
94cp16to8(const CHAR16 *src, char *dst, size_t len)
95{
96 size_t i;
97
98 for (i = 0; i < len && src[i]; i++)
99 dst[i] = (char)src[i];
100}
101
102static int
103has_keyboard(void)
104{
105 EFI_STATUS status;
106 EFI_DEVICE_PATH *path;
107 EFI_HANDLE *hin, *hin_end, *walker;
108 UINTN sz;
109 int retval = 0;
110
111 /*
112 * Find all the handles that support the SIMPLE_TEXT_INPUT_PROTOCOL and
113 * do the typical dance to get the right sized buffer.
114 */
115 sz = 0;
116 hin = NULL;
117 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz, 0);
118 if (status == EFI_BUFFER_TOO_SMALL) {
119 hin = (EFI_HANDLE *)malloc(sz);
120 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz,
121 hin);
122 if (EFI_ERROR(status))
123 free(hin);
124 }
125 if (EFI_ERROR(status))
126 return retval;
127
128 /*
129 * Look at each of the handles. If it supports the device path protocol,
130 * use it to get the device path for this handle. Then see if that
131 * device path matches either the USB device path for keyboards or the
132 * legacy device path for keyboards.
133 */
134 hin_end = &hin[sz / sizeof(*hin)];
135 for (walker = hin; walker < hin_end; walker++) {
136 status = BS->HandleProtocol(*walker, &devid, (VOID **)&path);
137 if (EFI_ERROR(status))
138 continue;
139
140 while (!IsDevicePathEnd(path)) {
141 /*
142 * Check for the ACPI keyboard node. All PNP3xx nodes
143 * are keyboards of different flavors. Note: It is
144 * unclear of there's always a keyboard node when
145 * there's a keyboard controller, or if there's only one
146 * when a keyboard is detected at boot.
147 */
148 if (DevicePathType(path) == ACPI_DEVICE_PATH &&
149 (DevicePathSubType(path) == ACPI_DP ||
150 DevicePathSubType(path) == ACPI_EXTENDED_DP)) {
151 ACPI_HID_DEVICE_PATH *acpi;
152
153 acpi = (ACPI_HID_DEVICE_PATH *)(void *)path;
154 if ((EISA_ID_TO_NUM(acpi->HID) & 0xff00) == 0x300 &&
155 (acpi->HID & 0xffff) == PNP_EISA_ID_CONST) {
156 retval = 1;
157 goto out;
158 }
159 /*
160 * Check for USB keyboard node, if present. Unlike a
161 * PS/2 keyboard, these definitely only appear when
162 * connected to the system.
163 */
164 } else if (DevicePathType(path) == MESSAGING_DEVICE_PATH &&
165 DevicePathSubType(path) == MSG_USB_CLASS_DP) {
166 USB_CLASS_DEVICE_PATH *usb;
167
168 usb = (USB_CLASS_DEVICE_PATH *)(void *)path;
169 if (usb->DeviceClass == 3 && /* HID */
170 usb->DeviceSubClass == 1 && /* Boot devices */
171 usb->DeviceProtocol == 1) { /* Boot keyboards */
172 retval = 1;
173 goto out;
174 }
175 }
176 path = NextDevicePathNode(path);
177 }
178 }
179out:
180 free(hin);
181 return retval;
182}
183
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200184static int
185find_currdev(EFI_LOADED_IMAGE *img, struct devsw **dev, int *unit,
186 uint64_t *extra)
187{
188 EFI_DEVICE_PATH *devpath, *copy;
189 EFI_HANDLE h;
190
191 /*
192 * Try the device handle from our loaded image first. If that
193 * fails, use the device path from the loaded image and see if
194 * any of the nodes in that path match one of the enumerated
195 * handles.
196 */
197 if (efi_handle_lookup(img->DeviceHandle, dev, unit, extra) == 0)
198 return (0);
199
200 copy = NULL;
201 devpath = efi_lookup_image_devpath(IH);
202 while (devpath != NULL) {
203 h = efi_devpath_handle(devpath);
204 if (h == NULL)
205 break;
206
207 free(copy);
208 copy = NULL;
209
210 if (efi_handle_lookup(h, dev, unit, extra) == 0)
211 return (0);
212
213 devpath = efi_lookup_devpath(h);
214 if (devpath != NULL) {
215 copy = efi_devpath_trim(devpath);
216 devpath = copy;
217 }
218 }
219 free(copy);
220
221 return (ENOENT);
222}
223
Toomas Soome199767f2015-10-25 00:06:51 +0300224EFI_STATUS
225main(int argc, CHAR16 *argv[])
226{
227 char var[128];
228 EFI_LOADED_IMAGE *img;
229 EFI_GUID *guid;
230 int i, j, vargood, unit, howto;
231 struct devsw *dev;
232 uint64_t pool_guid;
Toomas Soomedcba96f2016-10-09 17:12:23 +0300233 void *ptr;
Toomas Soome199767f2015-10-25 00:06:51 +0300234 UINTN k;
235 int has_kbd;
236
237 archsw.arch_autoload = efi_autoload;
238 archsw.arch_getdev = efi_getdev;
239 archsw.arch_copyin = efi_copyin;
240 archsw.arch_copyout = efi_copyout;
241 archsw.arch_readin = efi_readin;
242#ifdef EFI_ZFS_BOOT
243 /* Note this needs to be set before ZFS init. */
244 archsw.arch_zfs_probe = efi_zfs_probe;
245#endif
246
247 has_kbd = has_keyboard();
248
249 /*
250 * XXX Chicken-and-egg problem; we want to have console output
251 * early, but some console attributes may depend on reading from
252 * eg. the boot device, which we can't do yet. We can use
253 * printf() etc. once this is done.
254 */
255 cons_probe();
256
257 /*
258 * Initialise the block cache. Set the upper limit.
259 */
260 bcache_init(32768, 512);
261
262 /*
263 * Parse the args to set the console settings, etc
264 * boot1.efi passes these in, if it can read /boot.config or /boot/config
265 * or iPXE may be setup to pass these in.
266 *
267 * Loop through the args, and for each one that contains an '=' that is
268 * not the first character, add it to the environment. This allows
269 * loader and kernel env vars to be passed on the command line. Convert
270 * args from UCS-2 to ASCII (16 to 8 bit) as they are copied.
271 */
272 howto = 0;
273 for (i = 1; i < argc; i++) {
274 if (argv[i][0] == '-') {
275 for (j = 1; argv[i][j] != 0; j++) {
276 int ch;
277
278 ch = argv[i][j];
279 switch (ch) {
280 case 'a':
281 howto |= RB_ASKNAME;
282 break;
283 case 'd':
284 howto |= RB_KDB;
285 break;
286 case 'D':
287 howto |= RB_MULTIPLE;
288 break;
289 case 'h':
290 howto |= RB_SERIAL;
291 break;
292 case 'm':
293 howto |= RB_MUTE;
294 break;
295 case 'p':
296 howto |= RB_PAUSE;
297 break;
298 case 'P':
299 if (!has_kbd)
300 howto |= RB_SERIAL | RB_MULTIPLE;
301 break;
302 case 'r':
303 howto |= RB_DFLTROOT;
304 break;
305 case 's':
306 howto |= RB_SINGLE;
307 break;
308 case 'S':
309 if (argv[i][j + 1] == 0) {
310 if (i + 1 == argc) {
311 setenv("comconsole_speed", "115200", 1);
312 } else {
313 cp16to8(&argv[i + 1][0], var,
314 sizeof(var));
315 setenv("comconsole_speedspeed", var, 1);
316 }
317 i++;
318 break;
319 } else {
320 cp16to8(&argv[i][j + 1], var,
321 sizeof(var));
322 setenv("comconsole_speed", var, 1);
323 break;
324 }
325 case 'v':
326 howto |= RB_VERBOSE;
327 break;
328 }
329 }
330 } else {
331 vargood = 0;
332 for (j = 0; argv[i][j] != 0; j++) {
333 if (j == sizeof(var)) {
334 vargood = 0;
335 break;
336 }
337 if (j > 0 && argv[i][j] == '=')
338 vargood = 1;
339 var[j] = (char)argv[i][j];
340 }
341 if (vargood) {
342 var[j] = 0;
343 putenv(var);
344 }
345 }
346 }
347 for (i = 0; howto_names[i].ev != NULL; i++)
348 if (howto & howto_names[i].mask)
349 setenv(howto_names[i].ev, "YES", 1);
350 if (howto & RB_MULTIPLE) {
351 if (howto & RB_SERIAL)
352 setenv("console", "ttya text" , 1);
353 else
354 setenv("console", "text ttya" , 1);
355 } else if (howto & RB_SERIAL) {
356 setenv("console", "ttya" , 1);
357 }
358
359 if (efi_copy_init()) {
360 printf("failed to allocate staging area\n");
361 return (EFI_BUFFER_TOO_SMALL);
362 }
363
364 /*
365 * March through the device switch probing for things.
366 */
367 for (i = 0; devsw[i] != NULL; i++)
368 if (devsw[i]->dv_init != NULL)
369 (devsw[i]->dv_init)();
370
371 /* Get our loaded image protocol interface structure. */
372 BS->HandleProtocol(IH, &imgid, (VOID**)&img);
373
374 printf("Command line arguments:");
375 for (i = 0; i < argc; i++) {
376 printf(" ");
377 print_str16(argv[i]);
378 }
379 printf("\n");
380
381 printf("Image base: 0x%lx\n", (u_long)img->ImageBase);
382 printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16,
383 ST->Hdr.Revision & 0xffff);
384 printf("EFI Firmware: ");
385 /* printf doesn't understand EFI Unicode */
386 ST->ConOut->OutputString(ST->ConOut, ST->FirmwareVendor);
387 printf(" (rev %d.%02d)\n", ST->FirmwareRevision >> 16,
388 ST->FirmwareRevision & 0xffff);
389
390 printf("\n");
391 printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
392 printf("(%s, %s)\n", bootprog_maker, bootprog_date);
393
394 /*
395 * Disable the watchdog timer. By default the boot manager sets
396 * the timer to 5 minutes before invoking a boot option. If we
397 * want to return to the boot manager, we have to disable the
398 * watchdog timer and since we're an interactive program, we don't
399 * want to wait until the user types "quit". The timer may have
400 * fired by then. We don't care if this fails. It does not prevent
401 * normal functioning in any way...
402 */
403 BS->SetWatchdogTimer(0, 0, 0, NULL);
404
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200405 if (find_currdev(img, &dev, &unit, &pool_guid) != 0)
Toomas Soome199767f2015-10-25 00:06:51 +0300406 return (EFI_NOT_FOUND);
407
408 switch (dev->dv_type) {
409#ifdef EFI_ZFS_BOOT
410 case DEVT_ZFS: {
411 struct zfs_devdesc currdev;
412
413 currdev.d_dev = dev;
414 currdev.d_unit = unit;
415 currdev.d_type = currdev.d_dev->dv_type;
416 currdev.d_opendata = NULL;
417 currdev.pool_guid = pool_guid;
418 currdev.root_guid = 0;
419 env_setenv("currdev", EV_VOLATILE, efi_fmtdev(&currdev),
420 efi_setcurrdev, env_nounset);
421 env_setenv("loaddev", EV_VOLATILE, efi_fmtdev(&currdev), env_noset,
422 env_nounset);
423#ifdef __FreeBSD__
424 init_zfs_bootenv(zfs_fmtdev(&currdev));
425#endif
426 break;
427 }
428#endif
429 default: {
430 struct devdesc currdev;
431
432 currdev.d_dev = dev;
433 currdev.d_unit = unit;
434 currdev.d_opendata = NULL;
435 currdev.d_type = currdev.d_dev->dv_type;
436 env_setenv("currdev", EV_VOLATILE, efi_fmtdev(&currdev),
437 efi_setcurrdev, env_nounset);
438 env_setenv("loaddev", EV_VOLATILE, efi_fmtdev(&currdev), env_noset,
439 env_nounset);
440 break;
441 }
442 }
443
444 setenv("LINES", "24", 1); /* optional */
445 setenv("COLUMNS", "80", 1); /* optional */
446 setenv("ISADIR", "amd64", 1); /* we only build 64bit */
Toomas Soomedcba96f2016-10-09 17:12:23 +0300447 acpi_detect();
Toomas Soome199767f2015-10-25 00:06:51 +0300448
Toomas Soomedcba96f2016-10-09 17:12:23 +0300449 if ((ptr = efi_get_table(&smbios3)) == NULL)
450 ptr = efi_get_table(&smbios);
451 smbios_detect(ptr);
Toomas Soome199767f2015-10-25 00:06:51 +0300452
453 efi_serial_init(); /* detect and set up serial ports */
454 interact(NULL); /* doesn't return */
455
456 return (EFI_SUCCESS); /* keep compiler happy */
457}
458
459COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
460
461static int
462command_reboot(int argc __attribute((unused)),
463 char *argv[] __attribute((unused)))
464{
465 int i;
466 const CHAR16 *msg = L"Reboot from the loader";
467
468 for (i = 0; devsw[i] != NULL; ++i)
469 if (devsw[i]->dv_cleanup != NULL)
470 (devsw[i]->dv_cleanup)();
471
472 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 23, (CHAR16 *)msg);
473
474 /* NOTREACHED */
475 return (CMD_ERROR);
476}
477
478COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
479
480static int
481command_memmap(int argc __attribute((unused)),
482 char *argv[] __attribute((unused)))
483{
484 UINTN sz;
485 EFI_MEMORY_DESCRIPTOR *map, *p;
486 UINTN key, dsz;
487 UINT32 dver;
488 EFI_STATUS status;
489 int i, ndesc;
490 int rv = 0;
491 char line[80];
492 static const char *types[] = {
493 "Reserved",
494 "LoaderCode",
495 "LoaderData",
496 "BootServicesCode",
497 "BootServicesData",
498 "RuntimeServicesCode",
499 "RuntimeServicesData",
500 "ConventionalMemory",
501 "UnusableMemory",
502 "ACPIReclaimMemory",
503 "ACPIMemoryNVS",
504 "MemoryMappedIO",
505 "MemoryMappedIOPortSpace",
506 "PalCode"
507 };
508
509 sz = 0;
510 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
511 if (status != EFI_BUFFER_TOO_SMALL) {
512 printf("Can't determine memory map size\n");
513 return (CMD_ERROR);
514 }
515 map = malloc(sz);
516 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
517 if (EFI_ERROR(status)) {
518 printf("Can't read memory map\n");
519 return (CMD_ERROR);
520 }
521
522 ndesc = sz / dsz;
523 snprintf(line, 80, "%23s %12s %12s %8s %4s\n",
524 "Type", "Physical", "Virtual", "#Pages", "Attr");
525 pager_open();
526 rv = pager_output(line);
527 if (rv) {
528 pager_close();
529 return (CMD_OK);
530 }
531
532 for (i = 0, p = map; i < ndesc;
533 i++, p = NextMemoryDescriptor(p, dsz)) {
534 snprintf(line, 80, "%23s %012lx %012lx %08lx ",
535 types[p->Type],
536 p->PhysicalStart,
537 p->VirtualStart,
538 p->NumberOfPages);
539 rv = pager_output(line);
540 if (rv)
541 break;
542
543 if (p->Attribute & EFI_MEMORY_UC)
544 printf("UC ");
545 if (p->Attribute & EFI_MEMORY_WC)
546 printf("WC ");
547 if (p->Attribute & EFI_MEMORY_WT)
548 printf("WT ");
549 if (p->Attribute & EFI_MEMORY_WB)
550 printf("WB ");
551 if (p->Attribute & EFI_MEMORY_UCE)
552 printf("UCE ");
553 if (p->Attribute & EFI_MEMORY_WP)
554 printf("WP ");
555 if (p->Attribute & EFI_MEMORY_RP)
556 printf("RP ");
557 if (p->Attribute & EFI_MEMORY_XP)
558 printf("XP ");
559 rv = pager_output("\n");
560 if (rv)
561 break;
562 }
563
564 pager_close();
565 return (CMD_OK);
566}
567
568COMMAND_SET(configuration, "configuration", "print configuration tables",
569 command_configuration);
570
571static const char *
572guid_to_string(EFI_GUID *guid)
573{
574 static char buf[40];
575
576 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
577 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
578 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
579 guid->Data4[5], guid->Data4[6], guid->Data4[7]);
580 return (buf);
581}
582
583static int
584command_configuration(int argc __attribute((unused)),
585 char *argv[] __attribute((unused)))
586{
587 UINTN i;
588
589 printf("NumberOfTableEntries=%lu\n",
590 (unsigned long)ST->NumberOfTableEntries);
591 for (i = 0; i < ST->NumberOfTableEntries; i++) {
592 EFI_GUID *guid;
593
594 printf(" ");
595 guid = &ST->ConfigurationTable[i].VendorGuid;
596 if (!memcmp(guid, &mps, sizeof(EFI_GUID)))
597 printf("MPS Table");
598 else if (!memcmp(guid, &acpi, sizeof(EFI_GUID)))
599 printf("ACPI Table");
600 else if (!memcmp(guid, &acpi20, sizeof(EFI_GUID)))
601 printf("ACPI 2.0 Table");
602 else if (!memcmp(guid, &smbios, sizeof(EFI_GUID)))
603 printf("SMBIOS Table");
604 else if (!memcmp(guid, &smbios3, sizeof(EFI_GUID)))
605 printf("SMBIOS3 Table");
606 else if (!memcmp(guid, &dxe, sizeof(EFI_GUID)))
607 printf("DXE Table");
608 else if (!memcmp(guid, &hoblist, sizeof(EFI_GUID)))
609 printf("HOB List Table");
610 else if (!memcmp(guid, &memtype, sizeof(EFI_GUID)))
611 printf("Memory Type Information Table");
612 else if (!memcmp(guid, &debugimg, sizeof(EFI_GUID)))
613 printf("Debug Image Info Table");
614 else if (!memcmp(guid, &fdtdtb, sizeof(EFI_GUID)))
615 printf("FDT Table");
616 else
617 printf("Unknown Table (%s)", guid_to_string(guid));
618 printf(" at %p\n", ST->ConfigurationTable[i].VendorTable);
619 }
620
621 return (CMD_OK);
622}
623
624
625COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode);
626
627static int
628command_mode(int argc, char *argv[])
629{
630 UINTN cols, rows;
631 unsigned int mode;
632 int i;
633 char *cp;
634 char rowenv[8];
635 EFI_STATUS status;
636 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
637 extern void HO(void);
638
639 conout = ST->ConOut;
640
641 if (argc > 1) {
642 mode = strtol(argv[1], &cp, 0);
643 if (cp[0] != '\0') {
644 printf("Invalid mode\n");
645 return (CMD_ERROR);
646 }
647 status = conout->QueryMode(conout, mode, &cols, &rows);
648 if (EFI_ERROR(status)) {
649 printf("invalid mode %d\n", mode);
650 return (CMD_ERROR);
651 }
652 status = conout->SetMode(conout, mode);
653 if (EFI_ERROR(status)) {
654 printf("couldn't set mode %d\n", mode);
655 return (CMD_ERROR);
656 }
657 sprintf(rowenv, "%u", (unsigned)rows);
658 setenv("LINES", rowenv, 1);
659 sprintf(rowenv, "%u", (unsigned)cols);
660 setenv("COLUMNS", rowenv, 1);
661 HO(); /* set cursor */
662 return (CMD_OK);
663 }
664
665 printf("Current mode: %d\n", conout->Mode->Mode);
666 for (i = 0; i <= conout->Mode->MaxMode; i++) {
667 status = conout->QueryMode(conout, i, &cols, &rows);
668 if (EFI_ERROR(status))
669 continue;
670 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols,
671 (unsigned)rows);
672 }
673
674 if (i != 0)
675 printf("Select a mode with the command \"mode <number>\"\n");
676
677 return (CMD_OK);
678}
679
680
681COMMAND_SET(nvram, "nvram", "get NVRAM variables", command_nvram);
682
683static int
684command_nvram(int argc __attribute((unused)),
685 char *argv[] __attribute((unused)))
686{
687 CHAR16 var[128];
688 UINT8 *data; /* value is in bytes */
689 EFI_STATUS status;
690 EFI_GUID varguid = { 0,0,0,{0,0,0,0,0,0,0,0} };
691 UINTN varsz, datasz, i;
692 UINT32 attr;
693 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
694
695 conout = ST->ConOut;
696
697 pager_open();
698 var[0] = 0; /* Initiate the enumeration */
699 varsz = 128;
700
701 for (status = RS->GetNextVariableName(&varsz, var, &varguid);
702 status != EFI_NOT_FOUND;
703 status = RS->GetNextVariableName(&varsz, var, &varguid)) {
704
705 /*
706 * as term emu is keeping track on cursor, use putchar().
707 */
708 for (i = 0; var[i] != 0; i++)
709 putchar(var[i]);
710 varsz = 128; /* GetNextVariableName() did change it. */
711
712 printf(": Attributes:");
713 datasz = 0;
714 status = RS->GetVariable(var, &varguid, &attr, &datasz, NULL);
715 if ((data = malloc(datasz)) == NULL)
716 break;
717 status = RS->GetVariable(var, &varguid, &attr, &datasz, data);
718 if (EFI_ERROR(status))
719 printf("<error retrieving variable>");
720 else {
721 if (attr & EFI_VARIABLE_NON_VOLATILE)
722 printf(" NV");
723 if (attr & EFI_VARIABLE_BOOTSERVICE_ACCESS)
724 printf(" BS");
725 if (attr & EFI_VARIABLE_RUNTIME_ACCESS)
726 printf(" RS");
727 if (attr & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
728 printf(" HR");
729 if (attr & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
730 printf(" AW");
731 if (attr &
732 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
733 printf(" TW");
734
735 printf(": DataLength: %lld", (long long)datasz);
736 }
737 free(data);
738 if (pager_output("\n"))
739 break;
740 }
741
742 pager_close();
743 return (CMD_OK);
744}
745
746struct protocol_name {
747 EFI_GUID guid;
748 const char *name;
749} proto_names[] = {
750 { DEVICE_PATH_PROTOCOL, "device path" },
751 { BLOCK_IO_PROTOCOL, "block io" },
752 { DISK_IO_PROTOCOL, "disk io" },
753 { EFI_DISK_INFO_PROTOCOL_GUID, "disk info" },
754 { SIMPLE_FILE_SYSTEM_PROTOCOL, "simple fs" },
755 { LOAD_FILE_PROTOCOL, "load file" },
756 { DEVICE_IO_PROTOCOL, "device io" },
757 { UNICODE_COLLATION_PROTOCOL, "unicode collation" },
758 { EFI_UNICODE_COLLATION2_PROTOCOL_GUID, "unicode collation2" },
759 { EFI_SIMPLE_NETWORK_PROTOCOL, "simple network" },
760 { SIMPLE_TEXT_OUTPUT_PROTOCOL, "simple text output" },
761 { SIMPLE_TEXT_INPUT_PROTOCOL, "simple text input" },
762 { EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID, "simple text ex input" },
763 { EFI_CONSOLE_CONTROL_PROTOCOL_GUID, "console control" },
764 { EFI_CONSOLE_IN_DEVICE_GUID, "stdin" },
765 { EFI_CONSOLE_OUT_DEVICE_GUID, "stdout" },
766 { EFI_STANDARD_ERROR_DEVICE_GUID, "stderr" },
767 { EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID, "GOP" },
768 { EFI_UGA_DRAW_PROTOCOL_GUID, "UGA draw" },
769 { EFI_PXE_BASE_CODE_PROTOCOL, "PXE base code" },
770 { EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL, "PXE base code callback" },
771 { SERIAL_IO_PROTOCOL, "serial io" },
772 { LOADED_IMAGE_PROTOCOL, "loaded image" },
773 { EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID,
774 "loaded image device path" },
775 { EFI_ISA_IO_PROTOCOL_GUID, "ISA io" },
776 { EFI_IDE_CONTROLLER_INIT_PROTOCOL_GUID, "IDE controller init" },
777 { EFI_ISA_ACPI_PROTOCOL_GUID, "ISA ACPI" },
778 { EFI_PCI_IO_PROTOCOL_GUID, "PCI" },
779 { EFI_PCI_ROOT_IO_GUID, "PCI root" },
780 { EFI_PCI_ENUMERATION_COMPLETE_GUID, "PCI enumeration" },
781 { EFI_DRIVER_DIAGNOSTICS_PROTOCOL_GUID, "Driver diagnostics" },
782 { EFI_DRIVER_DIAGNOSTICS2_PROTOCOL_GUID, "Driver diagnostics2" },
783 { EFI_SIMPLE_POINTER_PROTOCOL_GUID, "simple pointer" },
784 { EFI_ABSOLUTE_POINTER_PROTOCOL_GUID, "absolute pointer" },
785 { EFI_VLAN_CONFIG_PROTOCOL_GUID, "VLAN config" },
786 { EFI_ARP_SERVICE_BINDING_PROTOCOL_GUID, "ARP service binding" },
787 { EFI_ARP_PROTOCOL_GUID, "ARP" },
788 { EFI_IP4_SERVICE_BINDING_PROTOCOL, "IPv4 service binding" },
789 { EFI_IP4_PROTOCOL, "IPv4" },
790 { EFI_IP4_CONFIG_PROTOCOL_GUID, "IPv4 config" },
791 { EFI_IP6_SERVICE_BINDING_PROTOCOL, "IPv6 service binding" },
792 { EFI_IP6_PROTOCOL, "IPv6" },
793 { EFI_IP6_CONFIG_PROTOCOL_GUID, "IPv6 config" },
794 { EFI_UDP4_PROTOCOL, "UDPv4" },
795 { EFI_UDP4_SERVICE_BINDING_PROTOCOL, "UDPv4 service binding" },
796 { EFI_UDP6_PROTOCOL, "UDPv6" },
797 { EFI_UDP6_SERVICE_BINDING_PROTOCOL, "UDPv6 service binding" },
798 { EFI_TCP4_PROTOCOL, "TCPv4" },
799 { EFI_TCP4_SERVICE_BINDING_PROTOCOL, "TCPv4 service binding" },
800 { EFI_TCP6_PROTOCOL, "TCPv6" },
801 { EFI_TCP6_SERVICE_BINDING_PROTOCOL, "TCPv6 service binding" },
802 { EFI_PART_TYPE_EFI_SYSTEM_PART_GUID, "EFI System partition" },
803 { EFI_PART_TYPE_LEGACY_MBR_GUID, "MBR legacy" },
804 { EFI_DEVICE_TREE_GUID, "device tree" },
805 { EFI_USB_IO_PROTOCOL_GUID, "USB io" },
806 { EFI_USB2_HC_PROTOCOL_GUID, "USB2 HC" },
807 { EFI_COMPONENT_NAME_PROTOCOL_GUID, "component name" },
808 { EFI_COMPONENT_NAME2_PROTOCOL_GUID, "component name2" },
809 { EFI_DRIVER_BINDING_PROTOCOL_GUID, "driver binding" },
810 { EFI_DRIVER_CONFIGURATION_PROTOCOL_GUID, "driver configuration" },
811 { EFI_DRIVER_CONFIGURATION2_PROTOCOL_GUID, "driver configuration2" },
812 { EFI_DECOMPRESS_PROTOCOL_GUID, "decompress" },
813 { EFI_EBC_INTERPRETER_PROTOCOL_GUID, "ebc interpreter" },
814 { EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL,
815 "network interface identifier" },
816 { EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL_31,
817 "network interface identifier_31" },
818 { EFI_MANAGED_NETWORK_SERVICE_BINDING_PROTOCOL_GUID,
819 "managed network service binding" },
820 { EFI_MANAGED_NETWORK_PROTOCOL_GUID, "managed network" },
821 { EFI_FORM_BROWSER2_PROTOCOL_GUID, "form browser" },
822 { EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID, "HII config routing" },
823 { EFI_HII_DATABASE_PROTOCOL_GUID, "HII database" },
824 { EFI_HII_STRING_PROTOCOL_GUID, "HII string" },
825 { EFI_HII_IMAGE_PROTOCOL_GUID, "HII image" },
826 { EFI_HII_FONT_PROTOCOL_GUID, "HII font" },
827 { EFI_HII_CONFIGURATION_ACCESS_PROTOCOL_GUID, "HII config" },
828 { EFI_MTFTP4_SERVICE_BINDING_PROTOCOL_GUID, "MTFTP4 service binding" },
829 { EFI_MTFTP4_PROTOCOL_GUID, "MTFTP4" },
830 { EFI_MTFTP6_SERVICE_BINDING_PROTOCOL_GUID, "MTFTP6 service binding" },
831 { EFI_MTFTP6_PROTOCOL_GUID, "MTFTP6" },
832 { EFI_DHCP4_SERVICE_BINDING_PROTOCOL_GUID, "DHCP4 service binding" },
833 { EFI_DHCP4_PROTOCOL_GUID, "DHCP4" },
834 { EFI_DHCP6_SERVICE_BINDING_PROTOCOL_GUID, "DHCP6 service binding" },
835 { EFI_DHCP6_PROTOCOL_GUID, "DHCP6" },
836 { EFI_SCSI_IO_PROTOCOL_GUID, "SCSI io" },
837 { EFI_SCSI_PASS_THRU_PROTOCOL_GUID, "SCSI pass thru" },
838 { EFI_EXT_SCSI_PASS_THRU_PROTOCOL_GUID, "SCSI pass thru ext" },
839 { EFI_CAPSULE_ARCH_PROTOCOL_GUID, "Capsule arch" },
840 { EFI_MONOTONIC_COUNTER_ARCH_PROTOCOL_GUID, "monotonic counter arch" },
841 { EFI_REALTIME_CLOCK_ARCH_PROTOCOL_GUID, "realtime clock arch" },
842 { EFI_VARIABLE_ARCH_PROTOCOL_GUID, "variable arch" },
843 { EFI_VARIABLE_WRITE_ARCH_PROTOCOL_GUID, "variable write arch" },
844 { EFI_WATCHDOG_TIMER_ARCH_PROTOCOL_GUID, "watchdog timer arch" },
845 { EFI_MP_SERVICES_PROTOCOL_GUID, "MP services" },
846 { EFI_ACPI_SUPPORT_PROTOCOL_GUID, "ACPI support" },
847 { EFI_BDS_ARCH_PROTOCOL_GUID, "BDS arch" },
848 { EFI_METRONOME_ARCH_PROTOCOL_GUID, "metronome arch" },
849 { EFI_TIMER_ARCH_PROTOCOL_GUID, "timer arch" },
850 { EFI_DPC_PROTOCOL_GUID, "DPC" },
851 { EFI_PRINT2_PROTOCOL_GUID, "print2" },
852 { EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID, "device path to text" },
853 { EFI_RESET_ARCH_PROTOCOL_GUID, "reset arch" },
854 { EFI_CPU_ARCH_PROTOCOL_GUID, "CPU arch" },
855 { EFI_CPU_IO2_PROTOCOL_GUID, "CPU IO2" },
856 { EFI_LEGACY_8259_PROTOCOL_GUID, "Legacy 8259" },
857 { EFI_SECURITY_ARCH_PROTOCOL_GUID, "Security arch" },
858 { EFI_SECURITY2_ARCH_PROTOCOL_GUID, "Security2 arch" },
859 { EFI_RUNTIME_ARCH_PROTOCOL_GUID, "Runtime arch" },
860 { EFI_STATUS_CODE_RUNTIME_PROTOCOL_GUID, "status code runtime" },
861 { EFI_DATA_HUB_PROTOCOL_GUID, "data hub" },
862 { PCD_PROTOCOL_GUID, "PCD" },
863 { EFI_PCD_PROTOCOL_GUID, "EFI PCD" },
864 { EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL_GUID, "firmware volume block" },
865 { EFI_FIRMWARE_VOLUME2_PROTOCOL_GUID, "firmware volume2" },
866 { EFI_FIRMWARE_VOLUME_DISPATCH_PROTOCOL_GUID,
867 "firmware volume dispatch" },
868 { LZMA_COMPRESS_GUID, "lzma compress" },
869 { { 0,0,0,{0,0,0,0,0,0,0,0} }, NULL } /* must be last entry */
870};
871
872COMMAND_SET(lsefi, "lsefi", "list EFI handles", command_lsefi);
873
874static int
875command_lsefi(int argc __attribute((unused)),
876 char *argv[] __attribute((unused)))
877{
878 EFI_HANDLE *buffer = NULL;
879 EFI_HANDLE handle;
880 UINTN bufsz = 0, i, j;
881 EFI_STATUS status;
882 int k, ret;
883
884 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
885 if (status != EFI_BUFFER_TOO_SMALL) {
886 snprintf(command_errbuf, sizeof (command_errbuf),
887 "unexpected error: %lld", (long long)status);
888 return (CMD_ERROR);
889 }
890 if ((buffer = malloc(bufsz)) == NULL) {
891 sprintf(command_errbuf, "out of memory");
892 return (CMD_ERROR);
893 }
894
895 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
896 if (EFI_ERROR(status)) {
897 free(buffer);
898 snprintf(command_errbuf, sizeof (command_errbuf),
899 "LocateHandle() error: %lld", (long long)status);
900 return (CMD_ERROR);
901 }
902
903 pager_open();
904 for (i = 0; i < (bufsz / sizeof (EFI_HANDLE)); i++) {
905 UINTN nproto = 0;
906 EFI_GUID **protocols = NULL;
907
908 handle = buffer[i];
909 printf("Handle %p", handle);
910 if (pager_output("\n"))
911 break;
912 /* device path */
913
914 status = BS->ProtocolsPerHandle(handle, &protocols, &nproto);
915 if (EFI_ERROR(status)) {
916 snprintf(command_errbuf, sizeof (command_errbuf),
917 "ProtocolsPerHandle() error: %lld",
918 (long long)status);
919 continue;
920 }
921 for (j = 0; j < nproto; j++) {
922 for (k = 0; proto_names[k].name != NULL; k++)
923 if (memcmp(protocols[j], &proto_names[k].guid,
924 sizeof (proto_names[k].guid)) == 0)
925 break;
926 if (proto_names[k].name != NULL)
927 printf(" %s", proto_names[k].name);
928 else
929 printf(" %s", guid_to_string(protocols[j]));
930 ret = pager_output("\n");
931 if (ret)
932 break;
933 }
934 BS->FreePool(protocols);
935 if (ret)
936 break;
937 }
938 pager_close();
939 free(buffer);
940 return (CMD_OK);
941}
942
943#ifdef EFI_ZFS_BOOT
944COMMAND_SET(lszfs, "lszfs", "list child datasets of a zfs dataset",
945 command_lszfs);
946
947static int
948command_lszfs(int argc, char *argv[])
949{
950 int err;
951
952 if (argc != 2) {
953 command_errmsg = "wrong number of arguments";
954 return (CMD_ERROR);
955 }
956
957 err = zfs_list(argv[1]);
958 if (err != 0) {
959 command_errmsg = strerror(err);
960 return (CMD_ERROR);
961 }
962 return (CMD_OK);
963}
964
965#ifdef __FreeBSD__
966COMMAND_SET(reloadbe, "reloadbe", "refresh the list of ZFS Boot Environments",
967 command_reloadbe);
968
969static int
970command_reloadbe(int argc, char *argv[])
971{
972 int err;
973 char *root;
974
975 if (argc > 2) {
976 command_errmsg = "wrong number of arguments";
977 return (CMD_ERROR);
978 }
979
980 if (argc == 2) {
981 err = zfs_bootenv(argv[1]);
982 } else {
983 root = getenv("zfs_be_root");
984 if (root == NULL) {
985 return (CMD_OK);
986 }
987 err = zfs_bootenv(root);
988 }
989
990 if (err != 0) {
991 command_errmsg = strerror(err);
992 return (CMD_ERROR);
993 }
994
995 return (CMD_OK);
996}
997#endif /* __FreeBSD__ */
998#endif
999
1000void
1001efi_serial_init(void)
1002{
1003 EFI_HANDLE *buffer = NULL;
1004 UINTN bufsz = 0, i;
1005 EFI_STATUS status;
1006 int serial = 0;
1007
1008 /*
1009 * get buffer size
1010 */
1011 status = BS->LocateHandle(ByProtocol, &serial_io, NULL, &bufsz, buffer);
1012 if (status != EFI_BUFFER_TOO_SMALL) {
1013 snprintf(command_errbuf, sizeof (command_errbuf),
1014 "unexpected error: %lld", (long long)status);
1015 return;
1016 }
1017 if ((buffer = malloc(bufsz)) == NULL) {
1018 sprintf(command_errbuf, "out of memory");
1019 return;
1020 }
1021
1022 /*
1023 * get handle array
1024 */
1025 status = BS->LocateHandle(ByProtocol, &serial_io, NULL, &bufsz, buffer);
1026 if (EFI_ERROR(status)) {
1027 free(buffer);
1028 snprintf(command_errbuf, sizeof (command_errbuf),
1029 "LocateHandle() error: %lld", (long long)status);
1030 return;
1031 }
1032
1033 for (i = 0; i < (bufsz / sizeof (EFI_HANDLE)); i++) {
1034 SERIAL_IO_INTERFACE *sio;
1035 status = BS->OpenProtocol(buffer[i], &serial_io, (void**)&sio,
1036 IH, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1037 if (EFI_ERROR(status)) {
1038 snprintf(command_errbuf, sizeof (command_errbuf),
1039 "OpenProtocol() error: %lld", (long long)status);
1040 }
1041 printf("serial# %d\n", serial++);
1042 }
1043
1044 free(buffer);
1045}
1046
1047#ifdef LOADER_FDT_SUPPORT
1048extern int command_fdt_internal(int argc, char *argv[]);
1049
1050/*
1051 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
1052 * and declaring it as extern is in contradiction with COMMAND_SET() macro
1053 * (which uses static pointer), we're defining wrapper function, which
1054 * calls the proper fdt handling routine.
1055 */
1056static int
1057command_fdt(int argc, char *argv[])
1058{
1059 return (command_fdt_internal(argc, argv));
1060}
1061
1062COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);
1063#endif
1064
1065#ifdef EFI_ZFS_BOOT
1066static void
1067efi_zfs_probe(void)
1068{
1069 EFI_HANDLE h;
1070 u_int unit;
1071 int i;
1072 char dname[SPECNAMELEN + 1];
1073 uint64_t guid;
1074
1075 unit = 0;
1076 h = efi_find_handle(&efipart_dev, 0);
1077 for (i = 0; h != NULL; h = efi_find_handle(&efipart_dev, ++i)) {
1078 snprintf(dname, sizeof(dname), "%s%d:", efipart_dev.dv_name, i);
1079 if (zfs_probe_dev(dname, &guid) == 0)
1080 (void)efi_handle_update_dev(h, &zfs_dev, unit++, guid);
1081 }
1082}
1083#endif