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