xref: /freebsd/stand/efi/include/efilib.h (revision 2008043f386721d58158e37e0d7e50df8095942d)
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 
35 extern EFI_HANDLE		IH;
36 extern EFI_SYSTEM_TABLE		*ST;
37 extern EFI_BOOT_SERVICES	*BS;
38 extern EFI_RUNTIME_SERVICES	*RS;
39 
40 extern struct devsw efipart_fddev;
41 extern struct devsw efipart_cddev;
42 extern struct devsw efipart_hddev;
43 extern struct devsw efihttp_dev;
44 extern struct devsw efinet_dev;
45 extern struct netif_driver efinetif;
46 
47 /* EFI block device data, included here to help efi_zfs_probe() */
48 typedef STAILQ_HEAD(pdinfo_list, pdinfo) pdinfo_list_t;
49 
50 typedef struct pdinfo
51 {
52 	STAILQ_ENTRY(pdinfo)	pd_link;	/* link in device list */
53 	pdinfo_list_t		pd_part;	/* list of partitions */
54 	EFI_HANDLE		pd_handle;
55 	EFI_HANDLE		pd_alias;
56 	EFI_DEVICE_PATH		*pd_devpath;
57 	EFI_BLOCK_IO		*pd_blkio;
58 	uint32_t		pd_unit;	/* unit number */
59 	uint32_t		pd_open;	/* reference counter */
60 	void			*pd_bcache;	/* buffer cache data */
61 	struct pdinfo		*pd_parent;	/* Linked items (eg partitions) */
62 	struct devsw		*pd_devsw;	/* Back pointer to devsw */
63 } pdinfo_t;
64 
65 pdinfo_list_t *efiblk_get_pdinfo_list(struct devsw *dev);
66 pdinfo_t *efiblk_get_pdinfo(struct devdesc *dev);
67 pdinfo_t *efiblk_get_pdinfo_by_handle(EFI_HANDLE h);
68 pdinfo_t *efiblk_get_pdinfo_by_device_path(EFI_DEVICE_PATH *path);
69 
70 /* libefi.c */
71 void *efi_get_table(EFI_GUID *tbl);
72 EFI_STATUS OpenProtocolByHandle(EFI_HANDLE, EFI_GUID *, void **);
73 
74 static inline EFI_STATUS
75 efi_exit_boot_services(UINTN key)
76 {
77 	EFI_STATUS status;
78 
79 	status = BS->ExitBootServices(IH, key);
80 	if (!EFI_ERROR(status))
81 		boot_services_active = false;
82 	return (status);
83 }
84 
85 int efi_getdev(void **vdev, const char *devspec, const char **path);
86 
87 int efi_register_handles(struct devsw *, EFI_HANDLE *, EFI_HANDLE *, int);
88 EFI_HANDLE efi_find_handle(struct devsw *, int);
89 int efi_handle_lookup(EFI_HANDLE, struct devsw **, int *,  uint64_t *);
90 int efi_handle_update_dev(EFI_HANDLE, struct devsw *, int, uint64_t);
91 
92 EFI_DEVICE_PATH *efi_lookup_image_devpath(EFI_HANDLE);
93 EFI_DEVICE_PATH *efi_lookup_devpath(EFI_HANDLE);
94 void efi_close_devpath(EFI_HANDLE);
95 EFI_HANDLE efi_devpath_handle(EFI_DEVICE_PATH *);
96 EFI_DEVICE_PATH *efi_devpath_last_node(EFI_DEVICE_PATH *);
97 EFI_DEVICE_PATH *efi_devpath_trim(EFI_DEVICE_PATH *);
98 EFI_DEVICE_PATH *efi_devpath_next_instance(EFI_DEVICE_PATH *);
99 bool efi_devpath_match(EFI_DEVICE_PATH *, EFI_DEVICE_PATH *);
100 bool efi_devpath_match_node(EFI_DEVICE_PATH *, EFI_DEVICE_PATH *);
101 bool efi_devpath_is_prefix(EFI_DEVICE_PATH *, EFI_DEVICE_PATH *);
102 CHAR16 *efi_devpath_name(EFI_DEVICE_PATH *);
103 void efi_free_devpath_name(CHAR16 *);
104 bool efi_devpath_same_disk(EFI_DEVICE_PATH *, EFI_DEVICE_PATH *);
105 EFI_DEVICE_PATH *efi_devpath_to_media_path(EFI_DEVICE_PATH *);
106 UINTN efi_devpath_length(EFI_DEVICE_PATH *);
107 EFI_DEVICE_PATH *efi_name_to_devpath(const char *path);
108 EFI_DEVICE_PATH *efi_name_to_devpath16(CHAR16 *path);
109 void efi_devpath_free(EFI_DEVICE_PATH *dp);
110 EFI_HANDLE efi_devpath_to_handle(EFI_DEVICE_PATH *path, EFI_HANDLE *handles, unsigned nhandles);
111 
112 int efi_status_to_errno(EFI_STATUS);
113 EFI_STATUS errno_to_efi_status(int errno);
114 
115 void efi_time_init(void);
116 void efi_time_fini(void);
117 
118 int parse_uefi_con_out(void);
119 EFI_STATUS efi_main(EFI_HANDLE Ximage, EFI_SYSTEM_TABLE* Xsystab);
120 
121 EFI_STATUS main(int argc, CHAR16 *argv[]);
122 void efi_exit(EFI_STATUS status) __dead2;
123 
124 /* EFI environment initialization. */
125 void efi_init_environment(void);
126 
127 /* EFI Memory type strings. */
128 const char *efi_memory_type(EFI_MEMORY_TYPE);
129 
130 /* CHAR16 utility functions. */
131 int wcscmp(CHAR16 *, CHAR16 *);
132 void cpy8to16(const char *, CHAR16 *, size_t);
133 void cpy16to8(const CHAR16 *, char *, size_t);
134 
135 /*
136  * Routines for interacting with EFI's env vars in a more unix-like
137  * way than the standard APIs. In addition, convenience routines for
138  * the loader setting / getting FreeBSD specific variables.
139  */
140 
141 EFI_STATUS efi_delenv(EFI_GUID *guid, const char *varname);
142 EFI_STATUS efi_freebsd_delenv(const char *varname);
143 EFI_STATUS efi_freebsd_getenv(const char *v, void *data, __size_t *len);
144 EFI_STATUS efi_getenv(EFI_GUID *g, const char *v, void *data, __size_t *len);
145 EFI_STATUS efi_global_getenv(const char *v, void *data, __size_t *len);
146 EFI_STATUS efi_setenv(EFI_GUID *guid, const char *varname, UINT32 attr, void *data, __size_t len);
147 EFI_STATUS efi_setenv_freebsd_wcs(const char *varname, CHAR16 *valstr);
148 
149 /* guids and names */
150 bool efi_guid_to_str(const EFI_GUID *, char **);
151 bool efi_str_to_guid(const char *, EFI_GUID *);
152 bool efi_name_to_guid(const char *, EFI_GUID *);
153 bool efi_guid_to_name(EFI_GUID *, char **);
154 
155 /* efipart.c */
156 int	efipart_inithandles(void);
157 
158 #endif	/* _LOADER_EFILIB_H */
159