blob: cfb3168e779190fbbbc0e66bf53397e7011385eb [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clarkad644e72017-09-13 18:05:37 -04002/*
3 * EFI utils
4 *
5 * Copyright (c) 2017 Rob Clark
Rob Clarkad644e72017-09-13 18:05:37 -04006 */
7
8#include <malloc.h>
9#include <charset.h>
10#include <efi_loader.h>
Heinrich Schuchardt6e37fa22019-01-18 12:31:54 +010011#include <hexdump.h>
Rob Clarkad644e72017-09-13 18:05:37 -040012
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
Heinrich Schuchardt506dc522018-09-23 04:08:09 +020048#define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
Rob Clarkad644e72017-09-13 18:05:37 -040049
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +010050/**
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 */
Heinrich Schuchardtdcdb64f2018-08-31 21:31:31 +020063static efi_status_t efi_to_native(char **native, const u16 *variable_name,
Heinrich Schuchardt0bda81b2018-12-30 20:53:51 +010064 const efi_guid_t *vendor)
Rob Clarkad644e72017-09-13 18:05:37 -040065{
66 size_t len;
Heinrich Schuchardtdcdb64f2018-08-31 21:31:31 +020067 char *pos;
Rob Clarkad644e72017-09-13 18:05:37 -040068
Heinrich Schuchardtdcdb64f2018-08-31 21:31:31 +020069 len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
70 *native = malloc(len);
71 if (!*native)
72 return EFI_OUT_OF_RESOURCES;
Rob Clarkad644e72017-09-13 18:05:37 -040073
Heinrich Schuchardtdcdb64f2018-08-31 21:31:31 +020074 pos = *native;
75 pos += sprintf(pos, "efi_%pUl_", vendor);
76 utf16_utf8_strcpy(&pos, variable_name);
Rob Clarkad644e72017-09-13 18:05:37 -040077
78 return EFI_SUCCESS;
79}
80
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +010081/**
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 */
Rob Clarkad644e72017-09-13 18:05:37 -040090static 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
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +010098/**
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 */
Rob Clarkad644e72017-09-13 18:05:37 -0400108static 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
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +0100145/**
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 */
Heinrich Schuchardt0bda81b2018-12-30 20:53:51 +0100160efi_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)
Rob Clarkad644e72017-09-13 18:05:37 -0400163{
Heinrich Schuchardtdcdb64f2018-08-31 21:31:31 +0200164 char *native_name;
Rob Clarkad644e72017-09-13 18:05:37 -0400165 efi_status_t ret;
166 unsigned long in_size;
167 const char *val, *s;
168 u32 attr;
169
Rob Clark778e6af2017-09-13 18:05:41 -0400170 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
Rob Clarkad644e72017-09-13 18:05:37 -0400171 data_size, data);
172
173 if (!variable_name || !vendor || !data_size)
174 return EFI_EXIT(EFI_INVALID_PARAMETER);
175
Heinrich Schuchardtdcdb64f2018-08-31 21:31:31 +0200176 ret = efi_to_native(&native_name, variable_name, vendor);
Rob Clarkad644e72017-09-13 18:05:37 -0400177 if (ret)
178 return EFI_EXIT(ret);
179
180 debug("%s: get '%s'\n", __func__, native_name);
181
182 val = env_get(native_name);
Heinrich Schuchardtdcdb64f2018-08-31 21:31:31 +0200183 free(native_name);
Rob Clarkad644e72017-09-13 18:05:37 -0400184 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)"))) {
Heinrich Schuchardt6e37fa22019-01-18 12:31:54 +0100192 size_t len = strlen(s);
Rob Clarkad644e72017-09-13 18:05:37 -0400193
Ivan Gorinovd73c8bc2018-05-11 13:18:25 -0700194 /* number of hexadecimal digits must be even */
195 if (len & 1)
196 return EFI_EXIT(EFI_DEVICE_ERROR);
197
Rob Clarkad644e72017-09-13 18:05:37 -0400198 /* two characters per byte: */
Ivan Gorinovd73c8bc2018-05-11 13:18:25 -0700199 len /= 2;
Rob Clarkad644e72017-09-13 18:05:37 -0400200 *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
Heinrich Schuchardt6e37fa22019-01-18 12:31:54 +0100208 if (hex2bin(data, s, len))
Rob Clarkad644e72017-09-13 18:05:37 -0400209 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
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +0100238/**
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 */
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200253efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
254 u16 *variable_name,
Heinrich Schuchardt0bda81b2018-12-30 20:53:51 +0100255 const efi_guid_t *vendor)
Rob Clarkad644e72017-09-13 18:05:37 -0400256{
Rob Clark778e6af2017-09-13 18:05:41 -0400257 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
Rob Clarkad644e72017-09-13 18:05:37 -0400258
259 return EFI_EXIT(EFI_DEVICE_ERROR);
260}
261
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +0100262/**
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 */
Heinrich Schuchardt0bda81b2018-12-30 20:53:51 +0100277efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
278 const efi_guid_t *vendor, u32 attributes,
Heinrich Schuchardt452257a2018-12-30 21:03:15 +0100279 efi_uintn_t data_size, const void *data)
Rob Clarkad644e72017-09-13 18:05:37 -0400280{
Heinrich Schuchardtdcdb64f2018-08-31 21:31:31 +0200281 char *native_name = NULL, *val = NULL, *s;
Rob Clarkad644e72017-09-13 18:05:37 -0400282 efi_status_t ret = EFI_SUCCESS;
Rob Clarkad644e72017-09-13 18:05:37 -0400283 u32 attr;
284
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200285 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
Rob Clarkad644e72017-09-13 18:05:37 -0400286 data_size, data);
287
Heinrich Schuchardtdcdb64f2018-08-31 21:31:31 +0200288 if (!variable_name || !vendor) {
289 ret = EFI_INVALID_PARAMETER;
290 goto out;
291 }
Rob Clarkad644e72017-09-13 18:05:37 -0400292
Heinrich Schuchardtdcdb64f2018-08-31 21:31:31 +0200293 ret = efi_to_native(&native_name, variable_name, vendor);
Rob Clarkad644e72017-09-13 18:05:37 -0400294 if (ret)
Heinrich Schuchardtdcdb64f2018-08-31 21:31:31 +0200295 goto out;
Rob Clarkad644e72017-09-13 18:05:37 -0400296
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);
Heinrich Schuchardtdcdb64f2018-08-31 21:31:31 +0200302 ret = EFI_SUCCESS;
303 goto out;
Rob Clarkad644e72017-09-13 18:05:37 -0400304 }
305
306 val = env_get(native_name);
307 if (val) {
308 parse_attr(val, &attr);
309
Heinrich Schuchardtdcdb64f2018-08-31 21:31:31 +0200310 if (attr & READ_ONLY) {
311 /* We should not free val */
312 val = NULL;
313 ret = EFI_WRITE_PROTECTED;
314 goto out;
315 }
Rob Clarkad644e72017-09-13 18:05:37 -0400316 }
317
318 val = malloc(2 * data_size + strlen("{ro,run,boot}(blob)") + 1);
Heinrich Schuchardtdadc2bd2018-10-02 05:30:05 +0200319 if (!val) {
320 ret = EFI_OUT_OF_RESOURCES;
321 goto out;
322 }
Rob Clarkad644e72017-09-13 18:05:37 -0400323
324 s = val;
325
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +0100326 /*
327 * store attributes
328 * TODO: several attributes are not supported
329 */
Rob Clarkad644e72017-09-13 18:05:37 -0400330 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)");
Heinrich Schuchardt8377ee32019-01-18 18:54:26 +0100348 s = bin2hex(s, data, data_size);
Rob Clarkad644e72017-09-13 18:05:37 -0400349 *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
Heinrich Schuchardtdcdb64f2018-08-31 21:31:31 +0200356out:
357 free(native_name);
Rob Clarkad644e72017-09-13 18:05:37 -0400358 free(val);
359
360 return EFI_EXIT(ret);
361}