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