efi_var_mem.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * File interface for UEFI variables
  4. *
  5. * Copyright (c) 2020, Heinrich Schuchardt
  6. */
  7. #include <common.h>
  8. #include <efi_loader.h>
  9. #include <efi_variable.h>
  10. #include <u-boot/crc.h>
  11. /*
  12. * The variables efi_var_file and efi_var_entry must be static to avoid
  13. * referencing them via the global offset table (section .got). The GOT
  14. * is neither mapped as EfiRuntimeServicesData nor do we support its
  15. * relocation during SetVirtualAddressMap().
  16. */
  17. static struct efi_var_file __efi_runtime_data *efi_var_buf;
  18. static struct efi_var_entry __efi_runtime_data *efi_current_var;
  19. /**
  20. * efi_var_mem_compare() - compare GUID and name with a variable
  21. *
  22. * @var: variable to compare
  23. * @guid: GUID to compare
  24. * @name: variable name to compare
  25. * @next: pointer to next variable
  26. * Return: true if match
  27. */
  28. static bool __efi_runtime
  29. efi_var_mem_compare(struct efi_var_entry *var, const efi_guid_t *guid,
  30. const u16 *name, struct efi_var_entry **next)
  31. {
  32. int i;
  33. u8 *guid1, *guid2;
  34. const u16 *data, *var_name;
  35. bool match = true;
  36. for (guid1 = (u8 *)&var->guid, guid2 = (u8 *)guid, i = 0;
  37. i < sizeof(efi_guid_t) && match; ++i)
  38. match = (guid1[i] == guid2[i]);
  39. for (data = var->name, var_name = name;; ++data, ++var_name) {
  40. if (match)
  41. match = (*data == *var_name);
  42. if (!*data)
  43. break;
  44. }
  45. ++data;
  46. if (next)
  47. *next = (struct efi_var_entry *)
  48. ALIGN((uintptr_t)data + var->length, 8);
  49. if (match)
  50. efi_current_var = var;
  51. return match;
  52. }
  53. struct efi_var_entry __efi_runtime
  54. *efi_var_mem_find(const efi_guid_t *guid, const u16 *name,
  55. struct efi_var_entry **next)
  56. {
  57. struct efi_var_entry *var, *last;
  58. last = (struct efi_var_entry *)
  59. ((uintptr_t)efi_var_buf + efi_var_buf->length);
  60. if (!*name) {
  61. if (next) {
  62. *next = efi_var_buf->var;
  63. if (*next >= last)
  64. *next = NULL;
  65. }
  66. return NULL;
  67. }
  68. if (efi_current_var &&
  69. efi_var_mem_compare(efi_current_var, guid, name, next)) {
  70. if (next && *next >= last)
  71. *next = NULL;
  72. return efi_current_var;
  73. }
  74. var = efi_var_buf->var;
  75. if (var < last) {
  76. for (; var;) {
  77. struct efi_var_entry *pos;
  78. bool match;
  79. match = efi_var_mem_compare(var, guid, name, &pos);
  80. if (pos >= last)
  81. pos = NULL;
  82. if (match) {
  83. if (next)
  84. *next = pos;
  85. return var;
  86. }
  87. var = pos;
  88. }
  89. }
  90. if (next)
  91. *next = NULL;
  92. return NULL;
  93. }
  94. void __efi_runtime efi_var_mem_del(struct efi_var_entry *var)
  95. {
  96. u16 *data;
  97. struct efi_var_entry *next, *last;
  98. if (!var)
  99. return;
  100. last = (struct efi_var_entry *)
  101. ((uintptr_t)efi_var_buf + efi_var_buf->length);
  102. if (var <= efi_current_var)
  103. efi_current_var = NULL;
  104. for (data = var->name; *data; ++data)
  105. ;
  106. ++data;
  107. next = (struct efi_var_entry *)
  108. ALIGN((uintptr_t)data + var->length, 8);
  109. efi_var_buf->length -= (uintptr_t)next - (uintptr_t)var;
  110. /* efi_memcpy_runtime() can be used because next >= var. */
  111. efi_memcpy_runtime(var, next, (uintptr_t)last - (uintptr_t)next);
  112. efi_var_buf->crc32 = crc32(0, (u8 *)efi_var_buf->var,
  113. efi_var_buf->length -
  114. sizeof(struct efi_var_file));
  115. }
  116. efi_status_t __efi_runtime efi_var_mem_ins(
  117. u16 *variable_name,
  118. const efi_guid_t *vendor, u32 attributes,
  119. const efi_uintn_t size1, const void *data1,
  120. const efi_uintn_t size2, const void *data2,
  121. const u64 time)
  122. {
  123. u16 *data;
  124. struct efi_var_entry *var;
  125. u32 var_name_len;
  126. var = (struct efi_var_entry *)
  127. ((uintptr_t)efi_var_buf + efi_var_buf->length);
  128. for (var_name_len = 0; variable_name[var_name_len]; ++var_name_len)
  129. ;
  130. ++var_name_len;
  131. data = var->name + var_name_len;
  132. if ((uintptr_t)data - (uintptr_t)efi_var_buf + size1 + size2 >
  133. EFI_VAR_BUF_SIZE)
  134. return EFI_OUT_OF_RESOURCES;
  135. var->attr = attributes;
  136. var->length = size1 + size2;
  137. var->time = time;
  138. efi_memcpy_runtime(&var->guid, vendor, sizeof(efi_guid_t));
  139. efi_memcpy_runtime(var->name, variable_name,
  140. sizeof(u16) * var_name_len);
  141. efi_memcpy_runtime(data, data1, size1);
  142. efi_memcpy_runtime((u8 *)data + size1, data2, size2);
  143. var = (struct efi_var_entry *)
  144. ALIGN((uintptr_t)data + var->length, 8);
  145. efi_var_buf->length = (uintptr_t)var - (uintptr_t)efi_var_buf;
  146. efi_var_buf->crc32 = crc32(0, (u8 *)efi_var_buf->var,
  147. efi_var_buf->length -
  148. sizeof(struct efi_var_file));
  149. return EFI_SUCCESS;
  150. }
  151. u64 __efi_runtime efi_var_mem_free(void)
  152. {
  153. return EFI_VAR_BUF_SIZE - efi_var_buf->length -
  154. sizeof(struct efi_var_entry);
  155. }
  156. /**
  157. * efi_var_mem_bs_del() - delete boot service only variables
  158. */
  159. static void efi_var_mem_bs_del(void)
  160. {
  161. struct efi_var_entry *var = efi_var_buf->var;
  162. for (;;) {
  163. struct efi_var_entry *last;
  164. last = (struct efi_var_entry *)
  165. ((uintptr_t)efi_var_buf + efi_var_buf->length);
  166. if (var >= last)
  167. break;
  168. if (var->attr & EFI_VARIABLE_RUNTIME_ACCESS) {
  169. u16 *data;
  170. /* skip variable */
  171. for (data = var->name; *data; ++data)
  172. ;
  173. ++data;
  174. var = (struct efi_var_entry *)
  175. ALIGN((uintptr_t)data + var->length, 8);
  176. } else {
  177. /* delete variable */
  178. efi_var_mem_del(var);
  179. }
  180. }
  181. }
  182. /**
  183. * efi_var_mem_notify_exit_boot_services() - ExitBootService callback
  184. *
  185. * @event: callback event
  186. * @context: callback context
  187. */
  188. static void EFIAPI
  189. efi_var_mem_notify_exit_boot_services(struct efi_event *event, void *context)
  190. {
  191. EFI_ENTRY("%p, %p", event, context);
  192. /* Delete boot service only variables */
  193. efi_var_mem_bs_del();
  194. EFI_EXIT(EFI_SUCCESS);
  195. }
  196. /**
  197. * efi_var_mem_notify_exit_boot_services() - SetVirtualMemoryMap callback
  198. *
  199. * @event: callback event
  200. * @context: callback context
  201. */
  202. static void EFIAPI __efi_runtime
  203. efi_var_mem_notify_virtual_address_map(struct efi_event *event, void *context)
  204. {
  205. efi_convert_pointer(0, (void **)&efi_var_buf);
  206. efi_current_var = NULL;
  207. }
  208. efi_status_t efi_var_mem_init(void)
  209. {
  210. u64 memory;
  211. efi_status_t ret;
  212. struct efi_event *event;
  213. ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
  214. EFI_RUNTIME_SERVICES_DATA,
  215. efi_size_in_pages(EFI_VAR_BUF_SIZE),
  216. &memory);
  217. if (ret != EFI_SUCCESS)
  218. return ret;
  219. efi_var_buf = (struct efi_var_file *)(uintptr_t)memory;
  220. memset(efi_var_buf, 0, EFI_VAR_BUF_SIZE);
  221. efi_var_buf->magic = EFI_VAR_FILE_MAGIC;
  222. efi_var_buf->length = (uintptr_t)efi_var_buf->var -
  223. (uintptr_t)efi_var_buf;
  224. /* crc32 for 0 bytes = 0 */
  225. ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
  226. efi_var_mem_notify_exit_boot_services, NULL,
  227. NULL, &event);
  228. if (ret != EFI_SUCCESS)
  229. return ret;
  230. ret = efi_create_event(EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE, TPL_CALLBACK,
  231. efi_var_mem_notify_virtual_address_map, NULL,
  232. NULL, &event);
  233. if (ret != EFI_SUCCESS)
  234. return ret;
  235. return ret;
  236. }
  237. efi_status_t __efi_runtime
  238. efi_get_variable_mem(u16 *variable_name, const efi_guid_t *vendor, u32 *attributes,
  239. efi_uintn_t *data_size, void *data, u64 *timep)
  240. {
  241. efi_uintn_t old_size;
  242. struct efi_var_entry *var;
  243. u16 *pdata;
  244. if (!variable_name || !vendor || !data_size)
  245. return EFI_INVALID_PARAMETER;
  246. var = efi_var_mem_find(vendor, variable_name, NULL);
  247. if (!var)
  248. return EFI_NOT_FOUND;
  249. if (attributes)
  250. *attributes = var->attr;
  251. if (timep)
  252. *timep = var->time;
  253. old_size = *data_size;
  254. *data_size = var->length;
  255. if (old_size < var->length)
  256. return EFI_BUFFER_TOO_SMALL;
  257. if (!data)
  258. return EFI_INVALID_PARAMETER;
  259. for (pdata = var->name; *pdata; ++pdata)
  260. ;
  261. ++pdata;
  262. efi_memcpy_runtime(data, pdata, var->length);
  263. return EFI_SUCCESS;
  264. }
  265. efi_status_t __efi_runtime
  266. efi_get_next_variable_name_mem(efi_uintn_t *variable_name_size,
  267. u16 *variable_name, efi_guid_t *vendor)
  268. {
  269. struct efi_var_entry *var;
  270. efi_uintn_t old_size;
  271. u16 *pdata;
  272. if (!variable_name_size || !variable_name || !vendor)
  273. return EFI_INVALID_PARAMETER;
  274. if (u16_strnlen(variable_name, *variable_name_size) ==
  275. *variable_name_size)
  276. return EFI_INVALID_PARAMETER;
  277. if (!efi_var_mem_find(vendor, variable_name, &var) && *variable_name)
  278. return EFI_INVALID_PARAMETER;
  279. if (!var)
  280. return EFI_NOT_FOUND;
  281. for (pdata = var->name; *pdata; ++pdata)
  282. ;
  283. ++pdata;
  284. old_size = *variable_name_size;
  285. *variable_name_size = (uintptr_t)pdata - (uintptr_t)var->name;
  286. if (old_size < *variable_name_size)
  287. return EFI_BUFFER_TOO_SMALL;
  288. efi_memcpy_runtime(variable_name, var->name, *variable_name_size);
  289. efi_memcpy_runtime(vendor, &var->guid, sizeof(efi_guid_t));
  290. return EFI_SUCCESS;
  291. }
  292. void efi_var_buf_update(struct efi_var_file *var_buf)
  293. {
  294. memcpy(efi_var_buf, var_buf, EFI_VAR_BUF_SIZE);
  295. }