nvedit_efi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Integrate UEFI variables to u-boot env interface
  4. *
  5. * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited
  6. */
  7. #include <charset.h>
  8. #include <common.h>
  9. #include <command.h>
  10. #include <efi_loader.h>
  11. #include <env.h>
  12. #include <exports.h>
  13. #include <hexdump.h>
  14. #include <malloc.h>
  15. #include <mapmem.h>
  16. #include <linux/kernel.h>
  17. /*
  18. * From efi_variable.c,
  19. *
  20. * Mapping between UEFI variables and u-boot variables:
  21. *
  22. * efi_$guid_$varname = {attributes}(type)value
  23. */
  24. static const struct {
  25. u32 mask;
  26. char *text;
  27. } efi_var_attrs[] = {
  28. {EFI_VARIABLE_NON_VOLATILE, "NV"},
  29. {EFI_VARIABLE_BOOTSERVICE_ACCESS, "BS"},
  30. {EFI_VARIABLE_RUNTIME_ACCESS, "RT"},
  31. {EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, "AW"},
  32. {EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, "AT"},
  33. };
  34. static const struct {
  35. efi_guid_t guid;
  36. char *text;
  37. } efi_guid_text[] = {
  38. /* signature database */
  39. {EFI_GLOBAL_VARIABLE_GUID, "EFI_GLOBAL_VARIABLE_GUID"},
  40. {EFI_IMAGE_SECURITY_DATABASE_GUID, "EFI_IMAGE_SECURITY_DATABASE_GUID"},
  41. /* certificate type */
  42. {EFI_CERT_SHA256_GUID, "EFI_CERT_SHA256_GUID"},
  43. {EFI_CERT_X509_GUID, "EFI_CERT_X509_GUID"},
  44. {EFI_CERT_TYPE_PKCS7_GUID, "EFI_CERT_TYPE_PKCS7_GUID"},
  45. };
  46. /* "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */
  47. static char unknown_guid[37];
  48. /**
  49. * efi_guid_to_str() - convert guid to readable name
  50. *
  51. * @guid: GUID
  52. * Return: string for GUID
  53. *
  54. * convert guid to readable name
  55. */
  56. static const char *efi_guid_to_str(const efi_guid_t *guid)
  57. {
  58. int i;
  59. for (i = 0; i < ARRAY_SIZE(efi_guid_text); i++)
  60. if (!guidcmp(guid, &efi_guid_text[i].guid))
  61. return efi_guid_text[i].text;
  62. uuid_bin_to_str((unsigned char *)guid->b, unknown_guid,
  63. UUID_STR_FORMAT_GUID);
  64. return unknown_guid;
  65. }
  66. /**
  67. * efi_dump_single_var() - show information about a UEFI variable
  68. *
  69. * @name: Name of the variable
  70. * @guid: Vendor GUID
  71. * @verbose: if true, dump data
  72. *
  73. * Show information encoded in one UEFI variable
  74. */
  75. static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, bool verbose)
  76. {
  77. u32 attributes;
  78. u8 *data;
  79. efi_uintn_t size;
  80. int count, i;
  81. efi_status_t ret;
  82. data = NULL;
  83. size = 0;
  84. ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size, data));
  85. if (ret == EFI_BUFFER_TOO_SMALL) {
  86. data = malloc(size);
  87. if (!data)
  88. goto out;
  89. ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size,
  90. data));
  91. }
  92. if (ret == EFI_NOT_FOUND) {
  93. printf("Error: \"%ls\" not defined\n", name);
  94. goto out;
  95. }
  96. if (ret != EFI_SUCCESS)
  97. goto out;
  98. printf("%ls:\n %s:", name, efi_guid_to_str(guid));
  99. for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++)
  100. if (attributes & efi_var_attrs[i].mask) {
  101. if (count)
  102. putc('|');
  103. else
  104. putc(' ');
  105. count++;
  106. puts(efi_var_attrs[i].text);
  107. }
  108. printf(", DataSize = 0x%zx\n", size);
  109. if (verbose)
  110. print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
  111. data, size, true);
  112. out:
  113. free(data);
  114. }
  115. /**
  116. * efi_dump_vars() - show information about named UEFI variables
  117. *
  118. * @argc: Number of arguments (variables)
  119. * @argv: Argument (variable name) array
  120. * @verbose: if true, dump data
  121. * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
  122. *
  123. * Show information encoded in named UEFI variables
  124. */
  125. static int efi_dump_vars(int argc, char * const argv[],
  126. const efi_guid_t *guid, bool verbose)
  127. {
  128. u16 *var_name16, *p;
  129. efi_uintn_t buf_size, size;
  130. buf_size = 128;
  131. var_name16 = malloc(buf_size);
  132. if (!var_name16)
  133. return CMD_RET_FAILURE;
  134. for (; argc > 0; argc--, argv++) {
  135. size = (utf8_utf16_strlen(argv[0]) + 1) * sizeof(u16);
  136. if (buf_size < size) {
  137. buf_size = size;
  138. p = realloc(var_name16, buf_size);
  139. if (!p) {
  140. free(var_name16);
  141. return CMD_RET_FAILURE;
  142. }
  143. var_name16 = p;
  144. }
  145. p = var_name16;
  146. utf8_utf16_strcpy(&p, argv[0]);
  147. efi_dump_single_var(var_name16, guid, verbose);
  148. }
  149. free(var_name16);
  150. return CMD_RET_SUCCESS;
  151. }
  152. static bool match_name(int argc, char * const argv[], u16 *var_name16)
  153. {
  154. char *buf, *p;
  155. size_t buflen;
  156. int i;
  157. bool result = false;
  158. buflen = utf16_utf8_strlen(var_name16) + 1;
  159. buf = calloc(1, buflen);
  160. if (!buf)
  161. return result;
  162. p = buf;
  163. utf16_utf8_strcpy(&p, var_name16);
  164. for (i = 0; i < argc; argc--, argv++) {
  165. if (!strcmp(buf, argv[i])) {
  166. result = true;
  167. goto out;
  168. }
  169. }
  170. out:
  171. free(buf);
  172. return result;
  173. }
  174. /**
  175. * efi_dump_var_all() - show information about all the UEFI variables
  176. *
  177. * @argc: Number of arguments (variables)
  178. * @argv: Argument (variable name) array
  179. * @verbose: if true, dump data
  180. * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
  181. *
  182. * Show information encoded in all the UEFI variables
  183. */
  184. static int efi_dump_var_all(int argc, char * const argv[],
  185. const efi_guid_t *guid_p, bool verbose)
  186. {
  187. u16 *var_name16, *p;
  188. efi_uintn_t buf_size, size;
  189. efi_guid_t guid;
  190. efi_status_t ret;
  191. if (argc && guid_p)
  192. /* simplified case */
  193. return efi_dump_vars(argc, argv, guid_p, verbose);
  194. buf_size = 128;
  195. var_name16 = malloc(buf_size);
  196. if (!var_name16)
  197. return CMD_RET_FAILURE;
  198. var_name16[0] = 0;
  199. for (;;) {
  200. size = buf_size;
  201. ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
  202. &guid));
  203. if (ret == EFI_NOT_FOUND)
  204. break;
  205. if (ret == EFI_BUFFER_TOO_SMALL) {
  206. buf_size = size;
  207. p = realloc(var_name16, buf_size);
  208. if (!p) {
  209. free(var_name16);
  210. return CMD_RET_FAILURE;
  211. }
  212. var_name16 = p;
  213. ret = EFI_CALL(efi_get_next_variable_name(&size,
  214. var_name16,
  215. &guid));
  216. }
  217. if (ret != EFI_SUCCESS) {
  218. free(var_name16);
  219. return CMD_RET_FAILURE;
  220. }
  221. if ((!guid_p || !guidcmp(guid_p, &guid)) &&
  222. (!argc || match_name(argc, argv, var_name16)))
  223. efi_dump_single_var(var_name16, &guid, verbose);
  224. }
  225. free(var_name16);
  226. return CMD_RET_SUCCESS;
  227. }
  228. /**
  229. * do_env_print_efi() - show information about UEFI variables
  230. *
  231. * @cmdtp: Command table
  232. * @flag: Command flag
  233. * @argc: Number of arguments
  234. * @argv: Argument array
  235. * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
  236. *
  237. * This function is for "env print -e" or "printenv -e" command:
  238. * => env print -e [-n] [-guid <guid> | -all] [var [...]]
  239. * If one or more variable names are specified, show information
  240. * named UEFI variables, otherwise show all the UEFI variables.
  241. */
  242. int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  243. {
  244. efi_guid_t guid;
  245. const efi_guid_t *guid_p;
  246. bool default_guid, guid_any, verbose;
  247. efi_status_t ret;
  248. /* Initialize EFI drivers */
  249. ret = efi_init_obj_list();
  250. if (ret != EFI_SUCCESS) {
  251. printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
  252. ret & ~EFI_ERROR_MASK);
  253. return CMD_RET_FAILURE;
  254. }
  255. default_guid = true;
  256. guid_any = false;
  257. verbose = true;
  258. for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  259. if (!strcmp(argv[0], "-guid")) {
  260. if (argc == 1)
  261. return CMD_RET_USAGE;
  262. /* -a already specified */
  263. if (!default_guid & guid_any)
  264. return CMD_RET_USAGE;
  265. argc--;
  266. argv++;
  267. if (uuid_str_to_bin(argv[0], guid.b,
  268. UUID_STR_FORMAT_GUID))
  269. return CMD_RET_USAGE;
  270. default_guid = false;
  271. } else if (!strcmp(argv[0], "-all")) {
  272. /* -guid already specified */
  273. if (!default_guid && !guid_any)
  274. return CMD_RET_USAGE;
  275. guid_any = true;
  276. default_guid = false;
  277. } else if (!strcmp(argv[0], "-n")) {
  278. verbose = false;
  279. } else {
  280. return CMD_RET_USAGE;
  281. }
  282. }
  283. if (guid_any)
  284. guid_p = NULL;
  285. else if (default_guid)
  286. guid_p = &efi_global_variable_guid;
  287. else
  288. guid_p = (const efi_guid_t *)guid.b;
  289. /* enumerate and show all UEFI variables */
  290. return efi_dump_var_all(argc, argv, guid_p, verbose);
  291. }
  292. /**
  293. * append_value() - encode UEFI variable's value
  294. * @bufp: Buffer of encoded UEFI variable's value
  295. * @sizep: Size of buffer
  296. * @data: data to be encoded into the value
  297. * Return: 0 on success, -1 otherwise
  298. *
  299. * Interpret a given data string and append it to buffer.
  300. * Buffer will be realloc'ed if necessary.
  301. *
  302. * Currently supported formats are:
  303. * =0x0123...: Hexadecimal number
  304. * =H0123...: Hexadecimal-byte array
  305. * ="...", =S"..." or <string>:
  306. * String
  307. */
  308. static int append_value(char **bufp, size_t *sizep, char *data)
  309. {
  310. char *tmp_buf = NULL, *new_buf = NULL, *value;
  311. unsigned long len = 0;
  312. if (!strncmp(data, "=0x", 2)) { /* hexadecimal number */
  313. union {
  314. u8 u8;
  315. u16 u16;
  316. u32 u32;
  317. u64 u64;
  318. } tmp_data;
  319. unsigned long hex_value;
  320. void *hex_ptr;
  321. data += 3;
  322. len = strlen(data);
  323. if ((len & 0x1)) /* not multiple of two */
  324. return -1;
  325. len /= 2;
  326. if (len > 8)
  327. return -1;
  328. else if (len > 4)
  329. len = 8;
  330. else if (len > 2)
  331. len = 4;
  332. /* convert hex hexadecimal number */
  333. if (strict_strtoul(data, 16, &hex_value) < 0)
  334. return -1;
  335. tmp_buf = malloc(len);
  336. if (!tmp_buf)
  337. return -1;
  338. if (len == 1) {
  339. tmp_data.u8 = hex_value;
  340. hex_ptr = &tmp_data.u8;
  341. } else if (len == 2) {
  342. tmp_data.u16 = hex_value;
  343. hex_ptr = &tmp_data.u16;
  344. } else if (len == 4) {
  345. tmp_data.u32 = hex_value;
  346. hex_ptr = &tmp_data.u32;
  347. } else {
  348. tmp_data.u64 = hex_value;
  349. hex_ptr = &tmp_data.u64;
  350. }
  351. memcpy(tmp_buf, hex_ptr, len);
  352. value = tmp_buf;
  353. } else if (!strncmp(data, "=H", 2)) { /* hexadecimal-byte array */
  354. data += 2;
  355. len = strlen(data);
  356. if (len & 0x1) /* not multiple of two */
  357. return -1;
  358. len /= 2;
  359. tmp_buf = malloc(len);
  360. if (!tmp_buf)
  361. return -1;
  362. if (hex2bin((u8 *)tmp_buf, data, len) < 0) {
  363. printf("Error: illegal hexadecimal string\n");
  364. free(tmp_buf);
  365. return -1;
  366. }
  367. value = tmp_buf;
  368. } else { /* string */
  369. if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) {
  370. if (data[1] == '"')
  371. data += 2;
  372. else
  373. data += 3;
  374. value = data;
  375. len = strlen(data) - 1;
  376. if (data[len] != '"')
  377. return -1;
  378. } else {
  379. value = data;
  380. len = strlen(data);
  381. }
  382. }
  383. new_buf = realloc(*bufp, *sizep + len);
  384. if (!new_buf)
  385. goto out;
  386. memcpy(new_buf + *sizep, value, len);
  387. *bufp = new_buf;
  388. *sizep += len;
  389. out:
  390. free(tmp_buf);
  391. return 0;
  392. }
  393. /**
  394. * do_env_set_efi() - set UEFI variable
  395. *
  396. * @cmdtp: Command table
  397. * @flag: Command flag
  398. * @argc: Number of arguments
  399. * @argv: Argument array
  400. * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
  401. *
  402. * This function is for "env set -e" or "setenv -e" command:
  403. * => env set -e [-guid guid][-nv][-bs][-rt][-at][-a][-v]
  404. * [-i address,size] var, or
  405. * var [value ...]
  406. * Encode values specified and set given UEFI variable.
  407. * If no value is specified, delete the variable.
  408. */
  409. int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  410. {
  411. char *var_name, *value, *ep;
  412. ulong addr;
  413. efi_uintn_t size;
  414. efi_guid_t guid;
  415. u32 attributes;
  416. bool default_guid, verbose, value_on_memory;
  417. u16 *var_name16 = NULL, *p;
  418. size_t len;
  419. efi_status_t ret;
  420. if (argc == 1)
  421. return CMD_RET_USAGE;
  422. /* Initialize EFI drivers */
  423. ret = efi_init_obj_list();
  424. if (ret != EFI_SUCCESS) {
  425. printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
  426. ret & ~EFI_ERROR_MASK);
  427. return CMD_RET_FAILURE;
  428. }
  429. /*
  430. * attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
  431. * EFI_VARIABLE_RUNTIME_ACCESS;
  432. */
  433. value = NULL;
  434. size = 0;
  435. attributes = 0;
  436. guid = efi_global_variable_guid;
  437. default_guid = true;
  438. verbose = false;
  439. value_on_memory = false;
  440. for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  441. if (!strcmp(argv[0], "-guid")) {
  442. if (argc == 1)
  443. return CMD_RET_USAGE;
  444. argc--;
  445. argv++;
  446. if (uuid_str_to_bin(argv[0], guid.b,
  447. UUID_STR_FORMAT_GUID)) {
  448. printf("## Guid not specified or in XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX format\n");
  449. return CMD_RET_FAILURE;
  450. }
  451. default_guid = false;
  452. } else if (!strcmp(argv[0], "-bs")) {
  453. attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
  454. } else if (!strcmp(argv[0], "-rt")) {
  455. attributes |= EFI_VARIABLE_RUNTIME_ACCESS;
  456. } else if (!strcmp(argv[0], "-nv")) {
  457. attributes |= EFI_VARIABLE_NON_VOLATILE;
  458. } else if (!strcmp(argv[0], "-at")) {
  459. attributes |=
  460. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
  461. } else if (!strcmp(argv[0], "-a")) {
  462. attributes |= EFI_VARIABLE_APPEND_WRITE;
  463. } else if (!strcmp(argv[0], "-i")) {
  464. /* data comes from memory */
  465. if (argc == 1)
  466. return CMD_RET_USAGE;
  467. argc--;
  468. argv++;
  469. addr = simple_strtoul(argv[0], &ep, 16);
  470. if (*ep != ',')
  471. return CMD_RET_USAGE;
  472. /* 0 should be allowed for delete */
  473. size = simple_strtoul(++ep, NULL, 16);
  474. value_on_memory = true;
  475. } else if (!strcmp(argv[0], "-v")) {
  476. verbose = true;
  477. } else {
  478. return CMD_RET_USAGE;
  479. }
  480. }
  481. if (!argc)
  482. return CMD_RET_USAGE;
  483. var_name = argv[0];
  484. if (default_guid) {
  485. if (!strcmp(var_name, "db") || !strcmp(var_name, "dbx") ||
  486. !strcmp(var_name, "dbt"))
  487. guid = efi_guid_image_security_database;
  488. else
  489. guid = efi_global_variable_guid;
  490. }
  491. if (verbose) {
  492. printf("GUID: %s\n", efi_guid_to_str((const efi_guid_t *)
  493. &guid));
  494. printf("Attributes: 0x%x\n", attributes);
  495. }
  496. /* for value */
  497. if (value_on_memory)
  498. value = map_sysmem(addr, 0);
  499. else if (argc > 1)
  500. for (argc--, argv++; argc > 0; argc--, argv++)
  501. if (append_value(&value, &size, argv[0]) < 0) {
  502. printf("## Failed to process an argument, %s\n",
  503. argv[0]);
  504. ret = CMD_RET_FAILURE;
  505. goto out;
  506. }
  507. if (size && verbose) {
  508. printf("Value:\n");
  509. print_hex_dump(" ", DUMP_PREFIX_OFFSET,
  510. 16, 1, value, size, true);
  511. }
  512. len = utf8_utf16_strnlen(var_name, strlen(var_name));
  513. var_name16 = malloc((len + 1) * 2);
  514. if (!var_name16) {
  515. printf("## Out of memory\n");
  516. ret = CMD_RET_FAILURE;
  517. goto out;
  518. }
  519. p = var_name16;
  520. utf8_utf16_strncpy(&p, var_name, len + 1);
  521. ret = EFI_CALL(efi_set_variable(var_name16, &guid, attributes,
  522. size, value));
  523. unmap_sysmem(value);
  524. if (ret == EFI_SUCCESS) {
  525. ret = CMD_RET_SUCCESS;
  526. } else {
  527. const char *msg;
  528. switch (ret) {
  529. case EFI_NOT_FOUND:
  530. msg = " (not found)";
  531. break;
  532. case EFI_WRITE_PROTECTED:
  533. msg = " (read only)";
  534. break;
  535. case EFI_INVALID_PARAMETER:
  536. msg = " (invalid parameter)";
  537. break;
  538. case EFI_SECURITY_VIOLATION:
  539. msg = " (validation failed)";
  540. break;
  541. case EFI_OUT_OF_RESOURCES:
  542. msg = " (out of memory)";
  543. break;
  544. default:
  545. msg = "";
  546. break;
  547. }
  548. printf("## Failed to set EFI variable%s\n", msg);
  549. ret = CMD_RET_FAILURE;
  550. }
  551. out:
  552. if (value_on_memory)
  553. unmap_sysmem(value);
  554. else
  555. free(value);
  556. free(var_name16);
  557. return ret;
  558. }