blob: 9700a88d6998fb6b0331e45fb0975486a4ca0f9d [file] [log] [blame]
Alexander Grafcb149c62016-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 Grafbee91162016-03-04 01:09:59 +01009#include <common.h>
Alexander Grafcb149c62016-03-04 01:09:58 +010010#include <part_efi.h>
11#include <efi_api.h>
Alexander Grafbee91162016-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 Grafcb149c62016-03-04 01:09:58 +010016#include <linux/list.h>
17
Alexander Grafbee91162016-03-04 01:09:59 +010018#define EFI_ENTRY(format, ...) do { \
19 efi_restore_gd(); \
Alexander Grafedcef3b2016-06-02 11:38:27 +020020 debug("EFI: Entry %s(" format ")\n", __func__, ##__VA_ARGS__); \
Alexander Grafbee91162016-03-04 01:09:59 +010021 } while(0)
Alexander Grafbee91162016-03-04 01:09:59 +010022
Rob Clark804b1d72017-07-24 10:31:52 -040023#define EFI_EXIT(ret) ({ \
Rob Clark3f1aa972017-07-27 08:04:16 -040024 efi_status_t _r = ret; \
25 debug("EFI: Exit: %s: %u\n", __func__, (u32)(_r & ~EFI_ERROR_MASK)); \
26 efi_exit_func(_r); \
Rob Clark804b1d72017-07-24 10:31:52 -040027 })
Alexander Grafbee91162016-03-04 01:09:59 +010028
Alexander Graf50149ea2016-03-04 01:10:01 +010029extern struct efi_runtime_services efi_runtime_services;
Alexander Grafbee91162016-03-04 01:09:59 +010030extern struct efi_system_table systab;
31
Alexander Grafc1311ad2016-03-04 01:10:00 +010032extern const struct efi_simple_text_output_protocol efi_con_out;
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +020033extern struct efi_simple_input_interface efi_con_in;
Alexander Grafc1311ad2016-03-04 01:10:00 +010034extern const struct efi_console_control_protocol efi_console_control;
xypron.glpk@gmx.decc5b7082017-07-11 22:06:25 +020035extern const struct efi_device_path_to_text_protocol efi_device_path_to_text;
Alexander Grafc1311ad2016-03-04 01:10:00 +010036
37extern const efi_guid_t efi_guid_console_control;
Alexander Grafcb149c62016-03-04 01:09:58 +010038extern const efi_guid_t efi_guid_device_path;
39extern const efi_guid_t efi_guid_loaded_image;
xypron.glpk@gmx.decc5b7082017-07-11 22:06:25 +020040extern const efi_guid_t efi_guid_device_path_to_text_protocol;
Alexander Grafcb149c62016-03-04 01:09:58 +010041
Alexander Graf50149ea2016-03-04 01:10:01 +010042extern unsigned int __efi_runtime_start, __efi_runtime_stop;
43extern unsigned int __efi_runtime_rel_start, __efi_runtime_rel_stop;
44
Alexander Grafbee91162016-03-04 01:09:59 +010045/*
Alexander Grafbee91162016-03-04 01:09:59 +010046 * When the UEFI payload wants to open a protocol on an object to get its
47 * interface (usually a struct with callback functions), this struct maps the
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +020048 * protocol GUID to the respective protocol interface */
Alexander Grafbee91162016-03-04 01:09:59 +010049struct efi_handler {
50 const efi_guid_t *guid;
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +020051 void *protocol_interface;
Alexander Grafbee91162016-03-04 01:09:59 +010052};
53
54/*
55 * UEFI has a poor man's OO model where one "object" can be polymorphic and have
56 * multiple different protocols (classes) attached to it.
57 *
58 * This struct is the parent struct for all of our actual implementation objects
59 * that can include it to make themselves an EFI object
60 */
61struct efi_object {
62 /* Every UEFI object is part of a global object list */
63 struct list_head link;
xypron.glpk@gmx.de011f4322017-07-11 22:06:23 +020064 /* We support up to 8 "protocols" an object can be accessed through */
65 struct efi_handler protocols[8];
Alexander Grafbee91162016-03-04 01:09:59 +010066 /* The object spawner can either use this for data or as identifier */
67 void *handle;
68};
69
Rob Clark641833d2017-07-24 10:39:00 -040070#define EFI_PROTOCOL_OBJECT(_guid, _protocol) (struct efi_object){ \
71 .protocols = {{ \
72 .guid = &(_guid), \
73 .protocol_interface = (void *)(_protocol), \
74 }}, \
75 .handle = (void *)(_protocol), \
76}
77
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +020078/**
79 * struct efi_event
80 *
81 * @type: Type of event, see efi_create_event
82 * @notify_tpl: Task priority level of notifications
83 * @trigger_time: Period of the timer
84 * @trigger_next: Next time to trigger the timer
85 * @nofify_function: Function to call when the event is triggered
86 * @notify_context: Data to be passed to the notify function
87 * @trigger_type: Type of timer, see efi_set_timer
88 * @signaled: The notify function was already called
89 */
90struct efi_event {
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +020091 uint32_t type;
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +020092 UINTN notify_tpl;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +020093 void (EFIAPI *notify_function)(struct efi_event *event, void *context);
94 void *notify_context;
95 u64 trigger_next;
96 u64 trigger_time;
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +020097 enum efi_timer_delay trigger_type;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +020098 int signaled;
99};
100
101
Alexander Grafbee91162016-03-04 01:09:59 +0100102/* This list contains all UEFI objects we know of */
103extern struct list_head efi_obj_list;
104
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200105/* Called by bootefi to make console interface available */
106int efi_console_register(void);
Alexander Graf2a22d052016-03-04 01:10:02 +0100107/* Called by bootefi to make all disk storage accessible as EFI objects */
108int efi_disk_register(void);
Alexander Grafbe8d3242016-03-15 18:38:21 +0100109/* Called by bootefi to make GOP (graphical) interface available */
110int efi_gop_register(void);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200111/* Called by bootefi to make the network interface available */
112int efi_net_register(void **handle);
Alexander Grafe663b352016-08-19 01:23:29 +0200113/* Called by bootefi to make SMBIOS tables available */
114void efi_smbios_register(void);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200115
116/* Called by networking code to memorize the dhcp ack package */
117void efi_net_set_dhcp_ack(void *pkt, int len);
118
Alexander Grafbee91162016-03-04 01:09:59 +0100119/* Called from places to check whether a timer expired */
120void efi_timer_check(void);
121/* PE loader implementation */
Alexander Grafcb149c62016-03-04 01:09:58 +0100122void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info);
Alexander Grafbee91162016-03-04 01:09:59 +0100123/* Called once to store the pristine gd pointer */
124void efi_save_gd(void);
125/* Called from EFI_ENTRY on callback entry to put gd into the gd register */
126void efi_restore_gd(void);
127/* Called from EFI_EXIT on callback exit to restore the gd register */
128efi_status_t efi_exit_func(efi_status_t ret);
Alexander Graf50149ea2016-03-04 01:10:01 +0100129/* Call this to relocate the runtime section to an address space */
130void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map);
Alexander Graf0f4060e2016-03-04 01:10:14 +0100131/* Call this to set the current device name */
Alexander Grafc07ad7c2016-04-11 16:16:19 +0200132void efi_set_bootdev(const char *dev, const char *devnr, const char *path);
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200133/* Call this to create an event */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200134efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200135 void (EFIAPI *notify_function) (
136 struct efi_event *event,
137 void *context),
138 void *notify_context, struct efi_event **event);
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200139/* Call this to set a timer */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200140efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200141 uint64_t trigger_time);
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200142/* Call this to signal an event */
143void efi_signal_event(struct efi_event *event);
Alexander Graf50149ea2016-03-04 01:10:01 +0100144
Alexander Graf5d009952016-03-04 01:10:04 +0100145/* Generic EFI memory allocator, call this to get memory */
146void *efi_alloc(uint64_t len, int memory_type);
147/* More specific EFI memory allocator, called by EFI payloads */
148efi_status_t efi_allocate_pages(int type, int memory_type, unsigned long pages,
149 uint64_t *memory);
Stefan Brünsb61d8572016-10-01 23:32:27 +0200150/* EFI memory free function. */
Alexander Graf5d009952016-03-04 01:10:04 +0100151efi_status_t efi_free_pages(uint64_t memory, unsigned long pages);
Stefan Brünsead12742016-10-09 22:17:18 +0200152/* EFI memory allocator for small allocations */
153efi_status_t efi_allocate_pool(int pool_type, unsigned long size,
154 void **buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200155/* EFI pool memory free function. */
156efi_status_t efi_free_pool(void *buffer);
Alexander Graf5d009952016-03-04 01:10:04 +0100157/* Returns the EFI memory map */
158efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
159 struct efi_mem_desc *memory_map,
160 unsigned long *map_key,
161 unsigned long *descriptor_size,
162 uint32_t *descriptor_version);
163/* Adds a range into the EFI memory map */
164uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
165 bool overlap_only_ram);
166/* Called by board init to initialize the EFI memory map */
167int efi_memory_init(void);
Alexander Graf488bf122016-08-19 01:23:24 +0200168/* Adds new or overrides configuration table entry to the system table */
169efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table);
Alexander Graf5d009952016-03-04 01:10:04 +0100170
Alexander Graf51735ae2016-05-11 18:25:48 +0200171#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
172extern void *efi_bounce_buffer;
173#define EFI_LOADER_BOUNCE_BUFFER_SIZE (64 * 1024 * 1024)
174#endif
175
Alexander Graf0f4060e2016-03-04 01:10:14 +0100176/* Convert strings from normal C strings to uEFI strings */
Simon Glass487d7562016-05-14 14:03:05 -0600177static inline void ascii2unicode(u16 *unicode, const char *ascii)
Alexander Graf0f4060e2016-03-04 01:10:14 +0100178{
179 while (*ascii)
180 *(unicode++) = *(ascii++);
181}
182
Rob Clark3e094c52017-07-24 07:59:11 -0400183static inline int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2)
184{
185 return memcmp(g1, g2, sizeof(efi_guid_t));
186}
187
Alexander Graf50149ea2016-03-04 01:10:01 +0100188/*
189 * Use these to indicate that your code / data should go into the EFI runtime
190 * section and thus still be available when the OS is running
191 */
Alexander Graf3c63db92016-10-14 13:45:30 +0200192#define __efi_runtime_data __attribute__ ((section ("efi_runtime_data")))
193#define __efi_runtime __attribute__ ((section ("efi_runtime_text")))
Alexander Grafbee91162016-03-04 01:09:59 +0100194
Alexander Graf80a48002016-08-16 21:08:45 +0200195/* Call this with mmio_ptr as the _pointer_ to a pointer to an MMIO region
196 * to make it available at runtime */
197void efi_add_runtime_mmio(void *mmio_ptr, u64 len);
198
199/* Boards may provide the functions below to implement RTS functionality */
200
Alexander Graf3c63db92016-10-14 13:45:30 +0200201void __efi_runtime EFIAPI efi_reset_system(
Alexander Graf80a48002016-08-16 21:08:45 +0200202 enum efi_reset_type reset_type,
203 efi_status_t reset_status,
204 unsigned long data_size, void *reset_data);
205void efi_reset_system_init(void);
206
Alexander Graf3c63db92016-10-14 13:45:30 +0200207efi_status_t __efi_runtime EFIAPI efi_get_time(
Alexander Graf80a48002016-08-16 21:08:45 +0200208 struct efi_time *time,
209 struct efi_time_cap *capabilities);
210void efi_get_time_init(void);
211
Alexander Grafbee91162016-03-04 01:09:59 +0100212#else /* defined(EFI_LOADER) && !defined(CONFIG_SPL_BUILD) */
213
Alexander Graf50149ea2016-03-04 01:10:01 +0100214/* Without CONFIG_EFI_LOADER we don't have a runtime section, stub it out */
Alexander Graf3c63db92016-10-14 13:45:30 +0200215#define __efi_runtime_data
216#define __efi_runtime
Alexander Graf97d01442016-11-17 01:02:56 +0100217static inline void efi_add_runtime_mmio(void *mmio_ptr, u64 len) { }
Alexander Graf50149ea2016-03-04 01:10:01 +0100218
Alexander Grafbee91162016-03-04 01:09:59 +0100219/* No loader configured, stub out EFI_ENTRY */
220static inline void efi_restore_gd(void) { }
Alexander Grafc07ad7c2016-04-11 16:16:19 +0200221static inline void efi_set_bootdev(const char *dev, const char *devnr,
222 const char *path) { }
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200223static inline void efi_net_set_dhcp_ack(void *pkt, int len) { }
Alexander Grafbee91162016-03-04 01:09:59 +0100224
225#endif