blob: f384cbbe771b07f6ff684944fd4611e5cd081b0b [file] [log] [blame]
Alexander Graf4e0c1c12016-03-04 01:09:58 +01001/*
2 * EFI application loader
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
Alexander Grafc15d9212016-03-04 01:09:59 +01009#include <common.h>
Alexander Graf4e0c1c12016-03-04 01:09:58 +010010#include <part_efi.h>
11#include <efi_api.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010012
13/* No need for efi loader support in SPL */
14#if defined(CONFIG_EFI_LOADER) && !defined(CONFIG_SPL_BUILD)
15
Alexander Graf4e0c1c12016-03-04 01:09:58 +010016#include <linux/list.h>
17
Alexander Grafc15d9212016-03-04 01:09:59 +010018#define EFI_ENTRY(format, ...) do { \
19 efi_restore_gd(); \
Alexander Graf62a67482016-06-02 11:38:27 +020020 debug("EFI: Entry %s(" format ")\n", __func__, ##__VA_ARGS__); \
Alexander Grafc15d9212016-03-04 01:09:59 +010021 } while(0)
Alexander Grafc15d9212016-03-04 01:09:59 +010022
Rob Clark84a19a02017-07-24 10:31:52 -040023#define EFI_EXIT(ret) ({ \
24 debug("EFI: Exit: %s: %u\n", __func__, (u32)((ret) & ~EFI_ERROR_MASK)); \
25 efi_exit_func(ret); \
26 })
Alexander Grafc15d9212016-03-04 01:09:59 +010027
Alexander Graf0bd425a2016-03-04 01:10:01 +010028extern struct efi_runtime_services efi_runtime_services;
Alexander Grafc15d9212016-03-04 01:09:59 +010029extern struct efi_system_table systab;
30
Alexander Graf4aacacc2016-03-04 01:10:00 +010031extern const struct efi_simple_text_output_protocol efi_con_out;
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +020032extern struct efi_simple_input_interface efi_con_in;
Alexander Graf4aacacc2016-03-04 01:10:00 +010033extern const struct efi_console_control_protocol efi_console_control;
xypron.glpk@gmx.de3c042b22017-07-11 22:06:25 +020034extern const struct efi_device_path_to_text_protocol efi_device_path_to_text;
Alexander Graf4aacacc2016-03-04 01:10:00 +010035
36extern const efi_guid_t efi_guid_console_control;
Alexander Graf4e0c1c12016-03-04 01:09:58 +010037extern const efi_guid_t efi_guid_device_path;
38extern const efi_guid_t efi_guid_loaded_image;
xypron.glpk@gmx.de3c042b22017-07-11 22:06:25 +020039extern const efi_guid_t efi_guid_device_path_to_text_protocol;
Alexander Graf4e0c1c12016-03-04 01:09:58 +010040
Alexander Graf0bd425a2016-03-04 01:10:01 +010041extern unsigned int __efi_runtime_start, __efi_runtime_stop;
42extern unsigned int __efi_runtime_rel_start, __efi_runtime_rel_stop;
43
Alexander Grafc15d9212016-03-04 01:09:59 +010044/*
Alexander Grafc15d9212016-03-04 01:09:59 +010045 * When the UEFI payload wants to open a protocol on an object to get its
46 * interface (usually a struct with callback functions), this struct maps the
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +020047 * protocol GUID to the respective protocol interface */
Alexander Grafc15d9212016-03-04 01:09:59 +010048struct efi_handler {
49 const efi_guid_t *guid;
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +020050 void *protocol_interface;
Alexander Grafc15d9212016-03-04 01:09:59 +010051};
52
53/*
54 * UEFI has a poor man's OO model where one "object" can be polymorphic and have
55 * multiple different protocols (classes) attached to it.
56 *
57 * This struct is the parent struct for all of our actual implementation objects
58 * that can include it to make themselves an EFI object
59 */
60struct efi_object {
61 /* Every UEFI object is part of a global object list */
62 struct list_head link;
xypron.glpk@gmx.decb486a42017-07-11 22:06:23 +020063 /* We support up to 8 "protocols" an object can be accessed through */
64 struct efi_handler protocols[8];
Alexander Grafc15d9212016-03-04 01:09:59 +010065 /* The object spawner can either use this for data or as identifier */
66 void *handle;
67};
68
Rob Clark64ca8eb2017-07-24 10:39:00 -040069#define EFI_PROTOCOL_OBJECT(_guid, _protocol) (struct efi_object){ \
70 .protocols = {{ \
71 .guid = &(_guid), \
72 .protocol_interface = (void *)(_protocol), \
73 }}, \
74 .handle = (void *)(_protocol), \
75}
76
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +020077/**
78 * struct efi_event
79 *
80 * @type: Type of event, see efi_create_event
81 * @notify_tpl: Task priority level of notifications
82 * @trigger_time: Period of the timer
83 * @trigger_next: Next time to trigger the timer
84 * @nofify_function: Function to call when the event is triggered
85 * @notify_context: Data to be passed to the notify function
86 * @trigger_type: Type of timer, see efi_set_timer
87 * @signaled: The notify function was already called
88 */
89struct efi_event {
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +020090 uint32_t type;
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +020091 UINTN notify_tpl;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +020092 void (EFIAPI *notify_function)(struct efi_event *event, void *context);
93 void *notify_context;
94 u64 trigger_next;
95 u64 trigger_time;
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +020096 enum efi_timer_delay trigger_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +020097 int signaled;
98};
99
100
Alexander Grafc15d9212016-03-04 01:09:59 +0100101/* This list contains all UEFI objects we know of */
102extern struct list_head efi_obj_list;
103
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200104/* Called by bootefi to make console interface available */
105int efi_console_register(void);
Alexander Grafe83be452016-03-04 01:10:02 +0100106/* Called by bootefi to make all disk storage accessible as EFI objects */
107int efi_disk_register(void);
Alexander Grafd65783d2016-03-15 18:38:21 +0100108/* Called by bootefi to make GOP (graphical) interface available */
109int efi_gop_register(void);
Alexander Graf94c4b992016-05-06 21:01:01 +0200110/* Called by bootefi to make the network interface available */
111int efi_net_register(void **handle);
Alexander Graf66f96e12016-08-19 01:23:29 +0200112/* Called by bootefi to make SMBIOS tables available */
113void efi_smbios_register(void);
Alexander Graf94c4b992016-05-06 21:01:01 +0200114
115/* Called by networking code to memorize the dhcp ack package */
116void efi_net_set_dhcp_ack(void *pkt, int len);
117
Alexander Grafc15d9212016-03-04 01:09:59 +0100118/* Called from places to check whether a timer expired */
119void efi_timer_check(void);
120/* PE loader implementation */
Alexander Graf4e0c1c12016-03-04 01:09:58 +0100121void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info);
Alexander Grafc15d9212016-03-04 01:09:59 +0100122/* Called once to store the pristine gd pointer */
123void efi_save_gd(void);
124/* Called from EFI_ENTRY on callback entry to put gd into the gd register */
125void efi_restore_gd(void);
126/* Called from EFI_EXIT on callback exit to restore the gd register */
127efi_status_t efi_exit_func(efi_status_t ret);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100128/* Call this to relocate the runtime section to an address space */
129void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map);
Alexander Graf8f19f262016-03-04 01:10:14 +0100130/* Call this to set the current device name */
Alexander Grafc49a1b82016-04-11 16:16:19 +0200131void efi_set_bootdev(const char *dev, const char *devnr, const char *path);
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200132/* Call this to create an event */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200133efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200134 void (EFIAPI *notify_function) (
135 struct efi_event *event,
136 void *context),
137 void *notify_context, struct efi_event **event);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200138/* Call this to set a timer */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200139efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200140 uint64_t trigger_time);
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200141/* Call this to signal an event */
142void efi_signal_event(struct efi_event *event);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100143
Alexander Graf8623f922016-03-04 01:10:04 +0100144/* Generic EFI memory allocator, call this to get memory */
145void *efi_alloc(uint64_t len, int memory_type);
146/* More specific EFI memory allocator, called by EFI payloads */
147efi_status_t efi_allocate_pages(int type, int memory_type, unsigned long pages,
148 uint64_t *memory);
Stefan Brünsa4f43022016-10-01 23:32:27 +0200149/* EFI memory free function. */
Alexander Graf8623f922016-03-04 01:10:04 +0100150efi_status_t efi_free_pages(uint64_t memory, unsigned long pages);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200151/* EFI memory allocator for small allocations */
152efi_status_t efi_allocate_pool(int pool_type, unsigned long size,
153 void **buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200154/* EFI pool memory free function. */
155efi_status_t efi_free_pool(void *buffer);
Alexander Graf8623f922016-03-04 01:10:04 +0100156/* Returns the EFI memory map */
157efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
158 struct efi_mem_desc *memory_map,
159 unsigned long *map_key,
160 unsigned long *descriptor_size,
161 uint32_t *descriptor_version);
162/* Adds a range into the EFI memory map */
163uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
164 bool overlap_only_ram);
165/* Called by board init to initialize the EFI memory map */
166int efi_memory_init(void);
Alexander Grafc5c11632016-08-19 01:23:24 +0200167/* Adds new or overrides configuration table entry to the system table */
168efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table);
Alexander Graf8623f922016-03-04 01:10:04 +0100169
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200170#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
171extern void *efi_bounce_buffer;
172#define EFI_LOADER_BOUNCE_BUFFER_SIZE (64 * 1024 * 1024)
173#endif
174
Alexander Graf8f19f262016-03-04 01:10:14 +0100175/* Convert strings from normal C strings to uEFI strings */
Simon Glass302d4f82016-05-14 14:03:05 -0600176static inline void ascii2unicode(u16 *unicode, const char *ascii)
Alexander Graf8f19f262016-03-04 01:10:14 +0100177{
178 while (*ascii)
179 *(unicode++) = *(ascii++);
180}
181
Rob Clark544df3b2017-07-24 07:59:11 -0400182static inline int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2)
183{
184 return memcmp(g1, g2, sizeof(efi_guid_t));
185}
186
Alexander Graf0bd425a2016-03-04 01:10:01 +0100187/*
188 * Use these to indicate that your code / data should go into the EFI runtime
189 * section and thus still be available when the OS is running
190 */
Alexander Graf393dd912016-10-14 13:45:30 +0200191#define __efi_runtime_data __attribute__ ((section ("efi_runtime_data")))
192#define __efi_runtime __attribute__ ((section ("efi_runtime_text")))
Alexander Grafc15d9212016-03-04 01:09:59 +0100193
Alexander Graf77256092016-08-16 21:08:45 +0200194/* Call this with mmio_ptr as the _pointer_ to a pointer to an MMIO region
195 * to make it available at runtime */
196void efi_add_runtime_mmio(void *mmio_ptr, u64 len);
197
198/* Boards may provide the functions below to implement RTS functionality */
199
Alexander Graf393dd912016-10-14 13:45:30 +0200200void __efi_runtime EFIAPI efi_reset_system(
Alexander Graf77256092016-08-16 21:08:45 +0200201 enum efi_reset_type reset_type,
202 efi_status_t reset_status,
203 unsigned long data_size, void *reset_data);
204void efi_reset_system_init(void);
205
Alexander Graf393dd912016-10-14 13:45:30 +0200206efi_status_t __efi_runtime EFIAPI efi_get_time(
Alexander Graf77256092016-08-16 21:08:45 +0200207 struct efi_time *time,
208 struct efi_time_cap *capabilities);
209void efi_get_time_init(void);
210
Alexander Grafc15d9212016-03-04 01:09:59 +0100211#else /* defined(EFI_LOADER) && !defined(CONFIG_SPL_BUILD) */
212
Alexander Graf0bd425a2016-03-04 01:10:01 +0100213/* Without CONFIG_EFI_LOADER we don't have a runtime section, stub it out */
Alexander Graf393dd912016-10-14 13:45:30 +0200214#define __efi_runtime_data
215#define __efi_runtime
Alexander Graf4c09ffa2016-11-17 01:02:56 +0100216static inline void efi_add_runtime_mmio(void *mmio_ptr, u64 len) { }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100217
Alexander Grafc15d9212016-03-04 01:09:59 +0100218/* No loader configured, stub out EFI_ENTRY */
219static inline void efi_restore_gd(void) { }
Alexander Grafc49a1b82016-04-11 16:16:19 +0200220static inline void efi_set_bootdev(const char *dev, const char *devnr,
221 const char *path) { }
Alexander Graf94c4b992016-05-06 21:01:01 +0200222static inline void efi_net_set_dhcp_ack(void *pkt, int len) { }
Alexander Grafc15d9212016-03-04 01:09:59 +0100223
224#endif