efi_variable.h 9.7 KB

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