Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2 | /* |
Heinrich Schuchardt | da8f900 | 2019-07-03 20:27:24 +0200 | [diff] [blame] | 3 | * EFI application boot time services |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 4 | * |
Heinrich Schuchardt | da8f900 | 2019-07-03 20:27:24 +0200 | [diff] [blame] | 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 8 | #include <common.h> |
Ilias Apalodimas | 4cc0632 | 2020-10-22 01:04:20 +0300 | [diff] [blame] | 9 | #include <bootm.h> |
Heinrich Schuchardt | 368ca64 | 2017-10-05 16:14:14 +0200 | [diff] [blame] | 10 | #include <div64.h> |
Ilias Apalodimas | c539fb8 | 2020-10-22 01:04:21 +0300 | [diff] [blame] | 11 | #include <dm/device.h> |
| 12 | #include <dm/root.h> |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 13 | #include <efi_loader.h> |
Simon Glass | 8f3f761 | 2019-11-14 12:57:42 -0700 | [diff] [blame] | 14 | #include <irq_func.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 16 | #include <malloc.h> |
Heinrich Schuchardt | 3758752 | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 17 | #include <pe.h> |
Ilias Apalodimas | 4cc0632 | 2020-10-22 01:04:20 +0300 | [diff] [blame] | 18 | #include <time.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 19 | #include <u-boot/crc.h> |
Ilias Apalodimas | c539fb8 | 2020-10-22 01:04:21 +0300 | [diff] [blame] | 20 | #include <usb.h> |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 21 | #include <watchdog.h> |
Ilias Apalodimas | 4cc0632 | 2020-10-22 01:04:20 +0300 | [diff] [blame] | 22 | #include <linux/libfdt_env.h> |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 23 | |
| 24 | DECLARE_GLOBAL_DATA_PTR; |
| 25 | |
Heinrich Schuchardt | 0c150ca | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 26 | /* Task priority level */ |
Heinrich Schuchardt | f8d4ec3 | 2017-11-06 21:17:47 +0100 | [diff] [blame] | 27 | static efi_uintn_t efi_tpl = TPL_APPLICATION; |
Heinrich Schuchardt | 0c150ca | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 28 | |
Alexander Graf | c15d921 | 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 | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 32 | /* List of all events */ |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 33 | __efi_runtime_data LIST_HEAD(efi_events); |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 34 | |
Heinrich Schuchardt | 35c0cf6 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 35 | /* List of queued events */ |
| 36 | LIST_HEAD(efi_event_queue); |
| 37 | |
Heinrich Schuchardt | 7b4b8d86 | 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 | 628dbd7 | 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 | 6eec42d | 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 | 8efe079 | 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 | f277d94 | 2020-09-10 12:22:54 +0200 | [diff] [blame] | 50 | #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 51 | /* |
Heinrich Schuchardt | f277d94 | 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 | c15d921 | 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 | 1a3732c | 2020-05-27 01:58:30 +0200 | [diff] [blame] | 57 | static volatile gd_t *efi_gd, *app_gd; |
Simon Glass | cdfe696 | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 58 | #endif |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 59 | |
Heinrich Schuchardt | 7250623 | 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 | e7896c3 | 2017-07-27 08:04:19 -0400 | [diff] [blame] | 62 | static int nesting_level; |
Heinrich Schuchardt | 44ab21b | 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 | 760255f | 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 | 86789d5 | 2017-07-27 08:04:18 -0400 | [diff] [blame] | 68 | |
Heinrich Schuchardt | 672d99e | 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 | b7cb8b4 | 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 | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 89 | |
Rob Clark | 86789d5 | 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 | f277d94 | 2020-09-10 12:22:54 +0200 | [diff] [blame] | 94 | #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) |
Rob Clark | 86789d5 | 2017-07-27 08:04:18 -0400 | [diff] [blame] | 95 | assert(efi_gd); |
| 96 | app_gd = gd; |
Heinrich Schuchardt | 1a3732c | 2020-05-27 01:58:30 +0200 | [diff] [blame] | 97 | set_gd(efi_gd); |
Rob Clark | 86789d5 | 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 | f277d94 | 2020-09-10 12:22:54 +0200 | [diff] [blame] | 106 | #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) |
Heinrich Schuchardt | 1a3732c | 2020-05-27 01:58:30 +0200 | [diff] [blame] | 107 | set_gd(app_gd); |
Rob Clark | 86789d5 | 2017-07-27 08:04:18 -0400 | [diff] [blame] | 108 | #endif |
| 109 | return ret; |
| 110 | } |
| 111 | |
Heinrich Schuchardt | c594941 | 2020-07-18 09:53:01 +0200 | [diff] [blame] | 112 | /** |
| 113 | * efi_save_gd() - save global data register |
| 114 | * |
Heinrich Schuchardt | f277d94 | 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 | c594941 | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 121 | void efi_save_gd(void) |
| 122 | { |
Heinrich Schuchardt | f277d94 | 2020-09-10 12:22:54 +0200 | [diff] [blame] | 123 | #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 124 | efi_gd = gd; |
Simon Glass | cdfe696 | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 125 | #endif |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Heinrich Schuchardt | c594941 | 2020-07-18 09:53:01 +0200 | [diff] [blame] | 128 | /** |
| 129 | * efi_restore_gd() - restore global data register |
| 130 | * |
Heinrich Schuchardt | f277d94 | 2020-09-10 12:22:54 +0200 | [diff] [blame] | 131 | * On the ARM and RISC-V architectures gd is mapped to a fixed register. |
Heinrich Schuchardt | c594941 | 2020-07-18 09:53:01 +0200 | [diff] [blame] | 132 | * Restore it after returning from the UEFI world to the value saved via |
| 133 | * efi_save_gd(). |
Rob Clark | 86789d5 | 2017-07-27 08:04:18 -0400 | [diff] [blame] | 134 | */ |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 135 | void efi_restore_gd(void) |
| 136 | { |
Heinrich Schuchardt | f277d94 | 2020-09-10 12:22:54 +0200 | [diff] [blame] | 137 | #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) |
Alexander Graf | c15d921 | 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 | 1a3732c | 2020-05-27 01:58:30 +0200 | [diff] [blame] | 141 | set_gd(efi_gd); |
Simon Glass | cdfe696 | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 142 | #endif |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 145 | /** |
Mario Six | 8fac291 | 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 | 091cf43 | 2018-01-24 19:21:36 +0100 | [diff] [blame] | 148 | * |
Heinrich Schuchardt | 5999917 | 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 | 8fac291 | 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 | e7896c3 | 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 | 9106459 | 2018-02-18 15:17:49 +0100 | [diff] [blame] | 159 | |
Rob Clark | e7896c3 | 2017-07-27 08:04:19 -0400 | [diff] [blame] | 160 | level = min(max, level * 2); |
| 161 | return &indent[max - level]; |
| 162 | } |
| 163 | |
Heinrich Schuchardt | 4d66489 | 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 | e7896c3 | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 179 | /** |
Heinrich Schuchardt | 35c0cf6 | 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 | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 218 | * efi_queue_event() - queue an EFI event |
| 219 | * @event: event to signal |
Heinrich Schuchardt | a40db6e | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 224 | */ |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 225 | static void efi_queue_event(struct efi_event *event) |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 226 | { |
Heinrich Schuchardt | ec94690 | 2020-03-06 21:56:10 +0100 | [diff] [blame] | 227 | struct efi_event *item; |
Heinrich Schuchardt | 35c0cf6 | 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 | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 247 | } |
Heinrich Schuchardt | 35c0cf6 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 248 | efi_process_event_queue(); |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 251 | /** |
Heinrich Schuchardt | 7f0f005 | 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 | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 255 | * Return: status code |
Heinrich Schuchardt | 7f0f005 | 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 | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 271 | * efi_signal_event() - signal an EFI event |
| 272 | * @event: event to signal |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 273 | * |
Mario Six | 8fac291 | 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 | bf7f169 | 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 | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 279 | */ |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 280 | void efi_signal_event(struct efi_event *event) |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 281 | { |
Heinrich Schuchardt | 0b4de56 | 2019-06-06 01:51:50 +0200 | [diff] [blame] | 282 | if (event->is_signaled) |
| 283 | return; |
Heinrich Schuchardt | bf7f169 | 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 | bf7f169 | 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 | 35c0cf6 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 301 | efi_queue_event(evt); |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 302 | } |
Heinrich Schuchardt | 66ddc2e | 2019-05-05 00:07:34 +0200 | [diff] [blame] | 303 | } else { |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 304 | event->is_signaled = true; |
Heinrich Schuchardt | 35c0cf6 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 305 | efi_queue_event(event); |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 309 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 312 | * |
| 313 | * This function implements the RaiseTpl service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 314 | * |
| 315 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 316 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 317 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 318 | * Return: old value of the task priority level |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 319 | */ |
Heinrich Schuchardt | f8d4ec3 | 2017-11-06 21:17:47 +0100 | [diff] [blame] | 320 | static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 321 | { |
Heinrich Schuchardt | f8d4ec3 | 2017-11-06 21:17:47 +0100 | [diff] [blame] | 322 | efi_uintn_t old_tpl = efi_tpl; |
Heinrich Schuchardt | 0c150ca | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 323 | |
xypron.glpk@gmx.de | 48df209 | 2017-07-18 20:17:19 +0200 | [diff] [blame] | 324 | EFI_ENTRY("0x%zx", new_tpl); |
Heinrich Schuchardt | 0c150ca | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 325 | |
| 326 | if (new_tpl < efi_tpl) |
Heinrich Schuchardt | 952e206 | 2019-05-05 11:56:23 +0200 | [diff] [blame] | 327 | EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__); |
Heinrich Schuchardt | 0c150ca | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 334 | } |
| 335 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 336 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 339 | * |
| 340 | * This function implements the RestoreTpl service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 341 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 342 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 343 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 344 | */ |
Heinrich Schuchardt | f8d4ec3 | 2017-11-06 21:17:47 +0100 | [diff] [blame] | 345 | static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 346 | { |
xypron.glpk@gmx.de | 48df209 | 2017-07-18 20:17:19 +0200 | [diff] [blame] | 347 | EFI_ENTRY("0x%zx", old_tpl); |
Heinrich Schuchardt | 0c150ca | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 348 | |
| 349 | if (old_tpl > efi_tpl) |
Heinrich Schuchardt | 952e206 | 2019-05-05 11:56:23 +0200 | [diff] [blame] | 350 | EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__); |
Heinrich Schuchardt | 0c150ca | 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 | 59b2095 | 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 | 0c150ca | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 360 | EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 361 | } |
| 362 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 363 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 369 | * |
| 370 | * This function implements the AllocatePages service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 371 | * |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 376 | */ |
Masahiro Yamada | b2a05c1 | 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 | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 378 | efi_uintn_t pages, |
Masahiro Yamada | b2a05c1 | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 379 | uint64_t *memory) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 380 | { |
| 381 | efi_status_t r; |
| 382 | |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 383 | EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory); |
Alexander Graf | c15d921 | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 388 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 392 | * |
| 393 | * This function implements the FreePages service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 394 | * |
| 395 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 396 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 397 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 398 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 399 | */ |
Masahiro Yamada | b2a05c1 | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 400 | static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 401 | efi_uintn_t pages) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 402 | { |
| 403 | efi_status_t r; |
| 404 | |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 405 | EFI_ENTRY("%llx, 0x%zx", memory, pages); |
Alexander Graf | c15d921 | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 410 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 418 | * |
| 419 | * This function implements the GetMemoryMap service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 420 | * |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 425 | */ |
Masahiro Yamada | b2a05c1 | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 426 | static efi_status_t EFIAPI efi_get_memory_map_ext( |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 427 | efi_uintn_t *memory_map_size, |
Masahiro Yamada | b2a05c1 | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 428 | struct efi_mem_desc *memory_map, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 429 | efi_uintn_t *map_key, |
| 430 | efi_uintn_t *descriptor_size, |
Masahiro Yamada | b2a05c1 | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 431 | uint32_t *descriptor_version) |
Alexander Graf | c15d921 | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 442 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 447 | * |
| 448 | * This function implements the AllocatePool service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 449 | * |
| 450 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 451 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 452 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 453 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 454 | */ |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 455 | static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 456 | efi_uintn_t size, |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 457 | void **buffer) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 458 | { |
Alexander Graf | 1c34fa8 | 2016-03-24 01:37:37 +0100 | [diff] [blame] | 459 | efi_status_t r; |
| 460 | |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 461 | EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer); |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 462 | r = efi_allocate_pool(pool_type, size, buffer); |
Alexander Graf | 1c34fa8 | 2016-03-24 01:37:37 +0100 | [diff] [blame] | 463 | return EFI_EXIT(r); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 464 | } |
| 465 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 466 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 469 | * |
| 470 | * This function implements the FreePool service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 471 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 472 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 473 | * details. |
| 474 | * |
| 475 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 476 | */ |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 477 | static efi_status_t EFIAPI efi_free_pool_ext(void *buffer) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 478 | { |
Alexander Graf | 1c34fa8 | 2016-03-24 01:37:37 +0100 | [diff] [blame] | 479 | efi_status_t r; |
| 480 | |
| 481 | EFI_ENTRY("%p", buffer); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 482 | r = efi_free_pool(buffer); |
Alexander Graf | 1c34fa8 | 2016-03-24 01:37:37 +0100 | [diff] [blame] | 483 | return EFI_EXIT(r); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 484 | } |
| 485 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 486 | /** |
Heinrich Schuchardt | 30cd046 | 2019-05-01 09:42:39 +0200 | [diff] [blame] | 487 | * efi_add_handle() - add a new handle to the object list |
| 488 | * |
| 489 | * @handle: handle to be added |
Heinrich Schuchardt | 967d7de | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 490 | * |
Heinrich Schuchardt | 30cd046 | 2019-05-01 09:42:39 +0200 | [diff] [blame] | 491 | * The protocols list is initialized. The handle is added to the list of known |
| 492 | * UEFI objects. |
Heinrich Schuchardt | 967d7de | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 493 | */ |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 494 | void efi_add_handle(efi_handle_t handle) |
Heinrich Schuchardt | 967d7de | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 495 | { |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 496 | if (!handle) |
Heinrich Schuchardt | 967d7de | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 497 | return; |
Heinrich Schuchardt | adb50d0 | 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 | 967d7de | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 500 | } |
| 501 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 502 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 503 | * efi_create_handle() - create handle |
| 504 | * @handle: new handle |
Heinrich Schuchardt | eb6106e | 2017-10-26 19:25:49 +0200 | [diff] [blame] | 505 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 506 | * Return: status code |
Heinrich Schuchardt | eb6106e | 2017-10-26 19:25:49 +0200 | [diff] [blame] | 507 | */ |
Heinrich Schuchardt | b7cb8b4 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 508 | efi_status_t efi_create_handle(efi_handle_t *handle) |
Heinrich Schuchardt | cd522cb | 2017-08-27 00:51:09 +0200 | [diff] [blame] | 509 | { |
| 510 | struct efi_object *obj; |
Heinrich Schuchardt | cd522cb | 2017-08-27 00:51:09 +0200 | [diff] [blame] | 511 | |
Heinrich Schuchardt | ae1d206 | 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 | 967d7de | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 516 | efi_add_handle(obj); |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 517 | *handle = obj; |
Heinrich Schuchardt | ae1d206 | 2018-05-27 16:47:21 +0200 | [diff] [blame] | 518 | |
| 519 | return EFI_SUCCESS; |
Heinrich Schuchardt | cd522cb | 2017-08-27 00:51:09 +0200 | [diff] [blame] | 520 | } |
| 521 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 522 | /** |
Mario Six | 8fac291 | 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 | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 527 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 528 | * Return: status code |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 529 | */ |
Heinrich Schuchardt | b7cb8b4 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 530 | efi_status_t efi_search_protocol(const efi_handle_t handle, |
Heinrich Schuchardt | 7d13546 | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 555 | /** |
Mario Six | 8fac291 | 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 | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 560 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 561 | * Return: status code |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 562 | */ |
Heinrich Schuchardt | b7cb8b4 | 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 | 7d13546 | 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 | b27594d | 2018-05-11 12:09:21 +0200 | [diff] [blame] | 573 | if (handler->protocol_interface != protocol_interface) |
Heinrich Schuchardt | fdeb1f0 | 2019-05-10 20:06:48 +0200 | [diff] [blame] | 574 | return EFI_NOT_FOUND; |
Heinrich Schuchardt | 7d13546 | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 580 | /** |
Mario Six | 8fac291 | 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 | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 583 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 584 | * Return: status code |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 585 | */ |
Heinrich Schuchardt | b7cb8b4 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 586 | efi_status_t efi_remove_all_protocols(const efi_handle_t handle) |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 587 | { |
| 588 | struct efi_object *efiobj; |
Heinrich Schuchardt | a84731d | 2018-01-11 08:15:55 +0100 | [diff] [blame] | 589 | struct efi_handler *protocol; |
| 590 | struct efi_handler *pos; |
Heinrich Schuchardt | 7d13546 | 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 | a84731d | 2018-01-11 08:15:55 +0100 | [diff] [blame] | 595 | list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) { |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 596 | efi_status_t ret; |
| 597 | |
Heinrich Schuchardt | 7d13546 | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 606 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 607 | * efi_delete_handle() - delete handle |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 608 | * |
Heinrich Schuchardt | 41cc3c1 | 2019-07-14 11:05:34 +0200 | [diff] [blame] | 609 | * @handle: handle to delete |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 610 | */ |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 611 | void efi_delete_handle(efi_handle_t handle) |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 612 | { |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 613 | if (!handle) |
Heinrich Schuchardt | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 614 | return; |
Heinrich Schuchardt | adb50d0 | 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 | 7d13546 | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 618 | } |
| 619 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 620 | /** |
Mario Six | 8fac291 | 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 | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 623 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 624 | * Return: status code |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 625 | */ |
Heinrich Schuchardt | 370c7a8 | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 638 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 639 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 640 | * efi_create_event() - create an event |
Heinrich Schuchardt | 41cc3c1 | 2019-07-14 11:05:34 +0200 | [diff] [blame] | 641 | * |
Mario Six | 8fac291 | 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 | a40db6e | 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 | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 654 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 655 | */ |
Heinrich Schuchardt | f8d4ec3 | 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 | 852a0e177 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 657 | void (EFIAPI *notify_function) ( |
xypron.glpk@gmx.de | cdbf3ac | 2017-07-18 20:17:17 +0200 | [diff] [blame] | 658 | struct efi_event *event, |
| 659 | void *context), |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 660 | void *notify_context, efi_guid_t *group, |
| 661 | struct efi_event **event) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 662 | { |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 663 | struct efi_event *evt; |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 664 | efi_status_t ret; |
| 665 | int pool_type; |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 666 | |
Jonathan Gray | 7758b21 | 2017-03-12 19:26:07 +1100 | [diff] [blame] | 667 | if (event == NULL) |
xypron.glpk@gmx.de | 852a0e177 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 668 | return EFI_INVALID_PARAMETER; |
Jonathan Gray | 7758b21 | 2017-03-12 19:26:07 +1100 | [diff] [blame] | 669 | |
Heinrich Schuchardt | 7f0f005 | 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 | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 678 | pool_type = EFI_BOOT_SERVICES_DATA; |
| 679 | break; |
Heinrich Schuchardt | 7f0f005 | 2018-07-02 12:53:52 +0200 | [diff] [blame] | 680 | case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE: |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 681 | pool_type = EFI_RUNTIME_SERVICES_DATA; |
Heinrich Schuchardt | 7f0f005 | 2018-07-02 12:53:52 +0200 | [diff] [blame] | 682 | break; |
| 683 | default: |
xypron.glpk@gmx.de | 852a0e177 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 684 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | 7f0f005 | 2018-07-02 12:53:52 +0200 | [diff] [blame] | 685 | } |
Jonathan Gray | 7758b21 | 2017-03-12 19:26:07 +1100 | [diff] [blame] | 686 | |
AKASHI Takahiro | 3f3835d | 2018-08-10 15:36:32 +0900 | [diff] [blame] | 687 | if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) && |
Heinrich Schuchardt | ad7a215 | 2019-04-25 07:30:41 +0200 | [diff] [blame] | 688 | (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS)) |
xypron.glpk@gmx.de | 852a0e177 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 689 | return EFI_INVALID_PARAMETER; |
Jonathan Gray | 7758b21 | 2017-03-12 19:26:07 +1100 | [diff] [blame] | 690 | |
Heinrich Schuchardt | 4429d87 | 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 | 370c7a8 | 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 | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 700 | evt->group = group; |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 701 | /* Disable timers on boot up */ |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 702 | evt->trigger_next = -1ULL; |
Heinrich Schuchardt | 370c7a8 | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 706 | } |
| 707 | |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 708 | /* |
Mario Six | 8fac291 | 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 | 717c458 | 2018-02-04 23:05:13 +0100 | [diff] [blame] | 716 | * |
| 717 | * This function implements the CreateEventEx service. |
Heinrich Schuchardt | 717c458 | 2018-02-04 23:05:13 +0100 | [diff] [blame] | 718 | * |
Mario Six | 8fac291 | 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 | 717c458 | 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 | ce66499 | 2019-05-04 10:12:50 +0200 | [diff] [blame] | 732 | efi_status_t ret; |
| 733 | |
Heinrich Schuchardt | 717c458 | 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 | ce66499 | 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 | 717c458 | 2018-02-04 23:05:13 +0100 | [diff] [blame] | 752 | } |
| 753 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 754 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 761 | * |
| 762 | * This function implements the CreateEvent service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 763 | * |
| 764 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 765 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 766 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 767 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 768 | */ |
xypron.glpk@gmx.de | 852a0e177 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 769 | static efi_status_t EFIAPI efi_create_event_ext( |
Heinrich Schuchardt | f8d4ec3 | 2017-11-06 21:17:47 +0100 | [diff] [blame] | 770 | uint32_t type, efi_uintn_t notify_tpl, |
xypron.glpk@gmx.de | 852a0e177 | 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 | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 779 | notify_context, NULL, event)); |
xypron.glpk@gmx.de | 852a0e177 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 780 | } |
| 781 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 782 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 783 | * efi_timer_check() - check if a timer event has occurred |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 784 | * |
Heinrich Schuchardt | a40db6e | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 788 | * Our timers have to work without interrupts, so we check whenever keyboard |
Heinrich Schuchardt | a40db6e | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 790 | */ |
| 791 | void efi_timer_check(void) |
| 792 | { |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 793 | struct efi_event *evt; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 794 | u64 now = timer_get_us(); |
| 795 | |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 796 | list_for_each_entry(evt, &efi_events, link) { |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 797 | if (!timers_enabled) |
| 798 | continue; |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 799 | if (!(evt->type & EVT_TIMER) || now < evt->trigger_next) |
Heinrich Schuchardt | 8b11a8a | 2017-09-15 10:06:13 +0200 | [diff] [blame] | 800 | continue; |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 801 | switch (evt->trigger_type) { |
Heinrich Schuchardt | 8b11a8a | 2017-09-15 10:06:13 +0200 | [diff] [blame] | 802 | case EFI_TIMER_RELATIVE: |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 803 | evt->trigger_type = EFI_TIMER_STOP; |
Heinrich Schuchardt | 8b11a8a | 2017-09-15 10:06:13 +0200 | [diff] [blame] | 804 | break; |
| 805 | case EFI_TIMER_PERIODIC: |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 806 | evt->trigger_next += evt->trigger_time; |
Heinrich Schuchardt | 8b11a8a | 2017-09-15 10:06:13 +0200 | [diff] [blame] | 807 | break; |
| 808 | default: |
| 809 | continue; |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 810 | } |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 811 | evt->is_signaled = false; |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 812 | efi_signal_event(evt); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 813 | } |
Heinrich Schuchardt | 35c0cf6 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 814 | efi_process_event_queue(); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 815 | WATCHDOG_RESET(); |
| 816 | } |
| 817 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 818 | /** |
Mario Six | 8fac291 | 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 | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 822 | * @trigger_time: trigger period in multiples of 100 ns |
Heinrich Schuchardt | a40db6e | 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 | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 827 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 828 | */ |
xypron.glpk@gmx.de | 3ecc6bd | 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 | a587fd1 | 2017-07-18 20:17:21 +0200 | [diff] [blame] | 830 | uint64_t trigger_time) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 831 | { |
Heinrich Schuchardt | 370c7a8 | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 835 | |
xypron.glpk@gmx.de | 44c4be0 | 2017-07-18 20:17:23 +0200 | [diff] [blame] | 836 | /* |
Heinrich Schuchardt | cf70a73 | 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 | 44c4be0 | 2017-07-18 20:17:23 +0200 | [diff] [blame] | 839 | */ |
Heinrich Schuchardt | 368ca64 | 2017-10-05 16:14:14 +0200 | [diff] [blame] | 840 | do_div(trigger_time, 10); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 841 | |
Heinrich Schuchardt | 370c7a8 | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 852 | } |
Heinrich Schuchardt | 370c7a8 | 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; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 857 | } |
| 858 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 859 | /** |
Mario Six | 8fac291 | 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 | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 864 | * @trigger_time: trigger period in multiples of 100 ns |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 865 | * |
| 866 | * This function implements the SetTimer service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 867 | * |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 873 | */ |
xypron.glpk@gmx.de | 3ecc6bd | 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 | a587fd1 | 2017-07-18 20:17:21 +0200 | [diff] [blame] | 877 | { |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 878 | EFI_ENTRY("%p, %d, %llx", event, type, trigger_time); |
xypron.glpk@gmx.de | a587fd1 | 2017-07-18 20:17:21 +0200 | [diff] [blame] | 879 | return EFI_EXIT(efi_set_timer(event, type, trigger_time)); |
| 880 | } |
| 881 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 882 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 887 | * |
| 888 | * This function implements the WaitForEvent service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 889 | * |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 894 | */ |
Heinrich Schuchardt | 798a441 | 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 | cdbf3ac | 2017-07-18 20:17:17 +0200 | [diff] [blame] | 896 | struct efi_event **event, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 897 | efi_uintn_t *index) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 898 | { |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 899 | int i; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 900 | |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 901 | EFI_ENTRY("%zd, %p, %p", num_events, event, index); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 902 | |
xypron.glpk@gmx.de | 3070823 | 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 | 0c150ca | 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 | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 909 | for (i = 0; i < num_events; ++i) { |
Heinrich Schuchardt | 370c7a8 | 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 | 3070823 | 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 | 1bbee39 | 2017-10-04 15:03:24 +0200 | [diff] [blame] | 914 | if (!event[i]->is_signaled) |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 915 | efi_queue_event(event[i]); |
xypron.glpk@gmx.de | 3070823 | 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 | 1bbee39 | 2017-10-04 15:03:24 +0200 | [diff] [blame] | 921 | if (event[i]->is_signaled) |
xypron.glpk@gmx.de | 3070823 | 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 | 1bbee39 | 2017-10-04 15:03:24 +0200 | [diff] [blame] | 933 | event[i]->is_signaled = false; |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 934 | if (index) |
| 935 | *index = i; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 936 | |
| 937 | return EFI_EXIT(EFI_SUCCESS); |
| 938 | } |
| 939 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 940 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 943 | * |
| 944 | * This function implements the SignalEvent service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 945 | * |
| 946 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 947 | * details. |
Heinrich Schuchardt | a40db6e | 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 | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 952 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 953 | */ |
xypron.glpk@gmx.de | 3070823 | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 955 | { |
| 956 | EFI_ENTRY("%p", event); |
Heinrich Schuchardt | 370c7a8 | 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 | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 959 | efi_signal_event(event); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 960 | return EFI_EXIT(EFI_SUCCESS); |
| 961 | } |
| 962 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 963 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 964 | * efi_close_event() - close an EFI event |
| 965 | * @event: event to close |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 966 | * |
| 967 | * This function implements the CloseEvent service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 968 | * |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 973 | */ |
xypron.glpk@gmx.de | cdbf3ac | 2017-07-18 20:17:17 +0200 | [diff] [blame] | 974 | static efi_status_t EFIAPI efi_close_event(struct efi_event *event) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 975 | { |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 976 | struct efi_register_notify_event *item, *next; |
| 977 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 978 | EFI_ENTRY("%p", event); |
Heinrich Schuchardt | 370c7a8 | 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 | 6eec42d | 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 | d375e75 | 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 | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 994 | list_del(&item->link); |
| 995 | free(item); |
| 996 | } |
| 997 | } |
Heinrich Schuchardt | 35c0cf6 | 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 | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1001 | |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 1002 | list_del(&event->link); |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 1003 | efi_free_pool(event); |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 1004 | return EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1005 | } |
| 1006 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1007 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1010 | * |
| 1011 | * This function implements the CheckEvent service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1012 | * |
| 1013 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1014 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1015 | * |
Mario Six | 8fac291 | 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. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1018 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1019 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1020 | */ |
xypron.glpk@gmx.de | cdbf3ac | 2017-07-18 20:17:17 +0200 | [diff] [blame] | 1021 | static efi_status_t EFIAPI efi_check_event(struct efi_event *event) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1022 | { |
| 1023 | EFI_ENTRY("%p", event); |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 1024 | efi_timer_check(); |
Heinrich Schuchardt | 370c7a8 | 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 | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 1029 | efi_queue_event(event); |
Heinrich Schuchardt | 370c7a8 | 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 | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 1033 | } |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 1034 | return EFI_EXIT(EFI_NOT_READY); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1035 | } |
| 1036 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1037 | /** |
Mario Six | 8fac291 | 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 | 37ebcba | 2017-10-18 18:13:03 +0200 | [diff] [blame] | 1040 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1041 | * Return: EFI object |
Heinrich Schuchardt | 37ebcba | 2017-10-18 18:13:03 +0200 | [diff] [blame] | 1042 | */ |
Heinrich Schuchardt | b7cb8b4 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 1043 | struct efi_object *efi_search_obj(const efi_handle_t handle) |
Heinrich Schuchardt | 37ebcba | 2017-10-18 18:13:03 +0200 | [diff] [blame] | 1044 | { |
Heinrich Schuchardt | 274cc87 | 2017-11-06 21:17:50 +0100 | [diff] [blame] | 1045 | struct efi_object *efiobj; |
Heinrich Schuchardt | 37ebcba | 2017-10-18 18:13:03 +0200 | [diff] [blame] | 1046 | |
Heinrich Schuchardt | 5d8231b | 2019-05-05 10:37:51 +0200 | [diff] [blame] | 1047 | if (!handle) |
| 1048 | return NULL; |
| 1049 | |
Heinrich Schuchardt | 274cc87 | 2017-11-06 21:17:50 +0100 | [diff] [blame] | 1050 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1051 | if (efiobj == handle) |
Heinrich Schuchardt | 37ebcba | 2017-10-18 18:13:03 +0200 | [diff] [blame] | 1052 | return efiobj; |
| 1053 | } |
Heinrich Schuchardt | 37ebcba | 2017-10-18 18:13:03 +0200 | [diff] [blame] | 1054 | return NULL; |
| 1055 | } |
| 1056 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1057 | /** |
Mario Six | 8fac291 | 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 | a277d3a | 2018-01-11 08:15:57 +0100 | [diff] [blame] | 1061 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1062 | * Return: open protocol info entry |
Heinrich Schuchardt | a277d3a | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1078 | /** |
Mario Six | 8fac291 | 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 | a277d3a | 2018-01-11 08:15:57 +0100 | [diff] [blame] | 1081 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1082 | * Return: status code |
Heinrich Schuchardt | a277d3a | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1092 | /** |
Mario Six | 8fac291 | 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 | 5aef61d | 2017-10-26 19:25:53 +0200 | [diff] [blame] | 1097 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1098 | * Return: status code |
Heinrich Schuchardt | 5aef61d | 2017-10-26 19:25:53 +0200 | [diff] [blame] | 1099 | */ |
Heinrich Schuchardt | b7cb8b4 | 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 | 5aef61d | 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 | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1107 | struct efi_register_notify_event *event; |
Heinrich Schuchardt | 5aef61d | 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 | 99ce206 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 1118 | handler->guid = protocol; |
| 1119 | handler->protocol_interface = protocol_interface; |
Heinrich Schuchardt | a277d3a | 2018-01-11 08:15:57 +0100 | [diff] [blame] | 1120 | INIT_LIST_HEAD(&handler->open_infos); |
Heinrich Schuchardt | 99ce206 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 1121 | list_add_tail(&handler->link, &efiobj->protocols); |
Heinrich Schuchardt | 6eec42d | 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 | d375e75 | 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 | ea0f6a8 | 2019-06-07 07:43:24 +0200 | [diff] [blame] | 1136 | event->event->is_signaled = false; |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 1137 | efi_signal_event(event->event); |
Heinrich Schuchardt | d375e75 | 2019-05-21 18:19:01 +0200 | [diff] [blame] | 1138 | } |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1139 | } |
| 1140 | |
Heinrich Schuchardt | 3d2abc3 | 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 | 99ce206 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 1143 | return EFI_SUCCESS; |
Heinrich Schuchardt | 5aef61d | 2017-10-26 19:25:53 +0200 | [diff] [blame] | 1144 | } |
| 1145 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1146 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1153 | * |
Heinrich Schuchardt | 0a27ac8 | 2017-11-06 21:17:44 +0100 | [diff] [blame] | 1154 | * This function implements the InstallProtocolInterface service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1155 | * |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1160 | */ |
Heinrich Schuchardt | 0a27ac8 | 2017-11-06 21:17:44 +0100 | [diff] [blame] | 1161 | static efi_status_t EFIAPI efi_install_protocol_interface( |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 1162 | efi_handle_t *handle, const efi_guid_t *protocol, |
Heinrich Schuchardt | 0a27ac8 | 2017-11-06 21:17:44 +0100 | [diff] [blame] | 1163 | int protocol_interface_type, void *protocol_interface) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1164 | { |
xypron.glpk@gmx.de | 0581fa8 | 2017-07-11 22:06:16 +0200 | [diff] [blame] | 1165 | efi_status_t r; |
| 1166 | |
Heinrich Schuchardt | 0a27ac8 | 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 | 0581fa8 | 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 | cd522cb | 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 | 952e206 | 2019-05-05 11:56:23 +0200 | [diff] [blame] | 1181 | EFI_PRINT("new handle %p\n", *handle); |
Heinrich Schuchardt | 50f0210 | 2017-10-26 19:25:43 +0200 | [diff] [blame] | 1182 | } else { |
Heinrich Schuchardt | 952e206 | 2019-05-05 11:56:23 +0200 | [diff] [blame] | 1183 | EFI_PRINT("handle %p\n", *handle); |
xypron.glpk@gmx.de | 0581fa8 | 2017-07-11 22:06:16 +0200 | [diff] [blame] | 1184 | } |
Heinrich Schuchardt | 865d5f3 | 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 | 0581fa8 | 2017-07-11 22:06:16 +0200 | [diff] [blame] | 1187 | out: |
Heinrich Schuchardt | 0a27ac8 | 2017-11-06 21:17:44 +0100 | [diff] [blame] | 1188 | return EFI_EXIT(r); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1189 | } |
xypron.glpk@gmx.de | 0581fa8 | 2017-07-11 22:06:16 +0200 | [diff] [blame] | 1190 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1191 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1192 | * efi_get_drivers() - get all drivers associated to a controller |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1193 | * @handle: handle of the controller |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 1194 | * @protocol: protocol GUID (optional) |
Mario Six | 8fac291 | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1197 | * |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1198 | * The allocated buffer has to be freed with free(). |
| 1199 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1200 | * Return: status code |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1201 | */ |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1202 | static efi_status_t efi_get_drivers(efi_handle_t handle, |
Heinrich Schuchardt | e994328 | 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 | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1213 | list_for_each_entry(handler, &handle->protocols, link) { |
Heinrich Schuchardt | e994328 | 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 | 7416aef | 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 | e994328 | 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 | e994328 | 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 | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1235 | list_for_each_entry(handler, &handle->protocols, link) { |
Heinrich Schuchardt | e994328 | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1260 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1261 | * efi_disconnect_all_drivers() - disconnect all drivers from a controller |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1262 | * @handle: handle of the controller |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 1263 | * @protocol: protocol GUID (optional) |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1264 | * @child_handle: handle of the child to destroy |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1265 | * |
| 1266 | * This function implements the DisconnectController service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1267 | * |
| 1268 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1269 | * details. |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1270 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1271 | * Return: status code |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1272 | */ |
Heinrich Schuchardt | adb50d0 | 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 | e994328 | 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 | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1282 | ret = efi_get_drivers(handle, protocol, &number_of_drivers, |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1283 | &driver_handle_buffer); |
| 1284 | if (ret != EFI_SUCCESS) |
| 1285 | return ret; |
Heinrich Schuchardt | 7416aef | 2019-06-02 01:43:33 +0200 | [diff] [blame] | 1286 | if (!number_of_drivers) |
| 1287 | return EFI_SUCCESS; |
Heinrich Schuchardt | e994328 | 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 | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1291 | handle, |
Heinrich Schuchardt | e994328 | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1301 | /** |
Heinrich Schuchardt | 1b45134 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 1302 | * efi_uninstall_protocol() - uninstall protocol interface |
| 1303 | * |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1307 | * |
Heinrich Schuchardt | 1b45134 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 1308 | * This function DOES NOT delete a handle without installed protocol. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1309 | * |
| 1310 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1311 | */ |
Heinrich Schuchardt | 1b45134 | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1315 | { |
Heinrich Schuchardt | 86637f3 | 2018-01-11 08:16:05 +0100 | [diff] [blame] | 1316 | struct efi_object *efiobj; |
Heinrich Schuchardt | 7538c27 | 2017-10-26 19:25:56 +0200 | [diff] [blame] | 1317 | struct efi_handler *handler; |
Heinrich Schuchardt | 86637f3 | 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 | 7538c27 | 2017-10-26 19:25:56 +0200 | [diff] [blame] | 1320 | efi_status_t r; |
xypron.glpk@gmx.de | 2cfad48 | 2017-07-11 22:06:17 +0200 | [diff] [blame] | 1321 | |
Heinrich Schuchardt | 86637f3 | 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 | 2cfad48 | 2017-07-11 22:06:17 +0200 | [diff] [blame] | 1325 | r = EFI_INVALID_PARAMETER; |
| 1326 | goto out; |
| 1327 | } |
Heinrich Schuchardt | 7538c27 | 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 | 86637f3 | 2018-01-11 08:16:05 +0100 | [diff] [blame] | 1332 | /* Disconnect controllers */ |
| 1333 | efi_disconnect_all_drivers(efiobj, protocol, NULL); |
Heinrich Schuchardt | 86637f3 | 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)) { |
Heinrich Schuchardt | 7538c27 | 2017-10-26 19:25:56 +0200 | [diff] [blame] | 1343 | r = EFI_ACCESS_DENIED; |
Heinrich Schuchardt | 86637f3 | 2018-01-11 08:16:05 +0100 | [diff] [blame] | 1344 | goto out; |
xypron.glpk@gmx.de | 2cfad48 | 2017-07-11 22:06:17 +0200 | [diff] [blame] | 1345 | } |
Heinrich Schuchardt | 86637f3 | 2018-01-11 08:16:05 +0100 | [diff] [blame] | 1346 | r = efi_remove_protocol(handle, protocol, protocol_interface); |
xypron.glpk@gmx.de | 2cfad48 | 2017-07-11 22:06:17 +0200 | [diff] [blame] | 1347 | out: |
Heinrich Schuchardt | 1b45134 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 1348 | return r; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1349 | } |
| 1350 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1351 | /** |
Heinrich Schuchardt | 1b45134 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 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); |
| 1383 | } |
| 1384 | |
| 1385 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 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 | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1396 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1397 | */ |
Heinrich Schuchardt | e547c66 | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1402 | { |
Heinrich Schuchardt | 6eec42d | 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 | 238f88c | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 1406 | EFI_ENTRY("%pUl, %p, %p", protocol, event, registration); |
Heinrich Schuchardt | 6eec42d | 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 | 196f66d | 2019-12-29 00:01:04 +0530 | [diff] [blame] | 1420 | guidcpy(&item->protocol, protocol); |
Heinrich Schuchardt | d375e75 | 2019-05-21 18:19:01 +0200 | [diff] [blame] | 1421 | INIT_LIST_HEAD(&item->handles); |
Heinrich Schuchardt | 6eec42d | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1428 | } |
| 1429 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1430 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1431 | * efi_search() - determine if an EFI handle implements a protocol |
Heinrich Schuchardt | 41cc3c1 | 2019-07-14 11:05:34 +0200 | [diff] [blame] | 1432 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1433 | * @search_type: selection criterion |
| 1434 | * @protocol: GUID of the protocol |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1435 | * @handle: handle |
Heinrich Schuchardt | a40db6e | 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 | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1439 | * Return: 0 if the handle implements the protocol |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1440 | */ |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1441 | static int efi_search(enum efi_locate_search_type search_type, |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1442 | const efi_guid_t *protocol, efi_handle_t handle) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1443 | { |
Heinrich Schuchardt | 6a43075 | 2017-10-26 19:25:55 +0200 | [diff] [blame] | 1444 | efi_status_t ret; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1445 | |
| 1446 | switch (search_type) { |
Heinrich Schuchardt | 68845f0 | 2017-11-06 21:17:42 +0100 | [diff] [blame] | 1447 | case ALL_HANDLES: |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1448 | return 0; |
Heinrich Schuchardt | 68845f0 | 2017-11-06 21:17:42 +0100 | [diff] [blame] | 1449 | case BY_PROTOCOL: |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1450 | ret = efi_search_protocol(handle, protocol, NULL); |
Heinrich Schuchardt | 6a43075 | 2017-10-26 19:25:55 +0200 | [diff] [blame] | 1451 | return (ret != EFI_SUCCESS); |
| 1452 | default: |
| 1453 | /* Invalid search type */ |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1454 | return -1; |
| 1455 | } |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1456 | } |
| 1457 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1458 | /** |
Heinrich Schuchardt | eb61652 | 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 | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1480 | * efi_locate_handle() - locate handles implementing a protocol |
Heinrich Schuchardt | 6eec42d | 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 | a40db6e | 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 | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1491 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1492 | */ |
xypron.glpk@gmx.de | cab4dd5 | 2017-08-09 20:55:00 +0200 | [diff] [blame] | 1493 | static efi_status_t efi_locate_handle( |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1494 | enum efi_locate_search_type search_type, |
Heinrich Schuchardt | e547c66 | 2017-10-05 16:35:53 +0200 | [diff] [blame] | 1495 | const efi_guid_t *protocol, void *search_key, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 1496 | efi_uintn_t *buffer_size, efi_handle_t *buffer) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1497 | { |
Heinrich Schuchardt | ec66fc8 | 2017-11-06 21:17:49 +0100 | [diff] [blame] | 1498 | struct efi_object *efiobj; |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 1499 | efi_uintn_t size = 0; |
Heinrich Schuchardt | eb61652 | 2019-05-29 07:46:33 +0200 | [diff] [blame] | 1500 | struct efi_register_notify_event *event; |
Heinrich Schuchardt | d375e75 | 2019-05-21 18:19:01 +0200 | [diff] [blame] | 1501 | struct efi_protocol_notification *handle = NULL; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1502 | |
Heinrich Schuchardt | ec66fc8 | 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 | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1510 | /* Check that the registration key is valid */ |
Heinrich Schuchardt | eb61652 | 2019-05-29 07:46:33 +0200 | [diff] [blame] | 1511 | event = efi_check_register_notify_event(search_key); |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1512 | if (!event) |
| 1513 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | 6eec42d | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1514 | break; |
Heinrich Schuchardt | ec66fc8 | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1523 | /* Count how much space we need */ |
Heinrich Schuchardt | d375e75 | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1539 | } |
| 1540 | |
Heinrich Schuchardt | c0ee5f3 | 2019-05-04 17:37:32 +0200 | [diff] [blame] | 1541 | if (!buffer_size) |
| 1542 | return EFI_INVALID_PARAMETER; |
| 1543 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1544 | if (*buffer_size < size) { |
| 1545 | *buffer_size = size; |
xypron.glpk@gmx.de | 69f9403 | 2017-07-11 22:06:21 +0200 | [diff] [blame] | 1546 | return EFI_BUFFER_TOO_SMALL; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1547 | } |
| 1548 | |
Rob Clark | cdee337 | 2017-08-06 14:10:07 -0400 | [diff] [blame] | 1549 | *buffer_size = size; |
Heinrich Schuchardt | c0ee5f3 | 2019-05-04 17:37:32 +0200 | [diff] [blame] | 1550 | |
Heinrich Schuchardt | c97f947 | 2019-05-10 19:03:49 +0200 | [diff] [blame] | 1551 | /* The buffer size is sufficient but there is no buffer */ |
Heinrich Schuchardt | c0ee5f3 | 2019-05-04 17:37:32 +0200 | [diff] [blame] | 1552 | if (!buffer) |
| 1553 | return EFI_INVALID_PARAMETER; |
Rob Clark | cdee337 | 2017-08-06 14:10:07 -0400 | [diff] [blame] | 1554 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1555 | /* Then fill the array */ |
Heinrich Schuchardt | d375e75 | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1564 | } |
| 1565 | |
xypron.glpk@gmx.de | 69f9403 | 2017-07-11 22:06:21 +0200 | [diff] [blame] | 1566 | return EFI_SUCCESS; |
| 1567 | } |
| 1568 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1569 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1576 | * |
| 1577 | * This function implements the LocateHandle service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1578 | * |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1583 | */ |
xypron.glpk@gmx.de | 69f9403 | 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 | e547c66 | 2017-10-05 16:35:53 +0200 | [diff] [blame] | 1586 | const efi_guid_t *protocol, void *search_key, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 1587 | efi_uintn_t *buffer_size, efi_handle_t *buffer) |
xypron.glpk@gmx.de | 69f9403 | 2017-07-11 22:06:21 +0200 | [diff] [blame] | 1588 | { |
Rob Clark | 238f88c | 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 | 69f9403 | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1594 | } |
| 1595 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1596 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1597 | * efi_remove_configuration_table() - collapses configuration table entries, |
| 1598 | * removing index i |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1599 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1600 | * @i: index of the table entry to be removed |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1601 | */ |
Alexander Graf | fe3366f | 2017-07-26 13:41:04 +0200 | [diff] [blame] | 1602 | static void efi_remove_configuration_table(int i) |
| 1603 | { |
Heinrich Schuchardt | 2f528c2 | 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 | fe3366f | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1612 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 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 | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1621 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1622 | */ |
Heinrich Schuchardt | 9106459 | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1625 | { |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1626 | struct efi_event *evt; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1627 | int i; |
| 1628 | |
Heinrich Schuchardt | 754ce10 | 2018-02-18 00:08:00 +0100 | [diff] [blame] | 1629 | if (!guid) |
| 1630 | return EFI_INVALID_PARAMETER; |
| 1631 | |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 1632 | /* Check for GUID override */ |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1633 | for (i = 0; i < systab.nr_tables; i++) { |
Heinrich Schuchardt | 2f528c2 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 1634 | if (!guidcmp(guid, &systab.tables[i].guid)) { |
Alexander Graf | fe3366f | 2017-07-26 13:41:04 +0200 | [diff] [blame] | 1635 | if (table) |
Heinrich Schuchardt | 2f528c2 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 1636 | systab.tables[i].table = table; |
Alexander Graf | fe3366f | 2017-07-26 13:41:04 +0200 | [diff] [blame] | 1637 | else |
| 1638 | efi_remove_configuration_table(i); |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1639 | goto out; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1640 | } |
| 1641 | } |
| 1642 | |
Alexander Graf | fe3366f | 2017-07-26 13:41:04 +0200 | [diff] [blame] | 1643 | if (!table) |
| 1644 | return EFI_NOT_FOUND; |
| 1645 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1646 | /* No override, check for overflow */ |
Heinrich Schuchardt | 2f528c2 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 1647 | if (i >= EFI_MAX_CONFIGURATION_TABLES) |
Alexander Graf | c5c1163 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 1648 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1649 | |
| 1650 | /* Add a new entry */ |
Sughosh Ganu | 196f66d | 2019-12-29 00:01:04 +0530 | [diff] [blame] | 1651 | guidcpy(&systab.tables[i].guid, guid); |
Heinrich Schuchardt | 2f528c2 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 1652 | systab.tables[i].table = table; |
Alexander Graf | 9982e67 | 2016-08-19 01:23:30 +0200 | [diff] [blame] | 1653 | systab.nr_tables = i + 1; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1654 | |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1655 | out: |
Heinrich Schuchardt | cf70a73 | 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 | ac2d4da | 2018-07-07 15:36:05 +0200 | [diff] [blame] | 1657 | efi_update_table_header_crc32(&systab.hdr); |
| 1658 | |
Heinrich Schuchardt | bf7f169 | 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 | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 1662 | efi_signal_event(evt); |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1663 | break; |
| 1664 | } |
| 1665 | } |
| 1666 | |
Alexander Graf | c5c1163 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 1667 | return EFI_SUCCESS; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1668 | } |
| 1669 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1670 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1675 | * |
| 1676 | * This function implements the InstallConfigurationTable service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1677 | * |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1682 | */ |
Alexander Graf | c5c1163 | 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 | 238f88c | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 1686 | EFI_ENTRY("%pUl, %p", guid, table); |
Alexander Graf | c5c1163 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 1687 | return EFI_EXIT(efi_install_configuration_table(guid, table)); |
| 1688 | } |
| 1689 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1690 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1691 | * efi_setup_loaded_image() - initialize a loaded image |
Heinrich Schuchardt | 5999917 | 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 | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 1694 | * protocols, boot-device, etc. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1695 | * |
Heinrich Schuchardt | 41cc3c1 | 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 | 6346a90 | 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 | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 1704 | */ |
Heinrich Schuchardt | 3c3a735 | 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 | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 1709 | { |
Heinrich Schuchardt | 24d3a66 | 2017-10-26 19:25:58 +0200 | [diff] [blame] | 1710 | efi_status_t ret; |
Heinrich Schuchardt | 6346a90 | 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 | bbe13da | 2019-03-27 13:40:32 +0900 | [diff] [blame] | 1713 | struct efi_device_path *dp; |
Heinrich Schuchardt | 6346a90 | 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 | 3c3a735 | 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 | b27ced4 | 2019-05-01 14:20:18 +0200 | [diff] [blame] | 1727 | obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE; |
Heinrich Schuchardt | 24d3a66 | 2017-10-26 19:25:58 +0200 | [diff] [blame] | 1728 | |
Heinrich Schuchardt | 967d7de | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 1729 | /* Add internal object to object list */ |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 1730 | efi_add_handle(&obj->header); |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 1731 | |
Heinrich Schuchardt | c47f870 | 2018-07-05 08:17:58 +0200 | [diff] [blame] | 1732 | info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION; |
Heinrich Schuchardt | 24d3a66 | 2017-10-26 19:25:58 +0200 | [diff] [blame] | 1733 | info->file_path = file_path; |
Heinrich Schuchardt | 8a7e09d | 2018-08-26 15:31:52 +0200 | [diff] [blame] | 1734 | info->system_table = &systab; |
Heinrich Schuchardt | 24d3a66 | 2017-10-26 19:25:58 +0200 | [diff] [blame] | 1735 | |
Heinrich Schuchardt | f3996be | 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 | bbe13da | 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 | f3996be | 2018-01-26 06:50:54 +0100 | [diff] [blame] | 1742 | goto failure; |
AKASHI Takahiro | bbe13da | 2019-03-27 13:40:32 +0900 | [diff] [blame] | 1743 | } |
| 1744 | } else { |
| 1745 | dp = NULL; |
Heinrich Schuchardt | f3996be | 2018-01-26 06:50:54 +0100 | [diff] [blame] | 1746 | } |
AKASHI Takahiro | bbe13da | 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; |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [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 | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 1756 | ret = efi_add_protocol(&obj->header, |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 1757 | &efi_guid_loaded_image, info); |
Heinrich Schuchardt | 24d3a66 | 2017-10-26 19:25:58 +0200 | [diff] [blame] | 1758 | if (ret != EFI_SUCCESS) |
| 1759 | goto failure; |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 1760 | |
Heinrich Schuchardt | d20f512 | 2019-03-19 18:58:58 +0100 | [diff] [blame] | 1761 | *info_ptr = info; |
| 1762 | *handle_ptr = obj; |
Heinrich Schuchardt | 6346a90 | 2019-02-06 19:41:29 +0100 | [diff] [blame] | 1763 | |
Heinrich Schuchardt | 7db9f89 | 2017-12-04 18:03:01 +0100 | [diff] [blame] | 1764 | return ret; |
Heinrich Schuchardt | 24d3a66 | 2017-10-26 19:25:58 +0200 | [diff] [blame] | 1765 | failure: |
| 1766 | printf("ERROR: Failure to install protocols for loaded image\n"); |
Heinrich Schuchardt | 6346a90 | 2019-02-06 19:41:29 +0100 | [diff] [blame] | 1767 | efi_delete_handle(&obj->header); |
| 1768 | free(info); |
Heinrich Schuchardt | 7db9f89 | 2017-12-04 18:03:01 +0100 | [diff] [blame] | 1769 | return ret; |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 1770 | } |
| 1771 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1772 | /** |
Heinrich Schuchardt | 1d59a57 | 2020-12-04 03:02:03 +0100 | [diff] [blame^] | 1773 | * efi_locate_device_path() - Get the device path and handle of an device |
| 1774 | * implementing a protocol |
| 1775 | * @protocol: GUID of the protocol |
| 1776 | * @device_path: device path |
| 1777 | * @device: handle of the device |
| 1778 | * |
| 1779 | * This function implements the LocateDevicePath service. |
| 1780 | * |
| 1781 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1782 | * details. |
| 1783 | * |
| 1784 | * Return: status code |
| 1785 | */ |
| 1786 | static efi_status_t EFIAPI efi_locate_device_path( |
| 1787 | const efi_guid_t *protocol, |
| 1788 | struct efi_device_path **device_path, |
| 1789 | efi_handle_t *device) |
| 1790 | { |
| 1791 | struct efi_device_path *dp; |
| 1792 | size_t i; |
| 1793 | struct efi_handler *handler; |
| 1794 | efi_handle_t *handles; |
| 1795 | size_t len, len_dp; |
| 1796 | size_t len_best = 0; |
| 1797 | efi_uintn_t no_handles; |
| 1798 | u8 *remainder; |
| 1799 | efi_status_t ret; |
| 1800 | |
| 1801 | EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device); |
| 1802 | |
| 1803 | if (!protocol || !device_path || !*device_path) { |
| 1804 | ret = EFI_INVALID_PARAMETER; |
| 1805 | goto out; |
| 1806 | } |
| 1807 | |
| 1808 | /* Find end of device path */ |
| 1809 | len = efi_dp_instance_size(*device_path); |
| 1810 | |
| 1811 | /* Get all handles implementing the protocol */ |
| 1812 | ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL, |
| 1813 | &no_handles, &handles)); |
| 1814 | if (ret != EFI_SUCCESS) |
| 1815 | goto out; |
| 1816 | |
| 1817 | for (i = 0; i < no_handles; ++i) { |
| 1818 | /* Find the device path protocol */ |
| 1819 | ret = efi_search_protocol(handles[i], &efi_guid_device_path, |
| 1820 | &handler); |
| 1821 | if (ret != EFI_SUCCESS) |
| 1822 | continue; |
| 1823 | dp = (struct efi_device_path *)handler->protocol_interface; |
| 1824 | len_dp = efi_dp_instance_size(dp); |
| 1825 | /* |
| 1826 | * This handle can only be a better fit |
| 1827 | * if its device path length is longer than the best fit and |
| 1828 | * if its device path length is shorter of equal the searched |
| 1829 | * device path. |
| 1830 | */ |
| 1831 | if (len_dp <= len_best || len_dp > len) |
| 1832 | continue; |
| 1833 | /* Check if dp is a subpath of device_path */ |
| 1834 | if (memcmp(*device_path, dp, len_dp)) |
| 1835 | continue; |
| 1836 | if (!device) { |
| 1837 | ret = EFI_INVALID_PARAMETER; |
| 1838 | goto out; |
| 1839 | } |
| 1840 | *device = handles[i]; |
| 1841 | len_best = len_dp; |
| 1842 | } |
| 1843 | if (len_best) { |
| 1844 | remainder = (u8 *)*device_path + len_best; |
| 1845 | *device_path = (struct efi_device_path *)remainder; |
| 1846 | ret = EFI_SUCCESS; |
| 1847 | } else { |
| 1848 | ret = EFI_NOT_FOUND; |
| 1849 | } |
| 1850 | out: |
| 1851 | return EFI_EXIT(ret); |
| 1852 | } |
| 1853 | |
| 1854 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1855 | * efi_load_image_from_path() - load an image using a file path |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1856 | * |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1857 | * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the |
| 1858 | * callers obligation to update the memory type as needed. |
| 1859 | * |
| 1860 | * @file_path: the path of the image to load |
| 1861 | * @buffer: buffer containing the loaded image |
| 1862 | * @size: size of the loaded image |
| 1863 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1864 | */ |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 1865 | static |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 1866 | efi_status_t efi_load_image_from_path(struct efi_device_path *file_path, |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1867 | void **buffer, efi_uintn_t *size) |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1868 | { |
| 1869 | struct efi_file_info *info = NULL; |
| 1870 | struct efi_file_handle *f; |
| 1871 | static efi_status_t ret; |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1872 | u64 addr; |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 1873 | efi_uintn_t bs; |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1874 | |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1875 | /* In case of failure nothing is returned */ |
| 1876 | *buffer = NULL; |
| 1877 | *size = 0; |
| 1878 | |
| 1879 | /* Open file */ |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1880 | f = efi_file_from_path(file_path); |
| 1881 | if (!f) |
Heinrich Schuchardt | 6a41cf2 | 2019-06-11 19:00:56 +0200 | [diff] [blame] | 1882 | return EFI_NOT_FOUND; |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1883 | |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1884 | /* Get file size */ |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1885 | bs = 0; |
| 1886 | EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, |
| 1887 | &bs, info)); |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1888 | if (ret != EFI_BUFFER_TOO_SMALL) { |
| 1889 | ret = EFI_DEVICE_ERROR; |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1890 | goto error; |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1891 | } |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1892 | |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1893 | info = malloc(bs); |
| 1894 | EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs, |
| 1895 | info)); |
| 1896 | if (ret != EFI_SUCCESS) |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1897 | goto error; |
| 1898 | |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1899 | /* |
| 1900 | * When reading the file we do not yet know if it contains an |
| 1901 | * application, a boottime driver, or a runtime driver. So here we |
| 1902 | * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to |
| 1903 | * update the reservation according to the image type. |
| 1904 | */ |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 1905 | bs = info->file_size; |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1906 | ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, |
| 1907 | EFI_BOOT_SERVICES_DATA, |
| 1908 | efi_size_in_pages(bs), &addr); |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1909 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1910 | ret = EFI_OUT_OF_RESOURCES; |
| 1911 | goto error; |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1912 | } |
| 1913 | |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1914 | /* Read file */ |
| 1915 | EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr)); |
| 1916 | if (ret != EFI_SUCCESS) |
| 1917 | efi_free_pages(addr, efi_size_in_pages(bs)); |
| 1918 | *buffer = (void *)(uintptr_t)addr; |
| 1919 | *size = bs; |
| 1920 | error: |
| 1921 | EFI_CALL(f->close(f)); |
| 1922 | free(info); |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1923 | return ret; |
| 1924 | } |
| 1925 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1926 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1927 | * efi_load_image() - load an EFI image into memory |
| 1928 | * @boot_policy: true for request originating from the boot manager |
| 1929 | * @parent_image: the caller's image handle |
| 1930 | * @file_path: the path of the image to load |
| 1931 | * @source_buffer: memory location from which the image is installed |
| 1932 | * @source_size: size of the memory area from which the image is installed |
| 1933 | * @image_handle: handle for the newly installed image |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1934 | * |
| 1935 | * This function implements the LoadImage service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1936 | * |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1937 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 1938 | * for details. |
| 1939 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1940 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1941 | */ |
AKASHI Takahiro | b0ad905 | 2019-03-05 14:53:31 +0900 | [diff] [blame] | 1942 | efi_status_t EFIAPI efi_load_image(bool boot_policy, |
| 1943 | efi_handle_t parent_image, |
| 1944 | struct efi_device_path *file_path, |
| 1945 | void *source_buffer, |
| 1946 | efi_uintn_t source_size, |
| 1947 | efi_handle_t *image_handle) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1948 | { |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1949 | struct efi_device_path *dp, *fp; |
Tom Rini | 04c4906 | 2018-09-30 10:38:15 -0400 | [diff] [blame] | 1950 | struct efi_loaded_image *info = NULL; |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 1951 | struct efi_loaded_image_obj **image_obj = |
| 1952 | (struct efi_loaded_image_obj **)image_handle; |
Heinrich Schuchardt | d4d7ca9 | 2017-12-04 18:03:03 +0100 | [diff] [blame] | 1953 | efi_status_t ret; |
Heinrich Schuchardt | 47e35a9 | 2019-03-04 17:50:05 +0100 | [diff] [blame] | 1954 | void *dest_buffer; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1955 | |
Heinrich Schuchardt | 4cde1fa | 2018-04-03 22:29:30 +0200 | [diff] [blame] | 1956 | EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1957 | file_path, source_buffer, source_size, image_handle); |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1958 | |
Heinrich Schuchardt | b7b1011 | 2019-06-11 18:52:33 +0200 | [diff] [blame] | 1959 | if (!image_handle || (!source_buffer && !file_path) || |
| 1960 | !efi_search_obj(parent_image) || |
| 1961 | /* The parent image handle must refer to a loaded image */ |
| 1962 | !parent_image->type) { |
Heinrich Schuchardt | 2e0a790 | 2019-05-05 16:55:06 +0200 | [diff] [blame] | 1963 | ret = EFI_INVALID_PARAMETER; |
| 1964 | goto error; |
| 1965 | } |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1966 | |
| 1967 | if (!source_buffer) { |
Heinrich Schuchardt | 47e35a9 | 2019-03-04 17:50:05 +0100 | [diff] [blame] | 1968 | ret = efi_load_image_from_path(file_path, &dest_buffer, |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1969 | &source_size); |
Heinrich Schuchardt | d4d7ca9 | 2017-12-04 18:03:03 +0100 | [diff] [blame] | 1970 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1971 | goto error; |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1972 | } else { |
Heinrich Schuchardt | 47e35a9 | 2019-03-04 17:50:05 +0100 | [diff] [blame] | 1973 | dest_buffer = source_buffer; |
Rob Clark | 857a122 | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1974 | } |
Heinrich Schuchardt | 28b3917 | 2019-04-20 19:24:43 +0000 | [diff] [blame] | 1975 | /* split file_path which contains both the device and file parts */ |
| 1976 | efi_dp_split_file_path(file_path, &dp, &fp); |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1977 | ret = efi_setup_loaded_image(dp, fp, image_obj, &info); |
Heinrich Schuchardt | 47e35a9 | 2019-03-04 17:50:05 +0100 | [diff] [blame] | 1978 | if (ret == EFI_SUCCESS) |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 1979 | ret = efi_load_pe(*image_obj, dest_buffer, source_size, info); |
Heinrich Schuchardt | 47e35a9 | 2019-03-04 17:50:05 +0100 | [diff] [blame] | 1980 | if (!source_buffer) |
| 1981 | /* Release buffer to which file was loaded */ |
| 1982 | efi_free_pages((uintptr_t)dest_buffer, |
| 1983 | efi_size_in_pages(source_size)); |
AKASHI Takahiro | 0e104e3 | 2020-04-14 11:51:44 +0900 | [diff] [blame] | 1984 | if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) { |
Heinrich Schuchardt | 47e35a9 | 2019-03-04 17:50:05 +0100 | [diff] [blame] | 1985 | info->system_table = &systab; |
| 1986 | info->parent_handle = parent_image; |
| 1987 | } else { |
| 1988 | /* The image is invalid. Release all associated resources. */ |
| 1989 | efi_delete_handle(*image_handle); |
| 1990 | *image_handle = NULL; |
| 1991 | free(info); |
| 1992 | } |
Heinrich Schuchardt | c935c2f | 2018-03-07 02:40:51 +0100 | [diff] [blame] | 1993 | error: |
Heinrich Schuchardt | d4d7ca9 | 2017-12-04 18:03:03 +0100 | [diff] [blame] | 1994 | return EFI_EXIT(ret); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1995 | } |
| 1996 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1997 | /** |
Alexander Graf | fa5246b | 2018-11-15 21:23:47 +0100 | [diff] [blame] | 1998 | * efi_exit_caches() - fix up caches for EFI payloads if necessary |
| 1999 | */ |
| 2000 | static void efi_exit_caches(void) |
| 2001 | { |
Heinrich Schuchardt | 149d5d4 | 2019-07-22 22:04:36 +0200 | [diff] [blame] | 2002 | #if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND) |
Alexander Graf | fa5246b | 2018-11-15 21:23:47 +0100 | [diff] [blame] | 2003 | /* |
Heinrich Schuchardt | 149d5d4 | 2019-07-22 22:04:36 +0200 | [diff] [blame] | 2004 | * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if |
| 2005 | * caches are enabled. |
| 2006 | * |
| 2007 | * TODO: |
| 2008 | * According to the UEFI spec caches that can be managed via CP15 |
| 2009 | * operations should be enabled. Caches requiring platform information |
| 2010 | * to manage should be disabled. This should not happen in |
| 2011 | * ExitBootServices() but before invoking any UEFI binary is invoked. |
| 2012 | * |
| 2013 | * We want to keep the current workaround while GRUB prior to version |
| 2014 | * 2.04 is still in use. |
Alexander Graf | fa5246b | 2018-11-15 21:23:47 +0100 | [diff] [blame] | 2015 | */ |
Heinrich Schuchardt | 149d5d4 | 2019-07-22 22:04:36 +0200 | [diff] [blame] | 2016 | cleanup_before_linux(); |
Alexander Graf | fa5246b | 2018-11-15 21:23:47 +0100 | [diff] [blame] | 2017 | #endif |
| 2018 | } |
| 2019 | |
| 2020 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2021 | * efi_exit_boot_services() - stop all boot services |
| 2022 | * @image_handle: handle of the loaded image |
| 2023 | * @map_key: key of the memory map |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2024 | * |
| 2025 | * This function implements the ExitBootServices service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2026 | * |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2027 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 2028 | * for details. |
| 2029 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2030 | * All timer events are disabled. For exit boot services events the |
| 2031 | * notification function is called. The boot services are disabled in the |
| 2032 | * system table. |
Heinrich Schuchardt | b1fbb21 | 2018-01-19 20:24:52 +0100 | [diff] [blame] | 2033 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2034 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2035 | */ |
Heinrich Schuchardt | b7cb8b4 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 2036 | static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle, |
Heinrich Schuchardt | 8bd4f41 | 2019-05-05 21:58:35 +0200 | [diff] [blame] | 2037 | efi_uintn_t map_key) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2038 | { |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 2039 | struct efi_event *evt, *next_event; |
Heinrich Schuchardt | afc3367 | 2019-06-11 20:05:40 +0200 | [diff] [blame] | 2040 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | 2d4c738 | 2017-09-15 10:06:18 +0200 | [diff] [blame] | 2041 | |
Heinrich Schuchardt | 8bd4f41 | 2019-05-05 21:58:35 +0200 | [diff] [blame] | 2042 | EFI_ENTRY("%p, %zx", image_handle, map_key); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2043 | |
Heinrich Schuchardt | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 2044 | /* Check that the caller has read the current memory map */ |
Heinrich Schuchardt | afc3367 | 2019-06-11 20:05:40 +0200 | [diff] [blame] | 2045 | if (map_key != efi_memory_map_key) { |
| 2046 | ret = EFI_INVALID_PARAMETER; |
| 2047 | goto out; |
| 2048 | } |
Heinrich Schuchardt | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 2049 | |
Heinrich Schuchardt | b1fbb21 | 2018-01-19 20:24:52 +0100 | [diff] [blame] | 2050 | /* Check if ExitBootServices has already been called */ |
| 2051 | if (!systab.boottime) |
Heinrich Schuchardt | afc3367 | 2019-06-11 20:05:40 +0200 | [diff] [blame] | 2052 | goto out; |
Heinrich Schuchardt | b1fbb21 | 2018-01-19 20:24:52 +0100 | [diff] [blame] | 2053 | |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 2054 | /* Stop all timer related activities */ |
| 2055 | timers_enabled = false; |
| 2056 | |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 2057 | /* Add related events to the event group */ |
| 2058 | list_for_each_entry(evt, &efi_events, link) { |
| 2059 | if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES) |
| 2060 | evt->group = &efi_guid_event_group_exit_boot_services; |
| 2061 | } |
Heinrich Schuchardt | 2d4c738 | 2017-09-15 10:06:18 +0200 | [diff] [blame] | 2062 | /* Notify that ExitBootServices is invoked. */ |
Heinrich Schuchardt | 370c7a8 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 2063 | list_for_each_entry(evt, &efi_events, link) { |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 2064 | if (evt->group && |
| 2065 | !guidcmp(evt->group, |
| 2066 | &efi_guid_event_group_exit_boot_services)) { |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 2067 | efi_signal_event(evt); |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 2068 | break; |
| 2069 | } |
Heinrich Schuchardt | 2d4c738 | 2017-09-15 10:06:18 +0200 | [diff] [blame] | 2070 | } |
Heinrich Schuchardt | 2d4c738 | 2017-09-15 10:06:18 +0200 | [diff] [blame] | 2071 | |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 2072 | /* Make sure that notification functions are not called anymore */ |
| 2073 | efi_tpl = TPL_HIGH_LEVEL; |
| 2074 | |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 2075 | /* Notify variable services */ |
| 2076 | efi_variables_boot_exit_notify(); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 2077 | |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 2078 | /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */ |
| 2079 | list_for_each_entry_safe(evt, next_event, &efi_events, link) { |
| 2080 | if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) |
| 2081 | list_del(&evt->link); |
| 2082 | } |
| 2083 | |
Heinrich Schuchardt | 628dbd7 | 2020-11-12 21:26:28 +0100 | [diff] [blame] | 2084 | if (!efi_st_keep_devices) { |
| 2085 | if IS_ENABLED(CONFIG_USB_DEVICE) |
| 2086 | udc_disconnect(); |
| 2087 | board_quiesce_devices(); |
| 2088 | dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); |
| 2089 | } |
Alexander Graf | 2ebeb44 | 2016-11-17 01:02:57 +0100 | [diff] [blame] | 2090 | |
Heinrich Schuchardt | 1943dbb | 2019-07-05 17:42:16 +0200 | [diff] [blame] | 2091 | /* Patch out unsupported runtime function */ |
| 2092 | efi_runtime_detach(); |
| 2093 | |
Alexander Graf | fa5246b | 2018-11-15 21:23:47 +0100 | [diff] [blame] | 2094 | /* Fix up caches for EFI payloads if necessary */ |
| 2095 | efi_exit_caches(); |
| 2096 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2097 | /* This stops all lingering devices */ |
| 2098 | bootm_disable_interrupts(); |
| 2099 | |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 2100 | /* Disable boot time services */ |
Heinrich Schuchardt | b1fbb21 | 2018-01-19 20:24:52 +0100 | [diff] [blame] | 2101 | systab.con_in_handle = NULL; |
| 2102 | systab.con_in = NULL; |
| 2103 | systab.con_out_handle = NULL; |
| 2104 | systab.con_out = NULL; |
| 2105 | systab.stderr_handle = NULL; |
| 2106 | systab.std_err = NULL; |
| 2107 | systab.boottime = NULL; |
| 2108 | |
| 2109 | /* Recalculate CRC32 */ |
Heinrich Schuchardt | 15070db | 2018-06-28 12:45:31 +0200 | [diff] [blame] | 2110 | efi_update_table_header_crc32(&systab.hdr); |
Heinrich Schuchardt | b1fbb21 | 2018-01-19 20:24:52 +0100 | [diff] [blame] | 2111 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2112 | /* Give the payload some time to boot */ |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 2113 | efi_set_watchdog(0); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2114 | WATCHDOG_RESET(); |
Heinrich Schuchardt | afc3367 | 2019-06-11 20:05:40 +0200 | [diff] [blame] | 2115 | out: |
| 2116 | return EFI_EXIT(ret); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2117 | } |
| 2118 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2119 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2120 | * efi_get_next_monotonic_count() - get next value of the counter |
| 2121 | * @count: returned value of the counter |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2122 | * |
| 2123 | * This function implements the NextMonotonicCount service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2124 | * |
| 2125 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2126 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2127 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2128 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2129 | */ |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2130 | static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count) |
| 2131 | { |
Heinrich Schuchardt | 9106459 | 2018-02-18 15:17:49 +0100 | [diff] [blame] | 2132 | static uint64_t mono; |
Heinrich Schuchardt | ddc787d | 2019-05-17 12:47:17 +0200 | [diff] [blame] | 2133 | efi_status_t ret; |
Heinrich Schuchardt | 9106459 | 2018-02-18 15:17:49 +0100 | [diff] [blame] | 2134 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2135 | EFI_ENTRY("%p", count); |
Heinrich Schuchardt | ddc787d | 2019-05-17 12:47:17 +0200 | [diff] [blame] | 2136 | if (!count) { |
| 2137 | ret = EFI_INVALID_PARAMETER; |
| 2138 | goto out; |
| 2139 | } |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2140 | *count = mono++; |
Heinrich Schuchardt | ddc787d | 2019-05-17 12:47:17 +0200 | [diff] [blame] | 2141 | ret = EFI_SUCCESS; |
| 2142 | out: |
| 2143 | return EFI_EXIT(ret); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2144 | } |
| 2145 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2146 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2147 | * efi_stall() - sleep |
| 2148 | * @microseconds: period to sleep in microseconds |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2149 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2150 | * This function implements the Stall service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2151 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2152 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2153 | * details. |
| 2154 | * |
| 2155 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2156 | */ |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2157 | static efi_status_t EFIAPI efi_stall(unsigned long microseconds) |
| 2158 | { |
Heinrich Schuchardt | 9912c03 | 2019-06-02 21:12:17 +0200 | [diff] [blame] | 2159 | u64 end_tick; |
| 2160 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2161 | EFI_ENTRY("%ld", microseconds); |
Heinrich Schuchardt | 9912c03 | 2019-06-02 21:12:17 +0200 | [diff] [blame] | 2162 | |
| 2163 | end_tick = get_ticks() + usec_to_tick(microseconds); |
| 2164 | while (get_ticks() < end_tick) |
| 2165 | efi_timer_check(); |
| 2166 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2167 | return EFI_EXIT(EFI_SUCCESS); |
| 2168 | } |
| 2169 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2170 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2171 | * efi_set_watchdog_timer() - reset the watchdog timer |
| 2172 | * @timeout: seconds before reset by watchdog |
| 2173 | * @watchdog_code: code to be logged when resetting |
| 2174 | * @data_size: size of buffer in bytes |
| 2175 | * @watchdog_data: buffer with data describing the reset reason |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2176 | * |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 2177 | * This function implements the SetWatchdogTimer service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2178 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2179 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2180 | * details. |
| 2181 | * |
| 2182 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2183 | */ |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2184 | static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout, |
| 2185 | uint64_t watchdog_code, |
| 2186 | unsigned long data_size, |
| 2187 | uint16_t *watchdog_data) |
| 2188 | { |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 2189 | EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2190 | data_size, watchdog_data); |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 2191 | return EFI_EXIT(efi_set_watchdog(timeout)); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2192 | } |
| 2193 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2194 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2195 | * efi_close_protocol() - close a protocol |
| 2196 | * @handle: handle on which the protocol shall be closed |
| 2197 | * @protocol: GUID of the protocol to close |
| 2198 | * @agent_handle: handle of the driver |
| 2199 | * @controller_handle: handle of the controller |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2200 | * |
| 2201 | * This function implements the CloseProtocol service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2202 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2203 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2204 | * details. |
| 2205 | * |
| 2206 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2207 | */ |
AKASHI Takahiro | d99b1b3 | 2020-03-17 11:12:36 +0900 | [diff] [blame] | 2208 | efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle, |
| 2209 | const efi_guid_t *protocol, |
| 2210 | efi_handle_t agent_handle, |
| 2211 | efi_handle_t controller_handle) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2212 | { |
Heinrich Schuchardt | 4fd1ee2 | 2018-01-11 08:15:59 +0100 | [diff] [blame] | 2213 | struct efi_handler *handler; |
| 2214 | struct efi_open_protocol_info_item *item; |
| 2215 | struct efi_open_protocol_info_item *pos; |
| 2216 | efi_status_t r; |
| 2217 | |
Rob Clark | 238f88c | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 2218 | EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2219 | controller_handle); |
Heinrich Schuchardt | 4fd1ee2 | 2018-01-11 08:15:59 +0100 | [diff] [blame] | 2220 | |
Heinrich Schuchardt | 5d8231b | 2019-05-05 10:37:51 +0200 | [diff] [blame] | 2221 | if (!efi_search_obj(agent_handle) || |
| 2222 | (controller_handle && !efi_search_obj(controller_handle))) { |
Heinrich Schuchardt | 4fd1ee2 | 2018-01-11 08:15:59 +0100 | [diff] [blame] | 2223 | r = EFI_INVALID_PARAMETER; |
| 2224 | goto out; |
| 2225 | } |
| 2226 | r = efi_search_protocol(handle, protocol, &handler); |
| 2227 | if (r != EFI_SUCCESS) |
| 2228 | goto out; |
| 2229 | |
| 2230 | r = EFI_NOT_FOUND; |
| 2231 | list_for_each_entry_safe(item, pos, &handler->open_infos, link) { |
| 2232 | if (item->info.agent_handle == agent_handle && |
| 2233 | item->info.controller_handle == controller_handle) { |
| 2234 | efi_delete_open_info(item); |
| 2235 | r = EFI_SUCCESS; |
Heinrich Schuchardt | 4fd1ee2 | 2018-01-11 08:15:59 +0100 | [diff] [blame] | 2236 | } |
| 2237 | } |
| 2238 | out: |
| 2239 | return EFI_EXIT(r); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2240 | } |
| 2241 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2242 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2243 | * efi_open_protocol_information() - provide information about then open status |
| 2244 | * of a protocol on a handle |
| 2245 | * @handle: handle for which the information shall be retrieved |
| 2246 | * @protocol: GUID of the protocol |
| 2247 | * @entry_buffer: buffer to receive the open protocol information |
| 2248 | * @entry_count: number of entries available in the buffer |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2249 | * |
| 2250 | * This function implements the OpenProtocolInformation service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2251 | * |
| 2252 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2253 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2254 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2255 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2256 | */ |
Heinrich Schuchardt | 9106459 | 2018-02-18 15:17:49 +0100 | [diff] [blame] | 2257 | static efi_status_t EFIAPI efi_open_protocol_information( |
| 2258 | efi_handle_t handle, const efi_guid_t *protocol, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2259 | struct efi_open_protocol_info_entry **entry_buffer, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 2260 | efi_uintn_t *entry_count) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2261 | { |
Heinrich Schuchardt | f2ef22e | 2018-01-11 08:16:00 +0100 | [diff] [blame] | 2262 | unsigned long buffer_size; |
| 2263 | unsigned long count; |
| 2264 | struct efi_handler *handler; |
| 2265 | struct efi_open_protocol_info_item *item; |
| 2266 | efi_status_t r; |
| 2267 | |
Rob Clark | 238f88c | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 2268 | EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2269 | entry_count); |
Heinrich Schuchardt | f2ef22e | 2018-01-11 08:16:00 +0100 | [diff] [blame] | 2270 | |
| 2271 | /* Check parameters */ |
| 2272 | if (!entry_buffer) { |
| 2273 | r = EFI_INVALID_PARAMETER; |
| 2274 | goto out; |
| 2275 | } |
| 2276 | r = efi_search_protocol(handle, protocol, &handler); |
| 2277 | if (r != EFI_SUCCESS) |
| 2278 | goto out; |
| 2279 | |
| 2280 | /* Count entries */ |
| 2281 | count = 0; |
| 2282 | list_for_each_entry(item, &handler->open_infos, link) { |
| 2283 | if (item->info.open_count) |
| 2284 | ++count; |
| 2285 | } |
| 2286 | *entry_count = count; |
| 2287 | *entry_buffer = NULL; |
| 2288 | if (!count) { |
| 2289 | r = EFI_SUCCESS; |
| 2290 | goto out; |
| 2291 | } |
| 2292 | |
| 2293 | /* Copy entries */ |
| 2294 | buffer_size = count * sizeof(struct efi_open_protocol_info_entry); |
Heinrich Schuchardt | abb4387 | 2018-10-03 20:02:29 +0200 | [diff] [blame] | 2295 | r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size, |
Heinrich Schuchardt | f2ef22e | 2018-01-11 08:16:00 +0100 | [diff] [blame] | 2296 | (void **)entry_buffer); |
| 2297 | if (r != EFI_SUCCESS) |
| 2298 | goto out; |
| 2299 | list_for_each_entry_reverse(item, &handler->open_infos, link) { |
| 2300 | if (item->info.open_count) |
| 2301 | (*entry_buffer)[--count] = item->info; |
| 2302 | } |
| 2303 | out: |
| 2304 | return EFI_EXIT(r); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2305 | } |
| 2306 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2307 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2308 | * efi_protocols_per_handle() - get protocols installed on a handle |
| 2309 | * @handle: handle for which the information is retrieved |
| 2310 | * @protocol_buffer: buffer with protocol GUIDs |
| 2311 | * @protocol_buffer_count: number of entries in the buffer |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2312 | * |
| 2313 | * This function implements the ProtocolsPerHandleService. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2314 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2315 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2316 | * details. |
| 2317 | * |
| 2318 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2319 | */ |
Heinrich Schuchardt | b7cb8b4 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 2320 | static efi_status_t EFIAPI efi_protocols_per_handle( |
| 2321 | efi_handle_t handle, efi_guid_t ***protocol_buffer, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 2322 | efi_uintn_t *protocol_buffer_count) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2323 | { |
xypron.glpk@gmx.de | 8960c97 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2324 | unsigned long buffer_size; |
| 2325 | struct efi_object *efiobj; |
Heinrich Schuchardt | 99ce206 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 2326 | struct list_head *protocol_handle; |
xypron.glpk@gmx.de | 8960c97 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2327 | efi_status_t r; |
| 2328 | |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2329 | EFI_ENTRY("%p, %p, %p", handle, protocol_buffer, |
| 2330 | protocol_buffer_count); |
xypron.glpk@gmx.de | 8960c97 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2331 | |
| 2332 | if (!handle || !protocol_buffer || !protocol_buffer_count) |
| 2333 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 2334 | |
| 2335 | *protocol_buffer = NULL; |
Rob Clark | d51b8ca | 2017-07-20 07:59:39 -0400 | [diff] [blame] | 2336 | *protocol_buffer_count = 0; |
xypron.glpk@gmx.de | 8960c97 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2337 | |
Heinrich Schuchardt | 99ce206 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 2338 | efiobj = efi_search_obj(handle); |
| 2339 | if (!efiobj) |
| 2340 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
xypron.glpk@gmx.de | 8960c97 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2341 | |
Heinrich Schuchardt | 99ce206 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 2342 | /* Count protocols */ |
| 2343 | list_for_each(protocol_handle, &efiobj->protocols) { |
| 2344 | ++*protocol_buffer_count; |
| 2345 | } |
| 2346 | |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 2347 | /* Copy GUIDs */ |
Heinrich Schuchardt | 99ce206 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 2348 | if (*protocol_buffer_count) { |
| 2349 | size_t j = 0; |
| 2350 | |
| 2351 | buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count; |
Heinrich Schuchardt | abb4387 | 2018-10-03 20:02:29 +0200 | [diff] [blame] | 2352 | r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size, |
Heinrich Schuchardt | 99ce206 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 2353 | (void **)protocol_buffer); |
| 2354 | if (r != EFI_SUCCESS) |
| 2355 | return EFI_EXIT(r); |
| 2356 | list_for_each(protocol_handle, &efiobj->protocols) { |
| 2357 | struct efi_handler *protocol; |
| 2358 | |
| 2359 | protocol = list_entry(protocol_handle, |
| 2360 | struct efi_handler, link); |
| 2361 | (*protocol_buffer)[j] = (void *)protocol->guid; |
| 2362 | ++j; |
xypron.glpk@gmx.de | 8960c97 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2363 | } |
xypron.glpk@gmx.de | 8960c97 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | return EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2367 | } |
| 2368 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2369 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2370 | * efi_locate_handle_buffer() - locate handles implementing a protocol |
| 2371 | * @search_type: selection criterion |
| 2372 | * @protocol: GUID of the protocol |
| 2373 | * @search_key: registration key |
| 2374 | * @no_handles: number of returned handles |
| 2375 | * @buffer: buffer with the returned handles |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2376 | * |
| 2377 | * This function implements the LocateHandleBuffer service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2378 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2379 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2380 | * details. |
| 2381 | * |
| 2382 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2383 | */ |
AKASHI Takahiro | d99b1b3 | 2020-03-17 11:12:36 +0900 | [diff] [blame] | 2384 | efi_status_t EFIAPI efi_locate_handle_buffer( |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2385 | enum efi_locate_search_type search_type, |
Heinrich Schuchardt | e547c66 | 2017-10-05 16:35:53 +0200 | [diff] [blame] | 2386 | const efi_guid_t *protocol, void *search_key, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 2387 | efi_uintn_t *no_handles, efi_handle_t **buffer) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2388 | { |
xypron.glpk@gmx.de | 550a68a | 2017-07-11 22:06:22 +0200 | [diff] [blame] | 2389 | efi_status_t r; |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 2390 | efi_uintn_t buffer_size = 0; |
xypron.glpk@gmx.de | 550a68a | 2017-07-11 22:06:22 +0200 | [diff] [blame] | 2391 | |
Rob Clark | 238f88c | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 2392 | EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2393 | no_handles, buffer); |
xypron.glpk@gmx.de | 550a68a | 2017-07-11 22:06:22 +0200 | [diff] [blame] | 2394 | |
| 2395 | if (!no_handles || !buffer) { |
| 2396 | r = EFI_INVALID_PARAMETER; |
| 2397 | goto out; |
| 2398 | } |
| 2399 | *no_handles = 0; |
| 2400 | *buffer = NULL; |
| 2401 | r = efi_locate_handle(search_type, protocol, search_key, &buffer_size, |
| 2402 | *buffer); |
| 2403 | if (r != EFI_BUFFER_TOO_SMALL) |
| 2404 | goto out; |
Heinrich Schuchardt | abb4387 | 2018-10-03 20:02:29 +0200 | [diff] [blame] | 2405 | r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size, |
xypron.glpk@gmx.de | 550a68a | 2017-07-11 22:06:22 +0200 | [diff] [blame] | 2406 | (void **)buffer); |
| 2407 | if (r != EFI_SUCCESS) |
| 2408 | goto out; |
| 2409 | r = efi_locate_handle(search_type, protocol, search_key, &buffer_size, |
| 2410 | *buffer); |
| 2411 | if (r == EFI_SUCCESS) |
Heinrich Schuchardt | b7cb8b4 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 2412 | *no_handles = buffer_size / sizeof(efi_handle_t); |
xypron.glpk@gmx.de | 550a68a | 2017-07-11 22:06:22 +0200 | [diff] [blame] | 2413 | out: |
| 2414 | return EFI_EXIT(r); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2415 | } |
| 2416 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2417 | /** |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2418 | * efi_locate_protocol() - find an interface implementing a protocol |
| 2419 | * @protocol: GUID of the protocol |
| 2420 | * @registration: registration key passed to the notification function |
| 2421 | * @protocol_interface: interface implementing the protocol |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2422 | * |
| 2423 | * This function implements the LocateProtocol service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2424 | * |
| 2425 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2426 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2427 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2428 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2429 | */ |
Heinrich Schuchardt | e547c66 | 2017-10-05 16:35:53 +0200 | [diff] [blame] | 2430 | static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2431 | void *registration, |
| 2432 | void **protocol_interface) |
| 2433 | { |
Heinrich Schuchardt | 2600eff | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2434 | struct efi_handler *handler; |
Heinrich Schuchardt | 57505e9 | 2017-10-26 19:25:57 +0200 | [diff] [blame] | 2435 | efi_status_t ret; |
Heinrich Schuchardt | 2600eff | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2436 | struct efi_object *efiobj; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2437 | |
Rob Clark | 238f88c | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 2438 | EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface); |
xypron.glpk@gmx.de | 4534ad6 | 2017-07-11 22:06:24 +0200 | [diff] [blame] | 2439 | |
Heinrich Schuchardt | 2600eff | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2440 | /* |
| 2441 | * The UEFI spec explicitly requires a protocol even if a registration |
| 2442 | * key is provided. This differs from the logic in LocateHandle(). |
| 2443 | */ |
xypron.glpk@gmx.de | 4534ad6 | 2017-07-11 22:06:24 +0200 | [diff] [blame] | 2444 | if (!protocol || !protocol_interface) |
| 2445 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 2446 | |
Heinrich Schuchardt | 2600eff | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2447 | if (registration) { |
| 2448 | struct efi_register_notify_event *event; |
| 2449 | struct efi_protocol_notification *handle; |
xypron.glpk@gmx.de | 4534ad6 | 2017-07-11 22:06:24 +0200 | [diff] [blame] | 2450 | |
Heinrich Schuchardt | 2600eff | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2451 | event = efi_check_register_notify_event(registration); |
| 2452 | if (!event) |
| 2453 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 2454 | /* |
| 2455 | * The UEFI spec requires to return EFI_NOT_FOUND if no |
| 2456 | * protocol instance matches protocol and registration. |
| 2457 | * So let's do the same for a mismatch between protocol and |
| 2458 | * registration. |
| 2459 | */ |
| 2460 | if (guidcmp(&event->protocol, protocol)) |
| 2461 | goto not_found; |
| 2462 | if (list_empty(&event->handles)) |
| 2463 | goto not_found; |
| 2464 | handle = list_first_entry(&event->handles, |
| 2465 | struct efi_protocol_notification, |
| 2466 | link); |
| 2467 | efiobj = handle->handle; |
| 2468 | list_del(&handle->link); |
| 2469 | free(handle); |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 2470 | ret = efi_search_protocol(efiobj, protocol, &handler); |
Heinrich Schuchardt | 2600eff | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2471 | if (ret == EFI_SUCCESS) |
| 2472 | goto found; |
| 2473 | } else { |
| 2474 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
| 2475 | ret = efi_search_protocol(efiobj, protocol, &handler); |
| 2476 | if (ret == EFI_SUCCESS) |
| 2477 | goto found; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2478 | } |
| 2479 | } |
Heinrich Schuchardt | 2600eff | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2480 | not_found: |
xypron.glpk@gmx.de | 4534ad6 | 2017-07-11 22:06:24 +0200 | [diff] [blame] | 2481 | *protocol_interface = NULL; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2482 | return EFI_EXIT(EFI_NOT_FOUND); |
Heinrich Schuchardt | 2600eff | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2483 | found: |
| 2484 | *protocol_interface = handler->protocol_interface; |
| 2485 | return EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2486 | } |
| 2487 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2488 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2494 | * |
| 2495 | * This function implements the MultipleProtocolInterfaces service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2496 | * |
| 2497 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2498 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2499 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2500 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2501 | */ |
Heinrich Schuchardt | 744d83e | 2019-04-12 06:59:49 +0200 | [diff] [blame] | 2502 | efi_status_t EFIAPI efi_install_multiple_protocol_interfaces |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 2503 | (efi_handle_t *handle, ...) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2504 | { |
| 2505 | EFI_ENTRY("%p", handle); |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2506 | |
Alexander Graf | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2507 | efi_va_list argptr; |
Heinrich Schuchardt | e547c66 | 2017-10-05 16:35:53 +0200 | [diff] [blame] | 2508 | const efi_guid_t *protocol; |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2509 | void *protocol_interface; |
Heinrich Schuchardt | 61ba10d | 2019-05-16 21:54:04 +0200 | [diff] [blame] | 2510 | efi_handle_t old_handle; |
xypron.glpk@gmx.de | 83eebc7 | 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 | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2517 | efi_va_start(argptr, handle); |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2518 | for (;;) { |
Alexander Graf | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2519 | protocol = efi_va_arg(argptr, efi_guid_t*); |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2520 | if (!protocol) |
| 2521 | break; |
Alexander Graf | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2522 | protocol_interface = efi_va_arg(argptr, void*); |
Heinrich Schuchardt | 61ba10d | 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 | 2934497 | 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 | 61ba10d | 2019-05-16 21:54:04 +0200 | [diff] [blame] | 2533 | r = EFI_ALREADY_STARTED; |
| 2534 | break; |
| 2535 | } |
| 2536 | } |
Heinrich Schuchardt | 0a27ac8 | 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 | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2541 | if (r != EFI_SUCCESS) |
| 2542 | break; |
| 2543 | i++; |
| 2544 | } |
Alexander Graf | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2545 | efi_va_end(argptr); |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2546 | if (r == EFI_SUCCESS) |
| 2547 | return EFI_EXIT(r); |
| 2548 | |
Heinrich Schuchardt | ec47f3e | 2017-10-26 19:25:42 +0200 | [diff] [blame] | 2549 | /* If an error occurred undo all changes. */ |
Alexander Graf | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2550 | efi_va_start(argptr, handle); |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2551 | for (; i; --i) { |
Alexander Graf | 404a7c8 | 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 | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 2554 | EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol, |
Heinrich Schuchardt | 7cdc17f | 2017-11-06 21:17:45 +0100 | [diff] [blame] | 2555 | protocol_interface)); |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2556 | } |
Alexander Graf | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2557 | efi_va_end(argptr); |
xypron.glpk@gmx.de | 83eebc7 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2558 | |
| 2559 | return EFI_EXIT(r); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2560 | } |
| 2561 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2562 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2568 | * |
| 2569 | * This function implements the UninstallMultipleProtocolInterfaces service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2570 | * |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2575 | */ |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2576 | static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces( |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 2577 | efi_handle_t handle, ...) |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2578 | { |
| 2579 | EFI_ENTRY("%p", handle); |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2580 | |
Alexander Graf | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2581 | efi_va_list argptr; |
Heinrich Schuchardt | a616dcf | 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 | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2590 | efi_va_start(argptr, handle); |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2591 | for (;;) { |
Alexander Graf | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2592 | protocol = efi_va_arg(argptr, efi_guid_t*); |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2593 | if (!protocol) |
| 2594 | break; |
Alexander Graf | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2595 | protocol_interface = efi_va_arg(argptr, void*); |
Heinrich Schuchardt | 1b45134 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 2596 | r = efi_uninstall_protocol(handle, protocol, |
| 2597 | protocol_interface); |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2598 | if (r != EFI_SUCCESS) |
| 2599 | break; |
| 2600 | i++; |
| 2601 | } |
Alexander Graf | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2602 | efi_va_end(argptr); |
Heinrich Schuchardt | 1b45134 | 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 | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2609 | return EFI_EXIT(r); |
Heinrich Schuchardt | 1b45134 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 2610 | } |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2611 | |
| 2612 | /* If an error occurred undo all changes. */ |
Alexander Graf | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2613 | efi_va_start(argptr, handle); |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2614 | for (; i; --i) { |
Alexander Graf | 404a7c8 | 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 | a616dcf | 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 | 404a7c8 | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2621 | efi_va_end(argptr); |
Heinrich Schuchardt | a616dcf | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2622 | |
Heinrich Schuchardt | 1c76eda | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2625 | } |
| 2626 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2627 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2632 | * |
| 2633 | * This function implements the CalculateCrc32 service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2634 | * |
| 2635 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2636 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2637 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2638 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2639 | */ |
Heinrich Schuchardt | f272558 | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2643 | { |
Heinrich Schuchardt | 3f0282c | 2019-05-16 23:31:29 +0200 | [diff] [blame] | 2644 | efi_status_t ret = EFI_SUCCESS; |
| 2645 | |
Heinrich Schuchardt | f272558 | 2018-07-07 15:36:04 +0200 | [diff] [blame] | 2646 | EFI_ENTRY("%p, %zu", data, data_size); |
Heinrich Schuchardt | 3f0282c | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2651 | *crc32_p = crc32(0, data, data_size); |
Heinrich Schuchardt | 3f0282c | 2019-05-16 23:31:29 +0200 | [diff] [blame] | 2652 | out: |
| 2653 | return EFI_EXIT(ret); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2654 | } |
| 2655 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2656 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2661 | * |
| 2662 | * This function implements the CopyMem service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2663 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2664 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2665 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2666 | */ |
Heinrich Schuchardt | d0a349e | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2669 | { |
Heinrich Schuchardt | d0a349e | 2017-10-05 16:35:52 +0200 | [diff] [blame] | 2670 | EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length); |
Heinrich Schuchardt | efba6ba | 2019-01-09 21:41:13 +0100 | [diff] [blame] | 2671 | memmove(destination, source, length); |
Heinrich Schuchardt | a5270e0 | 2017-10-05 16:35:51 +0200 | [diff] [blame] | 2672 | EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2673 | } |
| 2674 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2675 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2680 | * |
| 2681 | * This function implements the SetMem service. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2682 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2683 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2684 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2685 | */ |
Heinrich Schuchardt | d0a349e | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2687 | { |
Heinrich Schuchardt | d0a349e | 2017-10-05 16:35:52 +0200 | [diff] [blame] | 2688 | EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2689 | memset(buffer, value, size); |
Heinrich Schuchardt | a5270e0 | 2017-10-05 16:35:51 +0200 | [diff] [blame] | 2690 | EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2691 | } |
| 2692 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2693 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2700 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2701 | * Return: status code |
Heinrich Schuchardt | 8cb38c0 | 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 | da433d5 | 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 | 8cb38c0 | 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 | da433d5 | 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 | 8cb38c0 | 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 | fa8dddf | 2019-05-30 14:16:31 +0200 | [diff] [blame] | 2751 | disconnect_next: |
| 2752 | opened_by_driver = false; |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2753 | list_for_each_entry(item, &handler->open_infos, link) { |
Heinrich Schuchardt | fa8dddf | 2019-05-30 14:16:31 +0200 | [diff] [blame] | 2754 | efi_status_t ret; |
| 2755 | |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2756 | if (item->info.attributes == |
Heinrich Schuchardt | fa8dddf | 2019-05-30 14:16:31 +0200 | [diff] [blame] | 2757 | EFI_OPEN_PROTOCOL_BY_DRIVER) { |
| 2758 | ret = EFI_CALL(efi_disconnect_controller( |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2759 | item->info.controller_handle, |
| 2760 | item->info.agent_handle, |
| 2761 | NULL)); |
Heinrich Schuchardt | fa8dddf | 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 | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2772 | } |
Heinrich Schuchardt | fa8dddf | 2019-05-30 14:16:31 +0200 | [diff] [blame] | 2773 | /* Only one driver can be connected */ |
Heinrich Schuchardt | 8cb38c0 | 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 | 7d69fc3 | 2019-06-01 20:15:10 +0200 | [diff] [blame] | 2781 | item->info.controller_handle == controller_handle && |
| 2782 | item->info.attributes == attributes) |
Heinrich Schuchardt | 8cb38c0 | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2805 | /** |
Mario Six | 8fac291 | 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 | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2813 | * |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2814 | * This function implements the OpenProtocol interface. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2815 | * |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2820 | */ |
Heinrich Schuchardt | 4f94ec5 | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2825 | { |
Heinrich Schuchardt | e5e78a3 | 2017-11-26 14:05:15 +0100 | [diff] [blame] | 2826 | struct efi_handler *handler; |
xypron.glpk@gmx.de | f097c84 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 2827 | efi_status_t r = EFI_INVALID_PARAMETER; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2828 | |
Rob Clark | 238f88c | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 2829 | EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2830 | protocol_interface, agent_handle, controller_handle, |
| 2831 | attributes); |
xypron.glpk@gmx.de | c35c924 | 2017-07-11 22:06:14 +0200 | [diff] [blame] | 2832 | |
xypron.glpk@gmx.de | f097c84 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 2833 | if (!handle || !protocol || |
| 2834 | (!protocol_interface && attributes != |
| 2835 | EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) { |
xypron.glpk@gmx.de | c35c924 | 2017-07-11 22:06:14 +0200 | [diff] [blame] | 2836 | goto out; |
| 2837 | } |
| 2838 | |
xypron.glpk@gmx.de | f097c84 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 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 | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2847 | /* fall-through */ |
xypron.glpk@gmx.de | f097c84 | 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 | 8cb38c0 | 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 | f097c84 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 2852 | goto out; |
Heinrich Schuchardt | 8cb38c0 | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2853 | /* fall-through */ |
xypron.glpk@gmx.de | f097c84 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 2854 | case EFI_OPEN_PROTOCOL_EXCLUSIVE: |
Heinrich Schuchardt | 8cb38c0 | 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 | f097c84 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 2857 | goto out; |
| 2858 | break; |
| 2859 | default: |
| 2860 | goto out; |
| 2861 | } |
| 2862 | |
Heinrich Schuchardt | e5e78a3 | 2017-11-26 14:05:15 +0100 | [diff] [blame] | 2863 | r = efi_search_protocol(handle, protocol, &handler); |
Heinrich Schuchardt | b673e4b | 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; |
| 2869 | goto out; |
| 2870 | default: |
Heinrich Schuchardt | e5e78a3 | 2017-11-26 14:05:15 +0100 | [diff] [blame] | 2871 | goto out; |
Heinrich Schuchardt | b673e4b | 2019-05-05 11:24:53 +0200 | [diff] [blame] | 2872 | } |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2873 | |
Heinrich Schuchardt | 8cb38c0 | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2876 | out: |
| 2877 | return EFI_EXIT(r); |
| 2878 | } |
| 2879 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2880 | /** |
Heinrich Schuchardt | 2650a4d | 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 | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 2900 | void *info; |
| 2901 | efi_handle_t parent_image = current_image; |
Heinrich Schuchardt | 2650a4d | 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 | 0e104e3 | 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 | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 2908 | /* Check parameters */ |
Heinrich Schuchardt | ff94bca | 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 | 0e104e3 | 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 | 8efe079 | 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 | 3d44512 | 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 | 2650a4d | 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 | f277d94 | 2020-09-10 12:22:54 +0200 | [diff] [blame] | 2933 | #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) |
Heinrich Schuchardt | 2650a4d | 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 | 1a3732c | 2020-05-27 01:58:30 +0200 | [diff] [blame] | 2939 | set_gd(app_gd); |
Heinrich Schuchardt | 2650a4d | 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 | 952e206 | 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 | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 2949 | current_image = parent_image; |
Heinrich Schuchardt | 2650a4d | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 2950 | return EFI_EXIT(image_obj->exit_status); |
| 2951 | } |
| 2952 | |
Heinrich Schuchardt | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 2953 | current_image = image_handle; |
Heinrich Schuchardt | b27ced4 | 2019-05-01 14:20:18 +0200 | [diff] [blame] | 2954 | image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE; |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 2955 | EFI_PRINT("Jumping into 0x%p\n", image_obj->entry); |
Heinrich Schuchardt | 2650a4d | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 2956 | ret = EFI_CALL(image_obj->entry(image_handle, &systab)); |
| 2957 | |
| 2958 | /* |
Heinrich Schuchardt | b7bc28d | 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 | 2650a4d | 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 | 4ed66e5 | 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 | e3bca2a | 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 | 4ed66e5 | 2019-05-01 18:25:45 +0200 | [diff] [blame] | 2976 | { |
Heinrich Schuchardt | e3bca2a | 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 | 4ed66e5 | 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 | e3bca2a | 2019-06-02 20:02:32 +0200 | [diff] [blame] | 3011 | |
| 3012 | return ret; |
Heinrich Schuchardt | 4ed66e5 | 2019-05-01 18:25:45 +0200 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | /** |
Heinrich Schuchardt | 6b3581a | 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 | 4ed66e5 | 2019-05-01 18:25:45 +0200 | [diff] [blame] | 3028 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | 6b3581a | 2019-05-01 19:04:32 +0200 | [diff] [blame] | 3029 | struct efi_object *efiobj; |
Heinrich Schuchardt | 4ed66e5 | 2019-05-01 18:25:45 +0200 | [diff] [blame] | 3030 | struct efi_loaded_image *loaded_image_protocol; |
Heinrich Schuchardt | 6b3581a | 2019-05-01 19:04:32 +0200 | [diff] [blame] | 3031 | |
| 3032 | EFI_ENTRY("%p", image_handle); |
Heinrich Schuchardt | 6b3581a | 2019-05-01 19:04:32 +0200 | [diff] [blame] | 3033 | |
Heinrich Schuchardt | 4ed66e5 | 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 | 6b3581a | 2019-05-01 19:04:32 +0200 | [diff] [blame] | 3069 | } |
| 3070 | |
| 3071 | /** |
Heinrich Schuchardt | 3d44512 | 2019-04-30 17:57:30 +0200 | [diff] [blame] | 3072 | * efi_update_exit_data() - fill exit data parameters of StartImage() |
| 3073 | * |
Heinrich Schuchardt | 74c2529 | 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 | 3d44512 | 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 | 2650a4d | 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 | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3129 | efi_status_t ret; |
Heinrich Schuchardt | 3758752 | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 3130 | struct efi_loaded_image *loaded_image_protocol; |
Heinrich Schuchardt | 2650a4d | 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 | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3137 | /* Check parameters */ |
Heinrich Schuchardt | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3138 | ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image, |
Heinrich Schuchardt | 3758752 | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 3139 | (void **)&loaded_image_protocol, |
| 3140 | NULL, NULL, |
Heinrich Schuchardt | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3141 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); |
Heinrich Schuchardt | 3758752 | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 3142 | if (ret != EFI_SUCCESS) { |
| 3143 | ret = EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3144 | goto out; |
Heinrich Schuchardt | 3758752 | 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 | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3165 | |
Heinrich Schuchardt | 3d44512 | 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 | 3758752 | 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 | 3d44512 | 2019-04-30 17:57:30 +0200 | [diff] [blame] | 3177 | |
Heinrich Schuchardt | 2650a4d | 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 | 8efe079 | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3191 | out: |
Heinrich Schuchardt | 3758752 | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 3192 | return EFI_EXIT(ret); |
Heinrich Schuchardt | 2650a4d | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | /** |
Mario Six | 8fac291 | 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 | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 3200 | * |
| 3201 | * This function implements the HandleProtocol service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3202 | * |
| 3203 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 3204 | * details. |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 3205 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3206 | * Return: status code |
Heinrich Schuchardt | a40db6e | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 3207 | */ |
AKASHI Takahiro | d99b1b3 | 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 | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3211 | { |
Heinrich Schuchardt | ef09615 | 2019-06-01 19:29:39 +0200 | [diff] [blame] | 3212 | return efi_open_protocol(handle, protocol, protocol_interface, efi_root, |
xypron.glpk@gmx.de | 1bf5d87 | 2017-06-29 21:16:19 +0200 | [diff] [blame] | 3213 | NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL); |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3214 | } |
| 3215 | |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3216 | /** |
Mario Six | 8fac291 | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3221 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3222 | * Return: status code |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3223 | */ |
Heinrich Schuchardt | 760255f | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3252 | /** |
Mario Six | 8fac291 | 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 | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 3256 | * @remain_device_path: remaining path |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3257 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3258 | * Return: status code |
Heinrich Schuchardt | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3259 | */ |
Heinrich Schuchardt | 760255f | 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 | da8f900 | 2019-07-03 20:27:24 +0200 | [diff] [blame] | 3278 | /* Context Override */ |
Heinrich Schuchardt | 760255f | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3324 | /** |
Mario Six | 8fac291 | 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 | 760255f | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3330 | * |
| 3331 | * This function implements the ConnectController service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3332 | * |
| 3333 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 3334 | * details. |
Heinrich Schuchardt | 760255f | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3335 | * |
| 3336 | * First all driver binding protocol handles are tried for binding drivers. |
Heinrich Schuchardt | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 3337 | * Afterwards all handles that have opened a protocol of the controller |
Heinrich Schuchardt | 760255f | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3338 | * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers. |
| 3339 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3340 | * Return: status code |
Heinrich Schuchardt | 760255f | 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 | 7c89fb0 | 2018-12-09 16:39:20 +0100 | [diff] [blame] | 3352 | EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle, |
Heinrich Schuchardt | 760255f | 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 | da8f900 | 2019-07-03 20:27:24 +0200 | [diff] [blame] | 3385 | /* Check for child controller specified by end node */ |
Heinrich Schuchardt | 760255f | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3393 | /** |
Mario Six | 8fac291 | 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 | 90761b8 | 2018-05-11 12:09:22 +0200 | [diff] [blame] | 3399 | * |
| 3400 | * This function implements the ReinstallProtocolInterface service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3401 | * |
| 3402 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 3403 | * details. |
Heinrich Schuchardt | 90761b8 | 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 | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3408 | * Return: status code |
Heinrich Schuchardt | 90761b8 | 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 | 1b45134 | 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 | 90761b8 | 2018-05-11 12:09:22 +0200 | [diff] [blame] | 3421 | if (ret != EFI_SUCCESS) |
| 3422 | goto out; |
Heinrich Schuchardt | 1b45134 | 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 | 90761b8 | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3442 | /** |
Mario Six | 8fac291 | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3448 | * |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3449 | * The allocated buffer has to be freed with free(). |
| 3450 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3451 | * Return: status code |
Heinrich Schuchardt | e994328 | 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 | 0f321b6 | 2020-07-07 04:21:26 +0200 | [diff] [blame] | 3478 | if (!count) |
| 3479 | return EFI_SUCCESS; |
Heinrich Schuchardt | e994328 | 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 | 5999917 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3508 | /** |
Mario Six | 8fac291 | 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 | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3513 | * |
| 3514 | * This function implements the DisconnectController service. |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3515 | * |
| 3516 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 3517 | * details. |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3518 | * |
Mario Six | 8fac291 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3519 | * Return: status code |
Heinrich Schuchardt | e994328 | 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 | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3530 | struct efi_object *efiobj; |
Heinrich Schuchardt | c7ce404 | 2020-10-28 18:45:47 +0100 | [diff] [blame] | 3531 | bool sole_child; |
Heinrich Schuchardt | e994328 | 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 | c7ce404 | 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 | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3562 | if (child_handle) { |
| 3563 | number_of_children = 1; |
Heinrich Schuchardt | c7ce404 | 2020-10-28 18:45:47 +0100 | [diff] [blame] | 3564 | free(child_handle_buffer); |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3565 | child_handle_buffer = &child_handle; |
Heinrich Schuchardt | e994328 | 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 | 7962284 | 2019-09-13 18:20:40 +0200 | [diff] [blame] | 3574 | if (r != EFI_SUCCESS) { |
| 3575 | r = EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3576 | goto out; |
Heinrich Schuchardt | 7962284 | 2019-09-13 18:20:40 +0200 | [diff] [blame] | 3577 | } |
Heinrich Schuchardt | e994328 | 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 | 7962284 | 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 | e994328 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3588 | } |
| 3589 | /* Remove the driver */ |
Heinrich Schuchardt | c7ce404 | 2020-10-28 18:45:47 +0100 | [diff] [blame] | 3590 | if (!child_handle || sole_child) { |
Heinrich Schuchardt | e994328 | 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 | 7962284 | 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 | e994328 | 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 | 7962284 | 2019-09-13 18:20:40 +0200 | [diff] [blame] | 3602 | r = EFI_SUCCESS; |
Heinrich Schuchardt | e994328 | 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 | 15070db | 2018-06-28 12:45:31 +0200 | [diff] [blame] | 3609 | static struct efi_boot_services efi_boot_services = { |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3610 | .hdr = { |
Heinrich Schuchardt | e75b3cb | 2018-06-28 12:45:27 +0200 | [diff] [blame] | 3611 | .signature = EFI_BOOT_SERVICES_SIGNATURE, |
| 3612 | .revision = EFI_SPECIFICATION_VERSION, |
Heinrich Schuchardt | 1020425 | 2018-06-28 12:45:29 +0200 | [diff] [blame] | 3613 | .headersize = sizeof(struct efi_boot_services), |
Alexander Graf | c15d921 | 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 | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 3620 | .allocate_pool = efi_allocate_pool_ext, |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 3621 | .free_pool = efi_free_pool_ext, |
xypron.glpk@gmx.de | 852a0e177 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 3622 | .create_event = efi_create_event_ext, |
xypron.glpk@gmx.de | a587fd1 | 2017-07-18 20:17:21 +0200 | [diff] [blame] | 3623 | .set_timer = efi_set_timer_ext, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3624 | .wait_for_event = efi_wait_for_event, |
xypron.glpk@gmx.de | 3070823 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 3625 | .signal_event = efi_signal_event_ext, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3626 | .close_event = efi_close_event, |
| 3627 | .check_event = efi_check_event, |
Heinrich Schuchardt | 0a27ac8 | 2017-11-06 21:17:44 +0100 | [diff] [blame] | 3628 | .install_protocol_interface = efi_install_protocol_interface, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3629 | .reinstall_protocol_interface = efi_reinstall_protocol_interface, |
Heinrich Schuchardt | 7cdc17f | 2017-11-06 21:17:45 +0100 | [diff] [blame] | 3630 | .uninstall_protocol_interface = efi_uninstall_protocol_interface, |
Alexander Graf | c15d921 | 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 | 69f9403 | 2017-07-11 22:06:21 +0200 | [diff] [blame] | 3634 | .locate_handle = efi_locate_handle_ext, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3635 | .locate_device_path = efi_locate_device_path, |
Alexander Graf | c5c1163 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 3636 | .install_configuration_table = efi_install_configuration_table_ext, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3637 | .load_image = efi_load_image, |
| 3638 | .start_image = efi_start_image, |
Alexander Graf | 988c066 | 2016-05-20 23:28:23 +0200 | [diff] [blame] | 3639 | .exit = efi_exit, |
Alexander Graf | c15d921 | 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 | 9106459 | 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 | c15d921 | 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 | 717c458 | 2018-02-04 23:05:13 +0100 | [diff] [blame] | 3660 | .create_event_ex = efi_create_event_ex, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3661 | }; |
| 3662 | |
Heinrich Schuchardt | 27685f7 | 2018-06-28 12:45:30 +0200 | [diff] [blame] | 3663 | static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot"; |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3664 | |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 3665 | struct efi_system_table __efi_runtime_data systab = { |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3666 | .hdr = { |
| 3667 | .signature = EFI_SYSTEM_TABLE_SIGNATURE, |
Heinrich Schuchardt | e75b3cb | 2018-06-28 12:45:27 +0200 | [diff] [blame] | 3668 | .revision = EFI_SPECIFICATION_VERSION, |
Heinrich Schuchardt | 1020425 | 2018-06-28 12:45:29 +0200 | [diff] [blame] | 3669 | .headersize = sizeof(struct efi_system_table), |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3670 | }, |
Heinrich Schuchardt | 27685f7 | 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 | 372b893 | 2019-06-15 14:51:06 +0200 | [diff] [blame] | 3673 | .runtime = &efi_runtime_services, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3674 | .nr_tables = 0, |
Heinrich Schuchardt | 2f528c2 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 3675 | .tables = NULL, |
Alexander Graf | c15d921 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3676 | }; |
Heinrich Schuchardt | 15070db | 2018-06-28 12:45:31 +0200 | [diff] [blame] | 3677 | |
| 3678 | /** |
| 3679 | * efi_initialize_system_table() - Initialize system table |
| 3680 | * |
Heinrich Schuchardt | 7b4b2a2 | 2018-09-03 05:00:43 +0200 | [diff] [blame] | 3681 | * Return: status code |
Heinrich Schuchardt | 15070db | 2018-06-28 12:45:31 +0200 | [diff] [blame] | 3682 | */ |
| 3683 | efi_status_t efi_initialize_system_table(void) |
| 3684 | { |
Heinrich Schuchardt | 2f528c2 | 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 | 98f8936 | 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 | cf70a73 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 3702 | /* Set CRC32 field in table headers */ |
Heinrich Schuchardt | 15070db | 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 | 2f528c2 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 3706 | |
| 3707 | return ret; |
Heinrich Schuchardt | 15070db | 2018-06-28 12:45:31 +0200 | [diff] [blame] | 3708 | } |