]>
Commit | Line | Data |
---|---|---|
59df7e7e AT |
1 | // 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> | |
94686f60 | 11 | #include <efi_dt_fixup.h> |
cbea241e | 12 | #include <efi_load_initrd.h> |
59df7e7e | 13 | #include <efi_loader.h> |
79693351 | 14 | #include <efi_rng.h> |
acfe1def | 15 | #include <efi_variable.h> |
59df7e7e | 16 | #include <exports.h> |
39a1ff8c | 17 | #include <hexdump.h> |
f7ae49fc | 18 | #include <log.h> |
59df7e7e | 19 | #include <malloc.h> |
a415d61e | 20 | #include <mapmem.h> |
b9b0ea30 | 21 | #include <part.h> |
59df7e7e AT |
22 | #include <search.h> |
23 | #include <linux/ctype.h> | |
cbea241e | 24 | #include <linux/err.h> |
59df7e7e | 25 | |
355cdb5a | 26 | #define BS systab.boottime |
7f35cedf AT |
27 | #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 | */ | |
44 | static 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 | ||
7e5f460e | 64 | capsule = (typeof(capsule))hextoul(argv[1], &endp); |
7f35cedf AT |
65 | 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 | ||
df7d89a6 | 78 | ret = EFI_CALL(RT->update_capsule(&capsule, 1, 0)); |
7f35cedf AT |
79 | 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 | ||
74075952 SG |
87 | static 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 | ||
7f35cedf AT |
97 | /** |
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 | */ | |
111 | static 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 | ||
7e5f460e | 120 | capsule = (typeof(capsule))hextoul(argv[1], &endp); |
7f35cedf AT |
121 | 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 | ||
aa31a87d JM |
135 | #ifdef CONFIG_EFI_ESRT |
136 | ||
137 | #define EFI_ESRT_FW_TYPE_NUM 4 | |
138 | char *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 | |
142 | char *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) (\ | |
148 | EFI_ESRT_FW_TYPE_NUM > (idx) ? efi_fw_type_str[(idx)] : "error"\ | |
149 | ) | |
150 | ||
151 | #define EFI_FW_STATUS_STR_GET(idx) (\ | |
152 | EFI_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 | */ | |
170 | static 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 */ | |
7f35cedf AT |
211 | /** |
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 | */ | |
226 | static 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; | |
2ecee310 | 231 | u16 var_name16[12]; |
7f35cedf AT |
232 | 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); | |
acfe1def HS |
243 | ret = efi_get_variable_int(L"CapsuleLast", &guid, NULL, |
244 | &size, var_name16, NULL); | |
245 | ||
7f35cedf AT |
246 | 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 | ||
7e5f460e | 259 | capsule_id = hextoul(argv[0], &endp); |
7f35cedf AT |
260 | if (capsule_id < 0 || capsule_id > 0xffff) |
261 | return CMD_RET_USAGE; | |
262 | ||
2ecee310 HS |
263 | efi_create_indexed_name(var_name16, sizeof(var_name16), |
264 | "Capsule", capsule_id); | |
7f35cedf AT |
265 | } |
266 | ||
267 | size = 0; | |
acfe1def | 268 | ret = efi_get_variable_int(var_name16, &guid, NULL, &size, NULL, NULL); |
7f35cedf AT |
269 | if (ret == EFI_BUFFER_TOO_SMALL) { |
270 | result = malloc(size); | |
30f8222b AT |
271 | if (!result) |
272 | return CMD_RET_FAILURE; | |
acfe1def HS |
273 | ret = efi_get_variable_int(var_name16, &guid, NULL, &size, |
274 | result, NULL); | |
30f8222b AT |
275 | } |
276 | if (ret != EFI_SUCCESS) { | |
277 | free(result); | |
278 | printf("Failed to get %ls\n", var_name16); | |
7f35cedf | 279 | |
30f8222b | 280 | return CMD_RET_FAILURE; |
7f35cedf AT |
281 | } |
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 | ||
297 | static 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 | "", ""), | |
aa31a87d JM |
302 | #ifdef CONFIG_EFI_ESRT |
303 | U_BOOT_CMD_MKENT(esrt, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_esrt, | |
304 | "", ""), | |
305 | #endif | |
74075952 SG |
306 | U_BOOT_CMD_MKENT(disk-update, 0, 0, do_efi_capsule_on_disk_update, |
307 | "", ""), | |
7f35cedf AT |
308 | 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 | */ | |
324 | static 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 */ | |
59df7e7e | 342 | |
355cdb5a | 343 | /** |
d473063d | 344 | * efi_get_device_path_text() - get device path text |
355cdb5a | 345 | * |
d473063d | 346 | * Return the text representation of the device path of a handle. |
355cdb5a | 347 | * |
d473063d HS |
348 | * @handle: handle of UEFI device |
349 | * Return: | |
350 | * Pointer to the device path text or NULL. | |
351 | * The caller is responsible for calling FreePool(). | |
355cdb5a | 352 | */ |
d473063d | 353 | static u16 *efi_get_device_path_text(efi_handle_t handle) |
355cdb5a | 354 | { |
d473063d | 355 | struct efi_handler *handler; |
355cdb5a AT |
356 | efi_status_t ret; |
357 | ||
d473063d HS |
358 | 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); | |
355cdb5a | 363 | } else { |
d473063d | 364 | return NULL; |
355cdb5a AT |
365 | } |
366 | } | |
367 | ||
368 | #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2) | |
369 | ||
370 | static const char spc[] = " "; | |
371 | static 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 | */ | |
09140113 SG |
385 | static int do_efi_show_devices(struct cmd_tbl *cmdtp, int flag, |
386 | int argc, char *const argv[]) | |
355cdb5a AT |
387 | { |
388 | efi_handle_t *handles; | |
389 | efi_uintn_t num, i; | |
390 | u16 *dev_path_text; | |
391 | efi_status_t ret; | |
392 | ||
a30c7231 | 393 | ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL, |
355cdb5a AT |
394 | &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++) { | |
d473063d HS |
404 | dev_path_text = efi_get_device_path_text(handles[i]); |
405 | if (dev_path_text) { | |
355cdb5a AT |
406 | printf("%p %ls\n", handles[i], dev_path_text); |
407 | efi_free_pool(dev_path_text); | |
408 | } | |
409 | } | |
410 | ||
a30c7231 | 411 | efi_free_pool(handles); |
355cdb5a AT |
412 | |
413 | return CMD_RET_SUCCESS; | |
414 | } | |
415 | ||
66eaf566 AT |
416 | /** |
417 | * 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 | */ | |
427 | static 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 | */ | |
09140113 SG |
465 | static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag, |
466 | int argc, char *const argv[]) | |
66eaf566 AT |
467 | { |
468 | efi_handle_t *handles; | |
469 | efi_uintn_t num, i; | |
470 | u16 *driver_name, *image_path_text; | |
471 | efi_status_t ret; | |
472 | ||
a30c7231 | 473 | ret = EFI_CALL(efi_locate_handle_buffer( |
66eaf566 AT |
474 | 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); | |
a30c7231 HS |
495 | efi_free_pool(driver_name); |
496 | efi_free_pool(image_path_text); | |
66eaf566 AT |
497 | } |
498 | } | |
499 | ||
a30c7231 | 500 | efi_free_pool(handles); |
66eaf566 AT |
501 | |
502 | return CMD_RET_SUCCESS; | |
503 | } | |
504 | ||
a8014620 AT |
505 | /** |
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 | */ | |
09140113 SG |
518 | static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag, |
519 | int argc, char *const argv[]) | |
a8014620 AT |
520 | { |
521 | efi_handle_t *handles; | |
522 | efi_guid_t **guid; | |
523 | efi_uintn_t num, count, i, j; | |
a8014620 AT |
524 | efi_status_t ret; |
525 | ||
a30c7231 | 526 | ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL, |
a8014620 AT |
527 | &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 | ||
3adae642 | 551 | printf("%pUs", guid[j]); |
a8014620 AT |
552 | } |
553 | putc('\n'); | |
554 | } | |
555 | ||
a30c7231 | 556 | efi_free_pool(handles); |
a8014620 AT |
557 | |
558 | return CMD_RET_SUCCESS; | |
559 | } | |
560 | ||
fa536734 AT |
561 | /** |
562 | * 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 | */ | |
09140113 SG |
573 | static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag, |
574 | int argc, char *const argv[]) | |
fa536734 AT |
575 | { |
576 | efi_print_image_infos(NULL); | |
577 | ||
578 | return CMD_RET_SUCCESS; | |
579 | } | |
580 | ||
00358bb8 AT |
581 | static 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", | |
dd9056c0 | 596 | [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT", |
00358bb8 AT |
597 | }; |
598 | ||
599 | static 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"}, | |
255a4733 | 615 | {EFI_MEMORY_SP, "SP"}, |
00358bb8 AT |
616 | {EFI_MEMORY_RUNTIME, "RT"}, |
617 | }; | |
618 | ||
619 | /** | |
620 | * print_memory_attributes() - print memory map attributes | |
0b016569 | 621 | * |
00358bb8 AT |
622 | * @attributes: Attribute value |
623 | * | |
624 | * Print memory map attributes | |
625 | */ | |
626 | static 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 | */ | |
09140113 SG |
656 | static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag, |
657 | int argc, char *const argv[]) | |
00358bb8 AT |
658 | { |
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 | ||
a30c7231 | 665 | ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL); |
00358bb8 AT |
666 | if (ret == EFI_BUFFER_TOO_SMALL) { |
667 | map_size += sizeof(struct efi_mem_desc); /* for my own */ | |
a30c7231 HS |
668 | ret = efi_allocate_pool(EFI_LOADER_DATA, map_size, |
669 | (void *)&memmap); | |
00358bb8 AT |
670 | if (ret != EFI_SUCCESS) |
671 | return CMD_RET_FAILURE; | |
a30c7231 | 672 | ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL); |
00358bb8 AT |
673 | } |
674 | if (ret != EFI_SUCCESS) { | |
a30c7231 | 675 | efi_free_pool(memmap); |
00358bb8 AT |
676 | 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); | |
e2a5b860 AT |
683 | /* |
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 | */ | |
00358bb8 | 688 | for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) { |
dd9056c0 | 689 | if (map->type < ARRAY_SIZE(efi_mem_type_string)) |
00358bb8 AT |
690 | type = efi_mem_type_string[map->type]; |
691 | else | |
692 | type = "(unknown)"; | |
693 | ||
694 | printf("%-16s %.*llx-%.*llx", type, | |
695 | EFI_PHYS_ADDR_WIDTH, | |
6c0ef35c HS |
696 | (u64)map_to_sysmem((void *)(uintptr_t) |
697 | map->physical_start), | |
00358bb8 | 698 | EFI_PHYS_ADDR_WIDTH, |
6c0ef35c HS |
699 | (u64)map_to_sysmem((void *)(uintptr_t) |
700 | (map->physical_start + | |
701 | map->num_pages * EFI_PAGE_SIZE))); | |
00358bb8 AT |
702 | |
703 | print_memory_attributes(map->attribute); | |
704 | putc('\n'); | |
705 | } | |
706 | ||
a30c7231 | 707 | efi_free_pool(memmap); |
00358bb8 AT |
708 | |
709 | return CMD_RET_SUCCESS; | |
710 | } | |
711 | ||
986e0648 HS |
712 | /** |
713 | * 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 | */ | |
09140113 SG |
724 | static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag, |
725 | int argc, char *const argv[]) | |
986e0648 HS |
726 | { |
727 | efi_uintn_t i; | |
986e0648 | 728 | |
3adae642 HS |
729 | for (i = 0; i < systab.nr_tables; ++i) |
730 | printf("%pUl (%pUs)\n", | |
731 | &systab.tables[i].guid, &systab.tables[i].guid); | |
986e0648 HS |
732 | |
733 | return CMD_RET_SUCCESS; | |
734 | } | |
735 | ||
cbea241e IA |
736 | /** |
737 | * create_initrd_dp() - Create a special device for our Boot### option | |
738 | * | |
739 | * @dev: Device | |
740 | * @part: Disk partition | |
741 | * @file: Filename | |
742 | * Return: Pointer to the device path or ERR_PTR | |
743 | * | |
744 | */ | |
745 | static | |
746 | struct efi_device_path *create_initrd_dp(const char *dev, const char *part, | |
747 | const char *file) | |
748 | ||
749 | { | |
750 | struct efi_device_path *tmp_dp = NULL, *tmp_fp = NULL; | |
751 | 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 | } | |
774 | ||
775 | initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp, | |
776 | tmp_fp); | |
777 | ||
778 | out: | |
779 | efi_free_pool(tmp_dp); | |
780 | efi_free_pool(tmp_fp); | |
781 | return initrd_dp; | |
782 | } | |
783 | ||
59df7e7e AT |
784 | /** |
785 | * do_efi_boot_add() - set UEFI load option | |
786 | * | |
787 | * @cmdtp: Command table | |
788 | * @flag: Command flag | |
789 | * @argc: Number of arguments | |
790 | * @argv: Argument array | |
791 | * Return: CMD_RET_SUCCESS on success, | |
792 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure | |
793 | * | |
0b016569 HS |
794 | * Implement efidebug "boot add" sub-command. Create or change UEFI load option. |
795 | * | |
cbea241e IA |
796 | * efidebug boot add -b <id> <label> <interface> <devnum>[:<part>] <file> |
797 | * -i <file> <interface2> <devnum2>[:<part>] <initrd> | |
798 | * -s '<options>' | |
59df7e7e | 799 | */ |
09140113 SG |
800 | static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag, |
801 | int argc, char *const argv[]) | |
59df7e7e AT |
802 | { |
803 | int id; | |
804 | char *endp; | |
2ecee310 | 805 | u16 var_name16[9]; |
59df7e7e AT |
806 | efi_guid_t guid; |
807 | size_t label_len, label_len16; | |
808 | u16 *label; | |
809 | struct efi_device_path *device_path = NULL, *file_path = NULL; | |
cbea241e IA |
810 | struct efi_device_path *final_fp = NULL; |
811 | struct efi_device_path *initrd_dp = NULL; | |
59df7e7e AT |
812 | struct efi_load_option lo; |
813 | void *data = NULL; | |
814 | efi_uintn_t size; | |
cbea241e | 815 | efi_uintn_t fp_size = 0; |
428a470a | 816 | efi_status_t ret; |
a332f251 | 817 | int r = CMD_RET_SUCCESS; |
59df7e7e | 818 | |
59df7e7e AT |
819 | guid = efi_global_variable_guid; |
820 | ||
821 | /* attributes */ | |
822 | lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */ | |
cbea241e IA |
823 | lo.optional_data = NULL; |
824 | lo.label = NULL; | |
59df7e7e | 825 | |
cbea241e IA |
826 | argc--; |
827 | argv++; /* 'add' */ | |
828 | for (; argc > 0; argc--, argv++) { | |
829 | if (!strcmp(argv[0], "-b")) { | |
830 | if (argc < 5 || lo.label) { | |
831 | r = CMD_RET_USAGE; | |
832 | goto out; | |
833 | } | |
7e5f460e | 834 | id = (int)hextoul(argv[1], &endp); |
cbea241e IA |
835 | if (*endp != '\0' || id > 0xffff) |
836 | return CMD_RET_USAGE; | |
837 | ||
2ecee310 HS |
838 | efi_create_indexed_name(var_name16, sizeof(var_name16), |
839 | "Boot", id); | |
cbea241e IA |
840 | |
841 | /* label */ | |
842 | label_len = strlen(argv[2]); | |
843 | label_len16 = utf8_utf16_strnlen(argv[2], label_len); | |
844 | label = malloc((label_len16 + 1) * sizeof(u16)); | |
845 | if (!label) | |
846 | return CMD_RET_FAILURE; | |
847 | lo.label = label; /* label will be changed below */ | |
848 | utf8_utf16_strncpy(&label, argv[2], label_len); | |
849 | ||
850 | /* file path */ | |
851 | ret = efi_dp_from_name(argv[3], argv[4], argv[5], | |
852 | &device_path, &file_path); | |
853 | if (ret != EFI_SUCCESS) { | |
854 | printf("Cannot create device path for \"%s %s\"\n", | |
855 | argv[3], argv[4]); | |
856 | r = CMD_RET_FAILURE; | |
857 | goto out; | |
858 | } | |
859 | fp_size += efi_dp_size(file_path) + | |
860 | sizeof(struct efi_device_path); | |
861 | argc -= 5; | |
862 | argv += 5; | |
863 | } else if (!strcmp(argv[0], "-i")) { | |
864 | if (argc < 3 || initrd_dp) { | |
865 | r = CMD_RET_USAGE; | |
866 | goto out; | |
867 | } | |
59df7e7e | 868 | |
cbea241e IA |
869 | initrd_dp = create_initrd_dp(argv[1], argv[2], argv[3]); |
870 | if (!initrd_dp) { | |
871 | printf("Cannot add an initrd\n"); | |
872 | r = CMD_RET_FAILURE; | |
873 | goto out; | |
874 | } | |
875 | argc -= 3; | |
876 | argv += 3; | |
877 | fp_size += efi_dp_size(initrd_dp) + | |
878 | sizeof(struct efi_device_path); | |
879 | } else if (!strcmp(argv[0], "-s")) { | |
880 | if (argc < 1 || lo.optional_data) { | |
881 | r = CMD_RET_USAGE; | |
882 | goto out; | |
883 | } | |
884 | lo.optional_data = (const u8 *)argv[1]; | |
885 | argc -= 1; | |
886 | argv += 1; | |
887 | } else { | |
888 | r = CMD_RET_USAGE; | |
889 | goto out; | |
890 | } | |
891 | } | |
892 | ||
893 | if (!file_path) { | |
894 | printf("Missing binary\n"); | |
895 | r = CMD_RET_USAGE; | |
896 | goto out; | |
897 | } | |
898 | ||
899 | final_fp = efi_dp_concat(file_path, initrd_dp); | |
900 | if (!final_fp) { | |
901 | printf("Cannot create final device path\n"); | |
428a470a | 902 | r = CMD_RET_FAILURE; |
59df7e7e AT |
903 | goto out; |
904 | } | |
59df7e7e | 905 | |
cbea241e IA |
906 | lo.file_path = final_fp; |
907 | lo.file_path_length = fp_size; | |
59df7e7e AT |
908 | |
909 | size = efi_serialize_load_option(&lo, (u8 **)&data); | |
910 | if (!size) { | |
428a470a | 911 | r = CMD_RET_FAILURE; |
59df7e7e AT |
912 | goto out; |
913 | } | |
914 | ||
acfe1def HS |
915 | ret = efi_set_variable_int(var_name16, &guid, |
916 | EFI_VARIABLE_NON_VOLATILE | | |
917 | EFI_VARIABLE_BOOTSERVICE_ACCESS | | |
918 | EFI_VARIABLE_RUNTIME_ACCESS, | |
919 | size, data, false); | |
a332f251 HS |
920 | if (ret != EFI_SUCCESS) { |
921 | printf("Cannot set %ls\n", var_name16); | |
922 | r = CMD_RET_FAILURE; | |
923 | } | |
cbea241e | 924 | |
59df7e7e AT |
925 | out: |
926 | free(data); | |
cbea241e IA |
927 | efi_free_pool(final_fp); |
928 | efi_free_pool(initrd_dp); | |
59df7e7e AT |
929 | efi_free_pool(device_path); |
930 | efi_free_pool(file_path); | |
931 | free(lo.label); | |
932 | ||
428a470a | 933 | return r; |
59df7e7e AT |
934 | } |
935 | ||
936 | /** | |
937 | * do_efi_boot_rm() - delete UEFI load options | |
938 | * | |
939 | * @cmdtp: Command table | |
940 | * @flag: Command flag | |
941 | * @argc: Number of arguments | |
942 | * @argv: Argument array | |
943 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure | |
944 | * | |
945 | * Implement efidebug "boot rm" sub-command. | |
946 | * Delete UEFI load options. | |
0b016569 HS |
947 | * |
948 | * efidebug boot rm <id> ... | |
59df7e7e | 949 | */ |
09140113 SG |
950 | static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag, |
951 | int argc, char *const argv[]) | |
59df7e7e AT |
952 | { |
953 | efi_guid_t guid; | |
954 | int id, i; | |
955 | char *endp; | |
2ecee310 | 956 | u16 var_name16[9]; |
59df7e7e AT |
957 | efi_status_t ret; |
958 | ||
959 | if (argc == 1) | |
960 | return CMD_RET_USAGE; | |
961 | ||
962 | guid = efi_global_variable_guid; | |
963 | for (i = 1; i < argc; i++, argv++) { | |
7e5f460e | 964 | id = (int)hextoul(argv[1], &endp); |
59df7e7e AT |
965 | if (*endp != '\0' || id > 0xffff) |
966 | return CMD_RET_FAILURE; | |
967 | ||
2ecee310 HS |
968 | efi_create_indexed_name(var_name16, sizeof(var_name16), |
969 | "Boot", id); | |
acfe1def HS |
970 | ret = efi_set_variable_int(var_name16, &guid, 0, 0, NULL, |
971 | false); | |
59df7e7e | 972 | if (ret) { |
30efb5dd | 973 | printf("Cannot remove %ls\n", var_name16); |
59df7e7e AT |
974 | return CMD_RET_FAILURE; |
975 | } | |
976 | } | |
977 | ||
978 | return CMD_RET_SUCCESS; | |
979 | } | |
980 | ||
981 | /** | |
982 | * show_efi_boot_opt_data() - dump UEFI load option | |
983 | * | |
b5f4e9e3 | 984 | * @varname16: variable name |
39a1ff8c HS |
985 | * @data: value of UEFI load option variable |
986 | * @size: size of the boot option | |
59df7e7e AT |
987 | * |
988 | * Decode the value of UEFI load option variable and print information. | |
989 | */ | |
0e69bcfb | 990 | static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size) |
59df7e7e | 991 | { |
cbea241e | 992 | struct efi_device_path *initrd_path = NULL; |
59df7e7e | 993 | struct efi_load_option lo; |
0e69bcfb | 994 | efi_status_t ret; |
59df7e7e | 995 | |
0e69bcfb HS |
996 | ret = efi_deserialize_load_option(&lo, data, size); |
997 | if (ret != EFI_SUCCESS) { | |
998 | printf("%ls: invalid load option\n", varname16); | |
999 | return; | |
1000 | } | |
59df7e7e | 1001 | |
b5f4e9e3 HS |
1002 | printf("%ls:\nattributes: %c%c%c (0x%08x)\n", |
1003 | varname16, | |
59df7e7e AT |
1004 | /* ACTIVE */ |
1005 | lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-', | |
1006 | /* FORCE RECONNECT */ | |
1007 | lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-', | |
1008 | /* HIDDEN */ | |
1009 | lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-', | |
1010 | lo.attributes); | |
cd5a87e7 | 1011 | printf(" label: %ls\n", lo.label); |
59df7e7e | 1012 | |
fc42b8bb | 1013 | printf(" file_path: %pD\n", lo.file_path); |
59df7e7e | 1014 | |
9ad37fe4 | 1015 | initrd_path = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid); |
cbea241e | 1016 | if (initrd_path) { |
fc42b8bb | 1017 | printf(" initrd_path: %pD\n", initrd_path); |
cbea241e IA |
1018 | efi_free_pool(initrd_path); |
1019 | } | |
1020 | ||
39a1ff8c HS |
1021 | printf(" data:\n"); |
1022 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, | |
0e69bcfb | 1023 | lo.optional_data, *size, true); |
59df7e7e AT |
1024 | } |
1025 | ||
1026 | /** | |
1027 | * show_efi_boot_opt() - dump UEFI load option | |
1028 | * | |
b5f4e9e3 | 1029 | * @varname16: variable name |
59df7e7e AT |
1030 | * |
1031 | * Dump information defined by UEFI load option. | |
1032 | */ | |
b5f4e9e3 | 1033 | static void show_efi_boot_opt(u16 *varname16) |
59df7e7e | 1034 | { |
b5f4e9e3 | 1035 | void *data; |
59df7e7e | 1036 | efi_uintn_t size; |
0bffb8c4 | 1037 | efi_status_t ret; |
59df7e7e | 1038 | |
59df7e7e | 1039 | size = 0; |
b5f4e9e3 HS |
1040 | ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid, |
1041 | NULL, &size, NULL)); | |
0bffb8c4 | 1042 | if (ret == EFI_BUFFER_TOO_SMALL) { |
59df7e7e | 1043 | data = malloc(size); |
b5f4e9e3 HS |
1044 | if (!data) { |
1045 | printf("ERROR: Out of memory\n"); | |
1046 | return; | |
1047 | } | |
1048 | ret = EFI_CALL(efi_get_variable(varname16, | |
1049 | &efi_global_variable_guid, | |
1050 | NULL, &size, data)); | |
1051 | if (ret == EFI_SUCCESS) | |
0e69bcfb | 1052 | show_efi_boot_opt_data(varname16, data, &size); |
b5f4e9e3 | 1053 | free(data); |
59df7e7e | 1054 | } |
59df7e7e AT |
1055 | } |
1056 | ||
ffe21574 AT |
1057 | static int u16_tohex(u16 c) |
1058 | { | |
1059 | if (c >= '0' && c <= '9') | |
1060 | return c - '0'; | |
1061 | if (c >= 'A' && c <= 'F') | |
1062 | return c - 'A' + 10; | |
1063 | ||
1064 | /* not hexadecimal */ | |
1065 | return -1; | |
1066 | } | |
1067 | ||
59df7e7e AT |
1068 | /** |
1069 | * show_efi_boot_dump() - dump all UEFI load options | |
1070 | * | |
1071 | * @cmdtp: Command table | |
1072 | * @flag: Command flag | |
1073 | * @argc: Number of arguments | |
1074 | * @argv: Argument array | |
1075 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure | |
1076 | * | |
1077 | * Implement efidebug "boot dump" sub-command. | |
1078 | * Dump information of all UEFI load options defined. | |
a6ccba0c HS |
1079 | * |
1080 | * efidebug boot dump | |
59df7e7e | 1081 | */ |
09140113 SG |
1082 | static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag, |
1083 | int argc, char *const argv[]) | |
59df7e7e | 1084 | { |
ffe21574 AT |
1085 | u16 *var_name16, *p; |
1086 | efi_uintn_t buf_size, size; | |
1087 | efi_guid_t guid; | |
1088 | int id, i, digit; | |
1089 | efi_status_t ret; | |
59df7e7e AT |
1090 | |
1091 | if (argc > 1) | |
1092 | return CMD_RET_USAGE; | |
1093 | ||
ffe21574 AT |
1094 | buf_size = 128; |
1095 | var_name16 = malloc(buf_size); | |
1096 | if (!var_name16) | |
59df7e7e AT |
1097 | return CMD_RET_FAILURE; |
1098 | ||
ffe21574 AT |
1099 | var_name16[0] = 0; |
1100 | for (;;) { | |
1101 | size = buf_size; | |
1102 | ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16, | |
1103 | &guid)); | |
1104 | if (ret == EFI_NOT_FOUND) | |
59df7e7e | 1105 | break; |
ffe21574 AT |
1106 | if (ret == EFI_BUFFER_TOO_SMALL) { |
1107 | buf_size = size; | |
1108 | p = realloc(var_name16, buf_size); | |
1109 | if (!p) { | |
1110 | free(var_name16); | |
1111 | return CMD_RET_FAILURE; | |
1112 | } | |
1113 | var_name16 = p; | |
1114 | ret = EFI_CALL(efi_get_next_variable_name(&size, | |
1115 | var_name16, | |
1116 | &guid)); | |
1117 | } | |
1118 | if (ret != EFI_SUCCESS) { | |
1119 | free(var_name16); | |
1120 | return CMD_RET_FAILURE; | |
1121 | } | |
1122 | ||
1123 | if (memcmp(var_name16, L"Boot", 8)) | |
1124 | continue; | |
1125 | ||
1126 | for (id = 0, i = 0; i < 4; i++) { | |
1127 | digit = u16_tohex(var_name16[4 + i]); | |
1128 | if (digit < 0) | |
1129 | break; | |
1130 | id = (id << 4) + digit; | |
1131 | } | |
1132 | if (i == 4 && !var_name16[8]) | |
b5f4e9e3 | 1133 | show_efi_boot_opt(var_name16); |
59df7e7e | 1134 | } |
ffe21574 AT |
1135 | |
1136 | free(var_name16); | |
59df7e7e AT |
1137 | |
1138 | return CMD_RET_SUCCESS; | |
1139 | } | |
1140 | ||
1141 | /** | |
1142 | * show_efi_boot_order() - show order of UEFI load options | |
1143 | * | |
1144 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure | |
1145 | * | |
1146 | * Show order of UEFI load options defined by BootOrder variable. | |
1147 | */ | |
1148 | static int show_efi_boot_order(void) | |
1149 | { | |
f9f5f92b | 1150 | u16 *bootorder; |
59df7e7e AT |
1151 | efi_uintn_t size; |
1152 | int num, i; | |
2ecee310 | 1153 | u16 var_name16[9]; |
59df7e7e AT |
1154 | void *data; |
1155 | struct efi_load_option lo; | |
59df7e7e AT |
1156 | efi_status_t ret; |
1157 | ||
59df7e7e | 1158 | size = 0; |
a30c7231 | 1159 | ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid, |
f9f5f92b HS |
1160 | NULL, &size, NULL)); |
1161 | if (ret != EFI_BUFFER_TOO_SMALL) { | |
1162 | if (ret == EFI_NOT_FOUND) { | |
1163 | printf("BootOrder not defined\n"); | |
1164 | return CMD_RET_SUCCESS; | |
1165 | } else { | |
1166 | return CMD_RET_FAILURE; | |
1167 | } | |
59df7e7e | 1168 | } |
f9f5f92b HS |
1169 | bootorder = malloc(size); |
1170 | if (!bootorder) { | |
1171 | printf("ERROR: Out of memory\n"); | |
1172 | return CMD_RET_FAILURE; | |
1173 | } | |
1174 | ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid, | |
1175 | NULL, &size, bootorder)); | |
1176 | if (ret != EFI_SUCCESS) { | |
59df7e7e AT |
1177 | ret = CMD_RET_FAILURE; |
1178 | goto out; | |
1179 | } | |
1180 | ||
1181 | num = size / sizeof(u16); | |
1182 | for (i = 0; i < num; i++) { | |
2ecee310 | 1183 | efi_create_indexed_name(var_name16, sizeof(var_name16), |
2b8723c5 | 1184 | "Boot", bootorder[i]); |
59df7e7e AT |
1185 | |
1186 | size = 0; | |
f9f5f92b HS |
1187 | ret = EFI_CALL(efi_get_variable(var_name16, |
1188 | &efi_global_variable_guid, NULL, | |
1189 | &size, NULL)); | |
59df7e7e | 1190 | if (ret != EFI_BUFFER_TOO_SMALL) { |
2ecee310 | 1191 | printf("%2d: %ls: (not defined)\n", i + 1, var_name16); |
59df7e7e AT |
1192 | continue; |
1193 | } | |
1194 | ||
1195 | data = malloc(size); | |
1196 | if (!data) { | |
1197 | ret = CMD_RET_FAILURE; | |
1198 | goto out; | |
1199 | } | |
f9f5f92b HS |
1200 | ret = EFI_CALL(efi_get_variable(var_name16, |
1201 | &efi_global_variable_guid, NULL, | |
1202 | &size, data)); | |
59df7e7e AT |
1203 | if (ret != EFI_SUCCESS) { |
1204 | free(data); | |
1205 | ret = CMD_RET_FAILURE; | |
1206 | goto out; | |
1207 | } | |
1208 | ||
0e69bcfb HS |
1209 | ret = efi_deserialize_load_option(&lo, data, &size); |
1210 | if (ret != EFI_SUCCESS) { | |
1211 | printf("%ls: invalid load option\n", var_name16); | |
1212 | ret = CMD_RET_FAILURE; | |
1213 | goto out; | |
1214 | } | |
59df7e7e | 1215 | |
2ecee310 | 1216 | printf("%2d: %ls: %ls\n", i + 1, var_name16, lo.label); |
59df7e7e AT |
1217 | |
1218 | free(data); | |
1219 | } | |
1220 | out: | |
1221 | free(bootorder); | |
1222 | ||
1223 | return ret; | |
1224 | } | |
1225 | ||
1226 | /** | |
1227 | * do_efi_boot_next() - manage UEFI BootNext variable | |
1228 | * | |
1229 | * @cmdtp: Command table | |
1230 | * @flag: Command flag | |
1231 | * @argc: Number of arguments | |
1232 | * @argv: Argument array | |
1233 | * Return: CMD_RET_SUCCESS on success, | |
1234 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure | |
1235 | * | |
1236 | * Implement efidebug "boot next" sub-command. | |
1237 | * Set BootNext variable. | |
0b016569 HS |
1238 | * |
1239 | * efidebug boot next <id> | |
59df7e7e | 1240 | */ |
09140113 SG |
1241 | static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag, |
1242 | int argc, char *const argv[]) | |
59df7e7e AT |
1243 | { |
1244 | u16 bootnext; | |
1245 | efi_uintn_t size; | |
1246 | char *endp; | |
1247 | efi_guid_t guid; | |
1248 | efi_status_t ret; | |
a332f251 | 1249 | int r = CMD_RET_SUCCESS; |
59df7e7e AT |
1250 | |
1251 | if (argc != 2) | |
1252 | return CMD_RET_USAGE; | |
1253 | ||
7e5f460e | 1254 | bootnext = (u16)hextoul(argv[1], &endp); |
bdb15776 | 1255 | if (*endp) { |
59df7e7e | 1256 | printf("invalid value: %s\n", argv[1]); |
428a470a | 1257 | r = CMD_RET_FAILURE; |
59df7e7e AT |
1258 | goto out; |
1259 | } | |
1260 | ||
1261 | guid = efi_global_variable_guid; | |
1262 | size = sizeof(u16); | |
acfe1def | 1263 | ret = efi_set_variable_int(L"BootNext", &guid, |
f658c2e1 | 1264 | EFI_VARIABLE_NON_VOLATILE | |
59df7e7e AT |
1265 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
1266 | EFI_VARIABLE_RUNTIME_ACCESS, | |
acfe1def | 1267 | size, &bootnext, false); |
a332f251 HS |
1268 | if (ret != EFI_SUCCESS) { |
1269 | printf("Cannot set BootNext\n"); | |
1270 | r = CMD_RET_FAILURE; | |
1271 | } | |
59df7e7e | 1272 | out: |
428a470a | 1273 | return r; |
59df7e7e AT |
1274 | } |
1275 | ||
1276 | /** | |
1277 | * do_efi_boot_order() - manage UEFI BootOrder variable | |
1278 | * | |
1279 | * @cmdtp: Command table | |
1280 | * @flag: Command flag | |
1281 | * @argc: Number of arguments | |
1282 | * @argv: Argument array | |
1283 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure | |
1284 | * | |
1285 | * Implement efidebug "boot order" sub-command. | |
1286 | * Show order of UEFI load options, or change it in BootOrder variable. | |
0b016569 HS |
1287 | * |
1288 | * efidebug boot order [<id> ...] | |
59df7e7e | 1289 | */ |
09140113 SG |
1290 | static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag, |
1291 | int argc, char *const argv[]) | |
59df7e7e AT |
1292 | { |
1293 | u16 *bootorder = NULL; | |
1294 | efi_uintn_t size; | |
1295 | int id, i; | |
1296 | char *endp; | |
1297 | efi_guid_t guid; | |
1298 | efi_status_t ret; | |
a332f251 | 1299 | int r = CMD_RET_SUCCESS; |
59df7e7e AT |
1300 | |
1301 | if (argc == 1) | |
1302 | return show_efi_boot_order(); | |
1303 | ||
1304 | argc--; | |
1305 | argv++; | |
1306 | ||
1307 | size = argc * sizeof(u16); | |
1308 | bootorder = malloc(size); | |
1309 | if (!bootorder) | |
1310 | return CMD_RET_FAILURE; | |
1311 | ||
1312 | for (i = 0; i < argc; i++) { | |
7e5f460e | 1313 | id = (int)hextoul(argv[i], &endp); |
59df7e7e AT |
1314 | if (*endp != '\0' || id > 0xffff) { |
1315 | printf("invalid value: %s\n", argv[i]); | |
428a470a | 1316 | r = CMD_RET_FAILURE; |
59df7e7e AT |
1317 | goto out; |
1318 | } | |
1319 | ||
1320 | bootorder[i] = (u16)id; | |
1321 | } | |
1322 | ||
1323 | guid = efi_global_variable_guid; | |
acfe1def | 1324 | ret = efi_set_variable_int(L"BootOrder", &guid, |
f658c2e1 | 1325 | EFI_VARIABLE_NON_VOLATILE | |
59df7e7e AT |
1326 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
1327 | EFI_VARIABLE_RUNTIME_ACCESS, | |
acfe1def | 1328 | size, bootorder, true); |
a332f251 HS |
1329 | if (ret != EFI_SUCCESS) { |
1330 | printf("Cannot set BootOrder\n"); | |
1331 | r = CMD_RET_FAILURE; | |
1332 | } | |
59df7e7e AT |
1333 | out: |
1334 | free(bootorder); | |
1335 | ||
428a470a | 1336 | return r; |
59df7e7e AT |
1337 | } |
1338 | ||
09140113 | 1339 | static struct cmd_tbl cmd_efidebug_boot_sub[] = { |
59df7e7e AT |
1340 | U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""), |
1341 | U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""), | |
1342 | U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""), | |
1343 | U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""), | |
1344 | U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order, | |
1345 | "", ""), | |
1346 | }; | |
1347 | ||
1348 | /** | |
1349 | * do_efi_boot_opt() - manage UEFI load options | |
1350 | * | |
1351 | * @cmdtp: Command table | |
1352 | * @flag: Command flag | |
1353 | * @argc: Number of arguments | |
1354 | * @argv: Argument array | |
1355 | * Return: CMD_RET_SUCCESS on success, | |
1356 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure | |
1357 | * | |
1358 | * Implement efidebug "boot" sub-command. | |
59df7e7e | 1359 | */ |
09140113 SG |
1360 | static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag, |
1361 | int argc, char *const argv[]) | |
59df7e7e | 1362 | { |
09140113 | 1363 | struct cmd_tbl *cp; |
59df7e7e AT |
1364 | |
1365 | if (argc < 2) | |
1366 | return CMD_RET_USAGE; | |
1367 | ||
1368 | argc--; argv++; | |
1369 | ||
1370 | cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub, | |
1371 | ARRAY_SIZE(cmd_efidebug_boot_sub)); | |
1372 | if (!cp) | |
1373 | return CMD_RET_USAGE; | |
1374 | ||
1375 | return cp->cmd(cmdtp, flag, argc, argv); | |
1376 | } | |
1377 | ||
525fc067 AT |
1378 | /** |
1379 | * do_efi_test_bootmgr() - run simple bootmgr for test | |
1380 | * | |
1381 | * @cmdtp: Command table | |
1382 | * @flag: Command flag | |
1383 | * @argc: Number of arguments | |
1384 | * @argv: Argument array | |
1385 | * Return: CMD_RET_SUCCESS on success, | |
1386 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure | |
1387 | * | |
1388 | * Implement efidebug "test bootmgr" sub-command. | |
1389 | * Run simple bootmgr for test. | |
1390 | * | |
1391 | * efidebug test bootmgr | |
1392 | */ | |
ff2f532f HS |
1393 | static __maybe_unused int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag, |
1394 | int argc, char * const argv[]) | |
525fc067 AT |
1395 | { |
1396 | efi_handle_t image; | |
1397 | efi_uintn_t exit_data_size = 0; | |
1398 | u16 *exit_data = NULL; | |
1399 | efi_status_t ret; | |
bc78d22d | 1400 | void *load_options = NULL; |
525fc067 | 1401 | |
0ad64007 | 1402 | ret = efi_bootmgr_load(&image, &load_options); |
525fc067 AT |
1403 | printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK); |
1404 | ||
1405 | /* We call efi_start_image() even if error for test purpose. */ | |
1406 | ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data)); | |
1407 | printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK); | |
1408 | if (ret && exit_data) | |
1409 | efi_free_pool(exit_data); | |
1410 | ||
1411 | efi_restore_gd(); | |
1412 | ||
0ad64007 | 1413 | free(load_options); |
525fc067 AT |
1414 | return CMD_RET_SUCCESS; |
1415 | } | |
1416 | ||
09140113 | 1417 | static struct cmd_tbl cmd_efidebug_test_sub[] = { |
ff2f532f | 1418 | #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR |
525fc067 AT |
1419 | U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr, |
1420 | "", ""), | |
ff2f532f | 1421 | #endif |
525fc067 AT |
1422 | }; |
1423 | ||
1424 | /** | |
1425 | * do_efi_test() - manage UEFI load options | |
1426 | * | |
1427 | * @cmdtp: Command table | |
1428 | * @flag: Command flag | |
1429 | * @argc: Number of arguments | |
1430 | * @argv: Argument array | |
1431 | * Return: CMD_RET_SUCCESS on success, | |
1432 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure | |
1433 | * | |
1434 | * Implement efidebug "test" sub-command. | |
1435 | */ | |
09140113 | 1436 | static int do_efi_test(struct cmd_tbl *cmdtp, int flag, |
525fc067 AT |
1437 | int argc, char * const argv[]) |
1438 | { | |
09140113 | 1439 | struct cmd_tbl *cp; |
525fc067 AT |
1440 | |
1441 | if (argc < 2) | |
1442 | return CMD_RET_USAGE; | |
1443 | ||
1444 | argc--; argv++; | |
1445 | ||
1446 | cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub, | |
1447 | ARRAY_SIZE(cmd_efidebug_test_sub)); | |
1448 | if (!cp) | |
1449 | return CMD_RET_USAGE; | |
1450 | ||
1451 | return cp->cmd(cmdtp, flag, argc, argv); | |
1452 | } | |
1453 | ||
b0e4f2c7 IA |
1454 | /** |
1455 | * do_efi_query_info() - QueryVariableInfo EFI service | |
1456 | * | |
1457 | * @cmdtp: Command table | |
1458 | * @flag: Command flag | |
1459 | * @argc: Number of arguments | |
1460 | * @argv: Argument array | |
1461 | * Return: CMD_RET_SUCCESS on success, | |
1462 | * CMD_RET_USAGE or CMD_RET_FAILURE on failure | |
1463 | * | |
1464 | * Implement efidebug "test" sub-command. | |
1465 | */ | |
1466 | ||
09140113 | 1467 | static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag, |
b0e4f2c7 IA |
1468 | int argc, char * const argv[]) |
1469 | { | |
1470 | efi_status_t ret; | |
1471 | u32 attr = 0; | |
1472 | u64 max_variable_storage_size; | |
1473 | u64 remain_variable_storage_size; | |
1474 | u64 max_variable_size; | |
1475 | int i; | |
1476 | ||
1477 | for (i = 1; i < argc; i++) { | |
1478 | if (!strcmp(argv[i], "-bs")) | |
1479 | attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS; | |
1480 | else if (!strcmp(argv[i], "-rt")) | |
1481 | attr |= EFI_VARIABLE_RUNTIME_ACCESS; | |
1482 | else if (!strcmp(argv[i], "-nv")) | |
1483 | attr |= EFI_VARIABLE_NON_VOLATILE; | |
1484 | else if (!strcmp(argv[i], "-at")) | |
1485 | attr |= | |
1486 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; | |
1487 | } | |
1488 | ||
1489 | ret = EFI_CALL(efi_query_variable_info(attr, | |
1490 | &max_variable_storage_size, | |
1491 | &remain_variable_storage_size, | |
1492 | &max_variable_size)); | |
1493 | if (ret != EFI_SUCCESS) { | |
1494 | printf("Error: Cannot query UEFI variables, r = %lu\n", | |
1495 | ret & ~EFI_ERROR_MASK); | |
1496 | return CMD_RET_FAILURE; | |
1497 | } | |
1498 | ||
1499 | printf("Max storage size %llu\n", max_variable_storage_size); | |
1500 | printf("Remaining storage size %llu\n", remain_variable_storage_size); | |
1501 | printf("Max variable size %llu\n", max_variable_size); | |
1502 | ||
1503 | return CMD_RET_SUCCESS; | |
1504 | } | |
1505 | ||
09140113 | 1506 | static struct cmd_tbl cmd_efidebug_sub[] = { |
59df7e7e | 1507 | U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""), |
7f35cedf AT |
1508 | #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT |
1509 | U_BOOT_CMD_MKENT(capsule, CONFIG_SYS_MAXARGS, 1, do_efi_capsule, | |
1510 | "", ""), | |
1511 | #endif | |
355cdb5a AT |
1512 | U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices, |
1513 | "", ""), | |
66eaf566 AT |
1514 | U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers, |
1515 | "", ""), | |
a8014620 AT |
1516 | U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles, |
1517 | "", ""), | |
fa536734 AT |
1518 | U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images, |
1519 | "", ""), | |
00358bb8 AT |
1520 | U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap, |
1521 | "", ""), | |
986e0648 HS |
1522 | U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables, |
1523 | "", ""), | |
525fc067 AT |
1524 | U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test, |
1525 | "", ""), | |
b0e4f2c7 IA |
1526 | U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info, |
1527 | "", ""), | |
59df7e7e AT |
1528 | }; |
1529 | ||
1530 | /** | |
1531 | * do_efidebug() - display and configure UEFI environment | |
1532 | * | |
1533 | * @cmdtp: Command table | |
1534 | * @flag: Command flag | |
1535 | * @argc: Number of arguments | |
1536 | * @argv: Argument array | |
1537 | * Return: CMD_RET_SUCCESS on success, | |
1538 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure | |
1539 | * | |
1540 | * Implement efidebug command which allows us to display and | |
1541 | * configure UEFI environment. | |
59df7e7e | 1542 | */ |
09140113 SG |
1543 | static int do_efidebug(struct cmd_tbl *cmdtp, int flag, |
1544 | int argc, char *const argv[]) | |
59df7e7e | 1545 | { |
09140113 | 1546 | struct cmd_tbl *cp; |
59df7e7e AT |
1547 | efi_status_t r; |
1548 | ||
1549 | if (argc < 2) | |
1550 | return CMD_RET_USAGE; | |
1551 | ||
1552 | argc--; argv++; | |
1553 | ||
1554 | /* Initialize UEFI drivers */ | |
1555 | r = efi_init_obj_list(); | |
1556 | if (r != EFI_SUCCESS) { | |
1557 | printf("Error: Cannot initialize UEFI sub-system, r = %lu\n", | |
1558 | r & ~EFI_ERROR_MASK); | |
1559 | return CMD_RET_FAILURE; | |
1560 | } | |
1561 | ||
1562 | cp = find_cmd_tbl(argv[0], cmd_efidebug_sub, | |
1563 | ARRAY_SIZE(cmd_efidebug_sub)); | |
1564 | if (!cp) | |
1565 | return CMD_RET_USAGE; | |
1566 | ||
1567 | return cp->cmd(cmdtp, flag, argc, argv); | |
1568 | } | |
1569 | ||
1570 | #ifdef CONFIG_SYS_LONGHELP | |
1571 | static char efidebug_help_text[] = | |
1572 | " - UEFI Shell-like interface to configure UEFI environment\n" | |
1573 | "\n" | |
cbea241e IA |
1574 | "efidebug boot add " |
1575 | "-b <bootid> <label> <interface> <devnum>[:<part>] <file path> " | |
1576 | "-i <interface> <devnum>[:<part>] <initrd file path> " | |
1577 | "-s '<optional data>'\n" | |
59df7e7e AT |
1578 | " - set UEFI BootXXXX variable\n" |
1579 | " <load options> will be passed to UEFI application\n" | |
1580 | "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n" | |
1581 | " - delete UEFI BootXXXX variables\n" | |
1582 | "efidebug boot dump\n" | |
1583 | " - dump all UEFI BootXXXX variables\n" | |
1584 | "efidebug boot next <bootid>\n" | |
1585 | " - set UEFI BootNext variable\n" | |
1586 | "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n" | |
1587 | " - set/show UEFI boot order\n" | |
355cdb5a | 1588 | "\n" |
7f35cedf AT |
1589 | #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT |
1590 | "efidebug capsule update [-v] <capsule address>\n" | |
1591 | " - process a capsule\n" | |
74075952 SG |
1592 | "efidebug capsule disk-update\n" |
1593 | " - update a capsule from disk\n" | |
7f35cedf AT |
1594 | "efidebug capsule show <capsule address>\n" |
1595 | " - show capsule information\n" | |
1596 | "efidebug capsule result [<capsule result var>]\n" | |
1597 | " - show a capsule update result\n" | |
aa31a87d JM |
1598 | #ifdef CONFIG_EFI_ESRT |
1599 | "efidebug capsule esrt\n" | |
1600 | " - print the ESRT\n" | |
1601 | #endif | |
7f35cedf AT |
1602 | "\n" |
1603 | #endif | |
355cdb5a | 1604 | "efidebug devices\n" |
07e2fe79 | 1605 | " - show UEFI devices\n" |
66eaf566 | 1606 | "efidebug drivers\n" |
07e2fe79 | 1607 | " - show UEFI drivers\n" |
a8014620 | 1608 | "efidebug dh\n" |
07e2fe79 | 1609 | " - show UEFI handles\n" |
fa536734 | 1610 | "efidebug images\n" |
00358bb8 AT |
1611 | " - show loaded images\n" |
1612 | "efidebug memmap\n" | |
07e2fe79 | 1613 | " - show UEFI memory map\n" |
986e0648 | 1614 | "efidebug tables\n" |
525fc067 | 1615 | " - show UEFI configuration tables\n" |
ff2f532f | 1616 | #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR |
525fc067 | 1617 | "efidebug test bootmgr\n" |
b0e4f2c7 | 1618 | " - run simple bootmgr for test\n" |
ff2f532f | 1619 | #endif |
b0e4f2c7 IA |
1620 | "efidebug query [-nv][-bs][-rt][-at]\n" |
1621 | " - show size of UEFI variables store\n"; | |
59df7e7e AT |
1622 | #endif |
1623 | ||
1624 | U_BOOT_CMD( | |
cbea241e | 1625 | efidebug, CONFIG_SYS_MAXARGS, 0, do_efidebug, |
59df7e7e AT |
1626 | "Configure UEFI environment", |
1627 | efidebug_help_text | |
1628 | ); |