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 | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 54 | struct arch_switch archsw; /* MI/MD interface boundary */ |
| 55 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 56 | EFI_GUID devid = DEVICE_PATH_PROTOCOL; |
| 57 | EFI_GUID imgid = LOADED_IMAGE_PROTOCOL; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 58 | EFI_GUID smbios = SMBIOS_TABLE_GUID; |
| 59 | EFI_GUID smbios3 = SMBIOS3_TABLE_GUID; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 60 | EFI_GUID inputid = SIMPLE_TEXT_INPUT_PROTOCOL; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 61 | |
Toomas Soome | dcba96f | 2016-10-09 17:12:23 +0300 | [diff] [blame] | 62 | extern void acpi_detect(void); |
Toomas Soome | f9feecc | 2016-10-09 17:30:28 +0300 | [diff] [blame] | 63 | extern void efi_getsmap(void); |
Toomas Soome | 9f23ea4 | 2017-12-12 15:23:09 +0200 | [diff] [blame] | 64 | |
| 65 | static EFI_LOADED_IMAGE *img; |
| 66 | |
| 67 | bool |
| 68 | efi_zfs_is_preferred(EFI_HANDLE *h) |
| 69 | { |
| 70 | return (h == img->DeviceHandle); |
| 71 | } |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 72 | |
Toomas Soome | afa95be | 2018-06-16 10:22:42 +0300 | [diff] [blame] | 73 | static bool |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 74 | has_keyboard(void) |
| 75 | { |
| 76 | EFI_STATUS status; |
| 77 | EFI_DEVICE_PATH *path; |
| 78 | EFI_HANDLE *hin, *hin_end, *walker; |
| 79 | UINTN sz; |
Toomas Soome | afa95be | 2018-06-16 10:22:42 +0300 | [diff] [blame] | 80 | bool retval = false; |
Toomas Soome | 9f23ea4 | 2017-12-12 15:23:09 +0200 | [diff] [blame] | 81 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 82 | /* |
| 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 Soome | afa95be | 2018-06-16 10:22:42 +0300 | [diff] [blame] | 97 | return (retval); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * Look at each of the handles. If it supports the device path protocol, |
| 101 | * use it to get the device path for this handle. Then see if that |
| 102 | * device path matches either the USB device path for keyboards or the |
| 103 | * legacy device path for keyboards. |
| 104 | */ |
| 105 | hin_end = &hin[sz / sizeof(*hin)]; |
| 106 | for (walker = hin; walker < hin_end; walker++) { |
| 107 | status = BS->HandleProtocol(*walker, &devid, (VOID **)&path); |
| 108 | if (EFI_ERROR(status)) |
| 109 | continue; |
| 110 | |
| 111 | while (!IsDevicePathEnd(path)) { |
| 112 | /* |
| 113 | * Check for the ACPI keyboard node. All PNP3xx nodes |
| 114 | * are keyboards of different flavors. Note: It is |
| 115 | * unclear of there's always a keyboard node when |
| 116 | * there's a keyboard controller, or if there's only one |
| 117 | * when a keyboard is detected at boot. |
| 118 | */ |
| 119 | if (DevicePathType(path) == ACPI_DEVICE_PATH && |
| 120 | (DevicePathSubType(path) == ACPI_DP || |
| 121 | DevicePathSubType(path) == ACPI_EXTENDED_DP)) { |
| 122 | ACPI_HID_DEVICE_PATH *acpi; |
| 123 | |
| 124 | acpi = (ACPI_HID_DEVICE_PATH *)(void *)path; |
| 125 | if ((EISA_ID_TO_NUM(acpi->HID) & 0xff00) == 0x300 && |
| 126 | (acpi->HID & 0xffff) == PNP_EISA_ID_CONST) { |
Toomas Soome | afa95be | 2018-06-16 10:22:42 +0300 | [diff] [blame] | 127 | retval = true; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 128 | goto out; |
| 129 | } |
| 130 | /* |
| 131 | * Check for USB keyboard node, if present. Unlike a |
| 132 | * PS/2 keyboard, these definitely only appear when |
| 133 | * connected to the system. |
| 134 | */ |
| 135 | } else if (DevicePathType(path) == MESSAGING_DEVICE_PATH && |
| 136 | DevicePathSubType(path) == MSG_USB_CLASS_DP) { |
| 137 | USB_CLASS_DEVICE_PATH *usb; |
Toomas Soome | c142ce1 | 2018-03-13 14:16:40 +0200 | [diff] [blame] | 138 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 139 | 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 Soome | afa95be | 2018-06-16 10:22:42 +0300 | [diff] [blame] | 143 | retval = true; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 144 | goto out; |
| 145 | } |
| 146 | } |
| 147 | path = NextDevicePathNode(path); |
| 148 | } |
| 149 | } |
| 150 | out: |
| 151 | free(hin); |
Toomas Soome | afa95be | 2018-06-16 10:22:42 +0300 | [diff] [blame] | 152 | return (retval); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 153 | } |
| 154 | |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame] | 155 | static void |
| 156 | set_devdesc_currdev(struct devsw *dev, int unit) |
Toomas Soome | ab8c1d4 | 2016-11-12 13:46:26 +0200 | [diff] [blame] | 157 | { |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame] | 158 | struct devdesc currdev; |
| 159 | char *devname; |
| 160 | |
| 161 | currdev.d_dev = dev; |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame] | 162 | currdev.d_unit = unit; |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame] | 163 | 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 | |
| 170 | static int |
| 171 | find_currdev(EFI_LOADED_IMAGE *img) |
| 172 | { |
| 173 | pdinfo_list_t *pdi_list; |
| 174 | pdinfo_t *dp, *pp; |
Toomas Soome | ab8c1d4 | 2016-11-12 13:46:26 +0200 | [diff] [blame] | 175 | EFI_DEVICE_PATH *devpath, *copy; |
| 176 | EFI_HANDLE h; |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame] | 177 | 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 Soome | 76b3594 | 2018-03-13 11:58:56 +0200 | [diff] [blame] | 186 | currdev.dd.d_dev = &zfs_dev; |
| 187 | currdev.dd.d_unit = 0; |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame] | 188 | 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 Soome | 76b3594 | 2018-03-13 11:58:56 +0200 | [diff] [blame] | 204 | currdev.dd.d_dev = &efipart_hddev; |
Toomas Soome | 76b3594 | 2018-03-13 11:58:56 +0200 | [diff] [blame] | 205 | currdev.dd.d_unit = dp->pd_unit; |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame] | 206 | 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 Soome | ab8c1d4 | 2016-11-12 13:46:26 +0200 | [diff] [blame] | 250 | |
| 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 Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame] | 257 | if (efi_handle_lookup(img->DeviceHandle, &dev, &unit, &extra) == 0) { |
| 258 | set_devdesc_currdev(dev, unit); |
Toomas Soome | ab8c1d4 | 2016-11-12 13:46:26 +0200 | [diff] [blame] | 259 | return (0); |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame] | 260 | } |
Toomas Soome | ab8c1d4 | 2016-11-12 13:46:26 +0200 | [diff] [blame] | 261 | |
| 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 Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame] | 272 | if (efi_handle_lookup(h, &dev, &unit, &extra) == 0) { |
| 273 | set_devdesc_currdev(dev, unit); |
Toomas Soome | ab8c1d4 | 2016-11-12 13:46:26 +0200 | [diff] [blame] | 274 | return (0); |
Toomas Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame] | 275 | } |
Toomas Soome | ab8c1d4 | 2016-11-12 13:46:26 +0200 | [diff] [blame] | 276 | |
| 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 Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 288 | EFI_STATUS |
| 289 | main(int argc, CHAR16 *argv[]) |
| 290 | { |
| 291 | char var[128]; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 292 | EFI_GUID *guid; |
Toomas Soome | 8fe0f54 | 2018-06-14 10:00:19 +0300 | [diff] [blame] | 293 | int i, j, howto; |
| 294 | bool vargood; |
Toomas Soome | dcba96f | 2016-10-09 17:12:23 +0300 | [diff] [blame] | 295 | void *ptr; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 296 | UINTN k; |
Toomas Soome | afa95be | 2018-06-16 10:22:42 +0300 | [diff] [blame] | 297 | bool has_kbd; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 298 | |
| 299 | archsw.arch_autoload = efi_autoload; |
| 300 | archsw.arch_getdev = efi_getdev; |
| 301 | archsw.arch_copyin = efi_copyin; |
| 302 | archsw.arch_copyout = efi_copyout; |
| 303 | archsw.arch_readin = efi_readin; |
Toomas Soome | f9feecc | 2016-10-09 17:30:28 +0300 | [diff] [blame] | 304 | archsw.arch_loadaddr = efi_loadaddr; |
| 305 | archsw.arch_free_loadaddr = efi_free_loadaddr; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 306 | /* Note this needs to be set before ZFS init. */ |
| 307 | archsw.arch_zfs_probe = efi_zfs_probe; |
Toomas Soome | 9f23ea4 | 2017-12-12 15:23:09 +0200 | [diff] [blame] | 308 | |
| 309 | /* Get our loaded image protocol interface structure. */ |
| 310 | BS->HandleProtocol(IH, &imgid, (VOID**)&img); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 311 | |
Toomas Soome | 04f8e09 | 2016-11-10 21:05:15 +0200 | [diff] [blame] | 312 | /* Init the time source */ |
| 313 | efi_time_init(); |
| 314 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 315 | 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 Soome | f9feecc | 2016-10-09 17:30:28 +0300 | [diff] [blame] | 324 | efi_getsmap(); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 325 | |
| 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 Soome | 2aca6c6 | 2017-06-02 23:07:12 +0300 | [diff] [blame] | 380 | strncpy(var, "115200", |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 381 | sizeof(var)); |
Toomas Soome | 2aca6c6 | 2017-06-02 23:07:12 +0300 | [diff] [blame] | 382 | } else { |
| 383 | CHAR16 *ptr; |
| 384 | ptr = &argv[i + 1][0]; |
| 385 | cpy16to8(ptr, var, |
| 386 | sizeof(var)); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 387 | } |
| 388 | i++; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 389 | } else { |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 390 | cpy16to8(&argv[i][j + 1], var, |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 391 | sizeof(var)); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 392 | } |
Toomas Soome | 2aca6c6 | 2017-06-02 23:07:12 +0300 | [diff] [blame] | 393 | strncat(var, ",8,n,1,-", sizeof(var)); |
| 394 | setenv("ttya-mode", var, 1); |
| 395 | break; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 396 | case 'v': |
| 397 | howto |= RB_VERBOSE; |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | } else { |
Toomas Soome | 8fe0f54 | 2018-06-14 10:00:19 +0300 | [diff] [blame] | 402 | vargood = false; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 403 | for (j = 0; argv[i][j] != 0; j++) { |
| 404 | if (j == sizeof(var)) { |
Toomas Soome | 8fe0f54 | 2018-06-14 10:00:19 +0300 | [diff] [blame] | 405 | vargood = false; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 406 | break; |
| 407 | } |
| 408 | if (j > 0 && argv[i][j] == '=') |
Toomas Soome | 8fe0f54 | 2018-06-14 10:00:19 +0300 | [diff] [blame] | 409 | vargood = true; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 410 | 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 Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 430 | /* |
Toomas Soome | c00b6c9 | 2018-02-19 19:14:22 +0200 | [diff] [blame] | 431 | * Scan the BLOCK IO MEDIA handles then |
| 432 | * march through the device switch probing for things. |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 433 | */ |
Toomas Soome | c00b6c9 | 2018-02-19 19:14:22 +0200 | [diff] [blame] | 434 | 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 Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 440 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 441 | printf("Command line arguments:"); |
| 442 | for (i = 0; i < argc; i++) { |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 443 | printf(" %S", argv[i]); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 444 | } |
| 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 Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 450 | printf("EFI Firmware: %S (rev %d.%02d)\n", ST->FirmwareVendor, |
| 451 | ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 452 | |
Toomas Soome | f6c9444 | 2017-01-03 11:24:09 +0200 | [diff] [blame] | 453 | printf("\n%s", bootprog_info); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 454 | |
| 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 Soome | dbacaf5 | 2016-11-17 17:02:22 +0200 | [diff] [blame] | 466 | if (find_currdev(img) != 0) |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 467 | return (EFI_NOT_FOUND); |
| 468 | |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 469 | efi_init_environment(); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 470 | setenv("ISADIR", "amd64", 1); /* we only build 64bit */ |
Toomas Soome | d9256ff | 2016-07-17 20:39:53 +0300 | [diff] [blame] | 471 | bi_isadir(); /* set ISADIR */ |
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 | |
Toomas Soome | b31ca80 | 2018-10-10 15:29:10 +0300 | [diff] [blame] | 500 | COMMAND_SET(poweroff, "poweroff", "power off the system", command_poweroff); |
| 501 | |
| 502 | static int |
| 503 | command_poweroff(int argc __unused, char *argv[] __unused) |
| 504 | { |
| 505 | int i; |
| 506 | |
| 507 | for (i = 0; devsw[i] != NULL; ++i) |
| 508 | if (devsw[i]->dv_cleanup != NULL) |
| 509 | (devsw[i]->dv_cleanup)(); |
| 510 | |
| 511 | RS->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL); |
| 512 | |
| 513 | /* NOTREACHED */ |
| 514 | return (CMD_ERROR); |
| 515 | } |
| 516 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 517 | COMMAND_SET(memmap, "memmap", "print memory map", command_memmap); |
| 518 | |
| 519 | static int |
Toomas Soome | 30c75cb | 2016-03-13 20:45:57 +0200 | [diff] [blame] | 520 | command_memmap(int argc __unused, char *argv[] __unused) |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 521 | { |
| 522 | UINTN sz; |
| 523 | EFI_MEMORY_DESCRIPTOR *map, *p; |
| 524 | UINTN key, dsz; |
| 525 | UINT32 dver; |
| 526 | EFI_STATUS status; |
| 527 | int i, ndesc; |
| 528 | int rv = 0; |
| 529 | char line[80]; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 530 | |
| 531 | sz = 0; |
| 532 | status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver); |
| 533 | if (status != EFI_BUFFER_TOO_SMALL) { |
| 534 | printf("Can't determine memory map size\n"); |
| 535 | return (CMD_ERROR); |
| 536 | } |
| 537 | map = malloc(sz); |
| 538 | status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver); |
| 539 | if (EFI_ERROR(status)) { |
| 540 | printf("Can't read memory map\n"); |
| 541 | return (CMD_ERROR); |
| 542 | } |
| 543 | |
| 544 | ndesc = sz / dsz; |
| 545 | snprintf(line, 80, "%23s %12s %12s %8s %4s\n", |
| 546 | "Type", "Physical", "Virtual", "#Pages", "Attr"); |
| 547 | pager_open(); |
| 548 | rv = pager_output(line); |
| 549 | if (rv) { |
| 550 | pager_close(); |
| 551 | return (CMD_OK); |
| 552 | } |
| 553 | |
| 554 | for (i = 0, p = map; i < ndesc; |
| 555 | i++, p = NextMemoryDescriptor(p, dsz)) { |
Toomas Soome | 83b4671 | 2016-08-16 19:02:42 +0300 | [diff] [blame] | 556 | snprintf(line, 80, "%23s %012jx %012jx %08jx ", |
| 557 | efi_memory_type(p->Type), p->PhysicalStart, |
| 558 | p->VirtualStart, p->NumberOfPages); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 559 | rv = pager_output(line); |
| 560 | if (rv) |
| 561 | break; |
| 562 | |
| 563 | if (p->Attribute & EFI_MEMORY_UC) |
| 564 | printf("UC "); |
| 565 | if (p->Attribute & EFI_MEMORY_WC) |
| 566 | printf("WC "); |
| 567 | if (p->Attribute & EFI_MEMORY_WT) |
| 568 | printf("WT "); |
| 569 | if (p->Attribute & EFI_MEMORY_WB) |
| 570 | printf("WB "); |
| 571 | if (p->Attribute & EFI_MEMORY_UCE) |
| 572 | printf("UCE "); |
| 573 | if (p->Attribute & EFI_MEMORY_WP) |
| 574 | printf("WP "); |
| 575 | if (p->Attribute & EFI_MEMORY_RP) |
| 576 | printf("RP "); |
| 577 | if (p->Attribute & EFI_MEMORY_XP) |
| 578 | printf("XP "); |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 579 | if (p->Attribute & EFI_MEMORY_NV) |
| 580 | printf("NV "); |
| 581 | if (p->Attribute & EFI_MEMORY_MORE_RELIABLE) |
| 582 | printf("MR "); |
| 583 | if (p->Attribute & EFI_MEMORY_RO) |
| 584 | printf("RO "); |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 585 | rv = pager_output("\n"); |
| 586 | if (rv) |
| 587 | break; |
| 588 | } |
| 589 | |
| 590 | pager_close(); |
| 591 | return (CMD_OK); |
| 592 | } |
| 593 | |
| 594 | COMMAND_SET(configuration, "configuration", "print configuration tables", |
| 595 | command_configuration); |
| 596 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 597 | static int |
Toomas Soome | 30c75cb | 2016-03-13 20:45:57 +0200 | [diff] [blame] | 598 | command_configuration(int argc __unused, char *argv[] __unused) |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 599 | { |
| 600 | UINTN i; |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 601 | char *name; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 602 | |
| 603 | printf("NumberOfTableEntries=%lu\n", |
| 604 | (unsigned long)ST->NumberOfTableEntries); |
| 605 | for (i = 0; i < ST->NumberOfTableEntries; i++) { |
| 606 | EFI_GUID *guid; |
| 607 | |
| 608 | printf(" "); |
| 609 | guid = &ST->ConfigurationTable[i].VendorGuid; |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 610 | |
| 611 | if (efi_guid_to_name(guid, &name) == true) { |
| 612 | printf(name); |
| 613 | free(name); |
| 614 | } else { |
| 615 | printf("Error while translating UUID to name"); |
| 616 | } |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 617 | printf(" at %p\n", ST->ConfigurationTable[i].VendorTable); |
| 618 | } |
| 619 | |
| 620 | return (CMD_OK); |
| 621 | } |
| 622 | |
| 623 | |
| 624 | COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode); |
| 625 | |
| 626 | static int |
| 627 | command_mode(int argc, char *argv[]) |
| 628 | { |
| 629 | UINTN cols, rows; |
| 630 | unsigned int mode; |
| 631 | int i; |
| 632 | char *cp; |
| 633 | char rowenv[8]; |
| 634 | EFI_STATUS status; |
| 635 | SIMPLE_TEXT_OUTPUT_INTERFACE *conout; |
| 636 | extern void HO(void); |
| 637 | |
| 638 | conout = ST->ConOut; |
| 639 | |
| 640 | if (argc > 1) { |
| 641 | mode = strtol(argv[1], &cp, 0); |
| 642 | if (cp[0] != '\0') { |
| 643 | printf("Invalid mode\n"); |
| 644 | return (CMD_ERROR); |
| 645 | } |
| 646 | status = conout->QueryMode(conout, mode, &cols, &rows); |
| 647 | if (EFI_ERROR(status)) { |
| 648 | printf("invalid mode %d\n", mode); |
| 649 | return (CMD_ERROR); |
| 650 | } |
| 651 | status = conout->SetMode(conout, mode); |
| 652 | if (EFI_ERROR(status)) { |
| 653 | printf("couldn't set mode %d\n", mode); |
| 654 | return (CMD_ERROR); |
| 655 | } |
| 656 | sprintf(rowenv, "%u", (unsigned)rows); |
| 657 | setenv("LINES", rowenv, 1); |
| 658 | sprintf(rowenv, "%u", (unsigned)cols); |
| 659 | setenv("COLUMNS", rowenv, 1); |
| 660 | HO(); /* set cursor */ |
| 661 | return (CMD_OK); |
| 662 | } |
| 663 | |
| 664 | printf("Current mode: %d\n", conout->Mode->Mode); |
| 665 | for (i = 0; i <= conout->Mode->MaxMode; i++) { |
| 666 | status = conout->QueryMode(conout, i, &cols, &rows); |
| 667 | if (EFI_ERROR(status)) |
| 668 | continue; |
| 669 | printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols, |
| 670 | (unsigned)rows); |
| 671 | } |
| 672 | |
| 673 | if (i != 0) |
| 674 | printf("Select a mode with the command \"mode <number>\"\n"); |
| 675 | |
| 676 | return (CMD_OK); |
| 677 | } |
| 678 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 679 | COMMAND_SET(lsefi, "lsefi", "list EFI handles", command_lsefi); |
| 680 | |
| 681 | static int |
Toomas Soome | 30c75cb | 2016-03-13 20:45:57 +0200 | [diff] [blame] | 682 | command_lsefi(int argc __unused, char *argv[] __unused) |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 683 | { |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 684 | char *name; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 685 | EFI_HANDLE *buffer = NULL; |
| 686 | EFI_HANDLE handle; |
| 687 | UINTN bufsz = 0, i, j; |
| 688 | EFI_STATUS status; |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 689 | int ret; |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 690 | |
| 691 | status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer); |
| 692 | if (status != EFI_BUFFER_TOO_SMALL) { |
| 693 | snprintf(command_errbuf, sizeof (command_errbuf), |
| 694 | "unexpected error: %lld", (long long)status); |
| 695 | return (CMD_ERROR); |
| 696 | } |
| 697 | if ((buffer = malloc(bufsz)) == NULL) { |
| 698 | sprintf(command_errbuf, "out of memory"); |
| 699 | return (CMD_ERROR); |
| 700 | } |
| 701 | |
| 702 | status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer); |
| 703 | if (EFI_ERROR(status)) { |
| 704 | free(buffer); |
| 705 | snprintf(command_errbuf, sizeof (command_errbuf), |
| 706 | "LocateHandle() error: %lld", (long long)status); |
| 707 | return (CMD_ERROR); |
| 708 | } |
| 709 | |
| 710 | pager_open(); |
| 711 | for (i = 0; i < (bufsz / sizeof (EFI_HANDLE)); i++) { |
| 712 | UINTN nproto = 0; |
| 713 | EFI_GUID **protocols = NULL; |
| 714 | |
| 715 | handle = buffer[i]; |
| 716 | printf("Handle %p", handle); |
| 717 | if (pager_output("\n")) |
| 718 | break; |
| 719 | /* device path */ |
| 720 | |
| 721 | status = BS->ProtocolsPerHandle(handle, &protocols, &nproto); |
| 722 | if (EFI_ERROR(status)) { |
| 723 | snprintf(command_errbuf, sizeof (command_errbuf), |
| 724 | "ProtocolsPerHandle() error: %lld", |
| 725 | (long long)status); |
| 726 | continue; |
| 727 | } |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 728 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 729 | for (j = 0; j < nproto; j++) { |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 730 | if (efi_guid_to_name(protocols[j], &name) == true) { |
| 731 | printf(" %s", name); |
| 732 | free(name); |
| 733 | } else { |
| 734 | printf("Error while translating UUID to name"); |
| 735 | } |
| 736 | if ((ret = pager_output("\n")) != 0) |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 737 | break; |
| 738 | } |
| 739 | BS->FreePool(protocols); |
Toomas Soome | eee5904 | 2016-11-12 00:06:58 +0200 | [diff] [blame] | 740 | if (ret != 0) |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 741 | break; |
| 742 | } |
| 743 | pager_close(); |
| 744 | free(buffer); |
| 745 | return (CMD_OK); |
| 746 | } |
| 747 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 748 | COMMAND_SET(lszfs, "lszfs", "list child datasets of a zfs dataset", |
| 749 | command_lszfs); |
| 750 | |
| 751 | static int |
| 752 | command_lszfs(int argc, char *argv[]) |
| 753 | { |
| 754 | int err; |
| 755 | |
| 756 | if (argc != 2) { |
| 757 | command_errmsg = "wrong number of arguments"; |
| 758 | return (CMD_ERROR); |
| 759 | } |
| 760 | |
| 761 | err = zfs_list(argv[1]); |
| 762 | if (err != 0) { |
| 763 | command_errmsg = strerror(err); |
| 764 | return (CMD_ERROR); |
| 765 | } |
| 766 | return (CMD_OK); |
| 767 | } |
| 768 | |
| 769 | #ifdef __FreeBSD__ |
| 770 | COMMAND_SET(reloadbe, "reloadbe", "refresh the list of ZFS Boot Environments", |
| 771 | command_reloadbe); |
| 772 | |
| 773 | static int |
| 774 | command_reloadbe(int argc, char *argv[]) |
| 775 | { |
| 776 | int err; |
| 777 | char *root; |
| 778 | |
| 779 | if (argc > 2) { |
| 780 | command_errmsg = "wrong number of arguments"; |
| 781 | return (CMD_ERROR); |
| 782 | } |
| 783 | |
| 784 | if (argc == 2) { |
| 785 | err = zfs_bootenv(argv[1]); |
| 786 | } else { |
| 787 | root = getenv("zfs_be_root"); |
| 788 | if (root == NULL) { |
| 789 | return (CMD_OK); |
| 790 | } |
| 791 | err = zfs_bootenv(root); |
| 792 | } |
| 793 | |
| 794 | if (err != 0) { |
| 795 | command_errmsg = strerror(err); |
| 796 | return (CMD_ERROR); |
| 797 | } |
| 798 | |
| 799 | return (CMD_OK); |
| 800 | } |
| 801 | #endif /* __FreeBSD__ */ |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 802 | |
Toomas Soome | 199767f | 2015-10-25 00:06:51 +0300 | [diff] [blame] | 803 | #ifdef LOADER_FDT_SUPPORT |
| 804 | extern int command_fdt_internal(int argc, char *argv[]); |
| 805 | |
| 806 | /* |
| 807 | * Since proper fdt command handling function is defined in fdt_loader_cmd.c, |
| 808 | * and declaring it as extern is in contradiction with COMMAND_SET() macro |
| 809 | * (which uses static pointer), we're defining wrapper function, which |
| 810 | * calls the proper fdt handling routine. |
| 811 | */ |
| 812 | static int |
| 813 | command_fdt(int argc, char *argv[]) |
| 814 | { |
| 815 | return (command_fdt_internal(argc, argv)); |
| 816 | } |
| 817 | |
| 818 | COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt); |
| 819 | #endif |
| 820 | |
Toomas Soome | f9feecc | 2016-10-09 17:30:28 +0300 | [diff] [blame] | 821 | /* |
| 822 | * Chain load another efi loader. |
| 823 | */ |
| 824 | static int |
| 825 | command_chain(int argc, char *argv[]) |
| 826 | { |
| 827 | EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL; |
| 828 | EFI_HANDLE loaderhandle; |
| 829 | EFI_LOADED_IMAGE *loaded_image; |
| 830 | EFI_STATUS status; |
| 831 | struct stat st; |
| 832 | struct devdesc *dev; |
| 833 | char *name, *path; |
| 834 | void *buf; |
| 835 | int fd; |
| 836 | |
| 837 | if (argc < 2) { |
| 838 | command_errmsg = "wrong number of arguments"; |
| 839 | return (CMD_ERROR); |
| 840 | } |
| 841 | |
| 842 | name = argv[1]; |
| 843 | |
| 844 | if ((fd = open(name, O_RDONLY)) < 0) { |
| 845 | command_errmsg = "no such file"; |
| 846 | return (CMD_ERROR); |
| 847 | } |
| 848 | |
| 849 | if (fstat(fd, &st) < -1) { |
| 850 | command_errmsg = "stat failed"; |
| 851 | close(fd); |
| 852 | return (CMD_ERROR); |
| 853 | } |
| 854 | |
| 855 | status = BS->AllocatePool(EfiLoaderCode, (UINTN)st.st_size, &buf); |
| 856 | if (status != EFI_SUCCESS) { |
| 857 | command_errmsg = "failed to allocate buffer"; |
| 858 | close(fd); |
| 859 | return (CMD_ERROR); |
| 860 | } |
| 861 | if (read(fd, buf, st.st_size) != st.st_size) { |
| 862 | command_errmsg = "error while reading the file"; |
| 863 | (void)BS->FreePool(buf); |
| 864 | close(fd); |
| 865 | return (CMD_ERROR); |
| 866 | } |
| 867 | close(fd); |
| 868 | status = BS->LoadImage(FALSE, IH, NULL, buf, st.st_size, &loaderhandle); |
| 869 | (void)BS->FreePool(buf); |
| 870 | if (status != EFI_SUCCESS) { |
| 871 | command_errmsg = "LoadImage failed"; |
| 872 | return (CMD_ERROR); |
| 873 | } |
| 874 | status = BS->HandleProtocol(loaderhandle, &LoadedImageGUID, |
| 875 | (void **)&loaded_image); |
| 876 | |
| 877 | if (argc > 2) { |
| 878 | int i, len = 0; |
| 879 | CHAR16 *argp; |
| 880 | |
| 881 | for (i = 2; i < argc; i++) |
| 882 | len += strlen(argv[i]) + 1; |
| 883 | |
| 884 | len *= sizeof (*argp); |
| 885 | loaded_image->LoadOptions = argp = malloc (len); |
| 886 | if (loaded_image->LoadOptions == NULL) { |
| 887 | (void) BS->UnloadImage(loaded_image); |
| 888 | return (CMD_ERROR); |
| 889 | } |
| 890 | loaded_image->LoadOptionsSize = len; |
| 891 | for (i = 2; i < argc; i++) { |
| 892 | char *ptr = argv[i]; |
| 893 | while (*ptr) |
| 894 | *(argp++) = *(ptr++); |
| 895 | *(argp++) = ' '; |
| 896 | } |
| 897 | *(--argv) = 0; |
| 898 | } |
| 899 | |
Toomas Soome | eea30b2 | 2017-12-12 18:01:57 +0200 | [diff] [blame] | 900 | if (efi_getdev((void **)&dev, name, (const char **)&path) == 0) { |
| 901 | struct zfs_devdesc *z_dev; |
| 902 | struct disk_devdesc *d_dev; |
| 903 | pdinfo_t *hd, *pd; |
| 904 | |
Toomas Soome | c142ce1 | 2018-03-13 14:16:40 +0200 | [diff] [blame] | 905 | switch (dev->d_dev->dv_type) { |
Toomas Soome | eea30b2 | 2017-12-12 18:01:57 +0200 | [diff] [blame] | 906 | case DEVT_ZFS: |
| 907 | z_dev = (struct zfs_devdesc *)dev; |
| 908 | loaded_image->DeviceHandle = |
| 909 | efizfs_get_handle_by_guid(z_dev->pool_guid); |
| 910 | break; |
| 911 | case DEVT_NET: |
| 912 | loaded_image->DeviceHandle = |
| 913 | efi_find_handle(dev->d_dev, dev->d_unit); |
| 914 | break; |
| 915 | default: |
| 916 | hd = efiblk_get_pdinfo(dev); |
| 917 | if (STAILQ_EMPTY(&hd->pd_part)) { |
| 918 | loaded_image->DeviceHandle = hd->pd_handle; |
| 919 | break; |
| 920 | } |
| 921 | d_dev = (struct disk_devdesc *)dev; |
| 922 | STAILQ_FOREACH(pd, &hd->pd_part, pd_link) { |
| 923 | /* |
| 924 | * d_partition should be 255 |
| 925 | */ |
| 926 | if (pd->pd_unit == d_dev->d_slice) { |
| 927 | loaded_image->DeviceHandle = |
| 928 | pd->pd_handle; |
| 929 | break; |
| 930 | } |
| 931 | } |
| 932 | break; |
| 933 | } |
| 934 | } |
Toomas Soome | f9feecc | 2016-10-09 17:30:28 +0300 | [diff] [blame] | 935 | |
| 936 | dev_cleanup(); |
| 937 | status = BS->StartImage(loaderhandle, NULL, NULL); |
| 938 | if (status != EFI_SUCCESS) { |
| 939 | command_errmsg = "StartImage failed"; |
| 940 | free(loaded_image->LoadOptions); |
| 941 | loaded_image->LoadOptions = NULL; |
| 942 | status = BS->UnloadImage(loaded_image); |
| 943 | return (CMD_ERROR); |
| 944 | } |
| 945 | |
| 946 | return (CMD_ERROR); /* not reached */ |
| 947 | } |
| 948 | |
| 949 | COMMAND_SET(chain, "chain", "chain load file", command_chain); |