blob: 3cc6f2bfcca803ae710a8dc35e6bf36b2c701c84 [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 Schuchardt94686f62020-12-13 10:30:24 +010011#include <efi_dt_fixup.h>
Ilias Apalodimascbea2412021-03-17 21:55:01 +020012#include <efi_load_initrd.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090013#include <efi_loader.h>
Heinrich Schuchardt79693352020-09-27 14:50:25 +020014#include <efi_rng.h>
Heinrich Schuchardtacfe1de2021-05-24 11:10:59 +020015#include <efi_variable.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090016#include <exports.h>
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +020017#include <hexdump.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060018#include <log.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090019#include <malloc.h>
Heinrich Schuchardta415d612020-03-14 08:44:07 +010020#include <mapmem.h>
Heinrich Schuchardtb9b0ea32021-02-02 17:53:14 +010021#include <part.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090022#include <search.h>
23#include <linux/ctype.h>
Ilias Apalodimascbea2412021-03-17 21:55:01 +020024#include <linux/err.h>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +090025
AKASHI Takahiro355cdb52019-02-25 15:54:39 +090026#define BS systab.boottime
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +090027#define RT systab.runtime
28
29#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 Glass7e5f4602021-07-24 09:03:29 -060064 capsule = (typeof(capsule))hextoul(argv[1], &endp);
AKASHI Takahiro7f35ced2020-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
Simon Glassdf7d89a2021-02-07 14:27:02 -070078 ret = EFI_CALL(RT->update_capsule(&capsule, 1, 0));
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +090079 if (ret) {
80 printf("Cannot handle a capsule at %p", capsule);
81 return CMD_RET_FAILURE;
82 }
83
84 return CMD_RET_SUCCESS;
85}
86
Sughosh Ganu74075952020-12-30 19:27:11 +053087static int do_efi_capsule_on_disk_update(struct cmd_tbl *cmdtp, int flag,
88 int argc, char * const argv[])
89{
90 efi_status_t ret;
91
92 ret = efi_launch_capsules();
93
94 return ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
95}
96
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +090097/**
98 * do_efi_capsule_show() - show capsule information
99 *
100 * @cmdtp: Command table
101 * @flag: Command flag
102 * @argc: Number of arguments
103 * @argv: Argument array
104 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
105 *
106 * Implement efidebug "capsule show" sub-command.
107 * show capsule information.
108 *
109 * efidebug capsule show <capsule address>
110 */
111static int do_efi_capsule_show(struct cmd_tbl *cmdtp, int flag,
112 int argc, char * const argv[])
113{
114 struct efi_capsule_header *capsule;
115 char *endp;
116
117 if (argc != 2)
118 return CMD_RET_USAGE;
119
Simon Glass7e5f4602021-07-24 09:03:29 -0600120 capsule = (typeof(capsule))hextoul(argv[1], &endp);
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900121 if (endp == argv[1]) {
122 printf("Invalid address: %s", argv[1]);
123 return CMD_RET_FAILURE;
124 }
125
126 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
127 printf("Capsule flags: 0x%x\n", capsule->flags);
128 printf("Capsule header size: 0x%x\n", capsule->header_size);
129 printf("Capsule image size: 0x%x\n",
130 capsule->capsule_image_size);
131
132 return CMD_RET_SUCCESS;
133}
134
Jose Marinhoaa31a872021-03-11 13:18:51 +0000135#ifdef CONFIG_EFI_ESRT
136
137#define EFI_ESRT_FW_TYPE_NUM 4
138char *efi_fw_type_str[EFI_ESRT_FW_TYPE_NUM] = {"unknown", "system FW", "device FW",
139 "UEFI driver"};
140
141#define EFI_ESRT_UPDATE_STATUS_NUM 9
142char *efi_update_status_str[EFI_ESRT_UPDATE_STATUS_NUM] = {"success", "unsuccessful",
143 "insufficient resources", "incorrect version", "invalid format",
144 "auth error", "power event (AC)", "power event (batt)",
145 "unsatisfied dependencies"};
146
147#define EFI_FW_TYPE_STR_GET(idx) (\
148EFI_ESRT_FW_TYPE_NUM > (idx) ? efi_fw_type_str[(idx)] : "error"\
149)
150
151#define EFI_FW_STATUS_STR_GET(idx) (\
152EFI_ESRT_UPDATE_STATUS_NUM > (idx) ? efi_update_status_str[(idx)] : "error"\
153)
154
155/**
156 * do_efi_capsule_esrt() - manage UEFI capsules
157 *
158 * @cmdtp: Command table
159 * @flag: Command flag
160 * @argc: Number of arguments
161 * @argv: Argument array
162 * Return: CMD_RET_SUCCESS on success,
163 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
164 *
165 * Implement efidebug "capsule esrt" sub-command.
166 * The prints the current ESRT table.
167 *
168 * efidebug capsule esrt
169 */
170static int do_efi_capsule_esrt(struct cmd_tbl *cmdtp, int flag,
171 int argc, char * const argv[])
172{
173 struct efi_system_resource_table *esrt = NULL;
174
175 if (argc != 1)
176 return CMD_RET_USAGE;
177
178 for (int idx = 0; idx < systab.nr_tables; idx++)
179 if (!guidcmp(&efi_esrt_guid, &systab.tables[idx].guid))
180 esrt = (struct efi_system_resource_table *)systab.tables[idx].table;
181
182 if (!esrt) {
183 log_info("ESRT: table not present\n");
184 return CMD_RET_SUCCESS;
185 }
186
187 printf("========================================\n");
188 printf("ESRT: fw_resource_count=%d\n", esrt->fw_resource_count);
189 printf("ESRT: fw_resource_count_max=%d\n", esrt->fw_resource_count_max);
190 printf("ESRT: fw_resource_version=%lld\n", esrt->fw_resource_version);
191
192 for (int idx = 0; idx < esrt->fw_resource_count; idx++) {
193 printf("[entry %d]==============================\n", idx);
194 printf("ESRT: fw_class=%pUL\n", &esrt->entries[idx].fw_class);
195 printf("ESRT: fw_type=%s\n", EFI_FW_TYPE_STR_GET(esrt->entries[idx].fw_type));
196 printf("ESRT: fw_version=%d\n", esrt->entries[idx].fw_version);
197 printf("ESRT: lowest_supported_fw_version=%d\n",
198 esrt->entries[idx].lowest_supported_fw_version);
199 printf("ESRT: capsule_flags=%d\n",
200 esrt->entries[idx].capsule_flags);
201 printf("ESRT: last_attempt_version=%d\n",
202 esrt->entries[idx].last_attempt_version);
203 printf("ESRT: last_attempt_status=%s\n",
204 EFI_FW_STATUS_STR_GET(esrt->entries[idx].last_attempt_status));
205 }
206 printf("========================================\n");
207
208 return CMD_RET_SUCCESS;
209}
210#endif /* CONFIG_EFI_ESRT */
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900211/**
212 * do_efi_capsule_res() - show a capsule update result
213 *
214 * @cmdtp: Command table
215 * @flag: Command flag
216 * @argc: Number of arguments
217 * @argv: Argument array
218 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
219 *
220 * Implement efidebug "capsule result" sub-command.
221 * show a capsule update result.
222 * If result number is not specified, CapsuleLast will be shown.
223 *
224 * efidebug capsule result [<capsule result number>]
225 */
226static int do_efi_capsule_res(struct cmd_tbl *cmdtp, int flag,
227 int argc, char * const argv[])
228{
229 int capsule_id;
230 char *endp;
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +0200231 u16 var_name16[12];
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900232 efi_guid_t guid;
233 struct efi_capsule_result_variable_header *result = NULL;
234 efi_uintn_t size;
235 efi_status_t ret;
236
237 if (argc != 1 && argc != 2)
238 return CMD_RET_USAGE;
239
240 guid = efi_guid_capsule_report;
241 if (argc == 1) {
242 size = sizeof(var_name16);
Simon Glass156ccbc2022-01-23 12:55:12 -0700243 ret = efi_get_variable_int(u"CapsuleLast", &guid, NULL,
Heinrich Schuchardtacfe1de2021-05-24 11:10:59 +0200244 &size, var_name16, NULL);
245
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900246 if (ret != EFI_SUCCESS) {
247 if (ret == EFI_NOT_FOUND)
248 printf("CapsuleLast doesn't exist\n");
249 else
250 printf("Failed to get CapsuleLast\n");
251
252 return CMD_RET_FAILURE;
253 }
254 printf("CapsuleLast is %ls\n", var_name16);
255 } else {
256 argc--;
257 argv++;
258
Simon Glass7e5f4602021-07-24 09:03:29 -0600259 capsule_id = hextoul(argv[0], &endp);
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900260 if (capsule_id < 0 || capsule_id > 0xffff)
261 return CMD_RET_USAGE;
262
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +0200263 efi_create_indexed_name(var_name16, sizeof(var_name16),
264 "Capsule", capsule_id);
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900265 }
266
267 size = 0;
Heinrich Schuchardtacfe1de2021-05-24 11:10:59 +0200268 ret = efi_get_variable_int(var_name16, &guid, NULL, &size, NULL, NULL);
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900269 if (ret == EFI_BUFFER_TOO_SMALL) {
270 result = malloc(size);
AKASHI Takahiro30f82222021-01-22 10:42:48 +0900271 if (!result)
272 return CMD_RET_FAILURE;
Heinrich Schuchardtacfe1de2021-05-24 11:10:59 +0200273 ret = efi_get_variable_int(var_name16, &guid, NULL, &size,
274 result, NULL);
AKASHI Takahiro30f82222021-01-22 10:42:48 +0900275 }
276 if (ret != EFI_SUCCESS) {
277 free(result);
278 printf("Failed to get %ls\n", var_name16);
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900279
AKASHI Takahiro30f82222021-01-22 10:42:48 +0900280 return CMD_RET_FAILURE;
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900281 }
282
283 printf("Result total size: 0x%x\n", result->variable_total_size);
284 printf("Capsule guid: %pUl\n", &result->capsule_guid);
285 printf("Time processed: %04d-%02d-%02d %02d:%02d:%02d\n",
286 result->capsule_processed.year, result->capsule_processed.month,
287 result->capsule_processed.day, result->capsule_processed.hour,
288 result->capsule_processed.minute,
289 result->capsule_processed.second);
290 printf("Capsule status: 0x%lx\n", result->capsule_status);
291
292 free(result);
293
294 return CMD_RET_SUCCESS;
295}
296
297static struct cmd_tbl cmd_efidebug_capsule_sub[] = {
298 U_BOOT_CMD_MKENT(update, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_update,
299 "", ""),
300 U_BOOT_CMD_MKENT(show, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_show,
301 "", ""),
Jose Marinhoaa31a872021-03-11 13:18:51 +0000302#ifdef CONFIG_EFI_ESRT
303 U_BOOT_CMD_MKENT(esrt, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_esrt,
304 "", ""),
305#endif
Sughosh Ganu74075952020-12-30 19:27:11 +0530306 U_BOOT_CMD_MKENT(disk-update, 0, 0, do_efi_capsule_on_disk_update,
307 "", ""),
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +0900308 U_BOOT_CMD_MKENT(result, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_res,
309 "", ""),
310};
311
312/**
313 * do_efi_capsule() - manage UEFI capsules
314 *
315 * @cmdtp: Command table
316 * @flag: Command flag
317 * @argc: Number of arguments
318 * @argv: Argument array
319 * Return: CMD_RET_SUCCESS on success,
320 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
321 *
322 * Implement efidebug "capsule" sub-command.
323 */
324static int do_efi_capsule(struct cmd_tbl *cmdtp, int flag,
325 int argc, char * const argv[])
326{
327 struct cmd_tbl *cp;
328
329 if (argc < 2)
330 return CMD_RET_USAGE;
331
332 argc--; argv++;
333
334 cp = find_cmd_tbl(argv[0], cmd_efidebug_capsule_sub,
335 ARRAY_SIZE(cmd_efidebug_capsule_sub));
336 if (!cp)
337 return CMD_RET_USAGE;
338
339 return cp->cmd(cmdtp, flag, argc, argv);
340}
341#endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT */
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900342
343/**
Heinrich Schuchardtd4730632021-04-02 11:30:02 +0200344 * efi_get_device_path_text() - get device path text
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900345 *
Heinrich Schuchardtd4730632021-04-02 11:30:02 +0200346 * Return the text representation of the device path of a handle.
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900347 *
Heinrich Schuchardtd4730632021-04-02 11:30:02 +0200348 * @handle: handle of UEFI device
349 * Return:
350 * Pointer to the device path text or NULL.
351 * The caller is responsible for calling FreePool().
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900352 */
Heinrich Schuchardtd4730632021-04-02 11:30:02 +0200353static u16 *efi_get_device_path_text(efi_handle_t handle)
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900354{
Heinrich Schuchardtd4730632021-04-02 11:30:02 +0200355 struct efi_handler *handler;
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900356 efi_status_t ret;
357
Heinrich Schuchardtd4730632021-04-02 11:30:02 +0200358 ret = efi_search_protocol(handle, &efi_guid_device_path, &handler);
359 if (ret == EFI_SUCCESS && handler->protocol_interface) {
360 struct efi_device_path *dp = handler->protocol_interface;
361
362 return efi_dp_str(dp);
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900363 } else {
Heinrich Schuchardtd4730632021-04-02 11:30:02 +0200364 return NULL;
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900365 }
366}
367
368#define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
369
370static const char spc[] = " ";
371static const char sep[] = "================";
372
373/**
374 * do_efi_show_devices() - show UEFI devices
375 *
376 * @cmdtp: Command table
377 * @flag: Command flag
378 * @argc: Number of arguments
379 * @argv: Argument array
380 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
381 *
382 * Implement efidebug "devices" sub-command.
383 * Show all UEFI devices and their information.
384 */
Simon Glass09140112020-05-10 11:40:03 -0600385static int do_efi_show_devices(struct cmd_tbl *cmdtp, int flag,
386 int argc, char *const argv[])
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900387{
388 efi_handle_t *handles;
389 efi_uintn_t num, i;
390 u16 *dev_path_text;
391 efi_status_t ret;
392
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200393 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900394 &num, &handles));
395 if (ret != EFI_SUCCESS)
396 return CMD_RET_FAILURE;
397
398 if (!num)
399 return CMD_RET_SUCCESS;
400
401 printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc);
402 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
403 for (i = 0; i < num; i++) {
Heinrich Schuchardtd4730632021-04-02 11:30:02 +0200404 dev_path_text = efi_get_device_path_text(handles[i]);
405 if (dev_path_text) {
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900406 printf("%p %ls\n", handles[i], dev_path_text);
407 efi_free_pool(dev_path_text);
408 }
409 }
410
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200411 efi_free_pool(handles);
AKASHI Takahiro355cdb52019-02-25 15:54:39 +0900412
413 return CMD_RET_SUCCESS;
414}
415
416/**
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900417 * efi_get_driver_handle_info() - get information of UEFI driver
418 *
419 * @handle: Handle of UEFI device
420 * @driver_name: Driver name
421 * @image_path: Pointer to text of device path
422 * Return: 0 on success, -1 on failure
423 *
424 * Currently return no useful information as all UEFI drivers are
425 * built-in..
426 */
427static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
428 u16 **image_path)
429{
430 struct efi_handler *handler;
431 struct efi_loaded_image *image;
432 efi_status_t ret;
433
434 /*
435 * driver name
436 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
437 */
438 *driver_name = NULL;
439
440 /* image name */
441 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
442 if (ret != EFI_SUCCESS) {
443 *image_path = NULL;
444 return 0;
445 }
446
447 image = handler->protocol_interface;
448 *image_path = efi_dp_str(image->file_path);
449
450 return 0;
451}
452
453/**
454 * do_efi_show_drivers() - show UEFI drivers
455 *
456 * @cmdtp: Command table
457 * @flag: Command flag
458 * @argc: Number of arguments
459 * @argv: Argument array
460 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
461 *
462 * Implement efidebug "drivers" sub-command.
463 * Show all UEFI drivers and their information.
464 */
Simon Glass09140112020-05-10 11:40:03 -0600465static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
466 int argc, char *const argv[])
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900467{
468 efi_handle_t *handles;
469 efi_uintn_t num, i;
470 u16 *driver_name, *image_path_text;
471 efi_status_t ret;
472
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200473 ret = EFI_CALL(efi_locate_handle_buffer(
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900474 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
475 NULL, &num, &handles));
476 if (ret != EFI_SUCCESS)
477 return CMD_RET_FAILURE;
478
479 if (!num)
480 return CMD_RET_SUCCESS;
481
482 printf("Driver%.*s Name Image Path\n",
483 EFI_HANDLE_WIDTH - 6, spc);
484 printf("%.*s ==================== ====================\n",
485 EFI_HANDLE_WIDTH, sep);
486 for (i = 0; i < num; i++) {
487 if (!efi_get_driver_handle_info(handles[i], &driver_name,
488 &image_path_text)) {
489 if (image_path_text)
490 printf("%p %-20ls %ls\n", handles[i],
491 driver_name, image_path_text);
492 else
493 printf("%p %-20ls <built-in>\n",
494 handles[i], driver_name);
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200495 efi_free_pool(driver_name);
496 efi_free_pool(image_path_text);
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900497 }
498 }
499
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200500 efi_free_pool(handles);
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900501
502 return CMD_RET_SUCCESS;
503}
504
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900505/**
506 * do_efi_show_handles() - show UEFI handles
507 *
508 * @cmdtp: Command table
509 * @flag: Command flag
510 * @argc: Number of arguments
511 * @argv: Argument array
512 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
513 *
514 * Implement efidebug "dh" sub-command.
515 * Show all UEFI handles and their information, currently all protocols
516 * added to handle.
517 */
Simon Glass09140112020-05-10 11:40:03 -0600518static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
519 int argc, char *const argv[])
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900520{
521 efi_handle_t *handles;
522 efi_guid_t **guid;
523 efi_uintn_t num, count, i, j;
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900524 efi_status_t ret;
525
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200526 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900527 &num, &handles));
528 if (ret != EFI_SUCCESS)
529 return CMD_RET_FAILURE;
530
531 if (!num)
532 return CMD_RET_SUCCESS;
533
534 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
535 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
536 for (i = 0; i < num; i++) {
537 printf("%p", handles[i]);
538 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
539 &count));
540 if (ret || !count) {
541 putc('\n');
542 continue;
543 }
544
545 for (j = 0; j < count; j++) {
546 if (j)
547 printf(", ");
548 else
549 putc(' ');
550
Heinrich Schuchardt3adae642022-01-16 14:10:23 +0100551 printf("%pUs", guid[j]);
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900552 }
553 putc('\n');
554 }
555
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200556 efi_free_pool(handles);
AKASHI Takahiroa8014622019-02-25 15:54:41 +0900557
558 return CMD_RET_SUCCESS;
559}
560
AKASHI Takahiro66eaf562019-02-25 15:54:40 +0900561/**
AKASHI Takahirofa536732019-02-25 15:54:42 +0900562 * do_efi_show_images() - show UEFI images
563 *
564 * @cmdtp: Command table
565 * @flag: Command flag
566 * @argc: Number of arguments
567 * @argv: Argument array
568 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
569 *
570 * Implement efidebug "images" sub-command.
571 * Show all UEFI loaded images and their information.
572 */
Simon Glass09140112020-05-10 11:40:03 -0600573static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
574 int argc, char *const argv[])
AKASHI Takahirofa536732019-02-25 15:54:42 +0900575{
576 efi_print_image_infos(NULL);
577
578 return CMD_RET_SUCCESS;
579}
580
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900581static const char * const efi_mem_type_string[] = {
582 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
583 [EFI_LOADER_CODE] = "LOADER CODE",
584 [EFI_LOADER_DATA] = "LOADER DATA",
585 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
586 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
587 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
588 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
589 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
590 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
591 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
592 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
593 [EFI_MMAP_IO] = "IO",
594 [EFI_MMAP_IO_PORT] = "IO PORT",
595 [EFI_PAL_CODE] = "PAL",
Heinrich Schuchardtdd9056c2020-04-29 20:21:39 +0200596 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900597};
598
599static const struct efi_mem_attrs {
600 const u64 bit;
601 const char *text;
602} efi_mem_attrs[] = {
603 {EFI_MEMORY_UC, "UC"},
604 {EFI_MEMORY_UC, "UC"},
605 {EFI_MEMORY_WC, "WC"},
606 {EFI_MEMORY_WT, "WT"},
607 {EFI_MEMORY_WB, "WB"},
608 {EFI_MEMORY_UCE, "UCE"},
609 {EFI_MEMORY_WP, "WP"},
610 {EFI_MEMORY_RP, "RP"},
611 {EFI_MEMORY_XP, "WP"},
612 {EFI_MEMORY_NV, "NV"},
613 {EFI_MEMORY_MORE_RELIABLE, "REL"},
614 {EFI_MEMORY_RO, "RO"},
Heinrich Schuchardt255a4732020-05-19 07:20:46 +0200615 {EFI_MEMORY_SP, "SP"},
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900616 {EFI_MEMORY_RUNTIME, "RT"},
617};
618
619/**
620 * print_memory_attributes() - print memory map attributes
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200621 *
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900622 * @attributes: Attribute value
623 *
624 * Print memory map attributes
625 */
626static void print_memory_attributes(u64 attributes)
627{
628 int sep, i;
629
630 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
631 if (attributes & efi_mem_attrs[i].bit) {
632 if (sep) {
633 putc('|');
634 } else {
635 putc(' ');
636 sep = 1;
637 }
638 puts(efi_mem_attrs[i].text);
639 }
640}
641
642#define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
643
644/**
645 * do_efi_show_memmap() - show UEFI memory map
646 *
647 * @cmdtp: Command table
648 * @flag: Command flag
649 * @argc: Number of arguments
650 * @argv: Argument array
651 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
652 *
653 * Implement efidebug "memmap" sub-command.
654 * Show UEFI memory map.
655 */
Simon Glass09140112020-05-10 11:40:03 -0600656static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
657 int argc, char *const argv[])
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900658{
659 struct efi_mem_desc *memmap = NULL, *map;
660 efi_uintn_t map_size = 0;
661 const char *type;
662 int i;
663 efi_status_t ret;
664
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200665 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900666 if (ret == EFI_BUFFER_TOO_SMALL) {
667 map_size += sizeof(struct efi_mem_desc); /* for my own */
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200668 ret = efi_allocate_pool(EFI_LOADER_DATA, map_size,
669 (void *)&memmap);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900670 if (ret != EFI_SUCCESS)
671 return CMD_RET_FAILURE;
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200672 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900673 }
674 if (ret != EFI_SUCCESS) {
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200675 efi_free_pool(memmap);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900676 return CMD_RET_FAILURE;
677 }
678
679 printf("Type Start%.*s End%.*s Attributes\n",
680 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
681 printf("================ %.*s %.*s ==========\n",
682 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
AKASHI Takahiroe2a5b862020-05-08 14:50:18 +0900683 /*
684 * Coverity check: dereferencing null pointer "map."
685 * This is a false positive as memmap will always be
686 * populated by allocate_pool() above.
687 */
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900688 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
Heinrich Schuchardtdd9056c2020-04-29 20:21:39 +0200689 if (map->type < ARRAY_SIZE(efi_mem_type_string))
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900690 type = efi_mem_type_string[map->type];
691 else
692 type = "(unknown)";
693
694 printf("%-16s %.*llx-%.*llx", type,
695 EFI_PHYS_ADDR_WIDTH,
Heinrich Schuchardt6c0ef352020-03-27 04:33:17 +0000696 (u64)map_to_sysmem((void *)(uintptr_t)
697 map->physical_start),
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900698 EFI_PHYS_ADDR_WIDTH,
Heinrich Schuchardt6c0ef352020-03-27 04:33:17 +0000699 (u64)map_to_sysmem((void *)(uintptr_t)
700 (map->physical_start +
701 map->num_pages * EFI_PAGE_SIZE)));
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900702
703 print_memory_attributes(map->attribute);
704 putc('\n');
705 }
706
Heinrich Schuchardta30c7232020-05-02 16:08:37 +0200707 efi_free_pool(memmap);
AKASHI Takahiro00358bb2019-02-25 15:54:43 +0900708
709 return CMD_RET_SUCCESS;
710}
711
AKASHI Takahirofa536732019-02-25 15:54:42 +0900712/**
Heinrich Schuchardt986e0642020-01-07 05:57:47 +0100713 * do_efi_show_tables() - show UEFI configuration tables
714 *
715 * @cmdtp: Command table
716 * @flag: Command flag
717 * @argc: Number of arguments
718 * @argv: Argument array
719 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
720 *
721 * Implement efidebug "tables" sub-command.
722 * Show UEFI configuration tables.
723 */
Simon Glass09140112020-05-10 11:40:03 -0600724static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
725 int argc, char *const argv[])
Heinrich Schuchardt986e0642020-01-07 05:57:47 +0100726{
727 efi_uintn_t i;
Heinrich Schuchardt986e0642020-01-07 05:57:47 +0100728
Heinrich Schuchardt3adae642022-01-16 14:10:23 +0100729 for (i = 0; i < systab.nr_tables; ++i)
730 printf("%pUl (%pUs)\n",
731 &systab.tables[i].guid, &systab.tables[i].guid);
Heinrich Schuchardt986e0642020-01-07 05:57:47 +0100732
733 return CMD_RET_SUCCESS;
734}
735
736/**
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100737 * create_initrd_dp() - create a special device for our Boot### option
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200738 *
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100739 * @dev: device
740 * @part: disk partition
741 * @file: filename
742 * @shortform: create short form device path
743 * Return: pointer to the device path or ERR_PTR
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200744 */
745static
746struct efi_device_path *create_initrd_dp(const char *dev, const char *part,
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100747 const char *file, int shortform)
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200748
749{
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100750 struct efi_device_path *tmp_dp = NULL, *tmp_fp = NULL, *short_fp = NULL;
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200751 struct efi_device_path *initrd_dp = NULL;
752 efi_status_t ret;
753 const struct efi_initrd_dp id_dp = {
754 .vendor = {
755 {
756 DEVICE_PATH_TYPE_MEDIA_DEVICE,
757 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
758 sizeof(id_dp.vendor),
759 },
760 EFI_INITRD_MEDIA_GUID,
761 },
762 .end = {
763 DEVICE_PATH_TYPE_END,
764 DEVICE_PATH_SUB_TYPE_END,
765 sizeof(id_dp.end),
766 }
767 };
768
769 ret = efi_dp_from_name(dev, part, file, &tmp_dp, &tmp_fp);
770 if (ret != EFI_SUCCESS) {
771 printf("Cannot create device path for \"%s %s\"\n", part, file);
772 goto out;
773 }
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100774 if (shortform)
775 short_fp = efi_dp_shorten(tmp_fp);
776 if (!short_fp)
777 short_fp = tmp_fp;
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200778
779 initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp,
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100780 short_fp);
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200781
782out:
783 efi_free_pool(tmp_dp);
784 efi_free_pool(tmp_fp);
785 return initrd_dp;
786}
787
788/**
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900789 * do_efi_boot_add() - set UEFI load option
790 *
791 * @cmdtp: Command table
792 * @flag: Command flag
793 * @argc: Number of arguments
794 * @argv: Argument array
795 * Return: CMD_RET_SUCCESS on success,
796 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
797 *
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200798 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
799 *
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200800 * efidebug boot add -b <id> <label> <interface> <devnum>[:<part>] <file>
801 * -i <file> <interface2> <devnum2>[:<part>] <initrd>
802 * -s '<options>'
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900803 */
Simon Glass09140112020-05-10 11:40:03 -0600804static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
805 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900806{
807 int id;
808 char *endp;
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +0200809 u16 var_name16[9];
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900810 efi_guid_t guid;
811 size_t label_len, label_len16;
812 u16 *label;
Heinrich Schuchardtccc41fc2022-03-23 20:26:25 +0100813 struct efi_device_path *file_path = NULL;
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100814 struct efi_device_path *fp_free = NULL;
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200815 struct efi_device_path *final_fp = NULL;
816 struct efi_device_path *initrd_dp = NULL;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900817 struct efi_load_option lo;
818 void *data = NULL;
819 efi_uintn_t size;
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200820 efi_uintn_t fp_size = 0;
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200821 efi_status_t ret;
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200822 int r = CMD_RET_SUCCESS;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900823
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900824 guid = efi_global_variable_guid;
825
826 /* attributes */
827 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200828 lo.optional_data = NULL;
829 lo.label = NULL;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900830
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200831 argc--;
832 argv++; /* 'add' */
833 for (; argc > 0; argc--, argv++) {
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100834 int shortform;
835
836 if (*argv[0] != '-' || strlen(argv[0]) != 2) {
837 r = CMD_RET_USAGE;
838 goto out;
839 }
840 shortform = 0;
841 switch (argv[0][1]) {
842 case 'b':
843 shortform = 1;
844 /* fallthrough */
845 case 'B':
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200846 if (argc < 5 || lo.label) {
847 r = CMD_RET_USAGE;
848 goto out;
849 }
Simon Glass7e5f4602021-07-24 09:03:29 -0600850 id = (int)hextoul(argv[1], &endp);
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200851 if (*endp != '\0' || id > 0xffff)
852 return CMD_RET_USAGE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900853
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +0200854 efi_create_indexed_name(var_name16, sizeof(var_name16),
855 "Boot", id);
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200856
857 /* label */
858 label_len = strlen(argv[2]);
859 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
860 label = malloc((label_len16 + 1) * sizeof(u16));
861 if (!label)
862 return CMD_RET_FAILURE;
863 lo.label = label; /* label will be changed below */
864 utf8_utf16_strncpy(&label, argv[2], label_len);
865
866 /* file path */
867 ret = efi_dp_from_name(argv[3], argv[4], argv[5],
Heinrich Schuchardtccc41fc2022-03-23 20:26:25 +0100868 NULL, &fp_free);
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200869 if (ret != EFI_SUCCESS) {
870 printf("Cannot create device path for \"%s %s\"\n",
871 argv[3], argv[4]);
872 r = CMD_RET_FAILURE;
873 goto out;
874 }
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100875 if (shortform)
876 file_path = efi_dp_shorten(fp_free);
877 if (!file_path)
878 file_path = fp_free;
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200879 fp_size += efi_dp_size(file_path) +
880 sizeof(struct efi_device_path);
881 argc -= 5;
882 argv += 5;
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100883 break;
884 case 'i':
885 shortform = 1;
886 /* fallthrough */
887 case 'I':
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200888 if (argc < 3 || initrd_dp) {
889 r = CMD_RET_USAGE;
890 goto out;
891 }
892
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100893 initrd_dp = create_initrd_dp(argv[1], argv[2], argv[3],
894 shortform);
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200895 if (!initrd_dp) {
896 printf("Cannot add an initrd\n");
897 r = CMD_RET_FAILURE;
898 goto out;
899 }
900 argc -= 3;
901 argv += 3;
902 fp_size += efi_dp_size(initrd_dp) +
903 sizeof(struct efi_device_path);
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100904 break;
905 case 's':
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200906 if (argc < 1 || lo.optional_data) {
907 r = CMD_RET_USAGE;
908 goto out;
909 }
910 lo.optional_data = (const u8 *)argv[1];
911 argc -= 1;
912 argv += 1;
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100913 break;
914 default:
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200915 r = CMD_RET_USAGE;
916 goto out;
917 }
918 }
919
920 if (!file_path) {
921 printf("Missing binary\n");
922 r = CMD_RET_USAGE;
923 goto out;
924 }
925
926 final_fp = efi_dp_concat(file_path, initrd_dp);
927 if (!final_fp) {
928 printf("Cannot create final device path\n");
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200929 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900930 goto out;
931 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900932
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200933 lo.file_path = final_fp;
934 lo.file_path_length = fp_size;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900935
936 size = efi_serialize_load_option(&lo, (u8 **)&data);
937 if (!size) {
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200938 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900939 goto out;
940 }
941
Heinrich Schuchardtacfe1de2021-05-24 11:10:59 +0200942 ret = efi_set_variable_int(var_name16, &guid,
943 EFI_VARIABLE_NON_VOLATILE |
944 EFI_VARIABLE_BOOTSERVICE_ACCESS |
945 EFI_VARIABLE_RUNTIME_ACCESS,
946 size, data, false);
Heinrich Schuchardta332f252019-06-20 12:59:45 +0200947 if (ret != EFI_SUCCESS) {
948 printf("Cannot set %ls\n", var_name16);
949 r = CMD_RET_FAILURE;
950 }
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200951
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900952out:
953 free(data);
Ilias Apalodimascbea2412021-03-17 21:55:01 +0200954 efi_free_pool(final_fp);
955 efi_free_pool(initrd_dp);
Heinrich Schuchardt63276a52022-02-26 12:10:10 +0100956 efi_free_pool(fp_free);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900957 free(lo.label);
958
Heinrich Schuchardt428a4702019-06-20 12:48:04 +0200959 return r;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900960}
961
962/**
963 * do_efi_boot_rm() - delete UEFI load options
964 *
965 * @cmdtp: Command table
966 * @flag: Command flag
967 * @argc: Number of arguments
968 * @argv: Argument array
969 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
970 *
971 * Implement efidebug "boot rm" sub-command.
972 * Delete UEFI load options.
Heinrich Schuchardt0b016562019-07-14 14:00:41 +0200973 *
974 * efidebug boot rm <id> ...
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900975 */
Simon Glass09140112020-05-10 11:40:03 -0600976static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag,
977 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900978{
979 efi_guid_t guid;
980 int id, i;
981 char *endp;
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +0200982 u16 var_name16[9];
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900983 efi_status_t ret;
984
985 if (argc == 1)
986 return CMD_RET_USAGE;
987
988 guid = efi_global_variable_guid;
989 for (i = 1; i < argc; i++, argv++) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600990 id = (int)hextoul(argv[1], &endp);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900991 if (*endp != '\0' || id > 0xffff)
992 return CMD_RET_FAILURE;
993
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +0200994 efi_create_indexed_name(var_name16, sizeof(var_name16),
995 "Boot", id);
Heinrich Schuchardtacfe1de2021-05-24 11:10:59 +0200996 ret = efi_set_variable_int(var_name16, &guid, 0, 0, NULL,
997 false);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +0900998 if (ret) {
Heinrich Schuchardt30efb5d2020-03-02 20:13:10 +0100999 printf("Cannot remove %ls\n", var_name16);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001000 return CMD_RET_FAILURE;
1001 }
1002 }
1003
1004 return CMD_RET_SUCCESS;
1005}
1006
1007/**
1008 * show_efi_boot_opt_data() - dump UEFI load option
1009 *
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001010 * @varname16: variable name
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +02001011 * @data: value of UEFI load option variable
1012 * @size: size of the boot option
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001013 *
1014 * Decode the value of UEFI load option variable and print information.
1015 */
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +02001016static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001017{
Ilias Apalodimascbea2412021-03-17 21:55:01 +02001018 struct efi_device_path *initrd_path = NULL;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001019 struct efi_load_option lo;
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +02001020 efi_status_t ret;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001021
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +02001022 ret = efi_deserialize_load_option(&lo, data, size);
1023 if (ret != EFI_SUCCESS) {
1024 printf("%ls: invalid load option\n", varname16);
1025 return;
1026 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001027
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001028 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
1029 varname16,
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001030 /* ACTIVE */
1031 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
1032 /* FORCE RECONNECT */
1033 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
1034 /* HIDDEN */
1035 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
1036 lo.attributes);
Heinrich Schuchardtcd5a87e2021-05-24 10:35:25 +02001037 printf(" label: %ls\n", lo.label);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001038
Heinrich Schuchardtfc42b8b2021-10-15 01:47:40 +02001039 printf(" file_path: %pD\n", lo.file_path);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001040
Heinrich Schuchardt9ad37fe2021-10-15 02:33:33 +02001041 initrd_path = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
Ilias Apalodimascbea2412021-03-17 21:55:01 +02001042 if (initrd_path) {
Heinrich Schuchardtfc42b8b2021-10-15 01:47:40 +02001043 printf(" initrd_path: %pD\n", initrd_path);
Ilias Apalodimascbea2412021-03-17 21:55:01 +02001044 efi_free_pool(initrd_path);
1045 }
1046
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +02001047 printf(" data:\n");
1048 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +02001049 lo.optional_data, *size, true);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001050}
1051
1052/**
1053 * show_efi_boot_opt() - dump UEFI load option
1054 *
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001055 * @varname16: variable name
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001056 *
1057 * Dump information defined by UEFI load option.
1058 */
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001059static void show_efi_boot_opt(u16 *varname16)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001060{
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001061 void *data;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001062 efi_uintn_t size;
AKASHI Takahiro0bffb8c2019-11-26 10:11:22 +09001063 efi_status_t ret;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001064
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001065 size = 0;
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001066 ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
1067 NULL, &size, NULL));
AKASHI Takahiro0bffb8c2019-11-26 10:11:22 +09001068 if (ret == EFI_BUFFER_TOO_SMALL) {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001069 data = malloc(size);
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001070 if (!data) {
1071 printf("ERROR: Out of memory\n");
1072 return;
1073 }
1074 ret = EFI_CALL(efi_get_variable(varname16,
1075 &efi_global_variable_guid,
1076 NULL, &size, data));
1077 if (ret == EFI_SUCCESS)
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +02001078 show_efi_boot_opt_data(varname16, data, &size);
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001079 free(data);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001080 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001081}
1082
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001083static int u16_tohex(u16 c)
1084{
1085 if (c >= '0' && c <= '9')
1086 return c - '0';
1087 if (c >= 'A' && c <= 'F')
1088 return c - 'A' + 10;
1089
1090 /* not hexadecimal */
1091 return -1;
1092}
1093
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001094/**
1095 * show_efi_boot_dump() - dump all UEFI load options
1096 *
1097 * @cmdtp: Command table
1098 * @flag: Command flag
1099 * @argc: Number of arguments
1100 * @argv: Argument array
1101 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1102 *
1103 * Implement efidebug "boot dump" sub-command.
1104 * Dump information of all UEFI load options defined.
Heinrich Schuchardta6ccba02019-07-25 20:52:23 +02001105 *
1106 * efidebug boot dump
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001107 */
Simon Glass09140112020-05-10 11:40:03 -06001108static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
1109 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001110{
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001111 u16 *var_name16, *p;
1112 efi_uintn_t buf_size, size;
1113 efi_guid_t guid;
1114 int id, i, digit;
1115 efi_status_t ret;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001116
1117 if (argc > 1)
1118 return CMD_RET_USAGE;
1119
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001120 buf_size = 128;
1121 var_name16 = malloc(buf_size);
1122 if (!var_name16)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001123 return CMD_RET_FAILURE;
1124
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001125 var_name16[0] = 0;
1126 for (;;) {
1127 size = buf_size;
1128 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
1129 &guid));
1130 if (ret == EFI_NOT_FOUND)
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001131 break;
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001132 if (ret == EFI_BUFFER_TOO_SMALL) {
1133 buf_size = size;
1134 p = realloc(var_name16, buf_size);
1135 if (!p) {
1136 free(var_name16);
1137 return CMD_RET_FAILURE;
1138 }
1139 var_name16 = p;
1140 ret = EFI_CALL(efi_get_next_variable_name(&size,
1141 var_name16,
1142 &guid));
1143 }
1144 if (ret != EFI_SUCCESS) {
1145 free(var_name16);
1146 return CMD_RET_FAILURE;
1147 }
1148
Simon Glass156ccbc2022-01-23 12:55:12 -07001149 if (memcmp(var_name16, u"Boot", 8))
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001150 continue;
1151
1152 for (id = 0, i = 0; i < 4; i++) {
1153 digit = u16_tohex(var_name16[4 + i]);
1154 if (digit < 0)
1155 break;
1156 id = (id << 4) + digit;
1157 }
1158 if (i == 4 && !var_name16[8])
Heinrich Schuchardtb5f4e9e2020-04-29 19:20:35 +02001159 show_efi_boot_opt(var_name16);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001160 }
AKASHI Takahiroffe21572019-04-26 09:44:18 +09001161
1162 free(var_name16);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001163
1164 return CMD_RET_SUCCESS;
1165}
1166
1167/**
1168 * show_efi_boot_order() - show order of UEFI load options
1169 *
1170 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1171 *
1172 * Show order of UEFI load options defined by BootOrder variable.
1173 */
1174static int show_efi_boot_order(void)
1175{
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001176 u16 *bootorder;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001177 efi_uintn_t size;
1178 int num, i;
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +02001179 u16 var_name16[9];
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001180 void *data;
1181 struct efi_load_option lo;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001182 efi_status_t ret;
1183
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001184 size = 0;
Simon Glass156ccbc2022-01-23 12:55:12 -07001185 ret = EFI_CALL(efi_get_variable(u"BootOrder", &efi_global_variable_guid,
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001186 NULL, &size, NULL));
1187 if (ret != EFI_BUFFER_TOO_SMALL) {
1188 if (ret == EFI_NOT_FOUND) {
1189 printf("BootOrder not defined\n");
1190 return CMD_RET_SUCCESS;
1191 } else {
1192 return CMD_RET_FAILURE;
1193 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001194 }
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001195 bootorder = malloc(size);
1196 if (!bootorder) {
1197 printf("ERROR: Out of memory\n");
1198 return CMD_RET_FAILURE;
1199 }
Simon Glass156ccbc2022-01-23 12:55:12 -07001200 ret = EFI_CALL(efi_get_variable(u"BootOrder", &efi_global_variable_guid,
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001201 NULL, &size, bootorder));
1202 if (ret != EFI_SUCCESS) {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001203 ret = CMD_RET_FAILURE;
1204 goto out;
1205 }
1206
1207 num = size / sizeof(u16);
1208 for (i = 0; i < num; i++) {
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +02001209 efi_create_indexed_name(var_name16, sizeof(var_name16),
Heinrich Schuchardt2b8723c2021-06-12 00:01:44 +02001210 "Boot", bootorder[i]);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001211
1212 size = 0;
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001213 ret = EFI_CALL(efi_get_variable(var_name16,
1214 &efi_global_variable_guid, NULL,
1215 &size, NULL));
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001216 if (ret != EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +02001217 printf("%2d: %ls: (not defined)\n", i + 1, var_name16);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001218 continue;
1219 }
1220
1221 data = malloc(size);
1222 if (!data) {
1223 ret = CMD_RET_FAILURE;
1224 goto out;
1225 }
Heinrich Schuchardtf9f5f922020-04-29 21:15:08 +02001226 ret = EFI_CALL(efi_get_variable(var_name16,
1227 &efi_global_variable_guid, NULL,
1228 &size, data));
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001229 if (ret != EFI_SUCCESS) {
1230 free(data);
1231 ret = CMD_RET_FAILURE;
1232 goto out;
1233 }
1234
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +02001235 ret = efi_deserialize_load_option(&lo, data, &size);
1236 if (ret != EFI_SUCCESS) {
1237 printf("%ls: invalid load option\n", var_name16);
1238 ret = CMD_RET_FAILURE;
1239 goto out;
1240 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001241
Heinrich Schuchardt2ecee312021-05-24 10:57:03 +02001242 printf("%2d: %ls: %ls\n", i + 1, var_name16, lo.label);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001243
1244 free(data);
1245 }
1246out:
1247 free(bootorder);
1248
1249 return ret;
1250}
1251
1252/**
1253 * do_efi_boot_next() - manage UEFI BootNext variable
1254 *
1255 * @cmdtp: Command table
1256 * @flag: Command flag
1257 * @argc: Number of arguments
1258 * @argv: Argument array
1259 * Return: CMD_RET_SUCCESS on success,
1260 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1261 *
1262 * Implement efidebug "boot next" sub-command.
1263 * Set BootNext variable.
Heinrich Schuchardt0b016562019-07-14 14:00:41 +02001264 *
1265 * efidebug boot next <id>
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001266 */
Simon Glass09140112020-05-10 11:40:03 -06001267static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
1268 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001269{
1270 u16 bootnext;
1271 efi_uintn_t size;
1272 char *endp;
1273 efi_guid_t guid;
1274 efi_status_t ret;
Heinrich Schuchardta332f252019-06-20 12:59:45 +02001275 int r = CMD_RET_SUCCESS;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001276
1277 if (argc != 2)
1278 return CMD_RET_USAGE;
1279
Simon Glass7e5f4602021-07-24 09:03:29 -06001280 bootnext = (u16)hextoul(argv[1], &endp);
Heinrich Schuchardtbdb15772020-05-09 17:59:19 +02001281 if (*endp) {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001282 printf("invalid value: %s\n", argv[1]);
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001283 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001284 goto out;
1285 }
1286
1287 guid = efi_global_variable_guid;
1288 size = sizeof(u16);
Simon Glass156ccbc2022-01-23 12:55:12 -07001289 ret = efi_set_variable_int(u"BootNext", &guid,
1290 EFI_VARIABLE_NON_VOLATILE |
1291 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1292 EFI_VARIABLE_RUNTIME_ACCESS,
1293 size, &bootnext, false);
Heinrich Schuchardta332f252019-06-20 12:59:45 +02001294 if (ret != EFI_SUCCESS) {
1295 printf("Cannot set BootNext\n");
1296 r = CMD_RET_FAILURE;
1297 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001298out:
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001299 return r;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001300}
1301
1302/**
1303 * do_efi_boot_order() - manage UEFI BootOrder variable
1304 *
1305 * @cmdtp: Command table
1306 * @flag: Command flag
1307 * @argc: Number of arguments
1308 * @argv: Argument array
1309 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1310 *
1311 * Implement efidebug "boot order" sub-command.
1312 * Show order of UEFI load options, or change it in BootOrder variable.
Heinrich Schuchardt0b016562019-07-14 14:00:41 +02001313 *
1314 * efidebug boot order [<id> ...]
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001315 */
Simon Glass09140112020-05-10 11:40:03 -06001316static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
1317 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001318{
1319 u16 *bootorder = NULL;
1320 efi_uintn_t size;
1321 int id, i;
1322 char *endp;
1323 efi_guid_t guid;
1324 efi_status_t ret;
Heinrich Schuchardta332f252019-06-20 12:59:45 +02001325 int r = CMD_RET_SUCCESS;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001326
1327 if (argc == 1)
1328 return show_efi_boot_order();
1329
1330 argc--;
1331 argv++;
1332
1333 size = argc * sizeof(u16);
1334 bootorder = malloc(size);
1335 if (!bootorder)
1336 return CMD_RET_FAILURE;
1337
1338 for (i = 0; i < argc; i++) {
Simon Glass7e5f4602021-07-24 09:03:29 -06001339 id = (int)hextoul(argv[i], &endp);
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001340 if (*endp != '\0' || id > 0xffff) {
1341 printf("invalid value: %s\n", argv[i]);
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001342 r = CMD_RET_FAILURE;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001343 goto out;
1344 }
1345
1346 bootorder[i] = (u16)id;
1347 }
1348
1349 guid = efi_global_variable_guid;
Simon Glass156ccbc2022-01-23 12:55:12 -07001350 ret = efi_set_variable_int(u"BootOrder", &guid,
1351 EFI_VARIABLE_NON_VOLATILE |
1352 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1353 EFI_VARIABLE_RUNTIME_ACCESS,
1354 size, bootorder, true);
Heinrich Schuchardta332f252019-06-20 12:59:45 +02001355 if (ret != EFI_SUCCESS) {
1356 printf("Cannot set BootOrder\n");
1357 r = CMD_RET_FAILURE;
1358 }
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001359out:
1360 free(bootorder);
1361
Heinrich Schuchardt428a4702019-06-20 12:48:04 +02001362 return r;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001363}
1364
Simon Glass09140112020-05-10 11:40:03 -06001365static struct cmd_tbl cmd_efidebug_boot_sub[] = {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001366 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1367 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1368 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1369 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1370 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1371 "", ""),
1372};
1373
1374/**
1375 * do_efi_boot_opt() - manage UEFI load options
1376 *
1377 * @cmdtp: Command table
1378 * @flag: Command flag
1379 * @argc: Number of arguments
1380 * @argv: Argument array
1381 * Return: CMD_RET_SUCCESS on success,
1382 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1383 *
1384 * Implement efidebug "boot" sub-command.
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001385 */
Simon Glass09140112020-05-10 11:40:03 -06001386static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
1387 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001388{
Simon Glass09140112020-05-10 11:40:03 -06001389 struct cmd_tbl *cp;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001390
1391 if (argc < 2)
1392 return CMD_RET_USAGE;
1393
1394 argc--; argv++;
1395
1396 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1397 ARRAY_SIZE(cmd_efidebug_boot_sub));
1398 if (!cp)
1399 return CMD_RET_USAGE;
1400
1401 return cp->cmd(cmdtp, flag, argc, argv);
1402}
1403
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001404/**
1405 * do_efi_test_bootmgr() - run simple bootmgr for test
1406 *
1407 * @cmdtp: Command table
1408 * @flag: Command flag
1409 * @argc: Number of arguments
1410 * @argv: Argument array
1411 * Return: CMD_RET_SUCCESS on success,
1412 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1413 *
1414 * Implement efidebug "test bootmgr" sub-command.
1415 * Run simple bootmgr for test.
1416 *
1417 * efidebug test bootmgr
1418 */
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +01001419static __maybe_unused int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
1420 int argc, char * const argv[])
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001421{
1422 efi_handle_t image;
1423 efi_uintn_t exit_data_size = 0;
1424 u16 *exit_data = NULL;
1425 efi_status_t ret;
Heinrich Schuchardtbc78d222020-08-11 18:20:50 +02001426 void *load_options = NULL;
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001427
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +02001428 ret = efi_bootmgr_load(&image, &load_options);
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001429 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1430
1431 /* We call efi_start_image() even if error for test purpose. */
1432 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1433 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1434 if (ret && exit_data)
1435 efi_free_pool(exit_data);
1436
1437 efi_restore_gd();
1438
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +02001439 free(load_options);
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001440 return CMD_RET_SUCCESS;
1441}
1442
Simon Glass09140112020-05-10 11:40:03 -06001443static struct cmd_tbl cmd_efidebug_test_sub[] = {
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +01001444#ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001445 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1446 "", ""),
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +01001447#endif
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001448};
1449
1450/**
1451 * do_efi_test() - manage UEFI load options
1452 *
1453 * @cmdtp: Command table
1454 * @flag: Command flag
1455 * @argc: Number of arguments
1456 * @argv: Argument array
1457 * Return: CMD_RET_SUCCESS on success,
1458 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1459 *
1460 * Implement efidebug "test" sub-command.
1461 */
Simon Glass09140112020-05-10 11:40:03 -06001462static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001463 int argc, char * const argv[])
1464{
Simon Glass09140112020-05-10 11:40:03 -06001465 struct cmd_tbl *cp;
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001466
1467 if (argc < 2)
1468 return CMD_RET_USAGE;
1469
1470 argc--; argv++;
1471
1472 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1473 ARRAY_SIZE(cmd_efidebug_test_sub));
1474 if (!cp)
1475 return CMD_RET_USAGE;
1476
1477 return cp->cmd(cmdtp, flag, argc, argv);
1478}
1479
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001480/**
1481 * do_efi_query_info() - QueryVariableInfo EFI service
1482 *
1483 * @cmdtp: Command table
1484 * @flag: Command flag
1485 * @argc: Number of arguments
1486 * @argv: Argument array
1487 * Return: CMD_RET_SUCCESS on success,
1488 * CMD_RET_USAGE or CMD_RET_FAILURE on failure
1489 *
1490 * Implement efidebug "test" sub-command.
1491 */
1492
Simon Glass09140112020-05-10 11:40:03 -06001493static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001494 int argc, char * const argv[])
1495{
1496 efi_status_t ret;
1497 u32 attr = 0;
1498 u64 max_variable_storage_size;
1499 u64 remain_variable_storage_size;
1500 u64 max_variable_size;
1501 int i;
1502
1503 for (i = 1; i < argc; i++) {
1504 if (!strcmp(argv[i], "-bs"))
1505 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
1506 else if (!strcmp(argv[i], "-rt"))
1507 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
1508 else if (!strcmp(argv[i], "-nv"))
1509 attr |= EFI_VARIABLE_NON_VOLATILE;
1510 else if (!strcmp(argv[i], "-at"))
1511 attr |=
1512 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1513 }
1514
1515 ret = EFI_CALL(efi_query_variable_info(attr,
1516 &max_variable_storage_size,
1517 &remain_variable_storage_size,
1518 &max_variable_size));
1519 if (ret != EFI_SUCCESS) {
1520 printf("Error: Cannot query UEFI variables, r = %lu\n",
1521 ret & ~EFI_ERROR_MASK);
1522 return CMD_RET_FAILURE;
1523 }
1524
1525 printf("Max storage size %llu\n", max_variable_storage_size);
1526 printf("Remaining storage size %llu\n", remain_variable_storage_size);
1527 printf("Max variable size %llu\n", max_variable_size);
1528
1529 return CMD_RET_SUCCESS;
1530}
1531
Simon Glass09140112020-05-10 11:40:03 -06001532static struct cmd_tbl cmd_efidebug_sub[] = {
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001533 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +09001534#ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1535 U_BOOT_CMD_MKENT(capsule, CONFIG_SYS_MAXARGS, 1, do_efi_capsule,
1536 "", ""),
1537#endif
AKASHI Takahiro355cdb52019-02-25 15:54:39 +09001538 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1539 "", ""),
AKASHI Takahiro66eaf562019-02-25 15:54:40 +09001540 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1541 "", ""),
AKASHI Takahiroa8014622019-02-25 15:54:41 +09001542 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1543 "", ""),
AKASHI Takahirofa536732019-02-25 15:54:42 +09001544 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1545 "", ""),
AKASHI Takahiro00358bb2019-02-25 15:54:43 +09001546 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1547 "", ""),
Heinrich Schuchardt986e0642020-01-07 05:57:47 +01001548 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1549 "", ""),
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001550 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1551 "", ""),
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001552 U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
1553 "", ""),
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001554};
1555
1556/**
1557 * do_efidebug() - display and configure UEFI environment
1558 *
1559 * @cmdtp: Command table
1560 * @flag: Command flag
1561 * @argc: Number of arguments
1562 * @argv: Argument array
1563 * Return: CMD_RET_SUCCESS on success,
1564 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1565 *
1566 * Implement efidebug command which allows us to display and
1567 * configure UEFI environment.
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001568 */
Simon Glass09140112020-05-10 11:40:03 -06001569static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
1570 int argc, char *const argv[])
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001571{
Simon Glass09140112020-05-10 11:40:03 -06001572 struct cmd_tbl *cp;
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001573 efi_status_t r;
1574
1575 if (argc < 2)
1576 return CMD_RET_USAGE;
1577
1578 argc--; argv++;
1579
1580 /* Initialize UEFI drivers */
1581 r = efi_init_obj_list();
1582 if (r != EFI_SUCCESS) {
1583 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1584 r & ~EFI_ERROR_MASK);
1585 return CMD_RET_FAILURE;
1586 }
1587
1588 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1589 ARRAY_SIZE(cmd_efidebug_sub));
1590 if (!cp)
1591 return CMD_RET_USAGE;
1592
1593 return cp->cmd(cmdtp, flag, argc, argv);
1594}
1595
1596#ifdef CONFIG_SYS_LONGHELP
1597static char efidebug_help_text[] =
1598 " - UEFI Shell-like interface to configure UEFI environment\n"
1599 "\n"
Heinrich Schuchardt63276a52022-02-26 12:10:10 +01001600 "efidebug boot add - set UEFI BootXXXX variable\n"
1601 " -b|-B <bootid> <label> <interface> <devnum>[:<part>] <file path>\n"
1602 " -i|-I <interface> <devnum>[:<part>] <initrd file path>\n"
1603 " (-b, -i for short form device path)\n"
1604 " -s '<optional data>'\n"
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001605 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1606 " - delete UEFI BootXXXX variables\n"
1607 "efidebug boot dump\n"
1608 " - dump all UEFI BootXXXX variables\n"
1609 "efidebug boot next <bootid>\n"
1610 " - set UEFI BootNext variable\n"
1611 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1612 " - set/show UEFI boot order\n"
AKASHI Takahiro355cdb52019-02-25 15:54:39 +09001613 "\n"
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +09001614#ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1615 "efidebug capsule update [-v] <capsule address>\n"
1616 " - process a capsule\n"
Sughosh Ganu74075952020-12-30 19:27:11 +05301617 "efidebug capsule disk-update\n"
1618 " - update a capsule from disk\n"
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +09001619 "efidebug capsule show <capsule address>\n"
1620 " - show capsule information\n"
1621 "efidebug capsule result [<capsule result var>]\n"
1622 " - show a capsule update result\n"
Jose Marinhoaa31a872021-03-11 13:18:51 +00001623#ifdef CONFIG_EFI_ESRT
1624 "efidebug capsule esrt\n"
1625 " - print the ESRT\n"
1626#endif
AKASHI Takahiro7f35ced2020-11-17 09:28:01 +09001627 "\n"
1628#endif
AKASHI Takahiro355cdb52019-02-25 15:54:39 +09001629 "efidebug devices\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001630 " - show UEFI devices\n"
AKASHI Takahiro66eaf562019-02-25 15:54:40 +09001631 "efidebug drivers\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001632 " - show UEFI drivers\n"
AKASHI Takahiroa8014622019-02-25 15:54:41 +09001633 "efidebug dh\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001634 " - show UEFI handles\n"
AKASHI Takahirofa536732019-02-25 15:54:42 +09001635 "efidebug images\n"
AKASHI Takahiro00358bb2019-02-25 15:54:43 +09001636 " - show loaded images\n"
1637 "efidebug memmap\n"
Heinrich Schuchardt07e2fe72020-01-07 07:48:15 +01001638 " - show UEFI memory map\n"
Heinrich Schuchardt986e0642020-01-07 05:57:47 +01001639 "efidebug tables\n"
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001640 " - show UEFI configuration tables\n"
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +01001641#ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
AKASHI Takahiro525fc062020-04-14 11:51:48 +09001642 "efidebug test bootmgr\n"
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001643 " - run simple bootmgr for test\n"
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +01001644#endif
Ilias Apalodimasb0e4f2c2020-05-17 22:25:45 +03001645 "efidebug query [-nv][-bs][-rt][-at]\n"
1646 " - show size of UEFI variables store\n";
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001647#endif
1648
1649U_BOOT_CMD(
Ilias Apalodimascbea2412021-03-17 21:55:01 +02001650 efidebug, CONFIG_SYS_MAXARGS, 0, do_efidebug,
AKASHI Takahiro59df7e72019-02-25 15:54:38 +09001651 "Configure UEFI environment",
1652 efidebug_help_text
1653);