efi_var_file.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * File interface for UEFI variables
  4. *
  5. * Copyright (c) 2020, Heinrich Schuchardt
  6. */
  7. #define LOG_CATEGORY LOGC_EFI
  8. #include <common.h>
  9. #include <charset.h>
  10. #include <fs.h>
  11. #include <log.h>
  12. #include <malloc.h>
  13. #include <mapmem.h>
  14. #include <efi_loader.h>
  15. #include <efi_variable.h>
  16. #include <u-boot/crc.h>
  17. #define PART_STR_LEN 10
  18. /**
  19. * efi_set_blk_dev_to_system_partition() - select EFI system partition
  20. *
  21. * Set the EFI system partition as current block device.
  22. *
  23. * Return: status code
  24. */
  25. static efi_status_t __maybe_unused efi_set_blk_dev_to_system_partition(void)
  26. {
  27. char part_str[PART_STR_LEN];
  28. int r;
  29. if (!efi_system_partition.if_type) {
  30. log_err("No EFI system partition\n");
  31. return EFI_DEVICE_ERROR;
  32. }
  33. snprintf(part_str, PART_STR_LEN, "%u:%u",
  34. efi_system_partition.devnum, efi_system_partition.part);
  35. r = fs_set_blk_dev(blk_get_if_type_name(efi_system_partition.if_type),
  36. part_str, FS_TYPE_ANY);
  37. if (r) {
  38. log_err("Cannot read EFI system partition\n");
  39. return EFI_DEVICE_ERROR;
  40. }
  41. return EFI_SUCCESS;
  42. }
  43. /**
  44. * efi_var_collect() - collect non-volatile variables in buffer
  45. *
  46. * A buffer is allocated and filled with all non-volatile variables in a
  47. * format ready to be written to disk.
  48. *
  49. * @bufp: pointer to pointer of buffer with collected variables
  50. * @lenp: pointer to length of buffer
  51. * Return: status code
  52. */
  53. static efi_status_t __maybe_unused efi_var_collect(struct efi_var_file **bufp,
  54. loff_t *lenp)
  55. {
  56. size_t len = EFI_VAR_BUF_SIZE;
  57. struct efi_var_file *buf;
  58. struct efi_var_entry *var, *old_var;
  59. size_t old_var_name_length = 2;
  60. *bufp = NULL; /* Avoid double free() */
  61. buf = calloc(1, len);
  62. if (!buf)
  63. return EFI_OUT_OF_RESOURCES;
  64. var = buf->var;
  65. old_var = var;
  66. for (;;) {
  67. efi_uintn_t data_length, var_name_length;
  68. u8 *data;
  69. efi_status_t ret;
  70. if ((uintptr_t)buf + len <=
  71. (uintptr_t)var->name + old_var_name_length)
  72. return EFI_BUFFER_TOO_SMALL;
  73. var_name_length = (uintptr_t)buf + len - (uintptr_t)var->name;
  74. memcpy(var->name, old_var->name, old_var_name_length);
  75. guidcpy(&var->guid, &old_var->guid);
  76. ret = efi_get_next_variable_name_int(
  77. &var_name_length, var->name, &var->guid);
  78. if (ret == EFI_NOT_FOUND)
  79. break;
  80. if (ret != EFI_SUCCESS) {
  81. free(buf);
  82. return ret;
  83. }
  84. old_var_name_length = var_name_length;
  85. old_var = var;
  86. data = (u8 *)var->name + old_var_name_length;
  87. data_length = (uintptr_t)buf + len - (uintptr_t)data;
  88. ret = efi_get_variable_int(var->name, &var->guid,
  89. &var->attr, &data_length, data,
  90. &var->time);
  91. if (ret != EFI_SUCCESS) {
  92. free(buf);
  93. return ret;
  94. }
  95. if (!(var->attr & EFI_VARIABLE_NON_VOLATILE))
  96. continue;
  97. var->length = data_length;
  98. var = (struct efi_var_entry *)
  99. ALIGN((uintptr_t)data + data_length, 8);
  100. }
  101. buf->reserved = 0;
  102. buf->magic = EFI_VAR_FILE_MAGIC;
  103. len = (uintptr_t)var - (uintptr_t)buf;
  104. buf->crc32 = crc32(0, (u8 *)buf->var,
  105. len - sizeof(struct efi_var_file));
  106. buf->length = len;
  107. *bufp = buf;
  108. *lenp = len;
  109. return EFI_SUCCESS;
  110. }
  111. /**
  112. * efi_var_to_file() - save non-volatile variables as file
  113. *
  114. * File ubootefi.var is created on the EFI system partion.
  115. *
  116. * Return: status code
  117. */
  118. efi_status_t efi_var_to_file(void)
  119. {
  120. #ifdef CONFIG_EFI_VARIABLE_FILE_STORE
  121. efi_status_t ret;
  122. struct efi_var_file *buf;
  123. loff_t len;
  124. loff_t actlen;
  125. int r;
  126. ret = efi_var_collect(&buf, &len);
  127. if (ret != EFI_SUCCESS)
  128. goto error;
  129. ret = efi_set_blk_dev_to_system_partition();
  130. if (ret != EFI_SUCCESS)
  131. goto error;
  132. r = fs_write(EFI_VAR_FILE_NAME, map_to_sysmem(buf), 0, len, &actlen);
  133. if (r || len != actlen)
  134. ret = EFI_DEVICE_ERROR;
  135. error:
  136. if (ret != EFI_SUCCESS)
  137. log_err("Failed to persist EFI variables\n");
  138. free(buf);
  139. return ret;
  140. #else
  141. return EFI_SUCCESS;
  142. #endif
  143. }
  144. /**
  145. * efi_var_restore() - restore EFI variables from buffer
  146. *
  147. * @buf: buffer
  148. * Return: status code
  149. */
  150. static efi_status_t __maybe_unused efi_var_restore(struct efi_var_file *buf)
  151. {
  152. struct efi_var_entry *var, *last_var;
  153. efi_status_t ret;
  154. if (buf->reserved || buf->magic != EFI_VAR_FILE_MAGIC ||
  155. buf->crc32 != crc32(0, (u8 *)buf->var,
  156. buf->length - sizeof(struct efi_var_file))) {
  157. log_err("Invalid EFI variables file\n");
  158. return EFI_INVALID_PARAMETER;
  159. }
  160. var = buf->var;
  161. last_var = (struct efi_var_entry *)((u8 *)buf + buf->length);
  162. while (var < last_var) {
  163. u16 *data = var->name + u16_strlen(var->name) + 1;
  164. if (var->attr & EFI_VARIABLE_NON_VOLATILE && var->length) {
  165. ret = efi_var_mem_ins(var->name, &var->guid, var->attr,
  166. var->length, data, 0, NULL,
  167. var->time);
  168. if (ret != EFI_SUCCESS)
  169. log_err("Failed to set EFI variable %ls\n",
  170. var->name);
  171. }
  172. var = (struct efi_var_entry *)
  173. ALIGN((uintptr_t)data + var->length, 8);
  174. }
  175. return EFI_SUCCESS;
  176. }
  177. /**
  178. * efi_var_from_file() - read variables from file
  179. *
  180. * File ubootefi.var is read from the EFI system partitions and the variables
  181. * stored in the file are created.
  182. *
  183. * In case the file does not exist yet or a variable cannot be set EFI_SUCCESS
  184. * is returned.
  185. *
  186. * Return: status code
  187. */
  188. efi_status_t efi_var_from_file(void)
  189. {
  190. #ifdef CONFIG_EFI_VARIABLE_FILE_STORE
  191. struct efi_var_file *buf;
  192. loff_t len;
  193. efi_status_t ret;
  194. int r;
  195. buf = calloc(1, EFI_VAR_BUF_SIZE);
  196. if (!buf) {
  197. log_err("Out of memory\n");
  198. return EFI_OUT_OF_RESOURCES;
  199. }
  200. ret = efi_set_blk_dev_to_system_partition();
  201. if (ret != EFI_SUCCESS)
  202. goto error;
  203. r = fs_read(EFI_VAR_FILE_NAME, map_to_sysmem(buf), 0, EFI_VAR_BUF_SIZE,
  204. &len);
  205. if (r || len < sizeof(struct efi_var_file)) {
  206. log_err("Failed to load EFI variables\n");
  207. goto error;
  208. }
  209. if (buf->length != len || efi_var_restore(buf) != EFI_SUCCESS)
  210. log_err("Invalid EFI variables file\n");
  211. error:
  212. free(buf);
  213. #endif
  214. return EFI_SUCCESS;
  215. }