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 18*c160d2f5SRob Clark int __efi_entry_check(void); 19*c160d2f5SRob Clark int __efi_exit_check(void); 20*c160d2f5SRob Clark 21a095aadfSRob Clark /* 22a095aadfSRob Clark * Enter the u-boot world from UEFI: 23a095aadfSRob Clark */ 24bee91169SAlexander Graf #define EFI_ENTRY(format, ...) do { \ 25*c160d2f5SRob Clark assert(__efi_entry_check()); \ 26edcef3baSAlexander Graf debug("EFI: Entry %s(" format ")\n", __func__, ##__VA_ARGS__); \ 27bee91169SAlexander Graf } while(0) 28bee91169SAlexander Graf 29a095aadfSRob Clark /* 30a095aadfSRob Clark * Exit the u-boot world back to UEFI: 31a095aadfSRob Clark */ 32804b1d73SRob Clark #define EFI_EXIT(ret) ({ \ 333f1aa975SRob Clark efi_status_t _r = ret; \ 343f1aa975SRob Clark debug("EFI: Exit: %s: %u\n", __func__, (u32)(_r & ~EFI_ERROR_MASK)); \ 35*c160d2f5SRob Clark assert(__efi_exit_check()); \ 36*c160d2f5SRob Clark _r; \ 37804b1d73SRob Clark }) 38bee91169SAlexander Graf 39a095aadfSRob Clark /* 40a095aadfSRob Clark * Callback into UEFI world from u-boot: 41a095aadfSRob Clark */ 42a095aadfSRob Clark #define EFI_CALL(exp) do { \ 43a095aadfSRob Clark debug("EFI: Call: %s\n", #exp); \ 44*c160d2f5SRob Clark assert(__efi_exit_check()); \ 45a095aadfSRob Clark exp; \ 46*c160d2f5SRob Clark assert(__efi_entry_check()); \ 47a095aadfSRob Clark debug("EFI: Return From: %s\n", #exp); \ 48a095aadfSRob Clark } while(0) 49a095aadfSRob Clark 5050149ea3SAlexander Graf extern struct efi_runtime_services efi_runtime_services; 51bee91169SAlexander Graf extern struct efi_system_table systab; 52bee91169SAlexander Graf 53c1311ad4SAlexander Graf extern const struct efi_simple_text_output_protocol efi_con_out; 5491be9a77Sxypron.glpk@gmx.de extern struct efi_simple_input_interface efi_con_in; 55c1311ad4SAlexander Graf extern const struct efi_console_control_protocol efi_console_control; 56cc5b7081Sxypron.glpk@gmx.de extern const struct efi_device_path_to_text_protocol efi_device_path_to_text; 57c1311ad4SAlexander Graf 58c1311ad4SAlexander Graf extern const efi_guid_t efi_guid_console_control; 59cb149c66SAlexander Graf extern const efi_guid_t efi_guid_device_path; 60cb149c66SAlexander Graf extern const efi_guid_t efi_guid_loaded_image; 61cc5b7081Sxypron.glpk@gmx.de extern const efi_guid_t efi_guid_device_path_to_text_protocol; 62cb149c66SAlexander Graf 6350149ea3SAlexander Graf extern unsigned int __efi_runtime_start, __efi_runtime_stop; 6450149ea3SAlexander Graf extern unsigned int __efi_runtime_rel_start, __efi_runtime_rel_stop; 6550149ea3SAlexander Graf 66bee91169SAlexander Graf /* 67bee91169SAlexander Graf * When the UEFI payload wants to open a protocol on an object to get its 68bee91169SAlexander Graf * interface (usually a struct with callback functions), this struct maps the 69b5349f74Sxypron.glpk@gmx.de * protocol GUID to the respective protocol interface */ 70bee91169SAlexander Graf struct efi_handler { 71bee91169SAlexander Graf const efi_guid_t *guid; 72b5349f74Sxypron.glpk@gmx.de void *protocol_interface; 73bee91169SAlexander Graf }; 74bee91169SAlexander Graf 75bee91169SAlexander Graf /* 76bee91169SAlexander Graf * UEFI has a poor man's OO model where one "object" can be polymorphic and have 77bee91169SAlexander Graf * multiple different protocols (classes) attached to it. 78bee91169SAlexander Graf * 79bee91169SAlexander Graf * This struct is the parent struct for all of our actual implementation objects 80bee91169SAlexander Graf * that can include it to make themselves an EFI object 81bee91169SAlexander Graf */ 82bee91169SAlexander Graf struct efi_object { 83bee91169SAlexander Graf /* Every UEFI object is part of a global object list */ 84bee91169SAlexander Graf struct list_head link; 85011f4327Sxypron.glpk@gmx.de /* We support up to 8 "protocols" an object can be accessed through */ 86011f4327Sxypron.glpk@gmx.de struct efi_handler protocols[8]; 87bee91169SAlexander Graf /* The object spawner can either use this for data or as identifier */ 88bee91169SAlexander Graf void *handle; 89bee91169SAlexander Graf }; 90bee91169SAlexander Graf 91641833dbSRob Clark #define EFI_PROTOCOL_OBJECT(_guid, _protocol) (struct efi_object){ \ 92641833dbSRob Clark .protocols = {{ \ 93641833dbSRob Clark .guid = &(_guid), \ 94641833dbSRob Clark .protocol_interface = (void *)(_protocol), \ 95641833dbSRob Clark }}, \ 96641833dbSRob Clark .handle = (void *)(_protocol), \ 97641833dbSRob Clark } 98641833dbSRob Clark 99c6841592Sxypron.glpk@gmx.de /** 100c6841592Sxypron.glpk@gmx.de * struct efi_event 101c6841592Sxypron.glpk@gmx.de * 102c6841592Sxypron.glpk@gmx.de * @type: Type of event, see efi_create_event 103c6841592Sxypron.glpk@gmx.de * @notify_tpl: Task priority level of notifications 104c6841592Sxypron.glpk@gmx.de * @trigger_time: Period of the timer 105c6841592Sxypron.glpk@gmx.de * @trigger_next: Next time to trigger the timer 106c6841592Sxypron.glpk@gmx.de * @nofify_function: Function to call when the event is triggered 107c6841592Sxypron.glpk@gmx.de * @notify_context: Data to be passed to the notify function 108c6841592Sxypron.glpk@gmx.de * @trigger_type: Type of timer, see efi_set_timer 109c6841592Sxypron.glpk@gmx.de * @signaled: The notify function was already called 110c6841592Sxypron.glpk@gmx.de */ 111c6841592Sxypron.glpk@gmx.de struct efi_event { 112b521d29eSxypron.glpk@gmx.de uint32_t type; 113503f2695Sxypron.glpk@gmx.de UINTN notify_tpl; 114c6841592Sxypron.glpk@gmx.de void (EFIAPI *notify_function)(struct efi_event *event, void *context); 115c6841592Sxypron.glpk@gmx.de void *notify_context; 116c6841592Sxypron.glpk@gmx.de u64 trigger_next; 117c6841592Sxypron.glpk@gmx.de u64 trigger_time; 118b521d29eSxypron.glpk@gmx.de enum efi_timer_delay trigger_type; 119c6841592Sxypron.glpk@gmx.de int signaled; 120c6841592Sxypron.glpk@gmx.de }; 121c6841592Sxypron.glpk@gmx.de 122c6841592Sxypron.glpk@gmx.de 123bee91169SAlexander Graf /* This list contains all UEFI objects we know of */ 124bee91169SAlexander Graf extern struct list_head efi_obj_list; 125bee91169SAlexander Graf 12691be9a77Sxypron.glpk@gmx.de /* Called by bootefi to make console interface available */ 12791be9a77Sxypron.glpk@gmx.de int efi_console_register(void); 1282a22d05dSAlexander Graf /* Called by bootefi to make all disk storage accessible as EFI objects */ 1292a22d05dSAlexander Graf int efi_disk_register(void); 130be8d3241SAlexander Graf /* Called by bootefi to make GOP (graphical) interface available */ 131be8d3241SAlexander Graf int efi_gop_register(void); 1320efe1bcfSAlexander Graf /* Called by bootefi to make the network interface available */ 1330efe1bcfSAlexander Graf int efi_net_register(void **handle); 134e663b350SAlexander Graf /* Called by bootefi to make SMBIOS tables available */ 135e663b350SAlexander Graf void efi_smbios_register(void); 1360efe1bcfSAlexander Graf 1370efe1bcfSAlexander Graf /* Called by networking code to memorize the dhcp ack package */ 1380efe1bcfSAlexander Graf void efi_net_set_dhcp_ack(void *pkt, int len); 1390efe1bcfSAlexander Graf 140bee91169SAlexander Graf /* Called from places to check whether a timer expired */ 141bee91169SAlexander Graf void efi_timer_check(void); 142bee91169SAlexander Graf /* PE loader implementation */ 143cb149c66SAlexander Graf void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info); 144bee91169SAlexander Graf /* Called once to store the pristine gd pointer */ 145bee91169SAlexander Graf void efi_save_gd(void); 146*c160d2f5SRob Clark /* Special case handler for error/abort that just tries to dtrt to get 147*c160d2f5SRob Clark * back to u-boot world */ 148bee91169SAlexander Graf void efi_restore_gd(void); 14950149ea3SAlexander Graf /* Call this to relocate the runtime section to an address space */ 15050149ea3SAlexander Graf void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map); 1510f4060ebSAlexander Graf /* Call this to set the current device name */ 152c07ad7c0SAlexander Graf void efi_set_bootdev(const char *dev, const char *devnr, const char *path); 15349deb455Sxypron.glpk@gmx.de /* Call this to create an event */ 154b521d29eSxypron.glpk@gmx.de efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl, 15549deb455Sxypron.glpk@gmx.de void (EFIAPI *notify_function) ( 15649deb455Sxypron.glpk@gmx.de struct efi_event *event, 15749deb455Sxypron.glpk@gmx.de void *context), 15849deb455Sxypron.glpk@gmx.de void *notify_context, struct efi_event **event); 159bfc72462Sxypron.glpk@gmx.de /* Call this to set a timer */ 160b521d29eSxypron.glpk@gmx.de efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type, 161bfc72462Sxypron.glpk@gmx.de uint64_t trigger_time); 16291be9a77Sxypron.glpk@gmx.de /* Call this to signal an event */ 16391be9a77Sxypron.glpk@gmx.de void efi_signal_event(struct efi_event *event); 16450149ea3SAlexander Graf 1655d00995cSAlexander Graf /* Generic EFI memory allocator, call this to get memory */ 1665d00995cSAlexander Graf void *efi_alloc(uint64_t len, int memory_type); 1675d00995cSAlexander Graf /* More specific EFI memory allocator, called by EFI payloads */ 1685d00995cSAlexander Graf efi_status_t efi_allocate_pages(int type, int memory_type, unsigned long pages, 1695d00995cSAlexander Graf uint64_t *memory); 170b61d857bSStefan Brüns /* EFI memory free function. */ 1715d00995cSAlexander Graf efi_status_t efi_free_pages(uint64_t memory, unsigned long pages); 172ead1274bSStefan Brüns /* EFI memory allocator for small allocations */ 173ead1274bSStefan Brüns efi_status_t efi_allocate_pool(int pool_type, unsigned long size, 174ead1274bSStefan Brüns void **buffer); 17542417bc8SStefan Brüns /* EFI pool memory free function. */ 17642417bc8SStefan Brüns efi_status_t efi_free_pool(void *buffer); 1775d00995cSAlexander Graf /* Returns the EFI memory map */ 1785d00995cSAlexander Graf efi_status_t efi_get_memory_map(unsigned long *memory_map_size, 1795d00995cSAlexander Graf struct efi_mem_desc *memory_map, 1805d00995cSAlexander Graf unsigned long *map_key, 1815d00995cSAlexander Graf unsigned long *descriptor_size, 1825d00995cSAlexander Graf uint32_t *descriptor_version); 1835d00995cSAlexander Graf /* Adds a range into the EFI memory map */ 1845d00995cSAlexander Graf uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type, 1855d00995cSAlexander Graf bool overlap_only_ram); 1865d00995cSAlexander Graf /* Called by board init to initialize the EFI memory map */ 1875d00995cSAlexander Graf int efi_memory_init(void); 188488bf12dSAlexander Graf /* Adds new or overrides configuration table entry to the system table */ 189488bf12dSAlexander Graf efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table); 1905d00995cSAlexander Graf 19151735ae0SAlexander Graf #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER 19251735ae0SAlexander Graf extern void *efi_bounce_buffer; 19351735ae0SAlexander Graf #define EFI_LOADER_BOUNCE_BUFFER_SIZE (64 * 1024 * 1024) 19451735ae0SAlexander Graf #endif 19551735ae0SAlexander Graf 1960f4060ebSAlexander Graf /* Convert strings from normal C strings to uEFI strings */ 197487d756fSSimon Glass static inline void ascii2unicode(u16 *unicode, const char *ascii) 1980f4060ebSAlexander Graf { 1990f4060ebSAlexander Graf while (*ascii) 2000f4060ebSAlexander Graf *(unicode++) = *(ascii++); 2010f4060ebSAlexander Graf } 2020f4060ebSAlexander Graf 2033e094c59SRob Clark static inline int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2) 2043e094c59SRob Clark { 2053e094c59SRob Clark return memcmp(g1, g2, sizeof(efi_guid_t)); 2063e094c59SRob Clark } 2073e094c59SRob Clark 20850149ea3SAlexander Graf /* 20950149ea3SAlexander Graf * Use these to indicate that your code / data should go into the EFI runtime 21050149ea3SAlexander Graf * section and thus still be available when the OS is running 21150149ea3SAlexander Graf */ 2123c63db9cSAlexander Graf #define __efi_runtime_data __attribute__ ((section ("efi_runtime_data"))) 2133c63db9cSAlexander Graf #define __efi_runtime __attribute__ ((section ("efi_runtime_text"))) 214bee91169SAlexander Graf 21580a4800eSAlexander Graf /* Call this with mmio_ptr as the _pointer_ to a pointer to an MMIO region 21680a4800eSAlexander Graf * to make it available at runtime */ 21780a4800eSAlexander Graf void efi_add_runtime_mmio(void *mmio_ptr, u64 len); 21880a4800eSAlexander Graf 21980a4800eSAlexander Graf /* Boards may provide the functions below to implement RTS functionality */ 22080a4800eSAlexander Graf 2213c63db9cSAlexander Graf void __efi_runtime EFIAPI efi_reset_system( 22280a4800eSAlexander Graf enum efi_reset_type reset_type, 22380a4800eSAlexander Graf efi_status_t reset_status, 22480a4800eSAlexander Graf unsigned long data_size, void *reset_data); 22580a4800eSAlexander Graf void efi_reset_system_init(void); 22680a4800eSAlexander Graf 2273c63db9cSAlexander Graf efi_status_t __efi_runtime EFIAPI efi_get_time( 22880a4800eSAlexander Graf struct efi_time *time, 22980a4800eSAlexander Graf struct efi_time_cap *capabilities); 23080a4800eSAlexander Graf void efi_get_time_init(void); 23180a4800eSAlexander Graf 232bee91169SAlexander Graf #else /* defined(EFI_LOADER) && !defined(CONFIG_SPL_BUILD) */ 233bee91169SAlexander Graf 23450149ea3SAlexander Graf /* Without CONFIG_EFI_LOADER we don't have a runtime section, stub it out */ 2353c63db9cSAlexander Graf #define __efi_runtime_data 2363c63db9cSAlexander Graf #define __efi_runtime 23797d01444SAlexander Graf static inline void efi_add_runtime_mmio(void *mmio_ptr, u64 len) { } 23850149ea3SAlexander Graf 239bee91169SAlexander Graf /* No loader configured, stub out EFI_ENTRY */ 240bee91169SAlexander Graf static inline void efi_restore_gd(void) { } 241c07ad7c0SAlexander Graf static inline void efi_set_bootdev(const char *dev, const char *devnr, 242c07ad7c0SAlexander Graf const char *path) { } 2430efe1bcfSAlexander Graf static inline void efi_net_set_dhcp_ack(void *pkt, int len) { } 244bee91169SAlexander Graf 245bee91169SAlexander Graf #endif 246