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