xref: /netbsd-src/sys/dev/efi/efi.h (revision 7d62b00eb9ad855ffcd7da46b41e23feb5476fac)
1 /*	$NetBSD: efi.h,v 1.2 2022/09/22 14:46:37 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2004 Marcel Moolenaar
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef	_DEV_EFI_EFI_H_
30 #define	_DEV_EFI_EFI_H_
31 
32 /*
33  * Machine-independent UEFI protocol definitions.  Based on:
34  *
35  *	Unified Extensible Firmware Interface (UEFI) Specification,
36  *	Version 2.9, UEFI Forum, Inc., March 2021.
37  *
38  * https://uefi.org/sites/default/files/resources/UEFI_Spec_2_9_2021_03_18.pdf
39  */
40 
41 #if defined(__i386__) || defined(__x86_64__)
42 #define	EFIAPI	__attribute__((__ms_abi__))
43 #else
44 #define	EFIAPI	/* empty */
45 #endif
46 
47 enum efi_reset {
48 	EFI_RESET_COLD,
49 	EFI_RESET_WARM,
50 	EFI_RESET_SHUTDOWN,
51 	EFI_RESET_PLATFORM_SPECIFIC,
52 };
53 
54 typedef uint16_t	efi_char;
55 typedef unsigned long efi_status;
56 
57 struct efi_cfgtbl {
58 	struct uuid	ct_uuid;
59 	void		*ct_data;
60 };
61 
62 struct efi_md {
63 	uint32_t	md_type;
64 #define	EFI_MD_TYPE_NULL	0
65 #define	EFI_MD_TYPE_CODE	1	/* Loader text. */
66 #define	EFI_MD_TYPE_DATA	2	/* Loader data. */
67 #define	EFI_MD_TYPE_BS_CODE	3	/* Boot services text. */
68 #define	EFI_MD_TYPE_BS_DATA	4	/* Boot services data. */
69 #define	EFI_MD_TYPE_RT_CODE	5	/* Runtime services text. */
70 #define	EFI_MD_TYPE_RT_DATA	6	/* Runtime services data. */
71 #define	EFI_MD_TYPE_FREE	7	/* Unused/free memory. */
72 #define	EFI_MD_TYPE_BAD		8	/* Bad memory */
73 #define	EFI_MD_TYPE_RECLAIM	9	/* ACPI reclaimable memory. */
74 #define	EFI_MD_TYPE_FIRMWARE	10	/* ACPI NV memory */
75 #define	EFI_MD_TYPE_IOMEM	11	/* Memory-mapped I/O. */
76 #define	EFI_MD_TYPE_IOPORT	12	/* I/O port space. */
77 #define	EFI_MD_TYPE_PALCODE	13	/* PAL */
78 #define	EFI_MD_TYPE_PMEM	14	/* Persistent memory. */
79 	uint32_t	__pad;
80 	uint64_t	md_phys;
81 	uint64_t	md_virt;
82 	uint64_t	md_pages;
83 	uint64_t	md_attr;
84 #define	EFI_MD_ATTR_UC		0x0000000000000001UL
85 #define	EFI_MD_ATTR_WC		0x0000000000000002UL
86 #define	EFI_MD_ATTR_WT		0x0000000000000004UL
87 #define	EFI_MD_ATTR_WB		0x0000000000000008UL
88 #define	EFI_MD_ATTR_UCE		0x0000000000000010UL
89 #define	EFI_MD_ATTR_WP		0x0000000000001000UL
90 #define	EFI_MD_ATTR_RP		0x0000000000002000UL
91 #define	EFI_MD_ATTR_XP		0x0000000000004000UL
92 #define	EFI_MD_ATTR_NV		0x0000000000008000UL
93 #define	EFI_MD_ATTR_MORE_RELIABLE 0x0000000000010000UL
94 #define	EFI_MD_ATTR_RO		0x0000000000020000UL
95 #define	EFI_MD_ATTR_SP		0x0000000000040000UL
96 #define	EFI_MD_ATTR_CPU_CRYPTO	0x0000000000080000UL
97 #define	EFI_MD_ATTR_RT		0x8000000000000000UL
98 };
99 
100 struct efi_tm {
101 	uint16_t	tm_year;		/* 1998 - 20XX */
102 	uint8_t		tm_mon;			/* 1 - 12 */
103 	uint8_t		tm_mday;		/* 1 - 31 */
104 	uint8_t		tm_hour;		/* 0 - 23 */
105 	uint8_t		tm_min;			/* 0 - 59 */
106 	uint8_t		tm_sec;			/* 0 - 59 */
107 	uint8_t		__pad1;
108 	uint32_t	tm_nsec;		/* 0 - 999,999,999 */
109 	int16_t		tm_tz;			/* -1440 to 1440 or 2047 */
110 	uint8_t		tm_dst;
111 	uint8_t		__pad2;
112 };
113 
114 struct efi_tmcap {
115 	uint32_t	tc_res;		/* 1e-6 parts per million */
116 	uint32_t	tc_prec;	/* hertz */
117 	uint8_t		tc_stz;		/* Set clears sub-second time */
118 };
119 
120 struct efi_tblhdr {
121 	uint64_t	th_sig;
122 	uint32_t	th_rev;
123 	uint32_t	th_hdrsz;
124 	uint32_t	th_crc32;
125 	uint32_t	__res;
126 };
127 
128 struct efi_rt {
129 	struct efi_tblhdr rt_hdr;
130 	efi_status	(*rt_gettime)(struct efi_tm *, struct efi_tmcap *)
131 			    EFIAPI;
132 	efi_status	(*rt_settime)(struct efi_tm *) EFIAPI;
133 	efi_status	(*rt_getwaketime)(uint8_t *, uint8_t *,
134 			    struct efi_tm *) EFIAPI;
135 	efi_status	(*rt_setwaketime)(uint8_t, struct efi_tm *) EFIAPI;
136 	efi_status	(*rt_setvirtual)(u_long, u_long, uint32_t,
137 			    struct efi_md *) EFIAPI;
138 	efi_status	(*rt_cvtptr)(u_long, void **) EFIAPI;
139 	efi_status	(*rt_getvar)(efi_char *, struct uuid *, uint32_t *,
140 			    u_long *, void *) EFIAPI;
141 	efi_status	(*rt_scanvar)(u_long *, efi_char *, struct uuid *)
142 			    EFIAPI;
143 	efi_status	(*rt_setvar)(efi_char *, struct uuid *, uint32_t,
144 			    u_long, void *) EFIAPI;
145 	efi_status	(*rt_gethicnt)(uint32_t *) EFIAPI;
146 	efi_status	(*rt_reset)(enum efi_reset, efi_status, u_long,
147 			    efi_char *) EFIAPI;
148 };
149 
150 struct efi_systbl {
151 	struct efi_tblhdr st_hdr;
152 #define	EFI_SYSTBL_SIG	0x5453595320494249UL
153 	efi_char	*st_fwvendor;
154 	uint32_t	st_fwrev;
155 #ifdef _LP64
156 	uint32_t	__pad;
157 #endif
158 	void		*st_cin;
159 	void		*st_cinif;
160 	void		*st_cout;
161 	void		*st_coutif;
162 	void		*st_cerr;
163 	void		*st_cerrif;
164 	struct efi_rt	*st_rt;
165 	void		*st_bs;
166 	u_long		st_entries;
167 	struct efi_cfgtbl *st_cfgtbl;
168 };
169 #ifdef _LP64
170 __CTASSERT(sizeof(struct efi_systbl) == 120);
171 #else
172 __CTASSERT(sizeof(struct efi_systbl) == 72);
173 #endif
174 
175 #endif	/* _DEV_EFI_EFI_H_ */
176