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 18c160d2f5SRob Clark int __efi_entry_check(void); 19c160d2f5SRob Clark int __efi_exit_check(void); 20af65db85SRob Clark const char *__efi_nesting_inc(void); 21af65db85SRob Clark const char *__efi_nesting_dec(void); 22c160d2f5SRob Clark 23a095aadfSRob Clark /* 24a095aadfSRob Clark * Enter the u-boot world from UEFI: 25a095aadfSRob Clark */ 26bee91169SAlexander Graf #define EFI_ENTRY(format, ...) do { \ 27c160d2f5SRob Clark assert(__efi_entry_check()); \ 28af65db85SRob Clark debug("%sEFI: Entry %s(" format ")\n", __efi_nesting_inc(), \ 29af65db85SRob Clark __func__, ##__VA_ARGS__); \ 30bee91169SAlexander Graf } while(0) 31bee91169SAlexander Graf 32a095aadfSRob Clark /* 33a095aadfSRob Clark * Exit the u-boot world back to UEFI: 34a095aadfSRob Clark */ 35804b1d73SRob Clark #define EFI_EXIT(ret) ({ \ 36*c81883dfSxypron.glpk@gmx.de typeof(ret) _r = ret; \ 37af65db85SRob Clark debug("%sEFI: Exit: %s: %u\n", __efi_nesting_dec(), \ 38*c81883dfSxypron.glpk@gmx.de __func__, (u32)((uintptr_t) _r & ~EFI_ERROR_MASK)); \ 39c160d2f5SRob Clark assert(__efi_exit_check()); \ 40c160d2f5SRob Clark _r; \ 41804b1d73SRob Clark }) 42bee91169SAlexander Graf 43a095aadfSRob Clark /* 44a095aadfSRob Clark * Callback into UEFI world from u-boot: 45a095aadfSRob Clark */ 46a095aadfSRob Clark #define EFI_CALL(exp) do { \ 47af65db85SRob Clark debug("%sEFI: Call: %s\n", __efi_nesting_inc(), #exp); \ 48c160d2f5SRob Clark assert(__efi_exit_check()); \ 49a095aadfSRob Clark exp; \ 50c160d2f5SRob Clark assert(__efi_entry_check()); \ 51af65db85SRob Clark debug("%sEFI: Return From: %s\n", __efi_nesting_dec(), #exp); \ 52a095aadfSRob Clark } while(0) 53a095aadfSRob Clark 5450149ea3SAlexander Graf extern struct efi_runtime_services efi_runtime_services; 55bee91169SAlexander Graf extern struct efi_system_table systab; 56bee91169SAlexander Graf 57c1311ad4SAlexander Graf extern const struct efi_simple_text_output_protocol efi_con_out; 5891be9a77Sxypron.glpk@gmx.de extern struct efi_simple_input_interface efi_con_in; 59c1311ad4SAlexander Graf extern const struct efi_console_control_protocol efi_console_control; 60cc5b7081Sxypron.glpk@gmx.de extern const struct efi_device_path_to_text_protocol efi_device_path_to_text; 61c1311ad4SAlexander Graf 62c1311ad4SAlexander Graf extern const efi_guid_t efi_guid_console_control; 63cb149c66SAlexander Graf extern const efi_guid_t efi_guid_device_path; 64cb149c66SAlexander Graf extern const efi_guid_t efi_guid_loaded_image; 65cc5b7081Sxypron.glpk@gmx.de extern const efi_guid_t efi_guid_device_path_to_text_protocol; 66cb149c66SAlexander Graf 6750149ea3SAlexander Graf extern unsigned int __efi_runtime_start, __efi_runtime_stop; 6850149ea3SAlexander Graf extern unsigned int __efi_runtime_rel_start, __efi_runtime_rel_stop; 6950149ea3SAlexander Graf 70bee91169SAlexander Graf /* 71bee91169SAlexander Graf * When the UEFI payload wants to open a protocol on an object to get its 72bee91169SAlexander Graf * interface (usually a struct with callback functions), this struct maps the 73b5349f74Sxypron.glpk@gmx.de * protocol GUID to the respective protocol interface */ 74bee91169SAlexander Graf struct efi_handler { 75bee91169SAlexander Graf const efi_guid_t *guid; 76b5349f74Sxypron.glpk@gmx.de void *protocol_interface; 77bee91169SAlexander Graf }; 78bee91169SAlexander Graf 79bee91169SAlexander Graf /* 80bee91169SAlexander Graf * UEFI has a poor man's OO model where one "object" can be polymorphic and have 81bee91169SAlexander Graf * multiple different protocols (classes) attached to it. 82bee91169SAlexander Graf * 83bee91169SAlexander Graf * This struct is the parent struct for all of our actual implementation objects 84bee91169SAlexander Graf * that can include it to make themselves an EFI object 85bee91169SAlexander Graf */ 86bee91169SAlexander Graf struct efi_object { 87bee91169SAlexander Graf /* Every UEFI object is part of a global object list */ 88bee91169SAlexander Graf struct list_head link; 89011f4327Sxypron.glpk@gmx.de /* We support up to 8 "protocols" an object can be accessed through */ 90011f4327Sxypron.glpk@gmx.de struct efi_handler protocols[8]; 91bee91169SAlexander Graf /* The object spawner can either use this for data or as identifier */ 92bee91169SAlexander Graf void *handle; 93bee91169SAlexander Graf }; 94bee91169SAlexander Graf 95641833dbSRob Clark #define EFI_PROTOCOL_OBJECT(_guid, _protocol) (struct efi_object){ \ 96641833dbSRob Clark .protocols = {{ \ 97641833dbSRob Clark .guid = &(_guid), \ 98641833dbSRob Clark .protocol_interface = (void *)(_protocol), \ 99641833dbSRob Clark }}, \ 100641833dbSRob Clark .handle = (void *)(_protocol), \ 101641833dbSRob Clark } 102641833dbSRob Clark 103c6841592Sxypron.glpk@gmx.de /** 104c6841592Sxypron.glpk@gmx.de * struct efi_event 105c6841592Sxypron.glpk@gmx.de * 106c6841592Sxypron.glpk@gmx.de * @type: Type of event, see efi_create_event 107c6841592Sxypron.glpk@gmx.de * @notify_tpl: Task priority level of notifications 108c6841592Sxypron.glpk@gmx.de * @trigger_time: Period of the timer 109c6841592Sxypron.glpk@gmx.de * @trigger_next: Next time to trigger the timer 110c6841592Sxypron.glpk@gmx.de * @nofify_function: Function to call when the event is triggered 111c6841592Sxypron.glpk@gmx.de * @notify_context: Data to be passed to the notify function 112c6841592Sxypron.glpk@gmx.de * @trigger_type: Type of timer, see efi_set_timer 113c6841592Sxypron.glpk@gmx.de * @signaled: The notify function was already called 114c6841592Sxypron.glpk@gmx.de */ 115c6841592Sxypron.glpk@gmx.de struct efi_event { 116b521d29eSxypron.glpk@gmx.de uint32_t type; 117503f2695Sxypron.glpk@gmx.de UINTN notify_tpl; 118c6841592Sxypron.glpk@gmx.de void (EFIAPI *notify_function)(struct efi_event *event, void *context); 119c6841592Sxypron.glpk@gmx.de void *notify_context; 120c6841592Sxypron.glpk@gmx.de u64 trigger_next; 121c6841592Sxypron.glpk@gmx.de u64 trigger_time; 122b521d29eSxypron.glpk@gmx.de enum efi_timer_delay trigger_type; 123c6841592Sxypron.glpk@gmx.de int signaled; 124c6841592Sxypron.glpk@gmx.de }; 125c6841592Sxypron.glpk@gmx.de 126c6841592Sxypron.glpk@gmx.de 127bee91169SAlexander Graf /* This list contains all UEFI objects we know of */ 128bee91169SAlexander Graf extern struct list_head efi_obj_list; 129bee91169SAlexander Graf 13091be9a77Sxypron.glpk@gmx.de /* Called by bootefi to make console interface available */ 13191be9a77Sxypron.glpk@gmx.de int efi_console_register(void); 1322a22d05dSAlexander Graf /* Called by bootefi to make all disk storage accessible as EFI objects */ 1332a22d05dSAlexander Graf int efi_disk_register(void); 134be8d3241SAlexander Graf /* Called by bootefi to make GOP (graphical) interface available */ 135be8d3241SAlexander Graf int efi_gop_register(void); 1360efe1bcfSAlexander Graf /* Called by bootefi to make the network interface available */ 1370efe1bcfSAlexander Graf int efi_net_register(void **handle); 138e663b350SAlexander Graf /* Called by bootefi to make SMBIOS tables available */ 139e663b350SAlexander Graf void efi_smbios_register(void); 1400efe1bcfSAlexander Graf 1410efe1bcfSAlexander Graf /* Called by networking code to memorize the dhcp ack package */ 1420efe1bcfSAlexander Graf void efi_net_set_dhcp_ack(void *pkt, int len); 1430efe1bcfSAlexander Graf 144bee91169SAlexander Graf /* Called from places to check whether a timer expired */ 145bee91169SAlexander Graf void efi_timer_check(void); 146bee91169SAlexander Graf /* PE loader implementation */ 147cb149c66SAlexander Graf void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info); 148bee91169SAlexander Graf /* Called once to store the pristine gd pointer */ 149bee91169SAlexander Graf void efi_save_gd(void); 150c160d2f5SRob Clark /* Special case handler for error/abort that just tries to dtrt to get 151c160d2f5SRob Clark * back to u-boot world */ 152bee91169SAlexander Graf void efi_restore_gd(void); 15350149ea3SAlexander Graf /* Call this to relocate the runtime section to an address space */ 15450149ea3SAlexander Graf void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map); 1550f4060ebSAlexander Graf /* Call this to set the current device name */ 156c07ad7c0SAlexander Graf void efi_set_bootdev(const char *dev, const char *devnr, const char *path); 15749deb455Sxypron.glpk@gmx.de /* Call this to create an event */ 158b521d29eSxypron.glpk@gmx.de efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl, 15949deb455Sxypron.glpk@gmx.de void (EFIAPI *notify_function) ( 16049deb455Sxypron.glpk@gmx.de struct efi_event *event, 16149deb455Sxypron.glpk@gmx.de void *context), 16249deb455Sxypron.glpk@gmx.de void *notify_context, struct efi_event **event); 163bfc72462Sxypron.glpk@gmx.de /* Call this to set a timer */ 164b521d29eSxypron.glpk@gmx.de efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type, 165bfc72462Sxypron.glpk@gmx.de uint64_t trigger_time); 16691be9a77Sxypron.glpk@gmx.de /* Call this to signal an event */ 16791be9a77Sxypron.glpk@gmx.de void efi_signal_event(struct efi_event *event); 16850149ea3SAlexander Graf 1695d00995cSAlexander Graf /* Generic EFI memory allocator, call this to get memory */ 1705d00995cSAlexander Graf void *efi_alloc(uint64_t len, int memory_type); 1715d00995cSAlexander Graf /* More specific EFI memory allocator, called by EFI payloads */ 1725d00995cSAlexander Graf efi_status_t efi_allocate_pages(int type, int memory_type, unsigned long pages, 1735d00995cSAlexander Graf uint64_t *memory); 174b61d857bSStefan Brüns /* EFI memory free function. */ 1755d00995cSAlexander Graf efi_status_t efi_free_pages(uint64_t memory, unsigned long pages); 176ead1274bSStefan Brüns /* EFI memory allocator for small allocations */ 177ead1274bSStefan Brüns efi_status_t efi_allocate_pool(int pool_type, unsigned long size, 178ead1274bSStefan Brüns void **buffer); 17942417bc8SStefan Brüns /* EFI pool memory free function. */ 18042417bc8SStefan Brüns efi_status_t efi_free_pool(void *buffer); 1815d00995cSAlexander Graf /* Returns the EFI memory map */ 1825d00995cSAlexander Graf efi_status_t efi_get_memory_map(unsigned long *memory_map_size, 1835d00995cSAlexander Graf struct efi_mem_desc *memory_map, 1845d00995cSAlexander Graf unsigned long *map_key, 1855d00995cSAlexander Graf unsigned long *descriptor_size, 1865d00995cSAlexander Graf uint32_t *descriptor_version); 1875d00995cSAlexander Graf /* Adds a range into the EFI memory map */ 1885d00995cSAlexander Graf uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type, 1895d00995cSAlexander Graf bool overlap_only_ram); 1905d00995cSAlexander Graf /* Called by board init to initialize the EFI memory map */ 1915d00995cSAlexander Graf int efi_memory_init(void); 192488bf12dSAlexander Graf /* Adds new or overrides configuration table entry to the system table */ 193488bf12dSAlexander Graf efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table); 1945d00995cSAlexander Graf 19551735ae0SAlexander Graf #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER 19651735ae0SAlexander Graf extern void *efi_bounce_buffer; 19751735ae0SAlexander Graf #define EFI_LOADER_BOUNCE_BUFFER_SIZE (64 * 1024 * 1024) 19851735ae0SAlexander Graf #endif 19951735ae0SAlexander Graf 2000f4060ebSAlexander Graf /* Convert strings from normal C strings to uEFI strings */ 201487d756fSSimon Glass static inline void ascii2unicode(u16 *unicode, const char *ascii) 2020f4060ebSAlexander Graf { 2030f4060ebSAlexander Graf while (*ascii) 2040f4060ebSAlexander Graf *(unicode++) = *(ascii++); 2050f4060ebSAlexander Graf } 2060f4060ebSAlexander Graf 2073e094c59SRob Clark static inline int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2) 2083e094c59SRob Clark { 2093e094c59SRob Clark return memcmp(g1, g2, sizeof(efi_guid_t)); 2103e094c59SRob Clark } 2113e094c59SRob Clark 21250149ea3SAlexander Graf /* 21350149ea3SAlexander Graf * Use these to indicate that your code / data should go into the EFI runtime 21450149ea3SAlexander Graf * section and thus still be available when the OS is running 21550149ea3SAlexander Graf */ 2163c63db9cSAlexander Graf #define __efi_runtime_data __attribute__ ((section ("efi_runtime_data"))) 2173c63db9cSAlexander Graf #define __efi_runtime __attribute__ ((section ("efi_runtime_text"))) 218bee91169SAlexander Graf 21980a4800eSAlexander Graf /* Call this with mmio_ptr as the _pointer_ to a pointer to an MMIO region 22080a4800eSAlexander Graf * to make it available at runtime */ 22180a4800eSAlexander Graf void efi_add_runtime_mmio(void *mmio_ptr, u64 len); 22280a4800eSAlexander Graf 22380a4800eSAlexander Graf /* Boards may provide the functions below to implement RTS functionality */ 22480a4800eSAlexander Graf 2253c63db9cSAlexander Graf void __efi_runtime EFIAPI efi_reset_system( 22680a4800eSAlexander Graf enum efi_reset_type reset_type, 22780a4800eSAlexander Graf efi_status_t reset_status, 22880a4800eSAlexander Graf unsigned long data_size, void *reset_data); 22980a4800eSAlexander Graf void efi_reset_system_init(void); 23080a4800eSAlexander Graf 2313c63db9cSAlexander Graf efi_status_t __efi_runtime EFIAPI efi_get_time( 23280a4800eSAlexander Graf struct efi_time *time, 23380a4800eSAlexander Graf struct efi_time_cap *capabilities); 23480a4800eSAlexander Graf void efi_get_time_init(void); 23580a4800eSAlexander Graf 236bee91169SAlexander Graf #else /* defined(EFI_LOADER) && !defined(CONFIG_SPL_BUILD) */ 237bee91169SAlexander Graf 23850149ea3SAlexander Graf /* Without CONFIG_EFI_LOADER we don't have a runtime section, stub it out */ 2393c63db9cSAlexander Graf #define __efi_runtime_data 2403c63db9cSAlexander Graf #define __efi_runtime 24197d01444SAlexander Graf static inline void efi_add_runtime_mmio(void *mmio_ptr, u64 len) { } 24250149ea3SAlexander Graf 243bee91169SAlexander Graf /* No loader configured, stub out EFI_ENTRY */ 244bee91169SAlexander Graf static inline void efi_restore_gd(void) { } 245c07ad7c0SAlexander Graf static inline void efi_set_bootdev(const char *dev, const char *devnr, 246c07ad7c0SAlexander Graf const char *path) { } 2470efe1bcfSAlexander Graf static inline void efi_net_set_dhcp_ack(void *pkt, int len) { } 248bee91169SAlexander Graf 249bee91169SAlexander Graf #endif 250