3 Emulation Variable services operate on the runtime volatile memory.
4 The nonvolatile variable space doesn't exist.
6 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 /// Don't use module globals after the SetVirtualAddress map is signaled
22 ESAL_VARIABLE_GLOBAL
*mVariableModuleGlobal
;
24 VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
27 /// The size of a 3 character ISO639 language code.
29 #define ISO_639_2_ENTRY_SIZE 3
32 Update the variable region with Variable information. These are the same
33 arguments as the EFI Variable services.
35 @param[in] VariableName Name of variable
37 @param[in] VendorGuid Guid of variable
39 @param[in] Data Variable data
41 @param[in] DataSize Size of data. 0 means delete
43 @param[in] Attributes Attribues of the variable
45 @param[in] Variable The variable information which is used to keep track of variable usage.
47 @retval EFI_SUCCESS The update operation is success.
49 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
55 IN CHAR16
*VariableName
,
56 IN EFI_GUID
*VendorGuid
,
59 IN UINT32 Attributes OPTIONAL
,
60 IN VARIABLE_POINTER_TRACK
*Variable
64 Finds variable in storage blocks of volatile and non-volatile storage areas.
66 This code finds variable in storage blocks of volatile and non-volatile storage areas.
67 If VariableName is an empty string, then we just return the first
68 qualified variable without comparing VariableName and VendorGuid.
69 Otherwise, VariableName and VendorGuid are compared.
71 @param VariableName Name of the variable to be found.
72 @param VendorGuid Vendor GUID to be found.
73 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
74 including the range searched and the target position.
75 @param Global Pointer to VARIABLE_GLOBAL structure, including
76 base of volatile variable storage area, base of
77 NV variable storage area, and a lock.
79 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
81 @retval EFI_SUCCESS Variable successfully found.
82 @retval EFI_NOT_FOUND Variable not found.
87 IN CHAR16
*VariableName
,
88 IN EFI_GUID
*VendorGuid
,
89 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
90 IN VARIABLE_GLOBAL
*Global
94 Acquires lock only at boot time. Simply returns at runtime.
96 This is a temperary function which will be removed when
97 EfiAcquireLock() in UefiLib can handle the call in UEFI
98 Runtimer driver in RT phase.
99 It calls EfiAcquireLock() at boot time, and simply returns
102 @param Lock A pointer to the lock to acquire
106 AcquireLockOnlyAtBootTime (
110 if (!EfiAtRuntime ()) {
111 EfiAcquireLock (Lock
);
116 Releases lock only at boot time. Simply returns at runtime.
118 This is a temperary function which will be removed when
119 EfiReleaseLock() in UefiLib can handle the call in UEFI
120 Runtimer driver in RT phase.
121 It calls EfiReleaseLock() at boot time, and simply returns
124 @param Lock A pointer to the lock to release
128 ReleaseLockOnlyAtBootTime (
132 if (!EfiAtRuntime ()) {
133 EfiReleaseLock (Lock
);
138 Gets pointer to the variable data.
140 This function gets the pointer to the variable data according
141 to the input pointer to the variable header.
143 @param Variable Pointer to the variable header.
145 @return Pointer to variable data
150 IN VARIABLE_HEADER
*Variable
153 if (Variable
->StartId
!= VARIABLE_DATA
) {
157 // Be careful about pad size for alignment
159 return (UINT8
*) ((UINTN
) GET_VARIABLE_NAME_PTR (Variable
) + Variable
->NameSize
+ GET_PAD_SIZE (Variable
->NameSize
));
163 Gets pointer to header of the next potential variable.
165 This function gets the pointer to the next potential variable header
166 according to the input point to the variable header. The return value
167 is not a valid variable if the input variable was the last variable
168 in the variabl store.
170 @param Variable Pointer to header of the next variable
172 @return Pointer to next variable header.
173 @retval NULL Input was not a valid variable header.
177 GetNextPotentialVariablePtr (
178 IN VARIABLE_HEADER
*Variable
181 VARIABLE_HEADER
*VarHeader
;
183 if (Variable
->StartId
!= VARIABLE_DATA
) {
187 // Be careful about pad size for alignment
189 VarHeader
= (VARIABLE_HEADER
*) (GetVariableDataPtr (Variable
) + Variable
->DataSize
+ GET_PAD_SIZE (Variable
->DataSize
));
195 Gets pointer to header of the next variable.
197 This function gets the pointer to the next variable header according
198 to the input point to the variable header.
200 @param Variable Pointer to header of the next variable
202 @return Pointer to next variable header.
207 IN VARIABLE_HEADER
*Variable
210 VARIABLE_HEADER
*VarHeader
;
212 VarHeader
= GetNextPotentialVariablePtr (Variable
);
214 if ((VarHeader
== NULL
) || (VarHeader
->StartId
!= VARIABLE_DATA
)) {
222 Updates LastVariableOffset variable for the given variable store.
224 LastVariableOffset points to the offset to use for the next variable
225 when updating the variable store.
227 @param[in] VariableStore Pointer to the start of the variable store
228 @param[out] LastVariableOffset Offset to put the next new variable in
232 InitializeLocationForLastVariableOffset (
233 IN VARIABLE_STORE_HEADER
*VariableStore
,
234 OUT UINTN
*LastVariableOffset
237 VARIABLE_HEADER
*VarHeader
;
239 *LastVariableOffset
= sizeof (VARIABLE_STORE_HEADER
);
240 VarHeader
= (VARIABLE_HEADER
*) ((UINT8
*)VariableStore
+ *LastVariableOffset
);
241 while (VarHeader
->StartId
== VARIABLE_DATA
) {
242 VarHeader
= GetNextPotentialVariablePtr (VarHeader
);
244 if (VarHeader
!= NULL
) {
245 *LastVariableOffset
= (UINTN
) VarHeader
- (UINTN
) VariableStore
;
253 Gets pointer to the end of the variable storage area.
255 This function gets pointer to the end of the variable storage
256 area, according to the input variable store header.
258 @param VolHeader Pointer to the variale store header
260 @return Pointer to the end of the variable storage area.
265 IN VARIABLE_STORE_HEADER
*VolHeader
269 // The end of variable store
271 return (VARIABLE_HEADER
*) ((UINTN
) VolHeader
+ VolHeader
->Size
);
275 Routine used to track statistical information about variable usage.
276 The data is stored in the EFI system table so it can be accessed later.
277 VariableInfo.efi can dump out the table. Only Boot Services variable
278 accesses are tracked by this code. The PcdVariableCollectStatistics
279 build flag controls if this feature is enabled.
281 A read that hits in the cache will have Read and Cache true for
282 the transaction. Data is allocated by this routine, but never
285 @param[in] VariableName Name of the Variable to track
286 @param[in] VendorGuid Guid of the Variable to track
287 @param[in] Volatile TRUE if volatile FALSE if non-volatile
288 @param[in] Read TRUE if GetVariable() was called
289 @param[in] Write TRUE if SetVariable() was called
290 @param[in] Delete TRUE if deleted via SetVariable()
291 @param[in] Cache TRUE for a cache hit.
296 IN CHAR16
*VariableName
,
297 IN EFI_GUID
*VendorGuid
,
305 VARIABLE_INFO_ENTRY
*Entry
;
307 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
309 if (EfiAtRuntime ()) {
310 // Don't collect statistics at runtime
314 if (gVariableInfo
== NULL
) {
316 // on the first call allocate a entry and place a pointer to it in
317 // the EFI System Table
319 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
320 ASSERT (gVariableInfo
!= NULL
);
322 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
323 gVariableInfo
->Name
= AllocatePool (StrSize (VariableName
));
324 ASSERT (gVariableInfo
->Name
!= NULL
);
325 StrCpy (gVariableInfo
->Name
, VariableName
);
326 gVariableInfo
->Volatile
= Volatile
;
328 gBS
->InstallConfigurationTable (&gEfiVariableGuid
, gVariableInfo
);
332 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
333 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
334 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
342 Entry
->DeleteCount
++;
352 if (Entry
->Next
== NULL
) {
354 // If the entry is not in the table add it.
355 // Next iteration of the loop will fill in the data
357 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
358 ASSERT (Entry
->Next
!= NULL
);
360 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
361 Entry
->Next
->Name
= AllocatePool (StrSize (VariableName
));
362 ASSERT (Entry
->Next
->Name
!= NULL
);
363 StrCpy (Entry
->Next
->Name
, VariableName
);
364 Entry
->Next
->Volatile
= Volatile
;
372 Get index from supported language codes according to language string.
374 This code is used to get corresponding index in supported language codes. It can handle
375 RFC4646 and ISO639 language tags.
376 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
377 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
380 SupportedLang = "engfraengfra"
382 Iso639Language = TRUE
383 The return value is "0".
385 SupportedLang = "en;fr;en-US;fr-FR"
387 Iso639Language = FALSE
388 The return value is "3".
390 @param SupportedLang Platform supported language codes.
391 @param Lang Configured language.
392 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
394 @retval the index of language in the language codes.
398 GetIndexFromSupportedLangCodes(
399 IN CHAR8
*SupportedLang
,
401 IN BOOLEAN Iso639Language
406 UINTN LanguageLength
;
408 if (Iso639Language
) {
409 CompareLength
= ISO_639_2_ENTRY_SIZE
;
410 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
411 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
413 // Successfully find the index of Lang string in SupportedLang string.
415 Index
= Index
/ CompareLength
;
423 // Compare RFC4646 language code
426 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
428 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
430 // Skip ';' characters in SupportedLang
432 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
434 // Determine the length of the next language code in SupportedLang
436 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
438 if ((CompareLength
== LanguageLength
) &&
439 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
441 // Successfully find the index of Lang string in SupportedLang string.
452 Get language string from supported language codes according to index.
454 This code is used to get corresponding language string in supported language codes. It can handle
455 RFC4646 and ISO639 language tags.
456 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
457 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
460 SupportedLang = "engfraengfra"
462 Iso639Language = TRUE
463 The return value is "fra".
465 SupportedLang = "en;fr;en-US;fr-FR"
467 Iso639Language = FALSE
468 The return value is "fr".
470 @param SupportedLang Platform supported language codes.
471 @param Index the index in supported language codes.
472 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
474 @retval the language string in the language codes.
478 GetLangFromSupportedLangCodes (
479 IN CHAR8
*SupportedLang
,
481 IN BOOLEAN Iso639Language
489 Supported
= SupportedLang
;
490 if (Iso639Language
) {
492 // according to the index of Lang string in SupportedLang string to get the language.
493 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
494 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
496 CompareLength
= ISO_639_2_ENTRY_SIZE
;
497 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
498 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
503 // take semicolon as delimitation, sequentially traverse supported language codes.
505 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
508 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
510 // Have completed the traverse, but not find corrsponding string.
511 // This case is not allowed to happen.
516 if (SubIndex
== Index
) {
518 // according to the index of Lang string in SupportedLang string to get the language.
519 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
520 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
522 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
523 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
528 // Skip ';' characters in Supported
530 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
536 Returns a pointer to an allocated buffer that contains the best matching language
537 from a set of supported languages.
539 This function supports both ISO 639-2 and RFC 4646 language codes, but language
540 code types may not be mixed in a single call to this function. This function
541 supports a variable argument list that allows the caller to pass in a prioritized
542 list of language codes to test against all the language codes in SupportedLanguages.
544 If SupportedLanguages is NULL, then ASSERT().
546 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
547 contains a set of language codes in the format
548 specified by Iso639Language.
549 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
550 in ISO 639-2 format. If FALSE, then all language
551 codes are assumed to be in RFC 4646 language format
552 @param[in] ... A variable argument list that contains pointers to
553 Null-terminated ASCII strings that contain one or more
554 language codes in the format specified by Iso639Language.
555 The first language code from each of these language
556 code lists is used to determine if it is an exact or
557 close match to any of the language codes in
558 SupportedLanguages. Close matches only apply to RFC 4646
559 language codes, and the matching algorithm from RFC 4647
560 is used to determine if a close match is present. If
561 an exact or close match is found, then the matching
562 language code from SupportedLanguages is returned. If
563 no matches are found, then the next variable argument
564 parameter is evaluated. The variable argument list
565 is terminated by a NULL.
567 @retval NULL The best matching language could not be found in SupportedLanguages.
568 @retval NULL There are not enough resources available to return the best matching
570 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
571 language in SupportedLanguages.
575 VariableGetBestLanguage (
576 IN CONST CHAR8
*SupportedLanguages
,
577 IN BOOLEAN Iso639Language
,
584 UINTN LanguageLength
;
585 CONST CHAR8
*Supported
;
588 ASSERT (SupportedLanguages
!= NULL
);
590 VA_START (Args
, Iso639Language
);
591 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
593 // Default to ISO 639-2 mode
596 LanguageLength
= MIN (3, AsciiStrLen (Language
));
599 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
601 if (!Iso639Language
) {
602 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
606 // Trim back the length of Language used until it is empty
608 while (LanguageLength
> 0) {
610 // Loop through all language codes in SupportedLanguages
612 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
614 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
616 if (!Iso639Language
) {
618 // Skip ';' characters in Supported
620 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
622 // Determine the length of the next language code in Supported
624 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
626 // If Language is longer than the Supported, then skip to the next language
628 if (LanguageLength
> CompareLength
) {
633 // See if the first LanguageLength characters in Supported match Language
635 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
638 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
639 Buffer
[CompareLength
] = '\0';
640 return CopyMem (Buffer
, Supported
, CompareLength
);
644 if (Iso639Language
) {
646 // If ISO 639 mode, then each language can only be tested once
651 // If RFC 4646 mode, then trim Language from the right to the next '-' character
653 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
660 // No matches were found
666 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
668 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
670 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
671 and are read-only. Therefore, in variable driver, only store the original value for other use.
673 @param[in] VariableName Name of variable
675 @param[in] Data Variable data
677 @param[in] DataSize Size of data. 0 means delete
681 AutoUpdateLangVariable(
682 IN CHAR16
*VariableName
,
688 CHAR8
*BestPlatformLang
;
692 VARIABLE_POINTER_TRACK Variable
;
693 BOOLEAN SetLanguageCodes
;
696 // Don't do updates for delete operation
702 SetLanguageCodes
= FALSE
;
704 if (StrCmp (VariableName
, L
"PlatformLangCodes") == 0) {
706 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
708 if (EfiAtRuntime ()) {
712 SetLanguageCodes
= TRUE
;
715 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
716 // Therefore, in variable driver, only store the original value for other use.
718 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
719 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
721 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
722 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
725 // PlatformLang holds a single language from PlatformLangCodes,
726 // so the size of PlatformLangCodes is enough for the PlatformLang.
728 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
729 FreePool (mVariableModuleGlobal
->PlatformLang
);
731 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
732 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
734 } else if (StrCmp (VariableName
, L
"LangCodes") == 0) {
736 // LangCodes is a volatile variable, so it can not be updated at runtime.
738 if (EfiAtRuntime ()) {
742 SetLanguageCodes
= TRUE
;
745 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
746 // Therefore, in variable driver, only store the original value for other use.
748 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
749 FreePool (mVariableModuleGlobal
->LangCodes
);
751 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
752 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
756 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
757 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
759 // Update Lang if PlatformLang is already set
760 // Update PlatformLang if Lang is already set
762 Status
= FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*) mVariableModuleGlobal
);
763 if (!EFI_ERROR (Status
)) {
767 VariableName
= L
"PlatformLang";
768 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
769 DataSize
= Variable
.CurrPtr
->DataSize
;
771 Status
= FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*) mVariableModuleGlobal
);
772 if (!EFI_ERROR (Status
)) {
774 // Update PlatformLang
776 VariableName
= L
"Lang";
777 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
778 DataSize
= Variable
.CurrPtr
->DataSize
;
781 // Neither PlatformLang nor Lang is set, directly return
789 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
791 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
793 if (StrCmp (VariableName
, L
"PlatformLang") == 0) {
795 // Update Lang when PlatformLangCodes/LangCodes were set.
797 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
799 // When setting PlatformLang, firstly get most matched language string from supported language codes.
801 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
802 if (BestPlatformLang
!= NULL
) {
804 // Get the corresponding index in language codes.
806 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
809 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
811 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
814 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
816 FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*)mVariableModuleGlobal
);
818 Status
= UpdateVariable (L
"Lang", &gEfiGlobalVariableGuid
, BestLang
, ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, &Variable
);
820 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
822 ASSERT_EFI_ERROR(Status
);
826 } else if (StrCmp (VariableName
, L
"Lang") == 0) {
828 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
830 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
832 // When setting Lang, firstly get most matched language string from supported language codes.
834 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
835 if (BestLang
!= NULL
) {
837 // Get the corresponding index in language codes.
839 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
842 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
844 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
847 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
849 FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*)mVariableModuleGlobal
);
851 Status
= UpdateVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, BestPlatformLang
,
852 AsciiStrSize (BestPlatformLang
), Attributes
, &Variable
);
854 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
855 ASSERT_EFI_ERROR (Status
);
862 Update the variable region with Variable information. These are the same
863 arguments as the EFI Variable services.
865 @param[in] VariableName Name of variable
867 @param[in] VendorGuid Guid of variable
869 @param[in] Data Variable data
871 @param[in] DataSize Size of data. 0 means delete
873 @param[in] Attributes Attribues of the variable
875 @param[in] Variable The variable information which is used to keep track of variable usage.
877 @retval EFI_SUCCESS The update operation is success.
879 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
885 IN CHAR16
*VariableName
,
886 IN EFI_GUID
*VendorGuid
,
889 IN UINT32 Attributes OPTIONAL
,
890 IN VARIABLE_POINTER_TRACK
*Variable
894 VARIABLE_HEADER
*NextVariable
;
899 VARIABLE_GLOBAL
*Global
;
900 UINTN NonVolatileVarableStoreSize
;
902 Global
= &mVariableModuleGlobal
->VariableGlobal
[Physical
];
904 if (Variable
->CurrPtr
!= NULL
) {
906 // Update/Delete existing variable
909 if (EfiAtRuntime ()) {
911 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
912 // the volatile is ReadOnly, and SetVariable should be aborted and
913 // return EFI_WRITE_PROTECTED.
915 if (Variable
->Volatile
) {
916 Status
= EFI_WRITE_PROTECTED
;
920 // Only variable have NV attribute can be updated/deleted in Runtime
922 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
923 Status
= EFI_INVALID_PARAMETER
;
929 // Setting a data variable with no access, or zero DataSize attributes
930 // specified causes it to be deleted.
932 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
933 Variable
->CurrPtr
->State
&= VAR_DELETED
;
934 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
935 Status
= EFI_SUCCESS
;
940 // If the variable is marked valid and the same data has been passed in
941 // then return to the caller immediately.
943 if (Variable
->CurrPtr
->DataSize
== DataSize
&&
944 CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0
946 Status
= EFI_SUCCESS
;
948 } else if (Variable
->CurrPtr
->State
== VAR_ADDED
) {
950 // Mark the old variable as in delete transition
952 Variable
->CurrPtr
->State
&= VAR_IN_DELETED_TRANSITION
;
957 // No found existing variable, Create a new variable
961 // Make sure we are trying to create a new variable.
962 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
964 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
965 Status
= EFI_NOT_FOUND
;
970 // Only variable have NV|RT attribute can be created in Runtime
972 if (EfiAtRuntime () &&
973 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
974 Status
= EFI_INVALID_PARAMETER
;
980 // Function part - create a new variable and copy the data.
981 // Both update a variable and create a variable will come here.
984 VarNameOffset
= sizeof (VARIABLE_HEADER
);
985 VarNameSize
= StrSize (VariableName
);
986 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
987 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
989 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
990 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(Global
->NonVolatileVariableBase
))->Size
;
991 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
992 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
993 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
994 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
995 Status
= EFI_OUT_OF_RESOURCES
;
999 NextVariable
= (VARIABLE_HEADER
*) (UINT8
*) (mVariableModuleGlobal
->NonVolatileLastVariableOffset
1000 + (UINTN
) Global
->NonVolatileVariableBase
);
1001 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= VarSize
;
1003 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
1004 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VarSize
;
1006 mVariableModuleGlobal
->CommonVariableTotalSize
+= VarSize
;
1009 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1010 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (Global
->VolatileVariableBase
)))->Size
1012 Status
= EFI_OUT_OF_RESOURCES
;
1016 NextVariable
= (VARIABLE_HEADER
*) (UINT8
*) (mVariableModuleGlobal
->VolatileLastVariableOffset
1017 + (UINTN
) Global
->VolatileVariableBase
);
1018 mVariableModuleGlobal
->VolatileLastVariableOffset
+= VarSize
;
1021 NextVariable
->StartId
= VARIABLE_DATA
;
1022 NextVariable
->Attributes
= Attributes
;
1023 NextVariable
->State
= VAR_ADDED
;
1024 NextVariable
->Reserved
= 0;
1027 // There will be pad bytes after Data, the NextVariable->NameSize and
1028 // NextVariable->NameSize should not include pad size so that variable
1029 // service can get actual size in GetVariable
1031 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1032 NextVariable
->DataSize
= (UINT32
)DataSize
;
1034 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1036 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1041 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1047 // Mark the old variable as deleted
1049 if (Variable
->CurrPtr
!= NULL
) {
1050 Variable
->CurrPtr
->State
&= VAR_DELETED
;
1053 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1055 Status
= EFI_SUCCESS
;
1062 Finds variable in storage blocks of volatile and non-volatile storage areas.
1064 This code finds variable in storage blocks of volatile and non-volatile storage areas.
1065 If VariableName is an empty string, then we just return the first
1066 qualified variable without comparing VariableName and VendorGuid.
1067 Otherwise, VariableName and VendorGuid are compared.
1069 @param VariableName Name of the variable to be found.
1070 @param VendorGuid Vendor GUID to be found.
1071 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
1072 including the range searched and the target position.
1073 @param Global Pointer to VARIABLE_GLOBAL structure, including
1074 base of volatile variable storage area, base of
1075 NV variable storage area, and a lock.
1077 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
1079 @retval EFI_SUCCESS Variable successfully found.
1080 @retval EFI_NOT_FOUND Variable not found.
1085 IN CHAR16
*VariableName
,
1086 IN EFI_GUID
*VendorGuid
,
1087 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
1088 IN VARIABLE_GLOBAL
*Global
1091 VARIABLE_HEADER
*Variable
[2];
1092 VARIABLE_STORE_HEADER
*VariableStoreHeader
[2];
1096 // 0: Non-Volatile, 1: Volatile
1098 VariableStoreHeader
[0] = (VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->NonVolatileVariableBase
);
1099 VariableStoreHeader
[1] = (VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->VolatileVariableBase
);
1102 // Start Pointers for the variable.
1103 // Actual Data Pointer where data can be written.
1105 Variable
[0] = (VARIABLE_HEADER
*) (VariableStoreHeader
[0] + 1);
1106 Variable
[1] = (VARIABLE_HEADER
*) (VariableStoreHeader
[1] + 1);
1108 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
1109 return EFI_INVALID_PARAMETER
;
1112 // Find the variable by walk through non-volatile and volatile variable store
1114 for (Index
= 0; Index
< 2; Index
++) {
1115 PtrTrack
->StartPtr
= (VARIABLE_HEADER
*) (VariableStoreHeader
[Index
] + 1);
1116 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
1118 while ((Variable
[Index
] < GetEndPointer (VariableStoreHeader
[Index
])) && (Variable
[Index
] != NULL
)) {
1119 if (Variable
[Index
]->StartId
== VARIABLE_DATA
&& Variable
[Index
]->State
== VAR_ADDED
) {
1120 if (!(EfiAtRuntime () && ((Variable
[Index
]->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0))) {
1121 if (VariableName
[0] == 0) {
1122 PtrTrack
->CurrPtr
= Variable
[Index
];
1123 PtrTrack
->Volatile
= (BOOLEAN
) Index
;
1126 if (CompareGuid (VendorGuid
, &Variable
[Index
]->VendorGuid
)) {
1127 if (CompareMem (VariableName
, GET_VARIABLE_NAME_PTR (Variable
[Index
]), Variable
[Index
]->NameSize
) == 0) {
1128 PtrTrack
->CurrPtr
= Variable
[Index
];
1129 PtrTrack
->Volatile
= (BOOLEAN
) Index
;
1137 Variable
[Index
] = GetNextVariablePtr (Variable
[Index
]);
1140 PtrTrack
->CurrPtr
= NULL
;
1141 return EFI_NOT_FOUND
;
1145 This code finds variable in storage blocks (Volatile or Non-Volatile).
1147 @param VariableName A Null-terminated Unicode string that is the name of
1148 the vendor's variable.
1149 @param VendorGuid A unique identifier for the vendor.
1150 @param Attributes If not NULL, a pointer to the memory location to return the
1151 attributes bitmask for the variable.
1152 @param DataSize Size of Data found. If size is less than the
1153 data, this value contains the required size.
1154 @param Data On input, the size in bytes of the return Data buffer.
1155 On output, the size of data returned in Data.
1156 @param Global Pointer to VARIABLE_GLOBAL structure
1158 @retval EFI_SUCCESS The function completed successfully.
1159 @retval EFI_NOT_FOUND The variable was not found.
1160 @retval EFI_BUFFER_TOO_SMALL DataSize is too small for the result. DataSize has
1161 been updated with the size needed to complete the request.
1162 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid or DataSize is NULL.
1168 IN CHAR16
*VariableName
,
1169 IN EFI_GUID
*VendorGuid
,
1170 OUT UINT32
*Attributes OPTIONAL
,
1171 IN OUT UINTN
*DataSize
,
1173 IN VARIABLE_GLOBAL
*Global
1176 VARIABLE_POINTER_TRACK Variable
;
1180 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
1181 return EFI_INVALID_PARAMETER
;
1184 AcquireLockOnlyAtBootTime(&Global
->VariableServicesLock
);
1187 // Find existing variable
1189 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, Global
);
1191 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1197 VarDataSize
= Variable
.CurrPtr
->DataSize
;
1198 if (*DataSize
>= VarDataSize
) {
1200 Status
= EFI_INVALID_PARAMETER
;
1204 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
1205 if (Attributes
!= NULL
) {
1206 *Attributes
= Variable
.CurrPtr
->Attributes
;
1209 *DataSize
= VarDataSize
;
1210 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
1211 Status
= EFI_SUCCESS
;
1214 *DataSize
= VarDataSize
;
1215 Status
= EFI_BUFFER_TOO_SMALL
;
1220 ReleaseLockOnlyAtBootTime (&Global
->VariableServicesLock
);
1226 This code Finds the Next available variable.
1228 @param VariableNameSize Size of the variable.
1229 @param VariableName On input, supplies the last VariableName that was returned by GetNextVariableName().
1230 On output, returns the Null-terminated Unicode string of the current variable.
1231 @param VendorGuid On input, supplies the last VendorGuid that was returned by GetNextVariableName().
1232 On output, returns the VendorGuid of the current variable.
1233 @param Global Pointer to VARIABLE_GLOBAL structure.
1235 @retval EFI_SUCCESS The function completed successfully.
1236 @retval EFI_NOT_FOUND The next variable was not found.
1237 @retval EFI_BUFFER_TOO_SMALL VariableNameSize is too small for the result.
1238 VariableNameSize has been updated with the size needed to complete the request.
1239 @retval EFI_INVALID_PARAMETER VariableNameSize or VariableName or VendorGuid is NULL.
1244 EmuGetNextVariableName (
1245 IN OUT UINTN
*VariableNameSize
,
1246 IN OUT CHAR16
*VariableName
,
1247 IN OUT EFI_GUID
*VendorGuid
,
1248 IN VARIABLE_GLOBAL
*Global
1251 VARIABLE_POINTER_TRACK Variable
;
1255 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
1256 return EFI_INVALID_PARAMETER
;
1259 AcquireLockOnlyAtBootTime(&Global
->VariableServicesLock
);
1261 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, Global
);
1263 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1268 if (VariableName
[0] != 0) {
1270 // If variable name is not NULL, get next variable
1272 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1275 // If both volatile and non-volatile variable store are parsed,
1278 if (Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
) {
1279 Variable
.Volatile
= (BOOLEAN
) (Variable
.Volatile
^ ((BOOLEAN
) 0x1));
1280 if (Variable
.Volatile
) {
1281 Variable
.StartPtr
= (VARIABLE_HEADER
*) ((UINTN
) (Global
->VolatileVariableBase
+ sizeof (VARIABLE_STORE_HEADER
)));
1282 Variable
.EndPtr
= (VARIABLE_HEADER
*) GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->VolatileVariableBase
));
1284 Status
= EFI_NOT_FOUND
;
1288 Variable
.CurrPtr
= Variable
.StartPtr
;
1289 if (Variable
.CurrPtr
->StartId
!= VARIABLE_DATA
) {
1294 // Variable is found
1296 if (Variable
.CurrPtr
->StartId
== VARIABLE_DATA
&& Variable
.CurrPtr
->State
== VAR_ADDED
) {
1297 if (!(EfiAtRuntime () && ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0))) {
1298 VarNameSize
= Variable
.CurrPtr
->NameSize
;
1299 if (VarNameSize
<= *VariableNameSize
) {
1302 GET_VARIABLE_NAME_PTR (Variable
.CurrPtr
),
1307 &Variable
.CurrPtr
->VendorGuid
,
1310 Status
= EFI_SUCCESS
;
1312 Status
= EFI_BUFFER_TOO_SMALL
;
1315 *VariableNameSize
= VarNameSize
;
1322 ReleaseLockOnlyAtBootTime (&Global
->VariableServicesLock
);
1329 This code sets variable in storage blocks (Volatile or Non-Volatile).
1331 @param VariableName A Null-terminated Unicode string that is the name of the vendor's
1332 variable. Each VariableName is unique for each
1333 VendorGuid. VariableName must contain 1 or more
1334 Unicode characters. If VariableName is an empty Unicode
1335 string, then EFI_INVALID_PARAMETER is returned.
1336 @param VendorGuid A unique identifier for the vendor
1337 @param Attributes Attributes bitmask to set for the variable
1338 @param DataSize The size in bytes of the Data buffer. A size of zero causes the
1339 variable to be deleted.
1340 @param Data The contents for the variable
1341 @param Global Pointer to VARIABLE_GLOBAL structure
1342 @param VolatileOffset The offset of last volatile variable
1343 @param NonVolatileOffset The offset of last non-volatile variable
1345 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
1346 defined by the Attributes.
1347 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied, or the
1348 DataSize exceeds the maximum allowed, or VariableName is an empty
1349 Unicode string, or VendorGuid is NULL.
1350 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
1351 @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.
1352 @retval EFI_WRITE_PROTECTED The variable in question is read-only or cannot be deleted.
1353 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
1359 IN CHAR16
*VariableName
,
1360 IN EFI_GUID
*VendorGuid
,
1361 IN UINT32 Attributes
,
1364 IN VARIABLE_GLOBAL
*Global
,
1365 IN UINTN
*VolatileOffset
,
1366 IN UINTN
*NonVolatileOffset
1369 VARIABLE_POINTER_TRACK Variable
;
1373 // Check input parameters
1375 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
1376 return EFI_INVALID_PARAMETER
;
1379 if (DataSize
!= 0 && Data
== NULL
) {
1380 return EFI_INVALID_PARAMETER
;
1384 // Not support authenticated variable write yet.
1386 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1387 return EFI_INVALID_PARAMETER
;
1391 // Make sure if runtime bit is set, boot service bit is set also
1393 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1394 return EFI_INVALID_PARAMETER
;
1397 // The size of the VariableName, including the Unicode Null in bytes plus
1398 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
1399 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
1401 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1402 if ((DataSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
)) ||
1403 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
))) {
1404 return EFI_INVALID_PARAMETER
;
1407 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"
1409 if (StrnCmp(VariableName
, L
"HwErrRec", StrLen(L
"HwErrRec")) != 0) {
1410 return EFI_INVALID_PARAMETER
;
1414 // The size of the VariableName, including the Unicode Null in bytes plus
1415 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
1417 if ((DataSize
> PcdGet32 (PcdMaxVariableSize
)) ||
1418 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxVariableSize
))) {
1419 return EFI_INVALID_PARAMETER
;
1423 AcquireLockOnlyAtBootTime(&Global
->VariableServicesLock
);
1426 // Check whether the input variable is already existed
1429 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, Global
);
1432 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang
1434 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
1436 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, &Variable
);
1438 ReleaseLockOnlyAtBootTime (&Global
->VariableServicesLock
);
1444 This code returns information about the EFI variables.
1446 @param Attributes Attributes bitmask to specify the type of variables
1447 on which to return information.
1448 @param MaximumVariableStorageSize On output the maximum size of the storage space available for
1449 the EFI variables associated with the attributes specified.
1450 @param RemainingVariableStorageSize Returns the remaining size of the storage space available for EFI
1451 variables associated with the attributes specified.
1452 @param MaximumVariableSize Returns the maximum size of an individual EFI variable
1453 associated with the attributes specified.
1454 @param Global Pointer to VARIABLE_GLOBAL structure.
1456 @retval EFI_SUCCESS Valid answer returned.
1457 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied
1458 @retval EFI_UNSUPPORTED The attribute is not supported on this platform, and the
1459 MaximumVariableStorageSize, RemainingVariableStorageSize,
1460 MaximumVariableSize are undefined.
1465 EmuQueryVariableInfo (
1466 IN UINT32 Attributes
,
1467 OUT UINT64
*MaximumVariableStorageSize
,
1468 OUT UINT64
*RemainingVariableStorageSize
,
1469 OUT UINT64
*MaximumVariableSize
,
1470 IN VARIABLE_GLOBAL
*Global
1473 VARIABLE_HEADER
*Variable
;
1474 VARIABLE_HEADER
*NextVariable
;
1475 UINT64 VariableSize
;
1476 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1477 UINT64 CommonVariableTotalSize
;
1478 UINT64 HwErrVariableTotalSize
;
1480 CommonVariableTotalSize
= 0;
1481 HwErrVariableTotalSize
= 0;
1483 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
1484 return EFI_INVALID_PARAMETER
;
1487 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
1489 // Make sure the Attributes combination is supported by the platform.
1491 return EFI_UNSUPPORTED
;
1492 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1494 // Make sure if runtime bit is set, boot service bit is set also.
1496 return EFI_INVALID_PARAMETER
;
1497 } else if (EfiAtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
1499 // Make sure RT Attribute is set if we are in Runtime phase.
1501 return EFI_INVALID_PARAMETER
;
1502 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1504 // Make sure Hw Attribute is set with NV.
1506 return EFI_INVALID_PARAMETER
;
1507 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1509 // Not support authentiated variable write yet.
1511 return EFI_UNSUPPORTED
;
1514 AcquireLockOnlyAtBootTime(&Global
->VariableServicesLock
);
1516 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1518 // Query is Volatile related.
1520 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->VolatileVariableBase
);
1523 // Query is Non-Volatile related.
1525 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->NonVolatileVariableBase
);
1529 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1530 // with the storage size (excluding the storage header size)
1532 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1535 // Harware error record variable needs larger size.
1537 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
1538 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
1539 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
1541 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1542 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
1543 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
1547 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
1549 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
1553 // Point to the starting address of the variables.
1555 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
1558 // Now walk through the related variable store.
1560 while (Variable
< GetEndPointer (VariableStoreHeader
)) {
1561 NextVariable
= GetNextVariablePtr(Variable
);
1562 if (NextVariable
== NULL
) {
1565 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
1567 if ((NextVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1568 HwErrVariableTotalSize
+= VariableSize
;
1570 CommonVariableTotalSize
+= VariableSize
;
1574 // Go to the next one.
1576 Variable
= NextVariable
;
1579 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
1580 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
1582 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
1585 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
1586 *MaximumVariableSize
= 0;
1587 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
1588 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
1591 ReleaseLockOnlyAtBootTime (&Global
->VariableServicesLock
);
1596 Initializes variable store area.
1598 This function allocates memory space for variable store area and initializes its attributes.
1600 @param VolatileStore Indicates if the variable store is volatile.
1604 InitializeVariableStore (
1605 IN BOOLEAN VolatileStore
1608 VARIABLE_STORE_HEADER
*VariableStore
;
1609 BOOLEAN FullyInitializeStore
;
1610 EFI_PHYSICAL_ADDRESS
*VariableBase
;
1611 UINTN
*LastVariableOffset
;
1613 FullyInitializeStore
= TRUE
;
1615 if (VolatileStore
) {
1616 VariableBase
= &mVariableModuleGlobal
->VariableGlobal
[Physical
].VolatileVariableBase
;
1617 LastVariableOffset
= &mVariableModuleGlobal
->VolatileLastVariableOffset
;
1619 VariableBase
= &mVariableModuleGlobal
->VariableGlobal
[Physical
].NonVolatileVariableBase
;
1620 LastVariableOffset
= &mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1624 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
1625 // is stored with common variable in the same NV region. So the platform integrator should
1626 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
1627 // PcdVariableStoreSize.
1629 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdVariableStoreSize
));
1632 // Allocate memory for variable store.
1634 if (VolatileStore
|| (PcdGet64 (PcdEmuVariableNvStoreReserved
) == 0)) {
1635 VariableStore
= (VARIABLE_STORE_HEADER
*) AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
));
1638 // A memory location has been reserved for the NV variable store. Certain
1639 // platforms may be able to preserve a memory range across system resets,
1640 // thereby providing better NV variable emulation.
1643 (VARIABLE_STORE_HEADER
*)(VOID
*)(UINTN
)
1644 PcdGet64 (PcdEmuVariableNvStoreReserved
);
1646 (VariableStore
->Size
== PcdGet32 (PcdVariableStoreSize
)) &&
1647 (VariableStore
->Format
== VARIABLE_STORE_FORMATTED
) &&
1648 (VariableStore
->State
== VARIABLE_STORE_HEALTHY
)
1652 "Variable Store reserved at %p appears to be valid\n",
1655 FullyInitializeStore
= FALSE
;
1659 if (NULL
== VariableStore
) {
1660 return EFI_OUT_OF_RESOURCES
;
1663 if (FullyInitializeStore
) {
1664 SetMem (VariableStore
, PcdGet32 (PcdVariableStoreSize
), 0xff);
1668 // Variable Specific Data
1670 *VariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStore
;
1671 InitializeLocationForLastVariableOffset (VariableStore
, LastVariableOffset
);
1673 CopyGuid (&VariableStore
->Signature
, &gEfiVariableGuid
);
1674 VariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
1675 VariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
1676 VariableStore
->State
= VARIABLE_STORE_HEALTHY
;
1677 VariableStore
->Reserved
= 0;
1678 VariableStore
->Reserved1
= 0;
1684 Initializes variable store area for non-volatile and volatile variable.
1686 This function allocates and initializes memory space for global context of ESAL
1687 variable service and variable store area for non-volatile and volatile variable.
1689 @param ImageHandle The Image handle of this driver.
1690 @param SystemTable The pointer of EFI_SYSTEM_TABLE.
1692 @retval EFI_SUCCESS Function successfully executed.
1693 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
1698 VariableCommonInitialize (
1699 IN EFI_HANDLE ImageHandle
,
1700 IN EFI_SYSTEM_TABLE
*SystemTable
1706 // Allocate memory for mVariableModuleGlobal
1708 mVariableModuleGlobal
= (ESAL_VARIABLE_GLOBAL
*) AllocateRuntimeZeroPool (
1709 sizeof (ESAL_VARIABLE_GLOBAL
)
1711 if (NULL
== mVariableModuleGlobal
) {
1712 return EFI_OUT_OF_RESOURCES
;
1715 EfiInitializeLock(&mVariableModuleGlobal
->VariableGlobal
[Physical
].VariableServicesLock
, TPL_NOTIFY
);
1718 // Intialize volatile variable store
1720 Status
= InitializeVariableStore (TRUE
);
1721 if (EFI_ERROR (Status
)) {
1722 FreePool(mVariableModuleGlobal
);
1726 // Intialize non volatile variable store
1728 Status
= InitializeVariableStore (FALSE
);