blob: 85064a70a1a3b1e8280ce8f68a5bd31cb0d224d2 [file] [log] [blame]
Julian Hall65f7eb42021-11-22 16:06:42 +01001/*
2 * Copyright (c) 2021, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#ifndef VARIABLE_INDEX_H
9#define VARIABLE_INDEX_H
10
11#include <stdbool.h>
12#include <stddef.h>
13#include <stdint.h>
14#include <protocols/common/efi/efi_status.h>
15#include <protocols/service/smm_variable/smm_variable_proto.h>
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21/**
22 * Implementation limits
23 */
24#define VARIABLE_INDEX_MAX_NAME_SIZE (32)
25
26/**
27 * \brief variable_info structure definition
28 *
29 * Holds information about a stored variable.
30 */
31struct variable_info
32{
33 EFI_GUID guid;
34 size_t name_size;
35 int16_t name[VARIABLE_INDEX_MAX_NAME_SIZE];
36 uint32_t attributes;
37 uint64_t uid;
38};
39
40/**
41 * \brief An entry in the index
42 *
43 * Represents a store variable in the variable index.
44 */
45struct variable_entry
46{
47 struct variable_info info;
48
49 bool in_use;
50 bool dirty;
51};
52
53/**
54 * \brief variable_index structure definition
55 *
56 * Provides an index of stored variables to allow the uefi variable store
57 * contents to be enumerated.
58 */
59struct variable_index
60{
61 size_t max_variables;
62 struct variable_entry *entries;
63};
64
65/**
66 * @brief Initialises a variable_index
67 *
68 * @param[in] context variable_index
69 * @param[in] max_variables The maximum number of stored variables
70 *
71 * @return EFI_SUCCESS if initialized successfully
72 */
73efi_status_t variable_index_init(
74 struct variable_index *context,
75 size_t max_variables);
76
77/**
78 * @brief De-initialises a variable_index
79 *
80 * @param[in] context variable_index
81 */
82void variable_index_deinit(
83 struct variable_index *context);
84
85/**
86 * @brief Returns the maximum dump size
87 *
88 * For a given maximum index size, returns the size of the
89 * buffer that is needed to hold all serialized variable_info
90 * objects.
91 *
92 * @param[in] context variable_index
93 */
94size_t variable_index_max_dump_size(
95 struct variable_index *context);
96
97/**
98 * @brief Find info about a variable
99 *
100 * @param[in] context variable_index
101 * @param[in] guid The variable's guid
102 * @param[in] name_size The name parameter's size
103 * @param[in] name The variable's name
104 *
105 * @return Pointer to variable_info or NULL
106 */
107const struct variable_info *variable_index_find(
108 const struct variable_index *context,
109 const EFI_GUID *guid,
110 size_t name_size,
111 const int16_t *name);
112
113/**
114 * @brief Find the next variable in the index
115 *
116 * @param[in] context variable_index
117 * @param[in] guid The variable's guid
118 * @param[in] name_size The name parameter's size
119 * @param[in] name The variable's name
120 *
121 * @return Pointer to variable_info or NULL
122 */
123const struct variable_info *variable_index_find_next(
124 const struct variable_index *context,
125 const EFI_GUID *guid,
126 size_t name_size,
127 const int16_t *name);
128
129/**
130 * @brief Add a new variable to the index
131 *
132 * @param[in] context variable_index
133 * @param[in] guid The variable's guid
134 * @param[in] name_size The name parameter's size
135 * @param[in] name The variable's name
136 * @param[in] attributes The variable's attributes
137 *
138 * @return Pointer to variable_info or NULL
139 */
140const struct variable_info *variable_index_add(
141 struct variable_index *context,
142 const EFI_GUID *guid,
143 size_t name_size,
144 const int16_t *name,
145 uint32_t attributes);
146
147/**
148 * @brief Remove a variable from the index
149 *
150 * Removes a variable from the index if it exists.
151 *
152 * @param[in] context variable_index
153 * @param[in] guid The variable's guid
154 * @param[in] name_size The name parameter's size
155 * @param[in] name The variable's name
156 */
157void variable_index_remove(
158 struct variable_index *context,
159 const EFI_GUID *guid,
160 size_t name_size,
161 const int16_t *name);
162
163/**
164 * @brief Update variable attributes
165 *
166 * @param[in] context variable_index
167 * @param[in] info variable info
168 * @param[in] attributes The variable's attributes
169 */
170void variable_index_update_attributes(
171 struct variable_index *context,
172 const struct variable_info *info,
173 uint32_t attributes);
174
175/**
176 * @brief Dump the serialized index contents for persistent backup
177 *
178 * @param[in] context variable_index
179 * @param[in] buffer_size Size of destination buffer
180 * @param[in] buffer Dump to this buffer
181 * @param[out] data_len Length of serialized data
182 *
183 * @return True if there is unsaved data
184 */
185bool variable_index_dump(
186 struct variable_index *context,
187 size_t buffer_size,
188 uint8_t *buffer,
189 size_t *data_len);
190
191/**
192 * @brief Restore the serialized index contents
193 *
194 * Should be called straight after the variable index is initialized to
195 * restore any NV variable info from persistent storage.
196 *
197 * @param[in] context variable_index
198 * @param[in] data_len The length of the data to load
199 * @param[in] buffer Load from this buffer
200 *
201 * @return Number of bytes loaded
202 */
203size_t variable_index_restore(
204 const struct variable_index *context,
205 size_t data_len,
206 const uint8_t *buffer);
207
208
209#ifdef __cplusplus
210}
211#endif
212
213#endif /* VARIABLE_INDEX_H */