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