nvedit_efi.c 12 KB

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