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