]>
Commit | Line | Data |
---|---|---|
49d81fdf AT |
1 | // 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> | |
9fb625ce | 12 | #include <env.h> |
49d81fdf AT |
13 | #include <exports.h> |
14 | #include <hexdump.h> | |
15 | #include <malloc.h> | |
051aa89f | 16 | #include <mapmem.h> |
ba06b3c5 | 17 | #include <uuid.h> |
49d81fdf AT |
18 | #include <linux/kernel.h> |
19 | ||
20 | /* | |
21 | * From efi_variable.c, | |
22 | * | |
23 | * Mapping between UEFI variables and u-boot variables: | |
24 | * | |
25 | * efi_$guid_$varname = {attributes}(type)value | |
26 | */ | |
27 | ||
28 | static const struct { | |
29 | u32 mask; | |
30 | char *text; | |
31 | } efi_var_attrs[] = { | |
32 | {EFI_VARIABLE_NON_VOLATILE, "NV"}, | |
33 | {EFI_VARIABLE_BOOTSERVICE_ACCESS, "BS"}, | |
34 | {EFI_VARIABLE_RUNTIME_ACCESS, "RT"}, | |
35 | {EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, "AW"}, | |
36 | {EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, "AT"}, | |
37 | }; | |
38 | ||
051aa89f AT |
39 | static const struct { |
40 | efi_guid_t guid; | |
41 | char *text; | |
42 | } efi_guid_text[] = { | |
43 | /* signature database */ | |
44 | {EFI_GLOBAL_VARIABLE_GUID, "EFI_GLOBAL_VARIABLE_GUID"}, | |
f757d045 AT |
45 | {EFI_IMAGE_SECURITY_DATABASE_GUID, "EFI_IMAGE_SECURITY_DATABASE_GUID"}, |
46 | /* certificate type */ | |
47 | {EFI_CERT_SHA256_GUID, "EFI_CERT_SHA256_GUID"}, | |
48 | {EFI_CERT_X509_GUID, "EFI_CERT_X509_GUID"}, | |
49 | {EFI_CERT_TYPE_PKCS7_GUID, "EFI_CERT_TYPE_PKCS7_GUID"}, | |
051aa89f AT |
50 | }; |
51 | ||
52 | /* "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */ | |
53 | static char unknown_guid[37]; | |
54 | ||
55 | /** | |
56 | * efi_guid_to_str() - convert guid to readable name | |
57 | * | |
58 | * @guid: GUID | |
59 | * Return: string for GUID | |
60 | * | |
61 | * convert guid to readable name | |
62 | */ | |
63 | static const char *efi_guid_to_str(const efi_guid_t *guid) | |
64 | { | |
65 | int i; | |
66 | ||
67 | for (i = 0; i < ARRAY_SIZE(efi_guid_text); i++) | |
68 | if (!guidcmp(guid, &efi_guid_text[i].guid)) | |
69 | return efi_guid_text[i].text; | |
70 | ||
71 | uuid_bin_to_str((unsigned char *)guid->b, unknown_guid, | |
72 | UUID_STR_FORMAT_GUID); | |
73 | ||
74 | return unknown_guid; | |
75 | } | |
76 | ||
49d81fdf AT |
77 | /** |
78 | * efi_dump_single_var() - show information about a UEFI variable | |
79 | * | |
80 | * @name: Name of the variable | |
81 | * @guid: Vendor GUID | |
051aa89f | 82 | * @verbose: if true, dump data |
49d81fdf AT |
83 | * |
84 | * Show information encoded in one UEFI variable | |
85 | */ | |
051aa89f | 86 | static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, bool verbose) |
49d81fdf AT |
87 | { |
88 | u32 attributes; | |
89 | u8 *data; | |
90 | efi_uintn_t size; | |
91 | int count, i; | |
92 | efi_status_t ret; | |
93 | ||
94 | data = NULL; | |
95 | size = 0; | |
96 | ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size, data)); | |
97 | if (ret == EFI_BUFFER_TOO_SMALL) { | |
98 | data = malloc(size); | |
99 | if (!data) | |
100 | goto out; | |
101 | ||
102 | ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size, | |
103 | data)); | |
104 | } | |
105 | if (ret == EFI_NOT_FOUND) { | |
106 | printf("Error: \"%ls\" not defined\n", name); | |
107 | goto out; | |
108 | } | |
109 | if (ret != EFI_SUCCESS) | |
110 | goto out; | |
111 | ||
051aa89f | 112 | printf("%ls:\n %s:", name, efi_guid_to_str(guid)); |
49d81fdf AT |
113 | for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++) |
114 | if (attributes & efi_var_attrs[i].mask) { | |
115 | if (count) | |
116 | putc('|'); | |
117 | else | |
118 | putc(' '); | |
119 | count++; | |
120 | puts(efi_var_attrs[i].text); | |
121 | } | |
122 | printf(", DataSize = 0x%zx\n", size); | |
051aa89f AT |
123 | if (verbose) |
124 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, | |
125 | data, size, true); | |
49d81fdf | 126 | |
49d81fdf AT |
127 | out: |
128 | free(data); | |
129 | } | |
130 | ||
131 | /** | |
132 | * efi_dump_vars() - show information about named UEFI variables | |
133 | * | |
134 | * @argc: Number of arguments (variables) | |
135 | * @argv: Argument (variable name) array | |
051aa89f | 136 | * @verbose: if true, dump data |
49d81fdf AT |
137 | * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE |
138 | * | |
139 | * Show information encoded in named UEFI variables | |
140 | */ | |
09140113 | 141 | static int efi_dump_vars(int argc, char *const argv[], |
051aa89f | 142 | const efi_guid_t *guid, bool verbose) |
49d81fdf AT |
143 | { |
144 | u16 *var_name16, *p; | |
145 | efi_uintn_t buf_size, size; | |
146 | ||
147 | buf_size = 128; | |
148 | var_name16 = malloc(buf_size); | |
149 | if (!var_name16) | |
150 | return CMD_RET_FAILURE; | |
151 | ||
152 | for (; argc > 0; argc--, argv++) { | |
153 | size = (utf8_utf16_strlen(argv[0]) + 1) * sizeof(u16); | |
154 | if (buf_size < size) { | |
155 | buf_size = size; | |
156 | p = realloc(var_name16, buf_size); | |
157 | if (!p) { | |
158 | free(var_name16); | |
159 | return CMD_RET_FAILURE; | |
160 | } | |
161 | var_name16 = p; | |
162 | } | |
163 | ||
164 | p = var_name16; | |
165 | utf8_utf16_strcpy(&p, argv[0]); | |
166 | ||
051aa89f | 167 | efi_dump_single_var(var_name16, guid, verbose); |
49d81fdf AT |
168 | } |
169 | ||
170 | free(var_name16); | |
171 | ||
172 | return CMD_RET_SUCCESS; | |
173 | } | |
174 | ||
09140113 | 175 | static bool match_name(int argc, char *const argv[], u16 *var_name16) |
051aa89f AT |
176 | { |
177 | char *buf, *p; | |
178 | size_t buflen; | |
179 | int i; | |
180 | bool result = false; | |
181 | ||
182 | buflen = utf16_utf8_strlen(var_name16) + 1; | |
183 | buf = calloc(1, buflen); | |
184 | if (!buf) | |
185 | return result; | |
186 | ||
187 | p = buf; | |
188 | utf16_utf8_strcpy(&p, var_name16); | |
189 | ||
190 | for (i = 0; i < argc; argc--, argv++) { | |
191 | if (!strcmp(buf, argv[i])) { | |
192 | result = true; | |
193 | goto out; | |
194 | } | |
195 | } | |
196 | ||
197 | out: | |
198 | free(buf); | |
199 | ||
200 | return result; | |
201 | } | |
202 | ||
49d81fdf | 203 | /** |
051aa89f | 204 | * efi_dump_var_all() - show information about all the UEFI variables |
49d81fdf | 205 | * |
051aa89f AT |
206 | * @argc: Number of arguments (variables) |
207 | * @argv: Argument (variable name) array | |
208 | * @verbose: if true, dump data | |
49d81fdf AT |
209 | * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE |
210 | * | |
211 | * Show information encoded in all the UEFI variables | |
212 | */ | |
09140113 | 213 | static int efi_dump_var_all(int argc, char *const argv[], |
051aa89f | 214 | const efi_guid_t *guid_p, bool verbose) |
49d81fdf AT |
215 | { |
216 | u16 *var_name16, *p; | |
217 | efi_uintn_t buf_size, size; | |
218 | efi_guid_t guid; | |
219 | efi_status_t ret; | |
220 | ||
051aa89f AT |
221 | if (argc && guid_p) |
222 | /* simplified case */ | |
223 | return efi_dump_vars(argc, argv, guid_p, verbose); | |
224 | ||
49d81fdf AT |
225 | buf_size = 128; |
226 | var_name16 = malloc(buf_size); | |
227 | if (!var_name16) | |
228 | return CMD_RET_FAILURE; | |
229 | ||
230 | var_name16[0] = 0; | |
231 | for (;;) { | |
232 | size = buf_size; | |
233 | ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16, | |
234 | &guid)); | |
235 | if (ret == EFI_NOT_FOUND) | |
236 | break; | |
237 | if (ret == EFI_BUFFER_TOO_SMALL) { | |
238 | buf_size = size; | |
239 | p = realloc(var_name16, buf_size); | |
240 | if (!p) { | |
241 | free(var_name16); | |
242 | return CMD_RET_FAILURE; | |
243 | } | |
244 | var_name16 = p; | |
245 | ret = EFI_CALL(efi_get_next_variable_name(&size, | |
246 | var_name16, | |
247 | &guid)); | |
248 | } | |
249 | if (ret != EFI_SUCCESS) { | |
250 | free(var_name16); | |
251 | return CMD_RET_FAILURE; | |
252 | } | |
253 | ||
051aa89f AT |
254 | if ((!guid_p || !guidcmp(guid_p, &guid)) && |
255 | (!argc || match_name(argc, argv, var_name16))) | |
256 | efi_dump_single_var(var_name16, &guid, verbose); | |
49d81fdf AT |
257 | } |
258 | ||
259 | free(var_name16); | |
260 | ||
261 | return CMD_RET_SUCCESS; | |
262 | } | |
263 | ||
264 | /** | |
265 | * do_env_print_efi() - show information about UEFI variables | |
266 | * | |
267 | * @cmdtp: Command table | |
268 | * @flag: Command flag | |
269 | * @argc: Number of arguments | |
270 | * @argv: Argument array | |
271 | * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE | |
272 | * | |
273 | * This function is for "env print -e" or "printenv -e" command: | |
051aa89f | 274 | * => env print -e [-n] [-guid <guid> | -all] [var [...]] |
49d81fdf AT |
275 | * If one or more variable names are specified, show information |
276 | * named UEFI variables, otherwise show all the UEFI variables. | |
277 | */ | |
09140113 SG |
278 | int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc, |
279 | char *const argv[]) | |
49d81fdf | 280 | { |
051aa89f AT |
281 | efi_guid_t guid; |
282 | const efi_guid_t *guid_p; | |
283 | bool default_guid, guid_any, verbose; | |
49d81fdf AT |
284 | efi_status_t ret; |
285 | ||
286 | /* Initialize EFI drivers */ | |
287 | ret = efi_init_obj_list(); | |
288 | if (ret != EFI_SUCCESS) { | |
289 | printf("Error: Cannot initialize UEFI sub-system, r = %lu\n", | |
290 | ret & ~EFI_ERROR_MASK); | |
291 | return CMD_RET_FAILURE; | |
292 | } | |
293 | ||
051aa89f AT |
294 | default_guid = true; |
295 | guid_any = false; | |
296 | verbose = true; | |
297 | for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) { | |
298 | if (!strcmp(argv[0], "-guid")) { | |
299 | if (argc == 1) | |
300 | return CMD_RET_USAGE; | |
301 | ||
302 | /* -a already specified */ | |
9900e462 | 303 | if (!default_guid && guid_any) |
051aa89f AT |
304 | return CMD_RET_USAGE; |
305 | ||
306 | argc--; | |
307 | argv++; | |
308 | if (uuid_str_to_bin(argv[0], guid.b, | |
309 | UUID_STR_FORMAT_GUID)) | |
310 | return CMD_RET_USAGE; | |
311 | default_guid = false; | |
312 | } else if (!strcmp(argv[0], "-all")) { | |
313 | /* -guid already specified */ | |
314 | if (!default_guid && !guid_any) | |
315 | return CMD_RET_USAGE; | |
316 | ||
317 | guid_any = true; | |
318 | default_guid = false; | |
319 | } else if (!strcmp(argv[0], "-n")) { | |
320 | verbose = false; | |
321 | } else { | |
322 | return CMD_RET_USAGE; | |
323 | } | |
324 | } | |
325 | ||
326 | if (guid_any) | |
327 | guid_p = NULL; | |
328 | else if (default_guid) | |
329 | guid_p = &efi_global_variable_guid; | |
330 | else | |
331 | guid_p = (const efi_guid_t *)guid.b; | |
49d81fdf AT |
332 | |
333 | /* enumerate and show all UEFI variables */ | |
051aa89f | 334 | return efi_dump_var_all(argc, argv, guid_p, verbose); |
49d81fdf AT |
335 | } |
336 | ||
337 | /** | |
338 | * append_value() - encode UEFI variable's value | |
339 | * @bufp: Buffer of encoded UEFI variable's value | |
340 | * @sizep: Size of buffer | |
341 | * @data: data to be encoded into the value | |
342 | * Return: 0 on success, -1 otherwise | |
343 | * | |
344 | * Interpret a given data string and append it to buffer. | |
345 | * Buffer will be realloc'ed if necessary. | |
346 | * | |
347 | * Currently supported formats are: | |
348 | * =0x0123...: Hexadecimal number | |
349 | * =H0123...: Hexadecimal-byte array | |
350 | * ="...", =S"..." or <string>: | |
351 | * String | |
352 | */ | |
353 | static int append_value(char **bufp, size_t *sizep, char *data) | |
354 | { | |
355 | char *tmp_buf = NULL, *new_buf = NULL, *value; | |
356 | unsigned long len = 0; | |
357 | ||
358 | if (!strncmp(data, "=0x", 2)) { /* hexadecimal number */ | |
359 | union { | |
360 | u8 u8; | |
361 | u16 u16; | |
362 | u32 u32; | |
363 | u64 u64; | |
364 | } tmp_data; | |
365 | unsigned long hex_value; | |
366 | void *hex_ptr; | |
367 | ||
368 | data += 3; | |
369 | len = strlen(data); | |
370 | if ((len & 0x1)) /* not multiple of two */ | |
371 | return -1; | |
372 | ||
373 | len /= 2; | |
374 | if (len > 8) | |
375 | return -1; | |
376 | else if (len > 4) | |
377 | len = 8; | |
378 | else if (len > 2) | |
379 | len = 4; | |
380 | ||
381 | /* convert hex hexadecimal number */ | |
382 | if (strict_strtoul(data, 16, &hex_value) < 0) | |
383 | return -1; | |
384 | ||
385 | tmp_buf = malloc(len); | |
386 | if (!tmp_buf) | |
387 | return -1; | |
388 | ||
389 | if (len == 1) { | |
390 | tmp_data.u8 = hex_value; | |
391 | hex_ptr = &tmp_data.u8; | |
392 | } else if (len == 2) { | |
393 | tmp_data.u16 = hex_value; | |
394 | hex_ptr = &tmp_data.u16; | |
395 | } else if (len == 4) { | |
396 | tmp_data.u32 = hex_value; | |
397 | hex_ptr = &tmp_data.u32; | |
398 | } else { | |
399 | tmp_data.u64 = hex_value; | |
400 | hex_ptr = &tmp_data.u64; | |
401 | } | |
402 | memcpy(tmp_buf, hex_ptr, len); | |
403 | value = tmp_buf; | |
404 | ||
405 | } else if (!strncmp(data, "=H", 2)) { /* hexadecimal-byte array */ | |
406 | data += 2; | |
407 | len = strlen(data); | |
408 | if (len & 0x1) /* not multiple of two */ | |
409 | return -1; | |
410 | ||
411 | len /= 2; | |
412 | tmp_buf = malloc(len); | |
413 | if (!tmp_buf) | |
414 | return -1; | |
415 | ||
45203e0c HS |
416 | if (hex2bin((u8 *)tmp_buf, data, len) < 0) { |
417 | printf("Error: illegal hexadecimal string\n"); | |
418 | free(tmp_buf); | |
49d81fdf | 419 | return -1; |
45203e0c | 420 | } |
49d81fdf AT |
421 | |
422 | value = tmp_buf; | |
423 | } else { /* string */ | |
424 | if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) { | |
425 | if (data[1] == '"') | |
426 | data += 2; | |
427 | else | |
428 | data += 3; | |
429 | value = data; | |
430 | len = strlen(data) - 1; | |
431 | if (data[len] != '"') | |
432 | return -1; | |
433 | } else { | |
434 | value = data; | |
435 | len = strlen(data); | |
436 | } | |
437 | } | |
438 | ||
439 | new_buf = realloc(*bufp, *sizep + len); | |
440 | if (!new_buf) | |
441 | goto out; | |
442 | ||
443 | memcpy(new_buf + *sizep, value, len); | |
444 | *bufp = new_buf; | |
445 | *sizep += len; | |
446 | ||
447 | out: | |
448 | free(tmp_buf); | |
449 | ||
450 | return 0; | |
451 | } | |
452 | ||
453 | /** | |
6810caf8 | 454 | * do_env_set_efi() - set UEFI variable |
49d81fdf AT |
455 | * |
456 | * @cmdtp: Command table | |
457 | * @flag: Command flag | |
458 | * @argc: Number of arguments | |
459 | * @argv: Argument array | |
460 | * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE | |
461 | * | |
462 | * This function is for "env set -e" or "setenv -e" command: | |
e50e2878 | 463 | * => env set -e [-guid guid][-nv][-bs][-rt][-at][-a][-v] |
051aa89f AT |
464 | * [-i address,size] var, or |
465 | * var [value ...] | |
49d81fdf AT |
466 | * Encode values specified and set given UEFI variable. |
467 | * If no value is specified, delete the variable. | |
468 | */ | |
09140113 SG |
469 | int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc, |
470 | char *const argv[]) | |
49d81fdf | 471 | { |
051aa89f AT |
472 | char *var_name, *value, *ep; |
473 | ulong addr; | |
474 | efi_uintn_t size; | |
49d81fdf | 475 | efi_guid_t guid; |
4b27a761 | 476 | u32 attributes; |
051aa89f AT |
477 | bool default_guid, verbose, value_on_memory; |
478 | u16 *var_name16 = NULL, *p; | |
479 | size_t len; | |
49d81fdf AT |
480 | efi_status_t ret; |
481 | ||
482 | if (argc == 1) | |
483 | return CMD_RET_USAGE; | |
484 | ||
485 | /* Initialize EFI drivers */ | |
486 | ret = efi_init_obj_list(); | |
487 | if (ret != EFI_SUCCESS) { | |
488 | printf("Error: Cannot initialize UEFI sub-system, r = %lu\n", | |
489 | ret & ~EFI_ERROR_MASK); | |
490 | return CMD_RET_FAILURE; | |
491 | } | |
492 | ||
051aa89f AT |
493 | /* |
494 | * attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | | |
495 | * EFI_VARIABLE_RUNTIME_ACCESS; | |
496 | */ | |
497 | value = NULL; | |
498 | size = 0; | |
499 | attributes = 0; | |
500 | guid = efi_global_variable_guid; | |
501 | default_guid = true; | |
502 | verbose = false; | |
503 | value_on_memory = false; | |
504 | for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) { | |
505 | if (!strcmp(argv[0], "-guid")) { | |
506 | if (argc == 1) | |
507 | return CMD_RET_USAGE; | |
508 | ||
509 | argc--; | |
510 | argv++; | |
511 | if (uuid_str_to_bin(argv[0], guid.b, | |
512 | UUID_STR_FORMAT_GUID)) { | |
513 | printf("## Guid not specified or in XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX format\n"); | |
514 | return CMD_RET_FAILURE; | |
515 | } | |
516 | default_guid = false; | |
517 | } else if (!strcmp(argv[0], "-bs")) { | |
518 | attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS; | |
519 | } else if (!strcmp(argv[0], "-rt")) { | |
520 | attributes |= EFI_VARIABLE_RUNTIME_ACCESS; | |
521 | } else if (!strcmp(argv[0], "-nv")) { | |
522 | attributes |= EFI_VARIABLE_NON_VOLATILE; | |
e50e2878 AT |
523 | } else if (!strcmp(argv[0], "-at")) { |
524 | attributes |= | |
525 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; | |
051aa89f AT |
526 | } else if (!strcmp(argv[0], "-a")) { |
527 | attributes |= EFI_VARIABLE_APPEND_WRITE; | |
528 | } else if (!strcmp(argv[0], "-i")) { | |
529 | /* data comes from memory */ | |
530 | if (argc == 1) | |
531 | return CMD_RET_USAGE; | |
532 | ||
533 | argc--; | |
534 | argv++; | |
535 | addr = simple_strtoul(argv[0], &ep, 16); | |
536 | if (*ep != ',') | |
537 | return CMD_RET_USAGE; | |
538 | ||
f757d045 | 539 | /* 0 should be allowed for delete */ |
051aa89f | 540 | size = simple_strtoul(++ep, NULL, 16); |
f757d045 | 541 | |
051aa89f AT |
542 | value_on_memory = true; |
543 | } else if (!strcmp(argv[0], "-v")) { | |
544 | verbose = true; | |
545 | } else { | |
546 | return CMD_RET_USAGE; | |
547 | } | |
4b27a761 | 548 | } |
051aa89f AT |
549 | if (!argc) |
550 | return CMD_RET_USAGE; | |
4b27a761 | 551 | |
051aa89f | 552 | var_name = argv[0]; |
f757d045 AT |
553 | if (default_guid) { |
554 | if (!strcmp(var_name, "db") || !strcmp(var_name, "dbx") || | |
555 | !strcmp(var_name, "dbt")) | |
556 | guid = efi_guid_image_security_database; | |
557 | else | |
558 | guid = efi_global_variable_guid; | |
559 | } | |
051aa89f AT |
560 | |
561 | if (verbose) { | |
562 | printf("GUID: %s\n", efi_guid_to_str((const efi_guid_t *) | |
563 | &guid)); | |
564 | printf("Attributes: 0x%x\n", attributes); | |
565 | } | |
49d81fdf | 566 | |
051aa89f AT |
567 | /* for value */ |
568 | if (value_on_memory) | |
569 | value = map_sysmem(addr, 0); | |
570 | else if (argc > 1) | |
571 | for (argc--, argv++; argc > 0; argc--, argv++) | |
49d81fdf | 572 | if (append_value(&value, &size, argv[0]) < 0) { |
8190b4a3 AT |
573 | printf("## Failed to process an argument, %s\n", |
574 | argv[0]); | |
49d81fdf AT |
575 | ret = CMD_RET_FAILURE; |
576 | goto out; | |
577 | } | |
051aa89f AT |
578 | |
579 | if (size && verbose) { | |
580 | printf("Value:\n"); | |
581 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, | |
582 | 16, 1, value, size, true); | |
49d81fdf AT |
583 | } |
584 | ||
585 | len = utf8_utf16_strnlen(var_name, strlen(var_name)); | |
586 | var_name16 = malloc((len + 1) * 2); | |
587 | if (!var_name16) { | |
8190b4a3 | 588 | printf("## Out of memory\n"); |
49d81fdf AT |
589 | ret = CMD_RET_FAILURE; |
590 | goto out; | |
591 | } | |
592 | p = var_name16; | |
593 | utf8_utf16_strncpy(&p, var_name, len + 1); | |
594 | ||
4b27a761 | 595 | ret = EFI_CALL(efi_set_variable(var_name16, &guid, attributes, |
49d81fdf | 596 | size, value)); |
051aa89f | 597 | unmap_sysmem(value); |
8190b4a3 AT |
598 | if (ret == EFI_SUCCESS) { |
599 | ret = CMD_RET_SUCCESS; | |
600 | } else { | |
051aa89f AT |
601 | const char *msg; |
602 | ||
603 | switch (ret) { | |
604 | case EFI_NOT_FOUND: | |
605 | msg = " (not found)"; | |
606 | break; | |
607 | case EFI_WRITE_PROTECTED: | |
608 | msg = " (read only)"; | |
609 | break; | |
610 | case EFI_INVALID_PARAMETER: | |
611 | msg = " (invalid parameter)"; | |
612 | break; | |
613 | case EFI_SECURITY_VIOLATION: | |
614 | msg = " (validation failed)"; | |
615 | break; | |
616 | case EFI_OUT_OF_RESOURCES: | |
617 | msg = " (out of memory)"; | |
618 | break; | |
619 | default: | |
620 | msg = ""; | |
621 | break; | |
622 | } | |
623 | printf("## Failed to set EFI variable%s\n", msg); | |
8190b4a3 AT |
624 | ret = CMD_RET_FAILURE; |
625 | } | |
49d81fdf | 626 | out: |
051aa89f AT |
627 | if (value_on_memory) |
628 | unmap_sysmem(value); | |
629 | else | |
630 | free(value); | |
49d81fdf AT |
631 | free(var_name16); |
632 | ||
633 | return ret; | |
634 | } |