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