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 - 2018, 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 end of variable store
71 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
76 This code gets the pointer to the last variable memory pointer byte.
78 @param VarStoreHeader Pointer to the Variable Store Header.
80 @return VARIABLE_HEADER* pointer to last unavailable Variable Header.
85 IN VARIABLE_STORE_HEADER
*VarStoreHeader
89 // The end of variable store
91 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
96 This code checks if variable header is valid or not.
98 @param Variable Pointer to the Variable Header.
100 @retval TRUE Variable header is valid.
101 @retval FALSE Variable header is not valid.
105 IsValidVariableHeader (
106 IN VARIABLE_HEADER
*Variable
109 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
117 This code gets the size of variable header.
119 @param AuthFlag Authenticated variable flag.
121 @return Size of variable header in bytes in type UINTN.
125 GetVariableHeaderSize (
132 Value
= sizeof (AUTHENTICATED_VARIABLE_HEADER
);
134 Value
= sizeof (VARIABLE_HEADER
);
141 This code gets the size of name of variable.
143 @param Variable Pointer to the Variable Header.
144 @param AuthFlag Authenticated variable flag.
146 @return Size of variable in bytes in type UINTN.
151 IN VARIABLE_HEADER
*Variable
,
155 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
157 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*) Variable
;
159 if (AuthVariable
->State
== (UINT8
) (-1) ||
160 AuthVariable
->DataSize
== (UINT32
) (-1) ||
161 AuthVariable
->NameSize
== (UINT32
) (-1) ||
162 AuthVariable
->Attributes
== (UINT32
) (-1)) {
165 return (UINTN
) AuthVariable
->NameSize
;
167 if (Variable
->State
== (UINT8
) (-1) ||
168 Variable
->DataSize
== (UINT32
) (-1) ||
169 Variable
->NameSize
== (UINT32
) (-1) ||
170 Variable
->Attributes
== (UINT32
) (-1)) {
173 return (UINTN
) Variable
->NameSize
;
179 This code gets the size of data of variable.
181 @param Variable Pointer to the Variable Header.
182 @param AuthFlag Authenticated variable flag.
184 @return Size of variable in bytes in type UINTN.
189 IN VARIABLE_HEADER
*Variable
,
193 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
195 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*) Variable
;
197 if (AuthVariable
->State
== (UINT8
) (-1) ||
198 AuthVariable
->DataSize
== (UINT32
) (-1) ||
199 AuthVariable
->NameSize
== (UINT32
) (-1) ||
200 AuthVariable
->Attributes
== (UINT32
) (-1)) {
203 return (UINTN
) AuthVariable
->DataSize
;
205 if (Variable
->State
== (UINT8
) (-1) ||
206 Variable
->DataSize
== (UINT32
) (-1) ||
207 Variable
->NameSize
== (UINT32
) (-1) ||
208 Variable
->Attributes
== (UINT32
) (-1)) {
211 return (UINTN
) Variable
->DataSize
;
216 This code gets the pointer to the variable name.
218 @param Variable Pointer to the Variable Header.
219 @param AuthFlag Authenticated variable flag.
221 @return A CHAR16* pointer to Variable Name.
226 IN VARIABLE_HEADER
*Variable
,
230 return (CHAR16
*) ((UINTN
) Variable
+ GetVariableHeaderSize (AuthFlag
));
234 This code gets the pointer to the variable guid.
236 @param Variable Pointer to the Variable Header.
237 @param AuthFlag Authenticated variable flag.
239 @return A EFI_GUID* pointer to Vendor Guid.
244 IN VARIABLE_HEADER
*Variable
,
248 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
250 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*) Variable
;
252 return &AuthVariable
->VendorGuid
;
254 return &Variable
->VendorGuid
;
259 This code gets the pointer to the variable data.
261 @param Variable Pointer to the Variable Header.
262 @param VariableHeader Pointer to the Variable Header that has consecutive content.
263 @param AuthFlag Authenticated variable flag.
265 @return A UINT8* pointer to Variable Data.
270 IN VARIABLE_HEADER
*Variable
,
271 IN VARIABLE_HEADER
*VariableHeader
,
278 // Be careful about pad size for alignment
280 Value
= (UINTN
) GetVariableNamePtr (Variable
, AuthFlag
);
281 Value
+= NameSizeOfVariable (VariableHeader
, AuthFlag
);
282 Value
+= GET_PAD_SIZE (NameSizeOfVariable (VariableHeader
, AuthFlag
));
284 return (UINT8
*) Value
;
289 This code gets the pointer to the next variable header.
291 @param StoreInfo Pointer to variable store info structure.
292 @param Variable Pointer to the Variable Header.
293 @param VariableHeader Pointer to the Variable Header that has consecutive content.
295 @return A VARIABLE_HEADER* pointer to next variable header.
300 IN VARIABLE_STORE_INFO
*StoreInfo
,
301 IN VARIABLE_HEADER
*Variable
,
302 IN VARIABLE_HEADER
*VariableHeader
305 EFI_PHYSICAL_ADDRESS TargetAddress
;
306 EFI_PHYSICAL_ADDRESS SpareAddress
;
309 Value
= (UINTN
) GetVariableDataPtr (Variable
, VariableHeader
, StoreInfo
->AuthFlag
);
310 Value
+= DataSizeOfVariable (VariableHeader
, StoreInfo
->AuthFlag
);
311 Value
+= GET_PAD_SIZE (DataSizeOfVariable (VariableHeader
, StoreInfo
->AuthFlag
));
313 // Be careful about pad size for alignment
315 Value
= HEADER_ALIGN (Value
);
317 if (StoreInfo
->FtwLastWriteData
!= NULL
) {
318 TargetAddress
= StoreInfo
->FtwLastWriteData
->TargetAddress
;
319 SpareAddress
= StoreInfo
->FtwLastWriteData
->SpareAddress
;
320 if (((UINTN
) Variable
< (UINTN
) TargetAddress
) && (Value
>= (UINTN
) TargetAddress
)) {
322 // Next variable is in spare block.
324 Value
= (UINTN
) SpareAddress
+ (Value
- (UINTN
) TargetAddress
);
328 return (VARIABLE_HEADER
*) Value
;
332 Get variable store status.
334 @param VarStoreHeader Pointer to the Variable Store Header.
336 @retval EfiRaw Variable store is raw
337 @retval EfiValid Variable store is valid
338 @retval EfiInvalid Variable store is invalid
341 VARIABLE_STORE_STATUS
342 GetVariableStoreStatus (
343 IN VARIABLE_STORE_HEADER
*VarStoreHeader
346 if ((CompareGuid (&VarStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
) ||
347 CompareGuid (&VarStoreHeader
->Signature
, &gEfiVariableGuid
)) &&
348 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
349 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
355 if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
356 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
357 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
358 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
359 VarStoreHeader
->Size
== 0xffffffff &&
360 VarStoreHeader
->Format
== 0xff &&
361 VarStoreHeader
->State
== 0xff
371 Compare two variable names, one of them may be inconsecutive.
373 @param StoreInfo Pointer to variable store info structure.
374 @param Name1 Pointer to one variable name.
375 @param Name2 Pointer to another variable name.
376 @param NameSize Variable name size.
378 @retval TRUE Name1 and Name2 are identical.
379 @retval FALSE Name1 and Name2 are not identical.
383 CompareVariableName (
384 IN VARIABLE_STORE_INFO
*StoreInfo
,
385 IN CONST CHAR16
*Name1
,
386 IN CONST CHAR16
*Name2
,
390 EFI_PHYSICAL_ADDRESS TargetAddress
;
391 EFI_PHYSICAL_ADDRESS SpareAddress
;
392 UINTN PartialNameSize
;
394 if (StoreInfo
->FtwLastWriteData
!= NULL
) {
395 TargetAddress
= StoreInfo
->FtwLastWriteData
->TargetAddress
;
396 SpareAddress
= StoreInfo
->FtwLastWriteData
->SpareAddress
;
397 if (((UINTN
) Name1
< (UINTN
) TargetAddress
) && (((UINTN
) Name1
+ NameSize
) > (UINTN
) TargetAddress
)) {
399 // Name1 is inconsecutive.
401 PartialNameSize
= (UINTN
) TargetAddress
- (UINTN
) Name1
;
403 // Partial content is in NV storage.
405 if (CompareMem ((UINT8
*) Name1
, (UINT8
*) Name2
, PartialNameSize
) == 0) {
407 // Another partial content is in spare block.
409 if (CompareMem ((UINT8
*) (UINTN
) SpareAddress
, (UINT8
*) Name2
+ PartialNameSize
, NameSize
- PartialNameSize
) == 0) {
414 } else if (((UINTN
) Name2
< (UINTN
) TargetAddress
) && (((UINTN
) Name2
+ NameSize
) > (UINTN
) TargetAddress
)) {
416 // Name2 is inconsecutive.
418 PartialNameSize
= (UINTN
) TargetAddress
- (UINTN
) Name2
;
420 // Partial content is in NV storage.
422 if (CompareMem ((UINT8
*) Name2
, (UINT8
*) Name1
, PartialNameSize
) == 0) {
424 // Another partial content is in spare block.
426 if (CompareMem ((UINT8
*) (UINTN
) SpareAddress
, (UINT8
*) Name1
+ PartialNameSize
, NameSize
- PartialNameSize
) == 0) {
435 // Both Name1 and Name2 are consecutive.
437 if (CompareMem ((UINT8
*) Name1
, (UINT8
*) Name2
, NameSize
) == 0) {
444 This function compares a variable with variable entries in database.
446 @param StoreInfo Pointer to variable store info structure.
447 @param Variable Pointer to the variable in our database
448 @param VariableHeader Pointer to the Variable Header that has consecutive content.
449 @param VariableName Name of the variable to compare to 'Variable'
450 @param VendorGuid GUID of the variable to compare to 'Variable'
451 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
453 @retval EFI_SUCCESS Found match variable
454 @retval EFI_NOT_FOUND Variable not found
458 CompareWithValidVariable (
459 IN VARIABLE_STORE_INFO
*StoreInfo
,
460 IN VARIABLE_HEADER
*Variable
,
461 IN VARIABLE_HEADER
*VariableHeader
,
462 IN CONST CHAR16
*VariableName
,
463 IN CONST EFI_GUID
*VendorGuid
,
464 OUT VARIABLE_POINTER_TRACK
*PtrTrack
468 EFI_GUID
*TempVendorGuid
;
470 TempVendorGuid
= GetVendorGuidPtr (VariableHeader
, StoreInfo
->AuthFlag
);
472 if (VariableName
[0] == 0) {
473 PtrTrack
->CurrPtr
= Variable
;
477 // Don't use CompareGuid function here for performance reasons.
478 // Instead we compare the GUID a UINT32 at a time and branch
479 // on the first failed comparison.
481 if ((((INT32
*) VendorGuid
)[0] == ((INT32
*) TempVendorGuid
)[0]) &&
482 (((INT32
*) VendorGuid
)[1] == ((INT32
*) TempVendorGuid
)[1]) &&
483 (((INT32
*) VendorGuid
)[2] == ((INT32
*) TempVendorGuid
)[2]) &&
484 (((INT32
*) VendorGuid
)[3] == ((INT32
*) TempVendorGuid
)[3])
486 ASSERT (NameSizeOfVariable (VariableHeader
, StoreInfo
->AuthFlag
) != 0);
487 Point
= (VOID
*) GetVariableNamePtr (Variable
, StoreInfo
->AuthFlag
);
488 if (CompareVariableName (StoreInfo
, VariableName
, Point
, NameSizeOfVariable (VariableHeader
, StoreInfo
->AuthFlag
))) {
489 PtrTrack
->CurrPtr
= Variable
;
495 return EFI_NOT_FOUND
;
499 Get HOB variable store.
503 GetHobVariableStore (
504 OUT VARIABLE_STORE_INFO
*StoreInfo
,
505 OUT VARIABLE_STORE_HEADER
**VariableStoreHeader
508 EFI_HOB_GUID_TYPE
*GuidHob
;
510 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
511 if (GuidHob
!= NULL
) {
512 *VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) GET_GUID_HOB_DATA (GuidHob
);
513 StoreInfo
->AuthFlag
= TRUE
;
515 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
516 if (GuidHob
!= NULL
) {
517 *VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) GET_GUID_HOB_DATA (GuidHob
);
518 StoreInfo
->AuthFlag
= FALSE
;
524 Return the variable store header and the store info based on the Index.
526 @param Type The type of the variable store.
527 @param StoreInfo Return the store info.
529 @return Pointer to the variable store header.
531 VARIABLE_STORE_HEADER
*
533 IN VARIABLE_STORE_TYPE Type
,
534 OUT VARIABLE_STORE_INFO
*StoreInfo
537 EFI_HOB_GUID_TYPE
*GuidHob
;
538 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
539 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
540 EFI_PHYSICAL_ADDRESS NvStorageBase
;
541 UINT32 NvStorageSize
;
542 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*FtwLastWriteData
;
545 StoreInfo
->IndexTable
= NULL
;
546 StoreInfo
->FtwLastWriteData
= NULL
;
547 StoreInfo
->AuthFlag
= FALSE
;
548 VariableStoreHeader
= NULL
;
550 case VariableStoreTypeHob
:
551 GetHobVariableStore (StoreInfo
, &VariableStoreHeader
);
555 case VariableStoreTypeNv
:
556 if (GetBootModeHob () != BOOT_IN_RECOVERY_MODE
) {
558 // The content of NV storage for variable is not reliable in recovery boot mode.
561 NvStorageSize
= PcdGet32 (PcdFlashNvStorageVariableSize
);
562 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) (PcdGet64 (PcdFlashNvStorageVariableBase64
) != 0 ?
563 PcdGet64 (PcdFlashNvStorageVariableBase64
) :
564 PcdGet32 (PcdFlashNvStorageVariableBase
)
567 // First let FvHeader point to NV storage base.
569 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) NvStorageBase
;
572 // Check the FTW last write data hob.
575 GuidHob
= GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid
);
576 if (GuidHob
!= NULL
) {
577 FtwLastWriteData
= (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*) GET_GUID_HOB_DATA (GuidHob
);
578 if (FtwLastWriteData
->TargetAddress
== NvStorageBase
) {
580 // Let FvHeader point to spare block.
582 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) FtwLastWriteData
->SpareAddress
;
583 DEBUG ((EFI_D_INFO
, "PeiVariable: NV storage is backed up in spare block: 0x%x\n", (UINTN
) FtwLastWriteData
->SpareAddress
));
584 } else if ((FtwLastWriteData
->TargetAddress
> NvStorageBase
) && (FtwLastWriteData
->TargetAddress
< (NvStorageBase
+ NvStorageSize
))) {
585 StoreInfo
->FtwLastWriteData
= FtwLastWriteData
;
587 // Flash NV storage from the offset is backed up in spare block.
589 BackUpOffset
= (UINT32
) (FtwLastWriteData
->TargetAddress
- NvStorageBase
);
590 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
));
592 // At least one block data in flash NV storage is still valid, so still leave FvHeader point to NV storage base.
598 // Check if the Firmware Volume is not corrupted
600 if ((FvHeader
->Signature
!= EFI_FVH_SIGNATURE
) || (!CompareGuid (&gEfiSystemNvDataFvGuid
, &FvHeader
->FileSystemGuid
))) {
601 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
605 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINT8
*) FvHeader
+ FvHeader
->HeaderLength
);
607 StoreInfo
->AuthFlag
= (BOOLEAN
) (CompareGuid (&VariableStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
));
609 GuidHob
= GetFirstGuidHob (&gEfiVariableIndexTableGuid
);
610 if (GuidHob
!= NULL
) {
611 StoreInfo
->IndexTable
= GET_GUID_HOB_DATA (GuidHob
);
614 // If it's the first time to access variable region in flash, create a guid hob to record
615 // VAR_ADDED type variable info.
616 // Note that as the resource of PEI phase is limited, only store the limited number of
617 // VAR_ADDED type variables to reduce access time.
619 StoreInfo
->IndexTable
= (VARIABLE_INDEX_TABLE
*) BuildGuidHob (&gEfiVariableIndexTableGuid
, sizeof (VARIABLE_INDEX_TABLE
));
620 StoreInfo
->IndexTable
->Length
= 0;
621 StoreInfo
->IndexTable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
622 StoreInfo
->IndexTable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
623 StoreInfo
->IndexTable
->GoneThrough
= 0;
633 StoreInfo
->VariableStoreHeader
= VariableStoreHeader
;
634 return VariableStoreHeader
;
638 Get variable header that has consecutive content.
640 @param StoreInfo Pointer to variable store info structure.
641 @param Variable Pointer to the Variable Header.
642 @param VariableHeader Pointer to Pointer to the Variable Header that has consecutive content.
644 @retval TRUE Variable header is valid.
645 @retval FALSE Variable header is not valid.
650 IN VARIABLE_STORE_INFO
*StoreInfo
,
651 IN VARIABLE_HEADER
*Variable
,
652 OUT VARIABLE_HEADER
**VariableHeader
655 EFI_PHYSICAL_ADDRESS TargetAddress
;
656 EFI_PHYSICAL_ADDRESS SpareAddress
;
657 EFI_HOB_GUID_TYPE
*GuidHob
;
658 UINTN PartialHeaderSize
;
660 if (Variable
== NULL
) {
665 // First assume variable header pointed by Variable is consecutive.
667 *VariableHeader
= Variable
;
669 if (StoreInfo
->FtwLastWriteData
!= NULL
) {
670 TargetAddress
= StoreInfo
->FtwLastWriteData
->TargetAddress
;
671 SpareAddress
= StoreInfo
->FtwLastWriteData
->SpareAddress
;
672 if (((UINTN
) Variable
> (UINTN
) SpareAddress
) &&
673 (((UINTN
) Variable
- (UINTN
) SpareAddress
+ (UINTN
) TargetAddress
) >= (UINTN
) GetEndPointer (StoreInfo
->VariableStoreHeader
))) {
675 // Reach the end of variable store.
679 if (((UINTN
) Variable
< (UINTN
) TargetAddress
) && (((UINTN
) Variable
+ GetVariableHeaderSize (StoreInfo
->AuthFlag
)) > (UINTN
) TargetAddress
)) {
681 // Variable header pointed by Variable is inconsecutive,
682 // create a guid hob to combine the two partial variable header content together.
684 GuidHob
= GetFirstGuidHob (&gEfiCallerIdGuid
);
685 if (GuidHob
!= NULL
) {
686 *VariableHeader
= (VARIABLE_HEADER
*) GET_GUID_HOB_DATA (GuidHob
);
688 *VariableHeader
= (VARIABLE_HEADER
*) BuildGuidHob (&gEfiCallerIdGuid
, GetVariableHeaderSize (StoreInfo
->AuthFlag
));
689 PartialHeaderSize
= (UINTN
) TargetAddress
- (UINTN
) Variable
;
691 // Partial content is in NV storage.
693 CopyMem ((UINT8
*) *VariableHeader
, (UINT8
*) Variable
, PartialHeaderSize
);
695 // Another partial content is in spare block.
697 CopyMem ((UINT8
*) *VariableHeader
+ PartialHeaderSize
, (UINT8
*) (UINTN
) SpareAddress
, GetVariableHeaderSize (StoreInfo
->AuthFlag
) - PartialHeaderSize
);
701 if (Variable
>= GetEndPointer (StoreInfo
->VariableStoreHeader
)) {
703 // Reach the end of variable store.
709 return IsValidVariableHeader (*VariableHeader
);
713 Get variable name or data to output buffer.
715 @param StoreInfo Pointer to variable store info structure.
716 @param NameOrData Pointer to the variable name/data that may be inconsecutive.
717 @param Size Variable name/data size.
718 @param Buffer Pointer to output buffer to hold the variable name/data.
722 GetVariableNameOrData (
723 IN VARIABLE_STORE_INFO
*StoreInfo
,
724 IN UINT8
*NameOrData
,
729 EFI_PHYSICAL_ADDRESS TargetAddress
;
730 EFI_PHYSICAL_ADDRESS SpareAddress
;
733 if (StoreInfo
->FtwLastWriteData
!= NULL
) {
734 TargetAddress
= StoreInfo
->FtwLastWriteData
->TargetAddress
;
735 SpareAddress
= StoreInfo
->FtwLastWriteData
->SpareAddress
;
736 if (((UINTN
) NameOrData
< (UINTN
) TargetAddress
) && (((UINTN
) NameOrData
+ Size
) > (UINTN
) TargetAddress
)) {
738 // Variable name/data is inconsecutive.
740 PartialSize
= (UINTN
) TargetAddress
- (UINTN
) NameOrData
;
742 // Partial content is in NV storage.
744 CopyMem (Buffer
, NameOrData
, PartialSize
);
746 // Another partial content is in spare block.
748 CopyMem (Buffer
+ PartialSize
, (UINT8
*) (UINTN
) SpareAddress
, Size
- PartialSize
);
754 // Variable name/data is consecutive.
756 CopyMem (Buffer
, NameOrData
, Size
);
760 Find the variable in the specified variable store.
762 @param StoreInfo Pointer to the store info structure.
763 @param VariableName Name of the variable to be found
764 @param VendorGuid Vendor GUID to be found.
765 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
767 @retval EFI_SUCCESS Variable found successfully
768 @retval EFI_NOT_FOUND Variable not found
769 @retval EFI_INVALID_PARAMETER Invalid variable name
774 IN VARIABLE_STORE_INFO
*StoreInfo
,
775 IN CONST CHAR16
*VariableName
,
776 IN CONST EFI_GUID
*VendorGuid
,
777 OUT VARIABLE_POINTER_TRACK
*PtrTrack
780 VARIABLE_HEADER
*Variable
;
781 VARIABLE_HEADER
*LastVariable
;
782 VARIABLE_HEADER
*MaxIndex
;
786 VARIABLE_HEADER
*InDeletedVariable
;
787 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
788 VARIABLE_INDEX_TABLE
*IndexTable
;
789 VARIABLE_HEADER
*VariableHeader
;
791 VariableStoreHeader
= StoreInfo
->VariableStoreHeader
;
793 if (VariableStoreHeader
== NULL
) {
794 return EFI_INVALID_PARAMETER
;
797 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
798 return EFI_UNSUPPORTED
;
801 if (~VariableStoreHeader
->Size
== 0) {
802 return EFI_NOT_FOUND
;
805 IndexTable
= StoreInfo
->IndexTable
;
806 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
);
807 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
);
809 InDeletedVariable
= NULL
;
812 // No Variable Address equals zero, so 0 as initial value is safe.
815 VariableHeader
= NULL
;
817 if (IndexTable
!= NULL
) {
819 // traverse the variable index table to look for varible.
820 // The IndexTable->Index[Index] records the distance of two neighbouring VAR_ADDED type variables.
822 for (Offset
= 0, Index
= 0; Index
< IndexTable
->Length
; Index
++) {
823 ASSERT (Index
< sizeof (IndexTable
->Index
) / sizeof (IndexTable
->Index
[0]));
824 Offset
+= IndexTable
->Index
[Index
];
825 MaxIndex
= (VARIABLE_HEADER
*) ((UINT8
*) IndexTable
->StartPtr
+ Offset
);
826 GetVariableHeader (StoreInfo
, MaxIndex
, &VariableHeader
);
827 if (CompareWithValidVariable (StoreInfo
, MaxIndex
, VariableHeader
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
828 if (VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
829 InDeletedVariable
= PtrTrack
->CurrPtr
;
836 if (IndexTable
->GoneThrough
!= 0) {
838 // If the table has all the existing variables indexed, return.
840 PtrTrack
->CurrPtr
= InDeletedVariable
;
841 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
845 if (MaxIndex
!= NULL
) {
847 // HOB exists but the variable cannot be found in HOB
848 // If not found in HOB, then let's start from the MaxIndex we've found.
850 Variable
= GetNextVariablePtr (StoreInfo
, MaxIndex
, VariableHeader
);
851 LastVariable
= MaxIndex
;
854 // Start Pointers for the variable.
855 // Actual Data Pointer where data can be written.
857 Variable
= PtrTrack
->StartPtr
;
858 LastVariable
= PtrTrack
->StartPtr
;
862 // Find the variable by walk through variable store
865 while (GetVariableHeader (StoreInfo
, Variable
, &VariableHeader
)) {
866 if (VariableHeader
->State
== VAR_ADDED
|| VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
868 // Record Variable in VariableIndex HOB
870 if ((IndexTable
!= NULL
) && !StopRecord
) {
871 Offset
= (UINTN
) Variable
- (UINTN
) LastVariable
;
872 if ((Offset
> 0x0FFFF) || (IndexTable
->Length
== sizeof (IndexTable
->Index
) / sizeof (IndexTable
->Index
[0]))) {
874 // Stop to record if the distance of two neighbouring VAR_ADDED variable is larger than the allowable scope(UINT16),
875 // or the record buffer is full.
879 IndexTable
->Index
[IndexTable
->Length
++] = (UINT16
) Offset
;
880 LastVariable
= Variable
;
884 if (CompareWithValidVariable (StoreInfo
, Variable
, VariableHeader
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
885 if (VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
886 InDeletedVariable
= PtrTrack
->CurrPtr
;
893 Variable
= GetNextVariablePtr (StoreInfo
, Variable
, VariableHeader
);
896 // If gone through the VariableStore, that means we never find in Firmware any more.
898 if ((IndexTable
!= NULL
) && !StopRecord
) {
899 IndexTable
->GoneThrough
= 1;
902 PtrTrack
->CurrPtr
= InDeletedVariable
;
904 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
908 Find the variable in HOB and Non-Volatile variable storages.
910 @param VariableName Name of the variable to be found
911 @param VendorGuid Vendor GUID to be found.
912 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
913 @param StoreInfo Return the store info.
915 @retval EFI_SUCCESS Variable found successfully
916 @retval EFI_NOT_FOUND Variable not found
917 @retval EFI_INVALID_PARAMETER Invalid variable name
921 IN CONST CHAR16
*VariableName
,
922 IN CONST EFI_GUID
*VendorGuid
,
923 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
924 OUT VARIABLE_STORE_INFO
*StoreInfo
928 VARIABLE_STORE_TYPE Type
;
930 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
931 return EFI_INVALID_PARAMETER
;
934 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
935 GetVariableStore (Type
, StoreInfo
);
936 Status
= FindVariableEx (
942 if (!EFI_ERROR (Status
)) {
947 return EFI_NOT_FOUND
;
951 This service retrieves a variable's value using its name and GUID.
953 Read the specified variable from the UEFI variable store. If the Data
954 buffer is too small to hold the contents of the variable, the error
955 EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer
956 size to obtain the data.
958 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
959 @param VariableName A pointer to a null-terminated string that is the variable's name.
960 @param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of
961 VariableGuid and VariableName must be unique.
962 @param Attributes If non-NULL, on return, points to the variable's attributes.
963 @param DataSize On entry, points to the size in bytes of the Data buffer.
964 On return, points to the size of the data returned in Data.
965 @param Data Points to the buffer which will hold the returned variable value.
966 May be NULL with a zero DataSize in order to determine the size of the buffer needed.
968 @retval EFI_SUCCESS The variable was read successfully.
969 @retval EFI_NOT_FOUND The variable was be found.
970 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data.
971 DataSize is updated with the size required for
972 the specified variable.
973 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.
974 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
980 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
981 IN CONST CHAR16
*VariableName
,
982 IN CONST EFI_GUID
*VariableGuid
,
983 OUT UINT32
*Attributes
,
984 IN OUT UINTN
*DataSize
,
985 OUT VOID
*Data OPTIONAL
988 VARIABLE_POINTER_TRACK Variable
;
991 VARIABLE_STORE_INFO StoreInfo
;
992 VARIABLE_HEADER
*VariableHeader
;
994 if (VariableName
== NULL
|| VariableGuid
== NULL
|| DataSize
== NULL
) {
995 return EFI_INVALID_PARAMETER
;
998 if (VariableName
[0] == 0) {
999 return EFI_NOT_FOUND
;
1002 VariableHeader
= NULL
;
1005 // Find existing variable
1007 Status
= FindVariable (VariableName
, VariableGuid
, &Variable
, &StoreInfo
);
1008 if (EFI_ERROR (Status
)) {
1011 GetVariableHeader (&StoreInfo
, Variable
.CurrPtr
, &VariableHeader
);
1016 VarDataSize
= DataSizeOfVariable (VariableHeader
, StoreInfo
.AuthFlag
);
1017 if (*DataSize
>= VarDataSize
) {
1019 return EFI_INVALID_PARAMETER
;
1022 GetVariableNameOrData (&StoreInfo
, GetVariableDataPtr (Variable
.CurrPtr
, VariableHeader
, StoreInfo
.AuthFlag
), VarDataSize
, Data
);
1024 if (Attributes
!= NULL
) {
1025 *Attributes
= VariableHeader
->Attributes
;
1028 *DataSize
= VarDataSize
;
1031 *DataSize
= VarDataSize
;
1032 return EFI_BUFFER_TOO_SMALL
;
1037 Return the next variable name and GUID.
1039 This function is called multiple times to retrieve the VariableName
1040 and VariableGuid of all variables currently available in the system.
1041 On each call, the previous results are passed into the interface,
1042 and, on return, the interface returns the data for the next
1043 interface. When the entire variable list has been returned,
1044 EFI_NOT_FOUND is returned.
1046 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
1048 @param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.
1049 On return, the size of the variable name buffer.
1050 @param VariableName On entry, a pointer to a null-terminated string that is the variable's name.
1051 On return, points to the next variable's null-terminated name string.
1052 @param VariableGuid On entry, a pointer to an EFI_GUID that is the variable's GUID.
1053 On return, a pointer to the next variable's GUID.
1055 @retval EFI_SUCCESS The variable was read successfully.
1056 @retval EFI_NOT_FOUND The variable could not be found.
1057 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting
1058 data. VariableNameSize is updated with the size
1059 required for the specified variable.
1060 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or
1061 VariableNameSize is NULL.
1062 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
1067 PeiGetNextVariableName (
1068 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
1069 IN OUT UINTN
*VariableNameSize
,
1070 IN OUT CHAR16
*VariableName
,
1071 IN OUT EFI_GUID
*VariableGuid
1074 VARIABLE_STORE_TYPE Type
;
1075 VARIABLE_POINTER_TRACK Variable
;
1076 VARIABLE_POINTER_TRACK VariableInHob
;
1077 VARIABLE_POINTER_TRACK VariablePtrTrack
;
1080 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
1081 VARIABLE_HEADER
*VariableHeader
;
1082 VARIABLE_STORE_INFO StoreInfo
;
1083 VARIABLE_STORE_INFO StoreInfoForNv
;
1084 VARIABLE_STORE_INFO StoreInfoForHob
;
1086 if (VariableName
== NULL
|| VariableGuid
== NULL
|| VariableNameSize
== NULL
) {
1087 return EFI_INVALID_PARAMETER
;
1090 VariableHeader
= NULL
;
1092 Status
= FindVariable (VariableName
, VariableGuid
, &Variable
, &StoreInfo
);
1093 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
1097 if (VariableName
[0] != 0) {
1099 // If variable name is not NULL, get next variable
1101 GetVariableHeader (&StoreInfo
, Variable
.CurrPtr
, &VariableHeader
);
1102 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);
1105 VariableStoreHeader
[VariableStoreTypeHob
] = GetVariableStore (VariableStoreTypeHob
, &StoreInfoForHob
);
1106 VariableStoreHeader
[VariableStoreTypeNv
] = GetVariableStore (VariableStoreTypeNv
, &StoreInfoForNv
);
1110 // Switch from HOB to Non-Volatile.
1112 while (!GetVariableHeader (&StoreInfo
, Variable
.CurrPtr
, &VariableHeader
)) {
1114 // Find current storage index
1116 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
1117 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
1121 ASSERT (Type
< VariableStoreTypeMax
);
1123 // Switch to next storage
1125 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
1126 if (VariableStoreHeader
[Type
] != NULL
) {
1131 // Capture the case that
1132 // 1. current storage is the last one, or
1133 // 2. no further storage
1135 if (Type
== VariableStoreTypeMax
) {
1136 return EFI_NOT_FOUND
;
1138 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
1139 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
1140 Variable
.CurrPtr
= Variable
.StartPtr
;
1141 GetVariableStore (Type
, &StoreInfo
);
1144 if (VariableHeader
->State
== VAR_ADDED
|| VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1145 if (VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1147 // If it is a IN_DELETED_TRANSITION variable,
1148 // and there is also a same ADDED one at the same time,
1151 Status
= FindVariableEx (
1153 GetVariableNamePtr (Variable
.CurrPtr
, StoreInfo
.AuthFlag
),
1154 GetVendorGuidPtr (VariableHeader
, StoreInfo
.AuthFlag
),
1157 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
!= Variable
.CurrPtr
) {
1158 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);
1164 // Don't return NV variable when HOB overrides it
1166 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
1167 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
1169 Status
= FindVariableEx (
1171 GetVariableNamePtr (Variable
.CurrPtr
, StoreInfo
.AuthFlag
),
1172 GetVendorGuidPtr (VariableHeader
, StoreInfo
.AuthFlag
),
1175 if (!EFI_ERROR (Status
)) {
1176 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);
1181 VarNameSize
= NameSizeOfVariable (VariableHeader
, StoreInfo
.AuthFlag
);
1182 ASSERT (VarNameSize
!= 0);
1184 if (VarNameSize
<= *VariableNameSize
) {
1185 GetVariableNameOrData (&StoreInfo
, (UINT8
*) GetVariableNamePtr (Variable
.CurrPtr
, StoreInfo
.AuthFlag
), VarNameSize
, (UINT8
*) VariableName
);
1187 CopyMem (VariableGuid
, GetVendorGuidPtr (VariableHeader
, StoreInfo
.AuthFlag
), sizeof (EFI_GUID
));
1189 Status
= EFI_SUCCESS
;
1191 Status
= EFI_BUFFER_TOO_SMALL
;
1194 *VariableNameSize
= VarNameSize
;
1196 // Variable is found
1200 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);