xref: /openbmc/u-boot/include/efi.h (revision 9865543a)
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/linkage.h>
19 #include <linux/string.h>
20 #include <linux/types.h>
21 
22 /*
23  * EFI on x86_64 uses the Microsoft ABI which is not the default for GCC.
24  *
25  * There are two scenarios for EFI on x86_64: building a 64-bit EFI stub
26  * codes (CONFIG_EFI_STUB_64BIT) and building a 64-bit U-Boot (CONFIG_X86_64).
27  * Either needs to be properly built with the '-m64' compiler flag, and hence
28  * it is enough to only check the compiler provided define __x86_64__ here.
29  */
30 #ifdef __x86_64__
31 #define EFIAPI __attribute__((ms_abi))
32 #define efi_va_list __builtin_ms_va_list
33 #define efi_va_start __builtin_ms_va_start
34 #define efi_va_arg __builtin_va_arg
35 #define efi_va_end __builtin_ms_va_end
36 #else
37 #define EFIAPI asmlinkage
38 #define efi_va_list va_list
39 #define efi_va_start va_start
40 #define efi_va_arg va_arg
41 #define efi_va_end va_end
42 #endif /* __x86_64__ */
43 
44 #define EFI32_LOADER_SIGNATURE	"EL32"
45 #define EFI64_LOADER_SIGNATURE	"EL64"
46 
47 struct efi_device_path;
48 
49 typedef struct {
50 	u8 b[16];
51 } efi_guid_t;
52 
53 #define EFI_BITS_PER_LONG	(sizeof(long) * 8)
54 
55 /* Bit mask for EFI status code with error */
56 #define EFI_ERROR_MASK (1UL << (EFI_BITS_PER_LONG - 1))
57 /* Status codes returned by EFI protocols */
58 #define EFI_SUCCESS			0
59 #define EFI_LOAD_ERROR			(EFI_ERROR_MASK | 1)
60 #define EFI_INVALID_PARAMETER		(EFI_ERROR_MASK | 2)
61 #define EFI_UNSUPPORTED			(EFI_ERROR_MASK | 3)
62 #define EFI_BAD_BUFFER_SIZE		(EFI_ERROR_MASK | 4)
63 #define EFI_BUFFER_TOO_SMALL		(EFI_ERROR_MASK | 5)
64 #define EFI_NOT_READY			(EFI_ERROR_MASK | 6)
65 #define EFI_DEVICE_ERROR		(EFI_ERROR_MASK | 7)
66 #define EFI_WRITE_PROTECTED		(EFI_ERROR_MASK | 8)
67 #define EFI_OUT_OF_RESOURCES		(EFI_ERROR_MASK | 9)
68 #define EFI_VOLUME_CORRUPTED		(EFI_ERROR_MASK | 10)
69 #define EFI_VOLUME_FULL			(EFI_ERROR_MASK | 11)
70 #define EFI_NO_MEDIA			(EFI_ERROR_MASK | 12)
71 #define EFI_MEDIA_CHANGED		(EFI_ERROR_MASK | 13)
72 #define EFI_NOT_FOUND			(EFI_ERROR_MASK | 14)
73 #define EFI_ACCESS_DENIED		(EFI_ERROR_MASK | 15)
74 #define EFI_NO_RESPONSE			(EFI_ERROR_MASK | 16)
75 #define EFI_NO_MAPPING			(EFI_ERROR_MASK | 17)
76 #define EFI_TIMEOUT			(EFI_ERROR_MASK | 18)
77 #define EFI_NOT_STARTED			(EFI_ERROR_MASK | 19)
78 #define EFI_ALREADY_STARTED		(EFI_ERROR_MASK | 20)
79 #define EFI_ABORTED			(EFI_ERROR_MASK | 21)
80 #define EFI_ICMP_ERROR			(EFI_ERROR_MASK | 22)
81 #define EFI_TFTP_ERROR			(EFI_ERROR_MASK | 23)
82 #define EFI_PROTOCOL_ERROR		(EFI_ERROR_MASK | 24)
83 #define EFI_INCOMPATIBLE_VERSION	(EFI_ERROR_MASK | 25)
84 #define EFI_SECURITY_VIOLATION		(EFI_ERROR_MASK | 26)
85 #define EFI_CRC_ERROR			(EFI_ERROR_MASK | 27)
86 #define EFI_END_OF_MEDIA		(EFI_ERROR_MASK | 28)
87 #define EFI_END_OF_FILE			(EFI_ERROR_MASK | 31)
88 #define EFI_INVALID_LANGUAGE		(EFI_ERROR_MASK | 32)
89 #define EFI_COMPROMISED_DATA		(EFI_ERROR_MASK | 33)
90 #define EFI_IP_ADDRESS_CONFLICT		(EFI_ERROR_MASK | 34)
91 #define EFI_HTTP_ERROR			(EFI_ERROR_MASK | 35)
92 
93 #define EFI_WARN_DELETE_FAILURE	2
94 
95 typedef unsigned long efi_status_t;
96 typedef u64 efi_physical_addr_t;
97 typedef u64 efi_virtual_addr_t;
98 typedef void *efi_handle_t;
99 
100 #define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
101 	{{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, \
102 		((a) >> 24) & 0xff, \
103 		(b) & 0xff, ((b) >> 8) & 0xff, \
104 		(c) & 0xff, ((c) >> 8) & 0xff, \
105 		(d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) } }
106 
107 /* Generic EFI table header */
108 struct efi_table_hdr {
109 	u64 signature;
110 	u32 revision;
111 	u32 headersize;
112 	u32 crc32;
113 	u32 reserved;
114 };
115 
116 /* Enumeration of memory types introduced in UEFI */
117 enum efi_mem_type {
118 	EFI_RESERVED_MEMORY_TYPE,
119 	/*
120 	 * The code portions of a loaded application.
121 	 * (Note that UEFI OS loaders are UEFI applications.)
122 	 */
123 	EFI_LOADER_CODE,
124 	/*
125 	 * The data portions of a loaded application and
126 	 * the default data allocation type used by an application
127 	 * to allocate pool memory.
128 	 */
129 	EFI_LOADER_DATA,
130 	/* The code portions of a loaded Boot Services Driver */
131 	EFI_BOOT_SERVICES_CODE,
132 	/*
133 	 * The data portions of a loaded Boot Services Driver and
134 	 * the default data allocation type used by a Boot Services
135 	 * Driver to allocate pool memory.
136 	 */
137 	EFI_BOOT_SERVICES_DATA,
138 	/* The code portions of a loaded Runtime Services Driver */
139 	EFI_RUNTIME_SERVICES_CODE,
140 	/*
141 	 * The data portions of a loaded Runtime Services Driver and
142 	 * the default data allocation type used by a Runtime Services
143 	 * Driver to allocate pool memory.
144 	 */
145 	EFI_RUNTIME_SERVICES_DATA,
146 	/* Free (unallocated) memory */
147 	EFI_CONVENTIONAL_MEMORY,
148 	/* Memory in which errors have been detected */
149 	EFI_UNUSABLE_MEMORY,
150 	/* Memory that holds the ACPI tables */
151 	EFI_ACPI_RECLAIM_MEMORY,
152 	/* Address space reserved for use by the firmware */
153 	EFI_ACPI_MEMORY_NVS,
154 	/*
155 	 * Used by system firmware to request that a memory-mapped IO region
156 	 * be mapped by the OS to a virtual address so it can be accessed by
157 	 * EFI runtime services.
158 	 */
159 	EFI_MMAP_IO,
160 	/*
161 	 * System memory-mapped IO region that is used to translate
162 	 * memory cycles to IO cycles by the processor.
163 	 */
164 	EFI_MMAP_IO_PORT,
165 	/*
166 	 * Address space reserved by the firmware for code that is
167 	 * part of the processor.
168 	 */
169 	EFI_PAL_CODE,
170 
171 	EFI_MAX_MEMORY_TYPE,
172 	EFI_TABLE_END,	/* For efi_build_mem_table() */
173 };
174 
175 /* Attribute values */
176 #define EFI_MEMORY_UC		((u64)0x0000000000000001ULL)	/* uncached */
177 #define EFI_MEMORY_WC		((u64)0x0000000000000002ULL)	/* write-coalescing */
178 #define EFI_MEMORY_WT		((u64)0x0000000000000004ULL)	/* write-through */
179 #define EFI_MEMORY_WB		((u64)0x0000000000000008ULL)	/* write-back */
180 #define EFI_MEMORY_UCE		((u64)0x0000000000000010ULL)	/* uncached, exported */
181 #define EFI_MEMORY_WP		((u64)0x0000000000001000ULL)	/* write-protect */
182 #define EFI_MEMORY_RP		((u64)0x0000000000002000ULL)	/* read-protect */
183 #define EFI_MEMORY_XP		((u64)0x0000000000004000ULL)	/* execute-protect */
184 #define EFI_MEMORY_NV		((u64)0x0000000000008000ULL)	/* non-volatile */
185 #define EFI_MEMORY_MORE_RELIABLE \
186 				((u64)0x0000000000010000ULL)	/* higher reliability */
187 #define EFI_MEMORY_RO		((u64)0x0000000000020000ULL)	/* read-only */
188 #define EFI_MEMORY_RUNTIME	((u64)0x8000000000000000ULL)	/* range requires runtime mapping */
189 #define EFI_MEM_DESC_VERSION	1
190 
191 #define EFI_PAGE_SHIFT		12
192 #define EFI_PAGE_SIZE		(1UL << EFI_PAGE_SHIFT)
193 #define EFI_PAGE_MASK		(EFI_PAGE_SIZE - 1)
194 
195 struct efi_mem_desc {
196 	u32 type;
197 	u32 reserved;
198 	efi_physical_addr_t physical_start;
199 	efi_virtual_addr_t virtual_start;
200 	u64 num_pages;
201 	u64 attribute;
202 };
203 
204 #define EFI_MEMORY_DESCRIPTOR_VERSION 1
205 
206 /* Allocation types for calls to boottime->allocate_pages*/
207 #define EFI_ALLOCATE_ANY_PAGES		0
208 #define EFI_ALLOCATE_MAX_ADDRESS	1
209 #define EFI_ALLOCATE_ADDRESS		2
210 #define EFI_MAX_ALLOCATE_TYPE		3
211 
212 /* Types and defines for Time Services */
213 #define EFI_TIME_ADJUST_DAYLIGHT 0x1
214 #define EFI_TIME_IN_DAYLIGHT     0x2
215 #define EFI_UNSPECIFIED_TIMEZONE 0x07ff
216 
217 struct efi_time {
218 	u16 year;
219 	u8 month;
220 	u8 day;
221 	u8 hour;
222 	u8 minute;
223 	u8 second;
224 	u8 pad1;
225 	u32 nanosecond;
226 	s16 timezone;
227 	u8 daylight;
228 	u8 pad2;
229 };
230 
231 struct efi_time_cap {
232 	u32 resolution;
233 	u32 accuracy;
234 	u8 sets_to_zero;
235 };
236 
237 enum efi_locate_search_type {
238 	ALL_HANDLES,
239 	BY_REGISTER_NOTIFY,
240 	BY_PROTOCOL
241 };
242 
243 struct efi_open_protocol_info_entry {
244 	efi_handle_t agent_handle;
245 	efi_handle_t controller_handle;
246 	u32 attributes;
247 	u32 open_count;
248 };
249 
250 enum efi_entry_t {
251 	EFIET_END,	/* Signals this is the last (empty) entry */
252 	EFIET_MEMORY_MAP,
253 	EFIET_GOP_MODE,
254 	EFIET_SYS_TABLE,
255 
256 	/* Number of entries */
257 	EFIET_MEMORY_COUNT,
258 };
259 
260 #define EFI_TABLE_VERSION	1
261 
262 /**
263  * struct efi_info_hdr - Header for the EFI info table
264  *
265  * @version:	EFI_TABLE_VERSION
266  * @hdr_size:	Size of this struct in bytes
267  * @total_size:	Total size of this header plus following data
268  * @spare:	Spare space for expansion
269  */
270 struct efi_info_hdr {
271 	u32 version;
272 	u32 hdr_size;
273 	u32 total_size;
274 	u32 spare[5];
275 };
276 
277 /**
278  * struct efi_entry_hdr - Header for a table entry
279  *
280  * @type:	enum eft_entry_t
281  * @size	size of entry bytes excluding header and padding
282  * @addr:	address of this entry (0 if it follows the header )
283  * @link:	size of entry including header and padding
284  * @spare1:	Spare space for expansion
285  * @spare2:	Spare space for expansion
286  */
287 struct efi_entry_hdr {
288 	u32 type;
289 	u32 size;
290 	u64 addr;
291 	u32 link;
292 	u32 spare1;
293 	u64 spare2;
294 };
295 
296 /**
297  * struct efi_entry_memmap - a memory map table passed to U-Boot
298  *
299  * @version:	EFI's memory map table version
300  * @desc_size:	EFI's size of each memory descriptor
301  * @spare:	Spare space for expansion
302  * @desc:	An array of descriptors, each @desc_size bytes apart
303  */
304 struct efi_entry_memmap {
305 	u32 version;
306 	u32 desc_size;
307 	u64 spare;
308 	struct efi_mem_desc desc[];
309 };
310 
311 /**
312  * struct efi_entry_gopmode - a GOP mode table passed to U-Boot
313  *
314  * @fb_base:	EFI's framebuffer base address
315  * @fb_size:	EFI's framebuffer size
316  * @info_size:	GOP mode info structure size
317  * @info:	Start address of the GOP mode info structure
318  */
319 struct efi_entry_gopmode {
320 	efi_physical_addr_t fb_base;
321 	/*
322 	 * Not like the ones in 'struct efi_gop_mode' which are 'unsigned
323 	 * long', @fb_size and @info_size have to be 'u64' here. As the EFI
324 	 * stub codes may have different bit size from the U-Boot payload,
325 	 * using 'long' will cause mismatch between the producer (stub) and
326 	 * the consumer (payload).
327 	 */
328 	u64 fb_size;
329 	u64 info_size;
330 	/*
331 	 * We cannot directly use 'struct efi_gop_mode_info info[]' here as
332 	 * it causes compiler to complain: array type has incomplete element
333 	 * type 'struct efi_gop_mode_info'.
334 	 */
335 	struct /* efi_gop_mode_info */ {
336 		u32 version;
337 		u32 width;
338 		u32 height;
339 		u32 pixel_format;
340 		u32 pixel_bitmask[4];
341 		u32 pixels_per_scanline;
342 	} info[];
343 };
344 
345 /**
346  * struct efi_entry_systable - system table passed to U-Boot
347  *
348  * @sys_table:	EFI system table address
349  */
350 struct efi_entry_systable {
351 	efi_physical_addr_t sys_table;
352 };
353 
354 static inline struct efi_mem_desc *efi_get_next_mem_desc(
355 		struct efi_entry_memmap *map, struct efi_mem_desc *desc)
356 {
357 	return (struct efi_mem_desc *)((ulong)desc + map->desc_size);
358 }
359 
360 struct efi_priv {
361 	efi_handle_t parent_image;
362 	struct efi_device_path *device_path;
363 	struct efi_system_table *sys_table;
364 	struct efi_boot_services *boot;
365 	struct efi_runtime_services *run;
366 	bool use_pool_for_malloc;
367 	unsigned long ram_base;
368 	unsigned int image_data_type;
369 	struct efi_info_hdr *info;
370 	unsigned int info_size;
371 	void *next_hdr;
372 };
373 
374 /* Base address of the EFI image */
375 extern char image_base[];
376 
377 /* Start and end of U-Boot image (for payload) */
378 extern char _binary_u_boot_bin_start[], _binary_u_boot_bin_end[];
379 
380 /*
381  * Variable Attributes
382  */
383 #define EFI_VARIABLE_NON_VOLATILE       0x0000000000000001
384 #define EFI_VARIABLE_BOOTSERVICE_ACCESS 0x0000000000000002
385 #define EFI_VARIABLE_RUNTIME_ACCESS     0x0000000000000004
386 #define EFI_VARIABLE_HARDWARE_ERROR_RECORD 0x0000000000000008
387 #define EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS 0x0000000000000010
388 #define EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS 0x0000000000000020
389 #define EFI_VARIABLE_APPEND_WRITE	0x0000000000000040
390 
391 #define EFI_VARIABLE_MASK	(EFI_VARIABLE_NON_VOLATILE | \
392 				EFI_VARIABLE_BOOTSERVICE_ACCESS | \
393 				EFI_VARIABLE_RUNTIME_ACCESS | \
394 				EFI_VARIABLE_HARDWARE_ERROR_RECORD | \
395 				EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | \
396 				EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | \
397 				EFI_VARIABLE_APPEND_WRITE)
398 
399 /**
400  * efi_get_sys_table() - Get access to the main EFI system table
401  *
402  * @return pointer to EFI system table
403  */
404 
405 struct efi_system_table *efi_get_sys_table(void);
406 
407 /**
408  * efi_get_ram_base() - Find the base of RAM
409  *
410  * This is used when U-Boot is built as an EFI application.
411  *
412  * @return the base of RAM as known to U-Boot
413  */
414 unsigned long efi_get_ram_base(void);
415 
416 /**
417  * efi_init() - Set up ready for use of EFI boot services
418  *
419  * @priv:	Pointer to our private EFI structure to fill in
420  * @banner:	Banner to display when starting
421  * @image:	The image handle passed to efi_main()
422  * @sys_table:	The EFI system table pointer passed to efi_main()
423  */
424 int efi_init(struct efi_priv *priv, const char *banner, efi_handle_t image,
425 	     struct efi_system_table *sys_table);
426 
427 /**
428  * efi_malloc() - Allocate some memory from EFI
429  *
430  * @priv:	Pointer to private EFI structure
431  * @size:	Number of bytes to allocate
432  * @retp:	Return EFI status result
433  * @return pointer to memory allocated, or NULL on error
434  */
435 void *efi_malloc(struct efi_priv *priv, int size, efi_status_t *retp);
436 
437 /**
438  * efi_free() - Free memory allocated from EFI
439  *
440  * @priv:	Pointer to private EFI structure
441  * @ptr:	Pointer to memory to free
442  */
443 void efi_free(struct efi_priv *priv, void *ptr);
444 
445 /**
446  * efi_puts() - Write out a string to the EFI console
447  *
448  * @priv:	Pointer to private EFI structure
449  * @str:	String to write (note this is a ASCII, not unicode)
450  */
451 void efi_puts(struct efi_priv *priv, const char *str);
452 
453 /**
454  * efi_putc() - Write out a character to the EFI console
455  *
456  * @priv:	Pointer to private EFI structure
457  * @ch:		Character to write (note this is not unicode)
458  */
459 void efi_putc(struct efi_priv *priv, const char ch);
460 
461 /**
462  * efi_info_get() - get an entry from an EFI table
463  *
464  * @type:	Entry type to search for
465  * @datap:	Returns pointer to entry data
466  * @sizep:	Returns pointer to entry size
467  * @return 0 if OK, -ENODATA if there is no table, -ENOENT if there is no entry
468  * of the requested type, -EPROTONOSUPPORT if the table has the wrong version
469  */
470 int efi_info_get(enum efi_entry_t type, void **datap, int *sizep);
471 
472 /**
473  * efi_build_mem_table() - make a sorted copy of the memory table
474  *
475  * @map:	Pointer to EFI memory map table
476  * @size:	Size of table in bytes
477  * @skip_bs:	True to skip boot-time memory and merge it with conventional
478  *		memory. This will significantly reduce the number of table
479  *		entries.
480  * @return pointer to the new table. It should be freed with free() by the
481  *	   caller
482  */
483 void *efi_build_mem_table(struct efi_entry_memmap *map, int size, bool skip_bs);
484 
485 #endif /* _LINUX_EFI_H */
486