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> |
| 10 | #include <efi_loader.h> |
| 11 | #include <malloc.h> |
| 12 | #include <asm/global_data.h> |
| 13 | #include <libfdt_env.h> |
| 14 | #include <u-boot/crc.h> |
| 15 | #include <bootm.h> |
| 16 | #include <inttypes.h> |
| 17 | #include <watchdog.h> |
| 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
| 21 | /* This list contains all the EFI objects our payload has access to */ |
| 22 | LIST_HEAD(efi_obj_list); |
| 23 | |
| 24 | /* |
| 25 | * If we're running on nasty systems (32bit ARM booting into non-EFI Linux) |
| 26 | * we need to do trickery with caches. Since we don't want to break the EFI |
| 27 | * aware boot path, only apply hacks when loading exiting directly (breaking |
| 28 | * direct Linux EFI booting along the way - oh well). |
| 29 | */ |
| 30 | static bool efi_is_direct_boot = true; |
| 31 | |
| 32 | /* |
| 33 | * EFI can pass arbitrary additional "tables" containing vendor specific |
| 34 | * information to the payload. One such table is the FDT table which contains |
| 35 | * a pointer to a flattened device tree blob. |
| 36 | * |
| 37 | * In most cases we want to pass an FDT to the payload, so reserve one slot of |
| 38 | * config table space for it. The pointer gets populated by do_bootefi_exec(). |
| 39 | */ |
Alexander Graf | 3c63db9 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 40 | static struct efi_configuration_table __efi_runtime_data efi_conf_table[2]; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 41 | |
Simon Glass | 65e4c0b | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 42 | #ifdef CONFIG_ARM |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 43 | /* |
| 44 | * The "gd" pointer lives in a register on ARM and AArch64 that we declare |
| 45 | * fixed when compiling U-Boot. However, the payload does not know about that |
| 46 | * restriction so we need to manually swap its and our view of that register on |
| 47 | * EFI callback entry/exit. |
| 48 | */ |
| 49 | static volatile void *efi_gd, *app_gd; |
Simon Glass | 65e4c0b | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 50 | #endif |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 51 | |
| 52 | /* Called from do_bootefi_exec() */ |
| 53 | void efi_save_gd(void) |
| 54 | { |
Simon Glass | 65e4c0b | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 55 | #ifdef CONFIG_ARM |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 56 | efi_gd = gd; |
Simon Glass | 65e4c0b | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 57 | #endif |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /* Called on every callback entry */ |
| 61 | void efi_restore_gd(void) |
| 62 | { |
Simon Glass | 65e4c0b | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 63 | #ifdef CONFIG_ARM |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 64 | /* Only restore if we're already in EFI context */ |
| 65 | if (!efi_gd) |
| 66 | return; |
| 67 | |
| 68 | if (gd != efi_gd) |
| 69 | app_gd = gd; |
| 70 | gd = efi_gd; |
Simon Glass | 65e4c0b | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 71 | #endif |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* Called on every callback exit */ |
| 75 | efi_status_t efi_exit_func(efi_status_t ret) |
| 76 | { |
Simon Glass | 65e4c0b | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 77 | #ifdef CONFIG_ARM |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 78 | gd = app_gd; |
Simon Glass | 65e4c0b | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 79 | #endif |
| 80 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | static efi_status_t efi_unsupported(const char *funcname) |
| 85 | { |
Alexander Graf | edcef3b | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 86 | debug("EFI: App called into unimplemented function %s\n", funcname); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 87 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 88 | } |
| 89 | |
| 90 | static int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2) |
| 91 | { |
| 92 | return memcmp(g1, g2, sizeof(efi_guid_t)); |
| 93 | } |
| 94 | |
| 95 | static unsigned long EFIAPI efi_raise_tpl(unsigned long new_tpl) |
| 96 | { |
| 97 | EFI_ENTRY("0x%lx", new_tpl); |
| 98 | return EFI_EXIT(0); |
| 99 | } |
| 100 | |
| 101 | static void EFIAPI efi_restore_tpl(unsigned long old_tpl) |
| 102 | { |
| 103 | EFI_ENTRY("0x%lx", old_tpl); |
| 104 | EFI_EXIT(efi_unsupported(__func__)); |
| 105 | } |
| 106 | |
Masahiro Yamada | 6e0bf8d | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 107 | static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type, |
| 108 | unsigned long pages, |
| 109 | uint64_t *memory) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 110 | { |
| 111 | efi_status_t r; |
| 112 | |
| 113 | EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory); |
| 114 | r = efi_allocate_pages(type, memory_type, pages, memory); |
| 115 | return EFI_EXIT(r); |
| 116 | } |
| 117 | |
Masahiro Yamada | 6e0bf8d | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 118 | static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory, |
| 119 | unsigned long pages) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 120 | { |
| 121 | efi_status_t r; |
| 122 | |
| 123 | EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages); |
| 124 | r = efi_free_pages(memory, pages); |
| 125 | return EFI_EXIT(r); |
| 126 | } |
| 127 | |
Masahiro Yamada | 6e0bf8d | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 128 | static efi_status_t EFIAPI efi_get_memory_map_ext( |
| 129 | unsigned long *memory_map_size, |
| 130 | struct efi_mem_desc *memory_map, |
| 131 | unsigned long *map_key, |
| 132 | unsigned long *descriptor_size, |
| 133 | uint32_t *descriptor_version) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 134 | { |
| 135 | efi_status_t r; |
| 136 | |
| 137 | EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map, |
| 138 | map_key, descriptor_size, descriptor_version); |
| 139 | r = efi_get_memory_map(memory_map_size, memory_map, map_key, |
| 140 | descriptor_size, descriptor_version); |
| 141 | return EFI_EXIT(r); |
| 142 | } |
| 143 | |
Stefan Brüns | ead1274 | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 144 | static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type, |
| 145 | unsigned long size, |
| 146 | void **buffer) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 147 | { |
Alexander Graf | 1cd29f0 | 2016-03-24 01:37:37 +0100 | [diff] [blame] | 148 | efi_status_t r; |
| 149 | |
| 150 | EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer); |
Stefan Brüns | ead1274 | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 151 | r = efi_allocate_pool(pool_type, size, buffer); |
Alexander Graf | 1cd29f0 | 2016-03-24 01:37:37 +0100 | [diff] [blame] | 152 | return EFI_EXIT(r); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 155 | static efi_status_t EFIAPI efi_free_pool_ext(void *buffer) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 156 | { |
Alexander Graf | 1cd29f0 | 2016-03-24 01:37:37 +0100 | [diff] [blame] | 157 | efi_status_t r; |
| 158 | |
| 159 | EFI_ENTRY("%p", buffer); |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 160 | r = efi_free_pool(buffer); |
Alexander Graf | 1cd29f0 | 2016-03-24 01:37:37 +0100 | [diff] [blame] | 161 | return EFI_EXIT(r); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Our event capabilities are very limited. Only support a single |
| 166 | * event to exist, so we don't need to maintain lists. |
| 167 | */ |
| 168 | static struct { |
| 169 | enum efi_event_type type; |
| 170 | u32 trigger_type; |
| 171 | u32 trigger_time; |
| 172 | u64 trigger_next; |
| 173 | unsigned long notify_tpl; |
Simon Glass | e275458 | 2016-09-25 15:27:32 -0600 | [diff] [blame] | 174 | void (EFIAPI *notify_function) (void *event, void *context); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 175 | void *notify_context; |
| 176 | } efi_event = { |
| 177 | /* Disable timers on bootup */ |
| 178 | .trigger_next = -1ULL, |
| 179 | }; |
| 180 | |
| 181 | static efi_status_t EFIAPI efi_create_event( |
| 182 | enum efi_event_type type, ulong notify_tpl, |
Simon Glass | e275458 | 2016-09-25 15:27:32 -0600 | [diff] [blame] | 183 | void (EFIAPI *notify_function) (void *event, |
| 184 | void *context), |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 185 | void *notify_context, void **event) |
| 186 | { |
| 187 | EFI_ENTRY("%d, 0x%lx, %p, %p", type, notify_tpl, notify_function, |
| 188 | notify_context); |
| 189 | if (efi_event.notify_function) { |
| 190 | /* We only support one event at a time */ |
| 191 | return EFI_EXIT(EFI_OUT_OF_RESOURCES); |
| 192 | } |
| 193 | |
Jonathan Gray | a95343b | 2017-03-12 19:26:07 +1100 | [diff] [blame] | 194 | if (event == NULL) |
| 195 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 196 | |
| 197 | if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT)) |
| 198 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 199 | |
| 200 | if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) && |
| 201 | notify_function == NULL) |
| 202 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 203 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 204 | efi_event.type = type; |
| 205 | efi_event.notify_tpl = notify_tpl; |
| 206 | efi_event.notify_function = notify_function; |
| 207 | efi_event.notify_context = notify_context; |
| 208 | *event = &efi_event; |
| 209 | |
| 210 | return EFI_EXIT(EFI_SUCCESS); |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Our timers have to work without interrupts, so we check whenever keyboard |
| 215 | * input or disk accesses happen if enough time elapsed for it to fire. |
| 216 | */ |
| 217 | void efi_timer_check(void) |
| 218 | { |
| 219 | u64 now = timer_get_us(); |
| 220 | |
| 221 | if (now >= efi_event.trigger_next) { |
| 222 | /* Triggering! */ |
| 223 | if (efi_event.trigger_type == EFI_TIMER_PERIODIC) |
| 224 | efi_event.trigger_next += efi_event.trigger_time / 10; |
Jonathan Gray | 37a980b | 2017-03-12 19:26:06 +1100 | [diff] [blame] | 225 | if (efi_event.type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) |
| 226 | efi_event.notify_function(&efi_event, |
| 227 | efi_event.notify_context); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | WATCHDOG_RESET(); |
| 231 | } |
| 232 | |
| 233 | static efi_status_t EFIAPI efi_set_timer(void *event, int type, |
| 234 | uint64_t trigger_time) |
| 235 | { |
| 236 | /* We don't have 64bit division available everywhere, so limit timer |
| 237 | * distances to 32bit bits. */ |
| 238 | u32 trigger32 = trigger_time; |
| 239 | |
| 240 | EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time); |
| 241 | |
| 242 | if (trigger32 < trigger_time) { |
| 243 | printf("WARNING: Truncating timer from %"PRIx64" to %x\n", |
| 244 | trigger_time, trigger32); |
| 245 | } |
| 246 | |
| 247 | if (event != &efi_event) { |
| 248 | /* We only support one event at a time */ |
| 249 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 250 | } |
| 251 | |
| 252 | switch (type) { |
| 253 | case EFI_TIMER_STOP: |
| 254 | efi_event.trigger_next = -1ULL; |
| 255 | break; |
| 256 | case EFI_TIMER_PERIODIC: |
| 257 | case EFI_TIMER_RELATIVE: |
| 258 | efi_event.trigger_next = timer_get_us() + (trigger32 / 10); |
| 259 | break; |
| 260 | default: |
| 261 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 262 | } |
| 263 | efi_event.trigger_type = type; |
| 264 | efi_event.trigger_time = trigger_time; |
| 265 | |
| 266 | return EFI_EXIT(EFI_SUCCESS); |
| 267 | } |
| 268 | |
| 269 | static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events, |
| 270 | void *event, unsigned long *index) |
| 271 | { |
| 272 | u64 now; |
| 273 | |
| 274 | EFI_ENTRY("%ld, %p, %p", num_events, event, index); |
| 275 | |
| 276 | now = timer_get_us(); |
| 277 | while (now < efi_event.trigger_next) { } |
| 278 | efi_timer_check(); |
| 279 | |
| 280 | return EFI_EXIT(EFI_SUCCESS); |
| 281 | } |
| 282 | |
| 283 | static efi_status_t EFIAPI efi_signal_event(void *event) |
| 284 | { |
| 285 | EFI_ENTRY("%p", event); |
| 286 | return EFI_EXIT(EFI_SUCCESS); |
| 287 | } |
| 288 | |
| 289 | static efi_status_t EFIAPI efi_close_event(void *event) |
| 290 | { |
| 291 | EFI_ENTRY("%p", event); |
| 292 | efi_event.trigger_next = -1ULL; |
| 293 | return EFI_EXIT(EFI_SUCCESS); |
| 294 | } |
| 295 | |
| 296 | static efi_status_t EFIAPI efi_check_event(void *event) |
| 297 | { |
| 298 | EFI_ENTRY("%p", event); |
| 299 | return EFI_EXIT(EFI_NOT_READY); |
| 300 | } |
| 301 | |
| 302 | static efi_status_t EFIAPI efi_install_protocol_interface(void **handle, |
| 303 | efi_guid_t *protocol, int protocol_interface_type, |
| 304 | void *protocol_interface) |
| 305 | { |
| 306 | EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type, |
| 307 | protocol_interface); |
| 308 | return EFI_EXIT(EFI_OUT_OF_RESOURCES); |
| 309 | } |
| 310 | static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle, |
| 311 | efi_guid_t *protocol, void *old_interface, |
| 312 | void *new_interface) |
| 313 | { |
| 314 | EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface, |
| 315 | new_interface); |
| 316 | return EFI_EXIT(EFI_ACCESS_DENIED); |
| 317 | } |
| 318 | |
| 319 | static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle, |
| 320 | efi_guid_t *protocol, void *protocol_interface) |
| 321 | { |
| 322 | EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface); |
| 323 | return EFI_EXIT(EFI_NOT_FOUND); |
| 324 | } |
| 325 | |
| 326 | static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol, |
| 327 | void *event, |
| 328 | void **registration) |
| 329 | { |
| 330 | EFI_ENTRY("%p, %p, %p", protocol, event, registration); |
| 331 | return EFI_EXIT(EFI_OUT_OF_RESOURCES); |
| 332 | } |
| 333 | |
| 334 | static int efi_search(enum efi_locate_search_type search_type, |
| 335 | efi_guid_t *protocol, void *search_key, |
| 336 | struct efi_object *efiobj) |
| 337 | { |
| 338 | int i; |
| 339 | |
| 340 | switch (search_type) { |
| 341 | case all_handles: |
| 342 | return 0; |
| 343 | case by_register_notify: |
| 344 | return -1; |
| 345 | case by_protocol: |
| 346 | for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) { |
| 347 | const efi_guid_t *guid = efiobj->protocols[i].guid; |
| 348 | if (guid && !guidcmp(guid, protocol)) |
| 349 | return 0; |
| 350 | } |
| 351 | return -1; |
| 352 | } |
| 353 | |
| 354 | return -1; |
| 355 | } |
| 356 | |
| 357 | static efi_status_t EFIAPI efi_locate_handle( |
| 358 | enum efi_locate_search_type search_type, |
| 359 | efi_guid_t *protocol, void *search_key, |
| 360 | unsigned long *buffer_size, efi_handle_t *buffer) |
| 361 | { |
| 362 | struct list_head *lhandle; |
| 363 | unsigned long size = 0; |
| 364 | |
| 365 | EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key, |
| 366 | buffer_size, buffer); |
| 367 | |
| 368 | /* Count how much space we need */ |
| 369 | list_for_each(lhandle, &efi_obj_list) { |
| 370 | struct efi_object *efiobj; |
| 371 | efiobj = list_entry(lhandle, struct efi_object, link); |
| 372 | if (!efi_search(search_type, protocol, search_key, efiobj)) { |
| 373 | size += sizeof(void*); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | if (*buffer_size < size) { |
| 378 | *buffer_size = size; |
| 379 | return EFI_EXIT(EFI_BUFFER_TOO_SMALL); |
| 380 | } |
| 381 | |
| 382 | /* Then fill the array */ |
| 383 | list_for_each(lhandle, &efi_obj_list) { |
| 384 | struct efi_object *efiobj; |
| 385 | efiobj = list_entry(lhandle, struct efi_object, link); |
| 386 | if (!efi_search(search_type, protocol, search_key, efiobj)) { |
| 387 | *(buffer++) = efiobj->handle; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | *buffer_size = size; |
| 392 | return EFI_EXIT(EFI_SUCCESS); |
| 393 | } |
| 394 | |
| 395 | static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol, |
| 396 | struct efi_device_path **device_path, |
| 397 | efi_handle_t *device) |
| 398 | { |
| 399 | EFI_ENTRY("%p, %p, %p", protocol, device_path, device); |
| 400 | return EFI_EXIT(EFI_NOT_FOUND); |
| 401 | } |
| 402 | |
Alexander Graf | 488bf12 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 403 | 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] | 404 | { |
| 405 | int i; |
| 406 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 407 | /* Check for guid override */ |
| 408 | for (i = 0; i < systab.nr_tables; i++) { |
| 409 | if (!guidcmp(guid, &efi_conf_table[i].guid)) { |
| 410 | efi_conf_table[i].table = table; |
Alexander Graf | 488bf12 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 411 | return EFI_SUCCESS; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
| 415 | /* No override, check for overflow */ |
| 416 | if (i >= ARRAY_SIZE(efi_conf_table)) |
Alexander Graf | 488bf12 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 417 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 418 | |
| 419 | /* Add a new entry */ |
| 420 | memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid)); |
| 421 | efi_conf_table[i].table = table; |
Alexander Graf | aba5e91 | 2016-08-19 01:23:30 +0200 | [diff] [blame] | 422 | systab.nr_tables = i + 1; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 423 | |
Alexander Graf | 488bf12 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 424 | return EFI_SUCCESS; |
| 425 | } |
| 426 | |
| 427 | static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid, |
| 428 | void *table) |
| 429 | { |
| 430 | EFI_ENTRY("%p, %p", guid, table); |
| 431 | return EFI_EXIT(efi_install_configuration_table(guid, table)); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static efi_status_t EFIAPI efi_load_image(bool boot_policy, |
| 435 | efi_handle_t parent_image, |
| 436 | struct efi_device_path *file_path, |
| 437 | void *source_buffer, |
| 438 | unsigned long source_size, |
| 439 | efi_handle_t *image_handle) |
| 440 | { |
| 441 | static struct efi_object loaded_image_info_obj = { |
| 442 | .protocols = { |
| 443 | { |
| 444 | .guid = &efi_guid_loaded_image, |
| 445 | .open = &efi_return_handle, |
| 446 | }, |
| 447 | }, |
| 448 | }; |
| 449 | struct efi_loaded_image *info; |
| 450 | struct efi_object *obj; |
| 451 | |
| 452 | EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image, |
| 453 | file_path, source_buffer, source_size, image_handle); |
| 454 | info = malloc(sizeof(*info)); |
| 455 | obj = malloc(sizeof(loaded_image_info_obj)); |
| 456 | memset(info, 0, sizeof(*info)); |
| 457 | memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj)); |
| 458 | obj->handle = info; |
| 459 | info->file_path = file_path; |
| 460 | info->reserved = efi_load_pe(source_buffer, info); |
| 461 | if (!info->reserved) { |
| 462 | free(info); |
| 463 | free(obj); |
| 464 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 465 | } |
| 466 | |
| 467 | *image_handle = info; |
| 468 | list_add_tail(&obj->link, &efi_obj_list); |
| 469 | |
| 470 | return EFI_EXIT(EFI_SUCCESS); |
| 471 | } |
| 472 | |
| 473 | static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle, |
| 474 | unsigned long *exit_data_size, |
| 475 | s16 **exit_data) |
| 476 | { |
| 477 | ulong (*entry)(void *image_handle, struct efi_system_table *st); |
| 478 | struct efi_loaded_image *info = image_handle; |
| 479 | |
| 480 | EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data); |
| 481 | entry = info->reserved; |
| 482 | |
| 483 | efi_is_direct_boot = false; |
| 484 | |
| 485 | /* call the image! */ |
Alexander Graf | a86aeaf | 2016-05-20 23:28:23 +0200 | [diff] [blame] | 486 | if (setjmp(&info->exit_jmp)) { |
| 487 | /* We returned from the child image */ |
| 488 | return EFI_EXIT(info->exit_status); |
| 489 | } |
| 490 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 491 | entry(image_handle, &systab); |
| 492 | |
| 493 | /* Should usually never get here */ |
| 494 | return EFI_EXIT(EFI_SUCCESS); |
| 495 | } |
| 496 | |
Alexander Graf | a86aeaf | 2016-05-20 23:28:23 +0200 | [diff] [blame] | 497 | static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle, |
| 498 | efi_status_t exit_status, unsigned long exit_data_size, |
| 499 | int16_t *exit_data) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 500 | { |
Alexander Graf | a86aeaf | 2016-05-20 23:28:23 +0200 | [diff] [blame] | 501 | struct efi_loaded_image *loaded_image_info = (void*)image_handle; |
| 502 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 503 | EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status, |
| 504 | exit_data_size, exit_data); |
Alexander Graf | a86aeaf | 2016-05-20 23:28:23 +0200 | [diff] [blame] | 505 | |
| 506 | loaded_image_info->exit_status = exit_status; |
Alexander Graf | 692fcdd | 2016-09-27 09:30:32 +0200 | [diff] [blame] | 507 | longjmp(&loaded_image_info->exit_jmp, 1); |
Alexander Graf | a86aeaf | 2016-05-20 23:28:23 +0200 | [diff] [blame] | 508 | |
| 509 | panic("EFI application exited"); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | static struct efi_object *efi_search_obj(void *handle) |
| 513 | { |
| 514 | struct list_head *lhandle; |
| 515 | |
| 516 | list_for_each(lhandle, &efi_obj_list) { |
| 517 | struct efi_object *efiobj; |
| 518 | efiobj = list_entry(lhandle, struct efi_object, link); |
| 519 | if (efiobj->handle == handle) |
| 520 | return efiobj; |
| 521 | } |
| 522 | |
| 523 | return NULL; |
| 524 | } |
| 525 | |
| 526 | static efi_status_t EFIAPI efi_unload_image(void *image_handle) |
| 527 | { |
| 528 | struct efi_object *efiobj; |
| 529 | |
| 530 | EFI_ENTRY("%p", image_handle); |
| 531 | efiobj = efi_search_obj(image_handle); |
| 532 | if (efiobj) |
| 533 | list_del(&efiobj->link); |
| 534 | |
| 535 | return EFI_EXIT(EFI_SUCCESS); |
| 536 | } |
| 537 | |
| 538 | static void efi_exit_caches(void) |
| 539 | { |
| 540 | #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64) |
| 541 | /* |
| 542 | * Grub on 32bit ARM needs to have caches disabled before jumping into |
| 543 | * a zImage, but does not know of all cache layers. Give it a hand. |
| 544 | */ |
| 545 | if (efi_is_direct_boot) |
| 546 | cleanup_before_linux(); |
| 547 | #endif |
| 548 | } |
| 549 | |
| 550 | static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle, |
| 551 | unsigned long map_key) |
| 552 | { |
| 553 | EFI_ENTRY("%p, %ld", image_handle, map_key); |
| 554 | |
Alexander Graf | b7b8410 | 2016-11-17 01:02:57 +0100 | [diff] [blame] | 555 | board_quiesce_devices(); |
| 556 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 557 | /* Fix up caches for EFI payloads if necessary */ |
| 558 | efi_exit_caches(); |
| 559 | |
| 560 | /* This stops all lingering devices */ |
| 561 | bootm_disable_interrupts(); |
| 562 | |
| 563 | /* Give the payload some time to boot */ |
| 564 | WATCHDOG_RESET(); |
| 565 | |
| 566 | return EFI_EXIT(EFI_SUCCESS); |
| 567 | } |
| 568 | |
| 569 | static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count) |
| 570 | { |
| 571 | static uint64_t mono = 0; |
| 572 | EFI_ENTRY("%p", count); |
| 573 | *count = mono++; |
| 574 | return EFI_EXIT(EFI_SUCCESS); |
| 575 | } |
| 576 | |
| 577 | static efi_status_t EFIAPI efi_stall(unsigned long microseconds) |
| 578 | { |
| 579 | EFI_ENTRY("%ld", microseconds); |
| 580 | udelay(microseconds); |
| 581 | return EFI_EXIT(EFI_SUCCESS); |
| 582 | } |
| 583 | |
| 584 | static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout, |
| 585 | uint64_t watchdog_code, |
| 586 | unsigned long data_size, |
| 587 | uint16_t *watchdog_data) |
| 588 | { |
| 589 | EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code, |
| 590 | data_size, watchdog_data); |
| 591 | return EFI_EXIT(efi_unsupported(__func__)); |
| 592 | } |
| 593 | |
| 594 | static efi_status_t EFIAPI efi_connect_controller( |
| 595 | efi_handle_t controller_handle, |
| 596 | efi_handle_t *driver_image_handle, |
| 597 | struct efi_device_path *remain_device_path, |
| 598 | bool recursive) |
| 599 | { |
| 600 | EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle, |
| 601 | remain_device_path, recursive); |
| 602 | return EFI_EXIT(EFI_NOT_FOUND); |
| 603 | } |
| 604 | |
| 605 | static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle, |
| 606 | void *driver_image_handle, |
| 607 | void *child_handle) |
| 608 | { |
| 609 | EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle, |
| 610 | child_handle); |
| 611 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 612 | } |
| 613 | |
| 614 | static efi_status_t EFIAPI efi_close_protocol(void *handle, |
| 615 | efi_guid_t *protocol, |
| 616 | void *agent_handle, |
| 617 | void *controller_handle) |
| 618 | { |
| 619 | EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle, |
| 620 | controller_handle); |
| 621 | return EFI_EXIT(EFI_NOT_FOUND); |
| 622 | } |
| 623 | |
| 624 | static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle, |
| 625 | efi_guid_t *protocol, |
| 626 | struct efi_open_protocol_info_entry **entry_buffer, |
| 627 | unsigned long *entry_count) |
| 628 | { |
| 629 | EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer, |
| 630 | entry_count); |
| 631 | return EFI_EXIT(EFI_NOT_FOUND); |
| 632 | } |
| 633 | |
| 634 | static efi_status_t EFIAPI efi_protocols_per_handle(void *handle, |
| 635 | efi_guid_t ***protocol_buffer, |
| 636 | unsigned long *protocol_buffer_count) |
| 637 | { |
| 638 | EFI_ENTRY("%p, %p, %p", handle, protocol_buffer, |
| 639 | protocol_buffer_count); |
| 640 | return EFI_EXIT(EFI_OUT_OF_RESOURCES); |
| 641 | } |
| 642 | |
| 643 | static efi_status_t EFIAPI efi_locate_handle_buffer( |
| 644 | enum efi_locate_search_type search_type, |
| 645 | efi_guid_t *protocol, void *search_key, |
| 646 | unsigned long *no_handles, efi_handle_t **buffer) |
| 647 | { |
| 648 | EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key, |
| 649 | no_handles, buffer); |
| 650 | return EFI_EXIT(EFI_NOT_FOUND); |
| 651 | } |
| 652 | |
| 653 | static struct efi_class_map efi_class_maps[] = { |
| 654 | { |
| 655 | .guid = &efi_guid_console_control, |
| 656 | .interface = &efi_console_control |
| 657 | }, |
| 658 | }; |
| 659 | |
| 660 | static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol, |
| 661 | void *registration, |
| 662 | void **protocol_interface) |
| 663 | { |
| 664 | int i; |
| 665 | |
| 666 | EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface); |
| 667 | for (i = 0; i < ARRAY_SIZE(efi_class_maps); i++) { |
| 668 | struct efi_class_map *curmap = &efi_class_maps[i]; |
| 669 | if (!guidcmp(protocol, curmap->guid)) { |
| 670 | *protocol_interface = (void*)curmap->interface; |
| 671 | return EFI_EXIT(EFI_SUCCESS); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | return EFI_EXIT(EFI_NOT_FOUND); |
| 676 | } |
| 677 | |
| 678 | static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces( |
| 679 | void **handle, ...) |
| 680 | { |
| 681 | EFI_ENTRY("%p", handle); |
| 682 | return EFI_EXIT(EFI_OUT_OF_RESOURCES); |
| 683 | } |
| 684 | |
| 685 | static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces( |
| 686 | void *handle, ...) |
| 687 | { |
| 688 | EFI_ENTRY("%p", handle); |
| 689 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 690 | } |
| 691 | |
| 692 | static efi_status_t EFIAPI efi_calculate_crc32(void *data, |
| 693 | unsigned long data_size, |
| 694 | uint32_t *crc32_p) |
| 695 | { |
| 696 | EFI_ENTRY("%p, %ld", data, data_size); |
| 697 | *crc32_p = crc32(0, data, data_size); |
| 698 | return EFI_EXIT(EFI_SUCCESS); |
| 699 | } |
| 700 | |
| 701 | static void EFIAPI efi_copy_mem(void *destination, void *source, |
| 702 | unsigned long length) |
| 703 | { |
| 704 | EFI_ENTRY("%p, %p, %ld", destination, source, length); |
| 705 | memcpy(destination, source, length); |
| 706 | } |
| 707 | |
| 708 | static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value) |
| 709 | { |
| 710 | EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value); |
| 711 | memset(buffer, value, size); |
| 712 | } |
| 713 | |
| 714 | static efi_status_t EFIAPI efi_open_protocol( |
| 715 | void *handle, efi_guid_t *protocol, |
| 716 | void **protocol_interface, void *agent_handle, |
| 717 | void *controller_handle, uint32_t attributes) |
| 718 | { |
| 719 | struct list_head *lhandle; |
| 720 | int i; |
| 721 | efi_status_t r = EFI_UNSUPPORTED; |
| 722 | |
| 723 | EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol, |
| 724 | protocol_interface, agent_handle, controller_handle, |
| 725 | attributes); |
| 726 | list_for_each(lhandle, &efi_obj_list) { |
| 727 | struct efi_object *efiobj; |
| 728 | efiobj = list_entry(lhandle, struct efi_object, link); |
| 729 | |
| 730 | if (efiobj->handle != handle) |
| 731 | continue; |
| 732 | |
| 733 | for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) { |
| 734 | struct efi_handler *handler = &efiobj->protocols[i]; |
| 735 | const efi_guid_t *hprotocol = handler->guid; |
| 736 | if (!hprotocol) |
| 737 | break; |
| 738 | if (!guidcmp(hprotocol, protocol)) { |
| 739 | r = handler->open(handle, protocol, |
| 740 | protocol_interface, agent_handle, |
| 741 | controller_handle, attributes); |
| 742 | goto out; |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | out: |
| 748 | return EFI_EXIT(r); |
| 749 | } |
| 750 | |
| 751 | static efi_status_t EFIAPI efi_handle_protocol(void *handle, |
| 752 | efi_guid_t *protocol, |
| 753 | void **protocol_interface) |
| 754 | { |
xypron.glpk@gmx.de | 8e1d329 | 2017-06-29 21:16:19 +0200 | [diff] [blame] | 755 | return efi_open_protocol(handle, protocol, protocol_interface, NULL, |
| 756 | NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | static const struct efi_boot_services efi_boot_services = { |
| 760 | .hdr = { |
| 761 | .headersize = sizeof(struct efi_table_hdr), |
| 762 | }, |
| 763 | .raise_tpl = efi_raise_tpl, |
| 764 | .restore_tpl = efi_restore_tpl, |
| 765 | .allocate_pages = efi_allocate_pages_ext, |
| 766 | .free_pages = efi_free_pages_ext, |
| 767 | .get_memory_map = efi_get_memory_map_ext, |
Stefan Brüns | ead1274 | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 768 | .allocate_pool = efi_allocate_pool_ext, |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 769 | .free_pool = efi_free_pool_ext, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 770 | .create_event = efi_create_event, |
| 771 | .set_timer = efi_set_timer, |
| 772 | .wait_for_event = efi_wait_for_event, |
| 773 | .signal_event = efi_signal_event, |
| 774 | .close_event = efi_close_event, |
| 775 | .check_event = efi_check_event, |
| 776 | .install_protocol_interface = efi_install_protocol_interface, |
| 777 | .reinstall_protocol_interface = efi_reinstall_protocol_interface, |
| 778 | .uninstall_protocol_interface = efi_uninstall_protocol_interface, |
| 779 | .handle_protocol = efi_handle_protocol, |
| 780 | .reserved = NULL, |
| 781 | .register_protocol_notify = efi_register_protocol_notify, |
| 782 | .locate_handle = efi_locate_handle, |
| 783 | .locate_device_path = efi_locate_device_path, |
Alexander Graf | 488bf12 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 784 | .install_configuration_table = efi_install_configuration_table_ext, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 785 | .load_image = efi_load_image, |
| 786 | .start_image = efi_start_image, |
Alexander Graf | a86aeaf | 2016-05-20 23:28:23 +0200 | [diff] [blame] | 787 | .exit = efi_exit, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 788 | .unload_image = efi_unload_image, |
| 789 | .exit_boot_services = efi_exit_boot_services, |
| 790 | .get_next_monotonic_count = efi_get_next_monotonic_count, |
| 791 | .stall = efi_stall, |
| 792 | .set_watchdog_timer = efi_set_watchdog_timer, |
| 793 | .connect_controller = efi_connect_controller, |
| 794 | .disconnect_controller = efi_disconnect_controller, |
| 795 | .open_protocol = efi_open_protocol, |
| 796 | .close_protocol = efi_close_protocol, |
| 797 | .open_protocol_information = efi_open_protocol_information, |
| 798 | .protocols_per_handle = efi_protocols_per_handle, |
| 799 | .locate_handle_buffer = efi_locate_handle_buffer, |
| 800 | .locate_protocol = efi_locate_protocol, |
| 801 | .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces, |
| 802 | .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces, |
| 803 | .calculate_crc32 = efi_calculate_crc32, |
| 804 | .copy_mem = efi_copy_mem, |
| 805 | .set_mem = efi_set_mem, |
| 806 | }; |
| 807 | |
| 808 | |
Alexander Graf | 3c63db9 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 809 | static uint16_t __efi_runtime_data firmware_vendor[] = |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 810 | { 'D','a','s',' ','U','-','b','o','o','t',0 }; |
| 811 | |
Alexander Graf | 3c63db9 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 812 | struct efi_system_table __efi_runtime_data systab = { |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 813 | .hdr = { |
| 814 | .signature = EFI_SYSTEM_TABLE_SIGNATURE, |
| 815 | .revision = 0x20005, /* 2.5 */ |
| 816 | .headersize = sizeof(struct efi_table_hdr), |
| 817 | }, |
| 818 | .fw_vendor = (long)firmware_vendor, |
| 819 | .con_in = (void*)&efi_con_in, |
| 820 | .con_out = (void*)&efi_con_out, |
| 821 | .std_err = (void*)&efi_con_out, |
| 822 | .runtime = (void*)&efi_runtime_services, |
| 823 | .boottime = (void*)&efi_boot_services, |
| 824 | .nr_tables = 0, |
| 825 | .tables = (void*)efi_conf_table, |
| 826 | }; |