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