blob: 83bc2196a5a96c36a2a42121c01e631d76de0dcc [file] [log] [blame]
AKASHI Takahiroe7c08832019-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 Schuchardt2c1e2242020-12-13 10:30:24 +010011#include <efi_dt_fixup.h>
AKASHI Takahiroe7c08832019-02-25 15:54:38 +090012#include <efi_loader.h>
Heinrich Schuchardt50adeff2020-09-27 14:50:25 +020013#include <efi_rng.h>
AKASHI Takahiroe7c08832019-02-25 15:54:38 +090014#include <exports.h>
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +020015#include <hexdump.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
AKASHI Takahiroe7c08832019-02-25 15:54:38 +090017#include <malloc.h>
Heinrich Schuchardtfec10a42020-03-14 08:44:07 +010018#include <mapmem.h>
AKASHI Takahiroe7c08832019-02-25 15:54:38 +090019#include <search.h>
20#include <linux/ctype.h>
21
AKASHI Takahiroe32788a2019-02-25 15:54:39 +090022#define BS systab.boottime
AKASHI Takahiro38833c82020-11-17 09:28:01 +090023#define RT systab.runtime
AKASHI Takahiroe7c08832019-02-25 15:54:38 +090024
AKASHI Takahiro38833c82020-11-17 09:28:01 +090025#ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
26/**
27 * do_efi_capsule_update() - process a capsule update
28 *
29 * @cmdtp: Command table
30 * @flag: Command flag
31 * @argc: Number of arguments
32 * @argv: Argument array
33 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
34 *
35 * Implement efidebug "capsule update" sub-command.
36 * process a capsule update.
37 *
38 * efidebug capsule update [-v] <capsule address>
39 */
40static int do_efi_capsule_update(struct cmd_tbl *cmdtp, int flag,
41 int argc, char * const argv[])
42{
43 struct efi_capsule_header *capsule;
44 int verbose = 0;
45 char *endp;
46 efi_status_t ret;
47
48 if (argc != 2 && argc != 3)
49 return CMD_RET_USAGE;
50
51 if (argc == 3) {
52 if (strcmp(argv[1], "-v"))
53 return CMD_RET_USAGE;
54
55 verbose = 1;
56 argc--;
57 argv++;
58 }
59
60 capsule = (typeof(capsule))simple_strtoul(argv[1], &endp, 16);
61 if (endp == argv[1]) {
62 printf("Invalid address: %s", argv[1]);
63 return CMD_RET_FAILURE;
64 }
65
66 if (verbose) {
67 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
68 printf("Capsule flags: 0x%x\n", capsule->flags);
69 printf("Capsule header size: 0x%x\n", capsule->header_size);
70 printf("Capsule image size: 0x%x\n",
71 capsule->capsule_image_size);
72 }
73
74 ret = EFI_CALL(RT->update_capsule(&capsule, 1, (u64)NULL));
75 if (ret) {
76 printf("Cannot handle a capsule at %p", capsule);
77 return CMD_RET_FAILURE;
78 }
79
80 return CMD_RET_SUCCESS;
81}
82
Sughosh Ganue6f9efc2020-12-30 19:27:11 +053083static int do_efi_capsule_on_disk_update(struct cmd_tbl *cmdtp, int flag,
84 int argc, char * const argv[])
85{
86 efi_status_t ret;
87
88 ret = efi_launch_capsules();
89
90 return ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
91}
92
AKASHI Takahiro38833c82020-11-17 09:28:01 +090093/**
94 * do_efi_capsule_show() - show capsule information
95 *
96 * @cmdtp: Command table
97 * @flag: Command flag
98 * @argc: Number of arguments
99 * @argv: Argument array
100 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
101 *
102 * Implement efidebug "capsule show" sub-command.
103 * show capsule information.
104 *
105 * efidebug capsule show <capsule address>
106 */
107static int do_efi_capsule_show(struct cmd_tbl *cmdtp, int flag,
108 int argc, char * const argv[])
109{
110 struct efi_capsule_header *capsule;
111 char *endp;
112
113 if (argc != 2)
114 return CMD_RET_USAGE;
115
116 capsule = (typeof(capsule))simple_strtoul(argv[1], &endp, 16);
117 if (endp == argv[1]) {
118 printf("Invalid address: %s", argv[1]);
119 return CMD_RET_FAILURE;
120 }
121
122 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
123 printf("Capsule flags: 0x%x\n", capsule->flags);
124 printf("Capsule header size: 0x%x\n", capsule->header_size);
125 printf("Capsule image size: 0x%x\n",
126 capsule->capsule_image_size);
127
128 return CMD_RET_SUCCESS;
129}
130
131/**
132 * do_efi_capsule_res() - show a capsule update result
133 *
134 * @cmdtp: Command table
135 * @flag: Command flag
136 * @argc: Number of arguments
137 * @argv: Argument array
138 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
139 *
140 * Implement efidebug "capsule result" sub-command.
141 * show a capsule update result.
142 * If result number is not specified, CapsuleLast will be shown.
143 *
144 * efidebug capsule result [<capsule result number>]
145 */
146static int do_efi_capsule_res(struct cmd_tbl *cmdtp, int flag,
147 int argc, char * const argv[])
148{
149 int capsule_id;
150 char *endp;
151 char var_name[12];
152 u16 var_name16[12], *p;
153 efi_guid_t guid;
154 struct efi_capsule_result_variable_header *result = NULL;
155 efi_uintn_t size;
156 efi_status_t ret;
157
158 if (argc != 1 && argc != 2)
159 return CMD_RET_USAGE;
160
161 guid = efi_guid_capsule_report;
162 if (argc == 1) {
163 size = sizeof(var_name16);
164 ret = EFI_CALL(RT->get_variable(L"CapsuleLast", &guid, NULL,
165 &size, var_name16));
166 if (ret != EFI_SUCCESS) {
167 if (ret == EFI_NOT_FOUND)
168 printf("CapsuleLast doesn't exist\n");
169 else
170 printf("Failed to get CapsuleLast\n");
171
172 return CMD_RET_FAILURE;
173 }
174 printf("CapsuleLast is %ls\n", var_name16);
175 } else {
176 argc--;
177 argv++;
178
179 capsule_id = simple_strtoul(argv[0], &endp, 16);
180 if (capsule_id < 0 || capsule_id > 0xffff)
181 return CMD_RET_USAGE;
182
183 sprintf(var_name, "Capsule%04X", capsule_id);
184 p = var_name16;
185 utf8_utf16_strncpy(&p, var_name, 9);
186 }
187
188 size = 0;
189 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
190 if (ret == EFI_BUFFER_TOO_SMALL) {
191 result = malloc(size);
AKASHI Takahiro1af16732021-01-22 10:42:48 +0900192 if (!result)
193 return CMD_RET_FAILURE;
AKASHI Takahiro38833c82020-11-17 09:28:01 +0900194 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
195 result));
AKASHI Takahiro1af16732021-01-22 10:42:48 +0900196 }
197 if (ret != EFI_SUCCESS) {
198 free(result);
199 printf("Failed to get %ls\n", var_name16);
AKASHI Takahiro38833c82020-11-17 09:28:01 +0900200
AKASHI Takahiro1af16732021-01-22 10:42:48 +0900201 return CMD_RET_FAILURE;
AKASHI Takahiro38833c82020-11-17 09:28:01 +0900202 }
203
204 printf("Result total size: 0x%x\n", result->variable_total_size);
205 printf("Capsule guid: %pUl\n", &result->capsule_guid);
206 printf("Time processed: %04d-%02d-%02d %02d:%02d:%02d\n",
207 result->capsule_processed.year, result->capsule_processed.month,
208 result->capsule_processed.day, result->capsule_processed.hour,
209 result->capsule_processed.minute,
210 result->capsule_processed.second);
211 printf("Capsule status: 0x%lx\n", result->capsule_status);
212
213 free(result);
214
215 return CMD_RET_SUCCESS;
216}
217
218static struct cmd_tbl cmd_efidebug_capsule_sub[] = {
219 U_BOOT_CMD_MKENT(update, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_update,
220 "", ""),
221 U_BOOT_CMD_MKENT(show, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_show,
222 "", ""),
Sughosh Ganue6f9efc2020-12-30 19:27:11 +0530223 U_BOOT_CMD_MKENT(disk-update, 0, 0, do_efi_capsule_on_disk_update,
224 "", ""),
AKASHI Takahiro38833c82020-11-17 09:28:01 +0900225 U_BOOT_CMD_MKENT(result, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_res,
226 "", ""),
227};
228
229/**
230 * do_efi_capsule() - manage UEFI capsules
231 *
232 * @cmdtp: Command table
233 * @flag: Command flag
234 * @argc: Number of arguments
235 * @argv: Argument array
236 * Return: CMD_RET_SUCCESS on success,
237 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
238 *
239 * Implement efidebug "capsule" sub-command.
240 */
241static int do_efi_capsule(struct cmd_tbl *cmdtp, int flag,
242 int argc, char * const argv[])
243{
244 struct cmd_tbl *cp;
245
246 if (argc < 2)
247 return CMD_RET_USAGE;
248
249 argc--; argv++;
250
251 cp = find_cmd_tbl(argv[0], cmd_efidebug_capsule_sub,
252 ARRAY_SIZE(cmd_efidebug_capsule_sub));
253 if (!cp)
254 return CMD_RET_USAGE;
255
256 return cp->cmd(cmdtp, flag, argc, argv);
257}
258#endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT */
259
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900260/**
AKASHI Takahiroe32788a2019-02-25 15:54:39 +0900261 * efi_get_device_handle_info() - get information of UEFI device
262 *
263 * @handle: Handle of UEFI device
264 * @dev_path_text: Pointer to text of device path
265 * Return: 0 on success, -1 on failure
266 *
267 * Currently return a formatted text of device path.
268 */
269static int efi_get_device_handle_info(efi_handle_t handle, u16 **dev_path_text)
270{
271 struct efi_device_path *dp;
272 efi_status_t ret;
273
274 ret = EFI_CALL(BS->open_protocol(handle, &efi_guid_device_path,
275 (void **)&dp, NULL /* FIXME */, NULL,
276 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
277 if (ret == EFI_SUCCESS) {
278 *dev_path_text = efi_dp_str(dp);
279 return 0;
280 } else {
281 return -1;
282 }
283}
284
285#define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
286
287static const char spc[] = " ";
288static const char sep[] = "================";
289
290/**
291 * do_efi_show_devices() - show UEFI devices
292 *
293 * @cmdtp: Command table
294 * @flag: Command flag
295 * @argc: Number of arguments
296 * @argv: Argument array
297 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
298 *
299 * Implement efidebug "devices" sub-command.
300 * Show all UEFI devices and their information.
301 */
Simon Glassed38aef2020-05-10 11:40:03 -0600302static int do_efi_show_devices(struct cmd_tbl *cmdtp, int flag,
303 int argc, char *const argv[])
AKASHI Takahiroe32788a2019-02-25 15:54:39 +0900304{
305 efi_handle_t *handles;
306 efi_uintn_t num, i;
307 u16 *dev_path_text;
308 efi_status_t ret;
309
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200310 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
AKASHI Takahiroe32788a2019-02-25 15:54:39 +0900311 &num, &handles));
312 if (ret != EFI_SUCCESS)
313 return CMD_RET_FAILURE;
314
315 if (!num)
316 return CMD_RET_SUCCESS;
317
318 printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc);
319 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
320 for (i = 0; i < num; i++) {
321 if (!efi_get_device_handle_info(handles[i], &dev_path_text)) {
322 printf("%p %ls\n", handles[i], dev_path_text);
323 efi_free_pool(dev_path_text);
324 }
325 }
326
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200327 efi_free_pool(handles);
AKASHI Takahiroe32788a2019-02-25 15:54:39 +0900328
329 return CMD_RET_SUCCESS;
330}
331
332/**
AKASHI Takahiro185716a2019-02-25 15:54:40 +0900333 * efi_get_driver_handle_info() - get information of UEFI driver
334 *
335 * @handle: Handle of UEFI device
336 * @driver_name: Driver name
337 * @image_path: Pointer to text of device path
338 * Return: 0 on success, -1 on failure
339 *
340 * Currently return no useful information as all UEFI drivers are
341 * built-in..
342 */
343static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
344 u16 **image_path)
345{
346 struct efi_handler *handler;
347 struct efi_loaded_image *image;
348 efi_status_t ret;
349
350 /*
351 * driver name
352 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
353 */
354 *driver_name = NULL;
355
356 /* image name */
357 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
358 if (ret != EFI_SUCCESS) {
359 *image_path = NULL;
360 return 0;
361 }
362
363 image = handler->protocol_interface;
364 *image_path = efi_dp_str(image->file_path);
365
366 return 0;
367}
368
369/**
370 * do_efi_show_drivers() - show UEFI drivers
371 *
372 * @cmdtp: Command table
373 * @flag: Command flag
374 * @argc: Number of arguments
375 * @argv: Argument array
376 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
377 *
378 * Implement efidebug "drivers" sub-command.
379 * Show all UEFI drivers and their information.
380 */
Simon Glassed38aef2020-05-10 11:40:03 -0600381static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
382 int argc, char *const argv[])
AKASHI Takahiro185716a2019-02-25 15:54:40 +0900383{
384 efi_handle_t *handles;
385 efi_uintn_t num, i;
386 u16 *driver_name, *image_path_text;
387 efi_status_t ret;
388
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200389 ret = EFI_CALL(efi_locate_handle_buffer(
AKASHI Takahiro185716a2019-02-25 15:54:40 +0900390 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
391 NULL, &num, &handles));
392 if (ret != EFI_SUCCESS)
393 return CMD_RET_FAILURE;
394
395 if (!num)
396 return CMD_RET_SUCCESS;
397
398 printf("Driver%.*s Name Image Path\n",
399 EFI_HANDLE_WIDTH - 6, spc);
400 printf("%.*s ==================== ====================\n",
401 EFI_HANDLE_WIDTH, sep);
402 for (i = 0; i < num; i++) {
403 if (!efi_get_driver_handle_info(handles[i], &driver_name,
404 &image_path_text)) {
405 if (image_path_text)
406 printf("%p %-20ls %ls\n", handles[i],
407 driver_name, image_path_text);
408 else
409 printf("%p %-20ls <built-in>\n",
410 handles[i], driver_name);
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200411 efi_free_pool(driver_name);
412 efi_free_pool(image_path_text);
AKASHI Takahiro185716a2019-02-25 15:54:40 +0900413 }
414 }
415
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200416 efi_free_pool(handles);
AKASHI Takahiro185716a2019-02-25 15:54:40 +0900417
418 return CMD_RET_SUCCESS;
419}
420
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900421static const struct {
422 const char *text;
423 const efi_guid_t guid;
424} guid_list[] = {
425 {
426 "Device Path",
Heinrich Schuchardt788ad412019-04-20 07:39:11 +0200427 EFI_DEVICE_PATH_PROTOCOL_GUID,
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900428 },
429 {
430 "Device Path To Text",
431 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
432 },
433 {
434 "Device Path Utilities",
435 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
436 },
437 {
438 "Unicode Collation 2",
439 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
440 },
441 {
442 "Driver Binding",
443 EFI_DRIVER_BINDING_PROTOCOL_GUID,
444 },
445 {
446 "Simple Text Input",
447 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
448 },
449 {
450 "Simple Text Input Ex",
451 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
452 },
453 {
454 "Simple Text Output",
455 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
456 },
457 {
458 "Block IO",
Heinrich Schuchardt788ad412019-04-20 07:39:11 +0200459 EFI_BLOCK_IO_PROTOCOL_GUID,
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900460 },
461 {
462 "Simple File System",
463 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
464 },
465 {
466 "Loaded Image",
Heinrich Schuchardt788ad412019-04-20 07:39:11 +0200467 EFI_LOADED_IMAGE_PROTOCOL_GUID,
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900468 },
469 {
Heinrich Schuchardt788ad412019-04-20 07:39:11 +0200470 "Graphics Output",
471 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900472 },
Heinrich Schuchardtae776902019-04-20 07:57:28 +0200473 {
474 "HII String",
475 EFI_HII_STRING_PROTOCOL_GUID,
476 },
477 {
478 "HII Database",
479 EFI_HII_DATABASE_PROTOCOL_GUID,
480 },
481 {
482 "HII Config Routing",
483 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
484 },
485 {
Ilias Apalodimas3510ba72020-02-21 09:55:45 +0200486 "Load File2",
487 EFI_LOAD_FILE2_PROTOCOL_GUID,
488 },
489 {
Heinrich Schuchardt50adeff2020-09-27 14:50:25 +0200490 "Random Number Generator",
491 EFI_RNG_PROTOCOL_GUID,
492 },
493 {
Heinrich Schuchardtae776902019-04-20 07:57:28 +0200494 "Simple Network",
495 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
496 },
497 {
498 "PXE Base Code",
499 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
500 },
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100501 {
502 "Device-Tree Fixup",
503 EFI_DT_FIXUP_PROTOCOL_GUID,
504 },
Heinrich Schuchardt75844e52020-01-07 05:57:47 +0100505 /* Configuration table GUIDs */
506 {
507 "ACPI table",
508 EFI_ACPI_TABLE_GUID,
509 },
510 {
511 "device tree",
512 EFI_FDT_GUID,
513 },
514 {
515 "SMBIOS table",
516 SMBIOS_TABLE_GUID,
517 },
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100518 {
519 "Runtime properties",
520 EFI_RT_PROPERTIES_TABLE_GUID,
521 },
Ilias Apalodimasa51fb1c2020-11-30 11:47:41 +0200522 {
523 "TCG2 Final Events Table",
524 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
525 },
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900526};
527
528/**
Heinrich Schuchardtc6fc5022020-01-07 06:02:33 +0100529 * get_guid_text - get string of GUID
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900530 *
Heinrich Schuchardtc6fc5022020-01-07 06:02:33 +0100531 * Return description of GUID.
532 *
533 * @guid: GUID
534 * Return: description of GUID or NULL
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900535 */
Heinrich Schuchardtc6fc5022020-01-07 06:02:33 +0100536static const char *get_guid_text(const void *guid)
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900537{
538 int i;
539
Heinrich Schuchardtc6fc5022020-01-07 06:02:33 +0100540 for (i = 0; i < ARRAY_SIZE(guid_list); i++) {
541 /*
542 * As guidcmp uses memcmp() we can safely accept unaligned
543 * GUIDs.
544 */
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900545 if (!guidcmp(&guid_list[i].guid, guid))
Heinrich Schuchardtc6fc5022020-01-07 06:02:33 +0100546 return guid_list[i].text;
547 }
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900548
Heinrich Schuchardtc6fc5022020-01-07 06:02:33 +0100549 return NULL;
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900550}
551
552/**
553 * do_efi_show_handles() - show UEFI handles
554 *
555 * @cmdtp: Command table
556 * @flag: Command flag
557 * @argc: Number of arguments
558 * @argv: Argument array
559 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
560 *
561 * Implement efidebug "dh" sub-command.
562 * Show all UEFI handles and their information, currently all protocols
563 * added to handle.
564 */
Simon Glassed38aef2020-05-10 11:40:03 -0600565static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
566 int argc, char *const argv[])
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900567{
568 efi_handle_t *handles;
569 efi_guid_t **guid;
570 efi_uintn_t num, count, i, j;
571 const char *guid_text;
572 efi_status_t ret;
573
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200574 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900575 &num, &handles));
576 if (ret != EFI_SUCCESS)
577 return CMD_RET_FAILURE;
578
579 if (!num)
580 return CMD_RET_SUCCESS;
581
582 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
583 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
584 for (i = 0; i < num; i++) {
585 printf("%p", handles[i]);
586 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
587 &count));
588 if (ret || !count) {
589 putc('\n');
590 continue;
591 }
592
593 for (j = 0; j < count; j++) {
594 if (j)
595 printf(", ");
596 else
597 putc(' ');
598
599 guid_text = get_guid_text(guid[j]);
600 if (guid_text)
601 puts(guid_text);
602 else
603 printf("%pUl", guid[j]);
604 }
605 putc('\n');
606 }
607
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200608 efi_free_pool(handles);
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900609
610 return CMD_RET_SUCCESS;
611}
612
AKASHI Takahiro185716a2019-02-25 15:54:40 +0900613/**
AKASHI Takahiro71ab1bf2019-02-25 15:54:42 +0900614 * do_efi_show_images() - show UEFI images
615 *
616 * @cmdtp: Command table
617 * @flag: Command flag
618 * @argc: Number of arguments
619 * @argv: Argument array
620 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
621 *
622 * Implement efidebug "images" sub-command.
623 * Show all UEFI loaded images and their information.
624 */
Simon Glassed38aef2020-05-10 11:40:03 -0600625static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
626 int argc, char *const argv[])
AKASHI Takahiro71ab1bf2019-02-25 15:54:42 +0900627{
628 efi_print_image_infos(NULL);
629
630 return CMD_RET_SUCCESS;
631}
632
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900633static const char * const efi_mem_type_string[] = {
634 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
635 [EFI_LOADER_CODE] = "LOADER CODE",
636 [EFI_LOADER_DATA] = "LOADER DATA",
637 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
638 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
639 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
640 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
641 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
642 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
643 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
644 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
645 [EFI_MMAP_IO] = "IO",
646 [EFI_MMAP_IO_PORT] = "IO PORT",
647 [EFI_PAL_CODE] = "PAL",
Heinrich Schuchardt45176e82020-04-29 20:21:39 +0200648 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900649};
650
651static const struct efi_mem_attrs {
652 const u64 bit;
653 const char *text;
654} efi_mem_attrs[] = {
655 {EFI_MEMORY_UC, "UC"},
656 {EFI_MEMORY_UC, "UC"},
657 {EFI_MEMORY_WC, "WC"},
658 {EFI_MEMORY_WT, "WT"},
659 {EFI_MEMORY_WB, "WB"},
660 {EFI_MEMORY_UCE, "UCE"},
661 {EFI_MEMORY_WP, "WP"},
662 {EFI_MEMORY_RP, "RP"},
663 {EFI_MEMORY_XP, "WP"},
664 {EFI_MEMORY_NV, "NV"},
665 {EFI_MEMORY_MORE_RELIABLE, "REL"},
666 {EFI_MEMORY_RO, "RO"},
Heinrich Schuchardte76f0e42020-05-19 07:20:46 +0200667 {EFI_MEMORY_SP, "SP"},
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900668 {EFI_MEMORY_RUNTIME, "RT"},
669};
670
671/**
672 * print_memory_attributes() - print memory map attributes
Heinrich Schuchardtb1d65432019-07-14 14:00:41 +0200673 *
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900674 * @attributes: Attribute value
675 *
676 * Print memory map attributes
677 */
678static void print_memory_attributes(u64 attributes)
679{
680 int sep, i;
681
682 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
683 if (attributes & efi_mem_attrs[i].bit) {
684 if (sep) {
685 putc('|');
686 } else {
687 putc(' ');
688 sep = 1;
689 }
690 puts(efi_mem_attrs[i].text);
691 }
692}
693
694#define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
695
696/**
697 * do_efi_show_memmap() - show UEFI memory map
698 *
699 * @cmdtp: Command table
700 * @flag: Command flag
701 * @argc: Number of arguments
702 * @argv: Argument array
703 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
704 *
705 * Implement efidebug "memmap" sub-command.
706 * Show UEFI memory map.
707 */
Simon Glassed38aef2020-05-10 11:40:03 -0600708static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
709 int argc, char *const argv[])
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900710{
711 struct efi_mem_desc *memmap = NULL, *map;
712 efi_uintn_t map_size = 0;
713 const char *type;
714 int i;
715 efi_status_t ret;
716
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200717 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900718 if (ret == EFI_BUFFER_TOO_SMALL) {
719 map_size += sizeof(struct efi_mem_desc); /* for my own */
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200720 ret = efi_allocate_pool(EFI_LOADER_DATA, map_size,
721 (void *)&memmap);
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900722 if (ret != EFI_SUCCESS)
723 return CMD_RET_FAILURE;
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200724 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900725 }
726 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200727 efi_free_pool(memmap);
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900728 return CMD_RET_FAILURE;
729 }
730
731 printf("Type Start%.*s End%.*s Attributes\n",
732 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
733 printf("================ %.*s %.*s ==========\n",
734 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
AKASHI Takahiroac0abe22020-05-08 14:50:18 +0900735 /*
736 * Coverity check: dereferencing null pointer "map."
737 * This is a false positive as memmap will always be
738 * populated by allocate_pool() above.
739 */
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900740 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
Heinrich Schuchardt45176e82020-04-29 20:21:39 +0200741 if (map->type < ARRAY_SIZE(efi_mem_type_string))
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900742 type = efi_mem_type_string[map->type];
743 else
744 type = "(unknown)";
745
746 printf("%-16s %.*llx-%.*llx", type,
747 EFI_PHYS_ADDR_WIDTH,
Heinrich Schuchardt9d4c7882020-03-27 04:33:17 +0000748 (u64)map_to_sysmem((void *)(uintptr_t)
749 map->physical_start),
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900750 EFI_PHYS_ADDR_WIDTH,
Heinrich Schuchardt9d4c7882020-03-27 04:33:17 +0000751 (u64)map_to_sysmem((void *)(uintptr_t)
752 (map->physical_start +
753 map->num_pages * EFI_PAGE_SIZE)));
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900754
755 print_memory_attributes(map->attribute);
756 putc('\n');
757 }
758
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200759 efi_free_pool(memmap);
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900760
761 return CMD_RET_SUCCESS;
762}
763
AKASHI Takahiro71ab1bf2019-02-25 15:54:42 +0900764/**
Heinrich Schuchardt75844e52020-01-07 05:57:47 +0100765 * do_efi_show_tables() - show UEFI configuration tables
766 *
767 * @cmdtp: Command table
768 * @flag: Command flag
769 * @argc: Number of arguments
770 * @argv: Argument array
771 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
772 *
773 * Implement efidebug "tables" sub-command.
774 * Show UEFI configuration tables.
775 */
Simon Glassed38aef2020-05-10 11:40:03 -0600776static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
777 int argc, char *const argv[])
Heinrich Schuchardt75844e52020-01-07 05:57:47 +0100778{
779 efi_uintn_t i;
780 const char *guid_str;
781
782 for (i = 0; i < systab.nr_tables; ++i) {
783 guid_str = get_guid_text(&systab.tables[i].guid);
784 if (!guid_str)
785 guid_str = "";
786 printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
787 }
788
789 return CMD_RET_SUCCESS;
790}
791
792/**
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900793 * do_efi_boot_add() - set UEFI load option
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,
800 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
801 *
Heinrich Schuchardtb1d65432019-07-14 14:00:41 +0200802 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
803 *
804 * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900805 */
Simon Glassed38aef2020-05-10 11:40:03 -0600806static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
807 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900808{
809 int id;
810 char *endp;
811 char var_name[9];
812 u16 var_name16[9], *p;
813 efi_guid_t guid;
814 size_t label_len, label_len16;
815 u16 *label;
816 struct efi_device_path *device_path = NULL, *file_path = NULL;
817 struct efi_load_option lo;
818 void *data = NULL;
819 efi_uintn_t size;
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +0200820 efi_status_t ret;
Heinrich Schuchardt99a21fc2019-06-20 12:59:45 +0200821 int r = CMD_RET_SUCCESS;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900822
823 if (argc < 6 || argc > 7)
824 return CMD_RET_USAGE;
825
826 id = (int)simple_strtoul(argv[1], &endp, 16);
827 if (*endp != '\0' || id > 0xffff)
Heinrich Schuchardtb2469722019-02-28 20:41:58 +0100828 return CMD_RET_USAGE;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900829
830 sprintf(var_name, "Boot%04X", id);
831 p = var_name16;
832 utf8_utf16_strncpy(&p, var_name, 9);
833
834 guid = efi_global_variable_guid;
835
836 /* attributes */
837 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
838
839 /* label */
840 label_len = strlen(argv[2]);
841 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
842 label = malloc((label_len16 + 1) * sizeof(u16));
843 if (!label)
844 return CMD_RET_FAILURE;
845 lo.label = label; /* label will be changed below */
846 utf8_utf16_strncpy(&label, argv[2], label_len);
847
848 /* file path */
849 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
850 &file_path);
851 if (ret != EFI_SUCCESS) {
852 printf("Cannot create device path for \"%s %s\"\n",
853 argv[3], argv[4]);
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +0200854 r = CMD_RET_FAILURE;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900855 goto out;
856 }
857 lo.file_path = file_path;
858 lo.file_path_length = efi_dp_size(file_path)
859 + sizeof(struct efi_device_path); /* for END */
860
861 /* optional data */
AKASHI Takahiro085372e2020-05-08 14:50:47 +0900862 if (argc == 6)
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200863 lo.optional_data = NULL;
864 else
865 lo.optional_data = (const u8 *)argv[6];
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900866
867 size = efi_serialize_load_option(&lo, (u8 **)&data);
868 if (!size) {
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +0200869 r = CMD_RET_FAILURE;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900870 goto out;
871 }
872
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200873 ret = EFI_CALL(efi_set_variable(var_name16, &guid,
AKASHI Takahiroc1f3ebc2019-06-04 15:52:10 +0900874 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900875 EFI_VARIABLE_BOOTSERVICE_ACCESS |
876 EFI_VARIABLE_RUNTIME_ACCESS,
877 size, data));
Heinrich Schuchardt99a21fc2019-06-20 12:59:45 +0200878 if (ret != EFI_SUCCESS) {
879 printf("Cannot set %ls\n", var_name16);
880 r = CMD_RET_FAILURE;
881 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900882out:
883 free(data);
884 efi_free_pool(device_path);
885 efi_free_pool(file_path);
886 free(lo.label);
887
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +0200888 return r;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900889}
890
891/**
892 * do_efi_boot_rm() - delete UEFI load options
893 *
894 * @cmdtp: Command table
895 * @flag: Command flag
896 * @argc: Number of arguments
897 * @argv: Argument array
898 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
899 *
900 * Implement efidebug "boot rm" sub-command.
901 * Delete UEFI load options.
Heinrich Schuchardtb1d65432019-07-14 14:00:41 +0200902 *
903 * efidebug boot rm <id> ...
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900904 */
Simon Glassed38aef2020-05-10 11:40:03 -0600905static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag,
906 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900907{
908 efi_guid_t guid;
909 int id, i;
910 char *endp;
911 char var_name[9];
AKASHI Takahirocfdccd22020-02-28 09:05:04 +0900912 u16 var_name16[9], *p;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900913 efi_status_t ret;
914
915 if (argc == 1)
916 return CMD_RET_USAGE;
917
918 guid = efi_global_variable_guid;
919 for (i = 1; i < argc; i++, argv++) {
920 id = (int)simple_strtoul(argv[1], &endp, 16);
921 if (*endp != '\0' || id > 0xffff)
922 return CMD_RET_FAILURE;
923
924 sprintf(var_name, "Boot%04X", id);
AKASHI Takahirocfdccd22020-02-28 09:05:04 +0900925 p = var_name16;
926 utf8_utf16_strncpy(&p, var_name, 9);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900927
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200928 ret = EFI_CALL(efi_set_variable(var_name16, &guid, 0, 0, NULL));
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900929 if (ret) {
Heinrich Schuchardt3be0bda2020-03-02 20:13:10 +0100930 printf("Cannot remove %ls\n", var_name16);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900931 return CMD_RET_FAILURE;
932 }
933 }
934
935 return CMD_RET_SUCCESS;
936}
937
938/**
939 * show_efi_boot_opt_data() - dump UEFI load option
940 *
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200941 * @varname16: variable name
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200942 * @data: value of UEFI load option variable
943 * @size: size of the boot option
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900944 *
945 * Decode the value of UEFI load option variable and print information.
946 */
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200947static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size)
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900948{
949 struct efi_load_option lo;
950 char *label, *p;
951 size_t label_len16, label_len;
952 u16 *dp_str;
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200953 efi_status_t ret;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900954
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200955 ret = efi_deserialize_load_option(&lo, data, size);
956 if (ret != EFI_SUCCESS) {
957 printf("%ls: invalid load option\n", varname16);
958 return;
959 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900960
961 label_len16 = u16_strlen(lo.label);
962 label_len = utf16_utf8_strnlen(lo.label, label_len16);
963 label = malloc(label_len + 1);
964 if (!label)
965 return;
966 p = label;
967 utf16_utf8_strncpy(&p, lo.label, label_len16);
968
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200969 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
970 varname16,
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900971 /* ACTIVE */
972 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
973 /* FORCE RECONNECT */
974 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
975 /* HIDDEN */
976 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
977 lo.attributes);
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200978 printf(" label: %s\n", label);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900979
980 dp_str = efi_dp_str(lo.file_path);
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200981 printf(" file_path: %ls\n", dp_str);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900982 efi_free_pool(dp_str);
983
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200984 printf(" data:\n");
985 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200986 lo.optional_data, *size, true);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900987 free(label);
988}
989
990/**
991 * show_efi_boot_opt() - dump UEFI load option
992 *
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200993 * @varname16: variable name
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900994 *
995 * Dump information defined by UEFI load option.
996 */
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200997static void show_efi_boot_opt(u16 *varname16)
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900998{
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200999 void *data;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001000 efi_uintn_t size;
AKASHI Takahirodab93f42019-11-26 10:11:22 +09001001 efi_status_t ret;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001002
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001003 size = 0;
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +02001004 ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
1005 NULL, &size, NULL));
AKASHI Takahirodab93f42019-11-26 10:11:22 +09001006 if (ret == EFI_BUFFER_TOO_SMALL) {
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001007 data = malloc(size);
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +02001008 if (!data) {
1009 printf("ERROR: Out of memory\n");
1010 return;
1011 }
1012 ret = EFI_CALL(efi_get_variable(varname16,
1013 &efi_global_variable_guid,
1014 NULL, &size, data));
1015 if (ret == EFI_SUCCESS)
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +02001016 show_efi_boot_opt_data(varname16, data, &size);
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +02001017 free(data);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001018 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001019}
1020
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +09001021static int u16_tohex(u16 c)
1022{
1023 if (c >= '0' && c <= '9')
1024 return c - '0';
1025 if (c >= 'A' && c <= 'F')
1026 return c - 'A' + 10;
1027
1028 /* not hexadecimal */
1029 return -1;
1030}
1031
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001032/**
1033 * show_efi_boot_dump() - dump all UEFI load options
1034 *
1035 * @cmdtp: Command table
1036 * @flag: Command flag
1037 * @argc: Number of arguments
1038 * @argv: Argument array
1039 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1040 *
1041 * Implement efidebug "boot dump" sub-command.
1042 * Dump information of all UEFI load options defined.
Heinrich Schuchardt21715092019-07-25 20:52:23 +02001043 *
1044 * efidebug boot dump
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001045 */
Simon Glassed38aef2020-05-10 11:40:03 -06001046static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
1047 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001048{
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +09001049 u16 *var_name16, *p;
1050 efi_uintn_t buf_size, size;
1051 efi_guid_t guid;
1052 int id, i, digit;
1053 efi_status_t ret;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001054
1055 if (argc > 1)
1056 return CMD_RET_USAGE;
1057
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +09001058 buf_size = 128;
1059 var_name16 = malloc(buf_size);
1060 if (!var_name16)
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001061 return CMD_RET_FAILURE;
1062
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +09001063 var_name16[0] = 0;
1064 for (;;) {
1065 size = buf_size;
1066 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
1067 &guid));
1068 if (ret == EFI_NOT_FOUND)
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001069 break;
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +09001070 if (ret == EFI_BUFFER_TOO_SMALL) {
1071 buf_size = size;
1072 p = realloc(var_name16, buf_size);
1073 if (!p) {
1074 free(var_name16);
1075 return CMD_RET_FAILURE;
1076 }
1077 var_name16 = p;
1078 ret = EFI_CALL(efi_get_next_variable_name(&size,
1079 var_name16,
1080 &guid));
1081 }
1082 if (ret != EFI_SUCCESS) {
1083 free(var_name16);
1084 return CMD_RET_FAILURE;
1085 }
1086
1087 if (memcmp(var_name16, L"Boot", 8))
1088 continue;
1089
1090 for (id = 0, i = 0; i < 4; i++) {
1091 digit = u16_tohex(var_name16[4 + i]);
1092 if (digit < 0)
1093 break;
1094 id = (id << 4) + digit;
1095 }
1096 if (i == 4 && !var_name16[8])
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +02001097 show_efi_boot_opt(var_name16);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001098 }
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +09001099
1100 free(var_name16);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001101
1102 return CMD_RET_SUCCESS;
1103}
1104
1105/**
1106 * show_efi_boot_order() - show order of UEFI load options
1107 *
1108 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1109 *
1110 * Show order of UEFI load options defined by BootOrder variable.
1111 */
1112static int show_efi_boot_order(void)
1113{
Heinrich Schuchardt98fc6152020-04-29 21:15:08 +02001114 u16 *bootorder;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001115 efi_uintn_t size;
1116 int num, i;
1117 char var_name[9];
1118 u16 var_name16[9], *p16;
1119 void *data;
1120 struct efi_load_option lo;
1121 char *label, *p;
1122 size_t label_len16, label_len;
1123 efi_status_t ret;
1124
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001125 size = 0;
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +02001126 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
Heinrich Schuchardt98fc6152020-04-29 21:15:08 +02001127 NULL, &size, NULL));
1128 if (ret != EFI_BUFFER_TOO_SMALL) {
1129 if (ret == EFI_NOT_FOUND) {
1130 printf("BootOrder not defined\n");
1131 return CMD_RET_SUCCESS;
1132 } else {
1133 return CMD_RET_FAILURE;
1134 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001135 }
Heinrich Schuchardt98fc6152020-04-29 21:15:08 +02001136 bootorder = malloc(size);
1137 if (!bootorder) {
1138 printf("ERROR: Out of memory\n");
1139 return CMD_RET_FAILURE;
1140 }
1141 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
1142 NULL, &size, bootorder));
1143 if (ret != EFI_SUCCESS) {
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001144 ret = CMD_RET_FAILURE;
1145 goto out;
1146 }
1147
1148 num = size / sizeof(u16);
1149 for (i = 0; i < num; i++) {
1150 sprintf(var_name, "Boot%04X", bootorder[i]);
1151 p16 = var_name16;
1152 utf8_utf16_strncpy(&p16, var_name, 9);
1153
1154 size = 0;
Heinrich Schuchardt98fc6152020-04-29 21:15:08 +02001155 ret = EFI_CALL(efi_get_variable(var_name16,
1156 &efi_global_variable_guid, NULL,
1157 &size, NULL));
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001158 if (ret != EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardt98fc6152020-04-29 21:15:08 +02001159 printf("%2d: %s: (not defined)\n", i + 1, var_name);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001160 continue;
1161 }
1162
1163 data = malloc(size);
1164 if (!data) {
1165 ret = CMD_RET_FAILURE;
1166 goto out;
1167 }
Heinrich Schuchardt98fc6152020-04-29 21:15:08 +02001168 ret = EFI_CALL(efi_get_variable(var_name16,
1169 &efi_global_variable_guid, NULL,
1170 &size, data));
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001171 if (ret != EFI_SUCCESS) {
1172 free(data);
1173 ret = CMD_RET_FAILURE;
1174 goto out;
1175 }
1176
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +02001177 ret = efi_deserialize_load_option(&lo, data, &size);
1178 if (ret != EFI_SUCCESS) {
1179 printf("%ls: invalid load option\n", var_name16);
1180 ret = CMD_RET_FAILURE;
1181 goto out;
1182 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001183
1184 label_len16 = u16_strlen(lo.label);
1185 label_len = utf16_utf8_strnlen(lo.label, label_len16);
1186 label = malloc(label_len + 1);
1187 if (!label) {
1188 free(data);
1189 ret = CMD_RET_FAILURE;
1190 goto out;
1191 }
1192 p = label;
1193 utf16_utf8_strncpy(&p, lo.label, label_len16);
Heinrich Schuchardt98fc6152020-04-29 21:15:08 +02001194 printf("%2d: %s: %s\n", i + 1, var_name, label);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001195 free(label);
1196
1197 free(data);
1198 }
1199out:
1200 free(bootorder);
1201
1202 return ret;
1203}
1204
1205/**
1206 * do_efi_boot_next() - manage UEFI BootNext variable
1207 *
1208 * @cmdtp: Command table
1209 * @flag: Command flag
1210 * @argc: Number of arguments
1211 * @argv: Argument array
1212 * Return: CMD_RET_SUCCESS on success,
1213 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1214 *
1215 * Implement efidebug "boot next" sub-command.
1216 * Set BootNext variable.
Heinrich Schuchardtb1d65432019-07-14 14:00:41 +02001217 *
1218 * efidebug boot next <id>
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001219 */
Simon Glassed38aef2020-05-10 11:40:03 -06001220static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
1221 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001222{
1223 u16 bootnext;
1224 efi_uintn_t size;
1225 char *endp;
1226 efi_guid_t guid;
1227 efi_status_t ret;
Heinrich Schuchardt99a21fc2019-06-20 12:59:45 +02001228 int r = CMD_RET_SUCCESS;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001229
1230 if (argc != 2)
1231 return CMD_RET_USAGE;
1232
1233 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
Heinrich Schuchardt90c9d042020-05-09 17:59:19 +02001234 if (*endp) {
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001235 printf("invalid value: %s\n", argv[1]);
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +02001236 r = CMD_RET_FAILURE;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001237 goto out;
1238 }
1239
1240 guid = efi_global_variable_guid;
1241 size = sizeof(u16);
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +02001242 ret = EFI_CALL(efi_set_variable(L"BootNext", &guid,
AKASHI Takahiroc1f3ebc2019-06-04 15:52:10 +09001243 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001244 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1245 EFI_VARIABLE_RUNTIME_ACCESS,
1246 size, &bootnext));
Heinrich Schuchardt99a21fc2019-06-20 12:59:45 +02001247 if (ret != EFI_SUCCESS) {
1248 printf("Cannot set BootNext\n");
1249 r = CMD_RET_FAILURE;
1250 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001251out:
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +02001252 return r;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001253}
1254
1255/**
1256 * do_efi_boot_order() - manage UEFI BootOrder variable
1257 *
1258 * @cmdtp: Command table
1259 * @flag: Command flag
1260 * @argc: Number of arguments
1261 * @argv: Argument array
1262 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1263 *
1264 * Implement efidebug "boot order" sub-command.
1265 * Show order of UEFI load options, or change it in BootOrder variable.
Heinrich Schuchardtb1d65432019-07-14 14:00:41 +02001266 *
1267 * efidebug boot order [<id> ...]
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001268 */
Simon Glassed38aef2020-05-10 11:40:03 -06001269static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
1270 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001271{
1272 u16 *bootorder = NULL;
1273 efi_uintn_t size;
1274 int id, i;
1275 char *endp;
1276 efi_guid_t guid;
1277 efi_status_t ret;
Heinrich Schuchardt99a21fc2019-06-20 12:59:45 +02001278 int r = CMD_RET_SUCCESS;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001279
1280 if (argc == 1)
1281 return show_efi_boot_order();
1282
1283 argc--;
1284 argv++;
1285
1286 size = argc * sizeof(u16);
1287 bootorder = malloc(size);
1288 if (!bootorder)
1289 return CMD_RET_FAILURE;
1290
1291 for (i = 0; i < argc; i++) {
1292 id = (int)simple_strtoul(argv[i], &endp, 16);
1293 if (*endp != '\0' || id > 0xffff) {
1294 printf("invalid value: %s\n", argv[i]);
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +02001295 r = CMD_RET_FAILURE;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001296 goto out;
1297 }
1298
1299 bootorder[i] = (u16)id;
1300 }
1301
1302 guid = efi_global_variable_guid;
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +02001303 ret = EFI_CALL(efi_set_variable(L"BootOrder", &guid,
AKASHI Takahiroc1f3ebc2019-06-04 15:52:10 +09001304 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001305 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1306 EFI_VARIABLE_RUNTIME_ACCESS,
1307 size, bootorder));
Heinrich Schuchardt99a21fc2019-06-20 12:59:45 +02001308 if (ret != EFI_SUCCESS) {
1309 printf("Cannot set BootOrder\n");
1310 r = CMD_RET_FAILURE;
1311 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001312out:
1313 free(bootorder);
1314
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +02001315 return r;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001316}
1317
Simon Glassed38aef2020-05-10 11:40:03 -06001318static struct cmd_tbl cmd_efidebug_boot_sub[] = {
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001319 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1320 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1321 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1322 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1323 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1324 "", ""),
1325};
1326
1327/**
1328 * do_efi_boot_opt() - manage UEFI load options
1329 *
1330 * @cmdtp: Command table
1331 * @flag: Command flag
1332 * @argc: Number of arguments
1333 * @argv: Argument array
1334 * Return: CMD_RET_SUCCESS on success,
1335 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1336 *
1337 * Implement efidebug "boot" sub-command.
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001338 */
Simon Glassed38aef2020-05-10 11:40:03 -06001339static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
1340 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001341{
Simon Glassed38aef2020-05-10 11:40:03 -06001342 struct cmd_tbl *cp;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001343
1344 if (argc < 2)
1345 return CMD_RET_USAGE;
1346
1347 argc--; argv++;
1348
1349 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1350 ARRAY_SIZE(cmd_efidebug_boot_sub));
1351 if (!cp)
1352 return CMD_RET_USAGE;
1353
1354 return cp->cmd(cmdtp, flag, argc, argv);
1355}
1356
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001357/**
1358 * do_efi_test_bootmgr() - run simple bootmgr for test
1359 *
1360 * @cmdtp: Command table
1361 * @flag: Command flag
1362 * @argc: Number of arguments
1363 * @argv: Argument array
1364 * Return: CMD_RET_SUCCESS on success,
1365 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1366 *
1367 * Implement efidebug "test bootmgr" sub-command.
1368 * Run simple bootmgr for test.
1369 *
1370 * efidebug test bootmgr
1371 */
Heinrich Schuchardtb2625e82021-01-15 19:02:50 +01001372static __maybe_unused int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
1373 int argc, char * const argv[])
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001374{
1375 efi_handle_t image;
1376 efi_uintn_t exit_data_size = 0;
1377 u16 *exit_data = NULL;
1378 efi_status_t ret;
Heinrich Schuchardte6ed8352020-08-11 18:20:50 +02001379 void *load_options = NULL;
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001380
Heinrich Schuchardta7647a72020-08-07 17:49:39 +02001381 ret = efi_bootmgr_load(&image, &load_options);
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001382 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1383
1384 /* We call efi_start_image() even if error for test purpose. */
1385 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1386 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1387 if (ret && exit_data)
1388 efi_free_pool(exit_data);
1389
1390 efi_restore_gd();
1391
Heinrich Schuchardta7647a72020-08-07 17:49:39 +02001392 free(load_options);
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001393 return CMD_RET_SUCCESS;
1394}
1395
Simon Glassed38aef2020-05-10 11:40:03 -06001396static struct cmd_tbl cmd_efidebug_test_sub[] = {
Heinrich Schuchardtb2625e82021-01-15 19:02:50 +01001397#ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001398 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1399 "", ""),
Heinrich Schuchardtb2625e82021-01-15 19:02:50 +01001400#endif
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001401};
1402
1403/**
1404 * do_efi_test() - manage UEFI load options
1405 *
1406 * @cmdtp: Command table
1407 * @flag: Command flag
1408 * @argc: Number of arguments
1409 * @argv: Argument array
1410 * Return: CMD_RET_SUCCESS on success,
1411 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1412 *
1413 * Implement efidebug "test" sub-command.
1414 */
Simon Glassed38aef2020-05-10 11:40:03 -06001415static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001416 int argc, char * const argv[])
1417{
Simon Glassed38aef2020-05-10 11:40:03 -06001418 struct cmd_tbl *cp;
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001419
1420 if (argc < 2)
1421 return CMD_RET_USAGE;
1422
1423 argc--; argv++;
1424
1425 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1426 ARRAY_SIZE(cmd_efidebug_test_sub));
1427 if (!cp)
1428 return CMD_RET_USAGE;
1429
1430 return cp->cmd(cmdtp, flag, argc, argv);
1431}
1432
Ilias Apalodimasf5009402020-05-17 22:25:45 +03001433/**
1434 * do_efi_query_info() - QueryVariableInfo EFI service
1435 *
1436 * @cmdtp: Command table
1437 * @flag: Command flag
1438 * @argc: Number of arguments
1439 * @argv: Argument array
1440 * Return: CMD_RET_SUCCESS on success,
1441 * CMD_RET_USAGE or CMD_RET_FAILURE on failure
1442 *
1443 * Implement efidebug "test" sub-command.
1444 */
1445
Simon Glassed38aef2020-05-10 11:40:03 -06001446static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
Ilias Apalodimasf5009402020-05-17 22:25:45 +03001447 int argc, char * const argv[])
1448{
1449 efi_status_t ret;
1450 u32 attr = 0;
1451 u64 max_variable_storage_size;
1452 u64 remain_variable_storage_size;
1453 u64 max_variable_size;
1454 int i;
1455
1456 for (i = 1; i < argc; i++) {
1457 if (!strcmp(argv[i], "-bs"))
1458 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
1459 else if (!strcmp(argv[i], "-rt"))
1460 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
1461 else if (!strcmp(argv[i], "-nv"))
1462 attr |= EFI_VARIABLE_NON_VOLATILE;
1463 else if (!strcmp(argv[i], "-at"))
1464 attr |=
1465 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1466 }
1467
1468 ret = EFI_CALL(efi_query_variable_info(attr,
1469 &max_variable_storage_size,
1470 &remain_variable_storage_size,
1471 &max_variable_size));
1472 if (ret != EFI_SUCCESS) {
1473 printf("Error: Cannot query UEFI variables, r = %lu\n",
1474 ret & ~EFI_ERROR_MASK);
1475 return CMD_RET_FAILURE;
1476 }
1477
1478 printf("Max storage size %llu\n", max_variable_storage_size);
1479 printf("Remaining storage size %llu\n", remain_variable_storage_size);
1480 printf("Max variable size %llu\n", max_variable_size);
1481
1482 return CMD_RET_SUCCESS;
1483}
1484
Simon Glassed38aef2020-05-10 11:40:03 -06001485static struct cmd_tbl cmd_efidebug_sub[] = {
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001486 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
AKASHI Takahiro38833c82020-11-17 09:28:01 +09001487#ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1488 U_BOOT_CMD_MKENT(capsule, CONFIG_SYS_MAXARGS, 1, do_efi_capsule,
1489 "", ""),
1490#endif
AKASHI Takahiroe32788a2019-02-25 15:54:39 +09001491 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1492 "", ""),
AKASHI Takahiro185716a2019-02-25 15:54:40 +09001493 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1494 "", ""),
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +09001495 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1496 "", ""),
AKASHI Takahiro71ab1bf2019-02-25 15:54:42 +09001497 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1498 "", ""),
AKASHI Takahiro32979d82019-02-25 15:54:43 +09001499 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1500 "", ""),
Heinrich Schuchardt75844e52020-01-07 05:57:47 +01001501 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1502 "", ""),
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001503 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1504 "", ""),
Ilias Apalodimasf5009402020-05-17 22:25:45 +03001505 U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
1506 "", ""),
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001507};
1508
1509/**
1510 * do_efidebug() - display and configure UEFI environment
1511 *
1512 * @cmdtp: Command table
1513 * @flag: Command flag
1514 * @argc: Number of arguments
1515 * @argv: Argument array
1516 * Return: CMD_RET_SUCCESS on success,
1517 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1518 *
1519 * Implement efidebug command which allows us to display and
1520 * configure UEFI environment.
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001521 */
Simon Glassed38aef2020-05-10 11:40:03 -06001522static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
1523 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001524{
Simon Glassed38aef2020-05-10 11:40:03 -06001525 struct cmd_tbl *cp;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001526 efi_status_t r;
1527
1528 if (argc < 2)
1529 return CMD_RET_USAGE;
1530
1531 argc--; argv++;
1532
1533 /* Initialize UEFI drivers */
1534 r = efi_init_obj_list();
1535 if (r != EFI_SUCCESS) {
1536 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1537 r & ~EFI_ERROR_MASK);
1538 return CMD_RET_FAILURE;
1539 }
1540
1541 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1542 ARRAY_SIZE(cmd_efidebug_sub));
1543 if (!cp)
1544 return CMD_RET_USAGE;
1545
1546 return cp->cmd(cmdtp, flag, argc, argv);
1547}
1548
1549#ifdef CONFIG_SYS_LONGHELP
1550static char efidebug_help_text[] =
1551 " - UEFI Shell-like interface to configure UEFI environment\n"
1552 "\n"
1553 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
1554 " - set UEFI BootXXXX variable\n"
1555 " <load options> will be passed to UEFI application\n"
1556 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1557 " - delete UEFI BootXXXX variables\n"
1558 "efidebug boot dump\n"
1559 " - dump all UEFI BootXXXX variables\n"
1560 "efidebug boot next <bootid>\n"
1561 " - set UEFI BootNext variable\n"
1562 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1563 " - set/show UEFI boot order\n"
AKASHI Takahiroe32788a2019-02-25 15:54:39 +09001564 "\n"
AKASHI Takahiro38833c82020-11-17 09:28:01 +09001565#ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1566 "efidebug capsule update [-v] <capsule address>\n"
1567 " - process a capsule\n"
Sughosh Ganue6f9efc2020-12-30 19:27:11 +05301568 "efidebug capsule disk-update\n"
1569 " - update a capsule from disk\n"
AKASHI Takahiro38833c82020-11-17 09:28:01 +09001570 "efidebug capsule show <capsule address>\n"
1571 " - show capsule information\n"
1572 "efidebug capsule result [<capsule result var>]\n"
1573 " - show a capsule update result\n"
1574 "\n"
1575#endif
AKASHI Takahiroe32788a2019-02-25 15:54:39 +09001576 "efidebug devices\n"
Heinrich Schuchardtc414d882020-01-07 07:48:15 +01001577 " - show UEFI devices\n"
AKASHI Takahiro185716a2019-02-25 15:54:40 +09001578 "efidebug drivers\n"
Heinrich Schuchardtc414d882020-01-07 07:48:15 +01001579 " - show UEFI drivers\n"
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +09001580 "efidebug dh\n"
Heinrich Schuchardtc414d882020-01-07 07:48:15 +01001581 " - show UEFI handles\n"
AKASHI Takahiro71ab1bf2019-02-25 15:54:42 +09001582 "efidebug images\n"
AKASHI Takahiro32979d82019-02-25 15:54:43 +09001583 " - show loaded images\n"
1584 "efidebug memmap\n"
Heinrich Schuchardtc414d882020-01-07 07:48:15 +01001585 " - show UEFI memory map\n"
Heinrich Schuchardt75844e52020-01-07 05:57:47 +01001586 "efidebug tables\n"
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001587 " - show UEFI configuration tables\n"
Heinrich Schuchardtb2625e82021-01-15 19:02:50 +01001588#ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001589 "efidebug test bootmgr\n"
Ilias Apalodimasf5009402020-05-17 22:25:45 +03001590 " - run simple bootmgr for test\n"
Heinrich Schuchardtb2625e82021-01-15 19:02:50 +01001591#endif
Ilias Apalodimasf5009402020-05-17 22:25:45 +03001592 "efidebug query [-nv][-bs][-rt][-at]\n"
1593 " - show size of UEFI variables store\n";
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001594#endif
1595
1596U_BOOT_CMD(
1597 efidebug, 10, 0, do_efidebug,
1598 "Configure UEFI environment",
1599 efidebug_help_text
1600);