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