1cb149c66SAlexander Graf /* 2cb149c66SAlexander Graf * EFI application loader 3cb149c66SAlexander Graf * 4cb149c66SAlexander Graf * Copyright (c) 2016 Alexander Graf 5cb149c66SAlexander Graf * 6cb149c66SAlexander Graf * SPDX-License-Identifier: GPL-2.0+ 7cb149c66SAlexander Graf */ 8cb149c66SAlexander Graf 9bee91169SAlexander Graf #include <common.h> 10cb149c66SAlexander Graf #include <part_efi.h> 11cb149c66SAlexander Graf #include <efi_api.h> 12bee91169SAlexander Graf 13bee91169SAlexander Graf /* No need for efi loader support in SPL */ 14bee91169SAlexander Graf #if defined(CONFIG_EFI_LOADER) && !defined(CONFIG_SPL_BUILD) 15bee91169SAlexander Graf 16cb149c66SAlexander Graf #include <linux/list.h> 17cb149c66SAlexander Graf 18bee91169SAlexander Graf #define EFI_ENTRY(format, ...) do { \ 19bee91169SAlexander Graf efi_restore_gd(); \ 20edcef3baSAlexander Graf debug("EFI: Entry %s(" format ")\n", __func__, ##__VA_ARGS__); \ 21bee91169SAlexander Graf } while(0) 22bee91169SAlexander Graf 23bee91169SAlexander Graf #define EFI_EXIT(ret) efi_exit_func(ret); 24bee91169SAlexander Graf 2550149ea3SAlexander Graf extern struct efi_runtime_services efi_runtime_services; 26bee91169SAlexander Graf extern struct efi_system_table systab; 27bee91169SAlexander Graf 28c1311ad4SAlexander Graf extern const struct efi_simple_text_output_protocol efi_con_out; 29c1311ad4SAlexander Graf extern const struct efi_simple_input_interface efi_con_in; 30c1311ad4SAlexander Graf extern const struct efi_console_control_protocol efi_console_control; 31c1311ad4SAlexander Graf 32c1311ad4SAlexander Graf extern const efi_guid_t efi_guid_console_control; 33cb149c66SAlexander Graf extern const efi_guid_t efi_guid_device_path; 34cb149c66SAlexander Graf extern const efi_guid_t efi_guid_loaded_image; 35cb149c66SAlexander Graf 3650149ea3SAlexander Graf extern unsigned int __efi_runtime_start, __efi_runtime_stop; 3750149ea3SAlexander Graf extern unsigned int __efi_runtime_rel_start, __efi_runtime_rel_stop; 3850149ea3SAlexander Graf 39bee91169SAlexander Graf /* 40bee91169SAlexander Graf * While UEFI objects can have callbacks, you can also call functions on 41bee91169SAlexander Graf * protocols (classes) themselves. This struct maps a protocol GUID to its 42bee91169SAlexander Graf * interface (usually a struct with callback functions). 43bee91169SAlexander Graf */ 44bee91169SAlexander Graf struct efi_class_map { 45bee91169SAlexander Graf const efi_guid_t *guid; 46bee91169SAlexander Graf const void *interface; 47bee91169SAlexander Graf }; 48bee91169SAlexander Graf 49bee91169SAlexander Graf /* 50bee91169SAlexander Graf * When the UEFI payload wants to open a protocol on an object to get its 51bee91169SAlexander Graf * interface (usually a struct with callback functions), this struct maps the 52*b5349f74Sxypron.glpk@gmx.de * protocol GUID to the respective protocol interface */ 53bee91169SAlexander Graf struct efi_handler { 54bee91169SAlexander Graf const efi_guid_t *guid; 55*b5349f74Sxypron.glpk@gmx.de void *protocol_interface; 56bee91169SAlexander Graf }; 57bee91169SAlexander Graf 58bee91169SAlexander Graf /* 59bee91169SAlexander Graf * UEFI has a poor man's OO model where one "object" can be polymorphic and have 60bee91169SAlexander Graf * multiple different protocols (classes) attached to it. 61bee91169SAlexander Graf * 62bee91169SAlexander Graf * This struct is the parent struct for all of our actual implementation objects 63bee91169SAlexander Graf * that can include it to make themselves an EFI object 64bee91169SAlexander Graf */ 65bee91169SAlexander Graf struct efi_object { 66bee91169SAlexander Graf /* Every UEFI object is part of a global object list */ 67bee91169SAlexander Graf struct list_head link; 68bee91169SAlexander Graf /* We support up to 4 "protocols" an object can be accessed through */ 69bee91169SAlexander Graf struct efi_handler protocols[4]; 70bee91169SAlexander Graf /* The object spawner can either use this for data or as identifier */ 71bee91169SAlexander Graf void *handle; 72bee91169SAlexander Graf }; 73bee91169SAlexander Graf 74bee91169SAlexander Graf /* This list contains all UEFI objects we know of */ 75bee91169SAlexander Graf extern struct list_head efi_obj_list; 76bee91169SAlexander Graf 772a22d05dSAlexander Graf /* Called by bootefi to make all disk storage accessible as EFI objects */ 782a22d05dSAlexander Graf int efi_disk_register(void); 79be8d3241SAlexander Graf /* Called by bootefi to make GOP (graphical) interface available */ 80be8d3241SAlexander Graf int efi_gop_register(void); 810efe1bcfSAlexander Graf /* Called by bootefi to make the network interface available */ 820efe1bcfSAlexander Graf int efi_net_register(void **handle); 83e663b350SAlexander Graf /* Called by bootefi to make SMBIOS tables available */ 84e663b350SAlexander Graf void efi_smbios_register(void); 850efe1bcfSAlexander Graf 860efe1bcfSAlexander Graf /* Called by networking code to memorize the dhcp ack package */ 870efe1bcfSAlexander Graf void efi_net_set_dhcp_ack(void *pkt, int len); 880efe1bcfSAlexander Graf 89bee91169SAlexander Graf /* Called from places to check whether a timer expired */ 90bee91169SAlexander Graf void efi_timer_check(void); 91bee91169SAlexander Graf /* PE loader implementation */ 92cb149c66SAlexander Graf void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info); 93bee91169SAlexander Graf /* Called once to store the pristine gd pointer */ 94bee91169SAlexander Graf void efi_save_gd(void); 95bee91169SAlexander Graf /* Called from EFI_ENTRY on callback entry to put gd into the gd register */ 96bee91169SAlexander Graf void efi_restore_gd(void); 97bee91169SAlexander Graf /* Called from EFI_EXIT on callback exit to restore the gd register */ 98bee91169SAlexander Graf efi_status_t efi_exit_func(efi_status_t ret); 9950149ea3SAlexander Graf /* Call this to relocate the runtime section to an address space */ 10050149ea3SAlexander Graf void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map); 1010f4060ebSAlexander Graf /* Call this to set the current device name */ 102c07ad7c0SAlexander Graf void efi_set_bootdev(const char *dev, const char *devnr, const char *path); 10350149ea3SAlexander Graf 1045d00995cSAlexander Graf /* Generic EFI memory allocator, call this to get memory */ 1055d00995cSAlexander Graf void *efi_alloc(uint64_t len, int memory_type); 1065d00995cSAlexander Graf /* More specific EFI memory allocator, called by EFI payloads */ 1075d00995cSAlexander Graf efi_status_t efi_allocate_pages(int type, int memory_type, unsigned long pages, 1085d00995cSAlexander Graf uint64_t *memory); 109b61d857bSStefan Brüns /* EFI memory free function. */ 1105d00995cSAlexander Graf efi_status_t efi_free_pages(uint64_t memory, unsigned long pages); 111ead1274bSStefan Brüns /* EFI memory allocator for small allocations */ 112ead1274bSStefan Brüns efi_status_t efi_allocate_pool(int pool_type, unsigned long size, 113ead1274bSStefan Brüns void **buffer); 11442417bc8SStefan Brüns /* EFI pool memory free function. */ 11542417bc8SStefan Brüns efi_status_t efi_free_pool(void *buffer); 1165d00995cSAlexander Graf /* Returns the EFI memory map */ 1175d00995cSAlexander Graf efi_status_t efi_get_memory_map(unsigned long *memory_map_size, 1185d00995cSAlexander Graf struct efi_mem_desc *memory_map, 1195d00995cSAlexander Graf unsigned long *map_key, 1205d00995cSAlexander Graf unsigned long *descriptor_size, 1215d00995cSAlexander Graf uint32_t *descriptor_version); 1225d00995cSAlexander Graf /* Adds a range into the EFI memory map */ 1235d00995cSAlexander Graf uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type, 1245d00995cSAlexander Graf bool overlap_only_ram); 1255d00995cSAlexander Graf /* Called by board init to initialize the EFI memory map */ 1265d00995cSAlexander Graf int efi_memory_init(void); 127488bf12dSAlexander Graf /* Adds new or overrides configuration table entry to the system table */ 128488bf12dSAlexander Graf efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table); 1295d00995cSAlexander Graf 13051735ae0SAlexander Graf #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER 13151735ae0SAlexander Graf extern void *efi_bounce_buffer; 13251735ae0SAlexander Graf #define EFI_LOADER_BOUNCE_BUFFER_SIZE (64 * 1024 * 1024) 13351735ae0SAlexander Graf #endif 13451735ae0SAlexander Graf 1350f4060ebSAlexander Graf /* Convert strings from normal C strings to uEFI strings */ 136487d756fSSimon Glass static inline void ascii2unicode(u16 *unicode, const char *ascii) 1370f4060ebSAlexander Graf { 1380f4060ebSAlexander Graf while (*ascii) 1390f4060ebSAlexander Graf *(unicode++) = *(ascii++); 1400f4060ebSAlexander Graf } 1410f4060ebSAlexander Graf 14250149ea3SAlexander Graf /* 14350149ea3SAlexander Graf * Use these to indicate that your code / data should go into the EFI runtime 14450149ea3SAlexander Graf * section and thus still be available when the OS is running 14550149ea3SAlexander Graf */ 1463c63db9cSAlexander Graf #define __efi_runtime_data __attribute__ ((section ("efi_runtime_data"))) 1473c63db9cSAlexander Graf #define __efi_runtime __attribute__ ((section ("efi_runtime_text"))) 148bee91169SAlexander Graf 14980a4800eSAlexander Graf /* Call this with mmio_ptr as the _pointer_ to a pointer to an MMIO region 15080a4800eSAlexander Graf * to make it available at runtime */ 15180a4800eSAlexander Graf void efi_add_runtime_mmio(void *mmio_ptr, u64 len); 15280a4800eSAlexander Graf 15380a4800eSAlexander Graf /* Boards may provide the functions below to implement RTS functionality */ 15480a4800eSAlexander Graf 1553c63db9cSAlexander Graf void __efi_runtime EFIAPI efi_reset_system( 15680a4800eSAlexander Graf enum efi_reset_type reset_type, 15780a4800eSAlexander Graf efi_status_t reset_status, 15880a4800eSAlexander Graf unsigned long data_size, void *reset_data); 15980a4800eSAlexander Graf void efi_reset_system_init(void); 16080a4800eSAlexander Graf 1613c63db9cSAlexander Graf efi_status_t __efi_runtime EFIAPI efi_get_time( 16280a4800eSAlexander Graf struct efi_time *time, 16380a4800eSAlexander Graf struct efi_time_cap *capabilities); 16480a4800eSAlexander Graf void efi_get_time_init(void); 16580a4800eSAlexander Graf 166bee91169SAlexander Graf #else /* defined(EFI_LOADER) && !defined(CONFIG_SPL_BUILD) */ 167bee91169SAlexander Graf 16850149ea3SAlexander Graf /* Without CONFIG_EFI_LOADER we don't have a runtime section, stub it out */ 1693c63db9cSAlexander Graf #define __efi_runtime_data 1703c63db9cSAlexander Graf #define __efi_runtime 17197d01444SAlexander Graf static inline void efi_add_runtime_mmio(void *mmio_ptr, u64 len) { } 17250149ea3SAlexander Graf 173bee91169SAlexander Graf /* No loader configured, stub out EFI_ENTRY */ 174bee91169SAlexander Graf static inline void efi_restore_gd(void) { } 175c07ad7c0SAlexander Graf static inline void efi_set_bootdev(const char *dev, const char *devnr, 176c07ad7c0SAlexander Graf const char *path) { } 1770efe1bcfSAlexander Graf static inline void efi_net_set_dhcp_ack(void *pkt, int len) { } 178bee91169SAlexander Graf 179bee91169SAlexander Graf #endif 180