nvedit_efi.c 14 KB

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