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