xref: /netbsd-src/sys/arch/x86/include/efi.h (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*     $NetBSD: efi.h,v 1.9 2019/10/18 00:54:48 manu 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  * $FreeBSD$
29  */
30 
31 /* This file is mainly ia64/include/efi.h with little modifications. */
32 
33 #ifndef _X86_EFI_H_
34 #define _X86_EFI_H_
35 
36 #include <sys/uuid.h>
37 
38 #define        EFI_PAGE_SHIFT          12
39 #define        EFI_PAGE_SIZE           (1 << EFI_PAGE_SHIFT)
40 #define        EFI_PAGE_MASK           (EFI_PAGE_SIZE - 1)
41 
42 #define	EFI_TABLE_ACPI20						\
43 	{0x8868e871,0xe4f1,0x11d3,0xbc,0x22,{0x00,0x80,0xc7,0x3c,0x88,0x81}}
44 #define	EFI_TABLE_ACPI10						\
45 	{0xeb9d2d30,0x2d88,0x11d3,0x9a,0x16,{0x00,0x90,0x27,0x3f,0xc1,0x4d}}
46 #define	EFI_TABLE_SMBIOS						\
47 	{0xeb9d2d31,0x2d88,0x11d3,0x9a,0x16,{0x00,0x90,0x27,0x3f,0xc1,0x4d}}
48 #define	EFI_TABLE_SMBIOS3						\
49 	{0xf2fd1544,0x9794,0x4a2c,0x99,0x2e,{0xe5,0xbb,0xcf,0x20,0xe3,0x94}}
50 
51 extern const struct uuid EFI_UUID_ACPI20;
52 extern const struct uuid EFI_UUID_ACPI10;
53 extern const struct uuid EFI_UUID_SMBIOS;
54 extern const struct uuid EFI_UUID_SMBIOS3;
55 
56 extern bool bootmethod_efi;
57 
58 enum efi_reset {
59        EFI_RESET_COLD,
60        EFI_RESET_WARM
61 };
62 
63 typedef uint16_t       efi_char;
64 typedef unsigned long efi_status;
65 
66 #if defined(__amd64__)
67 typedef uint64_t uintn;
68 #elif defined(__i386__)
69 typedef uint32_t uintn;
70 #endif
71 
72 struct efi_cfgtbl {
73        struct uuid     ct_uuid;
74        void           *ct_data;
75 };
76 
77 struct efi_md {
78        uint32_t        md_type;
79 #define        EFI_MD_TYPE_NULL        0
80 #define        EFI_MD_TYPE_CODE        1       /* Loader text. */
81 #define        EFI_MD_TYPE_DATA        2       /* Loader data. */
82 #define        EFI_MD_TYPE_BS_CODE     3       /* Boot services text. */
83 #define        EFI_MD_TYPE_BS_DATA     4       /* Boot services data. */
84 #define        EFI_MD_TYPE_RT_CODE     5       /* Runtime services text. */
85 #define        EFI_MD_TYPE_RT_DATA     6       /* Runtime services data. */
86 #define        EFI_MD_TYPE_FREE        7       /* Unused/free memory. */
87 #define        EFI_MD_TYPE_BAD         8       /* Bad memory */
88 #define        EFI_MD_TYPE_RECLAIM     9       /* ACPI reclaimable memory. */
89 #define        EFI_MD_TYPE_FIRMWARE    10      /* ACPI NV memory */
90 #define        EFI_MD_TYPE_IOMEM       11      /* Memory-mapped I/O. */
91 #define        EFI_MD_TYPE_IOPORT      12      /* I/O port space. */
92 #define        EFI_MD_TYPE_PALCODE     13      /* PAL */
93 #define        EFI_MD_TYPE_PMEM        14      /* Persistent memory. */
94        uint32_t        __pad;
95        uint64_t        md_phys;
96        uint64_t        md_virt;
97        uint64_t        md_pages;
98        uint64_t        md_attr;
99 #define        EFI_MD_ATTR_UC          0x0000000000000001UL
100 #define        EFI_MD_ATTR_WC          0x0000000000000002UL
101 #define        EFI_MD_ATTR_WT          0x0000000000000004UL
102 #define        EFI_MD_ATTR_WB          0x0000000000000008UL
103 #define        EFI_MD_ATTR_UCE         0x0000000000000010UL
104 #define        EFI_MD_ATTR_WP          0x0000000000001000UL
105 #define        EFI_MD_ATTR_RP          0x0000000000002000UL
106 #define        EFI_MD_ATTR_XP          0x0000000000004000UL
107 #define        EFI_MD_ATTR_RT          0x8000000000000000UL
108 };
109 
110 struct efi_tm {
111        uint16_t        tm_year;                /* 1998 - 20XX */
112        uint8_t         tm_mon;                 /* 1 - 12 */
113        uint8_t         tm_mday;                /* 1 - 31 */
114        uint8_t         tm_hour;                /* 0 - 23 */
115        uint8_t         tm_min;                 /* 0 - 59 */
116        uint8_t         tm_sec;                 /* 0 - 59 */
117        uint8_t         __pad1;
118        uint32_t        tm_nsec;                /* 0 - 999,999,999 */
119        int16_t         tm_tz;                  /* -1440 to 1440 or 2047 */
120        uint8_t         tm_dst;
121        uint8_t         __pad2;
122 };
123 
124 struct efi_tmcap {
125        uint32_t        tc_res;         /* 1e-6 parts per million */
126        uint32_t        tc_prec;        /* hertz */
127        uint8_t         tc_stz;         /* Set clears sub-second time */
128 };
129 
130 struct efi_tblhdr {
131        uint64_t        th_sig;
132        uint32_t        th_rev;
133        uint32_t        th_hdrsz;
134        uint32_t        th_crc32;
135        uint32_t        __res;
136 };
137 
138 struct efi_rt {
139        struct efi_tblhdr rt_hdr;
140        efi_status      (*rt_gettime)(struct efi_tm *, struct efi_tmcap *);
141        efi_status      (*rt_settime)(struct efi_tm *);
142        efi_status      (*rt_getwaketime)(uint8_t *, uint8_t *,
143            struct efi_tm *);
144        efi_status      (*rt_setwaketime)(uint8_t, struct efi_tm *);
145        efi_status      (*rt_setvirtual)(u_long, u_long, uint32_t,
146            struct efi_md *);
147        efi_status      (*rt_cvtptr)(u_long, void **);
148        efi_status      (*rt_getvar)(efi_char *, struct uuid *, uint32_t *,
149            u_long *, void *);
150        efi_status      (*rt_scanvar)(u_long *, efi_char *, struct uuid *);
151        efi_status      (*rt_setvar)(efi_char *, struct uuid *, uint32_t,
152            u_long, void *);
153        efi_status      (*rt_gethicnt)(uint32_t *);
154        efi_status      (*rt_reset)(enum efi_reset, efi_status, u_long,
155            efi_char *);
156 };
157 
158 typedef uintn efi_tpl;
159 typedef void *efi_event;
160 typedef void (*efi_event_notify)(efi_event, void *);
161 typedef void *efi_handle;
162 typedef struct {
163 	uint8_t type;
164 	uint8_t subtype;
165 	uint8_t ldnegth[2];
166 } efi_device_path;
167 
168 struct efi_bs {
169        struct efi_tblhdr bs_hdr;
170 #define		EFI_BS_SIG  0x56524553544f4f42UL
171        efi_tpl         (*bs_raisetpl)(efi_tpl);
172        void            (*bs_restoretpl)(efi_tpl);
173        efi_status      (*bs_allocatepages)(uint32_t, uint32_t,
174 	   uintn, paddr_t *);
175        efi_status      (*bs_freepages)(paddr_t, uintn);
176        efi_status      (*bs_getmemorymap)(uintn *, struct efi_md *,
177 	   uintn *, uintn *, uint32_t *);
178        efi_status      (*bs_allocatepool)(uint32_t, uintn, void **);
179        efi_status      (*bs_freepool)(void *);
180        efi_status      (*bs_createevent)(uint32_t, efi_tpl, efi_event_notify,
181 	   void *, efi_event *);
182        efi_status      (*bs_settimer)(efi_event, uint32_t, uint64_t);
183        efi_status      (*bs_waitforevent)(uintn, efi_event *, uintn *);
184        efi_status      (*bs_signalevent)(efi_event);
185        efi_status      (*bs_closeevent)(efi_event);
186        efi_status      (*bs_checkevent)(efi_event);
187        efi_status      (*bs_installprotocolinterface)(efi_handle *,
188 	   struct uuid *, uint32_t, void *);
189        efi_status      (*bs_reinstallprotocolinterface)(efi_handle *,
190 	   struct uuid *, void *, void *);
191        efi_status      (*bs_uninstallprotocolinterface)(efi_handle *,
192 	   struct uuid *, void *);
193        efi_status      (*bs_handleprotocol)(efi_handle,
194 	   struct uuid *, void **);
195        efi_status      (*bs_pchandleprotocol)(efi_handle,
196 	   struct uuid *, void **);
197        efi_status      (*bs_registerprotocolnotify)(struct uuid *, efi_event,
198 	   void **);
199        efi_status      (*bs_locatehandle)(uint32_t, struct uuid *, void *,
200 	   uintn *, efi_handle *);
201        efi_status      (*bs_locatedevicepath)(struct uuid *, efi_device_path **,
202 	   efi_handle *);
203        efi_status      (*bs_installconfigurationtable)(struct uuid *, void *);
204        efi_status      (*bs_loadimage)(uint8_t, efi_handle, efi_device_path *,
205 	   void *, uintn, efi_handle *);
206        efi_status      (*bs_startimage)(efi_handle, uintn *, efi_char **);
207        efi_status      (*bs_exit)(efi_handle, efi_status, uintn, efi_char *);
208        efi_status      (*bs_unloadimage)(efi_handle);
209        efi_status      (*bs_exitbootservices)(efi_handle, uintn);
210        efi_status      (*bs_getnextmonotoniccount)(uint64_t *);
211        efi_status      (*bs_stall)(uintn);
212        efi_status      (*bs_setwatchdogtimer)(uintn, uint64_t,
213 	   uintn, efi_char *);
214        efi_status      (*bs_connectcontroller)(efi_handle, efi_handle *,
215 	    efi_device_path *, uint8_t);
216        efi_status      (*bs_disconnectcontroller)(efi_handle, efi_handle,
217 	    efi_handle);
218        efi_status      (*bs_openprotocol)(efi_handle, struct uuid *, void **,
219 	    efi_handle, efi_handle, uint32_t);
220        efi_status      (*bs_closeprotocol)(efi_handle, struct uuid *,
221 	    efi_handle, efi_handle);
222        efi_status      (*bs_openprotocolinformation)(efi_handle, efi_handle,
223 	    uint32_t, uint32_t);
224        efi_status      (*bs_protocolsperhandle)(efi_handle,
225 	    struct uuid ***, uintn *);
226        efi_status      (*bs_locatehandlebuffer)(uint32_t, struct uuid *,
227 	    void *, uintn *, efi_handle **);
228        efi_status      (*bs_locateprotocol)(struct uuid *, void *, void **);
229        efi_status      (*bs_installmultipleprotocolinterfaces)(efi_handle *,
230 	    ...);
231        efi_status      (*bs_uninstallmultipleprotocolinterfaces)(efi_handle,
232 	    ...);
233        efi_status      (*bs_calculatecrc32)(void *, uintn, uint32_t *);
234        efi_status      (*bs_copymem)(void *, void *, uintn);
235        efi_status      (*bs_setmem)(void *, uintn, uint8_t);
236        efi_status      (*bs_createeventex)(uint32_t, efi_tpl,
237 	   efi_event_notify, void *, struct uuid, efi_event *);
238 };
239 
240 struct efi_input;
241 
242 struct efi_key {
243 	uint16_t	scancode;
244 	efi_char	unicodechar;
245 };
246 
247 struct efi_input {
248 	efi_status     (*ei_reset)(struct efi_input *, uint8_t);
249 	efi_status     (*ei_readkeystroke)(struct efi_input *,
250 	    struct efi_key *);
251 	void            *ei_waitforkey;
252 };
253 
254 typedef struct efi_input efi_input;
255 
256 struct efi_text_output_mode {
257 	int32_t	maxmode;
258 	int32_t mode;
259 	int32_t attribute;
260 	int32_t cursorcolumn;
261 	int32_t cursorrow;
262 	uint8_t cursorvisible;
263 };
264 
265 
266 struct efi_output;
267 
268 struct efi_output {
269 	efi_status     (*ei_reset)(struct efi_output *, uint8_t);
270 	efi_status     (*ei_outputstring)(struct efi_output *, efi_char *);
271 	efi_status     (*ei_teststring)(struct efi_output *, efi_char *);
272 	efi_status     (*ei_textquerymode)(struct efi_output *,
273 	    uintn, uintn *, uintn *);
274 	efi_status     (*ei_textsetmode)(struct efi_output *, uintn);
275 	efi_status     (*ei_setattribute)(struct efi_output *, uintn);
276 	efi_status     (*ei_clearscreen)(struct efi_output *);
277 	efi_status     (*ei_setcursorposition)(struct efi_output *,
278 	    uintn, uintn);
279 	efi_status     (*ei_enablecursor)(struct efi_output *, uint8_t);
280 	struct efi_text_output_mode *ei_mode;
281 };
282 
283 typedef struct efi_output efi_output;
284 
285 struct efi_systbl {
286        struct efi_tblhdr st_hdr;
287 #define        EFI_SYSTBL_SIG  0x5453595320494249UL
288        efi_char        *st_fwvendor;
289        uint32_t        st_fwrev;
290 #ifdef __amd64__
291        uint32_t        __pad;
292 #endif
293        efi_handle      *st_cin;
294        efi_input       *st_cinif;
295        efi_handle      *st_cout;
296        efi_output      *st_coutif;
297        efi_handle      *st_cerr;
298        efi_output      *st_cerrif;
299        struct efi_rt   *st_rt;
300        struct efi_bs   *st_bs;
301        u_long          st_entries;
302        struct efi_cfgtbl *st_cfgtbl;
303 };
304 
305 #if defined(__amd64__)
306 struct efi_cfgtbl32 {
307 	struct uuid		ct_uuid;
308 	uint32_t		ct_data;	/* void * */
309 };
310 
311 struct efi_systbl32 {
312 	struct efi_tblhdr	st_hdr;
313 
314 	uint32_t		st_fwvendor;
315 	uint32_t		st_fwrev;
316 	uint32_t		st_cin;		/* = 0 */
317 	uint32_t		st_cinif;	/* = 0 */
318 	uint32_t		st_cout;	/* = 0 */
319 	uint32_t		st_coutif;	/* = 0 */
320 	uint32_t		st_cerr;	/* = 0 */
321 	uint32_t		st_cerrif;	/* = 0 */
322 	uint32_t		st_rt;		/* struct efi_rt32 * */
323 	uint32_t		st_bs;		/* = 0 */
324 	uint32_t		st_entries;
325 	uint32_t		st_cfgtbl;	/* struct efi_cfgtbl32 * */
326 };
327 #elif defined(__i386__)
328 struct efi_cfgtbl64 {
329 	struct uuid		ct_uuid;
330 	uint64_t		ct_data;	/* void * */
331 };
332 
333 struct efi_systbl64 {
334 	struct efi_tblhdr	st_hdr;
335 
336 	uint64_t		st_fwvendor;
337 	uint32_t		st_fwrev;
338 	uint32_t		__pad;
339 	uint64_t		st_cin;		/* = 0 */
340 	uint64_t		st_cinif;	/* = 0 */
341 	uint64_t		st_cout;	/* = 0 */
342 	uint64_t		st_coutif;	/* = 0 */
343 	uint64_t		st_cerr;	/* = 0 */
344 	uint64_t		st_cerrif;	/* = 0 */
345 	uint64_t		st_rt;		/* struct efi_rt64 * */
346 	uint64_t		st_bs;		/* = 0 */
347 	uint64_t		st_entries;
348 	uint64_t		st_cfgtbl;	/* struct efi_cfgtbl64 * */
349 };
350 #endif
351 
352 void               efi_init(void);
353 bool               efi_probe(void);
354 paddr_t            efi_getsystblpa(void);
355 struct efi_systbl *efi_getsystbl(void);
356 paddr_t            efi_getcfgtblpa(const struct uuid*);
357 void              *efi_getcfgtbl(const struct uuid*);
358 int                efi_getbiosmemtype(uint32_t, uint64_t);
359 const char        *efi_getmemtype_str(uint32_t);
360 struct btinfo_memmap;
361 struct btinfo_memmap *efi_get_e820memmap(void);
362 
363 /*
364 void efi_boot_finish(void);
365 int efi_boot_minimal(uint64_t);
366 void *efi_get_table(struct uuid *);
367 void efi_get_time(struct efi_tm *);
368 struct efi_md *efi_md_first(void);
369 struct efi_md *efi_md_next(struct efi_md *);
370 void efi_reset_system(void);
371 efi_status efi_set_time(struct efi_tm *);
372 */
373 
374 #endif /* _X86_EFI_H_ */
375