blob: 7d83cb367778ba46dcb7618b19fa7a7d292635a3 [file] [log] [blame]
Toomas Soome9f23ea42017-12-12 15:23:09 +02001/*
Toomas Soome199767f2015-10-25 00:06:51 +03002 * 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>
Toomas Soomeeee59042016-11-12 00:06:58 +020035#include <inttypes.h>
Toomas Soome199767f2015-10-25 00:06:51 +030036#include <string.h>
37#include <setjmp.h>
Toomas Soomedbacaf52016-11-17 17:02:22 +020038#include <disk.h>
Toomas Soome199767f2015-10-25 00:06:51 +030039
40#include <efi.h>
41#include <efilib.h>
42#include <efigpt.h>
43
Toomas Soomeeee59042016-11-12 00:06:58 +020044#include <uuid.h>
Toomas Soomeeee59042016-11-12 00:06:58 +020045
Toomas Soome199767f2015-10-25 00:06:51 +030046#include <bootstrap.h>
47#include <smbios.h>
48
Toomas Soome199767f2015-10-25 00:06:51 +030049#include <libzfs.h>
Toomas Soome9f23ea42017-12-12 15:23:09 +020050#include <efizfs.h>
Toomas Soome199767f2015-10-25 00:06:51 +030051
52#include "loader_efi.h"
53
Toomas Soomef6c94442017-01-03 11:24:09 +020054extern char bootprog_info[];
Toomas Soome199767f2015-10-25 00:06:51 +030055
56struct arch_switch archsw; /* MI/MD interface boundary */
57
Toomas Soome199767f2015-10-25 00:06:51 +030058EFI_GUID devid = DEVICE_PATH_PROTOCOL;
59EFI_GUID imgid = LOADED_IMAGE_PROTOCOL;
Toomas Soome199767f2015-10-25 00:06:51 +030060EFI_GUID smbios = SMBIOS_TABLE_GUID;
61EFI_GUID smbios3 = SMBIOS3_TABLE_GUID;
Toomas Soome199767f2015-10-25 00:06:51 +030062EFI_GUID inputid = SIMPLE_TEXT_INPUT_PROTOCOL;
Toomas Soome199767f2015-10-25 00:06:51 +030063
Toomas Soomedcba96f2016-10-09 17:12:23 +030064extern void acpi_detect(void);
Toomas Soomef9feecc2016-10-09 17:30:28 +030065extern void efi_getsmap(void);
Toomas Soome9f23ea42017-12-12 15:23:09 +020066
67static EFI_LOADED_IMAGE *img;
68
69bool
70efi_zfs_is_preferred(EFI_HANDLE *h)
71{
72 return (h == img->DeviceHandle);
73}
Toomas Soome199767f2015-10-25 00:06:51 +030074
Toomas Soome199767f2015-10-25 00:06:51 +030075static int
76has_keyboard(void)
77{
78 EFI_STATUS status;
79 EFI_DEVICE_PATH *path;
80 EFI_HANDLE *hin, *hin_end, *walker;
81 UINTN sz;
82 int retval = 0;
Toomas Soome9f23ea42017-12-12 15:23:09 +020083
Toomas Soome199767f2015-10-25 00:06:51 +030084 /*
85 * Find all the handles that support the SIMPLE_TEXT_INPUT_PROTOCOL and
86 * do the typical dance to get the right sized buffer.
87 */
88 sz = 0;
89 hin = NULL;
90 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz, 0);
91 if (status == EFI_BUFFER_TOO_SMALL) {
92 hin = (EFI_HANDLE *)malloc(sz);
93 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz,
94 hin);
95 if (EFI_ERROR(status))
96 free(hin);
97 }
98 if (EFI_ERROR(status))
99 return retval;
100
101 /*
102 * Look at each of the handles. If it supports the device path protocol,
103 * use it to get the device path for this handle. Then see if that
104 * device path matches either the USB device path for keyboards or the
105 * legacy device path for keyboards.
106 */
107 hin_end = &hin[sz / sizeof(*hin)];
108 for (walker = hin; walker < hin_end; walker++) {
109 status = BS->HandleProtocol(*walker, &devid, (VOID **)&path);
110 if (EFI_ERROR(status))
111 continue;
112
113 while (!IsDevicePathEnd(path)) {
114 /*
115 * Check for the ACPI keyboard node. All PNP3xx nodes
116 * are keyboards of different flavors. Note: It is
117 * unclear of there's always a keyboard node when
118 * there's a keyboard controller, or if there's only one
119 * when a keyboard is detected at boot.
120 */
121 if (DevicePathType(path) == ACPI_DEVICE_PATH &&
122 (DevicePathSubType(path) == ACPI_DP ||
123 DevicePathSubType(path) == ACPI_EXTENDED_DP)) {
124 ACPI_HID_DEVICE_PATH *acpi;
125
126 acpi = (ACPI_HID_DEVICE_PATH *)(void *)path;
127 if ((EISA_ID_TO_NUM(acpi->HID) & 0xff00) == 0x300 &&
128 (acpi->HID & 0xffff) == PNP_EISA_ID_CONST) {
129 retval = 1;
130 goto out;
131 }
132 /*
133 * Check for USB keyboard node, if present. Unlike a
134 * PS/2 keyboard, these definitely only appear when
135 * connected to the system.
136 */
137 } else if (DevicePathType(path) == MESSAGING_DEVICE_PATH &&
138 DevicePathSubType(path) == MSG_USB_CLASS_DP) {
139 USB_CLASS_DEVICE_PATH *usb;
Toomas Soomec142ce12018-03-13 14:16:40 +0200140
Toomas Soome199767f2015-10-25 00:06:51 +0300141 usb = (USB_CLASS_DEVICE_PATH *)(void *)path;
142 if (usb->DeviceClass == 3 && /* HID */
143 usb->DeviceSubClass == 1 && /* Boot devices */
144 usb->DeviceProtocol == 1) { /* Boot keyboards */
145 retval = 1;
146 goto out;
147 }
148 }
149 path = NextDevicePathNode(path);
150 }
151 }
152out:
153 free(hin);
154 return retval;
155}
156
Toomas Soomedbacaf52016-11-17 17:02:22 +0200157static void
158set_devdesc_currdev(struct devsw *dev, int unit)
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200159{
Toomas Soomedbacaf52016-11-17 17:02:22 +0200160 struct devdesc currdev;
161 char *devname;
162
163 currdev.d_dev = dev;
Toomas Soomedbacaf52016-11-17 17:02:22 +0200164 currdev.d_unit = unit;
Toomas Soomedbacaf52016-11-17 17:02:22 +0200165 devname = efi_fmtdev(&currdev);
166
167 env_setenv("currdev", EV_VOLATILE, devname, efi_setcurrdev,
168 env_nounset);
169 env_setenv("loaddev", EV_VOLATILE, devname, env_noset, env_nounset);
170}
171
172static int
173find_currdev(EFI_LOADED_IMAGE *img)
174{
175 pdinfo_list_t *pdi_list;
176 pdinfo_t *dp, *pp;
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200177 EFI_DEVICE_PATH *devpath, *copy;
178 EFI_HANDLE h;
Toomas Soomedbacaf52016-11-17 17:02:22 +0200179 char *devname;
180 struct devsw *dev;
181 int unit;
182 uint64_t extra;
183
184 /* Did efi_zfs_probe() detect the boot pool? */
185 if (pool_guid != 0) {
186 struct zfs_devdesc currdev;
187
Toomas Soome76b35942018-03-13 11:58:56 +0200188 currdev.dd.d_dev = &zfs_dev;
189 currdev.dd.d_unit = 0;
Toomas Soomedbacaf52016-11-17 17:02:22 +0200190 currdev.pool_guid = pool_guid;
191 currdev.root_guid = 0;
192 devname = efi_fmtdev(&currdev);
193
194 env_setenv("currdev", EV_VOLATILE, devname, efi_setcurrdev,
195 env_nounset);
196 env_setenv("loaddev", EV_VOLATILE, devname, env_noset,
197 env_nounset);
198 return (0);
199 }
200
201 /* We have device lists for hd, cd, fd, walk them all. */
202 pdi_list = efiblk_get_pdinfo_list(&efipart_hddev);
203 STAILQ_FOREACH(dp, pdi_list, pd_link) {
204 struct disk_devdesc currdev;
205
Toomas Soome76b35942018-03-13 11:58:56 +0200206 currdev.dd.d_dev = &efipart_hddev;
Toomas Soome76b35942018-03-13 11:58:56 +0200207 currdev.dd.d_unit = dp->pd_unit;
Toomas Soomedbacaf52016-11-17 17:02:22 +0200208 currdev.d_slice = -1;
209 currdev.d_partition = -1;
210
211 if (dp->pd_handle == img->DeviceHandle) {
212 devname = efi_fmtdev(&currdev);
213
214 env_setenv("currdev", EV_VOLATILE, devname,
215 efi_setcurrdev, env_nounset);
216 env_setenv("loaddev", EV_VOLATILE, devname,
217 env_noset, env_nounset);
218 return (0);
219 }
220 /* Assuming GPT partitioning. */
221 STAILQ_FOREACH(pp, &dp->pd_part, pd_link) {
222 if (pp->pd_handle == img->DeviceHandle) {
223 currdev.d_slice = pp->pd_unit;
224 currdev.d_partition = 255;
225 devname = efi_fmtdev(&currdev);
226
227 env_setenv("currdev", EV_VOLATILE, devname,
228 efi_setcurrdev, env_nounset);
229 env_setenv("loaddev", EV_VOLATILE, devname,
230 env_noset, env_nounset);
231 return (0);
232 }
233 }
234 }
235
236 pdi_list = efiblk_get_pdinfo_list(&efipart_cddev);
237 STAILQ_FOREACH(dp, pdi_list, pd_link) {
238 if (dp->pd_handle == img->DeviceHandle ||
239 dp->pd_alias == img->DeviceHandle) {
240 set_devdesc_currdev(&efipart_cddev, dp->pd_unit);
241 return (0);
242 }
243 }
244
245 pdi_list = efiblk_get_pdinfo_list(&efipart_fddev);
246 STAILQ_FOREACH(dp, pdi_list, pd_link) {
247 if (dp->pd_handle == img->DeviceHandle) {
248 set_devdesc_currdev(&efipart_fddev, dp->pd_unit);
249 return (0);
250 }
251 }
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200252
253 /*
254 * Try the device handle from our loaded image first. If that
255 * fails, use the device path from the loaded image and see if
256 * any of the nodes in that path match one of the enumerated
257 * handles.
258 */
Toomas Soomedbacaf52016-11-17 17:02:22 +0200259 if (efi_handle_lookup(img->DeviceHandle, &dev, &unit, &extra) == 0) {
260 set_devdesc_currdev(dev, unit);
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200261 return (0);
Toomas Soomedbacaf52016-11-17 17:02:22 +0200262 }
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200263
264 copy = NULL;
265 devpath = efi_lookup_image_devpath(IH);
266 while (devpath != NULL) {
267 h = efi_devpath_handle(devpath);
268 if (h == NULL)
269 break;
270
271 free(copy);
272 copy = NULL;
273
Toomas Soomedbacaf52016-11-17 17:02:22 +0200274 if (efi_handle_lookup(h, &dev, &unit, &extra) == 0) {
275 set_devdesc_currdev(dev, unit);
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200276 return (0);
Toomas Soomedbacaf52016-11-17 17:02:22 +0200277 }
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200278
279 devpath = efi_lookup_devpath(h);
280 if (devpath != NULL) {
281 copy = efi_devpath_trim(devpath);
282 devpath = copy;
283 }
284 }
285 free(copy);
286
287 return (ENOENT);
288}
289
Toomas Soome199767f2015-10-25 00:06:51 +0300290EFI_STATUS
291main(int argc, CHAR16 *argv[])
292{
293 char var[128];
Toomas Soome199767f2015-10-25 00:06:51 +0300294 EFI_GUID *guid;
Toomas Soome8fe0f542018-06-14 10:00:19 +0300295 int i, j, howto;
296 bool vargood;
Toomas Soomedcba96f2016-10-09 17:12:23 +0300297 void *ptr;
Toomas Soome199767f2015-10-25 00:06:51 +0300298 UINTN k;
299 int has_kbd;
300
301 archsw.arch_autoload = efi_autoload;
302 archsw.arch_getdev = efi_getdev;
303 archsw.arch_copyin = efi_copyin;
304 archsw.arch_copyout = efi_copyout;
305 archsw.arch_readin = efi_readin;
Toomas Soomef9feecc2016-10-09 17:30:28 +0300306 archsw.arch_loadaddr = efi_loadaddr;
307 archsw.arch_free_loadaddr = efi_free_loadaddr;
Toomas Soome199767f2015-10-25 00:06:51 +0300308 /* Note this needs to be set before ZFS init. */
309 archsw.arch_zfs_probe = efi_zfs_probe;
Toomas Soome9f23ea42017-12-12 15:23:09 +0200310
311 /* Get our loaded image protocol interface structure. */
312 BS->HandleProtocol(IH, &imgid, (VOID**)&img);
Toomas Soome199767f2015-10-25 00:06:51 +0300313
Toomas Soome04f8e092016-11-10 21:05:15 +0200314 /* Init the time source */
315 efi_time_init();
316
Toomas Soome199767f2015-10-25 00:06:51 +0300317 has_kbd = has_keyboard();
318
319 /*
320 * XXX Chicken-and-egg problem; we want to have console output
321 * early, but some console attributes may depend on reading from
322 * eg. the boot device, which we can't do yet. We can use
323 * printf() etc. once this is done.
324 */
325 cons_probe();
Toomas Soomef9feecc2016-10-09 17:30:28 +0300326 efi_getsmap();
Toomas Soome199767f2015-10-25 00:06:51 +0300327
328 /*
329 * Initialise the block cache. Set the upper limit.
330 */
331 bcache_init(32768, 512);
332
333 /*
334 * Parse the args to set the console settings, etc
335 * boot1.efi passes these in, if it can read /boot.config or /boot/config
336 * or iPXE may be setup to pass these in.
337 *
338 * Loop through the args, and for each one that contains an '=' that is
339 * not the first character, add it to the environment. This allows
340 * loader and kernel env vars to be passed on the command line. Convert
341 * args from UCS-2 to ASCII (16 to 8 bit) as they are copied.
342 */
343 howto = 0;
344 for (i = 1; i < argc; i++) {
345 if (argv[i][0] == '-') {
346 for (j = 1; argv[i][j] != 0; j++) {
347 int ch;
348
349 ch = argv[i][j];
350 switch (ch) {
351 case 'a':
352 howto |= RB_ASKNAME;
353 break;
354 case 'd':
355 howto |= RB_KDB;
356 break;
357 case 'D':
358 howto |= RB_MULTIPLE;
359 break;
360 case 'h':
361 howto |= RB_SERIAL;
362 break;
363 case 'm':
364 howto |= RB_MUTE;
365 break;
366 case 'p':
367 howto |= RB_PAUSE;
368 break;
369 case 'P':
370 if (!has_kbd)
371 howto |= RB_SERIAL | RB_MULTIPLE;
372 break;
373 case 'r':
374 howto |= RB_DFLTROOT;
375 break;
376 case 's':
377 howto |= RB_SINGLE;
378 break;
379 case 'S':
380 if (argv[i][j + 1] == 0) {
381 if (i + 1 == argc) {
Toomas Soome2aca6c62017-06-02 23:07:12 +0300382 strncpy(var, "115200",
Toomas Soome199767f2015-10-25 00:06:51 +0300383 sizeof(var));
Toomas Soome2aca6c62017-06-02 23:07:12 +0300384 } else {
385 CHAR16 *ptr;
386 ptr = &argv[i + 1][0];
387 cpy16to8(ptr, var,
388 sizeof(var));
Toomas Soome199767f2015-10-25 00:06:51 +0300389 }
390 i++;
Toomas Soome199767f2015-10-25 00:06:51 +0300391 } else {
Toomas Soomeeee59042016-11-12 00:06:58 +0200392 cpy16to8(&argv[i][j + 1], var,
Toomas Soome199767f2015-10-25 00:06:51 +0300393 sizeof(var));
Toomas Soome199767f2015-10-25 00:06:51 +0300394 }
Toomas Soome2aca6c62017-06-02 23:07:12 +0300395 strncat(var, ",8,n,1,-", sizeof(var));
396 setenv("ttya-mode", var, 1);
397 break;
Toomas Soome199767f2015-10-25 00:06:51 +0300398 case 'v':
399 howto |= RB_VERBOSE;
400 break;
401 }
402 }
403 } else {
Toomas Soome8fe0f542018-06-14 10:00:19 +0300404 vargood = false;
Toomas Soome199767f2015-10-25 00:06:51 +0300405 for (j = 0; argv[i][j] != 0; j++) {
406 if (j == sizeof(var)) {
Toomas Soome8fe0f542018-06-14 10:00:19 +0300407 vargood = false;
Toomas Soome199767f2015-10-25 00:06:51 +0300408 break;
409 }
410 if (j > 0 && argv[i][j] == '=')
Toomas Soome8fe0f542018-06-14 10:00:19 +0300411 vargood = true;
Toomas Soome199767f2015-10-25 00:06:51 +0300412 var[j] = (char)argv[i][j];
413 }
414 if (vargood) {
415 var[j] = 0;
416 putenv(var);
417 }
418 }
419 }
420 for (i = 0; howto_names[i].ev != NULL; i++)
421 if (howto & howto_names[i].mask)
422 setenv(howto_names[i].ev, "YES", 1);
423 if (howto & RB_MULTIPLE) {
424 if (howto & RB_SERIAL)
425 setenv("console", "ttya text" , 1);
426 else
427 setenv("console", "text ttya" , 1);
428 } else if (howto & RB_SERIAL) {
429 setenv("console", "ttya" , 1);
430 }
431
Toomas Soome199767f2015-10-25 00:06:51 +0300432 /*
Toomas Soomec00b6c92018-02-19 19:14:22 +0200433 * Scan the BLOCK IO MEDIA handles then
434 * march through the device switch probing for things.
Toomas Soome199767f2015-10-25 00:06:51 +0300435 */
Toomas Soomec00b6c92018-02-19 19:14:22 +0200436 if ((i = efipart_inithandles()) == 0) {
437 for (i = 0; devsw[i] != NULL; i++)
438 if (devsw[i]->dv_init != NULL)
439 (devsw[i]->dv_init)();
440 } else
441 printf("efipart_inithandles failed %d, expect failures", i);
Toomas Soome199767f2015-10-25 00:06:51 +0300442
Toomas Soome199767f2015-10-25 00:06:51 +0300443 printf("Command line arguments:");
444 for (i = 0; i < argc; i++) {
Toomas Soomeeee59042016-11-12 00:06:58 +0200445 printf(" %S", argv[i]);
Toomas Soome199767f2015-10-25 00:06:51 +0300446 }
447 printf("\n");
448
449 printf("Image base: 0x%lx\n", (u_long)img->ImageBase);
450 printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16,
451 ST->Hdr.Revision & 0xffff);
Toomas Soomeeee59042016-11-12 00:06:58 +0200452 printf("EFI Firmware: %S (rev %d.%02d)\n", ST->FirmwareVendor,
453 ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
Toomas Soome199767f2015-10-25 00:06:51 +0300454
Toomas Soomef6c94442017-01-03 11:24:09 +0200455 printf("\n%s", bootprog_info);
Toomas Soome199767f2015-10-25 00:06:51 +0300456
457 /*
458 * Disable the watchdog timer. By default the boot manager sets
459 * the timer to 5 minutes before invoking a boot option. If we
460 * want to return to the boot manager, we have to disable the
461 * watchdog timer and since we're an interactive program, we don't
462 * want to wait until the user types "quit". The timer may have
463 * fired by then. We don't care if this fails. It does not prevent
464 * normal functioning in any way...
465 */
466 BS->SetWatchdogTimer(0, 0, 0, NULL);
467
Toomas Soomedbacaf52016-11-17 17:02:22 +0200468 if (find_currdev(img) != 0)
Toomas Soome199767f2015-10-25 00:06:51 +0300469 return (EFI_NOT_FOUND);
470
Toomas Soomeeee59042016-11-12 00:06:58 +0200471 efi_init_environment();
Toomas Soome199767f2015-10-25 00:06:51 +0300472 setenv("ISADIR", "amd64", 1); /* we only build 64bit */
Toomas Soomedcba96f2016-10-09 17:12:23 +0300473 acpi_detect();
Toomas Soome199767f2015-10-25 00:06:51 +0300474
Toomas Soomedcba96f2016-10-09 17:12:23 +0300475 if ((ptr = efi_get_table(&smbios3)) == NULL)
476 ptr = efi_get_table(&smbios);
477 smbios_detect(ptr);
Toomas Soome199767f2015-10-25 00:06:51 +0300478
Toomas Soome199767f2015-10-25 00:06:51 +0300479 interact(NULL); /* doesn't return */
480
481 return (EFI_SUCCESS); /* keep compiler happy */
482}
483
484COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
485
486static int
Toomas Soome30c75cb2016-03-13 20:45:57 +0200487command_reboot(int argc __unused, char *argv[] __unused)
Toomas Soome199767f2015-10-25 00:06:51 +0300488{
489 int i;
Toomas Soome199767f2015-10-25 00:06:51 +0300490
491 for (i = 0; devsw[i] != NULL; ++i)
492 if (devsw[i]->dv_cleanup != NULL)
493 (devsw[i]->dv_cleanup)();
494
Toomas Soome48d84432017-04-27 19:19:29 +0300495 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
Toomas Soome199767f2015-10-25 00:06:51 +0300496
497 /* NOTREACHED */
498 return (CMD_ERROR);
499}
500
501COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
502
503static int
Toomas Soome30c75cb2016-03-13 20:45:57 +0200504command_memmap(int argc __unused, char *argv[] __unused)
Toomas Soome199767f2015-10-25 00:06:51 +0300505{
506 UINTN sz;
507 EFI_MEMORY_DESCRIPTOR *map, *p;
508 UINTN key, dsz;
509 UINT32 dver;
510 EFI_STATUS status;
511 int i, ndesc;
512 int rv = 0;
513 char line[80];
Toomas Soome199767f2015-10-25 00:06:51 +0300514
515 sz = 0;
516 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
517 if (status != EFI_BUFFER_TOO_SMALL) {
518 printf("Can't determine memory map size\n");
519 return (CMD_ERROR);
520 }
521 map = malloc(sz);
522 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
523 if (EFI_ERROR(status)) {
524 printf("Can't read memory map\n");
525 return (CMD_ERROR);
526 }
527
528 ndesc = sz / dsz;
529 snprintf(line, 80, "%23s %12s %12s %8s %4s\n",
530 "Type", "Physical", "Virtual", "#Pages", "Attr");
531 pager_open();
532 rv = pager_output(line);
533 if (rv) {
534 pager_close();
535 return (CMD_OK);
536 }
537
538 for (i = 0, p = map; i < ndesc;
539 i++, p = NextMemoryDescriptor(p, dsz)) {
540 snprintf(line, 80, "%23s %012lx %012lx %08lx ",
Toomas Soomeeee59042016-11-12 00:06:58 +0200541 efi_memory_type(p->Type),
Toomas Soome199767f2015-10-25 00:06:51 +0300542 p->PhysicalStart,
543 p->VirtualStart,
544 p->NumberOfPages);
545 rv = pager_output(line);
546 if (rv)
547 break;
548
549 if (p->Attribute & EFI_MEMORY_UC)
550 printf("UC ");
551 if (p->Attribute & EFI_MEMORY_WC)
552 printf("WC ");
553 if (p->Attribute & EFI_MEMORY_WT)
554 printf("WT ");
555 if (p->Attribute & EFI_MEMORY_WB)
556 printf("WB ");
557 if (p->Attribute & EFI_MEMORY_UCE)
558 printf("UCE ");
559 if (p->Attribute & EFI_MEMORY_WP)
560 printf("WP ");
561 if (p->Attribute & EFI_MEMORY_RP)
562 printf("RP ");
563 if (p->Attribute & EFI_MEMORY_XP)
564 printf("XP ");
Toomas Soomeeee59042016-11-12 00:06:58 +0200565 if (p->Attribute & EFI_MEMORY_NV)
566 printf("NV ");
567 if (p->Attribute & EFI_MEMORY_MORE_RELIABLE)
568 printf("MR ");
569 if (p->Attribute & EFI_MEMORY_RO)
570 printf("RO ");
Toomas Soome199767f2015-10-25 00:06:51 +0300571 rv = pager_output("\n");
572 if (rv)
573 break;
574 }
575
576 pager_close();
577 return (CMD_OK);
578}
579
580COMMAND_SET(configuration, "configuration", "print configuration tables",
581 command_configuration);
582
Toomas Soome199767f2015-10-25 00:06:51 +0300583static int
Toomas Soome30c75cb2016-03-13 20:45:57 +0200584command_configuration(int argc __unused, char *argv[] __unused)
Toomas Soome199767f2015-10-25 00:06:51 +0300585{
586 UINTN i;
Toomas Soomeeee59042016-11-12 00:06:58 +0200587 char *name;
Toomas Soome199767f2015-10-25 00:06:51 +0300588
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;
Toomas Soomeeee59042016-11-12 00:06:58 +0200596
597 if (efi_guid_to_name(guid, &name) == true) {
598 printf(name);
599 free(name);
600 } else {
601 printf("Error while translating UUID to name");
602 }
Toomas Soome199767f2015-10-25 00:06:51 +0300603 printf(" at %p\n", ST->ConfigurationTable[i].VendorTable);
604 }
605
606 return (CMD_OK);
607}
608
609
610COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode);
611
612static int
613command_mode(int argc, char *argv[])
614{
615 UINTN cols, rows;
616 unsigned int mode;
617 int i;
618 char *cp;
619 char rowenv[8];
620 EFI_STATUS status;
621 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
622 extern void HO(void);
623
624 conout = ST->ConOut;
625
626 if (argc > 1) {
627 mode = strtol(argv[1], &cp, 0);
628 if (cp[0] != '\0') {
629 printf("Invalid mode\n");
630 return (CMD_ERROR);
631 }
632 status = conout->QueryMode(conout, mode, &cols, &rows);
633 if (EFI_ERROR(status)) {
634 printf("invalid mode %d\n", mode);
635 return (CMD_ERROR);
636 }
637 status = conout->SetMode(conout, mode);
638 if (EFI_ERROR(status)) {
639 printf("couldn't set mode %d\n", mode);
640 return (CMD_ERROR);
641 }
642 sprintf(rowenv, "%u", (unsigned)rows);
643 setenv("LINES", rowenv, 1);
644 sprintf(rowenv, "%u", (unsigned)cols);
645 setenv("COLUMNS", rowenv, 1);
646 HO(); /* set cursor */
647 return (CMD_OK);
648 }
649
650 printf("Current mode: %d\n", conout->Mode->Mode);
651 for (i = 0; i <= conout->Mode->MaxMode; i++) {
652 status = conout->QueryMode(conout, i, &cols, &rows);
653 if (EFI_ERROR(status))
654 continue;
655 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols,
656 (unsigned)rows);
657 }
658
659 if (i != 0)
660 printf("Select a mode with the command \"mode <number>\"\n");
661
662 return (CMD_OK);
663}
664
Toomas Soome199767f2015-10-25 00:06:51 +0300665COMMAND_SET(lsefi, "lsefi", "list EFI handles", command_lsefi);
666
667static int
Toomas Soome30c75cb2016-03-13 20:45:57 +0200668command_lsefi(int argc __unused, char *argv[] __unused)
Toomas Soome199767f2015-10-25 00:06:51 +0300669{
Toomas Soomeeee59042016-11-12 00:06:58 +0200670 char *name;
Toomas Soome199767f2015-10-25 00:06:51 +0300671 EFI_HANDLE *buffer = NULL;
672 EFI_HANDLE handle;
673 UINTN bufsz = 0, i, j;
674 EFI_STATUS status;
Toomas Soomeeee59042016-11-12 00:06:58 +0200675 int ret;
Toomas Soome199767f2015-10-25 00:06:51 +0300676
677 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
678 if (status != EFI_BUFFER_TOO_SMALL) {
679 snprintf(command_errbuf, sizeof (command_errbuf),
680 "unexpected error: %lld", (long long)status);
681 return (CMD_ERROR);
682 }
683 if ((buffer = malloc(bufsz)) == NULL) {
684 sprintf(command_errbuf, "out of memory");
685 return (CMD_ERROR);
686 }
687
688 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
689 if (EFI_ERROR(status)) {
690 free(buffer);
691 snprintf(command_errbuf, sizeof (command_errbuf),
692 "LocateHandle() error: %lld", (long long)status);
693 return (CMD_ERROR);
694 }
695
696 pager_open();
697 for (i = 0; i < (bufsz / sizeof (EFI_HANDLE)); i++) {
698 UINTN nproto = 0;
699 EFI_GUID **protocols = NULL;
700
701 handle = buffer[i];
702 printf("Handle %p", handle);
703 if (pager_output("\n"))
704 break;
705 /* device path */
706
707 status = BS->ProtocolsPerHandle(handle, &protocols, &nproto);
708 if (EFI_ERROR(status)) {
709 snprintf(command_errbuf, sizeof (command_errbuf),
710 "ProtocolsPerHandle() error: %lld",
711 (long long)status);
712 continue;
713 }
Toomas Soomeeee59042016-11-12 00:06:58 +0200714
Toomas Soome199767f2015-10-25 00:06:51 +0300715 for (j = 0; j < nproto; j++) {
Toomas Soomeeee59042016-11-12 00:06:58 +0200716 if (efi_guid_to_name(protocols[j], &name) == true) {
717 printf(" %s", name);
718 free(name);
719 } else {
720 printf("Error while translating UUID to name");
721 }
722 if ((ret = pager_output("\n")) != 0)
Toomas Soome199767f2015-10-25 00:06:51 +0300723 break;
724 }
725 BS->FreePool(protocols);
Toomas Soomeeee59042016-11-12 00:06:58 +0200726 if (ret != 0)
Toomas Soome199767f2015-10-25 00:06:51 +0300727 break;
728 }
729 pager_close();
730 free(buffer);
731 return (CMD_OK);
732}
733
Toomas Soome199767f2015-10-25 00:06:51 +0300734COMMAND_SET(lszfs, "lszfs", "list child datasets of a zfs dataset",
735 command_lszfs);
736
737static int
738command_lszfs(int argc, char *argv[])
739{
740 int err;
741
742 if (argc != 2) {
743 command_errmsg = "wrong number of arguments";
744 return (CMD_ERROR);
745 }
746
747 err = zfs_list(argv[1]);
748 if (err != 0) {
749 command_errmsg = strerror(err);
750 return (CMD_ERROR);
751 }
752 return (CMD_OK);
753}
754
755#ifdef __FreeBSD__
756COMMAND_SET(reloadbe, "reloadbe", "refresh the list of ZFS Boot Environments",
757 command_reloadbe);
758
759static int
760command_reloadbe(int argc, char *argv[])
761{
762 int err;
763 char *root;
764
765 if (argc > 2) {
766 command_errmsg = "wrong number of arguments";
767 return (CMD_ERROR);
768 }
769
770 if (argc == 2) {
771 err = zfs_bootenv(argv[1]);
772 } else {
773 root = getenv("zfs_be_root");
774 if (root == NULL) {
775 return (CMD_OK);
776 }
777 err = zfs_bootenv(root);
778 }
779
780 if (err != 0) {
781 command_errmsg = strerror(err);
782 return (CMD_ERROR);
783 }
784
785 return (CMD_OK);
786}
787#endif /* __FreeBSD__ */
Toomas Soome199767f2015-10-25 00:06:51 +0300788
Toomas Soome199767f2015-10-25 00:06:51 +0300789#ifdef LOADER_FDT_SUPPORT
790extern int command_fdt_internal(int argc, char *argv[]);
791
792/*
793 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
794 * and declaring it as extern is in contradiction with COMMAND_SET() macro
795 * (which uses static pointer), we're defining wrapper function, which
796 * calls the proper fdt handling routine.
797 */
798static int
799command_fdt(int argc, char *argv[])
800{
801 return (command_fdt_internal(argc, argv));
802}
803
804COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);
805#endif
806
Toomas Soomef9feecc2016-10-09 17:30:28 +0300807/*
808 * Chain load another efi loader.
809 */
810static int
811command_chain(int argc, char *argv[])
812{
813 EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL;
814 EFI_HANDLE loaderhandle;
815 EFI_LOADED_IMAGE *loaded_image;
816 EFI_STATUS status;
817 struct stat st;
818 struct devdesc *dev;
819 char *name, *path;
820 void *buf;
821 int fd;
822
823 if (argc < 2) {
824 command_errmsg = "wrong number of arguments";
825 return (CMD_ERROR);
826 }
827
828 name = argv[1];
829
830 if ((fd = open(name, O_RDONLY)) < 0) {
831 command_errmsg = "no such file";
832 return (CMD_ERROR);
833 }
834
835 if (fstat(fd, &st) < -1) {
836 command_errmsg = "stat failed";
837 close(fd);
838 return (CMD_ERROR);
839 }
840
841 status = BS->AllocatePool(EfiLoaderCode, (UINTN)st.st_size, &buf);
842 if (status != EFI_SUCCESS) {
843 command_errmsg = "failed to allocate buffer";
844 close(fd);
845 return (CMD_ERROR);
846 }
847 if (read(fd, buf, st.st_size) != st.st_size) {
848 command_errmsg = "error while reading the file";
849 (void)BS->FreePool(buf);
850 close(fd);
851 return (CMD_ERROR);
852 }
853 close(fd);
854 status = BS->LoadImage(FALSE, IH, NULL, buf, st.st_size, &loaderhandle);
855 (void)BS->FreePool(buf);
856 if (status != EFI_SUCCESS) {
857 command_errmsg = "LoadImage failed";
858 return (CMD_ERROR);
859 }
860 status = BS->HandleProtocol(loaderhandle, &LoadedImageGUID,
861 (void **)&loaded_image);
862
863 if (argc > 2) {
864 int i, len = 0;
865 CHAR16 *argp;
866
867 for (i = 2; i < argc; i++)
868 len += strlen(argv[i]) + 1;
869
870 len *= sizeof (*argp);
871 loaded_image->LoadOptions = argp = malloc (len);
872 if (loaded_image->LoadOptions == NULL) {
873 (void) BS->UnloadImage(loaded_image);
874 return (CMD_ERROR);
875 }
876 loaded_image->LoadOptionsSize = len;
877 for (i = 2; i < argc; i++) {
878 char *ptr = argv[i];
879 while (*ptr)
880 *(argp++) = *(ptr++);
881 *(argp++) = ' ';
882 }
883 *(--argv) = 0;
884 }
885
Toomas Soomeeea30b22017-12-12 18:01:57 +0200886 if (efi_getdev((void **)&dev, name, (const char **)&path) == 0) {
887 struct zfs_devdesc *z_dev;
888 struct disk_devdesc *d_dev;
889 pdinfo_t *hd, *pd;
890
Toomas Soomec142ce12018-03-13 14:16:40 +0200891 switch (dev->d_dev->dv_type) {
Toomas Soomeeea30b22017-12-12 18:01:57 +0200892 case DEVT_ZFS:
893 z_dev = (struct zfs_devdesc *)dev;
894 loaded_image->DeviceHandle =
895 efizfs_get_handle_by_guid(z_dev->pool_guid);
896 break;
897 case DEVT_NET:
898 loaded_image->DeviceHandle =
899 efi_find_handle(dev->d_dev, dev->d_unit);
900 break;
901 default:
902 hd = efiblk_get_pdinfo(dev);
903 if (STAILQ_EMPTY(&hd->pd_part)) {
904 loaded_image->DeviceHandle = hd->pd_handle;
905 break;
906 }
907 d_dev = (struct disk_devdesc *)dev;
908 STAILQ_FOREACH(pd, &hd->pd_part, pd_link) {
909 /*
910 * d_partition should be 255
911 */
912 if (pd->pd_unit == d_dev->d_slice) {
913 loaded_image->DeviceHandle =
914 pd->pd_handle;
915 break;
916 }
917 }
918 break;
919 }
920 }
Toomas Soomef9feecc2016-10-09 17:30:28 +0300921
922 dev_cleanup();
923 status = BS->StartImage(loaderhandle, NULL, NULL);
924 if (status != EFI_SUCCESS) {
925 command_errmsg = "StartImage failed";
926 free(loaded_image->LoadOptions);
927 loaded_image->LoadOptions = NULL;
928 status = BS->UnloadImage(loaded_image);
929 return (CMD_ERROR);
930 }
931
932 return (CMD_ERROR); /* not reached */
933}
934
935COMMAND_SET(chain, "chain", "chain load file", command_chain);