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