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