blob: 9874838b005a0d8ef4768adfda9bef09adebf091 [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>
11#include <efi_loader.h>
AKASHI Takahiroe7c08832019-02-25 15:54:38 +090012#include <exports.h>
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +020013#include <hexdump.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
AKASHI Takahiroe7c08832019-02-25 15:54:38 +090015#include <malloc.h>
Heinrich Schuchardtfec10a42020-03-14 08:44:07 +010016#include <mapmem.h>
AKASHI Takahiroe7c08832019-02-25 15:54:38 +090017#include <search.h>
18#include <linux/ctype.h>
19
AKASHI Takahiroe32788a2019-02-25 15:54:39 +090020#define BS systab.boottime
AKASHI Takahiroe7c08832019-02-25 15:54:38 +090021
22/**
AKASHI Takahiroe32788a2019-02-25 15:54:39 +090023 * efi_get_device_handle_info() - get information of UEFI device
24 *
25 * @handle: Handle of UEFI device
26 * @dev_path_text: Pointer to text of device path
27 * Return: 0 on success, -1 on failure
28 *
29 * Currently return a formatted text of device path.
30 */
31static int efi_get_device_handle_info(efi_handle_t handle, u16 **dev_path_text)
32{
33 struct efi_device_path *dp;
34 efi_status_t ret;
35
36 ret = EFI_CALL(BS->open_protocol(handle, &efi_guid_device_path,
37 (void **)&dp, NULL /* FIXME */, NULL,
38 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
39 if (ret == EFI_SUCCESS) {
40 *dev_path_text = efi_dp_str(dp);
41 return 0;
42 } else {
43 return -1;
44 }
45}
46
47#define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
48
49static const char spc[] = " ";
50static const char sep[] = "================";
51
52/**
53 * do_efi_show_devices() - show UEFI devices
54 *
55 * @cmdtp: Command table
56 * @flag: Command flag
57 * @argc: Number of arguments
58 * @argv: Argument array
59 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
60 *
61 * Implement efidebug "devices" sub-command.
62 * Show all UEFI devices and their information.
63 */
Simon Glassed38aef2020-05-10 11:40:03 -060064static int do_efi_show_devices(struct cmd_tbl *cmdtp, int flag,
65 int argc, char *const argv[])
AKASHI Takahiroe32788a2019-02-25 15:54:39 +090066{
67 efi_handle_t *handles;
68 efi_uintn_t num, i;
69 u16 *dev_path_text;
70 efi_status_t ret;
71
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +020072 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
AKASHI Takahiroe32788a2019-02-25 15:54:39 +090073 &num, &handles));
74 if (ret != EFI_SUCCESS)
75 return CMD_RET_FAILURE;
76
77 if (!num)
78 return CMD_RET_SUCCESS;
79
80 printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc);
81 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
82 for (i = 0; i < num; i++) {
83 if (!efi_get_device_handle_info(handles[i], &dev_path_text)) {
84 printf("%p %ls\n", handles[i], dev_path_text);
85 efi_free_pool(dev_path_text);
86 }
87 }
88
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +020089 efi_free_pool(handles);
AKASHI Takahiroe32788a2019-02-25 15:54:39 +090090
91 return CMD_RET_SUCCESS;
92}
93
94/**
AKASHI Takahiro185716a2019-02-25 15:54:40 +090095 * efi_get_driver_handle_info() - get information of UEFI driver
96 *
97 * @handle: Handle of UEFI device
98 * @driver_name: Driver name
99 * @image_path: Pointer to text of device path
100 * Return: 0 on success, -1 on failure
101 *
102 * Currently return no useful information as all UEFI drivers are
103 * built-in..
104 */
105static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
106 u16 **image_path)
107{
108 struct efi_handler *handler;
109 struct efi_loaded_image *image;
110 efi_status_t ret;
111
112 /*
113 * driver name
114 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
115 */
116 *driver_name = NULL;
117
118 /* image name */
119 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
120 if (ret != EFI_SUCCESS) {
121 *image_path = NULL;
122 return 0;
123 }
124
125 image = handler->protocol_interface;
126 *image_path = efi_dp_str(image->file_path);
127
128 return 0;
129}
130
131/**
132 * do_efi_show_drivers() - show UEFI drivers
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 "drivers" sub-command.
141 * Show all UEFI drivers and their information.
142 */
Simon Glassed38aef2020-05-10 11:40:03 -0600143static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
144 int argc, char *const argv[])
AKASHI Takahiro185716a2019-02-25 15:54:40 +0900145{
146 efi_handle_t *handles;
147 efi_uintn_t num, i;
148 u16 *driver_name, *image_path_text;
149 efi_status_t ret;
150
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200151 ret = EFI_CALL(efi_locate_handle_buffer(
AKASHI Takahiro185716a2019-02-25 15:54:40 +0900152 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
153 NULL, &num, &handles));
154 if (ret != EFI_SUCCESS)
155 return CMD_RET_FAILURE;
156
157 if (!num)
158 return CMD_RET_SUCCESS;
159
160 printf("Driver%.*s Name Image Path\n",
161 EFI_HANDLE_WIDTH - 6, spc);
162 printf("%.*s ==================== ====================\n",
163 EFI_HANDLE_WIDTH, sep);
164 for (i = 0; i < num; i++) {
165 if (!efi_get_driver_handle_info(handles[i], &driver_name,
166 &image_path_text)) {
167 if (image_path_text)
168 printf("%p %-20ls %ls\n", handles[i],
169 driver_name, image_path_text);
170 else
171 printf("%p %-20ls <built-in>\n",
172 handles[i], driver_name);
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200173 efi_free_pool(driver_name);
174 efi_free_pool(image_path_text);
AKASHI Takahiro185716a2019-02-25 15:54:40 +0900175 }
176 }
177
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200178 efi_free_pool(handles);
AKASHI Takahiro185716a2019-02-25 15:54:40 +0900179
180 return CMD_RET_SUCCESS;
181}
182
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900183static const struct {
184 const char *text;
185 const efi_guid_t guid;
186} guid_list[] = {
187 {
188 "Device Path",
Heinrich Schuchardt788ad412019-04-20 07:39:11 +0200189 EFI_DEVICE_PATH_PROTOCOL_GUID,
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900190 },
191 {
192 "Device Path To Text",
193 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
194 },
195 {
196 "Device Path Utilities",
197 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
198 },
199 {
200 "Unicode Collation 2",
201 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
202 },
203 {
204 "Driver Binding",
205 EFI_DRIVER_BINDING_PROTOCOL_GUID,
206 },
207 {
208 "Simple Text Input",
209 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
210 },
211 {
212 "Simple Text Input Ex",
213 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
214 },
215 {
216 "Simple Text Output",
217 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
218 },
219 {
220 "Block IO",
Heinrich Schuchardt788ad412019-04-20 07:39:11 +0200221 EFI_BLOCK_IO_PROTOCOL_GUID,
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900222 },
223 {
224 "Simple File System",
225 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
226 },
227 {
228 "Loaded Image",
Heinrich Schuchardt788ad412019-04-20 07:39:11 +0200229 EFI_LOADED_IMAGE_PROTOCOL_GUID,
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900230 },
231 {
Heinrich Schuchardt788ad412019-04-20 07:39:11 +0200232 "Graphics Output",
233 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900234 },
Heinrich Schuchardtae776902019-04-20 07:57:28 +0200235 {
236 "HII String",
237 EFI_HII_STRING_PROTOCOL_GUID,
238 },
239 {
240 "HII Database",
241 EFI_HII_DATABASE_PROTOCOL_GUID,
242 },
243 {
244 "HII Config Routing",
245 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
246 },
247 {
Ilias Apalodimas3510ba72020-02-21 09:55:45 +0200248 "Load File2",
249 EFI_LOAD_FILE2_PROTOCOL_GUID,
250 },
251 {
Heinrich Schuchardtae776902019-04-20 07:57:28 +0200252 "Simple Network",
253 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
254 },
255 {
256 "PXE Base Code",
257 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
258 },
Heinrich Schuchardt75844e52020-01-07 05:57:47 +0100259 /* Configuration table GUIDs */
260 {
261 "ACPI table",
262 EFI_ACPI_TABLE_GUID,
263 },
264 {
265 "device tree",
266 EFI_FDT_GUID,
267 },
268 {
269 "SMBIOS table",
270 SMBIOS_TABLE_GUID,
271 },
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100272 {
273 "Runtime properties",
274 EFI_RT_PROPERTIES_TABLE_GUID,
275 },
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900276};
277
278/**
Heinrich Schuchardtc6fc5022020-01-07 06:02:33 +0100279 * get_guid_text - get string of GUID
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900280 *
Heinrich Schuchardtc6fc5022020-01-07 06:02:33 +0100281 * Return description of GUID.
282 *
283 * @guid: GUID
284 * Return: description of GUID or NULL
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900285 */
Heinrich Schuchardtc6fc5022020-01-07 06:02:33 +0100286static const char *get_guid_text(const void *guid)
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900287{
288 int i;
289
Heinrich Schuchardtc6fc5022020-01-07 06:02:33 +0100290 for (i = 0; i < ARRAY_SIZE(guid_list); i++) {
291 /*
292 * As guidcmp uses memcmp() we can safely accept unaligned
293 * GUIDs.
294 */
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900295 if (!guidcmp(&guid_list[i].guid, guid))
Heinrich Schuchardtc6fc5022020-01-07 06:02:33 +0100296 return guid_list[i].text;
297 }
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900298
Heinrich Schuchardtc6fc5022020-01-07 06:02:33 +0100299 return NULL;
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900300}
301
302/**
303 * do_efi_show_handles() - show UEFI handles
304 *
305 * @cmdtp: Command table
306 * @flag: Command flag
307 * @argc: Number of arguments
308 * @argv: Argument array
309 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
310 *
311 * Implement efidebug "dh" sub-command.
312 * Show all UEFI handles and their information, currently all protocols
313 * added to handle.
314 */
Simon Glassed38aef2020-05-10 11:40:03 -0600315static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
316 int argc, char *const argv[])
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900317{
318 efi_handle_t *handles;
319 efi_guid_t **guid;
320 efi_uintn_t num, count, i, j;
321 const char *guid_text;
322 efi_status_t ret;
323
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200324 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900325 &num, &handles));
326 if (ret != EFI_SUCCESS)
327 return CMD_RET_FAILURE;
328
329 if (!num)
330 return CMD_RET_SUCCESS;
331
332 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
333 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
334 for (i = 0; i < num; i++) {
335 printf("%p", handles[i]);
336 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
337 &count));
338 if (ret || !count) {
339 putc('\n');
340 continue;
341 }
342
343 for (j = 0; j < count; j++) {
344 if (j)
345 printf(", ");
346 else
347 putc(' ');
348
349 guid_text = get_guid_text(guid[j]);
350 if (guid_text)
351 puts(guid_text);
352 else
353 printf("%pUl", guid[j]);
354 }
355 putc('\n');
356 }
357
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200358 efi_free_pool(handles);
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900359
360 return CMD_RET_SUCCESS;
361}
362
AKASHI Takahiro185716a2019-02-25 15:54:40 +0900363/**
AKASHI Takahiro71ab1bf2019-02-25 15:54:42 +0900364 * do_efi_show_images() - show UEFI images
365 *
366 * @cmdtp: Command table
367 * @flag: Command flag
368 * @argc: Number of arguments
369 * @argv: Argument array
370 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
371 *
372 * Implement efidebug "images" sub-command.
373 * Show all UEFI loaded images and their information.
374 */
Simon Glassed38aef2020-05-10 11:40:03 -0600375static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
376 int argc, char *const argv[])
AKASHI Takahiro71ab1bf2019-02-25 15:54:42 +0900377{
378 efi_print_image_infos(NULL);
379
380 return CMD_RET_SUCCESS;
381}
382
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900383static const char * const efi_mem_type_string[] = {
384 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
385 [EFI_LOADER_CODE] = "LOADER CODE",
386 [EFI_LOADER_DATA] = "LOADER DATA",
387 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
388 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
389 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
390 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
391 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
392 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
393 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
394 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
395 [EFI_MMAP_IO] = "IO",
396 [EFI_MMAP_IO_PORT] = "IO PORT",
397 [EFI_PAL_CODE] = "PAL",
Heinrich Schuchardt45176e82020-04-29 20:21:39 +0200398 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900399};
400
401static const struct efi_mem_attrs {
402 const u64 bit;
403 const char *text;
404} efi_mem_attrs[] = {
405 {EFI_MEMORY_UC, "UC"},
406 {EFI_MEMORY_UC, "UC"},
407 {EFI_MEMORY_WC, "WC"},
408 {EFI_MEMORY_WT, "WT"},
409 {EFI_MEMORY_WB, "WB"},
410 {EFI_MEMORY_UCE, "UCE"},
411 {EFI_MEMORY_WP, "WP"},
412 {EFI_MEMORY_RP, "RP"},
413 {EFI_MEMORY_XP, "WP"},
414 {EFI_MEMORY_NV, "NV"},
415 {EFI_MEMORY_MORE_RELIABLE, "REL"},
416 {EFI_MEMORY_RO, "RO"},
Heinrich Schuchardte76f0e42020-05-19 07:20:46 +0200417 {EFI_MEMORY_SP, "SP"},
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900418 {EFI_MEMORY_RUNTIME, "RT"},
419};
420
421/**
422 * print_memory_attributes() - print memory map attributes
Heinrich Schuchardtb1d65432019-07-14 14:00:41 +0200423 *
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900424 * @attributes: Attribute value
425 *
426 * Print memory map attributes
427 */
428static void print_memory_attributes(u64 attributes)
429{
430 int sep, i;
431
432 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
433 if (attributes & efi_mem_attrs[i].bit) {
434 if (sep) {
435 putc('|');
436 } else {
437 putc(' ');
438 sep = 1;
439 }
440 puts(efi_mem_attrs[i].text);
441 }
442}
443
444#define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
445
446/**
447 * do_efi_show_memmap() - show UEFI memory map
448 *
449 * @cmdtp: Command table
450 * @flag: Command flag
451 * @argc: Number of arguments
452 * @argv: Argument array
453 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
454 *
455 * Implement efidebug "memmap" sub-command.
456 * Show UEFI memory map.
457 */
Simon Glassed38aef2020-05-10 11:40:03 -0600458static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
459 int argc, char *const argv[])
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900460{
461 struct efi_mem_desc *memmap = NULL, *map;
462 efi_uintn_t map_size = 0;
463 const char *type;
464 int i;
465 efi_status_t ret;
466
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200467 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900468 if (ret == EFI_BUFFER_TOO_SMALL) {
469 map_size += sizeof(struct efi_mem_desc); /* for my own */
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200470 ret = efi_allocate_pool(EFI_LOADER_DATA, map_size,
471 (void *)&memmap);
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900472 if (ret != EFI_SUCCESS)
473 return CMD_RET_FAILURE;
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200474 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900475 }
476 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200477 efi_free_pool(memmap);
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900478 return CMD_RET_FAILURE;
479 }
480
481 printf("Type Start%.*s End%.*s Attributes\n",
482 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
483 printf("================ %.*s %.*s ==========\n",
484 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
AKASHI Takahiroac0abe22020-05-08 14:50:18 +0900485 /*
486 * Coverity check: dereferencing null pointer "map."
487 * This is a false positive as memmap will always be
488 * populated by allocate_pool() above.
489 */
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900490 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
Heinrich Schuchardt45176e82020-04-29 20:21:39 +0200491 if (map->type < ARRAY_SIZE(efi_mem_type_string))
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900492 type = efi_mem_type_string[map->type];
493 else
494 type = "(unknown)";
495
496 printf("%-16s %.*llx-%.*llx", type,
497 EFI_PHYS_ADDR_WIDTH,
Heinrich Schuchardt9d4c7882020-03-27 04:33:17 +0000498 (u64)map_to_sysmem((void *)(uintptr_t)
499 map->physical_start),
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900500 EFI_PHYS_ADDR_WIDTH,
Heinrich Schuchardt9d4c7882020-03-27 04:33:17 +0000501 (u64)map_to_sysmem((void *)(uintptr_t)
502 (map->physical_start +
503 map->num_pages * EFI_PAGE_SIZE)));
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900504
505 print_memory_attributes(map->attribute);
506 putc('\n');
507 }
508
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200509 efi_free_pool(memmap);
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900510
511 return CMD_RET_SUCCESS;
512}
513
AKASHI Takahiro71ab1bf2019-02-25 15:54:42 +0900514/**
Heinrich Schuchardt75844e52020-01-07 05:57:47 +0100515 * do_efi_show_tables() - show UEFI configuration tables
516 *
517 * @cmdtp: Command table
518 * @flag: Command flag
519 * @argc: Number of arguments
520 * @argv: Argument array
521 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
522 *
523 * Implement efidebug "tables" sub-command.
524 * Show UEFI configuration tables.
525 */
Simon Glassed38aef2020-05-10 11:40:03 -0600526static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
527 int argc, char *const argv[])
Heinrich Schuchardt75844e52020-01-07 05:57:47 +0100528{
529 efi_uintn_t i;
530 const char *guid_str;
531
532 for (i = 0; i < systab.nr_tables; ++i) {
533 guid_str = get_guid_text(&systab.tables[i].guid);
534 if (!guid_str)
535 guid_str = "";
536 printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
537 }
538
539 return CMD_RET_SUCCESS;
540}
541
542/**
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900543 * do_efi_boot_add() - set UEFI load option
544 *
545 * @cmdtp: Command table
546 * @flag: Command flag
547 * @argc: Number of arguments
548 * @argv: Argument array
549 * Return: CMD_RET_SUCCESS on success,
550 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
551 *
Heinrich Schuchardtb1d65432019-07-14 14:00:41 +0200552 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
553 *
554 * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900555 */
Simon Glassed38aef2020-05-10 11:40:03 -0600556static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
557 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900558{
559 int id;
560 char *endp;
561 char var_name[9];
562 u16 var_name16[9], *p;
563 efi_guid_t guid;
564 size_t label_len, label_len16;
565 u16 *label;
566 struct efi_device_path *device_path = NULL, *file_path = NULL;
567 struct efi_load_option lo;
568 void *data = NULL;
569 efi_uintn_t size;
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +0200570 efi_status_t ret;
Heinrich Schuchardt99a21fc2019-06-20 12:59:45 +0200571 int r = CMD_RET_SUCCESS;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900572
573 if (argc < 6 || argc > 7)
574 return CMD_RET_USAGE;
575
576 id = (int)simple_strtoul(argv[1], &endp, 16);
577 if (*endp != '\0' || id > 0xffff)
Heinrich Schuchardtb2469722019-02-28 20:41:58 +0100578 return CMD_RET_USAGE;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900579
580 sprintf(var_name, "Boot%04X", id);
581 p = var_name16;
582 utf8_utf16_strncpy(&p, var_name, 9);
583
584 guid = efi_global_variable_guid;
585
586 /* attributes */
587 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
588
589 /* label */
590 label_len = strlen(argv[2]);
591 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
592 label = malloc((label_len16 + 1) * sizeof(u16));
593 if (!label)
594 return CMD_RET_FAILURE;
595 lo.label = label; /* label will be changed below */
596 utf8_utf16_strncpy(&label, argv[2], label_len);
597
598 /* file path */
599 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
600 &file_path);
601 if (ret != EFI_SUCCESS) {
602 printf("Cannot create device path for \"%s %s\"\n",
603 argv[3], argv[4]);
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +0200604 r = CMD_RET_FAILURE;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900605 goto out;
606 }
607 lo.file_path = file_path;
608 lo.file_path_length = efi_dp_size(file_path)
609 + sizeof(struct efi_device_path); /* for END */
610
611 /* optional data */
AKASHI Takahiro085372e2020-05-08 14:50:47 +0900612 if (argc == 6)
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200613 lo.optional_data = NULL;
614 else
615 lo.optional_data = (const u8 *)argv[6];
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900616
617 size = efi_serialize_load_option(&lo, (u8 **)&data);
618 if (!size) {
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +0200619 r = CMD_RET_FAILURE;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900620 goto out;
621 }
622
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200623 ret = EFI_CALL(efi_set_variable(var_name16, &guid,
AKASHI Takahiroc1f3ebc2019-06-04 15:52:10 +0900624 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900625 EFI_VARIABLE_BOOTSERVICE_ACCESS |
626 EFI_VARIABLE_RUNTIME_ACCESS,
627 size, data));
Heinrich Schuchardt99a21fc2019-06-20 12:59:45 +0200628 if (ret != EFI_SUCCESS) {
629 printf("Cannot set %ls\n", var_name16);
630 r = CMD_RET_FAILURE;
631 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900632out:
633 free(data);
634 efi_free_pool(device_path);
635 efi_free_pool(file_path);
636 free(lo.label);
637
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +0200638 return r;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900639}
640
641/**
642 * do_efi_boot_rm() - delete UEFI load options
643 *
644 * @cmdtp: Command table
645 * @flag: Command flag
646 * @argc: Number of arguments
647 * @argv: Argument array
648 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
649 *
650 * Implement efidebug "boot rm" sub-command.
651 * Delete UEFI load options.
Heinrich Schuchardtb1d65432019-07-14 14:00:41 +0200652 *
653 * efidebug boot rm <id> ...
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900654 */
Simon Glassed38aef2020-05-10 11:40:03 -0600655static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag,
656 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900657{
658 efi_guid_t guid;
659 int id, i;
660 char *endp;
661 char var_name[9];
AKASHI Takahirocfdccd22020-02-28 09:05:04 +0900662 u16 var_name16[9], *p;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900663 efi_status_t ret;
664
665 if (argc == 1)
666 return CMD_RET_USAGE;
667
668 guid = efi_global_variable_guid;
669 for (i = 1; i < argc; i++, argv++) {
670 id = (int)simple_strtoul(argv[1], &endp, 16);
671 if (*endp != '\0' || id > 0xffff)
672 return CMD_RET_FAILURE;
673
674 sprintf(var_name, "Boot%04X", id);
AKASHI Takahirocfdccd22020-02-28 09:05:04 +0900675 p = var_name16;
676 utf8_utf16_strncpy(&p, var_name, 9);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900677
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200678 ret = EFI_CALL(efi_set_variable(var_name16, &guid, 0, 0, NULL));
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900679 if (ret) {
Heinrich Schuchardt3be0bda2020-03-02 20:13:10 +0100680 printf("Cannot remove %ls\n", var_name16);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900681 return CMD_RET_FAILURE;
682 }
683 }
684
685 return CMD_RET_SUCCESS;
686}
687
688/**
689 * show_efi_boot_opt_data() - dump UEFI load option
690 *
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200691 * @varname16: variable name
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200692 * @data: value of UEFI load option variable
693 * @size: size of the boot option
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900694 *
695 * Decode the value of UEFI load option variable and print information.
696 */
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200697static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size)
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900698{
699 struct efi_load_option lo;
700 char *label, *p;
701 size_t label_len16, label_len;
702 u16 *dp_str;
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200703 efi_status_t ret;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900704
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200705 ret = efi_deserialize_load_option(&lo, data, size);
706 if (ret != EFI_SUCCESS) {
707 printf("%ls: invalid load option\n", varname16);
708 return;
709 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900710
711 label_len16 = u16_strlen(lo.label);
712 label_len = utf16_utf8_strnlen(lo.label, label_len16);
713 label = malloc(label_len + 1);
714 if (!label)
715 return;
716 p = label;
717 utf16_utf8_strncpy(&p, lo.label, label_len16);
718
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200719 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
720 varname16,
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900721 /* ACTIVE */
722 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
723 /* FORCE RECONNECT */
724 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
725 /* HIDDEN */
726 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
727 lo.attributes);
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200728 printf(" label: %s\n", label);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900729
730 dp_str = efi_dp_str(lo.file_path);
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200731 printf(" file_path: %ls\n", dp_str);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900732 efi_free_pool(dp_str);
733
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200734 printf(" data:\n");
735 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200736 lo.optional_data, *size, true);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900737 free(label);
738}
739
740/**
741 * show_efi_boot_opt() - dump UEFI load option
742 *
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200743 * @varname16: variable name
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900744 *
745 * Dump information defined by UEFI load option.
746 */
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200747static void show_efi_boot_opt(u16 *varname16)
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900748{
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200749 void *data;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900750 efi_uintn_t size;
AKASHI Takahirodab93f42019-11-26 10:11:22 +0900751 efi_status_t ret;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900752
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900753 size = 0;
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200754 ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
755 NULL, &size, NULL));
AKASHI Takahirodab93f42019-11-26 10:11:22 +0900756 if (ret == EFI_BUFFER_TOO_SMALL) {
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900757 data = malloc(size);
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200758 if (!data) {
759 printf("ERROR: Out of memory\n");
760 return;
761 }
762 ret = EFI_CALL(efi_get_variable(varname16,
763 &efi_global_variable_guid,
764 NULL, &size, data));
765 if (ret == EFI_SUCCESS)
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200766 show_efi_boot_opt_data(varname16, data, &size);
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200767 free(data);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900768 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900769}
770
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +0900771static int u16_tohex(u16 c)
772{
773 if (c >= '0' && c <= '9')
774 return c - '0';
775 if (c >= 'A' && c <= 'F')
776 return c - 'A' + 10;
777
778 /* not hexadecimal */
779 return -1;
780}
781
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900782/**
783 * show_efi_boot_dump() - dump all UEFI load options
784 *
785 * @cmdtp: Command table
786 * @flag: Command flag
787 * @argc: Number of arguments
788 * @argv: Argument array
789 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
790 *
791 * Implement efidebug "boot dump" sub-command.
792 * Dump information of all UEFI load options defined.
Heinrich Schuchardt21715092019-07-25 20:52:23 +0200793 *
794 * efidebug boot dump
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900795 */
Simon Glassed38aef2020-05-10 11:40:03 -0600796static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
797 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900798{
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +0900799 u16 *var_name16, *p;
800 efi_uintn_t buf_size, size;
801 efi_guid_t guid;
802 int id, i, digit;
803 efi_status_t ret;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900804
805 if (argc > 1)
806 return CMD_RET_USAGE;
807
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +0900808 buf_size = 128;
809 var_name16 = malloc(buf_size);
810 if (!var_name16)
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900811 return CMD_RET_FAILURE;
812
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +0900813 var_name16[0] = 0;
814 for (;;) {
815 size = buf_size;
816 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
817 &guid));
818 if (ret == EFI_NOT_FOUND)
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900819 break;
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +0900820 if (ret == EFI_BUFFER_TOO_SMALL) {
821 buf_size = size;
822 p = realloc(var_name16, buf_size);
823 if (!p) {
824 free(var_name16);
825 return CMD_RET_FAILURE;
826 }
827 var_name16 = p;
828 ret = EFI_CALL(efi_get_next_variable_name(&size,
829 var_name16,
830 &guid));
831 }
832 if (ret != EFI_SUCCESS) {
833 free(var_name16);
834 return CMD_RET_FAILURE;
835 }
836
837 if (memcmp(var_name16, L"Boot", 8))
838 continue;
839
840 for (id = 0, i = 0; i < 4; i++) {
841 digit = u16_tohex(var_name16[4 + i]);
842 if (digit < 0)
843 break;
844 id = (id << 4) + digit;
845 }
846 if (i == 4 && !var_name16[8])
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200847 show_efi_boot_opt(var_name16);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900848 }
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +0900849
850 free(var_name16);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900851
852 return CMD_RET_SUCCESS;
853}
854
855/**
856 * show_efi_boot_order() - show order of UEFI load options
857 *
858 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
859 *
860 * Show order of UEFI load options defined by BootOrder variable.
861 */
862static int show_efi_boot_order(void)
863{
Heinrich Schuchardt98fc6152020-04-29 21:15:08 +0200864 u16 *bootorder;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900865 efi_uintn_t size;
866 int num, i;
867 char var_name[9];
868 u16 var_name16[9], *p16;
869 void *data;
870 struct efi_load_option lo;
871 char *label, *p;
872 size_t label_len16, label_len;
873 efi_status_t ret;
874
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900875 size = 0;
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200876 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
Heinrich Schuchardt98fc6152020-04-29 21:15:08 +0200877 NULL, &size, NULL));
878 if (ret != EFI_BUFFER_TOO_SMALL) {
879 if (ret == EFI_NOT_FOUND) {
880 printf("BootOrder not defined\n");
881 return CMD_RET_SUCCESS;
882 } else {
883 return CMD_RET_FAILURE;
884 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900885 }
Heinrich Schuchardt98fc6152020-04-29 21:15:08 +0200886 bootorder = malloc(size);
887 if (!bootorder) {
888 printf("ERROR: Out of memory\n");
889 return CMD_RET_FAILURE;
890 }
891 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
892 NULL, &size, bootorder));
893 if (ret != EFI_SUCCESS) {
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900894 ret = CMD_RET_FAILURE;
895 goto out;
896 }
897
898 num = size / sizeof(u16);
899 for (i = 0; i < num; i++) {
900 sprintf(var_name, "Boot%04X", bootorder[i]);
901 p16 = var_name16;
902 utf8_utf16_strncpy(&p16, var_name, 9);
903
904 size = 0;
Heinrich Schuchardt98fc6152020-04-29 21:15:08 +0200905 ret = EFI_CALL(efi_get_variable(var_name16,
906 &efi_global_variable_guid, NULL,
907 &size, NULL));
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900908 if (ret != EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardt98fc6152020-04-29 21:15:08 +0200909 printf("%2d: %s: (not defined)\n", i + 1, var_name);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900910 continue;
911 }
912
913 data = malloc(size);
914 if (!data) {
915 ret = CMD_RET_FAILURE;
916 goto out;
917 }
Heinrich Schuchardt98fc6152020-04-29 21:15:08 +0200918 ret = EFI_CALL(efi_get_variable(var_name16,
919 &efi_global_variable_guid, NULL,
920 &size, data));
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900921 if (ret != EFI_SUCCESS) {
922 free(data);
923 ret = CMD_RET_FAILURE;
924 goto out;
925 }
926
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200927 ret = efi_deserialize_load_option(&lo, data, &size);
928 if (ret != EFI_SUCCESS) {
929 printf("%ls: invalid load option\n", var_name16);
930 ret = CMD_RET_FAILURE;
931 goto out;
932 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900933
934 label_len16 = u16_strlen(lo.label);
935 label_len = utf16_utf8_strnlen(lo.label, label_len16);
936 label = malloc(label_len + 1);
937 if (!label) {
938 free(data);
939 ret = CMD_RET_FAILURE;
940 goto out;
941 }
942 p = label;
943 utf16_utf8_strncpy(&p, lo.label, label_len16);
Heinrich Schuchardt98fc6152020-04-29 21:15:08 +0200944 printf("%2d: %s: %s\n", i + 1, var_name, label);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900945 free(label);
946
947 free(data);
948 }
949out:
950 free(bootorder);
951
952 return ret;
953}
954
955/**
956 * do_efi_boot_next() - manage UEFI BootNext variable
957 *
958 * @cmdtp: Command table
959 * @flag: Command flag
960 * @argc: Number of arguments
961 * @argv: Argument array
962 * Return: CMD_RET_SUCCESS on success,
963 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
964 *
965 * Implement efidebug "boot next" sub-command.
966 * Set BootNext variable.
Heinrich Schuchardtb1d65432019-07-14 14:00:41 +0200967 *
968 * efidebug boot next <id>
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900969 */
Simon Glassed38aef2020-05-10 11:40:03 -0600970static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
971 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900972{
973 u16 bootnext;
974 efi_uintn_t size;
975 char *endp;
976 efi_guid_t guid;
977 efi_status_t ret;
Heinrich Schuchardt99a21fc2019-06-20 12:59:45 +0200978 int r = CMD_RET_SUCCESS;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900979
980 if (argc != 2)
981 return CMD_RET_USAGE;
982
983 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
Heinrich Schuchardt90c9d042020-05-09 17:59:19 +0200984 if (*endp) {
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900985 printf("invalid value: %s\n", argv[1]);
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +0200986 r = CMD_RET_FAILURE;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900987 goto out;
988 }
989
990 guid = efi_global_variable_guid;
991 size = sizeof(u16);
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200992 ret = EFI_CALL(efi_set_variable(L"BootNext", &guid,
AKASHI Takahiroc1f3ebc2019-06-04 15:52:10 +0900993 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900994 EFI_VARIABLE_BOOTSERVICE_ACCESS |
995 EFI_VARIABLE_RUNTIME_ACCESS,
996 size, &bootnext));
Heinrich Schuchardt99a21fc2019-06-20 12:59:45 +0200997 if (ret != EFI_SUCCESS) {
998 printf("Cannot set BootNext\n");
999 r = CMD_RET_FAILURE;
1000 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001001out:
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +02001002 return r;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001003}
1004
1005/**
1006 * do_efi_boot_order() - manage UEFI BootOrder variable
1007 *
1008 * @cmdtp: Command table
1009 * @flag: Command flag
1010 * @argc: Number of arguments
1011 * @argv: Argument array
1012 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1013 *
1014 * Implement efidebug "boot order" sub-command.
1015 * Show order of UEFI load options, or change it in BootOrder variable.
Heinrich Schuchardtb1d65432019-07-14 14:00:41 +02001016 *
1017 * efidebug boot order [<id> ...]
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001018 */
Simon Glassed38aef2020-05-10 11:40:03 -06001019static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
1020 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001021{
1022 u16 *bootorder = NULL;
1023 efi_uintn_t size;
1024 int id, i;
1025 char *endp;
1026 efi_guid_t guid;
1027 efi_status_t ret;
Heinrich Schuchardt99a21fc2019-06-20 12:59:45 +02001028 int r = CMD_RET_SUCCESS;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001029
1030 if (argc == 1)
1031 return show_efi_boot_order();
1032
1033 argc--;
1034 argv++;
1035
1036 size = argc * sizeof(u16);
1037 bootorder = malloc(size);
1038 if (!bootorder)
1039 return CMD_RET_FAILURE;
1040
1041 for (i = 0; i < argc; i++) {
1042 id = (int)simple_strtoul(argv[i], &endp, 16);
1043 if (*endp != '\0' || id > 0xffff) {
1044 printf("invalid value: %s\n", argv[i]);
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +02001045 r = CMD_RET_FAILURE;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001046 goto out;
1047 }
1048
1049 bootorder[i] = (u16)id;
1050 }
1051
1052 guid = efi_global_variable_guid;
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +02001053 ret = EFI_CALL(efi_set_variable(L"BootOrder", &guid,
AKASHI Takahiroc1f3ebc2019-06-04 15:52:10 +09001054 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001055 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1056 EFI_VARIABLE_RUNTIME_ACCESS,
1057 size, bootorder));
Heinrich Schuchardt99a21fc2019-06-20 12:59:45 +02001058 if (ret != EFI_SUCCESS) {
1059 printf("Cannot set BootOrder\n");
1060 r = CMD_RET_FAILURE;
1061 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001062out:
1063 free(bootorder);
1064
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +02001065 return r;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001066}
1067
Simon Glassed38aef2020-05-10 11:40:03 -06001068static struct cmd_tbl cmd_efidebug_boot_sub[] = {
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001069 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1070 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1071 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1072 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1073 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1074 "", ""),
1075};
1076
1077/**
1078 * do_efi_boot_opt() - manage UEFI load options
1079 *
1080 * @cmdtp: Command table
1081 * @flag: Command flag
1082 * @argc: Number of arguments
1083 * @argv: Argument array
1084 * Return: CMD_RET_SUCCESS on success,
1085 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1086 *
1087 * Implement efidebug "boot" sub-command.
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001088 */
Simon Glassed38aef2020-05-10 11:40:03 -06001089static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
1090 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001091{
Simon Glassed38aef2020-05-10 11:40:03 -06001092 struct cmd_tbl *cp;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001093
1094 if (argc < 2)
1095 return CMD_RET_USAGE;
1096
1097 argc--; argv++;
1098
1099 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1100 ARRAY_SIZE(cmd_efidebug_boot_sub));
1101 if (!cp)
1102 return CMD_RET_USAGE;
1103
1104 return cp->cmd(cmdtp, flag, argc, argv);
1105}
1106
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001107/**
1108 * do_efi_test_bootmgr() - run simple bootmgr for test
1109 *
1110 * @cmdtp: Command table
1111 * @flag: Command flag
1112 * @argc: Number of arguments
1113 * @argv: Argument array
1114 * Return: CMD_RET_SUCCESS on success,
1115 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1116 *
1117 * Implement efidebug "test bootmgr" sub-command.
1118 * Run simple bootmgr for test.
1119 *
1120 * efidebug test bootmgr
1121 */
Simon Glassed38aef2020-05-10 11:40:03 -06001122static int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001123 int argc, char * const argv[])
1124{
1125 efi_handle_t image;
1126 efi_uintn_t exit_data_size = 0;
1127 u16 *exit_data = NULL;
1128 efi_status_t ret;
Heinrich Schuchardte6ed8352020-08-11 18:20:50 +02001129 void *load_options = NULL;
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001130
Heinrich Schuchardta7647a72020-08-07 17:49:39 +02001131 ret = efi_bootmgr_load(&image, &load_options);
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001132 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1133
1134 /* We call efi_start_image() even if error for test purpose. */
1135 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1136 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1137 if (ret && exit_data)
1138 efi_free_pool(exit_data);
1139
1140 efi_restore_gd();
1141
Heinrich Schuchardta7647a72020-08-07 17:49:39 +02001142 free(load_options);
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001143 return CMD_RET_SUCCESS;
1144}
1145
Simon Glassed38aef2020-05-10 11:40:03 -06001146static struct cmd_tbl cmd_efidebug_test_sub[] = {
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001147 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1148 "", ""),
1149};
1150
1151/**
1152 * do_efi_test() - manage UEFI load options
1153 *
1154 * @cmdtp: Command table
1155 * @flag: Command flag
1156 * @argc: Number of arguments
1157 * @argv: Argument array
1158 * Return: CMD_RET_SUCCESS on success,
1159 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1160 *
1161 * Implement efidebug "test" sub-command.
1162 */
Simon Glassed38aef2020-05-10 11:40:03 -06001163static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001164 int argc, char * const argv[])
1165{
Simon Glassed38aef2020-05-10 11:40:03 -06001166 struct cmd_tbl *cp;
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001167
1168 if (argc < 2)
1169 return CMD_RET_USAGE;
1170
1171 argc--; argv++;
1172
1173 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1174 ARRAY_SIZE(cmd_efidebug_test_sub));
1175 if (!cp)
1176 return CMD_RET_USAGE;
1177
1178 return cp->cmd(cmdtp, flag, argc, argv);
1179}
1180
Ilias Apalodimasf5009402020-05-17 22:25:45 +03001181/**
1182 * do_efi_query_info() - QueryVariableInfo EFI service
1183 *
1184 * @cmdtp: Command table
1185 * @flag: Command flag
1186 * @argc: Number of arguments
1187 * @argv: Argument array
1188 * Return: CMD_RET_SUCCESS on success,
1189 * CMD_RET_USAGE or CMD_RET_FAILURE on failure
1190 *
1191 * Implement efidebug "test" sub-command.
1192 */
1193
Simon Glassed38aef2020-05-10 11:40:03 -06001194static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
Ilias Apalodimasf5009402020-05-17 22:25:45 +03001195 int argc, char * const argv[])
1196{
1197 efi_status_t ret;
1198 u32 attr = 0;
1199 u64 max_variable_storage_size;
1200 u64 remain_variable_storage_size;
1201 u64 max_variable_size;
1202 int i;
1203
1204 for (i = 1; i < argc; i++) {
1205 if (!strcmp(argv[i], "-bs"))
1206 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
1207 else if (!strcmp(argv[i], "-rt"))
1208 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
1209 else if (!strcmp(argv[i], "-nv"))
1210 attr |= EFI_VARIABLE_NON_VOLATILE;
1211 else if (!strcmp(argv[i], "-at"))
1212 attr |=
1213 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1214 }
1215
1216 ret = EFI_CALL(efi_query_variable_info(attr,
1217 &max_variable_storage_size,
1218 &remain_variable_storage_size,
1219 &max_variable_size));
1220 if (ret != EFI_SUCCESS) {
1221 printf("Error: Cannot query UEFI variables, r = %lu\n",
1222 ret & ~EFI_ERROR_MASK);
1223 return CMD_RET_FAILURE;
1224 }
1225
1226 printf("Max storage size %llu\n", max_variable_storage_size);
1227 printf("Remaining storage size %llu\n", remain_variable_storage_size);
1228 printf("Max variable size %llu\n", max_variable_size);
1229
1230 return CMD_RET_SUCCESS;
1231}
1232
Simon Glassed38aef2020-05-10 11:40:03 -06001233static struct cmd_tbl cmd_efidebug_sub[] = {
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001234 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
AKASHI Takahiroe32788a2019-02-25 15:54:39 +09001235 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1236 "", ""),
AKASHI Takahiro185716a2019-02-25 15:54:40 +09001237 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1238 "", ""),
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +09001239 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1240 "", ""),
AKASHI Takahiro71ab1bf2019-02-25 15:54:42 +09001241 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1242 "", ""),
AKASHI Takahiro32979d82019-02-25 15:54:43 +09001243 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1244 "", ""),
Heinrich Schuchardt75844e52020-01-07 05:57:47 +01001245 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1246 "", ""),
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001247 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1248 "", ""),
Ilias Apalodimasf5009402020-05-17 22:25:45 +03001249 U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
1250 "", ""),
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001251};
1252
1253/**
1254 * do_efidebug() - display and configure UEFI environment
1255 *
1256 * @cmdtp: Command table
1257 * @flag: Command flag
1258 * @argc: Number of arguments
1259 * @argv: Argument array
1260 * Return: CMD_RET_SUCCESS on success,
1261 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1262 *
1263 * Implement efidebug command which allows us to display and
1264 * configure UEFI environment.
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001265 */
Simon Glassed38aef2020-05-10 11:40:03 -06001266static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
1267 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001268{
Simon Glassed38aef2020-05-10 11:40:03 -06001269 struct cmd_tbl *cp;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001270 efi_status_t r;
1271
1272 if (argc < 2)
1273 return CMD_RET_USAGE;
1274
1275 argc--; argv++;
1276
1277 /* Initialize UEFI drivers */
1278 r = efi_init_obj_list();
1279 if (r != EFI_SUCCESS) {
1280 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1281 r & ~EFI_ERROR_MASK);
1282 return CMD_RET_FAILURE;
1283 }
1284
1285 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1286 ARRAY_SIZE(cmd_efidebug_sub));
1287 if (!cp)
1288 return CMD_RET_USAGE;
1289
1290 return cp->cmd(cmdtp, flag, argc, argv);
1291}
1292
1293#ifdef CONFIG_SYS_LONGHELP
1294static char efidebug_help_text[] =
1295 " - UEFI Shell-like interface to configure UEFI environment\n"
1296 "\n"
1297 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
1298 " - set UEFI BootXXXX variable\n"
1299 " <load options> will be passed to UEFI application\n"
1300 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1301 " - delete UEFI BootXXXX variables\n"
1302 "efidebug boot dump\n"
1303 " - dump all UEFI BootXXXX variables\n"
1304 "efidebug boot next <bootid>\n"
1305 " - set UEFI BootNext variable\n"
1306 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1307 " - set/show UEFI boot order\n"
AKASHI Takahiroe32788a2019-02-25 15:54:39 +09001308 "\n"
1309 "efidebug devices\n"
Heinrich Schuchardtc414d882020-01-07 07:48:15 +01001310 " - show UEFI devices\n"
AKASHI Takahiro185716a2019-02-25 15:54:40 +09001311 "efidebug drivers\n"
Heinrich Schuchardtc414d882020-01-07 07:48:15 +01001312 " - show UEFI drivers\n"
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +09001313 "efidebug dh\n"
Heinrich Schuchardtc414d882020-01-07 07:48:15 +01001314 " - show UEFI handles\n"
AKASHI Takahiro71ab1bf2019-02-25 15:54:42 +09001315 "efidebug images\n"
AKASHI Takahiro32979d82019-02-25 15:54:43 +09001316 " - show loaded images\n"
1317 "efidebug memmap\n"
Heinrich Schuchardtc414d882020-01-07 07:48:15 +01001318 " - show UEFI memory map\n"
Heinrich Schuchardt75844e52020-01-07 05:57:47 +01001319 "efidebug tables\n"
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001320 " - show UEFI configuration tables\n"
1321 "efidebug test bootmgr\n"
Ilias Apalodimasf5009402020-05-17 22:25:45 +03001322 " - run simple bootmgr for test\n"
1323 "efidebug query [-nv][-bs][-rt][-at]\n"
1324 " - show size of UEFI variables store\n";
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001325#endif
1326
1327U_BOOT_CMD(
1328 efidebug, 10, 0, do_efidebug,
1329 "Configure UEFI environment",
1330 efidebug_help_text
1331);