xref: /illumos-gate/usr/src/boot/efi/include/efilib.h (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
1 /*
2  * Copyright (c) 2000 Doug Rabson
3  * Copyright (c) 2006 Marcel Moolenaar
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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 AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef _LOADER_EFILIB_H
29 #define	_LOADER_EFILIB_H
30 
31 #include <stand.h>
32 #include <stdbool.h>
33 #include <sys/queue.h>
34 #include <Protocol/BlockIo.h>
35 #include <Protocol/SerialIo.h>
36 #include <Protocol/IsaIo.h>
37 
38 extern EFI_HANDLE		IH;
39 extern EFI_SYSTEM_TABLE		*ST;
40 extern EFI_BOOT_SERVICES	*BS;
41 extern EFI_RUNTIME_SERVICES	*RS;
42 
43 extern struct devsw efipart_fddev;
44 extern struct devsw efipart_cddev;
45 extern struct devsw efipart_hddev;
46 extern struct devsw efinet_dev;
47 extern struct netif_driver efinetif;
48 
49 /*
50  * Serial port descriptor.
51  */
52 typedef STAILQ_HEAD(serial_list, serial) serial_list_t;
53 
54 struct serial {
55 	STAILQ_ENTRY(serial)	next;
56 	uint64_t	baudrate;
57 	uint32_t	timeout;
58 	uint32_t	receivefifodepth;
59 	uint32_t	databits;
60 	EFI_PARITY_TYPE	parity;
61 	EFI_STOP_BITS_TYPE	stopbits;
62 	bool		ignore_cd;
63 	bool		rtsdtr_off;
64 	bool		is_efi_console;	/* EFI Console device */
65 	EFI_HANDLE	currdev;	/* current serial device */
66 	EFI_HANDLE	iodev;		/* handle to IO layer */
67 	EFI_GUID	*guid;		/* Driver protocol guid */
68 	union {
69 		EFI_SERIAL_IO_PROTOCOL	*sio;
70 		EFI_ISA_IO_PROTOCOL	*isa;
71 	}		io;
72 	uint32_t	ioaddr;
73 	char		name;		/* 'a'-'d' or '0'-'9' */
74 };
75 
76 extern serial_list_t serials;		/* In efiserialio.c */
77 
78 /* EFI block device data, included here to help efi_zfs_probe() */
79 typedef STAILQ_HEAD(pdinfo_list, pdinfo) pdinfo_list_t;
80 
81 typedef struct pdinfo
82 {
83 	STAILQ_ENTRY(pdinfo)	pd_link;	/* link in device list */
84 	pdinfo_list_t		pd_part;	/* list of partitions */
85 	EFI_HANDLE		pd_handle;
86 	EFI_HANDLE		pd_alias;
87 	EFI_DEVICE_PATH		*pd_devpath;
88 	EFI_BLOCK_IO		*pd_blkio;
89 	uint32_t		pd_unit;	/* unit number */
90 	uint32_t		pd_open;	/* reference counter */
91 	void			*pd_bcache;	/* buffer cache data */
92 	struct pdinfo		*pd_parent; /* Linked items (eg partitions) */
93 	struct devsw		*pd_devsw;	/* Back pointer to devsw */
94 } pdinfo_t;
95 
96 pdinfo_list_t *efiblk_get_pdinfo_list(struct devsw *dev);
97 pdinfo_t *efiblk_get_pdinfo(struct devdesc *dev);
98 pdinfo_t *efiblk_get_pdinfo_by_handle(EFI_HANDLE h);
99 
100 void *efi_get_table(EFI_GUID *tbl);
101 EFI_STATUS OpenProtocolByHandle(EFI_HANDLE, EFI_GUID *, void **);
102 EFI_STATUS efi_get_protocol_handles(EFI_GUID *, uint_t *, EFI_HANDLE **);
103 
104 int efi_getdev(void **, const char *, const char **);
105 char *efi_fmtdev(void *);
106 int efi_setcurrdev(struct env_var *, int, const void *);
107 
108 int efi_register_handles(struct devsw *, EFI_HANDLE *, EFI_HANDLE *, int);
109 EFI_HANDLE efi_find_handle(struct devsw *, int);
110 int efi_handle_lookup(EFI_HANDLE, struct devsw **, int *,  uint64_t *);
111 int efi_handle_update_dev(EFI_HANDLE, struct devsw *, int, uint64_t);
112 
113 EFI_DEVICE_PATH *efi_lookup_image_devpath(EFI_HANDLE);
114 EFI_DEVICE_PATH *efi_lookup_devpath(EFI_HANDLE);
115 void efi_close_devpath(EFI_HANDLE);
116 EFI_HANDLE efi_devpath_handle(EFI_DEVICE_PATH *);
117 EFI_DEVICE_PATH *efi_devpath_last_node(EFI_DEVICE_PATH *);
118 EFI_DEVICE_PATH *efi_devpath_trim(EFI_DEVICE_PATH *);
119 EFI_DEVICE_PATH *efi_devpath_next_instance(EFI_DEVICE_PATH *);
120 bool efi_devpath_match(EFI_DEVICE_PATH *, EFI_DEVICE_PATH *);
121 bool efi_devpath_is_prefix(EFI_DEVICE_PATH *, EFI_DEVICE_PATH *);
122 CHAR16 *efi_devpath_name(EFI_DEVICE_PATH *);
123 void efi_free_devpath_name(CHAR16 *);
124 
125 int efi_status_to_errno(EFI_STATUS);
126 EFI_STATUS errno_to_efi_status(int errno);
127 
128 void efi_time_init(void);
129 void efi_time_fini(void);
130 
131 void efi_serial_ini(void);
132 void efi_isa_ini(void);
133 
134 EFI_STATUS efi_main(EFI_HANDLE Ximage, EFI_SYSTEM_TABLE* Xsystab);
135 
136 EFI_STATUS main(int argc, CHAR16 *argv[]);
137 void efi_exit(EFI_STATUS status) __dead2;
138 void delay(int usecs);
139 
140 /* EFI environment initialization. */
141 void efi_init_environment(void);
142 
143 /* EFI Memory type strings. */
144 const char *efi_memory_type(EFI_MEMORY_TYPE);
145 
146 /* CHAR16 utility functions. */
147 int wcscmp(CHAR16 *, CHAR16 *);
148 void cpy8to16(const char *, CHAR16 *, size_t);
149 void cpy16to8(const CHAR16 *, char *, size_t);
150 
151 /*
152  * Routines for interacting with EFI's env vars in a more unix-like
153  * way than the standard APIs. In addition, convenience routines for
154  * the loader setting / getting illumos specific variables.
155  */
156 
157 EFI_STATUS efi_illumos_getenv(const char *v, void *data, size_t *len);
158 EFI_STATUS efi_getenv(EFI_GUID *g, const char *v, void *data, size_t *len);
159 EFI_STATUS efi_setenv(EFI_GUID *g, uint32_t, const char *, void *, size_t);
160 EFI_STATUS efi_global_getenv(const char *v, void *data, size_t *len);
161 EFI_STATUS efi_global_setenv(const char *v, void *data, size_t len);
162 EFI_STATUS efi_setenv_illumos_wcs(const char *varname, CHAR16 *valstr);
163 
164 /* guids and names */
165 bool efi_guid_to_str(const EFI_GUID *, char **);
166 bool efi_str_to_guid(const char *, EFI_GUID *);
167 bool efi_name_to_guid(const char *, EFI_GUID *);
168 bool efi_guid_to_name(EFI_GUID *, char **);
169 
170 /* efipart.c */
171 int	efipart_inithandles(void);
172 
173 #endif /* _LOADER_EFILIB_H */
174