blob: 9622430c475ed897509d2a10fb474e9f4f8ca2d8 [file] [log] [blame]
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * UEFI Shell-like command
4 *
5 * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited
6 */
7
8#include <charset.h>
9#include <common.h>
10#include <command.h>
Heinrich Schuchardtd16463a2022-10-04 15:31:17 +020011#include <dm/device.h>
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +010012#include <efi_dt_fixup.h>
Ilias Apalodimas773c0902021-03-17 21:55:01 +020013#include <efi_load_initrd.h>
AKASHI Takahiroe7c08832019-02-25 15:54:38 +090014#include <efi_loader.h>
Heinrich Schuchardt50adeff2020-09-27 14:50:25 +020015#include <efi_rng.h>
Heinrich Schuchardte8324f52021-05-24 11:10:59 +020016#include <efi_variable.h>
AKASHI Takahiroe7c08832019-02-25 15:54:38 +090017#include <exports.h>
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +020018#include <hexdump.h>
Simon Glass0f2af882020-05-10 11:40:05 -060019#include <log.h>
AKASHI Takahiroe7c08832019-02-25 15:54:38 +090020#include <malloc.h>
Heinrich Schuchardtfec10a42020-03-14 08:44:07 +010021#include <mapmem.h>
Heinrich Schuchardt26397622021-02-02 17:53:14 +010022#include <part.h>
AKASHI Takahiroe7c08832019-02-25 15:54:38 +090023#include <search.h>
24#include <linux/ctype.h>
Ilias Apalodimas773c0902021-03-17 21:55:01 +020025#include <linux/err.h>
AKASHI Takahiroe7c08832019-02-25 15:54:38 +090026
AKASHI Takahiroe32788a2019-02-25 15:54:39 +090027#define BS systab.boottime
AKASHI Takahiroe7c08832019-02-25 15:54:38 +090028
AKASHI Takahiro38833c82020-11-17 09:28:01 +090029#ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
30/**
31 * do_efi_capsule_update() - process a capsule update
32 *
33 * @cmdtp: Command table
34 * @flag: Command flag
35 * @argc: Number of arguments
36 * @argv: Argument array
37 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
38 *
39 * Implement efidebug "capsule update" sub-command.
40 * process a capsule update.
41 *
42 * efidebug capsule update [-v] <capsule address>
43 */
44static int do_efi_capsule_update(struct cmd_tbl *cmdtp, int flag,
45 int argc, char * const argv[])
46{
47 struct efi_capsule_header *capsule;
48 int verbose = 0;
49 char *endp;
50 efi_status_t ret;
51
52 if (argc != 2 && argc != 3)
53 return CMD_RET_USAGE;
54
55 if (argc == 3) {
56 if (strcmp(argv[1], "-v"))
57 return CMD_RET_USAGE;
58
59 verbose = 1;
60 argc--;
61 argv++;
62 }
63
Simon Glass3ff49ec2021-07-24 09:03:29 -060064 capsule = (typeof(capsule))hextoul(argv[1], &endp);
AKASHI Takahiro38833c82020-11-17 09:28:01 +090065 if (endp == argv[1]) {
66 printf("Invalid address: %s", argv[1]);
67 return CMD_RET_FAILURE;
68 }
69
70 if (verbose) {
71 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
72 printf("Capsule flags: 0x%x\n", capsule->flags);
73 printf("Capsule header size: 0x%x\n", capsule->header_size);
74 printf("Capsule image size: 0x%x\n",
75 capsule->capsule_image_size);
76 }
77
Heinrich Schuchardtf3120872022-10-15 13:21:01 +020078 ret = EFI_CALL(efi_update_capsule(&capsule, 1, 0));
AKASHI Takahiro38833c82020-11-17 09:28:01 +090079 if (ret) {
Michal Simekecb7ce52022-08-09 16:31:12 +020080 printf("Cannot handle a capsule at %p\n", capsule);
AKASHI Takahiro38833c82020-11-17 09:28:01 +090081 return CMD_RET_FAILURE;
82 }
83
84 return CMD_RET_SUCCESS;
85}
86
Masami Hiramatsufc1f85e2022-03-21 22:37:35 +090087#ifdef CONFIG_EFI_CAPSULE_ON_DISK
Sughosh Ganue6f9efc2020-12-30 19:27:11 +053088static int do_efi_capsule_on_disk_update(struct cmd_tbl *cmdtp, int flag,
89 int argc, char * const argv[])
90{
91 efi_status_t ret;
92
93 ret = efi_launch_capsules();
94
95 return ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
96}
Masami Hiramatsufc1f85e2022-03-21 22:37:35 +090097#endif
Sughosh Ganue6f9efc2020-12-30 19:27:11 +053098
AKASHI Takahiro38833c82020-11-17 09:28:01 +090099/**
100 * do_efi_capsule_show() - show capsule information
101 *
102 * @cmdtp: Command table
103 * @flag: Command flag
104 * @argc: Number of arguments
105 * @argv: Argument array
106 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
107 *
108 * Implement efidebug "capsule show" sub-command.
109 * show capsule information.
110 *
111 * efidebug capsule show <capsule address>
112 */
113static int do_efi_capsule_show(struct cmd_tbl *cmdtp, int flag,
114 int argc, char * const argv[])
115{
116 struct efi_capsule_header *capsule;
117 char *endp;
118
119 if (argc != 2)
120 return CMD_RET_USAGE;
121
Simon Glass3ff49ec2021-07-24 09:03:29 -0600122 capsule = (typeof(capsule))hextoul(argv[1], &endp);
AKASHI Takahiro38833c82020-11-17 09:28:01 +0900123 if (endp == argv[1]) {
124 printf("Invalid address: %s", argv[1]);
125 return CMD_RET_FAILURE;
126 }
127
128 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
129 printf("Capsule flags: 0x%x\n", capsule->flags);
130 printf("Capsule header size: 0x%x\n", capsule->header_size);
131 printf("Capsule image size: 0x%x\n",
132 capsule->capsule_image_size);
133
134 return CMD_RET_SUCCESS;
135}
136
Jose Marinhob027e792021-03-11 13:18:51 +0000137#ifdef CONFIG_EFI_ESRT
138
139#define EFI_ESRT_FW_TYPE_NUM 4
140char *efi_fw_type_str[EFI_ESRT_FW_TYPE_NUM] = {"unknown", "system FW", "device FW",
141 "UEFI driver"};
142
143#define EFI_ESRT_UPDATE_STATUS_NUM 9
144char *efi_update_status_str[EFI_ESRT_UPDATE_STATUS_NUM] = {"success", "unsuccessful",
145 "insufficient resources", "incorrect version", "invalid format",
146 "auth error", "power event (AC)", "power event (batt)",
147 "unsatisfied dependencies"};
148
149#define EFI_FW_TYPE_STR_GET(idx) (\
150EFI_ESRT_FW_TYPE_NUM > (idx) ? efi_fw_type_str[(idx)] : "error"\
151)
152
153#define EFI_FW_STATUS_STR_GET(idx) (\
154EFI_ESRT_UPDATE_STATUS_NUM > (idx) ? efi_update_status_str[(idx)] : "error"\
155)
156
157/**
158 * do_efi_capsule_esrt() - manage UEFI capsules
159 *
160 * @cmdtp: Command table
161 * @flag: Command flag
162 * @argc: Number of arguments
163 * @argv: Argument array
164 * Return: CMD_RET_SUCCESS on success,
165 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
166 *
167 * Implement efidebug "capsule esrt" sub-command.
168 * The prints the current ESRT table.
169 *
170 * efidebug capsule esrt
171 */
172static int do_efi_capsule_esrt(struct cmd_tbl *cmdtp, int flag,
173 int argc, char * const argv[])
174{
175 struct efi_system_resource_table *esrt = NULL;
176
177 if (argc != 1)
178 return CMD_RET_USAGE;
179
180 for (int idx = 0; idx < systab.nr_tables; idx++)
181 if (!guidcmp(&efi_esrt_guid, &systab.tables[idx].guid))
182 esrt = (struct efi_system_resource_table *)systab.tables[idx].table;
183
184 if (!esrt) {
185 log_info("ESRT: table not present\n");
186 return CMD_RET_SUCCESS;
187 }
188
189 printf("========================================\n");
190 printf("ESRT: fw_resource_count=%d\n", esrt->fw_resource_count);
191 printf("ESRT: fw_resource_count_max=%d\n", esrt->fw_resource_count_max);
192 printf("ESRT: fw_resource_version=%lld\n", esrt->fw_resource_version);
193
194 for (int idx = 0; idx < esrt->fw_resource_count; idx++) {
195 printf("[entry %d]==============================\n", idx);
196 printf("ESRT: fw_class=%pUL\n", &esrt->entries[idx].fw_class);
197 printf("ESRT: fw_type=%s\n", EFI_FW_TYPE_STR_GET(esrt->entries[idx].fw_type));
198 printf("ESRT: fw_version=%d\n", esrt->entries[idx].fw_version);
199 printf("ESRT: lowest_supported_fw_version=%d\n",
200 esrt->entries[idx].lowest_supported_fw_version);
201 printf("ESRT: capsule_flags=%d\n",
202 esrt->entries[idx].capsule_flags);
203 printf("ESRT: last_attempt_version=%d\n",
204 esrt->entries[idx].last_attempt_version);
205 printf("ESRT: last_attempt_status=%s\n",
206 EFI_FW_STATUS_STR_GET(esrt->entries[idx].last_attempt_status));
207 }
208 printf("========================================\n");
209
210 return CMD_RET_SUCCESS;
211}
212#endif /* CONFIG_EFI_ESRT */
AKASHI Takahiro38833c82020-11-17 09:28:01 +0900213/**
214 * do_efi_capsule_res() - show a capsule update result
215 *
216 * @cmdtp: Command table
217 * @flag: Command flag
218 * @argc: Number of arguments
219 * @argv: Argument array
220 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
221 *
222 * Implement efidebug "capsule result" sub-command.
223 * show a capsule update result.
224 * If result number is not specified, CapsuleLast will be shown.
225 *
226 * efidebug capsule result [<capsule result number>]
227 */
228static int do_efi_capsule_res(struct cmd_tbl *cmdtp, int flag,
229 int argc, char * const argv[])
230{
231 int capsule_id;
232 char *endp;
Heinrich Schuchardt9a5e74e2021-05-24 10:57:03 +0200233 u16 var_name16[12];
AKASHI Takahiro38833c82020-11-17 09:28:01 +0900234 efi_guid_t guid;
235 struct efi_capsule_result_variable_header *result = NULL;
236 efi_uintn_t size;
237 efi_status_t ret;
238
239 if (argc != 1 && argc != 2)
240 return CMD_RET_USAGE;
241
242 guid = efi_guid_capsule_report;
243 if (argc == 1) {
244 size = sizeof(var_name16);
Simon Glass90975372022-01-23 12:55:12 -0700245 ret = efi_get_variable_int(u"CapsuleLast", &guid, NULL,
Heinrich Schuchardte8324f52021-05-24 11:10:59 +0200246 &size, var_name16, NULL);
247
AKASHI Takahiro38833c82020-11-17 09:28:01 +0900248 if (ret != EFI_SUCCESS) {
249 if (ret == EFI_NOT_FOUND)
250 printf("CapsuleLast doesn't exist\n");
251 else
252 printf("Failed to get CapsuleLast\n");
253
254 return CMD_RET_FAILURE;
255 }
256 printf("CapsuleLast is %ls\n", var_name16);
257 } else {
258 argc--;
259 argv++;
260
Simon Glass3ff49ec2021-07-24 09:03:29 -0600261 capsule_id = hextoul(argv[0], &endp);
AKASHI Takahiro38833c82020-11-17 09:28:01 +0900262 if (capsule_id < 0 || capsule_id > 0xffff)
263 return CMD_RET_USAGE;
264
Heinrich Schuchardt9a5e74e2021-05-24 10:57:03 +0200265 efi_create_indexed_name(var_name16, sizeof(var_name16),
266 "Capsule", capsule_id);
AKASHI Takahiro38833c82020-11-17 09:28:01 +0900267 }
268
269 size = 0;
Heinrich Schuchardte8324f52021-05-24 11:10:59 +0200270 ret = efi_get_variable_int(var_name16, &guid, NULL, &size, NULL, NULL);
AKASHI Takahiro38833c82020-11-17 09:28:01 +0900271 if (ret == EFI_BUFFER_TOO_SMALL) {
272 result = malloc(size);
AKASHI Takahiro1af16732021-01-22 10:42:48 +0900273 if (!result)
274 return CMD_RET_FAILURE;
Heinrich Schuchardte8324f52021-05-24 11:10:59 +0200275 ret = efi_get_variable_int(var_name16, &guid, NULL, &size,
276 result, NULL);
AKASHI Takahiro1af16732021-01-22 10:42:48 +0900277 }
278 if (ret != EFI_SUCCESS) {
279 free(result);
280 printf("Failed to get %ls\n", var_name16);
AKASHI Takahiro38833c82020-11-17 09:28:01 +0900281
AKASHI Takahiro1af16732021-01-22 10:42:48 +0900282 return CMD_RET_FAILURE;
AKASHI Takahiro38833c82020-11-17 09:28:01 +0900283 }
284
285 printf("Result total size: 0x%x\n", result->variable_total_size);
286 printf("Capsule guid: %pUl\n", &result->capsule_guid);
287 printf("Time processed: %04d-%02d-%02d %02d:%02d:%02d\n",
288 result->capsule_processed.year, result->capsule_processed.month,
289 result->capsule_processed.day, result->capsule_processed.hour,
290 result->capsule_processed.minute,
291 result->capsule_processed.second);
292 printf("Capsule status: 0x%lx\n", result->capsule_status);
293
294 free(result);
295
296 return CMD_RET_SUCCESS;
297}
298
299static struct cmd_tbl cmd_efidebug_capsule_sub[] = {
300 U_BOOT_CMD_MKENT(update, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_update,
301 "", ""),
302 U_BOOT_CMD_MKENT(show, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_show,
303 "", ""),
Jose Marinhob027e792021-03-11 13:18:51 +0000304#ifdef CONFIG_EFI_ESRT
305 U_BOOT_CMD_MKENT(esrt, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_esrt,
306 "", ""),
307#endif
Masami Hiramatsufc1f85e2022-03-21 22:37:35 +0900308#ifdef CONFIG_EFI_CAPSULE_ON_DISK
Sughosh Ganue6f9efc2020-12-30 19:27:11 +0530309 U_BOOT_CMD_MKENT(disk-update, 0, 0, do_efi_capsule_on_disk_update,
310 "", ""),
Masami Hiramatsufc1f85e2022-03-21 22:37:35 +0900311#endif
AKASHI Takahiro38833c82020-11-17 09:28:01 +0900312 U_BOOT_CMD_MKENT(result, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_res,
313 "", ""),
314};
315
316/**
317 * do_efi_capsule() - manage UEFI capsules
318 *
319 * @cmdtp: Command table
320 * @flag: Command flag
321 * @argc: Number of arguments
322 * @argv: Argument array
323 * Return: CMD_RET_SUCCESS on success,
324 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
325 *
326 * Implement efidebug "capsule" sub-command.
327 */
328static int do_efi_capsule(struct cmd_tbl *cmdtp, int flag,
329 int argc, char * const argv[])
330{
331 struct cmd_tbl *cp;
332
333 if (argc < 2)
334 return CMD_RET_USAGE;
335
336 argc--; argv++;
337
338 cp = find_cmd_tbl(argv[0], cmd_efidebug_capsule_sub,
339 ARRAY_SIZE(cmd_efidebug_capsule_sub));
340 if (!cp)
341 return CMD_RET_USAGE;
342
343 return cp->cmd(cmdtp, flag, argc, argv);
344}
345#endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT */
346
AKASHI Takahiroe32788a2019-02-25 15:54:39 +0900347#define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
348
349static const char spc[] = " ";
350static const char sep[] = "================";
351
352/**
AKASHI Takahiro185716a2019-02-25 15:54:40 +0900353 * efi_get_driver_handle_info() - get information of UEFI driver
354 *
355 * @handle: Handle of UEFI device
356 * @driver_name: Driver name
357 * @image_path: Pointer to text of device path
358 * Return: 0 on success, -1 on failure
359 *
360 * Currently return no useful information as all UEFI drivers are
361 * built-in..
362 */
363static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
364 u16 **image_path)
365{
366 struct efi_handler *handler;
367 struct efi_loaded_image *image;
368 efi_status_t ret;
369
370 /*
371 * driver name
372 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
373 */
374 *driver_name = NULL;
375
376 /* image name */
377 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
378 if (ret != EFI_SUCCESS) {
379 *image_path = NULL;
380 return 0;
381 }
382
383 image = handler->protocol_interface;
384 *image_path = efi_dp_str(image->file_path);
385
386 return 0;
387}
388
389/**
390 * do_efi_show_drivers() - show UEFI drivers
391 *
392 * @cmdtp: Command table
393 * @flag: Command flag
394 * @argc: Number of arguments
395 * @argv: Argument array
396 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
397 *
398 * Implement efidebug "drivers" sub-command.
399 * Show all UEFI drivers and their information.
400 */
Simon Glassed38aef2020-05-10 11:40:03 -0600401static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
402 int argc, char *const argv[])
AKASHI Takahiro185716a2019-02-25 15:54:40 +0900403{
404 efi_handle_t *handles;
405 efi_uintn_t num, i;
406 u16 *driver_name, *image_path_text;
407 efi_status_t ret;
408
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200409 ret = EFI_CALL(efi_locate_handle_buffer(
AKASHI Takahiro185716a2019-02-25 15:54:40 +0900410 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
411 NULL, &num, &handles));
412 if (ret != EFI_SUCCESS)
413 return CMD_RET_FAILURE;
414
415 if (!num)
416 return CMD_RET_SUCCESS;
417
418 printf("Driver%.*s Name Image Path\n",
419 EFI_HANDLE_WIDTH - 6, spc);
420 printf("%.*s ==================== ====================\n",
421 EFI_HANDLE_WIDTH, sep);
422 for (i = 0; i < num; i++) {
423 if (!efi_get_driver_handle_info(handles[i], &driver_name,
424 &image_path_text)) {
425 if (image_path_text)
426 printf("%p %-20ls %ls\n", handles[i],
427 driver_name, image_path_text);
428 else
429 printf("%p %-20ls <built-in>\n",
430 handles[i], driver_name);
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200431 efi_free_pool(driver_name);
432 efi_free_pool(image_path_text);
AKASHI Takahiro185716a2019-02-25 15:54:40 +0900433 }
434 }
435
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200436 efi_free_pool(handles);
AKASHI Takahiro185716a2019-02-25 15:54:40 +0900437
438 return CMD_RET_SUCCESS;
439}
440
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900441/**
442 * do_efi_show_handles() - show UEFI handles
443 *
444 * @cmdtp: Command table
445 * @flag: Command flag
446 * @argc: Number of arguments
447 * @argv: Argument array
448 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
449 *
450 * Implement efidebug "dh" sub-command.
451 * Show all UEFI handles and their information, currently all protocols
452 * added to handle.
453 */
Simon Glassed38aef2020-05-10 11:40:03 -0600454static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
455 int argc, char *const argv[])
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900456{
457 efi_handle_t *handles;
458 efi_guid_t **guid;
459 efi_uintn_t num, count, i, j;
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900460 efi_status_t ret;
461
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200462 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900463 &num, &handles));
464 if (ret != EFI_SUCCESS)
465 return CMD_RET_FAILURE;
466
467 if (!num)
468 return CMD_RET_SUCCESS;
469
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900470 for (i = 0; i < num; i++) {
Heinrich Schuchardtd16463a2022-10-04 15:31:17 +0200471 struct efi_handler *handler;
472
473 printf("\n%p", handles[i]);
474 if (handles[i]->dev)
475 printf(" (%s)", handles[i]->dev->name);
476 printf("\n");
477 /* Print device path */
478 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
479 &handler);
480 if (ret == EFI_SUCCESS)
481 printf(" %pD\n", handler->protocol_interface);
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900482 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
483 &count));
Heinrich Schuchardtd16463a2022-10-04 15:31:17 +0200484 /* Print other protocols */
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900485 for (j = 0; j < count; j++) {
Heinrich Schuchardtd16463a2022-10-04 15:31:17 +0200486 if (guidcmp(guid[j], &efi_guid_device_path))
487 printf(" %pUs\n", guid[j]);
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900488 }
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900489 }
490
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200491 efi_free_pool(handles);
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +0900492
493 return CMD_RET_SUCCESS;
494}
495
AKASHI Takahiro185716a2019-02-25 15:54:40 +0900496/**
AKASHI Takahiro71ab1bf2019-02-25 15:54:42 +0900497 * do_efi_show_images() - show UEFI images
498 *
499 * @cmdtp: Command table
500 * @flag: Command flag
501 * @argc: Number of arguments
502 * @argv: Argument array
503 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
504 *
505 * Implement efidebug "images" sub-command.
506 * Show all UEFI loaded images and their information.
507 */
Simon Glassed38aef2020-05-10 11:40:03 -0600508static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
509 int argc, char *const argv[])
AKASHI Takahiro71ab1bf2019-02-25 15:54:42 +0900510{
511 efi_print_image_infos(NULL);
512
513 return CMD_RET_SUCCESS;
514}
515
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900516static const char * const efi_mem_type_string[] = {
517 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
518 [EFI_LOADER_CODE] = "LOADER CODE",
519 [EFI_LOADER_DATA] = "LOADER DATA",
520 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
521 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
522 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
523 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
524 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
525 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
526 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
527 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
528 [EFI_MMAP_IO] = "IO",
529 [EFI_MMAP_IO_PORT] = "IO PORT",
530 [EFI_PAL_CODE] = "PAL",
Heinrich Schuchardt45176e82020-04-29 20:21:39 +0200531 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900532};
533
534static const struct efi_mem_attrs {
535 const u64 bit;
536 const char *text;
537} efi_mem_attrs[] = {
538 {EFI_MEMORY_UC, "UC"},
539 {EFI_MEMORY_UC, "UC"},
540 {EFI_MEMORY_WC, "WC"},
541 {EFI_MEMORY_WT, "WT"},
542 {EFI_MEMORY_WB, "WB"},
543 {EFI_MEMORY_UCE, "UCE"},
544 {EFI_MEMORY_WP, "WP"},
545 {EFI_MEMORY_RP, "RP"},
546 {EFI_MEMORY_XP, "WP"},
547 {EFI_MEMORY_NV, "NV"},
548 {EFI_MEMORY_MORE_RELIABLE, "REL"},
549 {EFI_MEMORY_RO, "RO"},
Heinrich Schuchardte76f0e42020-05-19 07:20:46 +0200550 {EFI_MEMORY_SP, "SP"},
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900551 {EFI_MEMORY_RUNTIME, "RT"},
552};
553
554/**
555 * print_memory_attributes() - print memory map attributes
Heinrich Schuchardtb1d65432019-07-14 14:00:41 +0200556 *
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900557 * @attributes: Attribute value
558 *
559 * Print memory map attributes
560 */
561static void print_memory_attributes(u64 attributes)
562{
563 int sep, i;
564
565 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
566 if (attributes & efi_mem_attrs[i].bit) {
567 if (sep) {
568 putc('|');
569 } else {
570 putc(' ');
571 sep = 1;
572 }
573 puts(efi_mem_attrs[i].text);
574 }
575}
576
577#define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
578
579/**
580 * do_efi_show_memmap() - show UEFI memory map
581 *
582 * @cmdtp: Command table
583 * @flag: Command flag
584 * @argc: Number of arguments
585 * @argv: Argument array
586 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
587 *
588 * Implement efidebug "memmap" sub-command.
589 * Show UEFI memory map.
590 */
Simon Glassed38aef2020-05-10 11:40:03 -0600591static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
592 int argc, char *const argv[])
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900593{
Heinrich Schuchardt13ce2d92023-01-05 18:26:01 +0100594 struct efi_mem_desc *memmap, *map;
595 efi_uintn_t map_size;
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900596 const char *type;
597 int i;
598 efi_status_t ret;
599
Heinrich Schuchardt13ce2d92023-01-05 18:26:01 +0100600 ret = efi_get_memory_map_alloc(&map_size, &memmap);
601 if (ret != EFI_SUCCESS)
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900602 return CMD_RET_FAILURE;
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900603
604 printf("Type Start%.*s End%.*s Attributes\n",
605 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
606 printf("================ %.*s %.*s ==========\n",
607 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
AKASHI Takahiroac0abe22020-05-08 14:50:18 +0900608 /*
609 * Coverity check: dereferencing null pointer "map."
610 * This is a false positive as memmap will always be
611 * populated by allocate_pool() above.
612 */
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900613 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
Heinrich Schuchardt45176e82020-04-29 20:21:39 +0200614 if (map->type < ARRAY_SIZE(efi_mem_type_string))
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900615 type = efi_mem_type_string[map->type];
616 else
617 type = "(unknown)";
618
619 printf("%-16s %.*llx-%.*llx", type,
620 EFI_PHYS_ADDR_WIDTH,
Heinrich Schuchardt9d4c7882020-03-27 04:33:17 +0000621 (u64)map_to_sysmem((void *)(uintptr_t)
622 map->physical_start),
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900623 EFI_PHYS_ADDR_WIDTH,
Heinrich Schuchardt9d4c7882020-03-27 04:33:17 +0000624 (u64)map_to_sysmem((void *)(uintptr_t)
625 (map->physical_start +
626 map->num_pages * EFI_PAGE_SIZE)));
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900627
628 print_memory_attributes(map->attribute);
629 putc('\n');
630 }
631
Heinrich Schuchardt982c7d92020-05-02 16:08:37 +0200632 efi_free_pool(memmap);
AKASHI Takahiro32979d82019-02-25 15:54:43 +0900633
634 return CMD_RET_SUCCESS;
635}
636
AKASHI Takahiro71ab1bf2019-02-25 15:54:42 +0900637/**
Heinrich Schuchardt75844e52020-01-07 05:57:47 +0100638 * do_efi_show_tables() - show UEFI configuration tables
639 *
640 * @cmdtp: Command table
641 * @flag: Command flag
642 * @argc: Number of arguments
643 * @argv: Argument array
644 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
645 *
646 * Implement efidebug "tables" sub-command.
647 * Show UEFI configuration tables.
648 */
Simon Glassed38aef2020-05-10 11:40:03 -0600649static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
650 int argc, char *const argv[])
Heinrich Schuchardt75844e52020-01-07 05:57:47 +0100651{
Simon Glass55de3e52023-03-20 08:30:14 +1300652 efi_show_tables(&systab);
Heinrich Schuchardt75844e52020-01-07 05:57:47 +0100653
654 return CMD_RET_SUCCESS;
655}
656
657/**
Heinrich Schuchardt57274c82022-02-26 12:10:10 +0100658 * create_initrd_dp() - create a special device for our Boot### option
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200659 *
Heinrich Schuchardt57274c82022-02-26 12:10:10 +0100660 * @dev: device
661 * @part: disk partition
662 * @file: filename
663 * @shortform: create short form device path
664 * Return: pointer to the device path or ERR_PTR
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200665 */
666static
667struct efi_device_path *create_initrd_dp(const char *dev, const char *part,
Heinrich Schuchardt57274c82022-02-26 12:10:10 +0100668 const char *file, int shortform)
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200669
670{
Heinrich Schuchardt57274c82022-02-26 12:10:10 +0100671 struct efi_device_path *tmp_dp = NULL, *tmp_fp = NULL, *short_fp = NULL;
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200672 struct efi_device_path *initrd_dp = NULL;
673 efi_status_t ret;
674 const struct efi_initrd_dp id_dp = {
675 .vendor = {
676 {
677 DEVICE_PATH_TYPE_MEDIA_DEVICE,
678 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
679 sizeof(id_dp.vendor),
680 },
681 EFI_INITRD_MEDIA_GUID,
682 },
683 .end = {
684 DEVICE_PATH_TYPE_END,
685 DEVICE_PATH_SUB_TYPE_END,
686 sizeof(id_dp.end),
687 }
688 };
689
690 ret = efi_dp_from_name(dev, part, file, &tmp_dp, &tmp_fp);
691 if (ret != EFI_SUCCESS) {
692 printf("Cannot create device path for \"%s %s\"\n", part, file);
693 goto out;
694 }
Heinrich Schuchardt57274c82022-02-26 12:10:10 +0100695 if (shortform)
696 short_fp = efi_dp_shorten(tmp_fp);
697 if (!short_fp)
698 short_fp = tmp_fp;
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200699
700 initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp,
Heinrich Schuchardt57274c82022-02-26 12:10:10 +0100701 short_fp);
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200702
703out:
704 efi_free_pool(tmp_dp);
705 efi_free_pool(tmp_fp);
706 return initrd_dp;
707}
708
709/**
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900710 * do_efi_boot_add() - set UEFI load option
711 *
712 * @cmdtp: Command table
713 * @flag: Command flag
714 * @argc: Number of arguments
715 * @argv: Argument array
716 * Return: CMD_RET_SUCCESS on success,
717 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
718 *
Heinrich Schuchardtb1d65432019-07-14 14:00:41 +0200719 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
720 *
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200721 * efidebug boot add -b <id> <label> <interface> <devnum>[:<part>] <file>
722 * -i <file> <interface2> <devnum2>[:<part>] <initrd>
723 * -s '<options>'
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900724 */
Simon Glassed38aef2020-05-10 11:40:03 -0600725static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
726 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900727{
728 int id;
729 char *endp;
Heinrich Schuchardt9a5e74e2021-05-24 10:57:03 +0200730 u16 var_name16[9];
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900731 efi_guid_t guid;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900732 u16 *label;
Heinrich Schuchardtefe92b72022-03-23 20:26:25 +0100733 struct efi_device_path *file_path = NULL;
Heinrich Schuchardt57274c82022-02-26 12:10:10 +0100734 struct efi_device_path *fp_free = NULL;
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200735 struct efi_device_path *final_fp = NULL;
736 struct efi_device_path *initrd_dp = NULL;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900737 struct efi_load_option lo;
738 void *data = NULL;
739 efi_uintn_t size;
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200740 efi_uintn_t fp_size = 0;
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +0200741 efi_status_t ret;
Heinrich Schuchardt99a21fc2019-06-20 12:59:45 +0200742 int r = CMD_RET_SUCCESS;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900743
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900744 guid = efi_global_variable_guid;
745
746 /* attributes */
747 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200748 lo.optional_data = NULL;
749 lo.label = NULL;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900750
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200751 argc--;
752 argv++; /* 'add' */
753 for (; argc > 0; argc--, argv++) {
Heinrich Schuchardt57274c82022-02-26 12:10:10 +0100754 int shortform;
755
756 if (*argv[0] != '-' || strlen(argv[0]) != 2) {
757 r = CMD_RET_USAGE;
758 goto out;
759 }
760 shortform = 0;
761 switch (argv[0][1]) {
762 case 'b':
763 shortform = 1;
764 /* fallthrough */
765 case 'B':
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200766 if (argc < 5 || lo.label) {
767 r = CMD_RET_USAGE;
768 goto out;
769 }
Simon Glass3ff49ec2021-07-24 09:03:29 -0600770 id = (int)hextoul(argv[1], &endp);
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200771 if (*endp != '\0' || id > 0xffff)
772 return CMD_RET_USAGE;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900773
Heinrich Schuchardt9a5e74e2021-05-24 10:57:03 +0200774 efi_create_indexed_name(var_name16, sizeof(var_name16),
775 "Boot", id);
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200776
777 /* label */
Heinrich Schuchardt412f9ec2022-10-06 06:48:02 +0200778 label = efi_convert_string(argv[2]);
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200779 if (!label)
780 return CMD_RET_FAILURE;
781 lo.label = label; /* label will be changed below */
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200782
783 /* file path */
784 ret = efi_dp_from_name(argv[3], argv[4], argv[5],
Heinrich Schuchardtefe92b72022-03-23 20:26:25 +0100785 NULL, &fp_free);
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200786 if (ret != EFI_SUCCESS) {
787 printf("Cannot create device path for \"%s %s\"\n",
788 argv[3], argv[4]);
789 r = CMD_RET_FAILURE;
790 goto out;
791 }
Heinrich Schuchardt57274c82022-02-26 12:10:10 +0100792 if (shortform)
793 file_path = efi_dp_shorten(fp_free);
794 if (!file_path)
795 file_path = fp_free;
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200796 fp_size += efi_dp_size(file_path) +
797 sizeof(struct efi_device_path);
798 argc -= 5;
799 argv += 5;
Heinrich Schuchardt57274c82022-02-26 12:10:10 +0100800 break;
801 case 'i':
802 shortform = 1;
803 /* fallthrough */
804 case 'I':
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200805 if (argc < 3 || initrd_dp) {
806 r = CMD_RET_USAGE;
807 goto out;
808 }
809
Heinrich Schuchardt57274c82022-02-26 12:10:10 +0100810 initrd_dp = create_initrd_dp(argv[1], argv[2], argv[3],
811 shortform);
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200812 if (!initrd_dp) {
813 printf("Cannot add an initrd\n");
814 r = CMD_RET_FAILURE;
815 goto out;
816 }
817 argc -= 3;
818 argv += 3;
819 fp_size += efi_dp_size(initrd_dp) +
820 sizeof(struct efi_device_path);
Heinrich Schuchardt57274c82022-02-26 12:10:10 +0100821 break;
822 case 's':
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200823 if (argc < 1 || lo.optional_data) {
824 r = CMD_RET_USAGE;
825 goto out;
826 }
827 lo.optional_data = (const u8 *)argv[1];
828 argc -= 1;
829 argv += 1;
Heinrich Schuchardt57274c82022-02-26 12:10:10 +0100830 break;
831 default:
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200832 r = CMD_RET_USAGE;
833 goto out;
834 }
835 }
836
837 if (!file_path) {
838 printf("Missing binary\n");
839 r = CMD_RET_USAGE;
840 goto out;
841 }
842
843 final_fp = efi_dp_concat(file_path, initrd_dp);
844 if (!final_fp) {
845 printf("Cannot create final device path\n");
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +0200846 r = CMD_RET_FAILURE;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900847 goto out;
848 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900849
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200850 lo.file_path = final_fp;
851 lo.file_path_length = fp_size;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900852
853 size = efi_serialize_load_option(&lo, (u8 **)&data);
854 if (!size) {
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +0200855 r = CMD_RET_FAILURE;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900856 goto out;
857 }
858
Heinrich Schuchardte8324f52021-05-24 11:10:59 +0200859 ret = efi_set_variable_int(var_name16, &guid,
860 EFI_VARIABLE_NON_VOLATILE |
861 EFI_VARIABLE_BOOTSERVICE_ACCESS |
862 EFI_VARIABLE_RUNTIME_ACCESS,
863 size, data, false);
Heinrich Schuchardt99a21fc2019-06-20 12:59:45 +0200864 if (ret != EFI_SUCCESS) {
865 printf("Cannot set %ls\n", var_name16);
866 r = CMD_RET_FAILURE;
867 }
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200868
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900869out:
870 free(data);
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200871 efi_free_pool(final_fp);
872 efi_free_pool(initrd_dp);
Heinrich Schuchardt57274c82022-02-26 12:10:10 +0100873 efi_free_pool(fp_free);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900874 free(lo.label);
875
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +0200876 return r;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900877}
878
879/**
880 * do_efi_boot_rm() - delete UEFI load options
881 *
882 * @cmdtp: Command table
883 * @flag: Command flag
884 * @argc: Number of arguments
885 * @argv: Argument array
886 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
887 *
888 * Implement efidebug "boot rm" sub-command.
889 * Delete UEFI load options.
Heinrich Schuchardtb1d65432019-07-14 14:00:41 +0200890 *
891 * efidebug boot rm <id> ...
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900892 */
Simon Glassed38aef2020-05-10 11:40:03 -0600893static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag,
894 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900895{
896 efi_guid_t guid;
897 int id, i;
898 char *endp;
Heinrich Schuchardt9a5e74e2021-05-24 10:57:03 +0200899 u16 var_name16[9];
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900900 efi_status_t ret;
901
902 if (argc == 1)
903 return CMD_RET_USAGE;
904
905 guid = efi_global_variable_guid;
906 for (i = 1; i < argc; i++, argv++) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600907 id = (int)hextoul(argv[1], &endp);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900908 if (*endp != '\0' || id > 0xffff)
909 return CMD_RET_FAILURE;
910
Heinrich Schuchardt9a5e74e2021-05-24 10:57:03 +0200911 efi_create_indexed_name(var_name16, sizeof(var_name16),
912 "Boot", id);
Heinrich Schuchardte8324f52021-05-24 11:10:59 +0200913 ret = efi_set_variable_int(var_name16, &guid, 0, 0, NULL,
914 false);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900915 if (ret) {
Heinrich Schuchardt3be0bda2020-03-02 20:13:10 +0100916 printf("Cannot remove %ls\n", var_name16);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900917 return CMD_RET_FAILURE;
918 }
919 }
920
921 return CMD_RET_SUCCESS;
922}
923
924/**
925 * show_efi_boot_opt_data() - dump UEFI load option
926 *
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200927 * @varname16: variable name
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200928 * @data: value of UEFI load option variable
929 * @size: size of the boot option
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900930 *
931 * Decode the value of UEFI load option variable and print information.
932 */
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200933static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size)
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900934{
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200935 struct efi_device_path *initrd_path = NULL;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900936 struct efi_load_option lo;
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200937 efi_status_t ret;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900938
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200939 ret = efi_deserialize_load_option(&lo, data, size);
940 if (ret != EFI_SUCCESS) {
941 printf("%ls: invalid load option\n", varname16);
942 return;
943 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900944
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200945 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
946 varname16,
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900947 /* ACTIVE */
948 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
949 /* FORCE RECONNECT */
950 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
951 /* HIDDEN */
952 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
953 lo.attributes);
Heinrich Schuchardte6720b92021-05-24 10:35:25 +0200954 printf(" label: %ls\n", lo.label);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900955
Heinrich Schuchardtca44a5d2021-10-15 01:47:40 +0200956 printf(" file_path: %pD\n", lo.file_path);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900957
Heinrich Schuchardt6c405cb2021-10-15 02:33:33 +0200958 initrd_path = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200959 if (initrd_path) {
Heinrich Schuchardtca44a5d2021-10-15 01:47:40 +0200960 printf(" initrd_path: %pD\n", initrd_path);
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200961 efi_free_pool(initrd_path);
962 }
963
Heinrich Schuchardt9df002e2019-04-29 13:51:45 +0200964 printf(" data:\n");
965 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200966 lo.optional_data, *size, true);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900967}
968
969/**
970 * show_efi_boot_opt() - dump UEFI load option
971 *
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200972 * @varname16: variable name
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900973 *
974 * Dump information defined by UEFI load option.
975 */
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200976static void show_efi_boot_opt(u16 *varname16)
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900977{
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200978 void *data;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900979 efi_uintn_t size;
AKASHI Takahirodab93f42019-11-26 10:11:22 +0900980 efi_status_t ret;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900981
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900982 size = 0;
Heinrich Schuchardtf3120872022-10-15 13:21:01 +0200983 ret = efi_get_variable_int(varname16, &efi_global_variable_guid,
984 NULL, &size, NULL, NULL);
AKASHI Takahirodab93f42019-11-26 10:11:22 +0900985 if (ret == EFI_BUFFER_TOO_SMALL) {
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900986 data = malloc(size);
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200987 if (!data) {
988 printf("ERROR: Out of memory\n");
989 return;
990 }
Heinrich Schuchardtf3120872022-10-15 13:21:01 +0200991 ret = efi_get_variable_int(varname16, &efi_global_variable_guid,
992 NULL, &size, data, NULL);
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200993 if (ret == EFI_SUCCESS)
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +0200994 show_efi_boot_opt_data(varname16, data, &size);
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +0200995 free(data);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900996 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +0900997}
998
999/**
1000 * show_efi_boot_dump() - dump all UEFI load options
1001 *
1002 * @cmdtp: Command table
1003 * @flag: Command flag
1004 * @argc: Number of arguments
1005 * @argv: Argument array
1006 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1007 *
1008 * Implement efidebug "boot dump" sub-command.
1009 * Dump information of all UEFI load options defined.
Heinrich Schuchardt21715092019-07-25 20:52:23 +02001010 *
1011 * efidebug boot dump
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001012 */
Simon Glassed38aef2020-05-10 11:40:03 -06001013static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
1014 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001015{
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +09001016 u16 *var_name16, *p;
1017 efi_uintn_t buf_size, size;
1018 efi_guid_t guid;
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +09001019 efi_status_t ret;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001020
1021 if (argc > 1)
1022 return CMD_RET_USAGE;
1023
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +09001024 buf_size = 128;
1025 var_name16 = malloc(buf_size);
1026 if (!var_name16)
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001027 return CMD_RET_FAILURE;
1028
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +09001029 var_name16[0] = 0;
1030 for (;;) {
1031 size = buf_size;
Heinrich Schuchardtf3120872022-10-15 13:21:01 +02001032 ret = efi_get_next_variable_name_int(&size, var_name16, &guid);
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +09001033 if (ret == EFI_NOT_FOUND)
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001034 break;
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +09001035 if (ret == EFI_BUFFER_TOO_SMALL) {
1036 buf_size = size;
1037 p = realloc(var_name16, buf_size);
1038 if (!p) {
1039 free(var_name16);
1040 return CMD_RET_FAILURE;
1041 }
1042 var_name16 = p;
Heinrich Schuchardtf3120872022-10-15 13:21:01 +02001043 ret = efi_get_next_variable_name_int(&size, var_name16,
1044 &guid);
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +09001045 }
1046 if (ret != EFI_SUCCESS) {
1047 free(var_name16);
1048 return CMD_RET_FAILURE;
1049 }
1050
Masahisa Kojima2f407f02022-12-02 13:59:35 +09001051 if (efi_varname_is_load_option(var_name16, NULL))
Heinrich Schuchardt8d8be692020-04-29 19:20:35 +02001052 show_efi_boot_opt(var_name16);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001053 }
AKASHI Takahiro016bfcd2019-04-26 09:44:18 +09001054
1055 free(var_name16);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001056
1057 return CMD_RET_SUCCESS;
1058}
1059
1060/**
1061 * show_efi_boot_order() - show order of UEFI load options
1062 *
1063 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1064 *
1065 * Show order of UEFI load options defined by BootOrder variable.
1066 */
1067static int show_efi_boot_order(void)
1068{
Heinrich Schuchardt98fc6152020-04-29 21:15:08 +02001069 u16 *bootorder;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001070 efi_uintn_t size;
1071 int num, i;
Heinrich Schuchardt9a5e74e2021-05-24 10:57:03 +02001072 u16 var_name16[9];
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001073 void *data;
1074 struct efi_load_option lo;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001075 efi_status_t ret;
1076
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001077 size = 0;
Heinrich Schuchardtf3120872022-10-15 13:21:01 +02001078 ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid,
1079 NULL, &size, NULL, NULL);
Heinrich Schuchardt98fc6152020-04-29 21:15:08 +02001080 if (ret != EFI_BUFFER_TOO_SMALL) {
1081 if (ret == EFI_NOT_FOUND) {
1082 printf("BootOrder not defined\n");
1083 return CMD_RET_SUCCESS;
1084 } else {
1085 return CMD_RET_FAILURE;
1086 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001087 }
Heinrich Schuchardt98fc6152020-04-29 21:15:08 +02001088 bootorder = malloc(size);
1089 if (!bootorder) {
1090 printf("ERROR: Out of memory\n");
1091 return CMD_RET_FAILURE;
1092 }
Heinrich Schuchardtf3120872022-10-15 13:21:01 +02001093 ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid,
1094 NULL, &size, bootorder, NULL);
Heinrich Schuchardt98fc6152020-04-29 21:15:08 +02001095 if (ret != EFI_SUCCESS) {
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001096 ret = CMD_RET_FAILURE;
1097 goto out;
1098 }
1099
1100 num = size / sizeof(u16);
1101 for (i = 0; i < num; i++) {
Heinrich Schuchardt9a5e74e2021-05-24 10:57:03 +02001102 efi_create_indexed_name(var_name16, sizeof(var_name16),
Heinrich Schuchardt205bc3f2021-06-12 00:01:44 +02001103 "Boot", bootorder[i]);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001104
1105 size = 0;
Heinrich Schuchardtf3120872022-10-15 13:21:01 +02001106 ret = efi_get_variable_int(var_name16,
1107 &efi_global_variable_guid, NULL,
1108 &size, NULL, NULL);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001109 if (ret != EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardt9a5e74e2021-05-24 10:57:03 +02001110 printf("%2d: %ls: (not defined)\n", i + 1, var_name16);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001111 continue;
1112 }
1113
1114 data = malloc(size);
1115 if (!data) {
1116 ret = CMD_RET_FAILURE;
1117 goto out;
1118 }
Heinrich Schuchardtf3120872022-10-15 13:21:01 +02001119 ret = efi_get_variable_int(var_name16,
1120 &efi_global_variable_guid, NULL,
1121 &size, data, NULL);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001122 if (ret != EFI_SUCCESS) {
1123 free(data);
1124 ret = CMD_RET_FAILURE;
1125 goto out;
1126 }
1127
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +02001128 ret = efi_deserialize_load_option(&lo, data, &size);
1129 if (ret != EFI_SUCCESS) {
1130 printf("%ls: invalid load option\n", var_name16);
1131 ret = CMD_RET_FAILURE;
1132 goto out;
1133 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001134
Heinrich Schuchardt9a5e74e2021-05-24 10:57:03 +02001135 printf("%2d: %ls: %ls\n", i + 1, var_name16, lo.label);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001136
1137 free(data);
1138 }
1139out:
1140 free(bootorder);
1141
1142 return ret;
1143}
1144
1145/**
1146 * do_efi_boot_next() - manage UEFI BootNext variable
1147 *
1148 * @cmdtp: Command table
1149 * @flag: Command flag
1150 * @argc: Number of arguments
1151 * @argv: Argument array
1152 * Return: CMD_RET_SUCCESS on success,
1153 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1154 *
1155 * Implement efidebug "boot next" sub-command.
1156 * Set BootNext variable.
Heinrich Schuchardtb1d65432019-07-14 14:00:41 +02001157 *
1158 * efidebug boot next <id>
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001159 */
Simon Glassed38aef2020-05-10 11:40:03 -06001160static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
1161 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001162{
1163 u16 bootnext;
1164 efi_uintn_t size;
1165 char *endp;
1166 efi_guid_t guid;
1167 efi_status_t ret;
Heinrich Schuchardt99a21fc2019-06-20 12:59:45 +02001168 int r = CMD_RET_SUCCESS;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001169
1170 if (argc != 2)
1171 return CMD_RET_USAGE;
1172
Simon Glass3ff49ec2021-07-24 09:03:29 -06001173 bootnext = (u16)hextoul(argv[1], &endp);
Heinrich Schuchardt90c9d042020-05-09 17:59:19 +02001174 if (*endp) {
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001175 printf("invalid value: %s\n", argv[1]);
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +02001176 r = CMD_RET_FAILURE;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001177 goto out;
1178 }
1179
1180 guid = efi_global_variable_guid;
1181 size = sizeof(u16);
Simon Glass90975372022-01-23 12:55:12 -07001182 ret = efi_set_variable_int(u"BootNext", &guid,
1183 EFI_VARIABLE_NON_VOLATILE |
1184 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1185 EFI_VARIABLE_RUNTIME_ACCESS,
1186 size, &bootnext, false);
Heinrich Schuchardt99a21fc2019-06-20 12:59:45 +02001187 if (ret != EFI_SUCCESS) {
1188 printf("Cannot set BootNext\n");
1189 r = CMD_RET_FAILURE;
1190 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001191out:
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +02001192 return r;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001193}
1194
1195/**
1196 * do_efi_boot_order() - manage UEFI BootOrder variable
1197 *
1198 * @cmdtp: Command table
1199 * @flag: Command flag
1200 * @argc: Number of arguments
1201 * @argv: Argument array
1202 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1203 *
1204 * Implement efidebug "boot order" sub-command.
1205 * Show order of UEFI load options, or change it in BootOrder variable.
Heinrich Schuchardtb1d65432019-07-14 14:00:41 +02001206 *
1207 * efidebug boot order [<id> ...]
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001208 */
Simon Glassed38aef2020-05-10 11:40:03 -06001209static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
1210 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001211{
1212 u16 *bootorder = NULL;
1213 efi_uintn_t size;
1214 int id, i;
1215 char *endp;
1216 efi_guid_t guid;
1217 efi_status_t ret;
Heinrich Schuchardt99a21fc2019-06-20 12:59:45 +02001218 int r = CMD_RET_SUCCESS;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001219
1220 if (argc == 1)
1221 return show_efi_boot_order();
1222
1223 argc--;
1224 argv++;
1225
1226 size = argc * sizeof(u16);
1227 bootorder = malloc(size);
1228 if (!bootorder)
1229 return CMD_RET_FAILURE;
1230
1231 for (i = 0; i < argc; i++) {
Simon Glass3ff49ec2021-07-24 09:03:29 -06001232 id = (int)hextoul(argv[i], &endp);
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001233 if (*endp != '\0' || id > 0xffff) {
1234 printf("invalid value: %s\n", argv[i]);
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +02001235 r = CMD_RET_FAILURE;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001236 goto out;
1237 }
1238
1239 bootorder[i] = (u16)id;
1240 }
1241
1242 guid = efi_global_variable_guid;
Simon Glass90975372022-01-23 12:55:12 -07001243 ret = efi_set_variable_int(u"BootOrder", &guid,
1244 EFI_VARIABLE_NON_VOLATILE |
1245 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1246 EFI_VARIABLE_RUNTIME_ACCESS,
1247 size, bootorder, true);
Heinrich Schuchardt99a21fc2019-06-20 12:59:45 +02001248 if (ret != EFI_SUCCESS) {
1249 printf("Cannot set BootOrder\n");
1250 r = CMD_RET_FAILURE;
1251 }
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001252out:
1253 free(bootorder);
1254
Heinrich Schuchardt8bdd1492019-06-20 12:48:04 +02001255 return r;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001256}
1257
Simon Glassed38aef2020-05-10 11:40:03 -06001258static struct cmd_tbl cmd_efidebug_boot_sub[] = {
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001259 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1260 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1261 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1262 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1263 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1264 "", ""),
1265};
1266
1267/**
1268 * do_efi_boot_opt() - manage UEFI load options
1269 *
1270 * @cmdtp: Command table
1271 * @flag: Command flag
1272 * @argc: Number of arguments
1273 * @argv: Argument array
1274 * Return: CMD_RET_SUCCESS on success,
1275 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1276 *
1277 * Implement efidebug "boot" sub-command.
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001278 */
Simon Glassed38aef2020-05-10 11:40:03 -06001279static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
1280 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001281{
Simon Glassed38aef2020-05-10 11:40:03 -06001282 struct cmd_tbl *cp;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001283
1284 if (argc < 2)
1285 return CMD_RET_USAGE;
1286
1287 argc--; argv++;
1288
1289 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1290 ARRAY_SIZE(cmd_efidebug_boot_sub));
1291 if (!cp)
1292 return CMD_RET_USAGE;
1293
1294 return cp->cmd(cmdtp, flag, argc, argv);
1295}
1296
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001297/**
1298 * do_efi_test_bootmgr() - run simple bootmgr for test
1299 *
1300 * @cmdtp: Command table
1301 * @flag: Command flag
1302 * @argc: Number of arguments
1303 * @argv: Argument array
1304 * Return: CMD_RET_SUCCESS on success,
1305 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1306 *
1307 * Implement efidebug "test bootmgr" sub-command.
1308 * Run simple bootmgr for test.
1309 *
1310 * efidebug test bootmgr
1311 */
Heinrich Schuchardtb2625e82021-01-15 19:02:50 +01001312static __maybe_unused int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
1313 int argc, char * const argv[])
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001314{
1315 efi_handle_t image;
1316 efi_uintn_t exit_data_size = 0;
1317 u16 *exit_data = NULL;
1318 efi_status_t ret;
Heinrich Schuchardte6ed8352020-08-11 18:20:50 +02001319 void *load_options = NULL;
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001320
Heinrich Schuchardta7647a72020-08-07 17:49:39 +02001321 ret = efi_bootmgr_load(&image, &load_options);
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001322 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1323
1324 /* We call efi_start_image() even if error for test purpose. */
1325 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1326 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1327 if (ret && exit_data)
1328 efi_free_pool(exit_data);
1329
1330 efi_restore_gd();
1331
Heinrich Schuchardta7647a72020-08-07 17:49:39 +02001332 free(load_options);
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001333 return CMD_RET_SUCCESS;
1334}
1335
Simon Glassed38aef2020-05-10 11:40:03 -06001336static struct cmd_tbl cmd_efidebug_test_sub[] = {
Heinrich Schuchardtb2625e82021-01-15 19:02:50 +01001337#ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001338 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1339 "", ""),
Heinrich Schuchardtb2625e82021-01-15 19:02:50 +01001340#endif
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001341};
1342
1343/**
1344 * do_efi_test() - manage UEFI load options
1345 *
1346 * @cmdtp: Command table
1347 * @flag: Command flag
1348 * @argc: Number of arguments
1349 * @argv: Argument array
1350 * Return: CMD_RET_SUCCESS on success,
1351 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1352 *
1353 * Implement efidebug "test" sub-command.
1354 */
Simon Glassed38aef2020-05-10 11:40:03 -06001355static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001356 int argc, char * const argv[])
1357{
Simon Glassed38aef2020-05-10 11:40:03 -06001358 struct cmd_tbl *cp;
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001359
1360 if (argc < 2)
1361 return CMD_RET_USAGE;
1362
1363 argc--; argv++;
1364
1365 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1366 ARRAY_SIZE(cmd_efidebug_test_sub));
1367 if (!cp)
1368 return CMD_RET_USAGE;
1369
1370 return cp->cmd(cmdtp, flag, argc, argv);
1371}
1372
Ilias Apalodimasf5009402020-05-17 22:25:45 +03001373/**
1374 * do_efi_query_info() - QueryVariableInfo EFI service
1375 *
1376 * @cmdtp: Command table
1377 * @flag: Command flag
1378 * @argc: Number of arguments
1379 * @argv: Argument array
1380 * Return: CMD_RET_SUCCESS on success,
1381 * CMD_RET_USAGE or CMD_RET_FAILURE on failure
1382 *
1383 * Implement efidebug "test" sub-command.
1384 */
1385
Simon Glassed38aef2020-05-10 11:40:03 -06001386static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
Ilias Apalodimasf5009402020-05-17 22:25:45 +03001387 int argc, char * const argv[])
1388{
1389 efi_status_t ret;
1390 u32 attr = 0;
1391 u64 max_variable_storage_size;
1392 u64 remain_variable_storage_size;
1393 u64 max_variable_size;
1394 int i;
1395
1396 for (i = 1; i < argc; i++) {
1397 if (!strcmp(argv[i], "-bs"))
1398 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
1399 else if (!strcmp(argv[i], "-rt"))
1400 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
1401 else if (!strcmp(argv[i], "-nv"))
1402 attr |= EFI_VARIABLE_NON_VOLATILE;
1403 else if (!strcmp(argv[i], "-at"))
1404 attr |=
1405 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1406 }
1407
Heinrich Schuchardtf3120872022-10-15 13:21:01 +02001408 ret = efi_query_variable_info_int(attr, &max_variable_storage_size,
1409 &remain_variable_storage_size,
1410 &max_variable_size);
Ilias Apalodimasf5009402020-05-17 22:25:45 +03001411 if (ret != EFI_SUCCESS) {
1412 printf("Error: Cannot query UEFI variables, r = %lu\n",
1413 ret & ~EFI_ERROR_MASK);
1414 return CMD_RET_FAILURE;
1415 }
1416
1417 printf("Max storage size %llu\n", max_variable_storage_size);
1418 printf("Remaining storage size %llu\n", remain_variable_storage_size);
1419 printf("Max variable size %llu\n", max_variable_size);
1420
1421 return CMD_RET_SUCCESS;
1422}
1423
Simon Glassed38aef2020-05-10 11:40:03 -06001424static struct cmd_tbl cmd_efidebug_sub[] = {
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001425 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
AKASHI Takahiro38833c82020-11-17 09:28:01 +09001426#ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1427 U_BOOT_CMD_MKENT(capsule, CONFIG_SYS_MAXARGS, 1, do_efi_capsule,
1428 "", ""),
1429#endif
AKASHI Takahiro185716a2019-02-25 15:54:40 +09001430 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1431 "", ""),
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +09001432 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1433 "", ""),
AKASHI Takahiro71ab1bf2019-02-25 15:54:42 +09001434 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1435 "", ""),
AKASHI Takahiro32979d82019-02-25 15:54:43 +09001436 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1437 "", ""),
Heinrich Schuchardt75844e52020-01-07 05:57:47 +01001438 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1439 "", ""),
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001440 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1441 "", ""),
Ilias Apalodimasf5009402020-05-17 22:25:45 +03001442 U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
1443 "", ""),
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001444};
1445
1446/**
1447 * do_efidebug() - display and configure UEFI environment
1448 *
1449 * @cmdtp: Command table
1450 * @flag: Command flag
1451 * @argc: Number of arguments
1452 * @argv: Argument array
1453 * Return: CMD_RET_SUCCESS on success,
1454 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1455 *
1456 * Implement efidebug command which allows us to display and
1457 * configure UEFI environment.
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001458 */
Simon Glassed38aef2020-05-10 11:40:03 -06001459static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
1460 int argc, char *const argv[])
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001461{
Simon Glassed38aef2020-05-10 11:40:03 -06001462 struct cmd_tbl *cp;
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001463 efi_status_t r;
1464
1465 if (argc < 2)
1466 return CMD_RET_USAGE;
1467
1468 argc--; argv++;
1469
1470 /* Initialize UEFI drivers */
1471 r = efi_init_obj_list();
1472 if (r != EFI_SUCCESS) {
1473 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1474 r & ~EFI_ERROR_MASK);
1475 return CMD_RET_FAILURE;
1476 }
1477
1478 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1479 ARRAY_SIZE(cmd_efidebug_sub));
1480 if (!cp)
1481 return CMD_RET_USAGE;
1482
1483 return cp->cmd(cmdtp, flag, argc, argv);
1484}
1485
1486#ifdef CONFIG_SYS_LONGHELP
1487static char efidebug_help_text[] =
1488 " - UEFI Shell-like interface to configure UEFI environment\n"
1489 "\n"
Heinrich Schuchardt57274c82022-02-26 12:10:10 +01001490 "efidebug boot add - set UEFI BootXXXX variable\n"
1491 " -b|-B <bootid> <label> <interface> <devnum>[:<part>] <file path>\n"
1492 " -i|-I <interface> <devnum>[:<part>] <initrd file path>\n"
1493 " (-b, -i for short form device path)\n"
1494 " -s '<optional data>'\n"
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001495 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1496 " - delete UEFI BootXXXX variables\n"
1497 "efidebug boot dump\n"
1498 " - dump all UEFI BootXXXX variables\n"
1499 "efidebug boot next <bootid>\n"
1500 " - set UEFI BootNext variable\n"
1501 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1502 " - set/show UEFI boot order\n"
AKASHI Takahiroe32788a2019-02-25 15:54:39 +09001503 "\n"
AKASHI Takahiro38833c82020-11-17 09:28:01 +09001504#ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1505 "efidebug capsule update [-v] <capsule address>\n"
1506 " - process a capsule\n"
Sughosh Ganue6f9efc2020-12-30 19:27:11 +05301507 "efidebug capsule disk-update\n"
1508 " - update a capsule from disk\n"
AKASHI Takahiro38833c82020-11-17 09:28:01 +09001509 "efidebug capsule show <capsule address>\n"
1510 " - show capsule information\n"
1511 "efidebug capsule result [<capsule result var>]\n"
1512 " - show a capsule update result\n"
Jose Marinhob027e792021-03-11 13:18:51 +00001513#ifdef CONFIG_EFI_ESRT
1514 "efidebug capsule esrt\n"
1515 " - print the ESRT\n"
1516#endif
AKASHI Takahiro38833c82020-11-17 09:28:01 +09001517 "\n"
1518#endif
AKASHI Takahiro185716a2019-02-25 15:54:40 +09001519 "efidebug drivers\n"
Heinrich Schuchardtc414d882020-01-07 07:48:15 +01001520 " - show UEFI drivers\n"
AKASHI Takahiro02e8ca62019-02-25 15:54:41 +09001521 "efidebug dh\n"
Heinrich Schuchardtc414d882020-01-07 07:48:15 +01001522 " - show UEFI handles\n"
AKASHI Takahiro71ab1bf2019-02-25 15:54:42 +09001523 "efidebug images\n"
AKASHI Takahiro32979d82019-02-25 15:54:43 +09001524 " - show loaded images\n"
1525 "efidebug memmap\n"
Heinrich Schuchardtc414d882020-01-07 07:48:15 +01001526 " - show UEFI memory map\n"
Heinrich Schuchardt75844e52020-01-07 05:57:47 +01001527 "efidebug tables\n"
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001528 " - show UEFI configuration tables\n"
Heinrich Schuchardtb2625e82021-01-15 19:02:50 +01001529#ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
AKASHI Takahiro36bba2c2020-04-14 11:51:48 +09001530 "efidebug test bootmgr\n"
Ilias Apalodimasf5009402020-05-17 22:25:45 +03001531 " - run simple bootmgr for test\n"
Heinrich Schuchardtb2625e82021-01-15 19:02:50 +01001532#endif
Ilias Apalodimasf5009402020-05-17 22:25:45 +03001533 "efidebug query [-nv][-bs][-rt][-at]\n"
1534 " - show size of UEFI variables store\n";
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001535#endif
1536
1537U_BOOT_CMD(
Ilias Apalodimas773c0902021-03-17 21:55:01 +02001538 efidebug, CONFIG_SYS_MAXARGS, 0, do_efidebug,
AKASHI Takahiroe7c08832019-02-25 15:54:38 +09001539 "Configure UEFI environment",
1540 efidebug_help_text
1541);