blob: 5cc0a41af3b441f072e7432e596dcbf079fbc357 [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>
11#include <efi_loader.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090012#include <exports.h>
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +020013#include <hexdump.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090014#include <malloc.h>
Heinrich Schuchardta415d612020-03-14 08:44:07 +010015#include <mapmem.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090016#include <search.h>
17#include <linux/ctype.h>
18
AKASHI Takahiro355cdb52019-02-25 15:54:39 +090019#define BS systab.boottime
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090020
21/**
AKASHI Takahiro355cdb52019-02-25 15:54:39 +090022 * efi_get_device_handle_info() - get information of UEFI device
23 *
24 * @handle: Handle of UEFI device
25 * @dev_path_text: Pointer to text of device path
26 * Return: 0 on success, -1 on failure
27 *
28 * Currently return a formatted text of device path.
29 */
30static int efi_get_device_handle_info(efi_handle_t handle, u16 **dev_path_text)
31{
32 struct efi_device_path *dp;
33 efi_status_t ret;
34
35 ret = EFI_CALL(BS->open_protocol(handle, &efi_guid_device_path,
36 (void **)&dp, NULL /* FIXME */, NULL,
37 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
38 if (ret == EFI_SUCCESS) {
39 *dev_path_text = efi_dp_str(dp);
40 return 0;
41 } else {
42 return -1;
43 }
44}
45
46#define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
47
48static const char spc[] = " ";
49static const char sep[] = "================";
50
51/**
52 * do_efi_show_devices() - show UEFI devices
53 *
54 * @cmdtp: Command table
55 * @flag: Command flag
56 * @argc: Number of arguments
57 * @argv: Argument array
58 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
59 *
60 * Implement efidebug "devices" sub-command.
61 * Show all UEFI devices and their information.
62 */
63static int do_efi_show_devices(cmd_tbl_t *cmdtp, int flag,
64 int argc, char * const argv[])
65{
66 efi_handle_t *handles;
67 efi_uintn_t num, i;
68 u16 *dev_path_text;
69 efi_status_t ret;
70
Heinrich Schuchardta30c7232020-05-02 16:08:37 +020071 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
AKASHI Takahiro355cdb52019-02-25 15:54:39 +090072 &num, &handles));
73 if (ret != EFI_SUCCESS)
74 return CMD_RET_FAILURE;
75
76 if (!num)
77 return CMD_RET_SUCCESS;
78
79 printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc);
80 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
81 for (i = 0; i < num; i++) {
82 if (!efi_get_device_handle_info(handles[i], &dev_path_text)) {
83 printf("%p %ls\n", handles[i], dev_path_text);
84 efi_free_pool(dev_path_text);
85 }
86 }
87
Heinrich Schuchardta30c7232020-05-02 16:08:37 +020088 efi_free_pool(handles);
AKASHI Takahiro355cdb52019-02-25 15:54:39 +090089
90 return CMD_RET_SUCCESS;
91}
92
93/**
AKASHI Takahiro66eaf562019-02-25 15:54:40 +090094 * efi_get_driver_handle_info() - get information of UEFI driver
95 *
96 * @handle: Handle of UEFI device
97 * @driver_name: Driver name
98 * @image_path: Pointer to text of device path
99 * Return: 0 on success, -1 on failure
100 *
101 * Currently return no useful information as all UEFI drivers are
102 * built-in..
103 */
104static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
105 u16 **image_path)
106{
107 struct efi_handler *handler;
108 struct efi_loaded_image *image;
109 efi_status_t ret;
110
111 /*
112 * driver name
113 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
114 */
115 *driver_name = NULL;
116
117 /* image name */
118 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
119 if (ret != EFI_SUCCESS) {
120 *image_path = NULL;
121 return 0;
122 }
123
124 image = handler->protocol_interface;
125 *image_path = efi_dp_str(image->file_path);
126
127 return 0;
128}
129
130/**
131 * do_efi_show_drivers() - show UEFI drivers
132 *
133 * @cmdtp: Command table
134 * @flag: Command flag
135 * @argc: Number of arguments
136 * @argv: Argument array
137 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
138 *
139 * Implement efidebug "drivers" sub-command.
140 * Show all UEFI drivers and their information.
141 */
142static int do_efi_show_drivers(cmd_tbl_t *cmdtp, int flag,
143 int argc, char * const argv[])
144{
145 efi_handle_t *handles;
146 efi_uintn_t num, i;
147 u16 *driver_name, *image_path_text;
148 efi_status_t ret;
149
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200150 ret = EFI_CALL(efi_locate_handle_buffer(
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900151 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
152 NULL, &num, &handles));
153 if (ret != EFI_SUCCESS)
154 return CMD_RET_FAILURE;
155
156 if (!num)
157 return CMD_RET_SUCCESS;
158
159 printf("Driver%.*s Name Image Path\n",
160 EFI_HANDLE_WIDTH - 6, spc);
161 printf("%.*s ==================== ====================\n",
162 EFI_HANDLE_WIDTH, sep);
163 for (i = 0; i < num; i++) {
164 if (!efi_get_driver_handle_info(handles[i], &driver_name,
165 &image_path_text)) {
166 if (image_path_text)
167 printf("%p %-20ls %ls\n", handles[i],
168 driver_name, image_path_text);
169 else
170 printf("%p %-20ls <built-in>\n",
171 handles[i], driver_name);
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200172 efi_free_pool(driver_name);
173 efi_free_pool(image_path_text);
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900174 }
175 }
176
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200177 efi_free_pool(handles);
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900178
179 return CMD_RET_SUCCESS;
180}
181
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900182static const struct {
183 const char *text;
184 const efi_guid_t guid;
185} guid_list[] = {
186 {
187 "Device Path",
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +0200188 EFI_DEVICE_PATH_PROTOCOL_GUID,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900189 },
190 {
191 "Device Path To Text",
192 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
193 },
194 {
195 "Device Path Utilities",
196 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
197 },
198 {
199 "Unicode Collation 2",
200 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
201 },
202 {
203 "Driver Binding",
204 EFI_DRIVER_BINDING_PROTOCOL_GUID,
205 },
206 {
207 "Simple Text Input",
208 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
209 },
210 {
211 "Simple Text Input Ex",
212 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
213 },
214 {
215 "Simple Text Output",
216 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
217 },
218 {
219 "Block IO",
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +0200220 EFI_BLOCK_IO_PROTOCOL_GUID,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900221 },
222 {
223 "Simple File System",
224 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
225 },
226 {
227 "Loaded Image",
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +0200228 EFI_LOADED_IMAGE_PROTOCOL_GUID,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900229 },
230 {
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +0200231 "Graphics Output",
232 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900233 },
Heinrich Schuchardt391bc8a2019-04-20 07:57:28 +0200234 {
235 "HII String",
236 EFI_HII_STRING_PROTOCOL_GUID,
237 },
238 {
239 "HII Database",
240 EFI_HII_DATABASE_PROTOCOL_GUID,
241 },
242 {
243 "HII Config Routing",
244 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
245 },
246 {
Ilias Apalodimasec80b472020-02-21 09:55:45 +0200247 "Load File2",
248 EFI_LOAD_FILE2_PROTOCOL_GUID,
249 },
250 {
Heinrich Schuchardt391bc8a2019-04-20 07:57:28 +0200251 "Simple Network",
252 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
253 },
254 {
255 "PXE Base Code",
256 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
257 },
Heinrich Schuchardt986e0642020-01-07 05:57:47 +0100258 /* Configuration table GUIDs */
259 {
260 "ACPI table",
261 EFI_ACPI_TABLE_GUID,
262 },
263 {
264 "device tree",
265 EFI_FDT_GUID,
266 },
267 {
268 "SMBIOS table",
269 SMBIOS_TABLE_GUID,
270 },
Heinrich Schuchardt76be6872020-02-19 20:48:49 +0100271 {
272 "Runtime properties",
273 EFI_RT_PROPERTIES_TABLE_GUID,
274 },
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900275};
276
277/**
Heinrich Schuchardt173cd9e2020-01-07 06:02:33 +0100278 * get_guid_text - get string of GUID
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900279 *
Heinrich Schuchardt173cd9e2020-01-07 06:02:33 +0100280 * Return description of GUID.
281 *
282 * @guid: GUID
283 * Return: description of GUID or NULL
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900284 */
Heinrich Schuchardt173cd9e2020-01-07 06:02:33 +0100285static const char *get_guid_text(const void *guid)
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900286{
287 int i;
288
Heinrich Schuchardt173cd9e2020-01-07 06:02:33 +0100289 for (i = 0; i < ARRAY_SIZE(guid_list); i++) {
290 /*
291 * As guidcmp uses memcmp() we can safely accept unaligned
292 * GUIDs.
293 */
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900294 if (!guidcmp(&guid_list[i].guid, guid))
Heinrich Schuchardt173cd9e2020-01-07 06:02:33 +0100295 return guid_list[i].text;
296 }
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900297
Heinrich Schuchardt173cd9e2020-01-07 06:02:33 +0100298 return NULL;
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900299}
300
301/**
302 * do_efi_show_handles() - show UEFI handles
303 *
304 * @cmdtp: Command table
305 * @flag: Command flag
306 * @argc: Number of arguments
307 * @argv: Argument array
308 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
309 *
310 * Implement efidebug "dh" sub-command.
311 * Show all UEFI handles and their information, currently all protocols
312 * added to handle.
313 */
314static int do_efi_show_handles(cmd_tbl_t *cmdtp, int flag,
315 int argc, char * const argv[])
316{
317 efi_handle_t *handles;
318 efi_guid_t **guid;
319 efi_uintn_t num, count, i, j;
320 const char *guid_text;
321 efi_status_t ret;
322
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200323 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900324 &num, &handles));
325 if (ret != EFI_SUCCESS)
326 return CMD_RET_FAILURE;
327
328 if (!num)
329 return CMD_RET_SUCCESS;
330
331 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
332 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
333 for (i = 0; i < num; i++) {
334 printf("%p", handles[i]);
335 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
336 &count));
337 if (ret || !count) {
338 putc('\n');
339 continue;
340 }
341
342 for (j = 0; j < count; j++) {
343 if (j)
344 printf(", ");
345 else
346 putc(' ');
347
348 guid_text = get_guid_text(guid[j]);
349 if (guid_text)
350 puts(guid_text);
351 else
352 printf("%pUl", guid[j]);
353 }
354 putc('\n');
355 }
356
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200357 efi_free_pool(handles);
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900358
359 return CMD_RET_SUCCESS;
360}
361
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900362/**
AKASHI Takahirofa536732019-02-25 15:54:42 +0900363 * do_efi_show_images() - show UEFI images
364 *
365 * @cmdtp: Command table
366 * @flag: Command flag
367 * @argc: Number of arguments
368 * @argv: Argument array
369 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
370 *
371 * Implement efidebug "images" sub-command.
372 * Show all UEFI loaded images and their information.
373 */
374static int do_efi_show_images(cmd_tbl_t *cmdtp, int flag,
375 int argc, char * const argv[])
376{
377 efi_print_image_infos(NULL);
378
379 return CMD_RET_SUCCESS;
380}
381
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900382static const char * const efi_mem_type_string[] = {
383 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
384 [EFI_LOADER_CODE] = "LOADER CODE",
385 [EFI_LOADER_DATA] = "LOADER DATA",
386 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
387 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
388 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
389 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
390 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
391 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
392 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
393 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
394 [EFI_MMAP_IO] = "IO",
395 [EFI_MMAP_IO_PORT] = "IO PORT",
396 [EFI_PAL_CODE] = "PAL",
Heinrich Schuchardtdd9056c2020-04-29 20:21:39 +0200397 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900398};
399
400static const struct efi_mem_attrs {
401 const u64 bit;
402 const char *text;
403} efi_mem_attrs[] = {
404 {EFI_MEMORY_UC, "UC"},
405 {EFI_MEMORY_UC, "UC"},
406 {EFI_MEMORY_WC, "WC"},
407 {EFI_MEMORY_WT, "WT"},
408 {EFI_MEMORY_WB, "WB"},
409 {EFI_MEMORY_UCE, "UCE"},
410 {EFI_MEMORY_WP, "WP"},
411 {EFI_MEMORY_RP, "RP"},
412 {EFI_MEMORY_XP, "WP"},
413 {EFI_MEMORY_NV, "NV"},
414 {EFI_MEMORY_MORE_RELIABLE, "REL"},
415 {EFI_MEMORY_RO, "RO"},
416 {EFI_MEMORY_RUNTIME, "RT"},
417};
418
419/**
420 * print_memory_attributes() - print memory map attributes
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200421 *
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900422 * @attributes: Attribute value
423 *
424 * Print memory map attributes
425 */
426static void print_memory_attributes(u64 attributes)
427{
428 int sep, i;
429
430 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
431 if (attributes & efi_mem_attrs[i].bit) {
432 if (sep) {
433 putc('|');
434 } else {
435 putc(' ');
436 sep = 1;
437 }
438 puts(efi_mem_attrs[i].text);
439 }
440}
441
442#define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
443
444/**
445 * do_efi_show_memmap() - show UEFI memory map
446 *
447 * @cmdtp: Command table
448 * @flag: Command flag
449 * @argc: Number of arguments
450 * @argv: Argument array
451 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
452 *
453 * Implement efidebug "memmap" sub-command.
454 * Show UEFI memory map.
455 */
456static int do_efi_show_memmap(cmd_tbl_t *cmdtp, int flag,
457 int argc, char * const argv[])
458{
459 struct efi_mem_desc *memmap = NULL, *map;
460 efi_uintn_t map_size = 0;
461 const char *type;
462 int i;
463 efi_status_t ret;
464
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200465 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900466 if (ret == EFI_BUFFER_TOO_SMALL) {
467 map_size += sizeof(struct efi_mem_desc); /* for my own */
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200468 ret = efi_allocate_pool(EFI_LOADER_DATA, map_size,
469 (void *)&memmap);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900470 if (ret != EFI_SUCCESS)
471 return CMD_RET_FAILURE;
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200472 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900473 }
474 if (ret != EFI_SUCCESS) {
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200475 efi_free_pool(memmap);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900476 return CMD_RET_FAILURE;
477 }
478
479 printf("Type Start%.*s End%.*s Attributes\n",
480 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
481 printf("================ %.*s %.*s ==========\n",
482 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
AKASHI Takahiroe2a5b862020-05-08 14:50:18 +0900483 /*
484 * Coverity check: dereferencing null pointer "map."
485 * This is a false positive as memmap will always be
486 * populated by allocate_pool() above.
487 */
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900488 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
Heinrich Schuchardtdd9056c2020-04-29 20:21:39 +0200489 if (map->type < ARRAY_SIZE(efi_mem_type_string))
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900490 type = efi_mem_type_string[map->type];
491 else
492 type = "(unknown)";
493
494 printf("%-16s %.*llx-%.*llx", type,
495 EFI_PHYS_ADDR_WIDTH,
Heinrich Schuchardt6c0ef352020-03-27 04:33:17 +0000496 (u64)map_to_sysmem((void *)(uintptr_t)
497 map->physical_start),
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900498 EFI_PHYS_ADDR_WIDTH,
Heinrich Schuchardt6c0ef352020-03-27 04:33:17 +0000499 (u64)map_to_sysmem((void *)(uintptr_t)
500 (map->physical_start +
501 map->num_pages * EFI_PAGE_SIZE)));
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900502
503 print_memory_attributes(map->attribute);
504 putc('\n');
505 }
506
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200507 efi_free_pool(memmap);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900508
509 return CMD_RET_SUCCESS;
510}
511
AKASHI Takahirofa536732019-02-25 15:54:42 +0900512/**
Heinrich Schuchardt986e0642020-01-07 05:57:47 +0100513 * do_efi_show_tables() - show UEFI configuration tables
514 *
515 * @cmdtp: Command table
516 * @flag: Command flag
517 * @argc: Number of arguments
518 * @argv: Argument array
519 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
520 *
521 * Implement efidebug "tables" sub-command.
522 * Show UEFI configuration tables.
523 */
524static int do_efi_show_tables(cmd_tbl_t *cmdtp, int flag,
525 int argc, char * const argv[])
526{
527 efi_uintn_t i;
528 const char *guid_str;
529
530 for (i = 0; i < systab.nr_tables; ++i) {
531 guid_str = get_guid_text(&systab.tables[i].guid);
532 if (!guid_str)
533 guid_str = "";
534 printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
535 }
536
537 return CMD_RET_SUCCESS;
538}
539
540/**
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900541 * do_efi_boot_add() - set UEFI load option
542 *
543 * @cmdtp: Command table
544 * @flag: Command flag
545 * @argc: Number of arguments
546 * @argv: Argument array
547 * Return: CMD_RET_SUCCESS on success,
548 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
549 *
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200550 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
551 *
552 * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900553 */
554static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
555 int argc, char * const argv[])
556{
557 int id;
558 char *endp;
559 char var_name[9];
560 u16 var_name16[9], *p;
561 efi_guid_t guid;
562 size_t label_len, label_len16;
563 u16 *label;
564 struct efi_device_path *device_path = NULL, *file_path = NULL;
565 struct efi_load_option lo;
566 void *data = NULL;
567 efi_uintn_t size;
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200568 efi_status_t ret;
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200569 int r = CMD_RET_SUCCESS;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900570
571 if (argc < 6 || argc > 7)
572 return CMD_RET_USAGE;
573
574 id = (int)simple_strtoul(argv[1], &endp, 16);
575 if (*endp != '\0' || id > 0xffff)
Heinrich Schuchardt1fa442e2019-02-28 20:41:58 +0100576 return CMD_RET_USAGE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900577
578 sprintf(var_name, "Boot%04X", id);
579 p = var_name16;
580 utf8_utf16_strncpy(&p, var_name, 9);
581
582 guid = efi_global_variable_guid;
583
584 /* attributes */
585 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
586
587 /* label */
588 label_len = strlen(argv[2]);
589 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
590 label = malloc((label_len16 + 1) * sizeof(u16));
591 if (!label)
592 return CMD_RET_FAILURE;
593 lo.label = label; /* label will be changed below */
594 utf8_utf16_strncpy(&label, argv[2], label_len);
595
596 /* file path */
597 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
598 &file_path);
599 if (ret != EFI_SUCCESS) {
600 printf("Cannot create device path for \"%s %s\"\n",
601 argv[3], argv[4]);
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200602 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900603 goto out;
604 }
605 lo.file_path = file_path;
606 lo.file_path_length = efi_dp_size(file_path)
607 + sizeof(struct efi_device_path); /* for END */
608
609 /* optional data */
AKASHI Takahirod67591d2020-05-08 14:50:47 +0900610 if (argc == 6)
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200611 lo.optional_data = NULL;
612 else
613 lo.optional_data = (const u8 *)argv[6];
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900614
615 size = efi_serialize_load_option(&lo, (u8 **)&data);
616 if (!size) {
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200617 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900618 goto out;
619 }
620
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200621 ret = EFI_CALL(efi_set_variable(var_name16, &guid,
AKASHI Takahirof658c2e2019-06-04 15:52:10 +0900622 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900623 EFI_VARIABLE_BOOTSERVICE_ACCESS |
624 EFI_VARIABLE_RUNTIME_ACCESS,
625 size, data));
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200626 if (ret != EFI_SUCCESS) {
627 printf("Cannot set %ls\n", var_name16);
628 r = CMD_RET_FAILURE;
629 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900630out:
631 free(data);
632 efi_free_pool(device_path);
633 efi_free_pool(file_path);
634 free(lo.label);
635
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200636 return r;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900637}
638
639/**
640 * do_efi_boot_rm() - delete UEFI load options
641 *
642 * @cmdtp: Command table
643 * @flag: Command flag
644 * @argc: Number of arguments
645 * @argv: Argument array
646 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
647 *
648 * Implement efidebug "boot rm" sub-command.
649 * Delete UEFI load options.
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200650 *
651 * efidebug boot rm <id> ...
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900652 */
653static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
654 int argc, char * const argv[])
655{
656 efi_guid_t guid;
657 int id, i;
658 char *endp;
659 char var_name[9];
AKASHI Takahiroe8bced62020-02-28 09:05:04 +0900660 u16 var_name16[9], *p;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900661 efi_status_t ret;
662
663 if (argc == 1)
664 return CMD_RET_USAGE;
665
666 guid = efi_global_variable_guid;
667 for (i = 1; i < argc; i++, argv++) {
668 id = (int)simple_strtoul(argv[1], &endp, 16);
669 if (*endp != '\0' || id > 0xffff)
670 return CMD_RET_FAILURE;
671
672 sprintf(var_name, "Boot%04X", id);
AKASHI Takahiroe8bced62020-02-28 09:05:04 +0900673 p = var_name16;
674 utf8_utf16_strncpy(&p, var_name, 9);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900675
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200676 ret = EFI_CALL(efi_set_variable(var_name16, &guid, 0, 0, NULL));
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900677 if (ret) {
Heinrich Schuchardt30efb5d2020-03-02 20:13:10 +0100678 printf("Cannot remove %ls\n", var_name16);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900679 return CMD_RET_FAILURE;
680 }
681 }
682
683 return CMD_RET_SUCCESS;
684}
685
686/**
687 * show_efi_boot_opt_data() - dump UEFI load option
688 *
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200689 * @varname16: variable name
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200690 * @data: value of UEFI load option variable
691 * @size: size of the boot option
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900692 *
693 * Decode the value of UEFI load option variable and print information.
694 */
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200695static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t size)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900696{
697 struct efi_load_option lo;
698 char *label, *p;
699 size_t label_len16, label_len;
700 u16 *dp_str;
701
702 efi_deserialize_load_option(&lo, data);
703
704 label_len16 = u16_strlen(lo.label);
705 label_len = utf16_utf8_strnlen(lo.label, label_len16);
706 label = malloc(label_len + 1);
707 if (!label)
708 return;
709 p = label;
710 utf16_utf8_strncpy(&p, lo.label, label_len16);
711
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200712 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
713 varname16,
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900714 /* ACTIVE */
715 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
716 /* FORCE RECONNECT */
717 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
718 /* HIDDEN */
719 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
720 lo.attributes);
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200721 printf(" label: %s\n", label);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900722
723 dp_str = efi_dp_str(lo.file_path);
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200724 printf(" file_path: %ls\n", dp_str);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900725 efi_free_pool(dp_str);
726
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200727 printf(" data:\n");
728 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
729 lo.optional_data, size + (u8 *)data -
730 (u8 *)lo.optional_data, true);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900731 free(label);
732}
733
734/**
735 * show_efi_boot_opt() - dump UEFI load option
736 *
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200737 * @varname16: variable name
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900738 *
739 * Dump information defined by UEFI load option.
740 */
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200741static void show_efi_boot_opt(u16 *varname16)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900742{
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200743 void *data;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900744 efi_uintn_t size;
AKASHI Takahiro0bffb8c2019-11-26 10:11:22 +0900745 efi_status_t ret;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900746
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900747 size = 0;
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200748 ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
749 NULL, &size, NULL));
AKASHI Takahiro0bffb8c2019-11-26 10:11:22 +0900750 if (ret == EFI_BUFFER_TOO_SMALL) {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900751 data = malloc(size);
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200752 if (!data) {
753 printf("ERROR: Out of memory\n");
754 return;
755 }
756 ret = EFI_CALL(efi_get_variable(varname16,
757 &efi_global_variable_guid,
758 NULL, &size, data));
759 if (ret == EFI_SUCCESS)
760 show_efi_boot_opt_data(varname16, data, size);
761 free(data);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900762 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900763}
764
AKASHI Takahiroffe21572019-04-26 09:44:18 +0900765static int u16_tohex(u16 c)
766{
767 if (c >= '0' && c <= '9')
768 return c - '0';
769 if (c >= 'A' && c <= 'F')
770 return c - 'A' + 10;
771
772 /* not hexadecimal */
773 return -1;
774}
775
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900776/**
777 * show_efi_boot_dump() - dump all UEFI load options
778 *
779 * @cmdtp: Command table
780 * @flag: Command flag
781 * @argc: Number of arguments
782 * @argv: Argument array
783 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
784 *
785 * Implement efidebug "boot dump" sub-command.
786 * Dump information of all UEFI load options defined.
Heinrich Schuchardta6ccba02019-07-25 20:52:23 +0200787 *
788 * efidebug boot dump
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900789 */
790static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag,
791 int argc, char * const argv[])
792{
AKASHI Takahiroffe21572019-04-26 09:44:18 +0900793 u16 *var_name16, *p;
794 efi_uintn_t buf_size, size;
795 efi_guid_t guid;
796 int id, i, digit;
797 efi_status_t ret;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900798
799 if (argc > 1)
800 return CMD_RET_USAGE;
801
AKASHI Takahiroffe21572019-04-26 09:44:18 +0900802 buf_size = 128;
803 var_name16 = malloc(buf_size);
804 if (!var_name16)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900805 return CMD_RET_FAILURE;
806
AKASHI Takahiroffe21572019-04-26 09:44:18 +0900807 var_name16[0] = 0;
808 for (;;) {
809 size = buf_size;
810 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
811 &guid));
812 if (ret == EFI_NOT_FOUND)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900813 break;
AKASHI Takahiroffe21572019-04-26 09:44:18 +0900814 if (ret == EFI_BUFFER_TOO_SMALL) {
815 buf_size = size;
816 p = realloc(var_name16, buf_size);
817 if (!p) {
818 free(var_name16);
819 return CMD_RET_FAILURE;
820 }
821 var_name16 = p;
822 ret = EFI_CALL(efi_get_next_variable_name(&size,
823 var_name16,
824 &guid));
825 }
826 if (ret != EFI_SUCCESS) {
827 free(var_name16);
828 return CMD_RET_FAILURE;
829 }
830
831 if (memcmp(var_name16, L"Boot", 8))
832 continue;
833
834 for (id = 0, i = 0; i < 4; i++) {
835 digit = u16_tohex(var_name16[4 + i]);
836 if (digit < 0)
837 break;
838 id = (id << 4) + digit;
839 }
840 if (i == 4 && !var_name16[8])
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200841 show_efi_boot_opt(var_name16);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900842 }
AKASHI Takahiroffe21572019-04-26 09:44:18 +0900843
844 free(var_name16);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900845
846 return CMD_RET_SUCCESS;
847}
848
849/**
850 * show_efi_boot_order() - show order of UEFI load options
851 *
852 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
853 *
854 * Show order of UEFI load options defined by BootOrder variable.
855 */
856static int show_efi_boot_order(void)
857{
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +0200858 u16 *bootorder;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900859 efi_uintn_t size;
860 int num, i;
861 char var_name[9];
862 u16 var_name16[9], *p16;
863 void *data;
864 struct efi_load_option lo;
865 char *label, *p;
866 size_t label_len16, label_len;
867 efi_status_t ret;
868
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900869 size = 0;
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200870 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +0200871 NULL, &size, NULL));
872 if (ret != EFI_BUFFER_TOO_SMALL) {
873 if (ret == EFI_NOT_FOUND) {
874 printf("BootOrder not defined\n");
875 return CMD_RET_SUCCESS;
876 } else {
877 return CMD_RET_FAILURE;
878 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900879 }
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +0200880 bootorder = malloc(size);
881 if (!bootorder) {
882 printf("ERROR: Out of memory\n");
883 return CMD_RET_FAILURE;
884 }
885 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
886 NULL, &size, bootorder));
887 if (ret != EFI_SUCCESS) {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900888 ret = CMD_RET_FAILURE;
889 goto out;
890 }
891
892 num = size / sizeof(u16);
893 for (i = 0; i < num; i++) {
894 sprintf(var_name, "Boot%04X", bootorder[i]);
895 p16 = var_name16;
896 utf8_utf16_strncpy(&p16, var_name, 9);
897
898 size = 0;
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +0200899 ret = EFI_CALL(efi_get_variable(var_name16,
900 &efi_global_variable_guid, NULL,
901 &size, NULL));
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900902 if (ret != EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +0200903 printf("%2d: %s: (not defined)\n", i + 1, var_name);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900904 continue;
905 }
906
907 data = malloc(size);
908 if (!data) {
909 ret = CMD_RET_FAILURE;
910 goto out;
911 }
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +0200912 ret = EFI_CALL(efi_get_variable(var_name16,
913 &efi_global_variable_guid, NULL,
914 &size, data));
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900915 if (ret != EFI_SUCCESS) {
916 free(data);
917 ret = CMD_RET_FAILURE;
918 goto out;
919 }
920
921 efi_deserialize_load_option(&lo, data);
922
923 label_len16 = u16_strlen(lo.label);
924 label_len = utf16_utf8_strnlen(lo.label, label_len16);
925 label = malloc(label_len + 1);
926 if (!label) {
927 free(data);
928 ret = CMD_RET_FAILURE;
929 goto out;
930 }
931 p = label;
932 utf16_utf8_strncpy(&p, lo.label, label_len16);
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +0200933 printf("%2d: %s: %s\n", i + 1, var_name, label);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900934 free(label);
935
936 free(data);
937 }
938out:
939 free(bootorder);
940
941 return ret;
942}
943
944/**
945 * do_efi_boot_next() - manage UEFI BootNext variable
946 *
947 * @cmdtp: Command table
948 * @flag: Command flag
949 * @argc: Number of arguments
950 * @argv: Argument array
951 * Return: CMD_RET_SUCCESS on success,
952 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
953 *
954 * Implement efidebug "boot next" sub-command.
955 * Set BootNext variable.
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200956 *
957 * efidebug boot next <id>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900958 */
959static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
960 int argc, char * const argv[])
961{
962 u16 bootnext;
963 efi_uintn_t size;
964 char *endp;
965 efi_guid_t guid;
966 efi_status_t ret;
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200967 int r = CMD_RET_SUCCESS;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900968
969 if (argc != 2)
970 return CMD_RET_USAGE;
971
972 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
Heinrich Schuchardtbdb15772020-05-09 17:59:19 +0200973 if (*endp) {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900974 printf("invalid value: %s\n", argv[1]);
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200975 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900976 goto out;
977 }
978
979 guid = efi_global_variable_guid;
980 size = sizeof(u16);
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200981 ret = EFI_CALL(efi_set_variable(L"BootNext", &guid,
AKASHI Takahirof658c2e2019-06-04 15:52:10 +0900982 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900983 EFI_VARIABLE_BOOTSERVICE_ACCESS |
984 EFI_VARIABLE_RUNTIME_ACCESS,
985 size, &bootnext));
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200986 if (ret != EFI_SUCCESS) {
987 printf("Cannot set BootNext\n");
988 r = CMD_RET_FAILURE;
989 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900990out:
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200991 return r;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900992}
993
994/**
995 * do_efi_boot_order() - manage UEFI BootOrder variable
996 *
997 * @cmdtp: Command table
998 * @flag: Command flag
999 * @argc: Number of arguments
1000 * @argv: Argument array
1001 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1002 *
1003 * Implement efidebug "boot order" sub-command.
1004 * Show order of UEFI load options, or change it in BootOrder variable.
Heinrich Schuchardt0b016562019-07-14 14:00:41 +02001005 *
1006 * efidebug boot order [<id> ...]
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001007 */
1008static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
1009 int argc, char * const argv[])
1010{
1011 u16 *bootorder = NULL;
1012 efi_uintn_t size;
1013 int id, i;
1014 char *endp;
1015 efi_guid_t guid;
1016 efi_status_t ret;
Heinrich Schuchardta332f252019-06-20 12:59:45 +02001017 int r = CMD_RET_SUCCESS;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001018
1019 if (argc == 1)
1020 return show_efi_boot_order();
1021
1022 argc--;
1023 argv++;
1024
1025 size = argc * sizeof(u16);
1026 bootorder = malloc(size);
1027 if (!bootorder)
1028 return CMD_RET_FAILURE;
1029
1030 for (i = 0; i < argc; i++) {
1031 id = (int)simple_strtoul(argv[i], &endp, 16);
1032 if (*endp != '\0' || id > 0xffff) {
1033 printf("invalid value: %s\n", argv[i]);
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001034 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001035 goto out;
1036 }
1037
1038 bootorder[i] = (u16)id;
1039 }
1040
1041 guid = efi_global_variable_guid;
Heinrich Schuchardta30c7232020-05-02 16:08:37 +02001042 ret = EFI_CALL(efi_set_variable(L"BootOrder", &guid,
AKASHI Takahirof658c2e2019-06-04 15:52:10 +09001043 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001044 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1045 EFI_VARIABLE_RUNTIME_ACCESS,
1046 size, bootorder));
Heinrich Schuchardta332f252019-06-20 12:59:45 +02001047 if (ret != EFI_SUCCESS) {
1048 printf("Cannot set BootOrder\n");
1049 r = CMD_RET_FAILURE;
1050 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001051out:
1052 free(bootorder);
1053
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001054 return r;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001055}
1056
1057static cmd_tbl_t cmd_efidebug_boot_sub[] = {
1058 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1059 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1060 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1061 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1062 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1063 "", ""),
1064};
1065
1066/**
1067 * do_efi_boot_opt() - manage UEFI load options
1068 *
1069 * @cmdtp: Command table
1070 * @flag: Command flag
1071 * @argc: Number of arguments
1072 * @argv: Argument array
1073 * Return: CMD_RET_SUCCESS on success,
1074 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1075 *
1076 * Implement efidebug "boot" sub-command.
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001077 */
1078static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag,
1079 int argc, char * const argv[])
1080{
1081 cmd_tbl_t *cp;
1082
1083 if (argc < 2)
1084 return CMD_RET_USAGE;
1085
1086 argc--; argv++;
1087
1088 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1089 ARRAY_SIZE(cmd_efidebug_boot_sub));
1090 if (!cp)
1091 return CMD_RET_USAGE;
1092
1093 return cp->cmd(cmdtp, flag, argc, argv);
1094}
1095
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001096/**
1097 * do_efi_test_bootmgr() - run simple bootmgr for test
1098 *
1099 * @cmdtp: Command table
1100 * @flag: Command flag
1101 * @argc: Number of arguments
1102 * @argv: Argument array
1103 * Return: CMD_RET_SUCCESS on success,
1104 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1105 *
1106 * Implement efidebug "test bootmgr" sub-command.
1107 * Run simple bootmgr for test.
1108 *
1109 * efidebug test bootmgr
1110 */
1111static int do_efi_test_bootmgr(cmd_tbl_t *cmdtp, int flag,
1112 int argc, char * const argv[])
1113{
1114 efi_handle_t image;
1115 efi_uintn_t exit_data_size = 0;
1116 u16 *exit_data = NULL;
1117 efi_status_t ret;
1118
1119 ret = efi_bootmgr_load(&image);
1120 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1121
1122 /* We call efi_start_image() even if error for test purpose. */
1123 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1124 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1125 if (ret && exit_data)
1126 efi_free_pool(exit_data);
1127
1128 efi_restore_gd();
1129
1130 return CMD_RET_SUCCESS;
1131}
1132
1133static cmd_tbl_t cmd_efidebug_test_sub[] = {
1134 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1135 "", ""),
1136};
1137
1138/**
1139 * do_efi_test() - manage UEFI load options
1140 *
1141 * @cmdtp: Command table
1142 * @flag: Command flag
1143 * @argc: Number of arguments
1144 * @argv: Argument array
1145 * Return: CMD_RET_SUCCESS on success,
1146 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1147 *
1148 * Implement efidebug "test" sub-command.
1149 */
1150static int do_efi_test(cmd_tbl_t *cmdtp, int flag,
1151 int argc, char * const argv[])
1152{
1153 cmd_tbl_t *cp;
1154
1155 if (argc < 2)
1156 return CMD_RET_USAGE;
1157
1158 argc--; argv++;
1159
1160 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1161 ARRAY_SIZE(cmd_efidebug_test_sub));
1162 if (!cp)
1163 return CMD_RET_USAGE;
1164
1165 return cp->cmd(cmdtp, flag, argc, argv);
1166}
1167
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001168static cmd_tbl_t cmd_efidebug_sub[] = {
1169 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
AKASHI Takahiro355cdb52019-02-25 15:54:39 +09001170 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1171 "", ""),
AKASHI Takahiro66eaf562019-02-25 15:54:40 +09001172 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1173 "", ""),
AKASHI Takahiroa8014622019-02-25 15:54:41 +09001174 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1175 "", ""),
AKASHI Takahirofa536732019-02-25 15:54:42 +09001176 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1177 "", ""),
AKASHI Takahiro00358bb2019-02-25 15:54:43 +09001178 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1179 "", ""),
Heinrich Schuchardt986e0642020-01-07 05:57:47 +01001180 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1181 "", ""),
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001182 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1183 "", ""),
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001184};
1185
1186/**
1187 * do_efidebug() - display and configure UEFI environment
1188 *
1189 * @cmdtp: Command table
1190 * @flag: Command flag
1191 * @argc: Number of arguments
1192 * @argv: Argument array
1193 * Return: CMD_RET_SUCCESS on success,
1194 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1195 *
1196 * Implement efidebug command which allows us to display and
1197 * configure UEFI environment.
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001198 */
1199static int do_efidebug(cmd_tbl_t *cmdtp, int flag,
1200 int argc, char * const argv[])
1201{
1202 cmd_tbl_t *cp;
1203 efi_status_t r;
1204
1205 if (argc < 2)
1206 return CMD_RET_USAGE;
1207
1208 argc--; argv++;
1209
1210 /* Initialize UEFI drivers */
1211 r = efi_init_obj_list();
1212 if (r != EFI_SUCCESS) {
1213 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1214 r & ~EFI_ERROR_MASK);
1215 return CMD_RET_FAILURE;
1216 }
1217
1218 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1219 ARRAY_SIZE(cmd_efidebug_sub));
1220 if (!cp)
1221 return CMD_RET_USAGE;
1222
1223 return cp->cmd(cmdtp, flag, argc, argv);
1224}
1225
1226#ifdef CONFIG_SYS_LONGHELP
1227static char efidebug_help_text[] =
1228 " - UEFI Shell-like interface to configure UEFI environment\n"
1229 "\n"
1230 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
1231 " - set UEFI BootXXXX variable\n"
1232 " <load options> will be passed to UEFI application\n"
1233 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1234 " - delete UEFI BootXXXX variables\n"
1235 "efidebug boot dump\n"
1236 " - dump all UEFI BootXXXX variables\n"
1237 "efidebug boot next <bootid>\n"
1238 " - set UEFI BootNext variable\n"
1239 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1240 " - set/show UEFI boot order\n"
AKASHI Takahiro355cdb52019-02-25 15:54:39 +09001241 "\n"
1242 "efidebug devices\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001243 " - show UEFI devices\n"
AKASHI Takahiro66eaf562019-02-25 15:54:40 +09001244 "efidebug drivers\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001245 " - show UEFI drivers\n"
AKASHI Takahiroa8014622019-02-25 15:54:41 +09001246 "efidebug dh\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001247 " - show UEFI handles\n"
AKASHI Takahirofa536732019-02-25 15:54:42 +09001248 "efidebug images\n"
AKASHI Takahiro00358bb2019-02-25 15:54:43 +09001249 " - show loaded images\n"
1250 "efidebug memmap\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001251 " - show UEFI memory map\n"
Heinrich Schuchardt986e0642020-01-07 05:57:47 +01001252 "efidebug tables\n"
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001253 " - show UEFI configuration tables\n"
1254 "efidebug test bootmgr\n"
1255 " - run simple bootmgr for test\n";
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001256#endif
1257
1258U_BOOT_CMD(
1259 efidebug, 10, 0, do_efidebug,
1260 "Configure UEFI environment",
1261 efidebug_help_text
1262);