blob: 80c5e5fa5e771c9713da3f40cbaded9a8026b285 [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
Julian Hallc0d54dc2021-10-13 15:18:30 +0100153 * @param[in] info The variable info corresponding to the entry to remove
Julian Hall65f7eb42021-11-22 16:06:42 +0100154 */
155void variable_index_remove(
156 struct variable_index *context,
Julian Hallc0d54dc2021-10-13 15:18:30 +0100157 const struct variable_info *info);
Julian Hall65f7eb42021-11-22 16:06:42 +0100158
159/**
160 * @brief Update variable attributes
161 *
162 * @param[in] context variable_index
163 * @param[in] info variable info
164 * @param[in] attributes The variable's attributes
165 */
166void variable_index_update_attributes(
167 struct variable_index *context,
168 const struct variable_info *info,
169 uint32_t attributes);
170
171/**
172 * @brief Dump the serialized index contents for persistent backup
173 *
174 * @param[in] context variable_index
175 * @param[in] buffer_size Size of destination buffer
176 * @param[in] buffer Dump to this buffer
177 * @param[out] data_len Length of serialized data
178 *
179 * @return True if there is unsaved data
180 */
181bool variable_index_dump(
182 struct variable_index *context,
183 size_t buffer_size,
184 uint8_t *buffer,
185 size_t *data_len);
186
187/**
188 * @brief Restore the serialized index contents
189 *
190 * Should be called straight after the variable index is initialized to
191 * restore any NV variable info from persistent storage.
192 *
193 * @param[in] context variable_index
194 * @param[in] data_len The length of the data to load
195 * @param[in] buffer Load from this buffer
196 *
197 * @return Number of bytes loaded
198 */
199size_t variable_index_restore(
200 const struct variable_index *context,
201 size_t data_len,
202 const uint8_t *buffer);
203
204
205#ifdef __cplusplus
206}
207#endif
208
209#endif /* VARIABLE_INDEX_H */