]>
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> | |
11 | #include <efi_loader.h> | |
12 | #include <environment.h> | |
13 | #include <exports.h> | |
39a1ff8c | 14 | #include <hexdump.h> |
59df7e7e AT |
15 | #include <malloc.h> |
16 | #include <search.h> | |
17 | #include <linux/ctype.h> | |
18 | ||
355cdb5a | 19 | #define BS systab.boottime |
59df7e7e AT |
20 | #define RT systab.runtime |
21 | ||
355cdb5a AT |
22 | /** |
23 | * efi_get_device_handle_info() - get information of UEFI device | |
24 | * | |
25 | * @handle: Handle of UEFI device | |
26 | * @dev_path_text: Pointer to text of device path | |
27 | * Return: 0 on success, -1 on failure | |
28 | * | |
29 | * Currently return a formatted text of device path. | |
30 | */ | |
31 | static int efi_get_device_handle_info(efi_handle_t handle, u16 **dev_path_text) | |
32 | { | |
33 | struct efi_device_path *dp; | |
34 | efi_status_t ret; | |
35 | ||
36 | ret = EFI_CALL(BS->open_protocol(handle, &efi_guid_device_path, | |
37 | (void **)&dp, NULL /* FIXME */, NULL, | |
38 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); | |
39 | if (ret == EFI_SUCCESS) { | |
40 | *dev_path_text = efi_dp_str(dp); | |
41 | return 0; | |
42 | } else { | |
43 | return -1; | |
44 | } | |
45 | } | |
46 | ||
47 | #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2) | |
48 | ||
49 | static const char spc[] = " "; | |
50 | static const char sep[] = "================"; | |
51 | ||
52 | /** | |
53 | * do_efi_show_devices() - show UEFI devices | |
54 | * | |
55 | * @cmdtp: Command table | |
56 | * @flag: Command flag | |
57 | * @argc: Number of arguments | |
58 | * @argv: Argument array | |
59 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure | |
60 | * | |
61 | * Implement efidebug "devices" sub-command. | |
62 | * Show all UEFI devices and their information. | |
63 | */ | |
64 | static int do_efi_show_devices(cmd_tbl_t *cmdtp, int flag, | |
65 | int argc, char * const argv[]) | |
66 | { | |
67 | efi_handle_t *handles; | |
68 | efi_uintn_t num, i; | |
69 | u16 *dev_path_text; | |
70 | efi_status_t ret; | |
71 | ||
72 | ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL, | |
73 | &num, &handles)); | |
74 | if (ret != EFI_SUCCESS) | |
75 | return CMD_RET_FAILURE; | |
76 | ||
77 | if (!num) | |
78 | return CMD_RET_SUCCESS; | |
79 | ||
80 | printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc); | |
81 | printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep); | |
82 | for (i = 0; i < num; i++) { | |
83 | if (!efi_get_device_handle_info(handles[i], &dev_path_text)) { | |
84 | printf("%p %ls\n", handles[i], dev_path_text); | |
85 | efi_free_pool(dev_path_text); | |
86 | } | |
87 | } | |
88 | ||
89 | EFI_CALL(BS->free_pool(handles)); | |
90 | ||
91 | return CMD_RET_SUCCESS; | |
92 | } | |
93 | ||
66eaf566 AT |
94 | /** |
95 | * efi_get_driver_handle_info() - get information of UEFI driver | |
96 | * | |
97 | * @handle: Handle of UEFI device | |
98 | * @driver_name: Driver name | |
99 | * @image_path: Pointer to text of device path | |
100 | * Return: 0 on success, -1 on failure | |
101 | * | |
102 | * Currently return no useful information as all UEFI drivers are | |
103 | * built-in.. | |
104 | */ | |
105 | static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name, | |
106 | u16 **image_path) | |
107 | { | |
108 | struct efi_handler *handler; | |
109 | struct efi_loaded_image *image; | |
110 | efi_status_t ret; | |
111 | ||
112 | /* | |
113 | * driver name | |
114 | * TODO: support EFI_COMPONENT_NAME2_PROTOCOL | |
115 | */ | |
116 | *driver_name = NULL; | |
117 | ||
118 | /* image name */ | |
119 | ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler); | |
120 | if (ret != EFI_SUCCESS) { | |
121 | *image_path = NULL; | |
122 | return 0; | |
123 | } | |
124 | ||
125 | image = handler->protocol_interface; | |
126 | *image_path = efi_dp_str(image->file_path); | |
127 | ||
128 | return 0; | |
129 | } | |
130 | ||
131 | /** | |
132 | * do_efi_show_drivers() - show UEFI drivers | |
133 | * | |
134 | * @cmdtp: Command table | |
135 | * @flag: Command flag | |
136 | * @argc: Number of arguments | |
137 | * @argv: Argument array | |
138 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure | |
139 | * | |
140 | * Implement efidebug "drivers" sub-command. | |
141 | * Show all UEFI drivers and their information. | |
142 | */ | |
143 | static int do_efi_show_drivers(cmd_tbl_t *cmdtp, int flag, | |
144 | int argc, char * const argv[]) | |
145 | { | |
146 | efi_handle_t *handles; | |
147 | efi_uintn_t num, i; | |
148 | u16 *driver_name, *image_path_text; | |
149 | efi_status_t ret; | |
150 | ||
151 | ret = EFI_CALL(BS->locate_handle_buffer( | |
152 | BY_PROTOCOL, &efi_guid_driver_binding_protocol, | |
153 | NULL, &num, &handles)); | |
154 | if (ret != EFI_SUCCESS) | |
155 | return CMD_RET_FAILURE; | |
156 | ||
157 | if (!num) | |
158 | return CMD_RET_SUCCESS; | |
159 | ||
160 | printf("Driver%.*s Name Image Path\n", | |
161 | EFI_HANDLE_WIDTH - 6, spc); | |
162 | printf("%.*s ==================== ====================\n", | |
163 | EFI_HANDLE_WIDTH, sep); | |
164 | for (i = 0; i < num; i++) { | |
165 | if (!efi_get_driver_handle_info(handles[i], &driver_name, | |
166 | &image_path_text)) { | |
167 | if (image_path_text) | |
168 | printf("%p %-20ls %ls\n", handles[i], | |
169 | driver_name, image_path_text); | |
170 | else | |
171 | printf("%p %-20ls <built-in>\n", | |
172 | handles[i], driver_name); | |
173 | EFI_CALL(BS->free_pool(driver_name)); | |
174 | EFI_CALL(BS->free_pool(image_path_text)); | |
175 | } | |
176 | } | |
177 | ||
178 | EFI_CALL(BS->free_pool(handles)); | |
179 | ||
180 | return CMD_RET_SUCCESS; | |
181 | } | |
182 | ||
a8014620 AT |
183 | static const struct { |
184 | const char *text; | |
185 | const efi_guid_t guid; | |
186 | } guid_list[] = { | |
187 | { | |
188 | "Device Path", | |
dec88e41 | 189 | EFI_DEVICE_PATH_PROTOCOL_GUID, |
a8014620 AT |
190 | }, |
191 | { | |
192 | "Device Path To Text", | |
193 | EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID, | |
194 | }, | |
195 | { | |
196 | "Device Path Utilities", | |
197 | EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID, | |
198 | }, | |
199 | { | |
200 | "Unicode Collation 2", | |
201 | EFI_UNICODE_COLLATION_PROTOCOL2_GUID, | |
202 | }, | |
203 | { | |
204 | "Driver Binding", | |
205 | EFI_DRIVER_BINDING_PROTOCOL_GUID, | |
206 | }, | |
207 | { | |
208 | "Simple Text Input", | |
209 | EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID, | |
210 | }, | |
211 | { | |
212 | "Simple Text Input Ex", | |
213 | EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID, | |
214 | }, | |
215 | { | |
216 | "Simple Text Output", | |
217 | EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID, | |
218 | }, | |
219 | { | |
220 | "Block IO", | |
dec88e41 | 221 | EFI_BLOCK_IO_PROTOCOL_GUID, |
a8014620 AT |
222 | }, |
223 | { | |
224 | "Simple File System", | |
225 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID, | |
226 | }, | |
227 | { | |
228 | "Loaded Image", | |
dec88e41 | 229 | EFI_LOADED_IMAGE_PROTOCOL_GUID, |
a8014620 AT |
230 | }, |
231 | { | |
dec88e41 HS |
232 | "Graphics Output", |
233 | EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID, | |
a8014620 | 234 | }, |
391bc8a9 HS |
235 | { |
236 | "HII String", | |
237 | EFI_HII_STRING_PROTOCOL_GUID, | |
238 | }, | |
239 | { | |
240 | "HII Database", | |
241 | EFI_HII_DATABASE_PROTOCOL_GUID, | |
242 | }, | |
243 | { | |
244 | "HII Config Routing", | |
245 | EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID, | |
246 | }, | |
247 | { | |
248 | "Simple Network", | |
249 | EFI_SIMPLE_NETWORK_PROTOCOL_GUID, | |
250 | }, | |
251 | { | |
252 | "PXE Base Code", | |
253 | EFI_PXE_BASE_CODE_PROTOCOL_GUID, | |
254 | }, | |
a8014620 AT |
255 | }; |
256 | ||
257 | /** | |
258 | * get_guid_text - get string of protocol guid | |
259 | * @guid: Protocol guid | |
260 | * Return: String | |
261 | * | |
262 | * Return string for display to represent the protocol. | |
263 | */ | |
264 | static const char *get_guid_text(const efi_guid_t *guid) | |
265 | { | |
266 | int i; | |
267 | ||
268 | for (i = 0; i < ARRAY_SIZE(guid_list); i++) | |
269 | if (!guidcmp(&guid_list[i].guid, guid)) | |
270 | break; | |
271 | ||
272 | if (i != ARRAY_SIZE(guid_list)) | |
273 | return guid_list[i].text; | |
274 | else | |
275 | return NULL; | |
276 | } | |
277 | ||
278 | /** | |
279 | * do_efi_show_handles() - show UEFI handles | |
280 | * | |
281 | * @cmdtp: Command table | |
282 | * @flag: Command flag | |
283 | * @argc: Number of arguments | |
284 | * @argv: Argument array | |
285 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure | |
286 | * | |
287 | * Implement efidebug "dh" sub-command. | |
288 | * Show all UEFI handles and their information, currently all protocols | |
289 | * added to handle. | |
290 | */ | |
291 | static int do_efi_show_handles(cmd_tbl_t *cmdtp, int flag, | |
292 | int argc, char * const argv[]) | |
293 | { | |
294 | efi_handle_t *handles; | |
295 | efi_guid_t **guid; | |
296 | efi_uintn_t num, count, i, j; | |
297 | const char *guid_text; | |
298 | efi_status_t ret; | |
299 | ||
300 | ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL, | |
301 | &num, &handles)); | |
302 | if (ret != EFI_SUCCESS) | |
303 | return CMD_RET_FAILURE; | |
304 | ||
305 | if (!num) | |
306 | return CMD_RET_SUCCESS; | |
307 | ||
308 | printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc); | |
309 | printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep); | |
310 | for (i = 0; i < num; i++) { | |
311 | printf("%p", handles[i]); | |
312 | ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid, | |
313 | &count)); | |
314 | if (ret || !count) { | |
315 | putc('\n'); | |
316 | continue; | |
317 | } | |
318 | ||
319 | for (j = 0; j < count; j++) { | |
320 | if (j) | |
321 | printf(", "); | |
322 | else | |
323 | putc(' '); | |
324 | ||
325 | guid_text = get_guid_text(guid[j]); | |
326 | if (guid_text) | |
327 | puts(guid_text); | |
328 | else | |
329 | printf("%pUl", guid[j]); | |
330 | } | |
331 | putc('\n'); | |
332 | } | |
333 | ||
334 | EFI_CALL(BS->free_pool(handles)); | |
335 | ||
336 | return CMD_RET_SUCCESS; | |
337 | } | |
338 | ||
fa536734 AT |
339 | /** |
340 | * do_efi_show_images() - show UEFI images | |
341 | * | |
342 | * @cmdtp: Command table | |
343 | * @flag: Command flag | |
344 | * @argc: Number of arguments | |
345 | * @argv: Argument array | |
346 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure | |
347 | * | |
348 | * Implement efidebug "images" sub-command. | |
349 | * Show all UEFI loaded images and their information. | |
350 | */ | |
351 | static int do_efi_show_images(cmd_tbl_t *cmdtp, int flag, | |
352 | int argc, char * const argv[]) | |
353 | { | |
354 | efi_print_image_infos(NULL); | |
355 | ||
356 | return CMD_RET_SUCCESS; | |
357 | } | |
358 | ||
00358bb8 AT |
359 | static const char * const efi_mem_type_string[] = { |
360 | [EFI_RESERVED_MEMORY_TYPE] = "RESERVED", | |
361 | [EFI_LOADER_CODE] = "LOADER CODE", | |
362 | [EFI_LOADER_DATA] = "LOADER DATA", | |
363 | [EFI_BOOT_SERVICES_CODE] = "BOOT CODE", | |
364 | [EFI_BOOT_SERVICES_DATA] = "BOOT DATA", | |
365 | [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE", | |
366 | [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA", | |
367 | [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL", | |
368 | [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM", | |
369 | [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM", | |
370 | [EFI_ACPI_MEMORY_NVS] = "ACPI NVS", | |
371 | [EFI_MMAP_IO] = "IO", | |
372 | [EFI_MMAP_IO_PORT] = "IO PORT", | |
373 | [EFI_PAL_CODE] = "PAL", | |
374 | }; | |
375 | ||
376 | static const struct efi_mem_attrs { | |
377 | const u64 bit; | |
378 | const char *text; | |
379 | } efi_mem_attrs[] = { | |
380 | {EFI_MEMORY_UC, "UC"}, | |
381 | {EFI_MEMORY_UC, "UC"}, | |
382 | {EFI_MEMORY_WC, "WC"}, | |
383 | {EFI_MEMORY_WT, "WT"}, | |
384 | {EFI_MEMORY_WB, "WB"}, | |
385 | {EFI_MEMORY_UCE, "UCE"}, | |
386 | {EFI_MEMORY_WP, "WP"}, | |
387 | {EFI_MEMORY_RP, "RP"}, | |
388 | {EFI_MEMORY_XP, "WP"}, | |
389 | {EFI_MEMORY_NV, "NV"}, | |
390 | {EFI_MEMORY_MORE_RELIABLE, "REL"}, | |
391 | {EFI_MEMORY_RO, "RO"}, | |
392 | {EFI_MEMORY_RUNTIME, "RT"}, | |
393 | }; | |
394 | ||
395 | /** | |
396 | * print_memory_attributes() - print memory map attributes | |
397 | * @attributes: Attribute value | |
398 | * | |
399 | * Print memory map attributes | |
400 | */ | |
401 | static void print_memory_attributes(u64 attributes) | |
402 | { | |
403 | int sep, i; | |
404 | ||
405 | for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++) | |
406 | if (attributes & efi_mem_attrs[i].bit) { | |
407 | if (sep) { | |
408 | putc('|'); | |
409 | } else { | |
410 | putc(' '); | |
411 | sep = 1; | |
412 | } | |
413 | puts(efi_mem_attrs[i].text); | |
414 | } | |
415 | } | |
416 | ||
417 | #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2) | |
418 | ||
419 | /** | |
420 | * do_efi_show_memmap() - show UEFI memory map | |
421 | * | |
422 | * @cmdtp: Command table | |
423 | * @flag: Command flag | |
424 | * @argc: Number of arguments | |
425 | * @argv: Argument array | |
426 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure | |
427 | * | |
428 | * Implement efidebug "memmap" sub-command. | |
429 | * Show UEFI memory map. | |
430 | */ | |
431 | static int do_efi_show_memmap(cmd_tbl_t *cmdtp, int flag, | |
432 | int argc, char * const argv[]) | |
433 | { | |
434 | struct efi_mem_desc *memmap = NULL, *map; | |
435 | efi_uintn_t map_size = 0; | |
436 | const char *type; | |
437 | int i; | |
438 | efi_status_t ret; | |
439 | ||
440 | ret = EFI_CALL(BS->get_memory_map(&map_size, memmap, NULL, NULL, NULL)); | |
441 | if (ret == EFI_BUFFER_TOO_SMALL) { | |
442 | map_size += sizeof(struct efi_mem_desc); /* for my own */ | |
443 | ret = EFI_CALL(BS->allocate_pool(EFI_LOADER_DATA, | |
444 | map_size, (void *)&memmap)); | |
445 | if (ret != EFI_SUCCESS) | |
446 | return CMD_RET_FAILURE; | |
447 | ret = EFI_CALL(BS->get_memory_map(&map_size, memmap, | |
448 | NULL, NULL, NULL)); | |
449 | } | |
450 | if (ret != EFI_SUCCESS) { | |
451 | EFI_CALL(BS->free_pool(memmap)); | |
452 | return CMD_RET_FAILURE; | |
453 | } | |
454 | ||
455 | printf("Type Start%.*s End%.*s Attributes\n", | |
456 | EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc); | |
457 | printf("================ %.*s %.*s ==========\n", | |
458 | EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep); | |
459 | for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) { | |
460 | if (map->type < EFI_MAX_MEMORY_TYPE) | |
461 | type = efi_mem_type_string[map->type]; | |
462 | else | |
463 | type = "(unknown)"; | |
464 | ||
465 | printf("%-16s %.*llx-%.*llx", type, | |
466 | EFI_PHYS_ADDR_WIDTH, | |
467 | map->physical_start, | |
468 | EFI_PHYS_ADDR_WIDTH, | |
469 | map->physical_start + map->num_pages * EFI_PAGE_SIZE); | |
470 | ||
471 | print_memory_attributes(map->attribute); | |
472 | putc('\n'); | |
473 | } | |
474 | ||
475 | EFI_CALL(BS->free_pool(memmap)); | |
476 | ||
477 | return CMD_RET_SUCCESS; | |
478 | } | |
479 | ||
59df7e7e AT |
480 | /** |
481 | * do_efi_boot_add() - set UEFI load option | |
482 | * | |
483 | * @cmdtp: Command table | |
484 | * @flag: Command flag | |
485 | * @argc: Number of arguments | |
486 | * @argv: Argument array | |
487 | * Return: CMD_RET_SUCCESS on success, | |
488 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure | |
489 | * | |
490 | * Implement efidebug "boot add" sub-command. | |
491 | * Create or change UEFI load option. | |
492 | * - boot add <id> <label> <interface> <devnum>[:<part>] <file> <options> | |
493 | */ | |
494 | static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag, | |
495 | int argc, char * const argv[]) | |
496 | { | |
497 | int id; | |
498 | char *endp; | |
499 | char var_name[9]; | |
500 | u16 var_name16[9], *p; | |
501 | efi_guid_t guid; | |
502 | size_t label_len, label_len16; | |
503 | u16 *label; | |
504 | struct efi_device_path *device_path = NULL, *file_path = NULL; | |
505 | struct efi_load_option lo; | |
506 | void *data = NULL; | |
507 | efi_uintn_t size; | |
508 | int ret; | |
509 | ||
510 | if (argc < 6 || argc > 7) | |
511 | return CMD_RET_USAGE; | |
512 | ||
513 | id = (int)simple_strtoul(argv[1], &endp, 16); | |
514 | if (*endp != '\0' || id > 0xffff) | |
1fa442ed | 515 | return CMD_RET_USAGE; |
59df7e7e AT |
516 | |
517 | sprintf(var_name, "Boot%04X", id); | |
518 | p = var_name16; | |
519 | utf8_utf16_strncpy(&p, var_name, 9); | |
520 | ||
521 | guid = efi_global_variable_guid; | |
522 | ||
523 | /* attributes */ | |
524 | lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */ | |
525 | ||
526 | /* label */ | |
527 | label_len = strlen(argv[2]); | |
528 | label_len16 = utf8_utf16_strnlen(argv[2], label_len); | |
529 | label = malloc((label_len16 + 1) * sizeof(u16)); | |
530 | if (!label) | |
531 | return CMD_RET_FAILURE; | |
532 | lo.label = label; /* label will be changed below */ | |
533 | utf8_utf16_strncpy(&label, argv[2], label_len); | |
534 | ||
535 | /* file path */ | |
536 | ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path, | |
537 | &file_path); | |
538 | if (ret != EFI_SUCCESS) { | |
539 | printf("Cannot create device path for \"%s %s\"\n", | |
540 | argv[3], argv[4]); | |
541 | ret = CMD_RET_FAILURE; | |
542 | goto out; | |
543 | } | |
544 | lo.file_path = file_path; | |
545 | lo.file_path_length = efi_dp_size(file_path) | |
546 | + sizeof(struct efi_device_path); /* for END */ | |
547 | ||
548 | /* optional data */ | |
39a1ff8c HS |
549 | if (argc < 6) |
550 | lo.optional_data = NULL; | |
551 | else | |
552 | lo.optional_data = (const u8 *)argv[6]; | |
59df7e7e AT |
553 | |
554 | size = efi_serialize_load_option(&lo, (u8 **)&data); | |
555 | if (!size) { | |
556 | ret = CMD_RET_FAILURE; | |
557 | goto out; | |
558 | } | |
559 | ||
560 | ret = EFI_CALL(RT->set_variable(var_name16, &guid, | |
561 | EFI_VARIABLE_BOOTSERVICE_ACCESS | | |
562 | EFI_VARIABLE_RUNTIME_ACCESS, | |
563 | size, data)); | |
564 | ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE); | |
565 | out: | |
566 | free(data); | |
567 | efi_free_pool(device_path); | |
568 | efi_free_pool(file_path); | |
569 | free(lo.label); | |
570 | ||
571 | return ret; | |
572 | } | |
573 | ||
574 | /** | |
575 | * do_efi_boot_rm() - delete UEFI load options | |
576 | * | |
577 | * @cmdtp: Command table | |
578 | * @flag: Command flag | |
579 | * @argc: Number of arguments | |
580 | * @argv: Argument array | |
581 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure | |
582 | * | |
583 | * Implement efidebug "boot rm" sub-command. | |
584 | * Delete UEFI load options. | |
585 | * - boot rm <id> ... | |
586 | */ | |
587 | static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag, | |
588 | int argc, char * const argv[]) | |
589 | { | |
590 | efi_guid_t guid; | |
591 | int id, i; | |
592 | char *endp; | |
593 | char var_name[9]; | |
594 | u16 var_name16[9]; | |
595 | efi_status_t ret; | |
596 | ||
597 | if (argc == 1) | |
598 | return CMD_RET_USAGE; | |
599 | ||
600 | guid = efi_global_variable_guid; | |
601 | for (i = 1; i < argc; i++, argv++) { | |
602 | id = (int)simple_strtoul(argv[1], &endp, 16); | |
603 | if (*endp != '\0' || id > 0xffff) | |
604 | return CMD_RET_FAILURE; | |
605 | ||
606 | sprintf(var_name, "Boot%04X", id); | |
607 | utf8_utf16_strncpy((u16 **)&var_name16, var_name, 9); | |
608 | ||
609 | ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL)); | |
610 | if (ret) { | |
611 | printf("cannot remove Boot%04X", id); | |
612 | return CMD_RET_FAILURE; | |
613 | } | |
614 | } | |
615 | ||
616 | return CMD_RET_SUCCESS; | |
617 | } | |
618 | ||
619 | /** | |
620 | * show_efi_boot_opt_data() - dump UEFI load option | |
621 | * | |
39a1ff8c HS |
622 | * @id: load option number |
623 | * @data: value of UEFI load option variable | |
624 | * @size: size of the boot option | |
59df7e7e AT |
625 | * |
626 | * Decode the value of UEFI load option variable and print information. | |
627 | */ | |
39a1ff8c | 628 | static void show_efi_boot_opt_data(int id, void *data, size_t size) |
59df7e7e AT |
629 | { |
630 | struct efi_load_option lo; | |
631 | char *label, *p; | |
632 | size_t label_len16, label_len; | |
633 | u16 *dp_str; | |
634 | ||
635 | efi_deserialize_load_option(&lo, data); | |
636 | ||
637 | label_len16 = u16_strlen(lo.label); | |
638 | label_len = utf16_utf8_strnlen(lo.label, label_len16); | |
639 | label = malloc(label_len + 1); | |
640 | if (!label) | |
641 | return; | |
642 | p = label; | |
643 | utf16_utf8_strncpy(&p, lo.label, label_len16); | |
644 | ||
645 | printf("Boot%04X:\n", id); | |
39a1ff8c | 646 | printf(" attributes: %c%c%c (0x%08x)\n", |
59df7e7e AT |
647 | /* ACTIVE */ |
648 | lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-', | |
649 | /* FORCE RECONNECT */ | |
650 | lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-', | |
651 | /* HIDDEN */ | |
652 | lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-', | |
653 | lo.attributes); | |
39a1ff8c | 654 | printf(" label: %s\n", label); |
59df7e7e AT |
655 | |
656 | dp_str = efi_dp_str(lo.file_path); | |
39a1ff8c | 657 | printf(" file_path: %ls\n", dp_str); |
59df7e7e AT |
658 | efi_free_pool(dp_str); |
659 | ||
39a1ff8c HS |
660 | printf(" data:\n"); |
661 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, | |
662 | lo.optional_data, size + (u8 *)data - | |
663 | (u8 *)lo.optional_data, true); | |
59df7e7e AT |
664 | free(label); |
665 | } | |
666 | ||
667 | /** | |
668 | * show_efi_boot_opt() - dump UEFI load option | |
669 | * | |
670 | * @id: Load option number | |
671 | * | |
672 | * Dump information defined by UEFI load option. | |
673 | */ | |
674 | static void show_efi_boot_opt(int id) | |
675 | { | |
676 | char var_name[9]; | |
677 | u16 var_name16[9], *p; | |
678 | efi_guid_t guid; | |
679 | void *data = NULL; | |
680 | efi_uintn_t size; | |
681 | int ret; | |
682 | ||
683 | sprintf(var_name, "Boot%04X", id); | |
684 | p = var_name16; | |
685 | utf8_utf16_strncpy(&p, var_name, 9); | |
686 | guid = efi_global_variable_guid; | |
687 | ||
688 | size = 0; | |
689 | ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL)); | |
690 | if (ret == (int)EFI_BUFFER_TOO_SMALL) { | |
691 | data = malloc(size); | |
692 | ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, | |
693 | data)); | |
694 | } | |
695 | if (ret == EFI_SUCCESS) | |
39a1ff8c | 696 | show_efi_boot_opt_data(id, data, size); |
59df7e7e AT |
697 | else if (ret == EFI_NOT_FOUND) |
698 | printf("Boot%04X: not found\n", id); | |
699 | ||
700 | free(data); | |
701 | } | |
702 | ||
ffe21574 AT |
703 | static int u16_tohex(u16 c) |
704 | { | |
705 | if (c >= '0' && c <= '9') | |
706 | return c - '0'; | |
707 | if (c >= 'A' && c <= 'F') | |
708 | return c - 'A' + 10; | |
709 | ||
710 | /* not hexadecimal */ | |
711 | return -1; | |
712 | } | |
713 | ||
59df7e7e AT |
714 | /** |
715 | * show_efi_boot_dump() - dump all UEFI load options | |
716 | * | |
717 | * @cmdtp: Command table | |
718 | * @flag: Command flag | |
719 | * @argc: Number of arguments | |
720 | * @argv: Argument array | |
721 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure | |
722 | * | |
723 | * Implement efidebug "boot dump" sub-command. | |
724 | * Dump information of all UEFI load options defined. | |
725 | * - boot dump | |
726 | */ | |
727 | static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag, | |
728 | int argc, char * const argv[]) | |
729 | { | |
ffe21574 AT |
730 | u16 *var_name16, *p; |
731 | efi_uintn_t buf_size, size; | |
732 | efi_guid_t guid; | |
733 | int id, i, digit; | |
734 | efi_status_t ret; | |
59df7e7e AT |
735 | |
736 | if (argc > 1) | |
737 | return CMD_RET_USAGE; | |
738 | ||
ffe21574 AT |
739 | buf_size = 128; |
740 | var_name16 = malloc(buf_size); | |
741 | if (!var_name16) | |
59df7e7e AT |
742 | return CMD_RET_FAILURE; |
743 | ||
ffe21574 AT |
744 | var_name16[0] = 0; |
745 | for (;;) { | |
746 | size = buf_size; | |
747 | ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16, | |
748 | &guid)); | |
749 | if (ret == EFI_NOT_FOUND) | |
59df7e7e | 750 | break; |
ffe21574 AT |
751 | if (ret == EFI_BUFFER_TOO_SMALL) { |
752 | buf_size = size; | |
753 | p = realloc(var_name16, buf_size); | |
754 | if (!p) { | |
755 | free(var_name16); | |
756 | return CMD_RET_FAILURE; | |
757 | } | |
758 | var_name16 = p; | |
759 | ret = EFI_CALL(efi_get_next_variable_name(&size, | |
760 | var_name16, | |
761 | &guid)); | |
762 | } | |
763 | if (ret != EFI_SUCCESS) { | |
764 | free(var_name16); | |
765 | return CMD_RET_FAILURE; | |
766 | } | |
767 | ||
768 | if (memcmp(var_name16, L"Boot", 8)) | |
769 | continue; | |
770 | ||
771 | for (id = 0, i = 0; i < 4; i++) { | |
772 | digit = u16_tohex(var_name16[4 + i]); | |
773 | if (digit < 0) | |
774 | break; | |
775 | id = (id << 4) + digit; | |
776 | } | |
777 | if (i == 4 && !var_name16[8]) | |
778 | show_efi_boot_opt(id); | |
59df7e7e | 779 | } |
ffe21574 AT |
780 | |
781 | free(var_name16); | |
59df7e7e AT |
782 | |
783 | return CMD_RET_SUCCESS; | |
784 | } | |
785 | ||
786 | /** | |
787 | * show_efi_boot_order() - show order of UEFI load options | |
788 | * | |
789 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure | |
790 | * | |
791 | * Show order of UEFI load options defined by BootOrder variable. | |
792 | */ | |
793 | static int show_efi_boot_order(void) | |
794 | { | |
795 | efi_guid_t guid; | |
796 | u16 *bootorder = NULL; | |
797 | efi_uintn_t size; | |
798 | int num, i; | |
799 | char var_name[9]; | |
800 | u16 var_name16[9], *p16; | |
801 | void *data; | |
802 | struct efi_load_option lo; | |
803 | char *label, *p; | |
804 | size_t label_len16, label_len; | |
805 | efi_status_t ret; | |
806 | ||
807 | guid = efi_global_variable_guid; | |
808 | size = 0; | |
809 | ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, &size, | |
810 | NULL)); | |
811 | if (ret == EFI_BUFFER_TOO_SMALL) { | |
812 | bootorder = malloc(size); | |
813 | ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, | |
814 | &size, bootorder)); | |
815 | } | |
816 | if (ret == EFI_NOT_FOUND) { | |
817 | printf("BootOrder not defined\n"); | |
818 | ret = CMD_RET_SUCCESS; | |
819 | goto out; | |
820 | } else if (ret != EFI_SUCCESS) { | |
821 | ret = CMD_RET_FAILURE; | |
822 | goto out; | |
823 | } | |
824 | ||
825 | num = size / sizeof(u16); | |
826 | for (i = 0; i < num; i++) { | |
827 | sprintf(var_name, "Boot%04X", bootorder[i]); | |
828 | p16 = var_name16; | |
829 | utf8_utf16_strncpy(&p16, var_name, 9); | |
830 | ||
831 | size = 0; | |
832 | ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, | |
833 | NULL)); | |
834 | if (ret != EFI_BUFFER_TOO_SMALL) { | |
835 | printf("%2d: Boot%04X: (not defined)\n", | |
836 | i + 1, bootorder[i]); | |
837 | continue; | |
838 | } | |
839 | ||
840 | data = malloc(size); | |
841 | if (!data) { | |
842 | ret = CMD_RET_FAILURE; | |
843 | goto out; | |
844 | } | |
845 | ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, | |
846 | data)); | |
847 | if (ret != EFI_SUCCESS) { | |
848 | free(data); | |
849 | ret = CMD_RET_FAILURE; | |
850 | goto out; | |
851 | } | |
852 | ||
853 | efi_deserialize_load_option(&lo, data); | |
854 | ||
855 | label_len16 = u16_strlen(lo.label); | |
856 | label_len = utf16_utf8_strnlen(lo.label, label_len16); | |
857 | label = malloc(label_len + 1); | |
858 | if (!label) { | |
859 | free(data); | |
860 | ret = CMD_RET_FAILURE; | |
861 | goto out; | |
862 | } | |
863 | p = label; | |
864 | utf16_utf8_strncpy(&p, lo.label, label_len16); | |
865 | printf("%2d: Boot%04X: %s\n", i + 1, bootorder[i], label); | |
866 | free(label); | |
867 | ||
868 | free(data); | |
869 | } | |
870 | out: | |
871 | free(bootorder); | |
872 | ||
873 | return ret; | |
874 | } | |
875 | ||
876 | /** | |
877 | * do_efi_boot_next() - manage UEFI BootNext variable | |
878 | * | |
879 | * @cmdtp: Command table | |
880 | * @flag: Command flag | |
881 | * @argc: Number of arguments | |
882 | * @argv: Argument array | |
883 | * Return: CMD_RET_SUCCESS on success, | |
884 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure | |
885 | * | |
886 | * Implement efidebug "boot next" sub-command. | |
887 | * Set BootNext variable. | |
888 | * - boot next <id> | |
889 | */ | |
890 | static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag, | |
891 | int argc, char * const argv[]) | |
892 | { | |
893 | u16 bootnext; | |
894 | efi_uintn_t size; | |
895 | char *endp; | |
896 | efi_guid_t guid; | |
897 | efi_status_t ret; | |
898 | ||
899 | if (argc != 2) | |
900 | return CMD_RET_USAGE; | |
901 | ||
902 | bootnext = (u16)simple_strtoul(argv[1], &endp, 16); | |
903 | if (*endp != '\0' || bootnext > 0xffff) { | |
904 | printf("invalid value: %s\n", argv[1]); | |
905 | ret = CMD_RET_FAILURE; | |
906 | goto out; | |
907 | } | |
908 | ||
909 | guid = efi_global_variable_guid; | |
910 | size = sizeof(u16); | |
911 | ret = EFI_CALL(RT->set_variable(L"BootNext", &guid, | |
912 | EFI_VARIABLE_BOOTSERVICE_ACCESS | | |
913 | EFI_VARIABLE_RUNTIME_ACCESS, | |
914 | size, &bootnext)); | |
915 | ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE); | |
916 | out: | |
917 | return ret; | |
918 | } | |
919 | ||
920 | /** | |
921 | * do_efi_boot_order() - manage UEFI BootOrder variable | |
922 | * | |
923 | * @cmdtp: Command table | |
924 | * @flag: Command flag | |
925 | * @argc: Number of arguments | |
926 | * @argv: Argument array | |
927 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure | |
928 | * | |
929 | * Implement efidebug "boot order" sub-command. | |
930 | * Show order of UEFI load options, or change it in BootOrder variable. | |
931 | * - boot order [<id> ...] | |
932 | */ | |
933 | static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag, | |
934 | int argc, char * const argv[]) | |
935 | { | |
936 | u16 *bootorder = NULL; | |
937 | efi_uintn_t size; | |
938 | int id, i; | |
939 | char *endp; | |
940 | efi_guid_t guid; | |
941 | efi_status_t ret; | |
942 | ||
943 | if (argc == 1) | |
944 | return show_efi_boot_order(); | |
945 | ||
946 | argc--; | |
947 | argv++; | |
948 | ||
949 | size = argc * sizeof(u16); | |
950 | bootorder = malloc(size); | |
951 | if (!bootorder) | |
952 | return CMD_RET_FAILURE; | |
953 | ||
954 | for (i = 0; i < argc; i++) { | |
955 | id = (int)simple_strtoul(argv[i], &endp, 16); | |
956 | if (*endp != '\0' || id > 0xffff) { | |
957 | printf("invalid value: %s\n", argv[i]); | |
958 | ret = CMD_RET_FAILURE; | |
959 | goto out; | |
960 | } | |
961 | ||
962 | bootorder[i] = (u16)id; | |
963 | } | |
964 | ||
965 | guid = efi_global_variable_guid; | |
966 | ret = EFI_CALL(RT->set_variable(L"BootOrder", &guid, | |
967 | EFI_VARIABLE_BOOTSERVICE_ACCESS | | |
968 | EFI_VARIABLE_RUNTIME_ACCESS, | |
969 | size, bootorder)); | |
970 | ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE); | |
971 | out: | |
972 | free(bootorder); | |
973 | ||
974 | return ret; | |
975 | } | |
976 | ||
977 | static cmd_tbl_t cmd_efidebug_boot_sub[] = { | |
978 | U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""), | |
979 | U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""), | |
980 | U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""), | |
981 | U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""), | |
982 | U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order, | |
983 | "", ""), | |
984 | }; | |
985 | ||
986 | /** | |
987 | * do_efi_boot_opt() - manage UEFI load options | |
988 | * | |
989 | * @cmdtp: Command table | |
990 | * @flag: Command flag | |
991 | * @argc: Number of arguments | |
992 | * @argv: Argument array | |
993 | * Return: CMD_RET_SUCCESS on success, | |
994 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure | |
995 | * | |
996 | * Implement efidebug "boot" sub-command. | |
997 | * See above for details of sub-commands. | |
998 | */ | |
999 | static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag, | |
1000 | int argc, char * const argv[]) | |
1001 | { | |
1002 | cmd_tbl_t *cp; | |
1003 | ||
1004 | if (argc < 2) | |
1005 | return CMD_RET_USAGE; | |
1006 | ||
1007 | argc--; argv++; | |
1008 | ||
1009 | cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub, | |
1010 | ARRAY_SIZE(cmd_efidebug_boot_sub)); | |
1011 | if (!cp) | |
1012 | return CMD_RET_USAGE; | |
1013 | ||
1014 | return cp->cmd(cmdtp, flag, argc, argv); | |
1015 | } | |
1016 | ||
1017 | static cmd_tbl_t cmd_efidebug_sub[] = { | |
1018 | U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""), | |
355cdb5a AT |
1019 | U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices, |
1020 | "", ""), | |
66eaf566 AT |
1021 | U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers, |
1022 | "", ""), | |
a8014620 AT |
1023 | U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles, |
1024 | "", ""), | |
fa536734 AT |
1025 | U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images, |
1026 | "", ""), | |
00358bb8 AT |
1027 | U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap, |
1028 | "", ""), | |
59df7e7e AT |
1029 | }; |
1030 | ||
1031 | /** | |
1032 | * do_efidebug() - display and configure UEFI environment | |
1033 | * | |
1034 | * @cmdtp: Command table | |
1035 | * @flag: Command flag | |
1036 | * @argc: Number of arguments | |
1037 | * @argv: Argument array | |
1038 | * Return: CMD_RET_SUCCESS on success, | |
1039 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure | |
1040 | * | |
1041 | * Implement efidebug command which allows us to display and | |
1042 | * configure UEFI environment. | |
1043 | * See above for details of sub-commands. | |
1044 | */ | |
1045 | static int do_efidebug(cmd_tbl_t *cmdtp, int flag, | |
1046 | int argc, char * const argv[]) | |
1047 | { | |
1048 | cmd_tbl_t *cp; | |
1049 | efi_status_t r; | |
1050 | ||
1051 | if (argc < 2) | |
1052 | return CMD_RET_USAGE; | |
1053 | ||
1054 | argc--; argv++; | |
1055 | ||
1056 | /* Initialize UEFI drivers */ | |
1057 | r = efi_init_obj_list(); | |
1058 | if (r != EFI_SUCCESS) { | |
1059 | printf("Error: Cannot initialize UEFI sub-system, r = %lu\n", | |
1060 | r & ~EFI_ERROR_MASK); | |
1061 | return CMD_RET_FAILURE; | |
1062 | } | |
1063 | ||
1064 | cp = find_cmd_tbl(argv[0], cmd_efidebug_sub, | |
1065 | ARRAY_SIZE(cmd_efidebug_sub)); | |
1066 | if (!cp) | |
1067 | return CMD_RET_USAGE; | |
1068 | ||
1069 | return cp->cmd(cmdtp, flag, argc, argv); | |
1070 | } | |
1071 | ||
1072 | #ifdef CONFIG_SYS_LONGHELP | |
1073 | static char efidebug_help_text[] = | |
1074 | " - UEFI Shell-like interface to configure UEFI environment\n" | |
1075 | "\n" | |
1076 | "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n" | |
1077 | " - set UEFI BootXXXX variable\n" | |
1078 | " <load options> will be passed to UEFI application\n" | |
1079 | "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n" | |
1080 | " - delete UEFI BootXXXX variables\n" | |
1081 | "efidebug boot dump\n" | |
1082 | " - dump all UEFI BootXXXX variables\n" | |
1083 | "efidebug boot next <bootid>\n" | |
1084 | " - set UEFI BootNext variable\n" | |
1085 | "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n" | |
1086 | " - set/show UEFI boot order\n" | |
355cdb5a AT |
1087 | "\n" |
1088 | "efidebug devices\n" | |
66eaf566 AT |
1089 | " - show uefi devices\n" |
1090 | "efidebug drivers\n" | |
a8014620 AT |
1091 | " - show uefi drivers\n" |
1092 | "efidebug dh\n" | |
fa536734 AT |
1093 | " - show uefi handles\n" |
1094 | "efidebug images\n" | |
00358bb8 AT |
1095 | " - show loaded images\n" |
1096 | "efidebug memmap\n" | |
1097 | " - show uefi memory map\n"; | |
59df7e7e AT |
1098 | #endif |
1099 | ||
1100 | U_BOOT_CMD( | |
1101 | efidebug, 10, 0, do_efidebug, | |
1102 | "Configure UEFI environment", | |
1103 | efidebug_help_text | |
1104 | ); |