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