efidebug.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780
  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. efi_status_t ret;
  1023. ret = efi_deserialize_load_option(&lo, data, size);
  1024. if (ret != EFI_SUCCESS) {
  1025. printf("%ls: invalid load option\n", varname16);
  1026. return;
  1027. }
  1028. printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
  1029. varname16,
  1030. /* ACTIVE */
  1031. lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
  1032. /* FORCE RECONNECT */
  1033. lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
  1034. /* HIDDEN */
  1035. lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
  1036. lo.attributes);
  1037. printf(" label: %ls\n", lo.label);
  1038. printf(" file_path: %pD\n", lo.file_path);
  1039. initrd_path = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
  1040. if (initrd_path) {
  1041. printf(" initrd_path: %pD\n", initrd_path);
  1042. efi_free_pool(initrd_path);
  1043. }
  1044. printf(" data:\n");
  1045. print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
  1046. lo.optional_data, *size, true);
  1047. }
  1048. /**
  1049. * show_efi_boot_opt() - dump UEFI load option
  1050. *
  1051. * @varname16: variable name
  1052. *
  1053. * Dump information defined by UEFI load option.
  1054. */
  1055. static void show_efi_boot_opt(u16 *varname16)
  1056. {
  1057. void *data;
  1058. efi_uintn_t size;
  1059. efi_status_t ret;
  1060. size = 0;
  1061. ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
  1062. NULL, &size, NULL));
  1063. if (ret == EFI_BUFFER_TOO_SMALL) {
  1064. data = malloc(size);
  1065. if (!data) {
  1066. printf("ERROR: Out of memory\n");
  1067. return;
  1068. }
  1069. ret = EFI_CALL(efi_get_variable(varname16,
  1070. &efi_global_variable_guid,
  1071. NULL, &size, data));
  1072. if (ret == EFI_SUCCESS)
  1073. show_efi_boot_opt_data(varname16, data, &size);
  1074. free(data);
  1075. }
  1076. }
  1077. static int u16_tohex(u16 c)
  1078. {
  1079. if (c >= '0' && c <= '9')
  1080. return c - '0';
  1081. if (c >= 'A' && c <= 'F')
  1082. return c - 'A' + 10;
  1083. /* not hexadecimal */
  1084. return -1;
  1085. }
  1086. /**
  1087. * show_efi_boot_dump() - dump all UEFI load options
  1088. *
  1089. * @cmdtp: Command table
  1090. * @flag: Command flag
  1091. * @argc: Number of arguments
  1092. * @argv: Argument array
  1093. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  1094. *
  1095. * Implement efidebug "boot dump" sub-command.
  1096. * Dump information of all UEFI load options defined.
  1097. *
  1098. * efidebug boot dump
  1099. */
  1100. static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
  1101. int argc, char *const argv[])
  1102. {
  1103. u16 *var_name16, *p;
  1104. efi_uintn_t buf_size, size;
  1105. efi_guid_t guid;
  1106. int id, i, digit;
  1107. efi_status_t ret;
  1108. if (argc > 1)
  1109. return CMD_RET_USAGE;
  1110. buf_size = 128;
  1111. var_name16 = malloc(buf_size);
  1112. if (!var_name16)
  1113. return CMD_RET_FAILURE;
  1114. var_name16[0] = 0;
  1115. for (;;) {
  1116. size = buf_size;
  1117. ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
  1118. &guid));
  1119. if (ret == EFI_NOT_FOUND)
  1120. break;
  1121. if (ret == EFI_BUFFER_TOO_SMALL) {
  1122. buf_size = size;
  1123. p = realloc(var_name16, buf_size);
  1124. if (!p) {
  1125. free(var_name16);
  1126. return CMD_RET_FAILURE;
  1127. }
  1128. var_name16 = p;
  1129. ret = EFI_CALL(efi_get_next_variable_name(&size,
  1130. var_name16,
  1131. &guid));
  1132. }
  1133. if (ret != EFI_SUCCESS) {
  1134. free(var_name16);
  1135. return CMD_RET_FAILURE;
  1136. }
  1137. if (memcmp(var_name16, L"Boot", 8))
  1138. continue;
  1139. for (id = 0, i = 0; i < 4; i++) {
  1140. digit = u16_tohex(var_name16[4 + i]);
  1141. if (digit < 0)
  1142. break;
  1143. id = (id << 4) + digit;
  1144. }
  1145. if (i == 4 && !var_name16[8])
  1146. show_efi_boot_opt(var_name16);
  1147. }
  1148. free(var_name16);
  1149. return CMD_RET_SUCCESS;
  1150. }
  1151. /**
  1152. * show_efi_boot_order() - show order of UEFI load options
  1153. *
  1154. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  1155. *
  1156. * Show order of UEFI load options defined by BootOrder variable.
  1157. */
  1158. static int show_efi_boot_order(void)
  1159. {
  1160. u16 *bootorder;
  1161. efi_uintn_t size;
  1162. int num, i;
  1163. u16 var_name16[9];
  1164. void *data;
  1165. struct efi_load_option lo;
  1166. efi_status_t ret;
  1167. size = 0;
  1168. ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
  1169. NULL, &size, NULL));
  1170. if (ret != EFI_BUFFER_TOO_SMALL) {
  1171. if (ret == EFI_NOT_FOUND) {
  1172. printf("BootOrder not defined\n");
  1173. return CMD_RET_SUCCESS;
  1174. } else {
  1175. return CMD_RET_FAILURE;
  1176. }
  1177. }
  1178. bootorder = malloc(size);
  1179. if (!bootorder) {
  1180. printf("ERROR: Out of memory\n");
  1181. return CMD_RET_FAILURE;
  1182. }
  1183. ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
  1184. NULL, &size, bootorder));
  1185. if (ret != EFI_SUCCESS) {
  1186. ret = CMD_RET_FAILURE;
  1187. goto out;
  1188. }
  1189. num = size / sizeof(u16);
  1190. for (i = 0; i < num; i++) {
  1191. efi_create_indexed_name(var_name16, sizeof(var_name16),
  1192. "Boot", bootorder[i]);
  1193. size = 0;
  1194. ret = EFI_CALL(efi_get_variable(var_name16,
  1195. &efi_global_variable_guid, NULL,
  1196. &size, NULL));
  1197. if (ret != EFI_BUFFER_TOO_SMALL) {
  1198. printf("%2d: %ls: (not defined)\n", i + 1, var_name16);
  1199. continue;
  1200. }
  1201. data = malloc(size);
  1202. if (!data) {
  1203. ret = CMD_RET_FAILURE;
  1204. goto out;
  1205. }
  1206. ret = EFI_CALL(efi_get_variable(var_name16,
  1207. &efi_global_variable_guid, NULL,
  1208. &size, data));
  1209. if (ret != EFI_SUCCESS) {
  1210. free(data);
  1211. ret = CMD_RET_FAILURE;
  1212. goto out;
  1213. }
  1214. ret = efi_deserialize_load_option(&lo, data, &size);
  1215. if (ret != EFI_SUCCESS) {
  1216. printf("%ls: invalid load option\n", var_name16);
  1217. ret = CMD_RET_FAILURE;
  1218. goto out;
  1219. }
  1220. printf("%2d: %ls: %ls\n", i + 1, var_name16, lo.label);
  1221. free(data);
  1222. }
  1223. out:
  1224. free(bootorder);
  1225. return ret;
  1226. }
  1227. /**
  1228. * do_efi_boot_next() - manage UEFI BootNext variable
  1229. *
  1230. * @cmdtp: Command table
  1231. * @flag: Command flag
  1232. * @argc: Number of arguments
  1233. * @argv: Argument array
  1234. * Return: CMD_RET_SUCCESS on success,
  1235. * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  1236. *
  1237. * Implement efidebug "boot next" sub-command.
  1238. * Set BootNext variable.
  1239. *
  1240. * efidebug boot next <id>
  1241. */
  1242. static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
  1243. int argc, char *const argv[])
  1244. {
  1245. u16 bootnext;
  1246. efi_uintn_t size;
  1247. char *endp;
  1248. efi_guid_t guid;
  1249. efi_status_t ret;
  1250. int r = CMD_RET_SUCCESS;
  1251. if (argc != 2)
  1252. return CMD_RET_USAGE;
  1253. bootnext = (u16)hextoul(argv[1], &endp);
  1254. if (*endp) {
  1255. printf("invalid value: %s\n", argv[1]);
  1256. r = CMD_RET_FAILURE;
  1257. goto out;
  1258. }
  1259. guid = efi_global_variable_guid;
  1260. size = sizeof(u16);
  1261. ret = efi_set_variable_int(L"BootNext", &guid,
  1262. EFI_VARIABLE_NON_VOLATILE |
  1263. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  1264. EFI_VARIABLE_RUNTIME_ACCESS,
  1265. size, &bootnext, false);
  1266. if (ret != EFI_SUCCESS) {
  1267. printf("Cannot set BootNext\n");
  1268. r = CMD_RET_FAILURE;
  1269. }
  1270. out:
  1271. return r;
  1272. }
  1273. /**
  1274. * do_efi_boot_order() - manage UEFI BootOrder variable
  1275. *
  1276. * @cmdtp: Command table
  1277. * @flag: Command flag
  1278. * @argc: Number of arguments
  1279. * @argv: Argument array
  1280. * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
  1281. *
  1282. * Implement efidebug "boot order" sub-command.
  1283. * Show order of UEFI load options, or change it in BootOrder variable.
  1284. *
  1285. * efidebug boot order [<id> ...]
  1286. */
  1287. static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
  1288. int argc, char *const argv[])
  1289. {
  1290. u16 *bootorder = NULL;
  1291. efi_uintn_t size;
  1292. int id, i;
  1293. char *endp;
  1294. efi_guid_t guid;
  1295. efi_status_t ret;
  1296. int r = CMD_RET_SUCCESS;
  1297. if (argc == 1)
  1298. return show_efi_boot_order();
  1299. argc--;
  1300. argv++;
  1301. size = argc * sizeof(u16);
  1302. bootorder = malloc(size);
  1303. if (!bootorder)
  1304. return CMD_RET_FAILURE;
  1305. for (i = 0; i < argc; i++) {
  1306. id = (int)hextoul(argv[i], &endp);
  1307. if (*endp != '\0' || id > 0xffff) {
  1308. printf("invalid value: %s\n", argv[i]);
  1309. r = CMD_RET_FAILURE;
  1310. goto out;
  1311. }
  1312. bootorder[i] = (u16)id;
  1313. }
  1314. guid = efi_global_variable_guid;
  1315. ret = efi_set_variable_int(L"BootOrder", &guid,
  1316. EFI_VARIABLE_NON_VOLATILE |
  1317. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  1318. EFI_VARIABLE_RUNTIME_ACCESS,
  1319. size, bootorder, true);
  1320. if (ret != EFI_SUCCESS) {
  1321. printf("Cannot set BootOrder\n");
  1322. r = CMD_RET_FAILURE;
  1323. }
  1324. out:
  1325. free(bootorder);
  1326. return r;
  1327. }
  1328. static struct cmd_tbl cmd_efidebug_boot_sub[] = {
  1329. U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
  1330. U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
  1331. U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
  1332. U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
  1333. U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
  1334. "", ""),
  1335. };
  1336. /**
  1337. * do_efi_boot_opt() - manage UEFI load options
  1338. *
  1339. * @cmdtp: Command table
  1340. * @flag: Command flag
  1341. * @argc: Number of arguments
  1342. * @argv: Argument array
  1343. * Return: CMD_RET_SUCCESS on success,
  1344. * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  1345. *
  1346. * Implement efidebug "boot" sub-command.
  1347. */
  1348. static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
  1349. int argc, char *const argv[])
  1350. {
  1351. struct cmd_tbl *cp;
  1352. if (argc < 2)
  1353. return CMD_RET_USAGE;
  1354. argc--; argv++;
  1355. cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
  1356. ARRAY_SIZE(cmd_efidebug_boot_sub));
  1357. if (!cp)
  1358. return CMD_RET_USAGE;
  1359. return cp->cmd(cmdtp, flag, argc, argv);
  1360. }
  1361. /**
  1362. * do_efi_test_bootmgr() - run simple bootmgr for test
  1363. *
  1364. * @cmdtp: Command table
  1365. * @flag: Command flag
  1366. * @argc: Number of arguments
  1367. * @argv: Argument array
  1368. * Return: CMD_RET_SUCCESS on success,
  1369. * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  1370. *
  1371. * Implement efidebug "test bootmgr" sub-command.
  1372. * Run simple bootmgr for test.
  1373. *
  1374. * efidebug test bootmgr
  1375. */
  1376. static __maybe_unused int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
  1377. int argc, char * const argv[])
  1378. {
  1379. efi_handle_t image;
  1380. efi_uintn_t exit_data_size = 0;
  1381. u16 *exit_data = NULL;
  1382. efi_status_t ret;
  1383. void *load_options = NULL;
  1384. ret = efi_bootmgr_load(&image, &load_options);
  1385. printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
  1386. /* We call efi_start_image() even if error for test purpose. */
  1387. ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
  1388. printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
  1389. if (ret && exit_data)
  1390. efi_free_pool(exit_data);
  1391. efi_restore_gd();
  1392. free(load_options);
  1393. return CMD_RET_SUCCESS;
  1394. }
  1395. static struct cmd_tbl cmd_efidebug_test_sub[] = {
  1396. #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
  1397. U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
  1398. "", ""),
  1399. #endif
  1400. };
  1401. /**
  1402. * do_efi_test() - manage UEFI load options
  1403. *
  1404. * @cmdtp: Command table
  1405. * @flag: Command flag
  1406. * @argc: Number of arguments
  1407. * @argv: Argument array
  1408. * Return: CMD_RET_SUCCESS on success,
  1409. * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  1410. *
  1411. * Implement efidebug "test" sub-command.
  1412. */
  1413. static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
  1414. int argc, char * const argv[])
  1415. {
  1416. struct cmd_tbl *cp;
  1417. if (argc < 2)
  1418. return CMD_RET_USAGE;
  1419. argc--; argv++;
  1420. cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
  1421. ARRAY_SIZE(cmd_efidebug_test_sub));
  1422. if (!cp)
  1423. return CMD_RET_USAGE;
  1424. return cp->cmd(cmdtp, flag, argc, argv);
  1425. }
  1426. /**
  1427. * do_efi_query_info() - QueryVariableInfo EFI service
  1428. *
  1429. * @cmdtp: Command table
  1430. * @flag: Command flag
  1431. * @argc: Number of arguments
  1432. * @argv: Argument array
  1433. * Return: CMD_RET_SUCCESS on success,
  1434. * CMD_RET_USAGE or CMD_RET_FAILURE on failure
  1435. *
  1436. * Implement efidebug "test" sub-command.
  1437. */
  1438. static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
  1439. int argc, char * const argv[])
  1440. {
  1441. efi_status_t ret;
  1442. u32 attr = 0;
  1443. u64 max_variable_storage_size;
  1444. u64 remain_variable_storage_size;
  1445. u64 max_variable_size;
  1446. int i;
  1447. for (i = 1; i < argc; i++) {
  1448. if (!strcmp(argv[i], "-bs"))
  1449. attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
  1450. else if (!strcmp(argv[i], "-rt"))
  1451. attr |= EFI_VARIABLE_RUNTIME_ACCESS;
  1452. else if (!strcmp(argv[i], "-nv"))
  1453. attr |= EFI_VARIABLE_NON_VOLATILE;
  1454. else if (!strcmp(argv[i], "-at"))
  1455. attr |=
  1456. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
  1457. }
  1458. ret = EFI_CALL(efi_query_variable_info(attr,
  1459. &max_variable_storage_size,
  1460. &remain_variable_storage_size,
  1461. &max_variable_size));
  1462. if (ret != EFI_SUCCESS) {
  1463. printf("Error: Cannot query UEFI variables, r = %lu\n",
  1464. ret & ~EFI_ERROR_MASK);
  1465. return CMD_RET_FAILURE;
  1466. }
  1467. printf("Max storage size %llu\n", max_variable_storage_size);
  1468. printf("Remaining storage size %llu\n", remain_variable_storage_size);
  1469. printf("Max variable size %llu\n", max_variable_size);
  1470. return CMD_RET_SUCCESS;
  1471. }
  1472. static struct cmd_tbl cmd_efidebug_sub[] = {
  1473. U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
  1474. #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
  1475. U_BOOT_CMD_MKENT(capsule, CONFIG_SYS_MAXARGS, 1, do_efi_capsule,
  1476. "", ""),
  1477. #endif
  1478. U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
  1479. "", ""),
  1480. U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
  1481. "", ""),
  1482. U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
  1483. "", ""),
  1484. U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
  1485. "", ""),
  1486. U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
  1487. "", ""),
  1488. U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
  1489. "", ""),
  1490. U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
  1491. "", ""),
  1492. U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
  1493. "", ""),
  1494. };
  1495. /**
  1496. * do_efidebug() - display and configure UEFI environment
  1497. *
  1498. * @cmdtp: Command table
  1499. * @flag: Command flag
  1500. * @argc: Number of arguments
  1501. * @argv: Argument array
  1502. * Return: CMD_RET_SUCCESS on success,
  1503. * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  1504. *
  1505. * Implement efidebug command which allows us to display and
  1506. * configure UEFI environment.
  1507. */
  1508. static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
  1509. int argc, char *const argv[])
  1510. {
  1511. struct cmd_tbl *cp;
  1512. efi_status_t r;
  1513. if (argc < 2)
  1514. return CMD_RET_USAGE;
  1515. argc--; argv++;
  1516. /* Initialize UEFI drivers */
  1517. r = efi_init_obj_list();
  1518. if (r != EFI_SUCCESS) {
  1519. printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
  1520. r & ~EFI_ERROR_MASK);
  1521. return CMD_RET_FAILURE;
  1522. }
  1523. cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
  1524. ARRAY_SIZE(cmd_efidebug_sub));
  1525. if (!cp)
  1526. return CMD_RET_USAGE;
  1527. return cp->cmd(cmdtp, flag, argc, argv);
  1528. }
  1529. #ifdef CONFIG_SYS_LONGHELP
  1530. static char efidebug_help_text[] =
  1531. " - UEFI Shell-like interface to configure UEFI environment\n"
  1532. "\n"
  1533. "efidebug boot add "
  1534. "-b <bootid> <label> <interface> <devnum>[:<part>] <file path> "
  1535. "-i <interface> <devnum>[:<part>] <initrd file path> "
  1536. "-s '<optional data>'\n"
  1537. " - set UEFI BootXXXX variable\n"
  1538. " <load options> will be passed to UEFI application\n"
  1539. "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
  1540. " - delete UEFI BootXXXX variables\n"
  1541. "efidebug boot dump\n"
  1542. " - dump all UEFI BootXXXX variables\n"
  1543. "efidebug boot next <bootid>\n"
  1544. " - set UEFI BootNext variable\n"
  1545. "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
  1546. " - set/show UEFI boot order\n"
  1547. "\n"
  1548. #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
  1549. "efidebug capsule update [-v] <capsule address>\n"
  1550. " - process a capsule\n"
  1551. "efidebug capsule disk-update\n"
  1552. " - update a capsule from disk\n"
  1553. "efidebug capsule show <capsule address>\n"
  1554. " - show capsule information\n"
  1555. "efidebug capsule result [<capsule result var>]\n"
  1556. " - show a capsule update result\n"
  1557. #ifdef CONFIG_EFI_ESRT
  1558. "efidebug capsule esrt\n"
  1559. " - print the ESRT\n"
  1560. #endif
  1561. "\n"
  1562. #endif
  1563. "efidebug devices\n"
  1564. " - show UEFI devices\n"
  1565. "efidebug drivers\n"
  1566. " - show UEFI drivers\n"
  1567. "efidebug dh\n"
  1568. " - show UEFI handles\n"
  1569. "efidebug images\n"
  1570. " - show loaded images\n"
  1571. "efidebug memmap\n"
  1572. " - show UEFI memory map\n"
  1573. "efidebug tables\n"
  1574. " - show UEFI configuration tables\n"
  1575. #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
  1576. "efidebug test bootmgr\n"
  1577. " - run simple bootmgr for test\n"
  1578. #endif
  1579. "efidebug query [-nv][-bs][-rt][-at]\n"
  1580. " - show size of UEFI variables store\n";
  1581. #endif
  1582. U_BOOT_CMD(
  1583. efidebug, CONFIG_SYS_MAXARGS, 0, do_efidebug,
  1584. "Configure UEFI environment",
  1585. efidebug_help_text
  1586. );