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