blob: 547418679ac00a946a51c3f5af51088250e731b2 [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 Soome9f23ea42017-12-12 15:23:09 +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;
164 currdev.d_type = currdev.d_dev->dv_type;
165 currdev.d_unit = unit;
Toomas Soomedbacaf52016-11-17 17:02:22 +0200166 devname = efi_fmtdev(&currdev);
167
168 env_setenv("currdev", EV_VOLATILE, devname, efi_setcurrdev,
169 env_nounset);
170 env_setenv("loaddev", EV_VOLATILE, devname, env_noset, env_nounset);
171}
172
173static int
174find_currdev(EFI_LOADED_IMAGE *img)
175{
176 pdinfo_list_t *pdi_list;
177 pdinfo_t *dp, *pp;
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200178 EFI_DEVICE_PATH *devpath, *copy;
179 EFI_HANDLE h;
Toomas Soomedbacaf52016-11-17 17:02:22 +0200180 char *devname;
181 struct devsw *dev;
182 int unit;
183 uint64_t extra;
184
185 /* Did efi_zfs_probe() detect the boot pool? */
186 if (pool_guid != 0) {
187 struct zfs_devdesc currdev;
188
Toomas Soome76b35942018-03-13 11:58:56 +0200189 currdev.dd.d_dev = &zfs_dev;
190 currdev.dd.d_unit = 0;
191 currdev.dd.d_type = currdev.dd.d_dev->dv_type;
Toomas Soomedbacaf52016-11-17 17:02:22 +0200192 currdev.pool_guid = pool_guid;
193 currdev.root_guid = 0;
194 devname = efi_fmtdev(&currdev);
195
196 env_setenv("currdev", EV_VOLATILE, devname, efi_setcurrdev,
197 env_nounset);
198 env_setenv("loaddev", EV_VOLATILE, devname, env_noset,
199 env_nounset);
200 return (0);
201 }
202
203 /* We have device lists for hd, cd, fd, walk them all. */
204 pdi_list = efiblk_get_pdinfo_list(&efipart_hddev);
205 STAILQ_FOREACH(dp, pdi_list, pd_link) {
206 struct disk_devdesc currdev;
207
Toomas Soome76b35942018-03-13 11:58:56 +0200208 currdev.dd.d_dev = &efipart_hddev;
209 currdev.dd.d_type = currdev.dd.d_dev->dv_type;
210 currdev.dd.d_unit = dp->pd_unit;
Toomas Soomedbacaf52016-11-17 17:02:22 +0200211 currdev.d_slice = -1;
212 currdev.d_partition = -1;
213
214 if (dp->pd_handle == img->DeviceHandle) {
215 devname = efi_fmtdev(&currdev);
216
217 env_setenv("currdev", EV_VOLATILE, devname,
218 efi_setcurrdev, env_nounset);
219 env_setenv("loaddev", EV_VOLATILE, devname,
220 env_noset, env_nounset);
221 return (0);
222 }
223 /* Assuming GPT partitioning. */
224 STAILQ_FOREACH(pp, &dp->pd_part, pd_link) {
225 if (pp->pd_handle == img->DeviceHandle) {
226 currdev.d_slice = pp->pd_unit;
227 currdev.d_partition = 255;
228 devname = efi_fmtdev(&currdev);
229
230 env_setenv("currdev", EV_VOLATILE, devname,
231 efi_setcurrdev, env_nounset);
232 env_setenv("loaddev", EV_VOLATILE, devname,
233 env_noset, env_nounset);
234 return (0);
235 }
236 }
237 }
238
239 pdi_list = efiblk_get_pdinfo_list(&efipart_cddev);
240 STAILQ_FOREACH(dp, pdi_list, pd_link) {
241 if (dp->pd_handle == img->DeviceHandle ||
242 dp->pd_alias == img->DeviceHandle) {
243 set_devdesc_currdev(&efipart_cddev, dp->pd_unit);
244 return (0);
245 }
246 }
247
248 pdi_list = efiblk_get_pdinfo_list(&efipart_fddev);
249 STAILQ_FOREACH(dp, pdi_list, pd_link) {
250 if (dp->pd_handle == img->DeviceHandle) {
251 set_devdesc_currdev(&efipart_fddev, dp->pd_unit);
252 return (0);
253 }
254 }
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200255
256 /*
257 * Try the device handle from our loaded image first. If that
258 * fails, use the device path from the loaded image and see if
259 * any of the nodes in that path match one of the enumerated
260 * handles.
261 */
Toomas Soomedbacaf52016-11-17 17:02:22 +0200262 if (efi_handle_lookup(img->DeviceHandle, &dev, &unit, &extra) == 0) {
263 set_devdesc_currdev(dev, unit);
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200264 return (0);
Toomas Soomedbacaf52016-11-17 17:02:22 +0200265 }
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200266
267 copy = NULL;
268 devpath = efi_lookup_image_devpath(IH);
269 while (devpath != NULL) {
270 h = efi_devpath_handle(devpath);
271 if (h == NULL)
272 break;
273
274 free(copy);
275 copy = NULL;
276
Toomas Soomedbacaf52016-11-17 17:02:22 +0200277 if (efi_handle_lookup(h, &dev, &unit, &extra) == 0) {
278 set_devdesc_currdev(dev, unit);
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200279 return (0);
Toomas Soomedbacaf52016-11-17 17:02:22 +0200280 }
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200281
282 devpath = efi_lookup_devpath(h);
283 if (devpath != NULL) {
284 copy = efi_devpath_trim(devpath);
285 devpath = copy;
286 }
287 }
288 free(copy);
289
290 return (ENOENT);
291}
292
Toomas Soome199767f2015-10-25 00:06:51 +0300293EFI_STATUS
294main(int argc, CHAR16 *argv[])
295{
296 char var[128];
Toomas Soome199767f2015-10-25 00:06:51 +0300297 EFI_GUID *guid;
Toomas Soomedbacaf52016-11-17 17:02:22 +0200298 int i, j, vargood, howto;
Toomas Soomedcba96f2016-10-09 17:12:23 +0300299 void *ptr;
Toomas Soome199767f2015-10-25 00:06:51 +0300300 UINTN k;
301 int has_kbd;
302
303 archsw.arch_autoload = efi_autoload;
304 archsw.arch_getdev = efi_getdev;
305 archsw.arch_copyin = efi_copyin;
306 archsw.arch_copyout = efi_copyout;
307 archsw.arch_readin = efi_readin;
Toomas Soomef9feecc2016-10-09 17:30:28 +0300308 archsw.arch_loadaddr = efi_loadaddr;
309 archsw.arch_free_loadaddr = efi_free_loadaddr;
Toomas Soome199767f2015-10-25 00:06:51 +0300310 /* Note this needs to be set before ZFS init. */
311 archsw.arch_zfs_probe = efi_zfs_probe;
Toomas Soome9f23ea42017-12-12 15:23:09 +0200312
313 /* Get our loaded image protocol interface structure. */
314 BS->HandleProtocol(IH, &imgid, (VOID**)&img);
Toomas Soome199767f2015-10-25 00:06:51 +0300315
Toomas Soome04f8e092016-11-10 21:05:15 +0200316 /* Init the time source */
317 efi_time_init();
318
Toomas Soome199767f2015-10-25 00:06:51 +0300319 has_kbd = has_keyboard();
320
321 /*
322 * XXX Chicken-and-egg problem; we want to have console output
323 * early, but some console attributes may depend on reading from
324 * eg. the boot device, which we can't do yet. We can use
325 * printf() etc. once this is done.
326 */
327 cons_probe();
Toomas Soomef9feecc2016-10-09 17:30:28 +0300328 efi_getsmap();
Toomas Soome199767f2015-10-25 00:06:51 +0300329
330 /*
331 * Initialise the block cache. Set the upper limit.
332 */
333 bcache_init(32768, 512);
334
335 /*
336 * Parse the args to set the console settings, etc
337 * boot1.efi passes these in, if it can read /boot.config or /boot/config
338 * or iPXE may be setup to pass these in.
339 *
340 * Loop through the args, and for each one that contains an '=' that is
341 * not the first character, add it to the environment. This allows
342 * loader and kernel env vars to be passed on the command line. Convert
343 * args from UCS-2 to ASCII (16 to 8 bit) as they are copied.
344 */
345 howto = 0;
346 for (i = 1; i < argc; i++) {
347 if (argv[i][0] == '-') {
348 for (j = 1; argv[i][j] != 0; j++) {
349 int ch;
350
351 ch = argv[i][j];
352 switch (ch) {
353 case 'a':
354 howto |= RB_ASKNAME;
355 break;
356 case 'd':
357 howto |= RB_KDB;
358 break;
359 case 'D':
360 howto |= RB_MULTIPLE;
361 break;
362 case 'h':
363 howto |= RB_SERIAL;
364 break;
365 case 'm':
366 howto |= RB_MUTE;
367 break;
368 case 'p':
369 howto |= RB_PAUSE;
370 break;
371 case 'P':
372 if (!has_kbd)
373 howto |= RB_SERIAL | RB_MULTIPLE;
374 break;
375 case 'r':
376 howto |= RB_DFLTROOT;
377 break;
378 case 's':
379 howto |= RB_SINGLE;
380 break;
381 case 'S':
382 if (argv[i][j + 1] == 0) {
383 if (i + 1 == argc) {
Toomas Soome2aca6c62017-06-02 23:07:12 +0300384 strncpy(var, "115200",
Toomas Soome199767f2015-10-25 00:06:51 +0300385 sizeof(var));
Toomas Soome2aca6c62017-06-02 23:07:12 +0300386 } else {
387 CHAR16 *ptr;
388 ptr = &argv[i + 1][0];
389 cpy16to8(ptr, var,
390 sizeof(var));
Toomas Soome199767f2015-10-25 00:06:51 +0300391 }
392 i++;
Toomas Soome199767f2015-10-25 00:06:51 +0300393 } else {
Toomas Soomeeee59042016-11-12 00:06:58 +0200394 cpy16to8(&argv[i][j + 1], var,
Toomas Soome199767f2015-10-25 00:06:51 +0300395 sizeof(var));
Toomas Soome199767f2015-10-25 00:06:51 +0300396 }
Toomas Soome2aca6c62017-06-02 23:07:12 +0300397 strncat(var, ",8,n,1,-", sizeof(var));
398 setenv("ttya-mode", var, 1);
399 break;
Toomas Soome199767f2015-10-25 00:06:51 +0300400 case 'v':
401 howto |= RB_VERBOSE;
402 break;
403 }
404 }
405 } else {
406 vargood = 0;
407 for (j = 0; argv[i][j] != 0; j++) {
408 if (j == sizeof(var)) {
409 vargood = 0;
410 break;
411 }
412 if (j > 0 && argv[i][j] == '=')
413 vargood = 1;
414 var[j] = (char)argv[i][j];
415 }
416 if (vargood) {
417 var[j] = 0;
418 putenv(var);
419 }
420 }
421 }
422 for (i = 0; howto_names[i].ev != NULL; i++)
423 if (howto & howto_names[i].mask)
424 setenv(howto_names[i].ev, "YES", 1);
425 if (howto & RB_MULTIPLE) {
426 if (howto & RB_SERIAL)
427 setenv("console", "ttya text" , 1);
428 else
429 setenv("console", "text ttya" , 1);
430 } else if (howto & RB_SERIAL) {
431 setenv("console", "ttya" , 1);
432 }
433
Toomas Soome199767f2015-10-25 00:06:51 +0300434 /*
Toomas Soomec00b6c92018-02-19 19:14:22 +0200435 * Scan the BLOCK IO MEDIA handles then
436 * march through the device switch probing for things.
Toomas Soome199767f2015-10-25 00:06:51 +0300437 */
Toomas Soomec00b6c92018-02-19 19:14:22 +0200438 if ((i = efipart_inithandles()) == 0) {
439 for (i = 0; devsw[i] != NULL; i++)
440 if (devsw[i]->dv_init != NULL)
441 (devsw[i]->dv_init)();
442 } else
443 printf("efipart_inithandles failed %d, expect failures", i);
Toomas Soome199767f2015-10-25 00:06:51 +0300444
Toomas Soome199767f2015-10-25 00:06:51 +0300445 printf("Command line arguments:");
446 for (i = 0; i < argc; i++) {
Toomas Soomeeee59042016-11-12 00:06:58 +0200447 printf(" %S", argv[i]);
Toomas Soome199767f2015-10-25 00:06:51 +0300448 }
449 printf("\n");
450
451 printf("Image base: 0x%lx\n", (u_long)img->ImageBase);
452 printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16,
453 ST->Hdr.Revision & 0xffff);
Toomas Soomeeee59042016-11-12 00:06:58 +0200454 printf("EFI Firmware: %S (rev %d.%02d)\n", ST->FirmwareVendor,
455 ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
Toomas Soome199767f2015-10-25 00:06:51 +0300456
Toomas Soomef6c94442017-01-03 11:24:09 +0200457 printf("\n%s", bootprog_info);
Toomas Soome199767f2015-10-25 00:06:51 +0300458
459 /*
460 * Disable the watchdog timer. By default the boot manager sets
461 * the timer to 5 minutes before invoking a boot option. If we
462 * want to return to the boot manager, we have to disable the
463 * watchdog timer and since we're an interactive program, we don't
464 * want to wait until the user types "quit". The timer may have
465 * fired by then. We don't care if this fails. It does not prevent
466 * normal functioning in any way...
467 */
468 BS->SetWatchdogTimer(0, 0, 0, NULL);
469
Toomas Soomedbacaf52016-11-17 17:02:22 +0200470 if (find_currdev(img) != 0)
Toomas Soome199767f2015-10-25 00:06:51 +0300471 return (EFI_NOT_FOUND);
472
Toomas Soomeeee59042016-11-12 00:06:58 +0200473 efi_init_environment();
Toomas Soome199767f2015-10-25 00:06:51 +0300474 setenv("ISADIR", "amd64", 1); /* we only build 64bit */
Toomas Soomedcba96f2016-10-09 17:12:23 +0300475 acpi_detect();
Toomas Soome199767f2015-10-25 00:06:51 +0300476
Toomas Soomedcba96f2016-10-09 17:12:23 +0300477 if ((ptr = efi_get_table(&smbios3)) == NULL)
478 ptr = efi_get_table(&smbios);
479 smbios_detect(ptr);
Toomas Soome199767f2015-10-25 00:06:51 +0300480
Toomas Soome199767f2015-10-25 00:06:51 +0300481 interact(NULL); /* doesn't return */
482
483 return (EFI_SUCCESS); /* keep compiler happy */
484}
485
486COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
487
488static int
Toomas Soome30c75cb2016-03-13 20:45:57 +0200489command_reboot(int argc __unused, char *argv[] __unused)
Toomas Soome199767f2015-10-25 00:06:51 +0300490{
491 int i;
Toomas Soome199767f2015-10-25 00:06:51 +0300492
493 for (i = 0; devsw[i] != NULL; ++i)
494 if (devsw[i]->dv_cleanup != NULL)
495 (devsw[i]->dv_cleanup)();
496
Toomas Soome48d84432017-04-27 19:19:29 +0300497 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
Toomas Soome199767f2015-10-25 00:06:51 +0300498
499 /* NOTREACHED */
500 return (CMD_ERROR);
501}
502
503COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
504
505static int
Toomas Soome30c75cb2016-03-13 20:45:57 +0200506command_memmap(int argc __unused, char *argv[] __unused)
Toomas Soome199767f2015-10-25 00:06:51 +0300507{
508 UINTN sz;
509 EFI_MEMORY_DESCRIPTOR *map, *p;
510 UINTN key, dsz;
511 UINT32 dver;
512 EFI_STATUS status;
513 int i, ndesc;
514 int rv = 0;
515 char line[80];
Toomas Soome199767f2015-10-25 00:06:51 +0300516
517 sz = 0;
518 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
519 if (status != EFI_BUFFER_TOO_SMALL) {
520 printf("Can't determine memory map size\n");
521 return (CMD_ERROR);
522 }
523 map = malloc(sz);
524 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
525 if (EFI_ERROR(status)) {
526 printf("Can't read memory map\n");
527 return (CMD_ERROR);
528 }
529
530 ndesc = sz / dsz;
531 snprintf(line, 80, "%23s %12s %12s %8s %4s\n",
532 "Type", "Physical", "Virtual", "#Pages", "Attr");
533 pager_open();
534 rv = pager_output(line);
535 if (rv) {
536 pager_close();
537 return (CMD_OK);
538 }
539
540 for (i = 0, p = map; i < ndesc;
541 i++, p = NextMemoryDescriptor(p, dsz)) {
542 snprintf(line, 80, "%23s %012lx %012lx %08lx ",
Toomas Soomeeee59042016-11-12 00:06:58 +0200543 efi_memory_type(p->Type),
Toomas Soome199767f2015-10-25 00:06:51 +0300544 p->PhysicalStart,
545 p->VirtualStart,
546 p->NumberOfPages);
547 rv = pager_output(line);
548 if (rv)
549 break;
550
551 if (p->Attribute & EFI_MEMORY_UC)
552 printf("UC ");
553 if (p->Attribute & EFI_MEMORY_WC)
554 printf("WC ");
555 if (p->Attribute & EFI_MEMORY_WT)
556 printf("WT ");
557 if (p->Attribute & EFI_MEMORY_WB)
558 printf("WB ");
559 if (p->Attribute & EFI_MEMORY_UCE)
560 printf("UCE ");
561 if (p->Attribute & EFI_MEMORY_WP)
562 printf("WP ");
563 if (p->Attribute & EFI_MEMORY_RP)
564 printf("RP ");
565 if (p->Attribute & EFI_MEMORY_XP)
566 printf("XP ");
Toomas Soomeeee59042016-11-12 00:06:58 +0200567 if (p->Attribute & EFI_MEMORY_NV)
568 printf("NV ");
569 if (p->Attribute & EFI_MEMORY_MORE_RELIABLE)
570 printf("MR ");
571 if (p->Attribute & EFI_MEMORY_RO)
572 printf("RO ");
Toomas Soome199767f2015-10-25 00:06:51 +0300573 rv = pager_output("\n");
574 if (rv)
575 break;
576 }
577
578 pager_close();
579 return (CMD_OK);
580}
581
582COMMAND_SET(configuration, "configuration", "print configuration tables",
583 command_configuration);
584
Toomas Soome199767f2015-10-25 00:06:51 +0300585static int
Toomas Soome30c75cb2016-03-13 20:45:57 +0200586command_configuration(int argc __unused, char *argv[] __unused)
Toomas Soome199767f2015-10-25 00:06:51 +0300587{
588 UINTN i;
Toomas Soomeeee59042016-11-12 00:06:58 +0200589 char *name;
Toomas Soome199767f2015-10-25 00:06:51 +0300590
591 printf("NumberOfTableEntries=%lu\n",
592 (unsigned long)ST->NumberOfTableEntries);
593 for (i = 0; i < ST->NumberOfTableEntries; i++) {
594 EFI_GUID *guid;
595
596 printf(" ");
597 guid = &ST->ConfigurationTable[i].VendorGuid;
Toomas Soomeeee59042016-11-12 00:06:58 +0200598
599 if (efi_guid_to_name(guid, &name) == true) {
600 printf(name);
601 free(name);
602 } else {
603 printf("Error while translating UUID to name");
604 }
Toomas Soome199767f2015-10-25 00:06:51 +0300605 printf(" at %p\n", ST->ConfigurationTable[i].VendorTable);
606 }
607
608 return (CMD_OK);
609}
610
611
612COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode);
613
614static int
615command_mode(int argc, char *argv[])
616{
617 UINTN cols, rows;
618 unsigned int mode;
619 int i;
620 char *cp;
621 char rowenv[8];
622 EFI_STATUS status;
623 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
624 extern void HO(void);
625
626 conout = ST->ConOut;
627
628 if (argc > 1) {
629 mode = strtol(argv[1], &cp, 0);
630 if (cp[0] != '\0') {
631 printf("Invalid mode\n");
632 return (CMD_ERROR);
633 }
634 status = conout->QueryMode(conout, mode, &cols, &rows);
635 if (EFI_ERROR(status)) {
636 printf("invalid mode %d\n", mode);
637 return (CMD_ERROR);
638 }
639 status = conout->SetMode(conout, mode);
640 if (EFI_ERROR(status)) {
641 printf("couldn't set mode %d\n", mode);
642 return (CMD_ERROR);
643 }
644 sprintf(rowenv, "%u", (unsigned)rows);
645 setenv("LINES", rowenv, 1);
646 sprintf(rowenv, "%u", (unsigned)cols);
647 setenv("COLUMNS", rowenv, 1);
648 HO(); /* set cursor */
649 return (CMD_OK);
650 }
651
652 printf("Current mode: %d\n", conout->Mode->Mode);
653 for (i = 0; i <= conout->Mode->MaxMode; i++) {
654 status = conout->QueryMode(conout, i, &cols, &rows);
655 if (EFI_ERROR(status))
656 continue;
657 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols,
658 (unsigned)rows);
659 }
660
661 if (i != 0)
662 printf("Select a mode with the command \"mode <number>\"\n");
663
664 return (CMD_OK);
665}
666
Toomas Soome199767f2015-10-25 00:06:51 +0300667COMMAND_SET(lsefi, "lsefi", "list EFI handles", command_lsefi);
668
669static int
Toomas Soome30c75cb2016-03-13 20:45:57 +0200670command_lsefi(int argc __unused, char *argv[] __unused)
Toomas Soome199767f2015-10-25 00:06:51 +0300671{
Toomas Soomeeee59042016-11-12 00:06:58 +0200672 char *name;
Toomas Soome199767f2015-10-25 00:06:51 +0300673 EFI_HANDLE *buffer = NULL;
674 EFI_HANDLE handle;
675 UINTN bufsz = 0, i, j;
676 EFI_STATUS status;
Toomas Soomeeee59042016-11-12 00:06:58 +0200677 int ret;
Toomas Soome199767f2015-10-25 00:06:51 +0300678
679 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
680 if (status != EFI_BUFFER_TOO_SMALL) {
681 snprintf(command_errbuf, sizeof (command_errbuf),
682 "unexpected error: %lld", (long long)status);
683 return (CMD_ERROR);
684 }
685 if ((buffer = malloc(bufsz)) == NULL) {
686 sprintf(command_errbuf, "out of memory");
687 return (CMD_ERROR);
688 }
689
690 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
691 if (EFI_ERROR(status)) {
692 free(buffer);
693 snprintf(command_errbuf, sizeof (command_errbuf),
694 "LocateHandle() error: %lld", (long long)status);
695 return (CMD_ERROR);
696 }
697
698 pager_open();
699 for (i = 0; i < (bufsz / sizeof (EFI_HANDLE)); i++) {
700 UINTN nproto = 0;
701 EFI_GUID **protocols = NULL;
702
703 handle = buffer[i];
704 printf("Handle %p", handle);
705 if (pager_output("\n"))
706 break;
707 /* device path */
708
709 status = BS->ProtocolsPerHandle(handle, &protocols, &nproto);
710 if (EFI_ERROR(status)) {
711 snprintf(command_errbuf, sizeof (command_errbuf),
712 "ProtocolsPerHandle() error: %lld",
713 (long long)status);
714 continue;
715 }
Toomas Soomeeee59042016-11-12 00:06:58 +0200716
Toomas Soome199767f2015-10-25 00:06:51 +0300717 for (j = 0; j < nproto; j++) {
Toomas Soomeeee59042016-11-12 00:06:58 +0200718 if (efi_guid_to_name(protocols[j], &name) == true) {
719 printf(" %s", name);
720 free(name);
721 } else {
722 printf("Error while translating UUID to name");
723 }
724 if ((ret = pager_output("\n")) != 0)
Toomas Soome199767f2015-10-25 00:06:51 +0300725 break;
726 }
727 BS->FreePool(protocols);
Toomas Soomeeee59042016-11-12 00:06:58 +0200728 if (ret != 0)
Toomas Soome199767f2015-10-25 00:06:51 +0300729 break;
730 }
731 pager_close();
732 free(buffer);
733 return (CMD_OK);
734}
735
Toomas Soome199767f2015-10-25 00:06:51 +0300736COMMAND_SET(lszfs, "lszfs", "list child datasets of a zfs dataset",
737 command_lszfs);
738
739static int
740command_lszfs(int argc, char *argv[])
741{
742 int err;
743
744 if (argc != 2) {
745 command_errmsg = "wrong number of arguments";
746 return (CMD_ERROR);
747 }
748
749 err = zfs_list(argv[1]);
750 if (err != 0) {
751 command_errmsg = strerror(err);
752 return (CMD_ERROR);
753 }
754 return (CMD_OK);
755}
756
757#ifdef __FreeBSD__
758COMMAND_SET(reloadbe, "reloadbe", "refresh the list of ZFS Boot Environments",
759 command_reloadbe);
760
761static int
762command_reloadbe(int argc, char *argv[])
763{
764 int err;
765 char *root;
766
767 if (argc > 2) {
768 command_errmsg = "wrong number of arguments";
769 return (CMD_ERROR);
770 }
771
772 if (argc == 2) {
773 err = zfs_bootenv(argv[1]);
774 } else {
775 root = getenv("zfs_be_root");
776 if (root == NULL) {
777 return (CMD_OK);
778 }
779 err = zfs_bootenv(root);
780 }
781
782 if (err != 0) {
783 command_errmsg = strerror(err);
784 return (CMD_ERROR);
785 }
786
787 return (CMD_OK);
788}
789#endif /* __FreeBSD__ */
Toomas Soome199767f2015-10-25 00:06:51 +0300790
Toomas Soome199767f2015-10-25 00:06:51 +0300791#ifdef LOADER_FDT_SUPPORT
792extern int command_fdt_internal(int argc, char *argv[]);
793
794/*
795 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
796 * and declaring it as extern is in contradiction with COMMAND_SET() macro
797 * (which uses static pointer), we're defining wrapper function, which
798 * calls the proper fdt handling routine.
799 */
800static int
801command_fdt(int argc, char *argv[])
802{
803 return (command_fdt_internal(argc, argv));
804}
805
806COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);
807#endif
808
Toomas Soomef9feecc2016-10-09 17:30:28 +0300809/*
810 * Chain load another efi loader.
811 */
812static int
813command_chain(int argc, char *argv[])
814{
815 EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL;
816 EFI_HANDLE loaderhandle;
817 EFI_LOADED_IMAGE *loaded_image;
818 EFI_STATUS status;
819 struct stat st;
820 struct devdesc *dev;
821 char *name, *path;
822 void *buf;
823 int fd;
824
825 if (argc < 2) {
826 command_errmsg = "wrong number of arguments";
827 return (CMD_ERROR);
828 }
829
830 name = argv[1];
831
832 if ((fd = open(name, O_RDONLY)) < 0) {
833 command_errmsg = "no such file";
834 return (CMD_ERROR);
835 }
836
837 if (fstat(fd, &st) < -1) {
838 command_errmsg = "stat failed";
839 close(fd);
840 return (CMD_ERROR);
841 }
842
843 status = BS->AllocatePool(EfiLoaderCode, (UINTN)st.st_size, &buf);
844 if (status != EFI_SUCCESS) {
845 command_errmsg = "failed to allocate buffer";
846 close(fd);
847 return (CMD_ERROR);
848 }
849 if (read(fd, buf, st.st_size) != st.st_size) {
850 command_errmsg = "error while reading the file";
851 (void)BS->FreePool(buf);
852 close(fd);
853 return (CMD_ERROR);
854 }
855 close(fd);
856 status = BS->LoadImage(FALSE, IH, NULL, buf, st.st_size, &loaderhandle);
857 (void)BS->FreePool(buf);
858 if (status != EFI_SUCCESS) {
859 command_errmsg = "LoadImage failed";
860 return (CMD_ERROR);
861 }
862 status = BS->HandleProtocol(loaderhandle, &LoadedImageGUID,
863 (void **)&loaded_image);
864
865 if (argc > 2) {
866 int i, len = 0;
867 CHAR16 *argp;
868
869 for (i = 2; i < argc; i++)
870 len += strlen(argv[i]) + 1;
871
872 len *= sizeof (*argp);
873 loaded_image->LoadOptions = argp = malloc (len);
874 if (loaded_image->LoadOptions == NULL) {
875 (void) BS->UnloadImage(loaded_image);
876 return (CMD_ERROR);
877 }
878 loaded_image->LoadOptionsSize = len;
879 for (i = 2; i < argc; i++) {
880 char *ptr = argv[i];
881 while (*ptr)
882 *(argp++) = *(ptr++);
883 *(argp++) = ' ';
884 }
885 *(--argv) = 0;
886 }
887
Toomas Soomeeea30b22017-12-12 18:01:57 +0200888 if (efi_getdev((void **)&dev, name, (const char **)&path) == 0) {
889 struct zfs_devdesc *z_dev;
890 struct disk_devdesc *d_dev;
891 pdinfo_t *hd, *pd;
892
893 switch (dev->d_type) {
894 case DEVT_ZFS:
895 z_dev = (struct zfs_devdesc *)dev;
896 loaded_image->DeviceHandle =
897 efizfs_get_handle_by_guid(z_dev->pool_guid);
898 break;
899 case DEVT_NET:
900 loaded_image->DeviceHandle =
901 efi_find_handle(dev->d_dev, dev->d_unit);
902 break;
903 default:
904 hd = efiblk_get_pdinfo(dev);
905 if (STAILQ_EMPTY(&hd->pd_part)) {
906 loaded_image->DeviceHandle = hd->pd_handle;
907 break;
908 }
909 d_dev = (struct disk_devdesc *)dev;
910 STAILQ_FOREACH(pd, &hd->pd_part, pd_link) {
911 /*
912 * d_partition should be 255
913 */
914 if (pd->pd_unit == d_dev->d_slice) {
915 loaded_image->DeviceHandle =
916 pd->pd_handle;
917 break;
918 }
919 }
920 break;
921 }
922 }
Toomas Soomef9feecc2016-10-09 17:30:28 +0300923
924 dev_cleanup();
925 status = BS->StartImage(loaderhandle, NULL, NULL);
926 if (status != EFI_SUCCESS) {
927 command_errmsg = "StartImage failed";
928 free(loaded_image->LoadOptions);
929 loaded_image->LoadOptions = NULL;
930 status = BS->UnloadImage(loaded_image);
931 return (CMD_ERROR);
932 }
933
934 return (CMD_ERROR); /* not reached */
935}
936
937COMMAND_SET(chain, "chain", "chain load file", command_chain);