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