3 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Provide support functions for variable services.
22 #include "FSVariable.h"
24 VARIABLE_STORE_HEADER mStoreHeaderTemplate
= {
25 VARIABLE_STORE_SIGNATURE
,
26 VOLATILE_VARIABLE_STORE_SIZE
,
27 VARIABLE_STORE_FORMATTED
,
28 VARIABLE_STORE_HEALTHY
,
34 // Don't use module globals after the SetVirtualAddress map is signaled
36 VARIABLE_GLOBAL
*mGlobal
;
39 Update the variable region with Variable information. These are the same
40 arguments as the EFI Variable services.
42 @param[in] VariableName Name of variable
44 @param[in] VendorGuid Guid of variable
46 @param[in] Data Variable data
48 @param[in] DataSize Size of data. 0 means delete
50 @param[in] Attributes Attribues of the variable
52 @param[in] Variable The variable information which is used to keep track of variable usage.
54 @retval EFI_SUCCESS The update operation is success.
56 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
62 IN CHAR16
*VariableName
,
63 IN EFI_GUID
*VendorGuid
,
66 IN UINT32 Attributes OPTIONAL
,
67 IN VARIABLE_POINTER_TRACK
*Variable
72 OnVirtualAddressChangeFsv (
79 OnSimpleFileSystemInstall (
85 IsValidVariableHeader (
86 IN VARIABLE_HEADER
*Variable
92 This code checks if variable header is valid or not.
95 Variable Pointer to the Variable Header.
98 TRUE Variable header is valid.
99 FALSE Variable header is not valid.
103 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
110 VARIABLE_STORE_STATUS
111 GetVariableStoreStatus (
112 IN VARIABLE_STORE_HEADER
*VarStoreHeader
118 This code gets the current status of Variable Store.
122 VarStoreHeader Pointer to the Variable Store Header.
126 EfiRaw Variable store status is raw
127 EfiValid Variable store status is valid
128 EfiInvalid Variable store status is invalid
132 if (CompareGuid (&VarStoreHeader
->Signature
, &mStoreHeaderTemplate
.Signature
) &&
133 (VarStoreHeader
->Format
== mStoreHeaderTemplate
.Format
) &&
134 (VarStoreHeader
->State
== mStoreHeaderTemplate
.State
)
137 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == VAR_DEFAULT_VALUE_32
&&
138 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == VAR_DEFAULT_VALUE_32
&&
139 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == VAR_DEFAULT_VALUE_32
&&
140 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == VAR_DEFAULT_VALUE_32
&&
141 VarStoreHeader
->Size
== VAR_DEFAULT_VALUE_32
&&
142 VarStoreHeader
->Format
== VAR_DEFAULT_VALUE
&&
143 VarStoreHeader
->State
== VAR_DEFAULT_VALUE
154 IN VARIABLE_HEADER
*Variable
160 This code gets the pointer to the variable data.
164 Variable Pointer to the Variable Header.
168 UINT8* Pointer to Variable Data
173 // Be careful about pad size for alignment
175 return (UINT8
*) ((UINTN
) GET_VARIABLE_NAME_PTR (Variable
) + Variable
->NameSize
+ GET_PAD_SIZE (Variable
->NameSize
));
180 IN VARIABLE_HEADER
*Variable
186 This code gets the pointer to the next variable header.
190 Variable Pointer to the Variable Header.
194 VARIABLE_HEADER* Pointer to next variable header.
198 if (!IsValidVariableHeader (Variable
)) {
202 // Be careful about pad size for alignment
204 return (VARIABLE_HEADER
*) ((UINTN
) GetVariableDataPtr (Variable
) + Variable
->DataSize
+ GET_PAD_SIZE (Variable
->DataSize
));
209 IN VARIABLE_STORE_HEADER
*VarStoreHeader
215 This code gets the pointer to the last variable memory pointer byte
219 VarStoreHeader Pointer to the Variable Store Header.
223 VARIABLE_HEADER* Pointer to last unavailable Variable Header
228 // The end of variable store
230 return (VARIABLE_HEADER
*) ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
235 IN VARIABLE_HEADER
*Variable
241 Check if exist newer variable when doing reclaim
245 Variable Pointer to start position
249 TRUE - Exists another variable, which is newer than the current one
250 FALSE - Doesn't exist another vairable which is newer than the current one
254 VARIABLE_HEADER
*NextVariable
;
255 CHAR16
*VariableName
;
256 EFI_GUID
*VendorGuid
;
258 VendorGuid
= &Variable
->VendorGuid
;
259 VariableName
= GET_VARIABLE_NAME_PTR(Variable
);
261 NextVariable
= GetNextVariablePtr (Variable
);
262 while (IsValidVariableHeader (NextVariable
)) {
263 if ((NextVariable
->State
== VAR_ADDED
) || (NextVariable
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
265 // If match Guid and Name
267 if (CompareGuid (VendorGuid
, &NextVariable
->VendorGuid
)) {
268 if (CompareMem (VariableName
, GET_VARIABLE_NAME_PTR (NextVariable
), StrSize (VariableName
)) == 0) {
273 NextVariable
= GetNextVariablePtr (NextVariable
);
280 IN VARIABLE_STORAGE_TYPE StorageType
,
281 IN VARIABLE_HEADER
*CurrentVariable OPTIONAL
287 Variable store garbage collection and reclaim operation
291 IsVolatile The variable store is volatile or not,
292 if it is non-volatile, need FTW
293 CurrentVairable If it is not NULL, it means not to process
294 current variable for Reclaim.
302 VARIABLE_HEADER
*Variable
;
303 VARIABLE_HEADER
*NextVariable
;
304 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
306 UINTN ValidBufferSize
;
311 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[StorageType
];
314 // Start Pointers for the variable.
316 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
319 // recaluate the total size of Common/HwErr type variables in non-volatile area.
322 mGlobal
->CommonVariableTotalSize
= 0;
323 mGlobal
->HwErrVariableTotalSize
= 0;
326 // To make the reclaim, here we just allocate a memory that equal to the original memory
328 ValidBufferSize
= sizeof (VARIABLE_STORE_HEADER
) + VariableStoreHeader
->Size
;
330 Status
= gBS
->AllocatePool (
333 (VOID
**) &ValidBuffer
335 if (EFI_ERROR (Status
)) {
339 CurrPtr
= ValidBuffer
;
342 // Copy variable store header
344 CopyMem (CurrPtr
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
345 CurrPtr
+= sizeof (VARIABLE_STORE_HEADER
);
348 // Start Pointers for the variable.
350 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
353 ValidBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
354 while (IsValidVariableHeader (Variable
)) {
355 NextVariable
= GetNextVariablePtr (Variable
);
357 // State VAR_ADDED or VAR_IN_DELETED_TRANSITION are to kept,
358 // The CurrentVariable, is also saved, as SetVariable may fail due to lack of space
360 if (Variable
->State
== VAR_ADDED
) {
361 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
362 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
363 ValidBufferSize
+= VariableSize
;
364 CurrPtr
+= VariableSize
;
365 if ((!StorageType
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
366 mGlobal
->HwErrVariableTotalSize
+= VariableSize
;
367 } else if ((!StorageType
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
368 mGlobal
->CommonVariableTotalSize
+= VariableSize
;
370 } else if (Variable
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
)) {
372 // As variables that with the same guid and name may exist in NV due to power failure during SetVariable,
373 // we will only save the latest valid one
375 if (!ExistNewerVariable(Variable
)) {
376 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
377 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
379 // If CurrentVariable == Variable, mark as VAR_IN_DELETED_TRANSITION
381 if (Variable
!= CurrentVariable
){
382 ((VARIABLE_HEADER
*)CurrPtr
)->State
= VAR_ADDED
;
384 CurrPtr
+= VariableSize
;
385 ValidBufferSize
+= VariableSize
;
386 if ((!StorageType
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
387 mGlobal
->HwErrVariableTotalSize
+= VariableSize
;
388 } else if ((!StorageType
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
389 mGlobal
->CommonVariableTotalSize
+= VariableSize
;
393 Variable
= NextVariable
;
396 mGlobal
->LastVariableOffset
[StorageType
] = ValidBufferSize
;
399 // TODO: cannot restore to original state, basic FTW needed
401 Status
= mGlobal
->VariableStore
[StorageType
]->Erase (
402 mGlobal
->VariableStore
[StorageType
]
404 Status
= mGlobal
->VariableStore
[StorageType
]->Write (
405 mGlobal
->VariableStore
[StorageType
],
411 if (EFI_ERROR (Status
)) {
413 // If error, then reset the last variable offset to zero.
415 mGlobal
->LastVariableOffset
[StorageType
] = 0;
418 gBS
->FreePool (ValidBuffer
);
425 IN CHAR16
*VariableName
,
426 IN EFI_GUID
*VendorGuid
,
427 OUT VARIABLE_POINTER_TRACK
*PtrTrack
433 This code finds variable in storage blocks (Volatile or Non-Volatile)
437 VariableName Name of the variable to be found
438 VendorGuid Vendor GUID to be found.
439 PtrTrack Variable Track Pointer structure that contains
440 Variable Information.
441 Contains the pointer of Variable header.
445 EFI_INVALID_PARAMETER - Invalid parameter
446 EFI_SUCCESS - Find the specified variable
447 EFI_NOT_FOUND - Not found
451 VARIABLE_HEADER
*Variable
;
452 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
454 VARIABLE_HEADER
*InDeleteVariable
;
456 VARIABLE_HEADER
*InDeleteStartPtr
;
457 VARIABLE_HEADER
*InDeleteEndPtr
;
459 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
460 return EFI_INVALID_PARAMETER
;
463 InDeleteVariable
= NULL
;
464 InDeleteIndex
= (UINTN
)-1;
465 InDeleteStartPtr
= NULL
;
466 InDeleteEndPtr
= NULL
;
468 for (Index
= 0; Index
< MaxType
; Index
++) {
470 // 0: Non-Volatile, 1: Volatile
472 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[Index
];
475 // Start Pointers for the variable.
476 // Actual Data Pointer where data can be written.
478 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
481 // Find the variable by walk through non-volatile and volatile variable store
483 PtrTrack
->StartPtr
= Variable
;
484 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
);
486 while ((Variable
< PtrTrack
->EndPtr
) && IsValidVariableHeader (Variable
)) {
487 if (Variable
->State
== VAR_ADDED
) {
488 if (!EfiAtRuntime () || (Variable
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
489 if (VariableName
[0] == 0) {
490 PtrTrack
->CurrPtr
= Variable
;
491 PtrTrack
->Type
= (VARIABLE_STORAGE_TYPE
) Index
;
494 if (CompareGuid (VendorGuid
, &Variable
->VendorGuid
)) {
495 if (!CompareMem (VariableName
, GET_VARIABLE_NAME_PTR (Variable
), StrSize (VariableName
))) {
496 PtrTrack
->CurrPtr
= Variable
;
497 PtrTrack
->Type
= (VARIABLE_STORAGE_TYPE
) Index
;
503 } else if (Variable
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
)) {
505 // VAR_IN_DELETED_TRANSITION should also be checked.
507 if (!EfiAtRuntime () || (Variable
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
508 if (VariableName
[0] == 0) {
509 InDeleteVariable
= Variable
;
510 InDeleteIndex
= Index
;
511 InDeleteStartPtr
= PtrTrack
->StartPtr
;
512 InDeleteEndPtr
= PtrTrack
->EndPtr
;
514 if (CompareGuid (VendorGuid
, &Variable
->VendorGuid
)) {
515 if (!CompareMem (VariableName
, GET_VARIABLE_NAME_PTR (Variable
), StrSize (VariableName
))) {
516 InDeleteVariable
= Variable
;
517 InDeleteIndex
= Index
;
518 InDeleteStartPtr
= PtrTrack
->StartPtr
;
519 InDeleteEndPtr
= PtrTrack
->EndPtr
;
526 Variable
= GetNextVariablePtr (Variable
);
537 // if VAR_IN_DELETED_TRANSITION found, and VAR_ADDED not found,
540 if (InDeleteVariable
!= NULL
) {
541 PtrTrack
->CurrPtr
= InDeleteVariable
;
542 PtrTrack
->Type
= (VARIABLE_STORAGE_TYPE
) InDeleteIndex
;
543 PtrTrack
->StartPtr
= InDeleteStartPtr
;
544 PtrTrack
->EndPtr
= InDeleteEndPtr
;
548 PtrTrack
->CurrPtr
= NULL
;
549 return EFI_NOT_FOUND
;
553 Get index from supported language codes according to language string.
555 This code is used to get corresponding index in supported language codes. It can handle
556 RFC4646 and ISO639 language tags.
557 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
558 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
561 SupportedLang = "engfraengfra"
563 Iso639Language = TRUE
564 The return value is "0".
566 SupportedLang = "en;fr;en-US;fr-FR"
568 Iso639Language = FALSE
569 The return value is "3".
571 @param SupportedLang Platform supported language codes.
572 @param Lang Configured language.
573 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
575 @retval the index of language in the language codes.
580 GetIndexFromSupportedLangCodes(
581 IN CHAR8
*SupportedLang
,
583 IN BOOLEAN Iso639Language
587 UINT32 CompareLength
;
591 Supported
= SupportedLang
;
592 if (Iso639Language
) {
594 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
595 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
597 // Successfully find the index of Lang string in SupportedLang string.
599 Index
= Index
/ CompareLength
;
607 // Compare RFC4646 language code
609 while (*Supported
!= '\0') {
611 // take semicolon as delimitation, sequentially traverse supported language codes.
613 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
616 if (AsciiStrnCmp (Lang
, Supported
- CompareLength
, CompareLength
) == 0) {
618 // Successfully find the index of Lang string in SupportedLang string.
630 Get language string from supported language codes according to index.
632 This code is used to get corresponding language string in supported language codes. It can handle
633 RFC4646 and ISO639 language tags.
634 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
635 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
638 SupportedLang = "engfraengfra"
640 Iso639Language = TRUE
641 The return value is "fra".
643 SupportedLang = "en;fr;en-US;fr-FR"
645 Iso639Language = FALSE
646 The return value is "fr".
648 @param SupportedLang Platform supported language codes.
649 @param Index the index in supported language codes.
650 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
652 @retval the language string in the language codes.
657 GetLangFromSupportedLangCodes (
658 IN CHAR8
*SupportedLang
,
660 IN BOOLEAN Iso639Language
664 UINT32 CompareLength
;
668 Supported
= SupportedLang
;
669 if (Iso639Language
) {
671 // according to the index of Lang string in SupportedLang string to get the language.
672 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
673 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
676 SetMem (mGlobal
->Lang
, sizeof(mGlobal
->Lang
), 0);
677 return CopyMem (mGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
682 // take semicolon as delimitation, sequentially traverse supported language codes.
684 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
687 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
689 // Have completed the traverse, but not find corrsponding string.
690 // This case is not allowed to happen.
695 if (SubIndex
== Index
) {
697 // according to the index of Lang string in SupportedLang string to get the language.
698 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
699 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
701 SetMem (mGlobal
->PlatformLang
, sizeof (mGlobal
->PlatformLang
), 0);
702 return CopyMem (mGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
710 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
712 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
714 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
715 and are read-only. Therefore, in variable driver, only store the original value for other use.
717 @param[in] VariableName Name of variable
719 @param[in] Data Variable data
721 @param[in] DataSize Size of data. 0 means delete
723 @retval EFI_SUCCESS auto update operation is successful.
728 AutoUpdateLangVariable(
729 IN CHAR16
*VariableName
,
735 CHAR8
*BestPlatformLang
;
739 VARIABLE_POINTER_TRACK Variable
;
742 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
744 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
746 if (StrCmp (VariableName
, L
"PlatformLangCodes") == 0) {
748 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
749 // Therefore, in variable driver, only store the original value for other use.
751 AsciiStrnCpy (mGlobal
->PlatformLangCodes
, Data
, DataSize
);
752 } else if (StrCmp (VariableName
, L
"LangCodes") == 0) {
754 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
755 // Therefore, in variable driver, only store the original value for other use.
757 AsciiStrnCpy (mGlobal
->LangCodes
, Data
, DataSize
);
758 } else if ((StrCmp (VariableName
, L
"PlatformLang") == 0) && (DataSize
!= 0)) {
759 ASSERT (AsciiStrLen (mGlobal
->PlatformLangCodes
) != 0);
762 // When setting PlatformLang, firstly get most matched language string from supported language codes.
764 BestPlatformLang
= GetBestLanguage(mGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
767 // Get the corresponding index in language codes.
769 Index
= GetIndexFromSupportedLangCodes(mGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
772 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
774 BestLang
= GetLangFromSupportedLangCodes(mGlobal
->LangCodes
, Index
, TRUE
);
777 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
779 FindVariable(L
"Lang", &gEfiGlobalVariableGuid
, &Variable
);
781 Status
= UpdateVariable(L
"Lang", &gEfiGlobalVariableGuid
,
782 BestLang
, ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, &Variable
);
784 DEBUG((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
786 ASSERT_EFI_ERROR(Status
);
788 } else if ((StrCmp (VariableName
, L
"Lang") == 0) && (DataSize
!= 0)) {
789 ASSERT (AsciiStrLen (mGlobal
->LangCodes
) != 0);
792 // When setting Lang, firstly get most matched language string from supported language codes.
794 BestLang
= GetBestLanguage(mGlobal
->LangCodes
, TRUE
, Data
, NULL
);
797 // Get the corresponding index in language codes.
799 Index
= GetIndexFromSupportedLangCodes(mGlobal
->LangCodes
, BestLang
, TRUE
);
802 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
804 BestPlatformLang
= GetLangFromSupportedLangCodes(mGlobal
->PlatformLangCodes
, Index
, FALSE
);
807 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
809 FindVariable(L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
);
811 Status
= UpdateVariable(L
"PlatformLang", &gEfiGlobalVariableGuid
,
812 BestPlatformLang
, AsciiStrSize (BestPlatformLang
), Attributes
, &Variable
);
814 DEBUG((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
815 ASSERT_EFI_ERROR(Status
);
821 Update the variable region with Variable information. These are the same
822 arguments as the EFI Variable services.
824 @param[in] VariableName Name of variable
826 @param[in] VendorGuid Guid of variable
828 @param[in] Data Variable data
830 @param[in] DataSize Size of data. 0 means delete
832 @param[in] Attributes Attribues of the variable
834 @param[in] Variable The variable information which is used to keep track of variable usage.
836 @retval EFI_SUCCESS The update operation is success.
838 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
844 IN CHAR16
*VariableName
,
845 IN EFI_GUID
*VendorGuid
,
848 IN UINT32 Attributes OPTIONAL
,
849 IN VARIABLE_POINTER_TRACK
*Variable
853 VARIABLE_HEADER
*NextVariable
;
860 VARIABLE_STORAGE_TYPE StorageType
;
864 if (Variable
->CurrPtr
!= NULL
) {
866 // Update/Delete existing variable
869 if (EfiAtRuntime ()) {
871 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
872 // the volatile is ReadOnly, and SetVariable should be aborted and
873 // return EFI_WRITE_PROTECTED.
875 if (Variable
->Type
== Volatile
) {
876 return EFI_WRITE_PROTECTED
;
879 // Only variable have NV attribute can be updated/deleted in Runtime
881 if (!(Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
)) {
882 return EFI_INVALID_PARAMETER
;
887 // Setting a data variable with no access, or zero DataSize attributes
888 // specified causes it to be deleted.
890 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
892 // Found this variable in storage
894 State
= Variable
->CurrPtr
->State
;
895 State
&= VAR_DELETED
;
897 Status
= mGlobal
->VariableStore
[Variable
->Type
]->Write (
898 mGlobal
->VariableStore
[Variable
->Type
],
899 VARIABLE_MEMBER_OFFSET (State
, (UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
),
900 sizeof (Variable
->CurrPtr
->State
),
904 // NOTE: Write operation at least can write data to memory cache
905 // Discard file writing failure here.
911 // Found this variable in storage
912 // If the variable is marked valid and the same data has been passed in
913 // then return to the caller immediately.
915 if ((Variable
->CurrPtr
->DataSize
== DataSize
) &&
916 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0)
919 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
920 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
922 // Mark the old variable as in delete transition
924 State
= Variable
->CurrPtr
->State
;
925 State
&= VAR_IN_DELETED_TRANSITION
;
927 Status
= mGlobal
->VariableStore
[Variable
->Type
]->Write (
928 mGlobal
->VariableStore
[Variable
->Type
],
929 VARIABLE_MEMBER_OFFSET (State
, (UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
),
930 sizeof (Variable
->CurrPtr
->State
),
934 // NOTE: Write operation at least can write data to memory cache
935 // Discard file writing failure here.
940 // Create a new variable
944 // Make sure we are trying to create a new variable.
945 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
947 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
948 return EFI_NOT_FOUND
;
951 // Only variable have NV|RT attribute can be created in Runtime
953 if (EfiAtRuntime () &&
954 (!(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) || !(Attributes
& EFI_VARIABLE_NON_VOLATILE
))) {
955 return EFI_INVALID_PARAMETER
;
961 // Function part - create a new variable and copy the data.
962 // Both update a variable and create a variable will come here.
963 // We can firstly write all the data in memory, then write them to file
964 // This can reduce the times of write operation
967 NextVariable
= (VARIABLE_HEADER
*) mGlobal
->Scratch
;
969 NextVariable
->StartId
= VARIABLE_DATA
;
970 NextVariable
->Attributes
= Attributes
;
971 NextVariable
->State
= VAR_ADDED
;
972 NextVariable
->Reserved
= 0;
973 VarNameOffset
= sizeof (VARIABLE_HEADER
);
974 VarNameSize
= StrSize (VariableName
);
976 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
980 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
982 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
986 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
988 // There will be pad bytes after Data, the NextVariable->NameSize and
989 // NextVariable->DataSize should not include pad size so that variable
990 // service can get actual size in GetVariable
992 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
993 NextVariable
->DataSize
= (UINT32
)DataSize
;
996 // The actual size of the variable that stores in storage should
998 // VarDataOffset: offset from begin of current variable header
1000 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1002 StorageType
= (Attributes
& EFI_VARIABLE_NON_VOLATILE
) ? NonVolatile
: Volatile
;
1004 if ((UINT32
) (VarSize
+ mGlobal
->LastVariableOffset
[StorageType
]) >
1005 ((VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[StorageType
])->Size
1007 if ((StorageType
== NonVolatile
) && EfiAtRuntime ()) {
1008 return EFI_OUT_OF_RESOURCES
;
1011 // Perform garbage collection & reclaim operation
1013 Status
= Reclaim (StorageType
, Variable
->CurrPtr
);
1014 if (EFI_ERROR (Status
)) {
1017 // we cannot restore to original state, fetal error, report to user
1019 DEBUG ((EFI_D_ERROR
, "FSVariable: Recalim error (fetal error) - %r\n", Status
));
1023 // If still no enough space, return out of resources
1025 if ((UINT32
) (VarSize
+ mGlobal
->LastVariableOffset
[StorageType
]) >
1026 ((VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[StorageType
])->Size
1028 return EFI_OUT_OF_RESOURCES
;
1033 Status
= mGlobal
->VariableStore
[StorageType
]->Write (
1034 mGlobal
->VariableStore
[StorageType
],
1035 mGlobal
->LastVariableOffset
[StorageType
],
1040 // NOTE: Write operation at least can write data to memory cache
1041 // Discard file writing failure here.
1043 mGlobal
->LastVariableOffset
[StorageType
] += VarSize
;
1045 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
1046 mGlobal
->HwErrVariableTotalSize
+= VarSize
;
1048 mGlobal
->CommonVariableTotalSize
+= VarSize
;
1052 // Mark the old variable as deleted
1054 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
1055 State
= Variable
->CurrPtr
->State
;
1056 State
&= VAR_DELETED
;
1058 Status
= mGlobal
->VariableStore
[StorageType
]->Write (
1059 mGlobal
->VariableStore
[StorageType
],
1060 VARIABLE_MEMBER_OFFSET (State
, (UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
),
1061 sizeof (Variable
->CurrPtr
->State
),
1065 // NOTE: Write operation at least can write data to memory cache
1066 // Discard file writing failure here.
1075 IN CHAR16
*VariableName
,
1076 IN EFI_GUID
*VendorGuid
,
1077 OUT UINT32
*Attributes OPTIONAL
,
1078 IN OUT UINTN
*DataSize
,
1083 Routine Description:
1085 This code finds variable in storage blocks (Volatile or Non-Volatile)
1089 VariableName Name of Variable to be found
1090 VendorGuid Variable vendor GUID
1091 Attributes OPTIONAL Attribute value of the variable found
1092 DataSize Size of Data found. If size is less than the
1093 data, this value contains the required size.
1102 VARIABLE_POINTER_TRACK Variable
;
1106 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
1107 return EFI_INVALID_PARAMETER
;
1111 // Find existing variable
1113 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
);
1115 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1121 VarDataSize
= Variable
.CurrPtr
->DataSize
;
1122 if (*DataSize
>= VarDataSize
) {
1124 return EFI_INVALID_PARAMETER
;
1126 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
1128 if (Attributes
!= NULL
) {
1129 *Attributes
= Variable
.CurrPtr
->Attributes
;
1132 *DataSize
= VarDataSize
;
1136 *DataSize
= VarDataSize
;
1137 return EFI_BUFFER_TOO_SMALL
;
1143 GetNextVariableName (
1144 IN OUT UINTN
*VariableNameSize
,
1145 IN OUT CHAR16
*VariableName
,
1146 IN OUT EFI_GUID
*VendorGuid
1150 Routine Description:
1152 This code Finds the Next available variable
1156 VariableNameSize Size of the variable
1157 VariableName Pointer to variable name
1158 VendorGuid Variable Vendor Guid
1166 VARIABLE_POINTER_TRACK Variable
;
1170 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
1171 return EFI_INVALID_PARAMETER
;
1174 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
);
1176 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1180 if (VariableName
[0] != 0) {
1182 // If variable name is not NULL, get next variable
1184 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1189 // The order we find variable is: 1). NonVolatile; 2). Volatile
1190 // If both volatile and non-volatile variable store are parsed,
1193 if (Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
) {
1194 if (Variable
.Type
== Volatile
) {
1196 // Since we met the end of Volatile storage, we have parsed all the stores.
1198 return EFI_NOT_FOUND
;
1202 // End of NonVolatile, continue to parse Volatile
1204 Variable
.Type
= Volatile
;
1205 Variable
.StartPtr
= (VARIABLE_HEADER
*) ((VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[Volatile
] + 1);
1206 Variable
.EndPtr
= (VARIABLE_HEADER
*) GetEndPointer ((VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[Volatile
]);
1208 Variable
.CurrPtr
= Variable
.StartPtr
;
1209 if (!IsValidVariableHeader (Variable
.CurrPtr
)) {
1214 // Variable is found
1216 if (IsValidVariableHeader (Variable
.CurrPtr
) &&
1217 ((Variable
.CurrPtr
->State
== VAR_ADDED
) ||
1218 (Variable
.CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
)))) {
1219 if (!EfiAtRuntime () || (Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
1220 VarNameSize
= Variable
.CurrPtr
->NameSize
;
1221 if (VarNameSize
<= *VariableNameSize
) {
1224 GET_VARIABLE_NAME_PTR (Variable
.CurrPtr
),
1229 &Variable
.CurrPtr
->VendorGuid
,
1232 Status
= EFI_SUCCESS
;
1234 Status
= EFI_BUFFER_TOO_SMALL
;
1237 *VariableNameSize
= VarNameSize
;
1242 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1249 IN CHAR16
*VariableName
,
1250 IN EFI_GUID
*VendorGuid
,
1251 IN UINT32 Attributes
,
1257 Routine Description:
1259 This code sets variable in storage blocks (Volatile or Non-Volatile)
1263 VariableName Name of Variable to be found
1264 VendorGuid Variable vendor GUID
1265 Attributes Attribute value of the variable found
1266 DataSize Size of Data found. If size is less than the
1267 data, this value contains the required size.
1272 EFI_INVALID_PARAMETER - Invalid parameter
1273 EFI_SUCCESS - Set successfully
1274 EFI_OUT_OF_RESOURCES - Resource not enough to set variable
1275 EFI_NOT_FOUND - Not found
1276 EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure
1277 EFI_WRITE_PROTECTED - Variable is read-only
1281 VARIABLE_POINTER_TRACK Variable
;
1285 // Check input parameters
1287 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
1288 return EFI_INVALID_PARAMETER
;
1291 if (DataSize
!= 0 && Data
== NULL
) {
1292 return EFI_INVALID_PARAMETER
;
1296 // Not support authenticated variable write yet.
1298 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1299 return EFI_INVALID_PARAMETER
;
1303 // Make sure if runtime bit is set, boot service bit is set also
1305 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1306 return EFI_INVALID_PARAMETER
;
1310 // The size of the VariableName, including the Unicode Null in bytes plus
1311 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
1312 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
1314 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1315 if ((DataSize
> PcdGet32(PcdMaxHardwareErrorVariableSize
)) ||
1316 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> PcdGet32(PcdMaxHardwareErrorVariableSize
))) {
1317 return EFI_INVALID_PARAMETER
;
1320 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"
1322 if (StrnCmp(VariableName
, L
"HwErrRec", StrLen(L
"HwErrRec")) != 0) {
1323 return EFI_INVALID_PARAMETER
;
1326 if ((DataSize
> PcdGet32(PcdMaxVariableSize
)) ||
1327 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> PcdGet32(PcdMaxVariableSize
))) {
1328 return EFI_INVALID_PARAMETER
;
1333 // Check whether the input variable is already existed
1335 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
);
1338 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang
1340 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
1342 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, &Variable
);
1350 IN UINT32 Attributes
,
1351 OUT UINT64
*MaximumVariableStorageSize
,
1352 OUT UINT64
*RemainingVariableStorageSize
,
1353 OUT UINT64
*MaximumVariableSize
1357 Routine Description:
1359 This code returns information about the EFI variables.
1363 Attributes Attributes bitmask to specify the type of variables
1364 on which to return information.
1365 MaximumVariableStorageSize Pointer to the maximum size of the storage space available
1366 for the EFI variables associated with the attributes specified.
1367 RemainingVariableStorageSize Pointer to the remaining size of the storage space available
1368 for the EFI variables associated with the attributes specified.
1369 MaximumVariableSize Pointer to the maximum size of the individual EFI variables
1370 associated with the attributes specified.
1375 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.
1376 EFI_SUCCESS - Query successfully.
1377 EFI_UNSUPPORTED - The attribute is not supported on this platform.
1381 VARIABLE_HEADER
*Variable
;
1382 VARIABLE_HEADER
*NextVariable
;
1383 UINT64 VariableSize
;
1384 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1385 UINT64 CommonVariableTotalSize
;
1386 UINT64 HwErrVariableTotalSize
;
1388 CommonVariableTotalSize
= 0;
1389 HwErrVariableTotalSize
= 0;
1391 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
1392 return EFI_INVALID_PARAMETER
;
1395 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
1397 // Make sure the Attributes combination is supported by the platform.
1399 return EFI_UNSUPPORTED
;
1400 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1402 // Make sure if runtime bit is set, boot service bit is set also.
1404 return EFI_INVALID_PARAMETER
;
1405 } else if (EfiAtRuntime () && !(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
1407 // Make sure RT Attribute is set if we are in Runtime phase.
1409 return EFI_INVALID_PARAMETER
;
1410 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1412 // Make sure Hw Attribute is set with NV.
1414 return EFI_INVALID_PARAMETER
;
1415 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1417 // Not support authentiated variable write yet.
1419 return EFI_UNSUPPORTED
;
1422 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[
1423 (Attributes
& EFI_VARIABLE_NON_VOLATILE
) ? NonVolatile
: Volatile
1426 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1427 // with the storage size (excluding the storage header size).
1429 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1432 // Harware error record variable needs larger size.
1434 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
1435 *MaximumVariableStorageSize
= PcdGet32(PcdHwErrStorageSize
);
1436 *MaximumVariableSize
= PcdGet32(PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
1438 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1439 ASSERT (PcdGet32(PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
1440 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
);
1444 // Let *MaximumVariableSize be PcdGet32(PcdMaxVariableSize) with the exception of the variable header size.
1446 *MaximumVariableSize
= PcdGet32(PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
1450 // Point to the starting address of the variables.
1452 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
1455 // Now walk through the related variable store.
1457 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
1458 NextVariable
= GetNextVariablePtr (Variable
);
1459 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
1461 if (EfiAtRuntime ()) {
1463 // we don't take the state of the variables in mind
1464 // when calculating RemainingVariableStorageSize,
1465 // since the space occupied by variables not marked with
1466 // VAR_ADDED is not allowed to be reclaimed in Runtime.
1468 if ((NextVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1469 HwErrVariableTotalSize
+= VariableSize
;
1471 CommonVariableTotalSize
+= VariableSize
;
1475 // Only care about Variables with State VAR_ADDED,because
1476 // the space not marked as VAR_ADDED is reclaimable now.
1478 if ((Variable
->State
== VAR_ADDED
) || (Variable
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1479 if ((NextVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1480 HwErrVariableTotalSize
+= VariableSize
;
1482 CommonVariableTotalSize
+= VariableSize
;
1488 // Go to the next one
1490 Variable
= NextVariable
;
1493 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
1494 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
1496 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
1504 VariableServiceInitialize (
1505 IN EFI_HANDLE ImageHandle
,
1506 IN EFI_SYSTEM_TABLE
*SystemTable
1510 Routine Description:
1511 This function does initialization for variable services
1515 ImageHandle - The firmware allocated handle for the EFI image.
1516 SystemTable - A pointer to the EFI System Table.
1522 EFI_NOT_FOUND - Variable store area not found.
1523 EFI_SUCCESS - Variable services successfully initialized.
1528 EFI_HANDLE NewHandle
;
1530 EFI_PEI_HOB_POINTERS GuidHob
;
1531 VARIABLE_HEADER
*Variable
;
1532 VARIABLE_HEADER
*NextVariable
;
1533 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1534 EFI_FLASH_MAP_FS_ENTRY_DATA
*FlashMapEntryData
;
1535 EFI_FLASH_SUBAREA_ENTRY VariableStoreEntry
;
1538 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
1540 Status
= gBS
->AllocatePool (
1541 EfiRuntimeServicesData
,
1542 (UINTN
) sizeof (VARIABLE_GLOBAL
),
1545 if (EFI_ERROR (Status
)) {
1549 ZeroMem (mGlobal
, (UINTN
) sizeof (VARIABLE_GLOBAL
));
1551 GuidHob
.Raw
= GetHobList ();
1552 FlashMapEntryData
= NULL
;
1553 while ((GuidHob
.Raw
= GetNextGuidHob (&gEfiFlashMapHobGuid
, GuidHob
.Raw
)) != NULL
) {
1554 FlashMapEntryData
= (EFI_FLASH_MAP_FS_ENTRY_DATA
*) GET_GUID_HOB_DATA (GuidHob
.Guid
);
1555 if (FlashMapEntryData
->AreaType
== EFI_FLASH_AREA_EFI_VARIABLES
) {
1558 GuidHob
.Raw
= GET_NEXT_HOB (GuidHob
);
1561 if (FlashMapEntryData
== NULL
) {
1562 DEBUG ((EFI_D_ERROR
, "FSVariable: Could not find flash area for variable!\n"));
1563 Status
= EFI_NOT_FOUND
;
1568 (VOID
*)&VariableStoreEntry
,
1569 (VOID
*)&FlashMapEntryData
->Entries
[0],
1570 sizeof(EFI_FLASH_SUBAREA_ENTRY
)
1574 // Mark the variable storage region of the FLASH as RUNTIME
1576 BaseAddress
= VariableStoreEntry
.Base
& (~EFI_PAGE_MASK
);
1577 Length
= VariableStoreEntry
.Length
+ (VariableStoreEntry
.Base
- BaseAddress
);
1578 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
1579 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
1580 if (EFI_ERROR (Status
)) {
1581 Status
= EFI_UNSUPPORTED
;
1584 Status
= gDS
->SetMemorySpaceAttributes (
1587 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
1589 if (EFI_ERROR (Status
)) {
1590 Status
= EFI_UNSUPPORTED
;
1594 Status
= FileStorageConstructor (
1595 &mGlobal
->VariableStore
[NonVolatile
],
1596 &mGlobal
->GoVirtualChildEvent
[NonVolatile
],
1597 VariableStoreEntry
.Base
,
1598 (UINT32
) VariableStoreEntry
.Length
,
1599 FlashMapEntryData
->VolumeId
,
1600 FlashMapEntryData
->FilePath
1602 ASSERT_EFI_ERROR (Status
);
1607 Status
= MemStorageConstructor (
1608 &mGlobal
->VariableStore
[Volatile
],
1609 &mGlobal
->GoVirtualChildEvent
[Volatile
],
1610 VOLATILE_VARIABLE_STORE_SIZE
1612 ASSERT_EFI_ERROR (Status
);
1617 Status
= gBS
->AllocatePool (
1618 EfiRuntimeServicesData
,
1619 VARIABLE_SCRATCH_SIZE
,
1622 ASSERT_EFI_ERROR (Status
);
1627 Dev
= DEV_FROM_THIS (mGlobal
->VariableStore
[NonVolatile
]);
1628 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) VAR_DATA_PTR (Dev
);
1629 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
1630 if (~VariableStoreHeader
->Size
== 0) {
1631 VariableStoreHeader
->Size
= (UINT32
) VariableStoreEntry
.Length
;
1635 // Calculate LastVariableOffset
1637 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
1638 while (IsValidVariableHeader (Variable
)) {
1639 UINTN VariableSize
= 0;
1640 NextVariable
= GetNextVariablePtr (Variable
);
1641 VariableSize
= NextVariable
- Variable
;
1642 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
1643 mGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
1645 mGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
1647 Variable
= NextVariable
;
1650 mGlobal
->LastVariableOffset
[NonVolatile
] = (UINTN
) Variable
- (UINTN
) VariableStoreHeader
;
1651 mGlobal
->VariableBase
[NonVolatile
] = VariableStoreHeader
;
1654 // Reclaim if remaining space is too small
1656 if ((VariableStoreHeader
->Size
- mGlobal
->LastVariableOffset
[NonVolatile
]) < VARIABLE_RECLAIM_THRESHOLD
) {
1657 Status
= Reclaim (NonVolatile
, NULL
);
1658 if (EFI_ERROR (Status
)) {
1661 // we cannot restore to original state
1663 DEBUG ((EFI_D_ERROR
, "FSVariable: Reclaim error (fatal error) - %r\n", Status
));
1664 ASSERT_EFI_ERROR (Status
);
1669 // 2. Volatile Storage
1671 Dev
= DEV_FROM_THIS (mGlobal
->VariableStore
[Volatile
]);
1672 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) VAR_DATA_PTR (Dev
);
1673 mGlobal
->VariableBase
[Volatile
] = VAR_DATA_PTR (Dev
);
1674 mGlobal
->LastVariableOffset
[Volatile
] = sizeof (VARIABLE_STORE_HEADER
);
1676 // init store_header & body in memory.
1678 mGlobal
->VariableStore
[Volatile
]->Erase (mGlobal
->VariableStore
[Volatile
]);
1679 mGlobal
->VariableStore
[Volatile
]->Write (
1680 mGlobal
->VariableStore
[Volatile
],
1682 sizeof (VARIABLE_STORE_HEADER
),
1683 &mStoreHeaderTemplate
1687 SystemTable
->RuntimeServices
->GetVariable
= DuetGetVariable
;
1688 SystemTable
->RuntimeServices
->GetNextVariableName
= GetNextVariableName
;
1689 SystemTable
->RuntimeServices
->SetVariable
= SetVariable
;
1691 SystemTable
->RuntimeServices
->QueryVariableInfo
= QueryVariableInfo
;
1694 // Now install the Variable Runtime Architectural Protocol on a new handle
1697 Status
= gBS
->InstallMultipleProtocolInterfaces (
1699 &gEfiVariableArchProtocolGuid
,
1701 &gEfiVariableWriteArchProtocolGuid
,
1705 ASSERT_EFI_ERROR (Status
);
1714 OnVirtualAddressChangeFsv (
1721 for (Index
= 0; Index
< MaxType
; Index
++) {
1722 mGlobal
->GoVirtualChildEvent
[Index
] (Event
, mGlobal
->VariableStore
[Index
]);
1723 EfiConvertPointer (0, (VOID
**) &mGlobal
->VariableStore
[Index
]);
1724 EfiConvertPointer (0, &mGlobal
->VariableBase
[Index
]);
1726 EfiConvertPointer (0, &mGlobal
->Scratch
);
1727 EfiConvertPointer (0, (VOID
**) &mGlobal
);