efidebug.c 27 KB

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