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