]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/efi.h
MFV: Restore the ability to process files from stdin immediately.
[FreeBSD/FreeBSD.git] / sys / sys / efi.h
1 /*-
2  * Copyright (c) 2004 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #ifndef _SYS_EFI_H_
30 #define _SYS_EFI_H_
31
32 #include <sys/uuid.h>
33 #include <machine/efi.h>
34
35 #define EFI_PAGE_SHIFT          12
36 #define EFI_PAGE_SIZE           (1 << EFI_PAGE_SHIFT)
37 #define EFI_PAGE_MASK           (EFI_PAGE_SIZE - 1)
38
39 #define EFI_TABLE_SMBIOS                                \
40         {0xeb9d2d31,0x2d88,0x11d3,0x9a,0x16,{0x00,0x90,0x27,0x3f,0xc1,0x4d}}
41 #define EFI_TABLE_SMBIOS3                               \
42         {0xf2fd1544,0x9794,0x4a2c,0x99,0x2e,{0xe5,0xbb,0xcf,0x20,0xe3,0x94}}
43 #define EFI_TABLE_ESRT                                  \
44         {0xb122a263,0x3661,0x4f68,0x99,0x29,{0x78,0xf8,0xb0,0xd6,0x21,0x80}}
45 #define EFI_PROPERTIES_TABLE                    \
46         {0x880aaca3,0x4adc,0x4a04,0x90,0x79,{0xb7,0x47,0x34,0x08,0x25,0xe5}}
47 #define LINUX_EFI_MEMRESERVE_TABLE                      \
48         {0x888eb0c6,0x8ede,0x4ff5,0xa8,0xf0,{0x9a,0xee,0x5c,0xb9,0x77,0xc2}}
49
50 enum efi_reset {
51         EFI_RESET_COLD = 0,
52         EFI_RESET_WARM = 1,
53         EFI_RESET_SHUTDOWN = 2,
54 };
55
56 typedef uint16_t        efi_char;
57 typedef unsigned long efi_status;
58
59 struct efi_cfgtbl {
60         struct uuid     ct_uuid;
61         void            *ct_data;
62 };
63
64 #define EFI_MEMORY_DESCRIPTOR_VERSION 1
65
66 struct efi_md {
67         uint32_t        md_type;
68 #define EFI_MD_TYPE_NULL        0
69 #define EFI_MD_TYPE_CODE        1       /* Loader text. */
70 #define EFI_MD_TYPE_DATA        2       /* Loader data. */
71 #define EFI_MD_TYPE_BS_CODE     3       /* Boot services text. */
72 #define EFI_MD_TYPE_BS_DATA     4       /* Boot services data. */
73 #define EFI_MD_TYPE_RT_CODE     5       /* Runtime services text. */
74 #define EFI_MD_TYPE_RT_DATA     6       /* Runtime services data. */
75 #define EFI_MD_TYPE_FREE        7       /* Unused/free memory. */
76 #define EFI_MD_TYPE_BAD         8       /* Bad memory */
77 #define EFI_MD_TYPE_RECLAIM     9       /* ACPI reclaimable memory. */
78 #define EFI_MD_TYPE_FIRMWARE    10      /* ACPI NV memory */
79 #define EFI_MD_TYPE_IOMEM       11      /* Memory-mapped I/O. */
80 #define EFI_MD_TYPE_IOPORT      12      /* I/O port space. */
81 #define EFI_MD_TYPE_PALCODE     13      /* PAL */
82 #define EFI_MD_TYPE_PERSISTENT  14      /* Persistent memory. */
83         uint32_t        __pad;
84         uint64_t        md_phys;
85         uint64_t        md_virt;
86         uint64_t        md_pages;
87         uint64_t        md_attr;
88 #define EFI_MD_ATTR_UC          0x0000000000000001UL
89 #define EFI_MD_ATTR_WC          0x0000000000000002UL
90 #define EFI_MD_ATTR_WT          0x0000000000000004UL
91 #define EFI_MD_ATTR_WB          0x0000000000000008UL
92 #define EFI_MD_ATTR_UCE         0x0000000000000010UL
93 #define EFI_MD_ATTR_WP          0x0000000000001000UL
94 #define EFI_MD_ATTR_RP          0x0000000000002000UL
95 #define EFI_MD_ATTR_XP          0x0000000000004000UL
96 #define EFI_MD_ATTR_NV          0x0000000000008000UL
97 #define EFI_MD_ATTR_MORE_RELIABLE \
98                                 0x0000000000010000UL
99 #define EFI_MD_ATTR_RO          0x0000000000020000UL
100 #define EFI_MD_ATTR_RT          0x8000000000000000UL
101 };
102
103 #define efi_next_descriptor(ptr, size) \
104     ((struct efi_md *)(((uint8_t *)(ptr)) + (size)))
105
106 struct efi_tm {
107         uint16_t        tm_year;                /* 1998 - 20XX */
108         uint8_t         tm_mon;                 /* 1 - 12 */
109         uint8_t         tm_mday;                /* 1 - 31 */
110         uint8_t         tm_hour;                /* 0 - 23 */
111         uint8_t         tm_min;                 /* 0 - 59 */
112         uint8_t         tm_sec;                 /* 0 - 59 */
113         uint8_t         __pad1;
114         uint32_t        tm_nsec;                /* 0 - 999,999,999 */
115         int16_t         tm_tz;                  /* -1440 to 1440 or 2047 */
116         uint8_t         tm_dst;
117         uint8_t         __pad2;
118 };
119
120 struct efi_tmcap {
121         uint32_t        tc_res;         /* 1e-6 parts per million */
122         uint32_t        tc_prec;        /* hertz */
123         uint8_t         tc_stz;         /* Set clears sub-second time */
124 };
125
126 struct efi_tblhdr {
127         uint64_t        th_sig;
128         uint32_t        th_rev;
129         uint32_t        th_hdrsz;
130         uint32_t        th_crc32;
131         uint32_t        __res;
132 };
133
134 #define ESRT_FIRMWARE_RESOURCE_VERSION 1
135
136 struct efi_esrt_table {
137         uint32_t        fw_resource_count;
138         uint32_t        fw_resource_count_max;
139         uint64_t        fw_resource_version;
140         uint8_t         entries[];
141 };
142
143 struct efi_esrt_entry_v1 {
144         struct uuid     fw_class;
145         uint32_t        fw_type;
146         uint32_t        fw_version;
147         uint32_t        lowest_supported_fw_version;
148         uint32_t        capsule_flags;
149         uint32_t        last_attempt_version;
150         uint32_t        last_attempt_status;
151 };
152
153 struct efi_prop_table {
154         uint32_t        version;
155         uint32_t        length;
156         uint64_t        memory_protection_attribute;
157 };
158
159 #ifdef _KERNEL
160
161 #ifdef EFIABI_ATTR
162 struct efi_rt {
163         struct efi_tblhdr rt_hdr;
164         efi_status      (*rt_gettime)(struct efi_tm *, struct efi_tmcap *)
165             EFIABI_ATTR;
166         efi_status      (*rt_settime)(struct efi_tm *) EFIABI_ATTR;
167         efi_status      (*rt_getwaketime)(uint8_t *, uint8_t *,
168             struct efi_tm *) EFIABI_ATTR;
169         efi_status      (*rt_setwaketime)(uint8_t, struct efi_tm *)
170             EFIABI_ATTR;
171         efi_status      (*rt_setvirtual)(u_long, u_long, uint32_t,
172             struct efi_md *) EFIABI_ATTR;
173         efi_status      (*rt_cvtptr)(u_long, void **) EFIABI_ATTR;
174         efi_status      (*rt_getvar)(efi_char *, struct uuid *, uint32_t *,
175             u_long *, void *) EFIABI_ATTR;
176         efi_status      (*rt_scanvar)(u_long *, efi_char *, struct uuid *)
177             EFIABI_ATTR;
178         efi_status      (*rt_setvar)(efi_char *, struct uuid *, uint32_t,
179             u_long, void *) EFIABI_ATTR;
180         efi_status      (*rt_gethicnt)(uint32_t *) EFIABI_ATTR;
181         efi_status      (*rt_reset)(enum efi_reset, efi_status, u_long,
182             efi_char *) EFIABI_ATTR;
183 };
184 #endif
185
186 struct efi_systbl {
187         struct efi_tblhdr st_hdr;
188 #define EFI_SYSTBL_SIG  0x5453595320494249UL
189         efi_char        *st_fwvendor;
190         uint32_t        st_fwrev;
191         uint32_t        __pad;
192         void            *st_cin;
193         void            *st_cinif;
194         void            *st_cout;
195         void            *st_coutif;
196         void            *st_cerr;
197         void            *st_cerrif;
198         uint64_t        st_rt;
199         void            *st_bs;
200         u_long          st_entries;
201         uint64_t        st_cfgtbl;
202 };
203
204 extern vm_paddr_t efi_systbl_phys;
205
206 /*
207  * When memory is reserved for some use, Linux will add a
208  * LINUX_EFI_MEMSERVE_TABLE to the cfgtbl array of tables to communicate
209  * this. At present, Linux only uses this as part of its workaround for a GICv3
210  * issue where you can't stop the controller long enough to move it's config and
211  * pending vectors. When the LinuxBoot environment kexec's a new kernel, the new
212  * kernel needs to use this old memory (and not use it for any other purpose).
213  *
214  * Linux stores the PA of this table in the cfgtbl. And all the addresses are
215  * the physical address of 'reserved' memory. The mr_next field creates a linked
216  * list of these tables, and all must be walked. If mr_count is 0, that entry
217  * should be ignored. There is no checksum for these tables, nor do they have
218  * a efi_tblhdr.
219  *
220  * This table is only documented in the Linux code in drivers/firmware/efi/efi.c.
221  */
222 struct linux_efi_memreserve_entry {
223         vm_offset_t     mre_base;       /* PA of reserved area */
224         vm_offset_t     mre_size;       /* Size of area */
225 };
226
227 struct linux_efi_memreserve {
228         uint32_t        mr_size;        /* Total size of table in bytes */
229         uint32_t        mr_count;       /* Count of entries used */
230         vm_offset_t     mr_next;        /* Next in chain (though unused?) */
231         struct linux_efi_memreserve_entry mr_entry[];
232 };
233
234 struct efirt_callinfo;
235
236 /* Internal MD EFI functions */
237 int efi_arch_enter(void);
238 void efi_arch_leave(void);
239 vm_offset_t efi_phys_to_kva(vm_paddr_t);
240 int efi_rt_arch_call(struct efirt_callinfo *);
241 bool efi_create_1t1_map(struct efi_md *, int, int);
242 void efi_destroy_1t1_map(void);
243
244 struct efi_ops {
245         /*
246          * The EFI calls might be virtualized in some environments, requiring
247          * FreeBSD to use a different interface (ie: hypercalls) in order to
248          * access them.
249          */
250         int     (*rt_ok)(void);
251         int     (*get_table)(struct uuid *, void **);
252         int     (*copy_table)(struct uuid *, void **, size_t, size_t *);
253         int     (*get_time)(struct efi_tm *);
254         int     (*get_time_capabilities)(struct efi_tmcap *);
255         int     (*reset_system)(enum efi_reset);
256         int     (*set_time)(struct efi_tm *);
257         int     (*var_get)(uint16_t *, struct uuid *, uint32_t *, size_t *,
258     void *);
259         int     (*var_nextname)(size_t *, uint16_t *, struct uuid *);
260         int     (*var_set)(uint16_t *, struct uuid *, uint32_t, size_t, void *);
261 };
262 extern const struct efi_ops *active_efi_ops;
263
264 /* Public MI EFI functions */
265 static inline int efi_rt_ok(void)
266 {
267
268         if (active_efi_ops->rt_ok == NULL)
269                 return (ENXIO);
270         return (active_efi_ops->rt_ok());
271 }
272
273 static inline int efi_get_table(struct uuid *uuid, void **ptr)
274 {
275
276         if (active_efi_ops->get_table == NULL)
277                 return (ENXIO);
278         return (active_efi_ops->get_table(uuid, ptr));
279 }
280
281 static inline int efi_copy_table(struct uuid *uuid, void **buf,
282     size_t buf_len, size_t *table_len)
283 {
284
285         if (active_efi_ops->copy_table == NULL)
286                 return (ENXIO);
287         return (active_efi_ops->copy_table(uuid, buf, buf_len, table_len));
288 }
289
290 static inline int efi_get_time(struct efi_tm *tm)
291 {
292
293         if (active_efi_ops->get_time == NULL)
294                 return (ENXIO);
295         return (active_efi_ops->get_time(tm));
296 }
297
298 static inline int efi_get_time_capabilities(struct efi_tmcap *tmcap)
299 {
300
301         if (active_efi_ops->get_time_capabilities == NULL)
302                 return (ENXIO);
303         return (active_efi_ops->get_time_capabilities(tmcap));
304 }
305
306 static inline int efi_reset_system(enum efi_reset type)
307 {
308
309         if (active_efi_ops->reset_system == NULL)
310                 return (ENXIO);
311         return (active_efi_ops->reset_system(type));
312 }
313
314 static inline int efi_set_time(struct efi_tm *tm)
315 {
316
317         if (active_efi_ops->set_time == NULL)
318                 return (ENXIO);
319         return (active_efi_ops->set_time(tm));
320 }
321
322 static inline int efi_var_get(uint16_t *name, struct uuid *vendor,
323     uint32_t *attrib, size_t *datasize, void *data)
324 {
325
326         if (active_efi_ops->var_get == NULL)
327                 return (ENXIO);
328         return (active_efi_ops->var_get(name, vendor, attrib, datasize, data));
329 }
330
331 static inline int efi_var_nextname(size_t *namesize, uint16_t *name,
332     struct uuid *vendor)
333 {
334
335         if (active_efi_ops->var_nextname == NULL)
336                 return (ENXIO);
337         return (active_efi_ops->var_nextname(namesize, name, vendor));
338 }
339
340 static inline int efi_var_set(uint16_t *name, struct uuid *vendor,
341     uint32_t attrib, size_t datasize, void *data)
342 {
343
344         if (active_efi_ops->var_set == NULL)
345                 return (ENXIO);
346         return (active_efi_ops->var_set(name, vendor, attrib, datasize, data));
347 }
348
349 int efi_status_to_errno(efi_status status);
350
351 #endif  /* _KERNEL */
352
353 #endif /* _SYS_EFI_H_ */