1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * EFI application loader
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (c) 2016 Alexander Graf
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <common.h>
10*4882a593Smuzhiyun #include <part_efi.h>
11*4882a593Smuzhiyun #include <efi_api.h>
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun /* No need for efi loader support in SPL */
14*4882a593Smuzhiyun #if defined(CONFIG_EFI_LOADER) && !defined(CONFIG_SPL_BUILD)
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include <linux/list.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun int __efi_entry_check(void);
19*4882a593Smuzhiyun int __efi_exit_check(void);
20*4882a593Smuzhiyun const char *__efi_nesting_inc(void);
21*4882a593Smuzhiyun const char *__efi_nesting_dec(void);
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun * Enter the u-boot world from UEFI:
25*4882a593Smuzhiyun */
26*4882a593Smuzhiyun #define EFI_ENTRY(format, ...) do { \
27*4882a593Smuzhiyun assert(__efi_entry_check()); \
28*4882a593Smuzhiyun debug("%sEFI: Entry %s(" format ")\n", __efi_nesting_inc(), \
29*4882a593Smuzhiyun __func__, ##__VA_ARGS__); \
30*4882a593Smuzhiyun } while(0)
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun * Exit the u-boot world back to UEFI:
34*4882a593Smuzhiyun */
35*4882a593Smuzhiyun #define EFI_EXIT(ret) ({ \
36*4882a593Smuzhiyun typeof(ret) _r = ret; \
37*4882a593Smuzhiyun debug("%sEFI: Exit: %s: %u\n", __efi_nesting_dec(), \
38*4882a593Smuzhiyun __func__, (u32)((uintptr_t) _r & ~EFI_ERROR_MASK)); \
39*4882a593Smuzhiyun assert(__efi_exit_check()); \
40*4882a593Smuzhiyun _r; \
41*4882a593Smuzhiyun })
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /*
44*4882a593Smuzhiyun * Callback into UEFI world from u-boot:
45*4882a593Smuzhiyun */
46*4882a593Smuzhiyun #define EFI_CALL(exp) do { \
47*4882a593Smuzhiyun debug("%sEFI: Call: %s\n", __efi_nesting_inc(), #exp); \
48*4882a593Smuzhiyun assert(__efi_exit_check()); \
49*4882a593Smuzhiyun exp; \
50*4882a593Smuzhiyun assert(__efi_entry_check()); \
51*4882a593Smuzhiyun debug("%sEFI: Return From: %s\n", __efi_nesting_dec(), #exp); \
52*4882a593Smuzhiyun } while(0)
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun extern struct efi_runtime_services efi_runtime_services;
55*4882a593Smuzhiyun extern struct efi_system_table systab;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun extern const struct efi_simple_text_output_protocol efi_con_out;
58*4882a593Smuzhiyun extern struct efi_simple_input_interface efi_con_in;
59*4882a593Smuzhiyun extern const struct efi_console_control_protocol efi_console_control;
60*4882a593Smuzhiyun extern const struct efi_device_path_to_text_protocol efi_device_path_to_text;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun extern const efi_guid_t efi_guid_console_control;
63*4882a593Smuzhiyun extern const efi_guid_t efi_guid_device_path;
64*4882a593Smuzhiyun extern const efi_guid_t efi_guid_loaded_image;
65*4882a593Smuzhiyun extern const efi_guid_t efi_guid_device_path_to_text_protocol;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun extern unsigned int __efi_runtime_start, __efi_runtime_stop;
68*4882a593Smuzhiyun extern unsigned int __efi_runtime_rel_start, __efi_runtime_rel_stop;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /*
71*4882a593Smuzhiyun * When the UEFI payload wants to open a protocol on an object to get its
72*4882a593Smuzhiyun * interface (usually a struct with callback functions), this struct maps the
73*4882a593Smuzhiyun * protocol GUID to the respective protocol interface */
74*4882a593Smuzhiyun struct efi_handler {
75*4882a593Smuzhiyun const efi_guid_t *guid;
76*4882a593Smuzhiyun void *protocol_interface;
77*4882a593Smuzhiyun };
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /*
80*4882a593Smuzhiyun * UEFI has a poor man's OO model where one "object" can be polymorphic and have
81*4882a593Smuzhiyun * multiple different protocols (classes) attached to it.
82*4882a593Smuzhiyun *
83*4882a593Smuzhiyun * This struct is the parent struct for all of our actual implementation objects
84*4882a593Smuzhiyun * that can include it to make themselves an EFI object
85*4882a593Smuzhiyun */
86*4882a593Smuzhiyun struct efi_object {
87*4882a593Smuzhiyun /* Every UEFI object is part of a global object list */
88*4882a593Smuzhiyun struct list_head link;
89*4882a593Smuzhiyun /* We support up to 8 "protocols" an object can be accessed through */
90*4882a593Smuzhiyun struct efi_handler protocols[8];
91*4882a593Smuzhiyun /* The object spawner can either use this for data or as identifier */
92*4882a593Smuzhiyun void *handle;
93*4882a593Smuzhiyun };
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun #define EFI_PROTOCOL_OBJECT(_guid, _protocol) (struct efi_object){ \
96*4882a593Smuzhiyun .protocols = {{ \
97*4882a593Smuzhiyun .guid = &(_guid), \
98*4882a593Smuzhiyun .protocol_interface = (void *)(_protocol), \
99*4882a593Smuzhiyun }}, \
100*4882a593Smuzhiyun .handle = (void *)(_protocol), \
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /**
104*4882a593Smuzhiyun * struct efi_event
105*4882a593Smuzhiyun *
106*4882a593Smuzhiyun * @type: Type of event, see efi_create_event
107*4882a593Smuzhiyun * @notify_tpl: Task priority level of notifications
108*4882a593Smuzhiyun * @trigger_time: Period of the timer
109*4882a593Smuzhiyun * @trigger_next: Next time to trigger the timer
110*4882a593Smuzhiyun * @nofify_function: Function to call when the event is triggered
111*4882a593Smuzhiyun * @notify_context: Data to be passed to the notify function
112*4882a593Smuzhiyun * @trigger_type: Type of timer, see efi_set_timer
113*4882a593Smuzhiyun * @signaled: The notify function was already called
114*4882a593Smuzhiyun */
115*4882a593Smuzhiyun struct efi_event {
116*4882a593Smuzhiyun uint32_t type;
117*4882a593Smuzhiyun UINTN notify_tpl;
118*4882a593Smuzhiyun void (EFIAPI *notify_function)(struct efi_event *event, void *context);
119*4882a593Smuzhiyun void *notify_context;
120*4882a593Smuzhiyun u64 trigger_next;
121*4882a593Smuzhiyun u64 trigger_time;
122*4882a593Smuzhiyun enum efi_timer_delay trigger_type;
123*4882a593Smuzhiyun int signaled;
124*4882a593Smuzhiyun };
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /* This list contains all UEFI objects we know of */
128*4882a593Smuzhiyun extern struct list_head efi_obj_list;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun /* Called by bootefi to make console interface available */
131*4882a593Smuzhiyun int efi_console_register(void);
132*4882a593Smuzhiyun /* Called by bootefi to make all disk storage accessible as EFI objects */
133*4882a593Smuzhiyun int efi_disk_register(void);
134*4882a593Smuzhiyun /* Called by bootefi to make GOP (graphical) interface available */
135*4882a593Smuzhiyun int efi_gop_register(void);
136*4882a593Smuzhiyun /* Called by bootefi to make the network interface available */
137*4882a593Smuzhiyun int efi_net_register(void);
138*4882a593Smuzhiyun /* Called by bootefi to make SMBIOS tables available */
139*4882a593Smuzhiyun void efi_smbios_register(void);
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun /* Called by networking code to memorize the dhcp ack package */
142*4882a593Smuzhiyun void efi_net_set_dhcp_ack(void *pkt, int len);
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun /* Called from places to check whether a timer expired */
145*4882a593Smuzhiyun void efi_timer_check(void);
146*4882a593Smuzhiyun /* PE loader implementation */
147*4882a593Smuzhiyun void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info);
148*4882a593Smuzhiyun /* Called once to store the pristine gd pointer */
149*4882a593Smuzhiyun void efi_save_gd(void);
150*4882a593Smuzhiyun /* Special case handler for error/abort that just tries to dtrt to get
151*4882a593Smuzhiyun * back to u-boot world */
152*4882a593Smuzhiyun void efi_restore_gd(void);
153*4882a593Smuzhiyun /* Call this to relocate the runtime section to an address space */
154*4882a593Smuzhiyun void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map);
155*4882a593Smuzhiyun /* Call this to set the current device name */
156*4882a593Smuzhiyun void efi_set_bootdev(const char *dev, const char *devnr, const char *path);
157*4882a593Smuzhiyun /* Call this to create an event */
158*4882a593Smuzhiyun efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
159*4882a593Smuzhiyun void (EFIAPI *notify_function) (
160*4882a593Smuzhiyun struct efi_event *event,
161*4882a593Smuzhiyun void *context),
162*4882a593Smuzhiyun void *notify_context, struct efi_event **event);
163*4882a593Smuzhiyun /* Call this to set a timer */
164*4882a593Smuzhiyun efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
165*4882a593Smuzhiyun uint64_t trigger_time);
166*4882a593Smuzhiyun /* Call this to signal an event */
167*4882a593Smuzhiyun void efi_signal_event(struct efi_event *event);
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun /* Generic EFI memory allocator, call this to get memory */
170*4882a593Smuzhiyun void *efi_alloc(uint64_t len, int memory_type);
171*4882a593Smuzhiyun /* More specific EFI memory allocator, called by EFI payloads */
172*4882a593Smuzhiyun efi_status_t efi_allocate_pages(int type, int memory_type, unsigned long pages,
173*4882a593Smuzhiyun uint64_t *memory);
174*4882a593Smuzhiyun /* EFI memory free function. */
175*4882a593Smuzhiyun efi_status_t efi_free_pages(uint64_t memory, unsigned long pages);
176*4882a593Smuzhiyun /* EFI memory allocator for small allocations */
177*4882a593Smuzhiyun efi_status_t efi_allocate_pool(int pool_type, unsigned long size,
178*4882a593Smuzhiyun void **buffer);
179*4882a593Smuzhiyun /* EFI pool memory free function. */
180*4882a593Smuzhiyun efi_status_t efi_free_pool(void *buffer);
181*4882a593Smuzhiyun /* Returns the EFI memory map */
182*4882a593Smuzhiyun efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
183*4882a593Smuzhiyun struct efi_mem_desc *memory_map,
184*4882a593Smuzhiyun unsigned long *map_key,
185*4882a593Smuzhiyun unsigned long *descriptor_size,
186*4882a593Smuzhiyun uint32_t *descriptor_version);
187*4882a593Smuzhiyun /* Adds a range into the EFI memory map */
188*4882a593Smuzhiyun uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
189*4882a593Smuzhiyun bool overlap_only_ram);
190*4882a593Smuzhiyun /* Called by board init to initialize the EFI memory map */
191*4882a593Smuzhiyun int efi_memory_init(void);
192*4882a593Smuzhiyun /* Adds new or overrides configuration table entry to the system table */
193*4882a593Smuzhiyun efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table);
194*4882a593Smuzhiyun void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
195*4882a593Smuzhiyun struct efi_device_path *device_path,
196*4882a593Smuzhiyun struct efi_device_path *file_path);
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
199*4882a593Smuzhiyun extern void *efi_bounce_buffer;
200*4882a593Smuzhiyun #define EFI_LOADER_BOUNCE_BUFFER_SIZE (64 * 1024 * 1024)
201*4882a593Smuzhiyun #endif
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun struct efi_device_path *efi_dp_next(const struct efi_device_path *dp);
205*4882a593Smuzhiyun int efi_dp_match(struct efi_device_path *a, struct efi_device_path *b);
206*4882a593Smuzhiyun struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
207*4882a593Smuzhiyun struct efi_device_path **rem);
208*4882a593Smuzhiyun unsigned efi_dp_size(const struct efi_device_path *dp);
209*4882a593Smuzhiyun struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp);
210*4882a593Smuzhiyun struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
211*4882a593Smuzhiyun const struct efi_device_path *dp2);
212*4882a593Smuzhiyun struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
213*4882a593Smuzhiyun const struct efi_device_path *node);
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun struct efi_device_path *efi_dp_from_dev(struct udevice *dev);
217*4882a593Smuzhiyun struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part);
218*4882a593Smuzhiyun struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
219*4882a593Smuzhiyun const char *path);
220*4882a593Smuzhiyun struct efi_device_path *efi_dp_from_eth(void);
221*4882a593Smuzhiyun void efi_dp_split_file_path(struct efi_device_path *full_path,
222*4882a593Smuzhiyun struct efi_device_path **device_path,
223*4882a593Smuzhiyun struct efi_device_path **file_path);
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun #define EFI_DP_TYPE(_dp, _type, _subtype) \
226*4882a593Smuzhiyun (((_dp)->type == DEVICE_PATH_TYPE_##_type) && \
227*4882a593Smuzhiyun ((_dp)->sub_type == DEVICE_PATH_SUB_TYPE_##_subtype))
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun /* Convert strings from normal C strings to uEFI strings */
ascii2unicode(u16 * unicode,const char * ascii)230*4882a593Smuzhiyun static inline void ascii2unicode(u16 *unicode, const char *ascii)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun while (*ascii)
233*4882a593Smuzhiyun *(unicode++) = *(ascii++);
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun
guidcmp(const efi_guid_t * g1,const efi_guid_t * g2)236*4882a593Smuzhiyun static inline int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun return memcmp(g1, g2, sizeof(efi_guid_t));
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun /*
242*4882a593Smuzhiyun * Use these to indicate that your code / data should go into the EFI runtime
243*4882a593Smuzhiyun * section and thus still be available when the OS is running
244*4882a593Smuzhiyun */
245*4882a593Smuzhiyun #define __efi_runtime_data __attribute__ ((section ("efi_runtime_data")))
246*4882a593Smuzhiyun #define __efi_runtime __attribute__ ((section ("efi_runtime_text")))
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /* Call this with mmio_ptr as the _pointer_ to a pointer to an MMIO region
249*4882a593Smuzhiyun * to make it available at runtime */
250*4882a593Smuzhiyun void efi_add_runtime_mmio(void *mmio_ptr, u64 len);
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /* Boards may provide the functions below to implement RTS functionality */
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun void __efi_runtime EFIAPI efi_reset_system(
255*4882a593Smuzhiyun enum efi_reset_type reset_type,
256*4882a593Smuzhiyun efi_status_t reset_status,
257*4882a593Smuzhiyun unsigned long data_size, void *reset_data);
258*4882a593Smuzhiyun void efi_reset_system_init(void);
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun efi_status_t __efi_runtime EFIAPI efi_get_time(
261*4882a593Smuzhiyun struct efi_time *time,
262*4882a593Smuzhiyun struct efi_time_cap *capabilities);
263*4882a593Smuzhiyun void efi_get_time_init(void);
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun #else /* defined(EFI_LOADER) && !defined(CONFIG_SPL_BUILD) */
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun /* Without CONFIG_EFI_LOADER we don't have a runtime section, stub it out */
268*4882a593Smuzhiyun #define __efi_runtime_data
269*4882a593Smuzhiyun #define __efi_runtime
efi_add_runtime_mmio(void * mmio_ptr,u64 len)270*4882a593Smuzhiyun static inline void efi_add_runtime_mmio(void *mmio_ptr, u64 len) { }
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun /* No loader configured, stub out EFI_ENTRY */
efi_restore_gd(void)273*4882a593Smuzhiyun static inline void efi_restore_gd(void) { }
efi_set_bootdev(const char * dev,const char * devnr,const char * path)274*4882a593Smuzhiyun static inline void efi_set_bootdev(const char *dev, const char *devnr,
275*4882a593Smuzhiyun const char *path) { }
efi_net_set_dhcp_ack(void * pkt,int len)276*4882a593Smuzhiyun static inline void efi_net_set_dhcp_ack(void *pkt, int len) { }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun #endif
279