1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (c) 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
6 #ifndef _EFI_VARIABLE_H
7 #define _EFI_VARIABLE_H
9 #include <linux/bitops.h>
11 #define EFI_VARIABLE_READ_ONLY BIT(31)
13 enum efi_auth_var_type {
14 EFI_AUTH_VAR_NONE = 0,
25 * efi_get_variable() - retrieve value of a UEFI variable
27 * @variable_name: name of the variable
28 * @vendor: vendor GUID
29 * @attributes: attributes of the variable
30 * @data_size: size of the buffer to which the variable value is copied
31 * @data: buffer to which the variable value is copied
32 * @timep: authentication time (seconds since start of epoch)
35 efi_status_t efi_get_variable_int(u16 *variable_name, const efi_guid_t *vendor,
36 u32 *attributes, efi_uintn_t *data_size,
37 void *data, u64 *timep);
40 * efi_set_variable() - set value of a UEFI variable
42 * @variable_name: name of the variable
43 * @vendor: vendor GUID
44 * @attributes: attributes of the variable
45 * @data_size: size of the buffer with the variable value
46 * @data: buffer with the variable value
47 * @ro_check: check the read only read only bit in attributes
50 efi_status_t efi_set_variable_int(u16 *variable_name, const efi_guid_t *vendor,
51 u32 attributes, efi_uintn_t data_size,
52 const void *data, bool ro_check);
55 * efi_get_next_variable_name_int() - enumerate the current variable names
57 * @variable_name_size: size of variable_name buffer in byte
58 * @variable_name: name of uefi variable's name in u16
59 * @vendor: vendor's guid
61 * See the Unified Extensible Firmware Interface (UEFI) specification for
66 efi_status_t efi_get_next_variable_name_int(efi_uintn_t *variable_name_size,
71 * efi_query_variable_info_int() - get information about EFI variables
73 * This function implements the QueryVariableInfo() runtime service.
75 * See the Unified Extensible Firmware Interface (UEFI) specification for
78 * @attributes: bitmask to select variables to be
80 * @maximum_variable_storage_size: maximum size of storage area for the
81 * selected variable types
82 * @remaining_variable_storage_size: remaining size of storage are for the
83 * selected variable types
84 * @maximum_variable_size: maximum size of a variable of the
86 * Returns: status code
88 efi_status_t efi_query_variable_info_int(u32 attributes,
89 u64 *maximum_variable_storage_size,
90 u64 *remaining_variable_storage_size,
91 u64 *maximum_variable_size);
93 #define EFI_VAR_FILE_NAME "ubootefi.var"
95 #define EFI_VAR_BUF_SIZE CONFIG_EFI_VAR_BUF_SIZE
98 * This constant identifies the file format for storing UEFI variables in
99 * struct efi_var_file.
101 #define EFI_VAR_FILE_MAGIC 0x0161566966456255 /* UbEfiVa, version 1 */
104 * struct efi_var_entry - UEFI variable file entry
106 * @length: length of enty, multiple of 8
107 * @attr: variable attributes
108 * @time: authentication time (seconds since start of epoch)
110 * @name: UTF16 variable name
112 struct efi_var_entry {
121 * struct efi_var_file - file for storing UEFI variables
123 * @reserved: unused, may be overwritten by memory probing
124 * @magic: identifies file format, takes value %EFI_VAR_FILE_MAGIC
125 * @length: length including header
126 * @crc32: CRC32 without header
129 struct efi_var_file {
134 struct efi_var_entry var[];
138 * efi_var_to_file() - save non-volatile variables as file
140 * File ubootefi.var is created on the EFI system partion.
142 * Return: status code
144 efi_status_t efi_var_to_file(void);
147 * efi_var_collect() - collect variables in buffer
149 * A buffer is allocated and filled with variables in a format ready to be
152 * @bufp: pointer to pointer of buffer with collected variables
153 * @lenp: pointer to length of buffer
154 * @check_attr_mask: bitmask with required attributes of variables to be collected.
155 * variables are only collected if all of the required
156 * attributes are set.
157 * Return: status code
159 efi_status_t __maybe_unused efi_var_collect(struct efi_var_file **bufp, loff_t *lenp,
160 u32 check_attr_mask);
163 * efi_var_restore() - restore EFI variables from buffer
165 * Only if @safe is set secure boot related variables will be restored.
168 * @safe: restoring from tamper-resistant storage
169 * Return: status code
171 efi_status_t efi_var_restore(struct efi_var_file *buf, bool safe);
174 * efi_var_from_file() - read variables from file
176 * File ubootefi.var is read from the EFI system partitions and the variables
177 * stored in the file are created.
179 * In case the file does not exist yet or a variable cannot be set EFI_SUCCESS
182 * Return: status code
184 efi_status_t efi_var_from_file(void);
187 * efi_var_mem_init() - set-up variable list
189 * Return: status code
191 efi_status_t efi_var_mem_init(void);
194 * efi_var_mem_find() - find a variable in the list
196 * @guid: GUID of the variable
197 * @name: name of the variable
198 * @next: on exit pointer to the next variable after the found one
199 * Return: found variable
201 struct efi_var_entry *efi_var_mem_find(const efi_guid_t *guid, const u16 *name,
202 struct efi_var_entry **next);
205 * efi_var_mem_del() - delete a variable from the list of variables
207 * @var: variable to delete
209 void efi_var_mem_del(struct efi_var_entry *var);
212 * efi_var_mem_ins() - append a variable to the list of variables
214 * The variable is appended without checking if a variable of the same name
215 * already exists. The two data buffers are concatenated.
217 * @variable_name: variable name
219 * @attributes: variable attributes
220 * @size1: size of the first data buffer
221 * @data1: first data buffer
222 * @size2: size of the second data field
223 * @data2: second data buffer
224 * @time: time of authentication (as seconds since start of epoch)
225 * Result: status code
227 efi_status_t efi_var_mem_ins(u16 *variable_name,
228 const efi_guid_t *vendor, u32 attributes,
229 const efi_uintn_t size1, const void *data1,
230 const efi_uintn_t size2, const void *data2,
234 * efi_var_mem_free() - determine free memory for variables
236 * Return: maximum data size plus variable name size
238 u64 efi_var_mem_free(void);
241 * efi_init_secure_state - initialize secure boot state
243 * Return: status code
245 efi_status_t efi_init_secure_state(void);
248 * efi_auth_var_get_type() - convert variable name and guid to enum
250 * @name: name of UEFI variable
251 * @guid: guid of UEFI variable
252 * Return: identifier for authentication related variables
254 enum efi_auth_var_type efi_auth_var_get_type(u16 *name, const efi_guid_t *guid);
257 * efi_get_next_variable_name_mem() - Runtime common code across efi variable
258 * implementations for GetNextVariable()
259 * from the cached memory copy
260 * @variable_name_size: size of variable_name buffer in byte
261 * @variable_name: name of uefi variable's name in u16
262 * @vendor: vendor's guid
264 * Return: status code
266 efi_status_t __efi_runtime
267 efi_get_next_variable_name_mem(efi_uintn_t *variable_name_size, u16 *variable_name,
270 * efi_get_variable_mem() - Runtime common code across efi variable
271 * implementations for GetVariable() from
272 * the cached memory copy
274 * @variable_name: name of the variable
275 * @vendor: vendor GUID
276 * @attributes: attributes of the variable
277 * @data_size: size of the buffer to which the variable value is copied
278 * @data: buffer to which the variable value is copied
279 * @timep: authentication time (seconds since start of epoch)
280 * Return: status code
282 efi_status_t __efi_runtime
283 efi_get_variable_mem(u16 *variable_name, const efi_guid_t *vendor, u32 *attributes,
284 efi_uintn_t *data_size, void *data, u64 *timep);
287 * efi_get_variable_runtime() - runtime implementation of GetVariable()
289 * @variable_name: name of the variable
291 * @attributes: attributes of the variable
292 * @data_size: size of the buffer to which the variable value is copied
293 * @data: buffer to which the variable value is copied
294 * Return: status code
296 efi_status_t __efi_runtime EFIAPI
297 efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *guid,
298 u32 *attributes, efi_uintn_t *data_size, void *data);
301 * efi_get_next_variable_name_runtime() - runtime implementation of
304 * @variable_name_size: size of variable_name buffer in byte
305 * @variable_name: name of uefi variable's name in u16
306 * @guid: vendor's guid
307 * Return: status code
309 efi_status_t __efi_runtime EFIAPI
310 efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
311 u16 *variable_name, efi_guid_t *guid);
314 * efi_var_buf_update() - udpate memory buffer for variables
316 * @var_buf: source buffer
318 * This function copies to the memory buffer for UEFI variables. Call this
319 * function in ExitBootServices() if memory backed variables are only used
320 * at runtime to fill the buffer.
322 void efi_var_buf_update(struct efi_var_file *var_buf);