3 The common variable operation routines shared by DXE_RINTIME variable
4 module and DXE_SMM variable module.
6 Copyright (c) 2006 - 2010, 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 Status
= Fvb
->GetPhysicalAddress(Fvb
, &FvVolHdr
);
203 ASSERT_EFI_ERROR (Status
);
205 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
207 // Data Pointer should point to the actual Address where data is to be
211 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
214 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
215 return EFI_INVALID_PARAMETER
;
219 // Data Pointer should point to the actual Address where data is to be
222 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
224 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
227 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
228 return EFI_INVALID_PARAMETER
;
232 // If Volatile Variable just do a simple mem copy.
234 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
239 // If we are here we are dealing with Non-Volatile Variables.
241 LinearOffset
= (UINTN
) FwVolHeader
;
242 CurrWritePtr
= (UINTN
) DataPtr
;
243 CurrWriteSize
= DataSize
;
247 if (CurrWritePtr
< LinearOffset
) {
248 return EFI_INVALID_PARAMETER
;
251 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
252 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
254 // Check to see if the Variable Writes are spanning through multiple
257 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
258 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
259 Status
= Fvb
->Write (
262 (UINTN
) (CurrWritePtr
- LinearOffset
),
268 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
269 Status
= Fvb
->Write (
272 (UINTN
) (CurrWritePtr
- LinearOffset
),
276 if (EFI_ERROR (Status
)) {
280 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
281 CurrBuffer
= CurrBuffer
+ Size
;
282 CurrWriteSize
= CurrWriteSize
- Size
;
286 LinearOffset
+= PtrBlockMapEntry
->Length
;
297 This code gets the current status of Variable Store.
299 @param VarStoreHeader Pointer to the Variable Store Header.
301 @retval EfiRaw Variable store status is raw.
302 @retval EfiValid Variable store status is valid.
303 @retval EfiInvalid Variable store status is invalid.
306 VARIABLE_STORE_STATUS
307 GetVariableStoreStatus (
308 IN VARIABLE_STORE_HEADER
*VarStoreHeader
311 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiVariableGuid
) &&
312 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
313 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
317 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
318 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
319 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
320 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
321 VarStoreHeader
->Size
== 0xffffffff &&
322 VarStoreHeader
->Format
== 0xff &&
323 VarStoreHeader
->State
== 0xff
335 This code gets the size of name of variable.
337 @param Variable Pointer to the Variable Header.
339 @return UINTN Size of variable in bytes.
344 IN VARIABLE_HEADER
*Variable
347 if (Variable
->State
== (UINT8
) (-1) ||
348 Variable
->DataSize
== (UINT32
) (-1) ||
349 Variable
->NameSize
== (UINT32
) (-1) ||
350 Variable
->Attributes
== (UINT32
) (-1)) {
353 return (UINTN
) Variable
->NameSize
;
358 This code gets the size of variable data.
360 @param Variable Pointer to the Variable Header.
362 @return Size of variable in bytes.
367 IN VARIABLE_HEADER
*Variable
370 if (Variable
->State
== (UINT8
) (-1) ||
371 Variable
->DataSize
== (UINT32
) (-1) ||
372 Variable
->NameSize
== (UINT32
) (-1) ||
373 Variable
->Attributes
== (UINT32
) (-1)) {
376 return (UINTN
) Variable
->DataSize
;
381 This code gets the pointer to the variable name.
383 @param Variable Pointer to the Variable Header.
385 @return Pointer to Variable Name which is Unicode encoding.
390 IN VARIABLE_HEADER
*Variable
394 return (CHAR16
*) (Variable
+ 1);
399 This code gets the pointer to the variable data.
401 @param Variable Pointer to the Variable Header.
403 @return Pointer to Variable Data.
408 IN VARIABLE_HEADER
*Variable
414 // Be careful about pad size for alignment.
416 Value
= (UINTN
) GetVariableNamePtr (Variable
);
417 Value
+= NameSizeOfVariable (Variable
);
418 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
420 return (UINT8
*) Value
;
426 This code gets the pointer to the next variable header.
428 @param Variable Pointer to the Variable Header.
430 @return Pointer to next variable header.
435 IN VARIABLE_HEADER
*Variable
440 if (!IsValidVariableHeader (Variable
)) {
444 Value
= (UINTN
) GetVariableDataPtr (Variable
);
445 Value
+= DataSizeOfVariable (Variable
);
446 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
449 // Be careful about pad size for alignment.
451 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
456 Gets the pointer to the first variable header in given variable store area.
458 @param VarStoreHeader Pointer to the Variable Store Header.
460 @return Pointer to the first variable header.
465 IN VARIABLE_STORE_HEADER
*VarStoreHeader
469 // The end of variable store.
471 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
476 Gets the pointer to the end of the variable storage area.
478 This function gets pointer to the end of the variable storage
479 area, according to the input variable store header.
481 @param VarStoreHeader Pointer to the Variable Store Header.
483 @return Pointer to the end of the variable storage area.
488 IN VARIABLE_STORE_HEADER
*VarStoreHeader
492 // The end of variable store
494 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
500 Variable store garbage collection and reclaim operation.
502 @param VariableBase Base address of variable store.
503 @param LastVariableOffset Offset of last variable.
504 @param IsVolatile The variable store is volatile or not;
505 if it is non-volatile, need FTW.
506 @param UpdatingVariable Pointer to updating variable.
508 @return EFI_OUT_OF_RESOURCES
515 IN EFI_PHYSICAL_ADDRESS VariableBase
,
516 OUT UINTN
*LastVariableOffset
,
517 IN BOOLEAN IsVolatile
,
518 IN VARIABLE_HEADER
*UpdatingVariable
521 VARIABLE_HEADER
*Variable
;
522 VARIABLE_HEADER
*AddedVariable
;
523 VARIABLE_HEADER
*NextVariable
;
524 VARIABLE_HEADER
*NextAddedVariable
;
525 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
527 UINTN MaximumBufferSize
;
529 UINTN VariableNameSize
;
530 UINTN UpdatingVariableNameSize
;
537 CHAR16
*VariableNamePtr
;
538 CHAR16
*UpdatingVariableNamePtr
;
540 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
542 // Recalculate the total size of Common/HwErr type variables in non-volatile area.
545 mVariableModuleGlobal
->CommonVariableTotalSize
= 0;
546 mVariableModuleGlobal
->HwErrVariableTotalSize
= 0;
550 // Start Pointers for the variable.
552 Variable
= GetStartPointer (VariableStoreHeader
);
553 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
555 while (IsValidVariableHeader (Variable
)) {
556 NextVariable
= GetNextVariablePtr (Variable
);
557 if (Variable
->State
== VAR_ADDED
||
558 Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
560 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
561 MaximumBufferSize
+= VariableSize
;
564 Variable
= NextVariable
;
568 // Reserve the 1 Bytes with Oxff to identify the
569 // end of the variable buffer.
571 MaximumBufferSize
+= 1;
572 ValidBuffer
= AllocatePool (MaximumBufferSize
);
573 if (ValidBuffer
== NULL
) {
574 return EFI_OUT_OF_RESOURCES
;
577 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
580 // Copy variable store header.
582 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
583 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
586 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
588 Variable
= GetStartPointer (VariableStoreHeader
);
589 while (IsValidVariableHeader (Variable
)) {
590 NextVariable
= GetNextVariablePtr (Variable
);
591 if (Variable
->State
== VAR_ADDED
) {
592 if (UpdatingVariable
!= NULL
) {
593 if (UpdatingVariable
== Variable
) {
594 Variable
= NextVariable
;
598 VariableNameSize
= NameSizeOfVariable(Variable
);
599 UpdatingVariableNameSize
= NameSizeOfVariable(UpdatingVariable
);
601 VariableNamePtr
= GetVariableNamePtr (Variable
);
602 UpdatingVariableNamePtr
= GetVariableNamePtr (UpdatingVariable
);
603 if (CompareGuid (&Variable
->VendorGuid
, &UpdatingVariable
->VendorGuid
) &&
604 VariableNameSize
== UpdatingVariableNameSize
&&
605 CompareMem (VariableNamePtr
, UpdatingVariableNamePtr
, VariableNameSize
) == 0 ) {
606 Variable
= NextVariable
;
610 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
611 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
612 CurrPtr
+= VariableSize
;
613 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
614 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
615 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
616 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
619 Variable
= NextVariable
;
623 // Reinstall the variable being updated if it is not NULL.
625 if (UpdatingVariable
!= NULL
) {
626 VariableSize
= (UINTN
)(GetNextVariablePtr (UpdatingVariable
)) - (UINTN
)UpdatingVariable
;
627 CopyMem (CurrPtr
, (UINT8
*) UpdatingVariable
, VariableSize
);
628 CurrPtr
+= VariableSize
;
629 if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
630 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
631 } else if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
632 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
637 // Reinstall all in delete transition variables.
639 Variable
= GetStartPointer (VariableStoreHeader
);
640 while (IsValidVariableHeader (Variable
)) {
641 NextVariable
= GetNextVariablePtr (Variable
);
642 if (Variable
!= UpdatingVariable
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
645 // Buffer has cached all ADDED variable.
646 // Per IN_DELETED variable, we have to guarantee that
647 // no ADDED one in previous buffer.
651 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
652 while (IsValidVariableHeader (AddedVariable
)) {
653 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
654 NameSize
= NameSizeOfVariable (AddedVariable
);
655 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
656 NameSize
== NameSizeOfVariable (Variable
)
658 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
659 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
660 if (CompareMem (Point0
, Point1
, NameSizeOfVariable (AddedVariable
)) == 0) {
665 AddedVariable
= NextAddedVariable
;
669 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
671 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
672 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
673 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
674 CurrPtr
+= VariableSize
;
675 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
676 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
677 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
678 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
683 Variable
= NextVariable
;
688 // If volatile variable store, just copy valid buffer.
690 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
691 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
));
692 Status
= EFI_SUCCESS
;
695 // If non-volatile variable store, perform FTW here.
697 Status
= FtwVariableSpace (
700 (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
)
702 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
704 if (!EFI_ERROR (Status
)) {
705 *LastVariableOffset
= (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
);
707 *LastVariableOffset
= 0;
710 FreePool (ValidBuffer
);
717 Finds variable in storage blocks of volatile and non-volatile storage areas.
719 This code finds variable in storage blocks of volatile and non-volatile storage areas.
720 If VariableName is an empty string, then we just return the first
721 qualified variable without comparing VariableName and VendorGuid.
722 Otherwise, VariableName and VendorGuid are compared.
724 @param VariableName Name of the variable to be found.
725 @param VendorGuid Vendor GUID to be found.
726 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
727 including the range searched and the target position.
728 @param Global Pointer to VARIABLE_GLOBAL structure, including
729 base of volatile variable storage area, base of
730 NV variable storage area, and a lock.
732 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
734 @retval EFI_SUCCESS Variable successfully found.
735 @retval EFI_NOT_FOUND Variable not found
740 IN CHAR16
*VariableName
,
741 IN EFI_GUID
*VendorGuid
,
742 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
743 IN VARIABLE_GLOBAL
*Global
746 VARIABLE_HEADER
*Variable
[2];
747 VARIABLE_HEADER
*InDeletedVariable
;
748 VARIABLE_STORE_HEADER
*VariableStoreHeader
[2];
749 UINTN InDeletedStorageIndex
;
754 // 0: Volatile, 1: Non-Volatile.
755 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
756 // make use of this mapping to implement search algorithm.
758 VariableStoreHeader
[0] = (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
759 VariableStoreHeader
[1] = mNvVariableCache
;
762 // Start Pointers for the variable.
763 // Actual Data Pointer where data can be written.
765 Variable
[0] = GetStartPointer (VariableStoreHeader
[0]);
766 Variable
[1] = GetStartPointer (VariableStoreHeader
[1]);
768 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
769 return EFI_INVALID_PARAMETER
;
773 // Find the variable by walk through volatile and then non-volatile variable store.
775 InDeletedVariable
= NULL
;
776 InDeletedStorageIndex
= 0;
777 for (Index
= 0; Index
< 2; Index
++) {
778 while ((Variable
[Index
] < GetEndPointer (VariableStoreHeader
[Index
])) && IsValidVariableHeader (Variable
[Index
])) {
779 if (Variable
[Index
]->State
== VAR_ADDED
||
780 Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
782 if (!AtRuntime () || ((Variable
[Index
]->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
783 if (VariableName
[0] == 0) {
784 if (Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
785 InDeletedVariable
= Variable
[Index
];
786 InDeletedStorageIndex
= Index
;
788 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
789 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
790 PtrTrack
->CurrPtr
= Variable
[Index
];
791 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
796 if (CompareGuid (VendorGuid
, &Variable
[Index
]->VendorGuid
)) {
797 Point
= (VOID
*) GetVariableNamePtr (Variable
[Index
]);
799 ASSERT (NameSizeOfVariable (Variable
[Index
]) != 0);
800 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (Variable
[Index
])) == 0) {
801 if (Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
802 InDeletedVariable
= Variable
[Index
];
803 InDeletedStorageIndex
= Index
;
805 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
806 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
807 PtrTrack
->CurrPtr
= Variable
[Index
];
808 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
818 Variable
[Index
] = GetNextVariablePtr (Variable
[Index
]);
820 if (InDeletedVariable
!= NULL
) {
821 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[InDeletedStorageIndex
]);
822 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[InDeletedStorageIndex
]);
823 PtrTrack
->CurrPtr
= InDeletedVariable
;
824 PtrTrack
->Volatile
= (BOOLEAN
)(InDeletedStorageIndex
== 0);
828 PtrTrack
->CurrPtr
= NULL
;
829 return EFI_NOT_FOUND
;
833 Get index from supported language codes according to language string.
835 This code is used to get corresponding index in supported language codes. It can handle
836 RFC4646 and ISO639 language tags.
837 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
838 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
841 SupportedLang = "engfraengfra"
843 Iso639Language = TRUE
844 The return value is "0".
846 SupportedLang = "en;fr;en-US;fr-FR"
848 Iso639Language = FALSE
849 The return value is "3".
851 @param SupportedLang Platform supported language codes.
852 @param Lang Configured language.
853 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
855 @retval The index of language in the language codes.
859 GetIndexFromSupportedLangCodes(
860 IN CHAR8
*SupportedLang
,
862 IN BOOLEAN Iso639Language
867 UINTN LanguageLength
;
869 if (Iso639Language
) {
870 CompareLength
= ISO_639_2_ENTRY_SIZE
;
871 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
872 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
874 // Successfully find the index of Lang string in SupportedLang string.
876 Index
= Index
/ CompareLength
;
884 // Compare RFC4646 language code
887 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
889 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
891 // Skip ';' characters in SupportedLang
893 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
895 // Determine the length of the next language code in SupportedLang
897 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
899 if ((CompareLength
== LanguageLength
) &&
900 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
902 // Successfully find the index of Lang string in SupportedLang string.
913 Get language string from supported language codes according to index.
915 This code is used to get corresponding language strings in supported language codes. It can handle
916 RFC4646 and ISO639 language tags.
917 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
918 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
921 SupportedLang = "engfraengfra"
923 Iso639Language = TRUE
924 The return value is "fra".
926 SupportedLang = "en;fr;en-US;fr-FR"
928 Iso639Language = FALSE
929 The return value is "fr".
931 @param SupportedLang Platform supported language codes.
932 @param Index The index in supported language codes.
933 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
935 @retval The language string in the language codes.
939 GetLangFromSupportedLangCodes (
940 IN CHAR8
*SupportedLang
,
942 IN BOOLEAN Iso639Language
950 Supported
= SupportedLang
;
951 if (Iso639Language
) {
953 // According to the index of Lang string in SupportedLang string to get the language.
954 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
955 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
957 CompareLength
= ISO_639_2_ENTRY_SIZE
;
958 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
959 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
964 // Take semicolon as delimitation, sequentially traverse supported language codes.
966 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
969 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
971 // Have completed the traverse, but not find corrsponding string.
972 // This case is not allowed to happen.
977 if (SubIndex
== Index
) {
979 // According to the index of Lang string in SupportedLang string to get the language.
980 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
981 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
983 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
984 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
989 // Skip ';' characters in Supported
991 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
997 Returns a pointer to an allocated buffer that contains the best matching language
998 from a set of supported languages.
1000 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1001 code types may not be mixed in a single call to this function. This function
1002 supports a variable argument list that allows the caller to pass in a prioritized
1003 list of language codes to test against all the language codes in SupportedLanguages.
1005 If SupportedLanguages is NULL, then ASSERT().
1007 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1008 contains a set of language codes in the format
1009 specified by Iso639Language.
1010 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1011 in ISO 639-2 format. If FALSE, then all language
1012 codes are assumed to be in RFC 4646 language format
1013 @param[in] ... A variable argument list that contains pointers to
1014 Null-terminated ASCII strings that contain one or more
1015 language codes in the format specified by Iso639Language.
1016 The first language code from each of these language
1017 code lists is used to determine if it is an exact or
1018 close match to any of the language codes in
1019 SupportedLanguages. Close matches only apply to RFC 4646
1020 language codes, and the matching algorithm from RFC 4647
1021 is used to determine if a close match is present. If
1022 an exact or close match is found, then the matching
1023 language code from SupportedLanguages is returned. If
1024 no matches are found, then the next variable argument
1025 parameter is evaluated. The variable argument list
1026 is terminated by a NULL.
1028 @retval NULL The best matching language could not be found in SupportedLanguages.
1029 @retval NULL There are not enough resources available to return the best matching
1031 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1032 language in SupportedLanguages.
1037 VariableGetBestLanguage (
1038 IN CONST CHAR8
*SupportedLanguages
,
1039 IN BOOLEAN Iso639Language
,
1045 UINTN CompareLength
;
1046 UINTN LanguageLength
;
1047 CONST CHAR8
*Supported
;
1050 ASSERT (SupportedLanguages
!= NULL
);
1052 VA_START (Args
, Iso639Language
);
1053 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1055 // Default to ISO 639-2 mode
1058 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1061 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1063 if (!Iso639Language
) {
1064 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1068 // Trim back the length of Language used until it is empty
1070 while (LanguageLength
> 0) {
1072 // Loop through all language codes in SupportedLanguages
1074 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1076 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1078 if (!Iso639Language
) {
1080 // Skip ';' characters in Supported
1082 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1084 // Determine the length of the next language code in Supported
1086 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1088 // If Language is longer than the Supported, then skip to the next language
1090 if (LanguageLength
> CompareLength
) {
1095 // See if the first LanguageLength characters in Supported match Language
1097 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1100 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1101 Buffer
[CompareLength
] = '\0';
1102 return CopyMem (Buffer
, Supported
, CompareLength
);
1106 if (Iso639Language
) {
1108 // If ISO 639 mode, then each language can only be tested once
1113 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1115 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1122 // No matches were found
1128 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1130 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1132 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1133 and are read-only. Therefore, in variable driver, only store the original value for other use.
1135 @param[in] VariableName Name of variable.
1137 @param[in] Data Variable data.
1139 @param[in] DataSize Size of data. 0 means delete.
1143 AutoUpdateLangVariable(
1144 IN CHAR16
*VariableName
,
1150 CHAR8
*BestPlatformLang
;
1154 VARIABLE_POINTER_TRACK Variable
;
1155 BOOLEAN SetLanguageCodes
;
1158 // Don't do updates for delete operation
1160 if (DataSize
== 0) {
1164 SetLanguageCodes
= FALSE
;
1166 if (StrCmp (VariableName
, L
"PlatformLangCodes") == 0) {
1168 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1174 SetLanguageCodes
= TRUE
;
1177 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1178 // Therefore, in variable driver, only store the original value for other use.
1180 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1181 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1183 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1184 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1187 // PlatformLang holds a single language from PlatformLangCodes,
1188 // so the size of PlatformLangCodes is enough for the PlatformLang.
1190 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1191 FreePool (mVariableModuleGlobal
->PlatformLang
);
1193 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1194 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1196 } else if (StrCmp (VariableName
, L
"LangCodes") == 0) {
1198 // LangCodes is a volatile variable, so it can not be updated at runtime.
1204 SetLanguageCodes
= TRUE
;
1207 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1208 // Therefore, in variable driver, only store the original value for other use.
1210 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1211 FreePool (mVariableModuleGlobal
->LangCodes
);
1213 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1214 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1217 if (SetLanguageCodes
1218 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1219 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1221 // Update Lang if PlatformLang is already set
1222 // Update PlatformLang if Lang is already set
1224 Status
= FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*) mVariableModuleGlobal
);
1225 if (!EFI_ERROR (Status
)) {
1229 VariableName
= L
"PlatformLang";
1230 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1231 DataSize
= Variable
.CurrPtr
->DataSize
;
1233 Status
= FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*) mVariableModuleGlobal
);
1234 if (!EFI_ERROR (Status
)) {
1236 // Update PlatformLang
1238 VariableName
= L
"Lang";
1239 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1240 DataSize
= Variable
.CurrPtr
->DataSize
;
1243 // Neither PlatformLang nor Lang is set, directly return
1251 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1253 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1255 if (StrCmp (VariableName
, L
"PlatformLang") == 0) {
1257 // Update Lang when PlatformLangCodes/LangCodes were set.
1259 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1261 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1263 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1264 if (BestPlatformLang
!= NULL
) {
1266 // Get the corresponding index in language codes.
1268 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1271 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1273 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1276 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1278 FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*)mVariableModuleGlobal
);
1280 Status
= UpdateVariable (L
"Lang", &gEfiGlobalVariableGuid
, BestLang
,
1281 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, &Variable
);
1283 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
1285 ASSERT_EFI_ERROR(Status
);
1289 } else if (StrCmp (VariableName
, L
"Lang") == 0) {
1291 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1293 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1295 // When setting Lang, firstly get most matched language string from supported language codes.
1297 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1298 if (BestLang
!= NULL
) {
1300 // Get the corresponding index in language codes.
1302 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1305 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1307 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1310 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1312 FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*)mVariableModuleGlobal
);
1314 Status
= UpdateVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, BestPlatformLang
,
1315 AsciiStrSize (BestPlatformLang
), Attributes
, &Variable
);
1317 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
1318 ASSERT_EFI_ERROR (Status
);
1325 Update the variable region with Variable information. These are the same
1326 arguments as the EFI Variable services.
1328 @param[in] VariableName Name of variable.
1329 @param[in] VendorGuid Guid of variable.
1330 @param[in] Data Variable data.
1331 @param[in] DataSize Size of data. 0 means delete.
1332 @param[in] Attributes Attribues of the variable.
1333 @param[in] CacheVariable The variable information which is used to keep track of variable usage.
1335 @retval EFI_SUCCESS The update operation is success.
1336 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1341 IN CHAR16
*VariableName
,
1342 IN EFI_GUID
*VendorGuid
,
1345 IN UINT32 Attributes OPTIONAL
,
1346 IN VARIABLE_POINTER_TRACK
*CacheVariable
1350 VARIABLE_HEADER
*NextVariable
;
1352 UINTN NonVolatileVarableStoreSize
;
1353 UINTN VarNameOffset
;
1354 UINTN VarDataOffset
;
1358 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1361 VARIABLE_POINTER_TRACK
*Variable
;
1362 VARIABLE_POINTER_TRACK NvVariable
;
1363 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1366 if (CacheVariable
->Volatile
) {
1367 Variable
= CacheVariable
;
1369 if (mVariableModuleGlobal
->FvbInstance
== NULL
) {
1371 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1373 return EFI_NOT_AVAILABLE_YET
;
1377 // CacheVariable points to the variable in the memory copy of Flash area
1378 // Now let Variable points to the same variable in Flash area.
1380 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1381 Variable
= &NvVariable
;
1382 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1383 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1384 if (CacheVariable
->CurrPtr
== NULL
) {
1385 Variable
->CurrPtr
= NULL
;
1387 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1389 Variable
->Volatile
= FALSE
;
1392 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1395 if (Variable
->CurrPtr
!= NULL
) {
1397 // Update/Delete existing variable.
1401 // If AtRuntime and the variable is Volatile and Runtime Access,
1402 // the volatile is ReadOnly, and SetVariable should be aborted and
1403 // return EFI_WRITE_PROTECTED.
1405 if (Variable
->Volatile
) {
1406 Status
= EFI_WRITE_PROTECTED
;
1410 // Only variable that have NV attributes can be updated/deleted in Runtime.
1412 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1413 Status
= EFI_INVALID_PARAMETER
;
1419 // Setting a data variable with no access, or zero DataSize attributes
1420 // causes it to be deleted.
1422 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1423 State
= Variable
->CurrPtr
->State
;
1424 State
&= VAR_DELETED
;
1426 Status
= UpdateVariableStore (
1427 &mVariableModuleGlobal
->VariableGlobal
,
1431 (UINTN
) &Variable
->CurrPtr
->State
,
1435 if (!EFI_ERROR (Status
)) {
1436 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1437 if (!Variable
->Volatile
) {
1438 CacheVariable
->CurrPtr
->State
= State
;
1444 // If the variable is marked valid, and the same data has been passed in,
1445 // then return to the caller immediately.
1447 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
1448 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0)) {
1450 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1451 Status
= EFI_SUCCESS
;
1453 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
1454 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1457 // Mark the old variable as in delete transition.
1459 State
= Variable
->CurrPtr
->State
;
1460 State
&= VAR_IN_DELETED_TRANSITION
;
1462 Status
= UpdateVariableStore (
1463 &mVariableModuleGlobal
->VariableGlobal
,
1467 (UINTN
) &Variable
->CurrPtr
->State
,
1471 if (EFI_ERROR (Status
)) {
1474 if (!Variable
->Volatile
) {
1475 CacheVariable
->CurrPtr
->State
= State
;
1480 // Not found existing variable. Create a new variable.
1484 // Make sure we are trying to create a new variable.
1485 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1487 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1488 Status
= EFI_NOT_FOUND
;
1493 // Only variable have NV|RT attribute can be created in Runtime.
1496 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1497 Status
= EFI_INVALID_PARAMETER
;
1503 // Function part - create a new variable and copy the data.
1504 // Both update a variable and create a variable will come here.
1507 // Tricky part: Use scratch data area at the end of volatile variable store
1508 // as a temporary storage.
1510 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1511 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1513 SetMem (NextVariable
, ScratchSize
, 0xff);
1515 NextVariable
->StartId
= VARIABLE_DATA
;
1516 NextVariable
->Attributes
= Attributes
;
1518 // NextVariable->State = VAR_ADDED;
1520 NextVariable
->Reserved
= 0;
1521 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1522 VarNameSize
= StrSize (VariableName
);
1524 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1528 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1530 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1534 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1536 // There will be pad bytes after Data, the NextVariable->NameSize and
1537 // NextVariable->DataSize should not include pad size so that variable
1538 // service can get actual size in GetVariable.
1540 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1541 NextVariable
->DataSize
= (UINT32
)DataSize
;
1544 // The actual size of the variable that stores in storage should
1545 // include pad size.
1547 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1548 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1550 // Create a nonvolatile variable.
1553 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1554 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1555 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1556 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1557 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1559 Status
= EFI_OUT_OF_RESOURCES
;
1563 // Perform garbage collection & reclaim operation.
1565 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1566 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
, FALSE
, Variable
->CurrPtr
);
1567 if (EFI_ERROR (Status
)) {
1571 // If still no enough space, return out of resources.
1573 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1574 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1575 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1576 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1577 Status
= EFI_OUT_OF_RESOURCES
;
1584 // 1. Write variable header
1585 // 2. Set variable state to header valid
1586 // 3. Write variable data
1587 // 4. Set variable state to valid
1592 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1593 Status
= UpdateVariableStore (
1594 &mVariableModuleGlobal
->VariableGlobal
,
1598 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1599 sizeof (VARIABLE_HEADER
),
1600 (UINT8
*) NextVariable
1603 if (EFI_ERROR (Status
)) {
1610 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1611 Status
= UpdateVariableStore (
1612 &mVariableModuleGlobal
->VariableGlobal
,
1616 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1618 &NextVariable
->State
1621 if (EFI_ERROR (Status
)) {
1627 Status
= UpdateVariableStore (
1628 &mVariableModuleGlobal
->VariableGlobal
,
1632 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
1633 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1634 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1637 if (EFI_ERROR (Status
)) {
1643 NextVariable
->State
= VAR_ADDED
;
1644 Status
= UpdateVariableStore (
1645 &mVariableModuleGlobal
->VariableGlobal
,
1649 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1651 &NextVariable
->State
1654 if (EFI_ERROR (Status
)) {
1658 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1660 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
1661 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1663 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1666 // update the memory copy of Flash region.
1668 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
1671 // Create a volatile variable.
1675 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1676 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1678 // Perform garbage collection & reclaim operation.
1680 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
1681 &mVariableModuleGlobal
->VolatileLastVariableOffset
, TRUE
, Variable
->CurrPtr
);
1682 if (EFI_ERROR (Status
)) {
1686 // If still no enough space, return out of resources.
1688 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1689 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1691 Status
= EFI_OUT_OF_RESOURCES
;
1697 NextVariable
->State
= VAR_ADDED
;
1698 Status
= UpdateVariableStore (
1699 &mVariableModuleGlobal
->VariableGlobal
,
1703 mVariableModuleGlobal
->VolatileLastVariableOffset
,
1705 (UINT8
*) NextVariable
1708 if (EFI_ERROR (Status
)) {
1712 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1716 // Mark the old variable as deleted.
1718 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
1719 State
= Variable
->CurrPtr
->State
;
1720 State
&= VAR_DELETED
;
1722 Status
= UpdateVariableStore (
1723 &mVariableModuleGlobal
->VariableGlobal
,
1727 (UINTN
) &Variable
->CurrPtr
->State
,
1731 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
1732 CacheVariable
->CurrPtr
->State
= State
;
1736 if (!EFI_ERROR (Status
)) {
1737 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1746 This code finds variable in storage blocks (Volatile or Non-Volatile).
1748 @param VariableName Name of Variable to be found.
1749 @param VendorGuid Variable vendor GUID.
1750 @param Attributes Attribute value of the variable found.
1751 @param DataSize Size of Data found. If size is less than the
1752 data, this value contains the required size.
1753 @param Data Data pointer.
1755 @return EFI_INVALID_PARAMETER Invalid parameter.
1756 @return EFI_SUCCESS Find the specified variable.
1757 @return EFI_NOT_FOUND Not found.
1758 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1763 VariableServiceGetVariable (
1764 IN CHAR16
*VariableName
,
1765 IN EFI_GUID
*VendorGuid
,
1766 OUT UINT32
*Attributes OPTIONAL
,
1767 IN OUT UINTN
*DataSize
,
1772 VARIABLE_POINTER_TRACK Variable
;
1775 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
1776 return EFI_INVALID_PARAMETER
;
1779 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1781 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1782 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1789 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
1790 ASSERT (VarDataSize
!= 0);
1792 if (*DataSize
>= VarDataSize
) {
1794 Status
= EFI_INVALID_PARAMETER
;
1798 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
1799 if (Attributes
!= NULL
) {
1800 *Attributes
= Variable
.CurrPtr
->Attributes
;
1803 *DataSize
= VarDataSize
;
1804 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
1806 Status
= EFI_SUCCESS
;
1809 *DataSize
= VarDataSize
;
1810 Status
= EFI_BUFFER_TOO_SMALL
;
1815 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1823 This code Finds the Next available variable.
1825 @param VariableNameSize Size of the variable name.
1826 @param VariableName Pointer to variable name.
1827 @param VendorGuid Variable Vendor Guid.
1829 @return EFI_INVALID_PARAMETER Invalid parameter.
1830 @return EFI_SUCCESS Find the specified variable.
1831 @return EFI_NOT_FOUND Not found.
1832 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1837 VariableServiceGetNextVariableName (
1838 IN OUT UINTN
*VariableNameSize
,
1839 IN OUT CHAR16
*VariableName
,
1840 IN OUT EFI_GUID
*VendorGuid
1843 VARIABLE_POINTER_TRACK Variable
;
1847 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
1848 return EFI_INVALID_PARAMETER
;
1851 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1853 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1854 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1858 if (VariableName
[0] != 0) {
1860 // If variable name is not NULL, get next variable.
1862 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1867 // If both volatile and non-volatile variable store are parsed,
1868 // return not found.
1870 if (Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
) {
1871 Variable
.Volatile
= (BOOLEAN
) (Variable
.Volatile
^ ((BOOLEAN
) 0x1));
1872 if (!Variable
.Volatile
) {
1873 Variable
.StartPtr
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1874 Variable
.EndPtr
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
));
1876 Status
= EFI_NOT_FOUND
;
1880 Variable
.CurrPtr
= Variable
.StartPtr
;
1881 if (!IsValidVariableHeader (Variable
.CurrPtr
)) {
1886 // Variable is found
1888 if (IsValidVariableHeader (Variable
.CurrPtr
) && Variable
.CurrPtr
->State
== VAR_ADDED
) {
1889 if ((AtRuntime () && ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) == 0) {
1890 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
1891 ASSERT (VarNameSize
!= 0);
1893 if (VarNameSize
<= *VariableNameSize
) {
1896 GetVariableNamePtr (Variable
.CurrPtr
),
1901 &Variable
.CurrPtr
->VendorGuid
,
1904 Status
= EFI_SUCCESS
;
1906 Status
= EFI_BUFFER_TOO_SMALL
;
1909 *VariableNameSize
= VarNameSize
;
1914 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1918 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1924 This code sets variable in storage blocks (Volatile or Non-Volatile).
1926 @param VariableName Name of Variable to be found.
1927 @param VendorGuid Variable vendor GUID.
1928 @param Attributes Attribute value of the variable found
1929 @param DataSize Size of Data found. If size is less than the
1930 data, this value contains the required size.
1931 @param Data Data pointer.
1933 @return EFI_INVALID_PARAMETER Invalid parameter.
1934 @return EFI_SUCCESS Set successfully.
1935 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
1936 @return EFI_NOT_FOUND Not found.
1937 @return EFI_WRITE_PROTECTED Variable is read-only.
1942 VariableServiceSetVariable (
1943 IN CHAR16
*VariableName
,
1944 IN EFI_GUID
*VendorGuid
,
1945 IN UINT32 Attributes
,
1950 VARIABLE_POINTER_TRACK Variable
;
1952 VARIABLE_HEADER
*NextVariable
;
1953 EFI_PHYSICAL_ADDRESS Point
;
1956 // Check input parameters.
1958 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
1959 return EFI_INVALID_PARAMETER
;
1962 if (DataSize
!= 0 && Data
== NULL
) {
1963 return EFI_INVALID_PARAMETER
;
1967 // Not support authenticated variable write yet.
1969 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1970 return EFI_INVALID_PARAMETER
;
1974 // Make sure if runtime bit is set, boot service bit is set also.
1976 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1977 return EFI_INVALID_PARAMETER
;
1981 // The size of the VariableName, including the Unicode Null in bytes plus
1982 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
1983 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
1985 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1986 if ((DataSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
)) ||
1987 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
))) {
1988 return EFI_INVALID_PARAMETER
;
1991 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX".
1993 if (StrnCmp(VariableName
, L
"HwErrRec", StrLen(L
"HwErrRec")) != 0) {
1994 return EFI_INVALID_PARAMETER
;
1998 // The size of the VariableName, including the Unicode Null in bytes plus
1999 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2001 if ((DataSize
> PcdGet32 (PcdMaxVariableSize
)) ||
2002 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxVariableSize
))) {
2003 return EFI_INVALID_PARAMETER
;
2007 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2010 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2012 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2013 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2015 // Parse non-volatile variable data and get last variable offset.
2017 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2018 while ((NextVariable
< GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))
2019 && IsValidVariableHeader (NextVariable
)) {
2020 NextVariable
= GetNextVariablePtr (NextVariable
);
2022 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2026 // Check whether the input variable is already existed.
2028 FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
2031 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2033 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2035 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, &Variable
);
2037 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2038 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2045 This code returns information about the EFI variables.
2047 @param Attributes Attributes bitmask to specify the type of variables
2048 on which to return information.
2049 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2050 for the EFI variables associated with the attributes specified.
2051 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2052 for EFI variables associated with the attributes specified.
2053 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2054 associated with the attributes specified.
2056 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2057 @return EFI_SUCCESS Query successfully.
2058 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2063 VariableServiceQueryVariableInfo (
2064 IN UINT32 Attributes
,
2065 OUT UINT64
*MaximumVariableStorageSize
,
2066 OUT UINT64
*RemainingVariableStorageSize
,
2067 OUT UINT64
*MaximumVariableSize
2070 VARIABLE_HEADER
*Variable
;
2071 VARIABLE_HEADER
*NextVariable
;
2072 UINT64 VariableSize
;
2073 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2074 UINT64 CommonVariableTotalSize
;
2075 UINT64 HwErrVariableTotalSize
;
2077 CommonVariableTotalSize
= 0;
2078 HwErrVariableTotalSize
= 0;
2080 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2081 return EFI_INVALID_PARAMETER
;
2084 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2086 // Make sure the Attributes combination is supported by the platform.
2088 return EFI_UNSUPPORTED
;
2089 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2091 // Make sure if runtime bit is set, boot service bit is set also.
2093 return EFI_INVALID_PARAMETER
;
2094 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2096 // Make sure RT Attribute is set if we are in Runtime phase.
2098 return EFI_INVALID_PARAMETER
;
2099 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2101 // Make sure Hw Attribute is set with NV.
2103 return EFI_INVALID_PARAMETER
;
2104 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2106 // Not support authentiated variable write yet.
2108 return EFI_UNSUPPORTED
;
2111 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2113 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2115 // Query is Volatile related.
2117 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2120 // Query is Non-Volatile related.
2122 VariableStoreHeader
= mNvVariableCache
;
2126 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2127 // with the storage size (excluding the storage header size).
2129 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2132 // Harware error record variable needs larger size.
2134 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2135 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2136 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2138 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2139 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
2140 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
2144 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2146 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2150 // Point to the starting address of the variables.
2152 Variable
= GetStartPointer (VariableStoreHeader
);
2155 // Now walk through the related variable store.
2157 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
2158 NextVariable
= GetNextVariablePtr (Variable
);
2159 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2163 // We don't take the state of the variables in mind
2164 // when calculating RemainingVariableStorageSize,
2165 // since the space occupied by variables not marked with
2166 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2168 if ((NextVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2169 HwErrVariableTotalSize
+= VariableSize
;
2171 CommonVariableTotalSize
+= VariableSize
;
2175 // Only care about Variables with State VAR_ADDED, because
2176 // the space not marked as VAR_ADDED is reclaimable now.
2178 if (Variable
->State
== VAR_ADDED
) {
2179 if ((NextVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2180 HwErrVariableTotalSize
+= VariableSize
;
2182 CommonVariableTotalSize
+= VariableSize
;
2188 // Go to the next one.
2190 Variable
= NextVariable
;
2193 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2194 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2196 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2199 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2200 *MaximumVariableSize
= 0;
2201 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2202 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2205 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2211 This function reclaims variable storage if free size is below the threshold.
2220 UINTN CommonVariableSpace
;
2221 UINTN RemainingCommonVariableSpace
;
2222 UINTN RemainingHwErrVariableSpace
;
2224 Status
= EFI_SUCCESS
;
2226 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
2228 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2230 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2232 // Check if the free area is blow a threshold.
2234 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2235 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2236 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2238 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2239 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2243 ASSERT_EFI_ERROR (Status
);
2249 Initializes variable write service after FVB was ready.
2251 @retval EFI_SUCCESS Function successfully executed.
2252 @retval Others Fail to initialize the variable service.
2256 VariableWriteServiceInitialize (
2261 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2264 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
2265 EFI_PHYSICAL_ADDRESS BaseAddress
;
2267 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2268 UINT64 VariableStoreLength
;
2270 VariableStoreBase
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2271 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2272 VariableStoreLength
= VariableStoreHeader
->Size
;
2275 // Check if the free area is really free.
2277 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreLength
; Index
++) {
2278 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
2281 // There must be something wrong in variable store, do reclaim operation.
2284 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2285 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2289 if (EFI_ERROR (Status
)) {
2297 // Mark the variable storage region of the FLASH as RUNTIME.
2299 BaseAddress
= VariableStoreBase
& (~EFI_PAGE_MASK
);
2300 Length
= VariableStoreLength
+ (VariableStoreBase
- BaseAddress
);
2301 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
2303 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
2304 if (EFI_ERROR (Status
)) {
2305 DEBUG ((DEBUG_WARN
, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
2307 Status
= gDS
->SetMemorySpaceAttributes (
2310 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
2312 if (EFI_ERROR (Status
)) {
2313 DEBUG ((DEBUG_WARN
, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
2321 Initializes variable store area for non-volatile and volatile variable.
2323 @retval EFI_SUCCESS Function successfully executed.
2324 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2328 VariableCommonInitialize (
2333 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
2334 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2335 VARIABLE_HEADER
*NextVariable
;
2336 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader
;
2337 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2338 UINT64 VariableStoreLength
;
2343 // Allocate runtime memory for variable driver global structure.
2345 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
2346 if (mVariableModuleGlobal
== NULL
) {
2347 return EFI_OUT_OF_RESOURCES
;
2350 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
2353 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2354 // is stored with common variable in the same NV region. So the platform integrator should
2355 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2356 // PcdFlashNvStorageVariableSize.
2358 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
2361 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
2363 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
2364 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
2365 if (VolatileVariableStore
== NULL
) {
2366 FreePool (mVariableModuleGlobal
);
2367 return EFI_OUT_OF_RESOURCES
;
2370 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
2373 // Initialize Variable Specific Data.
2375 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
2376 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
2377 mVariableModuleGlobal
->FvbInstance
= NULL
;
2379 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiVariableGuid
);
2380 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
2381 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
2382 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
2383 VolatileVariableStore
->Reserved
= 0;
2384 VolatileVariableStore
->Reserved1
= 0;
2387 // Get non-volatile varaible store.
2390 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
2391 if (TempVariableStoreHeader
== 0) {
2392 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
2394 VariableStoreBase
= TempVariableStoreHeader
+ \
2395 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2396 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
2397 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2399 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
2400 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2401 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
2402 Status
= EFI_VOLUME_CORRUPTED
;
2403 DEBUG((EFI_D_INFO
, "Variable Store header is corrupted\n"));
2406 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
2409 // Parse non-volatile variable data and get last variable offset.
2411 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
2412 while (IsValidVariableHeader (NextVariable
)) {
2413 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
2414 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2415 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2417 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2420 NextVariable
= GetNextVariablePtr (NextVariable
);
2423 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
2426 // Allocate runtime memory used for a memory copy of the FLASH region.
2427 // Keep the memory and the FLASH in sync as updates occur
2429 mNvVariableCache
= AllocateRuntimeZeroPool ((UINTN
)VariableStoreLength
);
2430 if (mNvVariableCache
== NULL
) {
2431 Status
= EFI_OUT_OF_RESOURCES
;
2434 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableStoreBase
, (UINTN
)VariableStoreLength
);
2435 Status
= EFI_SUCCESS
;
2438 if (EFI_ERROR (Status
)) {
2439 FreePool (mVariableModuleGlobal
);
2440 FreePool (VolatileVariableStore
);
2448 Get the proper fvb handle and/or fvb protocol by the given Flash address.
2450 @param[in] Address The Flash address.
2451 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
2452 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
2456 GetFvbInfoByAddress (
2457 IN EFI_PHYSICAL_ADDRESS Address
,
2458 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
2459 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
2463 EFI_HANDLE
*HandleBuffer
;
2466 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
2467 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
2468 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
2469 EFI_FVB_ATTRIBUTES_2 Attributes
;
2472 // Get all FVB handles.
2474 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
2475 if (EFI_ERROR (Status
)) {
2476 return EFI_NOT_FOUND
;
2480 // Get the FVB to access variable store.
2483 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
2484 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
2485 if (EFI_ERROR (Status
)) {
2486 Status
= EFI_NOT_FOUND
;
2491 // Ensure this FVB protocol supported Write operation.
2493 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
2494 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
2499 // Compare the address and select the right one.
2501 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
2502 if (EFI_ERROR (Status
)) {
2506 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
2507 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
2508 if (FvbHandle
!= NULL
) {
2509 *FvbHandle
= HandleBuffer
[Index
];
2511 if (FvbProtocol
!= NULL
) {
2514 Status
= EFI_SUCCESS
;
2518 FreePool (HandleBuffer
);
2521 Status
= EFI_NOT_FOUND
;