blob: 6f69a84feaa0e2d4fb146e8a1dc34a52b58119b8 [file] [log] [blame]
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +09001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Integrate UEFI variables to u-boot env interface
4 *
5 * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited
6 */
7
8#include <charset.h>
9#include <common.h>
10#include <command.h>
11#include <efi_loader.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060012#include <env.h>
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +090013#include <exports.h>
14#include <hexdump.h>
15#include <malloc.h>
AKASHI Takahiro051aa892019-10-24 15:17:13 +090016#include <mapmem.h>
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +090017#include <linux/kernel.h>
18
19/*
20 * From efi_variable.c,
21 *
22 * Mapping between UEFI variables and u-boot variables:
23 *
24 * efi_$guid_$varname = {attributes}(type)value
25 */
26
27static const struct {
28 u32 mask;
29 char *text;
30} efi_var_attrs[] = {
31 {EFI_VARIABLE_NON_VOLATILE, "NV"},
32 {EFI_VARIABLE_BOOTSERVICE_ACCESS, "BS"},
33 {EFI_VARIABLE_RUNTIME_ACCESS, "RT"},
34 {EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, "AW"},
35 {EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, "AT"},
36};
37
AKASHI Takahiro051aa892019-10-24 15:17:13 +090038static const struct {
39 efi_guid_t guid;
40 char *text;
41} efi_guid_text[] = {
42 /* signature database */
43 {EFI_GLOBAL_VARIABLE_GUID, "EFI_GLOBAL_VARIABLE_GUID"},
AKASHI Takahirof757d042020-04-14 11:51:46 +090044 {EFI_IMAGE_SECURITY_DATABASE_GUID, "EFI_IMAGE_SECURITY_DATABASE_GUID"},
45 /* certificate type */
46 {EFI_CERT_SHA256_GUID, "EFI_CERT_SHA256_GUID"},
47 {EFI_CERT_X509_GUID, "EFI_CERT_X509_GUID"},
48 {EFI_CERT_TYPE_PKCS7_GUID, "EFI_CERT_TYPE_PKCS7_GUID"},
AKASHI Takahiro051aa892019-10-24 15:17:13 +090049};
50
51/* "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */
52static char unknown_guid[37];
53
54/**
55 * efi_guid_to_str() - convert guid to readable name
56 *
57 * @guid: GUID
58 * Return: string for GUID
59 *
60 * convert guid to readable name
61 */
62static const char *efi_guid_to_str(const efi_guid_t *guid)
63{
64 int i;
65
66 for (i = 0; i < ARRAY_SIZE(efi_guid_text); i++)
67 if (!guidcmp(guid, &efi_guid_text[i].guid))
68 return efi_guid_text[i].text;
69
70 uuid_bin_to_str((unsigned char *)guid->b, unknown_guid,
71 UUID_STR_FORMAT_GUID);
72
73 return unknown_guid;
74}
75
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +090076/**
77 * efi_dump_single_var() - show information about a UEFI variable
78 *
79 * @name: Name of the variable
80 * @guid: Vendor GUID
AKASHI Takahiro051aa892019-10-24 15:17:13 +090081 * @verbose: if true, dump data
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +090082 *
83 * Show information encoded in one UEFI variable
84 */
AKASHI Takahiro051aa892019-10-24 15:17:13 +090085static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, bool verbose)
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +090086{
87 u32 attributes;
88 u8 *data;
89 efi_uintn_t size;
90 int count, i;
91 efi_status_t ret;
92
93 data = NULL;
94 size = 0;
95 ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size, data));
96 if (ret == EFI_BUFFER_TOO_SMALL) {
97 data = malloc(size);
98 if (!data)
99 goto out;
100
101 ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size,
102 data));
103 }
104 if (ret == EFI_NOT_FOUND) {
105 printf("Error: \"%ls\" not defined\n", name);
106 goto out;
107 }
108 if (ret != EFI_SUCCESS)
109 goto out;
110
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900111 printf("%ls:\n %s:", name, efi_guid_to_str(guid));
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900112 for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++)
113 if (attributes & efi_var_attrs[i].mask) {
114 if (count)
115 putc('|');
116 else
117 putc(' ');
118 count++;
119 puts(efi_var_attrs[i].text);
120 }
121 printf(", DataSize = 0x%zx\n", size);
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900122 if (verbose)
123 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
124 data, size, true);
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900125
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900126out:
127 free(data);
128}
129
130/**
131 * efi_dump_vars() - show information about named UEFI variables
132 *
133 * @argc: Number of arguments (variables)
134 * @argv: Argument (variable name) array
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900135 * @verbose: if true, dump data
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900136 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
137 *
138 * Show information encoded in named UEFI variables
139 */
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900140static int efi_dump_vars(int argc, char * const argv[],
141 const efi_guid_t *guid, bool verbose)
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900142{
143 u16 *var_name16, *p;
144 efi_uintn_t buf_size, size;
145
146 buf_size = 128;
147 var_name16 = malloc(buf_size);
148 if (!var_name16)
149 return CMD_RET_FAILURE;
150
151 for (; argc > 0; argc--, argv++) {
152 size = (utf8_utf16_strlen(argv[0]) + 1) * sizeof(u16);
153 if (buf_size < size) {
154 buf_size = size;
155 p = realloc(var_name16, buf_size);
156 if (!p) {
157 free(var_name16);
158 return CMD_RET_FAILURE;
159 }
160 var_name16 = p;
161 }
162
163 p = var_name16;
164 utf8_utf16_strcpy(&p, argv[0]);
165
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900166 efi_dump_single_var(var_name16, guid, verbose);
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900167 }
168
169 free(var_name16);
170
171 return CMD_RET_SUCCESS;
172}
173
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900174static bool match_name(int argc, char * const argv[], u16 *var_name16)
175{
176 char *buf, *p;
177 size_t buflen;
178 int i;
179 bool result = false;
180
181 buflen = utf16_utf8_strlen(var_name16) + 1;
182 buf = calloc(1, buflen);
183 if (!buf)
184 return result;
185
186 p = buf;
187 utf16_utf8_strcpy(&p, var_name16);
188
189 for (i = 0; i < argc; argc--, argv++) {
190 if (!strcmp(buf, argv[i])) {
191 result = true;
192 goto out;
193 }
194 }
195
196out:
197 free(buf);
198
199 return result;
200}
201
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900202/**
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900203 * efi_dump_var_all() - show information about all the UEFI variables
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900204 *
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900205 * @argc: Number of arguments (variables)
206 * @argv: Argument (variable name) array
207 * @verbose: if true, dump data
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900208 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
209 *
210 * Show information encoded in all the UEFI variables
211 */
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900212static int efi_dump_var_all(int argc, char * const argv[],
213 const efi_guid_t *guid_p, bool verbose)
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900214{
215 u16 *var_name16, *p;
216 efi_uintn_t buf_size, size;
217 efi_guid_t guid;
218 efi_status_t ret;
219
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900220 if (argc && guid_p)
221 /* simplified case */
222 return efi_dump_vars(argc, argv, guid_p, verbose);
223
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900224 buf_size = 128;
225 var_name16 = malloc(buf_size);
226 if (!var_name16)
227 return CMD_RET_FAILURE;
228
229 var_name16[0] = 0;
230 for (;;) {
231 size = buf_size;
232 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
233 &guid));
234 if (ret == EFI_NOT_FOUND)
235 break;
236 if (ret == EFI_BUFFER_TOO_SMALL) {
237 buf_size = size;
238 p = realloc(var_name16, buf_size);
239 if (!p) {
240 free(var_name16);
241 return CMD_RET_FAILURE;
242 }
243 var_name16 = p;
244 ret = EFI_CALL(efi_get_next_variable_name(&size,
245 var_name16,
246 &guid));
247 }
248 if (ret != EFI_SUCCESS) {
249 free(var_name16);
250 return CMD_RET_FAILURE;
251 }
252
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900253 if ((!guid_p || !guidcmp(guid_p, &guid)) &&
254 (!argc || match_name(argc, argv, var_name16)))
255 efi_dump_single_var(var_name16, &guid, verbose);
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900256 }
257
258 free(var_name16);
259
260 return CMD_RET_SUCCESS;
261}
262
263/**
264 * do_env_print_efi() - show information about UEFI variables
265 *
266 * @cmdtp: Command table
267 * @flag: Command flag
268 * @argc: Number of arguments
269 * @argv: Argument array
270 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
271 *
272 * This function is for "env print -e" or "printenv -e" command:
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900273 * => env print -e [-n] [-guid <guid> | -all] [var [...]]
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900274 * If one or more variable names are specified, show information
275 * named UEFI variables, otherwise show all the UEFI variables.
276 */
277int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
278{
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900279 efi_guid_t guid;
280 const efi_guid_t *guid_p;
281 bool default_guid, guid_any, verbose;
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900282 efi_status_t ret;
283
284 /* Initialize EFI drivers */
285 ret = efi_init_obj_list();
286 if (ret != EFI_SUCCESS) {
287 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
288 ret & ~EFI_ERROR_MASK);
289 return CMD_RET_FAILURE;
290 }
291
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900292 default_guid = true;
293 guid_any = false;
294 verbose = true;
295 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
296 if (!strcmp(argv[0], "-guid")) {
297 if (argc == 1)
298 return CMD_RET_USAGE;
299
300 /* -a already specified */
Heinrich Schuchardt9900e462020-05-06 02:01:34 +0200301 if (!default_guid && guid_any)
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900302 return CMD_RET_USAGE;
303
304 argc--;
305 argv++;
306 if (uuid_str_to_bin(argv[0], guid.b,
307 UUID_STR_FORMAT_GUID))
308 return CMD_RET_USAGE;
309 default_guid = false;
310 } else if (!strcmp(argv[0], "-all")) {
311 /* -guid already specified */
312 if (!default_guid && !guid_any)
313 return CMD_RET_USAGE;
314
315 guid_any = true;
316 default_guid = false;
317 } else if (!strcmp(argv[0], "-n")) {
318 verbose = false;
319 } else {
320 return CMD_RET_USAGE;
321 }
322 }
323
324 if (guid_any)
325 guid_p = NULL;
326 else if (default_guid)
327 guid_p = &efi_global_variable_guid;
328 else
329 guid_p = (const efi_guid_t *)guid.b;
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900330
331 /* enumerate and show all UEFI variables */
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900332 return efi_dump_var_all(argc, argv, guid_p, verbose);
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900333}
334
335/**
336 * append_value() - encode UEFI variable's value
337 * @bufp: Buffer of encoded UEFI variable's value
338 * @sizep: Size of buffer
339 * @data: data to be encoded into the value
340 * Return: 0 on success, -1 otherwise
341 *
342 * Interpret a given data string and append it to buffer.
343 * Buffer will be realloc'ed if necessary.
344 *
345 * Currently supported formats are:
346 * =0x0123...: Hexadecimal number
347 * =H0123...: Hexadecimal-byte array
348 * ="...", =S"..." or <string>:
349 * String
350 */
351static int append_value(char **bufp, size_t *sizep, char *data)
352{
353 char *tmp_buf = NULL, *new_buf = NULL, *value;
354 unsigned long len = 0;
355
356 if (!strncmp(data, "=0x", 2)) { /* hexadecimal number */
357 union {
358 u8 u8;
359 u16 u16;
360 u32 u32;
361 u64 u64;
362 } tmp_data;
363 unsigned long hex_value;
364 void *hex_ptr;
365
366 data += 3;
367 len = strlen(data);
368 if ((len & 0x1)) /* not multiple of two */
369 return -1;
370
371 len /= 2;
372 if (len > 8)
373 return -1;
374 else if (len > 4)
375 len = 8;
376 else if (len > 2)
377 len = 4;
378
379 /* convert hex hexadecimal number */
380 if (strict_strtoul(data, 16, &hex_value) < 0)
381 return -1;
382
383 tmp_buf = malloc(len);
384 if (!tmp_buf)
385 return -1;
386
387 if (len == 1) {
388 tmp_data.u8 = hex_value;
389 hex_ptr = &tmp_data.u8;
390 } else if (len == 2) {
391 tmp_data.u16 = hex_value;
392 hex_ptr = &tmp_data.u16;
393 } else if (len == 4) {
394 tmp_data.u32 = hex_value;
395 hex_ptr = &tmp_data.u32;
396 } else {
397 tmp_data.u64 = hex_value;
398 hex_ptr = &tmp_data.u64;
399 }
400 memcpy(tmp_buf, hex_ptr, len);
401 value = tmp_buf;
402
403 } else if (!strncmp(data, "=H", 2)) { /* hexadecimal-byte array */
404 data += 2;
405 len = strlen(data);
406 if (len & 0x1) /* not multiple of two */
407 return -1;
408
409 len /= 2;
410 tmp_buf = malloc(len);
411 if (!tmp_buf)
412 return -1;
413
Heinrich Schuchardt45203e02019-04-30 07:14:09 +0200414 if (hex2bin((u8 *)tmp_buf, data, len) < 0) {
415 printf("Error: illegal hexadecimal string\n");
416 free(tmp_buf);
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900417 return -1;
Heinrich Schuchardt45203e02019-04-30 07:14:09 +0200418 }
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900419
420 value = tmp_buf;
421 } else { /* string */
422 if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) {
423 if (data[1] == '"')
424 data += 2;
425 else
426 data += 3;
427 value = data;
428 len = strlen(data) - 1;
429 if (data[len] != '"')
430 return -1;
431 } else {
432 value = data;
433 len = strlen(data);
434 }
435 }
436
437 new_buf = realloc(*bufp, *sizep + len);
438 if (!new_buf)
439 goto out;
440
441 memcpy(new_buf + *sizep, value, len);
442 *bufp = new_buf;
443 *sizep += len;
444
445out:
446 free(tmp_buf);
447
448 return 0;
449}
450
451/**
AKASHI Takahiro6810caf2019-10-04 13:46:25 +0900452 * do_env_set_efi() - set UEFI variable
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900453 *
454 * @cmdtp: Command table
455 * @flag: Command flag
456 * @argc: Number of arguments
457 * @argv: Argument array
458 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
459 *
460 * This function is for "env set -e" or "setenv -e" command:
AKASHI Takahiroe50e2872020-04-14 11:51:47 +0900461 * => env set -e [-guid guid][-nv][-bs][-rt][-at][-a][-v]
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900462 * [-i address,size] var, or
463 * var [value ...]
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900464 * Encode values specified and set given UEFI variable.
465 * If no value is specified, delete the variable.
466 */
467int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
468{
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900469 char *var_name, *value, *ep;
470 ulong addr;
471 efi_uintn_t size;
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900472 efi_guid_t guid;
AKASHI Takahiro4b27a762019-06-04 15:52:11 +0900473 u32 attributes;
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900474 bool default_guid, verbose, value_on_memory;
475 u16 *var_name16 = NULL, *p;
476 size_t len;
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900477 efi_status_t ret;
478
479 if (argc == 1)
480 return CMD_RET_USAGE;
481
482 /* Initialize EFI drivers */
483 ret = efi_init_obj_list();
484 if (ret != EFI_SUCCESS) {
485 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
486 ret & ~EFI_ERROR_MASK);
487 return CMD_RET_FAILURE;
488 }
489
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900490 /*
491 * attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
492 * EFI_VARIABLE_RUNTIME_ACCESS;
493 */
494 value = NULL;
495 size = 0;
496 attributes = 0;
497 guid = efi_global_variable_guid;
498 default_guid = true;
499 verbose = false;
500 value_on_memory = false;
501 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
502 if (!strcmp(argv[0], "-guid")) {
503 if (argc == 1)
504 return CMD_RET_USAGE;
505
506 argc--;
507 argv++;
508 if (uuid_str_to_bin(argv[0], guid.b,
509 UUID_STR_FORMAT_GUID)) {
510 printf("## Guid not specified or in XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX format\n");
511 return CMD_RET_FAILURE;
512 }
513 default_guid = false;
514 } else if (!strcmp(argv[0], "-bs")) {
515 attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
516 } else if (!strcmp(argv[0], "-rt")) {
517 attributes |= EFI_VARIABLE_RUNTIME_ACCESS;
518 } else if (!strcmp(argv[0], "-nv")) {
519 attributes |= EFI_VARIABLE_NON_VOLATILE;
AKASHI Takahiroe50e2872020-04-14 11:51:47 +0900520 } else if (!strcmp(argv[0], "-at")) {
521 attributes |=
522 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900523 } else if (!strcmp(argv[0], "-a")) {
524 attributes |= EFI_VARIABLE_APPEND_WRITE;
525 } else if (!strcmp(argv[0], "-i")) {
526 /* data comes from memory */
527 if (argc == 1)
528 return CMD_RET_USAGE;
529
530 argc--;
531 argv++;
532 addr = simple_strtoul(argv[0], &ep, 16);
533 if (*ep != ',')
534 return CMD_RET_USAGE;
535
AKASHI Takahirof757d042020-04-14 11:51:46 +0900536 /* 0 should be allowed for delete */
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900537 size = simple_strtoul(++ep, NULL, 16);
AKASHI Takahirof757d042020-04-14 11:51:46 +0900538
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900539 value_on_memory = true;
540 } else if (!strcmp(argv[0], "-v")) {
541 verbose = true;
542 } else {
543 return CMD_RET_USAGE;
544 }
545 }
546 if (!argc)
547 return CMD_RET_USAGE;
548
549 var_name = argv[0];
AKASHI Takahirof757d042020-04-14 11:51:46 +0900550 if (default_guid) {
551 if (!strcmp(var_name, "db") || !strcmp(var_name, "dbx") ||
552 !strcmp(var_name, "dbt"))
553 guid = efi_guid_image_security_database;
554 else
555 guid = efi_global_variable_guid;
556 }
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900557
558 if (verbose) {
559 printf("GUID: %s\n", efi_guid_to_str((const efi_guid_t *)
560 &guid));
561 printf("Attributes: 0x%x\n", attributes);
AKASHI Takahiro4b27a762019-06-04 15:52:11 +0900562 }
563
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900564 /* for value */
565 if (value_on_memory)
566 value = map_sysmem(addr, 0);
567 else if (argc > 1)
568 for (argc--, argv++; argc > 0; argc--, argv++)
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900569 if (append_value(&value, &size, argv[0]) < 0) {
AKASHI Takahiro8190b4a2019-05-28 09:00:35 +0900570 printf("## Failed to process an argument, %s\n",
571 argv[0]);
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900572 ret = CMD_RET_FAILURE;
573 goto out;
574 }
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900575
576 if (size && verbose) {
577 printf("Value:\n");
578 print_hex_dump(" ", DUMP_PREFIX_OFFSET,
579 16, 1, value, size, true);
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900580 }
581
582 len = utf8_utf16_strnlen(var_name, strlen(var_name));
583 var_name16 = malloc((len + 1) * 2);
584 if (!var_name16) {
AKASHI Takahiro8190b4a2019-05-28 09:00:35 +0900585 printf("## Out of memory\n");
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900586 ret = CMD_RET_FAILURE;
587 goto out;
588 }
589 p = var_name16;
590 utf8_utf16_strncpy(&p, var_name, len + 1);
591
AKASHI Takahiro4b27a762019-06-04 15:52:11 +0900592 ret = EFI_CALL(efi_set_variable(var_name16, &guid, attributes,
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900593 size, value));
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900594 unmap_sysmem(value);
AKASHI Takahiro8190b4a2019-05-28 09:00:35 +0900595 if (ret == EFI_SUCCESS) {
596 ret = CMD_RET_SUCCESS;
597 } else {
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900598 const char *msg;
599
600 switch (ret) {
601 case EFI_NOT_FOUND:
602 msg = " (not found)";
603 break;
604 case EFI_WRITE_PROTECTED:
605 msg = " (read only)";
606 break;
607 case EFI_INVALID_PARAMETER:
608 msg = " (invalid parameter)";
609 break;
610 case EFI_SECURITY_VIOLATION:
611 msg = " (validation failed)";
612 break;
613 case EFI_OUT_OF_RESOURCES:
614 msg = " (out of memory)";
615 break;
616 default:
617 msg = "";
618 break;
619 }
620 printf("## Failed to set EFI variable%s\n", msg);
AKASHI Takahiro8190b4a2019-05-28 09:00:35 +0900621 ret = CMD_RET_FAILURE;
622 }
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900623out:
AKASHI Takahiro051aa892019-10-24 15:17:13 +0900624 if (value_on_memory)
625 unmap_sysmem(value);
626 else
627 free(value);
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900628 free(var_name16);
629
630 return ret;
631}