3 Implement ReadOnly Variable Services required by PEIM and install
4 PEI ReadOnly Varaiable2 PPI. These services operates the non volatile storage space.
6 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 EFI_PEI_READ_ONLY_VARIABLE2_PPI mVariablePpi
= {
25 PeiGetNextVariableName
28 EFI_PEI_PPI_DESCRIPTOR mPpiListVariable
= {
29 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
30 &gEfiPeiReadOnlyVariable2PpiGuid
,
36 Provide the functionality of the variable services.
38 @param FileHandle Handle of the file being invoked.
39 Type EFI_PEI_FILE_HANDLE is defined in FfsFindNextFile().
40 @param PeiServices General purpose services available to every PEIM.
42 @retval EFI_SUCCESS If the interface could be successfully installed
43 @retval Others Returned from PeiServicesInstallPpi()
47 PeimInitializeVariableServices (
48 IN EFI_PEI_FILE_HANDLE FileHandle
,
49 IN CONST EFI_PEI_SERVICES
**PeiServices
52 return PeiServicesInstallPpi (&mPpiListVariable
);
57 Gets the pointer to the first variable header in given variable store area.
59 @param VarStoreHeader Pointer to the Variable Store Header.
61 @return Pointer to the first variable header
66 IN VARIABLE_STORE_HEADER
*VarStoreHeader
70 // The end of variable store
72 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
77 This code gets the pointer to the last variable memory pointer byte.
79 @param VarStoreHeader Pointer to the Variable Store Header.
81 @return VARIABLE_HEADER* pointer to last unavailable Variable Header.
86 IN VARIABLE_STORE_HEADER
*VarStoreHeader
90 // The end of variable store
92 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
97 This code checks if variable header is valid or not.
99 @param Variable Pointer to the Variable Header.
101 @retval TRUE Variable header is valid.
102 @retval FALSE Variable header is not valid.
106 IsValidVariableHeader (
107 IN VARIABLE_HEADER
*Variable
110 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
119 This code gets the size of name of variable.
121 @param Variable Pointer to the Variable Header.
123 @return Size of variable in bytes in type UINTN.
128 IN VARIABLE_HEADER
*Variable
131 if (Variable
->State
== (UINT8
) (-1) ||
132 Variable
->DataSize
== (UINT32
) (-1) ||
133 Variable
->NameSize
== (UINT32
) (-1) ||
134 Variable
->Attributes
== (UINT32
) (-1)) {
137 return (UINTN
) Variable
->NameSize
;
142 This code gets the size of data of variable.
144 @param Variable Pointer to the Variable Header.
146 @return Size of variable in bytes in type UINTN.
151 IN VARIABLE_HEADER
*Variable
154 if (Variable
->State
== (UINT8
) (-1) ||
155 Variable
->DataSize
== (UINT32
) (-1) ||
156 Variable
->NameSize
== (UINT32
) (-1) ||
157 Variable
->Attributes
== (UINT32
) (-1)) {
160 return (UINTN
) Variable
->DataSize
;
164 This code gets the pointer to the variable name.
166 @param Variable Pointer to the Variable Header.
168 @return A CHAR16* pointer to Variable Name.
173 IN VARIABLE_HEADER
*Variable
176 return (CHAR16
*) (Variable
+ 1);
181 This code gets the pointer to the variable data.
183 @param Variable Pointer to the Variable Header.
184 @param VariableHeader Pointer to the Variable Header that has consecutive content.
186 @return A UINT8* pointer to Variable Data.
191 IN VARIABLE_HEADER
*Variable
,
192 IN VARIABLE_HEADER
*VariableHeader
198 // Be careful about pad size for alignment
200 Value
= (UINTN
) GetVariableNamePtr (Variable
);
201 Value
+= NameSizeOfVariable (VariableHeader
);
202 Value
+= GET_PAD_SIZE (NameSizeOfVariable (VariableHeader
));
204 return (UINT8
*) Value
;
209 This code gets the pointer to the next variable header.
211 @param StoreInfo Pointer to variable store info structure.
212 @param Variable Pointer to the Variable Header.
213 @param VariableHeader Pointer to the Variable Header that has consecutive content.
215 @return A VARIABLE_HEADER* pointer to next variable header.
220 IN VARIABLE_STORE_INFO
*StoreInfo
,
221 IN VARIABLE_HEADER
*Variable
,
222 IN VARIABLE_HEADER
*VariableHeader
225 EFI_PHYSICAL_ADDRESS TargetAddress
;
226 EFI_PHYSICAL_ADDRESS SpareAddress
;
229 Value
= (UINTN
) GetVariableDataPtr (Variable
, VariableHeader
);
230 Value
+= DataSizeOfVariable (VariableHeader
);
231 Value
+= GET_PAD_SIZE (DataSizeOfVariable (VariableHeader
));
233 // Be careful about pad size for alignment
235 Value
= HEADER_ALIGN (Value
);
237 if (StoreInfo
->FtwLastWriteData
!= NULL
) {
238 TargetAddress
= StoreInfo
->FtwLastWriteData
->TargetAddress
;
239 SpareAddress
= StoreInfo
->FtwLastWriteData
->SpareAddress
;
240 if (((UINTN
) Variable
< (UINTN
) TargetAddress
) && (Value
>= (UINTN
) TargetAddress
)) {
242 // Next variable is in spare block.
244 Value
= (UINTN
) SpareAddress
+ (Value
- (UINTN
) TargetAddress
);
248 return (VARIABLE_HEADER
*) Value
;
252 Get variable store status.
254 @param VarStoreHeader Pointer to the Variable Store Header.
256 @retval EfiRaw Variable store is raw
257 @retval EfiValid Variable store is valid
258 @retval EfiInvalid Variable store is invalid
261 VARIABLE_STORE_STATUS
262 GetVariableStoreStatus (
263 IN VARIABLE_STORE_HEADER
*VarStoreHeader
266 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiVariableGuid
) &&
267 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
268 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
274 if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
275 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
276 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
277 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
278 VarStoreHeader
->Size
== 0xffffffff &&
279 VarStoreHeader
->Format
== 0xff &&
280 VarStoreHeader
->State
== 0xff
290 Compare two variable names, one of them may be inconsecutive.
292 @param StoreInfo Pointer to variable store info structure.
293 @param Name1 Pointer to one variable name.
294 @param Name2 Pointer to another variable name.
295 @param NameSize Variable name size.
297 @retval TRUE Name1 and Name2 are identical.
298 @retval FALSE Name1 and Name2 are not identical.
302 CompareVariableName (
303 IN VARIABLE_STORE_INFO
*StoreInfo
,
304 IN CONST CHAR16
*Name1
,
305 IN CONST CHAR16
*Name2
,
309 EFI_PHYSICAL_ADDRESS TargetAddress
;
310 EFI_PHYSICAL_ADDRESS SpareAddress
;
311 UINTN PartialNameSize
;
313 if (StoreInfo
->FtwLastWriteData
!= NULL
) {
314 TargetAddress
= StoreInfo
->FtwLastWriteData
->TargetAddress
;
315 SpareAddress
= StoreInfo
->FtwLastWriteData
->SpareAddress
;
316 if (((UINTN
) Name1
< (UINTN
) TargetAddress
) && (((UINTN
) Name1
+ NameSize
) > (UINTN
) TargetAddress
)) {
318 // Name1 is inconsecutive.
320 PartialNameSize
= (UINTN
) TargetAddress
- (UINTN
) Name1
;
322 // Partial content is in NV storage.
324 if (CompareMem ((UINT8
*) Name1
, (UINT8
*) Name2
, PartialNameSize
) == 0) {
326 // Another partial content is in spare block.
328 if (CompareMem ((UINT8
*) (UINTN
) SpareAddress
, (UINT8
*) Name2
+ PartialNameSize
, NameSize
- PartialNameSize
) == 0) {
333 } else if (((UINTN
) Name2
< (UINTN
) TargetAddress
) && (((UINTN
) Name2
+ NameSize
) > (UINTN
) TargetAddress
)) {
335 // Name2 is inconsecutive.
337 PartialNameSize
= (UINTN
) TargetAddress
- (UINTN
) Name2
;
339 // Partial content is in NV storage.
341 if (CompareMem ((UINT8
*) Name2
, (UINT8
*) Name1
, PartialNameSize
) == 0) {
343 // Another partial content is in spare block.
345 if (CompareMem ((UINT8
*) (UINTN
) SpareAddress
, (UINT8
*) Name1
+ PartialNameSize
, NameSize
- PartialNameSize
) == 0) {
354 // Both Name1 and Name2 are consecutive.
356 if (CompareMem ((UINT8
*) Name1
, (UINT8
*) Name2
, NameSize
) == 0) {
363 This function compares a variable with variable entries in database.
365 @param StoreInfo Pointer to variable store info structure.
366 @param Variable Pointer to the variable in our database
367 @param VariableHeader Pointer to the Variable Header that has consecutive content.
368 @param VariableName Name of the variable to compare to 'Variable'
369 @param VendorGuid GUID of the variable to compare to 'Variable'
370 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
372 @retval EFI_SUCCESS Found match variable
373 @retval EFI_NOT_FOUND Variable not found
377 CompareWithValidVariable (
378 IN VARIABLE_STORE_INFO
*StoreInfo
,
379 IN VARIABLE_HEADER
*Variable
,
380 IN VARIABLE_HEADER
*VariableHeader
,
381 IN CONST CHAR16
*VariableName
,
382 IN CONST EFI_GUID
*VendorGuid
,
383 OUT VARIABLE_POINTER_TRACK
*PtrTrack
388 if (VariableName
[0] == 0) {
389 PtrTrack
->CurrPtr
= Variable
;
393 // Don't use CompareGuid function here for performance reasons.
394 // Instead we compare the GUID a UINT32 at a time and branch
395 // on the first failed comparison.
397 if ((((INT32
*) VendorGuid
)[0] == ((INT32
*) &VariableHeader
->VendorGuid
)[0]) &&
398 (((INT32
*) VendorGuid
)[1] == ((INT32
*) &VariableHeader
->VendorGuid
)[1]) &&
399 (((INT32
*) VendorGuid
)[2] == ((INT32
*) &VariableHeader
->VendorGuid
)[2]) &&
400 (((INT32
*) VendorGuid
)[3] == ((INT32
*) &VariableHeader
->VendorGuid
)[3])
402 ASSERT (NameSizeOfVariable (VariableHeader
) != 0);
403 Point
= (VOID
*) GetVariableNamePtr (Variable
);
404 if (CompareVariableName (StoreInfo
, VariableName
, Point
, NameSizeOfVariable (VariableHeader
))) {
405 PtrTrack
->CurrPtr
= Variable
;
411 return EFI_NOT_FOUND
;
415 Return the variable store header and the store info based on the Index.
417 @param Type The type of the variable store.
418 @param StoreInfo Return the store info.
420 @return Pointer to the variable store header.
422 VARIABLE_STORE_HEADER
*
424 IN VARIABLE_STORE_TYPE Type
,
425 OUT VARIABLE_STORE_INFO
*StoreInfo
428 EFI_HOB_GUID_TYPE
*GuidHob
;
429 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
430 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
431 EFI_PHYSICAL_ADDRESS NvStorageBase
;
432 UINT32 NvStorageSize
;
433 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*FtwLastWriteData
;
436 StoreInfo
->IndexTable
= NULL
;
437 StoreInfo
->FtwLastWriteData
= NULL
;
438 VariableStoreHeader
= NULL
;
440 case VariableStoreTypeHob
:
441 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
442 if (GuidHob
!= NULL
) {
443 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) GET_GUID_HOB_DATA (GuidHob
);
447 case VariableStoreTypeNv
:
448 if (GetBootModeHob () != BOOT_IN_RECOVERY_MODE
) {
450 // The content of NV storage for variable is not reliable in recovery boot mode.
453 NvStorageSize
= PcdGet32 (PcdFlashNvStorageVariableSize
);
454 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) (PcdGet64 (PcdFlashNvStorageVariableBase64
) != 0 ?
455 PcdGet64 (PcdFlashNvStorageVariableBase64
) :
456 PcdGet32 (PcdFlashNvStorageVariableBase
)
459 // First let FvHeader point to NV storage base.
461 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) NvStorageBase
;
464 // Check the FTW last write data hob.
467 GuidHob
= GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid
);
468 if (GuidHob
!= NULL
) {
469 FtwLastWriteData
= (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*) GET_GUID_HOB_DATA (GuidHob
);
470 if (FtwLastWriteData
->TargetAddress
== NvStorageBase
) {
472 // Let FvHeader point to spare block.
474 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) FtwLastWriteData
->SpareAddress
;
475 DEBUG ((EFI_D_INFO
, "PeiVariable: NV storage is backed up in spare block: 0x%x\n", (UINTN
) FtwLastWriteData
->SpareAddress
));
476 } else if ((FtwLastWriteData
->TargetAddress
> NvStorageBase
) && (FtwLastWriteData
->TargetAddress
< (NvStorageBase
+ NvStorageSize
))) {
477 StoreInfo
->FtwLastWriteData
= FtwLastWriteData
;
479 // Flash NV storage from the offset is backed up in spare block.
481 BackUpOffset
= (UINT32
) (FtwLastWriteData
->TargetAddress
- NvStorageBase
);
482 DEBUG ((EFI_D_INFO
, "PeiVariable: High partial NV storage from offset: %x is backed up in spare block: 0x%x\n", BackUpOffset
, (UINTN
) FtwLastWriteData
->SpareAddress
));
484 // At least one block data in flash NV storage is still valid, so still leave FvHeader point to NV storage base.
490 // Check if the Firmware Volume is not corrupted
492 if ((FvHeader
->Signature
!= EFI_FVH_SIGNATURE
) || (!CompareGuid (&gEfiSystemNvDataFvGuid
, &FvHeader
->FileSystemGuid
))) {
493 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
497 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINT8
*) FvHeader
+ FvHeader
->HeaderLength
);
499 GuidHob
= GetFirstGuidHob (&gEfiVariableIndexTableGuid
);
500 if (GuidHob
!= NULL
) {
501 StoreInfo
->IndexTable
= GET_GUID_HOB_DATA (GuidHob
);
504 // If it's the first time to access variable region in flash, create a guid hob to record
505 // VAR_ADDED type variable info.
506 // Note that as the resource of PEI phase is limited, only store the limited number of
507 // VAR_ADDED type variables to reduce access time.
509 StoreInfo
->IndexTable
= (VARIABLE_INDEX_TABLE
*) BuildGuidHob (&gEfiVariableIndexTableGuid
, sizeof (VARIABLE_INDEX_TABLE
));
510 StoreInfo
->IndexTable
->Length
= 0;
511 StoreInfo
->IndexTable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
512 StoreInfo
->IndexTable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
513 StoreInfo
->IndexTable
->GoneThrough
= 0;
523 StoreInfo
->VariableStoreHeader
= VariableStoreHeader
;
524 return VariableStoreHeader
;
528 Get variable header that has consecutive content.
530 @param StoreInfo Pointer to variable store info structure.
531 @param Variable Pointer to the Variable Header.
532 @param VariableHeader Pointer to Pointer to the Variable Header that has consecutive content.
534 @retval TRUE Variable header is valid.
535 @retval FALSE Variable header is not valid.
540 IN VARIABLE_STORE_INFO
*StoreInfo
,
541 IN VARIABLE_HEADER
*Variable
,
542 OUT VARIABLE_HEADER
**VariableHeader
545 EFI_PHYSICAL_ADDRESS TargetAddress
;
546 EFI_PHYSICAL_ADDRESS SpareAddress
;
547 EFI_HOB_GUID_TYPE
*GuidHob
;
548 UINTN PartialHeaderSize
;
550 if (Variable
== NULL
) {
555 // First assume variable header pointed by Variable is consecutive.
557 *VariableHeader
= Variable
;
559 if (StoreInfo
->FtwLastWriteData
!= NULL
) {
560 TargetAddress
= StoreInfo
->FtwLastWriteData
->TargetAddress
;
561 SpareAddress
= StoreInfo
->FtwLastWriteData
->SpareAddress
;
562 if (((UINTN
) Variable
> (UINTN
) SpareAddress
) &&
563 (((UINTN
) Variable
- (UINTN
) SpareAddress
+ (UINTN
) TargetAddress
) >= (UINTN
) GetEndPointer (StoreInfo
->VariableStoreHeader
))) {
565 // Reach the end of variable store.
569 if (((UINTN
) Variable
< (UINTN
) TargetAddress
) && (((UINTN
) Variable
+ sizeof (VARIABLE_HEADER
)) > (UINTN
) TargetAddress
)) {
571 // Variable header pointed by Variable is inconsecutive,
572 // create a guid hob to combine the two partial variable header content together.
574 GuidHob
= GetFirstGuidHob (&gEfiCallerIdGuid
);
575 if (GuidHob
!= NULL
) {
576 *VariableHeader
= (VARIABLE_HEADER
*) GET_GUID_HOB_DATA (GuidHob
);
578 *VariableHeader
= (VARIABLE_HEADER
*) BuildGuidHob (&gEfiCallerIdGuid
, sizeof (VARIABLE_HEADER
));
579 PartialHeaderSize
= (UINTN
) TargetAddress
- (UINTN
) Variable
;
581 // Partial content is in NV storage.
583 CopyMem ((UINT8
*) *VariableHeader
, (UINT8
*) Variable
, PartialHeaderSize
);
585 // Another partial content is in spare block.
587 CopyMem ((UINT8
*) *VariableHeader
+ PartialHeaderSize
, (UINT8
*) (UINTN
) SpareAddress
, sizeof (VARIABLE_HEADER
) - PartialHeaderSize
);
591 if (Variable
>= GetEndPointer (StoreInfo
->VariableStoreHeader
)) {
593 // Reach the end of variable store.
599 return IsValidVariableHeader (*VariableHeader
);
603 Get variable name or data to output buffer.
605 @param StoreInfo Pointer to variable store info structure.
606 @param NameOrData Pointer to the variable name/data that may be inconsecutive.
607 @param Size Variable name/data size.
608 @param Buffer Pointer to output buffer to hold the variable name/data.
612 GetVariableNameOrData (
613 IN VARIABLE_STORE_INFO
*StoreInfo
,
614 IN UINT8
*NameOrData
,
619 EFI_PHYSICAL_ADDRESS TargetAddress
;
620 EFI_PHYSICAL_ADDRESS SpareAddress
;
623 if (StoreInfo
->FtwLastWriteData
!= NULL
) {
624 TargetAddress
= StoreInfo
->FtwLastWriteData
->TargetAddress
;
625 SpareAddress
= StoreInfo
->FtwLastWriteData
->SpareAddress
;
626 if (((UINTN
) NameOrData
< (UINTN
) TargetAddress
) && (((UINTN
) NameOrData
+ Size
) > (UINTN
) TargetAddress
)) {
628 // Variable name/data is inconsecutive.
630 PartialSize
= (UINTN
) TargetAddress
- (UINTN
) NameOrData
;
632 // Partial content is in NV storage.
634 CopyMem (Buffer
, NameOrData
, PartialSize
);
636 // Another partial content is in spare block.
638 CopyMem (Buffer
+ PartialSize
, (UINT8
*) (UINTN
) SpareAddress
, Size
- PartialSize
);
644 // Variable name/data is consecutive.
646 CopyMem (Buffer
, NameOrData
, Size
);
650 Find the variable in the specified variable store.
652 @param StoreInfo Pointer to the store info structure.
653 @param VariableName Name of the variable to be found
654 @param VendorGuid Vendor GUID to be found.
655 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
657 @retval EFI_SUCCESS Variable found successfully
658 @retval EFI_NOT_FOUND Variable not found
659 @retval EFI_INVALID_PARAMETER Invalid variable name
664 IN VARIABLE_STORE_INFO
*StoreInfo
,
665 IN CONST CHAR16
*VariableName
,
666 IN CONST EFI_GUID
*VendorGuid
,
667 OUT VARIABLE_POINTER_TRACK
*PtrTrack
670 VARIABLE_HEADER
*Variable
;
671 VARIABLE_HEADER
*LastVariable
;
672 VARIABLE_HEADER
*MaxIndex
;
676 VARIABLE_HEADER
*InDeletedVariable
;
677 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
678 VARIABLE_INDEX_TABLE
*IndexTable
;
679 VARIABLE_HEADER
*VariableHeader
;
681 VariableStoreHeader
= StoreInfo
->VariableStoreHeader
;
683 if (VariableStoreHeader
== NULL
) {
684 return EFI_INVALID_PARAMETER
;
687 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
688 return EFI_UNSUPPORTED
;
691 if (~VariableStoreHeader
->Size
== 0) {
692 return EFI_NOT_FOUND
;
695 IndexTable
= StoreInfo
->IndexTable
;
696 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
);
697 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
);
699 InDeletedVariable
= NULL
;
702 // No Variable Address equals zero, so 0 as initial value is safe.
705 VariableHeader
= NULL
;
707 if (IndexTable
!= NULL
) {
709 // traverse the variable index table to look for varible.
710 // The IndexTable->Index[Index] records the distance of two neighbouring VAR_ADDED type variables.
712 for (Offset
= 0, Index
= 0; Index
< IndexTable
->Length
; Index
++) {
713 ASSERT (Index
< sizeof (IndexTable
->Index
) / sizeof (IndexTable
->Index
[0]));
714 Offset
+= IndexTable
->Index
[Index
];
715 MaxIndex
= (VARIABLE_HEADER
*) ((UINT8
*) IndexTable
->StartPtr
+ Offset
);
716 GetVariableHeader (StoreInfo
, MaxIndex
, &VariableHeader
);
717 if (CompareWithValidVariable (StoreInfo
, MaxIndex
, VariableHeader
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
718 if (VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
719 InDeletedVariable
= PtrTrack
->CurrPtr
;
726 if (IndexTable
->GoneThrough
!= 0) {
728 // If the table has all the existing variables indexed, return.
730 PtrTrack
->CurrPtr
= InDeletedVariable
;
731 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
735 if (MaxIndex
!= NULL
) {
737 // HOB exists but the variable cannot be found in HOB
738 // If not found in HOB, then let's start from the MaxIndex we've found.
740 Variable
= GetNextVariablePtr (StoreInfo
, MaxIndex
, VariableHeader
);
741 LastVariable
= MaxIndex
;
744 // Start Pointers for the variable.
745 // Actual Data Pointer where data can be written.
747 Variable
= PtrTrack
->StartPtr
;
748 LastVariable
= PtrTrack
->StartPtr
;
752 // Find the variable by walk through variable store
755 while (GetVariableHeader (StoreInfo
, Variable
, &VariableHeader
)) {
756 if (VariableHeader
->State
== VAR_ADDED
|| VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
758 // Record Variable in VariableIndex HOB
760 if ((IndexTable
!= NULL
) && !StopRecord
) {
761 Offset
= (UINTN
) Variable
- (UINTN
) LastVariable
;
762 if ((Offset
> 0x0FFFF) || (IndexTable
->Length
== sizeof (IndexTable
->Index
) / sizeof (IndexTable
->Index
[0]))) {
764 // Stop to record if the distance of two neighbouring VAR_ADDED variable is larger than the allowable scope(UINT16),
765 // or the record buffer is full.
769 IndexTable
->Index
[IndexTable
->Length
++] = (UINT16
) Offset
;
770 LastVariable
= Variable
;
774 if (CompareWithValidVariable (StoreInfo
, Variable
, VariableHeader
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
775 if (VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
776 InDeletedVariable
= PtrTrack
->CurrPtr
;
783 Variable
= GetNextVariablePtr (StoreInfo
, Variable
, VariableHeader
);
786 // If gone through the VariableStore, that means we never find in Firmware any more.
788 if ((IndexTable
!= NULL
) && !StopRecord
) {
789 IndexTable
->GoneThrough
= 1;
792 PtrTrack
->CurrPtr
= InDeletedVariable
;
794 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
798 Find the variable in HOB and Non-Volatile variable storages.
800 @param VariableName Name of the variable to be found
801 @param VendorGuid Vendor GUID to be found.
802 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
803 @param StoreInfo Return the store info.
805 @retval EFI_SUCCESS Variable found successfully
806 @retval EFI_NOT_FOUND Variable not found
807 @retval EFI_INVALID_PARAMETER Invalid variable name
811 IN CONST CHAR16
*VariableName
,
812 IN CONST EFI_GUID
*VendorGuid
,
813 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
814 OUT VARIABLE_STORE_INFO
*StoreInfo
818 VARIABLE_STORE_TYPE Type
;
820 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
821 return EFI_INVALID_PARAMETER
;
824 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
825 GetVariableStore (Type
, StoreInfo
);
826 Status
= FindVariableEx (
832 if (!EFI_ERROR (Status
)) {
837 return EFI_NOT_FOUND
;
841 This service retrieves a variable's value using its name and GUID.
843 Read the specified variable from the UEFI variable store. If the Data
844 buffer is too small to hold the contents of the variable, the error
845 EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer
846 size to obtain the data.
848 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
849 @param VariableName A pointer to a null-terminated string that is the variable's name.
850 @param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of
851 VariableGuid and VariableName must be unique.
852 @param Attributes If non-NULL, on return, points to the variable's attributes.
853 @param DataSize On entry, points to the size in bytes of the Data buffer.
854 On return, points to the size of the data returned in Data.
855 @param Data Points to the buffer which will hold the returned variable value.
857 @retval EFI_SUCCESS The variable was read successfully.
858 @retval EFI_NOT_FOUND The variable could not be found.
859 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data.
860 DataSize is updated with the size required for
861 the specified variable.
862 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.
863 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
869 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
870 IN CONST CHAR16
*VariableName
,
871 IN CONST EFI_GUID
*VariableGuid
,
872 OUT UINT32
*Attributes
,
873 IN OUT UINTN
*DataSize
,
877 VARIABLE_POINTER_TRACK Variable
;
880 VARIABLE_STORE_INFO StoreInfo
;
881 VARIABLE_HEADER
*VariableHeader
;
883 if (VariableName
== NULL
|| VariableGuid
== NULL
|| DataSize
== NULL
) {
884 return EFI_INVALID_PARAMETER
;
887 VariableHeader
= NULL
;
890 // Find existing variable
892 Status
= FindVariable (VariableName
, VariableGuid
, &Variable
, &StoreInfo
);
893 if (EFI_ERROR (Status
)) {
896 GetVariableHeader (&StoreInfo
, Variable
.CurrPtr
, &VariableHeader
);
901 VarDataSize
= DataSizeOfVariable (VariableHeader
);
902 if (*DataSize
>= VarDataSize
) {
904 return EFI_INVALID_PARAMETER
;
907 GetVariableNameOrData (&StoreInfo
, GetVariableDataPtr (Variable
.CurrPtr
, VariableHeader
), VarDataSize
, Data
);
909 if (Attributes
!= NULL
) {
910 *Attributes
= VariableHeader
->Attributes
;
913 *DataSize
= VarDataSize
;
916 *DataSize
= VarDataSize
;
917 return EFI_BUFFER_TOO_SMALL
;
922 Return the next variable name and GUID.
924 This function is called multiple times to retrieve the VariableName
925 and VariableGuid of all variables currently available in the system.
926 On each call, the previous results are passed into the interface,
927 and, on return, the interface returns the data for the next
928 interface. When the entire variable list has been returned,
929 EFI_NOT_FOUND is returned.
931 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
933 @param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.
934 On return, the size of the variable name buffer.
935 @param VariableName On entry, a pointer to a null-terminated string that is the variable's name.
936 On return, points to the next variable's null-terminated name string.
937 @param VariableGuid On entry, a pointer to an EFI_GUID that is the variable's GUID.
938 On return, a pointer to the next variable's GUID.
940 @retval EFI_SUCCESS The variable was read successfully.
941 @retval EFI_NOT_FOUND The variable could not be found.
942 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting
943 data. VariableNameSize is updated with the size
944 required for the specified variable.
945 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or
946 VariableNameSize is NULL.
947 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
952 PeiGetNextVariableName (
953 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
954 IN OUT UINTN
*VariableNameSize
,
955 IN OUT CHAR16
*VariableName
,
956 IN OUT EFI_GUID
*VariableGuid
959 VARIABLE_STORE_TYPE Type
;
960 VARIABLE_POINTER_TRACK Variable
;
961 VARIABLE_POINTER_TRACK VariableInHob
;
962 VARIABLE_POINTER_TRACK VariablePtrTrack
;
965 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
966 VARIABLE_HEADER
*VariableHeader
;
967 VARIABLE_STORE_INFO StoreInfo
;
968 VARIABLE_STORE_INFO StoreInfoForNv
;
969 VARIABLE_STORE_INFO StoreInfoForHob
;
971 if (VariableName
== NULL
|| VariableGuid
== NULL
|| VariableNameSize
== NULL
) {
972 return EFI_INVALID_PARAMETER
;
975 VariableHeader
= NULL
;
977 Status
= FindVariable (VariableName
, VariableGuid
, &Variable
, &StoreInfo
);
978 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
982 if (VariableName
[0] != 0) {
984 // If variable name is not NULL, get next variable
986 GetVariableHeader (&StoreInfo
, Variable
.CurrPtr
, &VariableHeader
);
987 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);
990 VariableStoreHeader
[VariableStoreTypeHob
] = GetVariableStore (VariableStoreTypeHob
, &StoreInfoForHob
);
991 VariableStoreHeader
[VariableStoreTypeNv
] = GetVariableStore (VariableStoreTypeNv
, &StoreInfoForNv
);
995 // Switch from HOB to Non-Volatile.
997 while (!GetVariableHeader (&StoreInfo
, Variable
.CurrPtr
, &VariableHeader
)) {
999 // Find current storage index
1001 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
1002 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
1006 ASSERT (Type
< VariableStoreTypeMax
);
1008 // Switch to next storage
1010 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
1011 if (VariableStoreHeader
[Type
] != NULL
) {
1016 // Capture the case that
1017 // 1. current storage is the last one, or
1018 // 2. no further storage
1020 if (Type
== VariableStoreTypeMax
) {
1021 return EFI_NOT_FOUND
;
1023 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
1024 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
1025 Variable
.CurrPtr
= Variable
.StartPtr
;
1026 GetVariableStore (Type
, &StoreInfo
);
1029 if (VariableHeader
->State
== VAR_ADDED
|| VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1030 if (VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1032 // If it is a IN_DELETED_TRANSITION variable,
1033 // and there is also a same ADDED one at the same time,
1036 Status
= FindVariableEx (
1038 GetVariableNamePtr (Variable
.CurrPtr
),
1039 &VariableHeader
->VendorGuid
,
1042 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
!= Variable
.CurrPtr
) {
1043 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);
1049 // Don't return NV variable when HOB overrides it
1051 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
1052 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
1054 Status
= FindVariableEx (
1056 GetVariableNamePtr (Variable
.CurrPtr
),
1057 &VariableHeader
->VendorGuid
,
1060 if (!EFI_ERROR (Status
)) {
1061 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);
1066 VarNameSize
= NameSizeOfVariable (VariableHeader
);
1067 ASSERT (VarNameSize
!= 0);
1069 if (VarNameSize
<= *VariableNameSize
) {
1070 GetVariableNameOrData (&StoreInfo
, (UINT8
*) GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
, (UINT8
*) VariableName
);
1072 CopyMem (VariableGuid
, &VariableHeader
->VendorGuid
, sizeof (EFI_GUID
));
1074 Status
= EFI_SUCCESS
;
1076 Status
= EFI_BUFFER_TOO_SMALL
;
1079 *VariableNameSize
= VarNameSize
;
1081 // Variable is found
1085 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);