blob: cd45451889d0475e1d6b3cad13e6b6b4193d6fd9 [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
Toomas Soome04f8e092016-11-10 21:05:15 +0200247 /* Init the time source */
248 efi_time_init();
249
Toomas Soome199767f2015-10-25 00:06:51 +0300250 has_kbd = has_keyboard();
251
252 /*
253 * XXX Chicken-and-egg problem; we want to have console output
254 * early, but some console attributes may depend on reading from
255 * eg. the boot device, which we can't do yet. We can use
256 * printf() etc. once this is done.
257 */
258 cons_probe();
259
260 /*
261 * Initialise the block cache. Set the upper limit.
262 */
263 bcache_init(32768, 512);
264
265 /*
266 * Parse the args to set the console settings, etc
267 * boot1.efi passes these in, if it can read /boot.config or /boot/config
268 * or iPXE may be setup to pass these in.
269 *
270 * Loop through the args, and for each one that contains an '=' that is
271 * not the first character, add it to the environment. This allows
272 * loader and kernel env vars to be passed on the command line. Convert
273 * args from UCS-2 to ASCII (16 to 8 bit) as they are copied.
274 */
275 howto = 0;
276 for (i = 1; i < argc; i++) {
277 if (argv[i][0] == '-') {
278 for (j = 1; argv[i][j] != 0; j++) {
279 int ch;
280
281 ch = argv[i][j];
282 switch (ch) {
283 case 'a':
284 howto |= RB_ASKNAME;
285 break;
286 case 'd':
287 howto |= RB_KDB;
288 break;
289 case 'D':
290 howto |= RB_MULTIPLE;
291 break;
292 case 'h':
293 howto |= RB_SERIAL;
294 break;
295 case 'm':
296 howto |= RB_MUTE;
297 break;
298 case 'p':
299 howto |= RB_PAUSE;
300 break;
301 case 'P':
302 if (!has_kbd)
303 howto |= RB_SERIAL | RB_MULTIPLE;
304 break;
305 case 'r':
306 howto |= RB_DFLTROOT;
307 break;
308 case 's':
309 howto |= RB_SINGLE;
310 break;
311 case 'S':
312 if (argv[i][j + 1] == 0) {
313 if (i + 1 == argc) {
314 setenv("comconsole_speed", "115200", 1);
315 } else {
316 cp16to8(&argv[i + 1][0], var,
317 sizeof(var));
318 setenv("comconsole_speedspeed", var, 1);
319 }
320 i++;
321 break;
322 } else {
323 cp16to8(&argv[i][j + 1], var,
324 sizeof(var));
325 setenv("comconsole_speed", var, 1);
326 break;
327 }
328 case 'v':
329 howto |= RB_VERBOSE;
330 break;
331 }
332 }
333 } else {
334 vargood = 0;
335 for (j = 0; argv[i][j] != 0; j++) {
336 if (j == sizeof(var)) {
337 vargood = 0;
338 break;
339 }
340 if (j > 0 && argv[i][j] == '=')
341 vargood = 1;
342 var[j] = (char)argv[i][j];
343 }
344 if (vargood) {
345 var[j] = 0;
346 putenv(var);
347 }
348 }
349 }
350 for (i = 0; howto_names[i].ev != NULL; i++)
351 if (howto & howto_names[i].mask)
352 setenv(howto_names[i].ev, "YES", 1);
353 if (howto & RB_MULTIPLE) {
354 if (howto & RB_SERIAL)
355 setenv("console", "ttya text" , 1);
356 else
357 setenv("console", "text ttya" , 1);
358 } else if (howto & RB_SERIAL) {
359 setenv("console", "ttya" , 1);
360 }
361
362 if (efi_copy_init()) {
363 printf("failed to allocate staging area\n");
364 return (EFI_BUFFER_TOO_SMALL);
365 }
366
367 /*
368 * March through the device switch probing for things.
369 */
370 for (i = 0; devsw[i] != NULL; i++)
371 if (devsw[i]->dv_init != NULL)
372 (devsw[i]->dv_init)();
373
374 /* Get our loaded image protocol interface structure. */
375 BS->HandleProtocol(IH, &imgid, (VOID**)&img);
376
377 printf("Command line arguments:");
378 for (i = 0; i < argc; i++) {
379 printf(" ");
380 print_str16(argv[i]);
381 }
382 printf("\n");
383
384 printf("Image base: 0x%lx\n", (u_long)img->ImageBase);
385 printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16,
386 ST->Hdr.Revision & 0xffff);
387 printf("EFI Firmware: ");
388 /* printf doesn't understand EFI Unicode */
389 ST->ConOut->OutputString(ST->ConOut, ST->FirmwareVendor);
390 printf(" (rev %d.%02d)\n", ST->FirmwareRevision >> 16,
391 ST->FirmwareRevision & 0xffff);
392
393 printf("\n");
394 printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
395 printf("(%s, %s)\n", bootprog_maker, bootprog_date);
396
397 /*
398 * Disable the watchdog timer. By default the boot manager sets
399 * the timer to 5 minutes before invoking a boot option. If we
400 * want to return to the boot manager, we have to disable the
401 * watchdog timer and since we're an interactive program, we don't
402 * want to wait until the user types "quit". The timer may have
403 * fired by then. We don't care if this fails. It does not prevent
404 * normal functioning in any way...
405 */
406 BS->SetWatchdogTimer(0, 0, 0, NULL);
407
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200408 if (find_currdev(img, &dev, &unit, &pool_guid) != 0)
Toomas Soome199767f2015-10-25 00:06:51 +0300409 return (EFI_NOT_FOUND);
410
411 switch (dev->dv_type) {
412#ifdef EFI_ZFS_BOOT
413 case DEVT_ZFS: {
414 struct zfs_devdesc currdev;
415
416 currdev.d_dev = dev;
417 currdev.d_unit = unit;
418 currdev.d_type = currdev.d_dev->dv_type;
419 currdev.d_opendata = NULL;
420 currdev.pool_guid = pool_guid;
421 currdev.root_guid = 0;
422 env_setenv("currdev", EV_VOLATILE, efi_fmtdev(&currdev),
423 efi_setcurrdev, env_nounset);
424 env_setenv("loaddev", EV_VOLATILE, efi_fmtdev(&currdev), env_noset,
425 env_nounset);
426#ifdef __FreeBSD__
427 init_zfs_bootenv(zfs_fmtdev(&currdev));
428#endif
429 break;
430 }
431#endif
432 default: {
433 struct devdesc currdev;
434
435 currdev.d_dev = dev;
436 currdev.d_unit = unit;
437 currdev.d_opendata = NULL;
438 currdev.d_type = currdev.d_dev->dv_type;
439 env_setenv("currdev", EV_VOLATILE, efi_fmtdev(&currdev),
440 efi_setcurrdev, env_nounset);
441 env_setenv("loaddev", EV_VOLATILE, efi_fmtdev(&currdev), env_noset,
442 env_nounset);
443 break;
444 }
445 }
446
447 setenv("LINES", "24", 1); /* optional */
448 setenv("COLUMNS", "80", 1); /* optional */
449 setenv("ISADIR", "amd64", 1); /* we only build 64bit */
Toomas Soomedcba96f2016-10-09 17:12:23 +0300450 acpi_detect();
Toomas Soome199767f2015-10-25 00:06:51 +0300451
Toomas Soomedcba96f2016-10-09 17:12:23 +0300452 if ((ptr = efi_get_table(&smbios3)) == NULL)
453 ptr = efi_get_table(&smbios);
454 smbios_detect(ptr);
Toomas Soome199767f2015-10-25 00:06:51 +0300455
456 efi_serial_init(); /* detect and set up serial ports */
457 interact(NULL); /* doesn't return */
458
459 return (EFI_SUCCESS); /* keep compiler happy */
460}
461
462COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
463
464static int
465command_reboot(int argc __attribute((unused)),
466 char *argv[] __attribute((unused)))
467{
468 int i;
469 const CHAR16 *msg = L"Reboot from the loader";
470
471 for (i = 0; devsw[i] != NULL; ++i)
472 if (devsw[i]->dv_cleanup != NULL)
473 (devsw[i]->dv_cleanup)();
474
475 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 23, (CHAR16 *)msg);
476
477 /* NOTREACHED */
478 return (CMD_ERROR);
479}
480
481COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
482
483static int
484command_memmap(int argc __attribute((unused)),
485 char *argv[] __attribute((unused)))
486{
487 UINTN sz;
488 EFI_MEMORY_DESCRIPTOR *map, *p;
489 UINTN key, dsz;
490 UINT32 dver;
491 EFI_STATUS status;
492 int i, ndesc;
493 int rv = 0;
494 char line[80];
495 static const char *types[] = {
496 "Reserved",
497 "LoaderCode",
498 "LoaderData",
499 "BootServicesCode",
500 "BootServicesData",
501 "RuntimeServicesCode",
502 "RuntimeServicesData",
503 "ConventionalMemory",
504 "UnusableMemory",
505 "ACPIReclaimMemory",
506 "ACPIMemoryNVS",
507 "MemoryMappedIO",
508 "MemoryMappedIOPortSpace",
509 "PalCode"
510 };
511
512 sz = 0;
513 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
514 if (status != EFI_BUFFER_TOO_SMALL) {
515 printf("Can't determine memory map size\n");
516 return (CMD_ERROR);
517 }
518 map = malloc(sz);
519 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
520 if (EFI_ERROR(status)) {
521 printf("Can't read memory map\n");
522 return (CMD_ERROR);
523 }
524
525 ndesc = sz / dsz;
526 snprintf(line, 80, "%23s %12s %12s %8s %4s\n",
527 "Type", "Physical", "Virtual", "#Pages", "Attr");
528 pager_open();
529 rv = pager_output(line);
530 if (rv) {
531 pager_close();
532 return (CMD_OK);
533 }
534
535 for (i = 0, p = map; i < ndesc;
536 i++, p = NextMemoryDescriptor(p, dsz)) {
537 snprintf(line, 80, "%23s %012lx %012lx %08lx ",
538 types[p->Type],
539 p->PhysicalStart,
540 p->VirtualStart,
541 p->NumberOfPages);
542 rv = pager_output(line);
543 if (rv)
544 break;
545
546 if (p->Attribute & EFI_MEMORY_UC)
547 printf("UC ");
548 if (p->Attribute & EFI_MEMORY_WC)
549 printf("WC ");
550 if (p->Attribute & EFI_MEMORY_WT)
551 printf("WT ");
552 if (p->Attribute & EFI_MEMORY_WB)
553 printf("WB ");
554 if (p->Attribute & EFI_MEMORY_UCE)
555 printf("UCE ");
556 if (p->Attribute & EFI_MEMORY_WP)
557 printf("WP ");
558 if (p->Attribute & EFI_MEMORY_RP)
559 printf("RP ");
560 if (p->Attribute & EFI_MEMORY_XP)
561 printf("XP ");
562 rv = pager_output("\n");
563 if (rv)
564 break;
565 }
566
567 pager_close();
568 return (CMD_OK);
569}
570
571COMMAND_SET(configuration, "configuration", "print configuration tables",
572 command_configuration);
573
574static const char *
575guid_to_string(EFI_GUID *guid)
576{
577 static char buf[40];
578
579 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
580 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
581 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
582 guid->Data4[5], guid->Data4[6], guid->Data4[7]);
583 return (buf);
584}
585
586static int
587command_configuration(int argc __attribute((unused)),
588 char *argv[] __attribute((unused)))
589{
590 UINTN i;
591
592 printf("NumberOfTableEntries=%lu\n",
593 (unsigned long)ST->NumberOfTableEntries);
594 for (i = 0; i < ST->NumberOfTableEntries; i++) {
595 EFI_GUID *guid;
596
597 printf(" ");
598 guid = &ST->ConfigurationTable[i].VendorGuid;
599 if (!memcmp(guid, &mps, sizeof(EFI_GUID)))
600 printf("MPS Table");
601 else if (!memcmp(guid, &acpi, sizeof(EFI_GUID)))
602 printf("ACPI Table");
603 else if (!memcmp(guid, &acpi20, sizeof(EFI_GUID)))
604 printf("ACPI 2.0 Table");
605 else if (!memcmp(guid, &smbios, sizeof(EFI_GUID)))
606 printf("SMBIOS Table");
607 else if (!memcmp(guid, &smbios3, sizeof(EFI_GUID)))
608 printf("SMBIOS3 Table");
609 else if (!memcmp(guid, &dxe, sizeof(EFI_GUID)))
610 printf("DXE Table");
611 else if (!memcmp(guid, &hoblist, sizeof(EFI_GUID)))
612 printf("HOB List Table");
613 else if (!memcmp(guid, &memtype, sizeof(EFI_GUID)))
614 printf("Memory Type Information Table");
615 else if (!memcmp(guid, &debugimg, sizeof(EFI_GUID)))
616 printf("Debug Image Info Table");
617 else if (!memcmp(guid, &fdtdtb, sizeof(EFI_GUID)))
618 printf("FDT Table");
619 else
620 printf("Unknown Table (%s)", guid_to_string(guid));
621 printf(" at %p\n", ST->ConfigurationTable[i].VendorTable);
622 }
623
624 return (CMD_OK);
625}
626
627
628COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode);
629
630static int
631command_mode(int argc, char *argv[])
632{
633 UINTN cols, rows;
634 unsigned int mode;
635 int i;
636 char *cp;
637 char rowenv[8];
638 EFI_STATUS status;
639 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
640 extern void HO(void);
641
642 conout = ST->ConOut;
643
644 if (argc > 1) {
645 mode = strtol(argv[1], &cp, 0);
646 if (cp[0] != '\0') {
647 printf("Invalid mode\n");
648 return (CMD_ERROR);
649 }
650 status = conout->QueryMode(conout, mode, &cols, &rows);
651 if (EFI_ERROR(status)) {
652 printf("invalid mode %d\n", mode);
653 return (CMD_ERROR);
654 }
655 status = conout->SetMode(conout, mode);
656 if (EFI_ERROR(status)) {
657 printf("couldn't set mode %d\n", mode);
658 return (CMD_ERROR);
659 }
660 sprintf(rowenv, "%u", (unsigned)rows);
661 setenv("LINES", rowenv, 1);
662 sprintf(rowenv, "%u", (unsigned)cols);
663 setenv("COLUMNS", rowenv, 1);
664 HO(); /* set cursor */
665 return (CMD_OK);
666 }
667
668 printf("Current mode: %d\n", conout->Mode->Mode);
669 for (i = 0; i <= conout->Mode->MaxMode; i++) {
670 status = conout->QueryMode(conout, i, &cols, &rows);
671 if (EFI_ERROR(status))
672 continue;
673 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols,
674 (unsigned)rows);
675 }
676
677 if (i != 0)
678 printf("Select a mode with the command \"mode <number>\"\n");
679
680 return (CMD_OK);
681}
682
683
684COMMAND_SET(nvram, "nvram", "get NVRAM variables", command_nvram);
685
686static int
687command_nvram(int argc __attribute((unused)),
688 char *argv[] __attribute((unused)))
689{
690 CHAR16 var[128];
691 UINT8 *data; /* value is in bytes */
692 EFI_STATUS status;
693 EFI_GUID varguid = { 0,0,0,{0,0,0,0,0,0,0,0} };
694 UINTN varsz, datasz, i;
695 UINT32 attr;
696 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
697
698 conout = ST->ConOut;
699
700 pager_open();
701 var[0] = 0; /* Initiate the enumeration */
702 varsz = 128;
703
704 for (status = RS->GetNextVariableName(&varsz, var, &varguid);
705 status != EFI_NOT_FOUND;
706 status = RS->GetNextVariableName(&varsz, var, &varguid)) {
707
708 /*
709 * as term emu is keeping track on cursor, use putchar().
710 */
711 for (i = 0; var[i] != 0; i++)
712 putchar(var[i]);
713 varsz = 128; /* GetNextVariableName() did change it. */
714
715 printf(": Attributes:");
716 datasz = 0;
717 status = RS->GetVariable(var, &varguid, &attr, &datasz, NULL);
718 if ((data = malloc(datasz)) == NULL)
719 break;
720 status = RS->GetVariable(var, &varguid, &attr, &datasz, data);
721 if (EFI_ERROR(status))
722 printf("<error retrieving variable>");
723 else {
724 if (attr & EFI_VARIABLE_NON_VOLATILE)
725 printf(" NV");
726 if (attr & EFI_VARIABLE_BOOTSERVICE_ACCESS)
727 printf(" BS");
728 if (attr & EFI_VARIABLE_RUNTIME_ACCESS)
729 printf(" RS");
730 if (attr & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
731 printf(" HR");
732 if (attr & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
733 printf(" AW");
734 if (attr &
735 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
736 printf(" TW");
737
738 printf(": DataLength: %lld", (long long)datasz);
739 }
740 free(data);
741 if (pager_output("\n"))
742 break;
743 }
744
745 pager_close();
746 return (CMD_OK);
747}
748
749struct protocol_name {
750 EFI_GUID guid;
751 const char *name;
752} proto_names[] = {
753 { DEVICE_PATH_PROTOCOL, "device path" },
754 { BLOCK_IO_PROTOCOL, "block io" },
755 { DISK_IO_PROTOCOL, "disk io" },
756 { EFI_DISK_INFO_PROTOCOL_GUID, "disk info" },
757 { SIMPLE_FILE_SYSTEM_PROTOCOL, "simple fs" },
758 { LOAD_FILE_PROTOCOL, "load file" },
759 { DEVICE_IO_PROTOCOL, "device io" },
760 { UNICODE_COLLATION_PROTOCOL, "unicode collation" },
761 { EFI_UNICODE_COLLATION2_PROTOCOL_GUID, "unicode collation2" },
762 { EFI_SIMPLE_NETWORK_PROTOCOL, "simple network" },
763 { SIMPLE_TEXT_OUTPUT_PROTOCOL, "simple text output" },
764 { SIMPLE_TEXT_INPUT_PROTOCOL, "simple text input" },
765 { EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID, "simple text ex input" },
766 { EFI_CONSOLE_CONTROL_PROTOCOL_GUID, "console control" },
767 { EFI_CONSOLE_IN_DEVICE_GUID, "stdin" },
768 { EFI_CONSOLE_OUT_DEVICE_GUID, "stdout" },
769 { EFI_STANDARD_ERROR_DEVICE_GUID, "stderr" },
770 { EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID, "GOP" },
771 { EFI_UGA_DRAW_PROTOCOL_GUID, "UGA draw" },
772 { EFI_PXE_BASE_CODE_PROTOCOL, "PXE base code" },
773 { EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL, "PXE base code callback" },
774 { SERIAL_IO_PROTOCOL, "serial io" },
775 { LOADED_IMAGE_PROTOCOL, "loaded image" },
776 { EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID,
777 "loaded image device path" },
778 { EFI_ISA_IO_PROTOCOL_GUID, "ISA io" },
779 { EFI_IDE_CONTROLLER_INIT_PROTOCOL_GUID, "IDE controller init" },
780 { EFI_ISA_ACPI_PROTOCOL_GUID, "ISA ACPI" },
781 { EFI_PCI_IO_PROTOCOL_GUID, "PCI" },
782 { EFI_PCI_ROOT_IO_GUID, "PCI root" },
783 { EFI_PCI_ENUMERATION_COMPLETE_GUID, "PCI enumeration" },
784 { EFI_DRIVER_DIAGNOSTICS_PROTOCOL_GUID, "Driver diagnostics" },
785 { EFI_DRIVER_DIAGNOSTICS2_PROTOCOL_GUID, "Driver diagnostics2" },
786 { EFI_SIMPLE_POINTER_PROTOCOL_GUID, "simple pointer" },
787 { EFI_ABSOLUTE_POINTER_PROTOCOL_GUID, "absolute pointer" },
788 { EFI_VLAN_CONFIG_PROTOCOL_GUID, "VLAN config" },
789 { EFI_ARP_SERVICE_BINDING_PROTOCOL_GUID, "ARP service binding" },
790 { EFI_ARP_PROTOCOL_GUID, "ARP" },
791 { EFI_IP4_SERVICE_BINDING_PROTOCOL, "IPv4 service binding" },
792 { EFI_IP4_PROTOCOL, "IPv4" },
793 { EFI_IP4_CONFIG_PROTOCOL_GUID, "IPv4 config" },
794 { EFI_IP6_SERVICE_BINDING_PROTOCOL, "IPv6 service binding" },
795 { EFI_IP6_PROTOCOL, "IPv6" },
796 { EFI_IP6_CONFIG_PROTOCOL_GUID, "IPv6 config" },
797 { EFI_UDP4_PROTOCOL, "UDPv4" },
798 { EFI_UDP4_SERVICE_BINDING_PROTOCOL, "UDPv4 service binding" },
799 { EFI_UDP6_PROTOCOL, "UDPv6" },
800 { EFI_UDP6_SERVICE_BINDING_PROTOCOL, "UDPv6 service binding" },
801 { EFI_TCP4_PROTOCOL, "TCPv4" },
802 { EFI_TCP4_SERVICE_BINDING_PROTOCOL, "TCPv4 service binding" },
803 { EFI_TCP6_PROTOCOL, "TCPv6" },
804 { EFI_TCP6_SERVICE_BINDING_PROTOCOL, "TCPv6 service binding" },
805 { EFI_PART_TYPE_EFI_SYSTEM_PART_GUID, "EFI System partition" },
806 { EFI_PART_TYPE_LEGACY_MBR_GUID, "MBR legacy" },
807 { EFI_DEVICE_TREE_GUID, "device tree" },
808 { EFI_USB_IO_PROTOCOL_GUID, "USB io" },
809 { EFI_USB2_HC_PROTOCOL_GUID, "USB2 HC" },
810 { EFI_COMPONENT_NAME_PROTOCOL_GUID, "component name" },
811 { EFI_COMPONENT_NAME2_PROTOCOL_GUID, "component name2" },
812 { EFI_DRIVER_BINDING_PROTOCOL_GUID, "driver binding" },
813 { EFI_DRIVER_CONFIGURATION_PROTOCOL_GUID, "driver configuration" },
814 { EFI_DRIVER_CONFIGURATION2_PROTOCOL_GUID, "driver configuration2" },
815 { EFI_DECOMPRESS_PROTOCOL_GUID, "decompress" },
816 { EFI_EBC_INTERPRETER_PROTOCOL_GUID, "ebc interpreter" },
817 { EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL,
818 "network interface identifier" },
819 { EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL_31,
820 "network interface identifier_31" },
821 { EFI_MANAGED_NETWORK_SERVICE_BINDING_PROTOCOL_GUID,
822 "managed network service binding" },
823 { EFI_MANAGED_NETWORK_PROTOCOL_GUID, "managed network" },
824 { EFI_FORM_BROWSER2_PROTOCOL_GUID, "form browser" },
825 { EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID, "HII config routing" },
826 { EFI_HII_DATABASE_PROTOCOL_GUID, "HII database" },
827 { EFI_HII_STRING_PROTOCOL_GUID, "HII string" },
828 { EFI_HII_IMAGE_PROTOCOL_GUID, "HII image" },
829 { EFI_HII_FONT_PROTOCOL_GUID, "HII font" },
830 { EFI_HII_CONFIGURATION_ACCESS_PROTOCOL_GUID, "HII config" },
831 { EFI_MTFTP4_SERVICE_BINDING_PROTOCOL_GUID, "MTFTP4 service binding" },
832 { EFI_MTFTP4_PROTOCOL_GUID, "MTFTP4" },
833 { EFI_MTFTP6_SERVICE_BINDING_PROTOCOL_GUID, "MTFTP6 service binding" },
834 { EFI_MTFTP6_PROTOCOL_GUID, "MTFTP6" },
835 { EFI_DHCP4_SERVICE_BINDING_PROTOCOL_GUID, "DHCP4 service binding" },
836 { EFI_DHCP4_PROTOCOL_GUID, "DHCP4" },
837 { EFI_DHCP6_SERVICE_BINDING_PROTOCOL_GUID, "DHCP6 service binding" },
838 { EFI_DHCP6_PROTOCOL_GUID, "DHCP6" },
839 { EFI_SCSI_IO_PROTOCOL_GUID, "SCSI io" },
840 { EFI_SCSI_PASS_THRU_PROTOCOL_GUID, "SCSI pass thru" },
841 { EFI_EXT_SCSI_PASS_THRU_PROTOCOL_GUID, "SCSI pass thru ext" },
842 { EFI_CAPSULE_ARCH_PROTOCOL_GUID, "Capsule arch" },
843 { EFI_MONOTONIC_COUNTER_ARCH_PROTOCOL_GUID, "monotonic counter arch" },
844 { EFI_REALTIME_CLOCK_ARCH_PROTOCOL_GUID, "realtime clock arch" },
845 { EFI_VARIABLE_ARCH_PROTOCOL_GUID, "variable arch" },
846 { EFI_VARIABLE_WRITE_ARCH_PROTOCOL_GUID, "variable write arch" },
847 { EFI_WATCHDOG_TIMER_ARCH_PROTOCOL_GUID, "watchdog timer arch" },
848 { EFI_MP_SERVICES_PROTOCOL_GUID, "MP services" },
849 { EFI_ACPI_SUPPORT_PROTOCOL_GUID, "ACPI support" },
850 { EFI_BDS_ARCH_PROTOCOL_GUID, "BDS arch" },
851 { EFI_METRONOME_ARCH_PROTOCOL_GUID, "metronome arch" },
852 { EFI_TIMER_ARCH_PROTOCOL_GUID, "timer arch" },
853 { EFI_DPC_PROTOCOL_GUID, "DPC" },
854 { EFI_PRINT2_PROTOCOL_GUID, "print2" },
855 { EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID, "device path to text" },
856 { EFI_RESET_ARCH_PROTOCOL_GUID, "reset arch" },
857 { EFI_CPU_ARCH_PROTOCOL_GUID, "CPU arch" },
858 { EFI_CPU_IO2_PROTOCOL_GUID, "CPU IO2" },
859 { EFI_LEGACY_8259_PROTOCOL_GUID, "Legacy 8259" },
860 { EFI_SECURITY_ARCH_PROTOCOL_GUID, "Security arch" },
861 { EFI_SECURITY2_ARCH_PROTOCOL_GUID, "Security2 arch" },
862 { EFI_RUNTIME_ARCH_PROTOCOL_GUID, "Runtime arch" },
863 { EFI_STATUS_CODE_RUNTIME_PROTOCOL_GUID, "status code runtime" },
864 { EFI_DATA_HUB_PROTOCOL_GUID, "data hub" },
865 { PCD_PROTOCOL_GUID, "PCD" },
866 { EFI_PCD_PROTOCOL_GUID, "EFI PCD" },
867 { EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL_GUID, "firmware volume block" },
868 { EFI_FIRMWARE_VOLUME2_PROTOCOL_GUID, "firmware volume2" },
869 { EFI_FIRMWARE_VOLUME_DISPATCH_PROTOCOL_GUID,
870 "firmware volume dispatch" },
871 { LZMA_COMPRESS_GUID, "lzma compress" },
872 { { 0,0,0,{0,0,0,0,0,0,0,0} }, NULL } /* must be last entry */
873};
874
875COMMAND_SET(lsefi, "lsefi", "list EFI handles", command_lsefi);
876
877static int
878command_lsefi(int argc __attribute((unused)),
879 char *argv[] __attribute((unused)))
880{
881 EFI_HANDLE *buffer = NULL;
882 EFI_HANDLE handle;
883 UINTN bufsz = 0, i, j;
884 EFI_STATUS status;
885 int k, ret;
886
887 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
888 if (status != EFI_BUFFER_TOO_SMALL) {
889 snprintf(command_errbuf, sizeof (command_errbuf),
890 "unexpected error: %lld", (long long)status);
891 return (CMD_ERROR);
892 }
893 if ((buffer = malloc(bufsz)) == NULL) {
894 sprintf(command_errbuf, "out of memory");
895 return (CMD_ERROR);
896 }
897
898 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
899 if (EFI_ERROR(status)) {
900 free(buffer);
901 snprintf(command_errbuf, sizeof (command_errbuf),
902 "LocateHandle() error: %lld", (long long)status);
903 return (CMD_ERROR);
904 }
905
906 pager_open();
907 for (i = 0; i < (bufsz / sizeof (EFI_HANDLE)); i++) {
908 UINTN nproto = 0;
909 EFI_GUID **protocols = NULL;
910
911 handle = buffer[i];
912 printf("Handle %p", handle);
913 if (pager_output("\n"))
914 break;
915 /* device path */
916
917 status = BS->ProtocolsPerHandle(handle, &protocols, &nproto);
918 if (EFI_ERROR(status)) {
919 snprintf(command_errbuf, sizeof (command_errbuf),
920 "ProtocolsPerHandle() error: %lld",
921 (long long)status);
922 continue;
923 }
924 for (j = 0; j < nproto; j++) {
925 for (k = 0; proto_names[k].name != NULL; k++)
926 if (memcmp(protocols[j], &proto_names[k].guid,
927 sizeof (proto_names[k].guid)) == 0)
928 break;
929 if (proto_names[k].name != NULL)
930 printf(" %s", proto_names[k].name);
931 else
932 printf(" %s", guid_to_string(protocols[j]));
933 ret = pager_output("\n");
934 if (ret)
935 break;
936 }
937 BS->FreePool(protocols);
938 if (ret)
939 break;
940 }
941 pager_close();
942 free(buffer);
943 return (CMD_OK);
944}
945
946#ifdef EFI_ZFS_BOOT
947COMMAND_SET(lszfs, "lszfs", "list child datasets of a zfs dataset",
948 command_lszfs);
949
950static int
951command_lszfs(int argc, char *argv[])
952{
953 int err;
954
955 if (argc != 2) {
956 command_errmsg = "wrong number of arguments";
957 return (CMD_ERROR);
958 }
959
960 err = zfs_list(argv[1]);
961 if (err != 0) {
962 command_errmsg = strerror(err);
963 return (CMD_ERROR);
964 }
965 return (CMD_OK);
966}
967
968#ifdef __FreeBSD__
969COMMAND_SET(reloadbe, "reloadbe", "refresh the list of ZFS Boot Environments",
970 command_reloadbe);
971
972static int
973command_reloadbe(int argc, char *argv[])
974{
975 int err;
976 char *root;
977
978 if (argc > 2) {
979 command_errmsg = "wrong number of arguments";
980 return (CMD_ERROR);
981 }
982
983 if (argc == 2) {
984 err = zfs_bootenv(argv[1]);
985 } else {
986 root = getenv("zfs_be_root");
987 if (root == NULL) {
988 return (CMD_OK);
989 }
990 err = zfs_bootenv(root);
991 }
992
993 if (err != 0) {
994 command_errmsg = strerror(err);
995 return (CMD_ERROR);
996 }
997
998 return (CMD_OK);
999}
1000#endif /* __FreeBSD__ */
1001#endif
1002
1003void
1004efi_serial_init(void)
1005{
1006 EFI_HANDLE *buffer = NULL;
1007 UINTN bufsz = 0, i;
1008 EFI_STATUS status;
1009 int serial = 0;
1010
1011 /*
1012 * get buffer size
1013 */
1014 status = BS->LocateHandle(ByProtocol, &serial_io, NULL, &bufsz, buffer);
1015 if (status != EFI_BUFFER_TOO_SMALL) {
1016 snprintf(command_errbuf, sizeof (command_errbuf),
1017 "unexpected error: %lld", (long long)status);
1018 return;
1019 }
1020 if ((buffer = malloc(bufsz)) == NULL) {
1021 sprintf(command_errbuf, "out of memory");
1022 return;
1023 }
1024
1025 /*
1026 * get handle array
1027 */
1028 status = BS->LocateHandle(ByProtocol, &serial_io, NULL, &bufsz, buffer);
1029 if (EFI_ERROR(status)) {
1030 free(buffer);
1031 snprintf(command_errbuf, sizeof (command_errbuf),
1032 "LocateHandle() error: %lld", (long long)status);
1033 return;
1034 }
1035
1036 for (i = 0; i < (bufsz / sizeof (EFI_HANDLE)); i++) {
1037 SERIAL_IO_INTERFACE *sio;
1038 status = BS->OpenProtocol(buffer[i], &serial_io, (void**)&sio,
1039 IH, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1040 if (EFI_ERROR(status)) {
1041 snprintf(command_errbuf, sizeof (command_errbuf),
1042 "OpenProtocol() error: %lld", (long long)status);
1043 }
1044 printf("serial# %d\n", serial++);
1045 }
1046
1047 free(buffer);
1048}
1049
1050#ifdef LOADER_FDT_SUPPORT
1051extern int command_fdt_internal(int argc, char *argv[]);
1052
1053/*
1054 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
1055 * and declaring it as extern is in contradiction with COMMAND_SET() macro
1056 * (which uses static pointer), we're defining wrapper function, which
1057 * calls the proper fdt handling routine.
1058 */
1059static int
1060command_fdt(int argc, char *argv[])
1061{
1062 return (command_fdt_internal(argc, argv));
1063}
1064
1065COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);
1066#endif
1067
1068#ifdef EFI_ZFS_BOOT
1069static void
1070efi_zfs_probe(void)
1071{
1072 EFI_HANDLE h;
1073 u_int unit;
1074 int i;
1075 char dname[SPECNAMELEN + 1];
1076 uint64_t guid;
1077
1078 unit = 0;
1079 h = efi_find_handle(&efipart_dev, 0);
1080 for (i = 0; h != NULL; h = efi_find_handle(&efipart_dev, ++i)) {
1081 snprintf(dname, sizeof(dname), "%s%d:", efipart_dev.dv_name, i);
1082 if (zfs_probe_dev(dname, &guid) == 0)
1083 (void)efi_handle_update_dev(h, &zfs_dev, unit++, guid);
1084 }
1085}
1086#endif