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