Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2 | /* |
Heinrich Schuchardt | da8f900 | 2019-07-03 20:27:24 +0200 | [diff] [blame] | 3 | * EFI application boot time services |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 4 | * |
Heinrich Schuchardt | da8f900 | 2019-07-03 20:27:24 +0200 | [diff] [blame] | 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 8 | #include <common.h> |
Ilias Apalodimas | 4cc0632 | 2020-10-22 01:04:20 +0300 | [diff] [blame] | 9 | #include <bootm.h> |
Heinrich Schuchardt | 368ca64 | 2017-10-05 16:14:14 +0200 | [diff] [blame] | 10 | #include <div64.h> |
Ilias Apalodimas | c539fb8 | 2020-10-22 01:04:21 +0300 | [diff] [blame] | 11 | #include <dm/device.h> |
| 12 | #include <dm/root.h> |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 13 | #include <efi_loader.h> |
Simon Glass | 8f3f761 | 2019-11-14 12:57:42 -0700 | [diff] [blame] | 14 | #include <irq_func.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 16 | #include <malloc.h> |
Heinrich Schuchardt | 3758752 | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 17 | #include <pe.h> |
Ilias Apalodimas | 4cc0632 | 2020-10-22 01:04:20 +0300 | [diff] [blame] | 18 | #include <time.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 19 | #include <u-boot/crc.h> |
Ilias Apalodimas | c539fb8 | 2020-10-22 01:04:21 +0300 | [diff] [blame] | 20 | #include <usb.h> |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 21 | #include <watchdog.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 22 | #include <asm/global_data.h> |
AKASHI Takahiro | 4cbba2b | 2021-07-20 14:57:02 +0900 | [diff] [blame] | 23 | #include <asm/setjmp.h> |
Ilias Apalodimas | 4cc0632 | 2020-10-22 01:04:20 +0300 | [diff] [blame] | 24 | #include <linux/libfdt_env.h> |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 25 | |
| 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
Heinrich Schuchardt | 0c150ca | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 28 | /* Task priority level */ |
Heinrich Schuchardt | f8d4ec3 | 2017-11-06 21:17:47 +0100 | [diff] [blame] | 29 | static efi_uintn_t efi_tpl = TPL_APPLICATION; |
Heinrich Schuchardt | 0c150ca | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 30 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 31 | /* This list contains all the EFI objects our payload has access to */ |
| 32 | LIST_HEAD(efi_obj_list); |
| 33 | |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 34 | /* List of all events */ |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 35 | __efi_runtime_data LIST_HEAD(efi_events); |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 36 | |
Heinrich Schuchardt | 35c0cf6 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 37 | /* List of queued events */ |
Bin Meng | d89dea3 | 2023-04-05 20:15:17 +0800 | [diff] [blame] | 38 | static LIST_HEAD(efi_event_queue); |
Heinrich Schuchardt | 35c0cf6 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 39 | |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 40 | /* Flag to disable timer activity in ExitBootServices() */ |
| 41 | static bool timers_enabled = true; |
| 42 | |
Heinrich Schuchardt | 628dbd7 | 2020-11-12 21:26:28 +0100 | [diff] [blame] | 43 | /* Flag used by the selftest to avoid detaching devices in ExitBootServices() */ |
| 44 | bool efi_st_keep_devices; |
| 45 | |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 46 | /* List of all events registered by RegisterProtocolNotify() */ |
Bin Meng | d89dea3 | 2023-04-05 20:15:17 +0800 | [diff] [blame] | 47 | static LIST_HEAD(efi_register_notify_events); |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 48 | |
Heinrich Schuchardt | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 49 | /* Handle of the currently executing image */ |
| 50 | static efi_handle_t current_image; |
| 51 | |
Heinrich Schuchardt | f277d94 | 2020-09-10 12:22:54 +0200 | [diff] [blame] | 52 | #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 53 | /* |
Heinrich Schuchardt | f277d94 | 2020-09-10 12:22:54 +0200 | [diff] [blame] | 54 | * The "gd" pointer lives in a register on ARM and RISC-V that we declare |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 55 | * fixed when compiling U-Boot. However, the payload does not know about that |
| 56 | * restriction so we need to manually swap its and our view of that register on |
| 57 | * EFI callback entry/exit. |
| 58 | */ |
Heinrich Schuchardt | 1a3732c | 2020-05-27 01:58:30 +0200 | [diff] [blame] | 59 | static volatile gd_t *efi_gd, *app_gd; |
Simon Glass | cdfe696 | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 60 | #endif |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 61 | |
Ilias Apalodimas | b09ecf1 | 2023-07-24 13:17:36 +0300 | [diff] [blame] | 62 | static efi_status_t efi_uninstall_protocol |
| 63 | (efi_handle_t handle, const efi_guid_t *protocol, |
Ilias Apalodimas | c76eade | 2023-08-24 17:21:09 +0300 | [diff] [blame] | 64 | void *protocol_interface, bool preserve); |
Ilias Apalodimas | b09ecf1 | 2023-07-24 13:17:36 +0300 | [diff] [blame] | 65 | |
Heinrich Schuchardt | 7250623 | 2019-02-09 14:10:39 +0100 | [diff] [blame] | 66 | /* 1 if inside U-Boot code, 0 if inside EFI payload code */ |
| 67 | static int entry_count = 1; |
Rob Clark | e7896c3 | 2017-07-27 08:04:19 -0400 | [diff] [blame] | 68 | static int nesting_level; |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 69 | /* GUID of the device tree table */ |
| 70 | const efi_guid_t efi_guid_fdt = EFI_FDT_GUID; |
Heinrich Schuchardt | 760255f | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 71 | /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */ |
| 72 | const efi_guid_t efi_guid_driver_binding_protocol = |
| 73 | EFI_DRIVER_BINDING_PROTOCOL_GUID; |
Rob Clark | 86789d5 | 2017-07-27 08:04:18 -0400 | [diff] [blame] | 74 | |
Heinrich Schuchardt | 672d99e | 2018-02-18 15:17:51 +0100 | [diff] [blame] | 75 | /* event group ExitBootServices() invoked */ |
| 76 | const efi_guid_t efi_guid_event_group_exit_boot_services = |
| 77 | EFI_EVENT_GROUP_EXIT_BOOT_SERVICES; |
Heinrich Schuchardt | 44772c4 | 2021-11-16 18:46:27 +0100 | [diff] [blame] | 78 | /* event group before ExitBootServices() invoked */ |
| 79 | const efi_guid_t efi_guid_event_group_before_exit_boot_services = |
| 80 | EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES; |
Heinrich Schuchardt | 672d99e | 2018-02-18 15:17:51 +0100 | [diff] [blame] | 81 | /* event group SetVirtualAddressMap() invoked */ |
| 82 | const efi_guid_t efi_guid_event_group_virtual_address_change = |
| 83 | EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE; |
| 84 | /* event group memory map changed */ |
| 85 | const efi_guid_t efi_guid_event_group_memory_map_change = |
| 86 | EFI_EVENT_GROUP_MEMORY_MAP_CHANGE; |
| 87 | /* event group boot manager about to boot */ |
| 88 | const efi_guid_t efi_guid_event_group_ready_to_boot = |
| 89 | EFI_EVENT_GROUP_READY_TO_BOOT; |
| 90 | /* event group ResetSystem() invoked (before ExitBootServices) */ |
| 91 | const efi_guid_t efi_guid_event_group_reset_system = |
| 92 | EFI_EVENT_GROUP_RESET_SYSTEM; |
Heinrich Schuchardt | 485956d | 2020-12-04 03:33:41 +0100 | [diff] [blame] | 93 | /* GUIDs of the Load File and Load File2 protocols */ |
| 94 | const efi_guid_t efi_guid_load_file_protocol = EFI_LOAD_FILE_PROTOCOL_GUID; |
| 95 | const efi_guid_t efi_guid_load_file2_protocol = EFI_LOAD_FILE2_PROTOCOL_GUID; |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 96 | /* GUID of the SMBIOS table */ |
| 97 | const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID; |
Heinrich Schuchardt | 672d99e | 2018-02-18 15:17:51 +0100 | [diff] [blame] | 98 | |
Heinrich Schuchardt | b7cb8b4 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 99 | static efi_status_t EFIAPI efi_disconnect_controller( |
| 100 | efi_handle_t controller_handle, |
| 101 | efi_handle_t driver_image_handle, |
| 102 | efi_handle_t child_handle); |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 103 | |
Ilias Apalodimas | 1e10d62 | 2023-06-20 09:19:28 +0300 | [diff] [blame] | 104 | static |
| 105 | efi_status_t EFIAPI efi_connect_controller(efi_handle_t controller_handle, |
| 106 | efi_handle_t *driver_image_handle, |
| 107 | struct efi_device_path *remain_device_path, |
| 108 | bool recursive); |
| 109 | |
Rob Clark | 86789d5 | 2017-07-27 08:04:18 -0400 | [diff] [blame] | 110 | /* Called on every callback entry */ |
| 111 | int __efi_entry_check(void) |
| 112 | { |
| 113 | int ret = entry_count++ == 0; |
Heinrich Schuchardt | f277d94 | 2020-09-10 12:22:54 +0200 | [diff] [blame] | 114 | #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) |
Rob Clark | 86789d5 | 2017-07-27 08:04:18 -0400 | [diff] [blame] | 115 | assert(efi_gd); |
| 116 | app_gd = gd; |
Heinrich Schuchardt | 1a3732c | 2020-05-27 01:58:30 +0200 | [diff] [blame] | 117 | set_gd(efi_gd); |
Rob Clark | 86789d5 | 2017-07-27 08:04:18 -0400 | [diff] [blame] | 118 | #endif |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | /* Called on every callback exit */ |
| 123 | int __efi_exit_check(void) |
| 124 | { |
| 125 | int ret = --entry_count == 0; |
Heinrich Schuchardt | f277d94 | 2020-09-10 12:22:54 +0200 | [diff] [blame] | 126 | #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) |
Heinrich Schuchardt | 1a3732c | 2020-05-27 01:58:30 +0200 | [diff] [blame] | 127 | set_gd(app_gd); |
Rob Clark | 86789d5 | 2017-07-27 08:04:18 -0400 | [diff] [blame] | 128 | #endif |
| 129 | return ret; |
| 130 | } |
| 131 | |
Heinrich Schuchardt | c594941 | 2020-07-18 09:53:01 +0200 | [diff] [blame] | 132 | /** |
| 133 | * efi_save_gd() - save global data register |
| 134 | * |
Heinrich Schuchardt | f277d94 | 2020-09-10 12:22:54 +0200 | [diff] [blame] | 135 | * On the ARM and RISC-V architectures gd is mapped to a fixed register. |
Heinrich Schuchardt | c594941 | 2020-07-18 09:53:01 +0200 | [diff] [blame] | 136 | * As this register may be overwritten by an EFI payload we save it here |
| 137 | * and restore it on every callback entered. |
| 138 | * |
| 139 | * This function is called after relocation from initr_reloc_global_data(). |
| 140 | */ |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 141 | void efi_save_gd(void) |
| 142 | { |
Heinrich Schuchardt | f277d94 | 2020-09-10 12:22:54 +0200 | [diff] [blame] | 143 | #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 144 | efi_gd = gd; |
Simon Glass | cdfe696 | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 145 | #endif |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Heinrich Schuchardt | c594941 | 2020-07-18 09:53:01 +0200 | [diff] [blame] | 148 | /** |
| 149 | * efi_restore_gd() - restore global data register |
| 150 | * |
Heinrich Schuchardt | f277d94 | 2020-09-10 12:22:54 +0200 | [diff] [blame] | 151 | * On the ARM and RISC-V architectures gd is mapped to a fixed register. |
Heinrich Schuchardt | c594941 | 2020-07-18 09:53:01 +0200 | [diff] [blame] | 152 | * Restore it after returning from the UEFI world to the value saved via |
| 153 | * efi_save_gd(). |
Rob Clark | 86789d5 | 2017-07-27 08:04:18 -0400 | [diff] [blame] | 154 | */ |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 155 | void efi_restore_gd(void) |
| 156 | { |
Heinrich Schuchardt | f277d94 | 2020-09-10 12:22:54 +0200 | [diff] [blame] | 157 | #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 158 | /* Only restore if we're already in EFI context */ |
| 159 | if (!efi_gd) |
| 160 | return; |
Heinrich Schuchardt | 1a3732c | 2020-05-27 01:58:30 +0200 | [diff] [blame] | 161 | set_gd(efi_gd); |
Simon Glass | cdfe696 | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 162 | #endif |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 165 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 166 | * indent_string() - returns a string for indenting with two spaces per level |
| 167 | * @level: indent level |
Heinrich Schuchardt | 091cf43 | 2018-01-24 19:21:36 +0100 | [diff] [blame] | 168 | * |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 169 | * A maximum of ten indent levels is supported. Higher indent levels will be |
| 170 | * truncated. |
| 171 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 172 | * Return: A string for indenting with two spaces per level is |
| 173 | * returned. |
Rob Clark | e7896c3 | 2017-07-27 08:04:19 -0400 | [diff] [blame] | 174 | */ |
| 175 | static const char *indent_string(int level) |
| 176 | { |
| 177 | const char *indent = " "; |
| 178 | const int max = strlen(indent); |
Heinrich Schuchardt | 9106459 | 2018-02-18 15:17:49 +0100 | [diff] [blame] | 179 | |
Rob Clark | e7896c3 | 2017-07-27 08:04:19 -0400 | [diff] [blame] | 180 | level = min(max, level * 2); |
| 181 | return &indent[max - level]; |
| 182 | } |
| 183 | |
Heinrich Schuchardt | 4d66489 | 2017-08-18 17:45:16 +0200 | [diff] [blame] | 184 | const char *__efi_nesting(void) |
| 185 | { |
| 186 | return indent_string(nesting_level); |
| 187 | } |
| 188 | |
Rob Clark | e7896c3 | 2017-07-27 08:04:19 -0400 | [diff] [blame] | 189 | const char *__efi_nesting_inc(void) |
| 190 | { |
| 191 | return indent_string(nesting_level++); |
| 192 | } |
| 193 | |
| 194 | const char *__efi_nesting_dec(void) |
| 195 | { |
| 196 | return indent_string(--nesting_level); |
| 197 | } |
| 198 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 199 | /** |
Heinrich Schuchardt | 35c0cf6 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 200 | * efi_event_is_queued() - check if an event is queued |
| 201 | * |
| 202 | * @event: event |
| 203 | * Return: true if event is queued |
| 204 | */ |
| 205 | static bool efi_event_is_queued(struct efi_event *event) |
| 206 | { |
| 207 | return !!event->queue_link.next; |
| 208 | } |
| 209 | |
| 210 | /** |
Ilias Apalodimas | c76eade | 2023-08-24 17:21:09 +0300 | [diff] [blame] | 211 | * efi_purge_handle() - Clean the deleted handle from the various lists |
| 212 | * @handle: handle to remove |
| 213 | * |
| 214 | * Return: status code |
| 215 | */ |
| 216 | static efi_status_t efi_purge_handle(efi_handle_t handle) |
| 217 | { |
| 218 | struct efi_register_notify_event *item; |
| 219 | |
| 220 | if (!list_empty(&handle->protocols)) |
| 221 | return EFI_ACCESS_DENIED; |
| 222 | /* The handle is about to be freed. Remove it from events */ |
| 223 | list_for_each_entry(item, &efi_register_notify_events, link) { |
| 224 | struct efi_protocol_notification *hitem, *hnext; |
| 225 | |
| 226 | list_for_each_entry_safe(hitem, hnext, &item->handles, link) { |
| 227 | if (handle == hitem->handle) { |
| 228 | list_del(&hitem->link); |
| 229 | free(hitem); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | /* The last protocol has been removed, delete the handle. */ |
| 234 | list_del(&handle->link); |
| 235 | free(handle); |
| 236 | |
| 237 | return EFI_SUCCESS; |
| 238 | } |
| 239 | |
| 240 | /** |
Heinrich Schuchardt | 35c0cf6 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 241 | * efi_process_event_queue() - process event queue |
| 242 | */ |
| 243 | static void efi_process_event_queue(void) |
| 244 | { |
| 245 | while (!list_empty(&efi_event_queue)) { |
| 246 | struct efi_event *event; |
| 247 | efi_uintn_t old_tpl; |
| 248 | |
| 249 | event = list_first_entry(&efi_event_queue, struct efi_event, |
| 250 | queue_link); |
| 251 | if (efi_tpl >= event->notify_tpl) |
| 252 | return; |
| 253 | list_del(&event->queue_link); |
| 254 | event->queue_link.next = NULL; |
| 255 | event->queue_link.prev = NULL; |
| 256 | /* Events must be executed at the event's TPL */ |
| 257 | old_tpl = efi_tpl; |
| 258 | efi_tpl = event->notify_tpl; |
| 259 | EFI_CALL_VOID(event->notify_function(event, |
| 260 | event->notify_context)); |
| 261 | efi_tpl = old_tpl; |
| 262 | if (event->type == EVT_NOTIFY_SIGNAL) |
| 263 | event->is_signaled = 0; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 268 | * efi_queue_event() - queue an EFI event |
| 269 | * @event: event to signal |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 270 | * |
| 271 | * This function queues the notification function of the event for future |
| 272 | * execution. |
| 273 | * |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 274 | */ |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 275 | static void efi_queue_event(struct efi_event *event) |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 276 | { |
Heinrich Schuchardt | ec94690 | 2020-03-06 21:56:10 +0100 | [diff] [blame] | 277 | struct efi_event *item; |
Heinrich Schuchardt | 35c0cf6 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 278 | |
| 279 | if (!event->notify_function) |
| 280 | return; |
| 281 | |
| 282 | if (!efi_event_is_queued(event)) { |
| 283 | /* |
| 284 | * Events must be notified in order of decreasing task priority |
| 285 | * level. Insert the new event accordingly. |
| 286 | */ |
| 287 | list_for_each_entry(item, &efi_event_queue, queue_link) { |
| 288 | if (item->notify_tpl < event->notify_tpl) { |
| 289 | list_add_tail(&event->queue_link, |
| 290 | &item->queue_link); |
| 291 | event = NULL; |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | if (event) |
| 296 | list_add_tail(&event->queue_link, &efi_event_queue); |
Heinrich Schuchardt | 3ef4c85 | 2020-12-28 00:25:34 +0100 | [diff] [blame] | 297 | efi_process_event_queue(); |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 301 | /** |
Heinrich Schuchardt | 7f0f005 | 2018-07-02 12:53:52 +0200 | [diff] [blame] | 302 | * is_valid_tpl() - check if the task priority level is valid |
| 303 | * |
| 304 | * @tpl: TPL level to check |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 305 | * Return: status code |
Heinrich Schuchardt | 7f0f005 | 2018-07-02 12:53:52 +0200 | [diff] [blame] | 306 | */ |
Heinrich Schuchardt | 8126a1e | 2023-02-10 08:50:06 +0100 | [diff] [blame] | 307 | static efi_status_t is_valid_tpl(efi_uintn_t tpl) |
Heinrich Schuchardt | 7f0f005 | 2018-07-02 12:53:52 +0200 | [diff] [blame] | 308 | { |
| 309 | switch (tpl) { |
| 310 | case TPL_APPLICATION: |
| 311 | case TPL_CALLBACK: |
| 312 | case TPL_NOTIFY: |
Heinrich Schuchardt | 7f0f005 | 2018-07-02 12:53:52 +0200 | [diff] [blame] | 313 | return EFI_SUCCESS; |
| 314 | default: |
| 315 | return EFI_INVALID_PARAMETER; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 320 | * efi_signal_event() - signal an EFI event |
| 321 | * @event: event to signal |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 322 | * |
Heinrich Schuchardt | 9de0fb7 | 2020-12-28 00:59:09 +0100 | [diff] [blame] | 323 | * This function signals an event. If the event belongs to an event group, all |
| 324 | * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL, |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 325 | * their notification function is queued. |
| 326 | * |
| 327 | * For the SignalEvent service see efi_signal_event_ext. |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 328 | */ |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 329 | void efi_signal_event(struct efi_event *event) |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 330 | { |
Heinrich Schuchardt | 0b4de56 | 2019-06-06 01:51:50 +0200 | [diff] [blame] | 331 | if (event->is_signaled) |
| 332 | return; |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 333 | if (event->group) { |
| 334 | struct efi_event *evt; |
| 335 | |
| 336 | /* |
| 337 | * The signaled state has to set before executing any |
| 338 | * notification function |
| 339 | */ |
| 340 | list_for_each_entry(evt, &efi_events, link) { |
| 341 | if (!evt->group || guidcmp(evt->group, event->group)) |
| 342 | continue; |
| 343 | if (evt->is_signaled) |
| 344 | continue; |
| 345 | evt->is_signaled = true; |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 346 | } |
| 347 | list_for_each_entry(evt, &efi_events, link) { |
| 348 | if (!evt->group || guidcmp(evt->group, event->group)) |
| 349 | continue; |
Heinrich Schuchardt | 35c0cf6 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 350 | efi_queue_event(evt); |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 351 | } |
Heinrich Schuchardt | 66ddc2e | 2019-05-05 00:07:34 +0200 | [diff] [blame] | 352 | } else { |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 353 | event->is_signaled = true; |
Heinrich Schuchardt | 35c0cf6 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 354 | efi_queue_event(event); |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 358 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 359 | * efi_raise_tpl() - raise the task priority level |
| 360 | * @new_tpl: new value of the task priority level |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 361 | * |
| 362 | * This function implements the RaiseTpl service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 363 | * |
| 364 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 365 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 366 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 367 | * Return: old value of the task priority level |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 368 | */ |
Heinrich Schuchardt | f8d4ec3 | 2017-11-06 21:17:47 +0100 | [diff] [blame] | 369 | static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 370 | { |
Heinrich Schuchardt | f8d4ec3 | 2017-11-06 21:17:47 +0100 | [diff] [blame] | 371 | efi_uintn_t old_tpl = efi_tpl; |
Heinrich Schuchardt | 0c150ca | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 372 | |
xypron.glpk@gmx.de | 48df209 | 2017-07-18 20:17:19 +0200 | [diff] [blame] | 373 | EFI_ENTRY("0x%zx", new_tpl); |
Heinrich Schuchardt | 0c150ca | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 374 | |
| 375 | if (new_tpl < efi_tpl) |
Heinrich Schuchardt | 952e206 | 2019-05-05 11:56:23 +0200 | [diff] [blame] | 376 | EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__); |
Heinrich Schuchardt | 0c150ca | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 377 | efi_tpl = new_tpl; |
| 378 | if (efi_tpl > TPL_HIGH_LEVEL) |
| 379 | efi_tpl = TPL_HIGH_LEVEL; |
| 380 | |
| 381 | EFI_EXIT(EFI_SUCCESS); |
| 382 | return old_tpl; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 383 | } |
| 384 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 385 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 386 | * efi_restore_tpl() - lower the task priority level |
| 387 | * @old_tpl: value of the task priority level to be restored |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 388 | * |
| 389 | * This function implements the RestoreTpl service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 390 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 391 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 392 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 393 | */ |
Heinrich Schuchardt | f8d4ec3 | 2017-11-06 21:17:47 +0100 | [diff] [blame] | 394 | static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 395 | { |
xypron.glpk@gmx.de | 48df209 | 2017-07-18 20:17:19 +0200 | [diff] [blame] | 396 | EFI_ENTRY("0x%zx", old_tpl); |
Heinrich Schuchardt | 0c150ca | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 397 | |
| 398 | if (old_tpl > efi_tpl) |
Heinrich Schuchardt | 952e206 | 2019-05-05 11:56:23 +0200 | [diff] [blame] | 399 | EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__); |
Heinrich Schuchardt | 0c150ca | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 400 | efi_tpl = old_tpl; |
| 401 | if (efi_tpl > TPL_HIGH_LEVEL) |
| 402 | efi_tpl = TPL_HIGH_LEVEL; |
| 403 | |
Heinrich Schuchardt | 59b2095 | 2018-03-24 18:40:21 +0100 | [diff] [blame] | 404 | /* |
| 405 | * Lowering the TPL may have made queued events eligible for execution. |
| 406 | */ |
| 407 | efi_timer_check(); |
| 408 | |
Heinrich Schuchardt | 0c150ca | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 409 | EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 410 | } |
| 411 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 412 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 413 | * efi_allocate_pages_ext() - allocate memory pages |
| 414 | * @type: type of allocation to be performed |
| 415 | * @memory_type: usage type of the allocated memory |
| 416 | * @pages: number of pages to be allocated |
| 417 | * @memory: allocated memory |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 418 | * |
| 419 | * This function implements the AllocatePages service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 420 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 421 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 422 | * details. |
| 423 | * |
| 424 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 425 | */ |
Masahiro Yamada | b2a05c1 | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 426 | static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 427 | efi_uintn_t pages, |
Masahiro Yamada | b2a05c1 | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 428 | uint64_t *memory) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 429 | { |
| 430 | efi_status_t r; |
| 431 | |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 432 | EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 433 | r = efi_allocate_pages(type, memory_type, pages, memory); |
| 434 | return EFI_EXIT(r); |
| 435 | } |
| 436 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 437 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 438 | * efi_free_pages_ext() - Free memory pages. |
| 439 | * @memory: start of the memory area to be freed |
| 440 | * @pages: number of pages to be freed |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 441 | * |
| 442 | * This function implements the FreePages service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 443 | * |
| 444 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 445 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 446 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 447 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 448 | */ |
Masahiro Yamada | b2a05c1 | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 449 | static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 450 | efi_uintn_t pages) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 451 | { |
| 452 | efi_status_t r; |
| 453 | |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 454 | EFI_ENTRY("%llx, 0x%zx", memory, pages); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 455 | r = efi_free_pages(memory, pages); |
| 456 | return EFI_EXIT(r); |
| 457 | } |
| 458 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 459 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 460 | * efi_get_memory_map_ext() - get map describing memory usage |
| 461 | * @memory_map_size: on entry the size, in bytes, of the memory map buffer, |
| 462 | * on exit the size of the copied memory map |
| 463 | * @memory_map: buffer to which the memory map is written |
| 464 | * @map_key: key for the memory map |
| 465 | * @descriptor_size: size of an individual memory descriptor |
| 466 | * @descriptor_version: version number of the memory descriptor structure |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 467 | * |
| 468 | * This function implements the GetMemoryMap service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 469 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 470 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 471 | * details. |
| 472 | * |
| 473 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 474 | */ |
Masahiro Yamada | b2a05c1 | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 475 | static efi_status_t EFIAPI efi_get_memory_map_ext( |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 476 | efi_uintn_t *memory_map_size, |
Masahiro Yamada | b2a05c1 | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 477 | struct efi_mem_desc *memory_map, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 478 | efi_uintn_t *map_key, |
| 479 | efi_uintn_t *descriptor_size, |
Masahiro Yamada | b2a05c1 | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 480 | uint32_t *descriptor_version) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 481 | { |
| 482 | efi_status_t r; |
| 483 | |
| 484 | EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map, |
| 485 | map_key, descriptor_size, descriptor_version); |
| 486 | r = efi_get_memory_map(memory_map_size, memory_map, map_key, |
| 487 | descriptor_size, descriptor_version); |
| 488 | return EFI_EXIT(r); |
| 489 | } |
| 490 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 491 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 492 | * efi_allocate_pool_ext() - allocate memory from pool |
| 493 | * @pool_type: type of the pool from which memory is to be allocated |
| 494 | * @size: number of bytes to be allocated |
| 495 | * @buffer: allocated memory |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 496 | * |
| 497 | * This function implements the AllocatePool service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 498 | * |
| 499 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 500 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 501 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 502 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 503 | */ |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 504 | static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 505 | efi_uintn_t size, |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 506 | void **buffer) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 507 | { |
Alexander Graf | 1c34fa8 | 2016-03-24 01:37:37 +0100 | [diff] [blame] | 508 | efi_status_t r; |
| 509 | |
Heinrich Schuchardt | e1e8a65 | 2022-02-03 22:21:51 +0100 | [diff] [blame] | 510 | EFI_ENTRY("%d, %zu, %p", pool_type, size, buffer); |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 511 | r = efi_allocate_pool(pool_type, size, buffer); |
Alexander Graf | 1c34fa8 | 2016-03-24 01:37:37 +0100 | [diff] [blame] | 512 | return EFI_EXIT(r); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 513 | } |
| 514 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 515 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 516 | * efi_free_pool_ext() - free memory from pool |
| 517 | * @buffer: start of memory to be freed |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 518 | * |
| 519 | * This function implements the FreePool service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 520 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 521 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 522 | * details. |
| 523 | * |
| 524 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 525 | */ |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 526 | static efi_status_t EFIAPI efi_free_pool_ext(void *buffer) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 527 | { |
Alexander Graf | 1c34fa8 | 2016-03-24 01:37:37 +0100 | [diff] [blame] | 528 | efi_status_t r; |
| 529 | |
| 530 | EFI_ENTRY("%p", buffer); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 531 | r = efi_free_pool(buffer); |
Alexander Graf | 1c34fa8 | 2016-03-24 01:37:37 +0100 | [diff] [blame] | 532 | return EFI_EXIT(r); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 533 | } |
| 534 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 535 | /** |
Heinrich Schuchardt | 30cd046 | 2019-05-01 09:42:39 +0200 | [diff] [blame] | 536 | * efi_add_handle() - add a new handle to the object list |
| 537 | * |
| 538 | * @handle: handle to be added |
Heinrich Schuchardt | 967d7de | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 539 | * |
Heinrich Schuchardt | 30cd046 | 2019-05-01 09:42:39 +0200 | [diff] [blame] | 540 | * The protocols list is initialized. The handle is added to the list of known |
| 541 | * UEFI objects. |
Heinrich Schuchardt | 967d7de | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 542 | */ |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 543 | void efi_add_handle(efi_handle_t handle) |
Heinrich Schuchardt | 967d7de | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 544 | { |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 545 | if (!handle) |
Heinrich Schuchardt | 967d7de | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 546 | return; |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 547 | INIT_LIST_HEAD(&handle->protocols); |
| 548 | list_add_tail(&handle->link, &efi_obj_list); |
Heinrich Schuchardt | 967d7de | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 549 | } |
| 550 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 551 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 552 | * efi_create_handle() - create handle |
| 553 | * @handle: new handle |
Heinrich Schuchardt | eb6106e | 2017-10-26 19:25:49 +0200 | [diff] [blame] | 554 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 555 | * Return: status code |
Heinrich Schuchardt | eb6106e | 2017-10-26 19:25:49 +0200 | [diff] [blame] | 556 | */ |
Heinrich Schuchardt | b7cb8b4 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 557 | efi_status_t efi_create_handle(efi_handle_t *handle) |
Heinrich Schuchardt | cd522cb | 2017-08-27 00:51:09 +0200 | [diff] [blame] | 558 | { |
| 559 | struct efi_object *obj; |
Heinrich Schuchardt | cd522cb | 2017-08-27 00:51:09 +0200 | [diff] [blame] | 560 | |
Heinrich Schuchardt | ae1d206 | 2018-05-27 16:47:21 +0200 | [diff] [blame] | 561 | obj = calloc(1, sizeof(struct efi_object)); |
| 562 | if (!obj) |
| 563 | return EFI_OUT_OF_RESOURCES; |
| 564 | |
Heinrich Schuchardt | 967d7de | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 565 | efi_add_handle(obj); |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 566 | *handle = obj; |
Heinrich Schuchardt | ae1d206 | 2018-05-27 16:47:21 +0200 | [diff] [blame] | 567 | |
| 568 | return EFI_SUCCESS; |
Heinrich Schuchardt | cd522cb | 2017-08-27 00:51:09 +0200 | [diff] [blame] | 569 | } |
| 570 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 571 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 572 | * efi_search_protocol() - find a protocol on a handle. |
| 573 | * @handle: handle |
| 574 | * @protocol_guid: GUID of the protocol |
| 575 | * @handler: reference to the protocol |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 576 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 577 | * Return: status code |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 578 | */ |
Heinrich Schuchardt | b7cb8b4 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 579 | efi_status_t efi_search_protocol(const efi_handle_t handle, |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 580 | const efi_guid_t *protocol_guid, |
| 581 | struct efi_handler **handler) |
| 582 | { |
| 583 | struct efi_object *efiobj; |
| 584 | struct list_head *lhandle; |
| 585 | |
| 586 | if (!handle || !protocol_guid) |
| 587 | return EFI_INVALID_PARAMETER; |
| 588 | efiobj = efi_search_obj(handle); |
| 589 | if (!efiobj) |
| 590 | return EFI_INVALID_PARAMETER; |
| 591 | list_for_each(lhandle, &efiobj->protocols) { |
| 592 | struct efi_handler *protocol; |
| 593 | |
| 594 | protocol = list_entry(lhandle, struct efi_handler, link); |
Heinrich Schuchardt | 2a22db9 | 2022-03-09 19:56:23 +0100 | [diff] [blame] | 595 | if (!guidcmp(&protocol->guid, protocol_guid)) { |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 596 | if (handler) |
| 597 | *handler = protocol; |
| 598 | return EFI_SUCCESS; |
| 599 | } |
| 600 | } |
| 601 | return EFI_NOT_FOUND; |
| 602 | } |
| 603 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 604 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 605 | * efi_remove_protocol() - delete protocol from a handle |
| 606 | * @handle: handle from which the protocol shall be deleted |
| 607 | * @protocol: GUID of the protocol to be deleted |
| 608 | * @protocol_interface: interface of the protocol implementation |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 609 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 610 | * Return: status code |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 611 | */ |
Ilias Apalodimas | e2effb8 | 2023-06-19 14:14:03 +0300 | [diff] [blame] | 612 | static efi_status_t efi_remove_protocol(const efi_handle_t handle, |
| 613 | const efi_guid_t *protocol, |
| 614 | void *protocol_interface) |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 615 | { |
| 616 | struct efi_handler *handler; |
| 617 | efi_status_t ret; |
| 618 | |
| 619 | ret = efi_search_protocol(handle, protocol, &handler); |
| 620 | if (ret != EFI_SUCCESS) |
| 621 | return ret; |
Heinrich Schuchardt | b27594d | 2018-05-11 12:09:21 +0200 | [diff] [blame] | 622 | if (handler->protocol_interface != protocol_interface) |
Heinrich Schuchardt | fdeb1f0 | 2019-05-10 20:06:48 +0200 | [diff] [blame] | 623 | return EFI_NOT_FOUND; |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 624 | list_del(&handler->link); |
| 625 | free(handler); |
| 626 | return EFI_SUCCESS; |
| 627 | } |
| 628 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 629 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 630 | * efi_remove_all_protocols() - delete all protocols from a handle |
| 631 | * @handle: handle from which the protocols shall be deleted |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 632 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 633 | * Return: status code |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 634 | */ |
Heinrich Schuchardt | 8126a1e | 2023-02-10 08:50:06 +0100 | [diff] [blame] | 635 | static efi_status_t efi_remove_all_protocols(const efi_handle_t handle) |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 636 | { |
| 637 | struct efi_object *efiobj; |
Heinrich Schuchardt | a84731d | 2018-01-11 08:15:55 +0100 | [diff] [blame] | 638 | struct efi_handler *protocol; |
| 639 | struct efi_handler *pos; |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 640 | |
| 641 | efiobj = efi_search_obj(handle); |
| 642 | if (!efiobj) |
| 643 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | a84731d | 2018-01-11 08:15:55 +0100 | [diff] [blame] | 644 | list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) { |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 645 | efi_status_t ret; |
| 646 | |
Ilias Apalodimas | b09ecf1 | 2023-07-24 13:17:36 +0300 | [diff] [blame] | 647 | ret = efi_uninstall_protocol(handle, &protocol->guid, |
Ilias Apalodimas | c76eade | 2023-08-24 17:21:09 +0300 | [diff] [blame] | 648 | protocol->protocol_interface, true); |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 649 | if (ret != EFI_SUCCESS) |
| 650 | return ret; |
| 651 | } |
| 652 | return EFI_SUCCESS; |
| 653 | } |
| 654 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 655 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 656 | * efi_delete_handle() - delete handle |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 657 | * |
Heinrich Schuchardt | 41cc3c1 | 2019-07-14 11:05:34 +0200 | [diff] [blame] | 658 | * @handle: handle to delete |
Ilias Apalodimas | b09ecf1 | 2023-07-24 13:17:36 +0300 | [diff] [blame] | 659 | * |
| 660 | * Return: status code |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 661 | */ |
Ilias Apalodimas | b09ecf1 | 2023-07-24 13:17:36 +0300 | [diff] [blame] | 662 | efi_status_t efi_delete_handle(efi_handle_t handle) |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 663 | { |
Etienne Carriere | b20a42b | 2022-09-07 10:20:13 +0200 | [diff] [blame] | 664 | efi_status_t ret; |
| 665 | |
| 666 | ret = efi_remove_all_protocols(handle); |
Ilias Apalodimas | b09ecf1 | 2023-07-24 13:17:36 +0300 | [diff] [blame] | 667 | if (ret != EFI_SUCCESS) { |
| 668 | log_err("Handle %p has protocols installed. Unable to delete\n", handle); |
| 669 | return ret; |
Etienne Carriere | b20a42b | 2022-09-07 10:20:13 +0200 | [diff] [blame] | 670 | } |
| 671 | |
Ilias Apalodimas | c76eade | 2023-08-24 17:21:09 +0300 | [diff] [blame] | 672 | return efi_purge_handle(handle); |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 673 | } |
| 674 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 675 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 676 | * efi_is_event() - check if a pointer is a valid event |
| 677 | * @event: pointer to check |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 678 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 679 | * Return: status code |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 680 | */ |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 681 | static efi_status_t efi_is_event(const struct efi_event *event) |
| 682 | { |
| 683 | const struct efi_event *evt; |
| 684 | |
| 685 | if (!event) |
| 686 | return EFI_INVALID_PARAMETER; |
| 687 | list_for_each_entry(evt, &efi_events, link) { |
| 688 | if (evt == event) |
| 689 | return EFI_SUCCESS; |
| 690 | } |
| 691 | return EFI_INVALID_PARAMETER; |
| 692 | } |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 693 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 694 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 695 | * efi_create_event() - create an event |
Heinrich Schuchardt | 41cc3c1 | 2019-07-14 11:05:34 +0200 | [diff] [blame] | 696 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 697 | * @type: type of the event to create |
| 698 | * @notify_tpl: task priority level of the event |
| 699 | * @notify_function: notification function of the event |
| 700 | * @notify_context: pointer passed to the notification function |
| 701 | * @group: event group |
| 702 | * @event: created event |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 703 | * |
| 704 | * This function is used inside U-Boot code to create an event. |
| 705 | * |
| 706 | * For the API function implementing the CreateEvent service see |
| 707 | * efi_create_event_ext. |
| 708 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 709 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 710 | */ |
Heinrich Schuchardt | f8d4ec3 | 2017-11-06 21:17:47 +0100 | [diff] [blame] | 711 | efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl, |
xypron.glpk@gmx.de | 852a0e177 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 712 | void (EFIAPI *notify_function) ( |
xypron.glpk@gmx.de | cdbf3ac | 2017-07-18 20:17:17 +0200 | [diff] [blame] | 713 | struct efi_event *event, |
| 714 | void *context), |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 715 | void *notify_context, efi_guid_t *group, |
| 716 | struct efi_event **event) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 717 | { |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 718 | struct efi_event *evt; |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 719 | efi_status_t ret; |
| 720 | int pool_type; |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 721 | |
Jonathan Gray | 7758b21 | 2017-03-12 19:26:07 +1100 | [diff] [blame] | 722 | if (event == NULL) |
xypron.glpk@gmx.de | 852a0e177 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 723 | return EFI_INVALID_PARAMETER; |
Jonathan Gray | 7758b21 | 2017-03-12 19:26:07 +1100 | [diff] [blame] | 724 | |
Heinrich Schuchardt | 7f0f005 | 2018-07-02 12:53:52 +0200 | [diff] [blame] | 725 | switch (type) { |
| 726 | case 0: |
| 727 | case EVT_TIMER: |
| 728 | case EVT_NOTIFY_SIGNAL: |
| 729 | case EVT_TIMER | EVT_NOTIFY_SIGNAL: |
| 730 | case EVT_NOTIFY_WAIT: |
| 731 | case EVT_TIMER | EVT_NOTIFY_WAIT: |
| 732 | case EVT_SIGNAL_EXIT_BOOT_SERVICES: |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 733 | pool_type = EFI_BOOT_SERVICES_DATA; |
| 734 | break; |
Heinrich Schuchardt | 7f0f005 | 2018-07-02 12:53:52 +0200 | [diff] [blame] | 735 | case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE: |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 736 | pool_type = EFI_RUNTIME_SERVICES_DATA; |
Heinrich Schuchardt | 7f0f005 | 2018-07-02 12:53:52 +0200 | [diff] [blame] | 737 | break; |
| 738 | default: |
xypron.glpk@gmx.de | 852a0e177 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 739 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | 7f0f005 | 2018-07-02 12:53:52 +0200 | [diff] [blame] | 740 | } |
Jonathan Gray | 7758b21 | 2017-03-12 19:26:07 +1100 | [diff] [blame] | 741 | |
Heinrich Schuchardt | 3d83b73 | 2021-01-06 12:55:22 +0100 | [diff] [blame] | 742 | /* |
| 743 | * The UEFI specification requires event notification levels to be |
| 744 | * > TPL_APPLICATION and <= TPL_HIGH_LEVEL. |
| 745 | * |
| 746 | * Parameter NotifyTpl should not be checked if it is not used. |
| 747 | */ |
AKASHI Takahiro | 3f3835d | 2018-08-10 15:36:32 +0900 | [diff] [blame] | 748 | if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) && |
Heinrich Schuchardt | 3d83b73 | 2021-01-06 12:55:22 +0100 | [diff] [blame] | 749 | (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS || |
| 750 | notify_tpl == TPL_APPLICATION)) |
xypron.glpk@gmx.de | 852a0e177 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 751 | return EFI_INVALID_PARAMETER; |
Jonathan Gray | 7758b21 | 2017-03-12 19:26:07 +1100 | [diff] [blame] | 752 | |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 753 | ret = efi_allocate_pool(pool_type, sizeof(struct efi_event), |
| 754 | (void **)&evt); |
| 755 | if (ret != EFI_SUCCESS) |
| 756 | return ret; |
| 757 | memset(evt, 0, sizeof(struct efi_event)); |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 758 | evt->type = type; |
| 759 | evt->notify_tpl = notify_tpl; |
| 760 | evt->notify_function = notify_function; |
| 761 | evt->notify_context = notify_context; |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 762 | evt->group = group; |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 763 | /* Disable timers on boot up */ |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 764 | evt->trigger_next = -1ULL; |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 765 | list_add_tail(&evt->link, &efi_events); |
| 766 | *event = evt; |
| 767 | return EFI_SUCCESS; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 768 | } |
| 769 | |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 770 | /* |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 771 | * efi_create_event_ex() - create an event in a group |
Heinrich Schuchardt | 8126a1e | 2023-02-10 08:50:06 +0100 | [diff] [blame] | 772 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 773 | * @type: type of the event to create |
| 774 | * @notify_tpl: task priority level of the event |
| 775 | * @notify_function: notification function of the event |
| 776 | * @notify_context: pointer passed to the notification function |
| 777 | * @event: created event |
| 778 | * @event_group: event group |
Heinrich Schuchardt | 717c458 | 2018-02-04 23:05:13 +0100 | [diff] [blame] | 779 | * |
| 780 | * This function implements the CreateEventEx service. |
Heinrich Schuchardt | 717c458 | 2018-02-04 23:05:13 +0100 | [diff] [blame] | 781 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 782 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 783 | * details. |
| 784 | * |
| 785 | * Return: status code |
Heinrich Schuchardt | 717c458 | 2018-02-04 23:05:13 +0100 | [diff] [blame] | 786 | */ |
Heinrich Schuchardt | 8126a1e | 2023-02-10 08:50:06 +0100 | [diff] [blame] | 787 | static |
Heinrich Schuchardt | 717c458 | 2018-02-04 23:05:13 +0100 | [diff] [blame] | 788 | efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl, |
| 789 | void (EFIAPI *notify_function) ( |
| 790 | struct efi_event *event, |
| 791 | void *context), |
| 792 | void *notify_context, |
| 793 | efi_guid_t *event_group, |
| 794 | struct efi_event **event) |
| 795 | { |
Heinrich Schuchardt | ce66499 | 2019-05-04 10:12:50 +0200 | [diff] [blame] | 796 | efi_status_t ret; |
| 797 | |
Heinrich Schuchardt | 282249d | 2022-01-16 14:15:31 +0100 | [diff] [blame] | 798 | EFI_ENTRY("%d, 0x%zx, %p, %p, %pUs", type, notify_tpl, notify_function, |
Heinrich Schuchardt | 717c458 | 2018-02-04 23:05:13 +0100 | [diff] [blame] | 799 | notify_context, event_group); |
Heinrich Schuchardt | ce66499 | 2019-05-04 10:12:50 +0200 | [diff] [blame] | 800 | |
| 801 | /* |
| 802 | * The allowable input parameters are the same as in CreateEvent() |
| 803 | * except for the following two disallowed event types. |
| 804 | */ |
| 805 | switch (type) { |
| 806 | case EVT_SIGNAL_EXIT_BOOT_SERVICES: |
| 807 | case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE: |
| 808 | ret = EFI_INVALID_PARAMETER; |
| 809 | goto out; |
| 810 | } |
| 811 | |
| 812 | ret = efi_create_event(type, notify_tpl, notify_function, |
| 813 | notify_context, event_group, event); |
| 814 | out: |
| 815 | return EFI_EXIT(ret); |
Heinrich Schuchardt | 717c458 | 2018-02-04 23:05:13 +0100 | [diff] [blame] | 816 | } |
| 817 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 818 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 819 | * efi_create_event_ext() - create an event |
| 820 | * @type: type of the event to create |
| 821 | * @notify_tpl: task priority level of the event |
| 822 | * @notify_function: notification function of the event |
| 823 | * @notify_context: pointer passed to the notification function |
| 824 | * @event: created event |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 825 | * |
| 826 | * This function implements the CreateEvent service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 827 | * |
| 828 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 829 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 830 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 831 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 832 | */ |
xypron.glpk@gmx.de | 852a0e177 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 833 | static efi_status_t EFIAPI efi_create_event_ext( |
Heinrich Schuchardt | f8d4ec3 | 2017-11-06 21:17:47 +0100 | [diff] [blame] | 834 | uint32_t type, efi_uintn_t notify_tpl, |
xypron.glpk@gmx.de | 852a0e177 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 835 | void (EFIAPI *notify_function) ( |
| 836 | struct efi_event *event, |
| 837 | void *context), |
| 838 | void *notify_context, struct efi_event **event) |
| 839 | { |
| 840 | EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function, |
| 841 | notify_context); |
| 842 | return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function, |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 843 | notify_context, NULL, event)); |
xypron.glpk@gmx.de | 852a0e177 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 844 | } |
| 845 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 846 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 847 | * efi_timer_check() - check if a timer event has occurred |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 848 | * |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 849 | * Check if a timer event has occurred or a queued notification function should |
| 850 | * be called. |
| 851 | * |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 852 | * Our timers have to work without interrupts, so we check whenever keyboard |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 853 | * input or disk accesses happen if enough time elapsed for them to fire. |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 854 | */ |
| 855 | void efi_timer_check(void) |
| 856 | { |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 857 | struct efi_event *evt; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 858 | u64 now = timer_get_us(); |
| 859 | |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 860 | list_for_each_entry(evt, &efi_events, link) { |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 861 | if (!timers_enabled) |
| 862 | continue; |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 863 | if (!(evt->type & EVT_TIMER) || now < evt->trigger_next) |
Heinrich Schuchardt | 8b11a8a | 2017-09-15 10:06:13 +0200 | [diff] [blame] | 864 | continue; |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 865 | switch (evt->trigger_type) { |
Heinrich Schuchardt | 8b11a8a | 2017-09-15 10:06:13 +0200 | [diff] [blame] | 866 | case EFI_TIMER_RELATIVE: |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 867 | evt->trigger_type = EFI_TIMER_STOP; |
Heinrich Schuchardt | 8b11a8a | 2017-09-15 10:06:13 +0200 | [diff] [blame] | 868 | break; |
| 869 | case EFI_TIMER_PERIODIC: |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 870 | evt->trigger_next += evt->trigger_time; |
Heinrich Schuchardt | 8b11a8a | 2017-09-15 10:06:13 +0200 | [diff] [blame] | 871 | break; |
| 872 | default: |
| 873 | continue; |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 874 | } |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 875 | evt->is_signaled = false; |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 876 | efi_signal_event(evt); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 877 | } |
Heinrich Schuchardt | 35c0cf6 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 878 | efi_process_event_queue(); |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 879 | schedule(); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 880 | } |
| 881 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 882 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 883 | * efi_set_timer() - set the trigger time for a timer event or stop the event |
| 884 | * @event: event for which the timer is set |
| 885 | * @type: type of the timer |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 886 | * @trigger_time: trigger period in multiples of 100 ns |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 887 | * |
| 888 | * This is the function for internal usage in U-Boot. For the API function |
| 889 | * implementing the SetTimer service see efi_set_timer_ext. |
| 890 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 891 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 892 | */ |
xypron.glpk@gmx.de | 3ecc6bd | 2017-07-19 19:22:34 +0200 | [diff] [blame] | 893 | efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type, |
xypron.glpk@gmx.de | a587fd1 | 2017-07-18 20:17:21 +0200 | [diff] [blame] | 894 | uint64_t trigger_time) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 895 | { |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 896 | /* Check that the event is valid */ |
| 897 | if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER)) |
| 898 | return EFI_INVALID_PARAMETER; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 899 | |
xypron.glpk@gmx.de | 44c4be0 | 2017-07-18 20:17:23 +0200 | [diff] [blame] | 900 | /* |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 901 | * The parameter defines a multiple of 100 ns. |
| 902 | * We use multiples of 1000 ns. So divide by 10. |
xypron.glpk@gmx.de | 44c4be0 | 2017-07-18 20:17:23 +0200 | [diff] [blame] | 903 | */ |
Heinrich Schuchardt | 368ca64 | 2017-10-05 16:14:14 +0200 | [diff] [blame] | 904 | do_div(trigger_time, 10); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 905 | |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 906 | switch (type) { |
| 907 | case EFI_TIMER_STOP: |
| 908 | event->trigger_next = -1ULL; |
| 909 | break; |
| 910 | case EFI_TIMER_PERIODIC: |
| 911 | case EFI_TIMER_RELATIVE: |
| 912 | event->trigger_next = timer_get_us() + trigger_time; |
| 913 | break; |
| 914 | default: |
| 915 | return EFI_INVALID_PARAMETER; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 916 | } |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 917 | event->trigger_type = type; |
| 918 | event->trigger_time = trigger_time; |
| 919 | event->is_signaled = false; |
| 920 | return EFI_SUCCESS; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 921 | } |
| 922 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 923 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 924 | * efi_set_timer_ext() - Set the trigger time for a timer event or stop the |
| 925 | * event |
| 926 | * @event: event for which the timer is set |
| 927 | * @type: type of the timer |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 928 | * @trigger_time: trigger period in multiples of 100 ns |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 929 | * |
| 930 | * This function implements the SetTimer service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 931 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 932 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 933 | * details. |
| 934 | * |
| 935 | * |
| 936 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 937 | */ |
xypron.glpk@gmx.de | 3ecc6bd | 2017-07-19 19:22:34 +0200 | [diff] [blame] | 938 | static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event, |
| 939 | enum efi_timer_delay type, |
| 940 | uint64_t trigger_time) |
xypron.glpk@gmx.de | a587fd1 | 2017-07-18 20:17:21 +0200 | [diff] [blame] | 941 | { |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 942 | EFI_ENTRY("%p, %d, %llx", event, type, trigger_time); |
xypron.glpk@gmx.de | a587fd1 | 2017-07-18 20:17:21 +0200 | [diff] [blame] | 943 | return EFI_EXIT(efi_set_timer(event, type, trigger_time)); |
| 944 | } |
| 945 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 946 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 947 | * efi_wait_for_event() - wait for events to be signaled |
| 948 | * @num_events: number of events to be waited for |
| 949 | * @event: events to be waited for |
| 950 | * @index: index of the event that was signaled |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 951 | * |
| 952 | * This function implements the WaitForEvent service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 953 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 954 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 955 | * details. |
| 956 | * |
| 957 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 958 | */ |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 959 | static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events, |
xypron.glpk@gmx.de | cdbf3ac | 2017-07-18 20:17:17 +0200 | [diff] [blame] | 960 | struct efi_event **event, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 961 | efi_uintn_t *index) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 962 | { |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 963 | int i; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 964 | |
Heinrich Schuchardt | e1e8a65 | 2022-02-03 22:21:51 +0100 | [diff] [blame] | 965 | EFI_ENTRY("%zu, %p, %p", num_events, event, index); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 966 | |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 967 | /* Check parameters */ |
| 968 | if (!num_events || !event) |
| 969 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
Heinrich Schuchardt | 0c150ca | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 970 | /* Check TPL */ |
| 971 | if (efi_tpl != TPL_APPLICATION) |
| 972 | return EFI_EXIT(EFI_UNSUPPORTED); |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 973 | for (i = 0; i < num_events; ++i) { |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 974 | if (efi_is_event(event[i]) != EFI_SUCCESS) |
| 975 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 976 | if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL) |
| 977 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
Heinrich Schuchardt | 1bbee39 | 2017-10-04 15:03:24 +0200 | [diff] [blame] | 978 | if (!event[i]->is_signaled) |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 979 | efi_queue_event(event[i]); |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | /* Wait for signal */ |
| 983 | for (;;) { |
| 984 | for (i = 0; i < num_events; ++i) { |
Heinrich Schuchardt | 1bbee39 | 2017-10-04 15:03:24 +0200 | [diff] [blame] | 985 | if (event[i]->is_signaled) |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 986 | goto out; |
| 987 | } |
| 988 | /* Allow events to occur. */ |
| 989 | efi_timer_check(); |
| 990 | } |
| 991 | |
| 992 | out: |
| 993 | /* |
| 994 | * Reset the signal which is passed to the caller to allow periodic |
| 995 | * events to occur. |
| 996 | */ |
Heinrich Schuchardt | 1bbee39 | 2017-10-04 15:03:24 +0200 | [diff] [blame] | 997 | event[i]->is_signaled = false; |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 998 | if (index) |
| 999 | *index = i; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1000 | |
| 1001 | return EFI_EXIT(EFI_SUCCESS); |
| 1002 | } |
| 1003 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1004 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1005 | * efi_signal_event_ext() - signal an EFI event |
| 1006 | * @event: event to signal |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1007 | * |
| 1008 | * This function implements the SignalEvent service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1009 | * |
| 1010 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1011 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1012 | * |
| 1013 | * This functions sets the signaled state of the event and queues the |
| 1014 | * notification function for execution. |
| 1015 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1016 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1017 | */ |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 1018 | static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1019 | { |
| 1020 | EFI_ENTRY("%p", event); |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 1021 | if (efi_is_event(event) != EFI_SUCCESS) |
| 1022 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 1023 | efi_signal_event(event); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1024 | return EFI_EXIT(EFI_SUCCESS); |
| 1025 | } |
| 1026 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1027 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1028 | * efi_close_event() - close an EFI event |
| 1029 | * @event: event to close |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1030 | * |
| 1031 | * This function implements the CloseEvent service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1032 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1033 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1034 | * details. |
| 1035 | * |
| 1036 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1037 | */ |
xypron.glpk@gmx.de | cdbf3ac | 2017-07-18 20:17:17 +0200 | [diff] [blame] | 1038 | static efi_status_t EFIAPI efi_close_event(struct efi_event *event) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1039 | { |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1040 | struct efi_register_notify_event *item, *next; |
| 1041 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1042 | EFI_ENTRY("%p", event); |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 1043 | if (efi_is_event(event) != EFI_SUCCESS) |
| 1044 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1045 | |
| 1046 | /* Remove protocol notify registrations for the event */ |
| 1047 | list_for_each_entry_safe(item, next, &efi_register_notify_events, |
| 1048 | link) { |
| 1049 | if (event == item->event) { |
Heinrich Schuchardt | d375e75 | 2019-05-21 18:19:01 +0200 | [diff] [blame] | 1050 | struct efi_protocol_notification *hitem, *hnext; |
| 1051 | |
| 1052 | /* Remove signaled handles */ |
| 1053 | list_for_each_entry_safe(hitem, hnext, &item->handles, |
| 1054 | link) { |
| 1055 | list_del(&hitem->link); |
| 1056 | free(hitem); |
| 1057 | } |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1058 | list_del(&item->link); |
| 1059 | free(item); |
| 1060 | } |
| 1061 | } |
Heinrich Schuchardt | 35c0cf6 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 1062 | /* Remove event from queue */ |
| 1063 | if (efi_event_is_queued(event)) |
| 1064 | list_del(&event->queue_link); |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1065 | |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 1066 | list_del(&event->link); |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 1067 | efi_free_pool(event); |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 1068 | return EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1069 | } |
| 1070 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1071 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1072 | * efi_check_event() - check if an event is signaled |
| 1073 | * @event: event to check |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1074 | * |
| 1075 | * This function implements the CheckEvent service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1076 | * |
| 1077 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1078 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1079 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1080 | * If an event is not signaled yet, the notification function is queued. The |
| 1081 | * signaled state is cleared. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1082 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1083 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1084 | */ |
xypron.glpk@gmx.de | cdbf3ac | 2017-07-18 20:17:17 +0200 | [diff] [blame] | 1085 | static efi_status_t EFIAPI efi_check_event(struct efi_event *event) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1086 | { |
| 1087 | EFI_ENTRY("%p", event); |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 1088 | efi_timer_check(); |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 1089 | if (efi_is_event(event) != EFI_SUCCESS || |
| 1090 | event->type & EVT_NOTIFY_SIGNAL) |
| 1091 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 1092 | if (!event->is_signaled) |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 1093 | efi_queue_event(event); |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 1094 | if (event->is_signaled) { |
| 1095 | event->is_signaled = false; |
| 1096 | return EFI_EXIT(EFI_SUCCESS); |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 1097 | } |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 1098 | return EFI_EXIT(EFI_NOT_READY); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1099 | } |
| 1100 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1101 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1102 | * efi_search_obj() - find the internal EFI object for a handle |
| 1103 | * @handle: handle to find |
Heinrich Schuchardt | 37ebcba | 2017-10-18 18:13:03 +0200 | [diff] [blame] | 1104 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1105 | * Return: EFI object |
Heinrich Schuchardt | 37ebcba | 2017-10-18 18:13:03 +0200 | [diff] [blame] | 1106 | */ |
Heinrich Schuchardt | b7cb8b4 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 1107 | struct efi_object *efi_search_obj(const efi_handle_t handle) |
Heinrich Schuchardt | 37ebcba | 2017-10-18 18:13:03 +0200 | [diff] [blame] | 1108 | { |
Heinrich Schuchardt | 274cc87 | 2017-11-06 21:17:50 +0100 | [diff] [blame] | 1109 | struct efi_object *efiobj; |
Heinrich Schuchardt | 37ebcba | 2017-10-18 18:13:03 +0200 | [diff] [blame] | 1110 | |
Heinrich Schuchardt | 5d8231b | 2019-05-05 10:37:51 +0200 | [diff] [blame] | 1111 | if (!handle) |
| 1112 | return NULL; |
| 1113 | |
Heinrich Schuchardt | 274cc87 | 2017-11-06 21:17:50 +0100 | [diff] [blame] | 1114 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1115 | if (efiobj == handle) |
Heinrich Schuchardt | 37ebcba | 2017-10-18 18:13:03 +0200 | [diff] [blame] | 1116 | return efiobj; |
| 1117 | } |
Heinrich Schuchardt | 37ebcba | 2017-10-18 18:13:03 +0200 | [diff] [blame] | 1118 | return NULL; |
| 1119 | } |
| 1120 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1121 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1122 | * efi_open_protocol_info_entry() - create open protocol info entry and add it |
| 1123 | * to a protocol |
| 1124 | * @handler: handler of a protocol |
Heinrich Schuchardt | a277d3a | 2018-01-11 08:15:57 +0100 | [diff] [blame] | 1125 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1126 | * Return: open protocol info entry |
Heinrich Schuchardt | a277d3a | 2018-01-11 08:15:57 +0100 | [diff] [blame] | 1127 | */ |
| 1128 | static struct efi_open_protocol_info_entry *efi_create_open_info( |
| 1129 | struct efi_handler *handler) |
| 1130 | { |
| 1131 | struct efi_open_protocol_info_item *item; |
| 1132 | |
| 1133 | item = calloc(1, sizeof(struct efi_open_protocol_info_item)); |
| 1134 | if (!item) |
| 1135 | return NULL; |
| 1136 | /* Append the item to the open protocol info list. */ |
| 1137 | list_add_tail(&item->link, &handler->open_infos); |
| 1138 | |
| 1139 | return &item->info; |
| 1140 | } |
| 1141 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1142 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1143 | * efi_delete_open_info() - remove an open protocol info entry from a protocol |
| 1144 | * @item: open protocol info entry to delete |
Heinrich Schuchardt | a277d3a | 2018-01-11 08:15:57 +0100 | [diff] [blame] | 1145 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1146 | * Return: status code |
Heinrich Schuchardt | a277d3a | 2018-01-11 08:15:57 +0100 | [diff] [blame] | 1147 | */ |
| 1148 | static efi_status_t efi_delete_open_info( |
| 1149 | struct efi_open_protocol_info_item *item) |
| 1150 | { |
| 1151 | list_del(&item->link); |
| 1152 | free(item); |
| 1153 | return EFI_SUCCESS; |
| 1154 | } |
| 1155 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1156 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1157 | * efi_add_protocol() - install new protocol on a handle |
| 1158 | * @handle: handle on which the protocol shall be installed |
| 1159 | * @protocol: GUID of the protocol to be installed |
| 1160 | * @protocol_interface: interface of the protocol implementation |
Heinrich Schuchardt | 5aef61d | 2017-10-26 19:25:53 +0200 | [diff] [blame] | 1161 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1162 | * Return: status code |
Heinrich Schuchardt | 5aef61d | 2017-10-26 19:25:53 +0200 | [diff] [blame] | 1163 | */ |
Heinrich Schuchardt | b7cb8b4 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 1164 | efi_status_t efi_add_protocol(const efi_handle_t handle, |
| 1165 | const efi_guid_t *protocol, |
Heinrich Schuchardt | 5aef61d | 2017-10-26 19:25:53 +0200 | [diff] [blame] | 1166 | void *protocol_interface) |
| 1167 | { |
| 1168 | struct efi_object *efiobj; |
| 1169 | struct efi_handler *handler; |
| 1170 | efi_status_t ret; |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1171 | struct efi_register_notify_event *event; |
Heinrich Schuchardt | 5aef61d | 2017-10-26 19:25:53 +0200 | [diff] [blame] | 1172 | |
| 1173 | efiobj = efi_search_obj(handle); |
| 1174 | if (!efiobj) |
| 1175 | return EFI_INVALID_PARAMETER; |
| 1176 | ret = efi_search_protocol(handle, protocol, NULL); |
| 1177 | if (ret != EFI_NOT_FOUND) |
| 1178 | return EFI_INVALID_PARAMETER; |
| 1179 | handler = calloc(1, sizeof(struct efi_handler)); |
| 1180 | if (!handler) |
| 1181 | return EFI_OUT_OF_RESOURCES; |
Heinrich Schuchardt | 2a22db9 | 2022-03-09 19:56:23 +0100 | [diff] [blame] | 1182 | memcpy((void *)&handler->guid, protocol, sizeof(efi_guid_t)); |
Heinrich Schuchardt | 99ce206 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 1183 | handler->protocol_interface = protocol_interface; |
Heinrich Schuchardt | a277d3a | 2018-01-11 08:15:57 +0100 | [diff] [blame] | 1184 | INIT_LIST_HEAD(&handler->open_infos); |
Heinrich Schuchardt | 99ce206 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 1185 | list_add_tail(&handler->link, &efiobj->protocols); |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1186 | |
| 1187 | /* Notify registered events */ |
| 1188 | list_for_each_entry(event, &efi_register_notify_events, link) { |
Heinrich Schuchardt | d375e75 | 2019-05-21 18:19:01 +0200 | [diff] [blame] | 1189 | if (!guidcmp(protocol, &event->protocol)) { |
| 1190 | struct efi_protocol_notification *notif; |
| 1191 | |
| 1192 | notif = calloc(1, sizeof(*notif)); |
| 1193 | if (!notif) { |
| 1194 | list_del(&handler->link); |
| 1195 | free(handler); |
| 1196 | return EFI_OUT_OF_RESOURCES; |
| 1197 | } |
| 1198 | notif->handle = handle; |
| 1199 | list_add_tail(¬if->link, &event->handles); |
Heinrich Schuchardt | ea0f6a8 | 2019-06-07 07:43:24 +0200 | [diff] [blame] | 1200 | event->event->is_signaled = false; |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 1201 | efi_signal_event(event->event); |
Heinrich Schuchardt | d375e75 | 2019-05-21 18:19:01 +0200 | [diff] [blame] | 1202 | } |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1203 | } |
| 1204 | |
Heinrich Schuchardt | 3d2abc3 | 2018-01-11 08:16:01 +0100 | [diff] [blame] | 1205 | if (!guidcmp(&efi_guid_device_path, protocol)) |
| 1206 | EFI_PRINT("installed device path '%pD'\n", protocol_interface); |
Heinrich Schuchardt | 99ce206 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 1207 | return EFI_SUCCESS; |
Heinrich Schuchardt | 5aef61d | 2017-10-26 19:25:53 +0200 | [diff] [blame] | 1208 | } |
| 1209 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1210 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1211 | * efi_install_protocol_interface() - install protocol interface |
| 1212 | * @handle: handle on which the protocol shall be installed |
| 1213 | * @protocol: GUID of the protocol to be installed |
| 1214 | * @protocol_interface_type: type of the interface to be installed, |
| 1215 | * always EFI_NATIVE_INTERFACE |
| 1216 | * @protocol_interface: interface of the protocol implementation |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1217 | * |
Heinrich Schuchardt | 0a27ac8 | 2017-11-06 21:17:44 +0100 | [diff] [blame] | 1218 | * This function implements the InstallProtocolInterface service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1219 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1220 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1221 | * details. |
| 1222 | * |
| 1223 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1224 | */ |
Heinrich Schuchardt | 0a27ac8 | 2017-11-06 21:17:44 +0100 | [diff] [blame] | 1225 | static efi_status_t EFIAPI efi_install_protocol_interface( |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 1226 | efi_handle_t *handle, const efi_guid_t *protocol, |
Heinrich Schuchardt | 0a27ac8 | 2017-11-06 21:17:44 +0100 | [diff] [blame] | 1227 | int protocol_interface_type, void *protocol_interface) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1228 | { |
xypron.glpk@gmx.de | 0581fa8 | 2017-07-11 22:06:16 +0200 | [diff] [blame] | 1229 | efi_status_t r; |
| 1230 | |
Heinrich Schuchardt | 282249d | 2022-01-16 14:15:31 +0100 | [diff] [blame] | 1231 | EFI_ENTRY("%p, %pUs, %d, %p", handle, protocol, protocol_interface_type, |
Heinrich Schuchardt | 0a27ac8 | 2017-11-06 21:17:44 +0100 | [diff] [blame] | 1232 | protocol_interface); |
| 1233 | |
xypron.glpk@gmx.de | 0581fa8 | 2017-07-11 22:06:16 +0200 | [diff] [blame] | 1234 | if (!handle || !protocol || |
| 1235 | protocol_interface_type != EFI_NATIVE_INTERFACE) { |
| 1236 | r = EFI_INVALID_PARAMETER; |
| 1237 | goto out; |
| 1238 | } |
| 1239 | |
| 1240 | /* Create new handle if requested. */ |
| 1241 | if (!*handle) { |
Heinrich Schuchardt | cd522cb | 2017-08-27 00:51:09 +0200 | [diff] [blame] | 1242 | r = efi_create_handle(handle); |
| 1243 | if (r != EFI_SUCCESS) |
| 1244 | goto out; |
Heinrich Schuchardt | 952e206 | 2019-05-05 11:56:23 +0200 | [diff] [blame] | 1245 | EFI_PRINT("new handle %p\n", *handle); |
Heinrich Schuchardt | 50f0210 | 2017-10-26 19:25:43 +0200 | [diff] [blame] | 1246 | } else { |
Heinrich Schuchardt | 952e206 | 2019-05-05 11:56:23 +0200 | [diff] [blame] | 1247 | EFI_PRINT("handle %p\n", *handle); |
xypron.glpk@gmx.de | 0581fa8 | 2017-07-11 22:06:16 +0200 | [diff] [blame] | 1248 | } |
Heinrich Schuchardt | 865d5f3 | 2017-10-26 19:25:54 +0200 | [diff] [blame] | 1249 | /* Add new protocol */ |
| 1250 | r = efi_add_protocol(*handle, protocol, protocol_interface); |
xypron.glpk@gmx.de | 0581fa8 | 2017-07-11 22:06:16 +0200 | [diff] [blame] | 1251 | out: |
Heinrich Schuchardt | 0a27ac8 | 2017-11-06 21:17:44 +0100 | [diff] [blame] | 1252 | return EFI_EXIT(r); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1253 | } |
xypron.glpk@gmx.de | 0581fa8 | 2017-07-11 22:06:16 +0200 | [diff] [blame] | 1254 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1255 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1256 | * efi_get_drivers() - get all drivers associated to a controller |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1257 | * @handle: handle of the controller |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 1258 | * @protocol: protocol GUID (optional) |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1259 | * @number_of_drivers: number of child controllers |
| 1260 | * @driver_handle_buffer: handles of the the drivers |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1261 | * |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1262 | * The allocated buffer has to be freed with free(). |
| 1263 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1264 | * Return: status code |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1265 | */ |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1266 | static efi_status_t efi_get_drivers(efi_handle_t handle, |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1267 | const efi_guid_t *protocol, |
| 1268 | efi_uintn_t *number_of_drivers, |
| 1269 | efi_handle_t **driver_handle_buffer) |
| 1270 | { |
| 1271 | struct efi_handler *handler; |
| 1272 | struct efi_open_protocol_info_item *item; |
| 1273 | efi_uintn_t count = 0, i; |
| 1274 | bool duplicate; |
| 1275 | |
| 1276 | /* Count all driver associations */ |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1277 | list_for_each_entry(handler, &handle->protocols, link) { |
Heinrich Schuchardt | 2a22db9 | 2022-03-09 19:56:23 +0100 | [diff] [blame] | 1278 | if (protocol && guidcmp(&handler->guid, protocol)) |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1279 | continue; |
| 1280 | list_for_each_entry(item, &handler->open_infos, link) { |
| 1281 | if (item->info.attributes & |
| 1282 | EFI_OPEN_PROTOCOL_BY_DRIVER) |
| 1283 | ++count; |
| 1284 | } |
| 1285 | } |
Heinrich Schuchardt | 7416aef | 2019-06-02 01:43:33 +0200 | [diff] [blame] | 1286 | *number_of_drivers = 0; |
| 1287 | if (!count) { |
| 1288 | *driver_handle_buffer = NULL; |
| 1289 | return EFI_SUCCESS; |
| 1290 | } |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1291 | /* |
| 1292 | * Create buffer. In case of duplicate driver assignments the buffer |
| 1293 | * will be too large. But that does not harm. |
| 1294 | */ |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1295 | *driver_handle_buffer = calloc(count, sizeof(efi_handle_t)); |
| 1296 | if (!*driver_handle_buffer) |
| 1297 | return EFI_OUT_OF_RESOURCES; |
| 1298 | /* Collect unique driver handles */ |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1299 | list_for_each_entry(handler, &handle->protocols, link) { |
Heinrich Schuchardt | 2a22db9 | 2022-03-09 19:56:23 +0100 | [diff] [blame] | 1300 | if (protocol && guidcmp(&handler->guid, protocol)) |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1301 | continue; |
| 1302 | list_for_each_entry(item, &handler->open_infos, link) { |
| 1303 | if (item->info.attributes & |
| 1304 | EFI_OPEN_PROTOCOL_BY_DRIVER) { |
| 1305 | /* Check this is a new driver */ |
| 1306 | duplicate = false; |
| 1307 | for (i = 0; i < *number_of_drivers; ++i) { |
| 1308 | if ((*driver_handle_buffer)[i] == |
| 1309 | item->info.agent_handle) |
| 1310 | duplicate = true; |
| 1311 | } |
| 1312 | /* Copy handle to buffer */ |
| 1313 | if (!duplicate) { |
| 1314 | i = (*number_of_drivers)++; |
| 1315 | (*driver_handle_buffer)[i] = |
| 1316 | item->info.agent_handle; |
| 1317 | } |
| 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | return EFI_SUCCESS; |
| 1322 | } |
| 1323 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1324 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1325 | * efi_disconnect_all_drivers() - disconnect all drivers from a controller |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1326 | * @handle: handle of the controller |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 1327 | * @protocol: protocol GUID (optional) |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1328 | * @child_handle: handle of the child to destroy |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1329 | * |
| 1330 | * This function implements the DisconnectController service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1331 | * |
| 1332 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1333 | * details. |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1334 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1335 | * Return: status code |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1336 | */ |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1337 | static efi_status_t efi_disconnect_all_drivers |
| 1338 | (efi_handle_t handle, |
| 1339 | const efi_guid_t *protocol, |
| 1340 | efi_handle_t child_handle) |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1341 | { |
Ilias Apalodimas | 1e10d62 | 2023-06-20 09:19:28 +0300 | [diff] [blame] | 1342 | efi_uintn_t number_of_drivers, tmp; |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1343 | efi_handle_t *driver_handle_buffer; |
| 1344 | efi_status_t r, ret; |
| 1345 | |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1346 | ret = efi_get_drivers(handle, protocol, &number_of_drivers, |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1347 | &driver_handle_buffer); |
| 1348 | if (ret != EFI_SUCCESS) |
| 1349 | return ret; |
Heinrich Schuchardt | 7416aef | 2019-06-02 01:43:33 +0200 | [diff] [blame] | 1350 | if (!number_of_drivers) |
| 1351 | return EFI_SUCCESS; |
Ilias Apalodimas | 1e10d62 | 2023-06-20 09:19:28 +0300 | [diff] [blame] | 1352 | |
| 1353 | tmp = number_of_drivers; |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1354 | while (number_of_drivers) { |
Ilias Apalodimas | 1e10d62 | 2023-06-20 09:19:28 +0300 | [diff] [blame] | 1355 | ret = EFI_CALL(efi_disconnect_controller( |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1356 | handle, |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1357 | driver_handle_buffer[--number_of_drivers], |
| 1358 | child_handle)); |
Ilias Apalodimas | 1e10d62 | 2023-06-20 09:19:28 +0300 | [diff] [blame] | 1359 | if (ret != EFI_SUCCESS) |
| 1360 | goto reconnect; |
| 1361 | } |
| 1362 | |
| 1363 | free(driver_handle_buffer); |
| 1364 | return ret; |
| 1365 | |
| 1366 | reconnect: |
| 1367 | /* Reconnect all disconnected drivers */ |
| 1368 | for (; number_of_drivers < tmp; number_of_drivers++) { |
| 1369 | r = EFI_CALL(efi_connect_controller(handle, |
| 1370 | &driver_handle_buffer[number_of_drivers], |
| 1371 | NULL, true)); |
| 1372 | if (r != EFI_SUCCESS) |
| 1373 | EFI_PRINT("Failed to reconnect controller\n"); |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1374 | } |
Ilias Apalodimas | 1e10d62 | 2023-06-20 09:19:28 +0300 | [diff] [blame] | 1375 | |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1376 | free(driver_handle_buffer); |
| 1377 | return ret; |
| 1378 | } |
| 1379 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1380 | /** |
Heinrich Schuchardt | 1b45134 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 1381 | * efi_uninstall_protocol() - uninstall protocol interface |
| 1382 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1383 | * @handle: handle from which the protocol shall be removed |
| 1384 | * @protocol: GUID of the protocol to be removed |
| 1385 | * @protocol_interface: interface to be removed |
Ilias Apalodimas | c76eade | 2023-08-24 17:21:09 +0300 | [diff] [blame] | 1386 | * @preserve: preserve or delete the handle and remove it from any |
| 1387 | * list it participates if no protocols remain |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1388 | * |
Heinrich Schuchardt | 1b45134 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 1389 | * This function DOES NOT delete a handle without installed protocol. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1390 | * |
| 1391 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1392 | */ |
Heinrich Schuchardt | 1b45134 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 1393 | static efi_status_t efi_uninstall_protocol |
| 1394 | (efi_handle_t handle, const efi_guid_t *protocol, |
Ilias Apalodimas | c76eade | 2023-08-24 17:21:09 +0300 | [diff] [blame] | 1395 | void *protocol_interface, bool preserve) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1396 | { |
Heinrich Schuchardt | 7538c27 | 2017-10-26 19:25:56 +0200 | [diff] [blame] | 1397 | struct efi_handler *handler; |
Heinrich Schuchardt | 86637f3 | 2018-01-11 08:16:05 +0100 | [diff] [blame] | 1398 | struct efi_open_protocol_info_item *item; |
| 1399 | struct efi_open_protocol_info_item *pos; |
Heinrich Schuchardt | 7538c27 | 2017-10-26 19:25:56 +0200 | [diff] [blame] | 1400 | efi_status_t r; |
xypron.glpk@gmx.de | 2cfad48 | 2017-07-11 22:06:17 +0200 | [diff] [blame] | 1401 | |
Heinrich Schuchardt | 7538c27 | 2017-10-26 19:25:56 +0200 | [diff] [blame] | 1402 | /* Find the protocol on the handle */ |
| 1403 | r = efi_search_protocol(handle, protocol, &handler); |
| 1404 | if (r != EFI_SUCCESS) |
| 1405 | goto out; |
Ilias Apalodimas | ac1abbe | 2023-06-20 09:19:30 +0300 | [diff] [blame] | 1406 | if (handler->protocol_interface != protocol_interface) |
| 1407 | return EFI_NOT_FOUND; |
Heinrich Schuchardt | 86637f3 | 2018-01-11 08:16:05 +0100 | [diff] [blame] | 1408 | /* Disconnect controllers */ |
Heinrich Schuchardt | d822f86 | 2023-06-18 09:00:45 +0200 | [diff] [blame] | 1409 | r = efi_disconnect_all_drivers(handle, protocol, NULL); |
Ilias Apalodimas | 106f0df | 2023-06-20 09:19:29 +0300 | [diff] [blame] | 1410 | if (r != EFI_SUCCESS) { |
| 1411 | r = EFI_ACCESS_DENIED; |
| 1412 | goto out; |
| 1413 | } |
Heinrich Schuchardt | 86637f3 | 2018-01-11 08:16:05 +0100 | [diff] [blame] | 1414 | /* Close protocol */ |
| 1415 | list_for_each_entry_safe(item, pos, &handler->open_infos, link) { |
| 1416 | if (item->info.attributes == |
| 1417 | EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL || |
| 1418 | item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL || |
| 1419 | item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL) |
Ilias Apalodimas | 106f0df | 2023-06-20 09:19:29 +0300 | [diff] [blame] | 1420 | efi_delete_open_info(item); |
Heinrich Schuchardt | 86637f3 | 2018-01-11 08:16:05 +0100 | [diff] [blame] | 1421 | } |
Ilias Apalodimas | 106f0df | 2023-06-20 09:19:29 +0300 | [diff] [blame] | 1422 | /* if agents didn't close the protocols properly */ |
Heinrich Schuchardt | 86637f3 | 2018-01-11 08:16:05 +0100 | [diff] [blame] | 1423 | if (!list_empty(&handler->open_infos)) { |
Heinrich Schuchardt | 7538c27 | 2017-10-26 19:25:56 +0200 | [diff] [blame] | 1424 | r = EFI_ACCESS_DENIED; |
Ilias Apalodimas | 106f0df | 2023-06-20 09:19:29 +0300 | [diff] [blame] | 1425 | EFI_CALL(efi_connect_controller(handle, NULL, NULL, true)); |
Heinrich Schuchardt | 86637f3 | 2018-01-11 08:16:05 +0100 | [diff] [blame] | 1426 | goto out; |
xypron.glpk@gmx.de | 2cfad48 | 2017-07-11 22:06:17 +0200 | [diff] [blame] | 1427 | } |
Heinrich Schuchardt | 86637f3 | 2018-01-11 08:16:05 +0100 | [diff] [blame] | 1428 | r = efi_remove_protocol(handle, protocol, protocol_interface); |
Ilias Apalodimas | c76eade | 2023-08-24 17:21:09 +0300 | [diff] [blame] | 1429 | if (r != EFI_SUCCESS) |
| 1430 | return r; |
| 1431 | /* |
| 1432 | * We don't care about the return value here since the |
| 1433 | * handle might have more protocols installed |
| 1434 | */ |
| 1435 | if (!preserve) |
| 1436 | efi_purge_handle(handle); |
xypron.glpk@gmx.de | 2cfad48 | 2017-07-11 22:06:17 +0200 | [diff] [blame] | 1437 | out: |
Heinrich Schuchardt | 1b45134 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 1438 | return r; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1439 | } |
| 1440 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1441 | /** |
Heinrich Schuchardt | 1b45134 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 1442 | * efi_uninstall_protocol_interface() - uninstall protocol interface |
| 1443 | * @handle: handle from which the protocol shall be removed |
| 1444 | * @protocol: GUID of the protocol to be removed |
| 1445 | * @protocol_interface: interface to be removed |
| 1446 | * |
| 1447 | * This function implements the UninstallProtocolInterface service. |
| 1448 | * |
| 1449 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1450 | * details. |
| 1451 | * |
| 1452 | * Return: status code |
| 1453 | */ |
| 1454 | static efi_status_t EFIAPI efi_uninstall_protocol_interface |
| 1455 | (efi_handle_t handle, const efi_guid_t *protocol, |
| 1456 | void *protocol_interface) |
| 1457 | { |
| 1458 | efi_status_t ret; |
| 1459 | |
Heinrich Schuchardt | 282249d | 2022-01-16 14:15:31 +0100 | [diff] [blame] | 1460 | EFI_ENTRY("%p, %pUs, %p", handle, protocol, protocol_interface); |
Heinrich Schuchardt | 1b45134 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 1461 | |
Ilias Apalodimas | c76eade | 2023-08-24 17:21:09 +0300 | [diff] [blame] | 1462 | ret = efi_uninstall_protocol(handle, protocol, protocol_interface, false); |
Heinrich Schuchardt | 1b45134 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 1463 | if (ret != EFI_SUCCESS) |
| 1464 | goto out; |
| 1465 | |
Heinrich Schuchardt | 1b45134 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 1466 | out: |
| 1467 | return EFI_EXIT(ret); |
| 1468 | } |
| 1469 | |
| 1470 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1471 | * efi_register_protocol_notify() - register an event for notification when a |
| 1472 | * protocol is installed. |
| 1473 | * @protocol: GUID of the protocol whose installation shall be notified |
| 1474 | * @event: event to be signaled upon installation of the protocol |
| 1475 | * @registration: key for retrieving the registration information |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1476 | * |
| 1477 | * This function implements the RegisterProtocolNotify service. |
| 1478 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 1479 | * for details. |
| 1480 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1481 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1482 | */ |
Jose Marinho | ebb61ee | 2021-03-02 17:26:38 +0000 | [diff] [blame] | 1483 | efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol, |
| 1484 | struct efi_event *event, |
| 1485 | void **registration) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1486 | { |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1487 | struct efi_register_notify_event *item; |
| 1488 | efi_status_t ret = EFI_SUCCESS; |
| 1489 | |
Heinrich Schuchardt | 282249d | 2022-01-16 14:15:31 +0100 | [diff] [blame] | 1490 | EFI_ENTRY("%pUs, %p, %p", protocol, event, registration); |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1491 | |
| 1492 | if (!protocol || !event || !registration) { |
| 1493 | ret = EFI_INVALID_PARAMETER; |
| 1494 | goto out; |
| 1495 | } |
| 1496 | |
| 1497 | item = calloc(1, sizeof(struct efi_register_notify_event)); |
| 1498 | if (!item) { |
| 1499 | ret = EFI_OUT_OF_RESOURCES; |
| 1500 | goto out; |
| 1501 | } |
| 1502 | |
| 1503 | item->event = event; |
Sughosh Ganu | 196f66d | 2019-12-29 00:01:04 +0530 | [diff] [blame] | 1504 | guidcpy(&item->protocol, protocol); |
Heinrich Schuchardt | d375e75 | 2019-05-21 18:19:01 +0200 | [diff] [blame] | 1505 | INIT_LIST_HEAD(&item->handles); |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1506 | |
| 1507 | list_add_tail(&item->link, &efi_register_notify_events); |
| 1508 | |
| 1509 | *registration = item; |
| 1510 | out: |
| 1511 | return EFI_EXIT(ret); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1512 | } |
| 1513 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1514 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1515 | * efi_search() - determine if an EFI handle implements a protocol |
Heinrich Schuchardt | 41cc3c1 | 2019-07-14 11:05:34 +0200 | [diff] [blame] | 1516 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1517 | * @search_type: selection criterion |
| 1518 | * @protocol: GUID of the protocol |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1519 | * @handle: handle |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1520 | * |
| 1521 | * See the documentation of the LocateHandle service in the UEFI specification. |
| 1522 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1523 | * Return: 0 if the handle implements the protocol |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1524 | */ |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1525 | static int efi_search(enum efi_locate_search_type search_type, |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1526 | const efi_guid_t *protocol, efi_handle_t handle) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1527 | { |
Heinrich Schuchardt | 6a43075 | 2017-10-26 19:25:55 +0200 | [diff] [blame] | 1528 | efi_status_t ret; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1529 | |
| 1530 | switch (search_type) { |
Heinrich Schuchardt | 68845f0 | 2017-11-06 21:17:42 +0100 | [diff] [blame] | 1531 | case ALL_HANDLES: |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1532 | return 0; |
Heinrich Schuchardt | 68845f0 | 2017-11-06 21:17:42 +0100 | [diff] [blame] | 1533 | case BY_PROTOCOL: |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1534 | ret = efi_search_protocol(handle, protocol, NULL); |
Heinrich Schuchardt | 6a43075 | 2017-10-26 19:25:55 +0200 | [diff] [blame] | 1535 | return (ret != EFI_SUCCESS); |
| 1536 | default: |
| 1537 | /* Invalid search type */ |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1538 | return -1; |
| 1539 | } |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1540 | } |
| 1541 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1542 | /** |
Heinrich Schuchardt | eb61652 | 2019-05-29 07:46:33 +0200 | [diff] [blame] | 1543 | * efi_check_register_notify_event() - check if registration key is valid |
| 1544 | * |
| 1545 | * Check that a pointer is a valid registration key as returned by |
| 1546 | * RegisterProtocolNotify(). |
| 1547 | * |
| 1548 | * @key: registration key |
| 1549 | * Return: valid registration key or NULL |
| 1550 | */ |
| 1551 | static struct efi_register_notify_event *efi_check_register_notify_event |
| 1552 | (void *key) |
| 1553 | { |
| 1554 | struct efi_register_notify_event *event; |
| 1555 | |
| 1556 | list_for_each_entry(event, &efi_register_notify_events, link) { |
| 1557 | if (event == (struct efi_register_notify_event *)key) |
| 1558 | return event; |
| 1559 | } |
| 1560 | return NULL; |
| 1561 | } |
| 1562 | |
| 1563 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1564 | * efi_locate_handle() - locate handles implementing a protocol |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1565 | * |
| 1566 | * @search_type: selection criterion |
| 1567 | * @protocol: GUID of the protocol |
| 1568 | * @search_key: registration key |
| 1569 | * @buffer_size: size of the buffer to receive the handles in bytes |
| 1570 | * @buffer: buffer to receive the relevant handles |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1571 | * |
| 1572 | * This function is meant for U-Boot internal calls. For the API implementation |
| 1573 | * of the LocateHandle service see efi_locate_handle_ext. |
| 1574 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1575 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1576 | */ |
xypron.glpk@gmx.de | cab4dd5 | 2017-08-09 20:55:00 +0200 | [diff] [blame] | 1577 | static efi_status_t efi_locate_handle( |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1578 | enum efi_locate_search_type search_type, |
Heinrich Schuchardt | e547c66 | 2017-10-05 16:35:53 +0200 | [diff] [blame] | 1579 | const efi_guid_t *protocol, void *search_key, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 1580 | efi_uintn_t *buffer_size, efi_handle_t *buffer) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1581 | { |
Heinrich Schuchardt | ec66fc8 | 2017-11-06 21:17:49 +0100 | [diff] [blame] | 1582 | struct efi_object *efiobj; |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 1583 | efi_uintn_t size = 0; |
Heinrich Schuchardt | eb61652 | 2019-05-29 07:46:33 +0200 | [diff] [blame] | 1584 | struct efi_register_notify_event *event; |
Heinrich Schuchardt | d375e75 | 2019-05-21 18:19:01 +0200 | [diff] [blame] | 1585 | struct efi_protocol_notification *handle = NULL; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1586 | |
Heinrich Schuchardt | ec66fc8 | 2017-11-06 21:17:49 +0100 | [diff] [blame] | 1587 | /* Check parameters */ |
| 1588 | switch (search_type) { |
| 1589 | case ALL_HANDLES: |
| 1590 | break; |
| 1591 | case BY_REGISTER_NOTIFY: |
| 1592 | if (!search_key) |
| 1593 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1594 | /* Check that the registration key is valid */ |
Heinrich Schuchardt | eb61652 | 2019-05-29 07:46:33 +0200 | [diff] [blame] | 1595 | event = efi_check_register_notify_event(search_key); |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1596 | if (!event) |
| 1597 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1598 | break; |
Heinrich Schuchardt | ec66fc8 | 2017-11-06 21:17:49 +0100 | [diff] [blame] | 1599 | case BY_PROTOCOL: |
| 1600 | if (!protocol) |
| 1601 | return EFI_INVALID_PARAMETER; |
| 1602 | break; |
| 1603 | default: |
| 1604 | return EFI_INVALID_PARAMETER; |
| 1605 | } |
| 1606 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1607 | /* Count how much space we need */ |
Heinrich Schuchardt | d375e75 | 2019-05-21 18:19:01 +0200 | [diff] [blame] | 1608 | if (search_type == BY_REGISTER_NOTIFY) { |
| 1609 | if (list_empty(&event->handles)) |
| 1610 | return EFI_NOT_FOUND; |
| 1611 | handle = list_first_entry(&event->handles, |
| 1612 | struct efi_protocol_notification, |
| 1613 | link); |
| 1614 | efiobj = handle->handle; |
| 1615 | size += sizeof(void *); |
| 1616 | } else { |
| 1617 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
| 1618 | if (!efi_search(search_type, protocol, efiobj)) |
| 1619 | size += sizeof(void *); |
| 1620 | } |
| 1621 | if (size == 0) |
| 1622 | return EFI_NOT_FOUND; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1623 | } |
| 1624 | |
Heinrich Schuchardt | c0ee5f3 | 2019-05-04 17:37:32 +0200 | [diff] [blame] | 1625 | if (!buffer_size) |
| 1626 | return EFI_INVALID_PARAMETER; |
| 1627 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1628 | if (*buffer_size < size) { |
| 1629 | *buffer_size = size; |
xypron.glpk@gmx.de | 69f9403 | 2017-07-11 22:06:21 +0200 | [diff] [blame] | 1630 | return EFI_BUFFER_TOO_SMALL; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1631 | } |
| 1632 | |
Rob Clark | cdee337 | 2017-08-06 14:10:07 -0400 | [diff] [blame] | 1633 | *buffer_size = size; |
Heinrich Schuchardt | c0ee5f3 | 2019-05-04 17:37:32 +0200 | [diff] [blame] | 1634 | |
Heinrich Schuchardt | c97f947 | 2019-05-10 19:03:49 +0200 | [diff] [blame] | 1635 | /* The buffer size is sufficient but there is no buffer */ |
Heinrich Schuchardt | c0ee5f3 | 2019-05-04 17:37:32 +0200 | [diff] [blame] | 1636 | if (!buffer) |
| 1637 | return EFI_INVALID_PARAMETER; |
Rob Clark | cdee337 | 2017-08-06 14:10:07 -0400 | [diff] [blame] | 1638 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1639 | /* Then fill the array */ |
Heinrich Schuchardt | d375e75 | 2019-05-21 18:19:01 +0200 | [diff] [blame] | 1640 | if (search_type == BY_REGISTER_NOTIFY) { |
| 1641 | *buffer = efiobj; |
| 1642 | list_del(&handle->link); |
| 1643 | } else { |
| 1644 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
| 1645 | if (!efi_search(search_type, protocol, efiobj)) |
| 1646 | *buffer++ = efiobj; |
| 1647 | } |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1648 | } |
| 1649 | |
xypron.glpk@gmx.de | 69f9403 | 2017-07-11 22:06:21 +0200 | [diff] [blame] | 1650 | return EFI_SUCCESS; |
| 1651 | } |
| 1652 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1653 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1654 | * efi_locate_handle_ext() - locate handles implementing a protocol. |
| 1655 | * @search_type: selection criterion |
| 1656 | * @protocol: GUID of the protocol |
| 1657 | * @search_key: registration key |
| 1658 | * @buffer_size: size of the buffer to receive the handles in bytes |
| 1659 | * @buffer: buffer to receive the relevant handles |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1660 | * |
| 1661 | * This function implements the LocateHandle service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1662 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1663 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1664 | * details. |
| 1665 | * |
| 1666 | * Return: 0 if the handle implements the protocol |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1667 | */ |
xypron.glpk@gmx.de | 69f9403 | 2017-07-11 22:06:21 +0200 | [diff] [blame] | 1668 | static efi_status_t EFIAPI efi_locate_handle_ext( |
| 1669 | enum efi_locate_search_type search_type, |
Heinrich Schuchardt | e547c66 | 2017-10-05 16:35:53 +0200 | [diff] [blame] | 1670 | const efi_guid_t *protocol, void *search_key, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 1671 | efi_uintn_t *buffer_size, efi_handle_t *buffer) |
xypron.glpk@gmx.de | 69f9403 | 2017-07-11 22:06:21 +0200 | [diff] [blame] | 1672 | { |
Heinrich Schuchardt | 282249d | 2022-01-16 14:15:31 +0100 | [diff] [blame] | 1673 | EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key, |
xypron.glpk@gmx.de | 69f9403 | 2017-07-11 22:06:21 +0200 | [diff] [blame] | 1674 | buffer_size, buffer); |
| 1675 | |
| 1676 | return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key, |
| 1677 | buffer_size, buffer)); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1678 | } |
| 1679 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1680 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1681 | * efi_remove_configuration_table() - collapses configuration table entries, |
| 1682 | * removing index i |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1683 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1684 | * @i: index of the table entry to be removed |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1685 | */ |
Alexander Graf | fe3366f | 2017-07-26 13:41:04 +0200 | [diff] [blame] | 1686 | static void efi_remove_configuration_table(int i) |
| 1687 | { |
Heinrich Schuchardt | 2f528c2 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 1688 | struct efi_configuration_table *this = &systab.tables[i]; |
| 1689 | struct efi_configuration_table *next = &systab.tables[i + 1]; |
| 1690 | struct efi_configuration_table *end = &systab.tables[systab.nr_tables]; |
Alexander Graf | fe3366f | 2017-07-26 13:41:04 +0200 | [diff] [blame] | 1691 | |
| 1692 | memmove(this, next, (ulong)end - (ulong)next); |
| 1693 | systab.nr_tables--; |
| 1694 | } |
| 1695 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1696 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1697 | * efi_install_configuration_table() - adds, updates, or removes a |
| 1698 | * configuration table |
| 1699 | * @guid: GUID of the installed table |
| 1700 | * @table: table to be installed |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1701 | * |
| 1702 | * This function is used for internal calls. For the API implementation of the |
| 1703 | * InstallConfigurationTable service see efi_install_configuration_table_ext. |
| 1704 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1705 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1706 | */ |
Heinrich Schuchardt | 9106459 | 2018-02-18 15:17:49 +0100 | [diff] [blame] | 1707 | efi_status_t efi_install_configuration_table(const efi_guid_t *guid, |
| 1708 | void *table) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1709 | { |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1710 | struct efi_event *evt; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1711 | int i; |
| 1712 | |
Heinrich Schuchardt | 754ce10 | 2018-02-18 00:08:00 +0100 | [diff] [blame] | 1713 | if (!guid) |
| 1714 | return EFI_INVALID_PARAMETER; |
| 1715 | |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 1716 | /* Check for GUID override */ |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1717 | for (i = 0; i < systab.nr_tables; i++) { |
Heinrich Schuchardt | 2f528c2 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 1718 | if (!guidcmp(guid, &systab.tables[i].guid)) { |
Alexander Graf | fe3366f | 2017-07-26 13:41:04 +0200 | [diff] [blame] | 1719 | if (table) |
Heinrich Schuchardt | 2f528c2 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 1720 | systab.tables[i].table = table; |
Alexander Graf | fe3366f | 2017-07-26 13:41:04 +0200 | [diff] [blame] | 1721 | else |
| 1722 | efi_remove_configuration_table(i); |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1723 | goto out; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1724 | } |
| 1725 | } |
| 1726 | |
Alexander Graf | fe3366f | 2017-07-26 13:41:04 +0200 | [diff] [blame] | 1727 | if (!table) |
| 1728 | return EFI_NOT_FOUND; |
| 1729 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1730 | /* No override, check for overflow */ |
Heinrich Schuchardt | 2f528c2 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 1731 | if (i >= EFI_MAX_CONFIGURATION_TABLES) |
Alexander Graf | c5c1163 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 1732 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1733 | |
| 1734 | /* Add a new entry */ |
Sughosh Ganu | 196f66d | 2019-12-29 00:01:04 +0530 | [diff] [blame] | 1735 | guidcpy(&systab.tables[i].guid, guid); |
Heinrich Schuchardt | 2f528c2 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 1736 | systab.tables[i].table = table; |
Alexander Graf | 9982e67 | 2016-08-19 01:23:30 +0200 | [diff] [blame] | 1737 | systab.nr_tables = i + 1; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1738 | |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1739 | out: |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 1740 | /* systab.nr_tables may have changed. So we need to update the CRC32 */ |
Heinrich Schuchardt | ac2d4da | 2018-07-07 15:36:05 +0200 | [diff] [blame] | 1741 | efi_update_table_header_crc32(&systab.hdr); |
| 1742 | |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1743 | /* Notify that the configuration table was changed */ |
| 1744 | list_for_each_entry(evt, &efi_events, link) { |
| 1745 | if (evt->group && !guidcmp(evt->group, guid)) { |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 1746 | efi_signal_event(evt); |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1747 | break; |
| 1748 | } |
| 1749 | } |
| 1750 | |
Alexander Graf | c5c1163 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 1751 | return EFI_SUCCESS; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1752 | } |
| 1753 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1754 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1755 | * efi_install_configuration_table_ex() - Adds, updates, or removes a |
| 1756 | * configuration table. |
| 1757 | * @guid: GUID of the installed table |
| 1758 | * @table: table to be installed |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1759 | * |
| 1760 | * This function implements the InstallConfigurationTable service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1761 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1762 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1763 | * details. |
| 1764 | * |
| 1765 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1766 | */ |
Masahisa Kojima | bc38d77 | 2021-10-22 20:24:24 +0900 | [diff] [blame] | 1767 | static efi_status_t |
| 1768 | EFIAPI efi_install_configuration_table_ext(const efi_guid_t *guid, |
| 1769 | void *table) |
Alexander Graf | c5c1163 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 1770 | { |
Heinrich Schuchardt | 282249d | 2022-01-16 14:15:31 +0100 | [diff] [blame] | 1771 | EFI_ENTRY("%pUs, %p", guid, table); |
Alexander Graf | c5c1163 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 1772 | return EFI_EXIT(efi_install_configuration_table(guid, table)); |
| 1773 | } |
| 1774 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1775 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1776 | * efi_setup_loaded_image() - initialize a loaded image |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1777 | * |
| 1778 | * Initialize a loaded_image_info and loaded_image_info object with correct |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 1779 | * protocols, boot-device, etc. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1780 | * |
Heinrich Schuchardt | 41cc3c1 | 2019-07-14 11:05:34 +0200 | [diff] [blame] | 1781 | * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error |
Heinrich Schuchardt | 6346a90 | 2019-02-06 19:41:29 +0100 | [diff] [blame] | 1782 | * code is returned. |
| 1783 | * |
| 1784 | * @device_path: device path of the loaded image |
| 1785 | * @file_path: file path of the loaded image |
| 1786 | * @handle_ptr: handle of the loaded image |
| 1787 | * @info_ptr: loaded image protocol |
| 1788 | * Return: status code |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 1789 | */ |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 1790 | efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path, |
| 1791 | struct efi_device_path *file_path, |
| 1792 | struct efi_loaded_image_obj **handle_ptr, |
| 1793 | struct efi_loaded_image **info_ptr) |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 1794 | { |
Heinrich Schuchardt | 24d3a66 | 2017-10-26 19:25:58 +0200 | [diff] [blame] | 1795 | efi_status_t ret; |
Heinrich Schuchardt | 6346a90 | 2019-02-06 19:41:29 +0100 | [diff] [blame] | 1796 | struct efi_loaded_image *info = NULL; |
| 1797 | struct efi_loaded_image_obj *obj = NULL; |
AKASHI Takahiro | bbe13da | 2019-03-27 13:40:32 +0900 | [diff] [blame] | 1798 | struct efi_device_path *dp; |
Heinrich Schuchardt | 6346a90 | 2019-02-06 19:41:29 +0100 | [diff] [blame] | 1799 | |
| 1800 | /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */ |
| 1801 | *handle_ptr = NULL; |
| 1802 | *info_ptr = NULL; |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 1803 | |
| 1804 | info = calloc(1, sizeof(*info)); |
| 1805 | if (!info) |
| 1806 | return EFI_OUT_OF_RESOURCES; |
| 1807 | obj = calloc(1, sizeof(*obj)); |
| 1808 | if (!obj) { |
| 1809 | free(info); |
| 1810 | return EFI_OUT_OF_RESOURCES; |
| 1811 | } |
Heinrich Schuchardt | b27ced4 | 2019-05-01 14:20:18 +0200 | [diff] [blame] | 1812 | obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE; |
Heinrich Schuchardt | 24d3a66 | 2017-10-26 19:25:58 +0200 | [diff] [blame] | 1813 | |
Heinrich Schuchardt | 967d7de | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 1814 | /* Add internal object to object list */ |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 1815 | efi_add_handle(&obj->header); |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 1816 | |
Heinrich Schuchardt | c47f870 | 2018-07-05 08:17:58 +0200 | [diff] [blame] | 1817 | info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION; |
Heinrich Schuchardt | 24d3a66 | 2017-10-26 19:25:58 +0200 | [diff] [blame] | 1818 | info->file_path = file_path; |
Heinrich Schuchardt | 8a7e09d | 2018-08-26 15:31:52 +0200 | [diff] [blame] | 1819 | info->system_table = &systab; |
Heinrich Schuchardt | 24d3a66 | 2017-10-26 19:25:58 +0200 | [diff] [blame] | 1820 | |
Heinrich Schuchardt | f3996be | 2018-01-26 06:50:54 +0100 | [diff] [blame] | 1821 | if (device_path) { |
Heinrich Schuchardt | 0a04a41 | 2022-03-19 06:35:43 +0100 | [diff] [blame] | 1822 | info->device_handle = efi_dp_find_obj(device_path, NULL, NULL); |
AKASHI Takahiro | bbe13da | 2019-03-27 13:40:32 +0900 | [diff] [blame] | 1823 | |
| 1824 | dp = efi_dp_append(device_path, file_path); |
| 1825 | if (!dp) { |
| 1826 | ret = EFI_OUT_OF_RESOURCES; |
Heinrich Schuchardt | f3996be | 2018-01-26 06:50:54 +0100 | [diff] [blame] | 1827 | goto failure; |
AKASHI Takahiro | bbe13da | 2019-03-27 13:40:32 +0900 | [diff] [blame] | 1828 | } |
| 1829 | } else { |
| 1830 | dp = NULL; |
Heinrich Schuchardt | f3996be | 2018-01-26 06:50:54 +0100 | [diff] [blame] | 1831 | } |
AKASHI Takahiro | bbe13da | 2019-03-27 13:40:32 +0900 | [diff] [blame] | 1832 | ret = efi_add_protocol(&obj->header, |
| 1833 | &efi_guid_loaded_image_device_path, dp); |
| 1834 | if (ret != EFI_SUCCESS) |
| 1835 | goto failure; |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 1836 | |
| 1837 | /* |
| 1838 | * When asking for the loaded_image interface, just |
| 1839 | * return handle which points to loaded_image_info |
| 1840 | */ |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 1841 | ret = efi_add_protocol(&obj->header, |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 1842 | &efi_guid_loaded_image, info); |
Heinrich Schuchardt | 24d3a66 | 2017-10-26 19:25:58 +0200 | [diff] [blame] | 1843 | if (ret != EFI_SUCCESS) |
| 1844 | goto failure; |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 1845 | |
Heinrich Schuchardt | d20f512 | 2019-03-19 18:58:58 +0100 | [diff] [blame] | 1846 | *info_ptr = info; |
| 1847 | *handle_ptr = obj; |
Heinrich Schuchardt | 6346a90 | 2019-02-06 19:41:29 +0100 | [diff] [blame] | 1848 | |
Heinrich Schuchardt | 7db9f89 | 2017-12-04 18:03:01 +0100 | [diff] [blame] | 1849 | return ret; |
Heinrich Schuchardt | 24d3a66 | 2017-10-26 19:25:58 +0200 | [diff] [blame] | 1850 | failure: |
| 1851 | printf("ERROR: Failure to install protocols for loaded image\n"); |
Heinrich Schuchardt | 6346a90 | 2019-02-06 19:41:29 +0100 | [diff] [blame] | 1852 | efi_delete_handle(&obj->header); |
| 1853 | free(info); |
Heinrich Schuchardt | 7db9f89 | 2017-12-04 18:03:01 +0100 | [diff] [blame] | 1854 | return ret; |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 1855 | } |
| 1856 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1857 | /** |
Heinrich Schuchardt | 1d59a57 | 2020-12-04 03:02:03 +0100 | [diff] [blame] | 1858 | * efi_locate_device_path() - Get the device path and handle of an device |
| 1859 | * implementing a protocol |
| 1860 | * @protocol: GUID of the protocol |
| 1861 | * @device_path: device path |
| 1862 | * @device: handle of the device |
| 1863 | * |
| 1864 | * This function implements the LocateDevicePath service. |
| 1865 | * |
| 1866 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1867 | * details. |
| 1868 | * |
| 1869 | * Return: status code |
| 1870 | */ |
AKASHI Takahiro | 537a693 | 2022-04-28 17:09:38 +0900 | [diff] [blame] | 1871 | efi_status_t EFIAPI efi_locate_device_path(const efi_guid_t *protocol, |
| 1872 | struct efi_device_path **device_path, |
| 1873 | efi_handle_t *device) |
Heinrich Schuchardt | 1d59a57 | 2020-12-04 03:02:03 +0100 | [diff] [blame] | 1874 | { |
| 1875 | struct efi_device_path *dp; |
| 1876 | size_t i; |
| 1877 | struct efi_handler *handler; |
| 1878 | efi_handle_t *handles; |
| 1879 | size_t len, len_dp; |
| 1880 | size_t len_best = 0; |
| 1881 | efi_uintn_t no_handles; |
| 1882 | u8 *remainder; |
| 1883 | efi_status_t ret; |
| 1884 | |
Heinrich Schuchardt | 282249d | 2022-01-16 14:15:31 +0100 | [diff] [blame] | 1885 | EFI_ENTRY("%pUs, %p, %p", protocol, device_path, device); |
Heinrich Schuchardt | 1d59a57 | 2020-12-04 03:02:03 +0100 | [diff] [blame] | 1886 | |
| 1887 | if (!protocol || !device_path || !*device_path) { |
| 1888 | ret = EFI_INVALID_PARAMETER; |
| 1889 | goto out; |
| 1890 | } |
| 1891 | |
| 1892 | /* Find end of device path */ |
| 1893 | len = efi_dp_instance_size(*device_path); |
| 1894 | |
| 1895 | /* Get all handles implementing the protocol */ |
| 1896 | ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL, |
| 1897 | &no_handles, &handles)); |
| 1898 | if (ret != EFI_SUCCESS) |
| 1899 | goto out; |
| 1900 | |
| 1901 | for (i = 0; i < no_handles; ++i) { |
| 1902 | /* Find the device path protocol */ |
| 1903 | ret = efi_search_protocol(handles[i], &efi_guid_device_path, |
| 1904 | &handler); |
| 1905 | if (ret != EFI_SUCCESS) |
| 1906 | continue; |
| 1907 | dp = (struct efi_device_path *)handler->protocol_interface; |
| 1908 | len_dp = efi_dp_instance_size(dp); |
| 1909 | /* |
| 1910 | * This handle can only be a better fit |
| 1911 | * if its device path length is longer than the best fit and |
| 1912 | * if its device path length is shorter of equal the searched |
| 1913 | * device path. |
| 1914 | */ |
| 1915 | if (len_dp <= len_best || len_dp > len) |
| 1916 | continue; |
| 1917 | /* Check if dp is a subpath of device_path */ |
| 1918 | if (memcmp(*device_path, dp, len_dp)) |
| 1919 | continue; |
| 1920 | if (!device) { |
| 1921 | ret = EFI_INVALID_PARAMETER; |
| 1922 | goto out; |
| 1923 | } |
| 1924 | *device = handles[i]; |
| 1925 | len_best = len_dp; |
| 1926 | } |
| 1927 | if (len_best) { |
| 1928 | remainder = (u8 *)*device_path + len_best; |
| 1929 | *device_path = (struct efi_device_path *)remainder; |
| 1930 | ret = EFI_SUCCESS; |
| 1931 | } else { |
| 1932 | ret = EFI_NOT_FOUND; |
| 1933 | } |
| 1934 | out: |
| 1935 | return EFI_EXIT(ret); |
| 1936 | } |
| 1937 | |
| 1938 | /** |
Heinrich Schuchardt | 3dd733e | 2020-12-06 10:47:57 +0100 | [diff] [blame] | 1939 | * efi_load_image_from_file() - load an image from file system |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1940 | * |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1941 | * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the |
| 1942 | * callers obligation to update the memory type as needed. |
| 1943 | * |
Heinrich Schuchardt | 471db44 | 2020-12-04 09:27:41 +0100 | [diff] [blame] | 1944 | * @file_path: the path of the image to load |
| 1945 | * @buffer: buffer containing the loaded image |
| 1946 | * @size: size of the loaded image |
| 1947 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1948 | */ |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 1949 | static |
Heinrich Schuchardt | 3dd733e | 2020-12-06 10:47:57 +0100 | [diff] [blame] | 1950 | efi_status_t efi_load_image_from_file(struct efi_device_path *file_path, |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1951 | void **buffer, efi_uintn_t *size) |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1952 | { |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1953 | struct efi_file_handle *f; |
Heinrich Schuchardt | 3dd733e | 2020-12-06 10:47:57 +0100 | [diff] [blame] | 1954 | efi_status_t ret; |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1955 | u64 addr; |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 1956 | efi_uintn_t bs; |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1957 | |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1958 | /* Open file */ |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1959 | f = efi_file_from_path(file_path); |
| 1960 | if (!f) |
Heinrich Schuchardt | 6a41cf2 | 2019-06-11 19:00:56 +0200 | [diff] [blame] | 1961 | return EFI_NOT_FOUND; |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1962 | |
Ilias Apalodimas | 03d0d76 | 2021-03-25 21:55:16 +0200 | [diff] [blame] | 1963 | ret = efi_file_size(f, &bs); |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1964 | if (ret != EFI_SUCCESS) |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1965 | goto error; |
| 1966 | |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1967 | /* |
| 1968 | * When reading the file we do not yet know if it contains an |
| 1969 | * application, a boottime driver, or a runtime driver. So here we |
| 1970 | * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to |
| 1971 | * update the reservation according to the image type. |
| 1972 | */ |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1973 | ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, |
| 1974 | EFI_BOOT_SERVICES_DATA, |
| 1975 | efi_size_in_pages(bs), &addr); |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1976 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1977 | ret = EFI_OUT_OF_RESOURCES; |
| 1978 | goto error; |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1979 | } |
| 1980 | |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1981 | /* Read file */ |
| 1982 | EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr)); |
| 1983 | if (ret != EFI_SUCCESS) |
| 1984 | efi_free_pages(addr, efi_size_in_pages(bs)); |
| 1985 | *buffer = (void *)(uintptr_t)addr; |
| 1986 | *size = bs; |
| 1987 | error: |
| 1988 | EFI_CALL(f->close(f)); |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1989 | return ret; |
| 1990 | } |
| 1991 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1992 | /** |
Heinrich Schuchardt | 3dd733e | 2020-12-06 10:47:57 +0100 | [diff] [blame] | 1993 | * efi_load_image_from_path() - load an image using a file path |
| 1994 | * |
| 1995 | * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the |
| 1996 | * callers obligation to update the memory type as needed. |
| 1997 | * |
| 1998 | * @boot_policy: true for request originating from the boot manager |
| 1999 | * @file_path: the path of the image to load |
| 2000 | * @buffer: buffer containing the loaded image |
| 2001 | * @size: size of the loaded image |
| 2002 | * Return: status code |
| 2003 | */ |
| 2004 | static |
| 2005 | efi_status_t efi_load_image_from_path(bool boot_policy, |
| 2006 | struct efi_device_path *file_path, |
| 2007 | void **buffer, efi_uintn_t *size) |
| 2008 | { |
| 2009 | efi_handle_t device; |
| 2010 | efi_status_t ret; |
Heinrich Schuchardt | a1d8246 | 2022-02-26 12:05:30 +0100 | [diff] [blame] | 2011 | struct efi_device_path *dp, *rem; |
Heinrich Schuchardt | 8260827 | 2020-12-06 13:00:15 +0100 | [diff] [blame] | 2012 | struct efi_load_file_protocol *load_file_protocol = NULL; |
| 2013 | efi_uintn_t buffer_size; |
| 2014 | uint64_t addr, pages; |
| 2015 | const efi_guid_t *guid; |
Heinrich Schuchardt | 94d7267 | 2023-01-24 20:36:45 +0100 | [diff] [blame] | 2016 | struct efi_handler *handler; |
Heinrich Schuchardt | 3dd733e | 2020-12-06 10:47:57 +0100 | [diff] [blame] | 2017 | |
| 2018 | /* In case of failure nothing is returned */ |
| 2019 | *buffer = NULL; |
| 2020 | *size = 0; |
| 2021 | |
| 2022 | dp = file_path; |
Heinrich Schuchardt | a1d8246 | 2022-02-26 12:05:30 +0100 | [diff] [blame] | 2023 | device = efi_dp_find_obj(dp, NULL, &rem); |
| 2024 | ret = efi_search_protocol(device, &efi_simple_file_system_protocol_guid, |
| 2025 | NULL); |
Heinrich Schuchardt | 3dd733e | 2020-12-06 10:47:57 +0100 | [diff] [blame] | 2026 | if (ret == EFI_SUCCESS) |
| 2027 | return efi_load_image_from_file(file_path, buffer, size); |
Heinrich Schuchardt | 8260827 | 2020-12-06 13:00:15 +0100 | [diff] [blame] | 2028 | |
Heinrich Schuchardt | a1d8246 | 2022-02-26 12:05:30 +0100 | [diff] [blame] | 2029 | ret = efi_search_protocol(device, &efi_guid_load_file_protocol, NULL); |
Heinrich Schuchardt | 8260827 | 2020-12-06 13:00:15 +0100 | [diff] [blame] | 2030 | if (ret == EFI_SUCCESS) { |
| 2031 | guid = &efi_guid_load_file_protocol; |
| 2032 | } else if (!boot_policy) { |
| 2033 | guid = &efi_guid_load_file2_protocol; |
Heinrich Schuchardt | a1d8246 | 2022-02-26 12:05:30 +0100 | [diff] [blame] | 2034 | ret = efi_search_protocol(device, guid, NULL); |
Heinrich Schuchardt | 8260827 | 2020-12-06 13:00:15 +0100 | [diff] [blame] | 2035 | } |
| 2036 | if (ret != EFI_SUCCESS) |
| 2037 | return EFI_NOT_FOUND; |
Heinrich Schuchardt | 94d7267 | 2023-01-24 20:36:45 +0100 | [diff] [blame] | 2038 | ret = efi_search_protocol(device, guid, &handler); |
Heinrich Schuchardt | 8260827 | 2020-12-06 13:00:15 +0100 | [diff] [blame] | 2039 | if (ret != EFI_SUCCESS) |
| 2040 | return EFI_NOT_FOUND; |
| 2041 | buffer_size = 0; |
Heinrich Schuchardt | 94d7267 | 2023-01-24 20:36:45 +0100 | [diff] [blame] | 2042 | load_file_protocol = handler->protocol_interface; |
Heinrich Schuchardt | a1d8246 | 2022-02-26 12:05:30 +0100 | [diff] [blame] | 2043 | ret = EFI_CALL(load_file_protocol->load_file( |
| 2044 | load_file_protocol, rem, boot_policy, |
| 2045 | &buffer_size, NULL)); |
Heinrich Schuchardt | 8260827 | 2020-12-06 13:00:15 +0100 | [diff] [blame] | 2046 | if (ret != EFI_BUFFER_TOO_SMALL) |
| 2047 | goto out; |
| 2048 | pages = efi_size_in_pages(buffer_size); |
| 2049 | ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA, |
| 2050 | pages, &addr); |
| 2051 | if (ret != EFI_SUCCESS) { |
| 2052 | ret = EFI_OUT_OF_RESOURCES; |
| 2053 | goto out; |
| 2054 | } |
| 2055 | ret = EFI_CALL(load_file_protocol->load_file( |
Heinrich Schuchardt | a1d8246 | 2022-02-26 12:05:30 +0100 | [diff] [blame] | 2056 | load_file_protocol, rem, boot_policy, |
Heinrich Schuchardt | 8260827 | 2020-12-06 13:00:15 +0100 | [diff] [blame] | 2057 | &buffer_size, (void *)(uintptr_t)addr)); |
| 2058 | if (ret != EFI_SUCCESS) |
| 2059 | efi_free_pages(addr, pages); |
| 2060 | out: |
Heinrich Schuchardt | 5e39433 | 2022-10-07 15:18:15 +0200 | [diff] [blame] | 2061 | efi_close_protocol(device, guid, efi_root, NULL); |
Heinrich Schuchardt | 8260827 | 2020-12-06 13:00:15 +0100 | [diff] [blame] | 2062 | if (ret == EFI_SUCCESS) { |
| 2063 | *buffer = (void *)(uintptr_t)addr; |
| 2064 | *size = buffer_size; |
| 2065 | } |
| 2066 | |
| 2067 | return ret; |
Heinrich Schuchardt | 3dd733e | 2020-12-06 10:47:57 +0100 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2071 | * efi_load_image() - load an EFI image into memory |
| 2072 | * @boot_policy: true for request originating from the boot manager |
| 2073 | * @parent_image: the caller's image handle |
| 2074 | * @file_path: the path of the image to load |
| 2075 | * @source_buffer: memory location from which the image is installed |
| 2076 | * @source_size: size of the memory area from which the image is installed |
| 2077 | * @image_handle: handle for the newly installed image |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2078 | * |
| 2079 | * This function implements the LoadImage service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2080 | * |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2081 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 2082 | * for details. |
| 2083 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2084 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2085 | */ |
AKASHI Takahiro | b0ad905 | 2019-03-05 14:53:31 +0900 | [diff] [blame] | 2086 | efi_status_t EFIAPI efi_load_image(bool boot_policy, |
| 2087 | efi_handle_t parent_image, |
| 2088 | struct efi_device_path *file_path, |
| 2089 | void *source_buffer, |
| 2090 | efi_uintn_t source_size, |
| 2091 | efi_handle_t *image_handle) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2092 | { |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 2093 | struct efi_device_path *dp, *fp; |
Tom Rini | 04c4906 | 2018-09-30 10:38:15 -0400 | [diff] [blame] | 2094 | struct efi_loaded_image *info = NULL; |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 2095 | struct efi_loaded_image_obj **image_obj = |
| 2096 | (struct efi_loaded_image_obj **)image_handle; |
Heinrich Schuchardt | d4d7ca9 | 2017-12-04 18:03:03 +0100 | [diff] [blame] | 2097 | efi_status_t ret; |
Heinrich Schuchardt | 47e35a9 | 2019-03-04 17:50:05 +0100 | [diff] [blame] | 2098 | void *dest_buffer; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2099 | |
Heinrich Schuchardt | e1e8a65 | 2022-02-03 22:21:51 +0100 | [diff] [blame] | 2100 | EFI_ENTRY("%d, %p, %pD, %p, %zu, %p", boot_policy, parent_image, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2101 | file_path, source_buffer, source_size, image_handle); |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 2102 | |
Heinrich Schuchardt | b7b1011 | 2019-06-11 18:52:33 +0200 | [diff] [blame] | 2103 | if (!image_handle || (!source_buffer && !file_path) || |
| 2104 | !efi_search_obj(parent_image) || |
| 2105 | /* The parent image handle must refer to a loaded image */ |
| 2106 | !parent_image->type) { |
Heinrich Schuchardt | 2e0a790 | 2019-05-05 16:55:06 +0200 | [diff] [blame] | 2107 | ret = EFI_INVALID_PARAMETER; |
| 2108 | goto error; |
| 2109 | } |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 2110 | |
| 2111 | if (!source_buffer) { |
Heinrich Schuchardt | 471db44 | 2020-12-04 09:27:41 +0100 | [diff] [blame] | 2112 | ret = efi_load_image_from_path(boot_policy, file_path, |
| 2113 | &dest_buffer, &source_size); |
Heinrich Schuchardt | d4d7ca9 | 2017-12-04 18:03:03 +0100 | [diff] [blame] | 2114 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 2115 | goto error; |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 2116 | } else { |
Heinrich Schuchardt | 47e35a9 | 2019-03-04 17:50:05 +0100 | [diff] [blame] | 2117 | dest_buffer = source_buffer; |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 2118 | } |
Heinrich Schuchardt | 28b3917 | 2019-04-20 19:24:43 +0000 | [diff] [blame] | 2119 | /* split file_path which contains both the device and file parts */ |
| 2120 | efi_dp_split_file_path(file_path, &dp, &fp); |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 2121 | ret = efi_setup_loaded_image(dp, fp, image_obj, &info); |
Heinrich Schuchardt | 47e35a9 | 2019-03-04 17:50:05 +0100 | [diff] [blame] | 2122 | if (ret == EFI_SUCCESS) |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 2123 | ret = efi_load_pe(*image_obj, dest_buffer, source_size, info); |
Heinrich Schuchardt | 47e35a9 | 2019-03-04 17:50:05 +0100 | [diff] [blame] | 2124 | if (!source_buffer) |
| 2125 | /* Release buffer to which file was loaded */ |
| 2126 | efi_free_pages((uintptr_t)dest_buffer, |
| 2127 | efi_size_in_pages(source_size)); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 2128 | if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) { |
Heinrich Schuchardt | 47e35a9 | 2019-03-04 17:50:05 +0100 | [diff] [blame] | 2129 | info->system_table = &systab; |
| 2130 | info->parent_handle = parent_image; |
| 2131 | } else { |
| 2132 | /* The image is invalid. Release all associated resources. */ |
| 2133 | efi_delete_handle(*image_handle); |
| 2134 | *image_handle = NULL; |
| 2135 | free(info); |
| 2136 | } |
Heinrich Schuchardt | c935c2f | 2018-03-07 02:40:51 +0100 | [diff] [blame] | 2137 | error: |
Heinrich Schuchardt | d4d7ca9 | 2017-12-04 18:03:03 +0100 | [diff] [blame] | 2138 | return EFI_EXIT(ret); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2139 | } |
| 2140 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2141 | /** |
Alexander Graf | fa5246b | 2018-11-15 21:23:47 +0100 | [diff] [blame] | 2142 | * efi_exit_caches() - fix up caches for EFI payloads if necessary |
| 2143 | */ |
| 2144 | static void efi_exit_caches(void) |
| 2145 | { |
Heinrich Schuchardt | 149d5d4 | 2019-07-22 22:04:36 +0200 | [diff] [blame] | 2146 | #if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND) |
Alexander Graf | fa5246b | 2018-11-15 21:23:47 +0100 | [diff] [blame] | 2147 | /* |
Heinrich Schuchardt | 149d5d4 | 2019-07-22 22:04:36 +0200 | [diff] [blame] | 2148 | * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if |
| 2149 | * caches are enabled. |
| 2150 | * |
| 2151 | * TODO: |
| 2152 | * According to the UEFI spec caches that can be managed via CP15 |
| 2153 | * operations should be enabled. Caches requiring platform information |
| 2154 | * to manage should be disabled. This should not happen in |
| 2155 | * ExitBootServices() but before invoking any UEFI binary is invoked. |
| 2156 | * |
| 2157 | * We want to keep the current workaround while GRUB prior to version |
| 2158 | * 2.04 is still in use. |
Alexander Graf | fa5246b | 2018-11-15 21:23:47 +0100 | [diff] [blame] | 2159 | */ |
Heinrich Schuchardt | 149d5d4 | 2019-07-22 22:04:36 +0200 | [diff] [blame] | 2160 | cleanup_before_linux(); |
Alexander Graf | fa5246b | 2018-11-15 21:23:47 +0100 | [diff] [blame] | 2161 | #endif |
| 2162 | } |
| 2163 | |
| 2164 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2165 | * efi_exit_boot_services() - stop all boot services |
| 2166 | * @image_handle: handle of the loaded image |
| 2167 | * @map_key: key of the memory map |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2168 | * |
| 2169 | * This function implements the ExitBootServices service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2170 | * |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2171 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 2172 | * for details. |
| 2173 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2174 | * All timer events are disabled. For exit boot services events the |
| 2175 | * notification function is called. The boot services are disabled in the |
| 2176 | * system table. |
Heinrich Schuchardt | b1fbb21 | 2018-01-19 20:24:52 +0100 | [diff] [blame] | 2177 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2178 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2179 | */ |
Heinrich Schuchardt | b7cb8b4 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 2180 | static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle, |
Heinrich Schuchardt | 8bd4f41 | 2019-05-05 21:58:35 +0200 | [diff] [blame] | 2181 | efi_uintn_t map_key) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2182 | { |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 2183 | struct efi_event *evt, *next_event; |
Heinrich Schuchardt | afc3367 | 2019-06-11 20:05:40 +0200 | [diff] [blame] | 2184 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | 2d4c738 | 2017-09-15 10:06:18 +0200 | [diff] [blame] | 2185 | |
Heinrich Schuchardt | 8bd4f41 | 2019-05-05 21:58:35 +0200 | [diff] [blame] | 2186 | EFI_ENTRY("%p, %zx", image_handle, map_key); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2187 | |
Heinrich Schuchardt | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 2188 | /* Check that the caller has read the current memory map */ |
Heinrich Schuchardt | afc3367 | 2019-06-11 20:05:40 +0200 | [diff] [blame] | 2189 | if (map_key != efi_memory_map_key) { |
| 2190 | ret = EFI_INVALID_PARAMETER; |
| 2191 | goto out; |
| 2192 | } |
Heinrich Schuchardt | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 2193 | |
Heinrich Schuchardt | b1fbb21 | 2018-01-19 20:24:52 +0100 | [diff] [blame] | 2194 | /* Check if ExitBootServices has already been called */ |
| 2195 | if (!systab.boottime) |
Heinrich Schuchardt | afc3367 | 2019-06-11 20:05:40 +0200 | [diff] [blame] | 2196 | goto out; |
Heinrich Schuchardt | b1fbb21 | 2018-01-19 20:24:52 +0100 | [diff] [blame] | 2197 | |
Heinrich Schuchardt | 44772c4 | 2021-11-16 18:46:27 +0100 | [diff] [blame] | 2198 | /* Notify EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES event group. */ |
| 2199 | list_for_each_entry(evt, &efi_events, link) { |
| 2200 | if (evt->group && |
| 2201 | !guidcmp(evt->group, |
| 2202 | &efi_guid_event_group_before_exit_boot_services)) { |
| 2203 | efi_signal_event(evt); |
| 2204 | break; |
| 2205 | } |
| 2206 | } |
| 2207 | |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 2208 | /* Stop all timer related activities */ |
| 2209 | timers_enabled = false; |
| 2210 | |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 2211 | /* Add related events to the event group */ |
| 2212 | list_for_each_entry(evt, &efi_events, link) { |
| 2213 | if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES) |
| 2214 | evt->group = &efi_guid_event_group_exit_boot_services; |
| 2215 | } |
Heinrich Schuchardt | 2d4c738 | 2017-09-15 10:06:18 +0200 | [diff] [blame] | 2216 | /* Notify that ExitBootServices is invoked. */ |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 2217 | list_for_each_entry(evt, &efi_events, link) { |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 2218 | if (evt->group && |
| 2219 | !guidcmp(evt->group, |
| 2220 | &efi_guid_event_group_exit_boot_services)) { |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 2221 | efi_signal_event(evt); |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 2222 | break; |
| 2223 | } |
Heinrich Schuchardt | 2d4c738 | 2017-09-15 10:06:18 +0200 | [diff] [blame] | 2224 | } |
Heinrich Schuchardt | 2d4c738 | 2017-09-15 10:06:18 +0200 | [diff] [blame] | 2225 | |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 2226 | /* Make sure that notification functions are not called anymore */ |
| 2227 | efi_tpl = TPL_HIGH_LEVEL; |
| 2228 | |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 2229 | /* Notify variable services */ |
| 2230 | efi_variables_boot_exit_notify(); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 2231 | |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 2232 | /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */ |
| 2233 | list_for_each_entry_safe(evt, next_event, &efi_events, link) { |
| 2234 | if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) |
| 2235 | list_del(&evt->link); |
| 2236 | } |
| 2237 | |
Heinrich Schuchardt | 628dbd7 | 2020-11-12 21:26:28 +0100 | [diff] [blame] | 2238 | if (!efi_st_keep_devices) { |
Tom Rini | 669ef7f | 2021-11-19 16:33:04 -0500 | [diff] [blame] | 2239 | bootm_disable_interrupts(); |
Heinrich Schuchardt | 20dbedb | 2020-12-27 15:33:09 +0100 | [diff] [blame] | 2240 | if (IS_ENABLED(CONFIG_USB_DEVICE)) |
Heinrich Schuchardt | 628dbd7 | 2020-11-12 21:26:28 +0100 | [diff] [blame] | 2241 | udc_disconnect(); |
| 2242 | board_quiesce_devices(); |
| 2243 | dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); |
| 2244 | } |
Alexander Graf | 2ebeb44 | 2016-11-17 01:02:57 +0100 | [diff] [blame] | 2245 | |
Heinrich Schuchardt | 1943dbb | 2019-07-05 17:42:16 +0200 | [diff] [blame] | 2246 | /* Patch out unsupported runtime function */ |
| 2247 | efi_runtime_detach(); |
| 2248 | |
Alexander Graf | fa5246b | 2018-11-15 21:23:47 +0100 | [diff] [blame] | 2249 | /* Fix up caches for EFI payloads if necessary */ |
| 2250 | efi_exit_caches(); |
| 2251 | |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 2252 | /* Disable boot time services */ |
Heinrich Schuchardt | b1fbb21 | 2018-01-19 20:24:52 +0100 | [diff] [blame] | 2253 | systab.con_in_handle = NULL; |
| 2254 | systab.con_in = NULL; |
| 2255 | systab.con_out_handle = NULL; |
| 2256 | systab.con_out = NULL; |
| 2257 | systab.stderr_handle = NULL; |
| 2258 | systab.std_err = NULL; |
| 2259 | systab.boottime = NULL; |
| 2260 | |
| 2261 | /* Recalculate CRC32 */ |
Heinrich Schuchardt | 15070db | 2018-06-28 12:45:31 +0200 | [diff] [blame] | 2262 | efi_update_table_header_crc32(&systab.hdr); |
Heinrich Schuchardt | b1fbb21 | 2018-01-19 20:24:52 +0100 | [diff] [blame] | 2263 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2264 | /* Give the payload some time to boot */ |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 2265 | efi_set_watchdog(0); |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 2266 | schedule(); |
Heinrich Schuchardt | afc3367 | 2019-06-11 20:05:40 +0200 | [diff] [blame] | 2267 | out: |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 2268 | if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) { |
| 2269 | if (ret != EFI_SUCCESS) |
| 2270 | efi_tcg2_notify_exit_boot_services_failed(); |
| 2271 | } |
| 2272 | |
Heinrich Schuchardt | afc3367 | 2019-06-11 20:05:40 +0200 | [diff] [blame] | 2273 | return EFI_EXIT(ret); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2274 | } |
| 2275 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2276 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2277 | * efi_get_next_monotonic_count() - get next value of the counter |
| 2278 | * @count: returned value of the counter |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2279 | * |
| 2280 | * This function implements the NextMonotonicCount service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2281 | * |
| 2282 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2283 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2284 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2285 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2286 | */ |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2287 | static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count) |
| 2288 | { |
Heinrich Schuchardt | 9106459 | 2018-02-18 15:17:49 +0100 | [diff] [blame] | 2289 | static uint64_t mono; |
Heinrich Schuchardt | ddc787d | 2019-05-17 12:47:17 +0200 | [diff] [blame] | 2290 | efi_status_t ret; |
Heinrich Schuchardt | 9106459 | 2018-02-18 15:17:49 +0100 | [diff] [blame] | 2291 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2292 | EFI_ENTRY("%p", count); |
Heinrich Schuchardt | ddc787d | 2019-05-17 12:47:17 +0200 | [diff] [blame] | 2293 | if (!count) { |
| 2294 | ret = EFI_INVALID_PARAMETER; |
| 2295 | goto out; |
| 2296 | } |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2297 | *count = mono++; |
Heinrich Schuchardt | ddc787d | 2019-05-17 12:47:17 +0200 | [diff] [blame] | 2298 | ret = EFI_SUCCESS; |
| 2299 | out: |
| 2300 | return EFI_EXIT(ret); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2301 | } |
| 2302 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2303 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2304 | * efi_stall() - sleep |
| 2305 | * @microseconds: period to sleep in microseconds |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2306 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2307 | * This function implements the Stall service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2308 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2309 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2310 | * details. |
| 2311 | * |
| 2312 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2313 | */ |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2314 | static efi_status_t EFIAPI efi_stall(unsigned long microseconds) |
| 2315 | { |
Heinrich Schuchardt | 9912c03 | 2019-06-02 21:12:17 +0200 | [diff] [blame] | 2316 | u64 end_tick; |
| 2317 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2318 | EFI_ENTRY("%ld", microseconds); |
Heinrich Schuchardt | 9912c03 | 2019-06-02 21:12:17 +0200 | [diff] [blame] | 2319 | |
| 2320 | end_tick = get_ticks() + usec_to_tick(microseconds); |
| 2321 | while (get_ticks() < end_tick) |
| 2322 | efi_timer_check(); |
| 2323 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2324 | return EFI_EXIT(EFI_SUCCESS); |
| 2325 | } |
| 2326 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2327 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2328 | * efi_set_watchdog_timer() - reset the watchdog timer |
| 2329 | * @timeout: seconds before reset by watchdog |
| 2330 | * @watchdog_code: code to be logged when resetting |
| 2331 | * @data_size: size of buffer in bytes |
| 2332 | * @watchdog_data: buffer with data describing the reset reason |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2333 | * |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 2334 | * This function implements the SetWatchdogTimer service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2335 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2336 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2337 | * details. |
| 2338 | * |
| 2339 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2340 | */ |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2341 | static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout, |
| 2342 | uint64_t watchdog_code, |
| 2343 | unsigned long data_size, |
| 2344 | uint16_t *watchdog_data) |
| 2345 | { |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 2346 | EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2347 | data_size, watchdog_data); |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 2348 | return EFI_EXIT(efi_set_watchdog(timeout)); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2349 | } |
| 2350 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2351 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2352 | * efi_close_protocol() - close a protocol |
| 2353 | * @handle: handle on which the protocol shall be closed |
| 2354 | * @protocol: GUID of the protocol to close |
| 2355 | * @agent_handle: handle of the driver |
| 2356 | * @controller_handle: handle of the controller |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2357 | * |
Heinrich Schuchardt | 5e39433 | 2022-10-07 15:18:15 +0200 | [diff] [blame] | 2358 | * This is the function implementing the CloseProtocol service is for internal |
| 2359 | * usage in U-Boot. For API usage wrapper efi_close_protocol_ext() is provided. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2360 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2361 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2362 | * details. |
| 2363 | * |
| 2364 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2365 | */ |
Heinrich Schuchardt | 5e39433 | 2022-10-07 15:18:15 +0200 | [diff] [blame] | 2366 | efi_status_t efi_close_protocol(efi_handle_t handle, const efi_guid_t *protocol, |
| 2367 | efi_handle_t agent_handle, |
| 2368 | efi_handle_t controller_handle) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2369 | { |
Heinrich Schuchardt | 4fd1ee2 | 2018-01-11 08:15:59 +0100 | [diff] [blame] | 2370 | struct efi_handler *handler; |
| 2371 | struct efi_open_protocol_info_item *item; |
| 2372 | struct efi_open_protocol_info_item *pos; |
Heinrich Schuchardt | 5e39433 | 2022-10-07 15:18:15 +0200 | [diff] [blame] | 2373 | efi_status_t ret; |
Heinrich Schuchardt | 4fd1ee2 | 2018-01-11 08:15:59 +0100 | [diff] [blame] | 2374 | |
Heinrich Schuchardt | 5d8231b | 2019-05-05 10:37:51 +0200 | [diff] [blame] | 2375 | if (!efi_search_obj(agent_handle) || |
Heinrich Schuchardt | 5e39433 | 2022-10-07 15:18:15 +0200 | [diff] [blame] | 2376 | (controller_handle && !efi_search_obj(controller_handle))) |
| 2377 | return EFI_INVALID_PARAMETER; |
| 2378 | ret = efi_search_protocol(handle, protocol, &handler); |
| 2379 | if (ret != EFI_SUCCESS) |
| 2380 | return ret; |
Heinrich Schuchardt | 4fd1ee2 | 2018-01-11 08:15:59 +0100 | [diff] [blame] | 2381 | |
Heinrich Schuchardt | 5e39433 | 2022-10-07 15:18:15 +0200 | [diff] [blame] | 2382 | ret = EFI_NOT_FOUND; |
Heinrich Schuchardt | 4fd1ee2 | 2018-01-11 08:15:59 +0100 | [diff] [blame] | 2383 | list_for_each_entry_safe(item, pos, &handler->open_infos, link) { |
| 2384 | if (item->info.agent_handle == agent_handle && |
| 2385 | item->info.controller_handle == controller_handle) { |
| 2386 | efi_delete_open_info(item); |
Heinrich Schuchardt | 5e39433 | 2022-10-07 15:18:15 +0200 | [diff] [blame] | 2387 | ret = EFI_SUCCESS; |
Heinrich Schuchardt | 4fd1ee2 | 2018-01-11 08:15:59 +0100 | [diff] [blame] | 2388 | } |
| 2389 | } |
Heinrich Schuchardt | 5e39433 | 2022-10-07 15:18:15 +0200 | [diff] [blame] | 2390 | |
| 2391 | return ret; |
| 2392 | } |
| 2393 | |
| 2394 | /** |
| 2395 | * efi_close_protocol_ext() - close a protocol |
| 2396 | * @handle: handle on which the protocol shall be closed |
| 2397 | * @protocol: GUID of the protocol to close |
| 2398 | * @agent_handle: handle of the driver |
| 2399 | * @controller_handle: handle of the controller |
| 2400 | * |
| 2401 | * This function implements the CloseProtocol service. |
| 2402 | * |
| 2403 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2404 | * details. |
| 2405 | * |
| 2406 | * Return: status code |
| 2407 | */ |
| 2408 | static efi_status_t EFIAPI |
| 2409 | efi_close_protocol_ext(efi_handle_t handle, const efi_guid_t *protocol, |
| 2410 | efi_handle_t agent_handle, |
| 2411 | efi_handle_t controller_handle) |
| 2412 | { |
| 2413 | efi_status_t ret; |
| 2414 | |
| 2415 | EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, agent_handle, |
| 2416 | controller_handle); |
| 2417 | |
| 2418 | ret = efi_close_protocol(handle, protocol, |
| 2419 | agent_handle, controller_handle); |
| 2420 | |
| 2421 | return EFI_EXIT(ret); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2422 | } |
| 2423 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2424 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2425 | * efi_open_protocol_information() - provide information about then open status |
| 2426 | * of a protocol on a handle |
| 2427 | * @handle: handle for which the information shall be retrieved |
| 2428 | * @protocol: GUID of the protocol |
| 2429 | * @entry_buffer: buffer to receive the open protocol information |
| 2430 | * @entry_count: number of entries available in the buffer |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2431 | * |
| 2432 | * This function implements the OpenProtocolInformation service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2433 | * |
| 2434 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2435 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2436 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2437 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2438 | */ |
Heinrich Schuchardt | 9106459 | 2018-02-18 15:17:49 +0100 | [diff] [blame] | 2439 | static efi_status_t EFIAPI efi_open_protocol_information( |
| 2440 | efi_handle_t handle, const efi_guid_t *protocol, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2441 | struct efi_open_protocol_info_entry **entry_buffer, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 2442 | efi_uintn_t *entry_count) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2443 | { |
Heinrich Schuchardt | f2ef22e | 2018-01-11 08:16:00 +0100 | [diff] [blame] | 2444 | unsigned long buffer_size; |
| 2445 | unsigned long count; |
| 2446 | struct efi_handler *handler; |
| 2447 | struct efi_open_protocol_info_item *item; |
| 2448 | efi_status_t r; |
| 2449 | |
Heinrich Schuchardt | 282249d | 2022-01-16 14:15:31 +0100 | [diff] [blame] | 2450 | EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, entry_buffer, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2451 | entry_count); |
Heinrich Schuchardt | f2ef22e | 2018-01-11 08:16:00 +0100 | [diff] [blame] | 2452 | |
| 2453 | /* Check parameters */ |
| 2454 | if (!entry_buffer) { |
| 2455 | r = EFI_INVALID_PARAMETER; |
| 2456 | goto out; |
| 2457 | } |
| 2458 | r = efi_search_protocol(handle, protocol, &handler); |
| 2459 | if (r != EFI_SUCCESS) |
| 2460 | goto out; |
| 2461 | |
| 2462 | /* Count entries */ |
| 2463 | count = 0; |
| 2464 | list_for_each_entry(item, &handler->open_infos, link) { |
| 2465 | if (item->info.open_count) |
| 2466 | ++count; |
| 2467 | } |
| 2468 | *entry_count = count; |
| 2469 | *entry_buffer = NULL; |
| 2470 | if (!count) { |
| 2471 | r = EFI_SUCCESS; |
| 2472 | goto out; |
| 2473 | } |
| 2474 | |
| 2475 | /* Copy entries */ |
| 2476 | buffer_size = count * sizeof(struct efi_open_protocol_info_entry); |
Heinrich Schuchardt | abb4387 | 2018-10-03 20:02:29 +0200 | [diff] [blame] | 2477 | r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size, |
Heinrich Schuchardt | f2ef22e | 2018-01-11 08:16:00 +0100 | [diff] [blame] | 2478 | (void **)entry_buffer); |
| 2479 | if (r != EFI_SUCCESS) |
| 2480 | goto out; |
| 2481 | list_for_each_entry_reverse(item, &handler->open_infos, link) { |
| 2482 | if (item->info.open_count) |
| 2483 | (*entry_buffer)[--count] = item->info; |
| 2484 | } |
| 2485 | out: |
| 2486 | return EFI_EXIT(r); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2487 | } |
| 2488 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2489 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2490 | * efi_protocols_per_handle() - get protocols installed on a handle |
| 2491 | * @handle: handle for which the information is retrieved |
| 2492 | * @protocol_buffer: buffer with protocol GUIDs |
| 2493 | * @protocol_buffer_count: number of entries in the buffer |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2494 | * |
| 2495 | * This function implements the ProtocolsPerHandleService. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2496 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2497 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2498 | * details. |
| 2499 | * |
| 2500 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2501 | */ |
Heinrich Schuchardt | b7cb8b4 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 2502 | static efi_status_t EFIAPI efi_protocols_per_handle( |
| 2503 | efi_handle_t handle, efi_guid_t ***protocol_buffer, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 2504 | efi_uintn_t *protocol_buffer_count) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2505 | { |
xypron.glpk@gmx.de | 8960c97 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2506 | unsigned long buffer_size; |
| 2507 | struct efi_object *efiobj; |
Heinrich Schuchardt | 99ce206 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 2508 | struct list_head *protocol_handle; |
xypron.glpk@gmx.de | 8960c97 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2509 | efi_status_t r; |
| 2510 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2511 | EFI_ENTRY("%p, %p, %p", handle, protocol_buffer, |
| 2512 | protocol_buffer_count); |
xypron.glpk@gmx.de | 8960c97 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2513 | |
| 2514 | if (!handle || !protocol_buffer || !protocol_buffer_count) |
| 2515 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 2516 | |
| 2517 | *protocol_buffer = NULL; |
Rob Clark | d51b8ca | 2017-07-20 07:59:39 -0400 | [diff] [blame] | 2518 | *protocol_buffer_count = 0; |
xypron.glpk@gmx.de | 8960c97 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2519 | |
Heinrich Schuchardt | 99ce206 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 2520 | efiobj = efi_search_obj(handle); |
| 2521 | if (!efiobj) |
| 2522 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
xypron.glpk@gmx.de | 8960c97 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2523 | |
Heinrich Schuchardt | 99ce206 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 2524 | /* Count protocols */ |
| 2525 | list_for_each(protocol_handle, &efiobj->protocols) { |
| 2526 | ++*protocol_buffer_count; |
| 2527 | } |
| 2528 | |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 2529 | /* Copy GUIDs */ |
Heinrich Schuchardt | 99ce206 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 2530 | if (*protocol_buffer_count) { |
| 2531 | size_t j = 0; |
| 2532 | |
| 2533 | buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count; |
Heinrich Schuchardt | abb4387 | 2018-10-03 20:02:29 +0200 | [diff] [blame] | 2534 | r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size, |
Heinrich Schuchardt | 99ce206 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 2535 | (void **)protocol_buffer); |
| 2536 | if (r != EFI_SUCCESS) |
| 2537 | return EFI_EXIT(r); |
| 2538 | list_for_each(protocol_handle, &efiobj->protocols) { |
| 2539 | struct efi_handler *protocol; |
| 2540 | |
| 2541 | protocol = list_entry(protocol_handle, |
| 2542 | struct efi_handler, link); |
Heinrich Schuchardt | 2a22db9 | 2022-03-09 19:56:23 +0100 | [diff] [blame] | 2543 | (*protocol_buffer)[j] = (void *)&protocol->guid; |
Heinrich Schuchardt | 99ce206 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 2544 | ++j; |
xypron.glpk@gmx.de | 8960c97 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2545 | } |
xypron.glpk@gmx.de | 8960c97 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2546 | } |
| 2547 | |
| 2548 | return EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2549 | } |
| 2550 | |
Masahisa Kojima | c5ff0a0 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 2551 | efi_status_t efi_locate_handle_buffer_int(enum efi_locate_search_type search_type, |
| 2552 | const efi_guid_t *protocol, void *search_key, |
| 2553 | efi_uintn_t *no_handles, efi_handle_t **buffer) |
| 2554 | { |
| 2555 | efi_status_t r; |
| 2556 | efi_uintn_t buffer_size = 0; |
| 2557 | |
| 2558 | if (!no_handles || !buffer) { |
| 2559 | r = EFI_INVALID_PARAMETER; |
| 2560 | goto out; |
| 2561 | } |
| 2562 | *no_handles = 0; |
| 2563 | *buffer = NULL; |
| 2564 | r = efi_locate_handle(search_type, protocol, search_key, &buffer_size, |
| 2565 | *buffer); |
| 2566 | if (r != EFI_BUFFER_TOO_SMALL) |
| 2567 | goto out; |
| 2568 | r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size, |
| 2569 | (void **)buffer); |
| 2570 | if (r != EFI_SUCCESS) |
| 2571 | goto out; |
| 2572 | r = efi_locate_handle(search_type, protocol, search_key, &buffer_size, |
| 2573 | *buffer); |
| 2574 | if (r == EFI_SUCCESS) |
| 2575 | *no_handles = buffer_size / sizeof(efi_handle_t); |
| 2576 | out: |
| 2577 | return r; |
| 2578 | } |
| 2579 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2580 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2581 | * efi_locate_handle_buffer() - locate handles implementing a protocol |
| 2582 | * @search_type: selection criterion |
| 2583 | * @protocol: GUID of the protocol |
| 2584 | * @search_key: registration key |
| 2585 | * @no_handles: number of returned handles |
| 2586 | * @buffer: buffer with the returned handles |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2587 | * |
| 2588 | * This function implements the LocateHandleBuffer service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2589 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2590 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2591 | * details. |
| 2592 | * |
| 2593 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2594 | */ |
AKASHI Takahiro | d99b1b3 | 2020-03-17 11:12:36 +0900 | [diff] [blame] | 2595 | efi_status_t EFIAPI efi_locate_handle_buffer( |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2596 | enum efi_locate_search_type search_type, |
Heinrich Schuchardt | e547c66 | 2017-10-05 16:35:53 +0200 | [diff] [blame] | 2597 | const efi_guid_t *protocol, void *search_key, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 2598 | efi_uintn_t *no_handles, efi_handle_t **buffer) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2599 | { |
xypron.glpk@gmx.de | 550a68a | 2017-07-11 22:06:22 +0200 | [diff] [blame] | 2600 | efi_status_t r; |
xypron.glpk@gmx.de | 550a68a | 2017-07-11 22:06:22 +0200 | [diff] [blame] | 2601 | |
Heinrich Schuchardt | 282249d | 2022-01-16 14:15:31 +0100 | [diff] [blame] | 2602 | EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2603 | no_handles, buffer); |
xypron.glpk@gmx.de | 550a68a | 2017-07-11 22:06:22 +0200 | [diff] [blame] | 2604 | |
Masahisa Kojima | c5ff0a0 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 2605 | r = efi_locate_handle_buffer_int(search_type, protocol, search_key, |
| 2606 | no_handles, buffer); |
| 2607 | |
xypron.glpk@gmx.de | 550a68a | 2017-07-11 22:06:22 +0200 | [diff] [blame] | 2608 | return EFI_EXIT(r); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2609 | } |
| 2610 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2611 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2612 | * efi_locate_protocol() - find an interface implementing a protocol |
| 2613 | * @protocol: GUID of the protocol |
| 2614 | * @registration: registration key passed to the notification function |
| 2615 | * @protocol_interface: interface implementing the protocol |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2616 | * |
| 2617 | * This function implements the LocateProtocol service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2618 | * |
| 2619 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2620 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2621 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2622 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2623 | */ |
Heinrich Schuchardt | e547c66 | 2017-10-05 16:35:53 +0200 | [diff] [blame] | 2624 | static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2625 | void *registration, |
| 2626 | void **protocol_interface) |
| 2627 | { |
Heinrich Schuchardt | 2600eff | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2628 | struct efi_handler *handler; |
Heinrich Schuchardt | 57505e9 | 2017-10-26 19:25:57 +0200 | [diff] [blame] | 2629 | efi_status_t ret; |
Heinrich Schuchardt | 2600eff | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2630 | struct efi_object *efiobj; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2631 | |
Heinrich Schuchardt | 282249d | 2022-01-16 14:15:31 +0100 | [diff] [blame] | 2632 | EFI_ENTRY("%pUs, %p, %p", protocol, registration, protocol_interface); |
xypron.glpk@gmx.de | 4534ad6 | 2017-07-11 22:06:24 +0200 | [diff] [blame] | 2633 | |
Heinrich Schuchardt | 2600eff | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2634 | /* |
| 2635 | * The UEFI spec explicitly requires a protocol even if a registration |
| 2636 | * key is provided. This differs from the logic in LocateHandle(). |
| 2637 | */ |
xypron.glpk@gmx.de | 4534ad6 | 2017-07-11 22:06:24 +0200 | [diff] [blame] | 2638 | if (!protocol || !protocol_interface) |
| 2639 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 2640 | |
Heinrich Schuchardt | 2600eff | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2641 | if (registration) { |
| 2642 | struct efi_register_notify_event *event; |
| 2643 | struct efi_protocol_notification *handle; |
xypron.glpk@gmx.de | 4534ad6 | 2017-07-11 22:06:24 +0200 | [diff] [blame] | 2644 | |
Heinrich Schuchardt | 2600eff | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2645 | event = efi_check_register_notify_event(registration); |
| 2646 | if (!event) |
| 2647 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 2648 | /* |
| 2649 | * The UEFI spec requires to return EFI_NOT_FOUND if no |
| 2650 | * protocol instance matches protocol and registration. |
| 2651 | * So let's do the same for a mismatch between protocol and |
| 2652 | * registration. |
| 2653 | */ |
| 2654 | if (guidcmp(&event->protocol, protocol)) |
| 2655 | goto not_found; |
| 2656 | if (list_empty(&event->handles)) |
| 2657 | goto not_found; |
| 2658 | handle = list_first_entry(&event->handles, |
| 2659 | struct efi_protocol_notification, |
| 2660 | link); |
| 2661 | efiobj = handle->handle; |
| 2662 | list_del(&handle->link); |
| 2663 | free(handle); |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 2664 | ret = efi_search_protocol(efiobj, protocol, &handler); |
Heinrich Schuchardt | 2600eff | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2665 | if (ret == EFI_SUCCESS) |
| 2666 | goto found; |
| 2667 | } else { |
| 2668 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
| 2669 | ret = efi_search_protocol(efiobj, protocol, &handler); |
| 2670 | if (ret == EFI_SUCCESS) |
| 2671 | goto found; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2672 | } |
| 2673 | } |
Heinrich Schuchardt | 2600eff | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2674 | not_found: |
xypron.glpk@gmx.de | 4534ad6 | 2017-07-11 22:06:24 +0200 | [diff] [blame] | 2675 | *protocol_interface = NULL; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2676 | return EFI_EXIT(EFI_NOT_FOUND); |
Heinrich Schuchardt | 2600eff | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2677 | found: |
| 2678 | *protocol_interface = handler->protocol_interface; |
| 2679 | return EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2680 | } |
| 2681 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2682 | /** |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2683 | * efi_install_multiple_protocol_interfaces_int() - Install multiple protocol |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2684 | * interfaces |
| 2685 | * @handle: handle on which the protocol interfaces shall be installed |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2686 | * @argptr: va_list of args |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2687 | * |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2688 | * Core functionality of efi_install_multiple_protocol_interfaces |
| 2689 | * Must not be called directly |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2690 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2691 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2692 | */ |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2693 | static efi_status_t EFIAPI |
| 2694 | efi_install_multiple_protocol_interfaces_int(efi_handle_t *handle, |
| 2695 | efi_va_list argptr) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2696 | { |
Heinrich Schuchardt | e547c66 | 2017-10-05 16:35:53 +0200 | [diff] [blame] | 2697 | const efi_guid_t *protocol; |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2698 | void *protocol_interface; |
Heinrich Schuchardt | 61ba10d | 2019-05-16 21:54:04 +0200 | [diff] [blame] | 2699 | efi_handle_t old_handle; |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2700 | efi_status_t ret = EFI_SUCCESS; |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2701 | int i = 0; |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2702 | efi_va_list argptr_copy; |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2703 | |
| 2704 | if (!handle) |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2705 | return EFI_INVALID_PARAMETER; |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2706 | |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2707 | efi_va_copy(argptr_copy, argptr); |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2708 | for (;;) { |
Alexander Graf | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2709 | protocol = efi_va_arg(argptr, efi_guid_t*); |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2710 | if (!protocol) |
| 2711 | break; |
Alexander Graf | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2712 | protocol_interface = efi_va_arg(argptr, void*); |
Heinrich Schuchardt | 61ba10d | 2019-05-16 21:54:04 +0200 | [diff] [blame] | 2713 | /* Check that a device path has not been installed before */ |
| 2714 | if (!guidcmp(protocol, &efi_guid_device_path)) { |
| 2715 | struct efi_device_path *dp = protocol_interface; |
| 2716 | |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2717 | ret = EFI_CALL(efi_locate_device_path(protocol, &dp, |
| 2718 | &old_handle)); |
| 2719 | if (ret == EFI_SUCCESS && |
Heinrich Schuchardt | 2934497 | 2019-05-20 19:55:18 +0000 | [diff] [blame] | 2720 | dp->type == DEVICE_PATH_TYPE_END) { |
| 2721 | EFI_PRINT("Path %pD already installed\n", |
| 2722 | protocol_interface); |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2723 | ret = EFI_ALREADY_STARTED; |
Heinrich Schuchardt | 61ba10d | 2019-05-16 21:54:04 +0200 | [diff] [blame] | 2724 | break; |
| 2725 | } |
| 2726 | } |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2727 | ret = EFI_CALL(efi_install_protocol_interface(handle, protocol, |
| 2728 | EFI_NATIVE_INTERFACE, |
| 2729 | protocol_interface)); |
| 2730 | if (ret != EFI_SUCCESS) |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2731 | break; |
| 2732 | i++; |
| 2733 | } |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2734 | if (ret == EFI_SUCCESS) |
| 2735 | goto out; |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2736 | |
Heinrich Schuchardt | ec47f3e | 2017-10-26 19:25:42 +0200 | [diff] [blame] | 2737 | /* If an error occurred undo all changes. */ |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2738 | for (; i; --i) { |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2739 | protocol = efi_va_arg(argptr_copy, efi_guid_t*); |
| 2740 | protocol_interface = efi_va_arg(argptr_copy, void*); |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 2741 | EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol, |
Heinrich Schuchardt | 7cdc17f | 2017-11-06 21:17:45 +0100 | [diff] [blame] | 2742 | protocol_interface)); |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2743 | } |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2744 | |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2745 | out: |
| 2746 | efi_va_end(argptr_copy); |
| 2747 | return ret; |
| 2748 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2749 | } |
| 2750 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2751 | /** |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2752 | * efi_install_multiple_protocol_interfaces() - Install multiple protocol |
| 2753 | * interfaces |
| 2754 | * @handle: handle on which the protocol interfaces shall be installed |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2755 | * @...: NULL terminated argument list with pairs of protocol GUIDS and |
| 2756 | * interfaces |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2757 | * |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2758 | * |
| 2759 | * This is the function for internal usage in U-Boot. For the API function |
| 2760 | * implementing the InstallMultipleProtocol service see |
| 2761 | * efi_install_multiple_protocol_interfaces_ext() |
| 2762 | * |
| 2763 | * Return: status code |
| 2764 | */ |
| 2765 | efi_status_t EFIAPI |
| 2766 | efi_install_multiple_protocol_interfaces(efi_handle_t *handle, ...) |
| 2767 | { |
| 2768 | efi_status_t ret; |
| 2769 | efi_va_list argptr; |
| 2770 | |
| 2771 | efi_va_start(argptr, handle); |
| 2772 | ret = efi_install_multiple_protocol_interfaces_int(handle, argptr); |
| 2773 | efi_va_end(argptr); |
| 2774 | return ret; |
| 2775 | } |
| 2776 | |
| 2777 | /** |
| 2778 | * efi_install_multiple_protocol_interfaces_ext() - Install multiple protocol |
| 2779 | * interfaces |
| 2780 | * @handle: handle on which the protocol interfaces shall be installed |
| 2781 | * @...: NULL terminated argument list with pairs of protocol GUIDS and |
| 2782 | * interfaces |
| 2783 | * |
| 2784 | * This function implements the MultipleProtocolInterfaces service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2785 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2786 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2787 | * details. |
| 2788 | * |
| 2789 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2790 | */ |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2791 | static efi_status_t EFIAPI |
| 2792 | efi_install_multiple_protocol_interfaces_ext(efi_handle_t *handle, ...) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2793 | { |
| 2794 | EFI_ENTRY("%p", handle); |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2795 | efi_status_t ret; |
Alexander Graf | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2796 | efi_va_list argptr; |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2797 | |
| 2798 | efi_va_start(argptr, handle); |
| 2799 | ret = efi_install_multiple_protocol_interfaces_int(handle, argptr); |
| 2800 | efi_va_end(argptr); |
| 2801 | return EFI_EXIT(ret); |
| 2802 | } |
| 2803 | |
| 2804 | /** |
| 2805 | * efi_uninstall_multiple_protocol_interfaces_int() - wrapper for uninstall |
| 2806 | * multiple protocol |
| 2807 | * interfaces |
| 2808 | * @handle: handle from which the protocol interfaces shall be removed |
| 2809 | * @argptr: va_list of args |
| 2810 | * |
| 2811 | * Core functionality of efi_uninstall_multiple_protocol_interfaces |
| 2812 | * Must not be called directly |
| 2813 | * |
| 2814 | * Return: status code |
| 2815 | */ |
| 2816 | static efi_status_t EFIAPI |
| 2817 | efi_uninstall_multiple_protocol_interfaces_int(efi_handle_t handle, |
| 2818 | efi_va_list argptr) |
| 2819 | { |
Ilias Apalodimas | c76eade | 2023-08-24 17:21:09 +0300 | [diff] [blame] | 2820 | const efi_guid_t *protocol, *next_protocol; |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2821 | void *protocol_interface; |
Ilias Apalodimas | 3b820bc | 2022-11-10 10:21:24 +0200 | [diff] [blame] | 2822 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2823 | size_t i = 0; |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2824 | efi_va_list argptr_copy; |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2825 | |
| 2826 | if (!handle) |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2827 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2828 | |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2829 | efi_va_copy(argptr_copy, argptr); |
Ilias Apalodimas | c76eade | 2023-08-24 17:21:09 +0300 | [diff] [blame] | 2830 | protocol = efi_va_arg(argptr, efi_guid_t*); |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2831 | for (;;) { |
Ilias Apalodimas | c76eade | 2023-08-24 17:21:09 +0300 | [diff] [blame] | 2832 | /* |
| 2833 | * If efi_uninstall_protocol() fails we need to be able to |
| 2834 | * reinstall the previously uninstalled protocols on the same |
| 2835 | * handle. |
| 2836 | * Instead of calling efi_uninstall_protocol(...,..., false) |
| 2837 | * and potentially removing the handle, only allow the handle |
| 2838 | * removal on the last protocol that we requested to uninstall. |
| 2839 | * That way we can preserve the handle in case the latter fails |
| 2840 | */ |
| 2841 | bool preserve = true; |
| 2842 | |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2843 | if (!protocol) |
| 2844 | break; |
Alexander Graf | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2845 | protocol_interface = efi_va_arg(argptr, void*); |
Ilias Apalodimas | c76eade | 2023-08-24 17:21:09 +0300 | [diff] [blame] | 2846 | next_protocol = efi_va_arg(argptr, efi_guid_t*); |
| 2847 | if (!next_protocol) |
| 2848 | preserve = false; |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2849 | ret = efi_uninstall_protocol(handle, protocol, |
Ilias Apalodimas | c76eade | 2023-08-24 17:21:09 +0300 | [diff] [blame] | 2850 | protocol_interface, preserve); |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2851 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2852 | break; |
| 2853 | i++; |
Ilias Apalodimas | c76eade | 2023-08-24 17:21:09 +0300 | [diff] [blame] | 2854 | protocol = next_protocol; |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2855 | } |
Ilias Apalodimas | c76eade | 2023-08-24 17:21:09 +0300 | [diff] [blame] | 2856 | if (ret == EFI_SUCCESS) |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2857 | goto out; |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2858 | |
| 2859 | /* If an error occurred undo all changes. */ |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2860 | for (; i; --i) { |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2861 | protocol = efi_va_arg(argptr_copy, efi_guid_t*); |
| 2862 | protocol_interface = efi_va_arg(argptr_copy, void*); |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2863 | EFI_CALL(efi_install_protocol_interface(&handle, protocol, |
| 2864 | EFI_NATIVE_INTERFACE, |
| 2865 | protocol_interface)); |
| 2866 | } |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2867 | /* |
| 2868 | * If any errors are generated while the protocol interfaces are being |
| 2869 | * uninstalled, then the protocols uninstalled prior to the error will |
| 2870 | * be reinstalled using InstallProtocolInterface() and the status code |
| 2871 | * EFI_INVALID_PARAMETER is returned. |
| 2872 | */ |
| 2873 | ret = EFI_INVALID_PARAMETER; |
| 2874 | |
| 2875 | out: |
| 2876 | efi_va_end(argptr_copy); |
| 2877 | return ret; |
| 2878 | } |
| 2879 | |
| 2880 | /** |
| 2881 | * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol |
| 2882 | * interfaces |
| 2883 | * @handle: handle from which the protocol interfaces shall be removed |
| 2884 | * @...: NULL terminated argument list with pairs of protocol GUIDS and |
| 2885 | * interfaces |
| 2886 | * |
| 2887 | * This function implements the UninstallMultipleProtocolInterfaces service. |
| 2888 | * |
| 2889 | * This is the function for internal usage in U-Boot. For the API function |
| 2890 | * implementing the UninstallMultipleProtocolInterfaces service see |
| 2891 | * efi_uninstall_multiple_protocol_interfaces_ext() |
| 2892 | * |
| 2893 | * Return: status code |
| 2894 | */ |
| 2895 | efi_status_t EFIAPI |
| 2896 | efi_uninstall_multiple_protocol_interfaces(efi_handle_t handle, ...) |
| 2897 | { |
| 2898 | efi_status_t ret; |
| 2899 | efi_va_list argptr; |
| 2900 | |
| 2901 | efi_va_start(argptr, handle); |
| 2902 | ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr); |
Alexander Graf | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2903 | efi_va_end(argptr); |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2904 | return ret; |
| 2905 | } |
| 2906 | |
| 2907 | /** |
| 2908 | * efi_uninstall_multiple_protocol_interfaces_ext() - uninstall multiple protocol |
| 2909 | * interfaces |
| 2910 | * @handle: handle from which the protocol interfaces shall be removed |
| 2911 | * @...: NULL terminated argument list with pairs of protocol GUIDS and |
| 2912 | * interfaces |
| 2913 | * |
| 2914 | * This function implements the UninstallMultipleProtocolInterfaces service. |
| 2915 | * |
| 2916 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2917 | * details. |
| 2918 | * |
| 2919 | * Return: status code |
| 2920 | */ |
| 2921 | static efi_status_t EFIAPI |
| 2922 | efi_uninstall_multiple_protocol_interfaces_ext(efi_handle_t handle, ...) |
| 2923 | { |
| 2924 | EFI_ENTRY("%p", handle); |
| 2925 | efi_status_t ret; |
| 2926 | efi_va_list argptr; |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2927 | |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 2928 | efi_va_start(argptr, handle); |
| 2929 | ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr); |
| 2930 | efi_va_end(argptr); |
| 2931 | return EFI_EXIT(ret); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2932 | } |
| 2933 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2934 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2935 | * efi_calculate_crc32() - calculate cyclic redundancy code |
| 2936 | * @data: buffer with data |
| 2937 | * @data_size: size of buffer in bytes |
| 2938 | * @crc32_p: cyclic redundancy code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2939 | * |
| 2940 | * This function implements the CalculateCrc32 service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2941 | * |
| 2942 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2943 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2944 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2945 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2946 | */ |
Heinrich Schuchardt | f272558 | 2018-07-07 15:36:04 +0200 | [diff] [blame] | 2947 | static efi_status_t EFIAPI efi_calculate_crc32(const void *data, |
| 2948 | efi_uintn_t data_size, |
| 2949 | u32 *crc32_p) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2950 | { |
Heinrich Schuchardt | 3f0282c | 2019-05-16 23:31:29 +0200 | [diff] [blame] | 2951 | efi_status_t ret = EFI_SUCCESS; |
| 2952 | |
Heinrich Schuchardt | f272558 | 2018-07-07 15:36:04 +0200 | [diff] [blame] | 2953 | EFI_ENTRY("%p, %zu", data, data_size); |
Heinrich Schuchardt | 3f0282c | 2019-05-16 23:31:29 +0200 | [diff] [blame] | 2954 | if (!data || !data_size || !crc32_p) { |
| 2955 | ret = EFI_INVALID_PARAMETER; |
| 2956 | goto out; |
| 2957 | } |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2958 | *crc32_p = crc32(0, data, data_size); |
Heinrich Schuchardt | 3f0282c | 2019-05-16 23:31:29 +0200 | [diff] [blame] | 2959 | out: |
| 2960 | return EFI_EXIT(ret); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2961 | } |
| 2962 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2963 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2964 | * efi_copy_mem() - copy memory |
| 2965 | * @destination: destination of the copy operation |
| 2966 | * @source: source of the copy operation |
| 2967 | * @length: number of bytes to copy |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2968 | * |
| 2969 | * This function implements the CopyMem service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2970 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2971 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2972 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2973 | */ |
Heinrich Schuchardt | d0a349e | 2017-10-05 16:35:52 +0200 | [diff] [blame] | 2974 | static void EFIAPI efi_copy_mem(void *destination, const void *source, |
| 2975 | size_t length) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2976 | { |
Heinrich Schuchardt | d0a349e | 2017-10-05 16:35:52 +0200 | [diff] [blame] | 2977 | EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length); |
Heinrich Schuchardt | efba6ba | 2019-01-09 21:41:13 +0100 | [diff] [blame] | 2978 | memmove(destination, source, length); |
Heinrich Schuchardt | a5270e0 | 2017-10-05 16:35:51 +0200 | [diff] [blame] | 2979 | EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2980 | } |
| 2981 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2982 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2983 | * efi_set_mem() - Fill memory with a byte value. |
| 2984 | * @buffer: buffer to fill |
| 2985 | * @size: size of buffer in bytes |
| 2986 | * @value: byte to copy to the buffer |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2987 | * |
| 2988 | * This function implements the SetMem service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2989 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2990 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2991 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2992 | */ |
Heinrich Schuchardt | d0a349e | 2017-10-05 16:35:52 +0200 | [diff] [blame] | 2993 | static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2994 | { |
Heinrich Schuchardt | d0a349e | 2017-10-05 16:35:52 +0200 | [diff] [blame] | 2995 | EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2996 | memset(buffer, value, size); |
Heinrich Schuchardt | a5270e0 | 2017-10-05 16:35:51 +0200 | [diff] [blame] | 2997 | EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2998 | } |
| 2999 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3000 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3001 | * efi_protocol_open() - open protocol interface on a handle |
| 3002 | * @handler: handler of a protocol |
| 3003 | * @protocol_interface: interface implementing the protocol |
| 3004 | * @agent_handle: handle of the driver |
| 3005 | * @controller_handle: handle of the controller |
| 3006 | * @attributes: attributes indicating how to open the protocol |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 3007 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3008 | * Return: status code |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 3009 | */ |
Heinrich Schuchardt | 9c89d14 | 2020-01-10 12:33:59 +0100 | [diff] [blame] | 3010 | efi_status_t efi_protocol_open( |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 3011 | struct efi_handler *handler, |
| 3012 | void **protocol_interface, void *agent_handle, |
| 3013 | void *controller_handle, uint32_t attributes) |
| 3014 | { |
| 3015 | struct efi_open_protocol_info_item *item; |
| 3016 | struct efi_open_protocol_info_entry *match = NULL; |
| 3017 | bool opened_by_driver = false; |
| 3018 | bool opened_exclusive = false; |
| 3019 | |
| 3020 | /* If there is no agent, only return the interface */ |
| 3021 | if (!agent_handle) |
| 3022 | goto out; |
| 3023 | |
| 3024 | /* For TEST_PROTOCOL ignore interface attribute */ |
| 3025 | if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL) |
| 3026 | *protocol_interface = NULL; |
| 3027 | |
| 3028 | /* |
| 3029 | * Check if the protocol is already opened by a driver with the same |
| 3030 | * attributes or opened exclusively |
| 3031 | */ |
| 3032 | list_for_each_entry(item, &handler->open_infos, link) { |
| 3033 | if (item->info.agent_handle == agent_handle) { |
| 3034 | if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) && |
| 3035 | (item->info.attributes == attributes)) |
| 3036 | return EFI_ALREADY_STARTED; |
Heinrich Schuchardt | da433d5 | 2019-05-30 14:16:31 +0200 | [diff] [blame] | 3037 | } else { |
| 3038 | if (item->info.attributes & |
| 3039 | EFI_OPEN_PROTOCOL_BY_DRIVER) |
| 3040 | opened_by_driver = true; |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 3041 | } |
| 3042 | if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) |
| 3043 | opened_exclusive = true; |
| 3044 | } |
| 3045 | |
| 3046 | /* Only one controller can open the protocol exclusively */ |
Heinrich Schuchardt | da433d5 | 2019-05-30 14:16:31 +0200 | [diff] [blame] | 3047 | if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) { |
| 3048 | if (opened_exclusive) |
| 3049 | return EFI_ACCESS_DENIED; |
| 3050 | } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) { |
| 3051 | if (opened_exclusive || opened_by_driver) |
| 3052 | return EFI_ACCESS_DENIED; |
| 3053 | } |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 3054 | |
| 3055 | /* Prepare exclusive opening */ |
| 3056 | if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) { |
| 3057 | /* Try to disconnect controllers */ |
Heinrich Schuchardt | fa8dddf | 2019-05-30 14:16:31 +0200 | [diff] [blame] | 3058 | disconnect_next: |
| 3059 | opened_by_driver = false; |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 3060 | list_for_each_entry(item, &handler->open_infos, link) { |
Heinrich Schuchardt | fa8dddf | 2019-05-30 14:16:31 +0200 | [diff] [blame] | 3061 | efi_status_t ret; |
| 3062 | |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 3063 | if (item->info.attributes == |
Heinrich Schuchardt | fa8dddf | 2019-05-30 14:16:31 +0200 | [diff] [blame] | 3064 | EFI_OPEN_PROTOCOL_BY_DRIVER) { |
| 3065 | ret = EFI_CALL(efi_disconnect_controller( |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 3066 | item->info.controller_handle, |
| 3067 | item->info.agent_handle, |
| 3068 | NULL)); |
Heinrich Schuchardt | fa8dddf | 2019-05-30 14:16:31 +0200 | [diff] [blame] | 3069 | if (ret == EFI_SUCCESS) |
| 3070 | /* |
| 3071 | * Child controllers may have been |
| 3072 | * removed from the open_infos list. So |
| 3073 | * let's restart the loop. |
| 3074 | */ |
| 3075 | goto disconnect_next; |
| 3076 | else |
| 3077 | opened_by_driver = true; |
| 3078 | } |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 3079 | } |
Heinrich Schuchardt | fa8dddf | 2019-05-30 14:16:31 +0200 | [diff] [blame] | 3080 | /* Only one driver can be connected */ |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 3081 | if (opened_by_driver) |
| 3082 | return EFI_ACCESS_DENIED; |
| 3083 | } |
| 3084 | |
| 3085 | /* Find existing entry */ |
| 3086 | list_for_each_entry(item, &handler->open_infos, link) { |
| 3087 | if (item->info.agent_handle == agent_handle && |
Heinrich Schuchardt | 7d69fc3 | 2019-06-01 20:15:10 +0200 | [diff] [blame] | 3088 | item->info.controller_handle == controller_handle && |
| 3089 | item->info.attributes == attributes) |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 3090 | match = &item->info; |
| 3091 | } |
| 3092 | /* None found, create one */ |
| 3093 | if (!match) { |
| 3094 | match = efi_create_open_info(handler); |
| 3095 | if (!match) |
| 3096 | return EFI_OUT_OF_RESOURCES; |
| 3097 | } |
| 3098 | |
| 3099 | match->agent_handle = agent_handle; |
| 3100 | match->controller_handle = controller_handle; |
| 3101 | match->attributes = attributes; |
| 3102 | match->open_count++; |
| 3103 | |
| 3104 | out: |
| 3105 | /* For TEST_PROTOCOL ignore interface attribute. */ |
| 3106 | if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL) |
| 3107 | *protocol_interface = handler->protocol_interface; |
| 3108 | |
| 3109 | return EFI_SUCCESS; |
| 3110 | } |
| 3111 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3112 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3113 | * efi_open_protocol() - open protocol interface on a handle |
| 3114 | * @handle: handle on which the protocol shall be opened |
| 3115 | * @protocol: GUID of the protocol |
| 3116 | * @protocol_interface: interface implementing the protocol |
| 3117 | * @agent_handle: handle of the driver |
| 3118 | * @controller_handle: handle of the controller |
| 3119 | * @attributes: attributes indicating how to open the protocol |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 3120 | * |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 3121 | * This function implements the OpenProtocol interface. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 3122 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3123 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 3124 | * details. |
| 3125 | * |
| 3126 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 3127 | */ |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 3128 | static efi_status_t EFIAPI efi_open_protocol |
| 3129 | (efi_handle_t handle, const efi_guid_t *protocol, |
| 3130 | void **protocol_interface, efi_handle_t agent_handle, |
| 3131 | efi_handle_t controller_handle, uint32_t attributes) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3132 | { |
Heinrich Schuchardt | e5e78a3 | 2017-11-26 14:05:15 +0100 | [diff] [blame] | 3133 | struct efi_handler *handler; |
xypron.glpk@gmx.de | f097c84 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 3134 | efi_status_t r = EFI_INVALID_PARAMETER; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3135 | |
Heinrich Schuchardt | 282249d | 2022-01-16 14:15:31 +0100 | [diff] [blame] | 3136 | EFI_ENTRY("%p, %pUs, %p, %p, %p, 0x%x", handle, protocol, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3137 | protocol_interface, agent_handle, controller_handle, |
| 3138 | attributes); |
xypron.glpk@gmx.de | c35c924 | 2017-07-11 22:06:14 +0200 | [diff] [blame] | 3139 | |
xypron.glpk@gmx.de | f097c84 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 3140 | if (!handle || !protocol || |
| 3141 | (!protocol_interface && attributes != |
| 3142 | EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) { |
xypron.glpk@gmx.de | c35c924 | 2017-07-11 22:06:14 +0200 | [diff] [blame] | 3143 | goto out; |
| 3144 | } |
| 3145 | |
xypron.glpk@gmx.de | f097c84 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 3146 | switch (attributes) { |
| 3147 | case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL: |
| 3148 | case EFI_OPEN_PROTOCOL_GET_PROTOCOL: |
| 3149 | case EFI_OPEN_PROTOCOL_TEST_PROTOCOL: |
| 3150 | break; |
| 3151 | case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER: |
| 3152 | if (controller_handle == handle) |
| 3153 | goto out; |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 3154 | /* fall-through */ |
xypron.glpk@gmx.de | f097c84 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 3155 | case EFI_OPEN_PROTOCOL_BY_DRIVER: |
| 3156 | case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE: |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 3157 | /* Check that the controller handle is valid */ |
| 3158 | if (!efi_search_obj(controller_handle)) |
xypron.glpk@gmx.de | f097c84 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 3159 | goto out; |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 3160 | /* fall-through */ |
xypron.glpk@gmx.de | f097c84 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 3161 | case EFI_OPEN_PROTOCOL_EXCLUSIVE: |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 3162 | /* Check that the agent handle is valid */ |
| 3163 | if (!efi_search_obj(agent_handle)) |
xypron.glpk@gmx.de | f097c84 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 3164 | goto out; |
| 3165 | break; |
| 3166 | default: |
| 3167 | goto out; |
| 3168 | } |
| 3169 | |
Heinrich Schuchardt | e5e78a3 | 2017-11-26 14:05:15 +0100 | [diff] [blame] | 3170 | r = efi_search_protocol(handle, protocol, &handler); |
Heinrich Schuchardt | b673e4b | 2019-05-05 11:24:53 +0200 | [diff] [blame] | 3171 | switch (r) { |
| 3172 | case EFI_SUCCESS: |
| 3173 | break; |
| 3174 | case EFI_NOT_FOUND: |
| 3175 | r = EFI_UNSUPPORTED; |
| 3176 | goto out; |
| 3177 | default: |
Heinrich Schuchardt | e5e78a3 | 2017-11-26 14:05:15 +0100 | [diff] [blame] | 3178 | goto out; |
Heinrich Schuchardt | b673e4b | 2019-05-05 11:24:53 +0200 | [diff] [blame] | 3179 | } |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3180 | |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 3181 | r = efi_protocol_open(handler, protocol_interface, agent_handle, |
| 3182 | controller_handle, attributes); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3183 | out: |
| 3184 | return EFI_EXIT(r); |
| 3185 | } |
| 3186 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3187 | /** |
Heinrich Schuchardt | 2650a4d | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3188 | * efi_start_image() - call the entry point of an image |
| 3189 | * @image_handle: handle of the image |
| 3190 | * @exit_data_size: size of the buffer |
| 3191 | * @exit_data: buffer to receive the exit data of the called image |
| 3192 | * |
| 3193 | * This function implements the StartImage service. |
| 3194 | * |
| 3195 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 3196 | * details. |
| 3197 | * |
| 3198 | * Return: status code |
| 3199 | */ |
| 3200 | efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle, |
| 3201 | efi_uintn_t *exit_data_size, |
| 3202 | u16 **exit_data) |
| 3203 | { |
| 3204 | struct efi_loaded_image_obj *image_obj = |
| 3205 | (struct efi_loaded_image_obj *)image_handle; |
| 3206 | efi_status_t ret; |
Heinrich Schuchardt | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3207 | void *info; |
| 3208 | efi_handle_t parent_image = current_image; |
Heinrich Schuchardt | 026c7ec | 2020-12-28 23:24:40 +0100 | [diff] [blame] | 3209 | efi_status_t exit_status; |
| 3210 | struct jmp_buf_data exit_jmp; |
Heinrich Schuchardt | 2650a4d | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3211 | |
| 3212 | EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data); |
| 3213 | |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 3214 | if (!efi_search_obj(image_handle)) |
| 3215 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 3216 | |
Heinrich Schuchardt | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3217 | /* Check parameters */ |
Heinrich Schuchardt | ff94bca | 2019-06-11 19:35:20 +0200 | [diff] [blame] | 3218 | if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE) |
| 3219 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 3220 | |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 3221 | if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED) |
| 3222 | return EFI_EXIT(EFI_SECURITY_VIOLATION); |
| 3223 | |
Heinrich Schuchardt | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3224 | ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image, |
| 3225 | &info, NULL, NULL, |
| 3226 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); |
| 3227 | if (ret != EFI_SUCCESS) |
| 3228 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 3229 | |
Heinrich Schuchardt | 3d44512 | 2019-04-30 17:57:30 +0200 | [diff] [blame] | 3230 | image_obj->exit_data_size = exit_data_size; |
| 3231 | image_obj->exit_data = exit_data; |
Heinrich Schuchardt | 026c7ec | 2020-12-28 23:24:40 +0100 | [diff] [blame] | 3232 | image_obj->exit_status = &exit_status; |
| 3233 | image_obj->exit_jmp = &exit_jmp; |
Heinrich Schuchardt | 3d44512 | 2019-04-30 17:57:30 +0200 | [diff] [blame] | 3234 | |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 3235 | if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) { |
| 3236 | if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) { |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 3237 | ret = efi_tcg2_measure_efi_app_invocation(image_obj); |
Masahisa Kojima | 38155ea | 2021-12-07 14:15:33 +0900 | [diff] [blame] | 3238 | if (ret == EFI_SECURITY_VIOLATION) { |
| 3239 | /* |
| 3240 | * TCG2 Protocol is installed but no TPM device found, |
| 3241 | * this is not expected. |
| 3242 | */ |
| 3243 | return EFI_EXIT(EFI_SECURITY_VIOLATION); |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 3244 | } |
| 3245 | } |
| 3246 | } |
| 3247 | |
Heinrich Schuchardt | 2650a4d | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3248 | /* call the image! */ |
Heinrich Schuchardt | 026c7ec | 2020-12-28 23:24:40 +0100 | [diff] [blame] | 3249 | if (setjmp(&exit_jmp)) { |
Heinrich Schuchardt | 2650a4d | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3250 | /* |
| 3251 | * We called the entry point of the child image with EFI_CALL |
| 3252 | * in the lines below. The child image called the Exit() boot |
| 3253 | * service efi_exit() which executed the long jump that brought |
| 3254 | * us to the current line. This implies that the second half |
| 3255 | * of the EFI_CALL macro has not been executed. |
| 3256 | */ |
Heinrich Schuchardt | f277d94 | 2020-09-10 12:22:54 +0200 | [diff] [blame] | 3257 | #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) |
Heinrich Schuchardt | 2650a4d | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3258 | /* |
| 3259 | * efi_exit() called efi_restore_gd(). We have to undo this |
| 3260 | * otherwise __efi_entry_check() will put the wrong value into |
| 3261 | * app_gd. |
| 3262 | */ |
Heinrich Schuchardt | 1a3732c | 2020-05-27 01:58:30 +0200 | [diff] [blame] | 3263 | set_gd(app_gd); |
Heinrich Schuchardt | 2650a4d | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3264 | #endif |
| 3265 | /* |
| 3266 | * To get ready to call EFI_EXIT below we have to execute the |
| 3267 | * missed out steps of EFI_CALL. |
| 3268 | */ |
| 3269 | assert(__efi_entry_check()); |
Heinrich Schuchardt | 952e206 | 2019-05-05 11:56:23 +0200 | [diff] [blame] | 3270 | EFI_PRINT("%lu returned by started image\n", |
Heinrich Schuchardt | 026c7ec | 2020-12-28 23:24:40 +0100 | [diff] [blame] | 3271 | (unsigned long)((uintptr_t)exit_status & |
Heinrich Schuchardt | 952e206 | 2019-05-05 11:56:23 +0200 | [diff] [blame] | 3272 | ~EFI_ERROR_MASK)); |
Heinrich Schuchardt | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3273 | current_image = parent_image; |
Heinrich Schuchardt | 026c7ec | 2020-12-28 23:24:40 +0100 | [diff] [blame] | 3274 | return EFI_EXIT(exit_status); |
Heinrich Schuchardt | 2650a4d | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3275 | } |
| 3276 | |
Heinrich Schuchardt | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3277 | current_image = image_handle; |
Heinrich Schuchardt | b27ced4 | 2019-05-01 14:20:18 +0200 | [diff] [blame] | 3278 | image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE; |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 3279 | EFI_PRINT("Jumping into 0x%p\n", image_obj->entry); |
Heinrich Schuchardt | 2650a4d | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3280 | ret = EFI_CALL(image_obj->entry(image_handle, &systab)); |
| 3281 | |
| 3282 | /* |
Heinrich Schuchardt | b7bc28d | 2020-01-10 22:06:54 +0100 | [diff] [blame] | 3283 | * Control is returned from a started UEFI image either by calling |
| 3284 | * Exit() (where exit data can be provided) or by simply returning from |
| 3285 | * the entry point. In the latter case call Exit() on behalf of the |
| 3286 | * image. |
Heinrich Schuchardt | 2650a4d | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3287 | */ |
| 3288 | return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL)); |
| 3289 | } |
| 3290 | |
| 3291 | /** |
Heinrich Schuchardt | 4ed66e5 | 2019-05-01 18:25:45 +0200 | [diff] [blame] | 3292 | * efi_delete_image() - delete loaded image from memory) |
| 3293 | * |
| 3294 | * @image_obj: handle of the loaded image |
| 3295 | * @loaded_image_protocol: loaded image protocol |
| 3296 | */ |
Heinrich Schuchardt | e3bca2a | 2019-06-02 20:02:32 +0200 | [diff] [blame] | 3297 | static efi_status_t efi_delete_image |
| 3298 | (struct efi_loaded_image_obj *image_obj, |
| 3299 | struct efi_loaded_image *loaded_image_protocol) |
Heinrich Schuchardt | 4ed66e5 | 2019-05-01 18:25:45 +0200 | [diff] [blame] | 3300 | { |
Heinrich Schuchardt | e3bca2a | 2019-06-02 20:02:32 +0200 | [diff] [blame] | 3301 | struct efi_object *efiobj; |
| 3302 | efi_status_t r, ret = EFI_SUCCESS; |
| 3303 | |
| 3304 | close_next: |
| 3305 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
| 3306 | struct efi_handler *protocol; |
| 3307 | |
| 3308 | list_for_each_entry(protocol, &efiobj->protocols, link) { |
| 3309 | struct efi_open_protocol_info_item *info; |
| 3310 | |
| 3311 | list_for_each_entry(info, &protocol->open_infos, link) { |
| 3312 | if (info->info.agent_handle != |
| 3313 | (efi_handle_t)image_obj) |
| 3314 | continue; |
Heinrich Schuchardt | 5e39433 | 2022-10-07 15:18:15 +0200 | [diff] [blame] | 3315 | r = efi_close_protocol( |
| 3316 | efiobj, &protocol->guid, |
| 3317 | info->info.agent_handle, |
| 3318 | info->info.controller_handle); |
Heinrich Schuchardt | e3bca2a | 2019-06-02 20:02:32 +0200 | [diff] [blame] | 3319 | if (r != EFI_SUCCESS) |
| 3320 | ret = r; |
| 3321 | /* |
| 3322 | * Closing protocols may results in further |
| 3323 | * items being deleted. To play it safe loop |
| 3324 | * over all elements again. |
| 3325 | */ |
| 3326 | goto close_next; |
| 3327 | } |
| 3328 | } |
| 3329 | } |
| 3330 | |
Heinrich Schuchardt | 4ed66e5 | 2019-05-01 18:25:45 +0200 | [diff] [blame] | 3331 | efi_free_pages((uintptr_t)loaded_image_protocol->image_base, |
| 3332 | efi_size_in_pages(loaded_image_protocol->image_size)); |
| 3333 | efi_delete_handle(&image_obj->header); |
Heinrich Schuchardt | e3bca2a | 2019-06-02 20:02:32 +0200 | [diff] [blame] | 3334 | |
| 3335 | return ret; |
Heinrich Schuchardt | 4ed66e5 | 2019-05-01 18:25:45 +0200 | [diff] [blame] | 3336 | } |
| 3337 | |
| 3338 | /** |
Heinrich Schuchardt | 6b3581a | 2019-05-01 19:04:32 +0200 | [diff] [blame] | 3339 | * efi_unload_image() - unload an EFI image |
| 3340 | * @image_handle: handle of the image to be unloaded |
| 3341 | * |
| 3342 | * This function implements the UnloadImage service. |
| 3343 | * |
| 3344 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 3345 | * details. |
| 3346 | * |
| 3347 | * Return: status code |
| 3348 | */ |
| 3349 | efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle) |
| 3350 | { |
Heinrich Schuchardt | 4ed66e5 | 2019-05-01 18:25:45 +0200 | [diff] [blame] | 3351 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | 6b3581a | 2019-05-01 19:04:32 +0200 | [diff] [blame] | 3352 | struct efi_object *efiobj; |
Heinrich Schuchardt | 4ed66e5 | 2019-05-01 18:25:45 +0200 | [diff] [blame] | 3353 | struct efi_loaded_image *loaded_image_protocol; |
Heinrich Schuchardt | 6b3581a | 2019-05-01 19:04:32 +0200 | [diff] [blame] | 3354 | |
| 3355 | EFI_ENTRY("%p", image_handle); |
Heinrich Schuchardt | 6b3581a | 2019-05-01 19:04:32 +0200 | [diff] [blame] | 3356 | |
Heinrich Schuchardt | 4ed66e5 | 2019-05-01 18:25:45 +0200 | [diff] [blame] | 3357 | efiobj = efi_search_obj(image_handle); |
| 3358 | if (!efiobj) { |
| 3359 | ret = EFI_INVALID_PARAMETER; |
| 3360 | goto out; |
| 3361 | } |
| 3362 | /* Find the loaded image protocol */ |
| 3363 | ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image, |
| 3364 | (void **)&loaded_image_protocol, |
| 3365 | NULL, NULL, |
| 3366 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); |
| 3367 | if (ret != EFI_SUCCESS) { |
| 3368 | ret = EFI_INVALID_PARAMETER; |
| 3369 | goto out; |
| 3370 | } |
| 3371 | switch (efiobj->type) { |
| 3372 | case EFI_OBJECT_TYPE_STARTED_IMAGE: |
| 3373 | /* Call the unload function */ |
| 3374 | if (!loaded_image_protocol->unload) { |
| 3375 | ret = EFI_UNSUPPORTED; |
| 3376 | goto out; |
| 3377 | } |
| 3378 | ret = EFI_CALL(loaded_image_protocol->unload(image_handle)); |
| 3379 | if (ret != EFI_SUCCESS) |
| 3380 | goto out; |
| 3381 | break; |
| 3382 | case EFI_OBJECT_TYPE_LOADED_IMAGE: |
| 3383 | break; |
| 3384 | default: |
| 3385 | ret = EFI_INVALID_PARAMETER; |
| 3386 | goto out; |
| 3387 | } |
| 3388 | efi_delete_image((struct efi_loaded_image_obj *)efiobj, |
| 3389 | loaded_image_protocol); |
| 3390 | out: |
| 3391 | return EFI_EXIT(ret); |
Heinrich Schuchardt | 6b3581a | 2019-05-01 19:04:32 +0200 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | /** |
Heinrich Schuchardt | 3d44512 | 2019-04-30 17:57:30 +0200 | [diff] [blame] | 3395 | * efi_update_exit_data() - fill exit data parameters of StartImage() |
| 3396 | * |
Heinrich Schuchardt | 74c2529 | 2019-07-14 11:25:06 +0200 | [diff] [blame] | 3397 | * @image_obj: image handle |
| 3398 | * @exit_data_size: size of the exit data buffer |
| 3399 | * @exit_data: buffer with data returned by UEFI payload |
Heinrich Schuchardt | 3d44512 | 2019-04-30 17:57:30 +0200 | [diff] [blame] | 3400 | * Return: status code |
| 3401 | */ |
| 3402 | static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj, |
| 3403 | efi_uintn_t exit_data_size, |
| 3404 | u16 *exit_data) |
| 3405 | { |
| 3406 | efi_status_t ret; |
| 3407 | |
| 3408 | /* |
| 3409 | * If exit_data is not provided to StartImage(), exit_data_size must be |
| 3410 | * ignored. |
| 3411 | */ |
| 3412 | if (!image_obj->exit_data) |
| 3413 | return EFI_SUCCESS; |
| 3414 | if (image_obj->exit_data_size) |
| 3415 | *image_obj->exit_data_size = exit_data_size; |
| 3416 | if (exit_data_size && exit_data) { |
| 3417 | ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, |
| 3418 | exit_data_size, |
| 3419 | (void **)image_obj->exit_data); |
| 3420 | if (ret != EFI_SUCCESS) |
| 3421 | return ret; |
| 3422 | memcpy(*image_obj->exit_data, exit_data, exit_data_size); |
| 3423 | } else { |
| 3424 | image_obj->exit_data = NULL; |
| 3425 | } |
| 3426 | return EFI_SUCCESS; |
| 3427 | } |
| 3428 | |
| 3429 | /** |
Heinrich Schuchardt | 2650a4d | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3430 | * efi_exit() - leave an EFI application or driver |
| 3431 | * @image_handle: handle of the application or driver that is exiting |
| 3432 | * @exit_status: status code |
| 3433 | * @exit_data_size: size of the buffer in bytes |
| 3434 | * @exit_data: buffer with data describing an error |
| 3435 | * |
| 3436 | * This function implements the Exit service. |
| 3437 | * |
| 3438 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 3439 | * details. |
| 3440 | * |
| 3441 | * Return: status code |
| 3442 | */ |
| 3443 | static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle, |
| 3444 | efi_status_t exit_status, |
| 3445 | efi_uintn_t exit_data_size, |
| 3446 | u16 *exit_data) |
| 3447 | { |
| 3448 | /* |
| 3449 | * TODO: We should call the unload procedure of the loaded |
| 3450 | * image protocol. |
| 3451 | */ |
Heinrich Schuchardt | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3452 | efi_status_t ret; |
Heinrich Schuchardt | 3758752 | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 3453 | struct efi_loaded_image *loaded_image_protocol; |
Heinrich Schuchardt | 2650a4d | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3454 | struct efi_loaded_image_obj *image_obj = |
| 3455 | (struct efi_loaded_image_obj *)image_handle; |
Heinrich Schuchardt | 026c7ec | 2020-12-28 23:24:40 +0100 | [diff] [blame] | 3456 | struct jmp_buf_data *exit_jmp; |
Heinrich Schuchardt | 2650a4d | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3457 | |
| 3458 | EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status, |
| 3459 | exit_data_size, exit_data); |
| 3460 | |
Heinrich Schuchardt | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3461 | /* Check parameters */ |
Heinrich Schuchardt | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3462 | ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image, |
Heinrich Schuchardt | 3758752 | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 3463 | (void **)&loaded_image_protocol, |
| 3464 | NULL, NULL, |
Heinrich Schuchardt | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3465 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); |
Heinrich Schuchardt | 3758752 | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 3466 | if (ret != EFI_SUCCESS) { |
| 3467 | ret = EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3468 | goto out; |
Heinrich Schuchardt | 3758752 | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 3469 | } |
| 3470 | |
| 3471 | /* Unloading of unstarted images */ |
| 3472 | switch (image_obj->header.type) { |
| 3473 | case EFI_OBJECT_TYPE_STARTED_IMAGE: |
| 3474 | break; |
| 3475 | case EFI_OBJECT_TYPE_LOADED_IMAGE: |
| 3476 | efi_delete_image(image_obj, loaded_image_protocol); |
| 3477 | ret = EFI_SUCCESS; |
| 3478 | goto out; |
| 3479 | default: |
| 3480 | /* Handle does not refer to loaded image */ |
| 3481 | ret = EFI_INVALID_PARAMETER; |
| 3482 | goto out; |
| 3483 | } |
| 3484 | /* A started image can only be unloaded it is the last one started. */ |
| 3485 | if (image_handle != current_image) { |
| 3486 | ret = EFI_INVALID_PARAMETER; |
| 3487 | goto out; |
| 3488 | } |
Heinrich Schuchardt | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3489 | |
Heinrich Schuchardt | 3d44512 | 2019-04-30 17:57:30 +0200 | [diff] [blame] | 3490 | /* Exit data is only foreseen in case of failure. */ |
| 3491 | if (exit_status != EFI_SUCCESS) { |
| 3492 | ret = efi_update_exit_data(image_obj, exit_data_size, |
| 3493 | exit_data); |
| 3494 | /* Exiting has priority. Don't return error to caller. */ |
| 3495 | if (ret != EFI_SUCCESS) |
| 3496 | EFI_PRINT("%s: out of memory\n", __func__); |
| 3497 | } |
Heinrich Schuchardt | 026c7ec | 2020-12-28 23:24:40 +0100 | [diff] [blame] | 3498 | /* efi_delete_image() frees image_obj. Copy before the call. */ |
| 3499 | exit_jmp = image_obj->exit_jmp; |
| 3500 | *image_obj->exit_status = exit_status; |
Heinrich Schuchardt | 3758752 | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 3501 | if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION || |
| 3502 | exit_status != EFI_SUCCESS) |
| 3503 | efi_delete_image(image_obj, loaded_image_protocol); |
Heinrich Schuchardt | 3d44512 | 2019-04-30 17:57:30 +0200 | [diff] [blame] | 3504 | |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 3505 | if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) { |
| 3506 | if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) { |
| 3507 | ret = efi_tcg2_measure_efi_app_exit(); |
| 3508 | if (ret != EFI_SUCCESS) { |
| 3509 | log_warning("tcg2 measurement fails(0x%lx)\n", |
| 3510 | ret); |
| 3511 | } |
| 3512 | } |
| 3513 | } |
| 3514 | |
Heinrich Schuchardt | 2650a4d | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3515 | /* Make sure entry/exit counts for EFI world cross-overs match */ |
| 3516 | EFI_EXIT(exit_status); |
| 3517 | |
| 3518 | /* |
| 3519 | * But longjmp out with the U-Boot gd, not the application's, as |
| 3520 | * the other end is a setjmp call inside EFI context. |
| 3521 | */ |
| 3522 | efi_restore_gd(); |
| 3523 | |
Heinrich Schuchardt | 026c7ec | 2020-12-28 23:24:40 +0100 | [diff] [blame] | 3524 | longjmp(exit_jmp, 1); |
Heinrich Schuchardt | 2650a4d | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3525 | |
| 3526 | panic("EFI application exited"); |
Heinrich Schuchardt | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3527 | out: |
Heinrich Schuchardt | 3758752 | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 3528 | return EFI_EXIT(ret); |
Heinrich Schuchardt | 2650a4d | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3532 | * efi_handle_protocol() - get interface of a protocol on a handle |
| 3533 | * @handle: handle on which the protocol shall be opened |
| 3534 | * @protocol: GUID of the protocol |
| 3535 | * @protocol_interface: interface implementing the protocol |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 3536 | * |
| 3537 | * This function implements the HandleProtocol service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3538 | * |
| 3539 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 3540 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 3541 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3542 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 3543 | */ |
AKASHI Takahiro | d99b1b3 | 2020-03-17 11:12:36 +0900 | [diff] [blame] | 3544 | efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle, |
| 3545 | const efi_guid_t *protocol, |
| 3546 | void **protocol_interface) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3547 | { |
Heinrich Schuchardt | ef09615 | 2019-06-01 19:29:39 +0200 | [diff] [blame] | 3548 | return efi_open_protocol(handle, protocol, protocol_interface, efi_root, |
xypron.glpk@gmx.de | 1bf5d87 | 2017-06-29 21:16:19 +0200 | [diff] [blame] | 3549 | NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3550 | } |
| 3551 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3552 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3553 | * efi_bind_controller() - bind a single driver to a controller |
| 3554 | * @controller_handle: controller handle |
| 3555 | * @driver_image_handle: driver handle |
| 3556 | * @remain_device_path: remaining path |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3557 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3558 | * Return: status code |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3559 | */ |
Heinrich Schuchardt | 760255f | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3560 | static efi_status_t efi_bind_controller( |
| 3561 | efi_handle_t controller_handle, |
| 3562 | efi_handle_t driver_image_handle, |
| 3563 | struct efi_device_path *remain_device_path) |
| 3564 | { |
| 3565 | struct efi_driver_binding_protocol *binding_protocol; |
| 3566 | efi_status_t r; |
| 3567 | |
| 3568 | r = EFI_CALL(efi_open_protocol(driver_image_handle, |
| 3569 | &efi_guid_driver_binding_protocol, |
| 3570 | (void **)&binding_protocol, |
| 3571 | driver_image_handle, NULL, |
| 3572 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); |
| 3573 | if (r != EFI_SUCCESS) |
| 3574 | return r; |
| 3575 | r = EFI_CALL(binding_protocol->supported(binding_protocol, |
| 3576 | controller_handle, |
| 3577 | remain_device_path)); |
| 3578 | if (r == EFI_SUCCESS) |
| 3579 | r = EFI_CALL(binding_protocol->start(binding_protocol, |
| 3580 | controller_handle, |
| 3581 | remain_device_path)); |
Heinrich Schuchardt | 5e39433 | 2022-10-07 15:18:15 +0200 | [diff] [blame] | 3582 | efi_close_protocol(driver_image_handle, |
| 3583 | &efi_guid_driver_binding_protocol, |
| 3584 | driver_image_handle, NULL); |
Heinrich Schuchardt | 760255f | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3585 | return r; |
| 3586 | } |
| 3587 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3588 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3589 | * efi_connect_single_controller() - connect a single driver to a controller |
| 3590 | * @controller_handle: controller |
| 3591 | * @driver_image_handle: driver |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 3592 | * @remain_device_path: remaining path |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3593 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3594 | * Return: status code |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3595 | */ |
Heinrich Schuchardt | 760255f | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3596 | static efi_status_t efi_connect_single_controller( |
| 3597 | efi_handle_t controller_handle, |
| 3598 | efi_handle_t *driver_image_handle, |
| 3599 | struct efi_device_path *remain_device_path) |
| 3600 | { |
| 3601 | efi_handle_t *buffer; |
| 3602 | size_t count; |
| 3603 | size_t i; |
| 3604 | efi_status_t r; |
| 3605 | size_t connected = 0; |
| 3606 | |
| 3607 | /* Get buffer with all handles with driver binding protocol */ |
| 3608 | r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, |
| 3609 | &efi_guid_driver_binding_protocol, |
| 3610 | NULL, &count, &buffer)); |
| 3611 | if (r != EFI_SUCCESS) |
| 3612 | return r; |
| 3613 | |
Heinrich Schuchardt | da8f900 | 2019-07-03 20:27:24 +0200 | [diff] [blame] | 3614 | /* Context Override */ |
Heinrich Schuchardt | 760255f | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3615 | if (driver_image_handle) { |
| 3616 | for (; *driver_image_handle; ++driver_image_handle) { |
| 3617 | for (i = 0; i < count; ++i) { |
| 3618 | if (buffer[i] == *driver_image_handle) { |
| 3619 | buffer[i] = NULL; |
| 3620 | r = efi_bind_controller( |
| 3621 | controller_handle, |
| 3622 | *driver_image_handle, |
| 3623 | remain_device_path); |
| 3624 | /* |
| 3625 | * For drivers that do not support the |
| 3626 | * controller or are already connected |
| 3627 | * we receive an error code here. |
| 3628 | */ |
| 3629 | if (r == EFI_SUCCESS) |
| 3630 | ++connected; |
| 3631 | } |
| 3632 | } |
| 3633 | } |
| 3634 | } |
| 3635 | |
| 3636 | /* |
| 3637 | * TODO: Some overrides are not yet implemented: |
| 3638 | * - Platform Driver Override |
| 3639 | * - Driver Family Override Search |
| 3640 | * - Bus Specific Driver Override |
| 3641 | */ |
| 3642 | |
| 3643 | /* Driver Binding Search */ |
| 3644 | for (i = 0; i < count; ++i) { |
| 3645 | if (buffer[i]) { |
| 3646 | r = efi_bind_controller(controller_handle, |
| 3647 | buffer[i], |
| 3648 | remain_device_path); |
| 3649 | if (r == EFI_SUCCESS) |
| 3650 | ++connected; |
| 3651 | } |
| 3652 | } |
| 3653 | |
| 3654 | efi_free_pool(buffer); |
| 3655 | if (!connected) |
| 3656 | return EFI_NOT_FOUND; |
| 3657 | return EFI_SUCCESS; |
| 3658 | } |
| 3659 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3660 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3661 | * efi_connect_controller() - connect a controller to a driver |
| 3662 | * @controller_handle: handle of the controller |
| 3663 | * @driver_image_handle: handle of the driver |
| 3664 | * @remain_device_path: device path of a child controller |
| 3665 | * @recursive: true to connect all child controllers |
Heinrich Schuchardt | 760255f | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3666 | * |
| 3667 | * This function implements the ConnectController service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3668 | * |
| 3669 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 3670 | * details. |
Heinrich Schuchardt | 760255f | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3671 | * |
| 3672 | * First all driver binding protocol handles are tried for binding drivers. |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 3673 | * Afterwards all handles that have opened a protocol of the controller |
Heinrich Schuchardt | 760255f | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3674 | * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers. |
| 3675 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3676 | * Return: status code |
Heinrich Schuchardt | 760255f | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3677 | */ |
| 3678 | static efi_status_t EFIAPI efi_connect_controller( |
| 3679 | efi_handle_t controller_handle, |
| 3680 | efi_handle_t *driver_image_handle, |
| 3681 | struct efi_device_path *remain_device_path, |
| 3682 | bool recursive) |
| 3683 | { |
| 3684 | efi_status_t r; |
| 3685 | efi_status_t ret = EFI_NOT_FOUND; |
| 3686 | struct efi_object *efiobj; |
| 3687 | |
Heinrich Schuchardt | 7c89fb0 | 2018-12-09 16:39:20 +0100 | [diff] [blame] | 3688 | EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle, |
Heinrich Schuchardt | 760255f | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3689 | remain_device_path, recursive); |
| 3690 | |
| 3691 | efiobj = efi_search_obj(controller_handle); |
| 3692 | if (!efiobj) { |
| 3693 | ret = EFI_INVALID_PARAMETER; |
| 3694 | goto out; |
| 3695 | } |
| 3696 | |
| 3697 | r = efi_connect_single_controller(controller_handle, |
| 3698 | driver_image_handle, |
| 3699 | remain_device_path); |
| 3700 | if (r == EFI_SUCCESS) |
| 3701 | ret = EFI_SUCCESS; |
| 3702 | if (recursive) { |
| 3703 | struct efi_handler *handler; |
| 3704 | struct efi_open_protocol_info_item *item; |
| 3705 | |
| 3706 | list_for_each_entry(handler, &efiobj->protocols, link) { |
| 3707 | list_for_each_entry(item, &handler->open_infos, link) { |
| 3708 | if (item->info.attributes & |
| 3709 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) { |
| 3710 | r = EFI_CALL(efi_connect_controller( |
| 3711 | item->info.controller_handle, |
| 3712 | driver_image_handle, |
| 3713 | remain_device_path, |
| 3714 | recursive)); |
| 3715 | if (r == EFI_SUCCESS) |
| 3716 | ret = EFI_SUCCESS; |
| 3717 | } |
| 3718 | } |
| 3719 | } |
| 3720 | } |
Heinrich Schuchardt | da8f900 | 2019-07-03 20:27:24 +0200 | [diff] [blame] | 3721 | /* Check for child controller specified by end node */ |
Heinrich Schuchardt | 760255f | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3722 | if (ret != EFI_SUCCESS && remain_device_path && |
| 3723 | remain_device_path->type == DEVICE_PATH_TYPE_END) |
| 3724 | ret = EFI_SUCCESS; |
| 3725 | out: |
| 3726 | return EFI_EXIT(ret); |
| 3727 | } |
| 3728 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3729 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3730 | * efi_reinstall_protocol_interface() - reinstall protocol interface |
| 3731 | * @handle: handle on which the protocol shall be reinstalled |
| 3732 | * @protocol: GUID of the protocol to be installed |
| 3733 | * @old_interface: interface to be removed |
| 3734 | * @new_interface: interface to be installed |
Heinrich Schuchardt | 90761b8 | 2018-05-11 12:09:22 +0200 | [diff] [blame] | 3735 | * |
| 3736 | * This function implements the ReinstallProtocolInterface service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3737 | * |
| 3738 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 3739 | * details. |
Heinrich Schuchardt | 90761b8 | 2018-05-11 12:09:22 +0200 | [diff] [blame] | 3740 | * |
| 3741 | * The old interface is uninstalled. The new interface is installed. |
| 3742 | * Drivers are connected. |
| 3743 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3744 | * Return: status code |
Heinrich Schuchardt | 90761b8 | 2018-05-11 12:09:22 +0200 | [diff] [blame] | 3745 | */ |
| 3746 | static efi_status_t EFIAPI efi_reinstall_protocol_interface( |
| 3747 | efi_handle_t handle, const efi_guid_t *protocol, |
| 3748 | void *old_interface, void *new_interface) |
| 3749 | { |
| 3750 | efi_status_t ret; |
| 3751 | |
Heinrich Schuchardt | 282249d | 2022-01-16 14:15:31 +0100 | [diff] [blame] | 3752 | EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, old_interface, |
Heinrich Schuchardt | 90761b8 | 2018-05-11 12:09:22 +0200 | [diff] [blame] | 3753 | new_interface); |
Heinrich Schuchardt | 1b45134 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 3754 | |
| 3755 | /* Uninstall protocol but do not delete handle */ |
Ilias Apalodimas | c76eade | 2023-08-24 17:21:09 +0300 | [diff] [blame] | 3756 | ret = efi_uninstall_protocol(handle, protocol, old_interface, true); |
Heinrich Schuchardt | 90761b8 | 2018-05-11 12:09:22 +0200 | [diff] [blame] | 3757 | if (ret != EFI_SUCCESS) |
| 3758 | goto out; |
Heinrich Schuchardt | 1b45134 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 3759 | |
| 3760 | /* Install the new protocol */ |
| 3761 | ret = efi_add_protocol(handle, protocol, new_interface); |
| 3762 | /* |
| 3763 | * The UEFI spec does not specify what should happen to the handle |
| 3764 | * if in case of an error no protocol interface remains on the handle. |
| 3765 | * So let's do nothing here. |
| 3766 | */ |
Heinrich Schuchardt | 90761b8 | 2018-05-11 12:09:22 +0200 | [diff] [blame] | 3767 | if (ret != EFI_SUCCESS) |
| 3768 | goto out; |
| 3769 | /* |
| 3770 | * The returned status code has to be ignored. |
| 3771 | * Do not create an error if no suitable driver for the handle exists. |
| 3772 | */ |
| 3773 | EFI_CALL(efi_connect_controller(handle, NULL, NULL, true)); |
| 3774 | out: |
| 3775 | return EFI_EXIT(ret); |
| 3776 | } |
| 3777 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3778 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3779 | * efi_get_child_controllers() - get all child controllers associated to a driver |
| 3780 | * @efiobj: handle of the controller |
| 3781 | * @driver_handle: handle of the driver |
| 3782 | * @number_of_children: number of child controllers |
| 3783 | * @child_handle_buffer: handles of the the child controllers |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3784 | * |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3785 | * The allocated buffer has to be freed with free(). |
| 3786 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3787 | * Return: status code |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3788 | */ |
| 3789 | static efi_status_t efi_get_child_controllers( |
| 3790 | struct efi_object *efiobj, |
| 3791 | efi_handle_t driver_handle, |
| 3792 | efi_uintn_t *number_of_children, |
| 3793 | efi_handle_t **child_handle_buffer) |
| 3794 | { |
| 3795 | struct efi_handler *handler; |
| 3796 | struct efi_open_protocol_info_item *item; |
| 3797 | efi_uintn_t count = 0, i; |
| 3798 | bool duplicate; |
| 3799 | |
| 3800 | /* Count all child controller associations */ |
| 3801 | list_for_each_entry(handler, &efiobj->protocols, link) { |
| 3802 | list_for_each_entry(item, &handler->open_infos, link) { |
| 3803 | if (item->info.agent_handle == driver_handle && |
| 3804 | item->info.attributes & |
| 3805 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) |
| 3806 | ++count; |
| 3807 | } |
| 3808 | } |
| 3809 | /* |
| 3810 | * Create buffer. In case of duplicate child controller assignments |
| 3811 | * the buffer will be too large. But that does not harm. |
| 3812 | */ |
| 3813 | *number_of_children = 0; |
Heinrich Schuchardt | 0f321b6 | 2020-07-07 04:21:26 +0200 | [diff] [blame] | 3814 | if (!count) |
| 3815 | return EFI_SUCCESS; |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3816 | *child_handle_buffer = calloc(count, sizeof(efi_handle_t)); |
| 3817 | if (!*child_handle_buffer) |
| 3818 | return EFI_OUT_OF_RESOURCES; |
| 3819 | /* Copy unique child handles */ |
| 3820 | list_for_each_entry(handler, &efiobj->protocols, link) { |
| 3821 | list_for_each_entry(item, &handler->open_infos, link) { |
| 3822 | if (item->info.agent_handle == driver_handle && |
| 3823 | item->info.attributes & |
| 3824 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) { |
| 3825 | /* Check this is a new child controller */ |
| 3826 | duplicate = false; |
| 3827 | for (i = 0; i < *number_of_children; ++i) { |
| 3828 | if ((*child_handle_buffer)[i] == |
| 3829 | item->info.controller_handle) |
| 3830 | duplicate = true; |
| 3831 | } |
| 3832 | /* Copy handle to buffer */ |
| 3833 | if (!duplicate) { |
| 3834 | i = (*number_of_children)++; |
| 3835 | (*child_handle_buffer)[i] = |
| 3836 | item->info.controller_handle; |
| 3837 | } |
| 3838 | } |
| 3839 | } |
| 3840 | } |
| 3841 | return EFI_SUCCESS; |
| 3842 | } |
| 3843 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3844 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3845 | * efi_disconnect_controller() - disconnect a controller from a driver |
| 3846 | * @controller_handle: handle of the controller |
| 3847 | * @driver_image_handle: handle of the driver |
| 3848 | * @child_handle: handle of the child to destroy |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3849 | * |
| 3850 | * This function implements the DisconnectController service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3851 | * |
| 3852 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 3853 | * details. |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3854 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3855 | * Return: status code |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3856 | */ |
| 3857 | static efi_status_t EFIAPI efi_disconnect_controller( |
| 3858 | efi_handle_t controller_handle, |
| 3859 | efi_handle_t driver_image_handle, |
| 3860 | efi_handle_t child_handle) |
| 3861 | { |
| 3862 | struct efi_driver_binding_protocol *binding_protocol; |
| 3863 | efi_handle_t *child_handle_buffer = NULL; |
| 3864 | size_t number_of_children = 0; |
| 3865 | efi_status_t r; |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3866 | struct efi_object *efiobj; |
Heinrich Schuchardt | c7ce404 | 2020-10-28 18:45:47 +0100 | [diff] [blame] | 3867 | bool sole_child; |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3868 | |
| 3869 | EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle, |
| 3870 | child_handle); |
| 3871 | |
| 3872 | efiobj = efi_search_obj(controller_handle); |
| 3873 | if (!efiobj) { |
| 3874 | r = EFI_INVALID_PARAMETER; |
| 3875 | goto out; |
| 3876 | } |
| 3877 | |
| 3878 | if (child_handle && !efi_search_obj(child_handle)) { |
| 3879 | r = EFI_INVALID_PARAMETER; |
| 3880 | goto out; |
| 3881 | } |
| 3882 | |
| 3883 | /* If no driver handle is supplied, disconnect all drivers */ |
| 3884 | if (!driver_image_handle) { |
| 3885 | r = efi_disconnect_all_drivers(efiobj, NULL, child_handle); |
| 3886 | goto out; |
| 3887 | } |
| 3888 | |
| 3889 | /* Create list of child handles */ |
Heinrich Schuchardt | c7ce404 | 2020-10-28 18:45:47 +0100 | [diff] [blame] | 3890 | r = efi_get_child_controllers(efiobj, |
| 3891 | driver_image_handle, |
| 3892 | &number_of_children, |
| 3893 | &child_handle_buffer); |
| 3894 | if (r != EFI_SUCCESS) |
| 3895 | return r; |
| 3896 | sole_child = (number_of_children == 1); |
| 3897 | |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3898 | if (child_handle) { |
| 3899 | number_of_children = 1; |
Heinrich Schuchardt | c7ce404 | 2020-10-28 18:45:47 +0100 | [diff] [blame] | 3900 | free(child_handle_buffer); |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3901 | child_handle_buffer = &child_handle; |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3902 | } |
| 3903 | |
| 3904 | /* Get the driver binding protocol */ |
| 3905 | r = EFI_CALL(efi_open_protocol(driver_image_handle, |
| 3906 | &efi_guid_driver_binding_protocol, |
| 3907 | (void **)&binding_protocol, |
| 3908 | driver_image_handle, NULL, |
| 3909 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); |
Heinrich Schuchardt | 7962284 | 2019-09-13 18:20:40 +0200 | [diff] [blame] | 3910 | if (r != EFI_SUCCESS) { |
| 3911 | r = EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3912 | goto out; |
Heinrich Schuchardt | 7962284 | 2019-09-13 18:20:40 +0200 | [diff] [blame] | 3913 | } |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3914 | /* Remove the children */ |
| 3915 | if (number_of_children) { |
| 3916 | r = EFI_CALL(binding_protocol->stop(binding_protocol, |
| 3917 | controller_handle, |
| 3918 | number_of_children, |
| 3919 | child_handle_buffer)); |
Heinrich Schuchardt | 7962284 | 2019-09-13 18:20:40 +0200 | [diff] [blame] | 3920 | if (r != EFI_SUCCESS) { |
| 3921 | r = EFI_DEVICE_ERROR; |
| 3922 | goto out; |
| 3923 | } |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3924 | } |
| 3925 | /* Remove the driver */ |
Heinrich Schuchardt | c7ce404 | 2020-10-28 18:45:47 +0100 | [diff] [blame] | 3926 | if (!child_handle || sole_child) { |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3927 | r = EFI_CALL(binding_protocol->stop(binding_protocol, |
| 3928 | controller_handle, |
| 3929 | 0, NULL)); |
Heinrich Schuchardt | 7962284 | 2019-09-13 18:20:40 +0200 | [diff] [blame] | 3930 | if (r != EFI_SUCCESS) { |
| 3931 | r = EFI_DEVICE_ERROR; |
| 3932 | goto out; |
| 3933 | } |
| 3934 | } |
Heinrich Schuchardt | 5e39433 | 2022-10-07 15:18:15 +0200 | [diff] [blame] | 3935 | efi_close_protocol(driver_image_handle, |
| 3936 | &efi_guid_driver_binding_protocol, |
| 3937 | driver_image_handle, NULL); |
Heinrich Schuchardt | 7962284 | 2019-09-13 18:20:40 +0200 | [diff] [blame] | 3938 | r = EFI_SUCCESS; |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3939 | out: |
| 3940 | if (!child_handle) |
| 3941 | free(child_handle_buffer); |
| 3942 | return EFI_EXIT(r); |
| 3943 | } |
| 3944 | |
Heinrich Schuchardt | 15070db | 2018-06-28 12:45:31 +0200 | [diff] [blame] | 3945 | static struct efi_boot_services efi_boot_services = { |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3946 | .hdr = { |
Heinrich Schuchardt | e75b3cb | 2018-06-28 12:45:27 +0200 | [diff] [blame] | 3947 | .signature = EFI_BOOT_SERVICES_SIGNATURE, |
| 3948 | .revision = EFI_SPECIFICATION_VERSION, |
Heinrich Schuchardt | 1020425 | 2018-06-28 12:45:29 +0200 | [diff] [blame] | 3949 | .headersize = sizeof(struct efi_boot_services), |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3950 | }, |
| 3951 | .raise_tpl = efi_raise_tpl, |
| 3952 | .restore_tpl = efi_restore_tpl, |
| 3953 | .allocate_pages = efi_allocate_pages_ext, |
| 3954 | .free_pages = efi_free_pages_ext, |
| 3955 | .get_memory_map = efi_get_memory_map_ext, |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 3956 | .allocate_pool = efi_allocate_pool_ext, |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 3957 | .free_pool = efi_free_pool_ext, |
xypron.glpk@gmx.de | 852a0e177 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 3958 | .create_event = efi_create_event_ext, |
xypron.glpk@gmx.de | a587fd1 | 2017-07-18 20:17:21 +0200 | [diff] [blame] | 3959 | .set_timer = efi_set_timer_ext, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3960 | .wait_for_event = efi_wait_for_event, |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 3961 | .signal_event = efi_signal_event_ext, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3962 | .close_event = efi_close_event, |
| 3963 | .check_event = efi_check_event, |
Heinrich Schuchardt | 0a27ac8 | 2017-11-06 21:17:44 +0100 | [diff] [blame] | 3964 | .install_protocol_interface = efi_install_protocol_interface, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3965 | .reinstall_protocol_interface = efi_reinstall_protocol_interface, |
Heinrich Schuchardt | 7cdc17f | 2017-11-06 21:17:45 +0100 | [diff] [blame] | 3966 | .uninstall_protocol_interface = efi_uninstall_protocol_interface, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3967 | .handle_protocol = efi_handle_protocol, |
| 3968 | .reserved = NULL, |
| 3969 | .register_protocol_notify = efi_register_protocol_notify, |
xypron.glpk@gmx.de | 69f9403 | 2017-07-11 22:06:21 +0200 | [diff] [blame] | 3970 | .locate_handle = efi_locate_handle_ext, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3971 | .locate_device_path = efi_locate_device_path, |
Alexander Graf | c5c1163 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 3972 | .install_configuration_table = efi_install_configuration_table_ext, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3973 | .load_image = efi_load_image, |
| 3974 | .start_image = efi_start_image, |
Alexander Graf | 988c066 | 2016-05-20 23:28:23 +0200 | [diff] [blame] | 3975 | .exit = efi_exit, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3976 | .unload_image = efi_unload_image, |
| 3977 | .exit_boot_services = efi_exit_boot_services, |
| 3978 | .get_next_monotonic_count = efi_get_next_monotonic_count, |
| 3979 | .stall = efi_stall, |
| 3980 | .set_watchdog_timer = efi_set_watchdog_timer, |
| 3981 | .connect_controller = efi_connect_controller, |
| 3982 | .disconnect_controller = efi_disconnect_controller, |
| 3983 | .open_protocol = efi_open_protocol, |
Heinrich Schuchardt | 5e39433 | 2022-10-07 15:18:15 +0200 | [diff] [blame] | 3984 | .close_protocol = efi_close_protocol_ext, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3985 | .open_protocol_information = efi_open_protocol_information, |
| 3986 | .protocols_per_handle = efi_protocols_per_handle, |
| 3987 | .locate_handle_buffer = efi_locate_handle_buffer, |
| 3988 | .locate_protocol = efi_locate_protocol, |
Heinrich Schuchardt | 9106459 | 2018-02-18 15:17:49 +0100 | [diff] [blame] | 3989 | .install_multiple_protocol_interfaces = |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 3990 | efi_install_multiple_protocol_interfaces_ext, |
Heinrich Schuchardt | 9106459 | 2018-02-18 15:17:49 +0100 | [diff] [blame] | 3991 | .uninstall_multiple_protocol_interfaces = |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 3992 | efi_uninstall_multiple_protocol_interfaces_ext, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3993 | .calculate_crc32 = efi_calculate_crc32, |
| 3994 | .copy_mem = efi_copy_mem, |
| 3995 | .set_mem = efi_set_mem, |
Heinrich Schuchardt | 717c458 | 2018-02-04 23:05:13 +0100 | [diff] [blame] | 3996 | .create_event_ex = efi_create_event_ex, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3997 | }; |
| 3998 | |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 3999 | static u16 __efi_runtime_data firmware_vendor[] = u"Das U-Boot"; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 4000 | |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 4001 | struct efi_system_table __efi_runtime_data systab = { |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 4002 | .hdr = { |
| 4003 | .signature = EFI_SYSTEM_TABLE_SIGNATURE, |
Heinrich Schuchardt | e75b3cb | 2018-06-28 12:45:27 +0200 | [diff] [blame] | 4004 | .revision = EFI_SPECIFICATION_VERSION, |
Heinrich Schuchardt | 1020425 | 2018-06-28 12:45:29 +0200 | [diff] [blame] | 4005 | .headersize = sizeof(struct efi_system_table), |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 4006 | }, |
Heinrich Schuchardt | 27685f7 | 2018-06-28 12:45:30 +0200 | [diff] [blame] | 4007 | .fw_vendor = firmware_vendor, |
| 4008 | .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8, |
Heinrich Schuchardt | 372b893 | 2019-06-15 14:51:06 +0200 | [diff] [blame] | 4009 | .runtime = &efi_runtime_services, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 4010 | .nr_tables = 0, |
Heinrich Schuchardt | 2f528c2 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 4011 | .tables = NULL, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 4012 | }; |
Heinrich Schuchardt | 15070db | 2018-06-28 12:45:31 +0200 | [diff] [blame] | 4013 | |
| 4014 | /** |
| 4015 | * efi_initialize_system_table() - Initialize system table |
| 4016 | * |
Heinrich Schuchardt | 7b4b2a2 | 2018-09-03 05:00:43 +0200 | [diff] [blame] | 4017 | * Return: status code |
Heinrich Schuchardt | 15070db | 2018-06-28 12:45:31 +0200 | [diff] [blame] | 4018 | */ |
| 4019 | efi_status_t efi_initialize_system_table(void) |
| 4020 | { |
Heinrich Schuchardt | 2f528c2 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 4021 | efi_status_t ret; |
| 4022 | |
| 4023 | /* Allocate configuration table array */ |
| 4024 | ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA, |
| 4025 | EFI_MAX_CONFIGURATION_TABLES * |
| 4026 | sizeof(struct efi_configuration_table), |
| 4027 | (void **)&systab.tables); |
| 4028 | |
Heinrich Schuchardt | 98f8936 | 2019-06-29 02:00:16 +0200 | [diff] [blame] | 4029 | /* |
| 4030 | * These entries will be set to NULL in ExitBootServices(). To avoid |
| 4031 | * relocation in SetVirtualAddressMap(), set them dynamically. |
| 4032 | */ |
Heinrich Schuchardt | ed64768 | 2023-01-04 05:56:09 +0100 | [diff] [blame] | 4033 | systab.con_in_handle = efi_root; |
Heinrich Schuchardt | 98f8936 | 2019-06-29 02:00:16 +0200 | [diff] [blame] | 4034 | systab.con_in = &efi_con_in; |
Heinrich Schuchardt | ed64768 | 2023-01-04 05:56:09 +0100 | [diff] [blame] | 4035 | systab.con_out_handle = efi_root; |
Heinrich Schuchardt | 98f8936 | 2019-06-29 02:00:16 +0200 | [diff] [blame] | 4036 | systab.con_out = &efi_con_out; |
Heinrich Schuchardt | ed64768 | 2023-01-04 05:56:09 +0100 | [diff] [blame] | 4037 | systab.stderr_handle = efi_root; |
Heinrich Schuchardt | 98f8936 | 2019-06-29 02:00:16 +0200 | [diff] [blame] | 4038 | systab.std_err = &efi_con_out; |
| 4039 | systab.boottime = &efi_boot_services; |
| 4040 | |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 4041 | /* Set CRC32 field in table headers */ |
Heinrich Schuchardt | 15070db | 2018-06-28 12:45:31 +0200 | [diff] [blame] | 4042 | efi_update_table_header_crc32(&systab.hdr); |
| 4043 | efi_update_table_header_crc32(&efi_runtime_services.hdr); |
| 4044 | efi_update_table_header_crc32(&efi_boot_services.hdr); |
Heinrich Schuchardt | 2f528c2 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 4045 | |
| 4046 | return ret; |
Heinrich Schuchardt | 15070db | 2018-06-28 12:45:31 +0200 | [diff] [blame] | 4047 | } |