Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 1 | /*- |
| 2 | * 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 Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 29 | |
Toomas Soome | edb3504 | 2016-11-07 15:37:12 +0200 | [diff] [blame] | 30 | #include <sys/disk.h> |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 31 | #include <sys/param.h> |
| 32 | #include <sys/reboot.h> |
| 33 | #include <sys/boot.h> |
| 34 | #include <stand.h> |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 35 | #include <inttypes.h> |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 36 | #include <string.h> |
| 37 | #include <setjmp.h> |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame^] | 38 | #include <disk.h> |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 39 | |
| 40 | #include <efi.h> |
| 41 | #include <efilib.h> |
| 42 | #include <efigpt.h> |
| 43 | |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 44 | #include <uuid.h> |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 45 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 46 | #include <bootstrap.h> |
| 47 | #include <smbios.h> |
| 48 | |
| 49 | #ifdef EFI_ZFS_BOOT |
| 50 | #include <libzfs.h> |
| 51 | #endif |
| 52 | |
| 53 | #include "loader_efi.h" |
| 54 | |
Toomas Soome | f6c9444 | 2017-01-03 11:24:09 +0200 | [diff] [blame] | 55 | extern char bootprog_info[]; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 56 | |
| 57 | struct arch_switch archsw; /* MI/MD interface boundary */ |
| 58 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 59 | EFI_GUID devid = DEVICE_PATH_PROTOCOL; |
| 60 | EFI_GUID imgid = LOADED_IMAGE_PROTOCOL; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 61 | EFI_GUID smbios = SMBIOS_TABLE_GUID; |
| 62 | EFI_GUID smbios3 = SMBIOS3_TABLE_GUID; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 63 | EFI_GUID inputid = SIMPLE_TEXT_INPUT_PROTOCOL; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 64 | |
Toomas Soome | dcba96f | 2016-10-09 17:12:23 +0300 | [diff] [blame] | 65 | extern void acpi_detect(void); |
Toomas Soome | f9feecc | 2016-10-09 17:30:28 +0300 | [diff] [blame] | 66 | extern void efi_getsmap(void); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 67 | #ifdef EFI_ZFS_BOOT |
| 68 | static void efi_zfs_probe(void); |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame^] | 69 | static uint64_t pool_guid; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 70 | #endif |
| 71 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 72 | static int |
| 73 | has_keyboard(void) |
| 74 | { |
| 75 | EFI_STATUS status; |
| 76 | EFI_DEVICE_PATH *path; |
| 77 | EFI_HANDLE *hin, *hin_end, *walker; |
| 78 | UINTN sz; |
| 79 | int retval = 0; |
| 80 | |
| 81 | /* |
| 82 | * Find all the handles that support the SIMPLE_TEXT_INPUT_PROTOCOL and |
| 83 | * do the typical dance to get the right sized buffer. |
| 84 | */ |
| 85 | sz = 0; |
| 86 | hin = NULL; |
| 87 | status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz, 0); |
| 88 | if (status == EFI_BUFFER_TOO_SMALL) { |
| 89 | hin = (EFI_HANDLE *)malloc(sz); |
| 90 | status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz, |
| 91 | hin); |
| 92 | if (EFI_ERROR(status)) |
| 93 | free(hin); |
| 94 | } |
| 95 | if (EFI_ERROR(status)) |
| 96 | return retval; |
| 97 | |
| 98 | /* |
| 99 | * Look at each of the handles. If it supports the device path protocol, |
| 100 | * use it to get the device path for this handle. Then see if that |
| 101 | * device path matches either the USB device path for keyboards or the |
| 102 | * legacy device path for keyboards. |
| 103 | */ |
| 104 | hin_end = &hin[sz / sizeof(*hin)]; |
| 105 | for (walker = hin; walker < hin_end; walker++) { |
| 106 | status = BS->HandleProtocol(*walker, &devid, (VOID **)&path); |
| 107 | if (EFI_ERROR(status)) |
| 108 | continue; |
| 109 | |
| 110 | while (!IsDevicePathEnd(path)) { |
| 111 | /* |
| 112 | * Check for the ACPI keyboard node. All PNP3xx nodes |
| 113 | * are keyboards of different flavors. Note: It is |
| 114 | * unclear of there's always a keyboard node when |
| 115 | * there's a keyboard controller, or if there's only one |
| 116 | * when a keyboard is detected at boot. |
| 117 | */ |
| 118 | if (DevicePathType(path) == ACPI_DEVICE_PATH && |
| 119 | (DevicePathSubType(path) == ACPI_DP || |
| 120 | DevicePathSubType(path) == ACPI_EXTENDED_DP)) { |
| 121 | ACPI_HID_DEVICE_PATH *acpi; |
| 122 | |
| 123 | acpi = (ACPI_HID_DEVICE_PATH *)(void *)path; |
| 124 | if ((EISA_ID_TO_NUM(acpi->HID) & 0xff00) == 0x300 && |
| 125 | (acpi->HID & 0xffff) == PNP_EISA_ID_CONST) { |
| 126 | retval = 1; |
| 127 | goto out; |
| 128 | } |
| 129 | /* |
| 130 | * Check for USB keyboard node, if present. Unlike a |
| 131 | * PS/2 keyboard, these definitely only appear when |
| 132 | * connected to the system. |
| 133 | */ |
| 134 | } else if (DevicePathType(path) == MESSAGING_DEVICE_PATH && |
| 135 | DevicePathSubType(path) == MSG_USB_CLASS_DP) { |
| 136 | USB_CLASS_DEVICE_PATH *usb; |
| 137 | |
| 138 | usb = (USB_CLASS_DEVICE_PATH *)(void *)path; |
| 139 | if (usb->DeviceClass == 3 && /* HID */ |
| 140 | usb->DeviceSubClass == 1 && /* Boot devices */ |
| 141 | usb->DeviceProtocol == 1) { /* Boot keyboards */ |
| 142 | retval = 1; |
| 143 | goto out; |
| 144 | } |
| 145 | } |
| 146 | path = NextDevicePathNode(path); |
| 147 | } |
| 148 | } |
| 149 | out: |
| 150 | free(hin); |
| 151 | return retval; |
| 152 | } |
| 153 | |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame^] | 154 | static void |
| 155 | set_devdesc_currdev(struct devsw *dev, int unit) |
Toomas Soome | ab8c1d4 | 2016-11-12 13:46:26 +0200 | [diff] [blame] | 156 | { |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame^] | 157 | struct devdesc currdev; |
| 158 | char *devname; |
| 159 | |
| 160 | currdev.d_dev = dev; |
| 161 | currdev.d_type = currdev.d_dev->dv_type; |
| 162 | currdev.d_unit = unit; |
| 163 | currdev.d_opendata = NULL; |
| 164 | devname = efi_fmtdev(&currdev); |
| 165 | |
| 166 | env_setenv("currdev", EV_VOLATILE, devname, efi_setcurrdev, |
| 167 | env_nounset); |
| 168 | env_setenv("loaddev", EV_VOLATILE, devname, env_noset, env_nounset); |
| 169 | } |
| 170 | |
| 171 | static int |
| 172 | find_currdev(EFI_LOADED_IMAGE *img) |
| 173 | { |
| 174 | pdinfo_list_t *pdi_list; |
| 175 | pdinfo_t *dp, *pp; |
Toomas Soome | ab8c1d4 | 2016-11-12 13:46:26 +0200 | [diff] [blame] | 176 | EFI_DEVICE_PATH *devpath, *copy; |
| 177 | EFI_HANDLE h; |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame^] | 178 | char *devname; |
| 179 | struct devsw *dev; |
| 180 | int unit; |
| 181 | uint64_t extra; |
| 182 | |
| 183 | /* Did efi_zfs_probe() detect the boot pool? */ |
| 184 | if (pool_guid != 0) { |
| 185 | struct zfs_devdesc currdev; |
| 186 | |
| 187 | currdev.d_dev = &zfs_dev; |
| 188 | currdev.d_unit = 0; |
| 189 | currdev.d_type = currdev.d_dev->dv_type; |
| 190 | currdev.d_opendata = NULL; |
| 191 | currdev.pool_guid = pool_guid; |
| 192 | currdev.root_guid = 0; |
| 193 | devname = efi_fmtdev(&currdev); |
| 194 | |
| 195 | env_setenv("currdev", EV_VOLATILE, devname, efi_setcurrdev, |
| 196 | env_nounset); |
| 197 | env_setenv("loaddev", EV_VOLATILE, devname, env_noset, |
| 198 | env_nounset); |
| 199 | return (0); |
| 200 | } |
| 201 | |
| 202 | /* We have device lists for hd, cd, fd, walk them all. */ |
| 203 | pdi_list = efiblk_get_pdinfo_list(&efipart_hddev); |
| 204 | STAILQ_FOREACH(dp, pdi_list, pd_link) { |
| 205 | struct disk_devdesc currdev; |
| 206 | |
| 207 | currdev.d_dev = &efipart_hddev; |
| 208 | currdev.d_type = currdev.d_dev->dv_type; |
| 209 | currdev.d_unit = dp->pd_unit; |
| 210 | currdev.d_opendata = NULL; |
| 211 | currdev.d_slice = -1; |
| 212 | currdev.d_partition = -1; |
| 213 | |
| 214 | if (dp->pd_handle == img->DeviceHandle) { |
| 215 | devname = efi_fmtdev(&currdev); |
| 216 | |
| 217 | env_setenv("currdev", EV_VOLATILE, devname, |
| 218 | efi_setcurrdev, env_nounset); |
| 219 | env_setenv("loaddev", EV_VOLATILE, devname, |
| 220 | env_noset, env_nounset); |
| 221 | return (0); |
| 222 | } |
| 223 | /* Assuming GPT partitioning. */ |
| 224 | STAILQ_FOREACH(pp, &dp->pd_part, pd_link) { |
| 225 | if (pp->pd_handle == img->DeviceHandle) { |
| 226 | currdev.d_slice = pp->pd_unit; |
| 227 | currdev.d_partition = 255; |
| 228 | devname = efi_fmtdev(&currdev); |
| 229 | |
| 230 | env_setenv("currdev", EV_VOLATILE, devname, |
| 231 | efi_setcurrdev, env_nounset); |
| 232 | env_setenv("loaddev", EV_VOLATILE, devname, |
| 233 | env_noset, env_nounset); |
| 234 | return (0); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | pdi_list = efiblk_get_pdinfo_list(&efipart_cddev); |
| 240 | STAILQ_FOREACH(dp, pdi_list, pd_link) { |
| 241 | if (dp->pd_handle == img->DeviceHandle || |
| 242 | dp->pd_alias == img->DeviceHandle) { |
| 243 | set_devdesc_currdev(&efipart_cddev, dp->pd_unit); |
| 244 | return (0); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | pdi_list = efiblk_get_pdinfo_list(&efipart_fddev); |
| 249 | STAILQ_FOREACH(dp, pdi_list, pd_link) { |
| 250 | if (dp->pd_handle == img->DeviceHandle) { |
| 251 | set_devdesc_currdev(&efipart_fddev, dp->pd_unit); |
| 252 | return (0); |
| 253 | } |
| 254 | } |
Toomas Soome | ab8c1d4 | 2016-11-12 13:46:26 +0200 | [diff] [blame] | 255 | |
| 256 | /* |
| 257 | * Try the device handle from our loaded image first. If that |
| 258 | * fails, use the device path from the loaded image and see if |
| 259 | * any of the nodes in that path match one of the enumerated |
| 260 | * handles. |
| 261 | */ |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame^] | 262 | if (efi_handle_lookup(img->DeviceHandle, &dev, &unit, &extra) == 0) { |
| 263 | set_devdesc_currdev(dev, unit); |
Toomas Soome | ab8c1d4 | 2016-11-12 13:46:26 +0200 | [diff] [blame] | 264 | return (0); |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame^] | 265 | } |
Toomas Soome | ab8c1d4 | 2016-11-12 13:46:26 +0200 | [diff] [blame] | 266 | |
| 267 | copy = NULL; |
| 268 | devpath = efi_lookup_image_devpath(IH); |
| 269 | while (devpath != NULL) { |
| 270 | h = efi_devpath_handle(devpath); |
| 271 | if (h == NULL) |
| 272 | break; |
| 273 | |
| 274 | free(copy); |
| 275 | copy = NULL; |
| 276 | |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame^] | 277 | if (efi_handle_lookup(h, &dev, &unit, &extra) == 0) { |
| 278 | set_devdesc_currdev(dev, unit); |
Toomas Soome | ab8c1d4 | 2016-11-12 13:46:26 +0200 | [diff] [blame] | 279 | return (0); |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame^] | 280 | } |
Toomas Soome | ab8c1d4 | 2016-11-12 13:46:26 +0200 | [diff] [blame] | 281 | |
| 282 | devpath = efi_lookup_devpath(h); |
| 283 | if (devpath != NULL) { |
| 284 | copy = efi_devpath_trim(devpath); |
| 285 | devpath = copy; |
| 286 | } |
| 287 | } |
| 288 | free(copy); |
| 289 | |
| 290 | return (ENOENT); |
| 291 | } |
| 292 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 293 | EFI_STATUS |
| 294 | main(int argc, CHAR16 *argv[]) |
| 295 | { |
| 296 | char var[128]; |
| 297 | EFI_LOADED_IMAGE *img; |
| 298 | EFI_GUID *guid; |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame^] | 299 | int i, j, vargood, howto; |
Toomas Soome | dcba96f | 2016-10-09 17:12:23 +0300 | [diff] [blame] | 300 | void *ptr; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 301 | UINTN k; |
| 302 | int has_kbd; |
| 303 | |
| 304 | archsw.arch_autoload = efi_autoload; |
| 305 | archsw.arch_getdev = efi_getdev; |
| 306 | archsw.arch_copyin = efi_copyin; |
| 307 | archsw.arch_copyout = efi_copyout; |
| 308 | archsw.arch_readin = efi_readin; |
Toomas Soome | f9feecc | 2016-10-09 17:30:28 +0300 | [diff] [blame] | 309 | archsw.arch_loadaddr = efi_loadaddr; |
| 310 | archsw.arch_free_loadaddr = efi_free_loadaddr; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 311 | #ifdef EFI_ZFS_BOOT |
| 312 | /* Note this needs to be set before ZFS init. */ |
| 313 | archsw.arch_zfs_probe = efi_zfs_probe; |
| 314 | #endif |
| 315 | |
Toomas Soome | 04f8e09 | 2016-11-10 21:05:15 +0200 | [diff] [blame] | 316 | /* Init the time source */ |
| 317 | efi_time_init(); |
| 318 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 319 | has_kbd = has_keyboard(); |
| 320 | |
| 321 | /* |
| 322 | * XXX Chicken-and-egg problem; we want to have console output |
| 323 | * early, but some console attributes may depend on reading from |
| 324 | * eg. the boot device, which we can't do yet. We can use |
| 325 | * printf() etc. once this is done. |
| 326 | */ |
| 327 | cons_probe(); |
Toomas Soome | f9feecc | 2016-10-09 17:30:28 +0300 | [diff] [blame] | 328 | efi_getsmap(); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 329 | |
| 330 | /* |
| 331 | * Initialise the block cache. Set the upper limit. |
| 332 | */ |
| 333 | bcache_init(32768, 512); |
| 334 | |
| 335 | /* |
| 336 | * Parse the args to set the console settings, etc |
| 337 | * boot1.efi passes these in, if it can read /boot.config or /boot/config |
| 338 | * or iPXE may be setup to pass these in. |
| 339 | * |
| 340 | * Loop through the args, and for each one that contains an '=' that is |
| 341 | * not the first character, add it to the environment. This allows |
| 342 | * loader and kernel env vars to be passed on the command line. Convert |
| 343 | * args from UCS-2 to ASCII (16 to 8 bit) as they are copied. |
| 344 | */ |
| 345 | howto = 0; |
| 346 | for (i = 1; i < argc; i++) { |
| 347 | if (argv[i][0] == '-') { |
| 348 | for (j = 1; argv[i][j] != 0; j++) { |
| 349 | int ch; |
| 350 | |
| 351 | ch = argv[i][j]; |
| 352 | switch (ch) { |
| 353 | case 'a': |
| 354 | howto |= RB_ASKNAME; |
| 355 | break; |
| 356 | case 'd': |
| 357 | howto |= RB_KDB; |
| 358 | break; |
| 359 | case 'D': |
| 360 | howto |= RB_MULTIPLE; |
| 361 | break; |
| 362 | case 'h': |
| 363 | howto |= RB_SERIAL; |
| 364 | break; |
| 365 | case 'm': |
| 366 | howto |= RB_MUTE; |
| 367 | break; |
| 368 | case 'p': |
| 369 | howto |= RB_PAUSE; |
| 370 | break; |
| 371 | case 'P': |
| 372 | if (!has_kbd) |
| 373 | howto |= RB_SERIAL | RB_MULTIPLE; |
| 374 | break; |
| 375 | case 'r': |
| 376 | howto |= RB_DFLTROOT; |
| 377 | break; |
| 378 | case 's': |
| 379 | howto |= RB_SINGLE; |
| 380 | break; |
| 381 | case 'S': |
| 382 | if (argv[i][j + 1] == 0) { |
| 383 | if (i + 1 == argc) { |
| 384 | setenv("comconsole_speed", "115200", 1); |
| 385 | } else { |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 386 | cpy16to8(&argv[i + 1][0], var, |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 387 | sizeof(var)); |
| 388 | setenv("comconsole_speedspeed", var, 1); |
| 389 | } |
| 390 | i++; |
| 391 | break; |
| 392 | } else { |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 393 | cpy16to8(&argv[i][j + 1], var, |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 394 | sizeof(var)); |
| 395 | setenv("comconsole_speed", var, 1); |
| 396 | break; |
| 397 | } |
| 398 | case 'v': |
| 399 | howto |= RB_VERBOSE; |
| 400 | break; |
| 401 | } |
| 402 | } |
| 403 | } else { |
| 404 | vargood = 0; |
| 405 | for (j = 0; argv[i][j] != 0; j++) { |
| 406 | if (j == sizeof(var)) { |
| 407 | vargood = 0; |
| 408 | break; |
| 409 | } |
| 410 | if (j > 0 && argv[i][j] == '=') |
| 411 | vargood = 1; |
| 412 | var[j] = (char)argv[i][j]; |
| 413 | } |
| 414 | if (vargood) { |
| 415 | var[j] = 0; |
| 416 | putenv(var); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | for (i = 0; howto_names[i].ev != NULL; i++) |
| 421 | if (howto & howto_names[i].mask) |
| 422 | setenv(howto_names[i].ev, "YES", 1); |
| 423 | if (howto & RB_MULTIPLE) { |
| 424 | if (howto & RB_SERIAL) |
| 425 | setenv("console", "ttya text" , 1); |
| 426 | else |
| 427 | setenv("console", "text ttya" , 1); |
| 428 | } else if (howto & RB_SERIAL) { |
| 429 | setenv("console", "ttya" , 1); |
| 430 | } |
| 431 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 432 | /* |
| 433 | * March through the device switch probing for things. |
| 434 | */ |
| 435 | for (i = 0; devsw[i] != NULL; i++) |
| 436 | if (devsw[i]->dv_init != NULL) |
| 437 | (devsw[i]->dv_init)(); |
| 438 | |
| 439 | /* Get our loaded image protocol interface structure. */ |
| 440 | BS->HandleProtocol(IH, &imgid, (VOID**)&img); |
| 441 | |
| 442 | printf("Command line arguments:"); |
| 443 | for (i = 0; i < argc; i++) { |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 444 | printf(" %S", argv[i]); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 445 | } |
| 446 | printf("\n"); |
| 447 | |
| 448 | printf("Image base: 0x%lx\n", (u_long)img->ImageBase); |
| 449 | printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16, |
| 450 | ST->Hdr.Revision & 0xffff); |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 451 | printf("EFI Firmware: %S (rev %d.%02d)\n", ST->FirmwareVendor, |
| 452 | ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 453 | |
Toomas Soome | f6c9444 | 2017-01-03 11:24:09 +0200 | [diff] [blame] | 454 | printf("\n%s", bootprog_info); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 455 | |
| 456 | /* |
| 457 | * Disable the watchdog timer. By default the boot manager sets |
| 458 | * the timer to 5 minutes before invoking a boot option. If we |
| 459 | * want to return to the boot manager, we have to disable the |
| 460 | * watchdog timer and since we're an interactive program, we don't |
| 461 | * want to wait until the user types "quit". The timer may have |
| 462 | * fired by then. We don't care if this fails. It does not prevent |
| 463 | * normal functioning in any way... |
| 464 | */ |
| 465 | BS->SetWatchdogTimer(0, 0, 0, NULL); |
| 466 | |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame^] | 467 | if (find_currdev(img) != 0) |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 468 | return (EFI_NOT_FOUND); |
| 469 | |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 470 | efi_init_environment(); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 471 | setenv("ISADIR", "amd64", 1); /* we only build 64bit */ |
Toomas Soome | dcba96f | 2016-10-09 17:12:23 +0300 | [diff] [blame] | 472 | acpi_detect(); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 473 | |
Toomas Soome | dcba96f | 2016-10-09 17:12:23 +0300 | [diff] [blame] | 474 | if ((ptr = efi_get_table(&smbios3)) == NULL) |
| 475 | ptr = efi_get_table(&smbios); |
| 476 | smbios_detect(ptr); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 477 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 478 | interact(NULL); /* doesn't return */ |
| 479 | |
| 480 | return (EFI_SUCCESS); /* keep compiler happy */ |
| 481 | } |
| 482 | |
| 483 | COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); |
| 484 | |
| 485 | static int |
Toomas Soome | 30c75cb | 2016-03-13 20:45:57 +0200 | [diff] [blame] | 486 | command_reboot(int argc __unused, char *argv[] __unused) |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 487 | { |
| 488 | int i; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 489 | |
| 490 | for (i = 0; devsw[i] != NULL; ++i) |
| 491 | if (devsw[i]->dv_cleanup != NULL) |
| 492 | (devsw[i]->dv_cleanup)(); |
| 493 | |
Toomas Soome | 48d8443 | 2017-04-27 19:19:29 +0300 | [diff] [blame] | 494 | RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 495 | |
| 496 | /* NOTREACHED */ |
| 497 | return (CMD_ERROR); |
| 498 | } |
| 499 | |
| 500 | COMMAND_SET(memmap, "memmap", "print memory map", command_memmap); |
| 501 | |
| 502 | static int |
Toomas Soome | 30c75cb | 2016-03-13 20:45:57 +0200 | [diff] [blame] | 503 | command_memmap(int argc __unused, char *argv[] __unused) |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 504 | { |
| 505 | UINTN sz; |
| 506 | EFI_MEMORY_DESCRIPTOR *map, *p; |
| 507 | UINTN key, dsz; |
| 508 | UINT32 dver; |
| 509 | EFI_STATUS status; |
| 510 | int i, ndesc; |
| 511 | int rv = 0; |
| 512 | char line[80]; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 513 | |
| 514 | sz = 0; |
| 515 | status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver); |
| 516 | if (status != EFI_BUFFER_TOO_SMALL) { |
| 517 | printf("Can't determine memory map size\n"); |
| 518 | return (CMD_ERROR); |
| 519 | } |
| 520 | map = malloc(sz); |
| 521 | status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver); |
| 522 | if (EFI_ERROR(status)) { |
| 523 | printf("Can't read memory map\n"); |
| 524 | return (CMD_ERROR); |
| 525 | } |
| 526 | |
| 527 | ndesc = sz / dsz; |
| 528 | snprintf(line, 80, "%23s %12s %12s %8s %4s\n", |
| 529 | "Type", "Physical", "Virtual", "#Pages", "Attr"); |
| 530 | pager_open(); |
| 531 | rv = pager_output(line); |
| 532 | if (rv) { |
| 533 | pager_close(); |
| 534 | return (CMD_OK); |
| 535 | } |
| 536 | |
| 537 | for (i = 0, p = map; i < ndesc; |
| 538 | i++, p = NextMemoryDescriptor(p, dsz)) { |
| 539 | snprintf(line, 80, "%23s %012lx %012lx %08lx ", |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 540 | efi_memory_type(p->Type), |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 541 | p->PhysicalStart, |
| 542 | p->VirtualStart, |
| 543 | p->NumberOfPages); |
| 544 | rv = pager_output(line); |
| 545 | if (rv) |
| 546 | break; |
| 547 | |
| 548 | if (p->Attribute & EFI_MEMORY_UC) |
| 549 | printf("UC "); |
| 550 | if (p->Attribute & EFI_MEMORY_WC) |
| 551 | printf("WC "); |
| 552 | if (p->Attribute & EFI_MEMORY_WT) |
| 553 | printf("WT "); |
| 554 | if (p->Attribute & EFI_MEMORY_WB) |
| 555 | printf("WB "); |
| 556 | if (p->Attribute & EFI_MEMORY_UCE) |
| 557 | printf("UCE "); |
| 558 | if (p->Attribute & EFI_MEMORY_WP) |
| 559 | printf("WP "); |
| 560 | if (p->Attribute & EFI_MEMORY_RP) |
| 561 | printf("RP "); |
| 562 | if (p->Attribute & EFI_MEMORY_XP) |
| 563 | printf("XP "); |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 564 | if (p->Attribute & EFI_MEMORY_NV) |
| 565 | printf("NV "); |
| 566 | if (p->Attribute & EFI_MEMORY_MORE_RELIABLE) |
| 567 | printf("MR "); |
| 568 | if (p->Attribute & EFI_MEMORY_RO) |
| 569 | printf("RO "); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 570 | rv = pager_output("\n"); |
| 571 | if (rv) |
| 572 | break; |
| 573 | } |
| 574 | |
| 575 | pager_close(); |
| 576 | return (CMD_OK); |
| 577 | } |
| 578 | |
| 579 | COMMAND_SET(configuration, "configuration", "print configuration tables", |
| 580 | command_configuration); |
| 581 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 582 | static int |
Toomas Soome | 30c75cb | 2016-03-13 20:45:57 +0200 | [diff] [blame] | 583 | command_configuration(int argc __unused, char *argv[] __unused) |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 584 | { |
| 585 | UINTN i; |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 586 | char *name; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 587 | |
| 588 | printf("NumberOfTableEntries=%lu\n", |
| 589 | (unsigned long)ST->NumberOfTableEntries); |
| 590 | for (i = 0; i < ST->NumberOfTableEntries; i++) { |
| 591 | EFI_GUID *guid; |
| 592 | |
| 593 | printf(" "); |
| 594 | guid = &ST->ConfigurationTable[i].VendorGuid; |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 595 | |
| 596 | if (efi_guid_to_name(guid, &name) == true) { |
| 597 | printf(name); |
| 598 | free(name); |
| 599 | } else { |
| 600 | printf("Error while translating UUID to name"); |
| 601 | } |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 602 | printf(" at %p\n", ST->ConfigurationTable[i].VendorTable); |
| 603 | } |
| 604 | |
| 605 | return (CMD_OK); |
| 606 | } |
| 607 | |
| 608 | |
| 609 | COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode); |
| 610 | |
| 611 | static int |
| 612 | command_mode(int argc, char *argv[]) |
| 613 | { |
| 614 | UINTN cols, rows; |
| 615 | unsigned int mode; |
| 616 | int i; |
| 617 | char *cp; |
| 618 | char rowenv[8]; |
| 619 | EFI_STATUS status; |
| 620 | SIMPLE_TEXT_OUTPUT_INTERFACE *conout; |
| 621 | extern void HO(void); |
| 622 | |
| 623 | conout = ST->ConOut; |
| 624 | |
| 625 | if (argc > 1) { |
| 626 | mode = strtol(argv[1], &cp, 0); |
| 627 | if (cp[0] != '\0') { |
| 628 | printf("Invalid mode\n"); |
| 629 | return (CMD_ERROR); |
| 630 | } |
| 631 | status = conout->QueryMode(conout, mode, &cols, &rows); |
| 632 | if (EFI_ERROR(status)) { |
| 633 | printf("invalid mode %d\n", mode); |
| 634 | return (CMD_ERROR); |
| 635 | } |
| 636 | status = conout->SetMode(conout, mode); |
| 637 | if (EFI_ERROR(status)) { |
| 638 | printf("couldn't set mode %d\n", mode); |
| 639 | return (CMD_ERROR); |
| 640 | } |
| 641 | sprintf(rowenv, "%u", (unsigned)rows); |
| 642 | setenv("LINES", rowenv, 1); |
| 643 | sprintf(rowenv, "%u", (unsigned)cols); |
| 644 | setenv("COLUMNS", rowenv, 1); |
| 645 | HO(); /* set cursor */ |
| 646 | return (CMD_OK); |
| 647 | } |
| 648 | |
| 649 | printf("Current mode: %d\n", conout->Mode->Mode); |
| 650 | for (i = 0; i <= conout->Mode->MaxMode; i++) { |
| 651 | status = conout->QueryMode(conout, i, &cols, &rows); |
| 652 | if (EFI_ERROR(status)) |
| 653 | continue; |
| 654 | printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols, |
| 655 | (unsigned)rows); |
| 656 | } |
| 657 | |
| 658 | if (i != 0) |
| 659 | printf("Select a mode with the command \"mode <number>\"\n"); |
| 660 | |
| 661 | return (CMD_OK); |
| 662 | } |
| 663 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 664 | COMMAND_SET(lsefi, "lsefi", "list EFI handles", command_lsefi); |
| 665 | |
| 666 | static int |
Toomas Soome | 30c75cb | 2016-03-13 20:45:57 +0200 | [diff] [blame] | 667 | command_lsefi(int argc __unused, char *argv[] __unused) |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 668 | { |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 669 | char *name; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 670 | EFI_HANDLE *buffer = NULL; |
| 671 | EFI_HANDLE handle; |
| 672 | UINTN bufsz = 0, i, j; |
| 673 | EFI_STATUS status; |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 674 | int ret; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 675 | |
| 676 | status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer); |
| 677 | if (status != EFI_BUFFER_TOO_SMALL) { |
| 678 | snprintf(command_errbuf, sizeof (command_errbuf), |
| 679 | "unexpected error: %lld", (long long)status); |
| 680 | return (CMD_ERROR); |
| 681 | } |
| 682 | if ((buffer = malloc(bufsz)) == NULL) { |
| 683 | sprintf(command_errbuf, "out of memory"); |
| 684 | return (CMD_ERROR); |
| 685 | } |
| 686 | |
| 687 | status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer); |
| 688 | if (EFI_ERROR(status)) { |
| 689 | free(buffer); |
| 690 | snprintf(command_errbuf, sizeof (command_errbuf), |
| 691 | "LocateHandle() error: %lld", (long long)status); |
| 692 | return (CMD_ERROR); |
| 693 | } |
| 694 | |
| 695 | pager_open(); |
| 696 | for (i = 0; i < (bufsz / sizeof (EFI_HANDLE)); i++) { |
| 697 | UINTN nproto = 0; |
| 698 | EFI_GUID **protocols = NULL; |
| 699 | |
| 700 | handle = buffer[i]; |
| 701 | printf("Handle %p", handle); |
| 702 | if (pager_output("\n")) |
| 703 | break; |
| 704 | /* device path */ |
| 705 | |
| 706 | status = BS->ProtocolsPerHandle(handle, &protocols, &nproto); |
| 707 | if (EFI_ERROR(status)) { |
| 708 | snprintf(command_errbuf, sizeof (command_errbuf), |
| 709 | "ProtocolsPerHandle() error: %lld", |
| 710 | (long long)status); |
| 711 | continue; |
| 712 | } |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 713 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 714 | for (j = 0; j < nproto; j++) { |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 715 | if (efi_guid_to_name(protocols[j], &name) == true) { |
| 716 | printf(" %s", name); |
| 717 | free(name); |
| 718 | } else { |
| 719 | printf("Error while translating UUID to name"); |
| 720 | } |
| 721 | if ((ret = pager_output("\n")) != 0) |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 722 | break; |
| 723 | } |
| 724 | BS->FreePool(protocols); |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 725 | if (ret != 0) |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 726 | break; |
| 727 | } |
| 728 | pager_close(); |
| 729 | free(buffer); |
| 730 | return (CMD_OK); |
| 731 | } |
| 732 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 733 | COMMAND_SET(lszfs, "lszfs", "list child datasets of a zfs dataset", |
| 734 | command_lszfs); |
| 735 | |
| 736 | static int |
| 737 | command_lszfs(int argc, char *argv[]) |
| 738 | { |
| 739 | int err; |
| 740 | |
| 741 | if (argc != 2) { |
| 742 | command_errmsg = "wrong number of arguments"; |
| 743 | return (CMD_ERROR); |
| 744 | } |
| 745 | |
| 746 | err = zfs_list(argv[1]); |
| 747 | if (err != 0) { |
| 748 | command_errmsg = strerror(err); |
| 749 | return (CMD_ERROR); |
| 750 | } |
| 751 | return (CMD_OK); |
| 752 | } |
| 753 | |
| 754 | #ifdef __FreeBSD__ |
| 755 | COMMAND_SET(reloadbe, "reloadbe", "refresh the list of ZFS Boot Environments", |
| 756 | command_reloadbe); |
| 757 | |
| 758 | static int |
| 759 | command_reloadbe(int argc, char *argv[]) |
| 760 | { |
| 761 | int err; |
| 762 | char *root; |
| 763 | |
| 764 | if (argc > 2) { |
| 765 | command_errmsg = "wrong number of arguments"; |
| 766 | return (CMD_ERROR); |
| 767 | } |
| 768 | |
| 769 | if (argc == 2) { |
| 770 | err = zfs_bootenv(argv[1]); |
| 771 | } else { |
| 772 | root = getenv("zfs_be_root"); |
| 773 | if (root == NULL) { |
| 774 | return (CMD_OK); |
| 775 | } |
| 776 | err = zfs_bootenv(root); |
| 777 | } |
| 778 | |
| 779 | if (err != 0) { |
| 780 | command_errmsg = strerror(err); |
| 781 | return (CMD_ERROR); |
| 782 | } |
| 783 | |
| 784 | return (CMD_OK); |
| 785 | } |
| 786 | #endif /* __FreeBSD__ */ |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 787 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 788 | #ifdef LOADER_FDT_SUPPORT |
| 789 | extern int command_fdt_internal(int argc, char *argv[]); |
| 790 | |
| 791 | /* |
| 792 | * Since proper fdt command handling function is defined in fdt_loader_cmd.c, |
| 793 | * and declaring it as extern is in contradiction with COMMAND_SET() macro |
| 794 | * (which uses static pointer), we're defining wrapper function, which |
| 795 | * calls the proper fdt handling routine. |
| 796 | */ |
| 797 | static int |
| 798 | command_fdt(int argc, char *argv[]) |
| 799 | { |
| 800 | return (command_fdt_internal(argc, argv)); |
| 801 | } |
| 802 | |
| 803 | COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt); |
| 804 | #endif |
| 805 | |
Toomas Soome | f9feecc | 2016-10-09 17:30:28 +0300 | [diff] [blame] | 806 | /* |
| 807 | * Chain load another efi loader. |
| 808 | */ |
| 809 | static int |
| 810 | command_chain(int argc, char *argv[]) |
| 811 | { |
| 812 | EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL; |
| 813 | EFI_HANDLE loaderhandle; |
| 814 | EFI_LOADED_IMAGE *loaded_image; |
| 815 | EFI_STATUS status; |
| 816 | struct stat st; |
| 817 | struct devdesc *dev; |
| 818 | char *name, *path; |
| 819 | void *buf; |
| 820 | int fd; |
| 821 | |
| 822 | if (argc < 2) { |
| 823 | command_errmsg = "wrong number of arguments"; |
| 824 | return (CMD_ERROR); |
| 825 | } |
| 826 | |
| 827 | name = argv[1]; |
| 828 | |
| 829 | if ((fd = open(name, O_RDONLY)) < 0) { |
| 830 | command_errmsg = "no such file"; |
| 831 | return (CMD_ERROR); |
| 832 | } |
| 833 | |
| 834 | if (fstat(fd, &st) < -1) { |
| 835 | command_errmsg = "stat failed"; |
| 836 | close(fd); |
| 837 | return (CMD_ERROR); |
| 838 | } |
| 839 | |
| 840 | status = BS->AllocatePool(EfiLoaderCode, (UINTN)st.st_size, &buf); |
| 841 | if (status != EFI_SUCCESS) { |
| 842 | command_errmsg = "failed to allocate buffer"; |
| 843 | close(fd); |
| 844 | return (CMD_ERROR); |
| 845 | } |
| 846 | if (read(fd, buf, st.st_size) != st.st_size) { |
| 847 | command_errmsg = "error while reading the file"; |
| 848 | (void)BS->FreePool(buf); |
| 849 | close(fd); |
| 850 | return (CMD_ERROR); |
| 851 | } |
| 852 | close(fd); |
| 853 | status = BS->LoadImage(FALSE, IH, NULL, buf, st.st_size, &loaderhandle); |
| 854 | (void)BS->FreePool(buf); |
| 855 | if (status != EFI_SUCCESS) { |
| 856 | command_errmsg = "LoadImage failed"; |
| 857 | return (CMD_ERROR); |
| 858 | } |
| 859 | status = BS->HandleProtocol(loaderhandle, &LoadedImageGUID, |
| 860 | (void **)&loaded_image); |
| 861 | |
| 862 | if (argc > 2) { |
| 863 | int i, len = 0; |
| 864 | CHAR16 *argp; |
| 865 | |
| 866 | for (i = 2; i < argc; i++) |
| 867 | len += strlen(argv[i]) + 1; |
| 868 | |
| 869 | len *= sizeof (*argp); |
| 870 | loaded_image->LoadOptions = argp = malloc (len); |
| 871 | if (loaded_image->LoadOptions == NULL) { |
| 872 | (void) BS->UnloadImage(loaded_image); |
| 873 | return (CMD_ERROR); |
| 874 | } |
| 875 | loaded_image->LoadOptionsSize = len; |
| 876 | for (i = 2; i < argc; i++) { |
| 877 | char *ptr = argv[i]; |
| 878 | while (*ptr) |
| 879 | *(argp++) = *(ptr++); |
| 880 | *(argp++) = ' '; |
| 881 | } |
| 882 | *(--argv) = 0; |
| 883 | } |
| 884 | |
| 885 | if (efi_getdev((void **)&dev, name, (const char **)&path) == 0) |
| 886 | loaded_image->DeviceHandle = |
| 887 | efi_find_handle(dev->d_dev, dev->d_unit); |
| 888 | |
| 889 | dev_cleanup(); |
| 890 | status = BS->StartImage(loaderhandle, NULL, NULL); |
| 891 | if (status != EFI_SUCCESS) { |
| 892 | command_errmsg = "StartImage failed"; |
| 893 | free(loaded_image->LoadOptions); |
| 894 | loaded_image->LoadOptions = NULL; |
| 895 | status = BS->UnloadImage(loaded_image); |
| 896 | return (CMD_ERROR); |
| 897 | } |
| 898 | |
| 899 | return (CMD_ERROR); /* not reached */ |
| 900 | } |
| 901 | |
| 902 | COMMAND_SET(chain, "chain", "chain load file", command_chain); |
| 903 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 904 | #ifdef EFI_ZFS_BOOT |
| 905 | static void |
| 906 | efi_zfs_probe(void) |
| 907 | { |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame^] | 908 | pdinfo_list_t *hdi; |
| 909 | pdinfo_t *hd, *pd = NULL; |
| 910 | EFI_GUID imgid = LOADED_IMAGE_PROTOCOL; |
| 911 | EFI_LOADED_IMAGE *img; |
| 912 | char devname[SPECNAMELEN + 1]; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 913 | |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame^] | 914 | BS->HandleProtocol(IH, &imgid, (VOID**)&img); |
| 915 | hdi = efiblk_get_pdinfo_list(&efipart_hddev); |
| 916 | |
| 917 | /* |
| 918 | * Find the handle for the boot device. The boot1 did find the |
| 919 | * device with loader binary, now we need to search for the |
| 920 | * same device and if it is part of the zfs pool, we record the |
| 921 | * pool GUID for currdev setup. |
| 922 | */ |
| 923 | STAILQ_FOREACH(hd, hdi, pd_link) { |
| 924 | STAILQ_FOREACH(pd, &hd->pd_part, pd_link) { |
| 925 | |
| 926 | snprintf(devname, sizeof(devname), "%s%dp%d:", |
| 927 | efipart_hddev.dv_name, hd->pd_unit, pd->pd_unit); |
| 928 | if (pd->pd_handle == img->DeviceHandle) |
| 929 | (void) zfs_probe_dev(devname, &pool_guid); |
| 930 | else |
| 931 | (void) zfs_probe_dev(devname, NULL); |
| 932 | } |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 933 | } |
| 934 | } |
Toomas Soome | edb3504 | 2016-11-07 15:37:12 +0200 | [diff] [blame] | 935 | |
| 936 | uint64_t |
| 937 | ldi_get_size(void *priv) |
| 938 | { |
| 939 | int fd = (uintptr_t) priv; |
| 940 | uint64_t size; |
| 941 | |
| 942 | ioctl(fd, DIOCGMEDIASIZE, &size); |
| 943 | return (size); |
| 944 | } |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 945 | #endif |