xref: /freebsd/sys/sys/efi.h (revision de060b6851a4c96defaa5dde6a1b6e7468486c8c)
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 
27 #ifndef _SYS_EFI_H_
28 #define _SYS_EFI_H_
29 
30 #include <sys/types.h>
31 #include <machine/efi.h>
32 #include <sys/efi-freebsd.h>
33 
34 #define	EFI_PAGE_SHIFT		12
35 #define	EFI_PAGE_SIZE		(1 << EFI_PAGE_SHIFT)
36 #define	EFI_PAGE_MASK		(EFI_PAGE_SIZE - 1)
37 
38 #define	EFI_TABLE_SMBIOS				\
39 	{0xeb9d2d31,0x2d88,0x11d3,{0x9a,0x16,0x00,0x90,0x27,0x3f,0xc1,0x4d}}
40 #define	EFI_TABLE_SMBIOS3				\
41 	{0xf2fd1544,0x9794,0x4a2c,{0x99,0x2e,0xe5,0xbb,0xcf,0x20,0xe3,0x94}}
42 #define	EFI_TABLE_ESRT					\
43 	{0xb122a263,0x3661,0x4f68,{0x99,0x29,0x78,0xf8,0xb0,0xd6,0x21,0x80}}
44 #define	EFI_PROPERTIES_TABLE				\
45 	{0x880aaca3,0x4adc,0x4a04,{0x90,0x79,0xb7,0x47,0x34,0x08,0x25,0xe5}}
46 #define	EFI_MEMORY_ATTRIBUTES_TABLE			\
47 	{0xdcfa911d,0x26eb,0x469f,{0xa2,0x20,0x38,0xb7,0xdc,0x46,0x12,0x20}}
48 #define LINUX_EFI_MEMRESERVE_TABLE			\
49 	{0x888eb0c6,0x8ede,0x4ff5,{0xa8,0xf0,0x9a,0xee,0x5c,0xb9,0x77,0xc2}}
50 
51 enum efi_reset {
52 	EFI_RESET_COLD = 0,
53 	EFI_RESET_WARM = 1,
54 	EFI_RESET_SHUTDOWN = 2,
55 };
56 
57 typedef uint16_t	efi_char;
58 typedef unsigned long	efi_status;
59 
60 /*
61  * This type-puns to a struct uuid, but all the EDK2 headers use this variation,
62  * and we use it in the loader to specify GUIDs. We define it here so that we
63  * can use EDK2 definitions both places.
64  */
65 typedef struct efi_guid {
66 	uint32_t	Data1;
67 	uint16_t	Data2;
68 	uint16_t	Data3;
69 	uint8_t		Data4[8];
70 } efi_guid_t;	/* Type puns with GUID and EFI_GUID */
71 
72 struct efi_cfgtbl {
73 	efi_guid_t	ct_guid;
74 	void		*ct_data;
75 };
76 
77 #define EFI_MEMORY_DESCRIPTOR_VERSION 1
78 
79 struct efi_md {
80 	uint32_t	md_type;
81 #define	EFI_MD_TYPE_NULL	0
82 #define	EFI_MD_TYPE_CODE	1	/* Loader text. */
83 #define	EFI_MD_TYPE_DATA	2	/* Loader data. */
84 #define	EFI_MD_TYPE_BS_CODE	3	/* Boot services text. */
85 #define	EFI_MD_TYPE_BS_DATA	4	/* Boot services data. */
86 #define	EFI_MD_TYPE_RT_CODE	5	/* Runtime services text. */
87 #define	EFI_MD_TYPE_RT_DATA	6	/* Runtime services data. */
88 #define	EFI_MD_TYPE_FREE	7	/* Unused/free memory. */
89 #define	EFI_MD_TYPE_BAD		8	/* Bad memory */
90 #define	EFI_MD_TYPE_RECLAIM	9	/* ACPI reclaimable memory. */
91 #define	EFI_MD_TYPE_FIRMWARE	10	/* ACPI NV memory */
92 #define	EFI_MD_TYPE_IOMEM	11	/* Memory-mapped I/O. */
93 #define	EFI_MD_TYPE_IOPORT	12	/* I/O port space. */
94 #define	EFI_MD_TYPE_PALCODE	13	/* PAL */
95 #define	EFI_MD_TYPE_PERSISTENT	14	/* Persistent memory. */
96 	uint32_t	__pad;
97 	uint64_t	md_phys;
98 	uint64_t	md_virt;
99 	uint64_t	md_pages;
100 	uint64_t	md_attr;
101 #define	EFI_MD_ATTR_UC		0x0000000000000001UL
102 #define	EFI_MD_ATTR_WC		0x0000000000000002UL
103 #define	EFI_MD_ATTR_WT		0x0000000000000004UL
104 #define	EFI_MD_ATTR_WB		0x0000000000000008UL
105 #define	EFI_MD_ATTR_UCE		0x0000000000000010UL
106 #define	EFI_MD_ATTR_WP		0x0000000000001000UL
107 #define	EFI_MD_ATTR_RP		0x0000000000002000UL
108 #define	EFI_MD_ATTR_XP		0x0000000000004000UL
109 #define	EFI_MD_ATTR_NV		0x0000000000008000UL
110 #define	EFI_MD_ATTR_MORE_RELIABLE \
111 				0x0000000000010000UL
112 #define	EFI_MD_ATTR_RO		0x0000000000020000UL
113 #define	EFI_MD_ATTR_RT		0x8000000000000000UL
114 };
115 
116 #define efi_next_descriptor(ptr, size) \
117     ((struct efi_md *)(((uint8_t *)(ptr)) + (size)))
118 
119 struct efi_tm {
120 	uint16_t	tm_year;		/* 1998 - 20XX */
121 	uint8_t		tm_mon;			/* 1 - 12 */
122 	uint8_t		tm_mday;		/* 1 - 31 */
123 	uint8_t		tm_hour;		/* 0 - 23 */
124 	uint8_t		tm_min;			/* 0 - 59 */
125 	uint8_t		tm_sec;			/* 0 - 59 */
126 	uint8_t		__pad1;
127 	uint32_t	tm_nsec;		/* 0 - 999,999,999 */
128 	int16_t		tm_tz;			/* -1440 to 1440 or 2047 */
129 	uint8_t		tm_dst;
130 	uint8_t		__pad2;
131 };
132 
133 struct efi_tmcap {
134 	uint32_t	tc_res;		/* 1e-6 parts per million */
135 	uint32_t	tc_prec;	/* hertz */
136 	uint8_t		tc_stz;		/* Set clears sub-second time */
137 };
138 
139 struct efi_tblhdr {
140 	uint64_t	th_sig;
141 	uint32_t	th_rev;
142 	uint32_t	th_hdrsz;
143 	uint32_t	th_crc32;
144 	uint32_t	__res;
145 };
146 
147 #define ESRT_FIRMWARE_RESOURCE_VERSION 1
148 
149 struct efi_esrt_table {
150 	uint32_t	fw_resource_count;
151 	uint32_t	fw_resource_count_max;
152 	uint64_t	fw_resource_version;
153 	uint8_t		entries[];
154 };
155 
156 struct efi_esrt_entry_v1 {
157 	efi_guid_t	fw_class;
158 	uint32_t 	fw_type;
159 	uint32_t	fw_version;
160 	uint32_t	lowest_supported_fw_version;
161 	uint32_t	capsule_flags;
162 	uint32_t	last_attempt_version;
163 	uint32_t	last_attempt_status;
164 };
165 
166 struct efi_prop_table {
167 	uint32_t	version;
168 	uint32_t	length;
169 	uint64_t	memory_protection_attribute;
170 };
171 
172 struct efi_memory_descriptor {
173 	uint32_t	type;
174 	caddr_t		phy_addr;
175 	caddr_t		virt_addr;
176 	uint64_t	pages;
177 	uint64_t	attrs;
178 };
179 
180 struct efi_memory_attribute_table {
181 	uint32_t	version;
182 	uint32_t	num_ents;
183 	uint32_t	descriptor_size;
184 	uint32_t	flags;
185 	struct efi_memory_descriptor tables[];
186 };
187 
188 #ifdef _KERNEL
189 
190 #ifdef EFIABI_ATTR
191 struct efi_rt {
192 	struct efi_tblhdr rt_hdr;
193 	efi_status	(*rt_gettime)(struct efi_tm *, struct efi_tmcap *)
194 	    EFIABI_ATTR;
195 	efi_status	(*rt_settime)(struct efi_tm *) EFIABI_ATTR;
196 	efi_status	(*rt_getwaketime)(uint8_t *, uint8_t *,
197 	    struct efi_tm *) EFIABI_ATTR;
198 	efi_status	(*rt_setwaketime)(uint8_t, struct efi_tm *)
199 	    EFIABI_ATTR;
200 	efi_status	(*rt_setvirtual)(u_long, u_long, uint32_t,
201 	    struct efi_md *) EFIABI_ATTR;
202 	efi_status	(*rt_cvtptr)(u_long, void **) EFIABI_ATTR;
203 	efi_status	(*rt_getvar)(efi_char *, efi_guid_t *, uint32_t *,
204 	    u_long *, void *) EFIABI_ATTR;
205 	efi_status	(*rt_scanvar)(u_long *, efi_char *, efi_guid_t *)
206 	    EFIABI_ATTR;
207 	efi_status	(*rt_setvar)(efi_char *, efi_guid_t *, uint32_t,
208 	    u_long, void *) EFIABI_ATTR;
209 	efi_status	(*rt_gethicnt)(uint32_t *) EFIABI_ATTR;
210 	efi_status	(*rt_reset)(enum efi_reset, efi_status, u_long,
211 	    efi_char *) EFIABI_ATTR;
212 };
213 #endif
214 
215 struct efi_systbl {
216 	struct efi_tblhdr st_hdr;
217 #define	EFI_SYSTBL_SIG	0x5453595320494249UL
218 	efi_char	*st_fwvendor;
219 	uint32_t	st_fwrev;
220 	uint32_t	__pad;
221 	void		*st_cin;
222 	void		*st_cinif;
223 	void		*st_cout;
224 	void		*st_coutif;
225 	void		*st_cerr;
226 	void		*st_cerrif;
227 	uint64_t	st_rt;
228 	void		*st_bs;
229 	u_long		st_entries;
230 	uint64_t	st_cfgtbl;
231 };
232 
233 extern vm_paddr_t efi_systbl_phys;
234 
235 /*
236  * When memory is reserved for some use, Linux will add a
237  * LINUX_EFI_MEMSERVE_TABLE to the cfgtbl array of tables to communicate
238  * this. At present, Linux only uses this as part of its workaround for a GICv3
239  * issue where you can't stop the controller long enough to move it's config and
240  * pending vectors. When the LinuxBoot environment kexec's a new kernel, the new
241  * kernel needs to use this old memory (and not use it for any other purpose).
242  *
243  * Linux stores the PA of this table in the cfgtbl. And all the addresses are
244  * the physical address of 'reserved' memory. The mr_next field creates a linked
245  * list of these tables, and all must be walked. If mr_count is 0, that entry
246  * should be ignored. There is no checksum for these tables, nor do they have
247  * a efi_tblhdr.
248  *
249  * This table is only documented in the Linux code in drivers/firmware/efi/efi.c.
250  */
251 struct linux_efi_memreserve_entry {
252 	vm_offset_t	mre_base;	/* PA of reserved area */
253 	vm_offset_t	mre_size;	/* Size of area */
254 };
255 
256 struct linux_efi_memreserve {
257 	uint32_t	mr_size;	/* Total size of table in bytes */
258 	uint32_t	mr_count;	/* Count of entries used */
259 	vm_offset_t	mr_next;	/* Next in chain (though unused?) */
260 	struct linux_efi_memreserve_entry mr_entry[];
261 };
262 
263 struct efirt_callinfo;
264 
265 /* Internal MD EFI functions */
266 int efi_arch_enter(void);
267 void efi_arch_leave(void);
268 vm_offset_t efi_phys_to_kva(vm_paddr_t);
269 int efi_rt_arch_call(struct efirt_callinfo *);
270 bool efi_create_1t1_map(struct efi_md *, int, int);
271 void efi_destroy_1t1_map(void);
272 
273 struct efi_ops {
274 	/*
275 	 * The EFI calls might be virtualized in some environments, requiring
276 	 * FreeBSD to use a different interface (ie: hypercalls) in order to
277 	 * access them.
278 	 */
279 	int	(*rt_ok)(void);
280 	int 	(*get_table)(efi_guid_t *, void **);
281 	int 	(*copy_table)(efi_guid_t *, void **, size_t, size_t *);
282 	int 	(*get_time)(struct efi_tm *);
283 	int 	(*get_time_capabilities)(struct efi_tmcap *);
284 	int	(*reset_system)(enum efi_reset);
285 	int 	(*set_time)(struct efi_tm *);
286 	int 	(*get_waketime)(uint8_t *enabled, uint8_t *pending,
287 	    struct efi_tm *tm);
288 	int 	(*set_waketime)(uint8_t enable, struct efi_tm *tm);
289 	int 	(*var_get)(uint16_t *, efi_guid_t *, uint32_t *, size_t *,
290     void *);
291 	int 	(*var_nextname)(size_t *, uint16_t *, efi_guid_t *);
292 	int 	(*var_set)(uint16_t *, efi_guid_t *, uint32_t, size_t, void *);
293 };
294 extern const struct efi_ops *active_efi_ops;
295 
296 /* Public MI EFI functions */
297 static inline int
efi_rt_ok(void)298 efi_rt_ok(void)
299 {
300 	if (active_efi_ops->rt_ok == NULL)
301 		return (ENXIO);
302 	return (active_efi_ops->rt_ok());
303 }
304 
305 static inline int
efi_get_table(efi_guid_t * guid,void ** ptr)306 efi_get_table(efi_guid_t *guid, void **ptr)
307 {
308         if (active_efi_ops->get_table == NULL)
309 		return (ENXIO);
310 	return (active_efi_ops->get_table(guid, ptr));
311 }
312 
313 static inline int
efi_copy_table(efi_guid_t * guid,void ** buf,size_t buf_len,size_t * table_len)314 efi_copy_table(efi_guid_t *guid, void **buf, size_t buf_len, size_t *table_len)
315 {
316 	if (active_efi_ops->copy_table == NULL)
317 		return (ENXIO);
318 	return (active_efi_ops->copy_table(guid, buf, buf_len, table_len));
319 }
320 
321 static inline int
efi_get_time(struct efi_tm * tm)322 efi_get_time(struct efi_tm *tm)
323 {
324 	if (active_efi_ops->get_time == NULL)
325 		return (ENXIO);
326 	return (active_efi_ops->get_time(tm));
327 }
328 
329 static inline int
efi_get_time_capabilities(struct efi_tmcap * tmcap)330 efi_get_time_capabilities(struct efi_tmcap *tmcap)
331 {
332 	if (active_efi_ops->get_time_capabilities == NULL)
333 		return (ENXIO);
334 	return (active_efi_ops->get_time_capabilities(tmcap));
335 }
336 
337 static inline int
efi_reset_system(enum efi_reset type)338 efi_reset_system(enum efi_reset type)
339 {
340 	if (active_efi_ops->reset_system == NULL)
341 		return (ENXIO);
342 	return (active_efi_ops->reset_system(type));
343 }
344 
345 static inline int
efi_set_time(struct efi_tm * tm)346 efi_set_time(struct efi_tm *tm)
347 {
348 	if (active_efi_ops->set_time == NULL)
349 		return (ENXIO);
350 	return (active_efi_ops->set_time(tm));
351 }
352 
353 static inline int
efi_get_waketime(uint8_t * enabled,uint8_t * pending,struct efi_tm * tm)354 efi_get_waketime(uint8_t *enabled, uint8_t *pending, struct efi_tm *tm)
355 {
356 	if (active_efi_ops->get_waketime == NULL)
357 		return (ENXIO);
358 	return (active_efi_ops->get_waketime(enabled, pending, tm));
359 }
360 
361 static inline int
efi_set_waketime(uint8_t enable,struct efi_tm * tm)362 efi_set_waketime(uint8_t enable, struct efi_tm *tm)
363 {
364 	if (active_efi_ops->set_waketime == NULL)
365 		return (ENXIO);
366 	return (active_efi_ops->set_waketime(enable, tm));
367 }
368 
369 static inline int
efi_var_get(uint16_t * name,efi_guid_t * vendor,uint32_t * attrib,size_t * datasize,void * data)370 efi_var_get(uint16_t *name, efi_guid_t *vendor, uint32_t *attrib,
371     size_t *datasize, void *data)
372 {
373 	if (active_efi_ops->var_get == NULL)
374 		return (ENXIO);
375 	return (active_efi_ops->var_get(name, vendor, attrib, datasize, data));
376 }
377 
378 static inline int
efi_var_nextname(size_t * namesize,uint16_t * name,efi_guid_t * vendor)379 efi_var_nextname(size_t *namesize, uint16_t *name, efi_guid_t *vendor)
380 {
381 	if (active_efi_ops->var_nextname == NULL)
382 		return (ENXIO);
383 	return (active_efi_ops->var_nextname(namesize, name, vendor));
384 }
385 
386 static inline int
efi_var_set(uint16_t * name,efi_guid_t * vendor,uint32_t attrib,size_t datasize,void * data)387 efi_var_set(uint16_t *name, efi_guid_t *vendor, uint32_t attrib,
388     size_t datasize, void *data)
389 {
390 	if (active_efi_ops->var_set == NULL)
391 		return (ENXIO);
392 	return (active_efi_ops->var_set(name, vendor, attrib, datasize, data));
393 }
394 
395 int efi_status_to_errno(efi_status status);
396 
397 #endif	/* _KERNEL */
398 
399 #endif /* _SYS_EFI_H_ */
400