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