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