Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 5 | * EFI information obtained here: |
| 6 | * http://wiki.phoenix.com/wiki/index.php/EFI_BOOT_SERVICES |
| 7 | * |
| 8 | * This file implements U-Boot running as an EFI application. |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Simon Glass | afb0215 | 2019-12-28 10:45:01 -0700 | [diff] [blame] | 12 | #include <cpu_func.h> |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 13 | #include <debug_uart.h> |
Bin Meng | 268bc9e | 2018-07-19 03:07:29 -0700 | [diff] [blame] | 14 | #include <dm.h> |
Simon Glass | f1b35bd | 2023-11-12 13:55:09 -0700 | [diff] [blame] | 15 | #include <efi.h> |
| 16 | #include <efi_api.h> |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 17 | #include <errno.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 18 | #include <init.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 19 | #include <malloc.h> |
Simon Glass | f1b35bd | 2023-11-12 13:55:09 -0700 | [diff] [blame] | 20 | #include <sysreset.h> |
| 21 | #include <uuid.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 22 | #include <asm/global_data.h> |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 23 | #include <linux/err.h> |
| 24 | #include <linux/types.h> |
Simon Glass | f1b35bd | 2023-11-12 13:55:09 -0700 | [diff] [blame] | 25 | #include <asm/global_data.h> |
Simon Glass | 2d4b33e | 2021-12-29 11:57:36 -0700 | [diff] [blame] | 26 | #include <dm/device-internal.h> |
| 27 | #include <dm/lists.h> |
| 28 | #include <dm/root.h> |
Simon Glass | f1b35bd | 2023-11-12 13:55:09 -0700 | [diff] [blame] | 29 | #include <mapmem.h> |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 30 | |
| 31 | DECLARE_GLOBAL_DATA_PTR; |
| 32 | |
Simon Glass | 0a3e7b3 | 2021-11-03 21:09:09 -0600 | [diff] [blame] | 33 | int efi_info_get(enum efi_entry_t type, void **datap, int *sizep) |
| 34 | { |
| 35 | return -ENOSYS; |
| 36 | } |
| 37 | |
Simon Glass | ba04253 | 2022-01-04 03:51:12 -0700 | [diff] [blame] | 38 | int efi_get_mmap(struct efi_mem_desc **descp, int *sizep, uint *keyp, |
| 39 | int *desc_sizep, uint *versionp) |
| 40 | { |
| 41 | struct efi_priv *priv = efi_get_priv(); |
| 42 | struct efi_boot_services *boot = priv->sys_table->boottime; |
| 43 | efi_uintn_t size, desc_size, key; |
| 44 | struct efi_mem_desc *desc; |
| 45 | efi_status_t ret; |
| 46 | u32 version; |
| 47 | |
| 48 | /* Get the memory map so we can switch off EFI */ |
| 49 | size = 0; |
| 50 | ret = boot->get_memory_map(&size, NULL, &key, &desc_size, &version); |
| 51 | if (ret != EFI_BUFFER_TOO_SMALL) |
| 52 | return log_msg_ret("get", -ENOMEM); |
| 53 | |
| 54 | desc = malloc(size); |
| 55 | if (!desc) |
| 56 | return log_msg_ret("mem", -ENOMEM); |
| 57 | |
| 58 | ret = boot->get_memory_map(&size, desc, &key, &desc_size, &version); |
| 59 | if (ret) |
| 60 | return log_msg_ret("get", -EINVAL); |
| 61 | |
| 62 | *descp = desc; |
| 63 | *sizep = size; |
| 64 | *desc_sizep = desc_size; |
| 65 | *versionp = version; |
| 66 | *keyp = key; |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
Simon Glass | 2d4b33e | 2021-12-29 11:57:36 -0700 | [diff] [blame] | 71 | /** |
| 72 | * efi_bind_block() - bind a new block device to an EFI device |
| 73 | * |
| 74 | * Binds a new top-level EFI_MEDIA device as well as a child block device so |
| 75 | * that the block device can be accessed in U-Boot. |
| 76 | * |
| 77 | * The device can then be accessed using 'part list efi 0', 'fat ls efi 0:1', |
| 78 | * for example, just like any other interface type. |
| 79 | * |
| 80 | * @handle: handle of the controller on which this driver is installed |
| 81 | * @blkio: block io protocol proxied by this driver |
| 82 | * @device_path: EFI device path structure for this |
| 83 | * @len: Length of @device_path in bytes |
| 84 | * @devp: Returns the bound device |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 85 | * Return: 0 if OK, -ve on error |
Simon Glass | 2d4b33e | 2021-12-29 11:57:36 -0700 | [diff] [blame] | 86 | */ |
| 87 | int efi_bind_block(efi_handle_t handle, struct efi_block_io *blkio, |
| 88 | struct efi_device_path *device_path, int len, |
| 89 | struct udevice **devp) |
| 90 | { |
| 91 | struct efi_media_plat plat; |
| 92 | struct udevice *dev; |
| 93 | char name[18]; |
| 94 | int ret; |
| 95 | |
| 96 | plat.handle = handle; |
| 97 | plat.blkio = blkio; |
| 98 | plat.device_path = malloc(device_path->length); |
| 99 | if (!plat.device_path) |
| 100 | return log_msg_ret("path", -ENOMEM); |
| 101 | memcpy(plat.device_path, device_path, device_path->length); |
| 102 | ret = device_bind(dm_root(), DM_DRIVER_GET(efi_media), "efi_media", |
| 103 | &plat, ofnode_null(), &dev); |
| 104 | if (ret) |
| 105 | return log_msg_ret("bind", ret); |
| 106 | |
| 107 | snprintf(name, sizeof(name), "efi_media_%x", dev_seq(dev)); |
| 108 | device_set_name(dev, name); |
| 109 | *devp = dev; |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 114 | static efi_status_t setup_memory(struct efi_priv *priv) |
| 115 | { |
| 116 | struct efi_boot_services *boot = priv->boot; |
| 117 | efi_physical_addr_t addr; |
| 118 | efi_status_t ret; |
| 119 | int pages; |
| 120 | |
| 121 | /* |
| 122 | * Use global_data_ptr instead of gd since it is an assignment. There |
| 123 | * are very few assignments to global_data in U-Boot and this makes |
| 124 | * it easier to find them. |
| 125 | */ |
| 126 | global_data_ptr = efi_malloc(priv, sizeof(struct global_data), &ret); |
| 127 | if (!global_data_ptr) |
| 128 | return ret; |
| 129 | memset(gd, '\0', sizeof(*gd)); |
| 130 | |
Andy Yan | 1fa20e4d | 2017-07-24 17:43:34 +0800 | [diff] [blame] | 131 | gd->malloc_base = (ulong)efi_malloc(priv, CONFIG_VAL(SYS_MALLOC_F_LEN), |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 132 | &ret); |
| 133 | if (!gd->malloc_base) |
| 134 | return ret; |
| 135 | pages = CONFIG_EFI_RAM_SIZE >> 12; |
| 136 | |
| 137 | /* |
| 138 | * Don't allocate any memory above 4GB. U-Boot is a 32-bit application |
| 139 | * so we want it to load below 4GB. |
| 140 | */ |
| 141 | addr = 1ULL << 32; |
| 142 | ret = boot->allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, |
| 143 | priv->image_data_type, pages, &addr); |
| 144 | if (ret) { |
Simon Glass | 3a4e8a9 | 2021-12-29 11:57:49 -0700 | [diff] [blame] | 145 | log_info("(using pool %lx) ", ret); |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 146 | priv->ram_base = (ulong)efi_malloc(priv, CONFIG_EFI_RAM_SIZE, |
| 147 | &ret); |
| 148 | if (!priv->ram_base) |
| 149 | return ret; |
| 150 | priv->use_pool_for_malloc = true; |
| 151 | } else { |
Simon Glass | 3a4e8a9 | 2021-12-29 11:57:49 -0700 | [diff] [blame] | 152 | log_info("(using allocated RAM address %lx) ", (ulong)addr); |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 153 | priv->ram_base = addr; |
| 154 | } |
| 155 | gd->ram_size = pages << 12; |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
Simon Glass | 2d4b33e | 2021-12-29 11:57:36 -0700 | [diff] [blame] | 160 | /** |
| 161 | * free_memory() - Free memory used by the U-Boot app |
| 162 | * |
| 163 | * This frees memory allocated in setup_memory(), in preparation for returning |
| 164 | * to UEFI. It also zeroes the global_data pointer. |
| 165 | * |
| 166 | * @priv: Private EFI data |
| 167 | */ |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 168 | static void free_memory(struct efi_priv *priv) |
| 169 | { |
| 170 | struct efi_boot_services *boot = priv->boot; |
| 171 | |
| 172 | if (priv->use_pool_for_malloc) |
| 173 | efi_free(priv, (void *)priv->ram_base); |
| 174 | else |
| 175 | boot->free_pages(priv->ram_base, gd->ram_size >> 12); |
| 176 | |
| 177 | efi_free(priv, (void *)gd->malloc_base); |
| 178 | efi_free(priv, gd); |
| 179 | global_data_ptr = NULL; |
| 180 | } |
| 181 | |
| 182 | /** |
Simon Glass | 2d4b33e | 2021-12-29 11:57:36 -0700 | [diff] [blame] | 183 | * devpath_is_partition() - Figure out if a device path is a partition |
| 184 | * |
| 185 | * Checks if a device path refers to a partition on some media device. This |
| 186 | * works by checking for a valid partition number in a hard-driver media device |
| 187 | * as the final component of the device path. |
| 188 | * |
| 189 | * @path: device path |
| 190 | * Return: true if a partition, false if not |
| 191 | * (e.g. it might be media which contains partitions) |
| 192 | */ |
| 193 | static bool devpath_is_partition(const struct efi_device_path *path) |
| 194 | { |
| 195 | const struct efi_device_path *p; |
Heinrich Schuchardt | e353ba1 | 2022-04-25 23:21:20 +0200 | [diff] [blame] | 196 | bool was_part = false; |
Simon Glass | 2d4b33e | 2021-12-29 11:57:36 -0700 | [diff] [blame] | 197 | |
| 198 | for (p = path; p->type != DEVICE_PATH_TYPE_END; |
| 199 | p = (void *)p + p->length) { |
| 200 | was_part = false; |
| 201 | if (p->type == DEVICE_PATH_TYPE_MEDIA_DEVICE && |
| 202 | p->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) { |
| 203 | struct efi_device_path_hard_drive_path *hd = |
| 204 | (void *)path; |
| 205 | |
| 206 | if (hd->partition_number) |
| 207 | was_part = true; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return was_part; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * setup_block() - Find all block devices and setup EFI devices for them |
| 216 | * |
| 217 | * Partitions are ignored, since U-Boot has partition handling. Errors with |
| 218 | * particular devices produce a warning but execution continues to try to |
| 219 | * find others. |
| 220 | * |
| 221 | * Return: 0 if found, -ENOSYS if there is no boot-services table, -ENOTSUPP |
| 222 | * if a required protocol is not supported |
| 223 | */ |
| 224 | static int setup_block(void) |
| 225 | { |
| 226 | efi_guid_t efi_blkio_guid = EFI_BLOCK_IO_PROTOCOL_GUID; |
| 227 | efi_guid_t efi_devpath_guid = EFI_DEVICE_PATH_PROTOCOL_GUID; |
| 228 | efi_guid_t efi_pathutil_guid = EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID; |
| 229 | efi_guid_t efi_pathtext_guid = EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID; |
| 230 | struct efi_boot_services *boot = efi_get_boot(); |
| 231 | struct efi_device_path_utilities_protocol *util; |
| 232 | struct efi_device_path_to_text_protocol *text; |
| 233 | struct efi_device_path *path; |
| 234 | struct efi_block_io *blkio; |
| 235 | efi_uintn_t num_handles; |
| 236 | efi_handle_t *handle; |
| 237 | int ret, i; |
| 238 | |
| 239 | if (!boot) |
| 240 | return log_msg_ret("sys", -ENOSYS); |
| 241 | |
| 242 | /* Find all devices which support the block I/O protocol */ |
| 243 | ret = boot->locate_handle_buffer(BY_PROTOCOL, &efi_blkio_guid, NULL, |
| 244 | &num_handles, &handle); |
| 245 | if (ret) |
| 246 | return log_msg_ret("loc", -ENOTSUPP); |
| 247 | log_debug("Found %d handles:\n", (int)num_handles); |
| 248 | |
| 249 | /* We need to look up the path size and convert it to text */ |
| 250 | ret = boot->locate_protocol(&efi_pathutil_guid, NULL, (void **)&util); |
| 251 | if (ret) |
| 252 | return log_msg_ret("util", -ENOTSUPP); |
| 253 | ret = boot->locate_protocol(&efi_pathtext_guid, NULL, (void **)&text); |
| 254 | if (ret) |
| 255 | return log_msg_ret("text", -ENOTSUPP); |
| 256 | |
| 257 | for (i = 0; i < num_handles; i++) { |
| 258 | struct udevice *dev; |
| 259 | const u16 *name; |
| 260 | bool is_part; |
| 261 | int len; |
| 262 | |
| 263 | ret = boot->handle_protocol(handle[i], &efi_devpath_guid, |
| 264 | (void **)&path); |
| 265 | if (ret) { |
| 266 | log_warning("- devpath %d failed (ret=%d)\n", i, ret); |
| 267 | continue; |
| 268 | } |
| 269 | |
| 270 | ret = boot->handle_protocol(handle[i], &efi_blkio_guid, |
| 271 | (void **)&blkio); |
| 272 | if (ret) { |
| 273 | log_warning("- blkio %d failed (ret=%d)\n", i, ret); |
| 274 | continue; |
| 275 | } |
| 276 | |
| 277 | name = text->convert_device_path_to_text(path, true, false); |
| 278 | is_part = devpath_is_partition(path); |
| 279 | |
| 280 | if (!is_part) { |
| 281 | len = util->get_device_path_size(path); |
| 282 | ret = efi_bind_block(handle[i], blkio, path, len, &dev); |
| 283 | if (ret) { |
| 284 | log_warning("- blkio bind %d failed (ret=%d)\n", |
| 285 | i, ret); |
| 286 | continue; |
| 287 | } |
| 288 | } else { |
| 289 | dev = NULL; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Show the device name if we created one. Otherwise indicate |
| 294 | * that it is a partition. |
| 295 | */ |
| 296 | printf("%2d: %-12s %ls\n", i, dev ? dev->name : "<partition>", |
| 297 | name); |
| 298 | } |
| 299 | boot->free_pool(handle); |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * dm_scan_other() - Scan for UEFI devices that should be available to U-Boot |
| 306 | * |
| 307 | * This sets up block devices within U-Boot for those found in UEFI. With this, |
| 308 | * U-Boot can access those devices |
| 309 | * |
| 310 | * @pre_reloc_only: true to only bind pre-relocation devices (ignored) |
| 311 | * Returns: 0 on success, -ve on error |
| 312 | */ |
| 313 | int dm_scan_other(bool pre_reloc_only) |
| 314 | { |
| 315 | if (gd->flags & GD_FLG_RELOC) { |
| 316 | int ret; |
| 317 | |
| 318 | ret = setup_block(); |
| 319 | if (ret) |
| 320 | return ret; |
| 321 | } |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
Simon Glass | f1b35bd | 2023-11-12 13:55:09 -0700 | [diff] [blame] | 326 | static void scan_tables(struct efi_system_table *sys_table) |
| 327 | { |
| 328 | efi_guid_t acpi = EFI_ACPI_TABLE_GUID; |
| 329 | uint i; |
| 330 | |
| 331 | for (i = 0; i < sys_table->nr_tables; i++) { |
| 332 | struct efi_configuration_table *tab = &sys_table->tables[i]; |
| 333 | |
| 334 | if (!memcmp(&tab->guid, &acpi, sizeof(efi_guid_t))) |
| 335 | gd_set_acpi_start(map_to_sysmem(tab->table)); |
| 336 | } |
| 337 | } |
| 338 | |
Simon Glass | 2d4b33e | 2021-12-29 11:57:36 -0700 | [diff] [blame] | 339 | /** |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 340 | * efi_main() - Start an EFI image |
| 341 | * |
| 342 | * This function is called by our EFI start-up code. It handles running |
| 343 | * U-Boot. If it returns, EFI will continue. Another way to get back to EFI |
| 344 | * is via reset_cpu(). |
| 345 | */ |
Ivan Gorinov | 4123662 | 2018-06-13 17:27:39 -0700 | [diff] [blame] | 346 | efi_status_t EFIAPI efi_main(efi_handle_t image, |
| 347 | struct efi_system_table *sys_table) |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 348 | { |
| 349 | struct efi_priv local_priv, *priv = &local_priv; |
| 350 | efi_status_t ret; |
| 351 | |
| 352 | /* Set up access to EFI data structures */ |
Simon Glass | a008d68 | 2021-12-29 11:57:47 -0700 | [diff] [blame] | 353 | ret = efi_init(priv, "App", image, sys_table); |
| 354 | if (ret) { |
| 355 | printf("Failed to set up U-Boot: err=%lx\n", ret); |
| 356 | return ret; |
| 357 | } |
Simon Glass | b13771f | 2021-12-29 11:57:45 -0700 | [diff] [blame] | 358 | efi_set_priv(priv); |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 359 | |
| 360 | /* |
| 361 | * Set up the EFI debug UART so that printf() works. This is |
| 362 | * implemented in the EFI serial driver, serial_efi.c. The application |
| 363 | * can use printf() freely. |
| 364 | */ |
| 365 | debug_uart_init(); |
| 366 | |
| 367 | ret = setup_memory(priv); |
| 368 | if (ret) { |
| 369 | printf("Failed to set up memory: ret=%lx\n", ret); |
| 370 | return ret; |
| 371 | } |
| 372 | |
Simon Glass | f1b35bd | 2023-11-12 13:55:09 -0700 | [diff] [blame] | 373 | scan_tables(priv->sys_table); |
| 374 | |
Simon Glass | 029f48e | 2022-01-04 03:51:10 -0700 | [diff] [blame] | 375 | /* |
| 376 | * We could store the EFI memory map here, but it changes all the time, |
| 377 | * so this is only useful for debugging. |
| 378 | * |
| 379 | * ret = efi_store_memory_map(priv); |
| 380 | * if (ret) |
| 381 | * return ret; |
| 382 | */ |
| 383 | |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 384 | printf("starting\n"); |
| 385 | |
| 386 | board_init_f(GD_FLG_SKIP_RELOC); |
| 387 | board_init_r(NULL, 0); |
| 388 | free_memory(priv); |
| 389 | |
| 390 | return EFI_SUCCESS; |
| 391 | } |
| 392 | |
Bin Meng | 268bc9e | 2018-07-19 03:07:29 -0700 | [diff] [blame] | 393 | static void efi_exit(void) |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 394 | { |
Simon Glass | b13771f | 2021-12-29 11:57:45 -0700 | [diff] [blame] | 395 | struct efi_priv *priv = efi_get_priv(); |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 396 | |
| 397 | free_memory(priv); |
| 398 | printf("U-Boot EFI exiting\n"); |
| 399 | priv->boot->exit(priv->parent_image, EFI_SUCCESS, 0, NULL); |
| 400 | } |
Bin Meng | 268bc9e | 2018-07-19 03:07:29 -0700 | [diff] [blame] | 401 | |
| 402 | static int efi_sysreset_request(struct udevice *dev, enum sysreset_t type) |
| 403 | { |
| 404 | efi_exit(); |
| 405 | |
| 406 | return -EINPROGRESS; |
| 407 | } |
| 408 | |
| 409 | static const struct udevice_id efi_sysreset_ids[] = { |
| 410 | { .compatible = "efi,reset" }, |
| 411 | { } |
| 412 | }; |
| 413 | |
| 414 | static struct sysreset_ops efi_sysreset_ops = { |
| 415 | .request = efi_sysreset_request, |
| 416 | }; |
| 417 | |
| 418 | U_BOOT_DRIVER(efi_sysreset) = { |
| 419 | .name = "efi-sysreset", |
| 420 | .id = UCLASS_SYSRESET, |
| 421 | .of_match = efi_sysreset_ids, |
| 422 | .ops = &efi_sysreset_ops, |
Bin Meng | 268bc9e | 2018-07-19 03:07:29 -0700 | [diff] [blame] | 423 | }; |