]> git.ipfire.org Git - thirdparty/u-boot.git/blame - cmd/nvedit_efi.c
arm: mvebu: turris_omnia: update defconfig
[thirdparty/u-boot.git] / cmd / nvedit_efi.c
CommitLineData
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>
12#include <exports.h>
13#include <hexdump.h>
14#include <malloc.h>
15#include <linux/kernel.h>
16
17/*
18 * From efi_variable.c,
19 *
20 * Mapping between UEFI variables and u-boot variables:
21 *
22 * efi_$guid_$varname = {attributes}(type)value
23 */
24
25static const struct {
26 u32 mask;
27 char *text;
28} efi_var_attrs[] = {
29 {EFI_VARIABLE_NON_VOLATILE, "NV"},
30 {EFI_VARIABLE_BOOTSERVICE_ACCESS, "BS"},
31 {EFI_VARIABLE_RUNTIME_ACCESS, "RT"},
32 {EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, "AW"},
33 {EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, "AT"},
34};
35
36/**
37 * efi_dump_single_var() - show information about a UEFI variable
38 *
39 * @name: Name of the variable
40 * @guid: Vendor GUID
41 *
42 * Show information encoded in one UEFI variable
43 */
44static void efi_dump_single_var(u16 *name, efi_guid_t *guid)
45{
46 u32 attributes;
47 u8 *data;
48 efi_uintn_t size;
49 int count, i;
50 efi_status_t ret;
51
52 data = NULL;
53 size = 0;
54 ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size, data));
55 if (ret == EFI_BUFFER_TOO_SMALL) {
56 data = malloc(size);
57 if (!data)
58 goto out;
59
60 ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size,
61 data));
62 }
63 if (ret == EFI_NOT_FOUND) {
64 printf("Error: \"%ls\" not defined\n", name);
65 goto out;
66 }
67 if (ret != EFI_SUCCESS)
68 goto out;
69
70 printf("%ls:", name);
71 for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++)
72 if (attributes & efi_var_attrs[i].mask) {
73 if (count)
74 putc('|');
75 else
76 putc(' ');
77 count++;
78 puts(efi_var_attrs[i].text);
79 }
80 printf(", DataSize = 0x%zx\n", size);
81 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, data, size, true);
82
49d81fdf
AT
83out:
84 free(data);
85}
86
87/**
88 * efi_dump_vars() - show information about named UEFI variables
89 *
90 * @argc: Number of arguments (variables)
91 * @argv: Argument (variable name) array
92 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
93 *
94 * Show information encoded in named UEFI variables
95 */
96static int efi_dump_vars(int argc, char * const argv[])
97{
98 u16 *var_name16, *p;
99 efi_uintn_t buf_size, size;
100
101 buf_size = 128;
102 var_name16 = malloc(buf_size);
103 if (!var_name16)
104 return CMD_RET_FAILURE;
105
106 for (; argc > 0; argc--, argv++) {
107 size = (utf8_utf16_strlen(argv[0]) + 1) * sizeof(u16);
108 if (buf_size < size) {
109 buf_size = size;
110 p = realloc(var_name16, buf_size);
111 if (!p) {
112 free(var_name16);
113 return CMD_RET_FAILURE;
114 }
115 var_name16 = p;
116 }
117
118 p = var_name16;
119 utf8_utf16_strcpy(&p, argv[0]);
120
121 efi_dump_single_var(var_name16,
122 (efi_guid_t *)&efi_global_variable_guid);
123 }
124
125 free(var_name16);
126
127 return CMD_RET_SUCCESS;
128}
129
130/**
131 * efi_dump_vars() - show information about all the UEFI variables
132 *
133 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
134 *
135 * Show information encoded in all the UEFI variables
136 */
137static int efi_dump_var_all(void)
138{
139 u16 *var_name16, *p;
140 efi_uintn_t buf_size, size;
141 efi_guid_t guid;
142 efi_status_t ret;
143
144 buf_size = 128;
145 var_name16 = malloc(buf_size);
146 if (!var_name16)
147 return CMD_RET_FAILURE;
148
149 var_name16[0] = 0;
150 for (;;) {
151 size = buf_size;
152 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
153 &guid));
154 if (ret == EFI_NOT_FOUND)
155 break;
156 if (ret == EFI_BUFFER_TOO_SMALL) {
157 buf_size = size;
158 p = realloc(var_name16, buf_size);
159 if (!p) {
160 free(var_name16);
161 return CMD_RET_FAILURE;
162 }
163 var_name16 = p;
164 ret = EFI_CALL(efi_get_next_variable_name(&size,
165 var_name16,
166 &guid));
167 }
168 if (ret != EFI_SUCCESS) {
169 free(var_name16);
170 return CMD_RET_FAILURE;
171 }
172
173 efi_dump_single_var(var_name16, &guid);
174 }
175
176 free(var_name16);
177
178 return CMD_RET_SUCCESS;
179}
180
181/**
182 * do_env_print_efi() - show information about UEFI variables
183 *
184 * @cmdtp: Command table
185 * @flag: Command flag
186 * @argc: Number of arguments
187 * @argv: Argument array
188 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
189 *
190 * This function is for "env print -e" or "printenv -e" command:
191 * => env print -e [var [...]]
192 * If one or more variable names are specified, show information
193 * named UEFI variables, otherwise show all the UEFI variables.
194 */
195int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
196{
197 efi_status_t ret;
198
199 /* Initialize EFI drivers */
200 ret = efi_init_obj_list();
201 if (ret != EFI_SUCCESS) {
202 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
203 ret & ~EFI_ERROR_MASK);
204 return CMD_RET_FAILURE;
205 }
206
207 if (argc > 1)
208 /* show specified UEFI variables */
209 return efi_dump_vars(--argc, ++argv);
210
211 /* enumerate and show all UEFI variables */
212 return efi_dump_var_all();
213}
214
215/**
216 * append_value() - encode UEFI variable's value
217 * @bufp: Buffer of encoded UEFI variable's value
218 * @sizep: Size of buffer
219 * @data: data to be encoded into the value
220 * Return: 0 on success, -1 otherwise
221 *
222 * Interpret a given data string and append it to buffer.
223 * Buffer will be realloc'ed if necessary.
224 *
225 * Currently supported formats are:
226 * =0x0123...: Hexadecimal number
227 * =H0123...: Hexadecimal-byte array
228 * ="...", =S"..." or <string>:
229 * String
230 */
231static int append_value(char **bufp, size_t *sizep, char *data)
232{
233 char *tmp_buf = NULL, *new_buf = NULL, *value;
234 unsigned long len = 0;
235
236 if (!strncmp(data, "=0x", 2)) { /* hexadecimal number */
237 union {
238 u8 u8;
239 u16 u16;
240 u32 u32;
241 u64 u64;
242 } tmp_data;
243 unsigned long hex_value;
244 void *hex_ptr;
245
246 data += 3;
247 len = strlen(data);
248 if ((len & 0x1)) /* not multiple of two */
249 return -1;
250
251 len /= 2;
252 if (len > 8)
253 return -1;
254 else if (len > 4)
255 len = 8;
256 else if (len > 2)
257 len = 4;
258
259 /* convert hex hexadecimal number */
260 if (strict_strtoul(data, 16, &hex_value) < 0)
261 return -1;
262
263 tmp_buf = malloc(len);
264 if (!tmp_buf)
265 return -1;
266
267 if (len == 1) {
268 tmp_data.u8 = hex_value;
269 hex_ptr = &tmp_data.u8;
270 } else if (len == 2) {
271 tmp_data.u16 = hex_value;
272 hex_ptr = &tmp_data.u16;
273 } else if (len == 4) {
274 tmp_data.u32 = hex_value;
275 hex_ptr = &tmp_data.u32;
276 } else {
277 tmp_data.u64 = hex_value;
278 hex_ptr = &tmp_data.u64;
279 }
280 memcpy(tmp_buf, hex_ptr, len);
281 value = tmp_buf;
282
283 } else if (!strncmp(data, "=H", 2)) { /* hexadecimal-byte array */
284 data += 2;
285 len = strlen(data);
286 if (len & 0x1) /* not multiple of two */
287 return -1;
288
289 len /= 2;
290 tmp_buf = malloc(len);
291 if (!tmp_buf)
292 return -1;
293
45203e0c
HS
294 if (hex2bin((u8 *)tmp_buf, data, len) < 0) {
295 printf("Error: illegal hexadecimal string\n");
296 free(tmp_buf);
49d81fdf 297 return -1;
45203e0c 298 }
49d81fdf
AT
299
300 value = tmp_buf;
301 } else { /* string */
302 if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) {
303 if (data[1] == '"')
304 data += 2;
305 else
306 data += 3;
307 value = data;
308 len = strlen(data) - 1;
309 if (data[len] != '"')
310 return -1;
311 } else {
312 value = data;
313 len = strlen(data);
314 }
315 }
316
317 new_buf = realloc(*bufp, *sizep + len);
318 if (!new_buf)
319 goto out;
320
321 memcpy(new_buf + *sizep, value, len);
322 *bufp = new_buf;
323 *sizep += len;
324
325out:
326 free(tmp_buf);
327
328 return 0;
329}
330
331/**
332 * do_env_print_efi() - set UEFI variable
333 *
334 * @cmdtp: Command table
335 * @flag: Command flag
336 * @argc: Number of arguments
337 * @argv: Argument array
338 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
339 *
340 * This function is for "env set -e" or "setenv -e" command:
341 * => env set -e var [value ...]]
342 * Encode values specified and set given UEFI variable.
343 * If no value is specified, delete the variable.
344 */
345int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
346{
347 char *var_name, *value = NULL;
348 efi_uintn_t size = 0;
349 u16 *var_name16 = NULL, *p;
350 size_t len;
351 efi_guid_t guid;
4b27a761 352 u32 attributes;
49d81fdf
AT
353 efi_status_t ret;
354
355 if (argc == 1)
356 return CMD_RET_USAGE;
357
358 /* Initialize EFI drivers */
359 ret = efi_init_obj_list();
360 if (ret != EFI_SUCCESS) {
361 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
362 ret & ~EFI_ERROR_MASK);
363 return CMD_RET_FAILURE;
364 }
365
4b27a761
AT
366 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
367 EFI_VARIABLE_RUNTIME_ACCESS;
368 if (!strcmp(argv[1], "-nv")) {
369 attributes |= EFI_VARIABLE_NON_VOLATILE;
370 argc--;
371 argv++;
372 if (argc == 1)
373 return CMD_RET_SUCCESS;
374 }
375
49d81fdf
AT
376 var_name = argv[1];
377 if (argc == 2) {
378 /* delete */
379 value = NULL;
380 size = 0;
381 } else { /* set */
382 argc -= 2;
383 argv += 2;
384
385 for ( ; argc > 0; argc--, argv++)
386 if (append_value(&value, &size, argv[0]) < 0) {
8190b4a3
AT
387 printf("## Failed to process an argument, %s\n",
388 argv[0]);
49d81fdf
AT
389 ret = CMD_RET_FAILURE;
390 goto out;
391 }
392 }
393
394 len = utf8_utf16_strnlen(var_name, strlen(var_name));
395 var_name16 = malloc((len + 1) * 2);
396 if (!var_name16) {
8190b4a3 397 printf("## Out of memory\n");
49d81fdf
AT
398 ret = CMD_RET_FAILURE;
399 goto out;
400 }
401 p = var_name16;
402 utf8_utf16_strncpy(&p, var_name, len + 1);
403
404 guid = efi_global_variable_guid;
4b27a761 405 ret = EFI_CALL(efi_set_variable(var_name16, &guid, attributes,
49d81fdf 406 size, value));
8190b4a3
AT
407 if (ret == EFI_SUCCESS) {
408 ret = CMD_RET_SUCCESS;
409 } else {
410 printf("## Failed to set EFI variable\n");
411 ret = CMD_RET_FAILURE;
412 }
49d81fdf
AT
413out:
414 free(value);
415 free(var_name16);
416
417 return ret;
418}