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