nvedit_efi.c 13 KB

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