blob: e65b38dbf399b9fbb22969de561b3abb2d215d04 [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>
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
AKASHI Takahiro117e68a2019-02-25 15:54:36 +090083out:
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
294 if (hex2bin((u8 *)tmp_buf, data, len) < 0)
295 return -1;
296
297 value = tmp_buf;
298 } else { /* string */
299 if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) {
300 if (data[1] == '"')
301 data += 2;
302 else
303 data += 3;
304 value = data;
305 len = strlen(data) - 1;
306 if (data[len] != '"')
307 return -1;
308 } else {
309 value = data;
310 len = strlen(data);
311 }
312 }
313
314 new_buf = realloc(*bufp, *sizep + len);
315 if (!new_buf)
316 goto out;
317
318 memcpy(new_buf + *sizep, value, len);
319 *bufp = new_buf;
320 *sizep += len;
321
322out:
323 free(tmp_buf);
324
325 return 0;
326}
327
328/**
329 * do_env_print_efi() - set UEFI variable
330 *
331 * @cmdtp: Command table
332 * @flag: Command flag
333 * @argc: Number of arguments
334 * @argv: Argument array
335 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
336 *
337 * This function is for "env set -e" or "setenv -e" command:
338 * => env set -e var [value ...]]
339 * Encode values specified and set given UEFI variable.
340 * If no value is specified, delete the variable.
341 */
342int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
343{
344 char *var_name, *value = NULL;
345 efi_uintn_t size = 0;
346 u16 *var_name16 = NULL, *p;
347 size_t len;
348 efi_guid_t guid;
349 efi_status_t ret;
350
351 if (argc == 1)
352 return CMD_RET_USAGE;
353
354 /* Initialize EFI drivers */
355 ret = efi_init_obj_list();
356 if (ret != EFI_SUCCESS) {
357 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
358 ret & ~EFI_ERROR_MASK);
359 return CMD_RET_FAILURE;
360 }
361
362 var_name = argv[1];
363 if (argc == 2) {
364 /* delete */
365 value = NULL;
366 size = 0;
367 } else { /* set */
368 argc -= 2;
369 argv += 2;
370
371 for ( ; argc > 0; argc--, argv++)
372 if (append_value(&value, &size, argv[0]) < 0) {
373 ret = CMD_RET_FAILURE;
374 goto out;
375 }
376 }
377
378 len = utf8_utf16_strnlen(var_name, strlen(var_name));
379 var_name16 = malloc((len + 1) * 2);
380 if (!var_name16) {
381 ret = CMD_RET_FAILURE;
382 goto out;
383 }
384 p = var_name16;
385 utf8_utf16_strncpy(&p, var_name, len + 1);
386
387 guid = efi_global_variable_guid;
388 ret = EFI_CALL(efi_set_variable(var_name16, &guid,
389 EFI_VARIABLE_BOOTSERVICE_ACCESS |
390 EFI_VARIABLE_RUNTIME_ACCESS,
391 size, value));
392 ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
393out:
394 free(value);
395 free(var_name16);
396
397 return ret;
398}