blob: 5288b9920b4d3c70e52ef764ca0174f60c44798e [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>
Heinrich Schuchardt79693352020-09-27 14:50:25 +020012#include <efi_rng.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090013#include <exports.h>
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +020014#include <hexdump.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090016#include <malloc.h>
Heinrich Schuchardta415d612020-03-14 08:44:07 +010017#include <mapmem.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090018#include <search.h>
19#include <linux/ctype.h>
20
AKASHI Takahiro355cdb52019-02-25 15:54:39 +090021#define BS systab.boottime
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090022
23/**
AKASHI Takahiro355cdb52019-02-25 15:54:39 +090024 * efi_get_device_handle_info() - get information of UEFI device
25 *
26 * @handle: Handle of UEFI device
27 * @dev_path_text: Pointer to text of device path
28 * Return: 0 on success, -1 on failure
29 *
30 * Currently return a formatted text of device path.
31 */
32static int efi_get_device_handle_info(efi_handle_t handle, u16 **dev_path_text)
33{
34 struct efi_device_path *dp;
35 efi_status_t ret;
36
37 ret = EFI_CALL(BS->open_protocol(handle, &efi_guid_device_path,
38 (void **)&dp, NULL /* FIXME */, NULL,
39 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
40 if (ret == EFI_SUCCESS) {
41 *dev_path_text = efi_dp_str(dp);
42 return 0;
43 } else {
44 return -1;
45 }
46}
47
48#define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
49
50static const char spc[] = " ";
51static const char sep[] = "================";
52
53/**
54 * do_efi_show_devices() - show UEFI devices
55 *
56 * @cmdtp: Command table
57 * @flag: Command flag
58 * @argc: Number of arguments
59 * @argv: Argument array
60 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
61 *
62 * Implement efidebug "devices" sub-command.
63 * Show all UEFI devices and their information.
64 */
Simon Glass09140112020-05-10 11:40:03 -060065static int do_efi_show_devices(struct cmd_tbl *cmdtp, int flag,
66 int argc, char *const argv[])
AKASHI Takahiro355cdb52019-02-25 15:54:39 +090067{
68 efi_handle_t *handles;
69 efi_uintn_t num, i;
70 u16 *dev_path_text;
71 efi_status_t ret;
72
Heinrich Schuchardta30c7232020-05-02 16:08:37 +020073 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
AKASHI Takahiro355cdb52019-02-25 15:54:39 +090074 &num, &handles));
75 if (ret != EFI_SUCCESS)
76 return CMD_RET_FAILURE;
77
78 if (!num)
79 return CMD_RET_SUCCESS;
80
81 printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc);
82 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
83 for (i = 0; i < num; i++) {
84 if (!efi_get_device_handle_info(handles[i], &dev_path_text)) {
85 printf("%p %ls\n", handles[i], dev_path_text);
86 efi_free_pool(dev_path_text);
87 }
88 }
89
Heinrich Schuchardta30c7232020-05-02 16:08:37 +020090 efi_free_pool(handles);
AKASHI Takahiro355cdb52019-02-25 15:54:39 +090091
92 return CMD_RET_SUCCESS;
93}
94
95/**
AKASHI Takahiro66eaf562019-02-25 15:54:40 +090096 * efi_get_driver_handle_info() - get information of UEFI driver
97 *
98 * @handle: Handle of UEFI device
99 * @driver_name: Driver name
100 * @image_path: Pointer to text of device path
101 * Return: 0 on success, -1 on failure
102 *
103 * Currently return no useful information as all UEFI drivers are
104 * built-in..
105 */
106static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
107 u16 **image_path)
108{
109 struct efi_handler *handler;
110 struct efi_loaded_image *image;
111 efi_status_t ret;
112
113 /*
114 * driver name
115 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
116 */
117 *driver_name = NULL;
118
119 /* image name */
120 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
121 if (ret != EFI_SUCCESS) {
122 *image_path = NULL;
123 return 0;
124 }
125
126 image = handler->protocol_interface;
127 *image_path = efi_dp_str(image->file_path);
128
129 return 0;
130}
131
132/**
133 * do_efi_show_drivers() - show UEFI drivers
134 *
135 * @cmdtp: Command table
136 * @flag: Command flag
137 * @argc: Number of arguments
138 * @argv: Argument array
139 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
140 *
141 * Implement efidebug "drivers" sub-command.
142 * Show all UEFI drivers and their information.
143 */
Simon Glass09140112020-05-10 11:40:03 -0600144static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
145 int argc, char *const argv[])
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900146{
147 efi_handle_t *handles;
148 efi_uintn_t num, i;
149 u16 *driver_name, *image_path_text;
150 efi_status_t ret;
151
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200152 ret = EFI_CALL(efi_locate_handle_buffer(
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900153 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
154 NULL, &num, &handles));
155 if (ret != EFI_SUCCESS)
156 return CMD_RET_FAILURE;
157
158 if (!num)
159 return CMD_RET_SUCCESS;
160
161 printf("Driver%.*s Name Image Path\n",
162 EFI_HANDLE_WIDTH - 6, spc);
163 printf("%.*s ==================== ====================\n",
164 EFI_HANDLE_WIDTH, sep);
165 for (i = 0; i < num; i++) {
166 if (!efi_get_driver_handle_info(handles[i], &driver_name,
167 &image_path_text)) {
168 if (image_path_text)
169 printf("%p %-20ls %ls\n", handles[i],
170 driver_name, image_path_text);
171 else
172 printf("%p %-20ls <built-in>\n",
173 handles[i], driver_name);
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200174 efi_free_pool(driver_name);
175 efi_free_pool(image_path_text);
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900176 }
177 }
178
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200179 efi_free_pool(handles);
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900180
181 return CMD_RET_SUCCESS;
182}
183
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900184static const struct {
185 const char *text;
186 const efi_guid_t guid;
187} guid_list[] = {
188 {
189 "Device Path",
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +0200190 EFI_DEVICE_PATH_PROTOCOL_GUID,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900191 },
192 {
193 "Device Path To Text",
194 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
195 },
196 {
197 "Device Path Utilities",
198 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
199 },
200 {
201 "Unicode Collation 2",
202 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
203 },
204 {
205 "Driver Binding",
206 EFI_DRIVER_BINDING_PROTOCOL_GUID,
207 },
208 {
209 "Simple Text Input",
210 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
211 },
212 {
213 "Simple Text Input Ex",
214 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
215 },
216 {
217 "Simple Text Output",
218 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
219 },
220 {
221 "Block IO",
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +0200222 EFI_BLOCK_IO_PROTOCOL_GUID,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900223 },
224 {
225 "Simple File System",
226 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
227 },
228 {
229 "Loaded Image",
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +0200230 EFI_LOADED_IMAGE_PROTOCOL_GUID,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900231 },
232 {
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +0200233 "Graphics Output",
234 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900235 },
Heinrich Schuchardt391bc8a2019-04-20 07:57:28 +0200236 {
237 "HII String",
238 EFI_HII_STRING_PROTOCOL_GUID,
239 },
240 {
241 "HII Database",
242 EFI_HII_DATABASE_PROTOCOL_GUID,
243 },
244 {
245 "HII Config Routing",
246 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
247 },
248 {
Ilias Apalodimasec80b472020-02-21 09:55:45 +0200249 "Load File2",
250 EFI_LOAD_FILE2_PROTOCOL_GUID,
251 },
252 {
Heinrich Schuchardt79693352020-09-27 14:50:25 +0200253 "Random Number Generator",
254 EFI_RNG_PROTOCOL_GUID,
255 },
256 {
Heinrich Schuchardt391bc8a2019-04-20 07:57:28 +0200257 "Simple Network",
258 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
259 },
260 {
261 "PXE Base Code",
262 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
263 },
Heinrich Schuchardt986e0642020-01-07 05:57:47 +0100264 /* Configuration table GUIDs */
265 {
266 "ACPI table",
267 EFI_ACPI_TABLE_GUID,
268 },
269 {
270 "device tree",
271 EFI_FDT_GUID,
272 },
273 {
274 "SMBIOS table",
275 SMBIOS_TABLE_GUID,
276 },
Heinrich Schuchardt76be6872020-02-19 20:48:49 +0100277 {
278 "Runtime properties",
279 EFI_RT_PROPERTIES_TABLE_GUID,
280 },
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900281};
282
283/**
Heinrich Schuchardt173cd9e2020-01-07 06:02:33 +0100284 * get_guid_text - get string of GUID
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900285 *
Heinrich Schuchardt173cd9e2020-01-07 06:02:33 +0100286 * Return description of GUID.
287 *
288 * @guid: GUID
289 * Return: description of GUID or NULL
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900290 */
Heinrich Schuchardt173cd9e2020-01-07 06:02:33 +0100291static const char *get_guid_text(const void *guid)
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900292{
293 int i;
294
Heinrich Schuchardt173cd9e2020-01-07 06:02:33 +0100295 for (i = 0; i < ARRAY_SIZE(guid_list); i++) {
296 /*
297 * As guidcmp uses memcmp() we can safely accept unaligned
298 * GUIDs.
299 */
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900300 if (!guidcmp(&guid_list[i].guid, guid))
Heinrich Schuchardt173cd9e2020-01-07 06:02:33 +0100301 return guid_list[i].text;
302 }
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900303
Heinrich Schuchardt173cd9e2020-01-07 06:02:33 +0100304 return NULL;
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900305}
306
307/**
308 * do_efi_show_handles() - show UEFI handles
309 *
310 * @cmdtp: Command table
311 * @flag: Command flag
312 * @argc: Number of arguments
313 * @argv: Argument array
314 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
315 *
316 * Implement efidebug "dh" sub-command.
317 * Show all UEFI handles and their information, currently all protocols
318 * added to handle.
319 */
Simon Glass09140112020-05-10 11:40:03 -0600320static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
321 int argc, char *const argv[])
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900322{
323 efi_handle_t *handles;
324 efi_guid_t **guid;
325 efi_uintn_t num, count, i, j;
326 const char *guid_text;
327 efi_status_t ret;
328
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200329 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900330 &num, &handles));
331 if (ret != EFI_SUCCESS)
332 return CMD_RET_FAILURE;
333
334 if (!num)
335 return CMD_RET_SUCCESS;
336
337 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
338 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
339 for (i = 0; i < num; i++) {
340 printf("%p", handles[i]);
341 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
342 &count));
343 if (ret || !count) {
344 putc('\n');
345 continue;
346 }
347
348 for (j = 0; j < count; j++) {
349 if (j)
350 printf(", ");
351 else
352 putc(' ');
353
354 guid_text = get_guid_text(guid[j]);
355 if (guid_text)
356 puts(guid_text);
357 else
358 printf("%pUl", guid[j]);
359 }
360 putc('\n');
361 }
362
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200363 efi_free_pool(handles);
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900364
365 return CMD_RET_SUCCESS;
366}
367
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900368/**
AKASHI Takahirofa536732019-02-25 15:54:42 +0900369 * do_efi_show_images() - show UEFI images
370 *
371 * @cmdtp: Command table
372 * @flag: Command flag
373 * @argc: Number of arguments
374 * @argv: Argument array
375 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
376 *
377 * Implement efidebug "images" sub-command.
378 * Show all UEFI loaded images and their information.
379 */
Simon Glass09140112020-05-10 11:40:03 -0600380static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
381 int argc, char *const argv[])
AKASHI Takahirofa536732019-02-25 15:54:42 +0900382{
383 efi_print_image_infos(NULL);
384
385 return CMD_RET_SUCCESS;
386}
387
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900388static const char * const efi_mem_type_string[] = {
389 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
390 [EFI_LOADER_CODE] = "LOADER CODE",
391 [EFI_LOADER_DATA] = "LOADER DATA",
392 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
393 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
394 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
395 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
396 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
397 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
398 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
399 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
400 [EFI_MMAP_IO] = "IO",
401 [EFI_MMAP_IO_PORT] = "IO PORT",
402 [EFI_PAL_CODE] = "PAL",
Heinrich Schuchardtdd9056c2020-04-29 20:21:39 +0200403 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900404};
405
406static const struct efi_mem_attrs {
407 const u64 bit;
408 const char *text;
409} efi_mem_attrs[] = {
410 {EFI_MEMORY_UC, "UC"},
411 {EFI_MEMORY_UC, "UC"},
412 {EFI_MEMORY_WC, "WC"},
413 {EFI_MEMORY_WT, "WT"},
414 {EFI_MEMORY_WB, "WB"},
415 {EFI_MEMORY_UCE, "UCE"},
416 {EFI_MEMORY_WP, "WP"},
417 {EFI_MEMORY_RP, "RP"},
418 {EFI_MEMORY_XP, "WP"},
419 {EFI_MEMORY_NV, "NV"},
420 {EFI_MEMORY_MORE_RELIABLE, "REL"},
421 {EFI_MEMORY_RO, "RO"},
Heinrich Schuchardt255a4732020-05-19 07:20:46 +0200422 {EFI_MEMORY_SP, "SP"},
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900423 {EFI_MEMORY_RUNTIME, "RT"},
424};
425
426/**
427 * print_memory_attributes() - print memory map attributes
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200428 *
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900429 * @attributes: Attribute value
430 *
431 * Print memory map attributes
432 */
433static void print_memory_attributes(u64 attributes)
434{
435 int sep, i;
436
437 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
438 if (attributes & efi_mem_attrs[i].bit) {
439 if (sep) {
440 putc('|');
441 } else {
442 putc(' ');
443 sep = 1;
444 }
445 puts(efi_mem_attrs[i].text);
446 }
447}
448
449#define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
450
451/**
452 * do_efi_show_memmap() - show UEFI memory map
453 *
454 * @cmdtp: Command table
455 * @flag: Command flag
456 * @argc: Number of arguments
457 * @argv: Argument array
458 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
459 *
460 * Implement efidebug "memmap" sub-command.
461 * Show UEFI memory map.
462 */
Simon Glass09140112020-05-10 11:40:03 -0600463static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
464 int argc, char *const argv[])
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900465{
466 struct efi_mem_desc *memmap = NULL, *map;
467 efi_uintn_t map_size = 0;
468 const char *type;
469 int i;
470 efi_status_t ret;
471
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 if (ret == EFI_BUFFER_TOO_SMALL) {
474 map_size += sizeof(struct efi_mem_desc); /* for my own */
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200475 ret = efi_allocate_pool(EFI_LOADER_DATA, map_size,
476 (void *)&memmap);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900477 if (ret != EFI_SUCCESS)
478 return CMD_RET_FAILURE;
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200479 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900480 }
481 if (ret != EFI_SUCCESS) {
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200482 efi_free_pool(memmap);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900483 return CMD_RET_FAILURE;
484 }
485
486 printf("Type Start%.*s End%.*s Attributes\n",
487 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
488 printf("================ %.*s %.*s ==========\n",
489 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
AKASHI Takahiroe2a5b862020-05-08 14:50:18 +0900490 /*
491 * Coverity check: dereferencing null pointer "map."
492 * This is a false positive as memmap will always be
493 * populated by allocate_pool() above.
494 */
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900495 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
Heinrich Schuchardtdd9056c2020-04-29 20:21:39 +0200496 if (map->type < ARRAY_SIZE(efi_mem_type_string))
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900497 type = efi_mem_type_string[map->type];
498 else
499 type = "(unknown)";
500
501 printf("%-16s %.*llx-%.*llx", type,
502 EFI_PHYS_ADDR_WIDTH,
Heinrich Schuchardt6c0ef352020-03-27 04:33:17 +0000503 (u64)map_to_sysmem((void *)(uintptr_t)
504 map->physical_start),
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900505 EFI_PHYS_ADDR_WIDTH,
Heinrich Schuchardt6c0ef352020-03-27 04:33:17 +0000506 (u64)map_to_sysmem((void *)(uintptr_t)
507 (map->physical_start +
508 map->num_pages * EFI_PAGE_SIZE)));
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900509
510 print_memory_attributes(map->attribute);
511 putc('\n');
512 }
513
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200514 efi_free_pool(memmap);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900515
516 return CMD_RET_SUCCESS;
517}
518
AKASHI Takahirofa536732019-02-25 15:54:42 +0900519/**
Heinrich Schuchardt986e0642020-01-07 05:57:47 +0100520 * do_efi_show_tables() - show UEFI configuration tables
521 *
522 * @cmdtp: Command table
523 * @flag: Command flag
524 * @argc: Number of arguments
525 * @argv: Argument array
526 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
527 *
528 * Implement efidebug "tables" sub-command.
529 * Show UEFI configuration tables.
530 */
Simon Glass09140112020-05-10 11:40:03 -0600531static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
532 int argc, char *const argv[])
Heinrich Schuchardt986e0642020-01-07 05:57:47 +0100533{
534 efi_uintn_t i;
535 const char *guid_str;
536
537 for (i = 0; i < systab.nr_tables; ++i) {
538 guid_str = get_guid_text(&systab.tables[i].guid);
539 if (!guid_str)
540 guid_str = "";
541 printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
542 }
543
544 return CMD_RET_SUCCESS;
545}
546
547/**
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900548 * do_efi_boot_add() - set UEFI load option
549 *
550 * @cmdtp: Command table
551 * @flag: Command flag
552 * @argc: Number of arguments
553 * @argv: Argument array
554 * Return: CMD_RET_SUCCESS on success,
555 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
556 *
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200557 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
558 *
559 * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900560 */
Simon Glass09140112020-05-10 11:40:03 -0600561static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
562 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900563{
564 int id;
565 char *endp;
566 char var_name[9];
567 u16 var_name16[9], *p;
568 efi_guid_t guid;
569 size_t label_len, label_len16;
570 u16 *label;
571 struct efi_device_path *device_path = NULL, *file_path = NULL;
572 struct efi_load_option lo;
573 void *data = NULL;
574 efi_uintn_t size;
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200575 efi_status_t ret;
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200576 int r = CMD_RET_SUCCESS;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900577
578 if (argc < 6 || argc > 7)
579 return CMD_RET_USAGE;
580
581 id = (int)simple_strtoul(argv[1], &endp, 16);
582 if (*endp != '\0' || id > 0xffff)
Heinrich Schuchardt1fa442e2019-02-28 20:41:58 +0100583 return CMD_RET_USAGE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900584
585 sprintf(var_name, "Boot%04X", id);
586 p = var_name16;
587 utf8_utf16_strncpy(&p, var_name, 9);
588
589 guid = efi_global_variable_guid;
590
591 /* attributes */
592 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
593
594 /* label */
595 label_len = strlen(argv[2]);
596 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
597 label = malloc((label_len16 + 1) * sizeof(u16));
598 if (!label)
599 return CMD_RET_FAILURE;
600 lo.label = label; /* label will be changed below */
601 utf8_utf16_strncpy(&label, argv[2], label_len);
602
603 /* file path */
604 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
605 &file_path);
606 if (ret != EFI_SUCCESS) {
607 printf("Cannot create device path for \"%s %s\"\n",
608 argv[3], argv[4]);
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200609 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900610 goto out;
611 }
612 lo.file_path = file_path;
613 lo.file_path_length = efi_dp_size(file_path)
614 + sizeof(struct efi_device_path); /* for END */
615
616 /* optional data */
AKASHI Takahirod67591d2020-05-08 14:50:47 +0900617 if (argc == 6)
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200618 lo.optional_data = NULL;
619 else
620 lo.optional_data = (const u8 *)argv[6];
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900621
622 size = efi_serialize_load_option(&lo, (u8 **)&data);
623 if (!size) {
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200624 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900625 goto out;
626 }
627
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200628 ret = EFI_CALL(efi_set_variable(var_name16, &guid,
AKASHI Takahirof658c2e2019-06-04 15:52:10 +0900629 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900630 EFI_VARIABLE_BOOTSERVICE_ACCESS |
631 EFI_VARIABLE_RUNTIME_ACCESS,
632 size, data));
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200633 if (ret != EFI_SUCCESS) {
634 printf("Cannot set %ls\n", var_name16);
635 r = CMD_RET_FAILURE;
636 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900637out:
638 free(data);
639 efi_free_pool(device_path);
640 efi_free_pool(file_path);
641 free(lo.label);
642
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200643 return r;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900644}
645
646/**
647 * do_efi_boot_rm() - delete UEFI load options
648 *
649 * @cmdtp: Command table
650 * @flag: Command flag
651 * @argc: Number of arguments
652 * @argv: Argument array
653 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
654 *
655 * Implement efidebug "boot rm" sub-command.
656 * Delete UEFI load options.
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200657 *
658 * efidebug boot rm <id> ...
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900659 */
Simon Glass09140112020-05-10 11:40:03 -0600660static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag,
661 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900662{
663 efi_guid_t guid;
664 int id, i;
665 char *endp;
666 char var_name[9];
AKASHI Takahiroe8bced62020-02-28 09:05:04 +0900667 u16 var_name16[9], *p;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900668 efi_status_t ret;
669
670 if (argc == 1)
671 return CMD_RET_USAGE;
672
673 guid = efi_global_variable_guid;
674 for (i = 1; i < argc; i++, argv++) {
675 id = (int)simple_strtoul(argv[1], &endp, 16);
676 if (*endp != '\0' || id > 0xffff)
677 return CMD_RET_FAILURE;
678
679 sprintf(var_name, "Boot%04X", id);
AKASHI Takahiroe8bced62020-02-28 09:05:04 +0900680 p = var_name16;
681 utf8_utf16_strncpy(&p, var_name, 9);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900682
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200683 ret = EFI_CALL(efi_set_variable(var_name16, &guid, 0, 0, NULL));
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900684 if (ret) {
Heinrich Schuchardt30efb5d2020-03-02 20:13:10 +0100685 printf("Cannot remove %ls\n", var_name16);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900686 return CMD_RET_FAILURE;
687 }
688 }
689
690 return CMD_RET_SUCCESS;
691}
692
693/**
694 * show_efi_boot_opt_data() - dump UEFI load option
695 *
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200696 * @varname16: variable name
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200697 * @data: value of UEFI load option variable
698 * @size: size of the boot option
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900699 *
700 * Decode the value of UEFI load option variable and print information.
701 */
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +0200702static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900703{
704 struct efi_load_option lo;
705 char *label, *p;
706 size_t label_len16, label_len;
707 u16 *dp_str;
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +0200708 efi_status_t ret;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900709
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +0200710 ret = efi_deserialize_load_option(&lo, data, size);
711 if (ret != EFI_SUCCESS) {
712 printf("%ls: invalid load option\n", varname16);
713 return;
714 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900715
716 label_len16 = u16_strlen(lo.label);
717 label_len = utf16_utf8_strnlen(lo.label, label_len16);
718 label = malloc(label_len + 1);
719 if (!label)
720 return;
721 p = label;
722 utf16_utf8_strncpy(&p, lo.label, label_len16);
723
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200724 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
725 varname16,
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900726 /* ACTIVE */
727 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
728 /* FORCE RECONNECT */
729 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
730 /* HIDDEN */
731 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
732 lo.attributes);
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200733 printf(" label: %s\n", label);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900734
735 dp_str = efi_dp_str(lo.file_path);
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200736 printf(" file_path: %ls\n", dp_str);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900737 efi_free_pool(dp_str);
738
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200739 printf(" data:\n");
740 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +0200741 lo.optional_data, *size, true);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900742 free(label);
743}
744
745/**
746 * show_efi_boot_opt() - dump UEFI load option
747 *
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200748 * @varname16: variable name
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900749 *
750 * Dump information defined by UEFI load option.
751 */
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200752static void show_efi_boot_opt(u16 *varname16)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900753{
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200754 void *data;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900755 efi_uintn_t size;
AKASHI Takahiro0bffb8c2019-11-26 10:11:22 +0900756 efi_status_t ret;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900757
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900758 size = 0;
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200759 ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
760 NULL, &size, NULL));
AKASHI Takahiro0bffb8c2019-11-26 10:11:22 +0900761 if (ret == EFI_BUFFER_TOO_SMALL) {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900762 data = malloc(size);
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200763 if (!data) {
764 printf("ERROR: Out of memory\n");
765 return;
766 }
767 ret = EFI_CALL(efi_get_variable(varname16,
768 &efi_global_variable_guid,
769 NULL, &size, data));
770 if (ret == EFI_SUCCESS)
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +0200771 show_efi_boot_opt_data(varname16, data, &size);
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200772 free(data);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900773 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900774}
775
AKASHI Takahiroffe21572019-04-26 09:44:18 +0900776static int u16_tohex(u16 c)
777{
778 if (c >= '0' && c <= '9')
779 return c - '0';
780 if (c >= 'A' && c <= 'F')
781 return c - 'A' + 10;
782
783 /* not hexadecimal */
784 return -1;
785}
786
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900787/**
788 * show_efi_boot_dump() - dump all UEFI load options
789 *
790 * @cmdtp: Command table
791 * @flag: Command flag
792 * @argc: Number of arguments
793 * @argv: Argument array
794 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
795 *
796 * Implement efidebug "boot dump" sub-command.
797 * Dump information of all UEFI load options defined.
Heinrich Schuchardta6ccba02019-07-25 20:52:23 +0200798 *
799 * efidebug boot dump
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900800 */
Simon Glass09140112020-05-10 11:40:03 -0600801static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
802 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900803{
AKASHI Takahiroffe21572019-04-26 09:44:18 +0900804 u16 *var_name16, *p;
805 efi_uintn_t buf_size, size;
806 efi_guid_t guid;
807 int id, i, digit;
808 efi_status_t ret;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900809
810 if (argc > 1)
811 return CMD_RET_USAGE;
812
AKASHI Takahiroffe21572019-04-26 09:44:18 +0900813 buf_size = 128;
814 var_name16 = malloc(buf_size);
815 if (!var_name16)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900816 return CMD_RET_FAILURE;
817
AKASHI Takahiroffe21572019-04-26 09:44:18 +0900818 var_name16[0] = 0;
819 for (;;) {
820 size = buf_size;
821 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
822 &guid));
823 if (ret == EFI_NOT_FOUND)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900824 break;
AKASHI Takahiroffe21572019-04-26 09:44:18 +0900825 if (ret == EFI_BUFFER_TOO_SMALL) {
826 buf_size = size;
827 p = realloc(var_name16, buf_size);
828 if (!p) {
829 free(var_name16);
830 return CMD_RET_FAILURE;
831 }
832 var_name16 = p;
833 ret = EFI_CALL(efi_get_next_variable_name(&size,
834 var_name16,
835 &guid));
836 }
837 if (ret != EFI_SUCCESS) {
838 free(var_name16);
839 return CMD_RET_FAILURE;
840 }
841
842 if (memcmp(var_name16, L"Boot", 8))
843 continue;
844
845 for (id = 0, i = 0; i < 4; i++) {
846 digit = u16_tohex(var_name16[4 + i]);
847 if (digit < 0)
848 break;
849 id = (id << 4) + digit;
850 }
851 if (i == 4 && !var_name16[8])
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200852 show_efi_boot_opt(var_name16);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900853 }
AKASHI Takahiroffe21572019-04-26 09:44:18 +0900854
855 free(var_name16);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900856
857 return CMD_RET_SUCCESS;
858}
859
860/**
861 * show_efi_boot_order() - show order of UEFI load options
862 *
863 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
864 *
865 * Show order of UEFI load options defined by BootOrder variable.
866 */
867static int show_efi_boot_order(void)
868{
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +0200869 u16 *bootorder;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900870 efi_uintn_t size;
871 int num, i;
872 char var_name[9];
873 u16 var_name16[9], *p16;
874 void *data;
875 struct efi_load_option lo;
876 char *label, *p;
877 size_t label_len16, label_len;
878 efi_status_t ret;
879
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900880 size = 0;
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200881 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +0200882 NULL, &size, NULL));
883 if (ret != EFI_BUFFER_TOO_SMALL) {
884 if (ret == EFI_NOT_FOUND) {
885 printf("BootOrder not defined\n");
886 return CMD_RET_SUCCESS;
887 } else {
888 return CMD_RET_FAILURE;
889 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900890 }
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +0200891 bootorder = malloc(size);
892 if (!bootorder) {
893 printf("ERROR: Out of memory\n");
894 return CMD_RET_FAILURE;
895 }
896 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
897 NULL, &size, bootorder));
898 if (ret != EFI_SUCCESS) {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900899 ret = CMD_RET_FAILURE;
900 goto out;
901 }
902
903 num = size / sizeof(u16);
904 for (i = 0; i < num; i++) {
905 sprintf(var_name, "Boot%04X", bootorder[i]);
906 p16 = var_name16;
907 utf8_utf16_strncpy(&p16, var_name, 9);
908
909 size = 0;
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +0200910 ret = EFI_CALL(efi_get_variable(var_name16,
911 &efi_global_variable_guid, NULL,
912 &size, NULL));
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900913 if (ret != EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +0200914 printf("%2d: %s: (not defined)\n", i + 1, var_name);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900915 continue;
916 }
917
918 data = malloc(size);
919 if (!data) {
920 ret = CMD_RET_FAILURE;
921 goto out;
922 }
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +0200923 ret = EFI_CALL(efi_get_variable(var_name16,
924 &efi_global_variable_guid, NULL,
925 &size, data));
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900926 if (ret != EFI_SUCCESS) {
927 free(data);
928 ret = CMD_RET_FAILURE;
929 goto out;
930 }
931
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +0200932 ret = efi_deserialize_load_option(&lo, data, &size);
933 if (ret != EFI_SUCCESS) {
934 printf("%ls: invalid load option\n", var_name16);
935 ret = CMD_RET_FAILURE;
936 goto out;
937 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900938
939 label_len16 = u16_strlen(lo.label);
940 label_len = utf16_utf8_strnlen(lo.label, label_len16);
941 label = malloc(label_len + 1);
942 if (!label) {
943 free(data);
944 ret = CMD_RET_FAILURE;
945 goto out;
946 }
947 p = label;
948 utf16_utf8_strncpy(&p, lo.label, label_len16);
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +0200949 printf("%2d: %s: %s\n", i + 1, var_name, label);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900950 free(label);
951
952 free(data);
953 }
954out:
955 free(bootorder);
956
957 return ret;
958}
959
960/**
961 * do_efi_boot_next() - manage UEFI BootNext variable
962 *
963 * @cmdtp: Command table
964 * @flag: Command flag
965 * @argc: Number of arguments
966 * @argv: Argument array
967 * Return: CMD_RET_SUCCESS on success,
968 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
969 *
970 * Implement efidebug "boot next" sub-command.
971 * Set BootNext variable.
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200972 *
973 * efidebug boot next <id>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900974 */
Simon Glass09140112020-05-10 11:40:03 -0600975static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
976 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900977{
978 u16 bootnext;
979 efi_uintn_t size;
980 char *endp;
981 efi_guid_t guid;
982 efi_status_t ret;
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200983 int r = CMD_RET_SUCCESS;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900984
985 if (argc != 2)
986 return CMD_RET_USAGE;
987
988 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
Heinrich Schuchardtbdb15772020-05-09 17:59:19 +0200989 if (*endp) {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900990 printf("invalid value: %s\n", argv[1]);
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200991 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900992 goto out;
993 }
994
995 guid = efi_global_variable_guid;
996 size = sizeof(u16);
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200997 ret = EFI_CALL(efi_set_variable(L"BootNext", &guid,
AKASHI Takahirof658c2e2019-06-04 15:52:10 +0900998 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900999 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1000 EFI_VARIABLE_RUNTIME_ACCESS,
1001 size, &bootnext));
Heinrich Schuchardta332f252019-06-20 12:59:45 +02001002 if (ret != EFI_SUCCESS) {
1003 printf("Cannot set BootNext\n");
1004 r = CMD_RET_FAILURE;
1005 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001006out:
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001007 return r;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001008}
1009
1010/**
1011 * do_efi_boot_order() - manage UEFI BootOrder variable
1012 *
1013 * @cmdtp: Command table
1014 * @flag: Command flag
1015 * @argc: Number of arguments
1016 * @argv: Argument array
1017 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1018 *
1019 * Implement efidebug "boot order" sub-command.
1020 * Show order of UEFI load options, or change it in BootOrder variable.
Heinrich Schuchardt0b016562019-07-14 14:00:41 +02001021 *
1022 * efidebug boot order [<id> ...]
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001023 */
Simon Glass09140112020-05-10 11:40:03 -06001024static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
1025 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001026{
1027 u16 *bootorder = NULL;
1028 efi_uintn_t size;
1029 int id, i;
1030 char *endp;
1031 efi_guid_t guid;
1032 efi_status_t ret;
Heinrich Schuchardta332f252019-06-20 12:59:45 +02001033 int r = CMD_RET_SUCCESS;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001034
1035 if (argc == 1)
1036 return show_efi_boot_order();
1037
1038 argc--;
1039 argv++;
1040
1041 size = argc * sizeof(u16);
1042 bootorder = malloc(size);
1043 if (!bootorder)
1044 return CMD_RET_FAILURE;
1045
1046 for (i = 0; i < argc; i++) {
1047 id = (int)simple_strtoul(argv[i], &endp, 16);
1048 if (*endp != '\0' || id > 0xffff) {
1049 printf("invalid value: %s\n", argv[i]);
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001050 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001051 goto out;
1052 }
1053
1054 bootorder[i] = (u16)id;
1055 }
1056
1057 guid = efi_global_variable_guid;
Heinrich Schuchardta30c7232020-05-02 16:08:37 +02001058 ret = EFI_CALL(efi_set_variable(L"BootOrder", &guid,
AKASHI Takahirof658c2e2019-06-04 15:52:10 +09001059 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001060 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1061 EFI_VARIABLE_RUNTIME_ACCESS,
1062 size, bootorder));
Heinrich Schuchardta332f252019-06-20 12:59:45 +02001063 if (ret != EFI_SUCCESS) {
1064 printf("Cannot set BootOrder\n");
1065 r = CMD_RET_FAILURE;
1066 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001067out:
1068 free(bootorder);
1069
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001070 return r;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001071}
1072
Simon Glass09140112020-05-10 11:40:03 -06001073static struct cmd_tbl cmd_efidebug_boot_sub[] = {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001074 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1075 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1076 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1077 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1078 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1079 "", ""),
1080};
1081
1082/**
1083 * do_efi_boot_opt() - manage UEFI load options
1084 *
1085 * @cmdtp: Command table
1086 * @flag: Command flag
1087 * @argc: Number of arguments
1088 * @argv: Argument array
1089 * Return: CMD_RET_SUCCESS on success,
1090 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1091 *
1092 * Implement efidebug "boot" sub-command.
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001093 */
Simon Glass09140112020-05-10 11:40:03 -06001094static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
1095 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001096{
Simon Glass09140112020-05-10 11:40:03 -06001097 struct cmd_tbl *cp;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001098
1099 if (argc < 2)
1100 return CMD_RET_USAGE;
1101
1102 argc--; argv++;
1103
1104 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1105 ARRAY_SIZE(cmd_efidebug_boot_sub));
1106 if (!cp)
1107 return CMD_RET_USAGE;
1108
1109 return cp->cmd(cmdtp, flag, argc, argv);
1110}
1111
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001112/**
1113 * do_efi_test_bootmgr() - run simple bootmgr for test
1114 *
1115 * @cmdtp: Command table
1116 * @flag: Command flag
1117 * @argc: Number of arguments
1118 * @argv: Argument array
1119 * Return: CMD_RET_SUCCESS on success,
1120 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1121 *
1122 * Implement efidebug "test bootmgr" sub-command.
1123 * Run simple bootmgr for test.
1124 *
1125 * efidebug test bootmgr
1126 */
Simon Glass09140112020-05-10 11:40:03 -06001127static int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001128 int argc, char * const argv[])
1129{
1130 efi_handle_t image;
1131 efi_uintn_t exit_data_size = 0;
1132 u16 *exit_data = NULL;
1133 efi_status_t ret;
Heinrich Schuchardtbc78d222020-08-11 18:20:50 +02001134 void *load_options = NULL;
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001135
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +02001136 ret = efi_bootmgr_load(&image, &load_options);
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001137 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1138
1139 /* We call efi_start_image() even if error for test purpose. */
1140 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1141 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1142 if (ret && exit_data)
1143 efi_free_pool(exit_data);
1144
1145 efi_restore_gd();
1146
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +02001147 free(load_options);
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001148 return CMD_RET_SUCCESS;
1149}
1150
Simon Glass09140112020-05-10 11:40:03 -06001151static struct cmd_tbl cmd_efidebug_test_sub[] = {
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001152 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1153 "", ""),
1154};
1155
1156/**
1157 * do_efi_test() - manage UEFI load options
1158 *
1159 * @cmdtp: Command table
1160 * @flag: Command flag
1161 * @argc: Number of arguments
1162 * @argv: Argument array
1163 * Return: CMD_RET_SUCCESS on success,
1164 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1165 *
1166 * Implement efidebug "test" sub-command.
1167 */
Simon Glass09140112020-05-10 11:40:03 -06001168static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001169 int argc, char * const argv[])
1170{
Simon Glass09140112020-05-10 11:40:03 -06001171 struct cmd_tbl *cp;
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001172
1173 if (argc < 2)
1174 return CMD_RET_USAGE;
1175
1176 argc--; argv++;
1177
1178 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1179 ARRAY_SIZE(cmd_efidebug_test_sub));
1180 if (!cp)
1181 return CMD_RET_USAGE;
1182
1183 return cp->cmd(cmdtp, flag, argc, argv);
1184}
1185
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001186/**
1187 * do_efi_query_info() - QueryVariableInfo EFI service
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_FAILURE on failure
1195 *
1196 * Implement efidebug "test" sub-command.
1197 */
1198
Simon Glass09140112020-05-10 11:40:03 -06001199static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001200 int argc, char * const argv[])
1201{
1202 efi_status_t ret;
1203 u32 attr = 0;
1204 u64 max_variable_storage_size;
1205 u64 remain_variable_storage_size;
1206 u64 max_variable_size;
1207 int i;
1208
1209 for (i = 1; i < argc; i++) {
1210 if (!strcmp(argv[i], "-bs"))
1211 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
1212 else if (!strcmp(argv[i], "-rt"))
1213 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
1214 else if (!strcmp(argv[i], "-nv"))
1215 attr |= EFI_VARIABLE_NON_VOLATILE;
1216 else if (!strcmp(argv[i], "-at"))
1217 attr |=
1218 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1219 }
1220
1221 ret = EFI_CALL(efi_query_variable_info(attr,
1222 &max_variable_storage_size,
1223 &remain_variable_storage_size,
1224 &max_variable_size));
1225 if (ret != EFI_SUCCESS) {
1226 printf("Error: Cannot query UEFI variables, r = %lu\n",
1227 ret & ~EFI_ERROR_MASK);
1228 return CMD_RET_FAILURE;
1229 }
1230
1231 printf("Max storage size %llu\n", max_variable_storage_size);
1232 printf("Remaining storage size %llu\n", remain_variable_storage_size);
1233 printf("Max variable size %llu\n", max_variable_size);
1234
1235 return CMD_RET_SUCCESS;
1236}
1237
Simon Glass09140112020-05-10 11:40:03 -06001238static struct cmd_tbl cmd_efidebug_sub[] = {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001239 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
AKASHI Takahiro355cdb52019-02-25 15:54:39 +09001240 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1241 "", ""),
AKASHI Takahiro66eaf562019-02-25 15:54:40 +09001242 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1243 "", ""),
AKASHI Takahiroa8014622019-02-25 15:54:41 +09001244 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1245 "", ""),
AKASHI Takahirofa536732019-02-25 15:54:42 +09001246 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1247 "", ""),
AKASHI Takahiro00358bb2019-02-25 15:54:43 +09001248 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1249 "", ""),
Heinrich Schuchardt986e0642020-01-07 05:57:47 +01001250 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1251 "", ""),
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001252 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1253 "", ""),
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001254 U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
1255 "", ""),
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001256};
1257
1258/**
1259 * do_efidebug() - display and configure UEFI environment
1260 *
1261 * @cmdtp: Command table
1262 * @flag: Command flag
1263 * @argc: Number of arguments
1264 * @argv: Argument array
1265 * Return: CMD_RET_SUCCESS on success,
1266 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1267 *
1268 * Implement efidebug command which allows us to display and
1269 * configure UEFI environment.
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001270 */
Simon Glass09140112020-05-10 11:40:03 -06001271static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
1272 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001273{
Simon Glass09140112020-05-10 11:40:03 -06001274 struct cmd_tbl *cp;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001275 efi_status_t r;
1276
1277 if (argc < 2)
1278 return CMD_RET_USAGE;
1279
1280 argc--; argv++;
1281
1282 /* Initialize UEFI drivers */
1283 r = efi_init_obj_list();
1284 if (r != EFI_SUCCESS) {
1285 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1286 r & ~EFI_ERROR_MASK);
1287 return CMD_RET_FAILURE;
1288 }
1289
1290 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1291 ARRAY_SIZE(cmd_efidebug_sub));
1292 if (!cp)
1293 return CMD_RET_USAGE;
1294
1295 return cp->cmd(cmdtp, flag, argc, argv);
1296}
1297
1298#ifdef CONFIG_SYS_LONGHELP
1299static char efidebug_help_text[] =
1300 " - UEFI Shell-like interface to configure UEFI environment\n"
1301 "\n"
1302 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
1303 " - set UEFI BootXXXX variable\n"
1304 " <load options> will be passed to UEFI application\n"
1305 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1306 " - delete UEFI BootXXXX variables\n"
1307 "efidebug boot dump\n"
1308 " - dump all UEFI BootXXXX variables\n"
1309 "efidebug boot next <bootid>\n"
1310 " - set UEFI BootNext variable\n"
1311 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1312 " - set/show UEFI boot order\n"
AKASHI Takahiro355cdb52019-02-25 15:54:39 +09001313 "\n"
1314 "efidebug devices\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001315 " - show UEFI devices\n"
AKASHI Takahiro66eaf562019-02-25 15:54:40 +09001316 "efidebug drivers\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001317 " - show UEFI drivers\n"
AKASHI Takahiroa8014622019-02-25 15:54:41 +09001318 "efidebug dh\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001319 " - show UEFI handles\n"
AKASHI Takahirofa536732019-02-25 15:54:42 +09001320 "efidebug images\n"
AKASHI Takahiro00358bb2019-02-25 15:54:43 +09001321 " - show loaded images\n"
1322 "efidebug memmap\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001323 " - show UEFI memory map\n"
Heinrich Schuchardt986e0642020-01-07 05:57:47 +01001324 "efidebug tables\n"
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001325 " - show UEFI configuration tables\n"
1326 "efidebug test bootmgr\n"
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001327 " - run simple bootmgr for test\n"
1328 "efidebug query [-nv][-bs][-rt][-at]\n"
1329 " - show size of UEFI variables store\n";
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001330#endif
1331
1332U_BOOT_CMD(
1333 efidebug, 10, 0, do_efidebug,
1334 "Configure UEFI environment",
1335 efidebug_help_text
1336);