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 - 2013, 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
;
551 // First assume variable header pointed by Variable is consecutive.
553 *VariableHeader
= Variable
;
555 if ((Variable
!= NULL
) && (StoreInfo
->FtwLastWriteData
!= NULL
)) {
556 TargetAddress
= StoreInfo
->FtwLastWriteData
->TargetAddress
;
557 SpareAddress
= StoreInfo
->FtwLastWriteData
->SpareAddress
;
558 if (((UINTN
) Variable
< (UINTN
) TargetAddress
) && (((UINTN
) Variable
+ sizeof (VARIABLE_HEADER
)) > (UINTN
) TargetAddress
)) {
560 // Variable header pointed by Variable is inconsecutive,
561 // create a guid hob to combine the two partial variable header content together.
563 GuidHob
= GetFirstGuidHob (&gEfiCallerIdGuid
);
564 if (GuidHob
!= NULL
) {
565 *VariableHeader
= (VARIABLE_HEADER
*) GET_GUID_HOB_DATA (GuidHob
);
567 *VariableHeader
= (VARIABLE_HEADER
*) BuildGuidHob (&gEfiCallerIdGuid
, sizeof (VARIABLE_HEADER
));
568 PartialHeaderSize
= (UINTN
) TargetAddress
- (UINTN
) Variable
;
570 // Partial content is in NV storage.
572 CopyMem ((UINT8
*) *VariableHeader
, (UINT8
*) Variable
, PartialHeaderSize
);
574 // Another partial content is in spare block.
576 CopyMem ((UINT8
*) *VariableHeader
+ PartialHeaderSize
, (UINT8
*) (UINTN
) SpareAddress
, sizeof (VARIABLE_HEADER
) - PartialHeaderSize
);
581 return IsValidVariableHeader (*VariableHeader
);
585 Get variable name or data to output buffer.
587 @param StoreInfo Pointer to variable store info structure.
588 @param NameOrData Pointer to the variable name/data that may be inconsecutive.
589 @param Size Variable name/data size.
590 @param Buffer Pointer to output buffer to hold the variable name/data.
594 GetVariableNameOrData (
595 IN VARIABLE_STORE_INFO
*StoreInfo
,
596 IN UINT8
*NameOrData
,
601 EFI_PHYSICAL_ADDRESS TargetAddress
;
602 EFI_PHYSICAL_ADDRESS SpareAddress
;
605 if (StoreInfo
->FtwLastWriteData
!= NULL
) {
606 TargetAddress
= StoreInfo
->FtwLastWriteData
->TargetAddress
;
607 SpareAddress
= StoreInfo
->FtwLastWriteData
->SpareAddress
;
608 if (((UINTN
) NameOrData
< (UINTN
) TargetAddress
) && (((UINTN
) NameOrData
+ Size
) > (UINTN
) TargetAddress
)) {
610 // Variable name/data is inconsecutive.
612 PartialSize
= (UINTN
) TargetAddress
- (UINTN
) NameOrData
;
614 // Partial content is in NV storage.
616 CopyMem (Buffer
, NameOrData
, PartialSize
);
618 // Another partial content is in spare block.
620 CopyMem (Buffer
+ PartialSize
, (UINT8
*) (UINTN
) SpareAddress
, Size
- PartialSize
);
626 // Variable name/data is consecutive.
628 CopyMem (Buffer
, NameOrData
, Size
);
632 Find the variable in the specified variable store.
634 @param StoreInfo Pointer to the store info structure.
635 @param VariableName Name of the variable to be found
636 @param VendorGuid Vendor GUID to be found.
637 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
639 @retval EFI_SUCCESS Variable found successfully
640 @retval EFI_NOT_FOUND Variable not found
641 @retval EFI_INVALID_PARAMETER Invalid variable name
646 IN VARIABLE_STORE_INFO
*StoreInfo
,
647 IN CONST CHAR16
*VariableName
,
648 IN CONST EFI_GUID
*VendorGuid
,
649 OUT VARIABLE_POINTER_TRACK
*PtrTrack
652 VARIABLE_HEADER
*Variable
;
653 VARIABLE_HEADER
*LastVariable
;
654 VARIABLE_HEADER
*MaxIndex
;
658 VARIABLE_HEADER
*InDeletedVariable
;
659 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
660 VARIABLE_INDEX_TABLE
*IndexTable
;
661 VARIABLE_HEADER
*VariableHeader
;
663 VariableStoreHeader
= StoreInfo
->VariableStoreHeader
;
665 if (VariableStoreHeader
== NULL
) {
666 return EFI_INVALID_PARAMETER
;
669 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
670 return EFI_UNSUPPORTED
;
673 if (~VariableStoreHeader
->Size
== 0) {
674 return EFI_NOT_FOUND
;
677 IndexTable
= StoreInfo
->IndexTable
;
678 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
);
679 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
);
681 InDeletedVariable
= NULL
;
684 // No Variable Address equals zero, so 0 as initial value is safe.
687 VariableHeader
= NULL
;
689 if (IndexTable
!= NULL
) {
691 // traverse the variable index table to look for varible.
692 // The IndexTable->Index[Index] records the distance of two neighbouring VAR_ADDED type variables.
694 for (Offset
= 0, Index
= 0; Index
< IndexTable
->Length
; Index
++) {
695 ASSERT (Index
< sizeof (IndexTable
->Index
) / sizeof (IndexTable
->Index
[0]));
696 Offset
+= IndexTable
->Index
[Index
];
697 MaxIndex
= (VARIABLE_HEADER
*) ((UINT8
*) IndexTable
->StartPtr
+ Offset
);
698 GetVariableHeader (StoreInfo
, MaxIndex
, &VariableHeader
);
699 if (CompareWithValidVariable (StoreInfo
, MaxIndex
, VariableHeader
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
700 if (VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
701 InDeletedVariable
= PtrTrack
->CurrPtr
;
708 if (IndexTable
->GoneThrough
!= 0) {
710 // If the table has all the existing variables indexed, return.
712 PtrTrack
->CurrPtr
= InDeletedVariable
;
713 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
717 if (MaxIndex
!= NULL
) {
719 // HOB exists but the variable cannot be found in HOB
720 // If not found in HOB, then let's start from the MaxIndex we've found.
722 Variable
= GetNextVariablePtr (StoreInfo
, MaxIndex
, VariableHeader
);
723 LastVariable
= MaxIndex
;
726 // Start Pointers for the variable.
727 // Actual Data Pointer where data can be written.
729 Variable
= PtrTrack
->StartPtr
;
730 LastVariable
= PtrTrack
->StartPtr
;
734 // Find the variable by walk through variable store
737 while (GetVariableHeader (StoreInfo
, Variable
, &VariableHeader
)) {
738 if (VariableHeader
->State
== VAR_ADDED
|| VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
740 // Record Variable in VariableIndex HOB
742 if ((IndexTable
!= NULL
) && !StopRecord
) {
743 Offset
= (UINTN
) Variable
- (UINTN
) LastVariable
;
744 if ((Offset
> 0x0FFFF) || (IndexTable
->Length
== sizeof (IndexTable
->Index
) / sizeof (IndexTable
->Index
[0]))) {
746 // Stop to record if the distance of two neighbouring VAR_ADDED variable is larger than the allowable scope(UINT16),
747 // or the record buffer is full.
751 IndexTable
->Index
[IndexTable
->Length
++] = (UINT16
) Offset
;
752 LastVariable
= Variable
;
756 if (CompareWithValidVariable (StoreInfo
, Variable
, VariableHeader
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
757 if (VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
758 InDeletedVariable
= PtrTrack
->CurrPtr
;
765 Variable
= GetNextVariablePtr (StoreInfo
, Variable
, VariableHeader
);
768 // If gone through the VariableStore, that means we never find in Firmware any more.
770 if ((IndexTable
!= NULL
) && !StopRecord
) {
771 IndexTable
->GoneThrough
= 1;
774 PtrTrack
->CurrPtr
= InDeletedVariable
;
776 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
780 Find the variable in HOB and Non-Volatile variable storages.
782 @param VariableName Name of the variable to be found
783 @param VendorGuid Vendor GUID to be found.
784 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
785 @param StoreInfo Return the store info.
787 @retval EFI_SUCCESS Variable found successfully
788 @retval EFI_NOT_FOUND Variable not found
789 @retval EFI_INVALID_PARAMETER Invalid variable name
793 IN CONST CHAR16
*VariableName
,
794 IN CONST EFI_GUID
*VendorGuid
,
795 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
796 OUT VARIABLE_STORE_INFO
*StoreInfo
800 VARIABLE_STORE_TYPE Type
;
802 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
803 return EFI_INVALID_PARAMETER
;
806 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
807 GetVariableStore (Type
, StoreInfo
);
808 Status
= FindVariableEx (
814 if (!EFI_ERROR (Status
)) {
819 return EFI_NOT_FOUND
;
823 This service retrieves a variable's value using its name and GUID.
825 Read the specified variable from the UEFI variable store. If the Data
826 buffer is too small to hold the contents of the variable, the error
827 EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer
828 size to obtain the data.
830 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
831 @param VariableName A pointer to a null-terminated string that is the variable's name.
832 @param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of
833 VariableGuid and VariableName must be unique.
834 @param Attributes If non-NULL, on return, points to the variable's attributes.
835 @param DataSize On entry, points to the size in bytes of the Data buffer.
836 On return, points to the size of the data returned in Data.
837 @param Data Points to the buffer which will hold the returned variable value.
839 @retval EFI_SUCCESS The variable was read successfully.
840 @retval EFI_NOT_FOUND The variable could not be found.
841 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data.
842 DataSize is updated with the size required for
843 the specified variable.
844 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.
845 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
851 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
852 IN CONST CHAR16
*VariableName
,
853 IN CONST EFI_GUID
*VariableGuid
,
854 OUT UINT32
*Attributes
,
855 IN OUT UINTN
*DataSize
,
859 VARIABLE_POINTER_TRACK Variable
;
862 VARIABLE_STORE_INFO StoreInfo
;
863 VARIABLE_HEADER
*VariableHeader
;
865 if (VariableName
== NULL
|| VariableGuid
== NULL
|| DataSize
== NULL
) {
866 return EFI_INVALID_PARAMETER
;
870 // Find existing variable
872 Status
= FindVariable (VariableName
, VariableGuid
, &Variable
, &StoreInfo
);
873 if (EFI_ERROR (Status
)) {
876 GetVariableHeader (&StoreInfo
, Variable
.CurrPtr
, &VariableHeader
);
881 VarDataSize
= DataSizeOfVariable (VariableHeader
);
882 if (*DataSize
>= VarDataSize
) {
884 return EFI_INVALID_PARAMETER
;
887 GetVariableNameOrData (&StoreInfo
, GetVariableDataPtr (Variable
.CurrPtr
, VariableHeader
), VarDataSize
, Data
);
889 if (Attributes
!= NULL
) {
890 *Attributes
= VariableHeader
->Attributes
;
893 *DataSize
= VarDataSize
;
896 *DataSize
= VarDataSize
;
897 return EFI_BUFFER_TOO_SMALL
;
902 Return the next variable name and GUID.
904 This function is called multiple times to retrieve the VariableName
905 and VariableGuid of all variables currently available in the system.
906 On each call, the previous results are passed into the interface,
907 and, on return, the interface returns the data for the next
908 interface. When the entire variable list has been returned,
909 EFI_NOT_FOUND is returned.
911 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
913 @param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.
914 On return, the size of the variable name buffer.
915 @param VariableName On entry, a pointer to a null-terminated string that is the variable's name.
916 On return, points to the next variable's null-terminated name string.
917 @param VariableGuid On entry, a pointer to an EFI_GUID that is the variable's GUID.
918 On return, a pointer to the next variable's GUID.
920 @retval EFI_SUCCESS The variable was read successfully.
921 @retval EFI_NOT_FOUND The variable could not be found.
922 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting
923 data. VariableNameSize is updated with the size
924 required for the specified variable.
925 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or
926 VariableNameSize is NULL.
927 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
932 PeiGetNextVariableName (
933 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
934 IN OUT UINTN
*VariableNameSize
,
935 IN OUT CHAR16
*VariableName
,
936 IN OUT EFI_GUID
*VariableGuid
939 VARIABLE_STORE_TYPE Type
;
940 VARIABLE_POINTER_TRACK Variable
;
941 VARIABLE_POINTER_TRACK VariableInHob
;
942 VARIABLE_POINTER_TRACK VariablePtrTrack
;
945 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
946 VARIABLE_HEADER
*VariableHeader
;
947 VARIABLE_STORE_INFO StoreInfo
;
948 VARIABLE_STORE_INFO StoreInfoForNv
;
949 VARIABLE_STORE_INFO StoreInfoForHob
;
951 if (VariableName
== NULL
|| VariableGuid
== NULL
|| VariableNameSize
== NULL
) {
952 return EFI_INVALID_PARAMETER
;
955 Status
= FindVariable (VariableName
, VariableGuid
, &Variable
, &StoreInfo
);
956 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
960 if (VariableName
[0] != 0) {
962 // If variable name is not NULL, get next variable
964 GetVariableHeader (&StoreInfo
, Variable
.CurrPtr
, &VariableHeader
);
965 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);
968 VariableStoreHeader
[VariableStoreTypeHob
] = GetVariableStore (VariableStoreTypeHob
, &StoreInfoForHob
);
969 VariableStoreHeader
[VariableStoreTypeNv
] = GetVariableStore (VariableStoreTypeNv
, &StoreInfoForNv
);
973 // Switch from HOB to Non-Volatile.
975 while (!GetVariableHeader (&StoreInfo
, Variable
.CurrPtr
, &VariableHeader
)) {
977 // Find current storage index
979 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
980 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
984 ASSERT (Type
< VariableStoreTypeMax
);
986 // Switch to next storage
988 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
989 if (VariableStoreHeader
[Type
] != NULL
) {
994 // Capture the case that
995 // 1. current storage is the last one, or
996 // 2. no further storage
998 if (Type
== VariableStoreTypeMax
) {
999 return EFI_NOT_FOUND
;
1001 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
1002 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
1003 Variable
.CurrPtr
= Variable
.StartPtr
;
1004 GetVariableStore (Type
, &StoreInfo
);
1007 if (VariableHeader
->State
== VAR_ADDED
|| VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1008 if (VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1010 // If it is a IN_DELETED_TRANSITION variable,
1011 // and there is also a same ADDED one at the same time,
1014 Status
= FindVariableEx (
1016 GetVariableNamePtr (Variable
.CurrPtr
),
1017 &VariableHeader
->VendorGuid
,
1020 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
!= Variable
.CurrPtr
) {
1021 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);
1027 // Don't return NV variable when HOB overrides it
1029 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
1030 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
1032 Status
= FindVariableEx (
1034 GetVariableNamePtr (Variable
.CurrPtr
),
1035 &VariableHeader
->VendorGuid
,
1038 if (!EFI_ERROR (Status
)) {
1039 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);
1044 VarNameSize
= NameSizeOfVariable (VariableHeader
);
1045 ASSERT (VarNameSize
!= 0);
1047 if (VarNameSize
<= *VariableNameSize
) {
1048 GetVariableNameOrData (&StoreInfo
, (UINT8
*) GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
, (UINT8
*) VariableName
);
1050 CopyMem (VariableGuid
, &VariableHeader
->VendorGuid
, sizeof (EFI_GUID
));
1052 Status
= EFI_SUCCESS
;
1054 Status
= EFI_BUFFER_TOO_SMALL
;
1057 *VariableNameSize
= VarNameSize
;
1059 // Variable is found
1063 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);