blob: fe2f26459136befd6190879b5b6416aa91610375 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clark15f3d742017-09-13 18:05:37 -04002/*
Heinrich Schuchardtc69addd2020-03-19 17:15:18 +00003 * UEFI runtime variable services
Rob Clark15f3d742017-09-13 18:05:37 -04004 *
Heinrich Schuchardtc69addd2020-03-19 17:15:18 +00005 * Copyright (c) 2017 Rob Clark
Rob Clark15f3d742017-09-13 18:05:37 -04006 */
7
Heinrich Schuchardt147d5d32019-10-26 23:53:48 +02008#include <common.h>
Rob Clark15f3d742017-09-13 18:05:37 -04009#include <efi_loader.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060010#include <env_internal.h>
Heinrich Schuchardt147d5d32019-10-26 23:53:48 +020011#include <hexdump.h>
12#include <malloc.h>
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +010013#include <search.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070014#include <u-boot/crc.h>
Rob Clark15f3d742017-09-13 18:05:37 -040015
16#define READ_ONLY BIT(31)
17
18/*
19 * Mapping between EFI variables and u-boot variables:
20 *
21 * efi_$guid_$varname = {attributes}(type)value
22 *
23 * For example:
24 *
25 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
26 * "{ro,boot,run}(blob)0000000000000000"
27 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
28 * "(blob)00010000"
29 *
30 * The attributes are a comma separated list of these possible
31 * attributes:
32 *
33 * + ro - read-only
34 * + boot - boot-services access
35 * + run - runtime access
36 *
37 * NOTE: with current implementation, no variables are available after
38 * ExitBootServices, and all are persisted (if possible).
39 *
40 * If not specified, the attributes default to "{boot}".
41 *
42 * The required type is one of:
43 *
44 * + utf8 - raw utf8 string
45 * + blob - arbitrary length hex string
46 *
47 * Maybe a utf16 type would be useful to for a string value to be auto
48 * converted to utf16?
49 */
50
Heinrich Schuchardt44389cb2018-09-23 04:08:09 +020051#define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
Rob Clark15f3d742017-09-13 18:05:37 -040052
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +010053/**
54 * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
55 * variable name
56 *
57 * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
58 * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
59 * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
60 *
61 * @native: pointer to pointer to U-Boot variable name
62 * @variable_name: UEFI variable name
63 * @vendor: vendor GUID
64 * Return: status code
65 */
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020066static efi_status_t efi_to_native(char **native, const u16 *variable_name,
Heinrich Schuchardte06ae192018-12-30 20:53:51 +010067 const efi_guid_t *vendor)
Rob Clark15f3d742017-09-13 18:05:37 -040068{
69 size_t len;
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020070 char *pos;
Rob Clark15f3d742017-09-13 18:05:37 -040071
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020072 len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
73 *native = malloc(len);
74 if (!*native)
75 return EFI_OUT_OF_RESOURCES;
Rob Clark15f3d742017-09-13 18:05:37 -040076
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020077 pos = *native;
78 pos += sprintf(pos, "efi_%pUl_", vendor);
79 utf16_utf8_strcpy(&pos, variable_name);
Rob Clark15f3d742017-09-13 18:05:37 -040080
81 return EFI_SUCCESS;
82}
83
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +010084/**
85 * prefix() - skip over prefix
86 *
87 * Skip over a prefix string.
88 *
89 * @str: string with prefix
90 * @prefix: prefix string
91 * Return: string without prefix, or NULL if prefix not found
92 */
Rob Clark15f3d742017-09-13 18:05:37 -040093static const char *prefix(const char *str, const char *prefix)
94{
95 size_t n = strlen(prefix);
96 if (!strncmp(prefix, str, n))
97 return str + n;
98 return NULL;
99}
100
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100101/**
102 * parse_attr() - decode attributes part of variable value
103 *
104 * Convert the string encoded attributes of a UEFI variable to a bit mask.
105 * TODO: Several attributes are not supported.
106 *
107 * @str: value of U-Boot variable
108 * @attrp: pointer to UEFI attributes
109 * Return: pointer to remainder of U-Boot variable value
110 */
Rob Clark15f3d742017-09-13 18:05:37 -0400111static const char *parse_attr(const char *str, u32 *attrp)
112{
113 u32 attr = 0;
114 char sep = '{';
115
116 if (*str != '{') {
117 *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
118 return str;
119 }
120
121 while (*str == sep) {
122 const char *s;
123
124 str++;
125
126 if ((s = prefix(str, "ro"))) {
127 attr |= READ_ONLY;
AKASHI Takahiro7f334332019-06-04 15:52:06 +0900128 } else if ((s = prefix(str, "nv"))) {
129 attr |= EFI_VARIABLE_NON_VOLATILE;
Rob Clark15f3d742017-09-13 18:05:37 -0400130 } else if ((s = prefix(str, "boot"))) {
131 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
132 } else if ((s = prefix(str, "run"))) {
133 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
134 } else {
135 printf("invalid attribute: %s\n", str);
136 break;
137 }
138
139 str = s;
140 sep = ',';
141 }
142
143 str++;
144
145 *attrp = attr;
146
147 return str;
148}
149
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100150/**
Heinrich Schuchardt0f042f92019-06-20 12:03:53 +0200151 * efi_get_variable() - retrieve value of a UEFI variable
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100152 *
153 * This function implements the GetVariable runtime service.
154 *
155 * See the Unified Extensible Firmware Interface (UEFI) specification for
156 * details.
157 *
158 * @variable_name: name of the variable
159 * @vendor: vendor GUID
160 * @attributes: attributes of the variable
161 * @data_size: size of the buffer to which the variable value is copied
162 * @data: buffer to which the variable value is copied
163 * Return: status code
164 */
Heinrich Schuchardte06ae192018-12-30 20:53:51 +0100165efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
166 const efi_guid_t *vendor, u32 *attributes,
167 efi_uintn_t *data_size, void *data)
Rob Clark15f3d742017-09-13 18:05:37 -0400168{
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200169 char *native_name;
Rob Clark15f3d742017-09-13 18:05:37 -0400170 efi_status_t ret;
171 unsigned long in_size;
172 const char *val, *s;
173 u32 attr;
174
Rob Clark238f88c2017-09-13 18:05:41 -0400175 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
Rob Clark15f3d742017-09-13 18:05:37 -0400176 data_size, data);
177
178 if (!variable_name || !vendor || !data_size)
179 return EFI_EXIT(EFI_INVALID_PARAMETER);
180
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200181 ret = efi_to_native(&native_name, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400182 if (ret)
183 return EFI_EXIT(ret);
184
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200185 EFI_PRINT("get '%s'\n", native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400186
187 val = env_get(native_name);
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200188 free(native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400189 if (!val)
190 return EFI_EXIT(EFI_NOT_FOUND);
191
192 val = parse_attr(val, &attr);
193
194 in_size = *data_size;
195
196 if ((s = prefix(val, "(blob)"))) {
Heinrich Schuchardt2d8aedc2019-01-18 12:31:54 +0100197 size_t len = strlen(s);
Rob Clark15f3d742017-09-13 18:05:37 -0400198
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700199 /* number of hexadecimal digits must be even */
200 if (len & 1)
201 return EFI_EXIT(EFI_DEVICE_ERROR);
202
Rob Clark15f3d742017-09-13 18:05:37 -0400203 /* two characters per byte: */
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700204 len /= 2;
Rob Clark15f3d742017-09-13 18:05:37 -0400205 *data_size = len;
206
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200207 if (in_size < len) {
208 ret = EFI_BUFFER_TOO_SMALL;
209 goto out;
210 }
Rob Clark15f3d742017-09-13 18:05:37 -0400211
212 if (!data)
213 return EFI_EXIT(EFI_INVALID_PARAMETER);
214
Heinrich Schuchardt2d8aedc2019-01-18 12:31:54 +0100215 if (hex2bin(data, s, len))
Rob Clark15f3d742017-09-13 18:05:37 -0400216 return EFI_EXIT(EFI_DEVICE_ERROR);
217
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200218 EFI_PRINT("got value: \"%s\"\n", s);
Rob Clark15f3d742017-09-13 18:05:37 -0400219 } else if ((s = prefix(val, "(utf8)"))) {
220 unsigned len = strlen(s) + 1;
221
222 *data_size = len;
223
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200224 if (in_size < len) {
225 ret = EFI_BUFFER_TOO_SMALL;
226 goto out;
227 }
Rob Clark15f3d742017-09-13 18:05:37 -0400228
229 if (!data)
230 return EFI_EXIT(EFI_INVALID_PARAMETER);
231
232 memcpy(data, s, len);
233 ((char *)data)[len] = '\0';
234
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200235 EFI_PRINT("got value: \"%s\"\n", (char *)data);
Rob Clark15f3d742017-09-13 18:05:37 -0400236 } else {
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200237 EFI_PRINT("invalid value: '%s'\n", val);
Rob Clark15f3d742017-09-13 18:05:37 -0400238 return EFI_EXIT(EFI_DEVICE_ERROR);
239 }
240
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200241out:
Rob Clark15f3d742017-09-13 18:05:37 -0400242 if (attributes)
243 *attributes = attr & EFI_VARIABLE_MASK;
244
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200245 return EFI_EXIT(ret);
Rob Clark15f3d742017-09-13 18:05:37 -0400246}
247
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100248static char *efi_variables_list;
249static char *efi_cur_variable;
250
251/**
252 * parse_uboot_variable() - parse a u-boot variable and get uefi-related
253 * information
254 * @variable: whole data of u-boot variable (ie. name=value)
255 * @variable_name_size: size of variable_name buffer in byte
256 * @variable_name: name of uefi variable in u16, null-terminated
257 * @vendor: vendor's guid
258 * @attributes: attributes
259 *
260 * A uefi variable is encoded into a u-boot variable as described above.
261 * This function parses such a u-boot variable and retrieve uefi-related
262 * information into respective parameters. In return, variable_name_size
263 * is the size of variable name including NULL.
264 *
265 * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200266 * the entire variable list has been returned,
267 * otherwise non-zero status code
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100268 */
269static efi_status_t parse_uboot_variable(char *variable,
270 efi_uintn_t *variable_name_size,
271 u16 *variable_name,
272 const efi_guid_t *vendor,
273 u32 *attributes)
274{
275 char *guid, *name, *end, c;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100276 size_t name_len;
277 efi_uintn_t old_variable_name_size;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100278 u16 *p;
279
280 guid = strchr(variable, '_');
281 if (!guid)
282 return EFI_INVALID_PARAMETER;
283 guid++;
284 name = strchr(guid, '_');
285 if (!name)
286 return EFI_INVALID_PARAMETER;
287 name++;
288 end = strchr(name, '=');
289 if (!end)
290 return EFI_INVALID_PARAMETER;
291
292 name_len = end - name;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100293 old_variable_name_size = *variable_name_size;
294 *variable_name_size = sizeof(u16) * (name_len + 1);
295 if (old_variable_name_size < *variable_name_size)
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100296 return EFI_BUFFER_TOO_SMALL;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100297
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100298 end++; /* point to value */
299
300 /* variable name */
301 p = variable_name;
302 utf8_utf16_strncpy(&p, name, name_len);
303 variable_name[name_len] = 0;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100304
305 /* guid */
306 c = *(name - 1);
307 *(name - 1) = '\0'; /* guid need be null-terminated here */
308 uuid_str_to_bin(guid, (unsigned char *)vendor, UUID_STR_FORMAT_GUID);
309 *(name - 1) = c;
310
311 /* attributes */
312 parse_attr(end, attributes);
313
314 return EFI_SUCCESS;
315}
316
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100317/**
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100318 * efi_get_next_variable_name() - enumerate the current variable names
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200319 *
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100320 * @variable_name_size: size of variable_name buffer in byte
321 * @variable_name: name of uefi variable's name in u16
322 * @vendor: vendor's guid
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100323 *
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100324 * This function implements the GetNextVariableName service.
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100325 *
326 * See the Unified Extensible Firmware Interface (UEFI) specification for
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200327 * details.
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100328 *
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100329 * Return: status code
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100330 */
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200331efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
332 u16 *variable_name,
Heinrich Schuchardt82cd6c42020-03-22 18:28:20 +0100333 efi_guid_t *vendor)
Rob Clark15f3d742017-09-13 18:05:37 -0400334{
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100335 char *native_name, *variable;
336 ssize_t name_len, list_len;
337 char regex[256];
338 char * const regexlist[] = {regex};
339 u32 attributes;
340 int i;
341 efi_status_t ret;
342
Rob Clark238f88c2017-09-13 18:05:41 -0400343 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400344
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100345 if (!variable_name_size || !variable_name || !vendor)
Heinrich Schuchardt793a6252019-03-19 18:36:21 +0100346 return EFI_EXIT(EFI_INVALID_PARAMETER);
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100347
348 if (variable_name[0]) {
349 /* check null-terminated string */
350 for (i = 0; i < *variable_name_size; i++)
351 if (!variable_name[i])
352 break;
353 if (i >= *variable_name_size)
354 return EFI_EXIT(EFI_INVALID_PARAMETER);
355
356 /* search for the last-returned variable */
357 ret = efi_to_native(&native_name, variable_name, vendor);
358 if (ret)
359 return EFI_EXIT(ret);
360
361 name_len = strlen(native_name);
362 for (variable = efi_variables_list; variable && *variable;) {
363 if (!strncmp(variable, native_name, name_len) &&
364 variable[name_len] == '=')
365 break;
366
367 variable = strchr(variable, '\n');
368 if (variable)
369 variable++;
370 }
371
372 free(native_name);
373 if (!(variable && *variable))
374 return EFI_EXIT(EFI_INVALID_PARAMETER);
375
376 /* next variable */
377 variable = strchr(variable, '\n');
378 if (variable)
379 variable++;
380 if (!(variable && *variable))
381 return EFI_EXIT(EFI_NOT_FOUND);
382 } else {
383 /*
384 *new search: free a list used in the previous search
385 */
386 free(efi_variables_list);
387 efi_variables_list = NULL;
388 efi_cur_variable = NULL;
389
390 snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
391 list_len = hexport_r(&env_htab, '\n',
392 H_MATCH_REGEX | H_MATCH_KEY,
393 &efi_variables_list, 0, 1, regexlist);
Heinrich Schuchardtfd738482019-01-22 20:10:46 +0100394 /* 1 indicates that no match was found */
395 if (list_len <= 1)
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100396 return EFI_EXIT(EFI_NOT_FOUND);
397
398 variable = efi_variables_list;
399 }
400
401 ret = parse_uboot_variable(variable, variable_name_size, variable_name,
402 vendor, &attributes);
403
404 return EFI_EXIT(ret);
Rob Clark15f3d742017-09-13 18:05:37 -0400405}
406
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100407/**
Heinrich Schuchardt0f042f92019-06-20 12:03:53 +0200408 * efi_set_variable() - set value of a UEFI variable
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100409 *
410 * This function implements the SetVariable runtime service.
411 *
412 * See the Unified Extensible Firmware Interface (UEFI) specification for
413 * details.
414 *
415 * @variable_name: name of the variable
416 * @vendor: vendor GUID
417 * @attributes: attributes of the variable
418 * @data_size: size of the buffer with the variable value
419 * @data: buffer with the variable value
420 * Return: status code
421 */
Heinrich Schuchardte06ae192018-12-30 20:53:51 +0100422efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
423 const efi_guid_t *vendor, u32 attributes,
Heinrich Schuchardt73e2ab02018-12-30 21:03:15 +0100424 efi_uintn_t data_size, const void *data)
Rob Clark15f3d742017-09-13 18:05:37 -0400425{
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200426 char *native_name = NULL, *val = NULL, *s;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900427 const char *old_val;
428 size_t old_size;
Rob Clark15f3d742017-09-13 18:05:37 -0400429 efi_status_t ret = EFI_SUCCESS;
Rob Clark15f3d742017-09-13 18:05:37 -0400430 u32 attr;
431
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200432 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
Rob Clark15f3d742017-09-13 18:05:37 -0400433 data_size, data);
434
Heinrich Schuchardtadba3962019-06-12 23:28:42 +0200435 if (!variable_name || !*variable_name || !vendor ||
436 ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900437 !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) {
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200438 ret = EFI_INVALID_PARAMETER;
439 goto out;
440 }
Rob Clark15f3d742017-09-13 18:05:37 -0400441
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200442 ret = efi_to_native(&native_name, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400443 if (ret)
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200444 goto out;
Rob Clark15f3d742017-09-13 18:05:37 -0400445
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900446 old_val = env_get(native_name);
447 if (old_val) {
448 old_val = parse_attr(old_val, &attr);
Rob Clark15f3d742017-09-13 18:05:37 -0400449
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900450 /* check read-only first */
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200451 if (attr & READ_ONLY) {
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200452 ret = EFI_WRITE_PROTECTED;
453 goto out;
454 }
AKASHI Takahiro186b5a42019-05-24 15:59:03 +0900455
Heinrich Schuchardta6c25fb2019-09-23 22:38:53 +0200456 if ((data_size == 0 &&
457 !(attributes & EFI_VARIABLE_APPEND_WRITE)) ||
458 !attributes) {
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900459 /* delete the variable: */
460 env_set(native_name, NULL);
461 ret = EFI_SUCCESS;
462 goto out;
463 }
464
465 /* attributes won't be changed */
466 if (attr != (attributes & ~EFI_VARIABLE_APPEND_WRITE)) {
AKASHI Takahiro186b5a42019-05-24 15:59:03 +0900467 ret = EFI_INVALID_PARAMETER;
468 goto out;
469 }
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900470
471 if (attributes & EFI_VARIABLE_APPEND_WRITE) {
472 if (!prefix(old_val, "(blob)")) {
Heinrich Schuchardte044a762019-09-23 22:18:09 +0200473 ret = EFI_DEVICE_ERROR;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900474 goto out;
475 }
476 old_size = strlen(old_val);
477 } else {
478 old_size = 0;
479 }
480 } else {
Heinrich Schuchardt8021bbe2019-09-26 21:40:18 +0200481 if (data_size == 0 || !attributes ||
482 (attributes & EFI_VARIABLE_APPEND_WRITE)) {
483 /*
484 * Trying to delete or to update a non-existent
485 * variable.
486 */
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900487 ret = EFI_NOT_FOUND;
488 goto out;
489 }
490
491 old_size = 0;
Rob Clark15f3d742017-09-13 18:05:37 -0400492 }
493
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900494 val = malloc(old_size + 2 * data_size
495 + strlen("{ro,run,boot,nv}(blob)") + 1);
Heinrich Schuchardtbd095f52018-10-02 05:30:05 +0200496 if (!val) {
497 ret = EFI_OUT_OF_RESOURCES;
498 goto out;
499 }
Rob Clark15f3d742017-09-13 18:05:37 -0400500
501 s = val;
502
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900503 /* store attributes */
AKASHI Takahiro7f334332019-06-04 15:52:06 +0900504 attributes &= (EFI_VARIABLE_NON_VOLATILE |
505 EFI_VARIABLE_BOOTSERVICE_ACCESS |
506 EFI_VARIABLE_RUNTIME_ACCESS);
Rob Clark15f3d742017-09-13 18:05:37 -0400507 s += sprintf(s, "{");
508 while (attributes) {
509 u32 attr = 1 << (ffs(attributes) - 1);
510
AKASHI Takahiro7f334332019-06-04 15:52:06 +0900511 if (attr == EFI_VARIABLE_NON_VOLATILE)
512 s += sprintf(s, "nv");
513 else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS)
Rob Clark15f3d742017-09-13 18:05:37 -0400514 s += sprintf(s, "boot");
515 else if (attr == EFI_VARIABLE_RUNTIME_ACCESS)
516 s += sprintf(s, "run");
517
518 attributes &= ~attr;
519 if (attributes)
520 s += sprintf(s, ",");
521 }
522 s += sprintf(s, "}");
523
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900524 if (old_size)
525 /* APPEND_WRITE */
526 s += sprintf(s, old_val);
527 else
528 s += sprintf(s, "(blob)");
529
Rob Clark15f3d742017-09-13 18:05:37 -0400530 /* store payload: */
Heinrich Schuchardt807899d2019-01-18 18:54:26 +0100531 s = bin2hex(s, data, data_size);
Rob Clark15f3d742017-09-13 18:05:37 -0400532 *s = '\0';
533
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200534 EFI_PRINT("setting: %s=%s\n", native_name, val);
Rob Clark15f3d742017-09-13 18:05:37 -0400535
536 if (env_set(native_name, val))
537 ret = EFI_DEVICE_ERROR;
538
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200539out:
540 free(native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400541 free(val);
542
543 return EFI_EXIT(ret);
544}
Heinrich Schuchardt9b19dae2019-06-20 12:13:05 +0200545
546/**
547 * efi_query_variable_info() - get information about EFI variables
548 *
549 * This function implements the QueryVariableInfo() runtime service.
550 *
551 * See the Unified Extensible Firmware Interface (UEFI) specification for
552 * details.
553 *
554 * @attributes: bitmask to select variables to be
555 * queried
556 * @maximum_variable_storage_size: maximum size of storage area for the
557 * selected variable types
558 * @remaining_variable_storage_size: remaining size of storage are for the
559 * selected variable types
560 * @maximum_variable_size: maximum size of a variable of the
561 * selected type
562 * Returns: status code
563 */
564efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
565 u32 attributes,
566 u64 *maximum_variable_storage_size,
567 u64 *remaining_variable_storage_size,
568 u64 *maximum_variable_size)
569{
570 return EFI_UNSUPPORTED;
571}
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +0200572
573/**
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +0200574 * efi_get_variable_runtime() - runtime implementation of GetVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200575 *
576 * @variable_name: name of the variable
577 * @vendor: vendor GUID
578 * @attributes: attributes of the variable
579 * @data_size: size of the buffer to which the variable value is copied
580 * @data: buffer to which the variable value is copied
581 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +0200582 */
583static efi_status_t __efi_runtime EFIAPI
584efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
585 u32 *attributes, efi_uintn_t *data_size, void *data)
586{
587 return EFI_UNSUPPORTED;
588}
589
590/**
591 * efi_get_next_variable_name_runtime() - runtime implementation of
592 * GetNextVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200593 *
594 * @variable_name_size: size of variable_name buffer in byte
595 * @variable_name: name of uefi variable's name in u16
596 * @vendor: vendor's guid
597 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +0200598 */
599static efi_status_t __efi_runtime EFIAPI
600efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
Heinrich Schuchardt82cd6c42020-03-22 18:28:20 +0100601 u16 *variable_name, efi_guid_t *vendor)
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +0200602{
603 return EFI_UNSUPPORTED;
604}
605
606/**
607 * efi_set_variable_runtime() - runtime implementation of SetVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200608 *
609 * @variable_name: name of the variable
610 * @vendor: vendor GUID
611 * @attributes: attributes of the variable
612 * @data_size: size of the buffer with the variable value
613 * @data: buffer with the variable value
614 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +0200615 */
616static efi_status_t __efi_runtime EFIAPI
617efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
618 u32 attributes, efi_uintn_t data_size,
619 const void *data)
620{
621 return EFI_UNSUPPORTED;
622}
623
624/**
625 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
626 */
627void efi_variables_boot_exit_notify(void)
628{
629 efi_runtime_services.get_variable = efi_get_variable_runtime;
630 efi_runtime_services.get_next_variable_name =
631 efi_get_next_variable_name_runtime;
632 efi_runtime_services.set_variable = efi_set_variable_runtime;
633 efi_update_table_header_crc32(&efi_runtime_services.hdr);
634}
635
636/**
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +0200637 * efi_init_variables() - initialize variable services
638 *
639 * Return: status code
640 */
641efi_status_t efi_init_variables(void)
642{
643 return EFI_SUCCESS;
644}