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