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