blob: 337192c373dbafa7e3e99ef0bd24d438a193365b [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 Soomeafa95be2018-06-16 10:22:42 +030073static bool
Toomas Soome199767f2015-10-25 00:06:51 +030074has_keyboard(void)
75{
76 EFI_STATUS status;
77 EFI_DEVICE_PATH *path;
78 EFI_HANDLE *hin, *hin_end, *walker;
79 UINTN sz;
Toomas Soomeafa95be2018-06-16 10:22:42 +030080 bool retval = false;
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))
Toomas Soomeafa95be2018-06-16 10:22:42 +030097 return (retval);
Toomas Soome199767f2015-10-25 00:06:51 +030098
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) {
Toomas Soomeafa95be2018-06-16 10:22:42 +0300127 retval = true;
Toomas Soome199767f2015-10-25 00:06:51 +0300128 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 */
Toomas Soomeafa95be2018-06-16 10:22:42 +0300143 retval = true;
Toomas Soome199767f2015-10-25 00:06:51 +0300144 goto out;
145 }
146 }
147 path = NextDevicePathNode(path);
148 }
149 }
150out:
151 free(hin);
Toomas Soomeafa95be2018-06-16 10:22:42 +0300152 return (retval);
Toomas Soome199767f2015-10-25 00:06:51 +0300153}
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;
Toomas Soomeafa95be2018-06-16 10:22:42 +0300297 bool has_kbd;
Toomas Soome199767f2015-10-25 00:06:51 +0300298
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
Toomas Soomeb31ca802018-10-10 15:29:10 +0300499COMMAND_SET(poweroff, "poweroff", "power off the system", command_poweroff);
500
501static int
502command_poweroff(int argc __unused, char *argv[] __unused)
503{
504 int i;
505
506 for (i = 0; devsw[i] != NULL; ++i)
507 if (devsw[i]->dv_cleanup != NULL)
508 (devsw[i]->dv_cleanup)();
509
510 RS->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
511
512 /* NOTREACHED */
513 return (CMD_ERROR);
514}
515
Toomas Soome199767f2015-10-25 00:06:51 +0300516COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
517
518static int
Toomas Soome30c75cb2016-03-13 20:45:57 +0200519command_memmap(int argc __unused, char *argv[] __unused)
Toomas Soome199767f2015-10-25 00:06:51 +0300520{
521 UINTN sz;
522 EFI_MEMORY_DESCRIPTOR *map, *p;
523 UINTN key, dsz;
524 UINT32 dver;
525 EFI_STATUS status;
526 int i, ndesc;
527 int rv = 0;
528 char line[80];
Toomas Soome199767f2015-10-25 00:06:51 +0300529
530 sz = 0;
531 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
532 if (status != EFI_BUFFER_TOO_SMALL) {
533 printf("Can't determine memory map size\n");
534 return (CMD_ERROR);
535 }
536 map = malloc(sz);
537 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
538 if (EFI_ERROR(status)) {
539 printf("Can't read memory map\n");
540 return (CMD_ERROR);
541 }
542
543 ndesc = sz / dsz;
544 snprintf(line, 80, "%23s %12s %12s %8s %4s\n",
545 "Type", "Physical", "Virtual", "#Pages", "Attr");
546 pager_open();
547 rv = pager_output(line);
548 if (rv) {
549 pager_close();
550 return (CMD_OK);
551 }
552
553 for (i = 0, p = map; i < ndesc;
554 i++, p = NextMemoryDescriptor(p, dsz)) {
Toomas Soome83b46712016-08-16 19:02:42 +0300555 snprintf(line, 80, "%23s %012jx %012jx %08jx ",
556 efi_memory_type(p->Type), p->PhysicalStart,
557 p->VirtualStart, p->NumberOfPages);
Toomas Soome199767f2015-10-25 00:06:51 +0300558 rv = pager_output(line);
559 if (rv)
560 break;
561
562 if (p->Attribute & EFI_MEMORY_UC)
563 printf("UC ");
564 if (p->Attribute & EFI_MEMORY_WC)
565 printf("WC ");
566 if (p->Attribute & EFI_MEMORY_WT)
567 printf("WT ");
568 if (p->Attribute & EFI_MEMORY_WB)
569 printf("WB ");
570 if (p->Attribute & EFI_MEMORY_UCE)
571 printf("UCE ");
572 if (p->Attribute & EFI_MEMORY_WP)
573 printf("WP ");
574 if (p->Attribute & EFI_MEMORY_RP)
575 printf("RP ");
576 if (p->Attribute & EFI_MEMORY_XP)
577 printf("XP ");
Toomas Soomeeee59042016-11-12 00:06:58 +0200578 if (p->Attribute & EFI_MEMORY_NV)
579 printf("NV ");
580 if (p->Attribute & EFI_MEMORY_MORE_RELIABLE)
581 printf("MR ");
582 if (p->Attribute & EFI_MEMORY_RO)
583 printf("RO ");
Toomas Soome199767f2015-10-25 00:06:51 +0300584 rv = pager_output("\n");
585 if (rv)
586 break;
587 }
588
589 pager_close();
590 return (CMD_OK);
591}
592
593COMMAND_SET(configuration, "configuration", "print configuration tables",
594 command_configuration);
595
Toomas Soome199767f2015-10-25 00:06:51 +0300596static int
Toomas Soome30c75cb2016-03-13 20:45:57 +0200597command_configuration(int argc __unused, char *argv[] __unused)
Toomas Soome199767f2015-10-25 00:06:51 +0300598{
599 UINTN i;
Toomas Soomeeee59042016-11-12 00:06:58 +0200600 char *name;
Toomas Soome199767f2015-10-25 00:06:51 +0300601
602 printf("NumberOfTableEntries=%lu\n",
603 (unsigned long)ST->NumberOfTableEntries);
604 for (i = 0; i < ST->NumberOfTableEntries; i++) {
605 EFI_GUID *guid;
606
607 printf(" ");
608 guid = &ST->ConfigurationTable[i].VendorGuid;
Toomas Soomeeee59042016-11-12 00:06:58 +0200609
610 if (efi_guid_to_name(guid, &name) == true) {
611 printf(name);
612 free(name);
613 } else {
614 printf("Error while translating UUID to name");
615 }
Toomas Soome199767f2015-10-25 00:06:51 +0300616 printf(" at %p\n", ST->ConfigurationTable[i].VendorTable);
617 }
618
619 return (CMD_OK);
620}
621
622
623COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode);
624
625static int
626command_mode(int argc, char *argv[])
627{
628 UINTN cols, rows;
629 unsigned int mode;
630 int i;
631 char *cp;
632 char rowenv[8];
633 EFI_STATUS status;
634 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
635 extern void HO(void);
636
637 conout = ST->ConOut;
638
639 if (argc > 1) {
640 mode = strtol(argv[1], &cp, 0);
641 if (cp[0] != '\0') {
642 printf("Invalid mode\n");
643 return (CMD_ERROR);
644 }
645 status = conout->QueryMode(conout, mode, &cols, &rows);
646 if (EFI_ERROR(status)) {
647 printf("invalid mode %d\n", mode);
648 return (CMD_ERROR);
649 }
650 status = conout->SetMode(conout, mode);
651 if (EFI_ERROR(status)) {
652 printf("couldn't set mode %d\n", mode);
653 return (CMD_ERROR);
654 }
655 sprintf(rowenv, "%u", (unsigned)rows);
656 setenv("LINES", rowenv, 1);
657 sprintf(rowenv, "%u", (unsigned)cols);
658 setenv("COLUMNS", rowenv, 1);
659 HO(); /* set cursor */
660 return (CMD_OK);
661 }
662
663 printf("Current mode: %d\n", conout->Mode->Mode);
664 for (i = 0; i <= conout->Mode->MaxMode; i++) {
665 status = conout->QueryMode(conout, i, &cols, &rows);
666 if (EFI_ERROR(status))
667 continue;
668 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols,
669 (unsigned)rows);
670 }
671
672 if (i != 0)
673 printf("Select a mode with the command \"mode <number>\"\n");
674
675 return (CMD_OK);
676}
677
Toomas Soome199767f2015-10-25 00:06:51 +0300678COMMAND_SET(lsefi, "lsefi", "list EFI handles", command_lsefi);
679
680static int
Toomas Soome30c75cb2016-03-13 20:45:57 +0200681command_lsefi(int argc __unused, char *argv[] __unused)
Toomas Soome199767f2015-10-25 00:06:51 +0300682{
Toomas Soomeeee59042016-11-12 00:06:58 +0200683 char *name;
Toomas Soome199767f2015-10-25 00:06:51 +0300684 EFI_HANDLE *buffer = NULL;
685 EFI_HANDLE handle;
686 UINTN bufsz = 0, i, j;
687 EFI_STATUS status;
Toomas Soomeeee59042016-11-12 00:06:58 +0200688 int ret;
Toomas Soome199767f2015-10-25 00:06:51 +0300689
690 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
691 if (status != EFI_BUFFER_TOO_SMALL) {
692 snprintf(command_errbuf, sizeof (command_errbuf),
693 "unexpected error: %lld", (long long)status);
694 return (CMD_ERROR);
695 }
696 if ((buffer = malloc(bufsz)) == NULL) {
697 sprintf(command_errbuf, "out of memory");
698 return (CMD_ERROR);
699 }
700
701 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
702 if (EFI_ERROR(status)) {
703 free(buffer);
704 snprintf(command_errbuf, sizeof (command_errbuf),
705 "LocateHandle() error: %lld", (long long)status);
706 return (CMD_ERROR);
707 }
708
709 pager_open();
710 for (i = 0; i < (bufsz / sizeof (EFI_HANDLE)); i++) {
711 UINTN nproto = 0;
712 EFI_GUID **protocols = NULL;
713
714 handle = buffer[i];
715 printf("Handle %p", handle);
716 if (pager_output("\n"))
717 break;
718 /* device path */
719
720 status = BS->ProtocolsPerHandle(handle, &protocols, &nproto);
721 if (EFI_ERROR(status)) {
722 snprintf(command_errbuf, sizeof (command_errbuf),
723 "ProtocolsPerHandle() error: %lld",
724 (long long)status);
725 continue;
726 }
Toomas Soomeeee59042016-11-12 00:06:58 +0200727
Toomas Soome199767f2015-10-25 00:06:51 +0300728 for (j = 0; j < nproto; j++) {
Toomas Soomeeee59042016-11-12 00:06:58 +0200729 if (efi_guid_to_name(protocols[j], &name) == true) {
730 printf(" %s", name);
731 free(name);
732 } else {
733 printf("Error while translating UUID to name");
734 }
735 if ((ret = pager_output("\n")) != 0)
Toomas Soome199767f2015-10-25 00:06:51 +0300736 break;
737 }
738 BS->FreePool(protocols);
Toomas Soomeeee59042016-11-12 00:06:58 +0200739 if (ret != 0)
Toomas Soome199767f2015-10-25 00:06:51 +0300740 break;
741 }
742 pager_close();
743 free(buffer);
744 return (CMD_OK);
745}
746
Toomas Soome199767f2015-10-25 00:06:51 +0300747COMMAND_SET(lszfs, "lszfs", "list child datasets of a zfs dataset",
748 command_lszfs);
749
750static int
751command_lszfs(int argc, char *argv[])
752{
753 int err;
754
755 if (argc != 2) {
756 command_errmsg = "wrong number of arguments";
757 return (CMD_ERROR);
758 }
759
760 err = zfs_list(argv[1]);
761 if (err != 0) {
762 command_errmsg = strerror(err);
763 return (CMD_ERROR);
764 }
765 return (CMD_OK);
766}
767
768#ifdef __FreeBSD__
769COMMAND_SET(reloadbe, "reloadbe", "refresh the list of ZFS Boot Environments",
770 command_reloadbe);
771
772static int
773command_reloadbe(int argc, char *argv[])
774{
775 int err;
776 char *root;
777
778 if (argc > 2) {
779 command_errmsg = "wrong number of arguments";
780 return (CMD_ERROR);
781 }
782
783 if (argc == 2) {
784 err = zfs_bootenv(argv[1]);
785 } else {
786 root = getenv("zfs_be_root");
787 if (root == NULL) {
788 return (CMD_OK);
789 }
790 err = zfs_bootenv(root);
791 }
792
793 if (err != 0) {
794 command_errmsg = strerror(err);
795 return (CMD_ERROR);
796 }
797
798 return (CMD_OK);
799}
800#endif /* __FreeBSD__ */
Toomas Soome199767f2015-10-25 00:06:51 +0300801
Toomas Soome199767f2015-10-25 00:06:51 +0300802#ifdef LOADER_FDT_SUPPORT
803extern int command_fdt_internal(int argc, char *argv[]);
804
805/*
806 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
807 * and declaring it as extern is in contradiction with COMMAND_SET() macro
808 * (which uses static pointer), we're defining wrapper function, which
809 * calls the proper fdt handling routine.
810 */
811static int
812command_fdt(int argc, char *argv[])
813{
814 return (command_fdt_internal(argc, argv));
815}
816
817COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);
818#endif
819
Toomas Soomef9feecc2016-10-09 17:30:28 +0300820/*
821 * Chain load another efi loader.
822 */
823static int
824command_chain(int argc, char *argv[])
825{
826 EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL;
827 EFI_HANDLE loaderhandle;
828 EFI_LOADED_IMAGE *loaded_image;
829 EFI_STATUS status;
830 struct stat st;
831 struct devdesc *dev;
832 char *name, *path;
833 void *buf;
834 int fd;
835
836 if (argc < 2) {
837 command_errmsg = "wrong number of arguments";
838 return (CMD_ERROR);
839 }
840
841 name = argv[1];
842
843 if ((fd = open(name, O_RDONLY)) < 0) {
844 command_errmsg = "no such file";
845 return (CMD_ERROR);
846 }
847
848 if (fstat(fd, &st) < -1) {
849 command_errmsg = "stat failed";
850 close(fd);
851 return (CMD_ERROR);
852 }
853
854 status = BS->AllocatePool(EfiLoaderCode, (UINTN)st.st_size, &buf);
855 if (status != EFI_SUCCESS) {
856 command_errmsg = "failed to allocate buffer";
857 close(fd);
858 return (CMD_ERROR);
859 }
860 if (read(fd, buf, st.st_size) != st.st_size) {
861 command_errmsg = "error while reading the file";
862 (void)BS->FreePool(buf);
863 close(fd);
864 return (CMD_ERROR);
865 }
866 close(fd);
867 status = BS->LoadImage(FALSE, IH, NULL, buf, st.st_size, &loaderhandle);
868 (void)BS->FreePool(buf);
869 if (status != EFI_SUCCESS) {
870 command_errmsg = "LoadImage failed";
871 return (CMD_ERROR);
872 }
873 status = BS->HandleProtocol(loaderhandle, &LoadedImageGUID,
874 (void **)&loaded_image);
875
876 if (argc > 2) {
877 int i, len = 0;
878 CHAR16 *argp;
879
880 for (i = 2; i < argc; i++)
881 len += strlen(argv[i]) + 1;
882
883 len *= sizeof (*argp);
884 loaded_image->LoadOptions = argp = malloc (len);
885 if (loaded_image->LoadOptions == NULL) {
886 (void) BS->UnloadImage(loaded_image);
887 return (CMD_ERROR);
888 }
889 loaded_image->LoadOptionsSize = len;
890 for (i = 2; i < argc; i++) {
891 char *ptr = argv[i];
892 while (*ptr)
893 *(argp++) = *(ptr++);
894 *(argp++) = ' ';
895 }
896 *(--argv) = 0;
897 }
898
Toomas Soomeeea30b22017-12-12 18:01:57 +0200899 if (efi_getdev((void **)&dev, name, (const char **)&path) == 0) {
900 struct zfs_devdesc *z_dev;
901 struct disk_devdesc *d_dev;
902 pdinfo_t *hd, *pd;
903
Toomas Soomec142ce12018-03-13 14:16:40 +0200904 switch (dev->d_dev->dv_type) {
Toomas Soomeeea30b22017-12-12 18:01:57 +0200905 case DEVT_ZFS:
906 z_dev = (struct zfs_devdesc *)dev;
907 loaded_image->DeviceHandle =
908 efizfs_get_handle_by_guid(z_dev->pool_guid);
909 break;
910 case DEVT_NET:
911 loaded_image->DeviceHandle =
912 efi_find_handle(dev->d_dev, dev->d_unit);
913 break;
914 default:
915 hd = efiblk_get_pdinfo(dev);
916 if (STAILQ_EMPTY(&hd->pd_part)) {
917 loaded_image->DeviceHandle = hd->pd_handle;
918 break;
919 }
920 d_dev = (struct disk_devdesc *)dev;
921 STAILQ_FOREACH(pd, &hd->pd_part, pd_link) {
922 /*
923 * d_partition should be 255
924 */
925 if (pd->pd_unit == d_dev->d_slice) {
926 loaded_image->DeviceHandle =
927 pd->pd_handle;
928 break;
929 }
930 }
931 break;
932 }
933 }
Toomas Soomef9feecc2016-10-09 17:30:28 +0300934
935 dev_cleanup();
936 status = BS->StartImage(loaderhandle, NULL, NULL);
937 if (status != EFI_SUCCESS) {
938 command_errmsg = "StartImage failed";
939 free(loaded_image->LoadOptions);
940 loaded_image->LoadOptions = NULL;
941 status = BS->UnloadImage(loaded_image);
942 return (CMD_ERROR);
943 }
944
945 return (CMD_ERROR); /* not reached */
946}
947
948COMMAND_SET(chain, "chain", "chain load file", command_chain);