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; 2991be9a77Sxypron.glpk@gmx.de extern struct efi_simple_input_interface efi_con_in; 30c1311ad4SAlexander Graf extern const struct efi_console_control_protocol efi_console_control; 31cc5b7081Sxypron.glpk@gmx.de extern const struct efi_device_path_to_text_protocol efi_device_path_to_text; 32c1311ad4SAlexander Graf 33c1311ad4SAlexander Graf extern const efi_guid_t efi_guid_console_control; 34cb149c66SAlexander Graf extern const efi_guid_t efi_guid_device_path; 35cb149c66SAlexander Graf extern const efi_guid_t efi_guid_loaded_image; 36cc5b7081Sxypron.glpk@gmx.de extern const efi_guid_t efi_guid_device_path_to_text_protocol; 37cb149c66SAlexander Graf 3850149ea3SAlexander Graf extern unsigned int __efi_runtime_start, __efi_runtime_stop; 3950149ea3SAlexander Graf extern unsigned int __efi_runtime_rel_start, __efi_runtime_rel_stop; 4050149ea3SAlexander Graf 41bee91169SAlexander Graf /* 42bee91169SAlexander Graf * When the UEFI payload wants to open a protocol on an object to get its 43bee91169SAlexander Graf * interface (usually a struct with callback functions), this struct maps the 44b5349f74Sxypron.glpk@gmx.de * protocol GUID to the respective protocol interface */ 45bee91169SAlexander Graf struct efi_handler { 46bee91169SAlexander Graf const efi_guid_t *guid; 47b5349f74Sxypron.glpk@gmx.de void *protocol_interface; 48bee91169SAlexander Graf }; 49bee91169SAlexander Graf 50bee91169SAlexander Graf /* 51bee91169SAlexander Graf * UEFI has a poor man's OO model where one "object" can be polymorphic and have 52bee91169SAlexander Graf * multiple different protocols (classes) attached to it. 53bee91169SAlexander Graf * 54bee91169SAlexander Graf * This struct is the parent struct for all of our actual implementation objects 55bee91169SAlexander Graf * that can include it to make themselves an EFI object 56bee91169SAlexander Graf */ 57bee91169SAlexander Graf struct efi_object { 58bee91169SAlexander Graf /* Every UEFI object is part of a global object list */ 59bee91169SAlexander Graf struct list_head link; 60011f4327Sxypron.glpk@gmx.de /* We support up to 8 "protocols" an object can be accessed through */ 61011f4327Sxypron.glpk@gmx.de struct efi_handler protocols[8]; 62bee91169SAlexander Graf /* The object spawner can either use this for data or as identifier */ 63bee91169SAlexander Graf void *handle; 64bee91169SAlexander Graf }; 65bee91169SAlexander Graf 66c6841592Sxypron.glpk@gmx.de /** 67c6841592Sxypron.glpk@gmx.de * struct efi_event 68c6841592Sxypron.glpk@gmx.de * 69c6841592Sxypron.glpk@gmx.de * @type: Type of event, see efi_create_event 70c6841592Sxypron.glpk@gmx.de * @notify_tpl: Task priority level of notifications 71c6841592Sxypron.glpk@gmx.de * @trigger_time: Period of the timer 72c6841592Sxypron.glpk@gmx.de * @trigger_next: Next time to trigger the timer 73c6841592Sxypron.glpk@gmx.de * @nofify_function: Function to call when the event is triggered 74c6841592Sxypron.glpk@gmx.de * @notify_context: Data to be passed to the notify function 75c6841592Sxypron.glpk@gmx.de * @trigger_type: Type of timer, see efi_set_timer 76c6841592Sxypron.glpk@gmx.de * @signaled: The notify function was already called 77c6841592Sxypron.glpk@gmx.de */ 78c6841592Sxypron.glpk@gmx.de struct efi_event { 79c6841592Sxypron.glpk@gmx.de u32 type; 80503f2695Sxypron.glpk@gmx.de UINTN notify_tpl; 81c6841592Sxypron.glpk@gmx.de void (EFIAPI *notify_function)(struct efi_event *event, void *context); 82c6841592Sxypron.glpk@gmx.de void *notify_context; 83c6841592Sxypron.glpk@gmx.de u64 trigger_next; 84c6841592Sxypron.glpk@gmx.de u64 trigger_time; 85c6841592Sxypron.glpk@gmx.de enum efi_event_type trigger_type; 86c6841592Sxypron.glpk@gmx.de int signaled; 87c6841592Sxypron.glpk@gmx.de }; 88c6841592Sxypron.glpk@gmx.de 89c6841592Sxypron.glpk@gmx.de 90bee91169SAlexander Graf /* This list contains all UEFI objects we know of */ 91bee91169SAlexander Graf extern struct list_head efi_obj_list; 92bee91169SAlexander Graf 9391be9a77Sxypron.glpk@gmx.de /* Called by bootefi to make console interface available */ 9491be9a77Sxypron.glpk@gmx.de int efi_console_register(void); 952a22d05dSAlexander Graf /* Called by bootefi to make all disk storage accessible as EFI objects */ 962a22d05dSAlexander Graf int efi_disk_register(void); 97be8d3241SAlexander Graf /* Called by bootefi to make GOP (graphical) interface available */ 98be8d3241SAlexander Graf int efi_gop_register(void); 990efe1bcfSAlexander Graf /* Called by bootefi to make the network interface available */ 1000efe1bcfSAlexander Graf int efi_net_register(void **handle); 101e663b350SAlexander Graf /* Called by bootefi to make SMBIOS tables available */ 102e663b350SAlexander Graf void efi_smbios_register(void); 1030efe1bcfSAlexander Graf 1040efe1bcfSAlexander Graf /* Called by networking code to memorize the dhcp ack package */ 1050efe1bcfSAlexander Graf void efi_net_set_dhcp_ack(void *pkt, int len); 1060efe1bcfSAlexander Graf 107bee91169SAlexander Graf /* Called from places to check whether a timer expired */ 108bee91169SAlexander Graf void efi_timer_check(void); 109bee91169SAlexander Graf /* PE loader implementation */ 110cb149c66SAlexander Graf void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info); 111bee91169SAlexander Graf /* Called once to store the pristine gd pointer */ 112bee91169SAlexander Graf void efi_save_gd(void); 113bee91169SAlexander Graf /* Called from EFI_ENTRY on callback entry to put gd into the gd register */ 114bee91169SAlexander Graf void efi_restore_gd(void); 115bee91169SAlexander Graf /* Called from EFI_EXIT on callback exit to restore the gd register */ 116bee91169SAlexander Graf efi_status_t efi_exit_func(efi_status_t ret); 11750149ea3SAlexander Graf /* Call this to relocate the runtime section to an address space */ 11850149ea3SAlexander Graf void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map); 1190f4060ebSAlexander Graf /* Call this to set the current device name */ 120c07ad7c0SAlexander Graf void efi_set_bootdev(const char *dev, const char *devnr, const char *path); 12149deb455Sxypron.glpk@gmx.de /* Call this to create an event */ 12249deb455Sxypron.glpk@gmx.de efi_status_t efi_create_event(enum efi_event_type type, UINTN notify_tpl, 12349deb455Sxypron.glpk@gmx.de void (EFIAPI *notify_function) ( 12449deb455Sxypron.glpk@gmx.de struct efi_event *event, 12549deb455Sxypron.glpk@gmx.de void *context), 12649deb455Sxypron.glpk@gmx.de void *notify_context, struct efi_event **event); 127bfc72462Sxypron.glpk@gmx.de /* Call this to set a timer */ 128bfc72462Sxypron.glpk@gmx.de efi_status_t efi_set_timer(struct efi_event *event, int type, 129bfc72462Sxypron.glpk@gmx.de uint64_t trigger_time); 13091be9a77Sxypron.glpk@gmx.de /* Call this to signal an event */ 13191be9a77Sxypron.glpk@gmx.de void efi_signal_event(struct efi_event *event); 13250149ea3SAlexander Graf 1335d00995cSAlexander Graf /* Generic EFI memory allocator, call this to get memory */ 1345d00995cSAlexander Graf void *efi_alloc(uint64_t len, int memory_type); 1355d00995cSAlexander Graf /* More specific EFI memory allocator, called by EFI payloads */ 1365d00995cSAlexander Graf efi_status_t efi_allocate_pages(int type, int memory_type, unsigned long pages, 1375d00995cSAlexander Graf uint64_t *memory); 138b61d857bSStefan Brüns /* EFI memory free function. */ 1395d00995cSAlexander Graf efi_status_t efi_free_pages(uint64_t memory, unsigned long pages); 140ead1274bSStefan Brüns /* EFI memory allocator for small allocations */ 141ead1274bSStefan Brüns efi_status_t efi_allocate_pool(int pool_type, unsigned long size, 142ead1274bSStefan Brüns void **buffer); 14342417bc8SStefan Brüns /* EFI pool memory free function. */ 14442417bc8SStefan Brüns efi_status_t efi_free_pool(void *buffer); 1455d00995cSAlexander Graf /* Returns the EFI memory map */ 1465d00995cSAlexander Graf efi_status_t efi_get_memory_map(unsigned long *memory_map_size, 1475d00995cSAlexander Graf struct efi_mem_desc *memory_map, 1485d00995cSAlexander Graf unsigned long *map_key, 1495d00995cSAlexander Graf unsigned long *descriptor_size, 1505d00995cSAlexander Graf uint32_t *descriptor_version); 1515d00995cSAlexander Graf /* Adds a range into the EFI memory map */ 1525d00995cSAlexander Graf uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type, 1535d00995cSAlexander Graf bool overlap_only_ram); 1545d00995cSAlexander Graf /* Called by board init to initialize the EFI memory map */ 1555d00995cSAlexander Graf int efi_memory_init(void); 156488bf12dSAlexander Graf /* Adds new or overrides configuration table entry to the system table */ 157488bf12dSAlexander Graf efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table); 1585d00995cSAlexander Graf 15951735ae0SAlexander Graf #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER 16051735ae0SAlexander Graf extern void *efi_bounce_buffer; 16151735ae0SAlexander Graf #define EFI_LOADER_BOUNCE_BUFFER_SIZE (64 * 1024 * 1024) 16251735ae0SAlexander Graf #endif 16351735ae0SAlexander Graf 1640f4060ebSAlexander Graf /* Convert strings from normal C strings to uEFI strings */ 165487d756fSSimon Glass static inline void ascii2unicode(u16 *unicode, const char *ascii) 1660f4060ebSAlexander Graf { 1670f4060ebSAlexander Graf while (*ascii) 1680f4060ebSAlexander Graf *(unicode++) = *(ascii++); 1690f4060ebSAlexander Graf } 1700f4060ebSAlexander Graf 171*3e094c59SRob Clark static inline int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2) 172*3e094c59SRob Clark { 173*3e094c59SRob Clark return memcmp(g1, g2, sizeof(efi_guid_t)); 174*3e094c59SRob Clark } 175*3e094c59SRob Clark 17650149ea3SAlexander Graf /* 17750149ea3SAlexander Graf * Use these to indicate that your code / data should go into the EFI runtime 17850149ea3SAlexander Graf * section and thus still be available when the OS is running 17950149ea3SAlexander Graf */ 1803c63db9cSAlexander Graf #define __efi_runtime_data __attribute__ ((section ("efi_runtime_data"))) 1813c63db9cSAlexander Graf #define __efi_runtime __attribute__ ((section ("efi_runtime_text"))) 182bee91169SAlexander Graf 18380a4800eSAlexander Graf /* Call this with mmio_ptr as the _pointer_ to a pointer to an MMIO region 18480a4800eSAlexander Graf * to make it available at runtime */ 18580a4800eSAlexander Graf void efi_add_runtime_mmio(void *mmio_ptr, u64 len); 18680a4800eSAlexander Graf 18780a4800eSAlexander Graf /* Boards may provide the functions below to implement RTS functionality */ 18880a4800eSAlexander Graf 1893c63db9cSAlexander Graf void __efi_runtime EFIAPI efi_reset_system( 19080a4800eSAlexander Graf enum efi_reset_type reset_type, 19180a4800eSAlexander Graf efi_status_t reset_status, 19280a4800eSAlexander Graf unsigned long data_size, void *reset_data); 19380a4800eSAlexander Graf void efi_reset_system_init(void); 19480a4800eSAlexander Graf 1953c63db9cSAlexander Graf efi_status_t __efi_runtime EFIAPI efi_get_time( 19680a4800eSAlexander Graf struct efi_time *time, 19780a4800eSAlexander Graf struct efi_time_cap *capabilities); 19880a4800eSAlexander Graf void efi_get_time_init(void); 19980a4800eSAlexander Graf 200bee91169SAlexander Graf #else /* defined(EFI_LOADER) && !defined(CONFIG_SPL_BUILD) */ 201bee91169SAlexander Graf 20250149ea3SAlexander Graf /* Without CONFIG_EFI_LOADER we don't have a runtime section, stub it out */ 2033c63db9cSAlexander Graf #define __efi_runtime_data 2043c63db9cSAlexander Graf #define __efi_runtime 20597d01444SAlexander Graf static inline void efi_add_runtime_mmio(void *mmio_ptr, u64 len) { } 20650149ea3SAlexander Graf 207bee91169SAlexander Graf /* No loader configured, stub out EFI_ENTRY */ 208bee91169SAlexander Graf static inline void efi_restore_gd(void) { } 209c07ad7c0SAlexander Graf static inline void efi_set_bootdev(const char *dev, const char *devnr, 210c07ad7c0SAlexander Graf const char *path) { } 2110efe1bcfSAlexander Graf static inline void efi_net_set_dhcp_ack(void *pkt, int len) { } 212bee91169SAlexander Graf 213bee91169SAlexander Graf #endif 214