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