blob: 00cbf668014001fcee25eae5cf92c63153ab6118 [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 Soome199767f2015-10-25 00:06:51 +030054struct arch_switch archsw; /* MI/MD interface boundary */
55
Toomas Soome199767f2015-10-25 00:06:51 +030056EFI_GUID devid = DEVICE_PATH_PROTOCOL;
57EFI_GUID imgid = LOADED_IMAGE_PROTOCOL;
Toomas Soome199767f2015-10-25 00:06:51 +030058EFI_GUID smbios = SMBIOS_TABLE_GUID;
59EFI_GUID smbios3 = SMBIOS3_TABLE_GUID;
Toomas Soome199767f2015-10-25 00:06:51 +030060EFI_GUID inputid = SIMPLE_TEXT_INPUT_PROTOCOL;
Toomas Soome199767f2015-10-25 00:06:51 +030061
Toomas Soomedcba96f2016-10-09 17:12:23 +030062extern void acpi_detect(void);
Toomas Soomef9feecc2016-10-09 17:30:28 +030063extern void efi_getsmap(void);
Toomas Soome9f23ea42017-12-12 15:23:09 +020064
65static EFI_LOADED_IMAGE *img;
66
67bool
68efi_zfs_is_preferred(EFI_HANDLE *h)
69{
70 return (h == img->DeviceHandle);
71}
Toomas Soome199767f2015-10-25 00:06:51 +030072
Toomas Soome199767f2015-10-25 00:06:51 +030073static int
74has_keyboard(void)
75{
76 EFI_STATUS status;
77 EFI_DEVICE_PATH *path;
78 EFI_HANDLE *hin, *hin_end, *walker;
79 UINTN sz;
80 int retval = 0;
Toomas Soome9f23ea42017-12-12 15:23:09 +020081
Toomas Soome199767f2015-10-25 00:06:51 +030082 /*
83 * Find all the handles that support the SIMPLE_TEXT_INPUT_PROTOCOL and
84 * do the typical dance to get the right sized buffer.
85 */
86 sz = 0;
87 hin = NULL;
88 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz, 0);
89 if (status == EFI_BUFFER_TOO_SMALL) {
90 hin = (EFI_HANDLE *)malloc(sz);
91 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz,
92 hin);
93 if (EFI_ERROR(status))
94 free(hin);
95 }
96 if (EFI_ERROR(status))
97 return retval;
98
99 /*
100 * Look at each of the handles. If it supports the device path protocol,
101 * use it to get the device path for this handle. Then see if that
102 * device path matches either the USB device path for keyboards or the
103 * legacy device path for keyboards.
104 */
105 hin_end = &hin[sz / sizeof(*hin)];
106 for (walker = hin; walker < hin_end; walker++) {
107 status = BS->HandleProtocol(*walker, &devid, (VOID **)&path);
108 if (EFI_ERROR(status))
109 continue;
110
111 while (!IsDevicePathEnd(path)) {
112 /*
113 * Check for the ACPI keyboard node. All PNP3xx nodes
114 * are keyboards of different flavors. Note: It is
115 * unclear of there's always a keyboard node when
116 * there's a keyboard controller, or if there's only one
117 * when a keyboard is detected at boot.
118 */
119 if (DevicePathType(path) == ACPI_DEVICE_PATH &&
120 (DevicePathSubType(path) == ACPI_DP ||
121 DevicePathSubType(path) == ACPI_EXTENDED_DP)) {
122 ACPI_HID_DEVICE_PATH *acpi;
123
124 acpi = (ACPI_HID_DEVICE_PATH *)(void *)path;
125 if ((EISA_ID_TO_NUM(acpi->HID) & 0xff00) == 0x300 &&
126 (acpi->HID & 0xffff) == PNP_EISA_ID_CONST) {
127 retval = 1;
128 goto out;
129 }
130 /*
131 * Check for USB keyboard node, if present. Unlike a
132 * PS/2 keyboard, these definitely only appear when
133 * connected to the system.
134 */
135 } else if (DevicePathType(path) == MESSAGING_DEVICE_PATH &&
136 DevicePathSubType(path) == MSG_USB_CLASS_DP) {
137 USB_CLASS_DEVICE_PATH *usb;
Toomas Soomec142ce12018-03-13 14:16:40 +0200138
Toomas Soome199767f2015-10-25 00:06:51 +0300139 usb = (USB_CLASS_DEVICE_PATH *)(void *)path;
140 if (usb->DeviceClass == 3 && /* HID */
141 usb->DeviceSubClass == 1 && /* Boot devices */
142 usb->DeviceProtocol == 1) { /* Boot keyboards */
143 retval = 1;
144 goto out;
145 }
146 }
147 path = NextDevicePathNode(path);
148 }
149 }
150out:
151 free(hin);
152 return retval;
153}
154
Toomas Soomedbacaf52016-11-17 17:02:22 +0200155static void
156set_devdesc_currdev(struct devsw *dev, int unit)
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200157{
Toomas Soomedbacaf52016-11-17 17:02:22 +0200158 struct devdesc currdev;
159 char *devname;
160
161 currdev.d_dev = dev;
Toomas Soomedbacaf52016-11-17 17:02:22 +0200162 currdev.d_unit = unit;
Toomas Soomedbacaf52016-11-17 17:02:22 +0200163 devname = efi_fmtdev(&currdev);
164
165 env_setenv("currdev", EV_VOLATILE, devname, efi_setcurrdev,
166 env_nounset);
167 env_setenv("loaddev", EV_VOLATILE, devname, env_noset, env_nounset);
168}
169
170static int
171find_currdev(EFI_LOADED_IMAGE *img)
172{
173 pdinfo_list_t *pdi_list;
174 pdinfo_t *dp, *pp;
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200175 EFI_DEVICE_PATH *devpath, *copy;
176 EFI_HANDLE h;
Toomas Soomedbacaf52016-11-17 17:02:22 +0200177 char *devname;
178 struct devsw *dev;
179 int unit;
180 uint64_t extra;
181
182 /* Did efi_zfs_probe() detect the boot pool? */
183 if (pool_guid != 0) {
184 struct zfs_devdesc currdev;
185
Toomas Soome76b35942018-03-13 11:58:56 +0200186 currdev.dd.d_dev = &zfs_dev;
187 currdev.dd.d_unit = 0;
Toomas Soomedbacaf52016-11-17 17:02:22 +0200188 currdev.pool_guid = pool_guid;
189 currdev.root_guid = 0;
190 devname = efi_fmtdev(&currdev);
191
192 env_setenv("currdev", EV_VOLATILE, devname, efi_setcurrdev,
193 env_nounset);
194 env_setenv("loaddev", EV_VOLATILE, devname, env_noset,
195 env_nounset);
196 return (0);
197 }
198
199 /* We have device lists for hd, cd, fd, walk them all. */
200 pdi_list = efiblk_get_pdinfo_list(&efipart_hddev);
201 STAILQ_FOREACH(dp, pdi_list, pd_link) {
202 struct disk_devdesc currdev;
203
Toomas Soome76b35942018-03-13 11:58:56 +0200204 currdev.dd.d_dev = &efipart_hddev;
Toomas Soome76b35942018-03-13 11:58:56 +0200205 currdev.dd.d_unit = dp->pd_unit;
Toomas Soomedbacaf52016-11-17 17:02:22 +0200206 currdev.d_slice = -1;
207 currdev.d_partition = -1;
208
209 if (dp->pd_handle == img->DeviceHandle) {
210 devname = efi_fmtdev(&currdev);
211
212 env_setenv("currdev", EV_VOLATILE, devname,
213 efi_setcurrdev, env_nounset);
214 env_setenv("loaddev", EV_VOLATILE, devname,
215 env_noset, env_nounset);
216 return (0);
217 }
218 /* Assuming GPT partitioning. */
219 STAILQ_FOREACH(pp, &dp->pd_part, pd_link) {
220 if (pp->pd_handle == img->DeviceHandle) {
221 currdev.d_slice = pp->pd_unit;
222 currdev.d_partition = 255;
223 devname = efi_fmtdev(&currdev);
224
225 env_setenv("currdev", EV_VOLATILE, devname,
226 efi_setcurrdev, env_nounset);
227 env_setenv("loaddev", EV_VOLATILE, devname,
228 env_noset, env_nounset);
229 return (0);
230 }
231 }
232 }
233
234 pdi_list = efiblk_get_pdinfo_list(&efipart_cddev);
235 STAILQ_FOREACH(dp, pdi_list, pd_link) {
236 if (dp->pd_handle == img->DeviceHandle ||
237 dp->pd_alias == img->DeviceHandle) {
238 set_devdesc_currdev(&efipart_cddev, dp->pd_unit);
239 return (0);
240 }
241 }
242
243 pdi_list = efiblk_get_pdinfo_list(&efipart_fddev);
244 STAILQ_FOREACH(dp, pdi_list, pd_link) {
245 if (dp->pd_handle == img->DeviceHandle) {
246 set_devdesc_currdev(&efipart_fddev, dp->pd_unit);
247 return (0);
248 }
249 }
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200250
251 /*
252 * Try the device handle from our loaded image first. If that
253 * fails, use the device path from the loaded image and see if
254 * any of the nodes in that path match one of the enumerated
255 * handles.
256 */
Toomas Soomedbacaf52016-11-17 17:02:22 +0200257 if (efi_handle_lookup(img->DeviceHandle, &dev, &unit, &extra) == 0) {
258 set_devdesc_currdev(dev, unit);
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200259 return (0);
Toomas Soomedbacaf52016-11-17 17:02:22 +0200260 }
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200261
262 copy = NULL;
263 devpath = efi_lookup_image_devpath(IH);
264 while (devpath != NULL) {
265 h = efi_devpath_handle(devpath);
266 if (h == NULL)
267 break;
268
269 free(copy);
270 copy = NULL;
271
Toomas Soomedbacaf52016-11-17 17:02:22 +0200272 if (efi_handle_lookup(h, &dev, &unit, &extra) == 0) {
273 set_devdesc_currdev(dev, unit);
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200274 return (0);
Toomas Soomedbacaf52016-11-17 17:02:22 +0200275 }
Toomas Soomeab8c1d42016-11-12 13:46:26 +0200276
277 devpath = efi_lookup_devpath(h);
278 if (devpath != NULL) {
279 copy = efi_devpath_trim(devpath);
280 devpath = copy;
281 }
282 }
283 free(copy);
284
285 return (ENOENT);
286}
287
Toomas Soome199767f2015-10-25 00:06:51 +0300288EFI_STATUS
289main(int argc, CHAR16 *argv[])
290{
291 char var[128];
Toomas Soome199767f2015-10-25 00:06:51 +0300292 EFI_GUID *guid;
Toomas Soome8fe0f542018-06-14 10:00:19 +0300293 int i, j, howto;
294 bool vargood;
Toomas Soomedcba96f2016-10-09 17:12:23 +0300295 void *ptr;
Toomas Soome199767f2015-10-25 00:06:51 +0300296 UINTN k;
297 int has_kbd;
298
299 archsw.arch_autoload = efi_autoload;
300 archsw.arch_getdev = efi_getdev;
301 archsw.arch_copyin = efi_copyin;
302 archsw.arch_copyout = efi_copyout;
303 archsw.arch_readin = efi_readin;
Toomas Soomef9feecc2016-10-09 17:30:28 +0300304 archsw.arch_loadaddr = efi_loadaddr;
305 archsw.arch_free_loadaddr = efi_free_loadaddr;
Toomas Soome199767f2015-10-25 00:06:51 +0300306 /* Note this needs to be set before ZFS init. */
307 archsw.arch_zfs_probe = efi_zfs_probe;
Toomas Soome9f23ea42017-12-12 15:23:09 +0200308
309 /* Get our loaded image protocol interface structure. */
310 BS->HandleProtocol(IH, &imgid, (VOID**)&img);
Toomas Soome199767f2015-10-25 00:06:51 +0300311
Toomas Soome04f8e092016-11-10 21:05:15 +0200312 /* Init the time source */
313 efi_time_init();
314
Toomas Soome199767f2015-10-25 00:06:51 +0300315 has_kbd = has_keyboard();
316
317 /*
318 * XXX Chicken-and-egg problem; we want to have console output
319 * early, but some console attributes may depend on reading from
320 * eg. the boot device, which we can't do yet. We can use
321 * printf() etc. once this is done.
322 */
323 cons_probe();
Toomas Soomef9feecc2016-10-09 17:30:28 +0300324 efi_getsmap();
Toomas Soome199767f2015-10-25 00:06:51 +0300325
326 /*
327 * Initialise the block cache. Set the upper limit.
328 */
329 bcache_init(32768, 512);
330
331 /*
332 * Parse the args to set the console settings, etc
333 * boot1.efi passes these in, if it can read /boot.config or /boot/config
334 * or iPXE may be setup to pass these in.
335 *
336 * Loop through the args, and for each one that contains an '=' that is
337 * not the first character, add it to the environment. This allows
338 * loader and kernel env vars to be passed on the command line. Convert
339 * args from UCS-2 to ASCII (16 to 8 bit) as they are copied.
340 */
341 howto = 0;
342 for (i = 1; i < argc; i++) {
343 if (argv[i][0] == '-') {
344 for (j = 1; argv[i][j] != 0; j++) {
345 int ch;
346
347 ch = argv[i][j];
348 switch (ch) {
349 case 'a':
350 howto |= RB_ASKNAME;
351 break;
352 case 'd':
353 howto |= RB_KDB;
354 break;
355 case 'D':
356 howto |= RB_MULTIPLE;
357 break;
358 case 'h':
359 howto |= RB_SERIAL;
360 break;
361 case 'm':
362 howto |= RB_MUTE;
363 break;
364 case 'p':
365 howto |= RB_PAUSE;
366 break;
367 case 'P':
368 if (!has_kbd)
369 howto |= RB_SERIAL | RB_MULTIPLE;
370 break;
371 case 'r':
372 howto |= RB_DFLTROOT;
373 break;
374 case 's':
375 howto |= RB_SINGLE;
376 break;
377 case 'S':
378 if (argv[i][j + 1] == 0) {
379 if (i + 1 == argc) {
Toomas Soome2aca6c62017-06-02 23:07:12 +0300380 strncpy(var, "115200",
Toomas Soome199767f2015-10-25 00:06:51 +0300381 sizeof(var));
Toomas Soome2aca6c62017-06-02 23:07:12 +0300382 } else {
383 CHAR16 *ptr;
384 ptr = &argv[i + 1][0];
385 cpy16to8(ptr, var,
386 sizeof(var));
Toomas Soome199767f2015-10-25 00:06:51 +0300387 }
388 i++;
Toomas Soome199767f2015-10-25 00:06:51 +0300389 } else {
Toomas Soomeeee59042016-11-12 00:06:58 +0200390 cpy16to8(&argv[i][j + 1], var,
Toomas Soome199767f2015-10-25 00:06:51 +0300391 sizeof(var));
Toomas Soome199767f2015-10-25 00:06:51 +0300392 }
Toomas Soome2aca6c62017-06-02 23:07:12 +0300393 strncat(var, ",8,n,1,-", sizeof(var));
394 setenv("ttya-mode", var, 1);
395 break;
Toomas Soome199767f2015-10-25 00:06:51 +0300396 case 'v':
397 howto |= RB_VERBOSE;
398 break;
399 }
400 }
401 } else {
Toomas Soome8fe0f542018-06-14 10:00:19 +0300402 vargood = false;
Toomas Soome199767f2015-10-25 00:06:51 +0300403 for (j = 0; argv[i][j] != 0; j++) {
404 if (j == sizeof(var)) {
Toomas Soome8fe0f542018-06-14 10:00:19 +0300405 vargood = false;
Toomas Soome199767f2015-10-25 00:06:51 +0300406 break;
407 }
408 if (j > 0 && argv[i][j] == '=')
Toomas Soome8fe0f542018-06-14 10:00:19 +0300409 vargood = true;
Toomas Soome199767f2015-10-25 00:06:51 +0300410 var[j] = (char)argv[i][j];
411 }
412 if (vargood) {
413 var[j] = 0;
414 putenv(var);
415 }
416 }
417 }
418 for (i = 0; howto_names[i].ev != NULL; i++)
419 if (howto & howto_names[i].mask)
420 setenv(howto_names[i].ev, "YES", 1);
421 if (howto & RB_MULTIPLE) {
422 if (howto & RB_SERIAL)
423 setenv("console", "ttya text" , 1);
424 else
425 setenv("console", "text ttya" , 1);
426 } else if (howto & RB_SERIAL) {
427 setenv("console", "ttya" , 1);
428 }
429
Toomas Soome199767f2015-10-25 00:06:51 +0300430 /*
Toomas Soomec00b6c92018-02-19 19:14:22 +0200431 * Scan the BLOCK IO MEDIA handles then
432 * march through the device switch probing for things.
Toomas Soome199767f2015-10-25 00:06:51 +0300433 */
Toomas Soomec00b6c92018-02-19 19:14:22 +0200434 if ((i = efipart_inithandles()) == 0) {
435 for (i = 0; devsw[i] != NULL; i++)
436 if (devsw[i]->dv_init != NULL)
437 (devsw[i]->dv_init)();
438 } else
439 printf("efipart_inithandles failed %d, expect failures", i);
Toomas Soome199767f2015-10-25 00:06:51 +0300440
Toomas Soome199767f2015-10-25 00:06:51 +0300441 printf("Command line arguments:");
442 for (i = 0; i < argc; i++) {
Toomas Soomeeee59042016-11-12 00:06:58 +0200443 printf(" %S", argv[i]);
Toomas Soome199767f2015-10-25 00:06:51 +0300444 }
445 printf("\n");
446
447 printf("Image base: 0x%lx\n", (u_long)img->ImageBase);
448 printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16,
449 ST->Hdr.Revision & 0xffff);
Toomas Soomeeee59042016-11-12 00:06:58 +0200450 printf("EFI Firmware: %S (rev %d.%02d)\n", ST->FirmwareVendor,
451 ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
Toomas Soome199767f2015-10-25 00:06:51 +0300452
Toomas Soomef6c94442017-01-03 11:24:09 +0200453 printf("\n%s", bootprog_info);
Toomas Soome199767f2015-10-25 00:06:51 +0300454
455 /*
456 * Disable the watchdog timer. By default the boot manager sets
457 * the timer to 5 minutes before invoking a boot option. If we
458 * want to return to the boot manager, we have to disable the
459 * watchdog timer and since we're an interactive program, we don't
460 * want to wait until the user types "quit". The timer may have
461 * fired by then. We don't care if this fails. It does not prevent
462 * normal functioning in any way...
463 */
464 BS->SetWatchdogTimer(0, 0, 0, NULL);
465
Toomas Soomedbacaf52016-11-17 17:02:22 +0200466 if (find_currdev(img) != 0)
Toomas Soome199767f2015-10-25 00:06:51 +0300467 return (EFI_NOT_FOUND);
468
Toomas Soomeeee59042016-11-12 00:06:58 +0200469 efi_init_environment();
Toomas Soome199767f2015-10-25 00:06:51 +0300470 setenv("ISADIR", "amd64", 1); /* we only build 64bit */
Toomas Soomedcba96f2016-10-09 17:12:23 +0300471 acpi_detect();
Toomas Soome199767f2015-10-25 00:06:51 +0300472
Toomas Soomedcba96f2016-10-09 17:12:23 +0300473 if ((ptr = efi_get_table(&smbios3)) == NULL)
474 ptr = efi_get_table(&smbios);
475 smbios_detect(ptr);
Toomas Soome199767f2015-10-25 00:06:51 +0300476
Toomas Soome199767f2015-10-25 00:06:51 +0300477 interact(NULL); /* doesn't return */
478
479 return (EFI_SUCCESS); /* keep compiler happy */
480}
481
482COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
483
484static int
Toomas Soome30c75cb2016-03-13 20:45:57 +0200485command_reboot(int argc __unused, char *argv[] __unused)
Toomas Soome199767f2015-10-25 00:06:51 +0300486{
487 int i;
Toomas Soome199767f2015-10-25 00:06:51 +0300488
489 for (i = 0; devsw[i] != NULL; ++i)
490 if (devsw[i]->dv_cleanup != NULL)
491 (devsw[i]->dv_cleanup)();
492
Toomas Soome48d84432017-04-27 19:19:29 +0300493 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
Toomas Soome199767f2015-10-25 00:06:51 +0300494
495 /* NOTREACHED */
496 return (CMD_ERROR);
497}
498
499COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
500
501static int
Toomas Soome30c75cb2016-03-13 20:45:57 +0200502command_memmap(int argc __unused, char *argv[] __unused)
Toomas Soome199767f2015-10-25 00:06:51 +0300503{
504 UINTN sz;
505 EFI_MEMORY_DESCRIPTOR *map, *p;
506 UINTN key, dsz;
507 UINT32 dver;
508 EFI_STATUS status;
509 int i, ndesc;
510 int rv = 0;
511 char line[80];
Toomas Soome199767f2015-10-25 00:06:51 +0300512
513 sz = 0;
514 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
515 if (status != EFI_BUFFER_TOO_SMALL) {
516 printf("Can't determine memory map size\n");
517 return (CMD_ERROR);
518 }
519 map = malloc(sz);
520 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
521 if (EFI_ERROR(status)) {
522 printf("Can't read memory map\n");
523 return (CMD_ERROR);
524 }
525
526 ndesc = sz / dsz;
527 snprintf(line, 80, "%23s %12s %12s %8s %4s\n",
528 "Type", "Physical", "Virtual", "#Pages", "Attr");
529 pager_open();
530 rv = pager_output(line);
531 if (rv) {
532 pager_close();
533 return (CMD_OK);
534 }
535
536 for (i = 0, p = map; i < ndesc;
537 i++, p = NextMemoryDescriptor(p, dsz)) {
538 snprintf(line, 80, "%23s %012lx %012lx %08lx ",
Toomas Soomeeee59042016-11-12 00:06:58 +0200539 efi_memory_type(p->Type),
Toomas Soome199767f2015-10-25 00:06:51 +0300540 p->PhysicalStart,
541 p->VirtualStart,
542 p->NumberOfPages);
543 rv = pager_output(line);
544 if (rv)
545 break;
546
547 if (p->Attribute & EFI_MEMORY_UC)
548 printf("UC ");
549 if (p->Attribute & EFI_MEMORY_WC)
550 printf("WC ");
551 if (p->Attribute & EFI_MEMORY_WT)
552 printf("WT ");
553 if (p->Attribute & EFI_MEMORY_WB)
554 printf("WB ");
555 if (p->Attribute & EFI_MEMORY_UCE)
556 printf("UCE ");
557 if (p->Attribute & EFI_MEMORY_WP)
558 printf("WP ");
559 if (p->Attribute & EFI_MEMORY_RP)
560 printf("RP ");
561 if (p->Attribute & EFI_MEMORY_XP)
562 printf("XP ");
Toomas Soomeeee59042016-11-12 00:06:58 +0200563 if (p->Attribute & EFI_MEMORY_NV)
564 printf("NV ");
565 if (p->Attribute & EFI_MEMORY_MORE_RELIABLE)
566 printf("MR ");
567 if (p->Attribute & EFI_MEMORY_RO)
568 printf("RO ");
Toomas Soome199767f2015-10-25 00:06:51 +0300569 rv = pager_output("\n");
570 if (rv)
571 break;
572 }
573
574 pager_close();
575 return (CMD_OK);
576}
577
578COMMAND_SET(configuration, "configuration", "print configuration tables",
579 command_configuration);
580
Toomas Soome199767f2015-10-25 00:06:51 +0300581static int
Toomas Soome30c75cb2016-03-13 20:45:57 +0200582command_configuration(int argc __unused, char *argv[] __unused)
Toomas Soome199767f2015-10-25 00:06:51 +0300583{
584 UINTN i;
Toomas Soomeeee59042016-11-12 00:06:58 +0200585 char *name;
Toomas Soome199767f2015-10-25 00:06:51 +0300586
587 printf("NumberOfTableEntries=%lu\n",
588 (unsigned long)ST->NumberOfTableEntries);
589 for (i = 0; i < ST->NumberOfTableEntries; i++) {
590 EFI_GUID *guid;
591
592 printf(" ");
593 guid = &ST->ConfigurationTable[i].VendorGuid;
Toomas Soomeeee59042016-11-12 00:06:58 +0200594
595 if (efi_guid_to_name(guid, &name) == true) {
596 printf(name);
597 free(name);
598 } else {
599 printf("Error while translating UUID to name");
600 }
Toomas Soome199767f2015-10-25 00:06:51 +0300601 printf(" at %p\n", ST->ConfigurationTable[i].VendorTable);
602 }
603
604 return (CMD_OK);
605}
606
607
608COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode);
609
610static int
611command_mode(int argc, char *argv[])
612{
613 UINTN cols, rows;
614 unsigned int mode;
615 int i;
616 char *cp;
617 char rowenv[8];
618 EFI_STATUS status;
619 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
620 extern void HO(void);
621
622 conout = ST->ConOut;
623
624 if (argc > 1) {
625 mode = strtol(argv[1], &cp, 0);
626 if (cp[0] != '\0') {
627 printf("Invalid mode\n");
628 return (CMD_ERROR);
629 }
630 status = conout->QueryMode(conout, mode, &cols, &rows);
631 if (EFI_ERROR(status)) {
632 printf("invalid mode %d\n", mode);
633 return (CMD_ERROR);
634 }
635 status = conout->SetMode(conout, mode);
636 if (EFI_ERROR(status)) {
637 printf("couldn't set mode %d\n", mode);
638 return (CMD_ERROR);
639 }
640 sprintf(rowenv, "%u", (unsigned)rows);
641 setenv("LINES", rowenv, 1);
642 sprintf(rowenv, "%u", (unsigned)cols);
643 setenv("COLUMNS", rowenv, 1);
644 HO(); /* set cursor */
645 return (CMD_OK);
646 }
647
648 printf("Current mode: %d\n", conout->Mode->Mode);
649 for (i = 0; i <= conout->Mode->MaxMode; i++) {
650 status = conout->QueryMode(conout, i, &cols, &rows);
651 if (EFI_ERROR(status))
652 continue;
653 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols,
654 (unsigned)rows);
655 }
656
657 if (i != 0)
658 printf("Select a mode with the command \"mode <number>\"\n");
659
660 return (CMD_OK);
661}
662
Toomas Soome199767f2015-10-25 00:06:51 +0300663COMMAND_SET(lsefi, "lsefi", "list EFI handles", command_lsefi);
664
665static int
Toomas Soome30c75cb2016-03-13 20:45:57 +0200666command_lsefi(int argc __unused, char *argv[] __unused)
Toomas Soome199767f2015-10-25 00:06:51 +0300667{
Toomas Soomeeee59042016-11-12 00:06:58 +0200668 char *name;
Toomas Soome199767f2015-10-25 00:06:51 +0300669 EFI_HANDLE *buffer = NULL;
670 EFI_HANDLE handle;
671 UINTN bufsz = 0, i, j;
672 EFI_STATUS status;
Toomas Soomeeee59042016-11-12 00:06:58 +0200673 int ret;
Toomas Soome199767f2015-10-25 00:06:51 +0300674
675 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
676 if (status != EFI_BUFFER_TOO_SMALL) {
677 snprintf(command_errbuf, sizeof (command_errbuf),
678 "unexpected error: %lld", (long long)status);
679 return (CMD_ERROR);
680 }
681 if ((buffer = malloc(bufsz)) == NULL) {
682 sprintf(command_errbuf, "out of memory");
683 return (CMD_ERROR);
684 }
685
686 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
687 if (EFI_ERROR(status)) {
688 free(buffer);
689 snprintf(command_errbuf, sizeof (command_errbuf),
690 "LocateHandle() error: %lld", (long long)status);
691 return (CMD_ERROR);
692 }
693
694 pager_open();
695 for (i = 0; i < (bufsz / sizeof (EFI_HANDLE)); i++) {
696 UINTN nproto = 0;
697 EFI_GUID **protocols = NULL;
698
699 handle = buffer[i];
700 printf("Handle %p", handle);
701 if (pager_output("\n"))
702 break;
703 /* device path */
704
705 status = BS->ProtocolsPerHandle(handle, &protocols, &nproto);
706 if (EFI_ERROR(status)) {
707 snprintf(command_errbuf, sizeof (command_errbuf),
708 "ProtocolsPerHandle() error: %lld",
709 (long long)status);
710 continue;
711 }
Toomas Soomeeee59042016-11-12 00:06:58 +0200712
Toomas Soome199767f2015-10-25 00:06:51 +0300713 for (j = 0; j < nproto; j++) {
Toomas Soomeeee59042016-11-12 00:06:58 +0200714 if (efi_guid_to_name(protocols[j], &name) == true) {
715 printf(" %s", name);
716 free(name);
717 } else {
718 printf("Error while translating UUID to name");
719 }
720 if ((ret = pager_output("\n")) != 0)
Toomas Soome199767f2015-10-25 00:06:51 +0300721 break;
722 }
723 BS->FreePool(protocols);
Toomas Soomeeee59042016-11-12 00:06:58 +0200724 if (ret != 0)
Toomas Soome199767f2015-10-25 00:06:51 +0300725 break;
726 }
727 pager_close();
728 free(buffer);
729 return (CMD_OK);
730}
731
Toomas Soome199767f2015-10-25 00:06:51 +0300732COMMAND_SET(lszfs, "lszfs", "list child datasets of a zfs dataset",
733 command_lszfs);
734
735static int
736command_lszfs(int argc, char *argv[])
737{
738 int err;
739
740 if (argc != 2) {
741 command_errmsg = "wrong number of arguments";
742 return (CMD_ERROR);
743 }
744
745 err = zfs_list(argv[1]);
746 if (err != 0) {
747 command_errmsg = strerror(err);
748 return (CMD_ERROR);
749 }
750 return (CMD_OK);
751}
752
753#ifdef __FreeBSD__
754COMMAND_SET(reloadbe, "reloadbe", "refresh the list of ZFS Boot Environments",
755 command_reloadbe);
756
757static int
758command_reloadbe(int argc, char *argv[])
759{
760 int err;
761 char *root;
762
763 if (argc > 2) {
764 command_errmsg = "wrong number of arguments";
765 return (CMD_ERROR);
766 }
767
768 if (argc == 2) {
769 err = zfs_bootenv(argv[1]);
770 } else {
771 root = getenv("zfs_be_root");
772 if (root == NULL) {
773 return (CMD_OK);
774 }
775 err = zfs_bootenv(root);
776 }
777
778 if (err != 0) {
779 command_errmsg = strerror(err);
780 return (CMD_ERROR);
781 }
782
783 return (CMD_OK);
784}
785#endif /* __FreeBSD__ */
Toomas Soome199767f2015-10-25 00:06:51 +0300786
Toomas Soome199767f2015-10-25 00:06:51 +0300787#ifdef LOADER_FDT_SUPPORT
788extern int command_fdt_internal(int argc, char *argv[]);
789
790/*
791 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
792 * and declaring it as extern is in contradiction with COMMAND_SET() macro
793 * (which uses static pointer), we're defining wrapper function, which
794 * calls the proper fdt handling routine.
795 */
796static int
797command_fdt(int argc, char *argv[])
798{
799 return (command_fdt_internal(argc, argv));
800}
801
802COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);
803#endif
804
Toomas Soomef9feecc2016-10-09 17:30:28 +0300805/*
806 * Chain load another efi loader.
807 */
808static int
809command_chain(int argc, char *argv[])
810{
811 EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL;
812 EFI_HANDLE loaderhandle;
813 EFI_LOADED_IMAGE *loaded_image;
814 EFI_STATUS status;
815 struct stat st;
816 struct devdesc *dev;
817 char *name, *path;
818 void *buf;
819 int fd;
820
821 if (argc < 2) {
822 command_errmsg = "wrong number of arguments";
823 return (CMD_ERROR);
824 }
825
826 name = argv[1];
827
828 if ((fd = open(name, O_RDONLY)) < 0) {
829 command_errmsg = "no such file";
830 return (CMD_ERROR);
831 }
832
833 if (fstat(fd, &st) < -1) {
834 command_errmsg = "stat failed";
835 close(fd);
836 return (CMD_ERROR);
837 }
838
839 status = BS->AllocatePool(EfiLoaderCode, (UINTN)st.st_size, &buf);
840 if (status != EFI_SUCCESS) {
841 command_errmsg = "failed to allocate buffer";
842 close(fd);
843 return (CMD_ERROR);
844 }
845 if (read(fd, buf, st.st_size) != st.st_size) {
846 command_errmsg = "error while reading the file";
847 (void)BS->FreePool(buf);
848 close(fd);
849 return (CMD_ERROR);
850 }
851 close(fd);
852 status = BS->LoadImage(FALSE, IH, NULL, buf, st.st_size, &loaderhandle);
853 (void)BS->FreePool(buf);
854 if (status != EFI_SUCCESS) {
855 command_errmsg = "LoadImage failed";
856 return (CMD_ERROR);
857 }
858 status = BS->HandleProtocol(loaderhandle, &LoadedImageGUID,
859 (void **)&loaded_image);
860
861 if (argc > 2) {
862 int i, len = 0;
863 CHAR16 *argp;
864
865 for (i = 2; i < argc; i++)
866 len += strlen(argv[i]) + 1;
867
868 len *= sizeof (*argp);
869 loaded_image->LoadOptions = argp = malloc (len);
870 if (loaded_image->LoadOptions == NULL) {
871 (void) BS->UnloadImage(loaded_image);
872 return (CMD_ERROR);
873 }
874 loaded_image->LoadOptionsSize = len;
875 for (i = 2; i < argc; i++) {
876 char *ptr = argv[i];
877 while (*ptr)
878 *(argp++) = *(ptr++);
879 *(argp++) = ' ';
880 }
881 *(--argv) = 0;
882 }
883
Toomas Soomeeea30b22017-12-12 18:01:57 +0200884 if (efi_getdev((void **)&dev, name, (const char **)&path) == 0) {
885 struct zfs_devdesc *z_dev;
886 struct disk_devdesc *d_dev;
887 pdinfo_t *hd, *pd;
888
Toomas Soomec142ce12018-03-13 14:16:40 +0200889 switch (dev->d_dev->dv_type) {
Toomas Soomeeea30b22017-12-12 18:01:57 +0200890 case DEVT_ZFS:
891 z_dev = (struct zfs_devdesc *)dev;
892 loaded_image->DeviceHandle =
893 efizfs_get_handle_by_guid(z_dev->pool_guid);
894 break;
895 case DEVT_NET:
896 loaded_image->DeviceHandle =
897 efi_find_handle(dev->d_dev, dev->d_unit);
898 break;
899 default:
900 hd = efiblk_get_pdinfo(dev);
901 if (STAILQ_EMPTY(&hd->pd_part)) {
902 loaded_image->DeviceHandle = hd->pd_handle;
903 break;
904 }
905 d_dev = (struct disk_devdesc *)dev;
906 STAILQ_FOREACH(pd, &hd->pd_part, pd_link) {
907 /*
908 * d_partition should be 255
909 */
910 if (pd->pd_unit == d_dev->d_slice) {
911 loaded_image->DeviceHandle =
912 pd->pd_handle;
913 break;
914 }
915 }
916 break;
917 }
918 }
Toomas Soomef9feecc2016-10-09 17:30:28 +0300919
920 dev_cleanup();
921 status = BS->StartImage(loaderhandle, NULL, NULL);
922 if (status != EFI_SUCCESS) {
923 command_errmsg = "StartImage failed";
924 free(loaded_image->LoadOptions);
925 loaded_image->LoadOptions = NULL;
926 status = BS->UnloadImage(loaded_image);
927 return (CMD_ERROR);
928 }
929
930 return (CMD_ERROR); /* not reached */
931}
932
933COMMAND_SET(chain, "chain", "chain load file", command_chain);