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