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