xref: /openbmc/u-boot/include/efi.h (revision 0c01c3e8)
1 /*
2  * Extensible Firmware Interface
3  * Based on 'Extensible Firmware Interface Specification' version 0.9,
4  * April 30, 1999
5  *
6  * Copyright (C) 1999 VA Linux Systems
7  * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
8  * Copyright (C) 1999, 2002-2003 Hewlett-Packard Co.
9  *	David Mosberger-Tang <davidm@hpl.hp.com>
10  *	Stephane Eranian <eranian@hpl.hp.com>
11  *
12  * From include/linux/efi.h in kernel 4.1 with some additions/subtractions
13  */
14 
15 #ifndef _EFI_H
16 #define _EFI_H
17 
18 #include <linux/string.h>
19 #include <linux/types.h>
20 
21 #ifdef CONFIG_EFI_STUB_64BIT
22 /* EFI uses the Microsoft ABI which is not the default for GCC */
23 #define EFIAPI __attribute__((ms_abi))
24 #else
25 #define EFIAPI
26 #endif
27 
28 struct efi_device_path;
29 
30 #define EFI_SUCCESS		0
31 #define EFI_LOAD_ERROR		(1 | (1UL << (BITS_PER_LONG - 1)))
32 #define EFI_INVALID_PARAMETER	(2 | (1UL << (BITS_PER_LONG - 1)))
33 #define EFI_UNSUPPORTED		(3 | (1UL << (BITS_PER_LONG - 1)))
34 #define EFI_BAD_BUFFER_SIZE	(4 | (1UL << (BITS_PER_LONG - 1)))
35 #define EFI_BUFFER_TOO_SMALL	(5 | (1UL << (BITS_PER_LONG - 1)))
36 #define EFI_NOT_READY		(6 | (1UL << (BITS_PER_LONG - 1)))
37 #define EFI_DEVICE_ERROR	(7 | (1UL << (BITS_PER_LONG - 1)))
38 #define EFI_WRITE_PROTECTED	(8 | (1UL << (BITS_PER_LONG - 1)))
39 #define EFI_OUT_OF_RESOURCES	(9 | (1UL << (BITS_PER_LONG - 1)))
40 #define EFI_NOT_FOUND		(14 | (1UL << (BITS_PER_LONG - 1)))
41 #define EFI_SECURITY_VIOLATION	(26 | (1UL << (BITS_PER_LONG - 1)))
42 
43 typedef unsigned long efi_status_t;
44 typedef u64 efi_physical_addr_t;
45 typedef u64 efi_virtual_addr_t;
46 typedef void *efi_handle_t;
47 
48 #define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
49 	((efi_guid_t) \
50 	{{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, \
51 		((a) >> 24) & 0xff, \
52 		(b) & 0xff, ((b) >> 8) & 0xff, \
53 		(c) & 0xff, ((c) >> 8) & 0xff, \
54 		(d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) } })
55 
56 /* Generic EFI table header */
57 struct efi_table_hdr {
58 	u64 signature;
59 	u32 revision;
60 	u32 headersize;
61 	u32 crc32;
62 	u32 reserved;
63 };
64 
65 /* Enumeration of memory types introduced in UEFI */
66 enum efi_mem_type {
67 	EFI_RESERVED_MEMORY_TYPE,
68 	/*
69 	 * The code portions of a loaded application.
70 	 * (Note that UEFI OS loaders are UEFI applications.)
71 	 */
72 	EFI_LOADER_CODE,
73 	/*
74 	 * The data portions of a loaded application and
75 	 * the default data allocation type used by an application
76 	 * to allocate pool memory.
77 	 */
78 	EFI_LOADER_DATA,
79 	/* The code portions of a loaded Boot Services Driver */
80 	EFI_BOOT_SERVICES_CODE,
81 	/*
82 	 * The data portions of a loaded Boot Serves Driver and
83 	 * the default data allocation type used by a Boot Services
84 	 * Driver to allocate pool memory.
85 	 */
86 	EFI_BOOT_SERVICES_DATA,
87 	/* The code portions of a loaded Runtime Services Driver */
88 	EFI_RUNTIME_SERVICES_CODE,
89 	/*
90 	 * The data portions of a loaded Runtime Services Driver and
91 	 * the default data allocation type used by a Runtime Services
92 	 * Driver to allocate pool memory.
93 	 */
94 	EFI_RUNTIME_SERVICES_DATA,
95 	/* Free (unallocated) memory */
96 	EFI_CONVENTIONAL_MEMORY,
97 	/* Memory in which errors have been detected */
98 	EFI_UNUSABLE_MEMORY,
99 	/* Memory that holds the ACPI tables */
100 	EFI_ACPI_RECLAIM_MEMORY,
101 	/* Address space reserved for use by the firmware */
102 	EFI_ACPI_MEMORY_NVS,
103 	/*
104 	 * Used by system firmware to request that a memory-mapped IO region
105 	 * be mapped by the OS to a virtual address so it can be accessed by
106 	 * EFI runtime services.
107 	 */
108 	EFI_MMAP_IO,
109 	/*
110 	 * System memory-mapped IO region that is used to translate
111 	 * memory cycles to IO cycles by the processor.
112 	 */
113 	EFI_MMAP_IO_PORT,
114 	/*
115 	 * Address space reserved by the firmware for code that is
116 	 * part of the processor.
117 	 */
118 	EFI_PAL_CODE,
119 
120 	EFI_MAX_MEMORY_TYPE,
121 	EFI_TABLE_END,	/* For efi_build_mem_table() */
122 };
123 
124 /* Attribute values */
125 enum {
126 	EFI_MEMORY_UC_SHIFT	= 0,	/* uncached */
127 	EFI_MEMORY_WC_SHIFT	= 1,	/* write-coalescing */
128 	EFI_MEMORY_WT_SHIFT	= 2,	/* write-through */
129 	EFI_MEMORY_WB_SHIFT	= 3,	/* write-back */
130 	EFI_MEMORY_UCE_SHIFT	= 4,	/* uncached, exported */
131 	EFI_MEMORY_WP_SHIFT	= 12,	/* write-protect */
132 	EFI_MEMORY_RP_SHIFT	= 13,	/* read-protect */
133 	EFI_MEMORY_XP_SHIFT	= 14,	/* execute-protect */
134 	EFI_MEMORY_RUNTIME_SHIFT = 63,	/* range requires runtime mapping */
135 
136 	EFI_MEMORY_RUNTIME = 1ULL << EFI_MEMORY_RUNTIME_SHIFT,
137 	EFI_MEM_DESC_VERSION	= 1,
138 };
139 
140 #define EFI_PAGE_SHIFT		12
141 #define EFI_PAGE_SIZE		(1UL << EFI_PAGE_SHIFT)
142 
143 struct efi_mem_desc {
144 	u32 type;
145 	u32 reserved;
146 	efi_physical_addr_t physical_start;
147 	efi_virtual_addr_t virtual_start;
148 	u64 num_pages;
149 	u64 attribute;
150 };
151 
152 /* Allocation types for calls to boottime->allocate_pages*/
153 #define EFI_ALLOCATE_ANY_PAGES		0
154 #define EFI_ALLOCATE_MAX_ADDRESS	1
155 #define EFI_ALLOCATE_ADDRESS		2
156 #define EFI_MAX_ALLOCATE_TYPE		3
157 
158 /* Types and defines for Time Services */
159 #define EFI_TIME_ADJUST_DAYLIGHT 0x1
160 #define EFI_TIME_IN_DAYLIGHT     0x2
161 #define EFI_UNSPECIFIED_TIMEZONE 0x07ff
162 
163 struct efi_time {
164 	u16 year;
165 	u8 month;
166 	u8 day;
167 	u8 hour;
168 	u8 minute;
169 	u8 second;
170 	u8 pad1;
171 	u32 nanosecond;
172 	s16 timezone;
173 	u8 daylight;
174 	u8 pad2;
175 };
176 
177 struct efi_time_cap {
178 	u32 resolution;
179 	u32 accuracy;
180 	u8 sets_to_zero;
181 };
182 
183 enum efi_locate_search_type {
184 	all_handles,
185 	by_register_notify,
186 	by_protocol
187 };
188 
189 struct efi_open_protocol_info_entry {
190 	efi_handle_t agent_handle;
191 	efi_handle_t controller_handle;
192 	u32 attributes;
193 	u32 open_count;
194 };
195 
196 enum efi_entry_t {
197 	EFIET_END,	/* Signals this is the last (empty) entry */
198 	EFIET_MEMORY_MAP,
199 
200 	/* Number of entries */
201 	EFIET_MEMORY_COUNT,
202 };
203 
204 #define EFI_TABLE_VERSION	1
205 
206 /**
207  * struct efi_info_hdr - Header for the EFI info table
208  *
209  * @version:	EFI_TABLE_VERSION
210  * @hdr_size:	Size of this struct in bytes
211  * @total_size:	Total size of this header plus following data
212  * @spare:	Spare space for expansion
213  */
214 struct efi_info_hdr {
215 	u32 version;
216 	u32 hdr_size;
217 	u32 total_size;
218 	u32 spare[5];
219 };
220 
221 /**
222  * struct efi_entry_hdr - Header for a table entry
223  *
224  * @type:	enum eft_entry_t
225  * @size	size of entry bytes excluding header and padding
226  * @addr:	address of this entry (0 if it follows the header )
227  * @link:	size of entry including header and padding
228  * @spare1:	Spare space for expansion
229  * @spare2:	Spare space for expansion
230  */
231 struct efi_entry_hdr {
232 	u32 type;
233 	u32 size;
234 	u64 addr;
235 	u32 link;
236 	u32 spare1;
237 	u64 spare2;
238 };
239 
240 /**
241  * struct efi_entry_memmap - a memory map table passed to U-Boot
242  *
243  * @version:	EFI's memory map table version
244  * @desc_size:	EFI's size of each memory descriptor
245  * @spare:	Spare space for expansion
246  * @desc:	An array of descriptors, each @desc_size bytes apart
247  */
248 struct efi_entry_memmap {
249 	u32 version;
250 	u32 desc_size;
251 	u64 spare;
252 	struct efi_mem_desc desc[];
253 };
254 
255 static inline struct efi_mem_desc *efi_get_next_mem_desc(
256 		struct efi_entry_memmap *map, struct efi_mem_desc *desc)
257 {
258 	return (struct efi_mem_desc *)((ulong)desc + map->desc_size);
259 }
260 
261 struct efi_priv {
262 	efi_handle_t parent_image;
263 	struct efi_device_path *device_path;
264 	struct efi_system_table *sys_table;
265 	struct efi_boot_services *boot;
266 	struct efi_runtime_services *run;
267 	bool use_pool_for_malloc;
268 	unsigned long ram_base;
269 	unsigned int image_data_type;
270 	struct efi_info_hdr *info;
271 	unsigned int info_size;
272 	void *next_hdr;
273 };
274 
275 /* Base address of the EFI image */
276 extern char image_base[];
277 
278 /* Start and end of U-Boot image (for payload) */
279 extern char _binary_u_boot_dtb_bin_start[], _binary_u_boot_dtb_bin_end[];
280 
281 /**
282  * efi_get_sys_table() - Get access to the main EFI system table
283  *
284  * @return pointer to EFI system table
285  */
286 
287 struct efi_system_table *efi_get_sys_table(void);
288 
289 /**
290  * efi_get_ram_base() - Find the base of RAM
291  *
292  * This is used when U-Boot is built as an EFI application.
293  *
294  * @return the base of RAM as known to U-Boot
295  */
296 unsigned long efi_get_ram_base(void);
297 
298 /**
299  * efi_init() - Set up ready for use of EFI boot services
300  *
301  * @priv:	Pointer to our private EFI structure to fill in
302  * @banner:	Banner to display when starting
303  * @image:	The image handle passed to efi_main()
304  * @sys_table:	The EFI system table pointer passed to efi_main()
305  */
306 int efi_init(struct efi_priv *priv, const char *banner, efi_handle_t image,
307 	     struct efi_system_table *sys_table);
308 
309 /**
310  * efi_malloc() - Allocate some memory from EFI
311  *
312  * @priv:	Pointer to private EFI structure
313  * @size:	Number of bytes to allocate
314  * @retp:	Return EFI status result
315  * @return pointer to memory allocated, or NULL on error
316  */
317 void *efi_malloc(struct efi_priv *priv, int size, efi_status_t *retp);
318 
319 /**
320  * efi_free() - Free memory allocated from EFI
321  *
322  * @priv:	Pointer to private EFI structure
323  * @ptr:	Pointer to memory to free
324  */
325 void efi_free(struct efi_priv *priv, void *ptr);
326 
327 /**
328  * efi_puts() - Write out a string to the EFI console
329  *
330  * @priv:	Pointer to private EFI structure
331  * @str:	String to write (note this is a ASCII, not unicode)
332  */
333 void efi_puts(struct efi_priv *priv, const char *str);
334 
335 /**
336  * efi_putc() - Write out a character to the EFI console
337  *
338  * @priv:	Pointer to private EFI structure
339  * @ch:		Character to write (note this is not unicode)
340  */
341 void efi_putc(struct efi_priv *priv, const char ch);
342 
343 /**
344  * efi_info_get() - get an entry from an EFI table
345  *
346  * @type:	Entry type to search for
347  * @datap:	Returns pointer to entry data
348  * @sizep:	Returns pointer to entry size
349  * @return 0 if OK, -ENODATA if there is no table, -ENOENT if there is no entry
350  * of the requested type, -EPROTONOSUPPORT if the table has the wrong version
351  */
352 int efi_info_get(enum efi_entry_t type, void **datap, int *sizep);
353 
354 /**
355  * efi_build_mem_table() - make a sorted copy of the memory table
356  *
357  * @map:	Pointer to EFI memory map table
358  * @size:	Size of table in bytes
359  * @skip_bs:	True to skip boot-time memory and merge it with conventional
360  *		memory. This will significantly reduce the number of table
361  *		entries.
362  * @return pointer to the new table. It should be freed with free() by the
363  *	   caller
364  */
365 void *efi_build_mem_table(struct efi_entry_memmap *map, int size, bool skip_bs);
366 
367 #endif /* _LINUX_EFI_H */
368