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
;
511 // Make sure there is no more than one Variable HOB.
514 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
515 if (GuidHob
!= NULL
) {
516 if ((GetNextGuidHob (&gEfiAuthenticatedVariableGuid
, GET_NEXT_HOB (GuidHob
)) != NULL
)) {
517 DEBUG ((DEBUG_ERROR
, "ERROR: Found two Auth Variable HOBs\n"));
519 } else if (GetFirstGuidHob (&gEfiVariableGuid
) != NULL
) {
520 DEBUG ((DEBUG_ERROR
, "ERROR: Found one Auth + one Normal Variable HOBs\n"));
524 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
525 if (GuidHob
!= NULL
) {
526 if ((GetNextGuidHob (&gEfiVariableGuid
, GET_NEXT_HOB (GuidHob
)) != NULL
)) {
527 DEBUG ((DEBUG_ERROR
, "ERROR: Found two Normal Variable HOBs\n"));
534 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
535 if (GuidHob
!= NULL
) {
536 *VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) GET_GUID_HOB_DATA (GuidHob
);
537 StoreInfo
->AuthFlag
= TRUE
;
539 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
540 if (GuidHob
!= NULL
) {
541 *VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) GET_GUID_HOB_DATA (GuidHob
);
542 StoreInfo
->AuthFlag
= FALSE
;
548 Return the variable store header and the store info based on the Index.
550 @param Type The type of the variable store.
551 @param StoreInfo Return the store info.
553 @return Pointer to the variable store header.
555 VARIABLE_STORE_HEADER
*
557 IN VARIABLE_STORE_TYPE Type
,
558 OUT VARIABLE_STORE_INFO
*StoreInfo
561 EFI_HOB_GUID_TYPE
*GuidHob
;
562 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
563 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
564 EFI_PHYSICAL_ADDRESS NvStorageBase
;
565 UINT32 NvStorageSize
;
566 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*FtwLastWriteData
;
569 StoreInfo
->IndexTable
= NULL
;
570 StoreInfo
->FtwLastWriteData
= NULL
;
571 StoreInfo
->AuthFlag
= FALSE
;
572 VariableStoreHeader
= NULL
;
574 case VariableStoreTypeHob
:
575 GetHobVariableStore (StoreInfo
, &VariableStoreHeader
);
579 case VariableStoreTypeNv
:
580 if (GetBootModeHob () != BOOT_IN_RECOVERY_MODE
) {
582 // The content of NV storage for variable is not reliable in recovery boot mode.
585 NvStorageSize
= PcdGet32 (PcdFlashNvStorageVariableSize
);
586 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) (PcdGet64 (PcdFlashNvStorageVariableBase64
) != 0 ?
587 PcdGet64 (PcdFlashNvStorageVariableBase64
) :
588 PcdGet32 (PcdFlashNvStorageVariableBase
)
591 // First let FvHeader point to NV storage base.
593 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) NvStorageBase
;
596 // Check the FTW last write data hob.
599 GuidHob
= GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid
);
600 if (GuidHob
!= NULL
) {
601 FtwLastWriteData
= (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*) GET_GUID_HOB_DATA (GuidHob
);
602 if (FtwLastWriteData
->TargetAddress
== NvStorageBase
) {
604 // Let FvHeader point to spare block.
606 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) FtwLastWriteData
->SpareAddress
;
607 DEBUG ((EFI_D_INFO
, "PeiVariable: NV storage is backed up in spare block: 0x%x\n", (UINTN
) FtwLastWriteData
->SpareAddress
));
608 } else if ((FtwLastWriteData
->TargetAddress
> NvStorageBase
) && (FtwLastWriteData
->TargetAddress
< (NvStorageBase
+ NvStorageSize
))) {
609 StoreInfo
->FtwLastWriteData
= FtwLastWriteData
;
611 // Flash NV storage from the offset is backed up in spare block.
613 BackUpOffset
= (UINT32
) (FtwLastWriteData
->TargetAddress
- NvStorageBase
);
614 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
));
616 // At least one block data in flash NV storage is still valid, so still leave FvHeader point to NV storage base.
622 // Check if the Firmware Volume is not corrupted
624 if ((FvHeader
->Signature
!= EFI_FVH_SIGNATURE
) || (!CompareGuid (&gEfiSystemNvDataFvGuid
, &FvHeader
->FileSystemGuid
))) {
625 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
629 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINT8
*) FvHeader
+ FvHeader
->HeaderLength
);
631 StoreInfo
->AuthFlag
= (BOOLEAN
) (CompareGuid (&VariableStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
));
633 GuidHob
= GetFirstGuidHob (&gEfiVariableIndexTableGuid
);
634 if (GuidHob
!= NULL
) {
635 StoreInfo
->IndexTable
= GET_GUID_HOB_DATA (GuidHob
);
638 // If it's the first time to access variable region in flash, create a guid hob to record
639 // VAR_ADDED type variable info.
640 // Note that as the resource of PEI phase is limited, only store the limited number of
641 // VAR_ADDED type variables to reduce access time.
643 StoreInfo
->IndexTable
= (VARIABLE_INDEX_TABLE
*) BuildGuidHob (&gEfiVariableIndexTableGuid
, sizeof (VARIABLE_INDEX_TABLE
));
644 StoreInfo
->IndexTable
->Length
= 0;
645 StoreInfo
->IndexTable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
646 StoreInfo
->IndexTable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
647 StoreInfo
->IndexTable
->GoneThrough
= 0;
657 StoreInfo
->VariableStoreHeader
= VariableStoreHeader
;
658 return VariableStoreHeader
;
662 Get variable header that has consecutive content.
664 @param StoreInfo Pointer to variable store info structure.
665 @param Variable Pointer to the Variable Header.
666 @param VariableHeader Pointer to Pointer to the Variable Header that has consecutive content.
668 @retval TRUE Variable header is valid.
669 @retval FALSE Variable header is not valid.
674 IN VARIABLE_STORE_INFO
*StoreInfo
,
675 IN VARIABLE_HEADER
*Variable
,
676 OUT VARIABLE_HEADER
**VariableHeader
679 EFI_PHYSICAL_ADDRESS TargetAddress
;
680 EFI_PHYSICAL_ADDRESS SpareAddress
;
681 EFI_HOB_GUID_TYPE
*GuidHob
;
682 UINTN PartialHeaderSize
;
684 if (Variable
== NULL
) {
689 // First assume variable header pointed by Variable is consecutive.
691 *VariableHeader
= Variable
;
693 if (StoreInfo
->FtwLastWriteData
!= NULL
) {
694 TargetAddress
= StoreInfo
->FtwLastWriteData
->TargetAddress
;
695 SpareAddress
= StoreInfo
->FtwLastWriteData
->SpareAddress
;
696 if (((UINTN
) Variable
> (UINTN
) SpareAddress
) &&
697 (((UINTN
) Variable
- (UINTN
) SpareAddress
+ (UINTN
) TargetAddress
) >= (UINTN
) GetEndPointer (StoreInfo
->VariableStoreHeader
))) {
699 // Reach the end of variable store.
703 if (((UINTN
) Variable
< (UINTN
) TargetAddress
) && (((UINTN
) Variable
+ GetVariableHeaderSize (StoreInfo
->AuthFlag
)) > (UINTN
) TargetAddress
)) {
705 // Variable header pointed by Variable is inconsecutive,
706 // create a guid hob to combine the two partial variable header content together.
708 GuidHob
= GetFirstGuidHob (&gEfiCallerIdGuid
);
709 if (GuidHob
!= NULL
) {
710 *VariableHeader
= (VARIABLE_HEADER
*) GET_GUID_HOB_DATA (GuidHob
);
712 *VariableHeader
= (VARIABLE_HEADER
*) BuildGuidHob (&gEfiCallerIdGuid
, GetVariableHeaderSize (StoreInfo
->AuthFlag
));
713 PartialHeaderSize
= (UINTN
) TargetAddress
- (UINTN
) Variable
;
715 // Partial content is in NV storage.
717 CopyMem ((UINT8
*) *VariableHeader
, (UINT8
*) Variable
, PartialHeaderSize
);
719 // Another partial content is in spare block.
721 CopyMem ((UINT8
*) *VariableHeader
+ PartialHeaderSize
, (UINT8
*) (UINTN
) SpareAddress
, GetVariableHeaderSize (StoreInfo
->AuthFlag
) - PartialHeaderSize
);
725 if (Variable
>= GetEndPointer (StoreInfo
->VariableStoreHeader
)) {
727 // Reach the end of variable store.
733 return IsValidVariableHeader (*VariableHeader
);
737 Get variable name or data to output buffer.
739 @param StoreInfo Pointer to variable store info structure.
740 @param NameOrData Pointer to the variable name/data that may be inconsecutive.
741 @param Size Variable name/data size.
742 @param Buffer Pointer to output buffer to hold the variable name/data.
746 GetVariableNameOrData (
747 IN VARIABLE_STORE_INFO
*StoreInfo
,
748 IN UINT8
*NameOrData
,
753 EFI_PHYSICAL_ADDRESS TargetAddress
;
754 EFI_PHYSICAL_ADDRESS SpareAddress
;
757 if (StoreInfo
->FtwLastWriteData
!= NULL
) {
758 TargetAddress
= StoreInfo
->FtwLastWriteData
->TargetAddress
;
759 SpareAddress
= StoreInfo
->FtwLastWriteData
->SpareAddress
;
760 if (((UINTN
) NameOrData
< (UINTN
) TargetAddress
) && (((UINTN
) NameOrData
+ Size
) > (UINTN
) TargetAddress
)) {
762 // Variable name/data is inconsecutive.
764 PartialSize
= (UINTN
) TargetAddress
- (UINTN
) NameOrData
;
766 // Partial content is in NV storage.
768 CopyMem (Buffer
, NameOrData
, PartialSize
);
770 // Another partial content is in spare block.
772 CopyMem (Buffer
+ PartialSize
, (UINT8
*) (UINTN
) SpareAddress
, Size
- PartialSize
);
778 // Variable name/data is consecutive.
780 CopyMem (Buffer
, NameOrData
, Size
);
784 Find the variable in the specified variable store.
786 @param StoreInfo Pointer to the store info structure.
787 @param VariableName Name of the variable to be found
788 @param VendorGuid Vendor GUID to be found.
789 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
791 @retval EFI_SUCCESS Variable found successfully
792 @retval EFI_NOT_FOUND Variable not found
793 @retval EFI_INVALID_PARAMETER Invalid variable name
798 IN VARIABLE_STORE_INFO
*StoreInfo
,
799 IN CONST CHAR16
*VariableName
,
800 IN CONST EFI_GUID
*VendorGuid
,
801 OUT VARIABLE_POINTER_TRACK
*PtrTrack
804 VARIABLE_HEADER
*Variable
;
805 VARIABLE_HEADER
*LastVariable
;
806 VARIABLE_HEADER
*MaxIndex
;
810 VARIABLE_HEADER
*InDeletedVariable
;
811 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
812 VARIABLE_INDEX_TABLE
*IndexTable
;
813 VARIABLE_HEADER
*VariableHeader
;
815 VariableStoreHeader
= StoreInfo
->VariableStoreHeader
;
817 if (VariableStoreHeader
== NULL
) {
818 return EFI_INVALID_PARAMETER
;
821 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
822 return EFI_UNSUPPORTED
;
825 if (~VariableStoreHeader
->Size
== 0) {
826 return EFI_NOT_FOUND
;
829 IndexTable
= StoreInfo
->IndexTable
;
830 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
);
831 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
);
833 InDeletedVariable
= NULL
;
836 // No Variable Address equals zero, so 0 as initial value is safe.
839 VariableHeader
= NULL
;
841 if (IndexTable
!= NULL
) {
843 // traverse the variable index table to look for varible.
844 // The IndexTable->Index[Index] records the distance of two neighbouring VAR_ADDED type variables.
846 for (Offset
= 0, Index
= 0; Index
< IndexTable
->Length
; Index
++) {
847 ASSERT (Index
< sizeof (IndexTable
->Index
) / sizeof (IndexTable
->Index
[0]));
848 Offset
+= IndexTable
->Index
[Index
];
849 MaxIndex
= (VARIABLE_HEADER
*) ((UINT8
*) IndexTable
->StartPtr
+ Offset
);
850 GetVariableHeader (StoreInfo
, MaxIndex
, &VariableHeader
);
851 if (CompareWithValidVariable (StoreInfo
, MaxIndex
, VariableHeader
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
852 if (VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
853 InDeletedVariable
= PtrTrack
->CurrPtr
;
860 if (IndexTable
->GoneThrough
!= 0) {
862 // If the table has all the existing variables indexed, return.
864 PtrTrack
->CurrPtr
= InDeletedVariable
;
865 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
869 if (MaxIndex
!= NULL
) {
871 // HOB exists but the variable cannot be found in HOB
872 // If not found in HOB, then let's start from the MaxIndex we've found.
874 Variable
= GetNextVariablePtr (StoreInfo
, MaxIndex
, VariableHeader
);
875 LastVariable
= MaxIndex
;
878 // Start Pointers for the variable.
879 // Actual Data Pointer where data can be written.
881 Variable
= PtrTrack
->StartPtr
;
882 LastVariable
= PtrTrack
->StartPtr
;
886 // Find the variable by walk through variable store
889 while (GetVariableHeader (StoreInfo
, Variable
, &VariableHeader
)) {
890 if (VariableHeader
->State
== VAR_ADDED
|| VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
892 // Record Variable in VariableIndex HOB
894 if ((IndexTable
!= NULL
) && !StopRecord
) {
895 Offset
= (UINTN
) Variable
- (UINTN
) LastVariable
;
896 if ((Offset
> 0x0FFFF) || (IndexTable
->Length
== sizeof (IndexTable
->Index
) / sizeof (IndexTable
->Index
[0]))) {
898 // Stop to record if the distance of two neighbouring VAR_ADDED variable is larger than the allowable scope(UINT16),
899 // or the record buffer is full.
903 IndexTable
->Index
[IndexTable
->Length
++] = (UINT16
) Offset
;
904 LastVariable
= Variable
;
908 if (CompareWithValidVariable (StoreInfo
, Variable
, VariableHeader
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
909 if (VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
910 InDeletedVariable
= PtrTrack
->CurrPtr
;
917 Variable
= GetNextVariablePtr (StoreInfo
, Variable
, VariableHeader
);
920 // If gone through the VariableStore, that means we never find in Firmware any more.
922 if ((IndexTable
!= NULL
) && !StopRecord
) {
923 IndexTable
->GoneThrough
= 1;
926 PtrTrack
->CurrPtr
= InDeletedVariable
;
928 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
932 Find the variable in HOB and Non-Volatile variable storages.
934 @param VariableName Name of the variable to be found
935 @param VendorGuid Vendor GUID to be found.
936 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
937 @param StoreInfo Return the store info.
939 @retval EFI_SUCCESS Variable found successfully
940 @retval EFI_NOT_FOUND Variable not found
941 @retval EFI_INVALID_PARAMETER Invalid variable name
945 IN CONST CHAR16
*VariableName
,
946 IN CONST EFI_GUID
*VendorGuid
,
947 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
948 OUT VARIABLE_STORE_INFO
*StoreInfo
952 VARIABLE_STORE_TYPE Type
;
954 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
955 return EFI_INVALID_PARAMETER
;
958 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
959 GetVariableStore (Type
, StoreInfo
);
960 Status
= FindVariableEx (
966 if (!EFI_ERROR (Status
)) {
971 return EFI_NOT_FOUND
;
975 This service retrieves a variable's value using its name and GUID.
977 Read the specified variable from the UEFI variable store. If the Data
978 buffer is too small to hold the contents of the variable, the error
979 EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer
980 size to obtain the data.
982 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
983 @param VariableName A pointer to a null-terminated string that is the variable's name.
984 @param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of
985 VariableGuid and VariableName must be unique.
986 @param Attributes If non-NULL, on return, points to the variable's attributes.
987 @param DataSize On entry, points to the size in bytes of the Data buffer.
988 On return, points to the size of the data returned in Data.
989 @param Data Points to the buffer which will hold the returned variable value.
990 May be NULL with a zero DataSize in order to determine the size of the buffer needed.
992 @retval EFI_SUCCESS The variable was read successfully.
993 @retval EFI_NOT_FOUND The variable was be found.
994 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data.
995 DataSize is updated with the size required for
996 the specified variable.
997 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.
998 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
1004 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
1005 IN CONST CHAR16
*VariableName
,
1006 IN CONST EFI_GUID
*VariableGuid
,
1007 OUT UINT32
*Attributes
,
1008 IN OUT UINTN
*DataSize
,
1009 OUT VOID
*Data OPTIONAL
1012 VARIABLE_POINTER_TRACK Variable
;
1015 VARIABLE_STORE_INFO StoreInfo
;
1016 VARIABLE_HEADER
*VariableHeader
;
1018 if (VariableName
== NULL
|| VariableGuid
== NULL
|| DataSize
== NULL
) {
1019 return EFI_INVALID_PARAMETER
;
1022 if (VariableName
[0] == 0) {
1023 return EFI_NOT_FOUND
;
1026 VariableHeader
= NULL
;
1029 // Find existing variable
1031 Status
= FindVariable (VariableName
, VariableGuid
, &Variable
, &StoreInfo
);
1032 if (EFI_ERROR (Status
)) {
1035 GetVariableHeader (&StoreInfo
, Variable
.CurrPtr
, &VariableHeader
);
1040 VarDataSize
= DataSizeOfVariable (VariableHeader
, StoreInfo
.AuthFlag
);
1041 if (*DataSize
>= VarDataSize
) {
1043 return EFI_INVALID_PARAMETER
;
1046 GetVariableNameOrData (&StoreInfo
, GetVariableDataPtr (Variable
.CurrPtr
, VariableHeader
, StoreInfo
.AuthFlag
), VarDataSize
, Data
);
1048 if (Attributes
!= NULL
) {
1049 *Attributes
= VariableHeader
->Attributes
;
1052 *DataSize
= VarDataSize
;
1055 *DataSize
= VarDataSize
;
1056 return EFI_BUFFER_TOO_SMALL
;
1061 Return the next variable name and GUID.
1063 This function is called multiple times to retrieve the VariableName
1064 and VariableGuid of all variables currently available in the system.
1065 On each call, the previous results are passed into the interface,
1066 and, on return, the interface returns the data for the next
1067 interface. When the entire variable list has been returned,
1068 EFI_NOT_FOUND is returned.
1070 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
1072 @param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.
1073 On return, the size of the variable name buffer.
1074 @param VariableName On entry, a pointer to a null-terminated string that is the variable's name.
1075 On return, points to the next variable's null-terminated name string.
1076 @param VariableGuid On entry, a pointer to an EFI_GUID that is the variable's GUID.
1077 On return, a pointer to the next variable's GUID.
1079 @retval EFI_SUCCESS The variable was read successfully.
1080 @retval EFI_NOT_FOUND The variable could not be found.
1081 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting
1082 data. VariableNameSize is updated with the size
1083 required for the specified variable.
1084 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or
1085 VariableNameSize is NULL.
1086 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
1091 PeiGetNextVariableName (
1092 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
1093 IN OUT UINTN
*VariableNameSize
,
1094 IN OUT CHAR16
*VariableName
,
1095 IN OUT EFI_GUID
*VariableGuid
1098 VARIABLE_STORE_TYPE Type
;
1099 VARIABLE_POINTER_TRACK Variable
;
1100 VARIABLE_POINTER_TRACK VariableInHob
;
1101 VARIABLE_POINTER_TRACK VariablePtrTrack
;
1104 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
1105 VARIABLE_HEADER
*VariableHeader
;
1106 VARIABLE_STORE_INFO StoreInfo
;
1107 VARIABLE_STORE_INFO StoreInfoForNv
;
1108 VARIABLE_STORE_INFO StoreInfoForHob
;
1110 if (VariableName
== NULL
|| VariableGuid
== NULL
|| VariableNameSize
== NULL
) {
1111 return EFI_INVALID_PARAMETER
;
1114 VariableHeader
= NULL
;
1116 Status
= FindVariable (VariableName
, VariableGuid
, &Variable
, &StoreInfo
);
1117 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
1121 if (VariableName
[0] != 0) {
1123 // If variable name is not NULL, get next variable
1125 GetVariableHeader (&StoreInfo
, Variable
.CurrPtr
, &VariableHeader
);
1126 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);
1129 VariableStoreHeader
[VariableStoreTypeHob
] = GetVariableStore (VariableStoreTypeHob
, &StoreInfoForHob
);
1130 VariableStoreHeader
[VariableStoreTypeNv
] = GetVariableStore (VariableStoreTypeNv
, &StoreInfoForNv
);
1134 // Switch from HOB to Non-Volatile.
1136 while (!GetVariableHeader (&StoreInfo
, Variable
.CurrPtr
, &VariableHeader
)) {
1138 // Find current storage index
1140 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
1141 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
1145 ASSERT (Type
< VariableStoreTypeMax
);
1147 // Switch to next storage
1149 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
1150 if (VariableStoreHeader
[Type
] != NULL
) {
1155 // Capture the case that
1156 // 1. current storage is the last one, or
1157 // 2. no further storage
1159 if (Type
== VariableStoreTypeMax
) {
1160 return EFI_NOT_FOUND
;
1162 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
1163 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
1164 Variable
.CurrPtr
= Variable
.StartPtr
;
1165 GetVariableStore (Type
, &StoreInfo
);
1168 if (VariableHeader
->State
== VAR_ADDED
|| VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1169 if (VariableHeader
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1171 // If it is a IN_DELETED_TRANSITION variable,
1172 // and there is also a same ADDED one at the same time,
1175 Status
= FindVariableEx (
1177 GetVariableNamePtr (Variable
.CurrPtr
, StoreInfo
.AuthFlag
),
1178 GetVendorGuidPtr (VariableHeader
, StoreInfo
.AuthFlag
),
1181 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
!= Variable
.CurrPtr
) {
1182 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);
1188 // Don't return NV variable when HOB overrides it
1190 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
1191 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
1193 Status
= FindVariableEx (
1195 GetVariableNamePtr (Variable
.CurrPtr
, StoreInfo
.AuthFlag
),
1196 GetVendorGuidPtr (VariableHeader
, StoreInfo
.AuthFlag
),
1199 if (!EFI_ERROR (Status
)) {
1200 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);
1205 VarNameSize
= NameSizeOfVariable (VariableHeader
, StoreInfo
.AuthFlag
);
1206 ASSERT (VarNameSize
!= 0);
1208 if (VarNameSize
<= *VariableNameSize
) {
1209 GetVariableNameOrData (&StoreInfo
, (UINT8
*) GetVariableNamePtr (Variable
.CurrPtr
, StoreInfo
.AuthFlag
), VarNameSize
, (UINT8
*) VariableName
);
1211 CopyMem (VariableGuid
, GetVendorGuidPtr (VariableHeader
, StoreInfo
.AuthFlag
), sizeof (EFI_GUID
));
1213 Status
= EFI_SUCCESS
;
1215 Status
= EFI_BUFFER_TOO_SMALL
;
1218 *VariableNameSize
= VarNameSize
;
1220 // Variable is found
1224 Variable
.CurrPtr
= GetNextVariablePtr (&StoreInfo
, Variable
.CurrPtr
, VariableHeader
);