1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI utils
4  *
5  *  Copyright (c) 2017 Rob Clark
6  */
7 
8 #include <malloc.h>
9 #include <charset.h>
10 #include <efi_loader.h>
11 #include <hexdump.h>
12 
13 #define READ_ONLY BIT(31)
14 
15 /*
16  * Mapping between EFI variables and u-boot variables:
17  *
18  *   efi_$guid_$varname = {attributes}(type)value
19  *
20  * For example:
21  *
22  *   efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
23  *      "{ro,boot,run}(blob)0000000000000000"
24  *   efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
25  *      "(blob)00010000"
26  *
27  * The attributes are a comma separated list of these possible
28  * attributes:
29  *
30  *   + ro   - read-only
31  *   + boot - boot-services access
32  *   + run  - runtime access
33  *
34  * NOTE: with current implementation, no variables are available after
35  * ExitBootServices, and all are persisted (if possible).
36  *
37  * If not specified, the attributes default to "{boot}".
38  *
39  * The required type is one of:
40  *
41  *   + utf8 - raw utf8 string
42  *   + blob - arbitrary length hex string
43  *
44  * Maybe a utf16 type would be useful to for a string value to be auto
45  * converted to utf16?
46  */
47 
48 #define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
49 
50 /**
51  * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
52  *		     variable name
53  *
54  * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
55  * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
56  * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
57  *
58  * @native:		pointer to pointer to U-Boot variable name
59  * @variable_name:	UEFI variable name
60  * @vendor:		vendor GUID
61  * Return:		status code
62  */
63 static efi_status_t efi_to_native(char **native, const u16 *variable_name,
64 				  const efi_guid_t *vendor)
65 {
66 	size_t len;
67 	char *pos;
68 
69 	len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
70 	*native = malloc(len);
71 	if (!*native)
72 		return EFI_OUT_OF_RESOURCES;
73 
74 	pos = *native;
75 	pos += sprintf(pos, "efi_%pUl_", vendor);
76 	utf16_utf8_strcpy(&pos, variable_name);
77 
78 	return EFI_SUCCESS;
79 }
80 
81 /**
82  * prefix() - skip over prefix
83  *
84  * Skip over a prefix string.
85  *
86  * @str:	string with prefix
87  * @prefix:	prefix string
88  * Return:	string without prefix, or NULL if prefix not found
89  */
90 static const char *prefix(const char *str, const char *prefix)
91 {
92 	size_t n = strlen(prefix);
93 	if (!strncmp(prefix, str, n))
94 		return str + n;
95 	return NULL;
96 }
97 
98 /**
99  * parse_attr() - decode attributes part of variable value
100  *
101  * Convert the string encoded attributes of a UEFI variable to a bit mask.
102  * TODO: Several attributes are not supported.
103  *
104  * @str:	value of U-Boot variable
105  * @attrp:	pointer to UEFI attributes
106  * Return:	pointer to remainder of U-Boot variable value
107  */
108 static const char *parse_attr(const char *str, u32 *attrp)
109 {
110 	u32 attr = 0;
111 	char sep = '{';
112 
113 	if (*str != '{') {
114 		*attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
115 		return str;
116 	}
117 
118 	while (*str == sep) {
119 		const char *s;
120 
121 		str++;
122 
123 		if ((s = prefix(str, "ro"))) {
124 			attr |= READ_ONLY;
125 		} else if ((s = prefix(str, "boot"))) {
126 			attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
127 		} else if ((s = prefix(str, "run"))) {
128 			attr |= EFI_VARIABLE_RUNTIME_ACCESS;
129 		} else {
130 			printf("invalid attribute: %s\n", str);
131 			break;
132 		}
133 
134 		str = s;
135 		sep = ',';
136 	}
137 
138 	str++;
139 
140 	*attrp = attr;
141 
142 	return str;
143 }
144 
145 /**
146  * efi_efi_get_variable() - retrieve value of a UEFI variable
147  *
148  * This function implements the GetVariable runtime service.
149  *
150  * See the Unified Extensible Firmware Interface (UEFI) specification for
151  * details.
152  *
153  * @variable_name:	name of the variable
154  * @vendor:		vendor GUID
155  * @attributes:		attributes of the variable
156  * @data_size:		size of the buffer to which the variable value is copied
157  * @data:		buffer to which the variable value is copied
158  * Return:		status code
159  */
160 efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
161 				     const efi_guid_t *vendor, u32 *attributes,
162 				     efi_uintn_t *data_size, void *data)
163 {
164 	char *native_name;
165 	efi_status_t ret;
166 	unsigned long in_size;
167 	const char *val, *s;
168 	u32 attr;
169 
170 	EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
171 		  data_size, data);
172 
173 	if (!variable_name || !vendor || !data_size)
174 		return EFI_EXIT(EFI_INVALID_PARAMETER);
175 
176 	ret = efi_to_native(&native_name, variable_name, vendor);
177 	if (ret)
178 		return EFI_EXIT(ret);
179 
180 	debug("%s: get '%s'\n", __func__, native_name);
181 
182 	val = env_get(native_name);
183 	free(native_name);
184 	if (!val)
185 		return EFI_EXIT(EFI_NOT_FOUND);
186 
187 	val = parse_attr(val, &attr);
188 
189 	in_size = *data_size;
190 
191 	if ((s = prefix(val, "(blob)"))) {
192 		size_t len = strlen(s);
193 
194 		/* number of hexadecimal digits must be even */
195 		if (len & 1)
196 			return EFI_EXIT(EFI_DEVICE_ERROR);
197 
198 		/* two characters per byte: */
199 		len /= 2;
200 		*data_size = len;
201 
202 		if (in_size < len)
203 			return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
204 
205 		if (!data)
206 			return EFI_EXIT(EFI_INVALID_PARAMETER);
207 
208 		if (hex2bin(data, s, len))
209 			return EFI_EXIT(EFI_DEVICE_ERROR);
210 
211 		debug("%s: got value: \"%s\"\n", __func__, s);
212 	} else if ((s = prefix(val, "(utf8)"))) {
213 		unsigned len = strlen(s) + 1;
214 
215 		*data_size = len;
216 
217 		if (in_size < len)
218 			return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
219 
220 		if (!data)
221 			return EFI_EXIT(EFI_INVALID_PARAMETER);
222 
223 		memcpy(data, s, len);
224 		((char *)data)[len] = '\0';
225 
226 		debug("%s: got value: \"%s\"\n", __func__, (char *)data);
227 	} else {
228 		debug("%s: invalid value: '%s'\n", __func__, val);
229 		return EFI_EXIT(EFI_DEVICE_ERROR);
230 	}
231 
232 	if (attributes)
233 		*attributes = attr & EFI_VARIABLE_MASK;
234 
235 	return EFI_EXIT(EFI_SUCCESS);
236 }
237 
238 /**
239  * efi_efi_get_next_variable() - get next UEFI variable
240  *
241  * This function implements the GetNextVariable runtime service.
242  *
243  * See the Unified Extensible Firmware Interface (UEFI) specification for
244  * details.
245  *
246  * @variable_name_size:	on entry size of the buffer for the variable name, on
247  *			exit the length of the name of the next variable
248  * @variable_name:	on entry name of the current variable, on exit the name
249  *			of the next variable
250  * @vendor:		vendor GUID
251  * Return:		status code
252  */
253 efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
254 					       u16 *variable_name,
255 					       const efi_guid_t *vendor)
256 {
257 	EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
258 
259 	return EFI_EXIT(EFI_DEVICE_ERROR);
260 }
261 
262 /**
263  * efi_efi_set_variable() - set value of a UEFI variable
264  *
265  * This function implements the SetVariable runtime service.
266  *
267  * See the Unified Extensible Firmware Interface (UEFI) specification for
268  * details.
269  *
270  * @variable_name:	name of the variable
271  * @vendor:		vendor GUID
272  * @attributes:		attributes of the variable
273  * @data_size:		size of the buffer with the variable value
274  * @data:		buffer with the variable value
275  * Return:		status code
276  */
277 efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
278 				     const efi_guid_t *vendor, u32 attributes,
279 				     efi_uintn_t data_size, const void *data)
280 {
281 	char *native_name = NULL, *val = NULL, *s;
282 	efi_status_t ret = EFI_SUCCESS;
283 	u32 attr;
284 
285 	EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
286 		  data_size, data);
287 
288 	if (!variable_name || !vendor) {
289 		ret = EFI_INVALID_PARAMETER;
290 		goto out;
291 	}
292 
293 	ret = efi_to_native(&native_name, variable_name, vendor);
294 	if (ret)
295 		goto out;
296 
297 #define ACCESS_ATTR (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)
298 
299 	if ((data_size == 0) || !(attributes & ACCESS_ATTR)) {
300 		/* delete the variable: */
301 		env_set(native_name, NULL);
302 		ret = EFI_SUCCESS;
303 		goto out;
304 	}
305 
306 	val = env_get(native_name);
307 	if (val) {
308 		parse_attr(val, &attr);
309 
310 		if (attr & READ_ONLY) {
311 			/* We should not free val */
312 			val = NULL;
313 			ret = EFI_WRITE_PROTECTED;
314 			goto out;
315 		}
316 	}
317 
318 	val = malloc(2 * data_size + strlen("{ro,run,boot}(blob)") + 1);
319 	if (!val) {
320 		ret = EFI_OUT_OF_RESOURCES;
321 		goto out;
322 	}
323 
324 	s = val;
325 
326 	/*
327 	 * store attributes
328 	 * TODO: several attributes are not supported
329 	 */
330 	attributes &= (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS);
331 	s += sprintf(s, "{");
332 	while (attributes) {
333 		u32 attr = 1 << (ffs(attributes) - 1);
334 
335 		if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS)
336 			s += sprintf(s, "boot");
337 		else if (attr == EFI_VARIABLE_RUNTIME_ACCESS)
338 			s += sprintf(s, "run");
339 
340 		attributes &= ~attr;
341 		if (attributes)
342 			s += sprintf(s, ",");
343 	}
344 	s += sprintf(s, "}");
345 
346 	/* store payload: */
347 	s += sprintf(s, "(blob)");
348 	s = bin2hex(s, data, data_size);
349 	*s = '\0';
350 
351 	debug("%s: setting: %s=%s\n", __func__, native_name, val);
352 
353 	if (env_set(native_name, val))
354 		ret = EFI_DEVICE_ERROR;
355 
356 out:
357 	free(native_name);
358 	free(val);
359 
360 	return EFI_EXIT(ret);
361 }
362