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