blob: 4b49f30d9377811bcbd5d94af874aa6e64fce5e1 [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>
Heinrich Schuchardtf3290be2022-10-04 15:31:17 +020011#include <dm/device.h>
Heinrich Schuchardt94686f62020-12-13 10:30:24 +010012#include <efi_dt_fixup.h>
Ilias Apalodimascbea2412021-03-17 21:55:01 +020013#include <efi_load_initrd.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090014#include <efi_loader.h>
Heinrich Schuchardt79693352020-09-27 14:50:25 +020015#include <efi_rng.h>
Heinrich Schuchardtacfe1de2021-05-24 11:10:59 +020016#include <efi_variable.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090017#include <exports.h>
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +020018#include <hexdump.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060019#include <log.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090020#include <malloc.h>
Heinrich Schuchardta415d612020-03-14 08:44:07 +010021#include <mapmem.h>
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +010022#include <part.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090023#include <search.h>
24#include <linux/ctype.h>
Ilias Apalodimascbea2412021-03-17 21:55:01 +020025#include <linux/err.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090026
AKASHI Takahiro355cdb52019-02-25 15:54:39 +090027#define BS systab.boottime
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +090028#define RT systab.runtime
29
30#ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
31/**
32 * do_efi_capsule_update() - process a capsule update
33 *
34 * @cmdtp: Command table
35 * @flag: Command flag
36 * @argc: Number of arguments
37 * @argv: Argument array
38 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
39 *
40 * Implement efidebug "capsule update" sub-command.
41 * process a capsule update.
42 *
43 * efidebug capsule update [-v] <capsule address>
44 */
45static int do_efi_capsule_update(struct cmd_tbl *cmdtp, int flag,
46 int argc, char * const argv[])
47{
48 struct efi_capsule_header *capsule;
49 int verbose = 0;
50 char *endp;
51 efi_status_t ret;
52
53 if (argc != 2 && argc != 3)
54 return CMD_RET_USAGE;
55
56 if (argc == 3) {
57 if (strcmp(argv[1], "-v"))
58 return CMD_RET_USAGE;
59
60 verbose = 1;
61 argc--;
62 argv++;
63 }
64
Simon Glass7e5f4602021-07-24 09:03:29 -060065 capsule = (typeof(capsule))hextoul(argv[1], &endp);
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +090066 if (endp == argv[1]) {
67 printf("Invalid address: %s", argv[1]);
68 return CMD_RET_FAILURE;
69 }
70
71 if (verbose) {
72 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
73 printf("Capsule flags: 0x%x\n", capsule->flags);
74 printf("Capsule header size: 0x%x\n", capsule->header_size);
75 printf("Capsule image size: 0x%x\n",
76 capsule->capsule_image_size);
77 }
78
Simon Glassdf7d89a2021-02-07 14:27:02 -070079 ret = EFI_CALL(RT->update_capsule(&capsule, 1, 0));
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +090080 if (ret) {
Michal Simek046d7a02022-08-09 16:31:12 +020081 printf("Cannot handle a capsule at %p\n", capsule);
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +090082 return CMD_RET_FAILURE;
83 }
84
85 return CMD_RET_SUCCESS;
86}
87
Masami Hiramatsu4cd1fca2022-03-21 22:37:35 +090088#ifdef CONFIG_EFI_CAPSULE_ON_DISK
Sughosh Ganu74075952020-12-30 19:27:11 +053089static int do_efi_capsule_on_disk_update(struct cmd_tbl *cmdtp, int flag,
90 int argc, char * const argv[])
91{
92 efi_status_t ret;
93
94 ret = efi_launch_capsules();
95
96 return ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
97}
Masami Hiramatsu4cd1fca2022-03-21 22:37:35 +090098#endif
Sughosh Ganu74075952020-12-30 19:27:11 +053099
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900100/**
101 * do_efi_capsule_show() - show capsule information
102 *
103 * @cmdtp: Command table
104 * @flag: Command flag
105 * @argc: Number of arguments
106 * @argv: Argument array
107 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
108 *
109 * Implement efidebug "capsule show" sub-command.
110 * show capsule information.
111 *
112 * efidebug capsule show <capsule address>
113 */
114static int do_efi_capsule_show(struct cmd_tbl *cmdtp, int flag,
115 int argc, char * const argv[])
116{
117 struct efi_capsule_header *capsule;
118 char *endp;
119
120 if (argc != 2)
121 return CMD_RET_USAGE;
122
Simon Glass7e5f4602021-07-24 09:03:29 -0600123 capsule = (typeof(capsule))hextoul(argv[1], &endp);
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900124 if (endp == argv[1]) {
125 printf("Invalid address: %s", argv[1]);
126 return CMD_RET_FAILURE;
127 }
128
129 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
130 printf("Capsule flags: 0x%x\n", capsule->flags);
131 printf("Capsule header size: 0x%x\n", capsule->header_size);
132 printf("Capsule image size: 0x%x\n",
133 capsule->capsule_image_size);
134
135 return CMD_RET_SUCCESS;
136}
137
Jose Marinhoaa31a872021-03-11 13:18:51 +0000138#ifdef CONFIG_EFI_ESRT
139
140#define EFI_ESRT_FW_TYPE_NUM 4
141char *efi_fw_type_str[EFI_ESRT_FW_TYPE_NUM] = {"unknown", "system FW", "device FW",
142 "UEFI driver"};
143
144#define EFI_ESRT_UPDATE_STATUS_NUM 9
145char *efi_update_status_str[EFI_ESRT_UPDATE_STATUS_NUM] = {"success", "unsuccessful",
146 "insufficient resources", "incorrect version", "invalid format",
147 "auth error", "power event (AC)", "power event (batt)",
148 "unsatisfied dependencies"};
149
150#define EFI_FW_TYPE_STR_GET(idx) (\
151EFI_ESRT_FW_TYPE_NUM > (idx) ? efi_fw_type_str[(idx)] : "error"\
152)
153
154#define EFI_FW_STATUS_STR_GET(idx) (\
155EFI_ESRT_UPDATE_STATUS_NUM > (idx) ? efi_update_status_str[(idx)] : "error"\
156)
157
158/**
159 * do_efi_capsule_esrt() - manage UEFI capsules
160 *
161 * @cmdtp: Command table
162 * @flag: Command flag
163 * @argc: Number of arguments
164 * @argv: Argument array
165 * Return: CMD_RET_SUCCESS on success,
166 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
167 *
168 * Implement efidebug "capsule esrt" sub-command.
169 * The prints the current ESRT table.
170 *
171 * efidebug capsule esrt
172 */
173static int do_efi_capsule_esrt(struct cmd_tbl *cmdtp, int flag,
174 int argc, char * const argv[])
175{
176 struct efi_system_resource_table *esrt = NULL;
177
178 if (argc != 1)
179 return CMD_RET_USAGE;
180
181 for (int idx = 0; idx < systab.nr_tables; idx++)
182 if (!guidcmp(&efi_esrt_guid, &systab.tables[idx].guid))
183 esrt = (struct efi_system_resource_table *)systab.tables[idx].table;
184
185 if (!esrt) {
186 log_info("ESRT: table not present\n");
187 return CMD_RET_SUCCESS;
188 }
189
190 printf("========================================\n");
191 printf("ESRT: fw_resource_count=%d\n", esrt->fw_resource_count);
192 printf("ESRT: fw_resource_count_max=%d\n", esrt->fw_resource_count_max);
193 printf("ESRT: fw_resource_version=%lld\n", esrt->fw_resource_version);
194
195 for (int idx = 0; idx < esrt->fw_resource_count; idx++) {
196 printf("[entry %d]==============================\n", idx);
197 printf("ESRT: fw_class=%pUL\n", &esrt->entries[idx].fw_class);
198 printf("ESRT: fw_type=%s\n", EFI_FW_TYPE_STR_GET(esrt->entries[idx].fw_type));
199 printf("ESRT: fw_version=%d\n", esrt->entries[idx].fw_version);
200 printf("ESRT: lowest_supported_fw_version=%d\n",
201 esrt->entries[idx].lowest_supported_fw_version);
202 printf("ESRT: capsule_flags=%d\n",
203 esrt->entries[idx].capsule_flags);
204 printf("ESRT: last_attempt_version=%d\n",
205 esrt->entries[idx].last_attempt_version);
206 printf("ESRT: last_attempt_status=%s\n",
207 EFI_FW_STATUS_STR_GET(esrt->entries[idx].last_attempt_status));
208 }
209 printf("========================================\n");
210
211 return CMD_RET_SUCCESS;
212}
213#endif /* CONFIG_EFI_ESRT */
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900214/**
215 * do_efi_capsule_res() - show a capsule update result
216 *
217 * @cmdtp: Command table
218 * @flag: Command flag
219 * @argc: Number of arguments
220 * @argv: Argument array
221 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
222 *
223 * Implement efidebug "capsule result" sub-command.
224 * show a capsule update result.
225 * If result number is not specified, CapsuleLast will be shown.
226 *
227 * efidebug capsule result [<capsule result number>]
228 */
229static int do_efi_capsule_res(struct cmd_tbl *cmdtp, int flag,
230 int argc, char * const argv[])
231{
232 int capsule_id;
233 char *endp;
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +0200234 u16 var_name16[12];
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900235 efi_guid_t guid;
236 struct efi_capsule_result_variable_header *result = NULL;
237 efi_uintn_t size;
238 efi_status_t ret;
239
240 if (argc != 1 && argc != 2)
241 return CMD_RET_USAGE;
242
243 guid = efi_guid_capsule_report;
244 if (argc == 1) {
245 size = sizeof(var_name16);
Simon Glass156ccbc2022-01-23 12:55:12 -0700246 ret = efi_get_variable_int(u"CapsuleLast", &guid, NULL,
Heinrich Schuchardtacfe1de2021-05-24 11:10:59 +0200247 &size, var_name16, NULL);
248
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900249 if (ret != EFI_SUCCESS) {
250 if (ret == EFI_NOT_FOUND)
251 printf("CapsuleLast doesn't exist\n");
252 else
253 printf("Failed to get CapsuleLast\n");
254
255 return CMD_RET_FAILURE;
256 }
257 printf("CapsuleLast is %ls\n", var_name16);
258 } else {
259 argc--;
260 argv++;
261
Simon Glass7e5f4602021-07-24 09:03:29 -0600262 capsule_id = hextoul(argv[0], &endp);
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900263 if (capsule_id < 0 || capsule_id > 0xffff)
264 return CMD_RET_USAGE;
265
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +0200266 efi_create_indexed_name(var_name16, sizeof(var_name16),
267 "Capsule", capsule_id);
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900268 }
269
270 size = 0;
Heinrich Schuchardtacfe1de2021-05-24 11:10:59 +0200271 ret = efi_get_variable_int(var_name16, &guid, NULL, &size, NULL, NULL);
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900272 if (ret == EFI_BUFFER_TOO_SMALL) {
273 result = malloc(size);
AKASHI Takahiro30f82222021-01-22 10:42:48 +0900274 if (!result)
275 return CMD_RET_FAILURE;
Heinrich Schuchardtacfe1de2021-05-24 11:10:59 +0200276 ret = efi_get_variable_int(var_name16, &guid, NULL, &size,
277 result, NULL);
AKASHI Takahiro30f82222021-01-22 10:42:48 +0900278 }
279 if (ret != EFI_SUCCESS) {
280 free(result);
281 printf("Failed to get %ls\n", var_name16);
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900282
AKASHI Takahiro30f82222021-01-22 10:42:48 +0900283 return CMD_RET_FAILURE;
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900284 }
285
286 printf("Result total size: 0x%x\n", result->variable_total_size);
287 printf("Capsule guid: %pUl\n", &result->capsule_guid);
288 printf("Time processed: %04d-%02d-%02d %02d:%02d:%02d\n",
289 result->capsule_processed.year, result->capsule_processed.month,
290 result->capsule_processed.day, result->capsule_processed.hour,
291 result->capsule_processed.minute,
292 result->capsule_processed.second);
293 printf("Capsule status: 0x%lx\n", result->capsule_status);
294
295 free(result);
296
297 return CMD_RET_SUCCESS;
298}
299
300static struct cmd_tbl cmd_efidebug_capsule_sub[] = {
301 U_BOOT_CMD_MKENT(update, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_update,
302 "", ""),
303 U_BOOT_CMD_MKENT(show, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_show,
304 "", ""),
Jose Marinhoaa31a872021-03-11 13:18:51 +0000305#ifdef CONFIG_EFI_ESRT
306 U_BOOT_CMD_MKENT(esrt, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_esrt,
307 "", ""),
308#endif
Masami Hiramatsu4cd1fca2022-03-21 22:37:35 +0900309#ifdef CONFIG_EFI_CAPSULE_ON_DISK
Sughosh Ganu74075952020-12-30 19:27:11 +0530310 U_BOOT_CMD_MKENT(disk-update, 0, 0, do_efi_capsule_on_disk_update,
311 "", ""),
Masami Hiramatsu4cd1fca2022-03-21 22:37:35 +0900312#endif
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900313 U_BOOT_CMD_MKENT(result, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_res,
314 "", ""),
315};
316
317/**
318 * do_efi_capsule() - manage UEFI capsules
319 *
320 * @cmdtp: Command table
321 * @flag: Command flag
322 * @argc: Number of arguments
323 * @argv: Argument array
324 * Return: CMD_RET_SUCCESS on success,
325 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
326 *
327 * Implement efidebug "capsule" sub-command.
328 */
329static int do_efi_capsule(struct cmd_tbl *cmdtp, int flag,
330 int argc, char * const argv[])
331{
332 struct cmd_tbl *cp;
333
334 if (argc < 2)
335 return CMD_RET_USAGE;
336
337 argc--; argv++;
338
339 cp = find_cmd_tbl(argv[0], cmd_efidebug_capsule_sub,
340 ARRAY_SIZE(cmd_efidebug_capsule_sub));
341 if (!cp)
342 return CMD_RET_USAGE;
343
344 return cp->cmd(cmdtp, flag, argc, argv);
345}
346#endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT */
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900347
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900348#define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
349
350static const char spc[] = " ";
351static const char sep[] = "================";
352
353/**
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900354 * efi_get_driver_handle_info() - get information of UEFI driver
355 *
356 * @handle: Handle of UEFI device
357 * @driver_name: Driver name
358 * @image_path: Pointer to text of device path
359 * Return: 0 on success, -1 on failure
360 *
361 * Currently return no useful information as all UEFI drivers are
362 * built-in..
363 */
364static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
365 u16 **image_path)
366{
367 struct efi_handler *handler;
368 struct efi_loaded_image *image;
369 efi_status_t ret;
370
371 /*
372 * driver name
373 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
374 */
375 *driver_name = NULL;
376
377 /* image name */
378 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
379 if (ret != EFI_SUCCESS) {
380 *image_path = NULL;
381 return 0;
382 }
383
384 image = handler->protocol_interface;
385 *image_path = efi_dp_str(image->file_path);
386
387 return 0;
388}
389
390/**
391 * do_efi_show_drivers() - show UEFI drivers
392 *
393 * @cmdtp: Command table
394 * @flag: Command flag
395 * @argc: Number of arguments
396 * @argv: Argument array
397 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
398 *
399 * Implement efidebug "drivers" sub-command.
400 * Show all UEFI drivers and their information.
401 */
Simon Glass09140112020-05-10 11:40:03 -0600402static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
403 int argc, char *const argv[])
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900404{
405 efi_handle_t *handles;
406 efi_uintn_t num, i;
407 u16 *driver_name, *image_path_text;
408 efi_status_t ret;
409
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200410 ret = EFI_CALL(efi_locate_handle_buffer(
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900411 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
412 NULL, &num, &handles));
413 if (ret != EFI_SUCCESS)
414 return CMD_RET_FAILURE;
415
416 if (!num)
417 return CMD_RET_SUCCESS;
418
419 printf("Driver%.*s Name Image Path\n",
420 EFI_HANDLE_WIDTH - 6, spc);
421 printf("%.*s ==================== ====================\n",
422 EFI_HANDLE_WIDTH, sep);
423 for (i = 0; i < num; i++) {
424 if (!efi_get_driver_handle_info(handles[i], &driver_name,
425 &image_path_text)) {
426 if (image_path_text)
427 printf("%p %-20ls %ls\n", handles[i],
428 driver_name, image_path_text);
429 else
430 printf("%p %-20ls <built-in>\n",
431 handles[i], driver_name);
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200432 efi_free_pool(driver_name);
433 efi_free_pool(image_path_text);
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900434 }
435 }
436
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200437 efi_free_pool(handles);
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900438
439 return CMD_RET_SUCCESS;
440}
441
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900442/**
443 * do_efi_show_handles() - show UEFI handles
444 *
445 * @cmdtp: Command table
446 * @flag: Command flag
447 * @argc: Number of arguments
448 * @argv: Argument array
449 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
450 *
451 * Implement efidebug "dh" sub-command.
452 * Show all UEFI handles and their information, currently all protocols
453 * added to handle.
454 */
Simon Glass09140112020-05-10 11:40:03 -0600455static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
456 int argc, char *const argv[])
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900457{
458 efi_handle_t *handles;
459 efi_guid_t **guid;
460 efi_uintn_t num, count, i, j;
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900461 efi_status_t ret;
462
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200463 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900464 &num, &handles));
465 if (ret != EFI_SUCCESS)
466 return CMD_RET_FAILURE;
467
468 if (!num)
469 return CMD_RET_SUCCESS;
470
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900471 for (i = 0; i < num; i++) {
Heinrich Schuchardtf3290be2022-10-04 15:31:17 +0200472 struct efi_handler *handler;
473
474 printf("\n%p", handles[i]);
475 if (handles[i]->dev)
476 printf(" (%s)", handles[i]->dev->name);
477 printf("\n");
478 /* Print device path */
479 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
480 &handler);
481 if (ret == EFI_SUCCESS)
482 printf(" %pD\n", handler->protocol_interface);
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900483 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
484 &count));
Heinrich Schuchardtf3290be2022-10-04 15:31:17 +0200485 /* Print other protocols */
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900486 for (j = 0; j < count; j++) {
Heinrich Schuchardtf3290be2022-10-04 15:31:17 +0200487 if (guidcmp(guid[j], &efi_guid_device_path))
488 printf(" %pUs\n", guid[j]);
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900489 }
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900490 }
491
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200492 efi_free_pool(handles);
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900493
494 return CMD_RET_SUCCESS;
495}
496
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900497/**
AKASHI Takahirofa536732019-02-25 15:54:42 +0900498 * do_efi_show_images() - show UEFI images
499 *
500 * @cmdtp: Command table
501 * @flag: Command flag
502 * @argc: Number of arguments
503 * @argv: Argument array
504 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
505 *
506 * Implement efidebug "images" sub-command.
507 * Show all UEFI loaded images and their information.
508 */
Simon Glass09140112020-05-10 11:40:03 -0600509static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
510 int argc, char *const argv[])
AKASHI Takahirofa536732019-02-25 15:54:42 +0900511{
512 efi_print_image_infos(NULL);
513
514 return CMD_RET_SUCCESS;
515}
516
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900517static const char * const efi_mem_type_string[] = {
518 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
519 [EFI_LOADER_CODE] = "LOADER CODE",
520 [EFI_LOADER_DATA] = "LOADER DATA",
521 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
522 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
523 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
524 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
525 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
526 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
527 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
528 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
529 [EFI_MMAP_IO] = "IO",
530 [EFI_MMAP_IO_PORT] = "IO PORT",
531 [EFI_PAL_CODE] = "PAL",
Heinrich Schuchardtdd9056c2020-04-29 20:21:39 +0200532 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900533};
534
535static const struct efi_mem_attrs {
536 const u64 bit;
537 const char *text;
538} efi_mem_attrs[] = {
539 {EFI_MEMORY_UC, "UC"},
540 {EFI_MEMORY_UC, "UC"},
541 {EFI_MEMORY_WC, "WC"},
542 {EFI_MEMORY_WT, "WT"},
543 {EFI_MEMORY_WB, "WB"},
544 {EFI_MEMORY_UCE, "UCE"},
545 {EFI_MEMORY_WP, "WP"},
546 {EFI_MEMORY_RP, "RP"},
547 {EFI_MEMORY_XP, "WP"},
548 {EFI_MEMORY_NV, "NV"},
549 {EFI_MEMORY_MORE_RELIABLE, "REL"},
550 {EFI_MEMORY_RO, "RO"},
Heinrich Schuchardt255a4732020-05-19 07:20:46 +0200551 {EFI_MEMORY_SP, "SP"},
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900552 {EFI_MEMORY_RUNTIME, "RT"},
553};
554
555/**
556 * print_memory_attributes() - print memory map attributes
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200557 *
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900558 * @attributes: Attribute value
559 *
560 * Print memory map attributes
561 */
562static void print_memory_attributes(u64 attributes)
563{
564 int sep, i;
565
566 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
567 if (attributes & efi_mem_attrs[i].bit) {
568 if (sep) {
569 putc('|');
570 } else {
571 putc(' ');
572 sep = 1;
573 }
574 puts(efi_mem_attrs[i].text);
575 }
576}
577
578#define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
579
580/**
581 * do_efi_show_memmap() - show UEFI memory map
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 "memmap" sub-command.
590 * Show UEFI memory map.
591 */
Simon Glass09140112020-05-10 11:40:03 -0600592static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
593 int argc, char *const argv[])
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900594{
595 struct efi_mem_desc *memmap = NULL, *map;
596 efi_uintn_t map_size = 0;
597 const char *type;
598 int i;
599 efi_status_t ret;
600
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200601 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900602 if (ret == EFI_BUFFER_TOO_SMALL) {
603 map_size += sizeof(struct efi_mem_desc); /* for my own */
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200604 ret = efi_allocate_pool(EFI_LOADER_DATA, map_size,
605 (void *)&memmap);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900606 if (ret != EFI_SUCCESS)
607 return CMD_RET_FAILURE;
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200608 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900609 }
610 if (ret != EFI_SUCCESS) {
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200611 efi_free_pool(memmap);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900612 return CMD_RET_FAILURE;
613 }
614
615 printf("Type Start%.*s End%.*s Attributes\n",
616 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
617 printf("================ %.*s %.*s ==========\n",
618 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
AKASHI Takahiroe2a5b862020-05-08 14:50:18 +0900619 /*
620 * Coverity check: dereferencing null pointer "map."
621 * This is a false positive as memmap will always be
622 * populated by allocate_pool() above.
623 */
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900624 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
Heinrich Schuchardtdd9056c2020-04-29 20:21:39 +0200625 if (map->type < ARRAY_SIZE(efi_mem_type_string))
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900626 type = efi_mem_type_string[map->type];
627 else
628 type = "(unknown)";
629
630 printf("%-16s %.*llx-%.*llx", type,
631 EFI_PHYS_ADDR_WIDTH,
Heinrich Schuchardt6c0ef352020-03-27 04:33:17 +0000632 (u64)map_to_sysmem((void *)(uintptr_t)
633 map->physical_start),
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900634 EFI_PHYS_ADDR_WIDTH,
Heinrich Schuchardt6c0ef352020-03-27 04:33:17 +0000635 (u64)map_to_sysmem((void *)(uintptr_t)
636 (map->physical_start +
637 map->num_pages * EFI_PAGE_SIZE)));
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900638
639 print_memory_attributes(map->attribute);
640 putc('\n');
641 }
642
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200643 efi_free_pool(memmap);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900644
645 return CMD_RET_SUCCESS;
646}
647
AKASHI Takahirofa536732019-02-25 15:54:42 +0900648/**
Heinrich Schuchardt986e0642020-01-07 05:57:47 +0100649 * do_efi_show_tables() - show UEFI configuration tables
650 *
651 * @cmdtp: Command table
652 * @flag: Command flag
653 * @argc: Number of arguments
654 * @argv: Argument array
655 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
656 *
657 * Implement efidebug "tables" sub-command.
658 * Show UEFI configuration tables.
659 */
Simon Glass09140112020-05-10 11:40:03 -0600660static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
661 int argc, char *const argv[])
Heinrich Schuchardt986e0642020-01-07 05:57:47 +0100662{
663 efi_uintn_t i;
Heinrich Schuchardt986e0642020-01-07 05:57:47 +0100664
Heinrich Schuchardt3adae642022-01-16 14:10:23 +0100665 for (i = 0; i < systab.nr_tables; ++i)
666 printf("%pUl (%pUs)\n",
667 &systab.tables[i].guid, &systab.tables[i].guid);
Heinrich Schuchardt986e0642020-01-07 05:57:47 +0100668
669 return CMD_RET_SUCCESS;
670}
671
672/**
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100673 * create_initrd_dp() - create a special device for our Boot### option
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200674 *
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100675 * @dev: device
676 * @part: disk partition
677 * @file: filename
678 * @shortform: create short form device path
679 * Return: pointer to the device path or ERR_PTR
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200680 */
681static
682struct efi_device_path *create_initrd_dp(const char *dev, const char *part,
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100683 const char *file, int shortform)
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200684
685{
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100686 struct efi_device_path *tmp_dp = NULL, *tmp_fp = NULL, *short_fp = NULL;
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200687 struct efi_device_path *initrd_dp = NULL;
688 efi_status_t ret;
689 const struct efi_initrd_dp id_dp = {
690 .vendor = {
691 {
692 DEVICE_PATH_TYPE_MEDIA_DEVICE,
693 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
694 sizeof(id_dp.vendor),
695 },
696 EFI_INITRD_MEDIA_GUID,
697 },
698 .end = {
699 DEVICE_PATH_TYPE_END,
700 DEVICE_PATH_SUB_TYPE_END,
701 sizeof(id_dp.end),
702 }
703 };
704
705 ret = efi_dp_from_name(dev, part, file, &tmp_dp, &tmp_fp);
706 if (ret != EFI_SUCCESS) {
707 printf("Cannot create device path for \"%s %s\"\n", part, file);
708 goto out;
709 }
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100710 if (shortform)
711 short_fp = efi_dp_shorten(tmp_fp);
712 if (!short_fp)
713 short_fp = tmp_fp;
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200714
715 initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp,
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100716 short_fp);
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200717
718out:
719 efi_free_pool(tmp_dp);
720 efi_free_pool(tmp_fp);
721 return initrd_dp;
722}
723
724/**
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900725 * do_efi_boot_add() - set UEFI load option
726 *
727 * @cmdtp: Command table
728 * @flag: Command flag
729 * @argc: Number of arguments
730 * @argv: Argument array
731 * Return: CMD_RET_SUCCESS on success,
732 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
733 *
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200734 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
735 *
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200736 * efidebug boot add -b <id> <label> <interface> <devnum>[:<part>] <file>
737 * -i <file> <interface2> <devnum2>[:<part>] <initrd>
738 * -s '<options>'
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900739 */
Simon Glass09140112020-05-10 11:40:03 -0600740static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
741 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900742{
743 int id;
744 char *endp;
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +0200745 u16 var_name16[9];
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900746 efi_guid_t guid;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900747 u16 *label;
Heinrich Schuchardtccc41fc2022-03-23 20:26:25 +0100748 struct efi_device_path *file_path = NULL;
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100749 struct efi_device_path *fp_free = NULL;
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200750 struct efi_device_path *final_fp = NULL;
751 struct efi_device_path *initrd_dp = NULL;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900752 struct efi_load_option lo;
753 void *data = NULL;
754 efi_uintn_t size;
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200755 efi_uintn_t fp_size = 0;
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200756 efi_status_t ret;
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200757 int r = CMD_RET_SUCCESS;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900758
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900759 guid = efi_global_variable_guid;
760
761 /* attributes */
762 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200763 lo.optional_data = NULL;
764 lo.label = NULL;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900765
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200766 argc--;
767 argv++; /* 'add' */
768 for (; argc > 0; argc--, argv++) {
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100769 int shortform;
770
771 if (*argv[0] != '-' || strlen(argv[0]) != 2) {
772 r = CMD_RET_USAGE;
773 goto out;
774 }
775 shortform = 0;
776 switch (argv[0][1]) {
777 case 'b':
778 shortform = 1;
779 /* fallthrough */
780 case 'B':
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200781 if (argc < 5 || lo.label) {
782 r = CMD_RET_USAGE;
783 goto out;
784 }
Simon Glass7e5f4602021-07-24 09:03:29 -0600785 id = (int)hextoul(argv[1], &endp);
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200786 if (*endp != '\0' || id > 0xffff)
787 return CMD_RET_USAGE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900788
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +0200789 efi_create_indexed_name(var_name16, sizeof(var_name16),
790 "Boot", id);
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200791
792 /* label */
Heinrich Schuchardt80fadf42022-10-06 06:48:02 +0200793 label = efi_convert_string(argv[2]);
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200794 if (!label)
795 return CMD_RET_FAILURE;
796 lo.label = label; /* label will be changed below */
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200797
798 /* file path */
799 ret = efi_dp_from_name(argv[3], argv[4], argv[5],
Heinrich Schuchardtccc41fc2022-03-23 20:26:25 +0100800 NULL, &fp_free);
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200801 if (ret != EFI_SUCCESS) {
802 printf("Cannot create device path for \"%s %s\"\n",
803 argv[3], argv[4]);
804 r = CMD_RET_FAILURE;
805 goto out;
806 }
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100807 if (shortform)
808 file_path = efi_dp_shorten(fp_free);
809 if (!file_path)
810 file_path = fp_free;
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200811 fp_size += efi_dp_size(file_path) +
812 sizeof(struct efi_device_path);
813 argc -= 5;
814 argv += 5;
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100815 break;
816 case 'i':
817 shortform = 1;
818 /* fallthrough */
819 case 'I':
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200820 if (argc < 3 || initrd_dp) {
821 r = CMD_RET_USAGE;
822 goto out;
823 }
824
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100825 initrd_dp = create_initrd_dp(argv[1], argv[2], argv[3],
826 shortform);
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200827 if (!initrd_dp) {
828 printf("Cannot add an initrd\n");
829 r = CMD_RET_FAILURE;
830 goto out;
831 }
832 argc -= 3;
833 argv += 3;
834 fp_size += efi_dp_size(initrd_dp) +
835 sizeof(struct efi_device_path);
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100836 break;
837 case 's':
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200838 if (argc < 1 || lo.optional_data) {
839 r = CMD_RET_USAGE;
840 goto out;
841 }
842 lo.optional_data = (const u8 *)argv[1];
843 argc -= 1;
844 argv += 1;
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100845 break;
846 default:
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200847 r = CMD_RET_USAGE;
848 goto out;
849 }
850 }
851
852 if (!file_path) {
853 printf("Missing binary\n");
854 r = CMD_RET_USAGE;
855 goto out;
856 }
857
858 final_fp = efi_dp_concat(file_path, initrd_dp);
859 if (!final_fp) {
860 printf("Cannot create final device path\n");
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200861 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900862 goto out;
863 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900864
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200865 lo.file_path = final_fp;
866 lo.file_path_length = fp_size;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900867
868 size = efi_serialize_load_option(&lo, (u8 **)&data);
869 if (!size) {
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200870 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900871 goto out;
872 }
873
Heinrich Schuchardtacfe1de2021-05-24 11:10:59 +0200874 ret = efi_set_variable_int(var_name16, &guid,
875 EFI_VARIABLE_NON_VOLATILE |
876 EFI_VARIABLE_BOOTSERVICE_ACCESS |
877 EFI_VARIABLE_RUNTIME_ACCESS,
878 size, data, false);
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200879 if (ret != EFI_SUCCESS) {
880 printf("Cannot set %ls\n", var_name16);
881 r = CMD_RET_FAILURE;
882 }
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200883
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900884out:
885 free(data);
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200886 efi_free_pool(final_fp);
887 efi_free_pool(initrd_dp);
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100888 efi_free_pool(fp_free);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900889 free(lo.label);
890
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200891 return r;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900892}
893
894/**
895 * do_efi_boot_rm() - delete UEFI load options
896 *
897 * @cmdtp: Command table
898 * @flag: Command flag
899 * @argc: Number of arguments
900 * @argv: Argument array
901 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
902 *
903 * Implement efidebug "boot rm" sub-command.
904 * Delete UEFI load options.
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200905 *
906 * efidebug boot rm <id> ...
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900907 */
Simon Glass09140112020-05-10 11:40:03 -0600908static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag,
909 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900910{
911 efi_guid_t guid;
912 int id, i;
913 char *endp;
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +0200914 u16 var_name16[9];
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900915 efi_status_t ret;
916
917 if (argc == 1)
918 return CMD_RET_USAGE;
919
920 guid = efi_global_variable_guid;
921 for (i = 1; i < argc; i++, argv++) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600922 id = (int)hextoul(argv[1], &endp);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900923 if (*endp != '\0' || id > 0xffff)
924 return CMD_RET_FAILURE;
925
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +0200926 efi_create_indexed_name(var_name16, sizeof(var_name16),
927 "Boot", id);
Heinrich Schuchardtacfe1de2021-05-24 11:10:59 +0200928 ret = efi_set_variable_int(var_name16, &guid, 0, 0, NULL,
929 false);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900930 if (ret) {
Heinrich Schuchardt30efb5d2020-03-02 20:13:10 +0100931 printf("Cannot remove %ls\n", var_name16);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900932 return CMD_RET_FAILURE;
933 }
934 }
935
936 return CMD_RET_SUCCESS;
937}
938
939/**
940 * show_efi_boot_opt_data() - dump UEFI load option
941 *
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200942 * @varname16: variable name
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200943 * @data: value of UEFI load option variable
944 * @size: size of the boot option
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900945 *
946 * Decode the value of UEFI load option variable and print information.
947 */
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +0200948static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900949{
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200950 struct efi_device_path *initrd_path = NULL;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900951 struct efi_load_option lo;
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +0200952 efi_status_t ret;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900953
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +0200954 ret = efi_deserialize_load_option(&lo, data, size);
955 if (ret != EFI_SUCCESS) {
956 printf("%ls: invalid load option\n", varname16);
957 return;
958 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900959
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200960 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
961 varname16,
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900962 /* ACTIVE */
963 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
964 /* FORCE RECONNECT */
965 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
966 /* HIDDEN */
967 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
968 lo.attributes);
Heinrich Schuchardtcd5a87e2021-05-24 10:35:25 +0200969 printf(" label: %ls\n", lo.label);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900970
Heinrich Schuchardtfc42b8b2021-10-15 01:47:40 +0200971 printf(" file_path: %pD\n", lo.file_path);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900972
Heinrich Schuchardt9ad37fe2021-10-15 02:33:33 +0200973 initrd_path = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200974 if (initrd_path) {
Heinrich Schuchardtfc42b8b2021-10-15 01:47:40 +0200975 printf(" initrd_path: %pD\n", initrd_path);
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200976 efi_free_pool(initrd_path);
977 }
978
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200979 printf(" data:\n");
980 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +0200981 lo.optional_data, *size, true);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900982}
983
984/**
985 * show_efi_boot_opt() - dump UEFI load option
986 *
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200987 * @varname16: variable name
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900988 *
989 * Dump information defined by UEFI load option.
990 */
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200991static void show_efi_boot_opt(u16 *varname16)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900992{
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200993 void *data;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900994 efi_uintn_t size;
AKASHI Takahiro0bffb8c2019-11-26 10:11:22 +0900995 efi_status_t ret;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900996
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900997 size = 0;
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +0200998 ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
999 NULL, &size, NULL));
AKASHI Takahiro0bffb8c2019-11-26 10:11:22 +09001000 if (ret == EFI_BUFFER_TOO_SMALL) {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001001 data = malloc(size);
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001002 if (!data) {
1003 printf("ERROR: Out of memory\n");
1004 return;
1005 }
1006 ret = EFI_CALL(efi_get_variable(varname16,
1007 &efi_global_variable_guid,
1008 NULL, &size, data));
1009 if (ret == EFI_SUCCESS)
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +02001010 show_efi_boot_opt_data(varname16, data, &size);
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001011 free(data);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001012 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001013}
1014
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001015static int u16_tohex(u16 c)
1016{
1017 if (c >= '0' && c <= '9')
1018 return c - '0';
1019 if (c >= 'A' && c <= 'F')
1020 return c - 'A' + 10;
1021
1022 /* not hexadecimal */
1023 return -1;
1024}
1025
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001026/**
1027 * show_efi_boot_dump() - dump all UEFI load options
1028 *
1029 * @cmdtp: Command table
1030 * @flag: Command flag
1031 * @argc: Number of arguments
1032 * @argv: Argument array
1033 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1034 *
1035 * Implement efidebug "boot dump" sub-command.
1036 * Dump information of all UEFI load options defined.
Heinrich Schuchardta6ccba02019-07-25 20:52:23 +02001037 *
1038 * efidebug boot dump
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001039 */
Simon Glass09140112020-05-10 11:40:03 -06001040static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
1041 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001042{
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001043 u16 *var_name16, *p;
1044 efi_uintn_t buf_size, size;
1045 efi_guid_t guid;
1046 int id, i, digit;
1047 efi_status_t ret;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001048
1049 if (argc > 1)
1050 return CMD_RET_USAGE;
1051
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001052 buf_size = 128;
1053 var_name16 = malloc(buf_size);
1054 if (!var_name16)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001055 return CMD_RET_FAILURE;
1056
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001057 var_name16[0] = 0;
1058 for (;;) {
1059 size = buf_size;
1060 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
1061 &guid));
1062 if (ret == EFI_NOT_FOUND)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001063 break;
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001064 if (ret == EFI_BUFFER_TOO_SMALL) {
1065 buf_size = size;
1066 p = realloc(var_name16, buf_size);
1067 if (!p) {
1068 free(var_name16);
1069 return CMD_RET_FAILURE;
1070 }
1071 var_name16 = p;
1072 ret = EFI_CALL(efi_get_next_variable_name(&size,
1073 var_name16,
1074 &guid));
1075 }
1076 if (ret != EFI_SUCCESS) {
1077 free(var_name16);
1078 return CMD_RET_FAILURE;
1079 }
1080
Simon Glass156ccbc2022-01-23 12:55:12 -07001081 if (memcmp(var_name16, u"Boot", 8))
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001082 continue;
1083
1084 for (id = 0, i = 0; i < 4; i++) {
1085 digit = u16_tohex(var_name16[4 + i]);
1086 if (digit < 0)
1087 break;
1088 id = (id << 4) + digit;
1089 }
1090 if (i == 4 && !var_name16[8])
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001091 show_efi_boot_opt(var_name16);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001092 }
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001093
1094 free(var_name16);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001095
1096 return CMD_RET_SUCCESS;
1097}
1098
1099/**
1100 * show_efi_boot_order() - show order of UEFI load options
1101 *
1102 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1103 *
1104 * Show order of UEFI load options defined by BootOrder variable.
1105 */
1106static int show_efi_boot_order(void)
1107{
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001108 u16 *bootorder;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001109 efi_uintn_t size;
1110 int num, i;
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +02001111 u16 var_name16[9];
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001112 void *data;
1113 struct efi_load_option lo;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001114 efi_status_t ret;
1115
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001116 size = 0;
Simon Glass156ccbc2022-01-23 12:55:12 -07001117 ret = EFI_CALL(efi_get_variable(u"BootOrder", &efi_global_variable_guid,
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001118 NULL, &size, NULL));
1119 if (ret != EFI_BUFFER_TOO_SMALL) {
1120 if (ret == EFI_NOT_FOUND) {
1121 printf("BootOrder not defined\n");
1122 return CMD_RET_SUCCESS;
1123 } else {
1124 return CMD_RET_FAILURE;
1125 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001126 }
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001127 bootorder = malloc(size);
1128 if (!bootorder) {
1129 printf("ERROR: Out of memory\n");
1130 return CMD_RET_FAILURE;
1131 }
Simon Glass156ccbc2022-01-23 12:55:12 -07001132 ret = EFI_CALL(efi_get_variable(u"BootOrder", &efi_global_variable_guid,
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001133 NULL, &size, bootorder));
1134 if (ret != EFI_SUCCESS) {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001135 ret = CMD_RET_FAILURE;
1136 goto out;
1137 }
1138
1139 num = size / sizeof(u16);
1140 for (i = 0; i < num; i++) {
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +02001141 efi_create_indexed_name(var_name16, sizeof(var_name16),
Heinrich Schuchardt2b8723c2021-06-12 00:01:44 +02001142 "Boot", bootorder[i]);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001143
1144 size = 0;
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001145 ret = EFI_CALL(efi_get_variable(var_name16,
1146 &efi_global_variable_guid, NULL,
1147 &size, NULL));
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001148 if (ret != EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +02001149 printf("%2d: %ls: (not defined)\n", i + 1, var_name16);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001150 continue;
1151 }
1152
1153 data = malloc(size);
1154 if (!data) {
1155 ret = CMD_RET_FAILURE;
1156 goto out;
1157 }
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001158 ret = EFI_CALL(efi_get_variable(var_name16,
1159 &efi_global_variable_guid, NULL,
1160 &size, data));
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001161 if (ret != EFI_SUCCESS) {
1162 free(data);
1163 ret = CMD_RET_FAILURE;
1164 goto out;
1165 }
1166
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +02001167 ret = efi_deserialize_load_option(&lo, data, &size);
1168 if (ret != EFI_SUCCESS) {
1169 printf("%ls: invalid load option\n", var_name16);
1170 ret = CMD_RET_FAILURE;
1171 goto out;
1172 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001173
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +02001174 printf("%2d: %ls: %ls\n", i + 1, var_name16, lo.label);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001175
1176 free(data);
1177 }
1178out:
1179 free(bootorder);
1180
1181 return ret;
1182}
1183
1184/**
1185 * do_efi_boot_next() - manage UEFI BootNext variable
1186 *
1187 * @cmdtp: Command table
1188 * @flag: Command flag
1189 * @argc: Number of arguments
1190 * @argv: Argument array
1191 * Return: CMD_RET_SUCCESS on success,
1192 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1193 *
1194 * Implement efidebug "boot next" sub-command.
1195 * Set BootNext variable.
Heinrich Schuchardt0b016562019-07-14 14:00:41 +02001196 *
1197 * efidebug boot next <id>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001198 */
Simon Glass09140112020-05-10 11:40:03 -06001199static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
1200 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001201{
1202 u16 bootnext;
1203 efi_uintn_t size;
1204 char *endp;
1205 efi_guid_t guid;
1206 efi_status_t ret;
Heinrich Schuchardta332f252019-06-20 12:59:45 +02001207 int r = CMD_RET_SUCCESS;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001208
1209 if (argc != 2)
1210 return CMD_RET_USAGE;
1211
Simon Glass7e5f4602021-07-24 09:03:29 -06001212 bootnext = (u16)hextoul(argv[1], &endp);
Heinrich Schuchardtbdb15772020-05-09 17:59:19 +02001213 if (*endp) {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001214 printf("invalid value: %s\n", argv[1]);
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001215 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001216 goto out;
1217 }
1218
1219 guid = efi_global_variable_guid;
1220 size = sizeof(u16);
Simon Glass156ccbc2022-01-23 12:55:12 -07001221 ret = efi_set_variable_int(u"BootNext", &guid,
1222 EFI_VARIABLE_NON_VOLATILE |
1223 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1224 EFI_VARIABLE_RUNTIME_ACCESS,
1225 size, &bootnext, false);
Heinrich Schuchardta332f252019-06-20 12:59:45 +02001226 if (ret != EFI_SUCCESS) {
1227 printf("Cannot set BootNext\n");
1228 r = CMD_RET_FAILURE;
1229 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001230out:
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001231 return r;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001232}
1233
1234/**
1235 * do_efi_boot_order() - manage UEFI BootOrder variable
1236 *
1237 * @cmdtp: Command table
1238 * @flag: Command flag
1239 * @argc: Number of arguments
1240 * @argv: Argument array
1241 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1242 *
1243 * Implement efidebug "boot order" sub-command.
1244 * Show order of UEFI load options, or change it in BootOrder variable.
Heinrich Schuchardt0b016562019-07-14 14:00:41 +02001245 *
1246 * efidebug boot order [<id> ...]
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001247 */
Simon Glass09140112020-05-10 11:40:03 -06001248static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
1249 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001250{
1251 u16 *bootorder = NULL;
1252 efi_uintn_t size;
1253 int id, i;
1254 char *endp;
1255 efi_guid_t guid;
1256 efi_status_t ret;
Heinrich Schuchardta332f252019-06-20 12:59:45 +02001257 int r = CMD_RET_SUCCESS;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001258
1259 if (argc == 1)
1260 return show_efi_boot_order();
1261
1262 argc--;
1263 argv++;
1264
1265 size = argc * sizeof(u16);
1266 bootorder = malloc(size);
1267 if (!bootorder)
1268 return CMD_RET_FAILURE;
1269
1270 for (i = 0; i < argc; i++) {
Simon Glass7e5f4602021-07-24 09:03:29 -06001271 id = (int)hextoul(argv[i], &endp);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001272 if (*endp != '\0' || id > 0xffff) {
1273 printf("invalid value: %s\n", argv[i]);
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001274 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001275 goto out;
1276 }
1277
1278 bootorder[i] = (u16)id;
1279 }
1280
1281 guid = efi_global_variable_guid;
Simon Glass156ccbc2022-01-23 12:55:12 -07001282 ret = efi_set_variable_int(u"BootOrder", &guid,
1283 EFI_VARIABLE_NON_VOLATILE |
1284 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1285 EFI_VARIABLE_RUNTIME_ACCESS,
1286 size, bootorder, true);
Heinrich Schuchardta332f252019-06-20 12:59:45 +02001287 if (ret != EFI_SUCCESS) {
1288 printf("Cannot set BootOrder\n");
1289 r = CMD_RET_FAILURE;
1290 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001291out:
1292 free(bootorder);
1293
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001294 return r;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001295}
1296
Simon Glass09140112020-05-10 11:40:03 -06001297static struct cmd_tbl cmd_efidebug_boot_sub[] = {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001298 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1299 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1300 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1301 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1302 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1303 "", ""),
1304};
1305
1306/**
1307 * do_efi_boot_opt() - manage UEFI load options
1308 *
1309 * @cmdtp: Command table
1310 * @flag: Command flag
1311 * @argc: Number of arguments
1312 * @argv: Argument array
1313 * Return: CMD_RET_SUCCESS on success,
1314 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1315 *
1316 * Implement efidebug "boot" sub-command.
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001317 */
Simon Glass09140112020-05-10 11:40:03 -06001318static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
1319 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001320{
Simon Glass09140112020-05-10 11:40:03 -06001321 struct cmd_tbl *cp;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001322
1323 if (argc < 2)
1324 return CMD_RET_USAGE;
1325
1326 argc--; argv++;
1327
1328 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1329 ARRAY_SIZE(cmd_efidebug_boot_sub));
1330 if (!cp)
1331 return CMD_RET_USAGE;
1332
1333 return cp->cmd(cmdtp, flag, argc, argv);
1334}
1335
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001336/**
1337 * do_efi_test_bootmgr() - run simple bootmgr for test
1338 *
1339 * @cmdtp: Command table
1340 * @flag: Command flag
1341 * @argc: Number of arguments
1342 * @argv: Argument array
1343 * Return: CMD_RET_SUCCESS on success,
1344 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1345 *
1346 * Implement efidebug "test bootmgr" sub-command.
1347 * Run simple bootmgr for test.
1348 *
1349 * efidebug test bootmgr
1350 */
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +01001351static __maybe_unused int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
1352 int argc, char * const argv[])
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001353{
1354 efi_handle_t image;
1355 efi_uintn_t exit_data_size = 0;
1356 u16 *exit_data = NULL;
1357 efi_status_t ret;
Heinrich Schuchardtbc78d222020-08-11 18:20:50 +02001358 void *load_options = NULL;
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001359
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +02001360 ret = efi_bootmgr_load(&image, &load_options);
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001361 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1362
1363 /* We call efi_start_image() even if error for test purpose. */
1364 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1365 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1366 if (ret && exit_data)
1367 efi_free_pool(exit_data);
1368
1369 efi_restore_gd();
1370
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +02001371 free(load_options);
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001372 return CMD_RET_SUCCESS;
1373}
1374
Simon Glass09140112020-05-10 11:40:03 -06001375static struct cmd_tbl cmd_efidebug_test_sub[] = {
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +01001376#ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001377 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1378 "", ""),
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +01001379#endif
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001380};
1381
1382/**
1383 * do_efi_test() - manage UEFI load options
1384 *
1385 * @cmdtp: Command table
1386 * @flag: Command flag
1387 * @argc: Number of arguments
1388 * @argv: Argument array
1389 * Return: CMD_RET_SUCCESS on success,
1390 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1391 *
1392 * Implement efidebug "test" sub-command.
1393 */
Simon Glass09140112020-05-10 11:40:03 -06001394static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001395 int argc, char * const argv[])
1396{
Simon Glass09140112020-05-10 11:40:03 -06001397 struct cmd_tbl *cp;
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001398
1399 if (argc < 2)
1400 return CMD_RET_USAGE;
1401
1402 argc--; argv++;
1403
1404 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1405 ARRAY_SIZE(cmd_efidebug_test_sub));
1406 if (!cp)
1407 return CMD_RET_USAGE;
1408
1409 return cp->cmd(cmdtp, flag, argc, argv);
1410}
1411
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001412/**
1413 * do_efi_query_info() - QueryVariableInfo EFI service
1414 *
1415 * @cmdtp: Command table
1416 * @flag: Command flag
1417 * @argc: Number of arguments
1418 * @argv: Argument array
1419 * Return: CMD_RET_SUCCESS on success,
1420 * CMD_RET_USAGE or CMD_RET_FAILURE on failure
1421 *
1422 * Implement efidebug "test" sub-command.
1423 */
1424
Simon Glass09140112020-05-10 11:40:03 -06001425static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001426 int argc, char * const argv[])
1427{
1428 efi_status_t ret;
1429 u32 attr = 0;
1430 u64 max_variable_storage_size;
1431 u64 remain_variable_storage_size;
1432 u64 max_variable_size;
1433 int i;
1434
1435 for (i = 1; i < argc; i++) {
1436 if (!strcmp(argv[i], "-bs"))
1437 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
1438 else if (!strcmp(argv[i], "-rt"))
1439 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
1440 else if (!strcmp(argv[i], "-nv"))
1441 attr |= EFI_VARIABLE_NON_VOLATILE;
1442 else if (!strcmp(argv[i], "-at"))
1443 attr |=
1444 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1445 }
1446
1447 ret = EFI_CALL(efi_query_variable_info(attr,
1448 &max_variable_storage_size,
1449 &remain_variable_storage_size,
1450 &max_variable_size));
1451 if (ret != EFI_SUCCESS) {
1452 printf("Error: Cannot query UEFI variables, r = %lu\n",
1453 ret & ~EFI_ERROR_MASK);
1454 return CMD_RET_FAILURE;
1455 }
1456
1457 printf("Max storage size %llu\n", max_variable_storage_size);
1458 printf("Remaining storage size %llu\n", remain_variable_storage_size);
1459 printf("Max variable size %llu\n", max_variable_size);
1460
1461 return CMD_RET_SUCCESS;
1462}
1463
Simon Glass09140112020-05-10 11:40:03 -06001464static struct cmd_tbl cmd_efidebug_sub[] = {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001465 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +09001466#ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1467 U_BOOT_CMD_MKENT(capsule, CONFIG_SYS_MAXARGS, 1, do_efi_capsule,
1468 "", ""),
1469#endif
AKASHI Takahiro66eaf562019-02-25 15:54:40 +09001470 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1471 "", ""),
AKASHI Takahiroa8014622019-02-25 15:54:41 +09001472 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1473 "", ""),
AKASHI Takahirofa536732019-02-25 15:54:42 +09001474 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1475 "", ""),
AKASHI Takahiro00358bb2019-02-25 15:54:43 +09001476 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1477 "", ""),
Heinrich Schuchardt986e0642020-01-07 05:57:47 +01001478 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1479 "", ""),
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001480 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1481 "", ""),
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001482 U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
1483 "", ""),
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001484};
1485
1486/**
1487 * do_efidebug() - display and configure UEFI environment
1488 *
1489 * @cmdtp: Command table
1490 * @flag: Command flag
1491 * @argc: Number of arguments
1492 * @argv: Argument array
1493 * Return: CMD_RET_SUCCESS on success,
1494 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1495 *
1496 * Implement efidebug command which allows us to display and
1497 * configure UEFI environment.
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001498 */
Simon Glass09140112020-05-10 11:40:03 -06001499static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
1500 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001501{
Simon Glass09140112020-05-10 11:40:03 -06001502 struct cmd_tbl *cp;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001503 efi_status_t r;
1504
1505 if (argc < 2)
1506 return CMD_RET_USAGE;
1507
1508 argc--; argv++;
1509
1510 /* Initialize UEFI drivers */
1511 r = efi_init_obj_list();
1512 if (r != EFI_SUCCESS) {
1513 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1514 r & ~EFI_ERROR_MASK);
1515 return CMD_RET_FAILURE;
1516 }
1517
1518 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1519 ARRAY_SIZE(cmd_efidebug_sub));
1520 if (!cp)
1521 return CMD_RET_USAGE;
1522
1523 return cp->cmd(cmdtp, flag, argc, argv);
1524}
1525
1526#ifdef CONFIG_SYS_LONGHELP
1527static char efidebug_help_text[] =
1528 " - UEFI Shell-like interface to configure UEFI environment\n"
1529 "\n"
Heinrich Schuchardt63276a52022-02-26 12:10:10 +01001530 "efidebug boot add - set UEFI BootXXXX variable\n"
1531 " -b|-B <bootid> <label> <interface> <devnum>[:<part>] <file path>\n"
1532 " -i|-I <interface> <devnum>[:<part>] <initrd file path>\n"
1533 " (-b, -i for short form device path)\n"
1534 " -s '<optional data>'\n"
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001535 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1536 " - delete UEFI BootXXXX variables\n"
1537 "efidebug boot dump\n"
1538 " - dump all UEFI BootXXXX variables\n"
1539 "efidebug boot next <bootid>\n"
1540 " - set UEFI BootNext variable\n"
1541 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1542 " - set/show UEFI boot order\n"
AKASHI Takahiro355cdb52019-02-25 15:54:39 +09001543 "\n"
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +09001544#ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1545 "efidebug capsule update [-v] <capsule address>\n"
1546 " - process a capsule\n"
Sughosh Ganu74075952020-12-30 19:27:11 +05301547 "efidebug capsule disk-update\n"
1548 " - update a capsule from disk\n"
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +09001549 "efidebug capsule show <capsule address>\n"
1550 " - show capsule information\n"
1551 "efidebug capsule result [<capsule result var>]\n"
1552 " - show a capsule update result\n"
Jose Marinhoaa31a872021-03-11 13:18:51 +00001553#ifdef CONFIG_EFI_ESRT
1554 "efidebug capsule esrt\n"
1555 " - print the ESRT\n"
1556#endif
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +09001557 "\n"
1558#endif
AKASHI Takahiro66eaf562019-02-25 15:54:40 +09001559 "efidebug drivers\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001560 " - show UEFI drivers\n"
AKASHI Takahiroa8014622019-02-25 15:54:41 +09001561 "efidebug dh\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001562 " - show UEFI handles\n"
AKASHI Takahirofa536732019-02-25 15:54:42 +09001563 "efidebug images\n"
AKASHI Takahiro00358bb2019-02-25 15:54:43 +09001564 " - show loaded images\n"
1565 "efidebug memmap\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001566 " - show UEFI memory map\n"
Heinrich Schuchardt986e0642020-01-07 05:57:47 +01001567 "efidebug tables\n"
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001568 " - show UEFI configuration tables\n"
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +01001569#ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001570 "efidebug test bootmgr\n"
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001571 " - run simple bootmgr for test\n"
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +01001572#endif
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001573 "efidebug query [-nv][-bs][-rt][-at]\n"
1574 " - show size of UEFI variables store\n";
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001575#endif
1576
1577U_BOOT_CMD(
Ilias Apalodimascbea2412021-03-17 21:55:01 +02001578 efidebug, CONFIG_SYS_MAXARGS, 0, do_efidebug,
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001579 "Configure UEFI environment",
1580 efidebug_help_text
1581);