blob: 021a74f309b0996ad92edf969e4835487ec129fe [file] [log] [blame]
Heinrich Schuchardtf2d2b3a2020-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 Schuchardt99bfab82020-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 Schuchardtf2d2b3a2020-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 Schuchardt01df8cf2020-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 Schuchardt5f7dcf02020-03-19 18:21:58 +000092#define EFI_VAR_FILE_NAME "ubootefi.var"
93
94#define EFI_VAR_BUF_SIZE 0x4000
95
96#define EFI_VAR_FILE_MAGIC 0x0161566966456255 /* UbEfiVa, version 1 */
97
98/**
99 * struct efi_var_entry - UEFI variable file entry
100 *
101 * @length: length of enty, multiple of 8
102 * @attr: variable attributes
103 * @time: authentication time (seconds since start of epoch)
104 * @guid: vendor GUID
105 * @name: UTF16 variable name
106 */
107struct efi_var_entry {
108 u32 length;
109 u32 attr;
110 u64 time;
111 efi_guid_t guid;
112 u16 name[];
113};
114
115/**
116 * struct efi_var_file - file for storing UEFI variables
117 *
118 * @reserved: unused, may be overwritten by memory probing
119 * @magic: identifies file format
120 * @length: length including header
121 * @crc32: CRC32 without header
122 * @var: variables
123 */
124struct efi_var_file {
125 u64 reserved;
126 u64 magic;
127 u32 length;
128 u32 crc32;
129 struct efi_var_entry var[];
130};
131
132/**
133 * efi_var_to_file() - save non-volatile variables as file
134 *
135 * File ubootefi.var is created on the EFI system partion.
136 *
137 * Return: status code
138 */
139efi_status_t efi_var_to_file(void);
140
141/**
142 * efi_var_from_file() - read variables from file
143 *
144 * File ubootefi.var is read from the EFI system partitions and the variables
145 * stored in the file are created.
146 *
147 * In case the file does not exist yet or a variable cannot be set EFI_SUCCESS
148 * is returned.
149 *
150 * Return: status code
151 */
152efi_status_t efi_var_from_file(void);
153
Heinrich Schuchardtf1f990a2020-03-22 09:07:50 +0100154/**
155 * efi_var_mem_init() - set-up variable list
156 *
157 * Return: status code
158 */
159efi_status_t efi_var_mem_init(void);
160
161/**
162 * efi_var_mem_find() - find a variable in the list
163 *
164 * @guid: GUID of the variable
165 * @name: name of the variable
166 * @next: on exit pointer to the next variable after the found one
167 * Return: found variable
168 */
169struct efi_var_entry *efi_var_mem_find(const efi_guid_t *guid, const u16 *name,
170 struct efi_var_entry **next);
171
172/**
173 * efi_var_mem_del() - delete a variable from the list of variables
174 *
175 * @var: variable to delete
176 */
177void efi_var_mem_del(struct efi_var_entry *var);
178
179/**
180 * efi_var_mem_ins() - append a variable to the list of variables
181 *
182 * The variable is appended without checking if a variable of the same name
183 * already exists. The two data buffers are concatenated.
184 *
185 * @variable_name: variable name
186 * @vendor: GUID
187 * @attributes: variable attributes
188 * @size1: size of the first data buffer
189 * @data1: first data buffer
190 * @size2: size of the second data field
191 * @data2: second data buffer
192 * @time: time of authentication (as seconds since start of epoch)
193 * Result: status code
194 */
195efi_status_t efi_var_mem_ins(u16 *variable_name,
196 const efi_guid_t *vendor, u32 attributes,
197 const efi_uintn_t size1, const void *data1,
198 const efi_uintn_t size2, const void *data2,
199 const u64 time);
200
201/**
202 * efi_var_mem_free() - determine free memory for variables
203 *
204 * Return: maximum data size plus variable name size
205 */
206u64 efi_var_mem_free(void);
207
Heinrich Schuchardt012c56a2020-07-14 08:04:49 +0200208/**
209 * efi_init_secure_state - initialize secure boot state
210 *
211 * Return: status code
212 */
213efi_status_t efi_init_secure_state(void);
214
Heinrich Schuchardt99bfab82020-07-15 12:40:35 +0200215/**
216 * efi_auth_var_get_type() - convert variable name and guid to enum
217 *
218 * @name: name of UEFI variable
219 * @guid: guid of UEFI variable
220 * Return: identifier for authentication related variables
221 */
222enum efi_auth_var_type efi_auth_var_get_type(u16 *name, const efi_guid_t *guid);
223
Heinrich Schuchardtf2d2b3a2020-06-22 18:10:27 +0200224#endif