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