efidebug.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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. };
  231. /**
  232. * get_guid_text - get string of protocol guid
  233. * @guid: Protocol guid
  234. * Return: String
  235. *
  236. * Return string for display to represent the protocol.
  237. */
  238. static const char *get_guid_text(const efi_guid_t *guid)
  239. {
  240. int i;
  241. for (i = 0; i < ARRAY_SIZE(guid_list); i++)
  242. if (!guidcmp(&guid_list[i].guid, guid))
  243. break;
  244. if (i != ARRAY_SIZE(guid_list))
  245. return guid_list[i].text;
  246. else
  247. return NULL;
  248. }
  249. /**
  250. * do_efi_show_handles() - show UEFI handles
  251. *
  252. * @cmdtp: Command table
  253. * @flag: Command flag
  254. * @argc: Number of arguments
  255. * @argv: Argument array
  256. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  257. *
  258. * Implement efidebug "dh" sub-command.
  259. * Show all UEFI handles and their information, currently all protocols
  260. * added to handle.
  261. */
  262. static int do_efi_show_handles(cmd_tbl_t *cmdtp, int flag,
  263. int argc, char * const argv[])
  264. {
  265. efi_handle_t *handles;
  266. efi_guid_t **guid;
  267. efi_uintn_t num, count, i, j;
  268. const char *guid_text;
  269. efi_status_t ret;
  270. ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL,
  271. &num, &handles));
  272. if (ret != EFI_SUCCESS)
  273. return CMD_RET_FAILURE;
  274. if (!num)
  275. return CMD_RET_SUCCESS;
  276. printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
  277. printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
  278. for (i = 0; i < num; i++) {
  279. printf("%p", handles[i]);
  280. ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
  281. &count));
  282. if (ret || !count) {
  283. putc('\n');
  284. continue;
  285. }
  286. for (j = 0; j < count; j++) {
  287. if (j)
  288. printf(", ");
  289. else
  290. putc(' ');
  291. guid_text = get_guid_text(guid[j]);
  292. if (guid_text)
  293. puts(guid_text);
  294. else
  295. printf("%pUl", guid[j]);
  296. }
  297. putc('\n');
  298. }
  299. EFI_CALL(BS->free_pool(handles));
  300. return CMD_RET_SUCCESS;
  301. }
  302. /**
  303. * do_efi_show_images() - show UEFI images
  304. *
  305. * @cmdtp: Command table
  306. * @flag: Command flag
  307. * @argc: Number of arguments
  308. * @argv: Argument array
  309. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  310. *
  311. * Implement efidebug "images" sub-command.
  312. * Show all UEFI loaded images and their information.
  313. */
  314. static int do_efi_show_images(cmd_tbl_t *cmdtp, int flag,
  315. int argc, char * const argv[])
  316. {
  317. efi_print_image_infos(NULL);
  318. return CMD_RET_SUCCESS;
  319. }
  320. static const char * const efi_mem_type_string[] = {
  321. [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
  322. [EFI_LOADER_CODE] = "LOADER CODE",
  323. [EFI_LOADER_DATA] = "LOADER DATA",
  324. [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
  325. [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
  326. [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
  327. [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
  328. [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
  329. [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
  330. [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
  331. [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
  332. [EFI_MMAP_IO] = "IO",
  333. [EFI_MMAP_IO_PORT] = "IO PORT",
  334. [EFI_PAL_CODE] = "PAL",
  335. };
  336. static const struct efi_mem_attrs {
  337. const u64 bit;
  338. const char *text;
  339. } efi_mem_attrs[] = {
  340. {EFI_MEMORY_UC, "UC"},
  341. {EFI_MEMORY_UC, "UC"},
  342. {EFI_MEMORY_WC, "WC"},
  343. {EFI_MEMORY_WT, "WT"},
  344. {EFI_MEMORY_WB, "WB"},
  345. {EFI_MEMORY_UCE, "UCE"},
  346. {EFI_MEMORY_WP, "WP"},
  347. {EFI_MEMORY_RP, "RP"},
  348. {EFI_MEMORY_XP, "WP"},
  349. {EFI_MEMORY_NV, "NV"},
  350. {EFI_MEMORY_MORE_RELIABLE, "REL"},
  351. {EFI_MEMORY_RO, "RO"},
  352. {EFI_MEMORY_RUNTIME, "RT"},
  353. };
  354. /**
  355. * print_memory_attributes() - print memory map attributes
  356. *
  357. * @attributes: Attribute value
  358. *
  359. * Print memory map attributes
  360. */
  361. static void print_memory_attributes(u64 attributes)
  362. {
  363. int sep, i;
  364. for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
  365. if (attributes & efi_mem_attrs[i].bit) {
  366. if (sep) {
  367. putc('|');
  368. } else {
  369. putc(' ');
  370. sep = 1;
  371. }
  372. puts(efi_mem_attrs[i].text);
  373. }
  374. }
  375. #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
  376. /**
  377. * do_efi_show_memmap() - show UEFI memory map
  378. *
  379. * @cmdtp: Command table
  380. * @flag: Command flag
  381. * @argc: Number of arguments
  382. * @argv: Argument array
  383. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  384. *
  385. * Implement efidebug "memmap" sub-command.
  386. * Show UEFI memory map.
  387. */
  388. static int do_efi_show_memmap(cmd_tbl_t *cmdtp, int flag,
  389. int argc, char * const argv[])
  390. {
  391. struct efi_mem_desc *memmap = NULL, *map;
  392. efi_uintn_t map_size = 0;
  393. const char *type;
  394. int i;
  395. efi_status_t ret;
  396. ret = EFI_CALL(BS->get_memory_map(&map_size, memmap, NULL, NULL, NULL));
  397. if (ret == EFI_BUFFER_TOO_SMALL) {
  398. map_size += sizeof(struct efi_mem_desc); /* for my own */
  399. ret = EFI_CALL(BS->allocate_pool(EFI_LOADER_DATA,
  400. map_size, (void *)&memmap));
  401. if (ret != EFI_SUCCESS)
  402. return CMD_RET_FAILURE;
  403. ret = EFI_CALL(BS->get_memory_map(&map_size, memmap,
  404. NULL, NULL, NULL));
  405. }
  406. if (ret != EFI_SUCCESS) {
  407. EFI_CALL(BS->free_pool(memmap));
  408. return CMD_RET_FAILURE;
  409. }
  410. printf("Type Start%.*s End%.*s Attributes\n",
  411. EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
  412. printf("================ %.*s %.*s ==========\n",
  413. EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
  414. for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
  415. if (map->type < EFI_MAX_MEMORY_TYPE)
  416. type = efi_mem_type_string[map->type];
  417. else
  418. type = "(unknown)";
  419. printf("%-16s %.*llx-%.*llx", type,
  420. EFI_PHYS_ADDR_WIDTH,
  421. map->physical_start,
  422. EFI_PHYS_ADDR_WIDTH,
  423. map->physical_start + map->num_pages * EFI_PAGE_SIZE);
  424. print_memory_attributes(map->attribute);
  425. putc('\n');
  426. }
  427. EFI_CALL(BS->free_pool(memmap));
  428. return CMD_RET_SUCCESS;
  429. }
  430. /**
  431. * do_efi_boot_add() - set UEFI load option
  432. *
  433. * @cmdtp: Command table
  434. * @flag: Command flag
  435. * @argc: Number of arguments
  436. * @argv: Argument array
  437. * Return: CMD_RET_SUCCESS on success,
  438. * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  439. *
  440. * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
  441. *
  442. * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
  443. */
  444. static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
  445. int argc, char * const argv[])
  446. {
  447. int id;
  448. char *endp;
  449. char var_name[9];
  450. u16 var_name16[9], *p;
  451. efi_guid_t guid;
  452. size_t label_len, label_len16;
  453. u16 *label;
  454. struct efi_device_path *device_path = NULL, *file_path = NULL;
  455. struct efi_load_option lo;
  456. void *data = NULL;
  457. efi_uintn_t size;
  458. efi_status_t ret;
  459. int r = CMD_RET_SUCCESS;
  460. if (argc < 6 || argc > 7)
  461. return CMD_RET_USAGE;
  462. id = (int)simple_strtoul(argv[1], &endp, 16);
  463. if (*endp != '\0' || id > 0xffff)
  464. return CMD_RET_USAGE;
  465. sprintf(var_name, "Boot%04X", id);
  466. p = var_name16;
  467. utf8_utf16_strncpy(&p, var_name, 9);
  468. guid = efi_global_variable_guid;
  469. /* attributes */
  470. lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
  471. /* label */
  472. label_len = strlen(argv[2]);
  473. label_len16 = utf8_utf16_strnlen(argv[2], label_len);
  474. label = malloc((label_len16 + 1) * sizeof(u16));
  475. if (!label)
  476. return CMD_RET_FAILURE;
  477. lo.label = label; /* label will be changed below */
  478. utf8_utf16_strncpy(&label, argv[2], label_len);
  479. /* file path */
  480. ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
  481. &file_path);
  482. if (ret != EFI_SUCCESS) {
  483. printf("Cannot create device path for \"%s %s\"\n",
  484. argv[3], argv[4]);
  485. r = CMD_RET_FAILURE;
  486. goto out;
  487. }
  488. lo.file_path = file_path;
  489. lo.file_path_length = efi_dp_size(file_path)
  490. + sizeof(struct efi_device_path); /* for END */
  491. /* optional data */
  492. if (argc < 6)
  493. lo.optional_data = NULL;
  494. else
  495. lo.optional_data = (const u8 *)argv[6];
  496. size = efi_serialize_load_option(&lo, (u8 **)&data);
  497. if (!size) {
  498. r = CMD_RET_FAILURE;
  499. goto out;
  500. }
  501. ret = EFI_CALL(RT->set_variable(var_name16, &guid,
  502. EFI_VARIABLE_NON_VOLATILE |
  503. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  504. EFI_VARIABLE_RUNTIME_ACCESS,
  505. size, data));
  506. if (ret != EFI_SUCCESS) {
  507. printf("Cannot set %ls\n", var_name16);
  508. r = CMD_RET_FAILURE;
  509. }
  510. out:
  511. free(data);
  512. efi_free_pool(device_path);
  513. efi_free_pool(file_path);
  514. free(lo.label);
  515. return r;
  516. }
  517. /**
  518. * do_efi_boot_rm() - delete UEFI load options
  519. *
  520. * @cmdtp: Command table
  521. * @flag: Command flag
  522. * @argc: Number of arguments
  523. * @argv: Argument array
  524. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  525. *
  526. * Implement efidebug "boot rm" sub-command.
  527. * Delete UEFI load options.
  528. *
  529. * efidebug boot rm <id> ...
  530. */
  531. static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
  532. int argc, char * const argv[])
  533. {
  534. efi_guid_t guid;
  535. int id, i;
  536. char *endp;
  537. char var_name[9];
  538. u16 var_name16[9];
  539. efi_status_t ret;
  540. if (argc == 1)
  541. return CMD_RET_USAGE;
  542. guid = efi_global_variable_guid;
  543. for (i = 1; i < argc; i++, argv++) {
  544. id = (int)simple_strtoul(argv[1], &endp, 16);
  545. if (*endp != '\0' || id > 0xffff)
  546. return CMD_RET_FAILURE;
  547. sprintf(var_name, "Boot%04X", id);
  548. utf8_utf16_strncpy((u16 **)&var_name16, var_name, 9);
  549. ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL));
  550. if (ret) {
  551. printf("Cannot remove Boot%04X", id);
  552. return CMD_RET_FAILURE;
  553. }
  554. }
  555. return CMD_RET_SUCCESS;
  556. }
  557. /**
  558. * show_efi_boot_opt_data() - dump UEFI load option
  559. *
  560. * @id: load option number
  561. * @data: value of UEFI load option variable
  562. * @size: size of the boot option
  563. *
  564. * Decode the value of UEFI load option variable and print information.
  565. */
  566. static void show_efi_boot_opt_data(int id, void *data, size_t size)
  567. {
  568. struct efi_load_option lo;
  569. char *label, *p;
  570. size_t label_len16, label_len;
  571. u16 *dp_str;
  572. efi_deserialize_load_option(&lo, data);
  573. label_len16 = u16_strlen(lo.label);
  574. label_len = utf16_utf8_strnlen(lo.label, label_len16);
  575. label = malloc(label_len + 1);
  576. if (!label)
  577. return;
  578. p = label;
  579. utf16_utf8_strncpy(&p, lo.label, label_len16);
  580. printf("Boot%04X:\n", id);
  581. printf(" attributes: %c%c%c (0x%08x)\n",
  582. /* ACTIVE */
  583. lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
  584. /* FORCE RECONNECT */
  585. lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
  586. /* HIDDEN */
  587. lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
  588. lo.attributes);
  589. printf(" label: %s\n", label);
  590. dp_str = efi_dp_str(lo.file_path);
  591. printf(" file_path: %ls\n", dp_str);
  592. efi_free_pool(dp_str);
  593. printf(" data:\n");
  594. print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
  595. lo.optional_data, size + (u8 *)data -
  596. (u8 *)lo.optional_data, true);
  597. free(label);
  598. }
  599. /**
  600. * show_efi_boot_opt() - dump UEFI load option
  601. *
  602. * @id: Load option number
  603. *
  604. * Dump information defined by UEFI load option.
  605. */
  606. static void show_efi_boot_opt(int id)
  607. {
  608. char var_name[9];
  609. u16 var_name16[9], *p;
  610. efi_guid_t guid;
  611. void *data = NULL;
  612. efi_uintn_t size;
  613. efi_status_t ret;
  614. sprintf(var_name, "Boot%04X", id);
  615. p = var_name16;
  616. utf8_utf16_strncpy(&p, var_name, 9);
  617. guid = efi_global_variable_guid;
  618. size = 0;
  619. ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
  620. if (ret == EFI_BUFFER_TOO_SMALL) {
  621. data = malloc(size);
  622. ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
  623. data));
  624. }
  625. if (ret == EFI_SUCCESS)
  626. show_efi_boot_opt_data(id, data, size);
  627. else if (ret == EFI_NOT_FOUND)
  628. printf("Boot%04X: not found\n", id);
  629. free(data);
  630. }
  631. static int u16_tohex(u16 c)
  632. {
  633. if (c >= '0' && c <= '9')
  634. return c - '0';
  635. if (c >= 'A' && c <= 'F')
  636. return c - 'A' + 10;
  637. /* not hexadecimal */
  638. return -1;
  639. }
  640. /**
  641. * show_efi_boot_dump() - dump all UEFI load options
  642. *
  643. * @cmdtp: Command table
  644. * @flag: Command flag
  645. * @argc: Number of arguments
  646. * @argv: Argument array
  647. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  648. *
  649. * Implement efidebug "boot dump" sub-command.
  650. * Dump information of all UEFI load options defined.
  651. *
  652. * efidebug boot dump
  653. */
  654. static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag,
  655. int argc, char * const argv[])
  656. {
  657. u16 *var_name16, *p;
  658. efi_uintn_t buf_size, size;
  659. efi_guid_t guid;
  660. int id, i, digit;
  661. efi_status_t ret;
  662. if (argc > 1)
  663. return CMD_RET_USAGE;
  664. buf_size = 128;
  665. var_name16 = malloc(buf_size);
  666. if (!var_name16)
  667. return CMD_RET_FAILURE;
  668. var_name16[0] = 0;
  669. for (;;) {
  670. size = buf_size;
  671. ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
  672. &guid));
  673. if (ret == EFI_NOT_FOUND)
  674. break;
  675. if (ret == EFI_BUFFER_TOO_SMALL) {
  676. buf_size = size;
  677. p = realloc(var_name16, buf_size);
  678. if (!p) {
  679. free(var_name16);
  680. return CMD_RET_FAILURE;
  681. }
  682. var_name16 = p;
  683. ret = EFI_CALL(efi_get_next_variable_name(&size,
  684. var_name16,
  685. &guid));
  686. }
  687. if (ret != EFI_SUCCESS) {
  688. free(var_name16);
  689. return CMD_RET_FAILURE;
  690. }
  691. if (memcmp(var_name16, L"Boot", 8))
  692. continue;
  693. for (id = 0, i = 0; i < 4; i++) {
  694. digit = u16_tohex(var_name16[4 + i]);
  695. if (digit < 0)
  696. break;
  697. id = (id << 4) + digit;
  698. }
  699. if (i == 4 && !var_name16[8])
  700. show_efi_boot_opt(id);
  701. }
  702. free(var_name16);
  703. return CMD_RET_SUCCESS;
  704. }
  705. /**
  706. * show_efi_boot_order() - show order of UEFI load options
  707. *
  708. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  709. *
  710. * Show order of UEFI load options defined by BootOrder variable.
  711. */
  712. static int show_efi_boot_order(void)
  713. {
  714. efi_guid_t guid;
  715. u16 *bootorder = NULL;
  716. efi_uintn_t size;
  717. int num, i;
  718. char var_name[9];
  719. u16 var_name16[9], *p16;
  720. void *data;
  721. struct efi_load_option lo;
  722. char *label, *p;
  723. size_t label_len16, label_len;
  724. efi_status_t ret;
  725. guid = efi_global_variable_guid;
  726. size = 0;
  727. ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, &size,
  728. NULL));
  729. if (ret == EFI_BUFFER_TOO_SMALL) {
  730. bootorder = malloc(size);
  731. ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL,
  732. &size, bootorder));
  733. }
  734. if (ret == EFI_NOT_FOUND) {
  735. printf("BootOrder not defined\n");
  736. ret = CMD_RET_SUCCESS;
  737. goto out;
  738. } else if (ret != EFI_SUCCESS) {
  739. ret = CMD_RET_FAILURE;
  740. goto out;
  741. }
  742. num = size / sizeof(u16);
  743. for (i = 0; i < num; i++) {
  744. sprintf(var_name, "Boot%04X", bootorder[i]);
  745. p16 = var_name16;
  746. utf8_utf16_strncpy(&p16, var_name, 9);
  747. size = 0;
  748. ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
  749. NULL));
  750. if (ret != EFI_BUFFER_TOO_SMALL) {
  751. printf("%2d: Boot%04X: (not defined)\n",
  752. i + 1, bootorder[i]);
  753. continue;
  754. }
  755. data = malloc(size);
  756. if (!data) {
  757. ret = CMD_RET_FAILURE;
  758. goto out;
  759. }
  760. ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
  761. data));
  762. if (ret != EFI_SUCCESS) {
  763. free(data);
  764. ret = CMD_RET_FAILURE;
  765. goto out;
  766. }
  767. efi_deserialize_load_option(&lo, data);
  768. label_len16 = u16_strlen(lo.label);
  769. label_len = utf16_utf8_strnlen(lo.label, label_len16);
  770. label = malloc(label_len + 1);
  771. if (!label) {
  772. free(data);
  773. ret = CMD_RET_FAILURE;
  774. goto out;
  775. }
  776. p = label;
  777. utf16_utf8_strncpy(&p, lo.label, label_len16);
  778. printf("%2d: Boot%04X: %s\n", i + 1, bootorder[i], label);
  779. free(label);
  780. free(data);
  781. }
  782. out:
  783. free(bootorder);
  784. return ret;
  785. }
  786. /**
  787. * do_efi_boot_next() - manage UEFI BootNext variable
  788. *
  789. * @cmdtp: Command table
  790. * @flag: Command flag
  791. * @argc: Number of arguments
  792. * @argv: Argument array
  793. * Return: CMD_RET_SUCCESS on success,
  794. * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  795. *
  796. * Implement efidebug "boot next" sub-command.
  797. * Set BootNext variable.
  798. *
  799. * efidebug boot next <id>
  800. */
  801. static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
  802. int argc, char * const argv[])
  803. {
  804. u16 bootnext;
  805. efi_uintn_t size;
  806. char *endp;
  807. efi_guid_t guid;
  808. efi_status_t ret;
  809. int r = CMD_RET_SUCCESS;
  810. if (argc != 2)
  811. return CMD_RET_USAGE;
  812. bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
  813. if (*endp != '\0' || bootnext > 0xffff) {
  814. printf("invalid value: %s\n", argv[1]);
  815. r = CMD_RET_FAILURE;
  816. goto out;
  817. }
  818. guid = efi_global_variable_guid;
  819. size = sizeof(u16);
  820. ret = EFI_CALL(RT->set_variable(L"BootNext", &guid,
  821. EFI_VARIABLE_NON_VOLATILE |
  822. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  823. EFI_VARIABLE_RUNTIME_ACCESS,
  824. size, &bootnext));
  825. if (ret != EFI_SUCCESS) {
  826. printf("Cannot set BootNext\n");
  827. r = CMD_RET_FAILURE;
  828. }
  829. out:
  830. return r;
  831. }
  832. /**
  833. * do_efi_boot_order() - manage UEFI BootOrder variable
  834. *
  835. * @cmdtp: Command table
  836. * @flag: Command flag
  837. * @argc: Number of arguments
  838. * @argv: Argument array
  839. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  840. *
  841. * Implement efidebug "boot order" sub-command.
  842. * Show order of UEFI load options, or change it in BootOrder variable.
  843. *
  844. * efidebug boot order [<id> ...]
  845. */
  846. static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
  847. int argc, char * const argv[])
  848. {
  849. u16 *bootorder = NULL;
  850. efi_uintn_t size;
  851. int id, i;
  852. char *endp;
  853. efi_guid_t guid;
  854. efi_status_t ret;
  855. int r = CMD_RET_SUCCESS;
  856. if (argc == 1)
  857. return show_efi_boot_order();
  858. argc--;
  859. argv++;
  860. size = argc * sizeof(u16);
  861. bootorder = malloc(size);
  862. if (!bootorder)
  863. return CMD_RET_FAILURE;
  864. for (i = 0; i < argc; i++) {
  865. id = (int)simple_strtoul(argv[i], &endp, 16);
  866. if (*endp != '\0' || id > 0xffff) {
  867. printf("invalid value: %s\n", argv[i]);
  868. r = CMD_RET_FAILURE;
  869. goto out;
  870. }
  871. bootorder[i] = (u16)id;
  872. }
  873. guid = efi_global_variable_guid;
  874. ret = EFI_CALL(RT->set_variable(L"BootOrder", &guid,
  875. EFI_VARIABLE_NON_VOLATILE |
  876. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  877. EFI_VARIABLE_RUNTIME_ACCESS,
  878. size, bootorder));
  879. if (ret != EFI_SUCCESS) {
  880. printf("Cannot set BootOrder\n");
  881. r = CMD_RET_FAILURE;
  882. }
  883. out:
  884. free(bootorder);
  885. return r;
  886. }
  887. static cmd_tbl_t cmd_efidebug_boot_sub[] = {
  888. U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
  889. U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
  890. U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
  891. U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
  892. U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
  893. "", ""),
  894. };
  895. /**
  896. * do_efi_boot_opt() - manage UEFI load options
  897. *
  898. * @cmdtp: Command table
  899. * @flag: Command flag
  900. * @argc: Number of arguments
  901. * @argv: Argument array
  902. * Return: CMD_RET_SUCCESS on success,
  903. * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  904. *
  905. * Implement efidebug "boot" sub-command.
  906. */
  907. static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag,
  908. int argc, char * const argv[])
  909. {
  910. cmd_tbl_t *cp;
  911. if (argc < 2)
  912. return CMD_RET_USAGE;
  913. argc--; argv++;
  914. cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
  915. ARRAY_SIZE(cmd_efidebug_boot_sub));
  916. if (!cp)
  917. return CMD_RET_USAGE;
  918. return cp->cmd(cmdtp, flag, argc, argv);
  919. }
  920. static cmd_tbl_t cmd_efidebug_sub[] = {
  921. U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
  922. U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
  923. "", ""),
  924. U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
  925. "", ""),
  926. U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
  927. "", ""),
  928. U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
  929. "", ""),
  930. U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
  931. "", ""),
  932. };
  933. /**
  934. * do_efidebug() - display and configure UEFI environment
  935. *
  936. * @cmdtp: Command table
  937. * @flag: Command flag
  938. * @argc: Number of arguments
  939. * @argv: Argument array
  940. * Return: CMD_RET_SUCCESS on success,
  941. * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  942. *
  943. * Implement efidebug command which allows us to display and
  944. * configure UEFI environment.
  945. */
  946. static int do_efidebug(cmd_tbl_t *cmdtp, int flag,
  947. int argc, char * const argv[])
  948. {
  949. cmd_tbl_t *cp;
  950. efi_status_t r;
  951. if (argc < 2)
  952. return CMD_RET_USAGE;
  953. argc--; argv++;
  954. /* Initialize UEFI drivers */
  955. r = efi_init_obj_list();
  956. if (r != EFI_SUCCESS) {
  957. printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
  958. r & ~EFI_ERROR_MASK);
  959. return CMD_RET_FAILURE;
  960. }
  961. cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
  962. ARRAY_SIZE(cmd_efidebug_sub));
  963. if (!cp)
  964. return CMD_RET_USAGE;
  965. return cp->cmd(cmdtp, flag, argc, argv);
  966. }
  967. #ifdef CONFIG_SYS_LONGHELP
  968. static char efidebug_help_text[] =
  969. " - UEFI Shell-like interface to configure UEFI environment\n"
  970. "\n"
  971. "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
  972. " - set UEFI BootXXXX variable\n"
  973. " <load options> will be passed to UEFI application\n"
  974. "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
  975. " - delete UEFI BootXXXX variables\n"
  976. "efidebug boot dump\n"
  977. " - dump all UEFI BootXXXX variables\n"
  978. "efidebug boot next <bootid>\n"
  979. " - set UEFI BootNext variable\n"
  980. "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
  981. " - set/show UEFI boot order\n"
  982. "\n"
  983. "efidebug devices\n"
  984. " - show uefi devices\n"
  985. "efidebug drivers\n"
  986. " - show uefi drivers\n"
  987. "efidebug dh\n"
  988. " - show uefi handles\n"
  989. "efidebug images\n"
  990. " - show loaded images\n"
  991. "efidebug memmap\n"
  992. " - show uefi memory map\n";
  993. #endif
  994. U_BOOT_CMD(
  995. efidebug, 10, 0, do_efidebug,
  996. "Configure UEFI environment",
  997. efidebug_help_text
  998. );