xref: /dflybsd-src/lib/libefivar/efivar.c (revision 4661c1690a24c9a90726afa4b714671b5ea02dc5)
1bb7548fdSMatthew Dillon /*-
2bb7548fdSMatthew Dillon  * Copyright (c) 2016 Netflix, Inc.
3bb7548fdSMatthew Dillon  *
4bb7548fdSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
5bb7548fdSMatthew Dillon  * modification, are permitted provided that the following conditions
6bb7548fdSMatthew Dillon  * are met:
7bb7548fdSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
8*4661c169SSascha Wildner  *    notice, this list of conditions and the following disclaimer.
9bb7548fdSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
10bb7548fdSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
11bb7548fdSMatthew Dillon  *    documentation and/or other materials provided with the distribution.
12bb7548fdSMatthew Dillon  *
13*4661c169SSascha Wildner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14*4661c169SSascha Wildner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*4661c169SSascha Wildner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*4661c169SSascha Wildner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17*4661c169SSascha Wildner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*4661c169SSascha Wildner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*4661c169SSascha Wildner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20*4661c169SSascha Wildner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21*4661c169SSascha Wildner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22*4661c169SSascha Wildner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23*4661c169SSascha Wildner  * SUCH DAMAGE.
24*4661c169SSascha Wildner  *
25*4661c169SSascha Wildner  * $FreeBSD: head/lib/libefivar/efivar.c 343755 2019-02-04 21:28:25Z imp $
26bb7548fdSMatthew Dillon  */
27bb7548fdSMatthew Dillon 
28bb7548fdSMatthew Dillon #include <efivar.h>
29bb7548fdSMatthew Dillon #include <sys/efiio.h>
30bb7548fdSMatthew Dillon #include <sys/param.h>
31bb7548fdSMatthew Dillon #include <errno.h>
32bb7548fdSMatthew Dillon #include <fcntl.h>
33bb7548fdSMatthew Dillon #include <stdio.h>
34bb7548fdSMatthew Dillon #include <stdlib.h>
35bb7548fdSMatthew Dillon #include <string.h>
36bb7548fdSMatthew Dillon 
37bb7548fdSMatthew Dillon #include "libefivar_int.h"
38bb7548fdSMatthew Dillon 
39bb7548fdSMatthew Dillon static int efi_fd = -2;
40bb7548fdSMatthew Dillon 
41bb7548fdSMatthew Dillon #define Z { 0, 0, 0, 0, 0, { 0 } }
42bb7548fdSMatthew Dillon 
43bb7548fdSMatthew Dillon const efi_guid_t efi_guid_empty = Z;
44bb7548fdSMatthew Dillon 
45*4661c169SSascha Wildner static struct uuid_table guid_tbl [] =
46bb7548fdSMatthew Dillon {
47bb7548fdSMatthew Dillon 	{ "00000000-0000-0000-0000-000000000000", "zero", Z },
48bb7548fdSMatthew Dillon 	{ "093e0fae-a6c4-4f50-9f1b-d41e2b89c19a", "sha512", Z },
49bb7548fdSMatthew Dillon 	{ "0abba7dc-e516-4167-bbf5-4d9d1c739416", "redhat", Z },
50bb7548fdSMatthew Dillon 	{ "0b6e5233-a65c-44c9-9407-d9ab83bfc8bd", "sha224", Z },
51bb7548fdSMatthew Dillon 	{ "126a762d-5758-4fca-8531-201a7f57f850", "lenovo_boot_menu", Z },
52bb7548fdSMatthew Dillon 	{ "3bd2a492-96c0-4079-b420-fcf98ef103ed", "x509_sha256", Z },
53bb7548fdSMatthew Dillon 	{ "3c5766e8-269c-4e34-aa14-ed776e85b3b6", "rsa2048", Z },
54bb7548fdSMatthew Dillon 	{ "3CC24E96-22C7-41D8-8863-8E39DCDCC2CF", "lenovo", Z },
55bb7548fdSMatthew Dillon 	{ "3f7e615b-0d45-4f80-88dc-26b234958560", "lenovo_diag", Z },
56bb7548fdSMatthew Dillon 	{ "446dbf63-2502-4cda-bcfa-2465d2b0fe9d", "x509_sha512", Z },
57bb7548fdSMatthew Dillon 	{ "4aafd29d-68df-49ee-8aa9-347d375665a7", "pkcs7_cert", Z },
58bb7548fdSMatthew Dillon 	{ "605dab50-e046-4300-abb6-3dd810dd8b23", "shim", Z },
59bb7548fdSMatthew Dillon 	{ "665d3f60-ad3e-4cad-8e26-db46eee9f1b5", "lenovo_rescue", Z },
60bb7548fdSMatthew Dillon 	{ "67f8444f-8743-48f1-a328-1eaab8736080", "rsa2048_sha1", Z },
61bb7548fdSMatthew Dillon 	{ "7076876e-80c2-4ee6-aad2-28b349a6865b", "x509_sha384", Z },
62bb7548fdSMatthew Dillon 	{ "721c8b66-426c-4e86-8e99-3457c46ab0b9", "lenovo_setup", Z },
63bb7548fdSMatthew Dillon 	{ "77fa9abd-0359-4d32-bd60-28f4e78f784b", "microsoft", Z },
64bb7548fdSMatthew Dillon 	{ "7FACC7B6-127F-4E9C-9C5D-080F98994345", "lenovo_2", Z },
65bb7548fdSMatthew Dillon 	{ "826ca512-cf10-4ac9-b187-be01496631bd", "sha1", Z },
66bb7548fdSMatthew Dillon 	{ "82988420-7467-4490-9059-feb448dd1963", "lenovo_me_config", Z },
67bb7548fdSMatthew Dillon 	{ "8be4df61-93ca-11d2-aa0d-00e098032b8c", "global", Z },
68bb7548fdSMatthew Dillon 	{ "a5c059a1-94e4-4aa7-87b5-ab155c2bf072", "x509_cert", Z },
69bb7548fdSMatthew Dillon 	{ "a7717414-c616-4977-9420-844712a735bf", "rsa2048_sha256_cert", Z },
70bb7548fdSMatthew Dillon 	{ "a7d8d9a6-6ab0-4aeb-ad9d-163e59a7a380", "lenovo_diag_splash", Z },
71bb7548fdSMatthew Dillon 	{ "ade9e48f-9cb8-98e6-31af-b4e6009e2fe3", "redhat_2", Z },
72bb7548fdSMatthew Dillon 	{ "bc7838d2-0f82-4d60-8316-c068ee79d25b", "lenovo_msg", Z },
73bb7548fdSMatthew Dillon 	{ "c1c41626-504c-4092-aca9-41f936934328", "sha256", Z },
74bb7548fdSMatthew Dillon 	{ "c57ad6b7-0515-40a8-9d21-551652854e37", "shell", Z },
75bb7548fdSMatthew Dillon 	{ "d719b2cb-3d3a-4596-a3bc-dad00e67656f", "security", Z },
76bb7548fdSMatthew Dillon 	{ "e2b36190-879b-4a3d-ad8d-f2e7bba32784", "rsa2048_sha256", Z },
77bb7548fdSMatthew Dillon 	{ "ff3e5307-9fd0-48c9-85f1-8ad56c701e01", "sha384", Z },
78bb7548fdSMatthew Dillon 	{ "f46ee6f4-4785-43a3-923d-7f786c3c8479", "lenovo_startup_interrupt", Z },
79bb7548fdSMatthew Dillon 	{ "ffffffff-ffff-ffff-ffff-ffffffffffff", "zzignore-this-guid", Z },
80bb7548fdSMatthew Dillon };
81bb7548fdSMatthew Dillon #undef Z
82bb7548fdSMatthew Dillon 
83bb7548fdSMatthew Dillon static void
efi_guid_tbl_compile(void)84bb7548fdSMatthew Dillon efi_guid_tbl_compile(void)
85bb7548fdSMatthew Dillon {
86bb7548fdSMatthew Dillon 	size_t i;
87bb7548fdSMatthew Dillon 	uint32_t status;
88*4661c169SSascha Wildner 	static int done = 0;
89bb7548fdSMatthew Dillon 
90*4661c169SSascha Wildner 	if (done)
91*4661c169SSascha Wildner 		return;
92bb7548fdSMatthew Dillon 	for (i = 0; i < nitems(guid_tbl); i++) {
93bb7548fdSMatthew Dillon 		uuid_from_string(guid_tbl[i].uuid_str, &guid_tbl[i].guid,
94bb7548fdSMatthew Dillon 		    &status);
95bb7548fdSMatthew Dillon 		/* all f's is a bad version, so ignore that error */
96bb7548fdSMatthew Dillon 		if (status != uuid_s_ok && status != uuid_s_bad_version)
97bb7548fdSMatthew Dillon 			fprintf(stderr, "Can't convert %s to a uuid for %s: %d\n",
98bb7548fdSMatthew Dillon 			    guid_tbl[i].uuid_str, guid_tbl[i].name, (int)status);
99bb7548fdSMatthew Dillon 	}
100*4661c169SSascha Wildner 	done = 1;
101*4661c169SSascha Wildner }
102*4661c169SSascha Wildner 
103*4661c169SSascha Wildner int
efi_known_guid(struct uuid_table ** tbl)104*4661c169SSascha Wildner efi_known_guid(struct uuid_table **tbl)
105*4661c169SSascha Wildner {
106*4661c169SSascha Wildner 
107*4661c169SSascha Wildner 	*tbl = guid_tbl;
108*4661c169SSascha Wildner 	return (nitems(guid_tbl));
109bb7548fdSMatthew Dillon }
110bb7548fdSMatthew Dillon 
111bb7548fdSMatthew Dillon static int
efi_open_dev(void)112bb7548fdSMatthew Dillon efi_open_dev(void)
113bb7548fdSMatthew Dillon {
114bb7548fdSMatthew Dillon 
115bb7548fdSMatthew Dillon 	if (efi_fd == -2)
116bb7548fdSMatthew Dillon 		efi_fd = open("/dev/efi", O_RDWR);
117bb7548fdSMatthew Dillon 	if (efi_fd < 0)
118bb7548fdSMatthew Dillon 		efi_fd = -1;
119bb7548fdSMatthew Dillon 	else
120bb7548fdSMatthew Dillon 		efi_guid_tbl_compile();
121bb7548fdSMatthew Dillon 	return (efi_fd);
122bb7548fdSMatthew Dillon }
123bb7548fdSMatthew Dillon 
124bb7548fdSMatthew Dillon static void
efi_var_reset(struct efi_var_ioc * var)125bb7548fdSMatthew Dillon efi_var_reset(struct efi_var_ioc *var)
126bb7548fdSMatthew Dillon {
127bb7548fdSMatthew Dillon 	var->name = NULL;
128bb7548fdSMatthew Dillon 	var->namesize = 0;
129bb7548fdSMatthew Dillon 	memset(&var->vendor, 0, sizeof(var->vendor));
130bb7548fdSMatthew Dillon 	var->attrib = 0;
131bb7548fdSMatthew Dillon 	var->data = NULL;
132bb7548fdSMatthew Dillon 	var->datasize = 0;
133bb7548fdSMatthew Dillon }
134bb7548fdSMatthew Dillon 
135bb7548fdSMatthew Dillon static int
rv_to_linux_rv(int rv)136bb7548fdSMatthew Dillon rv_to_linux_rv(int rv)
137bb7548fdSMatthew Dillon {
138bb7548fdSMatthew Dillon 	if (rv == 0)
139bb7548fdSMatthew Dillon 		rv = 1;
140*4661c169SSascha Wildner 	else
141bb7548fdSMatthew Dillon 		rv = -errno;
142bb7548fdSMatthew Dillon 	return (rv);
143bb7548fdSMatthew Dillon }
144bb7548fdSMatthew Dillon 
145bb7548fdSMatthew Dillon int
efi_append_variable(efi_guid_t guid,const char * name,uint8_t * data,size_t data_size,uint32_t attributes)146bb7548fdSMatthew Dillon efi_append_variable(efi_guid_t guid, const char *name,
147bb7548fdSMatthew Dillon     uint8_t *data, size_t data_size, uint32_t attributes)
148bb7548fdSMatthew Dillon {
149bb7548fdSMatthew Dillon 
150bb7548fdSMatthew Dillon 	return efi_set_variable(guid, name, data, data_size,
151*4661c169SSascha Wildner 	    attributes | EFI_VARIABLE_APPEND_WRITE);
152bb7548fdSMatthew Dillon }
153bb7548fdSMatthew Dillon 
154bb7548fdSMatthew Dillon int
efi_del_variable(efi_guid_t guid,const char * name)155bb7548fdSMatthew Dillon efi_del_variable(efi_guid_t guid, const char *name)
156bb7548fdSMatthew Dillon {
157bb7548fdSMatthew Dillon 
158bb7548fdSMatthew Dillon 	/* data_size of 0 deletes the variable */
159*4661c169SSascha Wildner 	return efi_set_variable(guid, name, NULL, 0, 0);
160bb7548fdSMatthew Dillon }
161bb7548fdSMatthew Dillon 
162bb7548fdSMatthew Dillon int
efi_get_variable(efi_guid_t guid,const char * name,uint8_t ** data,size_t * data_size,uint32_t * attributes)163bb7548fdSMatthew Dillon efi_get_variable(efi_guid_t guid, const char *name,
164bb7548fdSMatthew Dillon     uint8_t **data, size_t *data_size, uint32_t *attributes)
165bb7548fdSMatthew Dillon {
166bb7548fdSMatthew Dillon 	struct efi_var_ioc var;
167bb7548fdSMatthew Dillon 	int rv;
168bb7548fdSMatthew Dillon 	static uint8_t buf[1024*32];
169bb7548fdSMatthew Dillon 
170bb7548fdSMatthew Dillon 	if (efi_open_dev() == -1)
171bb7548fdSMatthew Dillon 		return -1;
172bb7548fdSMatthew Dillon 
173bb7548fdSMatthew Dillon 	efi_var_reset(&var);
174*4661c169SSascha Wildner 	rv = utf8_to_ucs2(name, &var.name, &var.namesize);
175bb7548fdSMatthew Dillon 	if (rv != 0)
176bb7548fdSMatthew Dillon 		goto errout;
177bb7548fdSMatthew Dillon 	var.vendor = guid;
178bb7548fdSMatthew Dillon 	var.data = buf;
179bb7548fdSMatthew Dillon 	var.datasize = sizeof(buf);
180bb7548fdSMatthew Dillon 	rv = ioctl(efi_fd, EFIIOC_VAR_GET, &var);
181bb7548fdSMatthew Dillon 	if (data_size != NULL)
182bb7548fdSMatthew Dillon 		*data_size = var.datasize;
183bb7548fdSMatthew Dillon 	if (data != NULL)
184bb7548fdSMatthew Dillon 		*data = buf;
185bb7548fdSMatthew Dillon 	if (attributes != NULL)
186bb7548fdSMatthew Dillon 		*attributes = var.attrib;
187bb7548fdSMatthew Dillon errout:
188bb7548fdSMatthew Dillon 	free(var.name);
189bb7548fdSMatthew Dillon 
190bb7548fdSMatthew Dillon 	return rv_to_linux_rv(rv);
191bb7548fdSMatthew Dillon }
192bb7548fdSMatthew Dillon 
193bb7548fdSMatthew Dillon int
efi_get_variable_attributes(efi_guid_t guid,const char * name,uint32_t * attributes)194bb7548fdSMatthew Dillon efi_get_variable_attributes(efi_guid_t guid, const char *name,
195bb7548fdSMatthew Dillon     uint32_t *attributes)
196bb7548fdSMatthew Dillon {
197bb7548fdSMatthew Dillon 	/* Make sure this construct works -- I think it will fail */
198bb7548fdSMatthew Dillon 
199bb7548fdSMatthew Dillon 	return efi_get_variable(guid, name, NULL, NULL, attributes);
200bb7548fdSMatthew Dillon }
201bb7548fdSMatthew Dillon 
202bb7548fdSMatthew Dillon int
efi_get_variable_size(efi_guid_t guid,const char * name,size_t * size)203bb7548fdSMatthew Dillon efi_get_variable_size(efi_guid_t guid, const char *name,
204bb7548fdSMatthew Dillon     size_t *size)
205bb7548fdSMatthew Dillon {
206bb7548fdSMatthew Dillon 
207bb7548fdSMatthew Dillon 	/* XXX check to make sure this matches the linux value */
208bb7548fdSMatthew Dillon 
209bb7548fdSMatthew Dillon 	*size = 0;
210bb7548fdSMatthew Dillon 	return efi_get_variable(guid, name, NULL, size, NULL);
211bb7548fdSMatthew Dillon }
212bb7548fdSMatthew Dillon 
213bb7548fdSMatthew Dillon int
efi_get_next_variable_name(efi_guid_t ** guid,char ** name)214bb7548fdSMatthew Dillon efi_get_next_variable_name(efi_guid_t **guid, char **name)
215bb7548fdSMatthew Dillon {
216bb7548fdSMatthew Dillon 	struct efi_var_ioc var;
217bb7548fdSMatthew Dillon 	int rv;
218bb7548fdSMatthew Dillon 	static efi_char *buf;
219bb7548fdSMatthew Dillon 	static size_t buflen = 256 * sizeof(efi_char);
220bb7548fdSMatthew Dillon 	static efi_guid_t retguid;
221bb7548fdSMatthew Dillon 	size_t size;
222bb7548fdSMatthew Dillon 
223bb7548fdSMatthew Dillon 	if (efi_open_dev() == -1)
224bb7548fdSMatthew Dillon 		return -1;
225bb7548fdSMatthew Dillon 
226*4661c169SSascha Wildner 	/*
227*4661c169SSascha Wildner 	 * Always allocate enough for an extra NUL on the end, but don't tell
228*4661c169SSascha Wildner 	 * the IOCTL about it so we can NUL terminate the name before converting
229*4661c169SSascha Wildner 	 * it to UTF8.
230*4661c169SSascha Wildner 	 */
231bb7548fdSMatthew Dillon 	if (buf == NULL)
232*4661c169SSascha Wildner 		buf = malloc(buflen + sizeof(efi_char));
233bb7548fdSMatthew Dillon 
234bb7548fdSMatthew Dillon again:
235bb7548fdSMatthew Dillon 	efi_var_reset(&var);
236bb7548fdSMatthew Dillon 	var.name = buf;
237bb7548fdSMatthew Dillon 	var.namesize = buflen;
238bb7548fdSMatthew Dillon 	if (*name == NULL) {
239bb7548fdSMatthew Dillon 		*buf = 0;
240bb7548fdSMatthew Dillon 		/* GUID zeroed in var_reset */
241bb7548fdSMatthew Dillon 	} else {
242*4661c169SSascha Wildner 		rv = utf8_to_ucs2(*name, &var.name, &size);
243bb7548fdSMatthew Dillon 		if (rv != 0)
244bb7548fdSMatthew Dillon 			goto errout;
245bb7548fdSMatthew Dillon 		var.vendor = **guid;
246bb7548fdSMatthew Dillon 	}
247bb7548fdSMatthew Dillon 	rv = ioctl(efi_fd, EFIIOC_VAR_NEXT, &var);
248bb7548fdSMatthew Dillon 	if (rv == 0 && var.name == NULL) {
249bb7548fdSMatthew Dillon 		/*
250*4661c169SSascha Wildner 		 * Variable name not long enough, so allocate more space for the
251*4661c169SSascha Wildner 		 * name and try again. As above, mind the NUL we add.
252bb7548fdSMatthew Dillon 		 */
253*4661c169SSascha Wildner 		void *new = realloc(buf, var.namesize + sizeof(efi_char));
254bb7548fdSMatthew Dillon 		if (new == NULL) {
255bb7548fdSMatthew Dillon 			rv = -1;
256bb7548fdSMatthew Dillon 			errno = ENOMEM;
257bb7548fdSMatthew Dillon 			goto done;
258bb7548fdSMatthew Dillon 		}
259*4661c169SSascha Wildner 		buflen = var.namesize;
260bb7548fdSMatthew Dillon 		buf = new;
261bb7548fdSMatthew Dillon 		goto again;
262bb7548fdSMatthew Dillon 	}
263bb7548fdSMatthew Dillon 
264bb7548fdSMatthew Dillon 	if (rv == 0) {
265*4661c169SSascha Wildner 		free(*name);			/* Free last name, to avoid leaking */
266*4661c169SSascha Wildner 		*name = NULL;			/* Force ucs2_to_utf8 to malloc new space */
267bb7548fdSMatthew Dillon 		var.name[var.namesize / sizeof(efi_char)] = 0;	/* EFI doesn't NUL terminate */
268*4661c169SSascha Wildner 		rv = ucs2_to_utf8(var.name, name);
269bb7548fdSMatthew Dillon 		if (rv != 0)
270bb7548fdSMatthew Dillon 			goto errout;
271bb7548fdSMatthew Dillon 		retguid = var.vendor;
272bb7548fdSMatthew Dillon 		*guid = &retguid;
273bb7548fdSMatthew Dillon 	}
274bb7548fdSMatthew Dillon errout:
275bb7548fdSMatthew Dillon 
276bb7548fdSMatthew Dillon 	/* XXX The linux interface expects name to be a static buffer -- fix or leak memory? */
277*4661c169SSascha Wildner 	/* XXX for the moment, we free just before we'd leak, but still leak last one */
278bb7548fdSMatthew Dillon done:
279*4661c169SSascha Wildner 	if (rv != 0 && errno == ENOENT) {
280*4661c169SSascha Wildner 		errno = 0;
281*4661c169SSascha Wildner 		free(*name);			/* Free last name, to avoid leaking */
282*4661c169SSascha Wildner 		return 0;
283*4661c169SSascha Wildner 	}
284*4661c169SSascha Wildner 
285bb7548fdSMatthew Dillon 	return (rv_to_linux_rv(rv));
286bb7548fdSMatthew Dillon }
287bb7548fdSMatthew Dillon 
288bb7548fdSMatthew Dillon int
efi_guid_cmp(const efi_guid_t * guid1,const efi_guid_t * guid2)289bb7548fdSMatthew Dillon efi_guid_cmp(const efi_guid_t *guid1, const efi_guid_t *guid2)
290bb7548fdSMatthew Dillon {
291bb7548fdSMatthew Dillon 	uint32_t status;
292bb7548fdSMatthew Dillon 
293bb7548fdSMatthew Dillon 	return uuid_compare(guid1, guid2, &status);
294bb7548fdSMatthew Dillon }
295bb7548fdSMatthew Dillon 
296bb7548fdSMatthew Dillon int
efi_guid_is_zero(const efi_guid_t * guid)297bb7548fdSMatthew Dillon efi_guid_is_zero(const efi_guid_t *guid)
298bb7548fdSMatthew Dillon {
299bb7548fdSMatthew Dillon 	uint32_t status;
300bb7548fdSMatthew Dillon 
301bb7548fdSMatthew Dillon 	return uuid_is_nil(guid, &status);
302bb7548fdSMatthew Dillon }
303bb7548fdSMatthew Dillon 
304bb7548fdSMatthew Dillon int
efi_guid_to_name(efi_guid_t * guid,char ** name)305bb7548fdSMatthew Dillon efi_guid_to_name(efi_guid_t *guid, char **name)
306bb7548fdSMatthew Dillon {
307bb7548fdSMatthew Dillon 	size_t i;
308bb7548fdSMatthew Dillon 	uint32_t status;
309bb7548fdSMatthew Dillon 
310*4661c169SSascha Wildner 	efi_guid_tbl_compile();
311bb7548fdSMatthew Dillon 	for (i = 0; i < nitems(guid_tbl); i++) {
312bb7548fdSMatthew Dillon 		if (uuid_equal(guid, &guid_tbl[i].guid, &status)) {
313bb7548fdSMatthew Dillon 			*name = strdup(guid_tbl[i].name);
314bb7548fdSMatthew Dillon 			return (0);
315bb7548fdSMatthew Dillon 		}
316bb7548fdSMatthew Dillon 	}
317bb7548fdSMatthew Dillon 	return (efi_guid_to_str(guid, name));
318bb7548fdSMatthew Dillon }
319bb7548fdSMatthew Dillon 
320bb7548fdSMatthew Dillon int
efi_guid_to_symbol(efi_guid_t * guid __unused,char ** symbol __unused)321bb7548fdSMatthew Dillon efi_guid_to_symbol(efi_guid_t *guid __unused, char **symbol __unused)
322bb7548fdSMatthew Dillon {
323bb7548fdSMatthew Dillon 
324bb7548fdSMatthew Dillon 	/*
325bb7548fdSMatthew Dillon 	 * Unsure what this is used for, efibootmgr doesn't use it.
326bb7548fdSMatthew Dillon 	 * Leave unimplemented for now.
327bb7548fdSMatthew Dillon 	 */
328bb7548fdSMatthew Dillon 	return -1;
329bb7548fdSMatthew Dillon }
330bb7548fdSMatthew Dillon 
331bb7548fdSMatthew Dillon int
efi_guid_to_str(const efi_guid_t * guid,char ** sp)332bb7548fdSMatthew Dillon efi_guid_to_str(const efi_guid_t *guid, char **sp)
333bb7548fdSMatthew Dillon {
334bb7548fdSMatthew Dillon 	uint32_t status;
335bb7548fdSMatthew Dillon 
336bb7548fdSMatthew Dillon 	/* knows efi_guid_t is a typedef of uuid_t */
337bb7548fdSMatthew Dillon 	uuid_to_string(guid, sp, &status);
338bb7548fdSMatthew Dillon 
339bb7548fdSMatthew Dillon 	return (status == uuid_s_ok ? 0 : -1);
340bb7548fdSMatthew Dillon }
341bb7548fdSMatthew Dillon 
342bb7548fdSMatthew Dillon int
efi_name_to_guid(const char * name,efi_guid_t * guid)343bb7548fdSMatthew Dillon efi_name_to_guid(const char *name, efi_guid_t *guid)
344bb7548fdSMatthew Dillon {
345bb7548fdSMatthew Dillon 	size_t i;
346bb7548fdSMatthew Dillon 
347*4661c169SSascha Wildner 	efi_guid_tbl_compile();
348bb7548fdSMatthew Dillon 	for (i = 0; i < nitems(guid_tbl); i++) {
349bb7548fdSMatthew Dillon 		if (strcmp(name, guid_tbl[i].name) == 0) {
350bb7548fdSMatthew Dillon 			*guid = guid_tbl[i].guid;
351bb7548fdSMatthew Dillon 			return (0);
352bb7548fdSMatthew Dillon 		}
353bb7548fdSMatthew Dillon 	}
354bb7548fdSMatthew Dillon 	return (efi_str_to_guid(name, guid));
355bb7548fdSMatthew Dillon }
356bb7548fdSMatthew Dillon 
357bb7548fdSMatthew Dillon int
efi_set_variable(efi_guid_t guid,const char * name,uint8_t * data,size_t data_size,uint32_t attributes)358bb7548fdSMatthew Dillon efi_set_variable(efi_guid_t guid, const char *name,
359*4661c169SSascha Wildner     uint8_t *data, size_t data_size, uint32_t attributes)
360bb7548fdSMatthew Dillon {
361bb7548fdSMatthew Dillon 	struct efi_var_ioc var;
362bb7548fdSMatthew Dillon 	int rv;
363bb7548fdSMatthew Dillon 
364bb7548fdSMatthew Dillon 	if (efi_open_dev() == -1)
365bb7548fdSMatthew Dillon 		return -1;
366bb7548fdSMatthew Dillon 
367bb7548fdSMatthew Dillon 	efi_var_reset(&var);
368*4661c169SSascha Wildner 	rv = utf8_to_ucs2(name, &var.name, &var.namesize);
369bb7548fdSMatthew Dillon 	if (rv != 0)
370bb7548fdSMatthew Dillon 		goto errout;
371bb7548fdSMatthew Dillon 	var.vendor = guid;
372bb7548fdSMatthew Dillon 	var.data = data;
373bb7548fdSMatthew Dillon 	var.datasize = data_size;
374bb7548fdSMatthew Dillon 	var.attrib = attributes;
375bb7548fdSMatthew Dillon 	rv = ioctl(efi_fd, EFIIOC_VAR_SET, &var);
376bb7548fdSMatthew Dillon errout:
377bb7548fdSMatthew Dillon 	free(var.name);
378bb7548fdSMatthew Dillon 
379bb7548fdSMatthew Dillon 	return rv;
380bb7548fdSMatthew Dillon }
381bb7548fdSMatthew Dillon 
382bb7548fdSMatthew Dillon int
efi_str_to_guid(const char * s,efi_guid_t * guid)383bb7548fdSMatthew Dillon efi_str_to_guid(const char *s, efi_guid_t *guid)
384bb7548fdSMatthew Dillon {
385bb7548fdSMatthew Dillon 	uint32_t status;
386bb7548fdSMatthew Dillon 
387bb7548fdSMatthew Dillon 	/* knows efi_guid_t is a typedef of uuid_t */
388bb7548fdSMatthew Dillon 	uuid_from_string(s, guid, &status);
389bb7548fdSMatthew Dillon 
390bb7548fdSMatthew Dillon 	return (status == uuid_s_ok ? 0 : -1);
391bb7548fdSMatthew Dillon }
392bb7548fdSMatthew Dillon 
393bb7548fdSMatthew Dillon int
efi_variables_supported(void)394bb7548fdSMatthew Dillon efi_variables_supported(void)
395bb7548fdSMatthew Dillon {
396bb7548fdSMatthew Dillon 
397bb7548fdSMatthew Dillon 	return efi_open_dev() != -1;
398bb7548fdSMatthew Dillon }
399