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