nvedit_efi.c 13 KB

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