blob: 6417a9b8c53a044e6485212bf4cfefc99bc14bff [file] [log] [blame]
Heinrich Schuchardt04a0f562018-09-08 10:57:59 +02001/* SPDX-License-Identifier: GPL-2.0 */
Simon Glass867a6ac2015-07-31 09:31:36 -06002/*
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
Simon Glassa0b49bc2016-09-25 15:27:31 -060019#include <linux/linkage.h>
Simon Glass867a6ac2015-07-31 09:31:36 -060020#include <linux/string.h>
21#include <linux/types.h>
22
Heinrich Schuchardt85fc2ad2021-01-05 07:52:48 +010023/* 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
Alexander Graf01866442018-06-22 01:38:26 -070028/*
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__
Simon Glass96a8d402015-08-04 12:33:56 -060037#define EFIAPI __attribute__((ms_abi))
Alexander Grafbeb077a2018-06-18 17:23:05 +020038#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
Simon Glass96a8d402015-08-04 12:33:56 -060042#else
Simon Glassa0b49bc2016-09-25 15:27:31 -060043#define EFIAPI asmlinkage
Alexander Grafbeb077a2018-06-18 17:23:05 +020044#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
Alexander Graf01866442018-06-22 01:38:26 -070048#endif /* __x86_64__ */
Simon Glass96a8d402015-08-04 12:33:56 -060049
Bin Meng1fdeacd2018-08-23 08:24:10 -070050#define EFI32_LOADER_SIGNATURE "EL32"
51#define EFI64_LOADER_SIGNATURE "EL64"
52
Simon Glass867a6ac2015-07-31 09:31:36 -060053struct efi_device_path;
54
Rob Clark0d6ab322017-09-13 18:05:24 -040055typedef struct {
56 u8 b[16];
Heinrich Schuchardt4b05fe92018-12-16 11:16:03 +010057} efi_guid_t __attribute__((aligned(8)));
Rob Clark0d6ab322017-09-13 18:05:24 -040058
Alexander Graf01866442018-06-22 01:38:26 -070059#define EFI_BITS_PER_LONG (sizeof(long) * 8)
Bin Meng3e6cc352016-08-25 01:47:18 -070060
xypron.glpk@gmx.de3c8ffb62017-07-04 23:15:22 +020061/* 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)
Simon Glass867a6ac2015-07-31 09:31:36 -060098
Heinrich Schuchardt7b31efc2020-01-03 22:47:19 +010099#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
Rob Clark2a920802017-09-13 18:05:34 -0400106
Simon Glass867a6ac2015-07-31 09:31:36 -0600107typedef unsigned long efi_status_t;
108typedef u64 efi_physical_addr_t;
109typedef u64 efi_virtual_addr_t;
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +0200110typedef struct efi_object *efi_handle_t;
Simon Glass867a6ac2015-07-31 09:31:36 -0600111
112#define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
Simon Glass867a6ac2015-07-31 09:31:36 -0600113 {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, \
114 ((a) >> 24) & 0xff, \
115 (b) & 0xff, ((b) >> 8) & 0xff, \
116 (c) & 0xff, ((c) >> 8) & 0xff, \
Heinrich Schuchardt44ab2d32018-06-09 17:50:18 +0200117 (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) } }
Simon Glass867a6ac2015-07-31 09:31:36 -0600118
119/* Generic EFI table header */
120struct 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 */
129enum 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 /*
Heinrich Schuchardt29f1a362017-09-18 07:54:50 +0200145 * The data portions of a loaded Boot Services Driver and
Simon Glass867a6ac2015-07-31 09:31:36 -0600146 * 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,
Heinrich Schuchardtf12bcc92019-04-23 00:30:53 +0200182 /*
Heinrich Schuchardt9933eb42021-04-02 22:16:22 +0200183 * Byte addressable non-volatile memory.
Heinrich Schuchardtf12bcc92019-04-23 00:30:53 +0200184 */
185 EFI_PERSISTENT_MEMORY_TYPE,
Heinrich Schuchardt9933eb42021-04-02 22:16:22 +0200186 /*
187 * Unaccepted memory must be accepted by boot target before usage.
188 */
189 EFI_UNACCEPTED_MEMORY_TYPE,
Simon Glass867a6ac2015-07-31 09:31:36 -0600190
191 EFI_MAX_MEMORY_TYPE,
Simon Glass867a6ac2015-07-31 09:31:36 -0600192};
193
194/* Attribute values */
Eugeniu Rosca9b891832018-07-14 22:53:30 +0200195#define EFI_MEMORY_UC ((u64)0x0000000000000001ULL) /* uncached */
196#define EFI_MEMORY_WC ((u64)0x0000000000000002ULL) /* write-coalescing */
197#define EFI_MEMORY_WT ((u64)0x0000000000000004ULL) /* write-through */
198#define EFI_MEMORY_WB ((u64)0x0000000000000008ULL) /* write-back */
199#define EFI_MEMORY_UCE ((u64)0x0000000000000010ULL) /* uncached, exported */
200#define EFI_MEMORY_WP ((u64)0x0000000000001000ULL) /* write-protect */
201#define EFI_MEMORY_RP ((u64)0x0000000000002000ULL) /* read-protect */
202#define EFI_MEMORY_XP ((u64)0x0000000000004000ULL) /* execute-protect */
Eugeniu Roscac3a40cc2018-07-14 22:53:31 +0200203#define EFI_MEMORY_NV ((u64)0x0000000000008000ULL) /* non-volatile */
204#define EFI_MEMORY_MORE_RELIABLE \
205 ((u64)0x0000000000010000ULL) /* higher reliability */
206#define EFI_MEMORY_RO ((u64)0x0000000000020000ULL) /* read-only */
Heinrich Schuchardt255a4732020-05-19 07:20:46 +0200207#define EFI_MEMORY_SP ((u64)0x0000000000040000ULL) /* specific-purpose memory (SPM) */
Heinrich Schuchardt9933eb42021-04-02 22:16:22 +0200208#define EFI_MEMORY_CPU_CRYPTO ((u64)0x0000000000080000ULL) /* cryptographically protectable */
Eugeniu Rosca9b891832018-07-14 22:53:30 +0200209#define EFI_MEMORY_RUNTIME ((u64)0x8000000000000000ULL) /* range requires runtime mapping */
210#define EFI_MEM_DESC_VERSION 1
Simon Glass867a6ac2015-07-31 09:31:36 -0600211
212#define EFI_PAGE_SHIFT 12
Patrick Wildt60fd8842019-04-09 22:58:30 +0200213#define EFI_PAGE_SIZE (1ULL << EFI_PAGE_SHIFT)
Alexander Graf2bb9b792016-03-04 01:09:57 +0100214#define EFI_PAGE_MASK (EFI_PAGE_SIZE - 1)
Simon Glass867a6ac2015-07-31 09:31:36 -0600215
216struct efi_mem_desc {
217 u32 type;
218 u32 reserved;
219 efi_physical_addr_t physical_start;
220 efi_virtual_addr_t virtual_start;
221 u64 num_pages;
222 u64 attribute;
223};
224
Mian Yousaf Kaukab4c02c112016-09-05 23:59:22 +0200225#define EFI_MEMORY_DESCRIPTOR_VERSION 1
226
Simon Glass867a6ac2015-07-31 09:31:36 -0600227/* Allocation types for calls to boottime->allocate_pages*/
228#define EFI_ALLOCATE_ANY_PAGES 0
229#define EFI_ALLOCATE_MAX_ADDRESS 1
230#define EFI_ALLOCATE_ADDRESS 2
231#define EFI_MAX_ALLOCATE_TYPE 3
232
233/* Types and defines for Time Services */
234#define EFI_TIME_ADJUST_DAYLIGHT 0x1
235#define EFI_TIME_IN_DAYLIGHT 0x2
236#define EFI_UNSPECIFIED_TIMEZONE 0x07ff
237
238struct efi_time {
239 u16 year;
240 u8 month;
241 u8 day;
242 u8 hour;
243 u8 minute;
244 u8 second;
245 u8 pad1;
246 u32 nanosecond;
247 s16 timezone;
248 u8 daylight;
249 u8 pad2;
250};
251
252struct efi_time_cap {
253 u32 resolution;
254 u32 accuracy;
255 u8 sets_to_zero;
256};
257
258enum efi_locate_search_type {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +0100259 ALL_HANDLES,
260 BY_REGISTER_NOTIFY,
261 BY_PROTOCOL
Simon Glass867a6ac2015-07-31 09:31:36 -0600262};
263
264struct efi_open_protocol_info_entry {
265 efi_handle_t agent_handle;
266 efi_handle_t controller_handle;
267 u32 attributes;
268 u32 open_count;
269};
270
271enum efi_entry_t {
272 EFIET_END, /* Signals this is the last (empty) entry */
273 EFIET_MEMORY_MAP,
Bin Mengd1fe9922018-06-12 08:36:21 -0700274 EFIET_GOP_MODE,
Bin Mengcbe503f2018-08-23 08:24:09 -0700275 EFIET_SYS_TABLE,
Simon Glass867a6ac2015-07-31 09:31:36 -0600276
277 /* Number of entries */
278 EFIET_MEMORY_COUNT,
279};
280
281#define EFI_TABLE_VERSION 1
282
283/**
284 * struct efi_info_hdr - Header for the EFI info table
285 *
286 * @version: EFI_TABLE_VERSION
287 * @hdr_size: Size of this struct in bytes
288 * @total_size: Total size of this header plus following data
289 * @spare: Spare space for expansion
290 */
291struct efi_info_hdr {
292 u32 version;
293 u32 hdr_size;
294 u32 total_size;
295 u32 spare[5];
296};
297
298/**
299 * struct efi_entry_hdr - Header for a table entry
300 *
301 * @type: enum eft_entry_t
302 * @size size of entry bytes excluding header and padding
303 * @addr: address of this entry (0 if it follows the header )
304 * @link: size of entry including header and padding
305 * @spare1: Spare space for expansion
306 * @spare2: Spare space for expansion
307 */
308struct efi_entry_hdr {
309 u32 type;
310 u32 size;
311 u64 addr;
312 u32 link;
313 u32 spare1;
314 u64 spare2;
315};
316
317/**
318 * struct efi_entry_memmap - a memory map table passed to U-Boot
319 *
320 * @version: EFI's memory map table version
321 * @desc_size: EFI's size of each memory descriptor
322 * @spare: Spare space for expansion
323 * @desc: An array of descriptors, each @desc_size bytes apart
324 */
325struct efi_entry_memmap {
326 u32 version;
327 u32 desc_size;
328 u64 spare;
329 struct efi_mem_desc desc[];
330};
331
Bin Mengd1fe9922018-06-12 08:36:21 -0700332/**
333 * struct efi_entry_gopmode - a GOP mode table passed to U-Boot
334 *
335 * @fb_base: EFI's framebuffer base address
336 * @fb_size: EFI's framebuffer size
337 * @info_size: GOP mode info structure size
338 * @info: Start address of the GOP mode info structure
339 */
340struct efi_entry_gopmode {
341 efi_physical_addr_t fb_base;
342 /*
343 * Not like the ones in 'struct efi_gop_mode' which are 'unsigned
344 * long', @fb_size and @info_size have to be 'u64' here. As the EFI
345 * stub codes may have different bit size from the U-Boot payload,
346 * using 'long' will cause mismatch between the producer (stub) and
347 * the consumer (payload).
348 */
349 u64 fb_size;
350 u64 info_size;
351 /*
352 * We cannot directly use 'struct efi_gop_mode_info info[]' here as
353 * it causes compiler to complain: array type has incomplete element
354 * type 'struct efi_gop_mode_info'.
355 */
356 struct /* efi_gop_mode_info */ {
357 u32 version;
358 u32 width;
359 u32 height;
360 u32 pixel_format;
361 u32 pixel_bitmask[4];
362 u32 pixels_per_scanline;
363 } info[];
364};
365
Bin Mengcbe503f2018-08-23 08:24:09 -0700366/**
367 * struct efi_entry_systable - system table passed to U-Boot
368 *
369 * @sys_table: EFI system table address
370 */
371struct efi_entry_systable {
372 efi_physical_addr_t sys_table;
373};
374
Simon Glass867a6ac2015-07-31 09:31:36 -0600375static inline struct efi_mem_desc *efi_get_next_mem_desc(
376 struct efi_entry_memmap *map, struct efi_mem_desc *desc)
377{
378 return (struct efi_mem_desc *)((ulong)desc + map->desc_size);
379}
380
381struct efi_priv {
382 efi_handle_t parent_image;
383 struct efi_device_path *device_path;
384 struct efi_system_table *sys_table;
385 struct efi_boot_services *boot;
386 struct efi_runtime_services *run;
387 bool use_pool_for_malloc;
388 unsigned long ram_base;
389 unsigned int image_data_type;
390 struct efi_info_hdr *info;
391 unsigned int info_size;
392 void *next_hdr;
393};
394
395/* Base address of the EFI image */
396extern char image_base[];
397
Simon Glass476476e2015-08-04 12:33:52 -0600398/* Start and end of U-Boot image (for payload) */
Bin Meng3dc51ab2016-08-25 01:47:17 -0700399extern char _binary_u_boot_bin_start[], _binary_u_boot_bin_end[];
Simon Glass476476e2015-08-04 12:33:52 -0600400
Rob Clarkad644e72017-09-13 18:05:37 -0400401/*
402 * Variable Attributes
403 */
404#define EFI_VARIABLE_NON_VOLATILE 0x0000000000000001
405#define EFI_VARIABLE_BOOTSERVICE_ACCESS 0x0000000000000002
406#define EFI_VARIABLE_RUNTIME_ACCESS 0x0000000000000004
407#define EFI_VARIABLE_HARDWARE_ERROR_RECORD 0x0000000000000008
408#define EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS 0x0000000000000010
409#define EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS 0x0000000000000020
410#define EFI_VARIABLE_APPEND_WRITE 0x0000000000000040
411
412#define EFI_VARIABLE_MASK (EFI_VARIABLE_NON_VOLATILE | \
413 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
414 EFI_VARIABLE_RUNTIME_ACCESS | \
415 EFI_VARIABLE_HARDWARE_ERROR_RECORD | \
416 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | \
417 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | \
418 EFI_VARIABLE_APPEND_WRITE)
419
Simon Glass867a6ac2015-07-31 09:31:36 -0600420/**
421 * efi_get_sys_table() - Get access to the main EFI system table
422 *
423 * @return pointer to EFI system table
424 */
Simon Glass476476e2015-08-04 12:33:52 -0600425
Simon Glass867a6ac2015-07-31 09:31:36 -0600426struct efi_system_table *efi_get_sys_table(void);
427
428/**
429 * efi_get_ram_base() - Find the base of RAM
430 *
431 * This is used when U-Boot is built as an EFI application.
432 *
433 * @return the base of RAM as known to U-Boot
434 */
435unsigned long efi_get_ram_base(void);
436
437/**
438 * efi_init() - Set up ready for use of EFI boot services
439 *
440 * @priv: Pointer to our private EFI structure to fill in
441 * @banner: Banner to display when starting
442 * @image: The image handle passed to efi_main()
443 * @sys_table: The EFI system table pointer passed to efi_main()
444 */
445int efi_init(struct efi_priv *priv, const char *banner, efi_handle_t image,
446 struct efi_system_table *sys_table);
447
448/**
449 * efi_malloc() - Allocate some memory from EFI
450 *
451 * @priv: Pointer to private EFI structure
452 * @size: Number of bytes to allocate
453 * @retp: Return EFI status result
454 * @return pointer to memory allocated, or NULL on error
455 */
456void *efi_malloc(struct efi_priv *priv, int size, efi_status_t *retp);
457
458/**
459 * efi_free() - Free memory allocated from EFI
460 *
461 * @priv: Pointer to private EFI structure
462 * @ptr: Pointer to memory to free
463 */
464void efi_free(struct efi_priv *priv, void *ptr);
465
466/**
467 * efi_puts() - Write out a string to the EFI console
468 *
469 * @priv: Pointer to private EFI structure
470 * @str: String to write (note this is a ASCII, not unicode)
471 */
472void efi_puts(struct efi_priv *priv, const char *str);
473
474/**
475 * efi_putc() - Write out a character to the EFI console
476 *
477 * @priv: Pointer to private EFI structure
478 * @ch: Character to write (note this is not unicode)
479 */
480void efi_putc(struct efi_priv *priv, const char ch);
481
482/**
483 * efi_info_get() - get an entry from an EFI table
484 *
485 * @type: Entry type to search for
486 * @datap: Returns pointer to entry data
487 * @sizep: Returns pointer to entry size
488 * @return 0 if OK, -ENODATA if there is no table, -ENOENT if there is no entry
489 * of the requested type, -EPROTONOSUPPORT if the table has the wrong version
490 */
491int efi_info_get(enum efi_entry_t type, void **datap, int *sizep);
492
Simon Glass867a6ac2015-07-31 09:31:36 -0600493#endif /* _LINUX_EFI_H */