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