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