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.
579 GetIndexFromSupportedLangCodes(
580 IN CHAR8
*SupportedLang
,
582 IN BOOLEAN Iso639Language
587 UINTN LanguageLength
;
589 if (Iso639Language
) {
590 CompareLength
= ISO_639_2_ENTRY_SIZE
;
591 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
592 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
594 // Successfully find the index of Lang string in SupportedLang string.
596 Index
= Index
/ CompareLength
;
604 // Compare RFC4646 language code
607 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
609 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
611 // Skip ';' characters in SupportedLang
613 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
615 // Determine the length of the next language code in SupportedLang
617 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
619 if ((CompareLength
== LanguageLength
) &&
620 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
622 // Successfully find the index of Lang string in SupportedLang string.
633 Get language string from supported language codes according to index.
635 This code is used to get corresponding language string in supported language codes. It can handle
636 RFC4646 and ISO639 language tags.
637 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
638 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
641 SupportedLang = "engfraengfra"
643 Iso639Language = TRUE
644 The return value is "fra".
646 SupportedLang = "en;fr;en-US;fr-FR"
648 Iso639Language = FALSE
649 The return value is "fr".
651 @param SupportedLang Platform supported language codes.
652 @param Index the index in supported language codes.
653 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
655 @retval the language string in the language codes.
659 GetLangFromSupportedLangCodes (
660 IN CHAR8
*SupportedLang
,
662 IN BOOLEAN Iso639Language
670 Supported
= SupportedLang
;
671 if (Iso639Language
) {
673 // according to the index of Lang string in SupportedLang string to get the language.
674 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
675 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
677 CompareLength
= ISO_639_2_ENTRY_SIZE
;
678 mGlobal
->Lang
[CompareLength
] = '\0';
679 return CopyMem (mGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
684 // take semicolon as delimitation, sequentially traverse supported language codes.
686 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
689 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
691 // Have completed the traverse, but not find corrsponding string.
692 // This case is not allowed to happen.
697 if (SubIndex
== Index
) {
699 // according to the index of Lang string in SupportedLang string to get the language.
700 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
701 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
703 mGlobal
->PlatformLang
[CompareLength
] = '\0';
704 return CopyMem (mGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
709 // Skip ';' characters in Supported
711 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
717 Returns a pointer to an allocated buffer that contains the best matching language
718 from a set of supported languages.
720 This function supports both ISO 639-2 and RFC 4646 language codes, but language
721 code types may not be mixed in a single call to this function. This function
722 supports a variable argument list that allows the caller to pass in a prioritized
723 list of language codes to test against all the language codes in SupportedLanguages.
725 If SupportedLanguages is NULL, then ASSERT().
727 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
728 contains a set of language codes in the format
729 specified by Iso639Language.
730 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
731 in ISO 639-2 format. If FALSE, then all language
732 codes are assumed to be in RFC 4646 language format
733 @param[in] ... A variable argument list that contains pointers to
734 Null-terminated ASCII strings that contain one or more
735 language codes in the format specified by Iso639Language.
736 The first language code from each of these language
737 code lists is used to determine if it is an exact or
738 close match to any of the language codes in
739 SupportedLanguages. Close matches only apply to RFC 4646
740 language codes, and the matching algorithm from RFC 4647
741 is used to determine if a close match is present. If
742 an exact or close match is found, then the matching
743 language code from SupportedLanguages is returned. If
744 no matches are found, then the next variable argument
745 parameter is evaluated. The variable argument list
746 is terminated by a NULL.
748 @retval NULL The best matching language could not be found in SupportedLanguages.
749 @retval NULL There are not enough resources available to return the best matching
751 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
752 language in SupportedLanguages.
756 VariableGetBestLanguage (
757 IN CONST CHAR8
*SupportedLanguages
,
758 IN BOOLEAN Iso639Language
,
765 UINTN LanguageLength
;
766 CONST CHAR8
*Supported
;
769 ASSERT (SupportedLanguages
!= NULL
);
771 VA_START (Args
, Iso639Language
);
772 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
774 // Default to ISO 639-2 mode
777 LanguageLength
= MIN (3, AsciiStrLen (Language
));
780 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
782 if (!Iso639Language
) {
783 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
787 // Trim back the length of Language used until it is empty
789 while (LanguageLength
> 0) {
791 // Loop through all language codes in SupportedLanguages
793 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
795 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
797 if (!Iso639Language
) {
799 // Skip ';' characters in Supported
801 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
803 // Determine the length of the next language code in Supported
805 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
807 // If Language is longer than the Supported, then skip to the next language
809 if (LanguageLength
> CompareLength
) {
814 // See if the first LanguageLength characters in Supported match Language
816 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
819 Buffer
= Iso639Language
? mGlobal
->Lang
: mGlobal
->PlatformLang
;
820 Buffer
[CompareLength
] = '\0';
821 return CopyMem (Buffer
, Supported
, CompareLength
);
825 if (Iso639Language
) {
827 // If ISO 639 mode, then each language can only be tested once
832 // If RFC 4646 mode, then trim Language from the right to the next '-' character
834 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
841 // No matches were found
847 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
849 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
851 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
852 and are read-only. Therefore, in variable driver, only store the original value for other use.
854 @param[in] VariableName Name of variable
856 @param[in] Data Variable data
858 @param[in] DataSize Size of data. 0 means delete
862 AutoUpdateLangVariable(
863 IN CHAR16
*VariableName
,
869 CHAR8
*BestPlatformLang
;
873 VARIABLE_POINTER_TRACK Variable
;
874 BOOLEAN SetLanguageCodes
;
877 // Don't do updates for delete operation
883 SetLanguageCodes
= FALSE
;
885 if (StrCmp (VariableName
, L
"PlatformLangCodes") == 0) {
887 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
889 if (EfiAtRuntime ()) {
893 SetLanguageCodes
= TRUE
;
896 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
897 // Therefore, in variable driver, only store the original value for other use.
899 if (mGlobal
->PlatformLangCodes
!= NULL
) {
900 FreePool (mGlobal
->PlatformLangCodes
);
902 mGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
903 ASSERT (mGlobal
->PlatformLangCodes
!= NULL
);
906 // PlatformLang holds a single language from PlatformLangCodes,
907 // so the size of PlatformLangCodes is enough for the PlatformLang.
909 if (mGlobal
->PlatformLang
!= NULL
) {
910 FreePool (mGlobal
->PlatformLang
);
912 mGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
913 ASSERT (mGlobal
->PlatformLang
!= NULL
);
915 } else if (StrCmp (VariableName
, L
"LangCodes") == 0) {
917 // LangCodes is a volatile variable, so it can not be updated at runtime.
919 if (EfiAtRuntime ()) {
923 SetLanguageCodes
= TRUE
;
926 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
927 // Therefore, in variable driver, only store the original value for other use.
929 if (mGlobal
->LangCodes
!= NULL
) {
930 FreePool (mGlobal
->LangCodes
);
932 mGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
933 ASSERT (mGlobal
->LangCodes
!= NULL
);
937 && (mGlobal
->PlatformLangCodes
!= NULL
)
938 && (mGlobal
->LangCodes
!= NULL
)) {
940 // Update Lang if PlatformLang is already set
941 // Update PlatformLang if Lang is already set
943 Status
= FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
);
944 if (!EFI_ERROR (Status
)) {
948 VariableName
= L
"PlatformLang";
949 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
950 DataSize
= Variable
.CurrPtr
->DataSize
;
952 Status
= FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
);
953 if (!EFI_ERROR (Status
)) {
955 // Update PlatformLang
957 VariableName
= L
"Lang";
958 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
959 DataSize
= Variable
.CurrPtr
->DataSize
;
962 // Neither PlatformLang nor Lang is set, directly return
970 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
972 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
974 if (StrCmp (VariableName
, L
"PlatformLang") == 0) {
976 // Update Lang when PlatformLangCodes/LangCodes were set.
978 if ((mGlobal
->PlatformLangCodes
!= NULL
) && (mGlobal
->LangCodes
!= NULL
)) {
980 // When setting PlatformLang, firstly get most matched language string from supported language codes.
982 BestPlatformLang
= VariableGetBestLanguage (mGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
983 if (BestPlatformLang
!= NULL
) {
985 // Get the corresponding index in language codes.
987 Index
= GetIndexFromSupportedLangCodes (mGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
990 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
992 BestLang
= GetLangFromSupportedLangCodes (mGlobal
->LangCodes
, Index
, TRUE
);
995 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
997 FindVariable(L
"Lang", &gEfiGlobalVariableGuid
, &Variable
);
999 Status
= UpdateVariable (L
"Lang", &gEfiGlobalVariableGuid
, BestLang
, ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, &Variable
);
1001 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
1003 ASSERT_EFI_ERROR(Status
);
1007 } else if (StrCmp (VariableName
, L
"Lang") == 0) {
1009 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1011 if ((mGlobal
->PlatformLangCodes
!= NULL
) && (mGlobal
->LangCodes
!= NULL
)) {
1013 // When setting Lang, firstly get most matched language string from supported language codes.
1015 BestLang
= VariableGetBestLanguage (mGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1016 if (BestLang
!= NULL
) {
1018 // Get the corresponding index in language codes.
1020 Index
= GetIndexFromSupportedLangCodes (mGlobal
->LangCodes
, BestLang
, TRUE
);
1023 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1025 BestPlatformLang
= GetLangFromSupportedLangCodes (mGlobal
->PlatformLangCodes
, Index
, FALSE
);
1028 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1030 FindVariable(L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
);
1032 Status
= UpdateVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, BestPlatformLang
,
1033 AsciiStrSize (BestPlatformLang
), Attributes
, &Variable
);
1035 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
1036 ASSERT_EFI_ERROR (Status
);
1043 Update the variable region with Variable information. These are the same
1044 arguments as the EFI Variable services.
1046 @param[in] VariableName Name of variable
1048 @param[in] VendorGuid Guid of variable
1050 @param[in] Data Variable data
1052 @param[in] DataSize Size of data. 0 means delete
1054 @param[in] Attributes Attribues of the variable
1056 @param[in] Variable The variable information which is used to keep track of variable usage.
1058 @retval EFI_SUCCESS The update operation is success.
1060 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1066 IN CHAR16
*VariableName
,
1067 IN EFI_GUID
*VendorGuid
,
1070 IN UINT32 Attributes OPTIONAL
,
1071 IN VARIABLE_POINTER_TRACK
*Variable
1075 VARIABLE_HEADER
*NextVariable
;
1076 UINTN VarNameOffset
;
1077 UINTN VarDataOffset
;
1082 VARIABLE_STORAGE_TYPE StorageType
;
1086 if (Variable
->CurrPtr
!= NULL
) {
1088 // Update/Delete existing variable
1091 if (EfiAtRuntime ()) {
1093 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
1094 // the volatile is ReadOnly, and SetVariable should be aborted and
1095 // return EFI_WRITE_PROTECTED.
1097 if (Variable
->Type
== Volatile
) {
1098 return EFI_WRITE_PROTECTED
;
1101 // Only variable have NV attribute can be updated/deleted in Runtime
1103 if (!(Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
)) {
1104 return EFI_INVALID_PARAMETER
;
1109 // Setting a data variable with no access, or zero DataSize attributes
1110 // specified causes it to be deleted.
1112 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1114 // Found this variable in storage
1116 State
= Variable
->CurrPtr
->State
;
1117 State
&= VAR_DELETED
;
1119 Status
= mGlobal
->VariableStore
[Variable
->Type
]->Write (
1120 mGlobal
->VariableStore
[Variable
->Type
],
1121 VARIABLE_MEMBER_OFFSET (State
, (UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
),
1122 sizeof (Variable
->CurrPtr
->State
),
1126 // NOTE: Write operation at least can write data to memory cache
1127 // Discard file writing failure here.
1133 // Found this variable in storage
1134 // If the variable is marked valid and the same data has been passed in
1135 // then return to the caller immediately.
1137 if ((Variable
->CurrPtr
->DataSize
== DataSize
) &&
1138 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0)
1141 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
1142 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1144 // Mark the old variable as in delete transition
1146 State
= Variable
->CurrPtr
->State
;
1147 State
&= VAR_IN_DELETED_TRANSITION
;
1149 Status
= mGlobal
->VariableStore
[Variable
->Type
]->Write (
1150 mGlobal
->VariableStore
[Variable
->Type
],
1151 VARIABLE_MEMBER_OFFSET (State
, (UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
),
1152 sizeof (Variable
->CurrPtr
->State
),
1156 // NOTE: Write operation at least can write data to memory cache
1157 // Discard file writing failure here.
1162 // Create a new variable
1166 // Make sure we are trying to create a new variable.
1167 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
1169 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1170 return EFI_NOT_FOUND
;
1173 // Only variable have NV|RT attribute can be created in Runtime
1175 if (EfiAtRuntime () &&
1176 (!(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) || !(Attributes
& EFI_VARIABLE_NON_VOLATILE
))) {
1177 return EFI_INVALID_PARAMETER
;
1183 // Function part - create a new variable and copy the data.
1184 // Both update a variable and create a variable will come here.
1185 // We can firstly write all the data in memory, then write them to file
1186 // This can reduce the times of write operation
1189 NextVariable
= (VARIABLE_HEADER
*) mGlobal
->Scratch
;
1191 NextVariable
->StartId
= VARIABLE_DATA
;
1192 NextVariable
->Attributes
= Attributes
;
1193 NextVariable
->State
= VAR_ADDED
;
1194 NextVariable
->Reserved
= 0;
1195 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1196 VarNameSize
= StrSize (VariableName
);
1198 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1202 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1204 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1208 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1210 // There will be pad bytes after Data, the NextVariable->NameSize and
1211 // NextVariable->DataSize should not include pad size so that variable
1212 // service can get actual size in GetVariable
1214 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1215 NextVariable
->DataSize
= (UINT32
)DataSize
;
1218 // The actual size of the variable that stores in storage should
1219 // include pad size.
1220 // VarDataOffset: offset from begin of current variable header
1222 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1224 StorageType
= (Attributes
& EFI_VARIABLE_NON_VOLATILE
) ? NonVolatile
: Volatile
;
1226 if ((UINT32
) (VarSize
+ mGlobal
->LastVariableOffset
[StorageType
]) >
1227 ((VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[StorageType
])->Size
1229 if ((StorageType
== NonVolatile
) && EfiAtRuntime ()) {
1230 return EFI_OUT_OF_RESOURCES
;
1233 // Perform garbage collection & reclaim operation
1235 Status
= Reclaim (StorageType
, Variable
->CurrPtr
);
1236 if (EFI_ERROR (Status
)) {
1239 // we cannot restore to original state, fetal error, report to user
1241 DEBUG ((EFI_D_ERROR
, "FSVariable: Recalim error (fetal error) - %r\n", Status
));
1245 // If still no enough space, return out of resources
1247 if ((UINT32
) (VarSize
+ mGlobal
->LastVariableOffset
[StorageType
]) >
1248 ((VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[StorageType
])->Size
1250 return EFI_OUT_OF_RESOURCES
;
1255 Status
= mGlobal
->VariableStore
[StorageType
]->Write (
1256 mGlobal
->VariableStore
[StorageType
],
1257 mGlobal
->LastVariableOffset
[StorageType
],
1262 // NOTE: Write operation at least can write data to memory cache
1263 // Discard file writing failure here.
1265 mGlobal
->LastVariableOffset
[StorageType
] += VarSize
;
1267 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
1268 mGlobal
->HwErrVariableTotalSize
+= VarSize
;
1270 mGlobal
->CommonVariableTotalSize
+= VarSize
;
1274 // Mark the old variable as deleted
1276 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
1277 State
= Variable
->CurrPtr
->State
;
1278 State
&= VAR_DELETED
;
1280 Status
= mGlobal
->VariableStore
[StorageType
]->Write (
1281 mGlobal
->VariableStore
[StorageType
],
1282 VARIABLE_MEMBER_OFFSET (State
, (UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
),
1283 sizeof (Variable
->CurrPtr
->State
),
1287 // NOTE: Write operation at least can write data to memory cache
1288 // Discard file writing failure here.
1297 IN CHAR16
*VariableName
,
1298 IN EFI_GUID
*VendorGuid
,
1299 OUT UINT32
*Attributes OPTIONAL
,
1300 IN OUT UINTN
*DataSize
,
1305 Routine Description:
1307 This code finds variable in storage blocks (Volatile or Non-Volatile)
1311 VariableName Name of Variable to be found
1312 VendorGuid Variable vendor GUID
1313 Attributes OPTIONAL Attribute value of the variable found
1314 DataSize Size of Data found. If size is less than the
1315 data, this value contains the required size.
1324 VARIABLE_POINTER_TRACK Variable
;
1328 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
1329 return EFI_INVALID_PARAMETER
;
1333 // Find existing variable
1335 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
);
1337 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1343 VarDataSize
= Variable
.CurrPtr
->DataSize
;
1344 if (*DataSize
>= VarDataSize
) {
1346 return EFI_INVALID_PARAMETER
;
1348 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
1350 if (Attributes
!= NULL
) {
1351 *Attributes
= Variable
.CurrPtr
->Attributes
;
1354 *DataSize
= VarDataSize
;
1358 *DataSize
= VarDataSize
;
1359 return EFI_BUFFER_TOO_SMALL
;
1365 GetNextVariableName (
1366 IN OUT UINTN
*VariableNameSize
,
1367 IN OUT CHAR16
*VariableName
,
1368 IN OUT EFI_GUID
*VendorGuid
1372 Routine Description:
1374 This code Finds the Next available variable
1378 VariableNameSize Size of the variable
1379 VariableName Pointer to variable name
1380 VendorGuid Variable Vendor Guid
1388 VARIABLE_POINTER_TRACK Variable
;
1392 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
1393 return EFI_INVALID_PARAMETER
;
1396 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
);
1398 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1402 if (VariableName
[0] != 0) {
1404 // If variable name is not NULL, get next variable
1406 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1411 // The order we find variable is: 1). NonVolatile; 2). Volatile
1412 // If both volatile and non-volatile variable store are parsed,
1415 if (Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
) {
1416 if (Variable
.Type
== Volatile
) {
1418 // Since we met the end of Volatile storage, we have parsed all the stores.
1420 return EFI_NOT_FOUND
;
1424 // End of NonVolatile, continue to parse Volatile
1426 Variable
.Type
= Volatile
;
1427 Variable
.StartPtr
= (VARIABLE_HEADER
*) ((VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[Volatile
] + 1);
1428 Variable
.EndPtr
= (VARIABLE_HEADER
*) GetEndPointer ((VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[Volatile
]);
1430 Variable
.CurrPtr
= Variable
.StartPtr
;
1431 if (!IsValidVariableHeader (Variable
.CurrPtr
)) {
1436 // Variable is found
1438 if (IsValidVariableHeader (Variable
.CurrPtr
) &&
1439 ((Variable
.CurrPtr
->State
== VAR_ADDED
) ||
1440 (Variable
.CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
)))) {
1441 if (!EfiAtRuntime () || (Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
1442 VarNameSize
= Variable
.CurrPtr
->NameSize
;
1443 if (VarNameSize
<= *VariableNameSize
) {
1446 GET_VARIABLE_NAME_PTR (Variable
.CurrPtr
),
1451 &Variable
.CurrPtr
->VendorGuid
,
1454 Status
= EFI_SUCCESS
;
1456 Status
= EFI_BUFFER_TOO_SMALL
;
1459 *VariableNameSize
= VarNameSize
;
1464 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1471 IN CHAR16
*VariableName
,
1472 IN EFI_GUID
*VendorGuid
,
1473 IN UINT32 Attributes
,
1479 Routine Description:
1481 This code sets variable in storage blocks (Volatile or Non-Volatile)
1485 VariableName Name of Variable to be found
1486 VendorGuid Variable vendor GUID
1487 Attributes Attribute value of the variable found
1488 DataSize Size of Data found. If size is less than the
1489 data, this value contains the required size.
1494 EFI_INVALID_PARAMETER - Invalid parameter
1495 EFI_SUCCESS - Set successfully
1496 EFI_OUT_OF_RESOURCES - Resource not enough to set variable
1497 EFI_NOT_FOUND - Not found
1498 EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure
1499 EFI_WRITE_PROTECTED - Variable is read-only
1503 VARIABLE_POINTER_TRACK Variable
;
1507 // Check input parameters
1509 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
1510 return EFI_INVALID_PARAMETER
;
1513 if (DataSize
!= 0 && Data
== NULL
) {
1514 return EFI_INVALID_PARAMETER
;
1518 // Not support authenticated variable write yet.
1520 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1521 return EFI_INVALID_PARAMETER
;
1525 // Make sure if runtime bit is set, boot service bit is set also
1527 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1528 return EFI_INVALID_PARAMETER
;
1532 // The size of the VariableName, including the Unicode Null in bytes plus
1533 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
1534 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
1536 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1537 if ((DataSize
> PcdGet32(PcdMaxHardwareErrorVariableSize
)) ||
1538 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> PcdGet32(PcdMaxHardwareErrorVariableSize
))) {
1539 return EFI_INVALID_PARAMETER
;
1542 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"
1544 if (StrnCmp(VariableName
, L
"HwErrRec", StrLen(L
"HwErrRec")) != 0) {
1545 return EFI_INVALID_PARAMETER
;
1548 if ((DataSize
> PcdGet32(PcdMaxVariableSize
)) ||
1549 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> PcdGet32(PcdMaxVariableSize
))) {
1550 return EFI_INVALID_PARAMETER
;
1555 // Check whether the input variable is already existed
1557 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
);
1560 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang
1562 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
1564 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, &Variable
);
1572 IN UINT32 Attributes
,
1573 OUT UINT64
*MaximumVariableStorageSize
,
1574 OUT UINT64
*RemainingVariableStorageSize
,
1575 OUT UINT64
*MaximumVariableSize
1579 Routine Description:
1581 This code returns information about the EFI variables.
1585 Attributes Attributes bitmask to specify the type of variables
1586 on which to return information.
1587 MaximumVariableStorageSize Pointer to the maximum size of the storage space available
1588 for the EFI variables associated with the attributes specified.
1589 RemainingVariableStorageSize Pointer to the remaining size of the storage space available
1590 for the EFI variables associated with the attributes specified.
1591 MaximumVariableSize Pointer to the maximum size of the individual EFI variables
1592 associated with the attributes specified.
1597 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.
1598 EFI_SUCCESS - Query successfully.
1599 EFI_UNSUPPORTED - The attribute is not supported on this platform.
1603 VARIABLE_HEADER
*Variable
;
1604 VARIABLE_HEADER
*NextVariable
;
1605 UINT64 VariableSize
;
1606 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1607 UINT64 CommonVariableTotalSize
;
1608 UINT64 HwErrVariableTotalSize
;
1610 CommonVariableTotalSize
= 0;
1611 HwErrVariableTotalSize
= 0;
1613 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
1614 return EFI_INVALID_PARAMETER
;
1617 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
1619 // Make sure the Attributes combination is supported by the platform.
1621 return EFI_UNSUPPORTED
;
1622 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1624 // Make sure if runtime bit is set, boot service bit is set also.
1626 return EFI_INVALID_PARAMETER
;
1627 } else if (EfiAtRuntime () && !(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
1629 // Make sure RT Attribute is set if we are in Runtime phase.
1631 return EFI_INVALID_PARAMETER
;
1632 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1634 // Make sure Hw Attribute is set with NV.
1636 return EFI_INVALID_PARAMETER
;
1637 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1639 // Not support authentiated variable write yet.
1641 return EFI_UNSUPPORTED
;
1644 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[
1645 (Attributes
& EFI_VARIABLE_NON_VOLATILE
) ? NonVolatile
: Volatile
1648 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1649 // with the storage size (excluding the storage header size).
1651 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1654 // Harware error record variable needs larger size.
1656 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
1657 *MaximumVariableStorageSize
= PcdGet32(PcdHwErrStorageSize
);
1658 *MaximumVariableSize
= PcdGet32(PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
1660 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1661 ASSERT (PcdGet32(PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
1662 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
);
1666 // Let *MaximumVariableSize be PcdGet32(PcdMaxVariableSize) with the exception of the variable header size.
1668 *MaximumVariableSize
= PcdGet32(PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
1672 // Point to the starting address of the variables.
1674 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
1677 // Now walk through the related variable store.
1679 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
1680 NextVariable
= GetNextVariablePtr (Variable
);
1681 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
1683 if (EfiAtRuntime ()) {
1685 // we don't take the state of the variables in mind
1686 // when calculating RemainingVariableStorageSize,
1687 // since the space occupied by variables not marked with
1688 // VAR_ADDED is not allowed to be reclaimed in Runtime.
1690 if ((NextVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1691 HwErrVariableTotalSize
+= VariableSize
;
1693 CommonVariableTotalSize
+= VariableSize
;
1697 // Only care about Variables with State VAR_ADDED,because
1698 // the space not marked as VAR_ADDED is reclaimable now.
1700 if ((Variable
->State
== VAR_ADDED
) || (Variable
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1701 if ((NextVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1702 HwErrVariableTotalSize
+= VariableSize
;
1704 CommonVariableTotalSize
+= VariableSize
;
1710 // Go to the next one
1712 Variable
= NextVariable
;
1715 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
1716 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
1718 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
1726 VariableServiceInitialize (
1727 IN EFI_HANDLE ImageHandle
,
1728 IN EFI_SYSTEM_TABLE
*SystemTable
1732 Routine Description:
1733 This function does initialization for variable services
1737 ImageHandle - The firmware allocated handle for the EFI image.
1738 SystemTable - A pointer to the EFI System Table.
1744 EFI_NOT_FOUND - Variable store area not found.
1745 EFI_SUCCESS - Variable services successfully initialized.
1750 EFI_HANDLE NewHandle
;
1752 EFI_PEI_HOB_POINTERS GuidHob
;
1753 VARIABLE_HEADER
*Variable
;
1754 VARIABLE_HEADER
*NextVariable
;
1755 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1756 EFI_FLASH_MAP_FS_ENTRY_DATA
*FlashMapEntryData
;
1757 EFI_FLASH_SUBAREA_ENTRY VariableStoreEntry
;
1760 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
1762 Status
= gBS
->AllocatePool (
1763 EfiRuntimeServicesData
,
1764 (UINTN
) sizeof (VARIABLE_GLOBAL
),
1767 if (EFI_ERROR (Status
)) {
1771 ZeroMem (mGlobal
, (UINTN
) sizeof (VARIABLE_GLOBAL
));
1773 GuidHob
.Raw
= GetHobList ();
1774 FlashMapEntryData
= NULL
;
1775 while ((GuidHob
.Raw
= GetNextGuidHob (&gEfiFlashMapHobGuid
, GuidHob
.Raw
)) != NULL
) {
1776 FlashMapEntryData
= (EFI_FLASH_MAP_FS_ENTRY_DATA
*) GET_GUID_HOB_DATA (GuidHob
.Guid
);
1777 if (FlashMapEntryData
->AreaType
== EFI_FLASH_AREA_EFI_VARIABLES
) {
1780 GuidHob
.Raw
= GET_NEXT_HOB (GuidHob
);
1783 if (FlashMapEntryData
== NULL
) {
1784 DEBUG ((EFI_D_ERROR
, "FSVariable: Could not find flash area for variable!\n"));
1785 Status
= EFI_NOT_FOUND
;
1790 (VOID
*)&VariableStoreEntry
,
1791 (VOID
*)&FlashMapEntryData
->Entries
[0],
1792 sizeof(EFI_FLASH_SUBAREA_ENTRY
)
1796 // Mark the variable storage region of the FLASH as RUNTIME
1798 BaseAddress
= VariableStoreEntry
.Base
& (~EFI_PAGE_MASK
);
1799 Length
= VariableStoreEntry
.Length
+ (VariableStoreEntry
.Base
- BaseAddress
);
1800 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
1801 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
1802 if (EFI_ERROR (Status
)) {
1803 Status
= EFI_UNSUPPORTED
;
1806 Status
= gDS
->SetMemorySpaceAttributes (
1809 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
1811 if (EFI_ERROR (Status
)) {
1812 Status
= EFI_UNSUPPORTED
;
1816 Status
= FileStorageConstructor (
1817 &mGlobal
->VariableStore
[NonVolatile
],
1818 &mGlobal
->GoVirtualChildEvent
[NonVolatile
],
1819 VariableStoreEntry
.Base
,
1820 (UINT32
) VariableStoreEntry
.Length
,
1821 FlashMapEntryData
->VolumeId
,
1822 FlashMapEntryData
->FilePath
1824 ASSERT_EFI_ERROR (Status
);
1829 Status
= MemStorageConstructor (
1830 &mGlobal
->VariableStore
[Volatile
],
1831 &mGlobal
->GoVirtualChildEvent
[Volatile
],
1832 VOLATILE_VARIABLE_STORE_SIZE
1834 ASSERT_EFI_ERROR (Status
);
1839 Status
= gBS
->AllocatePool (
1840 EfiRuntimeServicesData
,
1841 VARIABLE_SCRATCH_SIZE
,
1844 ASSERT_EFI_ERROR (Status
);
1849 Dev
= DEV_FROM_THIS (mGlobal
->VariableStore
[NonVolatile
]);
1850 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) VAR_DATA_PTR (Dev
);
1851 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
1852 if (~VariableStoreHeader
->Size
== 0) {
1853 VariableStoreHeader
->Size
= (UINT32
) VariableStoreEntry
.Length
;
1857 // Calculate LastVariableOffset
1859 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
1860 while (IsValidVariableHeader (Variable
)) {
1861 UINTN VariableSize
= 0;
1862 NextVariable
= GetNextVariablePtr (Variable
);
1863 VariableSize
= NextVariable
- Variable
;
1864 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
1865 mGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
1867 mGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
1869 Variable
= NextVariable
;
1872 mGlobal
->LastVariableOffset
[NonVolatile
] = (UINTN
) Variable
- (UINTN
) VariableStoreHeader
;
1873 mGlobal
->VariableBase
[NonVolatile
] = VariableStoreHeader
;
1876 // Reclaim if remaining space is too small
1878 if ((VariableStoreHeader
->Size
- mGlobal
->LastVariableOffset
[NonVolatile
]) < VARIABLE_RECLAIM_THRESHOLD
) {
1879 Status
= Reclaim (NonVolatile
, NULL
);
1880 if (EFI_ERROR (Status
)) {
1883 // we cannot restore to original state
1885 DEBUG ((EFI_D_ERROR
, "FSVariable: Reclaim error (fatal error) - %r\n", Status
));
1886 ASSERT_EFI_ERROR (Status
);
1891 // 2. Volatile Storage
1893 Dev
= DEV_FROM_THIS (mGlobal
->VariableStore
[Volatile
]);
1894 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) VAR_DATA_PTR (Dev
);
1895 mGlobal
->VariableBase
[Volatile
] = VAR_DATA_PTR (Dev
);
1896 mGlobal
->LastVariableOffset
[Volatile
] = sizeof (VARIABLE_STORE_HEADER
);
1898 // init store_header & body in memory.
1900 mGlobal
->VariableStore
[Volatile
]->Erase (mGlobal
->VariableStore
[Volatile
]);
1901 mGlobal
->VariableStore
[Volatile
]->Write (
1902 mGlobal
->VariableStore
[Volatile
],
1904 sizeof (VARIABLE_STORE_HEADER
),
1905 &mStoreHeaderTemplate
1909 SystemTable
->RuntimeServices
->GetVariable
= DuetGetVariable
;
1910 SystemTable
->RuntimeServices
->GetNextVariableName
= GetNextVariableName
;
1911 SystemTable
->RuntimeServices
->SetVariable
= SetVariable
;
1913 SystemTable
->RuntimeServices
->QueryVariableInfo
= QueryVariableInfo
;
1916 // Now install the Variable Runtime Architectural Protocol on a new handle
1919 Status
= gBS
->InstallMultipleProtocolInterfaces (
1921 &gEfiVariableArchProtocolGuid
,
1923 &gEfiVariableWriteArchProtocolGuid
,
1927 ASSERT_EFI_ERROR (Status
);
1936 OnVirtualAddressChangeFsv (
1943 for (Index
= 0; Index
< MaxType
; Index
++) {
1944 mGlobal
->GoVirtualChildEvent
[Index
] (Event
, mGlobal
->VariableStore
[Index
]);
1945 EfiConvertPointer (0, (VOID
**) &mGlobal
->VariableStore
[Index
]);
1946 EfiConvertPointer (0, &mGlobal
->VariableBase
[Index
]);
1948 EfiConvertPointer (0, (VOID
**) &mGlobal
->PlatformLangCodes
);
1949 EfiConvertPointer (0, (VOID
**) &mGlobal
->LangCodes
);
1950 EfiConvertPointer (0, (VOID
**) &mGlobal
->PlatformLang
);
1951 EfiConvertPointer (0, &mGlobal
->Scratch
);
1952 EfiConvertPointer (0, (VOID
**) &mGlobal
);