nvedit_efi.c 14 KB

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