nvedit_efi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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_FAILURE;
  169. }
  170. return CMD_RET_SUCCESS;
  171. }
  172. /**
  173. * do_env_print_efi() - show information about UEFI variables
  174. *
  175. * @cmdtp: Command table
  176. * @flag: Command flag
  177. * @argc: Number of arguments
  178. * @argv: Argument array
  179. * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
  180. *
  181. * This function is for "env print -e" or "printenv -e" command:
  182. * => env print -e [-n] [-guid <guid> | -all] [var [...]]
  183. * If one or more variable names are specified, show information
  184. * named UEFI variables, otherwise show all the UEFI variables.
  185. */
  186. int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc,
  187. char *const argv[])
  188. {
  189. const efi_guid_t *guid_p = NULL;
  190. efi_guid_t guid;
  191. bool verbose = true;
  192. efi_status_t ret;
  193. /* Initialize EFI drivers */
  194. ret = efi_init_obj_list();
  195. if (ret != EFI_SUCCESS) {
  196. printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
  197. ret & ~EFI_ERROR_MASK);
  198. return CMD_RET_FAILURE;
  199. }
  200. for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  201. if (!strcmp(argv[0], "-guid")) {
  202. if (argc == 1)
  203. return CMD_RET_USAGE;
  204. argc--;
  205. argv++;
  206. if (uuid_str_to_bin(argv[0], guid.b,
  207. UUID_STR_FORMAT_GUID))
  208. return CMD_RET_USAGE;
  209. guid_p = (const efi_guid_t *)guid.b;
  210. } else if (!strcmp(argv[0], "-n")) {
  211. verbose = false;
  212. } else {
  213. return CMD_RET_USAGE;
  214. }
  215. }
  216. /* enumerate and show all UEFI variables */
  217. return efi_dump_var_all(argc, argv, guid_p, verbose);
  218. }
  219. /**
  220. * append_value() - encode UEFI variable's value
  221. * @bufp: Buffer of encoded UEFI variable's value
  222. * @sizep: Size of buffer
  223. * @data: data to be encoded into the value
  224. * Return: 0 on success, -1 otherwise
  225. *
  226. * Interpret a given data string and append it to buffer.
  227. * Buffer will be realloc'ed if necessary.
  228. *
  229. * Currently supported formats are:
  230. * =0x0123...: Hexadecimal number
  231. * =H0123...: Hexadecimal-byte array
  232. * ="...", =S"..." or <string>:
  233. * String
  234. */
  235. static int append_value(char **bufp, size_t *sizep, char *data)
  236. {
  237. char *tmp_buf = NULL, *new_buf = NULL, *value;
  238. unsigned long len = 0;
  239. if (!strncmp(data, "=0x", 2)) { /* hexadecimal number */
  240. union {
  241. u8 u8;
  242. u16 u16;
  243. u32 u32;
  244. u64 u64;
  245. } tmp_data;
  246. unsigned long hex_value;
  247. void *hex_ptr;
  248. data += 3;
  249. len = strlen(data);
  250. if ((len & 0x1)) /* not multiple of two */
  251. return -1;
  252. len /= 2;
  253. if (len > 8)
  254. return -1;
  255. else if (len > 4)
  256. len = 8;
  257. else if (len > 2)
  258. len = 4;
  259. /* convert hex hexadecimal number */
  260. if (strict_strtoul(data, 16, &hex_value) < 0)
  261. return -1;
  262. tmp_buf = malloc(len);
  263. if (!tmp_buf)
  264. return -1;
  265. if (len == 1) {
  266. tmp_data.u8 = hex_value;
  267. hex_ptr = &tmp_data.u8;
  268. } else if (len == 2) {
  269. tmp_data.u16 = hex_value;
  270. hex_ptr = &tmp_data.u16;
  271. } else if (len == 4) {
  272. tmp_data.u32 = hex_value;
  273. hex_ptr = &tmp_data.u32;
  274. } else {
  275. tmp_data.u64 = hex_value;
  276. hex_ptr = &tmp_data.u64;
  277. }
  278. memcpy(tmp_buf, hex_ptr, len);
  279. value = tmp_buf;
  280. } else if (!strncmp(data, "=H", 2)) { /* hexadecimal-byte array */
  281. data += 2;
  282. len = strlen(data);
  283. if (len & 0x1) /* not multiple of two */
  284. return -1;
  285. len /= 2;
  286. tmp_buf = malloc(len);
  287. if (!tmp_buf)
  288. return -1;
  289. if (hex2bin((u8 *)tmp_buf, data, len) < 0) {
  290. printf("Error: illegal hexadecimal string\n");
  291. free(tmp_buf);
  292. return -1;
  293. }
  294. value = tmp_buf;
  295. } else { /* string */
  296. if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) {
  297. if (data[1] == '"')
  298. data += 2;
  299. else
  300. data += 3;
  301. value = data;
  302. len = strlen(data) - 1;
  303. if (data[len] != '"')
  304. return -1;
  305. } else {
  306. value = data;
  307. len = strlen(data);
  308. }
  309. }
  310. new_buf = realloc(*bufp, *sizep + len);
  311. if (!new_buf)
  312. goto out;
  313. memcpy(new_buf + *sizep, value, len);
  314. *bufp = new_buf;
  315. *sizep += len;
  316. out:
  317. free(tmp_buf);
  318. return 0;
  319. }
  320. /**
  321. * do_env_set_efi() - set UEFI variable
  322. *
  323. * @cmdtp: Command table
  324. * @flag: Command flag
  325. * @argc: Number of arguments
  326. * @argv: Argument array
  327. * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
  328. *
  329. * This function is for "env set -e" or "setenv -e" command:
  330. * => env set -e [-guid guid][-nv][-bs][-rt][-at][-a][-v]
  331. * [-i address,size] var, or
  332. * var [value ...]
  333. * Encode values specified and set given UEFI variable.
  334. * If no value is specified, delete the variable.
  335. */
  336. int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
  337. char *const argv[])
  338. {
  339. char *var_name, *value, *ep;
  340. ulong addr;
  341. efi_uintn_t size;
  342. efi_guid_t guid;
  343. u32 attributes;
  344. bool default_guid, verbose, value_on_memory;
  345. u16 *var_name16;
  346. efi_status_t ret;
  347. if (argc == 1)
  348. return CMD_RET_USAGE;
  349. /* Initialize EFI drivers */
  350. ret = efi_init_obj_list();
  351. if (ret != EFI_SUCCESS) {
  352. printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
  353. ret & ~EFI_ERROR_MASK);
  354. return CMD_RET_FAILURE;
  355. }
  356. /*
  357. * attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
  358. * EFI_VARIABLE_RUNTIME_ACCESS;
  359. */
  360. value = NULL;
  361. size = 0;
  362. attributes = 0;
  363. guid = efi_global_variable_guid;
  364. default_guid = true;
  365. verbose = false;
  366. value_on_memory = false;
  367. for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  368. if (!strcmp(argv[0], "-guid")) {
  369. if (argc == 1)
  370. return CMD_RET_USAGE;
  371. argc--;
  372. argv++;
  373. if (uuid_str_to_bin(argv[0], guid.b,
  374. UUID_STR_FORMAT_GUID)) {
  375. return CMD_RET_USAGE;
  376. }
  377. default_guid = false;
  378. } else if (!strcmp(argv[0], "-bs")) {
  379. attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
  380. } else if (!strcmp(argv[0], "-rt")) {
  381. attributes |= EFI_VARIABLE_RUNTIME_ACCESS;
  382. } else if (!strcmp(argv[0], "-nv")) {
  383. attributes |= EFI_VARIABLE_NON_VOLATILE;
  384. } else if (!strcmp(argv[0], "-at")) {
  385. attributes |=
  386. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
  387. } else if (!strcmp(argv[0], "-a")) {
  388. attributes |= EFI_VARIABLE_APPEND_WRITE;
  389. } else if (!strcmp(argv[0], "-i")) {
  390. /* data comes from memory */
  391. if (argc == 1)
  392. return CMD_RET_USAGE;
  393. argc--;
  394. argv++;
  395. addr = hextoul(argv[0], &ep);
  396. if (*ep != ':')
  397. return CMD_RET_USAGE;
  398. /* 0 should be allowed for delete */
  399. size = hextoul(++ep, NULL);
  400. value_on_memory = true;
  401. } else if (!strcmp(argv[0], "-v")) {
  402. verbose = true;
  403. } else {
  404. return CMD_RET_USAGE;
  405. }
  406. }
  407. if (!argc)
  408. return CMD_RET_USAGE;
  409. var_name = argv[0];
  410. if (default_guid) {
  411. if (!strcmp(var_name, "db") || !strcmp(var_name, "dbx") ||
  412. !strcmp(var_name, "dbt"))
  413. guid = efi_guid_image_security_database;
  414. else
  415. guid = efi_global_variable_guid;
  416. }
  417. if (verbose) {
  418. printf("GUID: %pUl (%pUs)\n", &guid, &guid);
  419. printf("Attributes: 0x%x\n", attributes);
  420. }
  421. /* for value */
  422. if (value_on_memory)
  423. value = map_sysmem(addr, 0);
  424. else if (argc > 1)
  425. for (argc--, argv++; argc > 0; argc--, argv++)
  426. if (append_value(&value, &size, argv[0]) < 0) {
  427. printf("## Failed to process an argument, %s\n",
  428. argv[0]);
  429. ret = CMD_RET_FAILURE;
  430. goto out;
  431. }
  432. if (size && verbose) {
  433. printf("Value:\n");
  434. print_hex_dump(" ", DUMP_PREFIX_OFFSET,
  435. 16, 1, value, size, true);
  436. }
  437. var_name16 = efi_convert_string(var_name);
  438. if (!var_name16) {
  439. printf("## Out of memory\n");
  440. ret = CMD_RET_FAILURE;
  441. goto out;
  442. }
  443. ret = efi_set_variable_int(var_name16, &guid, attributes, size, value,
  444. true);
  445. free(var_name16);
  446. unmap_sysmem(value);
  447. if (ret == EFI_SUCCESS) {
  448. ret = CMD_RET_SUCCESS;
  449. } else {
  450. const char *msg;
  451. switch (ret) {
  452. case EFI_NOT_FOUND:
  453. msg = " (not found)";
  454. break;
  455. case EFI_WRITE_PROTECTED:
  456. msg = " (read only)";
  457. break;
  458. case EFI_INVALID_PARAMETER:
  459. msg = " (invalid parameter)";
  460. break;
  461. case EFI_SECURITY_VIOLATION:
  462. msg = " (validation failed)";
  463. break;
  464. case EFI_OUT_OF_RESOURCES:
  465. msg = " (out of memory)";
  466. break;
  467. default:
  468. msg = "";
  469. break;
  470. }
  471. printf("## Failed to set EFI variable%s\n", msg);
  472. ret = CMD_RET_FAILURE;
  473. }
  474. out:
  475. if (value_on_memory)
  476. unmap_sysmem(value);
  477. else
  478. free(value);
  479. return ret;
  480. }