efidebug.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  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_dt_fixup.h>
  11. #include <efi_load_initrd.h>
  12. #include <efi_loader.h>
  13. #include <efi_rng.h>
  14. #include <exports.h>
  15. #include <hexdump.h>
  16. #include <log.h>
  17. #include <malloc.h>
  18. #include <mapmem.h>
  19. #include <part.h>
  20. #include <search.h>
  21. #include <linux/ctype.h>
  22. #include <linux/err.h>
  23. #define BS systab.boottime
  24. #define RT systab.runtime
  25. #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
  26. /**
  27. * do_efi_capsule_update() - process a capsule update
  28. *
  29. * @cmdtp: Command table
  30. * @flag: Command flag
  31. * @argc: Number of arguments
  32. * @argv: Argument array
  33. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  34. *
  35. * Implement efidebug "capsule update" sub-command.
  36. * process a capsule update.
  37. *
  38. * efidebug capsule update [-v] <capsule address>
  39. */
  40. static int do_efi_capsule_update(struct cmd_tbl *cmdtp, int flag,
  41. int argc, char * const argv[])
  42. {
  43. struct efi_capsule_header *capsule;
  44. int verbose = 0;
  45. char *endp;
  46. efi_status_t ret;
  47. if (argc != 2 && argc != 3)
  48. return CMD_RET_USAGE;
  49. if (argc == 3) {
  50. if (strcmp(argv[1], "-v"))
  51. return CMD_RET_USAGE;
  52. verbose = 1;
  53. argc--;
  54. argv++;
  55. }
  56. capsule = (typeof(capsule))simple_strtoul(argv[1], &endp, 16);
  57. if (endp == argv[1]) {
  58. printf("Invalid address: %s", argv[1]);
  59. return CMD_RET_FAILURE;
  60. }
  61. if (verbose) {
  62. printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
  63. printf("Capsule flags: 0x%x\n", capsule->flags);
  64. printf("Capsule header size: 0x%x\n", capsule->header_size);
  65. printf("Capsule image size: 0x%x\n",
  66. capsule->capsule_image_size);
  67. }
  68. ret = EFI_CALL(RT->update_capsule(&capsule, 1, 0));
  69. if (ret) {
  70. printf("Cannot handle a capsule at %p", capsule);
  71. return CMD_RET_FAILURE;
  72. }
  73. return CMD_RET_SUCCESS;
  74. }
  75. static int do_efi_capsule_on_disk_update(struct cmd_tbl *cmdtp, int flag,
  76. int argc, char * const argv[])
  77. {
  78. efi_status_t ret;
  79. ret = efi_launch_capsules();
  80. return ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
  81. }
  82. /**
  83. * do_efi_capsule_show() - show capsule information
  84. *
  85. * @cmdtp: Command table
  86. * @flag: Command flag
  87. * @argc: Number of arguments
  88. * @argv: Argument array
  89. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  90. *
  91. * Implement efidebug "capsule show" sub-command.
  92. * show capsule information.
  93. *
  94. * efidebug capsule show <capsule address>
  95. */
  96. static int do_efi_capsule_show(struct cmd_tbl *cmdtp, int flag,
  97. int argc, char * const argv[])
  98. {
  99. struct efi_capsule_header *capsule;
  100. char *endp;
  101. if (argc != 2)
  102. return CMD_RET_USAGE;
  103. capsule = (typeof(capsule))simple_strtoul(argv[1], &endp, 16);
  104. if (endp == argv[1]) {
  105. printf("Invalid address: %s", argv[1]);
  106. return CMD_RET_FAILURE;
  107. }
  108. printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
  109. printf("Capsule flags: 0x%x\n", capsule->flags);
  110. printf("Capsule header size: 0x%x\n", capsule->header_size);
  111. printf("Capsule image size: 0x%x\n",
  112. capsule->capsule_image_size);
  113. return CMD_RET_SUCCESS;
  114. }
  115. #ifdef CONFIG_EFI_ESRT
  116. #define EFI_ESRT_FW_TYPE_NUM 4
  117. char *efi_fw_type_str[EFI_ESRT_FW_TYPE_NUM] = {"unknown", "system FW", "device FW",
  118. "UEFI driver"};
  119. #define EFI_ESRT_UPDATE_STATUS_NUM 9
  120. char *efi_update_status_str[EFI_ESRT_UPDATE_STATUS_NUM] = {"success", "unsuccessful",
  121. "insufficient resources", "incorrect version", "invalid format",
  122. "auth error", "power event (AC)", "power event (batt)",
  123. "unsatisfied dependencies"};
  124. #define EFI_FW_TYPE_STR_GET(idx) (\
  125. EFI_ESRT_FW_TYPE_NUM > (idx) ? efi_fw_type_str[(idx)] : "error"\
  126. )
  127. #define EFI_FW_STATUS_STR_GET(idx) (\
  128. EFI_ESRT_UPDATE_STATUS_NUM > (idx) ? efi_update_status_str[(idx)] : "error"\
  129. )
  130. /**
  131. * do_efi_capsule_esrt() - manage UEFI capsules
  132. *
  133. * @cmdtp: Command table
  134. * @flag: Command flag
  135. * @argc: Number of arguments
  136. * @argv: Argument array
  137. * Return: CMD_RET_SUCCESS on success,
  138. * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  139. *
  140. * Implement efidebug "capsule esrt" sub-command.
  141. * The prints the current ESRT table.
  142. *
  143. * efidebug capsule esrt
  144. */
  145. static int do_efi_capsule_esrt(struct cmd_tbl *cmdtp, int flag,
  146. int argc, char * const argv[])
  147. {
  148. struct efi_system_resource_table *esrt = NULL;
  149. if (argc != 1)
  150. return CMD_RET_USAGE;
  151. for (int idx = 0; idx < systab.nr_tables; idx++)
  152. if (!guidcmp(&efi_esrt_guid, &systab.tables[idx].guid))
  153. esrt = (struct efi_system_resource_table *)systab.tables[idx].table;
  154. if (!esrt) {
  155. log_info("ESRT: table not present\n");
  156. return CMD_RET_SUCCESS;
  157. }
  158. printf("========================================\n");
  159. printf("ESRT: fw_resource_count=%d\n", esrt->fw_resource_count);
  160. printf("ESRT: fw_resource_count_max=%d\n", esrt->fw_resource_count_max);
  161. printf("ESRT: fw_resource_version=%lld\n", esrt->fw_resource_version);
  162. for (int idx = 0; idx < esrt->fw_resource_count; idx++) {
  163. printf("[entry %d]==============================\n", idx);
  164. printf("ESRT: fw_class=%pUL\n", &esrt->entries[idx].fw_class);
  165. printf("ESRT: fw_type=%s\n", EFI_FW_TYPE_STR_GET(esrt->entries[idx].fw_type));
  166. printf("ESRT: fw_version=%d\n", esrt->entries[idx].fw_version);
  167. printf("ESRT: lowest_supported_fw_version=%d\n",
  168. esrt->entries[idx].lowest_supported_fw_version);
  169. printf("ESRT: capsule_flags=%d\n",
  170. esrt->entries[idx].capsule_flags);
  171. printf("ESRT: last_attempt_version=%d\n",
  172. esrt->entries[idx].last_attempt_version);
  173. printf("ESRT: last_attempt_status=%s\n",
  174. EFI_FW_STATUS_STR_GET(esrt->entries[idx].last_attempt_status));
  175. }
  176. printf("========================================\n");
  177. return CMD_RET_SUCCESS;
  178. }
  179. #endif /* CONFIG_EFI_ESRT */
  180. /**
  181. * do_efi_capsule_res() - show a capsule update result
  182. *
  183. * @cmdtp: Command table
  184. * @flag: Command flag
  185. * @argc: Number of arguments
  186. * @argv: Argument array
  187. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  188. *
  189. * Implement efidebug "capsule result" sub-command.
  190. * show a capsule update result.
  191. * If result number is not specified, CapsuleLast will be shown.
  192. *
  193. * efidebug capsule result [<capsule result number>]
  194. */
  195. static int do_efi_capsule_res(struct cmd_tbl *cmdtp, int flag,
  196. int argc, char * const argv[])
  197. {
  198. int capsule_id;
  199. char *endp;
  200. char var_name[12];
  201. u16 var_name16[12], *p;
  202. efi_guid_t guid;
  203. struct efi_capsule_result_variable_header *result = NULL;
  204. efi_uintn_t size;
  205. efi_status_t ret;
  206. if (argc != 1 && argc != 2)
  207. return CMD_RET_USAGE;
  208. guid = efi_guid_capsule_report;
  209. if (argc == 1) {
  210. size = sizeof(var_name16);
  211. ret = EFI_CALL(RT->get_variable(L"CapsuleLast", &guid, NULL,
  212. &size, var_name16));
  213. if (ret != EFI_SUCCESS) {
  214. if (ret == EFI_NOT_FOUND)
  215. printf("CapsuleLast doesn't exist\n");
  216. else
  217. printf("Failed to get CapsuleLast\n");
  218. return CMD_RET_FAILURE;
  219. }
  220. printf("CapsuleLast is %ls\n", var_name16);
  221. } else {
  222. argc--;
  223. argv++;
  224. capsule_id = simple_strtoul(argv[0], &endp, 16);
  225. if (capsule_id < 0 || capsule_id > 0xffff)
  226. return CMD_RET_USAGE;
  227. sprintf(var_name, "Capsule%04X", capsule_id);
  228. p = var_name16;
  229. utf8_utf16_strncpy(&p, var_name, 9);
  230. }
  231. size = 0;
  232. ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
  233. if (ret == EFI_BUFFER_TOO_SMALL) {
  234. result = malloc(size);
  235. if (!result)
  236. return CMD_RET_FAILURE;
  237. ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
  238. result));
  239. }
  240. if (ret != EFI_SUCCESS) {
  241. free(result);
  242. printf("Failed to get %ls\n", var_name16);
  243. return CMD_RET_FAILURE;
  244. }
  245. printf("Result total size: 0x%x\n", result->variable_total_size);
  246. printf("Capsule guid: %pUl\n", &result->capsule_guid);
  247. printf("Time processed: %04d-%02d-%02d %02d:%02d:%02d\n",
  248. result->capsule_processed.year, result->capsule_processed.month,
  249. result->capsule_processed.day, result->capsule_processed.hour,
  250. result->capsule_processed.minute,
  251. result->capsule_processed.second);
  252. printf("Capsule status: 0x%lx\n", result->capsule_status);
  253. free(result);
  254. return CMD_RET_SUCCESS;
  255. }
  256. static struct cmd_tbl cmd_efidebug_capsule_sub[] = {
  257. U_BOOT_CMD_MKENT(update, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_update,
  258. "", ""),
  259. U_BOOT_CMD_MKENT(show, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_show,
  260. "", ""),
  261. #ifdef CONFIG_EFI_ESRT
  262. U_BOOT_CMD_MKENT(esrt, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_esrt,
  263. "", ""),
  264. #endif
  265. U_BOOT_CMD_MKENT(disk-update, 0, 0, do_efi_capsule_on_disk_update,
  266. "", ""),
  267. U_BOOT_CMD_MKENT(result, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_res,
  268. "", ""),
  269. };
  270. /**
  271. * do_efi_capsule() - manage UEFI capsules
  272. *
  273. * @cmdtp: Command table
  274. * @flag: Command flag
  275. * @argc: Number of arguments
  276. * @argv: Argument array
  277. * Return: CMD_RET_SUCCESS on success,
  278. * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  279. *
  280. * Implement efidebug "capsule" sub-command.
  281. */
  282. static int do_efi_capsule(struct cmd_tbl *cmdtp, int flag,
  283. int argc, char * const argv[])
  284. {
  285. struct cmd_tbl *cp;
  286. if (argc < 2)
  287. return CMD_RET_USAGE;
  288. argc--; argv++;
  289. cp = find_cmd_tbl(argv[0], cmd_efidebug_capsule_sub,
  290. ARRAY_SIZE(cmd_efidebug_capsule_sub));
  291. if (!cp)
  292. return CMD_RET_USAGE;
  293. return cp->cmd(cmdtp, flag, argc, argv);
  294. }
  295. #endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT */
  296. /**
  297. * efi_get_device_path_text() - get device path text
  298. *
  299. * Return the text representation of the device path of a handle.
  300. *
  301. * @handle: handle of UEFI device
  302. * Return:
  303. * Pointer to the device path text or NULL.
  304. * The caller is responsible for calling FreePool().
  305. */
  306. static u16 *efi_get_device_path_text(efi_handle_t handle)
  307. {
  308. struct efi_handler *handler;
  309. efi_status_t ret;
  310. ret = efi_search_protocol(handle, &efi_guid_device_path, &handler);
  311. if (ret == EFI_SUCCESS && handler->protocol_interface) {
  312. struct efi_device_path *dp = handler->protocol_interface;
  313. return efi_dp_str(dp);
  314. } else {
  315. return NULL;
  316. }
  317. }
  318. #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
  319. static const char spc[] = " ";
  320. static const char sep[] = "================";
  321. /**
  322. * do_efi_show_devices() - show UEFI devices
  323. *
  324. * @cmdtp: Command table
  325. * @flag: Command flag
  326. * @argc: Number of arguments
  327. * @argv: Argument array
  328. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  329. *
  330. * Implement efidebug "devices" sub-command.
  331. * Show all UEFI devices and their information.
  332. */
  333. static int do_efi_show_devices(struct cmd_tbl *cmdtp, int flag,
  334. int argc, char *const argv[])
  335. {
  336. efi_handle_t *handles;
  337. efi_uintn_t num, i;
  338. u16 *dev_path_text;
  339. efi_status_t ret;
  340. ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
  341. &num, &handles));
  342. if (ret != EFI_SUCCESS)
  343. return CMD_RET_FAILURE;
  344. if (!num)
  345. return CMD_RET_SUCCESS;
  346. printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc);
  347. printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
  348. for (i = 0; i < num; i++) {
  349. dev_path_text = efi_get_device_path_text(handles[i]);
  350. if (dev_path_text) {
  351. printf("%p %ls\n", handles[i], dev_path_text);
  352. efi_free_pool(dev_path_text);
  353. }
  354. }
  355. efi_free_pool(handles);
  356. return CMD_RET_SUCCESS;
  357. }
  358. /**
  359. * efi_get_driver_handle_info() - get information of UEFI driver
  360. *
  361. * @handle: Handle of UEFI device
  362. * @driver_name: Driver name
  363. * @image_path: Pointer to text of device path
  364. * Return: 0 on success, -1 on failure
  365. *
  366. * Currently return no useful information as all UEFI drivers are
  367. * built-in..
  368. */
  369. static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
  370. u16 **image_path)
  371. {
  372. struct efi_handler *handler;
  373. struct efi_loaded_image *image;
  374. efi_status_t ret;
  375. /*
  376. * driver name
  377. * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
  378. */
  379. *driver_name = NULL;
  380. /* image name */
  381. ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
  382. if (ret != EFI_SUCCESS) {
  383. *image_path = NULL;
  384. return 0;
  385. }
  386. image = handler->protocol_interface;
  387. *image_path = efi_dp_str(image->file_path);
  388. return 0;
  389. }
  390. /**
  391. * do_efi_show_drivers() - show UEFI drivers
  392. *
  393. * @cmdtp: Command table
  394. * @flag: Command flag
  395. * @argc: Number of arguments
  396. * @argv: Argument array
  397. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  398. *
  399. * Implement efidebug "drivers" sub-command.
  400. * Show all UEFI drivers and their information.
  401. */
  402. static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
  403. int argc, char *const argv[])
  404. {
  405. efi_handle_t *handles;
  406. efi_uintn_t num, i;
  407. u16 *driver_name, *image_path_text;
  408. efi_status_t ret;
  409. ret = EFI_CALL(efi_locate_handle_buffer(
  410. BY_PROTOCOL, &efi_guid_driver_binding_protocol,
  411. NULL, &num, &handles));
  412. if (ret != EFI_SUCCESS)
  413. return CMD_RET_FAILURE;
  414. if (!num)
  415. return CMD_RET_SUCCESS;
  416. printf("Driver%.*s Name Image Path\n",
  417. EFI_HANDLE_WIDTH - 6, spc);
  418. printf("%.*s ==================== ====================\n",
  419. EFI_HANDLE_WIDTH, sep);
  420. for (i = 0; i < num; i++) {
  421. if (!efi_get_driver_handle_info(handles[i], &driver_name,
  422. &image_path_text)) {
  423. if (image_path_text)
  424. printf("%p %-20ls %ls\n", handles[i],
  425. driver_name, image_path_text);
  426. else
  427. printf("%p %-20ls <built-in>\n",
  428. handles[i], driver_name);
  429. efi_free_pool(driver_name);
  430. efi_free_pool(image_path_text);
  431. }
  432. }
  433. efi_free_pool(handles);
  434. return CMD_RET_SUCCESS;
  435. }
  436. static const struct {
  437. const char *text;
  438. const efi_guid_t guid;
  439. } guid_list[] = {
  440. {
  441. "Device Path",
  442. EFI_DEVICE_PATH_PROTOCOL_GUID,
  443. },
  444. {
  445. "Device Path To Text",
  446. EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
  447. },
  448. {
  449. "Device Path Utilities",
  450. EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
  451. },
  452. {
  453. "Unicode Collation 2",
  454. EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
  455. },
  456. {
  457. "Driver Binding",
  458. EFI_DRIVER_BINDING_PROTOCOL_GUID,
  459. },
  460. {
  461. "Simple Text Input",
  462. EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
  463. },
  464. {
  465. "Simple Text Input Ex",
  466. EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
  467. },
  468. {
  469. "Simple Text Output",
  470. EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
  471. },
  472. {
  473. "Block IO",
  474. EFI_BLOCK_IO_PROTOCOL_GUID,
  475. },
  476. {
  477. "Simple File System",
  478. EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
  479. },
  480. {
  481. "Loaded Image",
  482. EFI_LOADED_IMAGE_PROTOCOL_GUID,
  483. },
  484. {
  485. "Graphics Output",
  486. EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
  487. },
  488. {
  489. "HII String",
  490. EFI_HII_STRING_PROTOCOL_GUID,
  491. },
  492. {
  493. "HII Database",
  494. EFI_HII_DATABASE_PROTOCOL_GUID,
  495. },
  496. {
  497. "HII Config Routing",
  498. EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
  499. },
  500. {
  501. "Load File2",
  502. EFI_LOAD_FILE2_PROTOCOL_GUID,
  503. },
  504. {
  505. "Random Number Generator",
  506. EFI_RNG_PROTOCOL_GUID,
  507. },
  508. {
  509. "Simple Network",
  510. EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
  511. },
  512. {
  513. "PXE Base Code",
  514. EFI_PXE_BASE_CODE_PROTOCOL_GUID,
  515. },
  516. {
  517. "Device-Tree Fixup",
  518. EFI_DT_FIXUP_PROTOCOL_GUID,
  519. },
  520. {
  521. "System Partition",
  522. PARTITION_SYSTEM_GUID
  523. },
  524. {
  525. "Firmware Management",
  526. EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
  527. },
  528. /* Configuration table GUIDs */
  529. {
  530. "ACPI table",
  531. EFI_ACPI_TABLE_GUID,
  532. },
  533. {
  534. "EFI System Resource Table",
  535. EFI_SYSTEM_RESOURCE_TABLE_GUID,
  536. },
  537. {
  538. "device tree",
  539. EFI_FDT_GUID,
  540. },
  541. {
  542. "SMBIOS table",
  543. SMBIOS_TABLE_GUID,
  544. },
  545. {
  546. "Runtime properties",
  547. EFI_RT_PROPERTIES_TABLE_GUID,
  548. },
  549. {
  550. "TCG2 Final Events Table",
  551. EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
  552. },
  553. };
  554. /**
  555. * get_guid_text - get string of GUID
  556. *
  557. * Return description of GUID.
  558. *
  559. * @guid: GUID
  560. * Return: description of GUID or NULL
  561. */
  562. static const char *get_guid_text(const void *guid)
  563. {
  564. int i;
  565. for (i = 0; i < ARRAY_SIZE(guid_list); i++) {
  566. /*
  567. * As guidcmp uses memcmp() we can safely accept unaligned
  568. * GUIDs.
  569. */
  570. if (!guidcmp(&guid_list[i].guid, guid))
  571. return guid_list[i].text;
  572. }
  573. return NULL;
  574. }
  575. /**
  576. * do_efi_show_handles() - show UEFI handles
  577. *
  578. * @cmdtp: Command table
  579. * @flag: Command flag
  580. * @argc: Number of arguments
  581. * @argv: Argument array
  582. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  583. *
  584. * Implement efidebug "dh" sub-command.
  585. * Show all UEFI handles and their information, currently all protocols
  586. * added to handle.
  587. */
  588. static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
  589. int argc, char *const argv[])
  590. {
  591. efi_handle_t *handles;
  592. efi_guid_t **guid;
  593. efi_uintn_t num, count, i, j;
  594. const char *guid_text;
  595. efi_status_t ret;
  596. ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
  597. &num, &handles));
  598. if (ret != EFI_SUCCESS)
  599. return CMD_RET_FAILURE;
  600. if (!num)
  601. return CMD_RET_SUCCESS;
  602. printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
  603. printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
  604. for (i = 0; i < num; i++) {
  605. printf("%p", handles[i]);
  606. ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
  607. &count));
  608. if (ret || !count) {
  609. putc('\n');
  610. continue;
  611. }
  612. for (j = 0; j < count; j++) {
  613. if (j)
  614. printf(", ");
  615. else
  616. putc(' ');
  617. guid_text = get_guid_text(guid[j]);
  618. if (guid_text)
  619. puts(guid_text);
  620. else
  621. printf("%pUl", guid[j]);
  622. }
  623. putc('\n');
  624. }
  625. efi_free_pool(handles);
  626. return CMD_RET_SUCCESS;
  627. }
  628. /**
  629. * do_efi_show_images() - show UEFI images
  630. *
  631. * @cmdtp: Command table
  632. * @flag: Command flag
  633. * @argc: Number of arguments
  634. * @argv: Argument array
  635. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  636. *
  637. * Implement efidebug "images" sub-command.
  638. * Show all UEFI loaded images and their information.
  639. */
  640. static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
  641. int argc, char *const argv[])
  642. {
  643. efi_print_image_infos(NULL);
  644. return CMD_RET_SUCCESS;
  645. }
  646. static const char * const efi_mem_type_string[] = {
  647. [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
  648. [EFI_LOADER_CODE] = "LOADER CODE",
  649. [EFI_LOADER_DATA] = "LOADER DATA",
  650. [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
  651. [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
  652. [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
  653. [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
  654. [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
  655. [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
  656. [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
  657. [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
  658. [EFI_MMAP_IO] = "IO",
  659. [EFI_MMAP_IO_PORT] = "IO PORT",
  660. [EFI_PAL_CODE] = "PAL",
  661. [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
  662. };
  663. static const struct efi_mem_attrs {
  664. const u64 bit;
  665. const char *text;
  666. } efi_mem_attrs[] = {
  667. {EFI_MEMORY_UC, "UC"},
  668. {EFI_MEMORY_UC, "UC"},
  669. {EFI_MEMORY_WC, "WC"},
  670. {EFI_MEMORY_WT, "WT"},
  671. {EFI_MEMORY_WB, "WB"},
  672. {EFI_MEMORY_UCE, "UCE"},
  673. {EFI_MEMORY_WP, "WP"},
  674. {EFI_MEMORY_RP, "RP"},
  675. {EFI_MEMORY_XP, "WP"},
  676. {EFI_MEMORY_NV, "NV"},
  677. {EFI_MEMORY_MORE_RELIABLE, "REL"},
  678. {EFI_MEMORY_RO, "RO"},
  679. {EFI_MEMORY_SP, "SP"},
  680. {EFI_MEMORY_RUNTIME, "RT"},
  681. };
  682. /**
  683. * print_memory_attributes() - print memory map attributes
  684. *
  685. * @attributes: Attribute value
  686. *
  687. * Print memory map attributes
  688. */
  689. static void print_memory_attributes(u64 attributes)
  690. {
  691. int sep, i;
  692. for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
  693. if (attributes & efi_mem_attrs[i].bit) {
  694. if (sep) {
  695. putc('|');
  696. } else {
  697. putc(' ');
  698. sep = 1;
  699. }
  700. puts(efi_mem_attrs[i].text);
  701. }
  702. }
  703. #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
  704. /**
  705. * do_efi_show_memmap() - show UEFI memory map
  706. *
  707. * @cmdtp: Command table
  708. * @flag: Command flag
  709. * @argc: Number of arguments
  710. * @argv: Argument array
  711. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  712. *
  713. * Implement efidebug "memmap" sub-command.
  714. * Show UEFI memory map.
  715. */
  716. static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
  717. int argc, char *const argv[])
  718. {
  719. struct efi_mem_desc *memmap = NULL, *map;
  720. efi_uintn_t map_size = 0;
  721. const char *type;
  722. int i;
  723. efi_status_t ret;
  724. ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
  725. if (ret == EFI_BUFFER_TOO_SMALL) {
  726. map_size += sizeof(struct efi_mem_desc); /* for my own */
  727. ret = efi_allocate_pool(EFI_LOADER_DATA, map_size,
  728. (void *)&memmap);
  729. if (ret != EFI_SUCCESS)
  730. return CMD_RET_FAILURE;
  731. ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
  732. }
  733. if (ret != EFI_SUCCESS) {
  734. efi_free_pool(memmap);
  735. return CMD_RET_FAILURE;
  736. }
  737. printf("Type Start%.*s End%.*s Attributes\n",
  738. EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
  739. printf("================ %.*s %.*s ==========\n",
  740. EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
  741. /*
  742. * Coverity check: dereferencing null pointer "map."
  743. * This is a false positive as memmap will always be
  744. * populated by allocate_pool() above.
  745. */
  746. for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
  747. if (map->type < ARRAY_SIZE(efi_mem_type_string))
  748. type = efi_mem_type_string[map->type];
  749. else
  750. type = "(unknown)";
  751. printf("%-16s %.*llx-%.*llx", type,
  752. EFI_PHYS_ADDR_WIDTH,
  753. (u64)map_to_sysmem((void *)(uintptr_t)
  754. map->physical_start),
  755. EFI_PHYS_ADDR_WIDTH,
  756. (u64)map_to_sysmem((void *)(uintptr_t)
  757. (map->physical_start +
  758. map->num_pages * EFI_PAGE_SIZE)));
  759. print_memory_attributes(map->attribute);
  760. putc('\n');
  761. }
  762. efi_free_pool(memmap);
  763. return CMD_RET_SUCCESS;
  764. }
  765. /**
  766. * do_efi_show_tables() - show UEFI configuration tables
  767. *
  768. * @cmdtp: Command table
  769. * @flag: Command flag
  770. * @argc: Number of arguments
  771. * @argv: Argument array
  772. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  773. *
  774. * Implement efidebug "tables" sub-command.
  775. * Show UEFI configuration tables.
  776. */
  777. static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
  778. int argc, char *const argv[])
  779. {
  780. efi_uintn_t i;
  781. const char *guid_str;
  782. for (i = 0; i < systab.nr_tables; ++i) {
  783. guid_str = get_guid_text(&systab.tables[i].guid);
  784. if (!guid_str)
  785. guid_str = "";
  786. printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
  787. }
  788. return CMD_RET_SUCCESS;
  789. }
  790. /**
  791. * create_initrd_dp() - Create a special device for our Boot### option
  792. *
  793. * @dev: Device
  794. * @part: Disk partition
  795. * @file: Filename
  796. * Return: Pointer to the device path or ERR_PTR
  797. *
  798. */
  799. static
  800. struct efi_device_path *create_initrd_dp(const char *dev, const char *part,
  801. const char *file)
  802. {
  803. struct efi_device_path *tmp_dp = NULL, *tmp_fp = NULL;
  804. struct efi_device_path *initrd_dp = NULL;
  805. efi_status_t ret;
  806. const struct efi_initrd_dp id_dp = {
  807. .vendor = {
  808. {
  809. DEVICE_PATH_TYPE_MEDIA_DEVICE,
  810. DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
  811. sizeof(id_dp.vendor),
  812. },
  813. EFI_INITRD_MEDIA_GUID,
  814. },
  815. .end = {
  816. DEVICE_PATH_TYPE_END,
  817. DEVICE_PATH_SUB_TYPE_END,
  818. sizeof(id_dp.end),
  819. }
  820. };
  821. ret = efi_dp_from_name(dev, part, file, &tmp_dp, &tmp_fp);
  822. if (ret != EFI_SUCCESS) {
  823. printf("Cannot create device path for \"%s %s\"\n", part, file);
  824. goto out;
  825. }
  826. initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp,
  827. tmp_fp);
  828. out:
  829. efi_free_pool(tmp_dp);
  830. efi_free_pool(tmp_fp);
  831. return initrd_dp;
  832. }
  833. /**
  834. * do_efi_boot_add() - set UEFI load option
  835. *
  836. * @cmdtp: Command table
  837. * @flag: Command flag
  838. * @argc: Number of arguments
  839. * @argv: Argument array
  840. * Return: CMD_RET_SUCCESS on success,
  841. * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  842. *
  843. * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
  844. *
  845. * efidebug boot add -b <id> <label> <interface> <devnum>[:<part>] <file>
  846. * -i <file> <interface2> <devnum2>[:<part>] <initrd>
  847. * -s '<options>'
  848. */
  849. static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
  850. int argc, char *const argv[])
  851. {
  852. int id;
  853. char *endp;
  854. char var_name[9];
  855. u16 var_name16[9], *p;
  856. efi_guid_t guid;
  857. size_t label_len, label_len16;
  858. u16 *label;
  859. struct efi_device_path *device_path = NULL, *file_path = NULL;
  860. struct efi_device_path *final_fp = NULL;
  861. struct efi_device_path *initrd_dp = NULL;
  862. struct efi_load_option lo;
  863. void *data = NULL;
  864. efi_uintn_t size;
  865. efi_uintn_t fp_size = 0;
  866. efi_status_t ret;
  867. int r = CMD_RET_SUCCESS;
  868. guid = efi_global_variable_guid;
  869. /* attributes */
  870. lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
  871. lo.optional_data = NULL;
  872. lo.label = NULL;
  873. argc--;
  874. argv++; /* 'add' */
  875. for (; argc > 0; argc--, argv++) {
  876. if (!strcmp(argv[0], "-b")) {
  877. if (argc < 5 || lo.label) {
  878. r = CMD_RET_USAGE;
  879. goto out;
  880. }
  881. id = (int)simple_strtoul(argv[1], &endp, 16);
  882. if (*endp != '\0' || id > 0xffff)
  883. return CMD_RET_USAGE;
  884. sprintf(var_name, "Boot%04X", id);
  885. p = var_name16;
  886. utf8_utf16_strncpy(&p, var_name, 9);
  887. /* label */
  888. label_len = strlen(argv[2]);
  889. label_len16 = utf8_utf16_strnlen(argv[2], label_len);
  890. label = malloc((label_len16 + 1) * sizeof(u16));
  891. if (!label)
  892. return CMD_RET_FAILURE;
  893. lo.label = label; /* label will be changed below */
  894. utf8_utf16_strncpy(&label, argv[2], label_len);
  895. /* file path */
  896. ret = efi_dp_from_name(argv[3], argv[4], argv[5],
  897. &device_path, &file_path);
  898. if (ret != EFI_SUCCESS) {
  899. printf("Cannot create device path for \"%s %s\"\n",
  900. argv[3], argv[4]);
  901. r = CMD_RET_FAILURE;
  902. goto out;
  903. }
  904. fp_size += efi_dp_size(file_path) +
  905. sizeof(struct efi_device_path);
  906. argc -= 5;
  907. argv += 5;
  908. } else if (!strcmp(argv[0], "-i")) {
  909. if (argc < 3 || initrd_dp) {
  910. r = CMD_RET_USAGE;
  911. goto out;
  912. }
  913. initrd_dp = create_initrd_dp(argv[1], argv[2], argv[3]);
  914. if (!initrd_dp) {
  915. printf("Cannot add an initrd\n");
  916. r = CMD_RET_FAILURE;
  917. goto out;
  918. }
  919. argc -= 3;
  920. argv += 3;
  921. fp_size += efi_dp_size(initrd_dp) +
  922. sizeof(struct efi_device_path);
  923. } else if (!strcmp(argv[0], "-s")) {
  924. if (argc < 1 || lo.optional_data) {
  925. r = CMD_RET_USAGE;
  926. goto out;
  927. }
  928. lo.optional_data = (const u8 *)argv[1];
  929. argc -= 1;
  930. argv += 1;
  931. } else {
  932. r = CMD_RET_USAGE;
  933. goto out;
  934. }
  935. }
  936. if (!file_path) {
  937. printf("Missing binary\n");
  938. r = CMD_RET_USAGE;
  939. goto out;
  940. }
  941. final_fp = efi_dp_concat(file_path, initrd_dp);
  942. if (!final_fp) {
  943. printf("Cannot create final device path\n");
  944. r = CMD_RET_FAILURE;
  945. goto out;
  946. }
  947. lo.file_path = final_fp;
  948. lo.file_path_length = fp_size;
  949. size = efi_serialize_load_option(&lo, (u8 **)&data);
  950. if (!size) {
  951. r = CMD_RET_FAILURE;
  952. goto out;
  953. }
  954. ret = EFI_CALL(efi_set_variable(var_name16, &guid,
  955. EFI_VARIABLE_NON_VOLATILE |
  956. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  957. EFI_VARIABLE_RUNTIME_ACCESS,
  958. size, data));
  959. if (ret != EFI_SUCCESS) {
  960. printf("Cannot set %ls\n", var_name16);
  961. r = CMD_RET_FAILURE;
  962. }
  963. out:
  964. free(data);
  965. efi_free_pool(final_fp);
  966. efi_free_pool(initrd_dp);
  967. efi_free_pool(device_path);
  968. efi_free_pool(file_path);
  969. free(lo.label);
  970. return r;
  971. }
  972. /**
  973. * do_efi_boot_rm() - delete UEFI load options
  974. *
  975. * @cmdtp: Command table
  976. * @flag: Command flag
  977. * @argc: Number of arguments
  978. * @argv: Argument array
  979. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  980. *
  981. * Implement efidebug "boot rm" sub-command.
  982. * Delete UEFI load options.
  983. *
  984. * efidebug boot rm <id> ...
  985. */
  986. static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag,
  987. int argc, char *const argv[])
  988. {
  989. efi_guid_t guid;
  990. int id, i;
  991. char *endp;
  992. char var_name[9];
  993. u16 var_name16[9], *p;
  994. efi_status_t ret;
  995. if (argc == 1)
  996. return CMD_RET_USAGE;
  997. guid = efi_global_variable_guid;
  998. for (i = 1; i < argc; i++, argv++) {
  999. id = (int)simple_strtoul(argv[1], &endp, 16);
  1000. if (*endp != '\0' || id > 0xffff)
  1001. return CMD_RET_FAILURE;
  1002. sprintf(var_name, "Boot%04X", id);
  1003. p = var_name16;
  1004. utf8_utf16_strncpy(&p, var_name, 9);
  1005. ret = EFI_CALL(efi_set_variable(var_name16, &guid, 0, 0, NULL));
  1006. if (ret) {
  1007. printf("Cannot remove %ls\n", var_name16);
  1008. return CMD_RET_FAILURE;
  1009. }
  1010. }
  1011. return CMD_RET_SUCCESS;
  1012. }
  1013. /**
  1014. * show_efi_boot_opt_data() - dump UEFI load option
  1015. *
  1016. * @varname16: variable name
  1017. * @data: value of UEFI load option variable
  1018. * @size: size of the boot option
  1019. *
  1020. * Decode the value of UEFI load option variable and print information.
  1021. */
  1022. static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size)
  1023. {
  1024. struct efi_device_path *initrd_path = NULL;
  1025. struct efi_load_option lo;
  1026. char *label, *p;
  1027. size_t label_len16, label_len;
  1028. u16 *dp_str;
  1029. efi_status_t ret;
  1030. efi_uintn_t initrd_dp_size;
  1031. const efi_guid_t lf2_initrd_guid = EFI_INITRD_MEDIA_GUID;
  1032. ret = efi_deserialize_load_option(&lo, data, size);
  1033. if (ret != EFI_SUCCESS) {
  1034. printf("%ls: invalid load option\n", varname16);
  1035. return;
  1036. }
  1037. label_len16 = u16_strlen(lo.label);
  1038. label_len = utf16_utf8_strnlen(lo.label, label_len16);
  1039. label = malloc(label_len + 1);
  1040. if (!label)
  1041. return;
  1042. p = label;
  1043. utf16_utf8_strncpy(&p, lo.label, label_len16);
  1044. printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
  1045. varname16,
  1046. /* ACTIVE */
  1047. lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
  1048. /* FORCE RECONNECT */
  1049. lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
  1050. /* HIDDEN */
  1051. lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
  1052. lo.attributes);
  1053. printf(" label: %s\n", label);
  1054. dp_str = efi_dp_str(lo.file_path);
  1055. printf(" file_path: %ls\n", dp_str);
  1056. efi_free_pool(dp_str);
  1057. initrd_path = efi_dp_from_lo(&lo, &initrd_dp_size, lf2_initrd_guid);
  1058. if (initrd_path) {
  1059. dp_str = efi_dp_str(initrd_path);
  1060. printf(" initrd_path: %ls\n", dp_str);
  1061. efi_free_pool(dp_str);
  1062. efi_free_pool(initrd_path);
  1063. }
  1064. printf(" data:\n");
  1065. print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
  1066. lo.optional_data, *size, true);
  1067. free(label);
  1068. }
  1069. /**
  1070. * show_efi_boot_opt() - dump UEFI load option
  1071. *
  1072. * @varname16: variable name
  1073. *
  1074. * Dump information defined by UEFI load option.
  1075. */
  1076. static void show_efi_boot_opt(u16 *varname16)
  1077. {
  1078. void *data;
  1079. efi_uintn_t size;
  1080. efi_status_t ret;
  1081. size = 0;
  1082. ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
  1083. NULL, &size, NULL));
  1084. if (ret == EFI_BUFFER_TOO_SMALL) {
  1085. data = malloc(size);
  1086. if (!data) {
  1087. printf("ERROR: Out of memory\n");
  1088. return;
  1089. }
  1090. ret = EFI_CALL(efi_get_variable(varname16,
  1091. &efi_global_variable_guid,
  1092. NULL, &size, data));
  1093. if (ret == EFI_SUCCESS)
  1094. show_efi_boot_opt_data(varname16, data, &size);
  1095. free(data);
  1096. }
  1097. }
  1098. static int u16_tohex(u16 c)
  1099. {
  1100. if (c >= '0' && c <= '9')
  1101. return c - '0';
  1102. if (c >= 'A' && c <= 'F')
  1103. return c - 'A' + 10;
  1104. /* not hexadecimal */
  1105. return -1;
  1106. }
  1107. /**
  1108. * show_efi_boot_dump() - dump all UEFI load options
  1109. *
  1110. * @cmdtp: Command table
  1111. * @flag: Command flag
  1112. * @argc: Number of arguments
  1113. * @argv: Argument array
  1114. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  1115. *
  1116. * Implement efidebug "boot dump" sub-command.
  1117. * Dump information of all UEFI load options defined.
  1118. *
  1119. * efidebug boot dump
  1120. */
  1121. static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
  1122. int argc, char *const argv[])
  1123. {
  1124. u16 *var_name16, *p;
  1125. efi_uintn_t buf_size, size;
  1126. efi_guid_t guid;
  1127. int id, i, digit;
  1128. efi_status_t ret;
  1129. if (argc > 1)
  1130. return CMD_RET_USAGE;
  1131. buf_size = 128;
  1132. var_name16 = malloc(buf_size);
  1133. if (!var_name16)
  1134. return CMD_RET_FAILURE;
  1135. var_name16[0] = 0;
  1136. for (;;) {
  1137. size = buf_size;
  1138. ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
  1139. &guid));
  1140. if (ret == EFI_NOT_FOUND)
  1141. break;
  1142. if (ret == EFI_BUFFER_TOO_SMALL) {
  1143. buf_size = size;
  1144. p = realloc(var_name16, buf_size);
  1145. if (!p) {
  1146. free(var_name16);
  1147. return CMD_RET_FAILURE;
  1148. }
  1149. var_name16 = p;
  1150. ret = EFI_CALL(efi_get_next_variable_name(&size,
  1151. var_name16,
  1152. &guid));
  1153. }
  1154. if (ret != EFI_SUCCESS) {
  1155. free(var_name16);
  1156. return CMD_RET_FAILURE;
  1157. }
  1158. if (memcmp(var_name16, L"Boot", 8))
  1159. continue;
  1160. for (id = 0, i = 0; i < 4; i++) {
  1161. digit = u16_tohex(var_name16[4 + i]);
  1162. if (digit < 0)
  1163. break;
  1164. id = (id << 4) + digit;
  1165. }
  1166. if (i == 4 && !var_name16[8])
  1167. show_efi_boot_opt(var_name16);
  1168. }
  1169. free(var_name16);
  1170. return CMD_RET_SUCCESS;
  1171. }
  1172. /**
  1173. * show_efi_boot_order() - show order of UEFI load options
  1174. *
  1175. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  1176. *
  1177. * Show order of UEFI load options defined by BootOrder variable.
  1178. */
  1179. static int show_efi_boot_order(void)
  1180. {
  1181. u16 *bootorder;
  1182. efi_uintn_t size;
  1183. int num, i;
  1184. char var_name[9];
  1185. u16 var_name16[9], *p16;
  1186. void *data;
  1187. struct efi_load_option lo;
  1188. char *label, *p;
  1189. size_t label_len16, label_len;
  1190. efi_status_t ret;
  1191. size = 0;
  1192. ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
  1193. NULL, &size, NULL));
  1194. if (ret != EFI_BUFFER_TOO_SMALL) {
  1195. if (ret == EFI_NOT_FOUND) {
  1196. printf("BootOrder not defined\n");
  1197. return CMD_RET_SUCCESS;
  1198. } else {
  1199. return CMD_RET_FAILURE;
  1200. }
  1201. }
  1202. bootorder = malloc(size);
  1203. if (!bootorder) {
  1204. printf("ERROR: Out of memory\n");
  1205. return CMD_RET_FAILURE;
  1206. }
  1207. ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
  1208. NULL, &size, bootorder));
  1209. if (ret != EFI_SUCCESS) {
  1210. ret = CMD_RET_FAILURE;
  1211. goto out;
  1212. }
  1213. num = size / sizeof(u16);
  1214. for (i = 0; i < num; i++) {
  1215. sprintf(var_name, "Boot%04X", bootorder[i]);
  1216. p16 = var_name16;
  1217. utf8_utf16_strncpy(&p16, var_name, 9);
  1218. size = 0;
  1219. ret = EFI_CALL(efi_get_variable(var_name16,
  1220. &efi_global_variable_guid, NULL,
  1221. &size, NULL));
  1222. if (ret != EFI_BUFFER_TOO_SMALL) {
  1223. printf("%2d: %s: (not defined)\n", i + 1, var_name);
  1224. continue;
  1225. }
  1226. data = malloc(size);
  1227. if (!data) {
  1228. ret = CMD_RET_FAILURE;
  1229. goto out;
  1230. }
  1231. ret = EFI_CALL(efi_get_variable(var_name16,
  1232. &efi_global_variable_guid, NULL,
  1233. &size, data));
  1234. if (ret != EFI_SUCCESS) {
  1235. free(data);
  1236. ret = CMD_RET_FAILURE;
  1237. goto out;
  1238. }
  1239. ret = efi_deserialize_load_option(&lo, data, &size);
  1240. if (ret != EFI_SUCCESS) {
  1241. printf("%ls: invalid load option\n", var_name16);
  1242. ret = CMD_RET_FAILURE;
  1243. goto out;
  1244. }
  1245. label_len16 = u16_strlen(lo.label);
  1246. label_len = utf16_utf8_strnlen(lo.label, label_len16);
  1247. label = malloc(label_len + 1);
  1248. if (!label) {
  1249. free(data);
  1250. ret = CMD_RET_FAILURE;
  1251. goto out;
  1252. }
  1253. p = label;
  1254. utf16_utf8_strncpy(&p, lo.label, label_len16);
  1255. printf("%2d: %s: %s\n", i + 1, var_name, label);
  1256. free(label);
  1257. free(data);
  1258. }
  1259. out:
  1260. free(bootorder);
  1261. return ret;
  1262. }
  1263. /**
  1264. * do_efi_boot_next() - manage UEFI BootNext variable
  1265. *
  1266. * @cmdtp: Command table
  1267. * @flag: Command flag
  1268. * @argc: Number of arguments
  1269. * @argv: Argument array
  1270. * Return: CMD_RET_SUCCESS on success,
  1271. * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  1272. *
  1273. * Implement efidebug "boot next" sub-command.
  1274. * Set BootNext variable.
  1275. *
  1276. * efidebug boot next <id>
  1277. */
  1278. static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
  1279. int argc, char *const argv[])
  1280. {
  1281. u16 bootnext;
  1282. efi_uintn_t size;
  1283. char *endp;
  1284. efi_guid_t guid;
  1285. efi_status_t ret;
  1286. int r = CMD_RET_SUCCESS;
  1287. if (argc != 2)
  1288. return CMD_RET_USAGE;
  1289. bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
  1290. if (*endp) {
  1291. printf("invalid value: %s\n", argv[1]);
  1292. r = CMD_RET_FAILURE;
  1293. goto out;
  1294. }
  1295. guid = efi_global_variable_guid;
  1296. size = sizeof(u16);
  1297. ret = EFI_CALL(efi_set_variable(L"BootNext", &guid,
  1298. EFI_VARIABLE_NON_VOLATILE |
  1299. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  1300. EFI_VARIABLE_RUNTIME_ACCESS,
  1301. size, &bootnext));
  1302. if (ret != EFI_SUCCESS) {
  1303. printf("Cannot set BootNext\n");
  1304. r = CMD_RET_FAILURE;
  1305. }
  1306. out:
  1307. return r;
  1308. }
  1309. /**
  1310. * do_efi_boot_order() - manage UEFI BootOrder variable
  1311. *
  1312. * @cmdtp: Command table
  1313. * @flag: Command flag
  1314. * @argc: Number of arguments
  1315. * @argv: Argument array
  1316. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  1317. *
  1318. * Implement efidebug "boot order" sub-command.
  1319. * Show order of UEFI load options, or change it in BootOrder variable.
  1320. *
  1321. * efidebug boot order [<id> ...]
  1322. */
  1323. static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
  1324. int argc, char *const argv[])
  1325. {
  1326. u16 *bootorder = NULL;
  1327. efi_uintn_t size;
  1328. int id, i;
  1329. char *endp;
  1330. efi_guid_t guid;
  1331. efi_status_t ret;
  1332. int r = CMD_RET_SUCCESS;
  1333. if (argc == 1)
  1334. return show_efi_boot_order();
  1335. argc--;
  1336. argv++;
  1337. size = argc * sizeof(u16);
  1338. bootorder = malloc(size);
  1339. if (!bootorder)
  1340. return CMD_RET_FAILURE;
  1341. for (i = 0; i < argc; i++) {
  1342. id = (int)simple_strtoul(argv[i], &endp, 16);
  1343. if (*endp != '\0' || id > 0xffff) {
  1344. printf("invalid value: %s\n", argv[i]);
  1345. r = CMD_RET_FAILURE;
  1346. goto out;
  1347. }
  1348. bootorder[i] = (u16)id;
  1349. }
  1350. guid = efi_global_variable_guid;
  1351. ret = EFI_CALL(efi_set_variable(L"BootOrder", &guid,
  1352. EFI_VARIABLE_NON_VOLATILE |
  1353. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  1354. EFI_VARIABLE_RUNTIME_ACCESS,
  1355. size, bootorder));
  1356. if (ret != EFI_SUCCESS) {
  1357. printf("Cannot set BootOrder\n");
  1358. r = CMD_RET_FAILURE;
  1359. }
  1360. out:
  1361. free(bootorder);
  1362. return r;
  1363. }
  1364. static struct cmd_tbl cmd_efidebug_boot_sub[] = {
  1365. U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
  1366. U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
  1367. U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
  1368. U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
  1369. U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
  1370. "", ""),
  1371. };
  1372. /**
  1373. * do_efi_boot_opt() - manage UEFI load options
  1374. *
  1375. * @cmdtp: Command table
  1376. * @flag: Command flag
  1377. * @argc: Number of arguments
  1378. * @argv: Argument array
  1379. * Return: CMD_RET_SUCCESS on success,
  1380. * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  1381. *
  1382. * Implement efidebug "boot" sub-command.
  1383. */
  1384. static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
  1385. int argc, char *const argv[])
  1386. {
  1387. struct cmd_tbl *cp;
  1388. if (argc < 2)
  1389. return CMD_RET_USAGE;
  1390. argc--; argv++;
  1391. cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
  1392. ARRAY_SIZE(cmd_efidebug_boot_sub));
  1393. if (!cp)
  1394. return CMD_RET_USAGE;
  1395. return cp->cmd(cmdtp, flag, argc, argv);
  1396. }
  1397. /**
  1398. * do_efi_test_bootmgr() - run simple bootmgr for test
  1399. *
  1400. * @cmdtp: Command table
  1401. * @flag: Command flag
  1402. * @argc: Number of arguments
  1403. * @argv: Argument array
  1404. * Return: CMD_RET_SUCCESS on success,
  1405. * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  1406. *
  1407. * Implement efidebug "test bootmgr" sub-command.
  1408. * Run simple bootmgr for test.
  1409. *
  1410. * efidebug test bootmgr
  1411. */
  1412. static __maybe_unused int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
  1413. int argc, char * const argv[])
  1414. {
  1415. efi_handle_t image;
  1416. efi_uintn_t exit_data_size = 0;
  1417. u16 *exit_data = NULL;
  1418. efi_status_t ret;
  1419. void *load_options = NULL;
  1420. ret = efi_bootmgr_load(&image, &load_options);
  1421. printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
  1422. /* We call efi_start_image() even if error for test purpose. */
  1423. ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
  1424. printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
  1425. if (ret && exit_data)
  1426. efi_free_pool(exit_data);
  1427. efi_restore_gd();
  1428. free(load_options);
  1429. return CMD_RET_SUCCESS;
  1430. }
  1431. static struct cmd_tbl cmd_efidebug_test_sub[] = {
  1432. #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
  1433. U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
  1434. "", ""),
  1435. #endif
  1436. };
  1437. /**
  1438. * do_efi_test() - manage UEFI load options
  1439. *
  1440. * @cmdtp: Command table
  1441. * @flag: Command flag
  1442. * @argc: Number of arguments
  1443. * @argv: Argument array
  1444. * Return: CMD_RET_SUCCESS on success,
  1445. * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  1446. *
  1447. * Implement efidebug "test" sub-command.
  1448. */
  1449. static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
  1450. int argc, char * const argv[])
  1451. {
  1452. struct cmd_tbl *cp;
  1453. if (argc < 2)
  1454. return CMD_RET_USAGE;
  1455. argc--; argv++;
  1456. cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
  1457. ARRAY_SIZE(cmd_efidebug_test_sub));
  1458. if (!cp)
  1459. return CMD_RET_USAGE;
  1460. return cp->cmd(cmdtp, flag, argc, argv);
  1461. }
  1462. /**
  1463. * do_efi_query_info() - QueryVariableInfo EFI service
  1464. *
  1465. * @cmdtp: Command table
  1466. * @flag: Command flag
  1467. * @argc: Number of arguments
  1468. * @argv: Argument array
  1469. * Return: CMD_RET_SUCCESS on success,
  1470. * CMD_RET_USAGE or CMD_RET_FAILURE on failure
  1471. *
  1472. * Implement efidebug "test" sub-command.
  1473. */
  1474. static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
  1475. int argc, char * const argv[])
  1476. {
  1477. efi_status_t ret;
  1478. u32 attr = 0;
  1479. u64 max_variable_storage_size;
  1480. u64 remain_variable_storage_size;
  1481. u64 max_variable_size;
  1482. int i;
  1483. for (i = 1; i < argc; i++) {
  1484. if (!strcmp(argv[i], "-bs"))
  1485. attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
  1486. else if (!strcmp(argv[i], "-rt"))
  1487. attr |= EFI_VARIABLE_RUNTIME_ACCESS;
  1488. else if (!strcmp(argv[i], "-nv"))
  1489. attr |= EFI_VARIABLE_NON_VOLATILE;
  1490. else if (!strcmp(argv[i], "-at"))
  1491. attr |=
  1492. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
  1493. }
  1494. ret = EFI_CALL(efi_query_variable_info(attr,
  1495. &max_variable_storage_size,
  1496. &remain_variable_storage_size,
  1497. &max_variable_size));
  1498. if (ret != EFI_SUCCESS) {
  1499. printf("Error: Cannot query UEFI variables, r = %lu\n",
  1500. ret & ~EFI_ERROR_MASK);
  1501. return CMD_RET_FAILURE;
  1502. }
  1503. printf("Max storage size %llu\n", max_variable_storage_size);
  1504. printf("Remaining storage size %llu\n", remain_variable_storage_size);
  1505. printf("Max variable size %llu\n", max_variable_size);
  1506. return CMD_RET_SUCCESS;
  1507. }
  1508. static struct cmd_tbl cmd_efidebug_sub[] = {
  1509. U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
  1510. #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
  1511. U_BOOT_CMD_MKENT(capsule, CONFIG_SYS_MAXARGS, 1, do_efi_capsule,
  1512. "", ""),
  1513. #endif
  1514. U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
  1515. "", ""),
  1516. U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
  1517. "", ""),
  1518. U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
  1519. "", ""),
  1520. U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
  1521. "", ""),
  1522. U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
  1523. "", ""),
  1524. U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
  1525. "", ""),
  1526. U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
  1527. "", ""),
  1528. U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
  1529. "", ""),
  1530. };
  1531. /**
  1532. * do_efidebug() - display and configure UEFI environment
  1533. *
  1534. * @cmdtp: Command table
  1535. * @flag: Command flag
  1536. * @argc: Number of arguments
  1537. * @argv: Argument array
  1538. * Return: CMD_RET_SUCCESS on success,
  1539. * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  1540. *
  1541. * Implement efidebug command which allows us to display and
  1542. * configure UEFI environment.
  1543. */
  1544. static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
  1545. int argc, char *const argv[])
  1546. {
  1547. struct cmd_tbl *cp;
  1548. efi_status_t r;
  1549. if (argc < 2)
  1550. return CMD_RET_USAGE;
  1551. argc--; argv++;
  1552. /* Initialize UEFI drivers */
  1553. r = efi_init_obj_list();
  1554. if (r != EFI_SUCCESS) {
  1555. printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
  1556. r & ~EFI_ERROR_MASK);
  1557. return CMD_RET_FAILURE;
  1558. }
  1559. cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
  1560. ARRAY_SIZE(cmd_efidebug_sub));
  1561. if (!cp)
  1562. return CMD_RET_USAGE;
  1563. return cp->cmd(cmdtp, flag, argc, argv);
  1564. }
  1565. #ifdef CONFIG_SYS_LONGHELP
  1566. static char efidebug_help_text[] =
  1567. " - UEFI Shell-like interface to configure UEFI environment\n"
  1568. "\n"
  1569. "efidebug boot add "
  1570. "-b <bootid> <label> <interface> <devnum>[:<part>] <file path> "
  1571. "-i <interface> <devnum>[:<part>] <initrd file path> "
  1572. "-s '<optional data>'\n"
  1573. " - set UEFI BootXXXX variable\n"
  1574. " <load options> will be passed to UEFI application\n"
  1575. "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
  1576. " - delete UEFI BootXXXX variables\n"
  1577. "efidebug boot dump\n"
  1578. " - dump all UEFI BootXXXX variables\n"
  1579. "efidebug boot next <bootid>\n"
  1580. " - set UEFI BootNext variable\n"
  1581. "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
  1582. " - set/show UEFI boot order\n"
  1583. "\n"
  1584. #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
  1585. "efidebug capsule update [-v] <capsule address>\n"
  1586. " - process a capsule\n"
  1587. "efidebug capsule disk-update\n"
  1588. " - update a capsule from disk\n"
  1589. "efidebug capsule show <capsule address>\n"
  1590. " - show capsule information\n"
  1591. "efidebug capsule result [<capsule result var>]\n"
  1592. " - show a capsule update result\n"
  1593. #ifdef CONFIG_EFI_ESRT
  1594. "efidebug capsule esrt\n"
  1595. " - print the ESRT\n"
  1596. #endif
  1597. "\n"
  1598. #endif
  1599. "efidebug devices\n"
  1600. " - show UEFI devices\n"
  1601. "efidebug drivers\n"
  1602. " - show UEFI drivers\n"
  1603. "efidebug dh\n"
  1604. " - show UEFI handles\n"
  1605. "efidebug images\n"
  1606. " - show loaded images\n"
  1607. "efidebug memmap\n"
  1608. " - show UEFI memory map\n"
  1609. "efidebug tables\n"
  1610. " - show UEFI configuration tables\n"
  1611. #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
  1612. "efidebug test bootmgr\n"
  1613. " - run simple bootmgr for test\n"
  1614. #endif
  1615. "efidebug query [-nv][-bs][-rt][-at]\n"
  1616. " - show size of UEFI variables store\n";
  1617. #endif
  1618. U_BOOT_CMD(
  1619. efidebug, CONFIG_SYS_MAXARGS, 0, do_efidebug,
  1620. "Configure UEFI environment",
  1621. efidebug_help_text
  1622. );