efidebug.c 43 KB

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