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