]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.h
58d1e5a8cdab4fe2bcef7bdfeb7fe4b79de2a7a8
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / RuntimeDxe / Variable.h
1 /** @file
2 The internal header file includes the common header files, defines
3 internal structure and functions used by Variable modules.
4
5 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #ifndef _VARIABLE_H_
17 #define _VARIABLE_H_
18
19 #include <PiDxe.h>
20 #include <Protocol/VariableWrite.h>
21 #include <Protocol/FaultTolerantWrite.h>
22 #include <Protocol/FirmwareVolumeBlock.h>
23 #include <Protocol/Variable.h>
24 #include <Library/PcdLib.h>
25 #include <Library/HobLib.h>
26 #include <Library/UefiDriverEntryPoint.h>
27 #include <Library/DxeServicesTableLib.h>
28 #include <Library/UefiRuntimeLib.h>
29 #include <Library/DebugLib.h>
30 #include <Library/BaseMemoryLib.h>
31 #include <Library/UefiBootServicesTableLib.h>
32 #include <Library/UefiLib.h>
33 #include <Library/BaseLib.h>
34 #include <Library/SynchronizationLib.h>
35 #include <Library/MemoryAllocationLib.h>
36 #include <Library/BaseCryptLib.h>
37 #include <Library/PlatformSecureLib.h>
38 #include <Guid/GlobalVariable.h>
39 #include <Guid/EventGroup.h>
40 #include <Guid/AuthenticatedVariableFormat.h>
41 #include <Guid/ImageAuthentication.h>
42 #include <Guid/SystemNvDataGuid.h>
43
44 #define VARIABLE_RECLAIM_THRESHOLD (1024)
45
46 ///
47 /// The size of a 3 character ISO639 language code.
48 ///
49 #define ISO_639_2_ENTRY_SIZE 3
50
51 typedef enum {
52 VariableStoreTypeVolatile,
53 VariableStoreTypeHob,
54 VariableStoreTypeNv,
55 VariableStoreTypeMax
56 } VARIABLE_STORE_TYPE;
57
58 typedef struct {
59 VARIABLE_HEADER *CurrPtr;
60 VARIABLE_HEADER *EndPtr;
61 VARIABLE_HEADER *StartPtr;
62 BOOLEAN Volatile;
63 } VARIABLE_POINTER_TRACK;
64
65 typedef struct {
66 EFI_PHYSICAL_ADDRESS HobVariableBase;
67 EFI_PHYSICAL_ADDRESS VolatileVariableBase;
68 EFI_PHYSICAL_ADDRESS NonVolatileVariableBase;
69 EFI_LOCK VariableServicesLock;
70 UINT32 ReentrantState;
71 } VARIABLE_GLOBAL;
72
73 typedef struct {
74 VARIABLE_GLOBAL VariableGlobal;
75 UINTN VolatileLastVariableOffset;
76 UINTN NonVolatileLastVariableOffset;
77 UINTN CommonVariableTotalSize;
78 UINTN HwErrVariableTotalSize;
79 CHAR8 *PlatformLangCodes;
80 CHAR8 *LangCodes;
81 CHAR8 *PlatformLang;
82 CHAR8 Lang[ISO_639_2_ENTRY_SIZE + 1];
83 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbInstance;
84 } VARIABLE_MODULE_GLOBAL;
85
86 typedef struct {
87 EFI_GUID *Guid;
88 CHAR16 *Name;
89 UINT32 Attributes;
90 UINTN DataSize;
91 VOID *Data;
92 } VARIABLE_CACHE_ENTRY;
93
94 /**
95 Writes a buffer to variable storage space, in the working block.
96
97 This function writes a buffer to variable storage space into a firmware
98 volume block device. The destination is specified by the parameter
99 VariableBase. Fault Tolerant Write protocol is used for writing.
100
101 @param VariableBase Base address of the variable to write.
102 @param Buffer Point to the data buffer.
103 @param BufferSize The number of bytes of the data Buffer.
104
105 @retval EFI_SUCCESS The function completed successfully.
106 @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol.
107 @retval EFI_ABORTED The function could not complete successfully.
108
109 **/
110 EFI_STATUS
111 FtwVariableSpace (
112 IN EFI_PHYSICAL_ADDRESS VariableBase,
113 IN UINT8 *Buffer,
114 IN UINTN BufferSize
115 );
116
117 /**
118 Finds variable in storage blocks of volatile and non-volatile storage areas.
119
120 This code finds variable in storage blocks of volatile and non-volatile storage areas.
121 If VariableName is an empty string, then we just return the first
122 qualified variable without comparing VariableName and VendorGuid.
123 Otherwise, VariableName and VendorGuid are compared.
124
125 @param VariableName Name of the variable to be found.
126 @param VendorGuid Vendor GUID to be found.
127 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
128 including the range searched and the target position.
129 @param Global Pointer to VARIABLE_GLOBAL structure, including
130 base of volatile variable storage area, base of
131 NV variable storage area, and a lock.
132
133 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
134 VendorGuid is NULL.
135 @retval EFI_SUCCESS Variable successfully found.
136 @retval EFI_INVALID_PARAMETER Variable not found.
137
138 **/
139 EFI_STATUS
140 FindVariable (
141 IN CHAR16 *VariableName,
142 IN EFI_GUID *VendorGuid,
143 OUT VARIABLE_POINTER_TRACK *PtrTrack,
144 IN VARIABLE_GLOBAL *Global
145 );
146
147 /**
148
149 This code gets the pointer to the variable data.
150
151 @param Variable Pointer to the Variable Header.
152
153 @return Pointer to Variable Data.
154
155 **/
156 UINT8 *
157 GetVariableDataPtr (
158 IN VARIABLE_HEADER *Variable
159 );
160
161 /**
162
163 This code gets the size of variable data.
164
165 @param Variable Pointer to the Variable Header.
166
167 @return Size of variable in bytes.
168
169 **/
170 UINTN
171 DataSizeOfVariable (
172 IN VARIABLE_HEADER *Variable
173 );
174
175 /**
176 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
177 index of associated public key is needed.
178
179 @param[in] VariableName Name of variable.
180 @param[in] VendorGuid Guid of variable.
181 @param[in] Data Variable data.
182 @param[in] DataSize Size of data. 0 means delete.
183 @param[in] Attributes Attributes of the variable.
184 @param[in] KeyIndex Index of associated public key.
185 @param[in] MonotonicCount Value of associated monotonic count.
186 @param[in] Variable The variable information that is used to keep track of variable usage.
187
188 @param[in] TimeStamp Value of associated TimeStamp.
189
190 @retval EFI_SUCCESS The update operation is success.
191 @retval EFI_OUT_OF_RESOURCES Variable region is full, cannot write other data into this region.
192
193 **/
194 EFI_STATUS
195 UpdateVariable (
196 IN CHAR16 *VariableName,
197 IN EFI_GUID *VendorGuid,
198 IN VOID *Data,
199 IN UINTN DataSize,
200 IN UINT32 Attributes OPTIONAL,
201 IN UINT32 KeyIndex OPTIONAL,
202 IN UINT64 MonotonicCount OPTIONAL,
203 IN VARIABLE_POINTER_TRACK *Variable,
204 IN EFI_TIME *TimeStamp OPTIONAL
205 );
206
207
208 /**
209 Return TRUE if ExitBootServices () has been called.
210
211 @retval TRUE If ExitBootServices () has been called.
212 **/
213 BOOLEAN
214 AtRuntime (
215 VOID
216 );
217
218 /**
219 Initializes a basic mutual exclusion lock.
220
221 This function initializes a basic mutual exclusion lock to the released state
222 and returns the lock. Each lock provides mutual exclusion access at its task
223 priority level. Since there is no preemption or multiprocessor support in EFI,
224 acquiring the lock only consists of raising to the locks TPL.
225 If Lock is NULL, then ASSERT().
226 If Priority is not a valid TPL value, then ASSERT().
227
228 @param Lock A pointer to the lock data structure to initialize.
229 @param Priority EFI TPL is associated with the lock.
230
231 @return The lock.
232
233 **/
234 EFI_LOCK *
235 InitializeLock (
236 IN OUT EFI_LOCK *Lock,
237 IN EFI_TPL Priority
238 );
239
240
241 /**
242 Acquires lock only at boot time. Simply returns at runtime.
243
244 This is a temperary function that will be removed when
245 EfiAcquireLock() in UefiLib can handle the call in UEFI
246 Runtimer driver in RT phase.
247 It calls EfiAcquireLock() at boot time, and simply returns
248 at runtime.
249
250 @param Lock A pointer to the lock to acquire.
251
252 **/
253 VOID
254 AcquireLockOnlyAtBootTime (
255 IN EFI_LOCK *Lock
256 );
257
258
259 /**
260 Releases lock only at boot time. Simply returns at runtime.
261
262 This is a temperary function which will be removed when
263 EfiReleaseLock() in UefiLib can handle the call in UEFI
264 Runtimer driver in RT phase.
265 It calls EfiReleaseLock() at boot time and simply returns
266 at runtime.
267
268 @param Lock A pointer to the lock to release.
269
270 **/
271 VOID
272 ReleaseLockOnlyAtBootTime (
273 IN EFI_LOCK *Lock
274 );
275
276 /**
277 Retrive the FVB protocol interface by HANDLE.
278
279 @param[in] FvBlockHandle The handle of FVB protocol that provides services for
280 reading, writing, and erasing the target block.
281 @param[out] FvBlock The interface of FVB protocol
282
283 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
284 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.
285 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
286
287 **/
288 EFI_STATUS
289 GetFvbByHandle (
290 IN EFI_HANDLE FvBlockHandle,
291 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
292 );
293
294
295 /**
296 Retrive the Swap Address Range protocol interface.
297
298 @param[out] SarProtocol The interface of SAR protocol
299
300 @retval EFI_SUCCESS The SAR protocol instance was found and returned in SarProtocol.
301 @retval EFI_NOT_FOUND The SAR protocol instance was not found.
302 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
303
304 **/
305 EFI_STATUS
306 GetSarProtocol (
307 OUT VOID **SarProtocol
308 );
309
310 /**
311 Function returns an array of handles that support the FVB protocol
312 in a buffer allocated from pool.
313
314 @param[out] NumberHandles The number of handles returned in Buffer.
315 @param[out] Buffer A pointer to the buffer to return the requested
316 array of handles that support FVB protocol.
317
318 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
319 handles in Buffer was returned in NumberHandles.
320 @retval EFI_NOT_FOUND No FVB handle was found.
321 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
322 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
323
324 **/
325 EFI_STATUS
326 GetFvbCountAndBuffer (
327 OUT UINTN *NumberHandles,
328 OUT EFI_HANDLE **Buffer
329 );
330
331 /**
332 Initializes variable store area for non-volatile and volatile variable.
333
334 @retval EFI_SUCCESS Function successfully executed.
335 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
336
337 **/
338 EFI_STATUS
339 VariableCommonInitialize (
340 VOID
341 );
342
343 /**
344 This function reclaims variable storage if free size is below the threshold.
345
346 **/
347 VOID
348 ReclaimForOS(
349 VOID
350 );
351
352
353 /**
354 Initializes variable write service after FVB was ready.
355
356 @retval EFI_SUCCESS Function successfully executed.
357 @retval Others Fail to initialize the variable service.
358
359 **/
360 EFI_STATUS
361 VariableWriteServiceInitialize (
362 VOID
363 );
364
365 /**
366 Retrive the SMM Fault Tolerent Write protocol interface.
367
368 @param[out] FtwProtocol The interface of SMM Ftw protocol
369
370 @retval EFI_SUCCESS The SMM SAR protocol instance was found and returned in SarProtocol.
371 @retval EFI_NOT_FOUND The SMM SAR protocol instance was not found.
372 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
373
374 **/
375 EFI_STATUS
376 GetFtwProtocol (
377 OUT VOID **FtwProtocol
378 );
379
380 /**
381 Get the proper fvb handle and/or fvb protocol by the given Flash address.
382
383 @param[in] Address The Flash address.
384 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
385 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
386
387 **/
388 EFI_STATUS
389 GetFvbInfoByAddress (
390 IN EFI_PHYSICAL_ADDRESS Address,
391 OUT EFI_HANDLE *FvbHandle OPTIONAL,
392 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL
393 );
394
395 /**
396
397 This code finds variable in storage blocks (Volatile or Non-Volatile).
398
399 @param VariableName Name of Variable to be found.
400 @param VendorGuid Variable vendor GUID.
401 @param Attributes Attribute value of the variable found.
402 @param DataSize Size of Data found. If size is less than the
403 data, this value contains the required size.
404 @param Data Data pointer.
405
406 @return EFI_INVALID_PARAMETER Invalid parameter.
407 @return EFI_SUCCESS Find the specified variable.
408 @return EFI_NOT_FOUND Not found.
409 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
410
411 **/
412 EFI_STATUS
413 EFIAPI
414 VariableServiceGetVariable (
415 IN CHAR16 *VariableName,
416 IN EFI_GUID *VendorGuid,
417 OUT UINT32 *Attributes OPTIONAL,
418 IN OUT UINTN *DataSize,
419 OUT VOID *Data
420 );
421
422 /**
423
424 This code Finds the Next available variable.
425
426 @param VariableNameSize Size of the variable name.
427 @param VariableName Pointer to variable name.
428 @param VendorGuid Variable Vendor Guid.
429
430 @return EFI_INVALID_PARAMETER Invalid parameter.
431 @return EFI_SUCCESS Find the specified variable.
432 @return EFI_NOT_FOUND Not found.
433 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
434
435 **/
436 EFI_STATUS
437 EFIAPI
438 VariableServiceGetNextVariableName (
439 IN OUT UINTN *VariableNameSize,
440 IN OUT CHAR16 *VariableName,
441 IN OUT EFI_GUID *VendorGuid
442 );
443
444 /**
445
446 This code sets variable in storage blocks (Volatile or Non-Volatile).
447
448 @param VariableName Name of Variable to be found.
449 @param VendorGuid Variable vendor GUID.
450 @param Attributes Attribute value of the variable found
451 @param DataSize Size of Data found. If size is less than the
452 data, this value contains the required size.
453 @param Data Data pointer.
454
455 @return EFI_INVALID_PARAMETER Invalid parameter.
456 @return EFI_SUCCESS Set successfully.
457 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
458 @return EFI_NOT_FOUND Not found.
459 @return EFI_WRITE_PROTECTED Variable is read-only.
460
461 **/
462 EFI_STATUS
463 EFIAPI
464 VariableServiceSetVariable (
465 IN CHAR16 *VariableName,
466 IN EFI_GUID *VendorGuid,
467 IN UINT32 Attributes,
468 IN UINTN DataSize,
469 IN VOID *Data
470 );
471
472 /**
473
474 This code returns information about the EFI variables.
475
476 @param Attributes Attributes bitmask to specify the type of variables
477 on which to return information.
478 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
479 for the EFI variables associated with the attributes specified.
480 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
481 for EFI variables associated with the attributes specified.
482 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
483 associated with the attributes specified.
484
485 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
486 @return EFI_SUCCESS Query successfully.
487 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
488
489 **/
490 EFI_STATUS
491 EFIAPI
492 VariableServiceQueryVariableInfo (
493 IN UINT32 Attributes,
494 OUT UINT64 *MaximumVariableStorageSize,
495 OUT UINT64 *RemainingVariableStorageSize,
496 OUT UINT64 *MaximumVariableSize
497 );
498
499 extern VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal;
500
501 #endif