3 Emulation Variable services operate on the runtime volatile memory.
4 The nonvolatile variable space doesn't exist.
6 Copyright (c) 2006 - 2008, Intel Corporation
7 All rights reserved. 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.
20 /// Don't use module globals after the SetVirtualAddress map is signaled
22 ESAL_VARIABLE_GLOBAL
*mVariableModuleGlobal
;
24 VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
27 /// The size of a 3 character ISO639 language code.
29 #define ISO_639_2_ENTRY_SIZE 3
32 Update the variable region with Variable information. These are the same
33 arguments as the EFI Variable services.
35 @param[in] VariableName Name of variable
37 @param[in] VendorGuid Guid of variable
39 @param[in] Data Variable data
41 @param[in] DataSize Size of data. 0 means delete
43 @param[in] Attributes Attribues of the variable
45 @param[in] Variable The variable information which is used to keep track of variable usage.
47 @retval EFI_SUCCESS The update operation is success.
49 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
55 IN CHAR16
*VariableName
,
56 IN EFI_GUID
*VendorGuid
,
59 IN UINT32 Attributes OPTIONAL
,
60 IN VARIABLE_POINTER_TRACK
*Variable
64 Finds variable in storage blocks of volatile and non-volatile storage areas.
66 This code finds variable in storage blocks of volatile and non-volatile storage areas.
67 If VariableName is an empty string, then we just return the first
68 qualified variable without comparing VariableName and VendorGuid.
69 Otherwise, VariableName and VendorGuid are compared.
71 @param VariableName Name of the variable to be found.
72 @param VendorGuid Vendor GUID to be found.
73 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
74 including the range searched and the target position.
75 @param Global Pointer to VARIABLE_GLOBAL structure, including
76 base of volatile variable storage area, base of
77 NV variable storage area, and a lock.
79 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
81 @retval EFI_SUCCESS Variable successfully found.
82 @retval EFI_NOT_FOUND Variable not found.
87 IN CHAR16
*VariableName
,
88 IN EFI_GUID
*VendorGuid
,
89 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
90 IN VARIABLE_GLOBAL
*Global
94 Acquires lock only at boot time. Simply returns at runtime.
96 This is a temperary function which will be removed when
97 EfiAcquireLock() in UefiLib can handle the call in UEFI
98 Runtimer driver in RT phase.
99 It calls EfiAcquireLock() at boot time, and simply returns
102 @param Lock A pointer to the lock to acquire
106 AcquireLockOnlyAtBootTime (
110 if (!EfiAtRuntime ()) {
111 EfiAcquireLock (Lock
);
116 Releases lock only at boot time. Simply returns at runtime.
118 This is a temperary function which will be removed when
119 EfiReleaseLock() in UefiLib can handle the call in UEFI
120 Runtimer driver in RT phase.
121 It calls EfiReleaseLock() at boot time, and simply returns
124 @param Lock A pointer to the lock to release
128 ReleaseLockOnlyAtBootTime (
132 if (!EfiAtRuntime ()) {
133 EfiReleaseLock (Lock
);
138 Gets pointer to the variable data.
140 This function gets the pointer to the variable data according
141 to the input pointer to the variable header.
143 @param Variable Pointer to the variable header.
145 @return Pointer to variable data
150 IN VARIABLE_HEADER
*Variable
153 if (Variable
->StartId
!= VARIABLE_DATA
) {
157 // Be careful about pad size for alignment
159 return (UINT8
*) ((UINTN
) GET_VARIABLE_NAME_PTR (Variable
) + Variable
->NameSize
+ GET_PAD_SIZE (Variable
->NameSize
));
163 Gets pointer to header of the next potential variable.
165 This function gets the pointer to the next potential variable header
166 according to the input point to the variable header. The return value
167 is not a valid variable if the input variable was the last variable
168 in the variabl store.
170 @param Variable Pointer to header of the next variable
172 @return Pointer to next variable header.
173 @retval NULL Input was not a valid variable header.
177 GetNextPotentialVariablePtr (
178 IN VARIABLE_HEADER
*Variable
181 VARIABLE_HEADER
*VarHeader
;
183 if (Variable
->StartId
!= VARIABLE_DATA
) {
187 // Be careful about pad size for alignment
189 VarHeader
= (VARIABLE_HEADER
*) (GetVariableDataPtr (Variable
) + Variable
->DataSize
+ GET_PAD_SIZE (Variable
->DataSize
));
195 Gets pointer to header of the next variable.
197 This function gets the pointer to the next variable header according
198 to the input point to the variable header.
200 @param Variable Pointer to header of the next variable
202 @return Pointer to next variable header.
207 IN VARIABLE_HEADER
*Variable
210 VARIABLE_HEADER
*VarHeader
;
212 VarHeader
= GetNextPotentialVariablePtr (Variable
);
214 if ((VarHeader
== NULL
) || (VarHeader
->StartId
!= VARIABLE_DATA
)) {
222 Updates LastVariableOffset variable for the given variable store.
224 LastVariableOffset points to the offset to use for the next variable
225 when updating the variable store.
227 @param[in] VariableStore Pointer to the start of the variable store
228 @param[out] LastVariableOffset Offset to put the next new variable in
232 InitializeLocationForLastVariableOffset (
233 IN VARIABLE_STORE_HEADER
*VariableStore
,
234 OUT UINTN
*LastVariableOffset
237 VARIABLE_HEADER
*VarHeader
;
239 *LastVariableOffset
= sizeof (VARIABLE_STORE_HEADER
);
240 VarHeader
= (VARIABLE_HEADER
*) ((UINT8
*)VariableStore
+ *LastVariableOffset
);
241 while (VarHeader
->StartId
== VARIABLE_DATA
) {
242 VarHeader
= GetNextPotentialVariablePtr (VarHeader
);
244 if (VarHeader
!= NULL
) {
245 *LastVariableOffset
= (UINTN
) VarHeader
- (UINTN
) VariableStore
;
253 Gets pointer to the end of the variable storage area.
255 This function gets pointer to the end of the variable storage
256 area, according to the input variable store header.
258 @param VolHeader Pointer to the variale store header
260 @return Pointer to the end of the variable storage area.
265 IN VARIABLE_STORE_HEADER
*VolHeader
269 // The end of variable store
271 return (VARIABLE_HEADER
*) ((UINTN
) VolHeader
+ VolHeader
->Size
);
275 Routine used to track statistical information about variable usage.
276 The data is stored in the EFI system table so it can be accessed later.
277 VariableInfo.efi can dump out the table. Only Boot Services variable
278 accesses are tracked by this code. The PcdVariableCollectStatistics
279 build flag controls if this feature is enabled.
281 A read that hits in the cache will have Read and Cache true for
282 the transaction. Data is allocated by this routine, but never
285 @param[in] VariableName Name of the Variable to track
286 @param[in] VendorGuid Guid of the Variable to track
287 @param[in] Volatile TRUE if volatile FALSE if non-volatile
288 @param[in] Read TRUE if GetVariable() was called
289 @param[in] Write TRUE if SetVariable() was called
290 @param[in] Delete TRUE if deleted via SetVariable()
291 @param[in] Cache TRUE for a cache hit.
296 IN CHAR16
*VariableName
,
297 IN EFI_GUID
*VendorGuid
,
305 VARIABLE_INFO_ENTRY
*Entry
;
307 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
309 if (EfiAtRuntime ()) {
310 // Don't collect statistics at runtime
314 if (gVariableInfo
== NULL
) {
316 // on the first call allocate a entry and place a pointer to it in
317 // the EFI System Table
319 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
320 ASSERT (gVariableInfo
!= NULL
);
322 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
323 gVariableInfo
->Name
= AllocatePool (StrSize (VariableName
));
324 ASSERT (gVariableInfo
->Name
!= NULL
);
325 StrCpy (gVariableInfo
->Name
, VariableName
);
326 gVariableInfo
->Volatile
= Volatile
;
328 gBS
->InstallConfigurationTable (&gEfiVariableGuid
, gVariableInfo
);
332 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
333 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
334 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
342 Entry
->DeleteCount
++;
352 if (Entry
->Next
== NULL
) {
354 // If the entry is not in the table add it.
355 // Next iteration of the loop will fill in the data
357 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
358 ASSERT (Entry
->Next
!= NULL
);
360 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
361 Entry
->Next
->Name
= AllocatePool (StrSize (VariableName
));
362 ASSERT (Entry
->Next
->Name
!= NULL
);
363 StrCpy (Entry
->Next
->Name
, VariableName
);
364 Entry
->Next
->Volatile
= Volatile
;
372 Get index from supported language codes according to language string.
374 This code is used to get corresponding index in supported language codes. It can handle
375 RFC4646 and ISO639 language tags.
376 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
377 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
380 SupportedLang = "engfraengfra"
382 Iso639Language = TRUE
383 The return value is "0".
385 SupportedLang = "en;fr;en-US;fr-FR"
387 Iso639Language = FALSE
388 The return value is "3".
390 @param SupportedLang Platform supported language codes.
391 @param Lang Configured language.
392 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
394 @retval the index of language in the language codes.
399 GetIndexFromSupportedLangCodes(
400 IN CHAR8
*SupportedLang
,
402 IN BOOLEAN Iso639Language
406 UINT32 CompareLength
;
410 Supported
= SupportedLang
;
411 if (Iso639Language
) {
413 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
414 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
416 // Successfully find the index of Lang string in SupportedLang string.
418 Index
= Index
/ CompareLength
;
426 // Compare RFC4646 language code
428 while (*Supported
!= '\0') {
430 // take semicolon as delimitation, sequentially traverse supported language codes.
432 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
435 if (AsciiStrnCmp (Lang
, Supported
- CompareLength
, CompareLength
) == 0) {
437 // Successfully find the index of Lang string in SupportedLang string.
449 Get language string from supported language codes according to index.
451 This code is used to get corresponding language string in supported language codes. It can handle
452 RFC4646 and ISO639 language tags.
453 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
454 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
457 SupportedLang = "engfraengfra"
459 Iso639Language = TRUE
460 The return value is "fra".
462 SupportedLang = "en;fr;en-US;fr-FR"
464 Iso639Language = FALSE
465 The return value is "fr".
467 @param SupportedLang Platform supported language codes.
468 @param Index the index in supported language codes.
469 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
471 @retval the language string in the language codes.
476 GetLangFromSupportedLangCodes (
477 IN CHAR8
*SupportedLang
,
479 IN BOOLEAN Iso639Language
483 UINT32 CompareLength
;
487 Supported
= SupportedLang
;
488 if (Iso639Language
) {
490 // according to the index of Lang string in SupportedLang string to get the language.
491 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
492 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
495 SetMem (mVariableModuleGlobal
->Lang
, sizeof(mVariableModuleGlobal
->Lang
), 0);
496 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
501 // take semicolon as delimitation, sequentially traverse supported language codes.
503 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
506 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
508 // Have completed the traverse, but not find corrsponding string.
509 // This case is not allowed to happen.
514 if (SubIndex
== Index
) {
516 // according to the index of Lang string in SupportedLang string to get the language.
517 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
518 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
520 SetMem (mVariableModuleGlobal
->PlatformLang
, sizeof (mVariableModuleGlobal
->PlatformLang
), 0);
521 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
529 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
531 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
533 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
534 and are read-only. Therefore, in variable driver, only store the original value for other use.
536 @param[in] VariableName Name of variable
538 @param[in] Data Variable data
540 @param[in] DataSize Size of data. 0 means delete
542 @retval EFI_SUCCESS auto update operation is successful.
547 AutoUpdateLangVariable(
548 IN CHAR16
*VariableName
,
554 CHAR8
*BestPlatformLang
;
558 VARIABLE_POINTER_TRACK Variable
;
561 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
563 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
565 if (StrCmp (VariableName
, L
"PlatformLangCodes") == 0) {
567 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
568 // Therefore, in variable driver, only store the original value for other use.
570 AsciiStrnCpy (mVariableModuleGlobal
->PlatformLangCodes
, Data
, DataSize
);
571 } else if (StrCmp (VariableName
, L
"LangCodes") == 0) {
573 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
574 // Therefore, in variable driver, only store the original value for other use.
576 AsciiStrnCpy (mVariableModuleGlobal
->LangCodes
, Data
, DataSize
);
577 } else if ((StrCmp (VariableName
, L
"PlatformLang") == 0) && (DataSize
!= 0)) {
578 ASSERT (AsciiStrLen (mVariableModuleGlobal
->PlatformLangCodes
) != 0);
581 // When setting PlatformLang, firstly get most matched language string from supported language codes.
583 BestPlatformLang
= GetBestLanguage(mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
586 // Get the corresponding index in language codes.
588 Index
= GetIndexFromSupportedLangCodes(mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
591 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
593 BestLang
= GetLangFromSupportedLangCodes(mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
596 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
598 FindVariable(L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*)mVariableModuleGlobal
);
600 Status
= UpdateVariable(L
"Lang", &gEfiGlobalVariableGuid
,
601 BestLang
, ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, &Variable
);
603 DEBUG((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
605 ASSERT_EFI_ERROR(Status
);
607 } else if ((StrCmp (VariableName
, L
"Lang") == 0) && (DataSize
!= 0)) {
608 ASSERT (AsciiStrLen (mVariableModuleGlobal
->LangCodes
) != 0);
611 // When setting Lang, firstly get most matched language string from supported language codes.
613 BestLang
= GetBestLanguage(mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
616 // Get the corresponding index in language codes.
618 Index
= GetIndexFromSupportedLangCodes(mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
621 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
623 BestPlatformLang
= GetLangFromSupportedLangCodes(mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
626 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
628 FindVariable(L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*)mVariableModuleGlobal
);
630 Status
= UpdateVariable(L
"PlatformLang", &gEfiGlobalVariableGuid
,
631 BestPlatformLang
, AsciiStrSize (BestPlatformLang
), Attributes
, &Variable
);
633 DEBUG((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
634 ASSERT_EFI_ERROR(Status
);
640 Update the variable region with Variable information. These are the same
641 arguments as the EFI Variable services.
643 @param[in] VariableName Name of variable
645 @param[in] VendorGuid Guid of variable
647 @param[in] Data Variable data
649 @param[in] DataSize Size of data. 0 means delete
651 @param[in] Attributes Attribues of the variable
653 @param[in] Variable The variable information which is used to keep track of variable usage.
655 @retval EFI_SUCCESS The update operation is success.
657 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
663 IN CHAR16
*VariableName
,
664 IN EFI_GUID
*VendorGuid
,
667 IN UINT32 Attributes OPTIONAL
,
668 IN VARIABLE_POINTER_TRACK
*Variable
672 VARIABLE_HEADER
*NextVariable
;
677 VARIABLE_GLOBAL
*Global
;
678 UINTN NonVolatileVarableStoreSize
;
680 Global
= &mVariableModuleGlobal
->VariableGlobal
[Physical
];
682 if (Variable
->CurrPtr
!= NULL
) {
684 // Update/Delete existing variable
687 if (EfiAtRuntime ()) {
689 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
690 // the volatile is ReadOnly, and SetVariable should be aborted and
691 // return EFI_WRITE_PROTECTED.
693 if (Variable
->Volatile
) {
694 Status
= EFI_WRITE_PROTECTED
;
698 // Only variable have NV attribute can be updated/deleted in Runtime
700 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
701 Status
= EFI_INVALID_PARAMETER
;
707 // Setting a data variable with no access, or zero DataSize attributes
708 // specified causes it to be deleted.
710 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
711 Variable
->CurrPtr
->State
&= VAR_DELETED
;
712 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
713 Status
= EFI_SUCCESS
;
718 // If the variable is marked valid and the same data has been passed in
719 // then return to the caller immediately.
721 if (Variable
->CurrPtr
->DataSize
== DataSize
&&
722 CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0
724 Status
= EFI_SUCCESS
;
726 } else if (Variable
->CurrPtr
->State
== VAR_ADDED
) {
728 // Mark the old variable as in delete transition
730 Variable
->CurrPtr
->State
&= VAR_IN_DELETED_TRANSITION
;
735 // No found existing variable, Create a new variable
739 // Make sure we are trying to create a new variable.
740 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
742 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
743 Status
= EFI_NOT_FOUND
;
748 // Only variable have NV|RT attribute can be created in Runtime
750 if (EfiAtRuntime () &&
751 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
752 Status
= EFI_INVALID_PARAMETER
;
758 // Function part - create a new variable and copy the data.
759 // Both update a variable and create a variable will come here.
762 VarNameOffset
= sizeof (VARIABLE_HEADER
);
763 VarNameSize
= StrSize (VariableName
);
764 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
765 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
767 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
768 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(Global
->NonVolatileVariableBase
))->Size
;
769 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
770 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > FixedPcdGet32(PcdHwErrStorageSize
)))
771 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
772 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - FixedPcdGet32(PcdHwErrStorageSize
)))) {
773 Status
= EFI_OUT_OF_RESOURCES
;
777 NextVariable
= (VARIABLE_HEADER
*) (UINT8
*) (mVariableModuleGlobal
->NonVolatileLastVariableOffset
778 + (UINTN
) Global
->NonVolatileVariableBase
);
779 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= VarSize
;
781 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
782 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VarSize
;
784 mVariableModuleGlobal
->CommonVariableTotalSize
+= VarSize
;
787 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
788 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (Global
->VolatileVariableBase
)))->Size
790 Status
= EFI_OUT_OF_RESOURCES
;
794 NextVariable
= (VARIABLE_HEADER
*) (UINT8
*) (mVariableModuleGlobal
->VolatileLastVariableOffset
795 + (UINTN
) Global
->VolatileVariableBase
);
796 mVariableModuleGlobal
->VolatileLastVariableOffset
+= VarSize
;
799 NextVariable
->StartId
= VARIABLE_DATA
;
800 NextVariable
->Attributes
= Attributes
;
801 NextVariable
->State
= VAR_ADDED
;
802 NextVariable
->Reserved
= 0;
805 // There will be pad bytes after Data, the NextVariable->NameSize and
806 // NextVariable->NameSize should not include pad size so that variable
807 // service can get actual size in GetVariable
809 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
810 NextVariable
->DataSize
= (UINT32
)DataSize
;
812 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
814 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
819 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
825 // Mark the old variable as deleted
827 if (Variable
->CurrPtr
!= NULL
) {
828 Variable
->CurrPtr
->State
&= VAR_DELETED
;
831 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
833 Status
= EFI_SUCCESS
;
840 Finds variable in storage blocks of volatile and non-volatile storage areas.
842 This code finds variable in storage blocks of volatile and non-volatile storage areas.
843 If VariableName is an empty string, then we just return the first
844 qualified variable without comparing VariableName and VendorGuid.
845 Otherwise, VariableName and VendorGuid are compared.
847 @param VariableName Name of the variable to be found.
848 @param VendorGuid Vendor GUID to be found.
849 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
850 including the range searched and the target position.
851 @param Global Pointer to VARIABLE_GLOBAL structure, including
852 base of volatile variable storage area, base of
853 NV variable storage area, and a lock.
855 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
857 @retval EFI_SUCCESS Variable successfully found.
858 @retval EFI_NOT_FOUND Variable not found.
863 IN CHAR16
*VariableName
,
864 IN EFI_GUID
*VendorGuid
,
865 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
866 IN VARIABLE_GLOBAL
*Global
869 VARIABLE_HEADER
*Variable
[2];
870 VARIABLE_STORE_HEADER
*VariableStoreHeader
[2];
874 // 0: Non-Volatile, 1: Volatile
876 VariableStoreHeader
[0] = (VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->NonVolatileVariableBase
);
877 VariableStoreHeader
[1] = (VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->VolatileVariableBase
);
880 // Start Pointers for the variable.
881 // Actual Data Pointer where data can be written.
883 Variable
[0] = (VARIABLE_HEADER
*) (VariableStoreHeader
[0] + 1);
884 Variable
[1] = (VARIABLE_HEADER
*) (VariableStoreHeader
[1] + 1);
886 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
887 return EFI_INVALID_PARAMETER
;
890 // Find the variable by walk through non-volatile and volatile variable store
892 for (Index
= 0; Index
< 2; Index
++) {
893 PtrTrack
->StartPtr
= (VARIABLE_HEADER
*) (VariableStoreHeader
[Index
] + 1);
894 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
896 while ((Variable
[Index
] < GetEndPointer (VariableStoreHeader
[Index
])) && (Variable
[Index
] != NULL
)) {
897 if (Variable
[Index
]->StartId
== VARIABLE_DATA
&& Variable
[Index
]->State
== VAR_ADDED
) {
898 if (!(EfiAtRuntime () && ((Variable
[Index
]->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0))) {
899 if (VariableName
[0] == 0) {
900 PtrTrack
->CurrPtr
= Variable
[Index
];
901 PtrTrack
->Volatile
= (BOOLEAN
) Index
;
904 if (CompareGuid (VendorGuid
, &Variable
[Index
]->VendorGuid
)) {
905 if (CompareMem (VariableName
, GET_VARIABLE_NAME_PTR (Variable
[Index
]), Variable
[Index
]->NameSize
) == 0) {
906 PtrTrack
->CurrPtr
= Variable
[Index
];
907 PtrTrack
->Volatile
= (BOOLEAN
) Index
;
915 Variable
[Index
] = GetNextVariablePtr (Variable
[Index
]);
918 PtrTrack
->CurrPtr
= NULL
;
919 return EFI_NOT_FOUND
;
923 This code finds variable in storage blocks (Volatile or Non-Volatile).
925 @param VariableName A Null-terminated Unicode string that is the name of
926 the vendor's variable.
927 @param VendorGuid A unique identifier for the vendor.
928 @param Attributes If not NULL, a pointer to the memory location to return the
929 attributes bitmask for the variable.
930 @param DataSize Size of Data found. If size is less than the
931 data, this value contains the required size.
932 @param Data On input, the size in bytes of the return Data buffer.
933 On output, the size of data returned in Data.
934 @param Global Pointer to VARIABLE_GLOBAL structure
936 @retval EFI_SUCCESS The function completed successfully.
937 @retval EFI_NOT_FOUND The variable was not found.
938 @retval EFI_BUFFER_TOO_SMALL DataSize is too small for the result. DataSize has
939 been updated with the size needed to complete the request.
940 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid or DataSize is NULL.
946 IN CHAR16
*VariableName
,
947 IN EFI_GUID
*VendorGuid
,
948 OUT UINT32
*Attributes OPTIONAL
,
949 IN OUT UINTN
*DataSize
,
951 IN VARIABLE_GLOBAL
*Global
954 VARIABLE_POINTER_TRACK Variable
;
958 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
959 return EFI_INVALID_PARAMETER
;
962 AcquireLockOnlyAtBootTime(&Global
->VariableServicesLock
);
965 // Find existing variable
967 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, Global
);
969 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
975 VarDataSize
= Variable
.CurrPtr
->DataSize
;
976 if (*DataSize
>= VarDataSize
) {
978 Status
= EFI_INVALID_PARAMETER
;
982 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
983 if (Attributes
!= NULL
) {
984 *Attributes
= Variable
.CurrPtr
->Attributes
;
987 *DataSize
= VarDataSize
;
988 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
989 Status
= EFI_SUCCESS
;
992 *DataSize
= VarDataSize
;
993 Status
= EFI_BUFFER_TOO_SMALL
;
998 ReleaseLockOnlyAtBootTime (&Global
->VariableServicesLock
);
1004 This code Finds the Next available variable.
1006 @param VariableNameSize Size of the variable.
1007 @param VariableName On input, supplies the last VariableName that was returned by GetNextVariableName().
1008 On output, returns the Null-terminated Unicode string of the current variable.
1009 @param VendorGuid On input, supplies the last VendorGuid that was returned by GetNextVariableName().
1010 On output, returns the VendorGuid of the current variable.
1011 @param Global Pointer to VARIABLE_GLOBAL structure.
1013 @retval EFI_SUCCESS The function completed successfully.
1014 @retval EFI_NOT_FOUND The next variable was not found.
1015 @retval EFI_BUFFER_TOO_SMALL VariableNameSize is too small for the result.
1016 VariableNameSize has been updated with the size needed to complete the request.
1017 @retval EFI_INVALID_PARAMETER VariableNameSize or VariableName or VendorGuid is NULL.
1022 EmuGetNextVariableName (
1023 IN OUT UINTN
*VariableNameSize
,
1024 IN OUT CHAR16
*VariableName
,
1025 IN OUT EFI_GUID
*VendorGuid
,
1026 IN VARIABLE_GLOBAL
*Global
1029 VARIABLE_POINTER_TRACK Variable
;
1033 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
1034 return EFI_INVALID_PARAMETER
;
1037 AcquireLockOnlyAtBootTime(&Global
->VariableServicesLock
);
1039 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, Global
);
1041 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1046 if (VariableName
[0] != 0) {
1048 // If variable name is not NULL, get next variable
1050 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1053 // If both volatile and non-volatile variable store are parsed,
1056 if (Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
) {
1057 Variable
.Volatile
= (BOOLEAN
) (Variable
.Volatile
^ ((BOOLEAN
) 0x1));
1058 if (Variable
.Volatile
) {
1059 Variable
.StartPtr
= (VARIABLE_HEADER
*) ((UINTN
) (Global
->VolatileVariableBase
+ sizeof (VARIABLE_STORE_HEADER
)));
1060 Variable
.EndPtr
= (VARIABLE_HEADER
*) GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->VolatileVariableBase
));
1062 Status
= EFI_NOT_FOUND
;
1066 Variable
.CurrPtr
= Variable
.StartPtr
;
1067 if (Variable
.CurrPtr
->StartId
!= VARIABLE_DATA
) {
1072 // Variable is found
1074 if (Variable
.CurrPtr
->StartId
== VARIABLE_DATA
&& Variable
.CurrPtr
->State
== VAR_ADDED
) {
1075 if (!(EfiAtRuntime () && ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0))) {
1076 VarNameSize
= Variable
.CurrPtr
->NameSize
;
1077 if (VarNameSize
<= *VariableNameSize
) {
1080 GET_VARIABLE_NAME_PTR (Variable
.CurrPtr
),
1085 &Variable
.CurrPtr
->VendorGuid
,
1088 Status
= EFI_SUCCESS
;
1090 Status
= EFI_BUFFER_TOO_SMALL
;
1093 *VariableNameSize
= VarNameSize
;
1100 ReleaseLockOnlyAtBootTime (&Global
->VariableServicesLock
);
1107 This code sets variable in storage blocks (Volatile or Non-Volatile).
1109 @param VariableName A Null-terminated Unicode string that is the name of the vendor's
1110 variable. Each VariableName is unique for each
1111 VendorGuid. VariableName must contain 1 or more
1112 Unicode characters. If VariableName is an empty Unicode
1113 string, then EFI_INVALID_PARAMETER is returned.
1114 @param VendorGuid A unique identifier for the vendor
1115 @param Attributes Attributes bitmask to set for the variable
1116 @param DataSize The size in bytes of the Data buffer. A size of zero causes the
1117 variable to be deleted.
1118 @param Data The contents for the variable
1119 @param Global Pointer to VARIABLE_GLOBAL structure
1120 @param VolatileOffset The offset of last volatile variable
1121 @param NonVolatileOffset The offset of last non-volatile variable
1123 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
1124 defined by the Attributes.
1125 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied, or the
1126 DataSize exceeds the maximum allowed, or VariableName is an empty
1127 Unicode string, or VendorGuid is NULL.
1128 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
1129 @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.
1130 @retval EFI_WRITE_PROTECTED The variable in question is read-only or cannot be deleted.
1131 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
1137 IN CHAR16
*VariableName
,
1138 IN EFI_GUID
*VendorGuid
,
1139 IN UINT32 Attributes
,
1142 IN VARIABLE_GLOBAL
*Global
,
1143 IN UINTN
*VolatileOffset
,
1144 IN UINTN
*NonVolatileOffset
1147 VARIABLE_POINTER_TRACK Variable
;
1151 // Check input parameters
1153 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
1154 return EFI_INVALID_PARAMETER
;
1157 if (DataSize
!= 0 && Data
== NULL
) {
1158 return EFI_INVALID_PARAMETER
;
1162 // Make sure if runtime bit is set, boot service bit is set also
1164 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1165 return EFI_INVALID_PARAMETER
;
1168 // The size of the VariableName, including the Unicode Null in bytes plus
1169 // the DataSize is limited to maximum size of FixedPcdGet32(PcdMaxHardwareErrorVariableSize)
1170 // bytes for HwErrRec, and FixedPcdGet32(PcdMaxVariableSize) bytes for the others.
1172 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1173 if ((DataSize
> FixedPcdGet32(PcdMaxHardwareErrorVariableSize
)) ||
1174 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> FixedPcdGet32(PcdMaxHardwareErrorVariableSize
))) {
1175 return EFI_INVALID_PARAMETER
;
1178 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"
1180 if (StrnCmp(VariableName
, L
"HwErrRec", StrLen(L
"HwErrRec")) != 0) {
1181 return EFI_INVALID_PARAMETER
;
1185 // The size of the VariableName, including the Unicode Null in bytes plus
1186 // the DataSize is limited to maximum size of FixedPcdGet32(PcdMaxVariableSize) bytes.
1188 if ((DataSize
> FixedPcdGet32(PcdMaxVariableSize
)) ||
1189 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> FixedPcdGet32(PcdMaxVariableSize
))) {
1190 return EFI_INVALID_PARAMETER
;
1194 AcquireLockOnlyAtBootTime(&Global
->VariableServicesLock
);
1197 // Check whether the input variable is already existed
1200 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, Global
);
1203 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang
1205 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
1207 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, &Variable
);
1209 ReleaseLockOnlyAtBootTime (&Global
->VariableServicesLock
);
1215 This code returns information about the EFI variables.
1217 @param Attributes Attributes bitmask to specify the type of variables
1218 on which to return information.
1219 @param MaximumVariableStorageSize On output the maximum size of the storage space available for
1220 the EFI variables associated with the attributes specified.
1221 @param RemainingVariableStorageSize Returns the remaining size of the storage space available for EFI
1222 variables associated with the attributes specified.
1223 @param MaximumVariableSize Returns the maximum size of an individual EFI variable
1224 associated with the attributes specified.
1225 @param Global Pointer to VARIABLE_GLOBAL structure.
1227 @retval EFI_SUCCESS Valid answer returned.
1228 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied
1229 @retval EFI_UNSUPPORTED The attribute is not supported on this platform, and the
1230 MaximumVariableStorageSize, RemainingVariableStorageSize,
1231 MaximumVariableSize are undefined.
1236 EmuQueryVariableInfo (
1237 IN UINT32 Attributes
,
1238 OUT UINT64
*MaximumVariableStorageSize
,
1239 OUT UINT64
*RemainingVariableStorageSize
,
1240 OUT UINT64
*MaximumVariableSize
,
1241 IN VARIABLE_GLOBAL
*Global
1244 VARIABLE_HEADER
*Variable
;
1245 VARIABLE_HEADER
*NextVariable
;
1246 UINT64 VariableSize
;
1247 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1248 UINT64 CommonVariableTotalSize
;
1249 UINT64 HwErrVariableTotalSize
;
1251 CommonVariableTotalSize
= 0;
1252 HwErrVariableTotalSize
= 0;
1254 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
1255 return EFI_INVALID_PARAMETER
;
1258 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
1260 // Make sure the Attributes combination is supported by the platform.
1262 return EFI_UNSUPPORTED
;
1263 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1265 // Make sure if runtime bit is set, boot service bit is set also.
1267 return EFI_INVALID_PARAMETER
;
1268 } else if (EfiAtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
1270 // Make sure RT Attribute is set if we are in Runtime phase.
1272 return EFI_INVALID_PARAMETER
;
1273 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1275 // Make sure Hw Attribute is set with NV.
1277 return EFI_INVALID_PARAMETER
;
1280 AcquireLockOnlyAtBootTime(&Global
->VariableServicesLock
);
1282 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1284 // Query is Volatile related.
1286 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->VolatileVariableBase
);
1289 // Query is Non-Volatile related.
1291 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->NonVolatileVariableBase
);
1295 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1296 // with the storage size (excluding the storage header size)
1298 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1301 // Harware error record variable needs larger size.
1303 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
1304 *MaximumVariableStorageSize
= FixedPcdGet32(PcdHwErrStorageSize
);
1305 *MaximumVariableSize
= FixedPcdGet32(PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
1307 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1308 ASSERT (FixedPcdGet32(PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
1309 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - FixedPcdGet32(PcdHwErrStorageSize
);
1313 // Let *MaximumVariableSize be FixedPcdGet32(PcdMaxVariableSize) with the exception of the variable header size.
1315 *MaximumVariableSize
= FixedPcdGet32(PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
1319 // Point to the starting address of the variables.
1321 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
1324 // Now walk through the related variable store.
1326 while (Variable
< GetEndPointer (VariableStoreHeader
)) {
1327 NextVariable
= GetNextVariablePtr(Variable
);
1328 if (NextVariable
== NULL
) {
1331 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
1333 if ((NextVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1334 HwErrVariableTotalSize
+= VariableSize
;
1336 CommonVariableTotalSize
+= VariableSize
;
1340 // Go to the next one.
1342 Variable
= NextVariable
;
1345 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
1346 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
1348 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
1351 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
1352 *MaximumVariableSize
= 0;
1353 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
1354 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
1357 ReleaseLockOnlyAtBootTime (&Global
->VariableServicesLock
);
1362 Initializes variable store area.
1364 This function allocates memory space for variable store area and initializes its attributes.
1366 @param VolatileStore Indicates if the variable store is volatile.
1370 InitializeVariableStore (
1371 IN BOOLEAN VolatileStore
1374 VARIABLE_STORE_HEADER
*VariableStore
;
1375 BOOLEAN FullyInitializeStore
;
1376 EFI_PHYSICAL_ADDRESS
*VariableBase
;
1377 UINTN
*LastVariableOffset
;
1379 FullyInitializeStore
= TRUE
;
1381 if (VolatileStore
) {
1382 VariableBase
= &mVariableModuleGlobal
->VariableGlobal
[Physical
].VolatileVariableBase
;
1383 LastVariableOffset
= &mVariableModuleGlobal
->VolatileLastVariableOffset
;
1385 VariableBase
= &mVariableModuleGlobal
->VariableGlobal
[Physical
].NonVolatileVariableBase
;
1386 LastVariableOffset
= &mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1390 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
1391 // is stored with common variable in the same NV region. So the platform integrator should
1392 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
1393 // PcdVariableStoreSize.
1395 ASSERT (FixedPcdGet32(PcdHwErrStorageSize
) <= FixedPcdGet32(PcdVariableStoreSize
));
1398 // Allocate memory for variable store.
1400 if (VolatileStore
|| (PcdGet64 (PcdEmuVariableNvStoreReserved
) == 0)) {
1401 VariableStore
= (VARIABLE_STORE_HEADER
*) AllocateRuntimePool (
1402 FixedPcdGet32(PcdVariableStoreSize
)
1406 // A memory location has been reserved for the NV variable store. Certain
1407 // platforms may be able to preserve a memory range across system resets,
1408 // thereby providing better NV variable emulation.
1411 (VARIABLE_STORE_HEADER
*)(VOID
*)(UINTN
)
1412 PcdGet64 (PcdEmuVariableNvStoreReserved
);
1414 (VariableStore
->Size
== FixedPcdGet32(PcdVariableStoreSize
)) &&
1415 (VariableStore
->Format
== VARIABLE_STORE_FORMATTED
) &&
1416 (VariableStore
->State
== VARIABLE_STORE_HEALTHY
)
1420 "Variable Store reserved at %p appears to be valid\n",
1423 FullyInitializeStore
= FALSE
;
1427 if (NULL
== VariableStore
) {
1428 return EFI_OUT_OF_RESOURCES
;
1431 if (FullyInitializeStore
) {
1432 SetMem (VariableStore
, FixedPcdGet32(PcdVariableStoreSize
), 0xff);
1436 // Variable Specific Data
1438 *VariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStore
;
1439 InitializeLocationForLastVariableOffset (VariableStore
, LastVariableOffset
);
1441 CopyGuid (&VariableStore
->Signature
, &gEfiVariableGuid
);
1442 VariableStore
->Size
= FixedPcdGet32(PcdVariableStoreSize
);
1443 VariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
1444 VariableStore
->State
= VARIABLE_STORE_HEALTHY
;
1445 VariableStore
->Reserved
= 0;
1446 VariableStore
->Reserved1
= 0;
1452 Initializes variable store area for non-volatile and volatile variable.
1454 This function allocates and initializes memory space for global context of ESAL
1455 variable service and variable store area for non-volatile and volatile variable.
1457 @param ImageHandle The Image handle of this driver.
1458 @param SystemTable The pointer of EFI_SYSTEM_TABLE.
1460 @retval EFI_SUCCESS Function successfully executed.
1461 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
1466 VariableCommonInitialize (
1467 IN EFI_HANDLE ImageHandle
,
1468 IN EFI_SYSTEM_TABLE
*SystemTable
1474 // Allocate memory for mVariableModuleGlobal
1476 mVariableModuleGlobal
= (ESAL_VARIABLE_GLOBAL
*) AllocateRuntimeZeroPool (
1477 sizeof (ESAL_VARIABLE_GLOBAL
)
1479 if (NULL
== mVariableModuleGlobal
) {
1480 return EFI_OUT_OF_RESOURCES
;
1483 EfiInitializeLock(&mVariableModuleGlobal
->VariableGlobal
[Physical
].VariableServicesLock
, TPL_NOTIFY
);
1486 // Intialize volatile variable store
1488 Status
= InitializeVariableStore (TRUE
);
1489 if (EFI_ERROR (Status
)) {
1490 FreePool(mVariableModuleGlobal
);
1494 // Intialize non volatile variable store
1496 Status
= InitializeVariableStore (FALSE
);