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