3 The common variable operation routines shared by DXE_RINTIME variable
4 module and DXE_SMM variable module.
6 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 VARIABLE_MODULE_GLOBAL
*mVariableModuleGlobal
;
22 /// Define a memory cache that improves the search performance for a variable.
24 VARIABLE_STORE_HEADER
*mNvVariableCache
= NULL
;
27 /// The memory entry used for variable statistics data.
29 VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
33 Routine used to track statistical information about variable usage.
34 The data is stored in the EFI system table so it can be accessed later.
35 VariableInfo.efi can dump out the table. Only Boot Services variable
36 accesses are tracked by this code. The PcdVariableCollectStatistics
37 build flag controls if this feature is enabled.
39 A read that hits in the cache will have Read and Cache true for
40 the transaction. Data is allocated by this routine, but never
43 @param[in] VariableName Name of the Variable to track.
44 @param[in] VendorGuid Guid of the Variable to track.
45 @param[in] Volatile TRUE if volatile FALSE if non-volatile.
46 @param[in] Read TRUE if GetVariable() was called.
47 @param[in] Write TRUE if SetVariable() was called.
48 @param[in] Delete TRUE if deleted via SetVariable().
49 @param[in] Cache TRUE for a cache hit.
54 IN CHAR16
*VariableName
,
55 IN EFI_GUID
*VendorGuid
,
63 VARIABLE_INFO_ENTRY
*Entry
;
65 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
68 // Don't collect statistics at runtime.
72 if (gVariableInfo
== NULL
) {
74 // On the first call allocate a entry and place a pointer to it in
75 // the EFI System Table.
77 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
78 ASSERT (gVariableInfo
!= NULL
);
80 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
81 gVariableInfo
->Name
= AllocatePool (StrSize (VariableName
));
82 ASSERT (gVariableInfo
->Name
!= NULL
);
83 StrCpy (gVariableInfo
->Name
, VariableName
);
84 gVariableInfo
->Volatile
= Volatile
;
88 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
89 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
90 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
108 if (Entry
->Next
== NULL
) {
110 // If the entry is not in the table add it.
111 // Next iteration of the loop will fill in the data.
113 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
114 ASSERT (Entry
->Next
!= NULL
);
116 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
117 Entry
->Next
->Name
= AllocatePool (StrSize (VariableName
));
118 ASSERT (Entry
->Next
->Name
!= NULL
);
119 StrCpy (Entry
->Next
->Name
, VariableName
);
120 Entry
->Next
->Volatile
= Volatile
;
130 This code checks if variable header is valid or not.
132 @param Variable Pointer to the Variable Header.
134 @retval TRUE Variable header is valid.
135 @retval FALSE Variable header is not valid.
139 IsValidVariableHeader (
140 IN VARIABLE_HEADER
*Variable
143 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
153 This function writes data to the FWH at the correct LBA even if the LBAs
156 @param Global Pointer to VARAIBLE_GLOBAL structure.
157 @param Volatile Point out the Variable is Volatile or Non-Volatile.
158 @param SetByIndex TRUE if target pointer is given as index.
159 FALSE if target pointer is absolute.
160 @param Fvb Pointer to the writable FVB protocol.
161 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
163 @param DataSize Size of data to be written.
164 @param Buffer Pointer to the buffer from which data is written.
166 @retval EFI_INVALID_PARAMETER Parameters not valid.
167 @retval EFI_SUCCESS Variable store successfully updated.
171 UpdateVariableStore (
172 IN VARIABLE_GLOBAL
*Global
,
174 IN BOOLEAN SetByIndex
,
175 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
176 IN UINTN DataPtrIndex
,
181 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
189 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
190 VARIABLE_STORE_HEADER
*VolatileBase
;
191 EFI_PHYSICAL_ADDRESS FvVolHdr
;
192 EFI_PHYSICAL_ADDRESS DataPtr
;
196 DataPtr
= DataPtrIndex
;
199 // Check if the Data is Volatile.
202 ASSERT (Fvb
!= NULL
);
203 Status
= Fvb
->GetPhysicalAddress(Fvb
, &FvVolHdr
);
204 ASSERT_EFI_ERROR (Status
);
206 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
208 // Data Pointer should point to the actual Address where data is to be
212 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
215 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
216 return EFI_INVALID_PARAMETER
;
220 // Data Pointer should point to the actual Address where data is to be
223 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
225 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
228 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
229 return EFI_INVALID_PARAMETER
;
233 // If Volatile Variable just do a simple mem copy.
235 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
240 // If we are here we are dealing with Non-Volatile Variables.
242 LinearOffset
= (UINTN
) FwVolHeader
;
243 CurrWritePtr
= (UINTN
) DataPtr
;
244 CurrWriteSize
= DataSize
;
248 if (CurrWritePtr
< LinearOffset
) {
249 return EFI_INVALID_PARAMETER
;
252 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
253 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
255 // Check to see if the Variable Writes are spanning through multiple
258 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
259 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
260 Status
= Fvb
->Write (
263 (UINTN
) (CurrWritePtr
- LinearOffset
),
269 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
270 Status
= Fvb
->Write (
273 (UINTN
) (CurrWritePtr
- LinearOffset
),
277 if (EFI_ERROR (Status
)) {
281 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
282 CurrBuffer
= CurrBuffer
+ Size
;
283 CurrWriteSize
= CurrWriteSize
- Size
;
287 LinearOffset
+= PtrBlockMapEntry
->Length
;
298 This code gets the current status of Variable Store.
300 @param VarStoreHeader Pointer to the Variable Store Header.
302 @retval EfiRaw Variable store status is raw.
303 @retval EfiValid Variable store status is valid.
304 @retval EfiInvalid Variable store status is invalid.
307 VARIABLE_STORE_STATUS
308 GetVariableStoreStatus (
309 IN VARIABLE_STORE_HEADER
*VarStoreHeader
312 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiVariableGuid
) &&
313 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
314 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
318 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
319 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
320 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
321 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
322 VarStoreHeader
->Size
== 0xffffffff &&
323 VarStoreHeader
->Format
== 0xff &&
324 VarStoreHeader
->State
== 0xff
336 This code gets the size of name of variable.
338 @param Variable Pointer to the Variable Header.
340 @return UINTN Size of variable in bytes.
345 IN VARIABLE_HEADER
*Variable
348 if (Variable
->State
== (UINT8
) (-1) ||
349 Variable
->DataSize
== (UINT32
) (-1) ||
350 Variable
->NameSize
== (UINT32
) (-1) ||
351 Variable
->Attributes
== (UINT32
) (-1)) {
354 return (UINTN
) Variable
->NameSize
;
359 This code gets the size of variable data.
361 @param Variable Pointer to the Variable Header.
363 @return Size of variable in bytes.
368 IN VARIABLE_HEADER
*Variable
371 if (Variable
->State
== (UINT8
) (-1) ||
372 Variable
->DataSize
== (UINT32
) (-1) ||
373 Variable
->NameSize
== (UINT32
) (-1) ||
374 Variable
->Attributes
== (UINT32
) (-1)) {
377 return (UINTN
) Variable
->DataSize
;
382 This code gets the pointer to the variable name.
384 @param Variable Pointer to the Variable Header.
386 @return Pointer to Variable Name which is Unicode encoding.
391 IN VARIABLE_HEADER
*Variable
395 return (CHAR16
*) (Variable
+ 1);
400 This code gets the pointer to the variable data.
402 @param Variable Pointer to the Variable Header.
404 @return Pointer to Variable Data.
409 IN VARIABLE_HEADER
*Variable
415 // Be careful about pad size for alignment.
417 Value
= (UINTN
) GetVariableNamePtr (Variable
);
418 Value
+= NameSizeOfVariable (Variable
);
419 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
421 return (UINT8
*) Value
;
427 This code gets the pointer to the next variable header.
429 @param Variable Pointer to the Variable Header.
431 @return Pointer to next variable header.
436 IN VARIABLE_HEADER
*Variable
441 if (!IsValidVariableHeader (Variable
)) {
445 Value
= (UINTN
) GetVariableDataPtr (Variable
);
446 Value
+= DataSizeOfVariable (Variable
);
447 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
450 // Be careful about pad size for alignment.
452 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
457 Gets the pointer to the first variable header in given variable store area.
459 @param VarStoreHeader Pointer to the Variable Store Header.
461 @return Pointer to the first variable header.
466 IN VARIABLE_STORE_HEADER
*VarStoreHeader
470 // The end of variable store.
472 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
477 Gets the pointer to the end of the variable storage area.
479 This function gets pointer to the end of the variable storage
480 area, according to the input variable store header.
482 @param VarStoreHeader Pointer to the Variable Store Header.
484 @return Pointer to the end of the variable storage area.
489 IN VARIABLE_STORE_HEADER
*VarStoreHeader
493 // The end of variable store
495 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
501 Variable store garbage collection and reclaim operation.
503 @param VariableBase Base address of variable store.
504 @param LastVariableOffset Offset of last variable.
505 @param IsVolatile The variable store is volatile or not;
506 if it is non-volatile, need FTW.
507 @param UpdatingVariable Pointer to updating variable.
509 @return EFI_OUT_OF_RESOURCES
516 IN EFI_PHYSICAL_ADDRESS VariableBase
,
517 OUT UINTN
*LastVariableOffset
,
518 IN BOOLEAN IsVolatile
,
519 IN VARIABLE_HEADER
*UpdatingVariable
522 VARIABLE_HEADER
*Variable
;
523 VARIABLE_HEADER
*AddedVariable
;
524 VARIABLE_HEADER
*NextVariable
;
525 VARIABLE_HEADER
*NextAddedVariable
;
526 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
528 UINTN MaximumBufferSize
;
530 UINTN VariableNameSize
;
531 UINTN UpdatingVariableNameSize
;
538 CHAR16
*VariableNamePtr
;
539 CHAR16
*UpdatingVariableNamePtr
;
541 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
543 // Recalculate the total size of Common/HwErr type variables in non-volatile area.
546 mVariableModuleGlobal
->CommonVariableTotalSize
= 0;
547 mVariableModuleGlobal
->HwErrVariableTotalSize
= 0;
551 // Start Pointers for the variable.
553 Variable
= GetStartPointer (VariableStoreHeader
);
554 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
556 while (IsValidVariableHeader (Variable
)) {
557 NextVariable
= GetNextVariablePtr (Variable
);
558 if (Variable
->State
== VAR_ADDED
||
559 Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
561 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
562 MaximumBufferSize
+= VariableSize
;
565 Variable
= NextVariable
;
569 // Reserve the 1 Bytes with Oxff to identify the
570 // end of the variable buffer.
572 MaximumBufferSize
+= 1;
573 ValidBuffer
= AllocatePool (MaximumBufferSize
);
574 if (ValidBuffer
== NULL
) {
575 return EFI_OUT_OF_RESOURCES
;
578 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
581 // Copy variable store header.
583 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
584 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
587 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
589 Variable
= GetStartPointer (VariableStoreHeader
);
590 while (IsValidVariableHeader (Variable
)) {
591 NextVariable
= GetNextVariablePtr (Variable
);
592 if (Variable
->State
== VAR_ADDED
) {
593 if (UpdatingVariable
!= NULL
) {
594 if (UpdatingVariable
== Variable
) {
595 Variable
= NextVariable
;
599 VariableNameSize
= NameSizeOfVariable(Variable
);
600 UpdatingVariableNameSize
= NameSizeOfVariable(UpdatingVariable
);
602 VariableNamePtr
= GetVariableNamePtr (Variable
);
603 UpdatingVariableNamePtr
= GetVariableNamePtr (UpdatingVariable
);
604 if (CompareGuid (&Variable
->VendorGuid
, &UpdatingVariable
->VendorGuid
) &&
605 VariableNameSize
== UpdatingVariableNameSize
&&
606 CompareMem (VariableNamePtr
, UpdatingVariableNamePtr
, VariableNameSize
) == 0 ) {
607 Variable
= NextVariable
;
611 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
612 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
613 CurrPtr
+= VariableSize
;
614 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
615 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
616 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
617 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
620 Variable
= NextVariable
;
624 // Reinstall the variable being updated if it is not NULL.
626 if (UpdatingVariable
!= NULL
) {
627 VariableSize
= (UINTN
)(GetNextVariablePtr (UpdatingVariable
)) - (UINTN
)UpdatingVariable
;
628 CopyMem (CurrPtr
, (UINT8
*) UpdatingVariable
, VariableSize
);
629 CurrPtr
+= VariableSize
;
630 if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
631 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
632 } else if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
633 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
638 // Reinstall all in delete transition variables.
640 Variable
= GetStartPointer (VariableStoreHeader
);
641 while (IsValidVariableHeader (Variable
)) {
642 NextVariable
= GetNextVariablePtr (Variable
);
643 if (Variable
!= UpdatingVariable
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
646 // Buffer has cached all ADDED variable.
647 // Per IN_DELETED variable, we have to guarantee that
648 // no ADDED one in previous buffer.
652 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
653 while (IsValidVariableHeader (AddedVariable
)) {
654 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
655 NameSize
= NameSizeOfVariable (AddedVariable
);
656 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
657 NameSize
== NameSizeOfVariable (Variable
)
659 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
660 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
661 if (CompareMem (Point0
, Point1
, NameSizeOfVariable (AddedVariable
)) == 0) {
666 AddedVariable
= NextAddedVariable
;
670 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
672 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
673 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
674 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
675 CurrPtr
+= VariableSize
;
676 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
677 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
678 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
679 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
684 Variable
= NextVariable
;
689 // If volatile variable store, just copy valid buffer.
691 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
692 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
));
693 Status
= EFI_SUCCESS
;
696 // If non-volatile variable store, perform FTW here.
698 Status
= FtwVariableSpace (
701 (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
)
703 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
705 if (!EFI_ERROR (Status
)) {
706 *LastVariableOffset
= (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
);
708 *LastVariableOffset
= 0;
711 FreePool (ValidBuffer
);
718 Finds variable in storage blocks of volatile and non-volatile storage areas.
720 This code finds variable in storage blocks of volatile and non-volatile storage areas.
721 If VariableName is an empty string, then we just return the first
722 qualified variable without comparing VariableName and VendorGuid.
723 Otherwise, VariableName and VendorGuid are compared.
725 @param VariableName Name of the variable to be found.
726 @param VendorGuid Vendor GUID to be found.
727 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
728 including the range searched and the target position.
729 @param Global Pointer to VARIABLE_GLOBAL structure, including
730 base of volatile variable storage area, base of
731 NV variable storage area, and a lock.
733 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
735 @retval EFI_SUCCESS Variable successfully found.
736 @retval EFI_NOT_FOUND Variable not found
741 IN CHAR16
*VariableName
,
742 IN EFI_GUID
*VendorGuid
,
743 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
744 IN VARIABLE_GLOBAL
*Global
747 VARIABLE_HEADER
*Variable
[2];
748 VARIABLE_HEADER
*InDeletedVariable
;
749 VARIABLE_STORE_HEADER
*VariableStoreHeader
[2];
750 UINTN InDeletedStorageIndex
;
755 // 0: Volatile, 1: Non-Volatile.
756 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
757 // make use of this mapping to implement search algorithm.
759 VariableStoreHeader
[0] = (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
760 VariableStoreHeader
[1] = mNvVariableCache
;
763 // Start Pointers for the variable.
764 // Actual Data Pointer where data can be written.
766 Variable
[0] = GetStartPointer (VariableStoreHeader
[0]);
767 Variable
[1] = GetStartPointer (VariableStoreHeader
[1]);
769 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
770 return EFI_INVALID_PARAMETER
;
774 // Find the variable by walk through volatile and then non-volatile variable store.
776 InDeletedVariable
= NULL
;
777 InDeletedStorageIndex
= 0;
778 for (Index
= 0; Index
< 2; Index
++) {
779 while ((Variable
[Index
] < GetEndPointer (VariableStoreHeader
[Index
])) && IsValidVariableHeader (Variable
[Index
])) {
780 if (Variable
[Index
]->State
== VAR_ADDED
||
781 Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
783 if (!AtRuntime () || ((Variable
[Index
]->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
784 if (VariableName
[0] == 0) {
785 if (Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
786 InDeletedVariable
= Variable
[Index
];
787 InDeletedStorageIndex
= Index
;
789 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
790 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
791 PtrTrack
->CurrPtr
= Variable
[Index
];
792 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
797 if (CompareGuid (VendorGuid
, &Variable
[Index
]->VendorGuid
)) {
798 Point
= (VOID
*) GetVariableNamePtr (Variable
[Index
]);
800 ASSERT (NameSizeOfVariable (Variable
[Index
]) != 0);
801 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (Variable
[Index
])) == 0) {
802 if (Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
803 InDeletedVariable
= Variable
[Index
];
804 InDeletedStorageIndex
= Index
;
806 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
807 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
808 PtrTrack
->CurrPtr
= Variable
[Index
];
809 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
819 Variable
[Index
] = GetNextVariablePtr (Variable
[Index
]);
821 if (InDeletedVariable
!= NULL
) {
822 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[InDeletedStorageIndex
]);
823 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[InDeletedStorageIndex
]);
824 PtrTrack
->CurrPtr
= InDeletedVariable
;
825 PtrTrack
->Volatile
= (BOOLEAN
)(InDeletedStorageIndex
== 0);
829 PtrTrack
->CurrPtr
= NULL
;
830 return EFI_NOT_FOUND
;
834 Get index from supported language codes according to language string.
836 This code is used to get corresponding index in supported language codes. It can handle
837 RFC4646 and ISO639 language tags.
838 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
839 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
842 SupportedLang = "engfraengfra"
844 Iso639Language = TRUE
845 The return value is "0".
847 SupportedLang = "en;fr;en-US;fr-FR"
849 Iso639Language = FALSE
850 The return value is "3".
852 @param SupportedLang Platform supported language codes.
853 @param Lang Configured language.
854 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
856 @retval The index of language in the language codes.
860 GetIndexFromSupportedLangCodes(
861 IN CHAR8
*SupportedLang
,
863 IN BOOLEAN Iso639Language
868 UINTN LanguageLength
;
870 if (Iso639Language
) {
871 CompareLength
= ISO_639_2_ENTRY_SIZE
;
872 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
873 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
875 // Successfully find the index of Lang string in SupportedLang string.
877 Index
= Index
/ CompareLength
;
885 // Compare RFC4646 language code
888 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
890 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
892 // Skip ';' characters in SupportedLang
894 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
896 // Determine the length of the next language code in SupportedLang
898 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
900 if ((CompareLength
== LanguageLength
) &&
901 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
903 // Successfully find the index of Lang string in SupportedLang string.
914 Get language string from supported language codes according to index.
916 This code is used to get corresponding language strings in supported language codes. It can handle
917 RFC4646 and ISO639 language tags.
918 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
919 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
922 SupportedLang = "engfraengfra"
924 Iso639Language = TRUE
925 The return value is "fra".
927 SupportedLang = "en;fr;en-US;fr-FR"
929 Iso639Language = FALSE
930 The return value is "fr".
932 @param SupportedLang Platform supported language codes.
933 @param Index The index in supported language codes.
934 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
936 @retval The language string in the language codes.
940 GetLangFromSupportedLangCodes (
941 IN CHAR8
*SupportedLang
,
943 IN BOOLEAN Iso639Language
951 Supported
= SupportedLang
;
952 if (Iso639Language
) {
954 // According to the index of Lang string in SupportedLang string to get the language.
955 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
956 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
958 CompareLength
= ISO_639_2_ENTRY_SIZE
;
959 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
960 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
965 // Take semicolon as delimitation, sequentially traverse supported language codes.
967 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
970 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
972 // Have completed the traverse, but not find corrsponding string.
973 // This case is not allowed to happen.
978 if (SubIndex
== Index
) {
980 // According to the index of Lang string in SupportedLang string to get the language.
981 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
982 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
984 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
985 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
990 // Skip ';' characters in Supported
992 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
998 Returns a pointer to an allocated buffer that contains the best matching language
999 from a set of supported languages.
1001 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1002 code types may not be mixed in a single call to this function. This function
1003 supports a variable argument list that allows the caller to pass in a prioritized
1004 list of language codes to test against all the language codes in SupportedLanguages.
1006 If SupportedLanguages is NULL, then ASSERT().
1008 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1009 contains a set of language codes in the format
1010 specified by Iso639Language.
1011 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1012 in ISO 639-2 format. If FALSE, then all language
1013 codes are assumed to be in RFC 4646 language format
1014 @param[in] ... A variable argument list that contains pointers to
1015 Null-terminated ASCII strings that contain one or more
1016 language codes in the format specified by Iso639Language.
1017 The first language code from each of these language
1018 code lists is used to determine if it is an exact or
1019 close match to any of the language codes in
1020 SupportedLanguages. Close matches only apply to RFC 4646
1021 language codes, and the matching algorithm from RFC 4647
1022 is used to determine if a close match is present. If
1023 an exact or close match is found, then the matching
1024 language code from SupportedLanguages is returned. If
1025 no matches are found, then the next variable argument
1026 parameter is evaluated. The variable argument list
1027 is terminated by a NULL.
1029 @retval NULL The best matching language could not be found in SupportedLanguages.
1030 @retval NULL There are not enough resources available to return the best matching
1032 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1033 language in SupportedLanguages.
1038 VariableGetBestLanguage (
1039 IN CONST CHAR8
*SupportedLanguages
,
1040 IN BOOLEAN Iso639Language
,
1046 UINTN CompareLength
;
1047 UINTN LanguageLength
;
1048 CONST CHAR8
*Supported
;
1051 ASSERT (SupportedLanguages
!= NULL
);
1053 VA_START (Args
, Iso639Language
);
1054 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1056 // Default to ISO 639-2 mode
1059 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1062 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1064 if (!Iso639Language
) {
1065 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1069 // Trim back the length of Language used until it is empty
1071 while (LanguageLength
> 0) {
1073 // Loop through all language codes in SupportedLanguages
1075 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1077 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1079 if (!Iso639Language
) {
1081 // Skip ';' characters in Supported
1083 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1085 // Determine the length of the next language code in Supported
1087 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1089 // If Language is longer than the Supported, then skip to the next language
1091 if (LanguageLength
> CompareLength
) {
1096 // See if the first LanguageLength characters in Supported match Language
1098 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1101 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1102 Buffer
[CompareLength
] = '\0';
1103 return CopyMem (Buffer
, Supported
, CompareLength
);
1107 if (Iso639Language
) {
1109 // If ISO 639 mode, then each language can only be tested once
1114 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1116 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1123 // No matches were found
1129 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1131 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1133 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1134 and are read-only. Therefore, in variable driver, only store the original value for other use.
1136 @param[in] VariableName Name of variable.
1138 @param[in] Data Variable data.
1140 @param[in] DataSize Size of data. 0 means delete.
1144 AutoUpdateLangVariable(
1145 IN CHAR16
*VariableName
,
1151 CHAR8
*BestPlatformLang
;
1155 VARIABLE_POINTER_TRACK Variable
;
1156 BOOLEAN SetLanguageCodes
;
1159 // Don't do updates for delete operation
1161 if (DataSize
== 0) {
1165 SetLanguageCodes
= FALSE
;
1167 if (StrCmp (VariableName
, L
"PlatformLangCodes") == 0) {
1169 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1175 SetLanguageCodes
= TRUE
;
1178 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1179 // Therefore, in variable driver, only store the original value for other use.
1181 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1182 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1184 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1185 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1188 // PlatformLang holds a single language from PlatformLangCodes,
1189 // so the size of PlatformLangCodes is enough for the PlatformLang.
1191 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1192 FreePool (mVariableModuleGlobal
->PlatformLang
);
1194 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1195 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1197 } else if (StrCmp (VariableName
, L
"LangCodes") == 0) {
1199 // LangCodes is a volatile variable, so it can not be updated at runtime.
1205 SetLanguageCodes
= TRUE
;
1208 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1209 // Therefore, in variable driver, only store the original value for other use.
1211 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1212 FreePool (mVariableModuleGlobal
->LangCodes
);
1214 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1215 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1218 if (SetLanguageCodes
1219 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1220 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1222 // Update Lang if PlatformLang is already set
1223 // Update PlatformLang if Lang is already set
1225 Status
= FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*) mVariableModuleGlobal
);
1226 if (!EFI_ERROR (Status
)) {
1230 VariableName
= L
"PlatformLang";
1231 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1232 DataSize
= Variable
.CurrPtr
->DataSize
;
1234 Status
= FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*) mVariableModuleGlobal
);
1235 if (!EFI_ERROR (Status
)) {
1237 // Update PlatformLang
1239 VariableName
= L
"Lang";
1240 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1241 DataSize
= Variable
.CurrPtr
->DataSize
;
1244 // Neither PlatformLang nor Lang is set, directly return
1252 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1254 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1256 if (StrCmp (VariableName
, L
"PlatformLang") == 0) {
1258 // Update Lang when PlatformLangCodes/LangCodes were set.
1260 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1262 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1264 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1265 if (BestPlatformLang
!= NULL
) {
1267 // Get the corresponding index in language codes.
1269 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1272 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1274 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1277 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1279 FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*)mVariableModuleGlobal
);
1281 Status
= UpdateVariable (L
"Lang", &gEfiGlobalVariableGuid
, BestLang
,
1282 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, &Variable
);
1284 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
1286 ASSERT_EFI_ERROR(Status
);
1290 } else if (StrCmp (VariableName
, L
"Lang") == 0) {
1292 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1294 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1296 // When setting Lang, firstly get most matched language string from supported language codes.
1298 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1299 if (BestLang
!= NULL
) {
1301 // Get the corresponding index in language codes.
1303 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1306 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1308 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1311 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1313 FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*)mVariableModuleGlobal
);
1315 Status
= UpdateVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, BestPlatformLang
,
1316 AsciiStrSize (BestPlatformLang
), Attributes
, &Variable
);
1318 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
1319 ASSERT_EFI_ERROR (Status
);
1326 Update the variable region with Variable information. These are the same
1327 arguments as the EFI Variable services.
1329 @param[in] VariableName Name of variable.
1330 @param[in] VendorGuid Guid of variable.
1331 @param[in] Data Variable data.
1332 @param[in] DataSize Size of data. 0 means delete.
1333 @param[in] Attributes Attribues of the variable.
1334 @param[in] CacheVariable The variable information which is used to keep track of variable usage.
1336 @retval EFI_SUCCESS The update operation is success.
1337 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1342 IN CHAR16
*VariableName
,
1343 IN EFI_GUID
*VendorGuid
,
1346 IN UINT32 Attributes OPTIONAL
,
1347 IN VARIABLE_POINTER_TRACK
*CacheVariable
1351 VARIABLE_HEADER
*NextVariable
;
1353 UINTN NonVolatileVarableStoreSize
;
1354 UINTN VarNameOffset
;
1355 UINTN VarDataOffset
;
1359 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1362 VARIABLE_POINTER_TRACK
*Variable
;
1363 VARIABLE_POINTER_TRACK NvVariable
;
1364 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1367 if ((mVariableModuleGlobal
->FvbInstance
== NULL
) && ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0)) {
1369 // The FVB protocol is not ready. Trying to update NV variable prior to the installation
1370 // of EFI_VARIABLE_WRITE_ARCH_PROTOCOL.
1372 return EFI_NOT_AVAILABLE_YET
;
1375 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1376 Variable
= CacheVariable
;
1379 // Update/Delete existing NV variable.
1380 // CacheVariable points to the variable in the memory copy of Flash area
1381 // Now let Variable points to the same variable in Flash area.
1383 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1384 Variable
= &NvVariable
;
1385 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1386 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1387 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1388 Variable
->Volatile
= FALSE
;
1391 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1394 if (Variable
->CurrPtr
!= NULL
) {
1396 // Update/Delete existing variable.
1400 // If AtRuntime and the variable is Volatile and Runtime Access,
1401 // the volatile is ReadOnly, and SetVariable should be aborted and
1402 // return EFI_WRITE_PROTECTED.
1404 if (Variable
->Volatile
) {
1405 Status
= EFI_WRITE_PROTECTED
;
1409 // Only variable that have NV attributes can be updated/deleted in Runtime.
1411 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1412 Status
= EFI_INVALID_PARAMETER
;
1418 // Setting a data variable with no access, or zero DataSize attributes
1419 // causes it to be deleted.
1421 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1422 State
= Variable
->CurrPtr
->State
;
1423 State
&= VAR_DELETED
;
1425 Status
= UpdateVariableStore (
1426 &mVariableModuleGlobal
->VariableGlobal
,
1430 (UINTN
) &Variable
->CurrPtr
->State
,
1434 if (!EFI_ERROR (Status
)) {
1435 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1436 if (!Variable
->Volatile
) {
1437 CacheVariable
->CurrPtr
->State
= State
;
1443 // If the variable is marked valid, and the same data has been passed in,
1444 // then return to the caller immediately.
1446 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
1447 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0)) {
1449 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1450 Status
= EFI_SUCCESS
;
1452 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
1453 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1456 // Mark the old variable as in delete transition.
1458 State
= Variable
->CurrPtr
->State
;
1459 State
&= VAR_IN_DELETED_TRANSITION
;
1461 Status
= UpdateVariableStore (
1462 &mVariableModuleGlobal
->VariableGlobal
,
1466 (UINTN
) &Variable
->CurrPtr
->State
,
1470 if (EFI_ERROR (Status
)) {
1473 if (!Variable
->Volatile
) {
1474 CacheVariable
->CurrPtr
->State
= State
;
1479 // Not found existing variable. Create a new variable.
1483 // Make sure we are trying to create a new variable.
1484 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1486 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1487 Status
= EFI_NOT_FOUND
;
1492 // Only variable have NV|RT attribute can be created in Runtime.
1495 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1496 Status
= EFI_INVALID_PARAMETER
;
1502 // Function part - create a new variable and copy the data.
1503 // Both update a variable and create a variable will come here.
1506 // Tricky part: Use scratch data area at the end of volatile variable store
1507 // as a temporary storage.
1509 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1510 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1512 SetMem (NextVariable
, ScratchSize
, 0xff);
1514 NextVariable
->StartId
= VARIABLE_DATA
;
1515 NextVariable
->Attributes
= Attributes
;
1517 // NextVariable->State = VAR_ADDED;
1519 NextVariable
->Reserved
= 0;
1520 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1521 VarNameSize
= StrSize (VariableName
);
1523 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1527 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1529 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1533 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1535 // There will be pad bytes after Data, the NextVariable->NameSize and
1536 // NextVariable->DataSize should not include pad size so that variable
1537 // service can get actual size in GetVariable.
1539 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1540 NextVariable
->DataSize
= (UINT32
)DataSize
;
1543 // The actual size of the variable that stores in storage should
1544 // include pad size.
1546 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1547 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1549 // Create a nonvolatile variable.
1552 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1553 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1554 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1555 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1556 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1558 Status
= EFI_OUT_OF_RESOURCES
;
1562 // Perform garbage collection & reclaim operation.
1564 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1565 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
, FALSE
, Variable
->CurrPtr
);
1566 if (EFI_ERROR (Status
)) {
1570 // If still no enough space, return out of resources.
1572 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1573 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1574 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1575 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1576 Status
= EFI_OUT_OF_RESOURCES
;
1583 // 1. Write variable header
1584 // 2. Set variable state to header valid
1585 // 3. Write variable data
1586 // 4. Set variable state to valid
1591 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1592 Status
= UpdateVariableStore (
1593 &mVariableModuleGlobal
->VariableGlobal
,
1597 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1598 sizeof (VARIABLE_HEADER
),
1599 (UINT8
*) NextVariable
1602 if (EFI_ERROR (Status
)) {
1609 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1610 Status
= UpdateVariableStore (
1611 &mVariableModuleGlobal
->VariableGlobal
,
1615 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1617 &NextVariable
->State
1620 if (EFI_ERROR (Status
)) {
1626 Status
= UpdateVariableStore (
1627 &mVariableModuleGlobal
->VariableGlobal
,
1631 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
1632 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1633 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1636 if (EFI_ERROR (Status
)) {
1642 NextVariable
->State
= VAR_ADDED
;
1643 Status
= UpdateVariableStore (
1644 &mVariableModuleGlobal
->VariableGlobal
,
1648 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1650 &NextVariable
->State
1653 if (EFI_ERROR (Status
)) {
1657 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1659 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
1660 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1662 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1665 // update the memory copy of Flash region.
1667 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
1670 // Create a volatile variable.
1674 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1675 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1677 // Perform garbage collection & reclaim operation.
1679 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
1680 &mVariableModuleGlobal
->VolatileLastVariableOffset
, TRUE
, Variable
->CurrPtr
);
1681 if (EFI_ERROR (Status
)) {
1685 // If still no enough space, return out of resources.
1687 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1688 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1690 Status
= EFI_OUT_OF_RESOURCES
;
1696 NextVariable
->State
= VAR_ADDED
;
1697 Status
= UpdateVariableStore (
1698 &mVariableModuleGlobal
->VariableGlobal
,
1702 mVariableModuleGlobal
->VolatileLastVariableOffset
,
1704 (UINT8
*) NextVariable
1707 if (EFI_ERROR (Status
)) {
1711 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1715 // Mark the old variable as deleted.
1717 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
1718 State
= Variable
->CurrPtr
->State
;
1719 State
&= VAR_DELETED
;
1721 Status
= UpdateVariableStore (
1722 &mVariableModuleGlobal
->VariableGlobal
,
1726 (UINTN
) &Variable
->CurrPtr
->State
,
1730 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
1731 CacheVariable
->CurrPtr
->State
= State
;
1735 if (!EFI_ERROR (Status
)) {
1736 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1745 This code finds variable in storage blocks (Volatile or Non-Volatile).
1747 @param VariableName Name of Variable to be found.
1748 @param VendorGuid Variable vendor GUID.
1749 @param Attributes Attribute value of the variable found.
1750 @param DataSize Size of Data found. If size is less than the
1751 data, this value contains the required size.
1752 @param Data Data pointer.
1754 @return EFI_INVALID_PARAMETER Invalid parameter.
1755 @return EFI_SUCCESS Find the specified variable.
1756 @return EFI_NOT_FOUND Not found.
1757 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1762 VariableServiceGetVariable (
1763 IN CHAR16
*VariableName
,
1764 IN EFI_GUID
*VendorGuid
,
1765 OUT UINT32
*Attributes OPTIONAL
,
1766 IN OUT UINTN
*DataSize
,
1771 VARIABLE_POINTER_TRACK Variable
;
1774 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
1775 return EFI_INVALID_PARAMETER
;
1778 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1780 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1781 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1788 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
1789 ASSERT (VarDataSize
!= 0);
1791 if (*DataSize
>= VarDataSize
) {
1793 Status
= EFI_INVALID_PARAMETER
;
1797 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
1798 if (Attributes
!= NULL
) {
1799 *Attributes
= Variable
.CurrPtr
->Attributes
;
1802 *DataSize
= VarDataSize
;
1803 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
1805 Status
= EFI_SUCCESS
;
1808 *DataSize
= VarDataSize
;
1809 Status
= EFI_BUFFER_TOO_SMALL
;
1814 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1822 This code Finds the Next available variable.
1824 @param VariableNameSize Size of the variable name.
1825 @param VariableName Pointer to variable name.
1826 @param VendorGuid Variable Vendor Guid.
1828 @return EFI_INVALID_PARAMETER Invalid parameter.
1829 @return EFI_SUCCESS Find the specified variable.
1830 @return EFI_NOT_FOUND Not found.
1831 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1836 VariableServiceGetNextVariableName (
1837 IN OUT UINTN
*VariableNameSize
,
1838 IN OUT CHAR16
*VariableName
,
1839 IN OUT EFI_GUID
*VendorGuid
1842 VARIABLE_POINTER_TRACK Variable
;
1846 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
1847 return EFI_INVALID_PARAMETER
;
1850 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1852 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1853 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1857 if (VariableName
[0] != 0) {
1859 // If variable name is not NULL, get next variable.
1861 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1866 // If both volatile and non-volatile variable store are parsed,
1867 // return not found.
1869 if (Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
) {
1870 Variable
.Volatile
= (BOOLEAN
) (Variable
.Volatile
^ ((BOOLEAN
) 0x1));
1871 if (!Variable
.Volatile
) {
1872 Variable
.StartPtr
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1873 Variable
.EndPtr
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
));
1875 Status
= EFI_NOT_FOUND
;
1879 Variable
.CurrPtr
= Variable
.StartPtr
;
1880 if (!IsValidVariableHeader (Variable
.CurrPtr
)) {
1885 // Variable is found
1887 if (IsValidVariableHeader (Variable
.CurrPtr
) && Variable
.CurrPtr
->State
== VAR_ADDED
) {
1888 if ((AtRuntime () && ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) == 0) {
1889 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
1890 ASSERT (VarNameSize
!= 0);
1892 if (VarNameSize
<= *VariableNameSize
) {
1895 GetVariableNamePtr (Variable
.CurrPtr
),
1900 &Variable
.CurrPtr
->VendorGuid
,
1903 Status
= EFI_SUCCESS
;
1905 Status
= EFI_BUFFER_TOO_SMALL
;
1908 *VariableNameSize
= VarNameSize
;
1913 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1917 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1923 This code sets variable in storage blocks (Volatile or Non-Volatile).
1925 @param VariableName Name of Variable to be found.
1926 @param VendorGuid Variable vendor GUID.
1927 @param Attributes Attribute value of the variable found
1928 @param DataSize Size of Data found. If size is less than the
1929 data, this value contains the required size.
1930 @param Data Data pointer.
1932 @return EFI_INVALID_PARAMETER Invalid parameter.
1933 @return EFI_SUCCESS Set successfully.
1934 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
1935 @return EFI_NOT_FOUND Not found.
1936 @return EFI_WRITE_PROTECTED Variable is read-only.
1941 VariableServiceSetVariable (
1942 IN CHAR16
*VariableName
,
1943 IN EFI_GUID
*VendorGuid
,
1944 IN UINT32 Attributes
,
1949 VARIABLE_POINTER_TRACK Variable
;
1951 VARIABLE_HEADER
*NextVariable
;
1952 EFI_PHYSICAL_ADDRESS Point
;
1955 // Check input parameters.
1957 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
1958 return EFI_INVALID_PARAMETER
;
1961 if (DataSize
!= 0 && Data
== NULL
) {
1962 return EFI_INVALID_PARAMETER
;
1966 // Not support authenticated variable write yet.
1968 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1969 return EFI_INVALID_PARAMETER
;
1973 // Make sure if runtime bit is set, boot service bit is set also.
1975 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1976 return EFI_INVALID_PARAMETER
;
1980 // The size of the VariableName, including the Unicode Null in bytes plus
1981 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
1982 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
1984 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1985 if ((DataSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
)) ||
1986 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
))) {
1987 return EFI_INVALID_PARAMETER
;
1990 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX".
1992 if (StrnCmp(VariableName
, L
"HwErrRec", StrLen(L
"HwErrRec")) != 0) {
1993 return EFI_INVALID_PARAMETER
;
1997 // The size of the VariableName, including the Unicode Null in bytes plus
1998 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2000 if ((DataSize
> PcdGet32 (PcdMaxVariableSize
)) ||
2001 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxVariableSize
))) {
2002 return EFI_INVALID_PARAMETER
;
2006 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2009 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2011 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2012 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2014 // Parse non-volatile variable data and get last variable offset.
2016 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2017 while ((NextVariable
< GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))
2018 && IsValidVariableHeader (NextVariable
)) {
2019 NextVariable
= GetNextVariablePtr (NextVariable
);
2021 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2025 // Check whether the input variable is already existed.
2027 FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
2030 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2032 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2034 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, &Variable
);
2036 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2037 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2044 This code returns information about the EFI variables.
2046 @param Attributes Attributes bitmask to specify the type of variables
2047 on which to return information.
2048 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2049 for the EFI variables associated with the attributes specified.
2050 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2051 for EFI variables associated with the attributes specified.
2052 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2053 associated with the attributes specified.
2055 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2056 @return EFI_SUCCESS Query successfully.
2057 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2062 VariableServiceQueryVariableInfo (
2063 IN UINT32 Attributes
,
2064 OUT UINT64
*MaximumVariableStorageSize
,
2065 OUT UINT64
*RemainingVariableStorageSize
,
2066 OUT UINT64
*MaximumVariableSize
2069 VARIABLE_HEADER
*Variable
;
2070 VARIABLE_HEADER
*NextVariable
;
2071 UINT64 VariableSize
;
2072 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2073 UINT64 CommonVariableTotalSize
;
2074 UINT64 HwErrVariableTotalSize
;
2076 CommonVariableTotalSize
= 0;
2077 HwErrVariableTotalSize
= 0;
2079 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2080 return EFI_INVALID_PARAMETER
;
2083 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2085 // Make sure the Attributes combination is supported by the platform.
2087 return EFI_UNSUPPORTED
;
2088 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2090 // Make sure if runtime bit is set, boot service bit is set also.
2092 return EFI_INVALID_PARAMETER
;
2093 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2095 // Make sure RT Attribute is set if we are in Runtime phase.
2097 return EFI_INVALID_PARAMETER
;
2098 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2100 // Make sure Hw Attribute is set with NV.
2102 return EFI_INVALID_PARAMETER
;
2103 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2105 // Not support authentiated variable write yet.
2107 return EFI_UNSUPPORTED
;
2110 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2112 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2114 // Query is Volatile related.
2116 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2119 // Query is Non-Volatile related.
2121 VariableStoreHeader
= mNvVariableCache
;
2125 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2126 // with the storage size (excluding the storage header size).
2128 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2131 // Harware error record variable needs larger size.
2133 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2134 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2135 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2137 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2138 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
2139 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
2143 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2145 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2149 // Point to the starting address of the variables.
2151 Variable
= GetStartPointer (VariableStoreHeader
);
2154 // Now walk through the related variable store.
2156 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
2157 NextVariable
= GetNextVariablePtr (Variable
);
2158 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2162 // We don't take the state of the variables in mind
2163 // when calculating RemainingVariableStorageSize,
2164 // since the space occupied by variables not marked with
2165 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2167 if ((NextVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2168 HwErrVariableTotalSize
+= VariableSize
;
2170 CommonVariableTotalSize
+= VariableSize
;
2174 // Only care about Variables with State VAR_ADDED, because
2175 // the space not marked as VAR_ADDED is reclaimable now.
2177 if (Variable
->State
== VAR_ADDED
) {
2178 if ((NextVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2179 HwErrVariableTotalSize
+= VariableSize
;
2181 CommonVariableTotalSize
+= VariableSize
;
2187 // Go to the next one.
2189 Variable
= NextVariable
;
2192 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2193 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2195 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2198 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2199 *MaximumVariableSize
= 0;
2200 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2201 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2204 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2210 This function reclaims variable storage if free size is below the threshold.
2219 UINTN CommonVariableSpace
;
2220 UINTN RemainingCommonVariableSpace
;
2221 UINTN RemainingHwErrVariableSpace
;
2223 Status
= EFI_SUCCESS
;
2225 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
2227 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2229 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2231 // Check if the free area is blow a threshold.
2233 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2234 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2235 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2237 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2238 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2242 ASSERT_EFI_ERROR (Status
);
2248 Initializes variable write service after FVB was ready.
2250 @retval EFI_SUCCESS Function successfully executed.
2251 @retval Others Fail to initialize the variable service.
2255 VariableWriteServiceInitialize (
2260 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2263 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2264 UINT64 VariableStoreLength
;
2266 VariableStoreBase
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2267 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2268 VariableStoreLength
= VariableStoreHeader
->Size
;
2271 // Check if the free area is really free.
2273 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreLength
; Index
++) {
2274 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
2277 // There must be something wrong in variable store, do reclaim operation.
2280 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2281 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2285 if (EFI_ERROR (Status
)) {
2297 Initializes variable store area for non-volatile and volatile variable.
2299 @retval EFI_SUCCESS Function successfully executed.
2300 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2304 VariableCommonInitialize (
2309 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
2310 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2311 VARIABLE_HEADER
*NextVariable
;
2312 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader
;
2313 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2314 UINT64 VariableStoreLength
;
2319 // Allocate runtime memory for variable driver global structure.
2321 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
2322 if (mVariableModuleGlobal
== NULL
) {
2323 return EFI_OUT_OF_RESOURCES
;
2326 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
2329 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2330 // is stored with common variable in the same NV region. So the platform integrator should
2331 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2332 // PcdFlashNvStorageVariableSize.
2334 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
2337 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
2339 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
2340 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
2341 if (VolatileVariableStore
== NULL
) {
2342 FreePool (mVariableModuleGlobal
);
2343 return EFI_OUT_OF_RESOURCES
;
2346 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
2349 // Initialize Variable Specific Data.
2351 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
2352 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
2353 mVariableModuleGlobal
->FvbInstance
= NULL
;
2355 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiVariableGuid
);
2356 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
2357 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
2358 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
2359 VolatileVariableStore
->Reserved
= 0;
2360 VolatileVariableStore
->Reserved1
= 0;
2363 // Get non-volatile variable store.
2366 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
2367 if (TempVariableStoreHeader
== 0) {
2368 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
2370 VariableStoreBase
= TempVariableStoreHeader
+ \
2371 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2372 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
2373 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2375 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
2376 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2377 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
2378 Status
= EFI_VOLUME_CORRUPTED
;
2379 DEBUG((EFI_D_INFO
, "Variable Store header is corrupted\n"));
2382 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
2385 // Parse non-volatile variable data and get last variable offset.
2387 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
2388 while (IsValidVariableHeader (NextVariable
)) {
2389 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
2390 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2391 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2393 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2396 NextVariable
= GetNextVariablePtr (NextVariable
);
2399 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
2402 // Allocate runtime memory used for a memory copy of the FLASH region.
2403 // Keep the memory and the FLASH in sync as updates occur
2405 mNvVariableCache
= AllocateRuntimeZeroPool ((UINTN
)VariableStoreLength
);
2406 if (mNvVariableCache
== NULL
) {
2407 Status
= EFI_OUT_OF_RESOURCES
;
2410 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableStoreBase
, (UINTN
)VariableStoreLength
);
2411 Status
= EFI_SUCCESS
;
2414 if (EFI_ERROR (Status
)) {
2415 FreePool (mVariableModuleGlobal
);
2416 FreePool (VolatileVariableStore
);
2424 Get the proper fvb handle and/or fvb protocol by the given Flash address.
2426 @param[in] Address The Flash address.
2427 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
2428 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
2432 GetFvbInfoByAddress (
2433 IN EFI_PHYSICAL_ADDRESS Address
,
2434 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
2435 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
2439 EFI_HANDLE
*HandleBuffer
;
2442 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
2443 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
2444 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
2445 EFI_FVB_ATTRIBUTES_2 Attributes
;
2448 // Get all FVB handles.
2450 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
2451 if (EFI_ERROR (Status
)) {
2452 return EFI_NOT_FOUND
;
2456 // Get the FVB to access variable store.
2459 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
2460 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
2461 if (EFI_ERROR (Status
)) {
2462 Status
= EFI_NOT_FOUND
;
2467 // Ensure this FVB protocol supported Write operation.
2469 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
2470 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
2475 // Compare the address and select the right one.
2477 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
2478 if (EFI_ERROR (Status
)) {
2482 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
2483 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
2484 if (FvbHandle
!= NULL
) {
2485 *FvbHandle
= HandleBuffer
[Index
];
2487 if (FvbProtocol
!= NULL
) {
2490 Status
= EFI_SUCCESS
;
2494 FreePool (HandleBuffer
);
2497 Status
= EFI_NOT_FOUND
;