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