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