blob: 0bf7b8856ca3dcb07224ff63907b68d8d2ffbd4e [file] [log] [blame]
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * UEFI Shell-like command
4 *
5 * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited
6 */
7
8#include <charset.h>
9#include <common.h>
10#include <command.h>
Heinrich Schuchardt94686f62020-12-13 10:30:24 +010011#include <efi_dt_fixup.h>
Ilias Apalodimascbea2412021-03-17 21:55:01 +020012#include <efi_load_initrd.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090013#include <efi_loader.h>
Heinrich Schuchardt79693352020-09-27 14:50:25 +020014#include <efi_rng.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090015#include <exports.h>
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +020016#include <hexdump.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060017#include <log.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090018#include <malloc.h>
Heinrich Schuchardta415d612020-03-14 08:44:07 +010019#include <mapmem.h>
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +010020#include <part.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090021#include <search.h>
22#include <linux/ctype.h>
Ilias Apalodimascbea2412021-03-17 21:55:01 +020023#include <linux/err.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090024
AKASHI Takahiro355cdb52019-02-25 15:54:39 +090025#define BS systab.boottime
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +090026#define RT systab.runtime
27
28#ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
29/**
30 * do_efi_capsule_update() - process a capsule update
31 *
32 * @cmdtp: Command table
33 * @flag: Command flag
34 * @argc: Number of arguments
35 * @argv: Argument array
36 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
37 *
38 * Implement efidebug "capsule update" sub-command.
39 * process a capsule update.
40 *
41 * efidebug capsule update [-v] <capsule address>
42 */
43static int do_efi_capsule_update(struct cmd_tbl *cmdtp, int flag,
44 int argc, char * const argv[])
45{
46 struct efi_capsule_header *capsule;
47 int verbose = 0;
48 char *endp;
49 efi_status_t ret;
50
51 if (argc != 2 && argc != 3)
52 return CMD_RET_USAGE;
53
54 if (argc == 3) {
55 if (strcmp(argv[1], "-v"))
56 return CMD_RET_USAGE;
57
58 verbose = 1;
59 argc--;
60 argv++;
61 }
62
63 capsule = (typeof(capsule))simple_strtoul(argv[1], &endp, 16);
64 if (endp == argv[1]) {
65 printf("Invalid address: %s", argv[1]);
66 return CMD_RET_FAILURE;
67 }
68
69 if (verbose) {
70 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
71 printf("Capsule flags: 0x%x\n", capsule->flags);
72 printf("Capsule header size: 0x%x\n", capsule->header_size);
73 printf("Capsule image size: 0x%x\n",
74 capsule->capsule_image_size);
75 }
76
Simon Glassdf7d89a2021-02-07 14:27:02 -070077 ret = EFI_CALL(RT->update_capsule(&capsule, 1, 0));
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +090078 if (ret) {
79 printf("Cannot handle a capsule at %p", capsule);
80 return CMD_RET_FAILURE;
81 }
82
83 return CMD_RET_SUCCESS;
84}
85
Sughosh Ganu74075952020-12-30 19:27:11 +053086static int do_efi_capsule_on_disk_update(struct cmd_tbl *cmdtp, int flag,
87 int argc, char * const argv[])
88{
89 efi_status_t ret;
90
91 ret = efi_launch_capsules();
92
93 return ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
94}
95
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +090096/**
97 * do_efi_capsule_show() - show capsule information
98 *
99 * @cmdtp: Command table
100 * @flag: Command flag
101 * @argc: Number of arguments
102 * @argv: Argument array
103 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
104 *
105 * Implement efidebug "capsule show" sub-command.
106 * show capsule information.
107 *
108 * efidebug capsule show <capsule address>
109 */
110static int do_efi_capsule_show(struct cmd_tbl *cmdtp, int flag,
111 int argc, char * const argv[])
112{
113 struct efi_capsule_header *capsule;
114 char *endp;
115
116 if (argc != 2)
117 return CMD_RET_USAGE;
118
119 capsule = (typeof(capsule))simple_strtoul(argv[1], &endp, 16);
120 if (endp == argv[1]) {
121 printf("Invalid address: %s", argv[1]);
122 return CMD_RET_FAILURE;
123 }
124
125 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
126 printf("Capsule flags: 0x%x\n", capsule->flags);
127 printf("Capsule header size: 0x%x\n", capsule->header_size);
128 printf("Capsule image size: 0x%x\n",
129 capsule->capsule_image_size);
130
131 return CMD_RET_SUCCESS;
132}
133
Jose Marinhoaa31a872021-03-11 13:18:51 +0000134#ifdef CONFIG_EFI_ESRT
135
136#define EFI_ESRT_FW_TYPE_NUM 4
137char *efi_fw_type_str[EFI_ESRT_FW_TYPE_NUM] = {"unknown", "system FW", "device FW",
138 "UEFI driver"};
139
140#define EFI_ESRT_UPDATE_STATUS_NUM 9
141char *efi_update_status_str[EFI_ESRT_UPDATE_STATUS_NUM] = {"success", "unsuccessful",
142 "insufficient resources", "incorrect version", "invalid format",
143 "auth error", "power event (AC)", "power event (batt)",
144 "unsatisfied dependencies"};
145
146#define EFI_FW_TYPE_STR_GET(idx) (\
147EFI_ESRT_FW_TYPE_NUM > (idx) ? efi_fw_type_str[(idx)] : "error"\
148)
149
150#define EFI_FW_STATUS_STR_GET(idx) (\
151EFI_ESRT_UPDATE_STATUS_NUM > (idx) ? efi_update_status_str[(idx)] : "error"\
152)
153
154/**
155 * do_efi_capsule_esrt() - manage UEFI capsules
156 *
157 * @cmdtp: Command table
158 * @flag: Command flag
159 * @argc: Number of arguments
160 * @argv: Argument array
161 * Return: CMD_RET_SUCCESS on success,
162 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
163 *
164 * Implement efidebug "capsule esrt" sub-command.
165 * The prints the current ESRT table.
166 *
167 * efidebug capsule esrt
168 */
169static int do_efi_capsule_esrt(struct cmd_tbl *cmdtp, int flag,
170 int argc, char * const argv[])
171{
172 struct efi_system_resource_table *esrt = NULL;
173
174 if (argc != 1)
175 return CMD_RET_USAGE;
176
177 for (int idx = 0; idx < systab.nr_tables; idx++)
178 if (!guidcmp(&efi_esrt_guid, &systab.tables[idx].guid))
179 esrt = (struct efi_system_resource_table *)systab.tables[idx].table;
180
181 if (!esrt) {
182 log_info("ESRT: table not present\n");
183 return CMD_RET_SUCCESS;
184 }
185
186 printf("========================================\n");
187 printf("ESRT: fw_resource_count=%d\n", esrt->fw_resource_count);
188 printf("ESRT: fw_resource_count_max=%d\n", esrt->fw_resource_count_max);
189 printf("ESRT: fw_resource_version=%lld\n", esrt->fw_resource_version);
190
191 for (int idx = 0; idx < esrt->fw_resource_count; idx++) {
192 printf("[entry %d]==============================\n", idx);
193 printf("ESRT: fw_class=%pUL\n", &esrt->entries[idx].fw_class);
194 printf("ESRT: fw_type=%s\n", EFI_FW_TYPE_STR_GET(esrt->entries[idx].fw_type));
195 printf("ESRT: fw_version=%d\n", esrt->entries[idx].fw_version);
196 printf("ESRT: lowest_supported_fw_version=%d\n",
197 esrt->entries[idx].lowest_supported_fw_version);
198 printf("ESRT: capsule_flags=%d\n",
199 esrt->entries[idx].capsule_flags);
200 printf("ESRT: last_attempt_version=%d\n",
201 esrt->entries[idx].last_attempt_version);
202 printf("ESRT: last_attempt_status=%s\n",
203 EFI_FW_STATUS_STR_GET(esrt->entries[idx].last_attempt_status));
204 }
205 printf("========================================\n");
206
207 return CMD_RET_SUCCESS;
208}
209#endif /* CONFIG_EFI_ESRT */
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900210/**
211 * do_efi_capsule_res() - show a capsule update result
212 *
213 * @cmdtp: Command table
214 * @flag: Command flag
215 * @argc: Number of arguments
216 * @argv: Argument array
217 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
218 *
219 * Implement efidebug "capsule result" sub-command.
220 * show a capsule update result.
221 * If result number is not specified, CapsuleLast will be shown.
222 *
223 * efidebug capsule result [<capsule result number>]
224 */
225static int do_efi_capsule_res(struct cmd_tbl *cmdtp, int flag,
226 int argc, char * const argv[])
227{
228 int capsule_id;
229 char *endp;
230 char var_name[12];
231 u16 var_name16[12], *p;
232 efi_guid_t guid;
233 struct efi_capsule_result_variable_header *result = NULL;
234 efi_uintn_t size;
235 efi_status_t ret;
236
237 if (argc != 1 && argc != 2)
238 return CMD_RET_USAGE;
239
240 guid = efi_guid_capsule_report;
241 if (argc == 1) {
242 size = sizeof(var_name16);
243 ret = EFI_CALL(RT->get_variable(L"CapsuleLast", &guid, NULL,
244 &size, var_name16));
245 if (ret != EFI_SUCCESS) {
246 if (ret == EFI_NOT_FOUND)
247 printf("CapsuleLast doesn't exist\n");
248 else
249 printf("Failed to get CapsuleLast\n");
250
251 return CMD_RET_FAILURE;
252 }
253 printf("CapsuleLast is %ls\n", var_name16);
254 } else {
255 argc--;
256 argv++;
257
258 capsule_id = simple_strtoul(argv[0], &endp, 16);
259 if (capsule_id < 0 || capsule_id > 0xffff)
260 return CMD_RET_USAGE;
261
262 sprintf(var_name, "Capsule%04X", capsule_id);
263 p = var_name16;
264 utf8_utf16_strncpy(&p, var_name, 9);
265 }
266
267 size = 0;
268 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
269 if (ret == EFI_BUFFER_TOO_SMALL) {
270 result = malloc(size);
AKASHI Takahiro30f82222021-01-22 10:42:48 +0900271 if (!result)
272 return CMD_RET_FAILURE;
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900273 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
274 result));
AKASHI Takahiro30f82222021-01-22 10:42:48 +0900275 }
276 if (ret != EFI_SUCCESS) {
277 free(result);
278 printf("Failed to get %ls\n", var_name16);
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900279
AKASHI Takahiro30f82222021-01-22 10:42:48 +0900280 return CMD_RET_FAILURE;
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900281 }
282
283 printf("Result total size: 0x%x\n", result->variable_total_size);
284 printf("Capsule guid: %pUl\n", &result->capsule_guid);
285 printf("Time processed: %04d-%02d-%02d %02d:%02d:%02d\n",
286 result->capsule_processed.year, result->capsule_processed.month,
287 result->capsule_processed.day, result->capsule_processed.hour,
288 result->capsule_processed.minute,
289 result->capsule_processed.second);
290 printf("Capsule status: 0x%lx\n", result->capsule_status);
291
292 free(result);
293
294 return CMD_RET_SUCCESS;
295}
296
297static struct cmd_tbl cmd_efidebug_capsule_sub[] = {
298 U_BOOT_CMD_MKENT(update, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_update,
299 "", ""),
300 U_BOOT_CMD_MKENT(show, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_show,
301 "", ""),
Jose Marinhoaa31a872021-03-11 13:18:51 +0000302#ifdef CONFIG_EFI_ESRT
303 U_BOOT_CMD_MKENT(esrt, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_esrt,
304 "", ""),
305#endif
Sughosh Ganu74075952020-12-30 19:27:11 +0530306 U_BOOT_CMD_MKENT(disk-update, 0, 0, do_efi_capsule_on_disk_update,
307 "", ""),
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900308 U_BOOT_CMD_MKENT(result, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_res,
309 "", ""),
310};
311
312/**
313 * do_efi_capsule() - manage UEFI capsules
314 *
315 * @cmdtp: Command table
316 * @flag: Command flag
317 * @argc: Number of arguments
318 * @argv: Argument array
319 * Return: CMD_RET_SUCCESS on success,
320 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
321 *
322 * Implement efidebug "capsule" sub-command.
323 */
324static int do_efi_capsule(struct cmd_tbl *cmdtp, int flag,
325 int argc, char * const argv[])
326{
327 struct cmd_tbl *cp;
328
329 if (argc < 2)
330 return CMD_RET_USAGE;
331
332 argc--; argv++;
333
334 cp = find_cmd_tbl(argv[0], cmd_efidebug_capsule_sub,
335 ARRAY_SIZE(cmd_efidebug_capsule_sub));
336 if (!cp)
337 return CMD_RET_USAGE;
338
339 return cp->cmd(cmdtp, flag, argc, argv);
340}
341#endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT */
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900342
343/**
Heinrich Schuchardtd4730632021-04-02 11:30:02 +0200344 * efi_get_device_path_text() - get device path text
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900345 *
Heinrich Schuchardtd4730632021-04-02 11:30:02 +0200346 * Return the text representation of the device path of a handle.
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900347 *
Heinrich Schuchardtd4730632021-04-02 11:30:02 +0200348 * @handle: handle of UEFI device
349 * Return:
350 * Pointer to the device path text or NULL.
351 * The caller is responsible for calling FreePool().
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900352 */
Heinrich Schuchardtd4730632021-04-02 11:30:02 +0200353static u16 *efi_get_device_path_text(efi_handle_t handle)
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900354{
Heinrich Schuchardtd4730632021-04-02 11:30:02 +0200355 struct efi_handler *handler;
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900356 efi_status_t ret;
357
Heinrich Schuchardtd4730632021-04-02 11:30:02 +0200358 ret = efi_search_protocol(handle, &efi_guid_device_path, &handler);
359 if (ret == EFI_SUCCESS && handler->protocol_interface) {
360 struct efi_device_path *dp = handler->protocol_interface;
361
362 return efi_dp_str(dp);
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900363 } else {
Heinrich Schuchardtd4730632021-04-02 11:30:02 +0200364 return NULL;
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900365 }
366}
367
368#define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
369
370static const char spc[] = " ";
371static const char sep[] = "================";
372
373/**
374 * do_efi_show_devices() - show UEFI devices
375 *
376 * @cmdtp: Command table
377 * @flag: Command flag
378 * @argc: Number of arguments
379 * @argv: Argument array
380 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
381 *
382 * Implement efidebug "devices" sub-command.
383 * Show all UEFI devices and their information.
384 */
Simon Glass09140112020-05-10 11:40:03 -0600385static int do_efi_show_devices(struct cmd_tbl *cmdtp, int flag,
386 int argc, char *const argv[])
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900387{
388 efi_handle_t *handles;
389 efi_uintn_t num, i;
390 u16 *dev_path_text;
391 efi_status_t ret;
392
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200393 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900394 &num, &handles));
395 if (ret != EFI_SUCCESS)
396 return CMD_RET_FAILURE;
397
398 if (!num)
399 return CMD_RET_SUCCESS;
400
401 printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc);
402 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
403 for (i = 0; i < num; i++) {
Heinrich Schuchardtd4730632021-04-02 11:30:02 +0200404 dev_path_text = efi_get_device_path_text(handles[i]);
405 if (dev_path_text) {
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900406 printf("%p %ls\n", handles[i], dev_path_text);
407 efi_free_pool(dev_path_text);
408 }
409 }
410
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200411 efi_free_pool(handles);
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900412
413 return CMD_RET_SUCCESS;
414}
415
416/**
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900417 * efi_get_driver_handle_info() - get information of UEFI driver
418 *
419 * @handle: Handle of UEFI device
420 * @driver_name: Driver name
421 * @image_path: Pointer to text of device path
422 * Return: 0 on success, -1 on failure
423 *
424 * Currently return no useful information as all UEFI drivers are
425 * built-in..
426 */
427static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
428 u16 **image_path)
429{
430 struct efi_handler *handler;
431 struct efi_loaded_image *image;
432 efi_status_t ret;
433
434 /*
435 * driver name
436 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
437 */
438 *driver_name = NULL;
439
440 /* image name */
441 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
442 if (ret != EFI_SUCCESS) {
443 *image_path = NULL;
444 return 0;
445 }
446
447 image = handler->protocol_interface;
448 *image_path = efi_dp_str(image->file_path);
449
450 return 0;
451}
452
453/**
454 * do_efi_show_drivers() - show UEFI drivers
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, CMD_RET_RET_FAILURE on failure
461 *
462 * Implement efidebug "drivers" sub-command.
463 * Show all UEFI drivers and their information.
464 */
Simon Glass09140112020-05-10 11:40:03 -0600465static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
466 int argc, char *const argv[])
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900467{
468 efi_handle_t *handles;
469 efi_uintn_t num, i;
470 u16 *driver_name, *image_path_text;
471 efi_status_t ret;
472
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200473 ret = EFI_CALL(efi_locate_handle_buffer(
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900474 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
475 NULL, &num, &handles));
476 if (ret != EFI_SUCCESS)
477 return CMD_RET_FAILURE;
478
479 if (!num)
480 return CMD_RET_SUCCESS;
481
482 printf("Driver%.*s Name Image Path\n",
483 EFI_HANDLE_WIDTH - 6, spc);
484 printf("%.*s ==================== ====================\n",
485 EFI_HANDLE_WIDTH, sep);
486 for (i = 0; i < num; i++) {
487 if (!efi_get_driver_handle_info(handles[i], &driver_name,
488 &image_path_text)) {
489 if (image_path_text)
490 printf("%p %-20ls %ls\n", handles[i],
491 driver_name, image_path_text);
492 else
493 printf("%p %-20ls <built-in>\n",
494 handles[i], driver_name);
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200495 efi_free_pool(driver_name);
496 efi_free_pool(image_path_text);
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900497 }
498 }
499
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200500 efi_free_pool(handles);
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900501
502 return CMD_RET_SUCCESS;
503}
504
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900505static const struct {
506 const char *text;
507 const efi_guid_t guid;
508} guid_list[] = {
509 {
510 "Device Path",
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +0200511 EFI_DEVICE_PATH_PROTOCOL_GUID,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900512 },
513 {
514 "Device Path To Text",
515 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
516 },
517 {
518 "Device Path Utilities",
519 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
520 },
521 {
522 "Unicode Collation 2",
523 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
524 },
525 {
526 "Driver Binding",
527 EFI_DRIVER_BINDING_PROTOCOL_GUID,
528 },
529 {
530 "Simple Text Input",
531 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
532 },
533 {
534 "Simple Text Input Ex",
535 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
536 },
537 {
538 "Simple Text Output",
539 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
540 },
541 {
542 "Block IO",
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +0200543 EFI_BLOCK_IO_PROTOCOL_GUID,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900544 },
545 {
546 "Simple File System",
547 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
548 },
549 {
550 "Loaded Image",
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +0200551 EFI_LOADED_IMAGE_PROTOCOL_GUID,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900552 },
553 {
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +0200554 "Graphics Output",
555 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900556 },
Heinrich Schuchardt391bc8a2019-04-20 07:57:28 +0200557 {
558 "HII String",
559 EFI_HII_STRING_PROTOCOL_GUID,
560 },
561 {
562 "HII Database",
563 EFI_HII_DATABASE_PROTOCOL_GUID,
564 },
565 {
566 "HII Config Routing",
567 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
568 },
569 {
Ilias Apalodimasec80b472020-02-21 09:55:45 +0200570 "Load File2",
571 EFI_LOAD_FILE2_PROTOCOL_GUID,
572 },
573 {
Heinrich Schuchardt79693352020-09-27 14:50:25 +0200574 "Random Number Generator",
575 EFI_RNG_PROTOCOL_GUID,
576 },
577 {
Heinrich Schuchardt391bc8a2019-04-20 07:57:28 +0200578 "Simple Network",
579 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
580 },
581 {
582 "PXE Base Code",
583 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
584 },
Heinrich Schuchardt94686f62020-12-13 10:30:24 +0100585 {
586 "Device-Tree Fixup",
587 EFI_DT_FIXUP_PROTOCOL_GUID,
588 },
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +0100589 {
590 "System Partition",
591 PARTITION_SYSTEM_GUID
592 },
Heinrich Schuchardta2c3f1b2021-02-26 17:57:47 +0100593 {
594 "Firmware Management",
595 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
596 },
Heinrich Schuchardt986e0642020-01-07 05:57:47 +0100597 /* Configuration table GUIDs */
598 {
599 "ACPI table",
600 EFI_ACPI_TABLE_GUID,
601 },
602 {
Jose Marinho64a8aae2021-03-02 17:26:38 +0000603 "EFI System Resource Table",
604 EFI_SYSTEM_RESOURCE_TABLE_GUID,
605 },
606 {
Heinrich Schuchardt986e0642020-01-07 05:57:47 +0100607 "device tree",
608 EFI_FDT_GUID,
609 },
610 {
611 "SMBIOS table",
612 SMBIOS_TABLE_GUID,
613 },
Heinrich Schuchardt76be6872020-02-19 20:48:49 +0100614 {
615 "Runtime properties",
616 EFI_RT_PROPERTIES_TABLE_GUID,
617 },
Ilias Apalodimas94e78202020-11-30 11:47:41 +0200618 {
619 "TCG2 Final Events Table",
620 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
621 },
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900622};
623
624/**
Heinrich Schuchardt173cd9e2020-01-07 06:02:33 +0100625 * get_guid_text - get string of GUID
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900626 *
Heinrich Schuchardt173cd9e2020-01-07 06:02:33 +0100627 * Return description of GUID.
628 *
629 * @guid: GUID
630 * Return: description of GUID or NULL
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900631 */
Heinrich Schuchardt173cd9e2020-01-07 06:02:33 +0100632static const char *get_guid_text(const void *guid)
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900633{
634 int i;
635
Heinrich Schuchardt173cd9e2020-01-07 06:02:33 +0100636 for (i = 0; i < ARRAY_SIZE(guid_list); i++) {
637 /*
638 * As guidcmp uses memcmp() we can safely accept unaligned
639 * GUIDs.
640 */
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900641 if (!guidcmp(&guid_list[i].guid, guid))
Heinrich Schuchardt173cd9e2020-01-07 06:02:33 +0100642 return guid_list[i].text;
643 }
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900644
Heinrich Schuchardt173cd9e2020-01-07 06:02:33 +0100645 return NULL;
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900646}
647
648/**
649 * do_efi_show_handles() - show UEFI handles
650 *
651 * @cmdtp: Command table
652 * @flag: Command flag
653 * @argc: Number of arguments
654 * @argv: Argument array
655 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
656 *
657 * Implement efidebug "dh" sub-command.
658 * Show all UEFI handles and their information, currently all protocols
659 * added to handle.
660 */
Simon Glass09140112020-05-10 11:40:03 -0600661static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
662 int argc, char *const argv[])
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900663{
664 efi_handle_t *handles;
665 efi_guid_t **guid;
666 efi_uintn_t num, count, i, j;
667 const char *guid_text;
668 efi_status_t ret;
669
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200670 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900671 &num, &handles));
672 if (ret != EFI_SUCCESS)
673 return CMD_RET_FAILURE;
674
675 if (!num)
676 return CMD_RET_SUCCESS;
677
678 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
679 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
680 for (i = 0; i < num; i++) {
681 printf("%p", handles[i]);
682 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
683 &count));
684 if (ret || !count) {
685 putc('\n');
686 continue;
687 }
688
689 for (j = 0; j < count; j++) {
690 if (j)
691 printf(", ");
692 else
693 putc(' ');
694
695 guid_text = get_guid_text(guid[j]);
696 if (guid_text)
697 puts(guid_text);
698 else
699 printf("%pUl", guid[j]);
700 }
701 putc('\n');
702 }
703
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200704 efi_free_pool(handles);
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900705
706 return CMD_RET_SUCCESS;
707}
708
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900709/**
AKASHI Takahirofa536732019-02-25 15:54:42 +0900710 * do_efi_show_images() - show UEFI images
711 *
712 * @cmdtp: Command table
713 * @flag: Command flag
714 * @argc: Number of arguments
715 * @argv: Argument array
716 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
717 *
718 * Implement efidebug "images" sub-command.
719 * Show all UEFI loaded images and their information.
720 */
Simon Glass09140112020-05-10 11:40:03 -0600721static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
722 int argc, char *const argv[])
AKASHI Takahirofa536732019-02-25 15:54:42 +0900723{
724 efi_print_image_infos(NULL);
725
726 return CMD_RET_SUCCESS;
727}
728
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900729static const char * const efi_mem_type_string[] = {
730 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
731 [EFI_LOADER_CODE] = "LOADER CODE",
732 [EFI_LOADER_DATA] = "LOADER DATA",
733 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
734 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
735 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
736 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
737 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
738 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
739 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
740 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
741 [EFI_MMAP_IO] = "IO",
742 [EFI_MMAP_IO_PORT] = "IO PORT",
743 [EFI_PAL_CODE] = "PAL",
Heinrich Schuchardtdd9056c2020-04-29 20:21:39 +0200744 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900745};
746
747static const struct efi_mem_attrs {
748 const u64 bit;
749 const char *text;
750} efi_mem_attrs[] = {
751 {EFI_MEMORY_UC, "UC"},
752 {EFI_MEMORY_UC, "UC"},
753 {EFI_MEMORY_WC, "WC"},
754 {EFI_MEMORY_WT, "WT"},
755 {EFI_MEMORY_WB, "WB"},
756 {EFI_MEMORY_UCE, "UCE"},
757 {EFI_MEMORY_WP, "WP"},
758 {EFI_MEMORY_RP, "RP"},
759 {EFI_MEMORY_XP, "WP"},
760 {EFI_MEMORY_NV, "NV"},
761 {EFI_MEMORY_MORE_RELIABLE, "REL"},
762 {EFI_MEMORY_RO, "RO"},
Heinrich Schuchardt255a4732020-05-19 07:20:46 +0200763 {EFI_MEMORY_SP, "SP"},
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900764 {EFI_MEMORY_RUNTIME, "RT"},
765};
766
767/**
768 * print_memory_attributes() - print memory map attributes
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200769 *
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900770 * @attributes: Attribute value
771 *
772 * Print memory map attributes
773 */
774static void print_memory_attributes(u64 attributes)
775{
776 int sep, i;
777
778 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
779 if (attributes & efi_mem_attrs[i].bit) {
780 if (sep) {
781 putc('|');
782 } else {
783 putc(' ');
784 sep = 1;
785 }
786 puts(efi_mem_attrs[i].text);
787 }
788}
789
790#define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
791
792/**
793 * do_efi_show_memmap() - show UEFI memory map
794 *
795 * @cmdtp: Command table
796 * @flag: Command flag
797 * @argc: Number of arguments
798 * @argv: Argument array
799 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
800 *
801 * Implement efidebug "memmap" sub-command.
802 * Show UEFI memory map.
803 */
Simon Glass09140112020-05-10 11:40:03 -0600804static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
805 int argc, char *const argv[])
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900806{
807 struct efi_mem_desc *memmap = NULL, *map;
808 efi_uintn_t map_size = 0;
809 const char *type;
810 int i;
811 efi_status_t ret;
812
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200813 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900814 if (ret == EFI_BUFFER_TOO_SMALL) {
815 map_size += sizeof(struct efi_mem_desc); /* for my own */
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200816 ret = efi_allocate_pool(EFI_LOADER_DATA, map_size,
817 (void *)&memmap);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900818 if (ret != EFI_SUCCESS)
819 return CMD_RET_FAILURE;
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200820 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900821 }
822 if (ret != EFI_SUCCESS) {
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200823 efi_free_pool(memmap);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900824 return CMD_RET_FAILURE;
825 }
826
827 printf("Type Start%.*s End%.*s Attributes\n",
828 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
829 printf("================ %.*s %.*s ==========\n",
830 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
AKASHI Takahiroe2a5b862020-05-08 14:50:18 +0900831 /*
832 * Coverity check: dereferencing null pointer "map."
833 * This is a false positive as memmap will always be
834 * populated by allocate_pool() above.
835 */
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900836 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
Heinrich Schuchardtdd9056c2020-04-29 20:21:39 +0200837 if (map->type < ARRAY_SIZE(efi_mem_type_string))
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900838 type = efi_mem_type_string[map->type];
839 else
840 type = "(unknown)";
841
842 printf("%-16s %.*llx-%.*llx", type,
843 EFI_PHYS_ADDR_WIDTH,
Heinrich Schuchardt6c0ef352020-03-27 04:33:17 +0000844 (u64)map_to_sysmem((void *)(uintptr_t)
845 map->physical_start),
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900846 EFI_PHYS_ADDR_WIDTH,
Heinrich Schuchardt6c0ef352020-03-27 04:33:17 +0000847 (u64)map_to_sysmem((void *)(uintptr_t)
848 (map->physical_start +
849 map->num_pages * EFI_PAGE_SIZE)));
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900850
851 print_memory_attributes(map->attribute);
852 putc('\n');
853 }
854
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200855 efi_free_pool(memmap);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900856
857 return CMD_RET_SUCCESS;
858}
859
AKASHI Takahirofa536732019-02-25 15:54:42 +0900860/**
Heinrich Schuchardt986e0642020-01-07 05:57:47 +0100861 * do_efi_show_tables() - show UEFI configuration tables
862 *
863 * @cmdtp: Command table
864 * @flag: Command flag
865 * @argc: Number of arguments
866 * @argv: Argument array
867 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
868 *
869 * Implement efidebug "tables" sub-command.
870 * Show UEFI configuration tables.
871 */
Simon Glass09140112020-05-10 11:40:03 -0600872static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
873 int argc, char *const argv[])
Heinrich Schuchardt986e0642020-01-07 05:57:47 +0100874{
875 efi_uintn_t i;
876 const char *guid_str;
877
878 for (i = 0; i < systab.nr_tables; ++i) {
879 guid_str = get_guid_text(&systab.tables[i].guid);
880 if (!guid_str)
881 guid_str = "";
882 printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
883 }
884
885 return CMD_RET_SUCCESS;
886}
887
888/**
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200889 * create_initrd_dp() - Create a special device for our Boot### option
890 *
891 * @dev: Device
892 * @part: Disk partition
893 * @file: Filename
894 * Return: Pointer to the device path or ERR_PTR
895 *
896 */
897static
898struct efi_device_path *create_initrd_dp(const char *dev, const char *part,
899 const char *file)
900
901{
902 struct efi_device_path *tmp_dp = NULL, *tmp_fp = NULL;
903 struct efi_device_path *initrd_dp = NULL;
904 efi_status_t ret;
905 const struct efi_initrd_dp id_dp = {
906 .vendor = {
907 {
908 DEVICE_PATH_TYPE_MEDIA_DEVICE,
909 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
910 sizeof(id_dp.vendor),
911 },
912 EFI_INITRD_MEDIA_GUID,
913 },
914 .end = {
915 DEVICE_PATH_TYPE_END,
916 DEVICE_PATH_SUB_TYPE_END,
917 sizeof(id_dp.end),
918 }
919 };
920
921 ret = efi_dp_from_name(dev, part, file, &tmp_dp, &tmp_fp);
922 if (ret != EFI_SUCCESS) {
923 printf("Cannot create device path for \"%s %s\"\n", part, file);
924 goto out;
925 }
926
927 initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp,
928 tmp_fp);
929
930out:
931 efi_free_pool(tmp_dp);
932 efi_free_pool(tmp_fp);
933 return initrd_dp;
934}
935
936/**
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900937 * do_efi_boot_add() - set UEFI load option
938 *
939 * @cmdtp: Command table
940 * @flag: Command flag
941 * @argc: Number of arguments
942 * @argv: Argument array
943 * Return: CMD_RET_SUCCESS on success,
944 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
945 *
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200946 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
947 *
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200948 * efidebug boot add -b <id> <label> <interface> <devnum>[:<part>] <file>
949 * -i <file> <interface2> <devnum2>[:<part>] <initrd>
950 * -s '<options>'
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900951 */
Simon Glass09140112020-05-10 11:40:03 -0600952static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
953 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900954{
955 int id;
956 char *endp;
957 char var_name[9];
958 u16 var_name16[9], *p;
959 efi_guid_t guid;
960 size_t label_len, label_len16;
961 u16 *label;
962 struct efi_device_path *device_path = NULL, *file_path = NULL;
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200963 struct efi_device_path *final_fp = NULL;
964 struct efi_device_path *initrd_dp = NULL;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900965 struct efi_load_option lo;
966 void *data = NULL;
967 efi_uintn_t size;
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200968 efi_uintn_t fp_size = 0;
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200969 efi_status_t ret;
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200970 int r = CMD_RET_SUCCESS;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900971
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900972 guid = efi_global_variable_guid;
973
974 /* attributes */
975 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200976 lo.optional_data = NULL;
977 lo.label = NULL;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900978
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200979 argc--;
980 argv++; /* 'add' */
981 for (; argc > 0; argc--, argv++) {
982 if (!strcmp(argv[0], "-b")) {
983 if (argc < 5 || lo.label) {
984 r = CMD_RET_USAGE;
985 goto out;
986 }
987 id = (int)simple_strtoul(argv[1], &endp, 16);
988 if (*endp != '\0' || id > 0xffff)
989 return CMD_RET_USAGE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900990
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200991 sprintf(var_name, "Boot%04X", id);
992 p = var_name16;
993 utf8_utf16_strncpy(&p, var_name, 9);
994
995 /* label */
996 label_len = strlen(argv[2]);
997 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
998 label = malloc((label_len16 + 1) * sizeof(u16));
999 if (!label)
1000 return CMD_RET_FAILURE;
1001 lo.label = label; /* label will be changed below */
1002 utf8_utf16_strncpy(&label, argv[2], label_len);
1003
1004 /* file path */
1005 ret = efi_dp_from_name(argv[3], argv[4], argv[5],
1006 &device_path, &file_path);
1007 if (ret != EFI_SUCCESS) {
1008 printf("Cannot create device path for \"%s %s\"\n",
1009 argv[3], argv[4]);
1010 r = CMD_RET_FAILURE;
1011 goto out;
1012 }
1013 fp_size += efi_dp_size(file_path) +
1014 sizeof(struct efi_device_path);
1015 argc -= 5;
1016 argv += 5;
1017 } else if (!strcmp(argv[0], "-i")) {
1018 if (argc < 3 || initrd_dp) {
1019 r = CMD_RET_USAGE;
1020 goto out;
1021 }
1022
1023 initrd_dp = create_initrd_dp(argv[1], argv[2], argv[3]);
1024 if (!initrd_dp) {
1025 printf("Cannot add an initrd\n");
1026 r = CMD_RET_FAILURE;
1027 goto out;
1028 }
1029 argc -= 3;
1030 argv += 3;
1031 fp_size += efi_dp_size(initrd_dp) +
1032 sizeof(struct efi_device_path);
1033 } else if (!strcmp(argv[0], "-s")) {
1034 if (argc < 1 || lo.optional_data) {
1035 r = CMD_RET_USAGE;
1036 goto out;
1037 }
1038 lo.optional_data = (const u8 *)argv[1];
1039 argc -= 1;
1040 argv += 1;
1041 } else {
1042 r = CMD_RET_USAGE;
1043 goto out;
1044 }
1045 }
1046
1047 if (!file_path) {
1048 printf("Missing binary\n");
1049 r = CMD_RET_USAGE;
1050 goto out;
1051 }
1052
1053 final_fp = efi_dp_concat(file_path, initrd_dp);
1054 if (!final_fp) {
1055 printf("Cannot create final device path\n");
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001056 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001057 goto out;
1058 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001059
Ilias Apalodimascbea2412021-03-17 21:55:01 +02001060 lo.file_path = final_fp;
1061 lo.file_path_length = fp_size;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001062
1063 size = efi_serialize_load_option(&lo, (u8 **)&data);
1064 if (!size) {
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001065 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001066 goto out;
1067 }
1068
Heinrich Schuchardta30c7232020-05-02 16:08:37 +02001069 ret = EFI_CALL(efi_set_variable(var_name16, &guid,
AKASHI Takahirof658c2e2019-06-04 15:52:10 +09001070 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001071 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1072 EFI_VARIABLE_RUNTIME_ACCESS,
1073 size, data));
Heinrich Schuchardta332f252019-06-20 12:59:45 +02001074 if (ret != EFI_SUCCESS) {
1075 printf("Cannot set %ls\n", var_name16);
1076 r = CMD_RET_FAILURE;
1077 }
Ilias Apalodimascbea2412021-03-17 21:55:01 +02001078
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001079out:
1080 free(data);
Ilias Apalodimascbea2412021-03-17 21:55:01 +02001081 efi_free_pool(final_fp);
1082 efi_free_pool(initrd_dp);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001083 efi_free_pool(device_path);
1084 efi_free_pool(file_path);
1085 free(lo.label);
1086
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001087 return r;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001088}
1089
1090/**
1091 * do_efi_boot_rm() - delete UEFI load options
1092 *
1093 * @cmdtp: Command table
1094 * @flag: Command flag
1095 * @argc: Number of arguments
1096 * @argv: Argument array
1097 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1098 *
1099 * Implement efidebug "boot rm" sub-command.
1100 * Delete UEFI load options.
Heinrich Schuchardt0b016562019-07-14 14:00:41 +02001101 *
1102 * efidebug boot rm <id> ...
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001103 */
Simon Glass09140112020-05-10 11:40:03 -06001104static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag,
1105 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001106{
1107 efi_guid_t guid;
1108 int id, i;
1109 char *endp;
1110 char var_name[9];
AKASHI Takahiroe8bced62020-02-28 09:05:04 +09001111 u16 var_name16[9], *p;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001112 efi_status_t ret;
1113
1114 if (argc == 1)
1115 return CMD_RET_USAGE;
1116
1117 guid = efi_global_variable_guid;
1118 for (i = 1; i < argc; i++, argv++) {
1119 id = (int)simple_strtoul(argv[1], &endp, 16);
1120 if (*endp != '\0' || id > 0xffff)
1121 return CMD_RET_FAILURE;
1122
1123 sprintf(var_name, "Boot%04X", id);
AKASHI Takahiroe8bced62020-02-28 09:05:04 +09001124 p = var_name16;
1125 utf8_utf16_strncpy(&p, var_name, 9);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001126
Heinrich Schuchardta30c7232020-05-02 16:08:37 +02001127 ret = EFI_CALL(efi_set_variable(var_name16, &guid, 0, 0, NULL));
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001128 if (ret) {
Heinrich Schuchardt30efb5d2020-03-02 20:13:10 +01001129 printf("Cannot remove %ls\n", var_name16);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001130 return CMD_RET_FAILURE;
1131 }
1132 }
1133
1134 return CMD_RET_SUCCESS;
1135}
1136
1137/**
1138 * show_efi_boot_opt_data() - dump UEFI load option
1139 *
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001140 * @varname16: variable name
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +02001141 * @data: value of UEFI load option variable
1142 * @size: size of the boot option
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001143 *
1144 * Decode the value of UEFI load option variable and print information.
1145 */
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +02001146static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001147{
Ilias Apalodimascbea2412021-03-17 21:55:01 +02001148 struct efi_device_path *initrd_path = NULL;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001149 struct efi_load_option lo;
1150 char *label, *p;
1151 size_t label_len16, label_len;
1152 u16 *dp_str;
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +02001153 efi_status_t ret;
Ilias Apalodimascbea2412021-03-17 21:55:01 +02001154 efi_uintn_t initrd_dp_size;
1155 const efi_guid_t lf2_initrd_guid = EFI_INITRD_MEDIA_GUID;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001156
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +02001157 ret = efi_deserialize_load_option(&lo, data, size);
1158 if (ret != EFI_SUCCESS) {
1159 printf("%ls: invalid load option\n", varname16);
1160 return;
1161 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001162
1163 label_len16 = u16_strlen(lo.label);
1164 label_len = utf16_utf8_strnlen(lo.label, label_len16);
1165 label = malloc(label_len + 1);
1166 if (!label)
1167 return;
1168 p = label;
1169 utf16_utf8_strncpy(&p, lo.label, label_len16);
1170
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001171 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
1172 varname16,
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001173 /* ACTIVE */
1174 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
1175 /* FORCE RECONNECT */
1176 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
1177 /* HIDDEN */
1178 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
1179 lo.attributes);
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +02001180 printf(" label: %s\n", label);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001181
1182 dp_str = efi_dp_str(lo.file_path);
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +02001183 printf(" file_path: %ls\n", dp_str);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001184 efi_free_pool(dp_str);
1185
Ilias Apalodimascbea2412021-03-17 21:55:01 +02001186 initrd_path = efi_dp_from_lo(&lo, &initrd_dp_size, lf2_initrd_guid);
1187 if (initrd_path) {
1188 dp_str = efi_dp_str(initrd_path);
1189 printf(" initrd_path: %ls\n", dp_str);
1190 efi_free_pool(dp_str);
1191 efi_free_pool(initrd_path);
1192 }
1193
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +02001194 printf(" data:\n");
1195 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +02001196 lo.optional_data, *size, true);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001197 free(label);
1198}
1199
1200/**
1201 * show_efi_boot_opt() - dump UEFI load option
1202 *
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001203 * @varname16: variable name
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001204 *
1205 * Dump information defined by UEFI load option.
1206 */
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001207static void show_efi_boot_opt(u16 *varname16)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001208{
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001209 void *data;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001210 efi_uintn_t size;
AKASHI Takahiro0bffb8c2019-11-26 10:11:22 +09001211 efi_status_t ret;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001212
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001213 size = 0;
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001214 ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
1215 NULL, &size, NULL));
AKASHI Takahiro0bffb8c2019-11-26 10:11:22 +09001216 if (ret == EFI_BUFFER_TOO_SMALL) {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001217 data = malloc(size);
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001218 if (!data) {
1219 printf("ERROR: Out of memory\n");
1220 return;
1221 }
1222 ret = EFI_CALL(efi_get_variable(varname16,
1223 &efi_global_variable_guid,
1224 NULL, &size, data));
1225 if (ret == EFI_SUCCESS)
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +02001226 show_efi_boot_opt_data(varname16, data, &size);
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001227 free(data);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001228 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001229}
1230
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001231static int u16_tohex(u16 c)
1232{
1233 if (c >= '0' && c <= '9')
1234 return c - '0';
1235 if (c >= 'A' && c <= 'F')
1236 return c - 'A' + 10;
1237
1238 /* not hexadecimal */
1239 return -1;
1240}
1241
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001242/**
1243 * show_efi_boot_dump() - dump all UEFI load options
1244 *
1245 * @cmdtp: Command table
1246 * @flag: Command flag
1247 * @argc: Number of arguments
1248 * @argv: Argument array
1249 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1250 *
1251 * Implement efidebug "boot dump" sub-command.
1252 * Dump information of all UEFI load options defined.
Heinrich Schuchardta6ccba02019-07-25 20:52:23 +02001253 *
1254 * efidebug boot dump
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001255 */
Simon Glass09140112020-05-10 11:40:03 -06001256static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
1257 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001258{
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001259 u16 *var_name16, *p;
1260 efi_uintn_t buf_size, size;
1261 efi_guid_t guid;
1262 int id, i, digit;
1263 efi_status_t ret;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001264
1265 if (argc > 1)
1266 return CMD_RET_USAGE;
1267
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001268 buf_size = 128;
1269 var_name16 = malloc(buf_size);
1270 if (!var_name16)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001271 return CMD_RET_FAILURE;
1272
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001273 var_name16[0] = 0;
1274 for (;;) {
1275 size = buf_size;
1276 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
1277 &guid));
1278 if (ret == EFI_NOT_FOUND)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001279 break;
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001280 if (ret == EFI_BUFFER_TOO_SMALL) {
1281 buf_size = size;
1282 p = realloc(var_name16, buf_size);
1283 if (!p) {
1284 free(var_name16);
1285 return CMD_RET_FAILURE;
1286 }
1287 var_name16 = p;
1288 ret = EFI_CALL(efi_get_next_variable_name(&size,
1289 var_name16,
1290 &guid));
1291 }
1292 if (ret != EFI_SUCCESS) {
1293 free(var_name16);
1294 return CMD_RET_FAILURE;
1295 }
1296
1297 if (memcmp(var_name16, L"Boot", 8))
1298 continue;
1299
1300 for (id = 0, i = 0; i < 4; i++) {
1301 digit = u16_tohex(var_name16[4 + i]);
1302 if (digit < 0)
1303 break;
1304 id = (id << 4) + digit;
1305 }
1306 if (i == 4 && !var_name16[8])
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001307 show_efi_boot_opt(var_name16);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001308 }
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001309
1310 free(var_name16);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001311
1312 return CMD_RET_SUCCESS;
1313}
1314
1315/**
1316 * show_efi_boot_order() - show order of UEFI load options
1317 *
1318 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1319 *
1320 * Show order of UEFI load options defined by BootOrder variable.
1321 */
1322static int show_efi_boot_order(void)
1323{
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001324 u16 *bootorder;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001325 efi_uintn_t size;
1326 int num, i;
1327 char var_name[9];
1328 u16 var_name16[9], *p16;
1329 void *data;
1330 struct efi_load_option lo;
1331 char *label, *p;
1332 size_t label_len16, label_len;
1333 efi_status_t ret;
1334
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001335 size = 0;
Heinrich Schuchardta30c7232020-05-02 16:08:37 +02001336 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001337 NULL, &size, NULL));
1338 if (ret != EFI_BUFFER_TOO_SMALL) {
1339 if (ret == EFI_NOT_FOUND) {
1340 printf("BootOrder not defined\n");
1341 return CMD_RET_SUCCESS;
1342 } else {
1343 return CMD_RET_FAILURE;
1344 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001345 }
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001346 bootorder = malloc(size);
1347 if (!bootorder) {
1348 printf("ERROR: Out of memory\n");
1349 return CMD_RET_FAILURE;
1350 }
1351 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
1352 NULL, &size, bootorder));
1353 if (ret != EFI_SUCCESS) {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001354 ret = CMD_RET_FAILURE;
1355 goto out;
1356 }
1357
1358 num = size / sizeof(u16);
1359 for (i = 0; i < num; i++) {
1360 sprintf(var_name, "Boot%04X", bootorder[i]);
1361 p16 = var_name16;
1362 utf8_utf16_strncpy(&p16, var_name, 9);
1363
1364 size = 0;
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001365 ret = EFI_CALL(efi_get_variable(var_name16,
1366 &efi_global_variable_guid, NULL,
1367 &size, NULL));
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001368 if (ret != EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001369 printf("%2d: %s: (not defined)\n", i + 1, var_name);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001370 continue;
1371 }
1372
1373 data = malloc(size);
1374 if (!data) {
1375 ret = CMD_RET_FAILURE;
1376 goto out;
1377 }
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001378 ret = EFI_CALL(efi_get_variable(var_name16,
1379 &efi_global_variable_guid, NULL,
1380 &size, data));
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001381 if (ret != EFI_SUCCESS) {
1382 free(data);
1383 ret = CMD_RET_FAILURE;
1384 goto out;
1385 }
1386
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +02001387 ret = efi_deserialize_load_option(&lo, data, &size);
1388 if (ret != EFI_SUCCESS) {
1389 printf("%ls: invalid load option\n", var_name16);
1390 ret = CMD_RET_FAILURE;
1391 goto out;
1392 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001393
1394 label_len16 = u16_strlen(lo.label);
1395 label_len = utf16_utf8_strnlen(lo.label, label_len16);
1396 label = malloc(label_len + 1);
1397 if (!label) {
1398 free(data);
1399 ret = CMD_RET_FAILURE;
1400 goto out;
1401 }
1402 p = label;
1403 utf16_utf8_strncpy(&p, lo.label, label_len16);
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001404 printf("%2d: %s: %s\n", i + 1, var_name, label);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001405 free(label);
1406
1407 free(data);
1408 }
1409out:
1410 free(bootorder);
1411
1412 return ret;
1413}
1414
1415/**
1416 * do_efi_boot_next() - manage UEFI BootNext variable
1417 *
1418 * @cmdtp: Command table
1419 * @flag: Command flag
1420 * @argc: Number of arguments
1421 * @argv: Argument array
1422 * Return: CMD_RET_SUCCESS on success,
1423 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1424 *
1425 * Implement efidebug "boot next" sub-command.
1426 * Set BootNext variable.
Heinrich Schuchardt0b016562019-07-14 14:00:41 +02001427 *
1428 * efidebug boot next <id>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001429 */
Simon Glass09140112020-05-10 11:40:03 -06001430static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
1431 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001432{
1433 u16 bootnext;
1434 efi_uintn_t size;
1435 char *endp;
1436 efi_guid_t guid;
1437 efi_status_t ret;
Heinrich Schuchardta332f252019-06-20 12:59:45 +02001438 int r = CMD_RET_SUCCESS;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001439
1440 if (argc != 2)
1441 return CMD_RET_USAGE;
1442
1443 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
Heinrich Schuchardtbdb15772020-05-09 17:59:19 +02001444 if (*endp) {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001445 printf("invalid value: %s\n", argv[1]);
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001446 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001447 goto out;
1448 }
1449
1450 guid = efi_global_variable_guid;
1451 size = sizeof(u16);
Heinrich Schuchardta30c7232020-05-02 16:08:37 +02001452 ret = EFI_CALL(efi_set_variable(L"BootNext", &guid,
AKASHI Takahirof658c2e2019-06-04 15:52:10 +09001453 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001454 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1455 EFI_VARIABLE_RUNTIME_ACCESS,
1456 size, &bootnext));
Heinrich Schuchardta332f252019-06-20 12:59:45 +02001457 if (ret != EFI_SUCCESS) {
1458 printf("Cannot set BootNext\n");
1459 r = CMD_RET_FAILURE;
1460 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001461out:
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001462 return r;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001463}
1464
1465/**
1466 * do_efi_boot_order() - manage UEFI BootOrder variable
1467 *
1468 * @cmdtp: Command table
1469 * @flag: Command flag
1470 * @argc: Number of arguments
1471 * @argv: Argument array
1472 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1473 *
1474 * Implement efidebug "boot order" sub-command.
1475 * Show order of UEFI load options, or change it in BootOrder variable.
Heinrich Schuchardt0b016562019-07-14 14:00:41 +02001476 *
1477 * efidebug boot order [<id> ...]
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001478 */
Simon Glass09140112020-05-10 11:40:03 -06001479static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
1480 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001481{
1482 u16 *bootorder = NULL;
1483 efi_uintn_t size;
1484 int id, i;
1485 char *endp;
1486 efi_guid_t guid;
1487 efi_status_t ret;
Heinrich Schuchardta332f252019-06-20 12:59:45 +02001488 int r = CMD_RET_SUCCESS;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001489
1490 if (argc == 1)
1491 return show_efi_boot_order();
1492
1493 argc--;
1494 argv++;
1495
1496 size = argc * sizeof(u16);
1497 bootorder = malloc(size);
1498 if (!bootorder)
1499 return CMD_RET_FAILURE;
1500
1501 for (i = 0; i < argc; i++) {
1502 id = (int)simple_strtoul(argv[i], &endp, 16);
1503 if (*endp != '\0' || id > 0xffff) {
1504 printf("invalid value: %s\n", argv[i]);
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001505 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001506 goto out;
1507 }
1508
1509 bootorder[i] = (u16)id;
1510 }
1511
1512 guid = efi_global_variable_guid;
Heinrich Schuchardta30c7232020-05-02 16:08:37 +02001513 ret = EFI_CALL(efi_set_variable(L"BootOrder", &guid,
AKASHI Takahirof658c2e2019-06-04 15:52:10 +09001514 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001515 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1516 EFI_VARIABLE_RUNTIME_ACCESS,
1517 size, bootorder));
Heinrich Schuchardta332f252019-06-20 12:59:45 +02001518 if (ret != EFI_SUCCESS) {
1519 printf("Cannot set BootOrder\n");
1520 r = CMD_RET_FAILURE;
1521 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001522out:
1523 free(bootorder);
1524
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001525 return r;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001526}
1527
Simon Glass09140112020-05-10 11:40:03 -06001528static struct cmd_tbl cmd_efidebug_boot_sub[] = {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001529 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1530 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1531 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1532 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1533 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1534 "", ""),
1535};
1536
1537/**
1538 * do_efi_boot_opt() - manage UEFI load options
1539 *
1540 * @cmdtp: Command table
1541 * @flag: Command flag
1542 * @argc: Number of arguments
1543 * @argv: Argument array
1544 * Return: CMD_RET_SUCCESS on success,
1545 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1546 *
1547 * Implement efidebug "boot" sub-command.
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001548 */
Simon Glass09140112020-05-10 11:40:03 -06001549static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
1550 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001551{
Simon Glass09140112020-05-10 11:40:03 -06001552 struct cmd_tbl *cp;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001553
1554 if (argc < 2)
1555 return CMD_RET_USAGE;
1556
1557 argc--; argv++;
1558
1559 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1560 ARRAY_SIZE(cmd_efidebug_boot_sub));
1561 if (!cp)
1562 return CMD_RET_USAGE;
1563
1564 return cp->cmd(cmdtp, flag, argc, argv);
1565}
1566
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001567/**
1568 * do_efi_test_bootmgr() - run simple bootmgr for test
1569 *
1570 * @cmdtp: Command table
1571 * @flag: Command flag
1572 * @argc: Number of arguments
1573 * @argv: Argument array
1574 * Return: CMD_RET_SUCCESS on success,
1575 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1576 *
1577 * Implement efidebug "test bootmgr" sub-command.
1578 * Run simple bootmgr for test.
1579 *
1580 * efidebug test bootmgr
1581 */
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +01001582static __maybe_unused int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
1583 int argc, char * const argv[])
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001584{
1585 efi_handle_t image;
1586 efi_uintn_t exit_data_size = 0;
1587 u16 *exit_data = NULL;
1588 efi_status_t ret;
Heinrich Schuchardtbc78d222020-08-11 18:20:50 +02001589 void *load_options = NULL;
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001590
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +02001591 ret = efi_bootmgr_load(&image, &load_options);
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001592 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1593
1594 /* We call efi_start_image() even if error for test purpose. */
1595 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1596 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1597 if (ret && exit_data)
1598 efi_free_pool(exit_data);
1599
1600 efi_restore_gd();
1601
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +02001602 free(load_options);
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001603 return CMD_RET_SUCCESS;
1604}
1605
Simon Glass09140112020-05-10 11:40:03 -06001606static struct cmd_tbl cmd_efidebug_test_sub[] = {
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +01001607#ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001608 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1609 "", ""),
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +01001610#endif
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001611};
1612
1613/**
1614 * do_efi_test() - manage UEFI load options
1615 *
1616 * @cmdtp: Command table
1617 * @flag: Command flag
1618 * @argc: Number of arguments
1619 * @argv: Argument array
1620 * Return: CMD_RET_SUCCESS on success,
1621 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1622 *
1623 * Implement efidebug "test" sub-command.
1624 */
Simon Glass09140112020-05-10 11:40:03 -06001625static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001626 int argc, char * const argv[])
1627{
Simon Glass09140112020-05-10 11:40:03 -06001628 struct cmd_tbl *cp;
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001629
1630 if (argc < 2)
1631 return CMD_RET_USAGE;
1632
1633 argc--; argv++;
1634
1635 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1636 ARRAY_SIZE(cmd_efidebug_test_sub));
1637 if (!cp)
1638 return CMD_RET_USAGE;
1639
1640 return cp->cmd(cmdtp, flag, argc, argv);
1641}
1642
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001643/**
1644 * do_efi_query_info() - QueryVariableInfo EFI service
1645 *
1646 * @cmdtp: Command table
1647 * @flag: Command flag
1648 * @argc: Number of arguments
1649 * @argv: Argument array
1650 * Return: CMD_RET_SUCCESS on success,
1651 * CMD_RET_USAGE or CMD_RET_FAILURE on failure
1652 *
1653 * Implement efidebug "test" sub-command.
1654 */
1655
Simon Glass09140112020-05-10 11:40:03 -06001656static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001657 int argc, char * const argv[])
1658{
1659 efi_status_t ret;
1660 u32 attr = 0;
1661 u64 max_variable_storage_size;
1662 u64 remain_variable_storage_size;
1663 u64 max_variable_size;
1664 int i;
1665
1666 for (i = 1; i < argc; i++) {
1667 if (!strcmp(argv[i], "-bs"))
1668 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
1669 else if (!strcmp(argv[i], "-rt"))
1670 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
1671 else if (!strcmp(argv[i], "-nv"))
1672 attr |= EFI_VARIABLE_NON_VOLATILE;
1673 else if (!strcmp(argv[i], "-at"))
1674 attr |=
1675 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1676 }
1677
1678 ret = EFI_CALL(efi_query_variable_info(attr,
1679 &max_variable_storage_size,
1680 &remain_variable_storage_size,
1681 &max_variable_size));
1682 if (ret != EFI_SUCCESS) {
1683 printf("Error: Cannot query UEFI variables, r = %lu\n",
1684 ret & ~EFI_ERROR_MASK);
1685 return CMD_RET_FAILURE;
1686 }
1687
1688 printf("Max storage size %llu\n", max_variable_storage_size);
1689 printf("Remaining storage size %llu\n", remain_variable_storage_size);
1690 printf("Max variable size %llu\n", max_variable_size);
1691
1692 return CMD_RET_SUCCESS;
1693}
1694
Simon Glass09140112020-05-10 11:40:03 -06001695static struct cmd_tbl cmd_efidebug_sub[] = {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001696 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +09001697#ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1698 U_BOOT_CMD_MKENT(capsule, CONFIG_SYS_MAXARGS, 1, do_efi_capsule,
1699 "", ""),
1700#endif
AKASHI Takahiro355cdb52019-02-25 15:54:39 +09001701 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1702 "", ""),
AKASHI Takahiro66eaf562019-02-25 15:54:40 +09001703 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1704 "", ""),
AKASHI Takahiroa8014622019-02-25 15:54:41 +09001705 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1706 "", ""),
AKASHI Takahirofa536732019-02-25 15:54:42 +09001707 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1708 "", ""),
AKASHI Takahiro00358bb2019-02-25 15:54:43 +09001709 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1710 "", ""),
Heinrich Schuchardt986e0642020-01-07 05:57:47 +01001711 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1712 "", ""),
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001713 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1714 "", ""),
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001715 U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
1716 "", ""),
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001717};
1718
1719/**
1720 * do_efidebug() - display and configure UEFI environment
1721 *
1722 * @cmdtp: Command table
1723 * @flag: Command flag
1724 * @argc: Number of arguments
1725 * @argv: Argument array
1726 * Return: CMD_RET_SUCCESS on success,
1727 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1728 *
1729 * Implement efidebug command which allows us to display and
1730 * configure UEFI environment.
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001731 */
Simon Glass09140112020-05-10 11:40:03 -06001732static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
1733 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001734{
Simon Glass09140112020-05-10 11:40:03 -06001735 struct cmd_tbl *cp;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001736 efi_status_t r;
1737
1738 if (argc < 2)
1739 return CMD_RET_USAGE;
1740
1741 argc--; argv++;
1742
1743 /* Initialize UEFI drivers */
1744 r = efi_init_obj_list();
1745 if (r != EFI_SUCCESS) {
1746 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1747 r & ~EFI_ERROR_MASK);
1748 return CMD_RET_FAILURE;
1749 }
1750
1751 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1752 ARRAY_SIZE(cmd_efidebug_sub));
1753 if (!cp)
1754 return CMD_RET_USAGE;
1755
1756 return cp->cmd(cmdtp, flag, argc, argv);
1757}
1758
1759#ifdef CONFIG_SYS_LONGHELP
1760static char efidebug_help_text[] =
1761 " - UEFI Shell-like interface to configure UEFI environment\n"
1762 "\n"
Ilias Apalodimascbea2412021-03-17 21:55:01 +02001763 "efidebug boot add "
1764 "-b <bootid> <label> <interface> <devnum>[:<part>] <file path> "
1765 "-i <interface> <devnum>[:<part>] <initrd file path> "
1766 "-s '<optional data>'\n"
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001767 " - set UEFI BootXXXX variable\n"
1768 " <load options> will be passed to UEFI application\n"
1769 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1770 " - delete UEFI BootXXXX variables\n"
1771 "efidebug boot dump\n"
1772 " - dump all UEFI BootXXXX variables\n"
1773 "efidebug boot next <bootid>\n"
1774 " - set UEFI BootNext variable\n"
1775 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1776 " - set/show UEFI boot order\n"
AKASHI Takahiro355cdb52019-02-25 15:54:39 +09001777 "\n"
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +09001778#ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1779 "efidebug capsule update [-v] <capsule address>\n"
1780 " - process a capsule\n"
Sughosh Ganu74075952020-12-30 19:27:11 +05301781 "efidebug capsule disk-update\n"
1782 " - update a capsule from disk\n"
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +09001783 "efidebug capsule show <capsule address>\n"
1784 " - show capsule information\n"
1785 "efidebug capsule result [<capsule result var>]\n"
1786 " - show a capsule update result\n"
Jose Marinhoaa31a872021-03-11 13:18:51 +00001787#ifdef CONFIG_EFI_ESRT
1788 "efidebug capsule esrt\n"
1789 " - print the ESRT\n"
1790#endif
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +09001791 "\n"
1792#endif
AKASHI Takahiro355cdb52019-02-25 15:54:39 +09001793 "efidebug devices\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001794 " - show UEFI devices\n"
AKASHI Takahiro66eaf562019-02-25 15:54:40 +09001795 "efidebug drivers\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001796 " - show UEFI drivers\n"
AKASHI Takahiroa8014622019-02-25 15:54:41 +09001797 "efidebug dh\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001798 " - show UEFI handles\n"
AKASHI Takahirofa536732019-02-25 15:54:42 +09001799 "efidebug images\n"
AKASHI Takahiro00358bb2019-02-25 15:54:43 +09001800 " - show loaded images\n"
1801 "efidebug memmap\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001802 " - show UEFI memory map\n"
Heinrich Schuchardt986e0642020-01-07 05:57:47 +01001803 "efidebug tables\n"
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001804 " - show UEFI configuration tables\n"
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +01001805#ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001806 "efidebug test bootmgr\n"
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001807 " - run simple bootmgr for test\n"
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +01001808#endif
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001809 "efidebug query [-nv][-bs][-rt][-at]\n"
1810 " - show size of UEFI variables store\n";
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001811#endif
1812
1813U_BOOT_CMD(
Ilias Apalodimascbea2412021-03-17 21:55:01 +02001814 efidebug, CONFIG_SYS_MAXARGS, 0, do_efidebug,
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001815 "Configure UEFI environment",
1816 efidebug_help_text
1817);