blob: 64ae2ad2ce24b926c21f728bf4b31b2dbbe4370a [file] [log] [blame]
AKASHI Takahiro117e68a2019-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>
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09009#include <command.h>
10#include <efi_loader.h>
Heinrich Schuchardt70035652020-06-24 19:55:13 +020011#include <efi_variable.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060012#include <env.h>
AKASHI Takahiro117e68a2019-02-25 15:54:36 +090013#include <exports.h>
14#include <hexdump.h>
15#include <malloc.h>
AKASHI Takahiro2e952122019-10-24 15:17:13 +090016#include <mapmem.h>
Heinrich Schuchardt70035652020-06-24 19:55:13 +020017#include <rtc.h>
Simon Glass6f044982020-05-10 11:39:52 -060018#include <uuid.h>
AKASHI Takahiro117e68a2019-02-25 15:54:36 +090019#include <linux/kernel.h>
20
21/*
22 * From efi_variable.c,
23 *
24 * Mapping between UEFI variables and u-boot variables:
25 *
26 * efi_$guid_$varname = {attributes}(type)value
27 */
28
29static const struct {
30 u32 mask;
31 char *text;
32} efi_var_attrs[] = {
33 {EFI_VARIABLE_NON_VOLATILE, "NV"},
34 {EFI_VARIABLE_BOOTSERVICE_ACCESS, "BS"},
35 {EFI_VARIABLE_RUNTIME_ACCESS, "RT"},
36 {EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, "AW"},
37 {EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, "AT"},
Heinrich Schuchardt70035652020-06-24 19:55:13 +020038 {EFI_VARIABLE_READ_ONLY, "RO"},
AKASHI Takahiro117e68a2019-02-25 15:54:36 +090039};
40
41/**
42 * efi_dump_single_var() - show information about a UEFI variable
43 *
44 * @name: Name of the variable
45 * @guid: Vendor GUID
AKASHI Takahiro2e952122019-10-24 15:17:13 +090046 * @verbose: if true, dump data
AKASHI Takahiro117e68a2019-02-25 15:54:36 +090047 *
48 * Show information encoded in one UEFI variable
49 */
AKASHI Takahiro2e952122019-10-24 15:17:13 +090050static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, bool verbose)
AKASHI Takahiro117e68a2019-02-25 15:54:36 +090051{
52 u32 attributes;
53 u8 *data;
Heinrich Schuchardt70035652020-06-24 19:55:13 +020054 u64 time;
55 struct rtc_time tm;
AKASHI Takahiro117e68a2019-02-25 15:54:36 +090056 efi_uintn_t size;
57 int count, i;
58 efi_status_t ret;
59
60 data = NULL;
61 size = 0;
Heinrich Schuchardt70035652020-06-24 19:55:13 +020062 ret = efi_get_variable_int(name, guid, &attributes, &size, data, &time);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +090063 if (ret == EFI_BUFFER_TOO_SMALL) {
64 data = malloc(size);
65 if (!data)
66 goto out;
67
Heinrich Schuchardt70035652020-06-24 19:55:13 +020068 ret = efi_get_variable_int(name, guid, &attributes, &size,
69 data, &time);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +090070 }
71 if (ret == EFI_NOT_FOUND) {
72 printf("Error: \"%ls\" not defined\n", name);
73 goto out;
74 }
75 if (ret != EFI_SUCCESS)
76 goto out;
77
Heinrich Schuchardt70035652020-06-24 19:55:13 +020078 rtc_to_tm(time, &tm);
Heinrich Schuchardt1bebcb62022-01-16 14:53:37 +010079 printf("%ls:\n %pUl (%pUs)\n", name, guid, guid);
Heinrich Schuchardt70035652020-06-24 19:55:13 +020080 if (attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
81 printf(" %04d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year,
82 tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
83 printf(" ");
AKASHI Takahiro117e68a2019-02-25 15:54:36 +090084 for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++)
85 if (attributes & efi_var_attrs[i].mask) {
86 if (count)
87 putc('|');
AKASHI Takahiro117e68a2019-02-25 15:54:36 +090088 count++;
89 puts(efi_var_attrs[i].text);
90 }
91 printf(", DataSize = 0x%zx\n", size);
AKASHI Takahiro2e952122019-10-24 15:17:13 +090092 if (verbose)
93 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
94 data, size, true);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +090095
AKASHI Takahiro117e68a2019-02-25 15:54:36 +090096out:
97 free(data);
98}
99
Simon Glassed38aef2020-05-10 11:40:03 -0600100static bool match_name(int argc, char *const argv[], u16 *var_name16)
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900101{
102 char *buf, *p;
103 size_t buflen;
104 int i;
105 bool result = false;
106
107 buflen = utf16_utf8_strlen(var_name16) + 1;
108 buf = calloc(1, buflen);
109 if (!buf)
110 return result;
111
112 p = buf;
113 utf16_utf8_strcpy(&p, var_name16);
114
115 for (i = 0; i < argc; argc--, argv++) {
116 if (!strcmp(buf, argv[i])) {
117 result = true;
118 goto out;
119 }
120 }
121
122out:
123 free(buf);
124
125 return result;
126}
127
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900128/**
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900129 * efi_dump_var_all() - show information about all the UEFI variables
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900130 *
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900131 * @argc: Number of arguments (variables)
132 * @argv: Argument (variable name) array
133 * @verbose: if true, dump data
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900134 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
135 *
136 * Show information encoded in all the UEFI variables
137 */
Simon Glassed38aef2020-05-10 11:40:03 -0600138static int efi_dump_var_all(int argc, char *const argv[],
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900139 const efi_guid_t *guid_p, bool verbose)
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900140{
141 u16 *var_name16, *p;
142 efi_uintn_t buf_size, size;
143 efi_guid_t guid;
144 efi_status_t ret;
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +0200145 bool match = false;
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900146
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900147 buf_size = 128;
148 var_name16 = malloc(buf_size);
149 if (!var_name16)
150 return CMD_RET_FAILURE;
151
152 var_name16[0] = 0;
153 for (;;) {
154 size = buf_size;
Heinrich Schuchardt9205cd92021-09-09 07:39:40 +0200155 ret = efi_get_next_variable_name_int(&size, var_name16,
156 &guid);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900157 if (ret == EFI_NOT_FOUND)
158 break;
159 if (ret == EFI_BUFFER_TOO_SMALL) {
160 buf_size = size;
161 p = realloc(var_name16, buf_size);
162 if (!p) {
163 free(var_name16);
164 return CMD_RET_FAILURE;
165 }
166 var_name16 = p;
Heinrich Schuchardt9205cd92021-09-09 07:39:40 +0200167 ret = efi_get_next_variable_name_int(&size, var_name16,
168 &guid);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900169 }
170 if (ret != EFI_SUCCESS) {
171 free(var_name16);
172 return CMD_RET_FAILURE;
173 }
174
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +0200175 if (guid_p && guidcmp(guid_p, &guid))
176 continue;
177 if (!argc || match_name(argc, argv, var_name16)) {
178 match = true;
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900179 efi_dump_single_var(var_name16, &guid, verbose);
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +0200180 }
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900181 }
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900182 free(var_name16);
183
Heinrich Schuchardtf4e7b842022-06-19 13:36:48 +0200184 if (!match && argc == 1) {
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +0200185 printf("Error: \"%s\" not defined\n", argv[0]);
Heinrich Schuchardtf4e7b842022-06-19 13:36:48 +0200186 return CMD_RET_FAILURE;
187 }
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +0200188
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900189 return CMD_RET_SUCCESS;
190}
191
192/**
193 * do_env_print_efi() - show information about UEFI variables
194 *
195 * @cmdtp: Command table
196 * @flag: Command flag
197 * @argc: Number of arguments
198 * @argv: Argument array
199 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
200 *
201 * This function is for "env print -e" or "printenv -e" command:
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900202 * => env print -e [-n] [-guid <guid> | -all] [var [...]]
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900203 * If one or more variable names are specified, show information
204 * named UEFI variables, otherwise show all the UEFI variables.
205 */
Simon Glassed38aef2020-05-10 11:40:03 -0600206int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc,
207 char *const argv[])
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900208{
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +0200209 const efi_guid_t *guid_p = NULL;
AKASHI Takahirofbf66872021-07-03 15:43:17 +0900210 efi_guid_t guid;
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +0200211 bool verbose = true;
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900212 efi_status_t ret;
213
214 /* Initialize EFI drivers */
215 ret = efi_init_obj_list();
216 if (ret != EFI_SUCCESS) {
217 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
218 ret & ~EFI_ERROR_MASK);
219 return CMD_RET_FAILURE;
220 }
221
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900222 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
223 if (!strcmp(argv[0], "-guid")) {
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +0200224 if (argc == 1)
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900225 return CMD_RET_USAGE;
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900226 argc--;
227 argv++;
228 if (uuid_str_to_bin(argv[0], guid.b,
229 UUID_STR_FORMAT_GUID))
230 return CMD_RET_USAGE;
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +0200231 guid_p = (const efi_guid_t *)guid.b;
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900232 } else if (!strcmp(argv[0], "-n")) {
233 verbose = false;
234 } else {
235 return CMD_RET_USAGE;
236 }
237 }
238
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900239 /* enumerate and show all UEFI variables */
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900240 return efi_dump_var_all(argc, argv, guid_p, verbose);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900241}
242
243/**
244 * append_value() - encode UEFI variable's value
245 * @bufp: Buffer of encoded UEFI variable's value
246 * @sizep: Size of buffer
247 * @data: data to be encoded into the value
248 * Return: 0 on success, -1 otherwise
249 *
250 * Interpret a given data string and append it to buffer.
251 * Buffer will be realloc'ed if necessary.
252 *
253 * Currently supported formats are:
254 * =0x0123...: Hexadecimal number
255 * =H0123...: Hexadecimal-byte array
256 * ="...", =S"..." or <string>:
257 * String
258 */
259static int append_value(char **bufp, size_t *sizep, char *data)
260{
261 char *tmp_buf = NULL, *new_buf = NULL, *value;
262 unsigned long len = 0;
263
Dan Carpenter1ae09172023-07-31 17:03:59 +0300264 if (!strncmp(data, "=0x", 3)) { /* hexadecimal number */
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900265 union {
266 u8 u8;
267 u16 u16;
268 u32 u32;
269 u64 u64;
270 } tmp_data;
271 unsigned long hex_value;
272 void *hex_ptr;
273
274 data += 3;
275 len = strlen(data);
276 if ((len & 0x1)) /* not multiple of two */
277 return -1;
278
279 len /= 2;
280 if (len > 8)
281 return -1;
282 else if (len > 4)
283 len = 8;
284 else if (len > 2)
285 len = 4;
286
287 /* convert hex hexadecimal number */
288 if (strict_strtoul(data, 16, &hex_value) < 0)
289 return -1;
290
291 tmp_buf = malloc(len);
292 if (!tmp_buf)
293 return -1;
294
295 if (len == 1) {
296 tmp_data.u8 = hex_value;
297 hex_ptr = &tmp_data.u8;
298 } else if (len == 2) {
299 tmp_data.u16 = hex_value;
300 hex_ptr = &tmp_data.u16;
301 } else if (len == 4) {
302 tmp_data.u32 = hex_value;
303 hex_ptr = &tmp_data.u32;
304 } else {
305 tmp_data.u64 = hex_value;
306 hex_ptr = &tmp_data.u64;
307 }
308 memcpy(tmp_buf, hex_ptr, len);
309 value = tmp_buf;
310
311 } else if (!strncmp(data, "=H", 2)) { /* hexadecimal-byte array */
312 data += 2;
313 len = strlen(data);
314 if (len & 0x1) /* not multiple of two */
315 return -1;
316
317 len /= 2;
318 tmp_buf = malloc(len);
319 if (!tmp_buf)
320 return -1;
321
Heinrich Schuchardte979ee92019-04-30 07:14:09 +0200322 if (hex2bin((u8 *)tmp_buf, data, len) < 0) {
323 printf("Error: illegal hexadecimal string\n");
324 free(tmp_buf);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900325 return -1;
Heinrich Schuchardte979ee92019-04-30 07:14:09 +0200326 }
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900327
328 value = tmp_buf;
329 } else { /* string */
330 if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) {
331 if (data[1] == '"')
332 data += 2;
333 else
334 data += 3;
335 value = data;
336 len = strlen(data) - 1;
337 if (data[len] != '"')
338 return -1;
339 } else {
340 value = data;
341 len = strlen(data);
342 }
343 }
344
345 new_buf = realloc(*bufp, *sizep + len);
346 if (!new_buf)
347 goto out;
348
349 memcpy(new_buf + *sizep, value, len);
350 *bufp = new_buf;
351 *sizep += len;
352
353out:
354 free(tmp_buf);
355
356 return 0;
357}
358
359/**
AKASHI Takahiroa383c812019-10-04 13:46:25 +0900360 * do_env_set_efi() - set UEFI variable
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900361 *
362 * @cmdtp: Command table
363 * @flag: Command flag
364 * @argc: Number of arguments
365 * @argv: Argument array
366 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
367 *
368 * This function is for "env set -e" or "setenv -e" command:
AKASHI Takahirobd7defa2020-04-14 11:51:47 +0900369 * => env set -e [-guid guid][-nv][-bs][-rt][-at][-a][-v]
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900370 * [-i address,size] var, or
371 * var [value ...]
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900372 * Encode values specified and set given UEFI variable.
373 * If no value is specified, delete the variable.
374 */
Simon Glassed38aef2020-05-10 11:40:03 -0600375int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
376 char *const argv[])
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900377{
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900378 char *var_name, *value, *ep;
379 ulong addr;
380 efi_uintn_t size;
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900381 efi_guid_t guid;
AKASHI Takahiro26105762019-06-04 15:52:11 +0900382 u32 attributes;
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900383 bool default_guid, verbose, value_on_memory;
Heinrich Schuchardtdb9361b2022-10-06 06:52:51 +0200384 u16 *var_name16;
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900385 efi_status_t ret;
386
387 if (argc == 1)
388 return CMD_RET_USAGE;
389
390 /* Initialize EFI drivers */
391 ret = efi_init_obj_list();
392 if (ret != EFI_SUCCESS) {
393 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
394 ret & ~EFI_ERROR_MASK);
395 return CMD_RET_FAILURE;
396 }
397
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900398 /*
399 * attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
400 * EFI_VARIABLE_RUNTIME_ACCESS;
401 */
402 value = NULL;
403 size = 0;
404 attributes = 0;
405 guid = efi_global_variable_guid;
406 default_guid = true;
407 verbose = false;
408 value_on_memory = false;
409 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
410 if (!strcmp(argv[0], "-guid")) {
411 if (argc == 1)
412 return CMD_RET_USAGE;
413
414 argc--;
415 argv++;
416 if (uuid_str_to_bin(argv[0], guid.b,
417 UUID_STR_FORMAT_GUID)) {
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +0200418 return CMD_RET_USAGE;
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900419 }
420 default_guid = false;
421 } else if (!strcmp(argv[0], "-bs")) {
422 attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
423 } else if (!strcmp(argv[0], "-rt")) {
424 attributes |= EFI_VARIABLE_RUNTIME_ACCESS;
425 } else if (!strcmp(argv[0], "-nv")) {
426 attributes |= EFI_VARIABLE_NON_VOLATILE;
AKASHI Takahirobd7defa2020-04-14 11:51:47 +0900427 } else if (!strcmp(argv[0], "-at")) {
428 attributes |=
429 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900430 } else if (!strcmp(argv[0], "-a")) {
431 attributes |= EFI_VARIABLE_APPEND_WRITE;
432 } else if (!strcmp(argv[0], "-i")) {
433 /* data comes from memory */
434 if (argc == 1)
435 return CMD_RET_USAGE;
436
437 argc--;
438 argv++;
Simon Glass3ff49ec2021-07-24 09:03:29 -0600439 addr = hextoul(argv[0], &ep);
Heinrich Schuchardtfa11c862020-08-24 08:27:49 +0200440 if (*ep != ':')
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900441 return CMD_RET_USAGE;
442
AKASHI Takahiro7ffa17a2020-04-14 11:51:46 +0900443 /* 0 should be allowed for delete */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600444 size = hextoul(++ep, NULL);
AKASHI Takahiro7ffa17a2020-04-14 11:51:46 +0900445
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900446 value_on_memory = true;
447 } else if (!strcmp(argv[0], "-v")) {
448 verbose = true;
449 } else {
450 return CMD_RET_USAGE;
451 }
AKASHI Takahiro26105762019-06-04 15:52:11 +0900452 }
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900453 if (!argc)
454 return CMD_RET_USAGE;
AKASHI Takahiro26105762019-06-04 15:52:11 +0900455
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900456 var_name = argv[0];
AKASHI Takahiro7ffa17a2020-04-14 11:51:46 +0900457 if (default_guid) {
458 if (!strcmp(var_name, "db") || !strcmp(var_name, "dbx") ||
459 !strcmp(var_name, "dbt"))
460 guid = efi_guid_image_security_database;
461 else
462 guid = efi_global_variable_guid;
463 }
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900464
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900465 if (verbose) {
Heinrich Schuchardt1bebcb62022-01-16 14:53:37 +0100466 printf("GUID: %pUl (%pUs)\n", &guid, &guid);
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900467 printf("Attributes: 0x%x\n", attributes);
468 }
469
470 /* for value */
471 if (value_on_memory)
472 value = map_sysmem(addr, 0);
473 else if (argc > 1)
474 for (argc--, argv++; argc > 0; argc--, argv++)
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900475 if (append_value(&value, &size, argv[0]) < 0) {
AKASHI Takahiro9054df12019-05-28 09:00:35 +0900476 printf("## Failed to process an argument, %s\n",
477 argv[0]);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900478 ret = CMD_RET_FAILURE;
479 goto out;
480 }
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900481
482 if (size && verbose) {
483 printf("Value:\n");
484 print_hex_dump(" ", DUMP_PREFIX_OFFSET,
485 16, 1, value, size, true);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900486 }
487
Heinrich Schuchardtdb9361b2022-10-06 06:52:51 +0200488 var_name16 = efi_convert_string(var_name);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900489 if (!var_name16) {
AKASHI Takahiro9054df12019-05-28 09:00:35 +0900490 printf("## Out of memory\n");
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900491 ret = CMD_RET_FAILURE;
492 goto out;
493 }
Heinrich Schuchardt70035652020-06-24 19:55:13 +0200494 ret = efi_set_variable_int(var_name16, &guid, attributes, size, value,
495 true);
Heinrich Schuchardtdb9361b2022-10-06 06:52:51 +0200496 free(var_name16);
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900497 unmap_sysmem(value);
AKASHI Takahiro9054df12019-05-28 09:00:35 +0900498 if (ret == EFI_SUCCESS) {
499 ret = CMD_RET_SUCCESS;
500 } else {
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900501 const char *msg;
502
503 switch (ret) {
504 case EFI_NOT_FOUND:
505 msg = " (not found)";
506 break;
507 case EFI_WRITE_PROTECTED:
508 msg = " (read only)";
509 break;
510 case EFI_INVALID_PARAMETER:
511 msg = " (invalid parameter)";
512 break;
513 case EFI_SECURITY_VIOLATION:
514 msg = " (validation failed)";
515 break;
516 case EFI_OUT_OF_RESOURCES:
517 msg = " (out of memory)";
518 break;
519 default:
520 msg = "";
521 break;
522 }
523 printf("## Failed to set EFI variable%s\n", msg);
AKASHI Takahiro9054df12019-05-28 09:00:35 +0900524 ret = CMD_RET_FAILURE;
525 }
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900526out:
AKASHI Takahiro2e952122019-10-24 15:17:13 +0900527 if (value_on_memory)
528 unmap_sysmem(value);
529 else
530 free(value);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900531
532 return ret;
533}