2 Implement ReadOnly Variable Services required by PEIM and install
3 PEI ReadOnly Varaiable2 PPI. These services operates the non volatile storage space.
5 Copyright (c) 2006 - 2019, 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
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.
22 EFI_PEI_READ_ONLY_VARIABLE2_PPI mVariablePpi
= {
24 PeiGetNextVariableName
27 EFI_PEI_PPI_DESCRIPTOR mPpiListVariable
= {
28 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
29 &gEfiPeiReadOnlyVariable2PpiGuid
,
35 Provide the functionality of the variable services.
37 @param FileHandle Handle of the file being invoked.
38 Type EFI_PEI_FILE_HANDLE is defined in FfsFindNextFile().
39 @param PeiServices General purpose services available to every PEIM.
41 @retval EFI_SUCCESS If the interface could be successfully installed
42 @retval Others Returned from PeiServicesInstallPpi()
46 PeimInitializeVariableServices (
47 IN EFI_PEI_FILE_HANDLE FileHandle
,
48 IN CONST EFI_PEI_SERVICES
**PeiServices
51 return PeiServicesInstallPpi (&mPpiListVariable
);
56 Gets the pointer to the first variable header in given variable store area.
58 @param VarStoreHeader Pointer to the Variable Store Header.
60 @return Pointer to the first variable header.
65 IN VARIABLE_STORE_HEADER
*VarStoreHeader
69 // The start of variable store
71 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
77 Gets the pointer to the end of the variable storage area.
79 This function gets pointer to the end of the variable storage
80 area, according to the input variable store header.
82 @param VarStoreHeader Pointer to the Variable Store Header.
84 @return Pointer to the end of the variable storage area.
89 IN VARIABLE_STORE_HEADER
*VarStoreHeader
93 // The end of variable store
95 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
100 This code checks if variable header is valid or not.
102 @param Variable Pointer to the Variable Header.
104 @retval TRUE Variable header is valid.
105 @retval FALSE Variable header is not valid.
109 IsValidVariableHeader (
110 IN VARIABLE_HEADER
*Variable
113 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
121 This code gets the size of variable header.
123 @param AuthFlag Authenticated variable flag.
125 @return Size of variable header in bytes in type UINTN.
129 GetVariableHeaderSize (
136 Value
= sizeof (AUTHENTICATED_VARIABLE_HEADER
);
138 Value
= sizeof (VARIABLE_HEADER
);
145 This code gets the size of name of variable.
147 @param Variable Pointer to the Variable Header.
148 @param AuthFlag Authenticated variable flag.
150 @return Size of variable in bytes in type UINTN.
155 IN VARIABLE_HEADER
*Variable
,
159 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
161 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*) Variable
;
163 if (AuthVariable
->State
== (UINT8
) (-1) ||
164 AuthVariable
->DataSize
== (UINT32
) (-1) ||
165 AuthVariable
->NameSize
== (UINT32
) (-1) ||
166 AuthVariable
->Attributes
== (UINT32
) (-1)) {
169 return (UINTN
) AuthVariable
->NameSize
;
171 if (Variable
->State
== (UINT8
) (-1) ||
172 Variable
->DataSize
== (UINT32
) (-1) ||
173 Variable
->NameSize
== (UINT32
) (-1) ||
174 Variable
->Attributes
== (UINT32
) (-1)) {
177 return (UINTN
) Variable
->NameSize
;
183 This code gets the size of data of variable.
185 @param Variable Pointer to the Variable Header.
186 @param AuthFlag Authenticated variable flag.
188 @return Size of variable in bytes in type UINTN.
193 IN VARIABLE_HEADER
*Variable
,
197 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
199 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*) Variable
;
201 if (AuthVariable
->State
== (UINT8
) (-1) ||
202 AuthVariable
->DataSize
== (UINT32
) (-1) ||
203 AuthVariable
->NameSize
== (UINT32
) (-1) ||
204 AuthVariable
->Attributes
== (UINT32
) (-1)) {
207 return (UINTN
) AuthVariable
->DataSize
;
209 if (Variable
->State
== (UINT8
) (-1) ||
210 Variable
->DataSize
== (UINT32
) (-1) ||
211 Variable
->NameSize
== (UINT32
) (-1) ||
212 Variable
->Attributes
== (UINT32
) (-1)) {
215 return (UINTN
) Variable
->DataSize
;
220 This code gets the pointer to the variable name.
222 @param Variable Pointer to the Variable Header.
223 @param AuthFlag Authenticated variable flag.
225 @return A CHAR16* pointer to Variable Name.
230 IN VARIABLE_HEADER
*Variable
,
234 return (CHAR16
*) ((UINTN
) Variable
+ GetVariableHeaderSize (AuthFlag
));
238 This code gets the pointer to the variable guid.
240 @param Variable Pointer to the Variable Header.
241 @param AuthFlag Authenticated variable flag.
243 @return A EFI_GUID* pointer to Vendor Guid.
248 IN VARIABLE_HEADER
*Variable
,
252 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
254 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*) Variable
;
256 return &AuthVariable
->VendorGuid
;
258 return &Variable
->VendorGuid
;
263 This code gets the pointer to the variable data.
265 @param Variable Pointer to the Variable Header.
266 @param VariableHeader Pointer to the Variable Header that has consecutive content.
267 @param AuthFlag Authenticated variable flag.
269 @return A UINT8* pointer to Variable Data.
274 IN VARIABLE_HEADER
*Variable
,
275 IN VARIABLE_HEADER
*VariableHeader
,
282 // Be careful about pad size for alignment
284 Value
= (UINTN
) GetVariableNamePtr (Variable
, AuthFlag
);
285 Value
+= NameSizeOfVariable (VariableHeader
, AuthFlag
);
286 Value
+= GET_PAD_SIZE (NameSizeOfVariable (VariableHeader
, AuthFlag
));
288 return (UINT8
*) Value
;
293 This code gets the pointer to the next variable header.
295 @param StoreInfo Pointer to variable store info structure.
296 @param Variable Pointer to the Variable Header.
297 @param VariableHeader Pointer to the Variable Header that has consecutive content.
299 @return A VARIABLE_HEADER* pointer to next variable header.
304 IN VARIABLE_STORE_INFO
*StoreInfo
,
305 IN VARIABLE_HEADER
*Variable
,
306 IN VARIABLE_HEADER
*VariableHeader
309 EFI_PHYSICAL_ADDRESS TargetAddress
;
310 EFI_PHYSICAL_ADDRESS SpareAddress
;
313 Value
= (UINTN
) GetVariableDataPtr (Variable
, VariableHeader
, StoreInfo
->AuthFlag
);
314 Value
+= DataSizeOfVariable (VariableHeader
, StoreInfo
->AuthFlag
);
315 Value
+= GET_PAD_SIZE (DataSizeOfVariable (VariableHeader
, StoreInfo
->AuthFlag
));
317 // Be careful about pad size for alignment
319 Value
= HEADER_ALIGN (Value
);
321 if (StoreInfo
->FtwLastWriteData
!= NULL
) {
322 TargetAddress
= StoreInfo
->FtwLastWriteData
->TargetAddress
;
323 SpareAddress
= StoreInfo
->FtwLastWriteData
->SpareAddress
;
324 if (((UINTN
) Variable
< (UINTN
) TargetAddress
) && (Value
>= (UINTN
) TargetAddress
)) {
326 // Next variable is in spare block.
328 Value
= (UINTN
) SpareAddress
+ (Value
- (UINTN
) TargetAddress
);
332 return (VARIABLE_HEADER
*) Value
;
336 Get variable store status.
338 @param VarStoreHeader Pointer to the Variable Store Header.
340 @retval EfiRaw Variable store is raw
341 @retval EfiValid Variable store is valid
342 @retval EfiInvalid Variable store is invalid
345 VARIABLE_STORE_STATUS
346 GetVariableStoreStatus (
347 IN VARIABLE_STORE_HEADER
*VarStoreHeader
350 if ((CompareGuid (&VarStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
) ||
351 CompareGuid (&VarStoreHeader
->Signature
, &gEfiVariableGuid
)) &&
352 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
353 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
359 if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
360 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
361 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
362 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
363 VarStoreHeader
->Size
== 0xffffffff &&
364 VarStoreHeader
->Format
== 0xff &&
365 VarStoreHeader
->State
== 0xff
375 Compare two variable names, one of them may be inconsecutive.
377 @param StoreInfo Pointer to variable store info structure.
378 @param Name1 Pointer to one variable name.
379 @param Name2 Pointer to another variable name.
380 @param NameSize Variable name size.
382 @retval TRUE Name1 and Name2 are identical.
383 @retval FALSE Name1 and Name2 are not identical.
387 CompareVariableName (
388 IN VARIABLE_STORE_INFO
*StoreInfo
,
389 IN CONST CHAR16
*Name1
,
390 IN CONST CHAR16
*Name2
,
394 EFI_PHYSICAL_ADDRESS TargetAddress
;
395 EFI_PHYSICAL_ADDRESS SpareAddress
;
396 UINTN PartialNameSize
;
398 if (StoreInfo
->FtwLastWriteData
!= NULL
) {
399 TargetAddress
= StoreInfo
->FtwLastWriteData
->TargetAddress
;
400 SpareAddress
= StoreInfo
->FtwLastWriteData
->SpareAddress
;
401 if (((UINTN
) Name1
< (UINTN
) TargetAddress
) && (((UINTN
) Name1
+ NameSize
) > (UINTN
) TargetAddress
)) {
403 // Name1 is inconsecutive.
405 PartialNameSize
= (UINTN
) TargetAddress
- (UINTN
) Name1
;
407 // Partial content is in NV storage.
409 if (CompareMem ((UINT8
*) Name1
, (UINT8
*) Name2
, PartialNameSize
) == 0) {
411 // Another partial content is in spare block.
413 if (CompareMem ((UINT8
*) (UINTN
) SpareAddress
, (UINT8
*) Name2
+ PartialNameSize
, NameSize
- PartialNameSize
) == 0) {
418 } else if (((UINTN
) Name2
< (UINTN
) TargetAddress
) && (((UINTN
) Name2
+ NameSize
) > (UINTN
) TargetAddress
)) {
420 // Name2 is inconsecutive.
422 PartialNameSize
= (UINTN
) TargetAddress
- (UINTN
) Name2
;
424 // Partial content is in NV storage.
426 if (CompareMem ((UINT8
*) Name2
, (UINT8
*) Name1
, PartialNameSize
) == 0) {
428 // Another partial content is in spare block.
430 if (CompareMem ((UINT8
*) (UINTN
) SpareAddress
, (UINT8
*) Name1
+ PartialNameSize
, NameSize
- PartialNameSize
) == 0) {
439 // Both Name1 and Name2 are consecutive.
441 if (CompareMem ((UINT8
*) Name1
, (UINT8
*) Name2
, NameSize
) == 0) {
448 This function compares a variable with variable entries in database.
450 @param StoreInfo Pointer to variable store info structure.
451 @param Variable Pointer to the variable in our database
452 @param VariableHeader Pointer to the Variable Header that has consecutive content.
453 @param VariableName Name of the variable to compare to 'Variable'
454 @param VendorGuid GUID of the variable to compare to 'Variable'
455 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
457 @retval EFI_SUCCESS Found match variable
458 @retval EFI_NOT_FOUND Variable not found
462 CompareWithValidVariable (
463 IN VARIABLE_STORE_INFO
*StoreInfo
,
464 IN VARIABLE_HEADER
*Variable
,
465 IN VARIABLE_HEADER
*VariableHeader
,
466 IN CONST CHAR16
*VariableName
,
467 IN CONST EFI_GUID
*VendorGuid
,
468 OUT VARIABLE_POINTER_TRACK
*PtrTrack
472 EFI_GUID
*TempVendorGuid
;
474 TempVendorGuid
= GetVendorGuidPtr (VariableHeader
, StoreInfo
->AuthFlag
);
476 if (VariableName
[0] == 0) {
477 PtrTrack
->CurrPtr
= Variable
;
481 // Don't use CompareGuid function here for performance reasons.
482 // Instead we compare the GUID a UINT32 at a time and branch
483 // on the first failed comparison.
485 if ((((INT32
*) VendorGuid
)[0] == ((INT32
*) TempVendorGuid
)[0]) &&
486 (((INT32
*) VendorGuid
)[1] == ((INT32
*) TempVendorGuid
)[1]) &&
487 (((INT32
*) VendorGuid
)[2] == ((INT32
*) TempVendorGuid
)[2]) &&
488 (((INT32
*) VendorGuid
)[3] == ((INT32
*) TempVendorGuid
)[3])
490 ASSERT (NameSizeOfVariable (VariableHeader
, StoreInfo
->AuthFlag
) != 0);
491 Point
= (VOID
*) GetVariableNamePtr (Variable
, StoreInfo
->AuthFlag
);
492 if (CompareVariableName (StoreInfo
, VariableName
, Point
, NameSizeOfVariable (VariableHeader
, StoreInfo
->AuthFlag
))) {
493 PtrTrack
->CurrPtr
= Variable
;
499 return EFI_NOT_FOUND
;
503 Get HOB variable store.
505 @param[out] StoreInfo Return the store info.
506 @param[out] VariableStoreHeader Return variable store header.
510 GetHobVariableStore (
511 OUT VARIABLE_STORE_INFO
*StoreInfo
,
512 OUT VARIABLE_STORE_HEADER
**VariableStoreHeader
515 EFI_HOB_GUID_TYPE
*GuidHob
;
518 // Make sure there is no more than one Variable HOB.
521 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
522 if (GuidHob
!= NULL
) {
523 if ((GetNextGuidHob (&gEfiAuthenticatedVariableGuid
, GET_NEXT_HOB (GuidHob
)) != NULL
)) {
524 DEBUG ((DEBUG_ERROR
, "ERROR: Found two Auth Variable HOBs\n"));
526 } else if (GetFirstGuidHob (&gEfiVariableGuid
) != NULL
) {
527 DEBUG ((DEBUG_ERROR
, "ERROR: Found one Auth + one Normal Variable HOBs\n"));
531 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
532 if (GuidHob
!= NULL
) {
533 if ((GetNextGuidHob (&gEfiVariableGuid
, GET_NEXT_HOB (GuidHob
)) != NULL
)) {
534 DEBUG ((DEBUG_ERROR
, "ERROR: Found two Normal Variable HOBs\n"));
541 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
542 if (GuidHob
!= NULL
) {
543 *VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) GET_GUID_HOB_DATA (GuidHob
);
544 StoreInfo
->AuthFlag
= TRUE
;
546 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
547 if (GuidHob
!= NULL
) {
548 *VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) GET_GUID_HOB_DATA (GuidHob
);
549 StoreInfo
->AuthFlag
= FALSE
;
555 Return the variable store header and the store info based on the Index.
557 @param Type The type of the variable store.
558 @param StoreInfo Return the store info.
560 @return Pointer to the variable store header.
562 VARIABLE_STORE_HEADER
*
564 IN VARIABLE_STORE_TYPE Type
,
565 OUT VARIABLE_STORE_INFO
*StoreInfo
568 EFI_HOB_GUID_TYPE
*GuidHob
;
569 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
570 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
571 EFI_PHYSICAL_ADDRESS NvStorageBase
;
572 UINT32 NvStorageSize
;
573 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*FtwLastWriteData
;
576 StoreInfo
->IndexTable
= NULL
;
577 StoreInfo
->FtwLastWriteData
= NULL
;
578 StoreInfo
->AuthFlag
= FALSE
;
579 VariableStoreHeader
= NULL
;
581 case VariableStoreTypeHob
:
582 GetHobVariableStore (StoreInfo
, &VariableStoreHeader
);
586 case VariableStoreTypeNv
:
587 if (!PcdGetBool (PcdEmuVariableNvModeEnable
)) {
589 // Emulated non-volatile variable mode is not enabled.
592 NvStorageSize
= PcdGet32 (PcdFlashNvStorageVariableSize
);
593 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) (PcdGet64 (PcdFlashNvStorageVariableBase64
) != 0 ?
594 PcdGet64 (PcdFlashNvStorageVariableBase64
) :
595 PcdGet32 (PcdFlashNvStorageVariableBase
)
597 ASSERT (NvStorageBase
!= 0);
600 // First let FvHeader point to NV storage base.
602 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) NvStorageBase
;
605 // Check the FTW last write data hob.
608 GuidHob
= GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid
);
609 if (GuidHob
!= NULL
) {
610 FtwLastWriteData
= (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*) GET_GUID_HOB_DATA (GuidHob
);
611 if (FtwLastWriteData
->TargetAddress
== NvStorageBase
) {
613 // Let FvHeader point to spare block.
615 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) FtwLastWriteData
->SpareAddress
;
616 DEBUG ((EFI_D_INFO
, "PeiVariable: NV storage is backed up in spare block: 0x%x\n", (UINTN
) FtwLastWriteData
->SpareAddress
));
617 } else if ((FtwLastWriteData
->TargetAddress
> NvStorageBase
) && (FtwLastWriteData
->TargetAddress
< (NvStorageBase
+ NvStorageSize
))) {
618 StoreInfo
->FtwLastWriteData
= FtwLastWriteData
;
620 // Flash NV storage from the offset is backed up in spare block.
622 BackUpOffset
= (UINT32
) (FtwLastWriteData
->TargetAddress
- NvStorageBase
);
623 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
));
625 // At least one block data in flash NV storage is still valid, so still leave FvHeader point to NV storage base.
631 // Check if the Firmware Volume is not corrupted
633 if ((FvHeader
->Signature
!= EFI_FVH_SIGNATURE
) || (!CompareGuid (&gEfiSystemNvDataFvGuid
, &FvHeader
->FileSystemGuid
))) {
634 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
638 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINT8
*) FvHeader
+ FvHeader
->HeaderLength
);
640 StoreInfo
->AuthFlag
= (BOOLEAN
) (CompareGuid (&VariableStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
));
642 GuidHob
= GetFirstGuidHob (&gEfiVariableIndexTableGuid
);
643 if (GuidHob
!= NULL
) {
644 StoreInfo
->IndexTable
= GET_GUID_HOB_DATA (GuidHob
);
647 // If it's the first time to access variable region in flash, create a guid hob to record
648 // VAR_ADDED type variable info.
649 // Note that as the resource of PEI phase is limited, only store the limited number of
650 // VAR_ADDED type variables to reduce access time.
652 StoreInfo
->IndexTable
= (VARIABLE_INDEX_TABLE
*) BuildGuidHob (&gEfiVariableIndexTableGuid
, sizeof (VARIABLE_INDEX_TABLE
));
653 StoreInfo
->IndexTable
->Length
= 0;
654 StoreInfo
->IndexTable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
655 StoreInfo
->IndexTable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
656 StoreInfo
->IndexTable
->GoneThrough
= 0;
666 StoreInfo
->VariableStoreHeader
= VariableStoreHeader
;
667 return VariableStoreHeader
;
671 Get variable header that has consecutive content.
673 @param StoreInfo Pointer to variable store info structure.
674 @param Variable Pointer to the Variable Header.
675 @param VariableHeader Pointer to Pointer to the Variable Header that has consecutive content.
677 @retval TRUE Variable header is valid.
678 @retval FALSE Variable header is not valid.
683 IN VARIABLE_STORE_INFO
*StoreInfo
,
684 IN VARIABLE_HEADER
*Variable
,
685 OUT VARIABLE_HEADER
**VariableHeader
688 EFI_PHYSICAL_ADDRESS TargetAddress
;
689 EFI_PHYSICAL_ADDRESS SpareAddress
;
690 EFI_HOB_GUID_TYPE
*GuidHob
;
691 UINTN PartialHeaderSize
;
693 if (Variable
== NULL
) {
698 // First assume variable header pointed by Variable is consecutive.
700 *VariableHeader
= Variable
;
702 if (StoreInfo
->FtwLastWriteData
!= NULL
) {
703 TargetAddress
= StoreInfo
->FtwLastWriteData
->TargetAddress
;
704 SpareAddress
= StoreInfo
->FtwLastWriteData
->SpareAddress
;
705 if (((UINTN
) Variable
> (UINTN
) SpareAddress
) &&
706 (((UINTN
) Variable
- (UINTN
) SpareAddress
+ (UINTN
) TargetAddress
) >= (UINTN
) GetEndPointer (StoreInfo
->VariableStoreHeader
))) {
708 // Reach the end of variable store.
712 if (((UINTN
) Variable
< (UINTN
) TargetAddress
) && (((UINTN
) Variable
+ GetVariableHeaderSize (StoreInfo
->AuthFlag
)) > (UINTN
) TargetAddress
)) {
714 // Variable header pointed by Variable is inconsecutive,
715 // create a guid hob to combine the two partial variable header content together.
717 GuidHob
= GetFirstGuidHob (&gEfiCallerIdGuid
);
718 if (GuidHob
!= NULL
) {
719 *VariableHeader
= (VARIABLE_HEADER
*) GET_GUID_HOB_DATA (GuidHob
);
721 *VariableHeader
= (VARIABLE_HEADER
*) BuildGuidHob (&gEfiCallerIdGuid
, GetVariableHeaderSize (StoreInfo
->AuthFlag
));
722 PartialHeaderSize
= (UINTN
) TargetAddress
- (UINTN
) Variable
;
724 // Partial content is in NV storage.
726 CopyMem ((UINT8
*) *VariableHeader
, (UINT8
*) Variable
, PartialHeaderSize
);
728 // Another partial content is in spare block.
730 CopyMem ((UINT8
*) *VariableHeader
+ PartialHeaderSize
, (UINT8
*) (UINTN
) SpareAddress
, GetVariableHeaderSize (StoreInfo
->AuthFlag
) - PartialHeaderSize
);
734 if (Variable
>= GetEndPointer (StoreInfo
->VariableStoreHeader
)) {
736 // Reach the end of variable store.
742 return IsValidVariableHeader (*VariableHeader
);
746 Get variable name or data to output buffer.
748 @param StoreInfo Pointer to variable store info structure.
749 @param NameOrData Pointer to the variable name/data that may be inconsecutive.
750 @param Size Variable name/data size.
751 @param Buffer Pointer to output buffer to hold the variable name/data.
755 GetVariableNameOrData (
756 IN VARIABLE_STORE_INFO
*StoreInfo
,
757 IN UINT8
*NameOrData
,
762 EFI_PHYSICAL_ADDRESS TargetAddress
;
763 EFI_PHYSICAL_ADDRESS SpareAddress
;
766 if (StoreInfo
->FtwLastWriteData
!= NULL
) {
767 TargetAddress
= StoreInfo
->FtwLastWriteData
->TargetAddress
;
768 SpareAddress
= StoreInfo
->FtwLastWriteData
->SpareAddress
;
769 if (((UINTN
) NameOrData
< (UINTN
) TargetAddress
) && (((UINTN
) NameOrData
+ Size
) > (UINTN
) TargetAddress
)) {
771 // Variable name/data is inconsecutive.
773 PartialSize
= (UINTN
) TargetAddress
- (UINTN
) NameOrData
;
775 // Partial content is in NV storage.
777 CopyMem (Buffer
, NameOrData
, PartialSize
);
779 // Another partial content is in spare block.
781 CopyMem (Buffer
+ PartialSize
, (UINT8
*) (UINTN
) SpareAddress
, Size
- PartialSize
);
787 // Variable name/data is consecutive.
789 CopyMem (Buffer
, NameOrData
, Size
);
793 Find the variable in the specified variable store.
795 @param StoreInfo Pointer to the store info structure.
796 @param VariableName Name of the variable to be found
797 @param VendorGuid Vendor GUID to be found.
798 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
800 @retval EFI_SUCCESS Variable found successfully
801 @retval EFI_NOT_FOUND Variable not found
802 @retval EFI_INVALID_PARAMETER Invalid variable name
807 IN VARIABLE_STORE_INFO
*StoreInfo
,
808 IN CONST CHAR16
*VariableName
,
809 IN CONST EFI_GUID
*VendorGuid
,
810 OUT VARIABLE_POINTER_TRACK
*PtrTrack
813 VARIABLE_HEADER
*Variable
;
814 VARIABLE_HEADER
*LastVariable
;
815 VARIABLE_HEADER
*MaxIndex
;
819 VARIABLE_HEADER
*InDeletedVariable
;
820 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
821 VARIABLE_INDEX_TABLE
*IndexTable
;
822 VARIABLE_HEADER
*VariableHeader
;
824 VariableStoreHeader
= StoreInfo
->VariableStoreHeader
;
826 if (VariableStoreHeader
== NULL
) {
827 return EFI_INVALID_PARAMETER
;
830 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
831 return EFI_UNSUPPORTED
;
834 if (~VariableStoreHeader
->Size
== 0) {
835 return EFI_NOT_FOUND
;
838 IndexTable
= StoreInfo
->IndexTable
;
839 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
);
840 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
);
842 InDeletedVariable
= NULL
;
845 // No Variable Address equals zero, so 0 as initial value is safe.
848 VariableHeader
= NULL
;
850 if (IndexTable
!= NULL
) {
852 // traverse the variable index table to look for varible.
853 // The IndexTable->Index[Index] records the distance of two neighbouring VAR_ADDED type variables.
855 for (Offset
= 0, Index
= 0; Index
< IndexTable
->Length
; Index
++) {
856 ASSERT (Index
< sizeof (IndexTable
->Index
) / sizeof (IndexTable
->Index
[0]));
857 Offset
+= IndexTable
->Index
[Index
];
858 MaxIndex
= (VARIABLE_HEADER
*) ((UINT8
*) IndexTable
->StartPtr
+ Offset
);
859 GetVariableHeader (StoreInfo
, MaxIndex
, &VariableHeader
);
860 if (CompareWithValidVariable (StoreInfo
, MaxIndex
, VariableHeader
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
861 if (VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
862 InDeletedVariable
= PtrTrack
->CurrPtr
;
869 if (IndexTable
->GoneThrough
!= 0) {
871 // If the table has all the existing variables indexed, return.
873 PtrTrack
->CurrPtr
= InDeletedVariable
;
874 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
878 if (MaxIndex
!= NULL
) {
880 // HOB exists but the variable cannot be found in HOB
881 // If not found in HOB, then let's start from the MaxIndex we've found.
883 Variable
= GetNextVariablePtr (StoreInfo
, MaxIndex
, VariableHeader
);
884 LastVariable
= MaxIndex
;
887 // Start Pointers for the variable.
888 // Actual Data Pointer where data can be written.
890 Variable
= PtrTrack
->StartPtr
;
891 LastVariable
= PtrTrack
->StartPtr
;
895 // Find the variable by walk through variable store
898 while (GetVariableHeader (StoreInfo
, Variable
, &VariableHeader
)) {
899 if (VariableHeader
->State
== VAR_ADDED
|| VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
901 // Record Variable in VariableIndex HOB
903 if ((IndexTable
!= NULL
) && !StopRecord
) {
904 Offset
= (UINTN
) Variable
- (UINTN
) LastVariable
;
905 if ((Offset
> 0x0FFFF) || (IndexTable
->Length
== sizeof (IndexTable
->Index
) / sizeof (IndexTable
->Index
[0]))) {
907 // Stop to record if the distance of two neighbouring VAR_ADDED variable is larger than the allowable scope(UINT16),
908 // or the record buffer is full.
912 IndexTable
->Index
[IndexTable
->Length
++] = (UINT16
) Offset
;
913 LastVariable
= Variable
;
917 if (CompareWithValidVariable (StoreInfo
, Variable
, VariableHeader
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
918 if (VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
919 InDeletedVariable
= PtrTrack
->CurrPtr
;
926 Variable
= GetNextVariablePtr (StoreInfo
, Variable
, VariableHeader
);
929 // If gone through the VariableStore, that means we never find in Firmware any more.
931 if ((IndexTable
!= NULL
) && !StopRecord
) {
932 IndexTable
->GoneThrough
= 1;
935 PtrTrack
->CurrPtr
= InDeletedVariable
;
937 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
941 Find the variable in HOB and Non-Volatile variable storages.
943 @param VariableName Name of the variable to be found
944 @param VendorGuid Vendor GUID to be found.
945 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
946 @param StoreInfo Return the store info.
948 @retval EFI_SUCCESS Variable found successfully
949 @retval EFI_NOT_FOUND Variable not found
950 @retval EFI_INVALID_PARAMETER Invalid variable name
954 IN CONST CHAR16
*VariableName
,
955 IN CONST EFI_GUID
*VendorGuid
,
956 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
957 OUT VARIABLE_STORE_INFO
*StoreInfo
961 VARIABLE_STORE_TYPE Type
;
963 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
964 return EFI_INVALID_PARAMETER
;
967 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
968 GetVariableStore (Type
, StoreInfo
);
969 Status
= FindVariableEx (
975 if (!EFI_ERROR (Status
)) {
980 return EFI_NOT_FOUND
;
984 This service retrieves a variable's value using its name and GUID.
986 Read the specified variable from the UEFI variable store. If the Data
987 buffer is too small to hold the contents of the variable, the error
988 EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer
989 size to obtain the data.
991 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
992 @param VariableName A pointer to a null-terminated string that is the variable's name.
993 @param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of
994 VariableGuid and VariableName must be unique.
995 @param Attributes If non-NULL, on return, points to the variable's attributes.
996 @param DataSize On entry, points to the size in bytes of the Data buffer.
997 On return, points to the size of the data returned in Data.
998 @param Data Points to the buffer which will hold the returned variable value.
999 May be NULL with a zero DataSize in order to determine the size of the buffer needed.
1001 @retval EFI_SUCCESS The variable was read successfully.
1002 @retval EFI_NOT_FOUND The variable was be found.
1003 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data.
1004 DataSize is updated with the size required for
1005 the specified variable.
1006 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.
1007 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
1013 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
1014 IN CONST CHAR16
*VariableName
,
1015 IN CONST EFI_GUID
*VariableGuid
,
1016 OUT UINT32
*Attributes
,
1017 IN OUT UINTN
*DataSize
,
1018 OUT VOID
*Data OPTIONAL
1021 VARIABLE_POINTER_TRACK Variable
;
1024 VARIABLE_STORE_INFO StoreInfo
;
1025 VARIABLE_HEADER
*VariableHeader
;
1027 if (VariableName
== NULL
|| VariableGuid
== NULL
|| DataSize
== NULL
) {
1028 return EFI_INVALID_PARAMETER
;
1031 if (VariableName
[0] == 0) {
1032 return EFI_NOT_FOUND
;
1035 VariableHeader
= NULL
;
1038 // Find existing variable
1040 Status
= FindVariable (VariableName
, VariableGuid
, &Variable
, &StoreInfo
);
1041 if (EFI_ERROR (Status
)) {
1044 GetVariableHeader (&StoreInfo
, Variable
.CurrPtr
, &VariableHeader
);
1049 VarDataSize
= DataSizeOfVariable (VariableHeader
, StoreInfo
.AuthFlag
);
1050 if (*DataSize
>= VarDataSize
) {
1052 return EFI_INVALID_PARAMETER
;
1055 GetVariableNameOrData (&StoreInfo
, GetVariableDataPtr (Variable
.CurrPtr
, VariableHeader
, StoreInfo
.AuthFlag
), VarDataSize
, Data
);
1057 if (Attributes
!= NULL
) {
1058 *Attributes
= VariableHeader
->Attributes
;
1061 *DataSize
= VarDataSize
;
1064 *DataSize
= VarDataSize
;
1065 return EFI_BUFFER_TOO_SMALL
;
1070 Return the next variable name and GUID.
1072 This function is called multiple times to retrieve the VariableName
1073 and VariableGuid of all variables currently available in the system.
1074 On each call, the previous results are passed into the interface,
1075 and, on return, the interface returns the data for the next
1076 interface. When the entire variable list has been returned,
1077 EFI_NOT_FOUND is returned.
1079 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
1081 @param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.
1082 On return, the size of the variable name buffer.
1083 @param VariableName On entry, a pointer to a null-terminated string that is the variable's name.
1084 On return, points to the next variable's null-terminated name string.
1085 @param VariableGuid On entry, a pointer to an EFI_GUID that is the variable's GUID.
1086 On return, a pointer to the next variable's GUID.
1088 @retval EFI_SUCCESS The variable was read successfully.
1089 @retval EFI_NOT_FOUND The variable could not be found.
1090 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting
1091 data. VariableNameSize is updated with the size
1092 required for the specified variable.
1093 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or
1094 VariableNameSize is NULL.
1095 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
1100 PeiGetNextVariableName (
1101 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
1102 IN OUT UINTN
*VariableNameSize
,
1103 IN OUT CHAR16
*VariableName
,
1104 IN OUT EFI_GUID
*VariableGuid
1107 VARIABLE_STORE_TYPE Type
;
1108 VARIABLE_POINTER_TRACK Variable
;
1109 VARIABLE_POINTER_TRACK VariableInHob
;
1110 VARIABLE_POINTER_TRACK VariablePtrTrack
;
1113 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
1114 VARIABLE_HEADER
*VariableHeader
;
1115 VARIABLE_STORE_INFO StoreInfo
;
1116 VARIABLE_STORE_INFO StoreInfoForNv
;
1117 VARIABLE_STORE_INFO StoreInfoForHob
;
1119 if (VariableName
== NULL
|| VariableGuid
== NULL
|| VariableNameSize
== NULL
) {
1120 return EFI_INVALID_PARAMETER
;
1123 VariableHeader
= NULL
;
1125 Status
= FindVariable (VariableName
, VariableGuid
, &Variable
, &StoreInfo
);
1126 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
1130 if (VariableName
[0] != 0) {
1132 // If variable name is not NULL, get next variable
1134 GetVariableHeader (&StoreInfo
, Variable
.CurrPtr
, &VariableHeader
);
1135 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);
1138 VariableStoreHeader
[VariableStoreTypeHob
] = GetVariableStore (VariableStoreTypeHob
, &StoreInfoForHob
);
1139 VariableStoreHeader
[VariableStoreTypeNv
] = GetVariableStore (VariableStoreTypeNv
, &StoreInfoForNv
);
1143 // Switch from HOB to Non-Volatile.
1145 while (!GetVariableHeader (&StoreInfo
, Variable
.CurrPtr
, &VariableHeader
)) {
1147 // Find current storage index
1149 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
1150 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
1154 ASSERT (Type
< VariableStoreTypeMax
);
1156 // Switch to next storage
1158 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
1159 if (VariableStoreHeader
[Type
] != NULL
) {
1164 // Capture the case that
1165 // 1. current storage is the last one, or
1166 // 2. no further storage
1168 if (Type
== VariableStoreTypeMax
) {
1169 return EFI_NOT_FOUND
;
1171 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
1172 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
1173 Variable
.CurrPtr
= Variable
.StartPtr
;
1174 GetVariableStore (Type
, &StoreInfo
);
1177 if (VariableHeader
->State
== VAR_ADDED
|| VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1178 if (VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1180 // If it is a IN_DELETED_TRANSITION variable,
1181 // and there is also a same ADDED one at the same time,
1184 Status
= FindVariableEx (
1186 GetVariableNamePtr (Variable
.CurrPtr
, StoreInfo
.AuthFlag
),
1187 GetVendorGuidPtr (VariableHeader
, StoreInfo
.AuthFlag
),
1190 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
!= Variable
.CurrPtr
) {
1191 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);
1197 // Don't return NV variable when HOB overrides it
1199 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
1200 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
1202 Status
= FindVariableEx (
1204 GetVariableNamePtr (Variable
.CurrPtr
, StoreInfo
.AuthFlag
),
1205 GetVendorGuidPtr (VariableHeader
, StoreInfo
.AuthFlag
),
1208 if (!EFI_ERROR (Status
)) {
1209 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);
1214 VarNameSize
= NameSizeOfVariable (VariableHeader
, StoreInfo
.AuthFlag
);
1215 ASSERT (VarNameSize
!= 0);
1217 if (VarNameSize
<= *VariableNameSize
) {
1218 GetVariableNameOrData (&StoreInfo
, (UINT8
*) GetVariableNamePtr (Variable
.CurrPtr
, StoreInfo
.AuthFlag
), VarNameSize
, (UINT8
*) VariableName
);
1220 CopyMem (VariableGuid
, GetVendorGuidPtr (VariableHeader
, StoreInfo
.AuthFlag
), sizeof (EFI_GUID
));
1222 Status
= EFI_SUCCESS
;
1224 Status
= EFI_BUFFER_TOO_SMALL
;
1227 *VariableNameSize
= VarNameSize
;
1229 // Variable is found
1233 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);