nvedit_efi.c 9.0 KB

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