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