efi_var_file.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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, "%x:%x",
  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. efi_status_t __maybe_unused efi_var_collect(struct efi_var_file **bufp, loff_t *lenp,
  44. u32 check_attr_mask)
  45. {
  46. size_t len = EFI_VAR_BUF_SIZE;
  47. struct efi_var_file *buf;
  48. struct efi_var_entry *var, *old_var;
  49. size_t old_var_name_length = 2;
  50. *bufp = NULL; /* Avoid double free() */
  51. buf = calloc(1, len);
  52. if (!buf)
  53. return EFI_OUT_OF_RESOURCES;
  54. var = buf->var;
  55. old_var = var;
  56. for (;;) {
  57. efi_uintn_t data_length, var_name_length;
  58. u8 *data;
  59. efi_status_t ret;
  60. if ((uintptr_t)buf + len <=
  61. (uintptr_t)var->name + old_var_name_length)
  62. return EFI_BUFFER_TOO_SMALL;
  63. var_name_length = (uintptr_t)buf + len - (uintptr_t)var->name;
  64. memcpy(var->name, old_var->name, old_var_name_length);
  65. guidcpy(&var->guid, &old_var->guid);
  66. ret = efi_get_next_variable_name_int(
  67. &var_name_length, var->name, &var->guid);
  68. if (ret == EFI_NOT_FOUND)
  69. break;
  70. if (ret != EFI_SUCCESS) {
  71. free(buf);
  72. return ret;
  73. }
  74. old_var_name_length = var_name_length;
  75. old_var = var;
  76. data = (u8 *)var->name + old_var_name_length;
  77. data_length = (uintptr_t)buf + len - (uintptr_t)data;
  78. ret = efi_get_variable_int(var->name, &var->guid,
  79. &var->attr, &data_length, data,
  80. &var->time);
  81. if (ret != EFI_SUCCESS) {
  82. free(buf);
  83. return ret;
  84. }
  85. if ((var->attr & check_attr_mask) == check_attr_mask) {
  86. var->length = data_length;
  87. var = (struct efi_var_entry *)ALIGN((uintptr_t)data + data_length, 8);
  88. }
  89. }
  90. buf->reserved = 0;
  91. buf->magic = EFI_VAR_FILE_MAGIC;
  92. len = (uintptr_t)var - (uintptr_t)buf;
  93. buf->crc32 = crc32(0, (u8 *)buf->var,
  94. len - sizeof(struct efi_var_file));
  95. buf->length = len;
  96. *bufp = buf;
  97. *lenp = len;
  98. return EFI_SUCCESS;
  99. }
  100. /**
  101. * efi_var_to_file() - save non-volatile variables as file
  102. *
  103. * File ubootefi.var is created on the EFI system partion.
  104. *
  105. * Return: status code
  106. */
  107. efi_status_t efi_var_to_file(void)
  108. {
  109. #ifdef CONFIG_EFI_VARIABLE_FILE_STORE
  110. efi_status_t ret;
  111. struct efi_var_file *buf;
  112. loff_t len;
  113. loff_t actlen;
  114. int r;
  115. ret = efi_var_collect(&buf, &len, EFI_VARIABLE_NON_VOLATILE);
  116. if (ret != EFI_SUCCESS)
  117. goto error;
  118. ret = efi_set_blk_dev_to_system_partition();
  119. if (ret != EFI_SUCCESS)
  120. goto error;
  121. r = fs_write(EFI_VAR_FILE_NAME, map_to_sysmem(buf), 0, len, &actlen);
  122. if (r || len != actlen)
  123. ret = EFI_DEVICE_ERROR;
  124. error:
  125. if (ret != EFI_SUCCESS)
  126. log_err("Failed to persist EFI variables\n");
  127. free(buf);
  128. return ret;
  129. #else
  130. return EFI_SUCCESS;
  131. #endif
  132. }
  133. efi_status_t efi_var_restore(struct efi_var_file *buf, bool safe)
  134. {
  135. struct efi_var_entry *var, *last_var;
  136. u16 *data;
  137. efi_status_t ret;
  138. if (buf->reserved || buf->magic != EFI_VAR_FILE_MAGIC ||
  139. buf->crc32 != crc32(0, (u8 *)buf->var,
  140. buf->length - sizeof(struct efi_var_file))) {
  141. log_err("Invalid EFI variables file\n");
  142. return EFI_INVALID_PARAMETER;
  143. }
  144. last_var = (struct efi_var_entry *)((u8 *)buf + buf->length);
  145. for (var = buf->var; var < last_var;
  146. var = (struct efi_var_entry *)
  147. ALIGN((uintptr_t)data + var->length, 8)) {
  148. data = var->name + u16_strlen(var->name) + 1;
  149. /*
  150. * Secure boot related and non-volatile variables shall only be
  151. * restored from U-Boot's preseed.
  152. */
  153. if (!safe &&
  154. (efi_auth_var_get_type(var->name, &var->guid) !=
  155. EFI_AUTH_VAR_NONE ||
  156. !(var->attr & EFI_VARIABLE_NON_VOLATILE)))
  157. continue;
  158. if (!var->length)
  159. continue;
  160. ret = efi_var_mem_ins(var->name, &var->guid, var->attr,
  161. var->length, data, 0, NULL,
  162. var->time);
  163. if (ret != EFI_SUCCESS)
  164. log_err("Failed to set EFI variable %ls\n", var->name);
  165. }
  166. return EFI_SUCCESS;
  167. }
  168. /**
  169. * efi_var_from_file() - read variables from file
  170. *
  171. * File ubootefi.var is read from the EFI system partitions and the variables
  172. * stored in the file are created.
  173. *
  174. * In case the file does not exist yet or a variable cannot be set EFI_SUCCESS
  175. * is returned.
  176. *
  177. * Return: status code
  178. */
  179. efi_status_t efi_var_from_file(void)
  180. {
  181. #ifdef CONFIG_EFI_VARIABLE_FILE_STORE
  182. struct efi_var_file *buf;
  183. loff_t len;
  184. efi_status_t ret;
  185. int r;
  186. buf = calloc(1, EFI_VAR_BUF_SIZE);
  187. if (!buf) {
  188. log_err("Out of memory\n");
  189. return EFI_OUT_OF_RESOURCES;
  190. }
  191. ret = efi_set_blk_dev_to_system_partition();
  192. if (ret != EFI_SUCCESS)
  193. goto error;
  194. r = fs_read(EFI_VAR_FILE_NAME, map_to_sysmem(buf), 0, EFI_VAR_BUF_SIZE,
  195. &len);
  196. if (r || len < sizeof(struct efi_var_file)) {
  197. log_err("Failed to load EFI variables\n");
  198. goto error;
  199. }
  200. if (buf->length != len || efi_var_restore(buf, false) != EFI_SUCCESS)
  201. log_err("Invalid EFI variables file\n");
  202. error:
  203. free(buf);
  204. #endif
  205. return EFI_SUCCESS;
  206. }