nvedit_efi.c 14 KB

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