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