1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
/** @file
Variable services implemented from system memory
There is just a single runtime memory buffer that contans all the data.
Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
UINT64 mMaximumVariableStorageSize;
UINT64 mRemainingVariableStorageSize;
UINT64 mMaximumVariableSize;
typedef struct {
EFI_GUID VendorGuid;
UINT32 Attribute;
UINTN DataSize;
} VARIABLE_ARRAY_ENTRY;
// CHAR16 VariableName[]
// UINT8 Data[]
VARIABLE_ARRAY_ENTRY *mVariableArray = NULL;
VARIABLE_ARRAY_ENTRY *mVariableArrayNextFree = NULL;
VARIABLE_ARRAY_ENTRY *mVariableArrayEnd = NULL;
VARIABLE_ARRAY_ENTRY *
AddEntry (
IN CHAR16 *VariableName,
IN EFI_GUID *VendorGuid,
IN UINT32 Attributes,
IN UINTN DataSize,
IN VOID *Data
)
{
UINTN Size;
UINTN SizeOfString;
VARIABLE_ARRAY_ENTRY *Entry;
EFI_TPL CurrentTpl;
SizeOfString = StrSize (VariableName);
Size = SizeOfString + sizeof (VARIABLE_ARRAY_ENTRY) + DataSize;
if ((VARIABLE_ARRAY_ENTRY *)(((UINT8 *)mVariableArrayNextFree) + Size) > mVariableArrayEnd) {
// ran out of space
return NULL;
}
if (!EfiAtRuntime ()) {
// Enter critical section
CurrentTpl = gBS->RaiseTPL (EFI_TPL_HIGH_LEVEL);
}
Entry = mVariableArrayNextFree;
CopyGuid (&Entry->VendorGuid, VendorGuid);
Entry->Attribute = Attributes;
Entry->DataSize = DataSize;
StrCpy ((CHAR16 *)++mVariableArrayNextFree, VariableName);
mVariableArrayNextFree = (VARIABLE_ARRAY_ENTRY *)(((UINT8 *)mVariableArrayNextFree) + SizeOfString);
CopyMem (mVariableArrayNextFree, Data, DataSize);
mVariableArrayNextFree = (VARIABLE_ARRAY_ENTRY *)(((UINT8 *)mVariableArrayNextFree) + DataSize);
if (!EfiAtRuntime ()) {
// Exit Critical section
gBS->RestoreTPL (CurrentTpl);
}
return Entry;
}
VOID
DeleteEntry (
IN VARIABLE_ARRAY_ENTRY *Entry
)
{
UINTN Size;
UINT8 *Data;
EFI_TPL CurrentTpl;
Size = StrSize ((CHAR16 *)(Entry + 1)) + sizeof (VARIABLE_ARRAY_ENTRY) + Entry->DataSize;
Data = ((UINT8 *)Entry) + Size;
CopyMem (Entry, Data, (UINTN)mVariableArrayNextFree - (UINTN)Data);
if (!EfiAtRuntime ()) {
// Enter critical section
CurrentTpl = gBS->RaiseTPL (EFI_TPL_HIGH_LEVEL);
}
mVariableArrayNextFree = (VARIABLE_ARRAY_ENTRY *)(((UINT8 *)mVariableArrayNextFree) - Size);
if (!EfiAtRuntime ()) {
// Exit Critical section
gBS->RestoreTPL (CurrentTpl);
}
}
VARIABLE_ARRAY_ENTRY *
GetVariableArrayEntry (
IN CHAR16 *VariableName,
IN EFI_GUID *VendorGuid,
OUT VOID **Data OPTIONAL
)
{
VARIABLE_ARRAY_ENTRY *Entry;
UINTN Size;
if (*VariableName == L'\0') {
// by definition first entry is null-terminated string
if (mVariableArray == mVariableArrayNextFree) {
return NULL;
}
return mVariableArray;
}
for (Entry = mVariableArray; Entry < mVariableArrayEnd;) {
if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
if (StrCmp (VariableName, (CHAR16 *)(Entry + 1))) {
Size = StrSize ((CHAR16 *)(Entry + 1));
if (Data != NULL) {
*Data = (VOID *)(((UINT8 *)Entry) + (Size + sizeof (VARIABLE_ARRAY_ENTRY)));
}
return Entry;
}
}
Size = StrSize ((CHAR16 *)(Entry + 1)) + sizeof (VARIABLE_ARRAY_ENTRY) + Entry->DataSize;
Entry = (VARIABLE_ARRAY_ENTRY *)(((UINT8 *)Entry) + Size);
}
return NULL;
}
EFI_STATUS
LibGetVariable (
IN CHAR16 *VariableName,
IN EFI_GUID *VendorGuid,
OUT UINT32 *Attributes OPTIONAL,
IN OUT UINTN *DataSize,
OUT VOID *Data
)
{
VARIABLE_ARRAY_ENTRY *Entry;
VOID *InternalData;
if (EfiAtRuntime () && (Attributes != NULL)) {
if ((*Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) {
return EFI_NOT_FOUND;
}
}
Entry = GetVariableArrayEntry (VariableName, VendorGuid, &InternalData);
if (Entry == NULL) {
return EFI_NOT_FOUND;
}
if (*DataSize < Entry->DataSize) {
*DataSize = Entry->DataSize;
return EFI_BUFFER_TOO_SMALL;
}
*DataSize = Entry->DataSize;
if (Attributes != NULL) {
*Attributes = Entry->Attribute;
}
CopyMem (Data, InternalData, *DataSize);
return EFI_SUCCESS;
}
EFI_STATUS
LibGetNextVariableName (
IN OUT UINTN *VariableNameSize,
IN OUT CHAR16 *VariableName,
IN OUT EFI_GUID *VendorGuid
)
{
VARIABLE_ARRAY_ENTRY *Entry;
VOID *InternalData;
UINTN StringSize;
BOOLEAN Done;
for (Done = FALSE; !Done; ) {
Entry = GetVariableArrayEntry (VariableName, VendorGuid, &InternalData);
if (Entry == NULL) {
return EFI_NOT_FOUND;
}
// If we are at runtime skip variables that do not have the Runitme attribute set.
Done = (EfiAtRuntime () && ((Entry->Attribute & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) ? FALSE : TRUE;
}
StringSize = StrSize ((CHAR16 *)(Entry + 1));
Entry = (VARIABLE_ARRAY_ENTRY *)(((UINT8 *)Entry) + (StringSize + sizeof (VARIABLE_ARRAY_ENTRY) + Entry->DataSize));
if (Entry >= mVariableArrayEnd) {
return EFI_NOT_FOUND;
}
if (*VariableNameSize < StringSize) {
*VariableNameSize = StringSize;
return EFI_BUFFER_TOO_SMALL;
}
*VariableNameSize = StringSize;
CopyMem (VariableName, (CHAR16 *)(Entry + 1), StringSize);
CopyMem (VendorGuid, &Entry->VendorGuid, sizeof (EFI_GUID));
return EFI_SUCCESS;
}
EFI_STATUS
LibSetVariable (
IN CHAR16 *VariableName,
IN EFI_GUID *VendorGuid,
IN UINT32 Attributes,
IN UINTN DataSize,
IN VOID *Data
)
{
VARIABLE_ARRAY_ENTRY *Entry;
VOID *InternalData;
if (EfiAtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {
return EFI_NOT_FOUND;
}
Entry = GetVariableArrayEntry (VariableName, VendorGuid, &InternalData);
if (Entry == NULL) {
if (DataSize == 0) {
return EFI_NOT_FOUND;
}
Entry = AddEntry (VariableName, VendorGuid, Attributes, DataSize, Data);
return (Entry == NULL) ? EFI_OUT_OF_RESOURCES : EFI_SUCCESS;
} else if (DataSize == 0) {
// DataSize is zero so delete
DeleteEntry (Entry);
} else if (DataSize == Entry->DataSize) {
// No change is size so just update the store
Entry->Attribute |= Attributes;
CopyMem (InternalData, Data, DataSize);
} else {
// Grow the entry by deleting and adding back. Don't lose previous Attributes
Attributes |= Entry->Attribute;
DeleteEntry (Entry);
Entry = AddEntry (VariableName, VendorGuid, Attributes, DataSize, Data);
return (Entry == NULL) ? EFI_OUT_OF_RESOURCES : EFI_SUCCESS;
}
}
EFI_STATUS
LibQueryVariableInfo (
IN UINT32 Attributes,
OUT UINT64 *MaximumVariableStorageSize,
OUT UINT64 *RemainingVariableStorageSize,
OUT UINT64 *MaximumVariableSize
)
{
*MaximumVariableStorageSize = mMaximumVariableStorageSize;
*RemainingVariableStorageSize = mRemainingVariableStorageSize;
*MaximumVariableStorageSize = mRemainingVariableStorageSize;
return EFI_SUCCESS;
}
VOID
LibVariableVirtualAddressChangeEvent (VOID)
{
EfiConvertPointer (0, (VOID **)&mVariableArray);
EfiConvertPointer (0, (VOID **)&mVariableArrayNextFree);
EfiConvertPointer (0, (VOID **)&mVariableArrayEnd);
}
VOID
LibVariableInitialize (VOID)
{
UINTN Size;
Size = PcdGet32 (PcdEmbeddedMemVariableStoreSize);
mVariableArray = mVariableArrayNextFree = (VARIABLE_ARRAY_ENTRY *)AllocateRuntimePool (Size);
ASSERT (mVariableArray != NULL);
mVariableArrayEnd = (VARIABLE_ARRAY_ENTRY *)(((UINT8 *)mVariableArray) + Size);
mMaximumVariableStorageSize = Size - sizeof (VARIABLE_ARRAY_ENTRY);
mRemainingVariableStorageSize = mMaximumVariableStorageSize;
mMaximumVariableSize = mMaximumVariableStorageSize;
}
|