efi_variable.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
  4. */
  5. #ifndef _EFI_VARIABLE_H
  6. #define _EFI_VARIABLE_H
  7. #include <linux/bitops.h>
  8. #define EFI_VARIABLE_READ_ONLY BIT(31)
  9. enum efi_auth_var_type {
  10. EFI_AUTH_VAR_NONE = 0,
  11. EFI_AUTH_VAR_PK,
  12. EFI_AUTH_VAR_KEK,
  13. EFI_AUTH_VAR_DB,
  14. EFI_AUTH_VAR_DBX,
  15. EFI_AUTH_VAR_DBT,
  16. EFI_AUTH_VAR_DBR,
  17. };
  18. /**
  19. * efi_get_variable() - retrieve value of a UEFI variable
  20. *
  21. * @variable_name: name of the variable
  22. * @vendor: vendor GUID
  23. * @attributes: attributes of the variable
  24. * @data_size: size of the buffer to which the variable value is copied
  25. * @data: buffer to which the variable value is copied
  26. * @timep: authentication time (seconds since start of epoch)
  27. * Return: status code
  28. */
  29. efi_status_t efi_get_variable_int(u16 *variable_name, const efi_guid_t *vendor,
  30. u32 *attributes, efi_uintn_t *data_size,
  31. void *data, u64 *timep);
  32. /**
  33. * efi_set_variable() - set value of a UEFI variable
  34. *
  35. * @variable_name: name of the variable
  36. * @vendor: vendor GUID
  37. * @attributes: attributes of the variable
  38. * @data_size: size of the buffer with the variable value
  39. * @data: buffer with the variable value
  40. * @ro_check: check the read only read only bit in attributes
  41. * Return: status code
  42. */
  43. efi_status_t efi_set_variable_int(u16 *variable_name, const efi_guid_t *vendor,
  44. u32 attributes, efi_uintn_t data_size,
  45. const void *data, bool ro_check);
  46. /**
  47. * efi_get_next_variable_name_int() - enumerate the current variable names
  48. *
  49. * @variable_name_size: size of variable_name buffer in byte
  50. * @variable_name: name of uefi variable's name in u16
  51. * @vendor: vendor's guid
  52. *
  53. * See the Unified Extensible Firmware Interface (UEFI) specification for
  54. * details.
  55. *
  56. * Return: status code
  57. */
  58. efi_status_t efi_get_next_variable_name_int(efi_uintn_t *variable_name_size,
  59. u16 *variable_name,
  60. efi_guid_t *vendor);
  61. /**
  62. * efi_query_variable_info_int() - get information about EFI variables
  63. *
  64. * This function implements the QueryVariableInfo() runtime service.
  65. *
  66. * See the Unified Extensible Firmware Interface (UEFI) specification for
  67. * details.
  68. *
  69. * @attributes: bitmask to select variables to be
  70. * queried
  71. * @maximum_variable_storage_size: maximum size of storage area for the
  72. * selected variable types
  73. * @remaining_variable_storage_size: remaining size of storage are for the
  74. * selected variable types
  75. * @maximum_variable_size: maximum size of a variable of the
  76. * selected type
  77. * Returns: status code
  78. */
  79. efi_status_t efi_query_variable_info_int(u32 attributes,
  80. u64 *maximum_variable_storage_size,
  81. u64 *remaining_variable_storage_size,
  82. u64 *maximum_variable_size);
  83. #define EFI_VAR_FILE_NAME "ubootefi.var"
  84. #define EFI_VAR_BUF_SIZE CONFIG_EFI_VAR_BUF_SIZE
  85. /*
  86. * This constant identifies the file format for storing UEFI variables in
  87. * struct efi_var_file.
  88. */
  89. #define EFI_VAR_FILE_MAGIC 0x0161566966456255 /* UbEfiVa, version 1 */
  90. /**
  91. * struct efi_var_entry - UEFI variable file entry
  92. *
  93. * @length: length of enty, multiple of 8
  94. * @attr: variable attributes
  95. * @time: authentication time (seconds since start of epoch)
  96. * @guid: vendor GUID
  97. * @name: UTF16 variable name
  98. */
  99. struct efi_var_entry {
  100. u32 length;
  101. u32 attr;
  102. u64 time;
  103. efi_guid_t guid;
  104. u16 name[];
  105. };
  106. /**
  107. * struct efi_var_file - file for storing UEFI variables
  108. *
  109. * @reserved: unused, may be overwritten by memory probing
  110. * @magic: identifies file format, takes value %EFI_VAR_FILE_MAGIC
  111. * @length: length including header
  112. * @crc32: CRC32 without header
  113. * @var: variables
  114. */
  115. struct efi_var_file {
  116. u64 reserved;
  117. u64 magic;
  118. u32 length;
  119. u32 crc32;
  120. struct efi_var_entry var[];
  121. };
  122. /**
  123. * efi_var_to_file() - save non-volatile variables as file
  124. *
  125. * File ubootefi.var is created on the EFI system partion.
  126. *
  127. * Return: status code
  128. */
  129. efi_status_t efi_var_to_file(void);
  130. /**
  131. * efi_var_collect() - collect variables in buffer
  132. *
  133. * A buffer is allocated and filled with variables in a format ready to be
  134. * written to disk.
  135. *
  136. * @bufp: pointer to pointer of buffer with collected variables
  137. * @lenp: pointer to length of buffer
  138. * @check_attr_mask: bitmask with required attributes of variables to be collected.
  139. * variables are only collected if all of the required
  140. * attributes are set.
  141. * Return: status code
  142. */
  143. efi_status_t __maybe_unused efi_var_collect(struct efi_var_file **bufp, loff_t *lenp,
  144. u32 check_attr_mask);
  145. /**
  146. * efi_var_restore() - restore EFI variables from buffer
  147. *
  148. * @buf: buffer
  149. * Return: status code
  150. */
  151. efi_status_t efi_var_restore(struct efi_var_file *buf);
  152. /**
  153. * efi_var_from_file() - read variables from file
  154. *
  155. * File ubootefi.var is read from the EFI system partitions and the variables
  156. * stored in the file are created.
  157. *
  158. * In case the file does not exist yet or a variable cannot be set EFI_SUCCESS
  159. * is returned.
  160. *
  161. * Return: status code
  162. */
  163. efi_status_t efi_var_from_file(void);
  164. /**
  165. * efi_var_mem_init() - set-up variable list
  166. *
  167. * Return: status code
  168. */
  169. efi_status_t efi_var_mem_init(void);
  170. /**
  171. * efi_var_mem_find() - find a variable in the list
  172. *
  173. * @guid: GUID of the variable
  174. * @name: name of the variable
  175. * @next: on exit pointer to the next variable after the found one
  176. * Return: found variable
  177. */
  178. struct efi_var_entry *efi_var_mem_find(const efi_guid_t *guid, const u16 *name,
  179. struct efi_var_entry **next);
  180. /**
  181. * efi_var_mem_del() - delete a variable from the list of variables
  182. *
  183. * @var: variable to delete
  184. */
  185. void efi_var_mem_del(struct efi_var_entry *var);
  186. /**
  187. * efi_var_mem_ins() - append a variable to the list of variables
  188. *
  189. * The variable is appended without checking if a variable of the same name
  190. * already exists. The two data buffers are concatenated.
  191. *
  192. * @variable_name: variable name
  193. * @vendor: GUID
  194. * @attributes: variable attributes
  195. * @size1: size of the first data buffer
  196. * @data1: first data buffer
  197. * @size2: size of the second data field
  198. * @data2: second data buffer
  199. * @time: time of authentication (as seconds since start of epoch)
  200. * Result: status code
  201. */
  202. efi_status_t efi_var_mem_ins(u16 *variable_name,
  203. const efi_guid_t *vendor, u32 attributes,
  204. const efi_uintn_t size1, const void *data1,
  205. const efi_uintn_t size2, const void *data2,
  206. const u64 time);
  207. /**
  208. * efi_var_mem_free() - determine free memory for variables
  209. *
  210. * Return: maximum data size plus variable name size
  211. */
  212. u64 efi_var_mem_free(void);
  213. /**
  214. * efi_init_secure_state - initialize secure boot state
  215. *
  216. * Return: status code
  217. */
  218. efi_status_t efi_init_secure_state(void);
  219. /**
  220. * efi_auth_var_get_type() - convert variable name and guid to enum
  221. *
  222. * @name: name of UEFI variable
  223. * @guid: guid of UEFI variable
  224. * Return: identifier for authentication related variables
  225. */
  226. enum efi_auth_var_type efi_auth_var_get_type(u16 *name, const efi_guid_t *guid);
  227. /**
  228. * efi_get_next_variable_name_mem() - Runtime common code across efi variable
  229. * implementations for GetNextVariable()
  230. * from the cached memory copy
  231. * @variable_name_size: size of variable_name buffer in byte
  232. * @variable_name: name of uefi variable's name in u16
  233. * @vendor: vendor's guid
  234. *
  235. * Return: status code
  236. */
  237. efi_status_t __efi_runtime
  238. efi_get_next_variable_name_mem(efi_uintn_t *variable_name_size, u16 *variable_name,
  239. efi_guid_t *vendor);
  240. /**
  241. * efi_get_variable_mem() - Runtime common code across efi variable
  242. * implementations for GetVariable() from
  243. * the cached memory copy
  244. *
  245. * @variable_name: name of the variable
  246. * @vendor: vendor GUID
  247. * @attributes: attributes of the variable
  248. * @data_size: size of the buffer to which the variable value is copied
  249. * @data: buffer to which the variable value is copied
  250. * @timep: authentication time (seconds since start of epoch)
  251. * Return: status code
  252. */
  253. efi_status_t __efi_runtime
  254. efi_get_variable_mem(u16 *variable_name, const efi_guid_t *vendor, u32 *attributes,
  255. efi_uintn_t *data_size, void *data, u64 *timep);
  256. /**
  257. * efi_get_variable_runtime() - runtime implementation of GetVariable()
  258. *
  259. * @variable_name: name of the variable
  260. * @guid: vendor GUID
  261. * @attributes: attributes of the variable
  262. * @data_size: size of the buffer to which the variable value is copied
  263. * @data: buffer to which the variable value is copied
  264. * Return: status code
  265. */
  266. efi_status_t __efi_runtime EFIAPI
  267. efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *guid,
  268. u32 *attributes, efi_uintn_t *data_size, void *data);
  269. /**
  270. * efi_get_next_variable_name_runtime() - runtime implementation of
  271. * GetNextVariable()
  272. *
  273. * @variable_name_size: size of variable_name buffer in byte
  274. * @variable_name: name of uefi variable's name in u16
  275. * @guid: vendor's guid
  276. * Return: status code
  277. */
  278. efi_status_t __efi_runtime EFIAPI
  279. efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
  280. u16 *variable_name, efi_guid_t *guid);
  281. #endif