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