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