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