blob: 02dc491a68b6df91956ffed820adfef17ac63add [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>
12#include <environment.h>
13#include <exports.h>
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +020014#include <hexdump.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090015#include <malloc.h>
16#include <search.h>
17#include <linux/ctype.h>
18
AKASHI Takahiro355cdb52019-02-25 15:54:39 +090019#define BS systab.boottime
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090020#define RT systab.runtime
21
22/**
AKASHI Takahiro355cdb52019-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 */
64static int do_efi_show_devices(cmd_tbl_t *cmdtp, int flag,
65 int argc, char * const argv[])
66{
67 efi_handle_t *handles;
68 efi_uintn_t num, i;
69 u16 *dev_path_text;
70 efi_status_t ret;
71
72 ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL,
73 &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
89 EFI_CALL(BS->free_pool(handles));
90
91 return CMD_RET_SUCCESS;
92}
93
94/**
AKASHI Takahiro66eaf562019-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 */
143static int do_efi_show_drivers(cmd_tbl_t *cmdtp, int flag,
144 int argc, char * const argv[])
145{
146 efi_handle_t *handles;
147 efi_uintn_t num, i;
148 u16 *driver_name, *image_path_text;
149 efi_status_t ret;
150
151 ret = EFI_CALL(BS->locate_handle_buffer(
152 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);
173 EFI_CALL(BS->free_pool(driver_name));
174 EFI_CALL(BS->free_pool(image_path_text));
175 }
176 }
177
178 EFI_CALL(BS->free_pool(handles));
179
180 return CMD_RET_SUCCESS;
181}
182
AKASHI Takahiroa8014622019-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 Schuchardtdec88e42019-04-20 07:39:11 +0200189 EFI_DEVICE_PATH_PROTOCOL_GUID,
AKASHI Takahiroa8014622019-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 Schuchardtdec88e42019-04-20 07:39:11 +0200221 EFI_BLOCK_IO_PROTOCOL_GUID,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900222 },
223 {
224 "Simple File System",
225 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
226 },
227 {
228 "Loaded Image",
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +0200229 EFI_LOADED_IMAGE_PROTOCOL_GUID,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900230 },
231 {
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +0200232 "Graphics Output",
233 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900234 },
Heinrich Schuchardt391bc8a2019-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 {
248 "Simple Network",
249 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
250 },
251 {
252 "PXE Base Code",
253 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
254 },
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900255};
256
257/**
258 * get_guid_text - get string of protocol guid
259 * @guid: Protocol guid
260 * Return: String
261 *
262 * Return string for display to represent the protocol.
263 */
264static const char *get_guid_text(const efi_guid_t *guid)
265{
266 int i;
267
268 for (i = 0; i < ARRAY_SIZE(guid_list); i++)
269 if (!guidcmp(&guid_list[i].guid, guid))
270 break;
271
272 if (i != ARRAY_SIZE(guid_list))
273 return guid_list[i].text;
274 else
275 return NULL;
276}
277
278/**
279 * do_efi_show_handles() - show UEFI handles
280 *
281 * @cmdtp: Command table
282 * @flag: Command flag
283 * @argc: Number of arguments
284 * @argv: Argument array
285 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
286 *
287 * Implement efidebug "dh" sub-command.
288 * Show all UEFI handles and their information, currently all protocols
289 * added to handle.
290 */
291static int do_efi_show_handles(cmd_tbl_t *cmdtp, int flag,
292 int argc, char * const argv[])
293{
294 efi_handle_t *handles;
295 efi_guid_t **guid;
296 efi_uintn_t num, count, i, j;
297 const char *guid_text;
298 efi_status_t ret;
299
300 ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL,
301 &num, &handles));
302 if (ret != EFI_SUCCESS)
303 return CMD_RET_FAILURE;
304
305 if (!num)
306 return CMD_RET_SUCCESS;
307
308 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
309 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
310 for (i = 0; i < num; i++) {
311 printf("%p", handles[i]);
312 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
313 &count));
314 if (ret || !count) {
315 putc('\n');
316 continue;
317 }
318
319 for (j = 0; j < count; j++) {
320 if (j)
321 printf(", ");
322 else
323 putc(' ');
324
325 guid_text = get_guid_text(guid[j]);
326 if (guid_text)
327 puts(guid_text);
328 else
329 printf("%pUl", guid[j]);
330 }
331 putc('\n');
332 }
333
334 EFI_CALL(BS->free_pool(handles));
335
336 return CMD_RET_SUCCESS;
337}
338
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900339/**
AKASHI Takahirofa536732019-02-25 15:54:42 +0900340 * do_efi_show_images() - show UEFI images
341 *
342 * @cmdtp: Command table
343 * @flag: Command flag
344 * @argc: Number of arguments
345 * @argv: Argument array
346 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
347 *
348 * Implement efidebug "images" sub-command.
349 * Show all UEFI loaded images and their information.
350 */
351static int do_efi_show_images(cmd_tbl_t *cmdtp, int flag,
352 int argc, char * const argv[])
353{
354 efi_print_image_infos(NULL);
355
356 return CMD_RET_SUCCESS;
357}
358
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900359static const char * const efi_mem_type_string[] = {
360 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
361 [EFI_LOADER_CODE] = "LOADER CODE",
362 [EFI_LOADER_DATA] = "LOADER DATA",
363 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
364 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
365 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
366 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
367 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
368 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
369 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
370 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
371 [EFI_MMAP_IO] = "IO",
372 [EFI_MMAP_IO_PORT] = "IO PORT",
373 [EFI_PAL_CODE] = "PAL",
374};
375
376static const struct efi_mem_attrs {
377 const u64 bit;
378 const char *text;
379} efi_mem_attrs[] = {
380 {EFI_MEMORY_UC, "UC"},
381 {EFI_MEMORY_UC, "UC"},
382 {EFI_MEMORY_WC, "WC"},
383 {EFI_MEMORY_WT, "WT"},
384 {EFI_MEMORY_WB, "WB"},
385 {EFI_MEMORY_UCE, "UCE"},
386 {EFI_MEMORY_WP, "WP"},
387 {EFI_MEMORY_RP, "RP"},
388 {EFI_MEMORY_XP, "WP"},
389 {EFI_MEMORY_NV, "NV"},
390 {EFI_MEMORY_MORE_RELIABLE, "REL"},
391 {EFI_MEMORY_RO, "RO"},
392 {EFI_MEMORY_RUNTIME, "RT"},
393};
394
395/**
396 * print_memory_attributes() - print memory map attributes
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200397 *
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900398 * @attributes: Attribute value
399 *
400 * Print memory map attributes
401 */
402static void print_memory_attributes(u64 attributes)
403{
404 int sep, i;
405
406 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
407 if (attributes & efi_mem_attrs[i].bit) {
408 if (sep) {
409 putc('|');
410 } else {
411 putc(' ');
412 sep = 1;
413 }
414 puts(efi_mem_attrs[i].text);
415 }
416}
417
418#define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
419
420/**
421 * do_efi_show_memmap() - show UEFI memory map
422 *
423 * @cmdtp: Command table
424 * @flag: Command flag
425 * @argc: Number of arguments
426 * @argv: Argument array
427 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
428 *
429 * Implement efidebug "memmap" sub-command.
430 * Show UEFI memory map.
431 */
432static int do_efi_show_memmap(cmd_tbl_t *cmdtp, int flag,
433 int argc, char * const argv[])
434{
435 struct efi_mem_desc *memmap = NULL, *map;
436 efi_uintn_t map_size = 0;
437 const char *type;
438 int i;
439 efi_status_t ret;
440
441 ret = EFI_CALL(BS->get_memory_map(&map_size, memmap, NULL, NULL, NULL));
442 if (ret == EFI_BUFFER_TOO_SMALL) {
443 map_size += sizeof(struct efi_mem_desc); /* for my own */
444 ret = EFI_CALL(BS->allocate_pool(EFI_LOADER_DATA,
445 map_size, (void *)&memmap));
446 if (ret != EFI_SUCCESS)
447 return CMD_RET_FAILURE;
448 ret = EFI_CALL(BS->get_memory_map(&map_size, memmap,
449 NULL, NULL, NULL));
450 }
451 if (ret != EFI_SUCCESS) {
452 EFI_CALL(BS->free_pool(memmap));
453 return CMD_RET_FAILURE;
454 }
455
456 printf("Type Start%.*s End%.*s Attributes\n",
457 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
458 printf("================ %.*s %.*s ==========\n",
459 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
460 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
461 if (map->type < EFI_MAX_MEMORY_TYPE)
462 type = efi_mem_type_string[map->type];
463 else
464 type = "(unknown)";
465
466 printf("%-16s %.*llx-%.*llx", type,
467 EFI_PHYS_ADDR_WIDTH,
468 map->physical_start,
469 EFI_PHYS_ADDR_WIDTH,
470 map->physical_start + map->num_pages * EFI_PAGE_SIZE);
471
472 print_memory_attributes(map->attribute);
473 putc('\n');
474 }
475
476 EFI_CALL(BS->free_pool(memmap));
477
478 return CMD_RET_SUCCESS;
479}
480
AKASHI Takahirofa536732019-02-25 15:54:42 +0900481/**
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900482 * do_efi_boot_add() - set UEFI load option
483 *
484 * @cmdtp: Command table
485 * @flag: Command flag
486 * @argc: Number of arguments
487 * @argv: Argument array
488 * Return: CMD_RET_SUCCESS on success,
489 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
490 *
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200491 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
492 *
493 * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900494 */
495static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
496 int argc, char * const argv[])
497{
498 int id;
499 char *endp;
500 char var_name[9];
501 u16 var_name16[9], *p;
502 efi_guid_t guid;
503 size_t label_len, label_len16;
504 u16 *label;
505 struct efi_device_path *device_path = NULL, *file_path = NULL;
506 struct efi_load_option lo;
507 void *data = NULL;
508 efi_uintn_t size;
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200509 efi_status_t ret;
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200510 int r = CMD_RET_SUCCESS;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900511
512 if (argc < 6 || argc > 7)
513 return CMD_RET_USAGE;
514
515 id = (int)simple_strtoul(argv[1], &endp, 16);
516 if (*endp != '\0' || id > 0xffff)
Heinrich Schuchardt1fa442e2019-02-28 20:41:58 +0100517 return CMD_RET_USAGE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900518
519 sprintf(var_name, "Boot%04X", id);
520 p = var_name16;
521 utf8_utf16_strncpy(&p, var_name, 9);
522
523 guid = efi_global_variable_guid;
524
525 /* attributes */
526 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
527
528 /* label */
529 label_len = strlen(argv[2]);
530 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
531 label = malloc((label_len16 + 1) * sizeof(u16));
532 if (!label)
533 return CMD_RET_FAILURE;
534 lo.label = label; /* label will be changed below */
535 utf8_utf16_strncpy(&label, argv[2], label_len);
536
537 /* file path */
538 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
539 &file_path);
540 if (ret != EFI_SUCCESS) {
541 printf("Cannot create device path for \"%s %s\"\n",
542 argv[3], argv[4]);
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200543 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900544 goto out;
545 }
546 lo.file_path = file_path;
547 lo.file_path_length = efi_dp_size(file_path)
548 + sizeof(struct efi_device_path); /* for END */
549
550 /* optional data */
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200551 if (argc < 6)
552 lo.optional_data = NULL;
553 else
554 lo.optional_data = (const u8 *)argv[6];
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900555
556 size = efi_serialize_load_option(&lo, (u8 **)&data);
557 if (!size) {
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200558 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900559 goto out;
560 }
561
562 ret = EFI_CALL(RT->set_variable(var_name16, &guid,
AKASHI Takahirof658c2e2019-06-04 15:52:10 +0900563 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900564 EFI_VARIABLE_BOOTSERVICE_ACCESS |
565 EFI_VARIABLE_RUNTIME_ACCESS,
566 size, data));
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200567 if (ret != EFI_SUCCESS) {
568 printf("Cannot set %ls\n", var_name16);
569 r = CMD_RET_FAILURE;
570 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900571out:
572 free(data);
573 efi_free_pool(device_path);
574 efi_free_pool(file_path);
575 free(lo.label);
576
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200577 return r;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900578}
579
580/**
581 * do_efi_boot_rm() - delete UEFI load options
582 *
583 * @cmdtp: Command table
584 * @flag: Command flag
585 * @argc: Number of arguments
586 * @argv: Argument array
587 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
588 *
589 * Implement efidebug "boot rm" sub-command.
590 * Delete UEFI load options.
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200591 *
592 * efidebug boot rm <id> ...
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900593 */
594static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
595 int argc, char * const argv[])
596{
597 efi_guid_t guid;
598 int id, i;
599 char *endp;
600 char var_name[9];
601 u16 var_name16[9];
602 efi_status_t ret;
603
604 if (argc == 1)
605 return CMD_RET_USAGE;
606
607 guid = efi_global_variable_guid;
608 for (i = 1; i < argc; i++, argv++) {
609 id = (int)simple_strtoul(argv[1], &endp, 16);
610 if (*endp != '\0' || id > 0xffff)
611 return CMD_RET_FAILURE;
612
613 sprintf(var_name, "Boot%04X", id);
614 utf8_utf16_strncpy((u16 **)&var_name16, var_name, 9);
615
616 ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL));
617 if (ret) {
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200618 printf("Cannot remove Boot%04X", id);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900619 return CMD_RET_FAILURE;
620 }
621 }
622
623 return CMD_RET_SUCCESS;
624}
625
626/**
627 * show_efi_boot_opt_data() - dump UEFI load option
628 *
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200629 * @id: load option number
630 * @data: value of UEFI load option variable
631 * @size: size of the boot option
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900632 *
633 * Decode the value of UEFI load option variable and print information.
634 */
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200635static void show_efi_boot_opt_data(int id, void *data, size_t size)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900636{
637 struct efi_load_option lo;
638 char *label, *p;
639 size_t label_len16, label_len;
640 u16 *dp_str;
641
642 efi_deserialize_load_option(&lo, data);
643
644 label_len16 = u16_strlen(lo.label);
645 label_len = utf16_utf8_strnlen(lo.label, label_len16);
646 label = malloc(label_len + 1);
647 if (!label)
648 return;
649 p = label;
650 utf16_utf8_strncpy(&p, lo.label, label_len16);
651
652 printf("Boot%04X:\n", id);
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200653 printf(" attributes: %c%c%c (0x%08x)\n",
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900654 /* ACTIVE */
655 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
656 /* FORCE RECONNECT */
657 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
658 /* HIDDEN */
659 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
660 lo.attributes);
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200661 printf(" label: %s\n", label);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900662
663 dp_str = efi_dp_str(lo.file_path);
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200664 printf(" file_path: %ls\n", dp_str);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900665 efi_free_pool(dp_str);
666
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200667 printf(" data:\n");
668 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
669 lo.optional_data, size + (u8 *)data -
670 (u8 *)lo.optional_data, true);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900671 free(label);
672}
673
674/**
675 * show_efi_boot_opt() - dump UEFI load option
676 *
677 * @id: Load option number
678 *
679 * Dump information defined by UEFI load option.
680 */
681static void show_efi_boot_opt(int id)
682{
683 char var_name[9];
684 u16 var_name16[9], *p;
685 efi_guid_t guid;
686 void *data = NULL;
687 efi_uintn_t size;
688 int ret;
689
690 sprintf(var_name, "Boot%04X", id);
691 p = var_name16;
692 utf8_utf16_strncpy(&p, var_name, 9);
693 guid = efi_global_variable_guid;
694
695 size = 0;
696 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
697 if (ret == (int)EFI_BUFFER_TOO_SMALL) {
698 data = malloc(size);
699 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
700 data));
701 }
702 if (ret == EFI_SUCCESS)
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200703 show_efi_boot_opt_data(id, data, size);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900704 else if (ret == EFI_NOT_FOUND)
705 printf("Boot%04X: not found\n", id);
706
707 free(data);
708}
709
AKASHI Takahiroffe21572019-04-26 09:44:18 +0900710static int u16_tohex(u16 c)
711{
712 if (c >= '0' && c <= '9')
713 return c - '0';
714 if (c >= 'A' && c <= 'F')
715 return c - 'A' + 10;
716
717 /* not hexadecimal */
718 return -1;
719}
720
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900721/**
722 * show_efi_boot_dump() - dump all UEFI load options
723 *
724 * @cmdtp: Command table
725 * @flag: Command flag
726 * @argc: Number of arguments
727 * @argv: Argument array
728 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
729 *
730 * Implement efidebug "boot dump" sub-command.
731 * Dump information of all UEFI load options defined.
732 * - boot dump
733 */
734static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag,
735 int argc, char * const argv[])
736{
AKASHI Takahiroffe21572019-04-26 09:44:18 +0900737 u16 *var_name16, *p;
738 efi_uintn_t buf_size, size;
739 efi_guid_t guid;
740 int id, i, digit;
741 efi_status_t ret;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900742
743 if (argc > 1)
744 return CMD_RET_USAGE;
745
AKASHI Takahiroffe21572019-04-26 09:44:18 +0900746 buf_size = 128;
747 var_name16 = malloc(buf_size);
748 if (!var_name16)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900749 return CMD_RET_FAILURE;
750
AKASHI Takahiroffe21572019-04-26 09:44:18 +0900751 var_name16[0] = 0;
752 for (;;) {
753 size = buf_size;
754 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
755 &guid));
756 if (ret == EFI_NOT_FOUND)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900757 break;
AKASHI Takahiroffe21572019-04-26 09:44:18 +0900758 if (ret == EFI_BUFFER_TOO_SMALL) {
759 buf_size = size;
760 p = realloc(var_name16, buf_size);
761 if (!p) {
762 free(var_name16);
763 return CMD_RET_FAILURE;
764 }
765 var_name16 = p;
766 ret = EFI_CALL(efi_get_next_variable_name(&size,
767 var_name16,
768 &guid));
769 }
770 if (ret != EFI_SUCCESS) {
771 free(var_name16);
772 return CMD_RET_FAILURE;
773 }
774
775 if (memcmp(var_name16, L"Boot", 8))
776 continue;
777
778 for (id = 0, i = 0; i < 4; i++) {
779 digit = u16_tohex(var_name16[4 + i]);
780 if (digit < 0)
781 break;
782 id = (id << 4) + digit;
783 }
784 if (i == 4 && !var_name16[8])
785 show_efi_boot_opt(id);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900786 }
AKASHI Takahiroffe21572019-04-26 09:44:18 +0900787
788 free(var_name16);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900789
790 return CMD_RET_SUCCESS;
791}
792
793/**
794 * show_efi_boot_order() - show order of UEFI load options
795 *
796 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
797 *
798 * Show order of UEFI load options defined by BootOrder variable.
799 */
800static int show_efi_boot_order(void)
801{
802 efi_guid_t guid;
803 u16 *bootorder = NULL;
804 efi_uintn_t size;
805 int num, i;
806 char var_name[9];
807 u16 var_name16[9], *p16;
808 void *data;
809 struct efi_load_option lo;
810 char *label, *p;
811 size_t label_len16, label_len;
812 efi_status_t ret;
813
814 guid = efi_global_variable_guid;
815 size = 0;
816 ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, &size,
817 NULL));
818 if (ret == EFI_BUFFER_TOO_SMALL) {
819 bootorder = malloc(size);
820 ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL,
821 &size, bootorder));
822 }
823 if (ret == EFI_NOT_FOUND) {
824 printf("BootOrder not defined\n");
825 ret = CMD_RET_SUCCESS;
826 goto out;
827 } else if (ret != EFI_SUCCESS) {
828 ret = CMD_RET_FAILURE;
829 goto out;
830 }
831
832 num = size / sizeof(u16);
833 for (i = 0; i < num; i++) {
834 sprintf(var_name, "Boot%04X", bootorder[i]);
835 p16 = var_name16;
836 utf8_utf16_strncpy(&p16, var_name, 9);
837
838 size = 0;
839 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
840 NULL));
841 if (ret != EFI_BUFFER_TOO_SMALL) {
842 printf("%2d: Boot%04X: (not defined)\n",
843 i + 1, bootorder[i]);
844 continue;
845 }
846
847 data = malloc(size);
848 if (!data) {
849 ret = CMD_RET_FAILURE;
850 goto out;
851 }
852 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
853 data));
854 if (ret != EFI_SUCCESS) {
855 free(data);
856 ret = CMD_RET_FAILURE;
857 goto out;
858 }
859
860 efi_deserialize_load_option(&lo, data);
861
862 label_len16 = u16_strlen(lo.label);
863 label_len = utf16_utf8_strnlen(lo.label, label_len16);
864 label = malloc(label_len + 1);
865 if (!label) {
866 free(data);
867 ret = CMD_RET_FAILURE;
868 goto out;
869 }
870 p = label;
871 utf16_utf8_strncpy(&p, lo.label, label_len16);
872 printf("%2d: Boot%04X: %s\n", i + 1, bootorder[i], label);
873 free(label);
874
875 free(data);
876 }
877out:
878 free(bootorder);
879
880 return ret;
881}
882
883/**
884 * do_efi_boot_next() - manage UEFI BootNext variable
885 *
886 * @cmdtp: Command table
887 * @flag: Command flag
888 * @argc: Number of arguments
889 * @argv: Argument array
890 * Return: CMD_RET_SUCCESS on success,
891 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
892 *
893 * Implement efidebug "boot next" sub-command.
894 * Set BootNext variable.
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200895 *
896 * efidebug boot next <id>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900897 */
898static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
899 int argc, char * const argv[])
900{
901 u16 bootnext;
902 efi_uintn_t size;
903 char *endp;
904 efi_guid_t guid;
905 efi_status_t ret;
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200906 int r = CMD_RET_SUCCESS;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900907
908 if (argc != 2)
909 return CMD_RET_USAGE;
910
911 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
912 if (*endp != '\0' || bootnext > 0xffff) {
913 printf("invalid value: %s\n", argv[1]);
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200914 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900915 goto out;
916 }
917
918 guid = efi_global_variable_guid;
919 size = sizeof(u16);
920 ret = EFI_CALL(RT->set_variable(L"BootNext", &guid,
AKASHI Takahirof658c2e2019-06-04 15:52:10 +0900921 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900922 EFI_VARIABLE_BOOTSERVICE_ACCESS |
923 EFI_VARIABLE_RUNTIME_ACCESS,
924 size, &bootnext));
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200925 if (ret != EFI_SUCCESS) {
926 printf("Cannot set BootNext\n");
927 r = CMD_RET_FAILURE;
928 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900929out:
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200930 return r;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900931}
932
933/**
934 * do_efi_boot_order() - manage UEFI BootOrder variable
935 *
936 * @cmdtp: Command table
937 * @flag: Command flag
938 * @argc: Number of arguments
939 * @argv: Argument array
940 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
941 *
942 * Implement efidebug "boot order" sub-command.
943 * Show order of UEFI load options, or change it in BootOrder variable.
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200944 *
945 * efidebug boot order [<id> ...]
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900946 */
947static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
948 int argc, char * const argv[])
949{
950 u16 *bootorder = NULL;
951 efi_uintn_t size;
952 int id, i;
953 char *endp;
954 efi_guid_t guid;
955 efi_status_t ret;
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200956 int r = CMD_RET_SUCCESS;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900957
958 if (argc == 1)
959 return show_efi_boot_order();
960
961 argc--;
962 argv++;
963
964 size = argc * sizeof(u16);
965 bootorder = malloc(size);
966 if (!bootorder)
967 return CMD_RET_FAILURE;
968
969 for (i = 0; i < argc; i++) {
970 id = (int)simple_strtoul(argv[i], &endp, 16);
971 if (*endp != '\0' || id > 0xffff) {
972 printf("invalid value: %s\n", argv[i]);
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200973 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900974 goto out;
975 }
976
977 bootorder[i] = (u16)id;
978 }
979
980 guid = efi_global_variable_guid;
981 ret = EFI_CALL(RT->set_variable(L"BootOrder", &guid,
AKASHI Takahirof658c2e2019-06-04 15:52:10 +0900982 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900983 EFI_VARIABLE_BOOTSERVICE_ACCESS |
984 EFI_VARIABLE_RUNTIME_ACCESS,
985 size, bootorder));
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200986 if (ret != EFI_SUCCESS) {
987 printf("Cannot set BootOrder\n");
988 r = CMD_RET_FAILURE;
989 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900990out:
991 free(bootorder);
992
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200993 return r;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900994}
995
996static cmd_tbl_t cmd_efidebug_boot_sub[] = {
997 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
998 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
999 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1000 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1001 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1002 "", ""),
1003};
1004
1005/**
1006 * do_efi_boot_opt() - manage UEFI load options
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,
1013 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1014 *
1015 * Implement efidebug "boot" sub-command.
1016 * See above for details of sub-commands.
1017 */
1018static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag,
1019 int argc, char * const argv[])
1020{
1021 cmd_tbl_t *cp;
1022
1023 if (argc < 2)
1024 return CMD_RET_USAGE;
1025
1026 argc--; argv++;
1027
1028 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1029 ARRAY_SIZE(cmd_efidebug_boot_sub));
1030 if (!cp)
1031 return CMD_RET_USAGE;
1032
1033 return cp->cmd(cmdtp, flag, argc, argv);
1034}
1035
1036static cmd_tbl_t cmd_efidebug_sub[] = {
1037 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
AKASHI Takahiro355cdb52019-02-25 15:54:39 +09001038 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1039 "", ""),
AKASHI Takahiro66eaf562019-02-25 15:54:40 +09001040 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1041 "", ""),
AKASHI Takahiroa8014622019-02-25 15:54:41 +09001042 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1043 "", ""),
AKASHI Takahirofa536732019-02-25 15:54:42 +09001044 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1045 "", ""),
AKASHI Takahiro00358bb2019-02-25 15:54:43 +09001046 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1047 "", ""),
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001048};
1049
1050/**
1051 * do_efidebug() - display and configure UEFI environment
1052 *
1053 * @cmdtp: Command table
1054 * @flag: Command flag
1055 * @argc: Number of arguments
1056 * @argv: Argument array
1057 * Return: CMD_RET_SUCCESS on success,
1058 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1059 *
1060 * Implement efidebug command which allows us to display and
1061 * configure UEFI environment.
1062 * See above for details of sub-commands.
1063 */
1064static int do_efidebug(cmd_tbl_t *cmdtp, int flag,
1065 int argc, char * const argv[])
1066{
1067 cmd_tbl_t *cp;
1068 efi_status_t r;
1069
1070 if (argc < 2)
1071 return CMD_RET_USAGE;
1072
1073 argc--; argv++;
1074
1075 /* Initialize UEFI drivers */
1076 r = efi_init_obj_list();
1077 if (r != EFI_SUCCESS) {
1078 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1079 r & ~EFI_ERROR_MASK);
1080 return CMD_RET_FAILURE;
1081 }
1082
1083 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1084 ARRAY_SIZE(cmd_efidebug_sub));
1085 if (!cp)
1086 return CMD_RET_USAGE;
1087
1088 return cp->cmd(cmdtp, flag, argc, argv);
1089}
1090
1091#ifdef CONFIG_SYS_LONGHELP
1092static char efidebug_help_text[] =
1093 " - UEFI Shell-like interface to configure UEFI environment\n"
1094 "\n"
1095 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
1096 " - set UEFI BootXXXX variable\n"
1097 " <load options> will be passed to UEFI application\n"
1098 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1099 " - delete UEFI BootXXXX variables\n"
1100 "efidebug boot dump\n"
1101 " - dump all UEFI BootXXXX variables\n"
1102 "efidebug boot next <bootid>\n"
1103 " - set UEFI BootNext variable\n"
1104 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1105 " - set/show UEFI boot order\n"
AKASHI Takahiro355cdb52019-02-25 15:54:39 +09001106 "\n"
1107 "efidebug devices\n"
AKASHI Takahiro66eaf562019-02-25 15:54:40 +09001108 " - show uefi devices\n"
1109 "efidebug drivers\n"
AKASHI Takahiroa8014622019-02-25 15:54:41 +09001110 " - show uefi drivers\n"
1111 "efidebug dh\n"
AKASHI Takahirofa536732019-02-25 15:54:42 +09001112 " - show uefi handles\n"
1113 "efidebug images\n"
AKASHI Takahiro00358bb2019-02-25 15:54:43 +09001114 " - show loaded images\n"
1115 "efidebug memmap\n"
1116 " - show uefi memory map\n";
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001117#endif
1118
1119U_BOOT_CMD(
1120 efidebug, 10, 0, do_efidebug,
1121 "Configure UEFI environment",
1122 efidebug_help_text
1123);