nvedit_efi.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 <linux/kernel.h>
  16. /*
  17. * From efi_variable.c,
  18. *
  19. * Mapping between UEFI variables and u-boot variables:
  20. *
  21. * efi_$guid_$varname = {attributes}(type)value
  22. */
  23. static const struct {
  24. u32 mask;
  25. char *text;
  26. } efi_var_attrs[] = {
  27. {EFI_VARIABLE_NON_VOLATILE, "NV"},
  28. {EFI_VARIABLE_BOOTSERVICE_ACCESS, "BS"},
  29. {EFI_VARIABLE_RUNTIME_ACCESS, "RT"},
  30. {EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, "AW"},
  31. {EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, "AT"},
  32. };
  33. /**
  34. * efi_dump_single_var() - show information about a UEFI variable
  35. *
  36. * @name: Name of the variable
  37. * @guid: Vendor GUID
  38. *
  39. * Show information encoded in one UEFI variable
  40. */
  41. static void efi_dump_single_var(u16 *name, efi_guid_t *guid)
  42. {
  43. u32 attributes;
  44. u8 *data;
  45. efi_uintn_t size;
  46. int count, i;
  47. efi_status_t ret;
  48. data = NULL;
  49. size = 0;
  50. ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size, data));
  51. if (ret == EFI_BUFFER_TOO_SMALL) {
  52. data = malloc(size);
  53. if (!data)
  54. goto out;
  55. ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size,
  56. data));
  57. }
  58. if (ret == EFI_NOT_FOUND) {
  59. printf("Error: \"%ls\" not defined\n", name);
  60. goto out;
  61. }
  62. if (ret != EFI_SUCCESS)
  63. goto out;
  64. printf("%ls:", name);
  65. for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++)
  66. if (attributes & efi_var_attrs[i].mask) {
  67. if (count)
  68. putc('|');
  69. else
  70. putc(' ');
  71. count++;
  72. puts(efi_var_attrs[i].text);
  73. }
  74. printf(", DataSize = 0x%zx\n", size);
  75. print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, data, size, true);
  76. out:
  77. free(data);
  78. }
  79. /**
  80. * efi_dump_vars() - show information about named UEFI variables
  81. *
  82. * @argc: Number of arguments (variables)
  83. * @argv: Argument (variable name) array
  84. * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
  85. *
  86. * Show information encoded in named UEFI variables
  87. */
  88. static int efi_dump_vars(int argc, char * const argv[])
  89. {
  90. u16 *var_name16, *p;
  91. efi_uintn_t buf_size, size;
  92. buf_size = 128;
  93. var_name16 = malloc(buf_size);
  94. if (!var_name16)
  95. return CMD_RET_FAILURE;
  96. for (; argc > 0; argc--, argv++) {
  97. size = (utf8_utf16_strlen(argv[0]) + 1) * sizeof(u16);
  98. if (buf_size < size) {
  99. buf_size = size;
  100. p = realloc(var_name16, buf_size);
  101. if (!p) {
  102. free(var_name16);
  103. return CMD_RET_FAILURE;
  104. }
  105. var_name16 = p;
  106. }
  107. p = var_name16;
  108. utf8_utf16_strcpy(&p, argv[0]);
  109. efi_dump_single_var(var_name16,
  110. (efi_guid_t *)&efi_global_variable_guid);
  111. }
  112. free(var_name16);
  113. return CMD_RET_SUCCESS;
  114. }
  115. /**
  116. * efi_dump_vars() - show information about all the UEFI variables
  117. *
  118. * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
  119. *
  120. * Show information encoded in all the UEFI variables
  121. */
  122. static int efi_dump_var_all(void)
  123. {
  124. u16 *var_name16, *p;
  125. efi_uintn_t buf_size, size;
  126. efi_guid_t guid;
  127. efi_status_t ret;
  128. buf_size = 128;
  129. var_name16 = malloc(buf_size);
  130. if (!var_name16)
  131. return CMD_RET_FAILURE;
  132. var_name16[0] = 0;
  133. for (;;) {
  134. size = buf_size;
  135. ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
  136. &guid));
  137. if (ret == EFI_NOT_FOUND)
  138. break;
  139. if (ret == EFI_BUFFER_TOO_SMALL) {
  140. buf_size = size;
  141. p = realloc(var_name16, buf_size);
  142. if (!p) {
  143. free(var_name16);
  144. return CMD_RET_FAILURE;
  145. }
  146. var_name16 = p;
  147. ret = EFI_CALL(efi_get_next_variable_name(&size,
  148. var_name16,
  149. &guid));
  150. }
  151. if (ret != EFI_SUCCESS) {
  152. free(var_name16);
  153. return CMD_RET_FAILURE;
  154. }
  155. efi_dump_single_var(var_name16, &guid);
  156. }
  157. free(var_name16);
  158. return CMD_RET_SUCCESS;
  159. }
  160. /**
  161. * do_env_print_efi() - show information about UEFI variables
  162. *
  163. * @cmdtp: Command table
  164. * @flag: Command flag
  165. * @argc: Number of arguments
  166. * @argv: Argument array
  167. * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
  168. *
  169. * This function is for "env print -e" or "printenv -e" command:
  170. * => env print -e [var [...]]
  171. * If one or more variable names are specified, show information
  172. * named UEFI variables, otherwise show all the UEFI variables.
  173. */
  174. int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  175. {
  176. efi_status_t ret;
  177. /* Initialize EFI drivers */
  178. ret = efi_init_obj_list();
  179. if (ret != EFI_SUCCESS) {
  180. printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
  181. ret & ~EFI_ERROR_MASK);
  182. return CMD_RET_FAILURE;
  183. }
  184. if (argc > 1)
  185. /* show specified UEFI variables */
  186. return efi_dump_vars(--argc, ++argv);
  187. /* enumerate and show all UEFI variables */
  188. return efi_dump_var_all();
  189. }
  190. /**
  191. * append_value() - encode UEFI variable's value
  192. * @bufp: Buffer of encoded UEFI variable's value
  193. * @sizep: Size of buffer
  194. * @data: data to be encoded into the value
  195. * Return: 0 on success, -1 otherwise
  196. *
  197. * Interpret a given data string and append it to buffer.
  198. * Buffer will be realloc'ed if necessary.
  199. *
  200. * Currently supported formats are:
  201. * =0x0123...: Hexadecimal number
  202. * =H0123...: Hexadecimal-byte array
  203. * ="...", =S"..." or <string>:
  204. * String
  205. */
  206. static int append_value(char **bufp, size_t *sizep, char *data)
  207. {
  208. char *tmp_buf = NULL, *new_buf = NULL, *value;
  209. unsigned long len = 0;
  210. if (!strncmp(data, "=0x", 2)) { /* hexadecimal number */
  211. union {
  212. u8 u8;
  213. u16 u16;
  214. u32 u32;
  215. u64 u64;
  216. } tmp_data;
  217. unsigned long hex_value;
  218. void *hex_ptr;
  219. data += 3;
  220. len = strlen(data);
  221. if ((len & 0x1)) /* not multiple of two */
  222. return -1;
  223. len /= 2;
  224. if (len > 8)
  225. return -1;
  226. else if (len > 4)
  227. len = 8;
  228. else if (len > 2)
  229. len = 4;
  230. /* convert hex hexadecimal number */
  231. if (strict_strtoul(data, 16, &hex_value) < 0)
  232. return -1;
  233. tmp_buf = malloc(len);
  234. if (!tmp_buf)
  235. return -1;
  236. if (len == 1) {
  237. tmp_data.u8 = hex_value;
  238. hex_ptr = &tmp_data.u8;
  239. } else if (len == 2) {
  240. tmp_data.u16 = hex_value;
  241. hex_ptr = &tmp_data.u16;
  242. } else if (len == 4) {
  243. tmp_data.u32 = hex_value;
  244. hex_ptr = &tmp_data.u32;
  245. } else {
  246. tmp_data.u64 = hex_value;
  247. hex_ptr = &tmp_data.u64;
  248. }
  249. memcpy(tmp_buf, hex_ptr, len);
  250. value = tmp_buf;
  251. } else if (!strncmp(data, "=H", 2)) { /* hexadecimal-byte array */
  252. data += 2;
  253. len = strlen(data);
  254. if (len & 0x1) /* not multiple of two */
  255. return -1;
  256. len /= 2;
  257. tmp_buf = malloc(len);
  258. if (!tmp_buf)
  259. return -1;
  260. if (hex2bin((u8 *)tmp_buf, data, len) < 0) {
  261. printf("Error: illegal hexadecimal string\n");
  262. free(tmp_buf);
  263. return -1;
  264. }
  265. value = tmp_buf;
  266. } else { /* string */
  267. if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) {
  268. if (data[1] == '"')
  269. data += 2;
  270. else
  271. data += 3;
  272. value = data;
  273. len = strlen(data) - 1;
  274. if (data[len] != '"')
  275. return -1;
  276. } else {
  277. value = data;
  278. len = strlen(data);
  279. }
  280. }
  281. new_buf = realloc(*bufp, *sizep + len);
  282. if (!new_buf)
  283. goto out;
  284. memcpy(new_buf + *sizep, value, len);
  285. *bufp = new_buf;
  286. *sizep += len;
  287. out:
  288. free(tmp_buf);
  289. return 0;
  290. }
  291. /**
  292. * do_env_set_efi() - set UEFI variable
  293. *
  294. * @cmdtp: Command table
  295. * @flag: Command flag
  296. * @argc: Number of arguments
  297. * @argv: Argument array
  298. * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
  299. *
  300. * This function is for "env set -e" or "setenv -e" command:
  301. * => env set -e var [value ...]]
  302. * Encode values specified and set given UEFI variable.
  303. * If no value is specified, delete the variable.
  304. */
  305. int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  306. {
  307. char *var_name, *value = NULL;
  308. efi_uintn_t size = 0;
  309. u16 *var_name16 = NULL, *p;
  310. size_t len;
  311. efi_guid_t guid;
  312. u32 attributes;
  313. efi_status_t ret;
  314. if (argc == 1)
  315. return CMD_RET_USAGE;
  316. /* Initialize EFI drivers */
  317. ret = efi_init_obj_list();
  318. if (ret != EFI_SUCCESS) {
  319. printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
  320. ret & ~EFI_ERROR_MASK);
  321. return CMD_RET_FAILURE;
  322. }
  323. attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
  324. EFI_VARIABLE_RUNTIME_ACCESS;
  325. if (!strcmp(argv[1], "-nv")) {
  326. attributes |= EFI_VARIABLE_NON_VOLATILE;
  327. argc--;
  328. argv++;
  329. if (argc == 1)
  330. return CMD_RET_SUCCESS;
  331. }
  332. var_name = argv[1];
  333. if (argc == 2) {
  334. /* delete */
  335. value = NULL;
  336. size = 0;
  337. } else { /* set */
  338. argc -= 2;
  339. argv += 2;
  340. for ( ; argc > 0; argc--, argv++)
  341. if (append_value(&value, &size, argv[0]) < 0) {
  342. printf("## Failed to process an argument, %s\n",
  343. argv[0]);
  344. ret = CMD_RET_FAILURE;
  345. goto out;
  346. }
  347. }
  348. len = utf8_utf16_strnlen(var_name, strlen(var_name));
  349. var_name16 = malloc((len + 1) * 2);
  350. if (!var_name16) {
  351. printf("## Out of memory\n");
  352. ret = CMD_RET_FAILURE;
  353. goto out;
  354. }
  355. p = var_name16;
  356. utf8_utf16_strncpy(&p, var_name, len + 1);
  357. guid = efi_global_variable_guid;
  358. ret = EFI_CALL(efi_set_variable(var_name16, &guid, attributes,
  359. size, value));
  360. if (ret == EFI_SUCCESS) {
  361. ret = CMD_RET_SUCCESS;
  362. } else {
  363. printf("## Failed to set EFI variable\n");
  364. ret = CMD_RET_FAILURE;
  365. }
  366. out:
  367. free(value);
  368. free(var_name16);
  369. return ret;
  370. }