efi/arm: Move FDT specific definitions into fdtparams.c
[platform/kernel/linux-starfive.git] / include / linux / efi.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_EFI_H
3 #define _LINUX_EFI_H
4
5 /*
6  * Extensible Firmware Interface
7  * Based on 'Extensible Firmware Interface Specification' version 0.9, April 30, 1999
8  *
9  * Copyright (C) 1999 VA Linux Systems
10  * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
11  * Copyright (C) 1999, 2002-2003 Hewlett-Packard Co.
12  *      David Mosberger-Tang <davidm@hpl.hp.com>
13  *      Stephane Eranian <eranian@hpl.hp.com>
14  */
15 #include <linux/init.h>
16 #include <linux/string.h>
17 #include <linux/time.h>
18 #include <linux/types.h>
19 #include <linux/proc_fs.h>
20 #include <linux/rtc.h>
21 #include <linux/ioport.h>
22 #include <linux/pfn.h>
23 #include <linux/pstore.h>
24 #include <linux/range.h>
25 #include <linux/reboot.h>
26 #include <linux/uuid.h>
27 #include <linux/screen_info.h>
28
29 #include <asm/page.h>
30
31 #define EFI_SUCCESS             0
32 #define EFI_LOAD_ERROR          ( 1 | (1UL << (BITS_PER_LONG-1)))
33 #define EFI_INVALID_PARAMETER   ( 2 | (1UL << (BITS_PER_LONG-1)))
34 #define EFI_UNSUPPORTED         ( 3 | (1UL << (BITS_PER_LONG-1)))
35 #define EFI_BAD_BUFFER_SIZE     ( 4 | (1UL << (BITS_PER_LONG-1)))
36 #define EFI_BUFFER_TOO_SMALL    ( 5 | (1UL << (BITS_PER_LONG-1)))
37 #define EFI_NOT_READY           ( 6 | (1UL << (BITS_PER_LONG-1)))
38 #define EFI_DEVICE_ERROR        ( 7 | (1UL << (BITS_PER_LONG-1)))
39 #define EFI_WRITE_PROTECTED     ( 8 | (1UL << (BITS_PER_LONG-1)))
40 #define EFI_OUT_OF_RESOURCES    ( 9 | (1UL << (BITS_PER_LONG-1)))
41 #define EFI_NOT_FOUND           (14 | (1UL << (BITS_PER_LONG-1)))
42 #define EFI_ABORTED             (21 | (1UL << (BITS_PER_LONG-1)))
43 #define EFI_SECURITY_VIOLATION  (26 | (1UL << (BITS_PER_LONG-1)))
44
45 typedef unsigned long efi_status_t;
46 typedef u8 efi_bool_t;
47 typedef u16 efi_char16_t;               /* UNICODE character */
48 typedef u64 efi_physical_addr_t;
49 typedef void *efi_handle_t;
50
51 #if defined(CONFIG_X86_64)
52 #define __efiapi __attribute__((ms_abi))
53 #elif defined(CONFIG_X86_32)
54 #define __efiapi __attribute__((regparm(0)))
55 #else
56 #define __efiapi
57 #endif
58
59 /*
60  * The UEFI spec and EDK2 reference implementation both define EFI_GUID as
61  * struct { u32 a; u16; b; u16 c; u8 d[8]; }; and so the implied alignment
62  * is 32 bits not 8 bits like our guid_t. In some cases (i.e., on 32-bit ARM),
63  * this means that firmware services invoked by the kernel may assume that
64  * efi_guid_t* arguments are 32-bit aligned, and use memory accessors that
65  * do not tolerate misalignment. So let's set the minimum alignment to 32 bits.
66  *
67  * Note that the UEFI spec as well as some comments in the EDK2 code base
68  * suggest that EFI_GUID should be 64-bit aligned, but this appears to be
69  * a mistake, given that no code seems to exist that actually enforces that
70  * or relies on it.
71  */
72 typedef guid_t efi_guid_t __aligned(__alignof__(u32));
73
74 #define EFI_GUID(a,b,c,d0,d1,d2,d3,d4,d5,d6,d7) \
75         GUID_INIT(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7)
76
77 /*
78  * Generic EFI table header
79  */
80 typedef struct {
81         u64 signature;
82         u32 revision;
83         u32 headersize;
84         u32 crc32;
85         u32 reserved;
86 } efi_table_hdr_t;
87
88 /*
89  * Memory map descriptor:
90  */
91
92 /* Memory types: */
93 #define EFI_RESERVED_TYPE                0
94 #define EFI_LOADER_CODE                  1
95 #define EFI_LOADER_DATA                  2
96 #define EFI_BOOT_SERVICES_CODE           3
97 #define EFI_BOOT_SERVICES_DATA           4
98 #define EFI_RUNTIME_SERVICES_CODE        5
99 #define EFI_RUNTIME_SERVICES_DATA        6
100 #define EFI_CONVENTIONAL_MEMORY          7
101 #define EFI_UNUSABLE_MEMORY              8
102 #define EFI_ACPI_RECLAIM_MEMORY          9
103 #define EFI_ACPI_MEMORY_NVS             10
104 #define EFI_MEMORY_MAPPED_IO            11
105 #define EFI_MEMORY_MAPPED_IO_PORT_SPACE 12
106 #define EFI_PAL_CODE                    13
107 #define EFI_PERSISTENT_MEMORY           14
108 #define EFI_MAX_MEMORY_TYPE             15
109
110 /* Attribute values: */
111 #define EFI_MEMORY_UC           ((u64)0x0000000000000001ULL)    /* uncached */
112 #define EFI_MEMORY_WC           ((u64)0x0000000000000002ULL)    /* write-coalescing */
113 #define EFI_MEMORY_WT           ((u64)0x0000000000000004ULL)    /* write-through */
114 #define EFI_MEMORY_WB           ((u64)0x0000000000000008ULL)    /* write-back */
115 #define EFI_MEMORY_UCE          ((u64)0x0000000000000010ULL)    /* uncached, exported */
116 #define EFI_MEMORY_WP           ((u64)0x0000000000001000ULL)    /* write-protect */
117 #define EFI_MEMORY_RP           ((u64)0x0000000000002000ULL)    /* read-protect */
118 #define EFI_MEMORY_XP           ((u64)0x0000000000004000ULL)    /* execute-protect */
119 #define EFI_MEMORY_NV           ((u64)0x0000000000008000ULL)    /* non-volatile */
120 #define EFI_MEMORY_MORE_RELIABLE \
121                                 ((u64)0x0000000000010000ULL)    /* higher reliability */
122 #define EFI_MEMORY_RO           ((u64)0x0000000000020000ULL)    /* read-only */
123 #define EFI_MEMORY_SP           ((u64)0x0000000000040000ULL)    /* soft reserved */
124 #define EFI_MEMORY_RUNTIME      ((u64)0x8000000000000000ULL)    /* range requires runtime mapping */
125 #define EFI_MEMORY_DESCRIPTOR_VERSION   1
126
127 #define EFI_PAGE_SHIFT          12
128 #define EFI_PAGE_SIZE           (1UL << EFI_PAGE_SHIFT)
129 #define EFI_PAGES_MAX           (U64_MAX >> EFI_PAGE_SHIFT)
130
131 typedef struct {
132         u32 type;
133         u32 pad;
134         u64 phys_addr;
135         u64 virt_addr;
136         u64 num_pages;
137         u64 attribute;
138 } efi_memory_desc_t;
139
140 typedef struct {
141         efi_guid_t guid;
142         u32 headersize;
143         u32 flags;
144         u32 imagesize;
145 } efi_capsule_header_t;
146
147 /*
148  * EFI capsule flags
149  */
150 #define EFI_CAPSULE_PERSIST_ACROSS_RESET        0x00010000
151 #define EFI_CAPSULE_POPULATE_SYSTEM_TABLE       0x00020000
152 #define EFI_CAPSULE_INITIATE_RESET              0x00040000
153
154 struct capsule_info {
155         efi_capsule_header_t    header;
156         efi_capsule_header_t    *capsule;
157         int                     reset_type;
158         long                    index;
159         size_t                  count;
160         size_t                  total_size;
161         struct page             **pages;
162         phys_addr_t             *phys;
163         size_t                  page_bytes_remain;
164 };
165
166 int __efi_capsule_setup_info(struct capsule_info *cap_info);
167
168 typedef int (*efi_freemem_callback_t) (u64 start, u64 end, void *arg);
169
170 /*
171  * Types and defines for Time Services
172  */
173 #define EFI_TIME_ADJUST_DAYLIGHT 0x1
174 #define EFI_TIME_IN_DAYLIGHT     0x2
175 #define EFI_UNSPECIFIED_TIMEZONE 0x07ff
176
177 typedef struct {
178         u16 year;
179         u8 month;
180         u8 day;
181         u8 hour;
182         u8 minute;
183         u8 second;
184         u8 pad1;
185         u32 nanosecond;
186         s16 timezone;
187         u8 daylight;
188         u8 pad2;
189 } efi_time_t;
190
191 typedef struct {
192         u32 resolution;
193         u32 accuracy;
194         u8 sets_to_zero;
195 } efi_time_cap_t;
196
197 typedef union efi_boot_services efi_boot_services_t;
198
199 /*
200  * Types and defines for EFI ResetSystem
201  */
202 #define EFI_RESET_COLD 0
203 #define EFI_RESET_WARM 1
204 #define EFI_RESET_SHUTDOWN 2
205
206 /*
207  * EFI Runtime Services table
208  */
209 #define EFI_RUNTIME_SERVICES_SIGNATURE ((u64)0x5652453544e5552ULL)
210 #define EFI_RUNTIME_SERVICES_REVISION  0x00010000
211
212 typedef struct {
213         efi_table_hdr_t hdr;
214         u32 get_time;
215         u32 set_time;
216         u32 get_wakeup_time;
217         u32 set_wakeup_time;
218         u32 set_virtual_address_map;
219         u32 convert_pointer;
220         u32 get_variable;
221         u32 get_next_variable;
222         u32 set_variable;
223         u32 get_next_high_mono_count;
224         u32 reset_system;
225         u32 update_capsule;
226         u32 query_capsule_caps;
227         u32 query_variable_info;
228 } efi_runtime_services_32_t;
229
230 typedef efi_status_t efi_get_time_t (efi_time_t *tm, efi_time_cap_t *tc);
231 typedef efi_status_t efi_set_time_t (efi_time_t *tm);
232 typedef efi_status_t efi_get_wakeup_time_t (efi_bool_t *enabled, efi_bool_t *pending,
233                                             efi_time_t *tm);
234 typedef efi_status_t efi_set_wakeup_time_t (efi_bool_t enabled, efi_time_t *tm);
235 typedef efi_status_t efi_get_variable_t (efi_char16_t *name, efi_guid_t *vendor, u32 *attr,
236                                          unsigned long *data_size, void *data);
237 typedef efi_status_t efi_get_next_variable_t (unsigned long *name_size, efi_char16_t *name,
238                                               efi_guid_t *vendor);
239 typedef efi_status_t efi_set_variable_t (efi_char16_t *name, efi_guid_t *vendor, 
240                                          u32 attr, unsigned long data_size,
241                                          void *data);
242 typedef efi_status_t efi_get_next_high_mono_count_t (u32 *count);
243 typedef void efi_reset_system_t (int reset_type, efi_status_t status,
244                                  unsigned long data_size, efi_char16_t *data);
245 typedef efi_status_t efi_set_virtual_address_map_t (unsigned long memory_map_size,
246                                                 unsigned long descriptor_size,
247                                                 u32 descriptor_version,
248                                                 efi_memory_desc_t *virtual_map);
249 typedef efi_status_t efi_query_variable_info_t(u32 attr,
250                                                u64 *storage_space,
251                                                u64 *remaining_space,
252                                                u64 *max_variable_size);
253 typedef efi_status_t efi_update_capsule_t(efi_capsule_header_t **capsules,
254                                           unsigned long count,
255                                           unsigned long sg_list);
256 typedef efi_status_t efi_query_capsule_caps_t(efi_capsule_header_t **capsules,
257                                               unsigned long count,
258                                               u64 *max_size,
259                                               int *reset_type);
260 typedef efi_status_t efi_query_variable_store_t(u32 attributes,
261                                                 unsigned long size,
262                                                 bool nonblocking);
263
264 typedef union {
265         struct {
266                 efi_table_hdr_t                         hdr;
267                 efi_get_time_t __efiapi                 *get_time;
268                 efi_set_time_t __efiapi                 *set_time;
269                 efi_get_wakeup_time_t __efiapi          *get_wakeup_time;
270                 efi_set_wakeup_time_t __efiapi          *set_wakeup_time;
271                 efi_set_virtual_address_map_t __efiapi  *set_virtual_address_map;
272                 void                                    *convert_pointer;
273                 efi_get_variable_t __efiapi             *get_variable;
274                 efi_get_next_variable_t __efiapi        *get_next_variable;
275                 efi_set_variable_t __efiapi             *set_variable;
276                 efi_get_next_high_mono_count_t __efiapi *get_next_high_mono_count;
277                 efi_reset_system_t __efiapi             *reset_system;
278                 efi_update_capsule_t __efiapi           *update_capsule;
279                 efi_query_capsule_caps_t __efiapi       *query_capsule_caps;
280                 efi_query_variable_info_t __efiapi      *query_variable_info;
281         };
282         efi_runtime_services_32_t mixed_mode;
283 } efi_runtime_services_t;
284
285 void efi_native_runtime_setup(void);
286
287 /*
288  * EFI Configuration Table and GUID definitions
289  *
290  * These are all defined in a single line to make them easier to
291  * grep for and to see them at a glance - while still having a
292  * similar structure to the definitions in the spec.
293  *
294  * Here's how they are structured:
295  *
296  * GUID: 12345678-1234-1234-1234-123456789012
297  * Spec:
298  *      #define EFI_SOME_PROTOCOL_GUID \
299  *        {0x12345678,0x1234,0x1234,\
300  *          {0x12,0x34,0x12,0x34,0x56,0x78,0x90,0x12}}
301  * Here:
302  *      #define SOME_PROTOCOL_GUID              EFI_GUID(0x12345678, 0x1234, 0x1234,  0x12, 0x34, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12)
303  *                                      ^ tabs                                      ^extra space
304  *
305  * Note that the 'extra space' separates the values at the same place
306  * where the UEFI SPEC breaks the line.
307  */
308 #define NULL_GUID                               EFI_GUID(0x00000000, 0x0000, 0x0000,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
309 #define MPS_TABLE_GUID                          EFI_GUID(0xeb9d2d2f, 0x2d88, 0x11d3,  0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d)
310 #define ACPI_TABLE_GUID                         EFI_GUID(0xeb9d2d30, 0x2d88, 0x11d3,  0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d)
311 #define ACPI_20_TABLE_GUID                      EFI_GUID(0x8868e871, 0xe4f1, 0x11d3,  0xbc, 0x22, 0x00, 0x80, 0xc7, 0x3c, 0x88, 0x81)
312 #define SMBIOS_TABLE_GUID                       EFI_GUID(0xeb9d2d31, 0x2d88, 0x11d3,  0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d)
313 #define SMBIOS3_TABLE_GUID                      EFI_GUID(0xf2fd1544, 0x9794, 0x4a2c,  0x99, 0x2e, 0xe5, 0xbb, 0xcf, 0x20, 0xe3, 0x94)
314 #define SAL_SYSTEM_TABLE_GUID                   EFI_GUID(0xeb9d2d32, 0x2d88, 0x11d3,  0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d)
315 #define HCDP_TABLE_GUID                         EFI_GUID(0xf951938d, 0x620b, 0x42ef,  0x82, 0x79, 0xa8, 0x4b, 0x79, 0x61, 0x78, 0x98)
316 #define UGA_IO_PROTOCOL_GUID                    EFI_GUID(0x61a4d49e, 0x6f68, 0x4f1b,  0xb9, 0x22, 0xa8, 0x6e, 0xed, 0x0b, 0x07, 0xa2)
317 #define EFI_GLOBAL_VARIABLE_GUID                EFI_GUID(0x8be4df61, 0x93ca, 0x11d2,  0xaa, 0x0d, 0x00, 0xe0, 0x98, 0x03, 0x2b, 0x8c)
318 #define UV_SYSTEM_TABLE_GUID                    EFI_GUID(0x3b13a7d4, 0x633e, 0x11dd,  0x93, 0xec, 0xda, 0x25, 0x56, 0xd8, 0x95, 0x93)
319 #define LINUX_EFI_CRASH_GUID                    EFI_GUID(0xcfc8fc79, 0xbe2e, 0x4ddc,  0x97, 0xf0, 0x9f, 0x98, 0xbf, 0xe2, 0x98, 0xa0)
320 #define LOADED_IMAGE_PROTOCOL_GUID              EFI_GUID(0x5b1b31a1, 0x9562, 0x11d2,  0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b)
321 #define EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID       EFI_GUID(0x9042a9de, 0x23dc, 0x4a38,  0x96, 0xfb, 0x7a, 0xde, 0xd0, 0x80, 0x51, 0x6a)
322 #define EFI_UGA_PROTOCOL_GUID                   EFI_GUID(0x982c298b, 0xf4fa, 0x41cb,  0xb8, 0x38, 0x77, 0xaa, 0x68, 0x8f, 0xb8, 0x39)
323 #define EFI_PCI_IO_PROTOCOL_GUID                EFI_GUID(0x4cf5b200, 0x68b8, 0x4ca5,  0x9e, 0xec, 0xb2, 0x3e, 0x3f, 0x50, 0x02, 0x9a)
324 #define EFI_FILE_INFO_ID                        EFI_GUID(0x09576e92, 0x6d3f, 0x11d2,  0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b)
325 #define EFI_SYSTEM_RESOURCE_TABLE_GUID          EFI_GUID(0xb122a263, 0x3661, 0x4f68,  0x99, 0x29, 0x78, 0xf8, 0xb0, 0xd6, 0x21, 0x80)
326 #define EFI_FILE_SYSTEM_GUID                    EFI_GUID(0x964e5b22, 0x6459, 0x11d2,  0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b)
327 #define DEVICE_TREE_GUID                        EFI_GUID(0xb1b621d5, 0xf19c, 0x41a5,  0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69, 0xaa, 0xe0)
328 #define EFI_PROPERTIES_TABLE_GUID               EFI_GUID(0x880aaca3, 0x4adc, 0x4a04,  0x90, 0x79, 0xb7, 0x47, 0x34, 0x08, 0x25, 0xe5)
329 #define EFI_RNG_PROTOCOL_GUID                   EFI_GUID(0x3152bca5, 0xeade, 0x433d,  0x86, 0x2e, 0xc0, 0x1c, 0xdc, 0x29, 0x1f, 0x44)
330 #define EFI_RNG_ALGORITHM_RAW                   EFI_GUID(0xe43176d7, 0xb6e8, 0x4827,  0xb7, 0x84, 0x7f, 0xfd, 0xc4, 0xb6, 0x85, 0x61)
331 #define EFI_MEMORY_ATTRIBUTES_TABLE_GUID        EFI_GUID(0xdcfa911d, 0x26eb, 0x469f,  0xa2, 0x20, 0x38, 0xb7, 0xdc, 0x46, 0x12, 0x20)
332 #define EFI_CONSOLE_OUT_DEVICE_GUID             EFI_GUID(0xd3b36f2c, 0xd551, 0x11d4,  0x9a, 0x46, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d)
333 #define APPLE_PROPERTIES_PROTOCOL_GUID          EFI_GUID(0x91bd12fe, 0xf6c3, 0x44fb,  0xa5, 0xb7, 0x51, 0x22, 0xab, 0x30, 0x3a, 0xe0)
334 #define EFI_TCG2_PROTOCOL_GUID                  EFI_GUID(0x607f766c, 0x7455, 0x42be,  0x93, 0x0b, 0xe4, 0xd7, 0x6d, 0xb2, 0x72, 0x0f)
335 #define EFI_LOAD_FILE_PROTOCOL_GUID             EFI_GUID(0x56ec3091, 0x954c, 0x11d2,  0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b)
336 #define EFI_LOAD_FILE2_PROTOCOL_GUID            EFI_GUID(0x4006c0c1, 0xfcb3, 0x403e,  0x99, 0x6d, 0x4a, 0x6c, 0x87, 0x24, 0xe0, 0x6d)
337
338 #define EFI_IMAGE_SECURITY_DATABASE_GUID        EFI_GUID(0xd719b2cb, 0x3d3a, 0x4596,  0xa3, 0xbc, 0xda, 0xd0, 0x0e, 0x67, 0x65, 0x6f)
339 #define EFI_SHIM_LOCK_GUID                      EFI_GUID(0x605dab50, 0xe046, 0x4300,  0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23)
340
341 #define EFI_CERT_SHA256_GUID                    EFI_GUID(0xc1c41626, 0x504c, 0x4092, 0xac, 0xa9, 0x41, 0xf9, 0x36, 0x93, 0x43, 0x28)
342 #define EFI_CERT_X509_GUID                      EFI_GUID(0xa5c059a1, 0x94e4, 0x4aa7, 0x87, 0xb5, 0xab, 0x15, 0x5c, 0x2b, 0xf0, 0x72)
343 #define EFI_CERT_X509_SHA256_GUID               EFI_GUID(0x3bd2a492, 0x96c0, 0x4079, 0xb4, 0x20, 0xfc, 0xf9, 0x8e, 0xf1, 0x03, 0xed)
344
345 /*
346  * This GUID is used to pass to the kernel proper the struct screen_info
347  * structure that was populated by the stub based on the GOP protocol instance
348  * associated with ConOut
349  */
350 #define LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID    EFI_GUID(0xe03fc20a, 0x85dc, 0x406e,  0xb9, 0x0e, 0x4a, 0xb5, 0x02, 0x37, 0x1d, 0x95)
351 #define LINUX_EFI_LOADER_ENTRY_GUID             EFI_GUID(0x4a67b082, 0x0a4c, 0x41cf,  0xb6, 0xc7, 0x44, 0x0b, 0x29, 0xbb, 0x8c, 0x4f)
352 #define LINUX_EFI_RANDOM_SEED_TABLE_GUID        EFI_GUID(0x1ce1e5bc, 0x7ceb, 0x42f2,  0x81, 0xe5, 0x8a, 0xad, 0xf1, 0x80, 0xf5, 0x7b)
353 #define LINUX_EFI_TPM_EVENT_LOG_GUID            EFI_GUID(0xb7799cb0, 0xeca2, 0x4943,  0x96, 0x67, 0x1f, 0xae, 0x07, 0xb7, 0x47, 0xfa)
354 #define LINUX_EFI_TPM_FINAL_LOG_GUID            EFI_GUID(0x1e2ed096, 0x30e2, 0x4254,  0xbd, 0x89, 0x86, 0x3b, 0xbe, 0xf8, 0x23, 0x25)
355 #define LINUX_EFI_MEMRESERVE_TABLE_GUID         EFI_GUID(0x888eb0c6, 0x8ede, 0x4ff5,  0xa8, 0xf0, 0x9a, 0xee, 0x5c, 0xb9, 0x77, 0xc2)
356 #define LINUX_EFI_INITRD_MEDIA_GUID             EFI_GUID(0x5568e427, 0x68fc, 0x4f3d,  0xac, 0x74, 0xca, 0x55, 0x52, 0x31, 0xcc, 0x68)
357
358 /* OEM GUIDs */
359 #define DELLEMC_EFI_RCI2_TABLE_GUID             EFI_GUID(0x2d9f28a2, 0xa886, 0x456a,  0x97, 0xa8, 0xf1, 0x1e, 0xf2, 0x4f, 0xf4, 0x55)
360
361 typedef struct {
362         efi_guid_t guid;
363         u64 table;
364 } efi_config_table_64_t;
365
366 typedef struct {
367         efi_guid_t guid;
368         u32 table;
369 } efi_config_table_32_t;
370
371 typedef union {
372         struct {
373                 efi_guid_t guid;
374                 void *table;
375         };
376         efi_config_table_32_t mixed_mode;
377 } efi_config_table_t;
378
379 typedef struct {
380         efi_guid_t guid;
381         const char *name;
382         unsigned long *ptr;
383 } efi_config_table_type_t;
384
385 #define EFI_SYSTEM_TABLE_SIGNATURE ((u64)0x5453595320494249ULL)
386
387 #define EFI_2_30_SYSTEM_TABLE_REVISION  ((2 << 16) | (30))
388 #define EFI_2_20_SYSTEM_TABLE_REVISION  ((2 << 16) | (20))
389 #define EFI_2_10_SYSTEM_TABLE_REVISION  ((2 << 16) | (10))
390 #define EFI_2_00_SYSTEM_TABLE_REVISION  ((2 << 16) | (00))
391 #define EFI_1_10_SYSTEM_TABLE_REVISION  ((1 << 16) | (10))
392 #define EFI_1_02_SYSTEM_TABLE_REVISION  ((1 << 16) | (02))
393
394 typedef struct {
395         efi_table_hdr_t hdr;
396         u64 fw_vendor;  /* physical addr of CHAR16 vendor string */
397         u32 fw_revision;
398         u32 __pad1;
399         u64 con_in_handle;
400         u64 con_in;
401         u64 con_out_handle;
402         u64 con_out;
403         u64 stderr_handle;
404         u64 stderr;
405         u64 runtime;
406         u64 boottime;
407         u32 nr_tables;
408         u32 __pad2;
409         u64 tables;
410 } efi_system_table_64_t;
411
412 typedef struct {
413         efi_table_hdr_t hdr;
414         u32 fw_vendor;  /* physical addr of CHAR16 vendor string */
415         u32 fw_revision;
416         u32 con_in_handle;
417         u32 con_in;
418         u32 con_out_handle;
419         u32 con_out;
420         u32 stderr_handle;
421         u32 stderr;
422         u32 runtime;
423         u32 boottime;
424         u32 nr_tables;
425         u32 tables;
426 } efi_system_table_32_t;
427
428 typedef union efi_simple_text_output_protocol efi_simple_text_output_protocol_t;
429
430 typedef union {
431         struct {
432                 efi_table_hdr_t hdr;
433                 unsigned long fw_vendor;        /* physical addr of CHAR16 vendor string */
434                 u32 fw_revision;
435                 unsigned long con_in_handle;
436                 unsigned long con_in;
437                 unsigned long con_out_handle;
438                 efi_simple_text_output_protocol_t *con_out;
439                 unsigned long stderr_handle;
440                 unsigned long stderr;
441                 efi_runtime_services_t *runtime;
442                 efi_boot_services_t *boottime;
443                 unsigned long nr_tables;
444                 unsigned long tables;
445         };
446         efi_system_table_32_t mixed_mode;
447 } efi_system_table_t;
448
449 /*
450  * Architecture independent structure for describing a memory map for the
451  * benefit of efi_memmap_init_early(), and for passing context between
452  * efi_memmap_alloc() and efi_memmap_install().
453  */
454 struct efi_memory_map_data {
455         phys_addr_t phys_map;
456         unsigned long size;
457         unsigned long desc_version;
458         unsigned long desc_size;
459         unsigned long flags;
460 };
461
462 struct efi_memory_map {
463         phys_addr_t phys_map;
464         void *map;
465         void *map_end;
466         int nr_map;
467         unsigned long desc_version;
468         unsigned long desc_size;
469 #define EFI_MEMMAP_LATE (1UL << 0)
470 #define EFI_MEMMAP_MEMBLOCK (1UL << 1)
471 #define EFI_MEMMAP_SLAB (1UL << 2)
472         unsigned long flags;
473 };
474
475 struct efi_mem_range {
476         struct range range;
477         u64 attribute;
478 };
479
480 typedef struct {
481         u32 version;
482         u32 length;
483         u64 memory_protection_attribute;
484 } efi_properties_table_t;
485
486 #define EFI_PROPERTIES_TABLE_VERSION    0x00010000
487 #define EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA 0x1
488
489 #define EFI_INVALID_TABLE_ADDR          (~0UL)
490
491 typedef struct {
492         u32 version;
493         u32 num_entries;
494         u32 desc_size;
495         u32 reserved;
496         efi_memory_desc_t entry[0];
497 } efi_memory_attributes_table_t;
498
499 typedef struct {
500         efi_guid_t signature_owner;
501         u8 signature_data[];
502 } efi_signature_data_t;
503
504 typedef struct {
505         efi_guid_t signature_type;
506         u32 signature_list_size;
507         u32 signature_header_size;
508         u32 signature_size;
509         u8 signature_header[];
510         /* efi_signature_data_t signatures[][] */
511 } efi_signature_list_t;
512
513 typedef u8 efi_sha256_hash_t[32];
514
515 typedef struct {
516         efi_sha256_hash_t to_be_signed_hash;
517         efi_time_t time_of_revocation;
518 } efi_cert_x509_sha256_t;
519
520 /*
521  * All runtime access to EFI goes through this structure:
522  */
523 extern struct efi {
524         const efi_runtime_services_t    *runtime;               /* EFI runtime services table */
525         unsigned int                    runtime_version;        /* Runtime services version */
526
527         unsigned long                   acpi;                   /* ACPI table  (IA64 ext 0.71) */
528         unsigned long                   acpi20;                 /* ACPI table  (ACPI 2.0) */
529         unsigned long                   smbios;                 /* SMBIOS table (32 bit entry point) */
530         unsigned long                   smbios3;                /* SMBIOS table (64 bit entry point) */
531         unsigned long                   esrt;                   /* ESRT table */
532         unsigned long                   tpm_log;                /* TPM2 Event Log table */
533         unsigned long                   tpm_final_log;          /* TPM2 Final Events Log table */
534
535         efi_get_time_t                  *get_time;
536         efi_set_time_t                  *set_time;
537         efi_get_wakeup_time_t           *get_wakeup_time;
538         efi_set_wakeup_time_t           *set_wakeup_time;
539         efi_get_variable_t              *get_variable;
540         efi_get_next_variable_t         *get_next_variable;
541         efi_set_variable_t              *set_variable;
542         efi_set_variable_t              *set_variable_nonblocking;
543         efi_query_variable_info_t       *query_variable_info;
544         efi_query_variable_info_t       *query_variable_info_nonblocking;
545         efi_update_capsule_t            *update_capsule;
546         efi_query_capsule_caps_t        *query_capsule_caps;
547         efi_get_next_high_mono_count_t  *get_next_high_mono_count;
548         efi_reset_system_t              *reset_system;
549
550         struct efi_memory_map           memmap;
551         unsigned long                   flags;
552 } efi;
553
554 extern struct mm_struct efi_mm;
555
556 static inline int
557 efi_guidcmp (efi_guid_t left, efi_guid_t right)
558 {
559         return memcmp(&left, &right, sizeof (efi_guid_t));
560 }
561
562 static inline char *
563 efi_guid_to_str(efi_guid_t *guid, char *out)
564 {
565         sprintf(out, "%pUl", guid->b);
566         return out;
567 }
568
569 extern void efi_init (void);
570 extern void *efi_get_pal_addr (void);
571 extern void efi_map_pal_code (void);
572 extern void efi_memmap_walk (efi_freemem_callback_t callback, void *arg);
573 extern void efi_gettimeofday (struct timespec64 *ts);
574 extern void efi_enter_virtual_mode (void);      /* switch EFI to virtual mode, if possible */
575 #ifdef CONFIG_X86
576 extern efi_status_t efi_query_variable_store(u32 attributes,
577                                              unsigned long size,
578                                              bool nonblocking);
579 #else
580
581 static inline efi_status_t efi_query_variable_store(u32 attributes,
582                                                     unsigned long size,
583                                                     bool nonblocking)
584 {
585         return EFI_SUCCESS;
586 }
587 #endif
588 extern void __iomem *efi_lookup_mapped_addr(u64 phys_addr);
589
590 extern int __init efi_memmap_alloc(unsigned int num_entries,
591                                    struct efi_memory_map_data *data);
592 extern void __efi_memmap_free(u64 phys, unsigned long size,
593                               unsigned long flags);
594 extern int __init efi_memmap_init_early(struct efi_memory_map_data *data);
595 extern int __init efi_memmap_init_late(phys_addr_t addr, unsigned long size);
596 extern void __init efi_memmap_unmap(void);
597 extern int __init efi_memmap_install(struct efi_memory_map_data *data);
598 extern int __init efi_memmap_split_count(efi_memory_desc_t *md,
599                                          struct range *range);
600 extern void __init efi_memmap_insert(struct efi_memory_map *old_memmap,
601                                      void *buf, struct efi_mem_range *mem);
602
603 #ifdef CONFIG_EFI_ESRT
604 extern void __init efi_esrt_init(void);
605 #else
606 static inline void efi_esrt_init(void) { }
607 #endif
608 extern int efi_config_parse_tables(const efi_config_table_t *config_tables,
609                                    int count,
610                                    const efi_config_table_type_t *arch_tables);
611 extern int efi_systab_check_header(const efi_table_hdr_t *systab_hdr,
612                                    int min_major_version);
613 extern void efi_systab_report_header(const efi_table_hdr_t *systab_hdr,
614                                      unsigned long fw_vendor);
615 extern u64 efi_get_iobase (void);
616 extern int efi_mem_type(unsigned long phys_addr);
617 extern u64 efi_mem_attributes (unsigned long phys_addr);
618 extern u64 efi_mem_attribute (unsigned long phys_addr, unsigned long size);
619 extern int __init efi_uart_console_only (void);
620 extern u64 efi_mem_desc_end(efi_memory_desc_t *md);
621 extern int efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md);
622 extern void efi_mem_reserve(phys_addr_t addr, u64 size);
623 extern int efi_mem_reserve_persistent(phys_addr_t addr, u64 size);
624 extern void efi_initialize_iomem_resources(struct resource *code_resource,
625                 struct resource *data_resource, struct resource *bss_resource);
626 extern u64 efi_get_fdt_params(struct efi_memory_map_data *data);
627 extern struct kobject *efi_kobj;
628
629 extern int efi_reboot_quirk_mode;
630 extern bool efi_poweroff_required(void);
631
632 #ifdef CONFIG_EFI_FAKE_MEMMAP
633 extern void __init efi_fake_memmap(void);
634 #else
635 static inline void efi_fake_memmap(void) { }
636 #endif
637
638 extern unsigned long efi_mem_attr_table;
639
640 /*
641  * efi_memattr_perm_setter - arch specific callback function passed into
642  *                           efi_memattr_apply_permissions() that updates the
643  *                           mapping permissions described by the second
644  *                           argument in the page tables referred to by the
645  *                           first argument.
646  */
647 typedef int (*efi_memattr_perm_setter)(struct mm_struct *, efi_memory_desc_t *);
648
649 extern int efi_memattr_init(void);
650 extern int efi_memattr_apply_permissions(struct mm_struct *mm,
651                                          efi_memattr_perm_setter fn);
652
653 /*
654  * efi_early_memdesc_ptr - get the n-th EFI memmap descriptor
655  * @map: the start of efi memmap
656  * @desc_size: the size of space for each EFI memmap descriptor
657  * @n: the index of efi memmap descriptor
658  *
659  * EFI boot service provides the GetMemoryMap() function to get a copy of the
660  * current memory map which is an array of memory descriptors, each of
661  * which describes a contiguous block of memory. It also gets the size of the
662  * map, and the size of each descriptor, etc.
663  *
664  * Note that per section 6.2 of UEFI Spec 2.6 Errata A, the returned size of
665  * each descriptor might not be equal to sizeof(efi_memory_memdesc_t),
666  * since efi_memory_memdesc_t may be extended in the future. Thus the OS
667  * MUST use the returned size of the descriptor to find the start of each
668  * efi_memory_memdesc_t in the memory map array. This should only be used
669  * during bootup since for_each_efi_memory_desc_xxx() is available after the
670  * kernel initializes the EFI subsystem to set up struct efi_memory_map.
671  */
672 #define efi_early_memdesc_ptr(map, desc_size, n)                        \
673         (efi_memory_desc_t *)((void *)(map) + ((n) * (desc_size)))
674
675 /* Iterate through an efi_memory_map */
676 #define for_each_efi_memory_desc_in_map(m, md)                             \
677         for ((md) = (m)->map;                                              \
678              (md) && ((void *)(md) + (m)->desc_size) <= (m)->map_end;      \
679              (md) = (void *)(md) + (m)->desc_size)
680
681 /**
682  * for_each_efi_memory_desc - iterate over descriptors in efi.memmap
683  * @md: the efi_memory_desc_t * iterator
684  *
685  * Once the loop finishes @md must not be accessed.
686  */
687 #define for_each_efi_memory_desc(md) \
688         for_each_efi_memory_desc_in_map(&efi.memmap, md)
689
690 /*
691  * Format an EFI memory descriptor's type and attributes to a user-provided
692  * character buffer, as per snprintf(), and return the buffer.
693  */
694 char * __init efi_md_typeattr_format(char *buf, size_t size,
695                                      const efi_memory_desc_t *md);
696
697
698 typedef void (*efi_element_handler_t)(const char *source,
699                                       const void *element_data,
700                                       size_t element_size);
701 extern int __init parse_efi_signature_list(
702         const char *source,
703         const void *data, size_t size,
704         efi_element_handler_t (*get_handler_for_guid)(const efi_guid_t *));
705
706 /**
707  * efi_range_is_wc - check the WC bit on an address range
708  * @start: starting kvirt address
709  * @len: length of range
710  *
711  * Consult the EFI memory map and make sure it's ok to set this range WC.
712  * Returns true or false.
713  */
714 static inline int efi_range_is_wc(unsigned long start, unsigned long len)
715 {
716         unsigned long i;
717
718         for (i = 0; i < len; i += (1UL << EFI_PAGE_SHIFT)) {
719                 unsigned long paddr = __pa(start + i);
720                 if (!(efi_mem_attributes(paddr) & EFI_MEMORY_WC))
721                         return 0;
722         }
723         /* The range checked out */
724         return 1;
725 }
726
727 #ifdef CONFIG_EFI_PCDP
728 extern int __init efi_setup_pcdp_console(char *);
729 #endif
730
731 /*
732  * We play games with efi_enabled so that the compiler will, if
733  * possible, remove EFI-related code altogether.
734  */
735 #define EFI_BOOT                0       /* Were we booted from EFI? */
736 #define EFI_CONFIG_TABLES       2       /* Can we use EFI config tables? */
737 #define EFI_RUNTIME_SERVICES    3       /* Can we use runtime services? */
738 #define EFI_MEMMAP              4       /* Can we use EFI memory map? */
739 #define EFI_64BIT               5       /* Is the firmware 64-bit? */
740 #define EFI_PARAVIRT            6       /* Access is via a paravirt interface */
741 #define EFI_ARCH_1              7       /* First arch-specific bit */
742 #define EFI_DBG                 8       /* Print additional debug info at runtime */
743 #define EFI_NX_PE_DATA          9       /* Can runtime data regions be mapped non-executable? */
744 #define EFI_MEM_ATTR            10      /* Did firmware publish an EFI_MEMORY_ATTRIBUTES table? */
745 #define EFI_MEM_NO_SOFT_RESERVE 11      /* Is the kernel configured to ignore soft reservations? */
746
747 #ifdef CONFIG_EFI
748 /*
749  * Test whether the above EFI_* bits are enabled.
750  */
751 static inline bool efi_enabled(int feature)
752 {
753         return test_bit(feature, &efi.flags) != 0;
754 }
755 extern void efi_reboot(enum reboot_mode reboot_mode, const char *__unused);
756
757 bool __pure __efi_soft_reserve_enabled(void);
758
759 static inline bool __pure efi_soft_reserve_enabled(void)
760 {
761         return IS_ENABLED(CONFIG_EFI_SOFT_RESERVE)
762                 && __efi_soft_reserve_enabled();
763 }
764 #else
765 static inline bool efi_enabled(int feature)
766 {
767         return false;
768 }
769 static inline void
770 efi_reboot(enum reboot_mode reboot_mode, const char *__unused) {}
771
772 static inline bool
773 efi_capsule_pending(int *reset_type)
774 {
775         return false;
776 }
777
778 static inline bool efi_soft_reserve_enabled(void)
779 {
780         return false;
781 }
782 #endif
783
784 extern int efi_status_to_err(efi_status_t status);
785
786 /*
787  * Variable Attributes
788  */
789 #define EFI_VARIABLE_NON_VOLATILE       0x0000000000000001
790 #define EFI_VARIABLE_BOOTSERVICE_ACCESS 0x0000000000000002
791 #define EFI_VARIABLE_RUNTIME_ACCESS     0x0000000000000004
792 #define EFI_VARIABLE_HARDWARE_ERROR_RECORD 0x0000000000000008
793 #define EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS 0x0000000000000010
794 #define EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS 0x0000000000000020
795 #define EFI_VARIABLE_APPEND_WRITE       0x0000000000000040
796
797 #define EFI_VARIABLE_MASK       (EFI_VARIABLE_NON_VOLATILE | \
798                                 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
799                                 EFI_VARIABLE_RUNTIME_ACCESS | \
800                                 EFI_VARIABLE_HARDWARE_ERROR_RECORD | \
801                                 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | \
802                                 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | \
803                                 EFI_VARIABLE_APPEND_WRITE)
804 /*
805  * Length of a GUID string (strlen("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"))
806  * not including trailing NUL
807  */
808 #define EFI_VARIABLE_GUID_LEN   UUID_STRING_LEN
809
810 /*
811  * EFI Device Path information
812  */
813 #define EFI_DEV_HW                      0x01
814 #define  EFI_DEV_PCI                             1
815 #define  EFI_DEV_PCCARD                          2
816 #define  EFI_DEV_MEM_MAPPED                      3
817 #define  EFI_DEV_VENDOR                          4
818 #define  EFI_DEV_CONTROLLER                      5
819 #define EFI_DEV_ACPI                    0x02
820 #define   EFI_DEV_BASIC_ACPI                     1
821 #define   EFI_DEV_EXPANDED_ACPI                  2
822 #define EFI_DEV_MSG                     0x03
823 #define   EFI_DEV_MSG_ATAPI                      1
824 #define   EFI_DEV_MSG_SCSI                       2
825 #define   EFI_DEV_MSG_FC                         3
826 #define   EFI_DEV_MSG_1394                       4
827 #define   EFI_DEV_MSG_USB                        5
828 #define   EFI_DEV_MSG_USB_CLASS                 15
829 #define   EFI_DEV_MSG_I20                        6
830 #define   EFI_DEV_MSG_MAC                       11
831 #define   EFI_DEV_MSG_IPV4                      12
832 #define   EFI_DEV_MSG_IPV6                      13
833 #define   EFI_DEV_MSG_INFINIBAND                 9
834 #define   EFI_DEV_MSG_UART                      14
835 #define   EFI_DEV_MSG_VENDOR                    10
836 #define EFI_DEV_MEDIA                   0x04
837 #define   EFI_DEV_MEDIA_HARD_DRIVE               1
838 #define   EFI_DEV_MEDIA_CDROM                    2
839 #define   EFI_DEV_MEDIA_VENDOR                   3
840 #define   EFI_DEV_MEDIA_FILE                     4
841 #define   EFI_DEV_MEDIA_PROTOCOL                 5
842 #define EFI_DEV_BIOS_BOOT               0x05
843 #define EFI_DEV_END_PATH                0x7F
844 #define EFI_DEV_END_PATH2               0xFF
845 #define   EFI_DEV_END_INSTANCE                  0x01
846 #define   EFI_DEV_END_ENTIRE                    0xFF
847
848 struct efi_generic_dev_path {
849         u8                              type;
850         u8                              sub_type;
851         u16                             length;
852 } __packed;
853
854 struct efi_acpi_dev_path {
855         struct efi_generic_dev_path     header;
856         u32                             hid;
857         u32                             uid;
858 } __packed;
859
860 struct efi_pci_dev_path {
861         struct efi_generic_dev_path     header;
862         u8                              fn;
863         u8                              dev;
864 } __packed;
865
866 struct efi_vendor_dev_path {
867         struct efi_generic_dev_path     header;
868         efi_guid_t                      vendorguid;
869         u8                              vendordata[];
870 } __packed;
871
872 struct efi_dev_path {
873         union {
874                 struct efi_generic_dev_path     header;
875                 struct efi_acpi_dev_path        acpi;
876                 struct efi_pci_dev_path         pci;
877                 struct efi_vendor_dev_path      vendor;
878         };
879 } __packed;
880
881 struct device *efi_get_device_by_path(const struct efi_dev_path **node,
882                                       size_t *len);
883
884 static inline void memrange_efi_to_native(u64 *addr, u64 *npages)
885 {
886         *npages = PFN_UP(*addr + (*npages<<EFI_PAGE_SHIFT)) - PFN_DOWN(*addr);
887         *addr &= PAGE_MASK;
888 }
889
890 /*
891  * EFI Variable support.
892  *
893  * Different firmware drivers can expose their EFI-like variables using
894  * the following.
895  */
896
897 struct efivar_operations {
898         efi_get_variable_t *get_variable;
899         efi_get_next_variable_t *get_next_variable;
900         efi_set_variable_t *set_variable;
901         efi_set_variable_t *set_variable_nonblocking;
902         efi_query_variable_store_t *query_variable_store;
903 };
904
905 struct efivars {
906         struct kset *kset;
907         struct kobject *kobject;
908         const struct efivar_operations *ops;
909 };
910
911 /*
912  * The maximum size of VariableName + Data = 1024
913  * Therefore, it's reasonable to save that much
914  * space in each part of the structure,
915  * and we use a page for reading/writing.
916  */
917
918 #define EFI_VAR_NAME_LEN        1024
919
920 struct efi_variable {
921         efi_char16_t  VariableName[EFI_VAR_NAME_LEN/sizeof(efi_char16_t)];
922         efi_guid_t    VendorGuid;
923         unsigned long DataSize;
924         __u8          Data[1024];
925         efi_status_t  Status;
926         __u32         Attributes;
927 } __attribute__((packed));
928
929 struct efivar_entry {
930         struct efi_variable var;
931         struct list_head list;
932         struct kobject kobj;
933         bool scanning;
934         bool deleting;
935 };
936
937 extern struct list_head efivar_sysfs_list;
938
939 static inline void
940 efivar_unregister(struct efivar_entry *var)
941 {
942         kobject_put(&var->kobj);
943 }
944
945 int efivars_register(struct efivars *efivars,
946                      const struct efivar_operations *ops,
947                      struct kobject *kobject);
948 int efivars_unregister(struct efivars *efivars);
949 struct kobject *efivars_kobject(void);
950
951 int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
952                 void *data, bool duplicates, struct list_head *head);
953
954 int efivar_entry_add(struct efivar_entry *entry, struct list_head *head);
955 int efivar_entry_remove(struct efivar_entry *entry);
956
957 int __efivar_entry_delete(struct efivar_entry *entry);
958 int efivar_entry_delete(struct efivar_entry *entry);
959
960 int efivar_entry_size(struct efivar_entry *entry, unsigned long *size);
961 int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
962                        unsigned long *size, void *data);
963 int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
964                      unsigned long *size, void *data);
965 int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
966                      unsigned long size, void *data, struct list_head *head);
967 int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
968                               unsigned long *size, void *data, bool *set);
969 int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
970                           bool block, unsigned long size, void *data);
971
972 int efivar_entry_iter_begin(void);
973 void efivar_entry_iter_end(void);
974
975 int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
976                         struct list_head *head, void *data,
977                         struct efivar_entry **prev);
978 int efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
979                       struct list_head *head, void *data);
980
981 struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid,
982                                        struct list_head *head, bool remove);
983
984 bool efivar_validate(efi_guid_t vendor, efi_char16_t *var_name, u8 *data,
985                      unsigned long data_size);
986 bool efivar_variable_is_removable(efi_guid_t vendor, const char *name,
987                                   size_t len);
988
989 extern struct work_struct efivar_work;
990 void efivar_run_worker(void);
991
992 #if defined(CONFIG_EFI_VARS) || defined(CONFIG_EFI_VARS_MODULE)
993 int efivars_sysfs_init(void);
994
995 #define EFIVARS_DATA_SIZE_MAX 1024
996
997 #endif /* CONFIG_EFI_VARS */
998 extern bool efi_capsule_pending(int *reset_type);
999
1000 extern int efi_capsule_supported(efi_guid_t guid, u32 flags,
1001                                  size_t size, int *reset);
1002
1003 extern int efi_capsule_update(efi_capsule_header_t *capsule,
1004                               phys_addr_t *pages);
1005
1006 #ifdef CONFIG_EFI_RUNTIME_MAP
1007 int efi_runtime_map_init(struct kobject *);
1008 int efi_get_runtime_map_size(void);
1009 int efi_get_runtime_map_desc_size(void);
1010 int efi_runtime_map_copy(void *buf, size_t bufsz);
1011 #else
1012 static inline int efi_runtime_map_init(struct kobject *kobj)
1013 {
1014         return 0;
1015 }
1016
1017 static inline int efi_get_runtime_map_size(void)
1018 {
1019         return 0;
1020 }
1021
1022 static inline int efi_get_runtime_map_desc_size(void)
1023 {
1024         return 0;
1025 }
1026
1027 static inline int efi_runtime_map_copy(void *buf, size_t bufsz)
1028 {
1029         return 0;
1030 }
1031
1032 #endif
1033
1034 #ifdef CONFIG_EFI
1035 extern bool efi_runtime_disabled(void);
1036 #else
1037 static inline bool efi_runtime_disabled(void) { return true; }
1038 #endif
1039
1040 extern void efi_call_virt_check_flags(unsigned long flags, const char *call);
1041 extern unsigned long efi_call_virt_save_flags(void);
1042
1043 enum efi_secureboot_mode {
1044         efi_secureboot_mode_unset,
1045         efi_secureboot_mode_unknown,
1046         efi_secureboot_mode_disabled,
1047         efi_secureboot_mode_enabled,
1048 };
1049 enum efi_secureboot_mode efi_get_secureboot(void);
1050
1051 #ifdef CONFIG_RESET_ATTACK_MITIGATION
1052 void efi_enable_reset_attack_mitigation(void);
1053 #else
1054 static inline void
1055 efi_enable_reset_attack_mitigation(void) { }
1056 #endif
1057
1058 efi_status_t efi_random_get_seed(void);
1059
1060 void efi_retrieve_tpm2_eventlog(void);
1061
1062 /*
1063  * Arch code can implement the following three template macros, avoiding
1064  * reptition for the void/non-void return cases of {__,}efi_call_virt():
1065  *
1066  *  * arch_efi_call_virt_setup()
1067  *
1068  *    Sets up the environment for the call (e.g. switching page tables,
1069  *    allowing kernel-mode use of floating point, if required).
1070  *
1071  *  * arch_efi_call_virt()
1072  *
1073  *    Performs the call. The last expression in the macro must be the call
1074  *    itself, allowing the logic to be shared by the void and non-void
1075  *    cases.
1076  *
1077  *  * arch_efi_call_virt_teardown()
1078  *
1079  *    Restores the usual kernel environment once the call has returned.
1080  */
1081
1082 #define efi_call_virt_pointer(p, f, args...)                            \
1083 ({                                                                      \
1084         efi_status_t __s;                                               \
1085         unsigned long __flags;                                          \
1086                                                                         \
1087         arch_efi_call_virt_setup();                                     \
1088                                                                         \
1089         __flags = efi_call_virt_save_flags();                           \
1090         __s = arch_efi_call_virt(p, f, args);                           \
1091         efi_call_virt_check_flags(__flags, __stringify(f));             \
1092                                                                         \
1093         arch_efi_call_virt_teardown();                                  \
1094                                                                         \
1095         __s;                                                            \
1096 })
1097
1098 #define __efi_call_virt_pointer(p, f, args...)                          \
1099 ({                                                                      \
1100         unsigned long __flags;                                          \
1101                                                                         \
1102         arch_efi_call_virt_setup();                                     \
1103                                                                         \
1104         __flags = efi_call_virt_save_flags();                           \
1105         arch_efi_call_virt(p, f, args);                                 \
1106         efi_call_virt_check_flags(__flags, __stringify(f));             \
1107                                                                         \
1108         arch_efi_call_virt_teardown();                                  \
1109 })
1110
1111 #define EFI_RANDOM_SEED_SIZE            64U
1112
1113 struct linux_efi_random_seed {
1114         u32     size;
1115         u8      bits[];
1116 };
1117
1118 struct linux_efi_tpm_eventlog {
1119         u32     size;
1120         u32     final_events_preboot_size;
1121         u8      version;
1122         u8      log[];
1123 };
1124
1125 extern int efi_tpm_eventlog_init(void);
1126
1127 struct efi_tcg2_final_events_table {
1128         u64 version;
1129         u64 nr_events;
1130         u8 events[];
1131 };
1132 extern int efi_tpm_final_log_size;
1133
1134 extern unsigned long rci2_table_phys;
1135
1136 /*
1137  * efi_runtime_service() function identifiers.
1138  * "NONE" is used by efi_recover_from_page_fault() to check if the page
1139  * fault happened while executing an efi runtime service.
1140  */
1141 enum efi_rts_ids {
1142         EFI_NONE,
1143         EFI_GET_TIME,
1144         EFI_SET_TIME,
1145         EFI_GET_WAKEUP_TIME,
1146         EFI_SET_WAKEUP_TIME,
1147         EFI_GET_VARIABLE,
1148         EFI_GET_NEXT_VARIABLE,
1149         EFI_SET_VARIABLE,
1150         EFI_QUERY_VARIABLE_INFO,
1151         EFI_GET_NEXT_HIGH_MONO_COUNT,
1152         EFI_RESET_SYSTEM,
1153         EFI_UPDATE_CAPSULE,
1154         EFI_QUERY_CAPSULE_CAPS,
1155 };
1156
1157 /*
1158  * efi_runtime_work:    Details of EFI Runtime Service work
1159  * @arg<1-5>:           EFI Runtime Service function arguments
1160  * @status:             Status of executing EFI Runtime Service
1161  * @efi_rts_id:         EFI Runtime Service function identifier
1162  * @efi_rts_comp:       Struct used for handling completions
1163  */
1164 struct efi_runtime_work {
1165         void *arg1;
1166         void *arg2;
1167         void *arg3;
1168         void *arg4;
1169         void *arg5;
1170         efi_status_t status;
1171         struct work_struct work;
1172         enum efi_rts_ids efi_rts_id;
1173         struct completion efi_rts_comp;
1174 };
1175
1176 extern struct efi_runtime_work efi_rts_work;
1177
1178 /* Workqueue to queue EFI Runtime Services */
1179 extern struct workqueue_struct *efi_rts_wq;
1180
1181 struct linux_efi_memreserve {
1182         int             size;                   // allocated size of the array
1183         atomic_t        count;                  // number of entries used
1184         phys_addr_t     next;                   // pa of next struct instance
1185         struct {
1186                 phys_addr_t     base;
1187                 phys_addr_t     size;
1188         } entry[0];
1189 };
1190
1191 #define EFI_MEMRESERVE_SIZE(count) (sizeof(struct linux_efi_memreserve) + \
1192         (count) * sizeof(((struct linux_efi_memreserve *)0)->entry[0]))
1193
1194 #define EFI_MEMRESERVE_COUNT(size) (((size) - sizeof(struct linux_efi_memreserve)) \
1195         / sizeof(((struct linux_efi_memreserve *)0)->entry[0]))
1196
1197 #endif /* _LINUX_EFI_H */