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