2 The implementation of Extended SAL variable services.
4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "AuthService.h"
19 // Don't use module globals after the SetVirtualAddress map is signaled
21 ESAL_VARIABLE_GLOBAL
*mVariableModuleGlobal
;
22 CHAR16
*mVariableName
[NUM_VAR_NAME
] = {
30 EFI_PLATFORM_KEY_NAME
,
31 EFI_KEY_EXCHANGE_KEY_NAME
34 GLOBAL_REMOVE_IF_UNREFERENCED VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
37 // The current Hii implementation accesses this variable a larg # of times on every boot.
38 // Other common variables are only accessed a single time. This is why this cache algorithm
39 // only targets a single variable. Probably to get an performance improvement out of
40 // a Cache you would need a cache that improves the search performance for a variable.
42 VARIABLE_CACHE_ENTRY mVariableCache
[] = {
44 &gEfiGlobalVariableGuid
,
51 &gEfiGlobalVariableGuid
,
60 Acquires lock only at boot time. Simply returns at runtime.
62 This is a temperary function which will be removed when
63 EfiAcquireLock() in UefiLib can handle the call in UEFI
64 Runtimer driver in RT phase.
65 It calls EfiAcquireLock() at boot time, and simply returns
68 @param[in] Lock A pointer to the lock to acquire.
72 AcquireLockOnlyAtBootTime (
76 if (!EfiAtRuntime ()) {
77 EfiAcquireLock (Lock
);
82 Releases lock only at boot time. Simply returns at runtime.
84 This is a temperary function which will be removed when
85 EfiReleaseLock() in UefiLib can handle the call in UEFI
86 Runtimer driver in RT phase.
87 It calls EfiReleaseLock() at boot time, and simply returns
90 @param[in] Lock A pointer to the lock to release.
94 ReleaseLockOnlyAtBootTime (
98 if (!EfiAtRuntime ()) {
99 EfiReleaseLock (Lock
);
104 Reads/Writes variable storage, volatile or non-volatile.
106 This function reads or writes volatile or non-volatile variable stroage.
107 For volatile storage, it performs memory copy.
108 For non-volatile storage, it accesses data on firmware storage. Data
109 area to access can span multiple firmware blocks.
111 @param[in] Write TRUE - Write variable store.
112 FALSE - Read variable store.
113 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.
114 @param[in] Volatile TRUE - Variable is volatile.
115 FALSE - Variable is non-volatile.
116 @param[in] Instance Instance of FV Block services.
117 @param[in] StartAddress Start address of data to access.
118 @param[in] DataSize Size of data to access.
119 @param[in, out] Buffer For write, pointer to the buffer from which data is written.
120 For read, pointer to the buffer to hold the data read.
122 @retval EFI_SUCCESS Variable store successfully accessed.
123 @retval EFI_INVALID_PARAMETER Data area to access exceeds valid variable storage.
127 AccessVariableStore (
129 IN VARIABLE_GLOBAL
*Global
,
132 IN EFI_PHYSICAL_ADDRESS StartAddress
,
137 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
145 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
146 VARIABLE_STORE_HEADER
*VolatileBase
;
147 EFI_PHYSICAL_ADDRESS FvVolHdr
;
149 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
156 // If data is volatile, simply calculate the data pointer and copy memory.
157 // Data pointer should point to the actual address where data is to be
160 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->VolatileVariableBase
);
162 if ((StartAddress
+ DataSize
) > ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
163 return EFI_INVALID_PARAMETER
;
167 // For volatile variable, a simple memory copy is enough.
170 CopyMem ((VOID
*) StartAddress
, Buffer
, DataSize
);
172 CopyMem (Buffer
, (VOID
*) StartAddress
, DataSize
);
179 // If data is non-volatile, calculate firmware volume header and data pointer.
181 Status
= (EFI_STATUS
) EsalCall (
182 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO
,
183 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI
,
184 GetPhysicalAddressFunctionId
,
193 ASSERT_EFI_ERROR (Status
);
195 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
196 ASSERT (FwVolHeader
!= NULL
);
197 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(FwVolHeader
+ 1);
199 if ((StartAddress
+ DataSize
) > ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((CHAR8
*)VariableStoreHeader
+ VariableStoreHeader
->Size
))) {
200 return EFI_INVALID_PARAMETER
;
203 LinearOffset
= (UINTN
) FwVolHeader
;
204 CurrWritePtr
= StartAddress
;
205 CurrWriteSize
= DataSize
;
209 if (CurrWritePtr
< LinearOffset
) {
210 return EFI_INVALID_PARAMETER
;
214 // Traverse data blocks of this firmware storage to find the one where CurrWritePtr locates
216 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
217 for (BlockIndex
= 0; BlockIndex
< PtrBlockMapEntry
->NumBlocks
; BlockIndex
++) {
218 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
220 // Check to see if the data area to access spans multiple blocks.
222 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
224 // If data area to access is contained in one block, just access and return.
227 Status
= (EFI_STATUS
) EsalCall (
228 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO
,
229 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI
,
233 (CurrWritePtr
- LinearOffset
),
234 (UINT64
) &CurrWriteSize
,
240 Status
= (EFI_STATUS
) EsalCall (
241 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO
,
242 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI
,
246 (CurrWritePtr
- LinearOffset
),
247 (UINT64
) &CurrWriteSize
,
256 // If data area to access spans multiple blocks, access this one and adjust for the next one.
258 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
260 Status
= (EFI_STATUS
) EsalCall (
261 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO
,
262 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI
,
266 (CurrWritePtr
- LinearOffset
),
273 Status
= (EFI_STATUS
) EsalCall (
274 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO
,
275 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI
,
279 (CurrWritePtr
- LinearOffset
),
286 if (EFI_ERROR (Status
)) {
290 // Adjust for the remaining data.
292 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
293 CurrBuffer
= CurrBuffer
+ Size
;
294 CurrWriteSize
= CurrWriteSize
- Size
;
298 LinearOffset
+= PtrBlockMapEntry
->Length
;
307 Retrieves header of volatile or non-volatile variable stroage.
309 @param[in] VarStoreAddress Start address of variable storage.
310 @param[in] Volatile TRUE - Variable storage is volatile.
311 FALSE - Variable storage is non-volatile.
312 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.
313 @param[in] Instance Instance of FV Block services.
314 @param[out] VarStoreHeader Pointer to VARIABLE_STORE_HEADER for output.
319 IN EFI_PHYSICAL_ADDRESS VarStoreAddress
,
321 IN VARIABLE_GLOBAL
*Global
,
323 OUT VARIABLE_STORE_HEADER
*VarStoreHeader
328 Status
= AccessVariableStore (
334 sizeof (VARIABLE_STORE_HEADER
),
337 ASSERT_EFI_ERROR (Status
);
341 Checks variable header.
343 This function checks if variable header is valid or not.
345 @param[in] VariableAddress Start address of variable header.
346 @param[in] Volatile TRUE - Variable is volatile.
347 FALSE - Variable is non-volatile.
348 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.
349 @param[in] Instance Instance of FV Block services.
350 @param[out] VariableHeader Pointer to VARIABLE_HEADER for output.
352 @retval TRUE Variable header is valid.
353 @retval FALSE Variable header is not valid.
357 IsValidVariableHeader (
358 IN EFI_PHYSICAL_ADDRESS VariableAddress
,
360 IN VARIABLE_GLOBAL
*Global
,
362 OUT VARIABLE_HEADER
*VariableHeader OPTIONAL
366 VARIABLE_HEADER LocalVariableHeader
;
368 Status
= AccessVariableStore (
374 sizeof (VARIABLE_HEADER
),
378 if (EFI_ERROR (Status
) || LocalVariableHeader
.StartId
!= VARIABLE_DATA
) {
382 if (VariableHeader
!= NULL
) {
383 CopyMem (VariableHeader
, &LocalVariableHeader
, sizeof (VARIABLE_HEADER
));
390 Gets status of variable store.
392 This function gets the current status of variable store.
394 @param[in] VarStoreHeader Pointer to header of variable store.
396 @retval EfiRaw Variable store status is raw.
397 @retval EfiValid Variable store status is valid.
398 @retval EfiInvalid Variable store status is invalid.
401 VARIABLE_STORE_STATUS
402 GetVariableStoreStatus (
403 IN VARIABLE_STORE_HEADER
*VarStoreHeader
407 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
) &&
408 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
409 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
413 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
414 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
415 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
416 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
417 VarStoreHeader
->Size
== 0xffffffff &&
418 VarStoreHeader
->Format
== 0xff &&
419 VarStoreHeader
->State
== 0xff
429 Gets the size of variable name.
431 This function gets the size of variable name.
432 The variable is specified by its variable header.
433 If variable header contains raw data, just return 0.
435 @param[in] Variable Pointer to the variable header.
437 @return Size of variable name in bytes.
442 IN VARIABLE_HEADER
*Variable
445 if (Variable
->State
== (UINT8
) (-1) ||
446 Variable
->DataSize
== (UINT32
) -1 ||
447 Variable
->NameSize
== (UINT32
) -1 ||
448 Variable
->Attributes
== (UINT32
) -1) {
451 return (UINTN
) Variable
->NameSize
;
455 Gets the size of variable data area.
457 This function gets the size of variable data area.
458 The variable is specified by its variable header.
459 If variable header contains raw data, just return 0.
461 @param[in] Variable Pointer to the variable header.
463 @return Size of variable data area in bytes.
468 IN VARIABLE_HEADER
*Variable
471 if (Variable
->State
== (UINT8
) -1 ||
472 Variable
->DataSize
== (UINT32
) -1 ||
473 Variable
->NameSize
== (UINT32
) -1 ||
474 Variable
->Attributes
== (UINT32
) -1) {
477 return (UINTN
) Variable
->DataSize
;
481 Gets the pointer to variable name.
483 This function gets the pointer to variable name.
484 The variable is specified by its variable header.
486 @param[in] VariableAddress Start address of variable header.
487 @param[in] Volatile TRUE - Variable is volatile.
488 FALSE - Variable is non-volatile.
489 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.
490 @param[in] Instance Instance of FV Block services.
491 @param[out] VariableName Buffer to hold variable name for output.
496 IN EFI_PHYSICAL_ADDRESS VariableAddress
,
498 IN VARIABLE_GLOBAL
*Global
,
500 OUT CHAR16
*VariableName
504 EFI_PHYSICAL_ADDRESS Address
;
505 VARIABLE_HEADER VariableHeader
;
508 IsValid
= IsValidVariableHeader (VariableAddress
, Volatile
, Global
, Instance
, &VariableHeader
);
512 // Name area follows variable header.
514 Address
= VariableAddress
+ sizeof (VARIABLE_HEADER
);
516 Status
= AccessVariableStore (
522 VariableHeader
.NameSize
,
525 ASSERT_EFI_ERROR (Status
);
529 Gets the pointer to variable data area.
531 This function gets the pointer to variable data area.
532 The variable is specified by its variable header.
534 @param[in] VariableAddress Start address of variable header.
535 @param[in] Volatile TRUE - Variable is volatile.
536 FALSE - Variable is non-volatile.
537 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.
538 @param[in] Instance Instance of FV Block services.
539 @param[out] VariableData Buffer to hold variable data for output.
544 IN EFI_PHYSICAL_ADDRESS VariableAddress
,
546 IN VARIABLE_GLOBAL
*Global
,
548 OUT CHAR16
*VariableData
552 EFI_PHYSICAL_ADDRESS Address
;
553 VARIABLE_HEADER VariableHeader
;
556 IsValid
= IsValidVariableHeader (VariableAddress
, Volatile
, Global
, Instance
, &VariableHeader
);
560 // Data area follows variable name.
561 // Be careful about pad size for alignment
563 Address
= VariableAddress
+ sizeof (VARIABLE_HEADER
);
564 Address
+= NameSizeOfVariable (&VariableHeader
);
565 Address
+= GET_PAD_SIZE (NameSizeOfVariable (&VariableHeader
));
567 Status
= AccessVariableStore (
573 VariableHeader
.DataSize
,
576 ASSERT_EFI_ERROR (Status
);
581 Gets the pointer to the next variable header.
583 This function gets the pointer to the next variable header.
584 The variable is specified by its variable header.
586 @param[in] VariableAddress Start address of variable header.
587 @param[in] Volatile TRUE - Variable is volatile.
588 FALSE - Variable is non-volatile.
589 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.
590 @param[in] Instance Instance of FV Block services.
592 @return Pointer to the next variable header.
593 NULL if variable header is invalid.
598 IN EFI_PHYSICAL_ADDRESS VariableAddress
,
600 IN VARIABLE_GLOBAL
*Global
,
604 EFI_PHYSICAL_ADDRESS Address
;
605 VARIABLE_HEADER VariableHeader
;
607 if (!IsValidVariableHeader (VariableAddress
, Volatile
, Global
, Instance
, &VariableHeader
)) {
612 // Header of next variable follows data area of this variable
614 Address
= VariableAddress
+ sizeof (VARIABLE_HEADER
);
615 Address
+= NameSizeOfVariable (&VariableHeader
);
616 Address
+= GET_PAD_SIZE (NameSizeOfVariable (&VariableHeader
));
617 Address
+= DataSizeOfVariable (&VariableHeader
);
618 Address
+= GET_PAD_SIZE (DataSizeOfVariable (&VariableHeader
));
621 // Be careful about pad size for alignment
623 return HEADER_ALIGN (Address
);
627 Gets the pointer to the first variable header in given variable store area.
629 This function gets the pointer to the first variable header in given variable
630 store area. The variable store area is given by its start address.
632 @param[in] VarStoreHeaderAddress Pointer to the header of variable store area.
634 @return Pointer to the first variable header.
639 IN EFI_PHYSICAL_ADDRESS VarStoreHeaderAddress
642 return HEADER_ALIGN (VarStoreHeaderAddress
+ sizeof (VARIABLE_STORE_HEADER
));
646 Gets the pointer to the end of given variable store area.
648 This function gets the pointer to the end of given variable store area.
649 The variable store area is given by its start address.
651 @param[in] VarStoreHeaderAddress Pointer to the header of variable store area.
652 @param[in] Volatile TRUE - Variable is volatile.
653 FALSE - Variable is non-volatile.
654 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.
655 @param[in] Instance Instance of FV Block services.
657 @return Pointer to the end of given variable store area.
662 IN EFI_PHYSICAL_ADDRESS VarStoreHeaderAddress
,
664 IN VARIABLE_GLOBAL
*Global
,
669 VARIABLE_STORE_HEADER VariableStoreHeader
;
671 Status
= AccessVariableStore (
676 VarStoreHeaderAddress
,
677 sizeof (VARIABLE_STORE_HEADER
),
681 ASSERT_EFI_ERROR (Status
);
682 return HEADER_ALIGN (VarStoreHeaderAddress
+ VariableStoreHeader
.Size
);
686 Updates variable info entry in EFI system table for statistical information.
688 Routine used to track statistical information about variable usage.
689 The data is stored in the EFI system table so it can be accessed later.
690 VariableInfo.efi can dump out the table. Only Boot Services variable
691 accesses are tracked by this code. The PcdVariableCollectStatistics
692 build flag controls if this feature is enabled.
693 A read that hits in the cache will have Read and Cache true for
694 the transaction. Data is allocated by this routine, but never
697 @param[in] VariableName Name of the Variable to track.
698 @param[in] VendorGuid Guid of the Variable to track.
699 @param[in] Volatile TRUE if volatile FALSE if non-volatile.
700 @param[in] Read TRUE if GetVariable() was called.
701 @param[in] Write TRUE if SetVariable() was called.
702 @param[in] Delete TRUE if deleted via SetVariable().
703 @param[in] Cache TRUE for a cache hit.
708 IN CHAR16
*VariableName
,
709 IN EFI_GUID
*VendorGuid
,
717 VARIABLE_INFO_ENTRY
*Entry
;
719 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
721 if (EfiAtRuntime ()) {
723 // Don't collect statistics at runtime
728 if (gVariableInfo
== NULL
) {
730 // on the first call allocate a entry and place a pointer to it in
731 // the EFI System Table
733 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
734 ASSERT (gVariableInfo
!= NULL
);
736 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
737 gVariableInfo
->Name
= AllocatePool (StrSize (VariableName
));
738 ASSERT (gVariableInfo
->Name
!= NULL
);
739 StrCpy (gVariableInfo
->Name
, VariableName
);
740 gVariableInfo
->Volatile
= Volatile
;
742 gBS
->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid
, gVariableInfo
);
746 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
747 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
748 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
750 // Find the entry matching both variable name and vender GUID,
751 // and update counters for all types.
760 Entry
->DeleteCount
++;
770 if (Entry
->Next
== NULL
) {
772 // If the entry is not in the table add it.
773 // Next iteration of the loop will fill in the data
775 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
776 ASSERT (Entry
->Next
!= NULL
);
778 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
779 Entry
->Next
->Name
= AllocatePool (StrSize (VariableName
));
780 ASSERT (Entry
->Next
->Name
!= NULL
);
781 StrCpy (Entry
->Next
->Name
, VariableName
);
782 Entry
->Next
->Volatile
= Volatile
;
790 Updates variable in cache.
792 This function searches the variable cache. If the variable to set exists in the cache,
793 it updates the variable in cache. It has the same parameters with UEFI SetVariable()
796 @param[in] VariableName A Null-terminated Unicode string that is the name of the vendor's
797 variable. Each VariableName is unique for each VendorGuid.
798 @param[in] VendorGuid A unique identifier for the vendor.
799 @param[in] Attributes Attributes bitmask to set for the variable.
800 @param[in] DataSize The size in bytes of the Data buffer. A size of zero causes the
801 variable to be deleted.
802 @param[in] Data The contents for the variable.
806 UpdateVariableCache (
807 IN CHAR16
*VariableName
,
808 IN EFI_GUID
*VendorGuid
,
809 IN UINT32 Attributes
,
814 VARIABLE_CACHE_ENTRY
*Entry
;
817 if (EfiAtRuntime ()) {
819 // Don't use the cache at runtime
825 // Searches cache for the variable to update. If it exists, update it.
827 for (Index
= 0, Entry
= mVariableCache
; Index
< sizeof (mVariableCache
)/sizeof (VARIABLE_CACHE_ENTRY
); Index
++, Entry
++) {
828 if (CompareGuid (VendorGuid
, Entry
->Guid
)) {
829 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
830 Entry
->Attributes
= Attributes
;
833 // If DataSize is 0, delete the variable.
835 if (Entry
->DataSize
!= 0) {
836 FreePool (Entry
->Data
);
838 Entry
->DataSize
= DataSize
;
839 } else if (DataSize
== Entry
->DataSize
) {
841 // If size of data does not change, simply copy data
843 CopyMem (Entry
->Data
, Data
, DataSize
);
846 // If size of data changes, allocate pool and copy data.
848 Entry
->Data
= AllocatePool (DataSize
);
849 Entry
->DataSize
= DataSize
;
850 CopyMem (Entry
->Data
, Data
, DataSize
);
859 Search the cache to check if the variable is in it.
861 This function searches the variable cache. If the variable to find exists, return its data
864 @param[in] VariableName A Null-terminated Unicode string that is the name of the vendor's
865 variable. Each VariableName is unique for each VendorGuid.
866 @param[in] VendorGuid A unique identifier for the vendor
867 @param[out] Attributes Pointer to the attributes bitmask of the variable for output.
868 @param[in, out] DataSize On input, size of the buffer of Data.
869 On output, size of the variable's data.
870 @param[out] Data Pointer to the data buffer for output.
872 @retval EFI_SUCCESS VariableGuid & VariableName data was returned.
873 @retval EFI_NOT_FOUND No matching variable found in cache.
874 @retval EFI_BUFFER_TOO_SMALL *DataSize is smaller than size of the variable's data to return.
878 FindVariableInCache (
879 IN CHAR16
*VariableName
,
880 IN EFI_GUID
*VendorGuid
,
881 OUT UINT32
*Attributes OPTIONAL
,
882 IN OUT UINTN
*DataSize
,
886 VARIABLE_CACHE_ENTRY
*Entry
;
889 if (EfiAtRuntime ()) {
891 // Don't use the cache at runtime
893 return EFI_NOT_FOUND
;
897 // Searches cache for the variable
899 for (Index
= 0, Entry
= mVariableCache
; Index
< sizeof (mVariableCache
)/sizeof (VARIABLE_CACHE_ENTRY
); Index
++, Entry
++) {
900 if (CompareGuid (VendorGuid
, Entry
->Guid
)) {
901 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
902 if (Entry
->DataSize
== 0) {
904 // Variable has been deleted so return EFI_NOT_FOUND
906 return EFI_NOT_FOUND
;
907 } else if (Entry
->DataSize
> *DataSize
) {
909 // If buffer is too small, return the size needed and EFI_BUFFER_TOO_SMALL
911 *DataSize
= Entry
->DataSize
;
912 return EFI_BUFFER_TOO_SMALL
;
915 // If buffer is large enough, return the data
917 *DataSize
= Entry
->DataSize
;
918 CopyMem (Data
, Entry
->Data
, Entry
->DataSize
);
920 // If Attributes is not NULL, return the variable's attribute.
922 if (Attributes
!= NULL
) {
923 *Attributes
= Entry
->Attributes
;
931 return EFI_NOT_FOUND
;
935 Finds variable in volatile and non-volatile storage areas.
937 This code finds variable in volatile and non-volatile storage areas.
938 If VariableName is an empty string, then we just return the first
939 qualified variable without comparing VariableName and VendorGuid.
940 Otherwise, VariableName and VendorGuid are compared.
942 @param[in] VariableName Name of the variable to be found.
943 @param[in] VendorGuid Vendor GUID to be found.
944 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,
945 including the range searched and the target position.
946 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including
947 base of volatile variable storage area, base of
948 NV variable storage area, and a lock.
949 @param[in] Instance Instance of FV Block services.
951 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
953 @retval EFI_SUCCESS Variable successfully found.
954 @retval EFI_INVALID_PARAMETER Variable not found.
959 IN CHAR16
*VariableName
,
960 IN EFI_GUID
*VendorGuid
,
961 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
962 IN VARIABLE_GLOBAL
*Global
,
966 EFI_PHYSICAL_ADDRESS Variable
[2];
967 EFI_PHYSICAL_ADDRESS InDeletedVariable
;
968 EFI_PHYSICAL_ADDRESS VariableStoreHeader
[2];
969 UINTN InDeletedStorageIndex
;
971 CHAR16 LocalVariableName
[MAX_NAME_SIZE
];
973 VARIABLE_HEADER VariableHeader
;
976 // 0: Volatile, 1: Non-Volatile
977 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
978 // make use of this mapping to implement search algorithme.
980 VariableStoreHeader
[0] = Global
->VolatileVariableBase
;
981 VariableStoreHeader
[1] = Global
->NonVolatileVariableBase
;
984 // Start Pointers for the variable.
985 // Actual Data Pointer where data can be written.
987 Variable
[0] = GetStartPointer (VariableStoreHeader
[0]);
988 Variable
[1] = GetStartPointer (VariableStoreHeader
[1]);
990 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
991 return EFI_INVALID_PARAMETER
;
995 // Find the variable by walk through volatile and then non-volatile variable store
997 InDeletedVariable
= 0x0;
998 InDeletedStorageIndex
= 0;
1000 for (Index
= 0; Index
< 2; Index
++) {
1004 while (IsValidVariableHeader (Variable
[Index
], Volatile
, Global
, Instance
, &VariableHeader
)) {
1005 if (VariableHeader
.State
== VAR_ADDED
||
1006 VariableHeader
.State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
1008 if (!EfiAtRuntime () || ((VariableHeader
.Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
1009 if (VariableName
[0] == 0) {
1011 // If VariableName is an empty string, then we just find the first qualified variable
1012 // without comparing VariableName and VendorGuid
1014 if (VariableHeader
.State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1016 // If variable is in delete transition, record it.
1018 InDeletedVariable
= Variable
[Index
];
1019 InDeletedStorageIndex
= Index
;
1022 // If variable is not in delete transition, return it.
1024 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
1025 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
], Volatile
, Global
, Instance
);
1026 PtrTrack
->CurrPtr
= Variable
[Index
];
1027 PtrTrack
->Volatile
= Volatile
;
1033 // If VariableName is not an empty string, then VariableName and VendorGuid are compared.
1035 if (CompareGuid (VendorGuid
, &VariableHeader
.VendorGuid
)) {
1036 GetVariableNamePtr (
1044 ASSERT (NameSizeOfVariable (&VariableHeader
) != 0);
1045 if (CompareMem (VariableName
, LocalVariableName
, NameSizeOfVariable (&VariableHeader
)) == 0) {
1046 if (VariableHeader
.State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1048 // If variable is in delete transition, record it.
1049 // We will use if only no VAR_ADDED variable is found.
1051 InDeletedVariable
= Variable
[Index
];
1052 InDeletedStorageIndex
= Index
;
1055 // If variable is not in delete transition, return it.
1057 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
1058 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
], Volatile
, Global
, Instance
);
1059 PtrTrack
->CurrPtr
= Variable
[Index
];
1060 PtrTrack
->Volatile
= Volatile
;
1070 Variable
[Index
] = GetNextVariablePtr (
1077 if (InDeletedVariable
!= 0x0) {
1079 // If no VAR_ADDED variable is found, and only variable in delete transition, then use this one.
1081 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[InDeletedStorageIndex
]);
1082 PtrTrack
->EndPtr
= GetEndPointer (
1083 VariableStoreHeader
[InDeletedStorageIndex
],
1084 (BOOLEAN
)(InDeletedStorageIndex
== 0),
1088 PtrTrack
->CurrPtr
= InDeletedVariable
;
1089 PtrTrack
->Volatile
= (BOOLEAN
)(InDeletedStorageIndex
== 0);
1093 PtrTrack
->CurrPtr
= 0x0;
1094 return EFI_NOT_FOUND
;
1098 Variable store garbage collection and reclaim operation.
1100 @param[in] VariableBase Base address of variable store area.
1101 @param[out] LastVariableOffset Offset of last variable.
1102 @param[in] IsVolatile The variable store is volatile or not,
1103 if it is non-volatile, need FTW.
1104 @param[in] VirtualMode Current calling mode for this function.
1105 @param[in] Global Context of this Extended SAL Variable Services Class call.
1106 @param[in] UpdatingVariable Pointer to header of the variable that is being updated.
1108 @retval EFI_SUCCESS Variable store successfully reclaimed.
1109 @retval EFI_OUT_OF_RESOURCES Fail to allocate memory buffer to hold all valid variables.
1114 IN EFI_PHYSICAL_ADDRESS VariableBase
,
1115 OUT UINTN
*LastVariableOffset
,
1116 IN BOOLEAN IsVolatile
,
1117 IN BOOLEAN VirtualMode
,
1118 IN ESAL_VARIABLE_GLOBAL
*Global
,
1119 IN EFI_PHYSICAL_ADDRESS UpdatingVariable
1122 EFI_PHYSICAL_ADDRESS Variable
;
1123 EFI_PHYSICAL_ADDRESS AddedVariable
;
1124 EFI_PHYSICAL_ADDRESS NextVariable
;
1125 EFI_PHYSICAL_ADDRESS NextAddedVariable
;
1126 VARIABLE_STORE_HEADER VariableStoreHeader
;
1127 VARIABLE_HEADER VariableHeader
;
1128 VARIABLE_HEADER AddedVariableHeader
;
1129 CHAR16 VariableName
[MAX_NAME_SIZE
];
1130 CHAR16 AddedVariableName
[MAX_NAME_SIZE
];
1132 UINTN MaximumBufferSize
;
1138 VARIABLE_GLOBAL
*VariableGlobal
;
1141 VariableGlobal
= &Global
->VariableGlobal
[VirtualMode
];
1142 Instance
= Global
->FvbInstance
;
1144 GetVarStoreHeader (VariableBase
, IsVolatile
, VariableGlobal
, Instance
, &VariableStoreHeader
);
1146 // recaluate the total size of Common/HwErr type variables in non-volatile area.
1149 Global
->CommonVariableTotalSize
= 0;
1150 Global
->HwErrVariableTotalSize
= 0;
1154 // Calculate the size of buffer needed to gather all valid variables
1156 Variable
= GetStartPointer (VariableBase
);
1157 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
1159 while (IsValidVariableHeader (Variable
, IsVolatile
, VariableGlobal
, Instance
, &VariableHeader
)) {
1160 NextVariable
= GetNextVariablePtr (Variable
, IsVolatile
, VariableGlobal
, Instance
);
1162 // Collect VAR_ADDED variables, and variables in delete transition status.
1164 if (VariableHeader
.State
== VAR_ADDED
||
1165 VariableHeader
.State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
1167 VariableSize
= NextVariable
- Variable
;
1168 MaximumBufferSize
+= VariableSize
;
1171 Variable
= NextVariable
;
1175 // Reserve the 1 Bytes with Oxff to identify the
1176 // end of the variable buffer.
1178 MaximumBufferSize
+= 1;
1179 ValidBuffer
= AllocatePool (MaximumBufferSize
);
1180 if (ValidBuffer
== NULL
) {
1181 return EFI_OUT_OF_RESOURCES
;
1184 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
1187 // Copy variable store header
1189 CopyMem (ValidBuffer
, &VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
1190 CurrPtr
= (UINT8
*) GetStartPointer ((EFI_PHYSICAL_ADDRESS
) ValidBuffer
);
1193 // Reinstall all ADDED variables
1195 Variable
= GetStartPointer (VariableBase
);
1196 while (IsValidVariableHeader (Variable
, IsVolatile
, VariableGlobal
, Instance
, &VariableHeader
)) {
1197 NextVariable
= GetNextVariablePtr (Variable
, IsVolatile
, VariableGlobal
, Instance
);
1198 if (VariableHeader
.State
== VAR_ADDED
) {
1199 VariableSize
= NextVariable
- Variable
;
1200 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
1201 CurrPtr
+= VariableSize
;
1202 if ((!IsVolatile
) && ((((VARIABLE_HEADER
*)Variable
)->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
1203 Global
->HwErrVariableTotalSize
+= VariableSize
;
1204 } else if ((!IsVolatile
) && ((((VARIABLE_HEADER
*)Variable
)->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
1205 Global
->CommonVariableTotalSize
+= VariableSize
;
1208 Variable
= NextVariable
;
1211 // Reinstall in delete transition variables
1213 Variable
= GetStartPointer (VariableBase
);
1214 while (IsValidVariableHeader (Variable
, IsVolatile
, VariableGlobal
, Instance
, &VariableHeader
)) {
1215 NextVariable
= GetNextVariablePtr (Variable
, IsVolatile
, VariableGlobal
, Instance
);
1216 if (VariableHeader
.State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1219 // Buffer has cached all ADDED variable.
1220 // Per IN_DELETED variable, we have to guarantee that
1221 // no ADDED one in previous buffer.
1224 AddedVariable
= GetStartPointer ((EFI_PHYSICAL_ADDRESS
) ValidBuffer
);
1225 while (IsValidVariableHeader (AddedVariable
, IsVolatile
, VariableGlobal
, Instance
, &AddedVariableHeader
)) {
1226 NextAddedVariable
= GetNextVariablePtr (AddedVariable
, IsVolatile
, VariableGlobal
, Instance
);
1227 NameSize
= NameSizeOfVariable (&AddedVariableHeader
);
1228 if (CompareGuid (&AddedVariableHeader
.VendorGuid
, &VariableHeader
.VendorGuid
) &&
1229 NameSize
== NameSizeOfVariable (&VariableHeader
)
1231 GetVariableNamePtr (Variable
, IsVolatile
, VariableGlobal
, Instance
, VariableName
);
1232 GetVariableNamePtr (AddedVariable
, IsVolatile
, VariableGlobal
, Instance
, AddedVariableName
);
1233 if (CompareMem (VariableName
, AddedVariableName
, NameSize
) == 0) {
1235 // If ADDED variable with the same name and vender GUID has been reinstalled,
1236 // then discard this IN_DELETED copy.
1242 AddedVariable
= NextAddedVariable
;
1245 // Add IN_DELETE variables that have not been added to buffer
1248 VariableSize
= NextVariable
- Variable
;
1249 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
1250 if (Variable
!= UpdatingVariable
) {
1252 // Make this IN_DELETE instance valid if:
1253 // 1. No valid instance of this variable exists.
1254 // 2. It is not the variable that is going to be updated.
1256 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
1258 CurrPtr
+= VariableSize
;
1259 if ((!IsVolatile
) && ((((VARIABLE_HEADER
*)Variable
)->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
1260 Global
->HwErrVariableTotalSize
+= VariableSize
;
1261 } else if ((!IsVolatile
) && ((((VARIABLE_HEADER
*)Variable
)->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
1262 Global
->CommonVariableTotalSize
+= VariableSize
;
1266 Variable
= NextVariable
;
1271 // If volatile variable store, just copy valid buffer
1273 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
.Size
, 0xff);
1274 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
));
1275 Status
= EFI_SUCCESS
;
1278 // If non-volatile variable store, perform FTW here.
1279 // Write ValidBuffer to destination specified by VariableBase.
1281 Status
= FtwVariableSpace (
1284 (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
)
1287 if (!EFI_ERROR (Status
)) {
1288 *LastVariableOffset
= (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
);
1290 *LastVariableOffset
= 0;
1293 FreePool (ValidBuffer
);
1299 Get index from supported language codes according to language string.
1301 This code is used to get corresponding index in supported language codes. It can handle
1302 RFC4646 and ISO639 language tags.
1303 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
1304 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
1307 SupportedLang = "engfraengfra"
1309 Iso639Language = TRUE
1310 The return value is "0".
1312 SupportedLang = "en;fr;en-US;fr-FR"
1314 Iso639Language = FALSE
1315 The return value is "3".
1317 @param[in] SupportedLang Platform supported language codes.
1318 @param[in] Lang Configured language.
1319 @param[in] Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1321 @return The index of language in the language codes.
1325 GetIndexFromSupportedLangCodes(
1326 IN CHAR8
*SupportedLang
,
1328 IN BOOLEAN Iso639Language
1332 UINTN CompareLength
;
1333 UINTN LanguageLength
;
1335 if (Iso639Language
) {
1336 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1337 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
1338 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
1340 // Successfully find the index of Lang string in SupportedLang string.
1342 Index
= Index
/ CompareLength
;
1350 // Compare RFC4646 language code
1353 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
1355 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
1357 // Skip ';' characters in SupportedLang
1359 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
1361 // Determine the length of the next language code in SupportedLang
1363 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
1365 if ((CompareLength
== LanguageLength
) &&
1366 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
1368 // Successfully find the index of Lang string in SupportedLang string.
1379 Get language string from supported language codes according to index.
1381 This code is used to get corresponding language string in supported language codes. It can handle
1382 RFC4646 and ISO639 language tags.
1383 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
1384 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
1387 SupportedLang = "engfraengfra"
1389 Iso639Language = TRUE
1390 The return value is "fra".
1392 SupportedLang = "en;fr;en-US;fr-FR"
1394 Iso639Language = FALSE
1395 The return value is "fr".
1397 @param[in] SupportedLang Platform supported language codes.
1398 @param[in] Index the index in supported language codes.
1399 @param[in] Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1400 @param[in] VirtualMode Current calling mode for this function.
1401 @param[in] Global Context of this Extended SAL Variable Services Class call.
1403 @return The language string in the language codes.
1407 GetLangFromSupportedLangCodes (
1408 IN CHAR8
*SupportedLang
,
1410 IN BOOLEAN Iso639Language
,
1411 IN BOOLEAN VirtualMode
,
1412 IN ESAL_VARIABLE_GLOBAL
*Global
1416 UINTN CompareLength
;
1420 Supported
= SupportedLang
;
1421 if (Iso639Language
) {
1423 // according to the index of Lang string in SupportedLang string to get the language.
1424 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1425 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1427 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1428 Global
->Lang
[CompareLength
] = '\0';
1429 return CopyMem (Global
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
1434 // take semicolon as delimitation, sequentially traverse supported language codes.
1436 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
1439 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
1441 // Have completed the traverse, but not find corrsponding string.
1442 // This case is not allowed to happen.
1447 if (SubIndex
== Index
) {
1449 // according to the index of Lang string in SupportedLang string to get the language.
1450 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1451 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1453 Global
->PlatformLang
[VirtualMode
][CompareLength
] = '\0';
1454 return CopyMem (Global
->PlatformLang
[VirtualMode
], Supported
- CompareLength
, CompareLength
);
1459 // Skip ';' characters in Supported
1461 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1467 Returns a pointer to an allocated buffer that contains the best matching language
1468 from a set of supported languages.
1470 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1471 code types may not be mixed in a single call to this function. This function
1472 supports a variable argument list that allows the caller to pass in a prioritized
1473 list of language codes to test against all the language codes in SupportedLanguages.
1475 If SupportedLanguages is NULL, then ASSERT().
1477 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1478 contains a set of language codes in the format
1479 specified by Iso639Language.
1480 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1481 in ISO 639-2 format. If FALSE, then all language
1482 codes are assumed to be in RFC 4646 language format.
1483 @param[in] VirtualMode Current calling mode for this function.
1484 @param[in] ... A variable argument list that contains pointers to
1485 Null-terminated ASCII strings that contain one or more
1486 language codes in the format specified by Iso639Language.
1487 The first language code from each of these language
1488 code lists is used to determine if it is an exact or
1489 close match to any of the language codes in
1490 SupportedLanguages. Close matches only apply to RFC 4646
1491 language codes, and the matching algorithm from RFC 4647
1492 is used to determine if a close match is present. If
1493 an exact or close match is found, then the matching
1494 language code from SupportedLanguages is returned. If
1495 no matches are found, then the next variable argument
1496 parameter is evaluated. The variable argument list
1497 is terminated by a NULL.
1499 @retval NULL The best matching language could not be found in SupportedLanguages.
1500 @retval NULL There are not enough resources available to return the best matching
1502 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1503 language in SupportedLanguages.
1507 VariableGetBestLanguage (
1508 IN CONST CHAR8
*SupportedLanguages
,
1509 IN BOOLEAN Iso639Language
,
1510 IN BOOLEAN VirtualMode
,
1516 UINTN CompareLength
;
1517 UINTN LanguageLength
;
1518 CONST CHAR8
*Supported
;
1521 ASSERT (SupportedLanguages
!= NULL
);
1523 VA_START (Args
, VirtualMode
);
1524 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1526 // Default to ISO 639-2 mode
1529 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1532 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1534 if (!Iso639Language
) {
1535 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1539 // Trim back the length of Language used until it is empty
1541 while (LanguageLength
> 0) {
1543 // Loop through all language codes in SupportedLanguages
1545 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1547 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1549 if (!Iso639Language
) {
1551 // Skip ';' characters in Supported
1553 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1555 // Determine the length of the next language code in Supported
1557 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1559 // If Language is longer than the Supported, then skip to the next language
1561 if (LanguageLength
> CompareLength
) {
1566 // See if the first LanguageLength characters in Supported match Language
1568 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1571 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
[VirtualMode
];
1572 Buffer
[CompareLength
] = '\0';
1573 return CopyMem (Buffer
, Supported
, CompareLength
);
1577 if (Iso639Language
) {
1579 // If ISO 639 mode, then each language can only be tested once
1584 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1586 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1593 // No matches were found
1599 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1601 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1602 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1603 and are read-only. Therefore, in variable driver, only store the original value for other use.
1605 @param[in] VariableName Name of variable.
1606 @param[in] Data Variable data.
1607 @param[in] DataSize Size of data. 0 means delete.
1608 @param[in] VirtualMode Current calling mode for this function.
1609 @param[in] Global Context of this Extended SAL Variable Services Class call.
1613 AutoUpdateLangVariable(
1614 IN CHAR16
*VariableName
,
1617 IN BOOLEAN VirtualMode
,
1618 IN ESAL_VARIABLE_GLOBAL
*Global
1622 CHAR8
*BestPlatformLang
;
1626 VARIABLE_POINTER_TRACK Variable
;
1627 BOOLEAN SetLanguageCodes
;
1628 CHAR16
**PredefinedVariableName
;
1629 VARIABLE_GLOBAL
*VariableGlobal
;
1633 // Don't do updates for delete operation
1635 if (DataSize
== 0) {
1639 SetLanguageCodes
= FALSE
;
1640 VariableGlobal
= &Global
->VariableGlobal
[VirtualMode
];
1641 Instance
= Global
->FvbInstance
;
1644 PredefinedVariableName
= &Global
->VariableName
[VirtualMode
][0];
1645 if (StrCmp (VariableName
, PredefinedVariableName
[VAR_PLATFORM_LANG_CODES
]) == 0) {
1647 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1649 if (EfiAtRuntime ()) {
1653 SetLanguageCodes
= TRUE
;
1656 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1657 // Therefore, in variable driver, only store the original value for other use.
1659 if (Global
->PlatformLangCodes
[VirtualMode
] != NULL
) {
1660 FreePool (Global
->PlatformLangCodes
[VirtualMode
]);
1662 Global
->PlatformLangCodes
[VirtualMode
] = AllocateRuntimeCopyPool (DataSize
, Data
);
1663 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
[VirtualMode
] != NULL
);
1666 // PlatformLang holds a single language from PlatformLangCodes,
1667 // so the size of PlatformLangCodes is enough for the PlatformLang.
1669 if (Global
->PlatformLang
[VirtualMode
] != NULL
) {
1670 FreePool (Global
->PlatformLang
[VirtualMode
]);
1672 Global
->PlatformLang
[VirtualMode
] = AllocateRuntimePool (DataSize
);
1673 ASSERT (Global
->PlatformLang
[VirtualMode
] != NULL
);
1675 } else if (StrCmp (VariableName
, PredefinedVariableName
[VAR_LANG_CODES
]) == 0) {
1677 // LangCodes is a volatile variable, so it can not be updated at runtime.
1679 if (EfiAtRuntime ()) {
1683 SetLanguageCodes
= TRUE
;
1686 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1687 // Therefore, in variable driver, only store the original value for other use.
1689 if (Global
->LangCodes
[VirtualMode
] != NULL
) {
1690 FreePool (Global
->LangCodes
[VirtualMode
]);
1692 Global
->LangCodes
[VirtualMode
] = AllocateRuntimeCopyPool (DataSize
, Data
);
1693 ASSERT (Global
->LangCodes
[VirtualMode
] != NULL
);
1696 if (SetLanguageCodes
1697 && (Global
->PlatformLangCodes
[VirtualMode
] != NULL
)
1698 && (Global
->LangCodes
[VirtualMode
] != NULL
)) {
1700 // Update Lang if PlatformLang is already set
1701 // Update PlatformLang if Lang is already set
1703 Status
= FindVariable (PredefinedVariableName
[VAR_PLATFORM_LANG
], Global
->GlobalVariableGuid
[VirtualMode
], &Variable
, VariableGlobal
, Instance
);
1704 if (!EFI_ERROR (Status
)) {
1708 VariableName
= PredefinedVariableName
[VAR_PLATFORM_LANG
];
1710 Status
= FindVariable (PredefinedVariableName
[VAR_LANG
], Global
->GlobalVariableGuid
[VirtualMode
], &Variable
, VariableGlobal
, Instance
);
1711 if (!EFI_ERROR (Status
)) {
1713 // Update PlatformLang
1715 VariableName
= PredefinedVariableName
[VAR_LANG
];
1718 // Neither PlatformLang nor Lang is set, directly return
1723 Data
= (VOID
*) GetEndPointer (VariableGlobal
->VolatileVariableBase
, TRUE
, VariableGlobal
, Instance
);
1724 GetVariableDataPtr ((EFI_PHYSICAL_ADDRESS
) Variable
.CurrPtr
, Variable
.Volatile
, VariableGlobal
, Instance
, (CHAR16
*) Data
);
1726 Status
= AccessVariableStore (
1731 (UINTN
) &(((VARIABLE_HEADER
*)Variable
.CurrPtr
)->DataSize
),
1735 ASSERT_EFI_ERROR (Status
);
1739 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1741 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1743 if (StrCmp (VariableName
, PredefinedVariableName
[VAR_PLATFORM_LANG
]) == 0) {
1745 // Update Lang when PlatformLangCodes/LangCodes were set.
1747 if ((Global
->PlatformLangCodes
[VirtualMode
] != NULL
) && (Global
->LangCodes
[VirtualMode
] != NULL
)) {
1749 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1751 BestPlatformLang
= VariableGetBestLanguage (Global
->PlatformLangCodes
[VirtualMode
], FALSE
, VirtualMode
, Data
, NULL
);
1752 if (BestPlatformLang
!= NULL
) {
1754 // Get the corresponding index in language codes.
1756 Index
= GetIndexFromSupportedLangCodes (Global
->PlatformLangCodes
[VirtualMode
], BestPlatformLang
, FALSE
);
1759 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1761 BestLang
= GetLangFromSupportedLangCodes (Global
->LangCodes
[VirtualMode
], Index
, TRUE
, VirtualMode
, Global
);
1764 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1766 FindVariable (PredefinedVariableName
[VAR_LANG
], Global
->GlobalVariableGuid
[VirtualMode
], &Variable
, VariableGlobal
, Instance
);
1768 Status
= UpdateVariable (
1769 PredefinedVariableName
[VAR_LANG
],
1770 Global
->GlobalVariableGuid
[VirtualMode
],
1772 ISO_639_2_ENTRY_SIZE
+ 1,
1781 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
1783 ASSERT_EFI_ERROR (Status
);
1787 } else if (StrCmp (VariableName
, PredefinedVariableName
[VAR_LANG
]) == 0) {
1789 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1791 if ((Global
->PlatformLangCodes
[VirtualMode
] != NULL
) && (Global
->LangCodes
[VirtualMode
] != NULL
)) {
1793 // When setting Lang, firstly get most matched language string from supported language codes.
1795 BestLang
= VariableGetBestLanguage (Global
->LangCodes
[VirtualMode
], TRUE
, VirtualMode
, Data
, NULL
);
1796 if (BestLang
!= NULL
) {
1798 // Get the corresponding index in language codes.
1800 Index
= GetIndexFromSupportedLangCodes (Global
->LangCodes
[VirtualMode
], BestLang
, TRUE
);
1803 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1805 BestPlatformLang
= GetLangFromSupportedLangCodes (Global
->PlatformLangCodes
[VirtualMode
], Index
, FALSE
, VirtualMode
, Global
);
1808 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1810 FindVariable (PredefinedVariableName
[VAR_PLATFORM_LANG
], Global
->GlobalVariableGuid
[VirtualMode
], &Variable
, VariableGlobal
, Instance
);
1812 Status
= UpdateVariable (
1813 PredefinedVariableName
[VAR_PLATFORM_LANG
],
1814 Global
->GlobalVariableGuid
[VirtualMode
],
1816 AsciiStrSize (BestPlatformLang
),
1825 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
1826 ASSERT_EFI_ERROR (Status
);
1833 Update the variable region with Variable information. These are the same
1834 arguments as the EFI Variable services.
1836 @param[in] VariableName Name of variable.
1837 @param[in] VendorGuid Guid of variable.
1838 @param[in] Data Variable data.
1839 @param[in] DataSize Size of data. 0 means delete.
1840 @param[in] Attributes Attributes of the variable.
1841 @param[in] KeyIndex Index of associated public key.
1842 @param[in] MonotonicCount Value of associated monotonic count.
1843 @param[in] VirtualMode Current calling mode for this function.
1844 @param[in] Global Context of this Extended SAL Variable Services Class call.
1845 @param[in] Variable The variable information which is used to keep track of variable usage.
1847 @retval EFI_SUCCESS The update operation is success.
1848 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1854 IN CHAR16
*VariableName
,
1855 IN EFI_GUID
*VendorGuid
,
1858 IN UINT32 Attributes OPTIONAL
,
1859 IN UINT32 KeyIndex OPTIONAL
,
1860 IN UINT64 MonotonicCount OPTIONAL
,
1861 IN BOOLEAN VirtualMode
,
1862 IN ESAL_VARIABLE_GLOBAL
*Global
,
1863 IN VARIABLE_POINTER_TRACK
*Variable
1867 VARIABLE_HEADER
*NextVariable
;
1868 UINTN VarNameOffset
;
1869 UINTN VarDataOffset
;
1874 VARIABLE_HEADER VariableHeader
;
1875 VARIABLE_HEADER
*NextVariableHeader
;
1878 VARIABLE_STORE_HEADER VariableStoreHeader
;
1880 VARIABLE_GLOBAL
*VariableGlobal
;
1883 VariableGlobal
= &Global
->VariableGlobal
[VirtualMode
];
1884 Instance
= Global
->FvbInstance
;
1888 if (Variable
->CurrPtr
!= 0) {
1890 Valid
= IsValidVariableHeader (Variable
->CurrPtr
, Variable
->Volatile
, VariableGlobal
, Instance
, &VariableHeader
);
1892 Status
= EFI_NOT_FOUND
;
1897 // Update/Delete existing variable
1899 Volatile
= Variable
->Volatile
;
1901 if (EfiAtRuntime ()) {
1903 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
1904 // the volatile is ReadOnly, and SetVariable should be aborted and
1905 // return EFI_WRITE_PROTECTED.
1907 if (Variable
->Volatile
) {
1908 Status
= EFI_WRITE_PROTECTED
;
1912 // Only variable have NV attribute can be updated/deleted in Runtime
1914 if ((VariableHeader
.Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1915 Status
= EFI_INVALID_PARAMETER
;
1920 // Setting a data variable with no access, or zero DataSize attributes
1921 // specified causes it to be deleted.
1923 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1924 State
= VariableHeader
.State
;
1925 State
&= VAR_DELETED
;
1927 Status
= AccessVariableStore (
1932 (UINTN
) &(((VARIABLE_HEADER
*)Variable
->CurrPtr
)->State
),
1936 if (!EFI_ERROR (Status
)) {
1937 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1938 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1943 // Logic comes here to update variable.
1944 // If the variable is marked valid and the same data has been passed in
1945 // then return to the caller immediately.
1947 if (DataSizeOfVariable (&VariableHeader
) == DataSize
) {
1948 NextVariable
= (VARIABLE_HEADER
*)GetEndPointer (VariableGlobal
->VolatileVariableBase
, TRUE
, VariableGlobal
, Instance
);
1949 GetVariableDataPtr (Variable
->CurrPtr
, Variable
->Volatile
, VariableGlobal
, Instance
, (CHAR16
*) NextVariable
);
1950 if (CompareMem (Data
, (VOID
*) NextVariable
, DataSize
) == 0) {
1951 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1952 Status
= EFI_SUCCESS
;
1956 if ((VariableHeader
.State
== VAR_ADDED
) ||
1957 (VariableHeader
.State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1959 // If new data is different from the old one, mark the old one as VAR_IN_DELETED_TRANSITION.
1960 // It will be deleted if new variable is successfully written.
1962 State
= VariableHeader
.State
;
1963 State
&= VAR_IN_DELETED_TRANSITION
;
1965 Status
= AccessVariableStore (
1970 (UINTN
) &(((VARIABLE_HEADER
*)Variable
->CurrPtr
)->State
),
1974 if (EFI_ERROR (Status
)) {
1980 // Create a new variable
1984 // Make sure we are trying to create a new variable.
1985 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
1987 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1988 Status
= EFI_NOT_FOUND
;
1993 // Only variable have NV|RT attribute can be created in Runtime
1995 if (EfiAtRuntime () &&
1996 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1997 Status
= EFI_INVALID_PARAMETER
;
2003 // Function part - create a new variable and copy the data.
2004 // Both update a variable and create a variable will come here.
2006 // Tricky part: Use scratch data area at the end of volatile variable store
2007 // as a temporary storage.
2009 NextVariable
= (VARIABLE_HEADER
*)GetEndPointer (VariableGlobal
->VolatileVariableBase
, TRUE
, VariableGlobal
, Instance
);
2010 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
2011 NextVariableHeader
= (VARIABLE_HEADER
*) NextVariable
;
2013 SetMem (NextVariableHeader
, ScratchSize
, 0xff);
2015 NextVariableHeader
->StartId
= VARIABLE_DATA
;
2016 NextVariableHeader
->Attributes
= Attributes
;
2017 NextVariableHeader
->PubKeyIndex
= KeyIndex
;
2018 NextVariableHeader
->MonotonicCount
= MonotonicCount
;
2019 NextVariableHeader
->Reserved
= 0;
2020 VarNameOffset
= sizeof (VARIABLE_HEADER
);
2021 VarNameSize
= StrSize (VariableName
);
2023 (UINT8
*) ((UINTN
)NextVariable
+ VarNameOffset
),
2027 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
2029 (UINT8
*) ((UINTN
)NextVariable
+ VarDataOffset
),
2033 CopyMem (&NextVariableHeader
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
2035 // There will be pad bytes after Data, the NextVariable->NameSize and
2036 // NextVariable->DataSize should not include pad size so that variable
2037 // service can get actual size in GetVariable.
2039 NextVariableHeader
->NameSize
= (UINT32
)VarNameSize
;
2040 NextVariableHeader
->DataSize
= (UINT32
)DataSize
;
2043 // The actual size of the variable that stores in storage should
2044 // include pad size.
2046 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
2047 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2049 // Create a nonvolatile variable
2053 GetVarStoreHeader (VariableGlobal
->NonVolatileVariableBase
, FALSE
, VariableGlobal
, Instance
, &VariableStoreHeader
);
2054 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
2055 && ((HEADER_ALIGN (VarSize
) + Global
->HwErrVariableTotalSize
) > PcdGet32(PcdHwErrStorageSize
)))
2056 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
2057 && ((HEADER_ALIGN (VarSize
) + Global
->CommonVariableTotalSize
) > VariableStoreHeader
.Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
)))) {
2058 if (EfiAtRuntime ()) {
2059 Status
= EFI_OUT_OF_RESOURCES
;
2063 // Perform garbage collection & reclaim operation
2065 Status
= Reclaim (VariableGlobal
->NonVolatileVariableBase
, &(Global
->NonVolatileLastVariableOffset
), FALSE
, VirtualMode
, Global
, Variable
->CurrPtr
);
2066 if (EFI_ERROR (Status
)) {
2072 // If still no enough space, return out of resources
2074 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
2075 && ((HEADER_ALIGN (VarSize
) + Global
->HwErrVariableTotalSize
) > PcdGet32(PcdHwErrStorageSize
)))
2076 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
2077 && ((HEADER_ALIGN (VarSize
) + Global
->CommonVariableTotalSize
) > VariableStoreHeader
.Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
)))) {
2078 Status
= EFI_OUT_OF_RESOURCES
;
2084 // 1. Write variable header
2085 // 2. Set variable state to header valid
2086 // 3. Write variable data
2087 // 4. Set variable state to valid
2092 Status
= AccessVariableStore (
2097 VariableGlobal
->NonVolatileVariableBase
+ Global
->NonVolatileLastVariableOffset
,
2098 sizeof (VARIABLE_HEADER
),
2099 (UINT8
*) NextVariable
2102 if (EFI_ERROR (Status
)) {
2109 NextVariableHeader
->State
= VAR_HEADER_VALID_ONLY
;
2110 Status
= AccessVariableStore (
2115 VariableGlobal
->NonVolatileVariableBase
+ Global
->NonVolatileLastVariableOffset
,
2116 sizeof (VARIABLE_HEADER
),
2117 (UINT8
*) NextVariable
2120 if (EFI_ERROR (Status
)) {
2126 Status
= AccessVariableStore (
2131 VariableGlobal
->NonVolatileVariableBase
+ Global
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
2132 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
2133 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
2136 if (EFI_ERROR (Status
)) {
2142 NextVariableHeader
->State
= VAR_ADDED
;
2143 Status
= AccessVariableStore (
2148 VariableGlobal
->NonVolatileVariableBase
+ Global
->NonVolatileLastVariableOffset
,
2149 sizeof (VARIABLE_HEADER
),
2150 (UINT8
*) NextVariable
2153 if (EFI_ERROR (Status
)) {
2157 Global
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2159 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
2160 Global
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2162 Global
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2166 // Create a volatile variable
2170 if ((UINT32
) (HEADER_ALIGN(VarSize
) + Global
->VolatileLastVariableOffset
) >
2171 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (VariableGlobal
->VolatileVariableBase
)))->Size
) {
2173 // Perform garbage collection & reclaim operation
2175 Status
= Reclaim (VariableGlobal
->VolatileVariableBase
, &Global
->VolatileLastVariableOffset
, TRUE
, VirtualMode
, Global
, Variable
->CurrPtr
);
2176 if (EFI_ERROR (Status
)) {
2180 // If still no enough space, return out of resources
2182 if ((UINT32
) (HEADER_ALIGN (VarSize
) + Global
->VolatileLastVariableOffset
) >
2183 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (VariableGlobal
->VolatileVariableBase
)))->Size
2185 Status
= EFI_OUT_OF_RESOURCES
;
2191 NextVariableHeader
->State
= VAR_ADDED
;
2192 Status
= AccessVariableStore (
2197 VariableGlobal
->VolatileVariableBase
+ Global
->VolatileLastVariableOffset
,
2199 (UINT8
*) NextVariable
2202 if (EFI_ERROR (Status
)) {
2206 Global
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2209 // Mark the old variable as deleted
2210 // If storage has just been reclaimed, the old variable marked as VAR_IN_DELETED_TRANSITION
2211 // has already been eliminated, so no need to delete it.
2213 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
->CurrPtr
!= 0) {
2214 State
= ((VARIABLE_HEADER
*)Variable
->CurrPtr
)->State
;
2215 State
&= VAR_DELETED
;
2217 Status
= AccessVariableStore (
2222 (UINTN
) &(((VARIABLE_HEADER
*)Variable
->CurrPtr
)->State
),
2228 if (!EFI_ERROR (Status
)) {
2229 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
2230 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
2238 Implements EsalGetVariable function of Extended SAL Variable Services Class.
2240 This function implements EsalGetVariable function of Extended SAL Variable Services Class.
2241 It is equivalent in functionality to the EFI Runtime Service GetVariable().
2243 @param[in] VariableName A Null-terminated Unicode string that is the name of
2244 the vendor's variable.
2245 @param[in] VendorGuid A unique identifier for the vendor.
2246 @param[out] Attributes If not NULL, a pointer to the memory location to return the
2247 attributes bitmask for the variable.
2248 @param[in, out] DataSize Size of Data found. If size is less than the
2249 data, this value contains the required size.
2250 @param[out] Data On input, the size in bytes of the return Data buffer.
2251 On output, the size of data returned in Data.
2252 @param[in] VirtualMode Current calling mode for this function.
2253 @param[in] Global Context of this Extended SAL Variable Services Class call.
2255 @retval EFI_SUCCESS The function completed successfully.
2256 @retval EFI_NOT_FOUND The variable was not found.
2257 @retval EFI_BUFFER_TOO_SMALL DataSize is too small for the result. DataSize has
2258 been updated with the size needed to complete the request.
2259 @retval EFI_INVALID_PARAMETER VariableName is NULL.
2260 @retval EFI_INVALID_PARAMETER VendorGuid is NULL.
2261 @retval EFI_INVALID_PARAMETER DataSize is NULL.
2262 @retval EFI_INVALID_PARAMETER DataSize is not too small and Data is NULL.
2263 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
2264 @retval EFI_SECURITY_VIOLATION The variable could not be retrieved due to an authentication failure.
2270 IN CHAR16
*VariableName
,
2271 IN EFI_GUID
*VendorGuid
,
2272 OUT UINT32
*Attributes OPTIONAL
,
2273 IN OUT UINTN
*DataSize
,
2275 IN BOOLEAN VirtualMode
,
2276 IN ESAL_VARIABLE_GLOBAL
*Global
2279 VARIABLE_POINTER_TRACK Variable
;
2282 VARIABLE_HEADER VariableHeader
;
2284 VARIABLE_GLOBAL
*VariableGlobal
;
2287 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
2288 return EFI_INVALID_PARAMETER
;
2291 VariableGlobal
= &Global
->VariableGlobal
[VirtualMode
];
2292 Instance
= Global
->FvbInstance
;
2294 AcquireLockOnlyAtBootTime(&VariableGlobal
->VariableServicesLock
);
2297 // Check if this variable exists in cache.
2299 Status
= FindVariableInCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
2300 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_SUCCESS
)){
2302 // If variable exists in cache, just update statistical information for it and finish.
2303 // Here UpdateVariableInfo() has already retrieved data & attributes for output.
2305 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, TRUE
, FALSE
, FALSE
, TRUE
);
2309 // If variable does not exist in cache, search for it in variable storage area.
2311 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, VariableGlobal
, Instance
);
2312 if (Variable
.CurrPtr
== 0x0 || EFI_ERROR (Status
)) {
2314 // If it cannot be found in variable storage area, goto Done.
2319 Valid
= IsValidVariableHeader (Variable
.CurrPtr
, Variable
.Volatile
, VariableGlobal
, Instance
, &VariableHeader
);
2321 Status
= EFI_NOT_FOUND
;
2325 // If variable exists, but not in cache, get its data and attributes, update
2326 // statistical information, and update cache.
2328 VarDataSize
= DataSizeOfVariable (&VariableHeader
);
2329 ASSERT (VarDataSize
!= 0);
2331 if (*DataSize
>= VarDataSize
) {
2333 Status
= EFI_INVALID_PARAMETER
;
2337 GetVariableDataPtr (
2344 if (Attributes
!= NULL
) {
2345 *Attributes
= VariableHeader
.Attributes
;
2348 *DataSize
= VarDataSize
;
2349 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
2350 UpdateVariableCache (VariableName
, VendorGuid
, VariableHeader
.Attributes
, VarDataSize
, Data
);
2352 Status
= EFI_SUCCESS
;
2356 // If DataSize is too small for the result, return EFI_BUFFER_TOO_SMALL.
2358 *DataSize
= VarDataSize
;
2359 Status
= EFI_BUFFER_TOO_SMALL
;
2364 ReleaseLockOnlyAtBootTime (&VariableGlobal
->VariableServicesLock
);
2369 Implements EsalGetNextVariableName function of Extended SAL Variable Services Class.
2371 This function implements EsalGetNextVariableName function of Extended SAL Variable Services Class.
2372 It is equivalent in functionality to the EFI Runtime Service GetNextVariableName().
2374 @param[in, out] VariableNameSize Size of the variable
2375 @param[in, out] VariableName On input, supplies the last VariableName that was returned by GetNextVariableName().
2376 On output, returns the Null-terminated Unicode string of the current variable.
2377 @param[in, out] VendorGuid On input, supplies the last VendorGuid that was returned by GetNextVariableName().
2378 On output, returns the VendorGuid of the current variable.
2379 @param[in] VirtualMode Current calling mode for this function.
2380 @param[in] Global Context of this Extended SAL Variable Services Class call.
2382 @retval EFI_SUCCESS The function completed successfully.
2383 @retval EFI_NOT_FOUND The next variable was not found.
2384 @retval EFI_BUFFER_TOO_SMALL VariableNameSize is too small for the result.
2385 VariableNameSize has been updated with the size needed to complete the request.
2386 @retval EFI_INVALID_PARAMETER VariableNameSize is NULL.
2387 @retval EFI_INVALID_PARAMETER VariableName is NULL.
2388 @retval EFI_INVALID_PARAMETER VendorGuid is NULL.
2389 @retval EFI_DEVICE_ERROR The variable name could not be retrieved due to a hardware error.
2394 EsalGetNextVariableName (
2395 IN OUT UINTN
*VariableNameSize
,
2396 IN OUT CHAR16
*VariableName
,
2397 IN OUT EFI_GUID
*VendorGuid
,
2398 IN BOOLEAN VirtualMode
,
2399 IN ESAL_VARIABLE_GLOBAL
*Global
2402 VARIABLE_POINTER_TRACK Variable
;
2405 VARIABLE_HEADER VariableHeader
;
2406 VARIABLE_GLOBAL
*VariableGlobal
;
2409 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
2410 return EFI_INVALID_PARAMETER
;
2413 VariableGlobal
= &Global
->VariableGlobal
[VirtualMode
];
2414 Instance
= Global
->FvbInstance
;
2416 AcquireLockOnlyAtBootTime(&VariableGlobal
->VariableServicesLock
);
2418 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, VariableGlobal
, Instance
);
2420 // If the variable does not exist, goto Done and return.
2422 if (Variable
.CurrPtr
== 0x0 || EFI_ERROR (Status
)) {
2426 if (VariableName
[0] != 0) {
2428 // If variable name is not NULL, get next variable
2430 Variable
.CurrPtr
= GetNextVariablePtr (
2439 if (Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== 0x0) {
2441 // If fail to find a variable in current area, reverse the volatile attribute of area to search.
2443 Variable
.Volatile
= (BOOLEAN
) (Variable
.Volatile
^ ((BOOLEAN
) 0x1));
2445 // Here we depend on the searching sequence of FindVariable().
2446 // It first searches volatile area, then NV area.
2447 // So if the volatile attribute after switching is non-volatile, it means that we have finished searching volatile area,
2448 // and EFI_NOT_FOUND is returnd.
2449 // Otherwise, it means that we have finished searchig non-volatile area, and we will continue to search volatile area.
2451 if (!Variable
.Volatile
) {
2452 Variable
.StartPtr
= GetStartPointer (VariableGlobal
->NonVolatileVariableBase
);
2453 Variable
.EndPtr
= GetEndPointer (VariableGlobal
->NonVolatileVariableBase
, FALSE
, VariableGlobal
, Instance
);
2455 Status
= EFI_NOT_FOUND
;
2459 Variable
.CurrPtr
= Variable
.StartPtr
;
2460 if (!IsValidVariableHeader (Variable
.CurrPtr
, Variable
.Volatile
, VariableGlobal
, Instance
, NULL
)) {
2465 // Variable is found
2467 if (IsValidVariableHeader (Variable
.CurrPtr
, Variable
.Volatile
, VariableGlobal
, Instance
, &VariableHeader
)) {
2468 if ((VariableHeader
.State
== VAR_ADDED
) &&
2469 (!(EfiAtRuntime () && ((VariableHeader
.Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)))) {
2470 VarNameSize
= NameSizeOfVariable (&VariableHeader
);
2471 ASSERT (VarNameSize
!= 0);
2473 if (VarNameSize
<= *VariableNameSize
) {
2474 GetVariableNamePtr (
2483 &VariableHeader
.VendorGuid
,
2486 Status
= EFI_SUCCESS
;
2488 Status
= EFI_BUFFER_TOO_SMALL
;
2491 *VariableNameSize
= VarNameSize
;
2496 Variable
.CurrPtr
= GetNextVariablePtr (
2505 ReleaseLockOnlyAtBootTime (&VariableGlobal
->VariableServicesLock
);
2510 Implements EsalSetVariable function of Extended SAL Variable Services Class.
2512 This function implements EsalSetVariable function of Extended SAL Variable Services Class.
2513 It is equivalent in functionality to the EFI Runtime Service SetVariable().
2515 @param[in] VariableName A Null-terminated Unicode string that is the name of the vendor's
2516 variable. Each VariableName is unique for each
2517 VendorGuid. VariableName must contain 1 or more
2518 Unicode characters. If VariableName is an empty Unicode
2519 string, then EFI_INVALID_PARAMETER is returned.
2520 @param[in] VendorGuid A unique identifier for the vendor.
2521 @param[in] Attributes Attributes bitmask to set for the variable.
2522 @param[in] DataSize The size in bytes of the Data buffer. A size of zero causes the
2523 variable to be deleted.
2524 @param[in] Data The contents for the variable.
2525 @param[in] VirtualMode Current calling mode for this function.
2526 @param[in] Global Context of this Extended SAL Variable Services Class call.
2528 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
2529 defined by the Attributes.
2530 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied, or the
2531 DataSize exceeds the maximum allowed.
2532 @retval EFI_INVALID_PARAMETER VariableName is an empty Unicode string.
2533 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
2534 @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.
2535 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
2536 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
2537 @retval EFI_SECURITY_VIOLATION The variable could not be retrieved due to an authentication failure.
2538 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
2544 IN CHAR16
*VariableName
,
2545 IN EFI_GUID
*VendorGuid
,
2546 IN UINT32 Attributes
,
2549 IN BOOLEAN VirtualMode
,
2550 IN ESAL_VARIABLE_GLOBAL
*Global
2553 VARIABLE_POINTER_TRACK Variable
;
2555 EFI_PHYSICAL_ADDRESS NextVariable
;
2556 EFI_PHYSICAL_ADDRESS Point
;
2557 VARIABLE_GLOBAL
*VariableGlobal
;
2560 UINT64 MonotonicCount
;
2564 // Check input parameters
2566 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2567 return EFI_INVALID_PARAMETER
;
2570 if (DataSize
!= 0 && Data
== NULL
) {
2571 return EFI_INVALID_PARAMETER
;
2575 // EFI_VARIABLE_RUNTIME_ACCESS bit cannot be set without EFI_VARIABLE_BOOTSERVICE_ACCESS bit.
2577 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2578 return EFI_INVALID_PARAMETER
;
2581 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) {
2582 if (DataSize
< AUTHINFO_SIZE
) {
2584 // Try to write Authencated Variable without AuthInfo
2586 return EFI_SECURITY_VIOLATION
;
2588 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
2590 PayloadSize
= DataSize
;
2593 VariableGlobal
= &Global
->VariableGlobal
[VirtualMode
];
2594 Instance
= Global
->FvbInstance
;
2596 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2598 // For variable for hardware error record, the size of the VariableName, including the Unicode Null
2599 // in bytes plus the DataSize is limited to maximum size of PcdGet32(PcdMaxHardwareErrorVariableSize) bytes.
2601 if ((PayloadSize
> PcdGet32(PcdMaxHardwareErrorVariableSize
)) ||
2602 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32(PcdMaxHardwareErrorVariableSize
))) {
2603 return EFI_INVALID_PARAMETER
;
2606 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"
2608 if (StrnCmp (VariableName
, \
2609 Global
->VariableName
[VirtualMode
][VAR_HW_ERR_REC
], \
2610 StrLen(Global
->VariableName
[VirtualMode
][VAR_HW_ERR_REC
])) != 0) {
2611 return EFI_INVALID_PARAMETER
;
2615 // For variable not for hardware error record, the size of the VariableName, including the
2616 // Unicode Null in bytes plus the DataSize is limited to maximum size of PcdGet32(PcdMaxVariableSize) bytes.
2618 if ((PayloadSize
> PcdGet32(PcdMaxVariableSize
)) ||
2619 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32(PcdMaxVariableSize
))) {
2620 return EFI_INVALID_PARAMETER
;
2624 AcquireLockOnlyAtBootTime(&VariableGlobal
->VariableServicesLock
);
2627 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated;
2629 if (InterlockedIncrement (&Global
->ReentrantState
) > 1) {
2630 Point
= VariableGlobal
->NonVolatileVariableBase
;;
2632 // Parse non-volatile variable data and get last variable offset
2634 NextVariable
= GetStartPointer (Point
);
2635 while (IsValidVariableHeader (NextVariable
, FALSE
, VariableGlobal
, Instance
, NULL
)) {
2636 NextVariable
= GetNextVariablePtr (NextVariable
, FALSE
, VariableGlobal
, Instance
);
2638 Global
->NonVolatileLastVariableOffset
= NextVariable
- Point
;
2642 // Check whether the input variable exists
2645 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, VariableGlobal
, Instance
);
2648 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang
2650 AutoUpdateLangVariable (VariableName
, Data
, PayloadSize
, VirtualMode
, Global
);
2653 // Process PK, KEK, Sigdb seperately
2655 if (CompareGuid (VendorGuid
, Global
->GlobalVariableGuid
[VirtualMode
]) && (StrCmp (VariableName
, Global
->VariableName
[VirtualMode
][VAR_PLATFORM_KEY
]) == 0)) {
2656 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, VirtualMode
, Global
, &Variable
, Attributes
, TRUE
);
2657 } else if (CompareGuid (VendorGuid
, Global
->GlobalVariableGuid
[VirtualMode
]) && (StrCmp (VariableName
, Global
->VariableName
[VirtualMode
][VAR_KEY_EXCHANGE_KEY
]) == 0)) {
2658 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, VirtualMode
, Global
, &Variable
, Attributes
, FALSE
);
2659 } else if (CompareGuid (VendorGuid
, Global
->ImageSecurityDatabaseGuid
[VirtualMode
])) {
2660 Status
= ProcessVarWithKek (VariableName
, VendorGuid
, Data
, DataSize
, VirtualMode
, Global
, &Variable
, Attributes
);
2662 Status
= VerifyVariable (Data
, DataSize
, VirtualMode
, Global
, &Variable
, Attributes
, &KeyIndex
, &MonotonicCount
);
2663 if (!EFI_ERROR(Status
)) {
2665 // Verification pass
2667 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2669 // Cut the certificate size before set
2671 Status
= UpdateVariable (
2674 (UINT8
*)Data
+ AUTHINFO_SIZE
,
2675 DataSize
- AUTHINFO_SIZE
,
2685 // Update variable as usual
2687 Status
= UpdateVariable (
2703 InterlockedDecrement (&Global
->ReentrantState
);
2704 ReleaseLockOnlyAtBootTime (&VariableGlobal
->VariableServicesLock
);
2709 Implements EsalQueryVariableInfo function of Extended SAL Variable Services Class.
2711 This function implements EsalQueryVariableInfo function of Extended SAL Variable Services Class.
2712 It is equivalent in functionality to the EFI Runtime Service QueryVariableInfo().
2714 @param[in] Attributes Attributes bitmask to specify the type of variables
2715 on which to return information.
2716 @param[out] MaximumVariableStorageSize On output the maximum size of the storage space available for
2717 the EFI variables associated with the attributes specified.
2718 @param[out] RemainingVariableStorageSize Returns the remaining size of the storage space available for EFI
2719 variables associated with the attributes specified.
2720 @param[out] MaximumVariableSize Returns the maximum size of an individual EFI variable
2721 associated with the attributes specified.
2722 @param[in] VirtualMode Current calling mode for this function
2723 @param[in] Global Context of this Extended SAL Variable Services Class call
2725 @retval EFI_SUCCESS Valid answer returned.
2726 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2727 @retval EFI_UNSUPPORTED The attribute is not supported on this platform, and the
2728 MaximumVariableStorageSize, RemainingVariableStorageSize,
2729 MaximumVariableSize are undefined.
2733 EsalQueryVariableInfo (
2734 IN UINT32 Attributes
,
2735 OUT UINT64
*MaximumVariableStorageSize
,
2736 OUT UINT64
*RemainingVariableStorageSize
,
2737 OUT UINT64
*MaximumVariableSize
,
2738 IN BOOLEAN VirtualMode
,
2739 IN ESAL_VARIABLE_GLOBAL
*Global
2742 EFI_PHYSICAL_ADDRESS Variable
;
2743 EFI_PHYSICAL_ADDRESS NextVariable
;
2744 UINT64 VariableSize
;
2745 EFI_PHYSICAL_ADDRESS VariableStoreHeaderAddress
;
2747 VARIABLE_STORE_HEADER VarStoreHeader
;
2748 VARIABLE_HEADER VariableHeader
;
2749 UINT64 CommonVariableTotalSize
;
2750 UINT64 HwErrVariableTotalSize
;
2751 VARIABLE_GLOBAL
*VariableGlobal
;
2754 CommonVariableTotalSize
= 0;
2755 HwErrVariableTotalSize
= 0;
2757 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2758 return EFI_INVALID_PARAMETER
;
2761 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2763 // Make sure the Attributes combination is supported by the platform.
2765 return EFI_UNSUPPORTED
;
2766 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2768 // Make sure if runtime bit is set, boot service bit is set also.
2770 return EFI_INVALID_PARAMETER
;
2771 } else if (EfiAtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2773 // Make sure RT Attribute is set if we are in Runtime phase.
2775 return EFI_INVALID_PARAMETER
;
2776 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2778 // Make sure Hw Attribute is set with NV.
2780 return EFI_INVALID_PARAMETER
;
2783 VariableGlobal
= &Global
->VariableGlobal
[VirtualMode
];
2784 Instance
= Global
->FvbInstance
;
2786 AcquireLockOnlyAtBootTime(&VariableGlobal
->VariableServicesLock
);
2788 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2790 // Query is Volatile related.
2793 VariableStoreHeaderAddress
= VariableGlobal
->VolatileVariableBase
;
2796 // Query is Non-Volatile related.
2799 VariableStoreHeaderAddress
= VariableGlobal
->NonVolatileVariableBase
;
2803 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2804 // with the storage size (excluding the storage header size).
2806 GetVarStoreHeader (VariableStoreHeaderAddress
, Volatile
, VariableGlobal
, Instance
, &VarStoreHeader
);
2808 *MaximumVariableStorageSize
= VarStoreHeader
.Size
- sizeof (VARIABLE_STORE_HEADER
);
2810 // Harware error record variable needs larger size.
2812 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2813 *MaximumVariableStorageSize
= PcdGet32(PcdHwErrStorageSize
);
2814 *MaximumVariableSize
= PcdGet32(PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2816 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2817 ASSERT (PcdGet32(PcdHwErrStorageSize
) < VarStoreHeader
.Size
);
2818 *MaximumVariableStorageSize
= VarStoreHeader
.Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
);
2822 // Let *MaximumVariableSize be PcdGet32(PcdMaxVariableSize) with the exception of the variable header size.
2824 *MaximumVariableSize
= PcdGet32(PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2828 // Point to the starting address of the variables.
2830 Variable
= GetStartPointer (VariableStoreHeaderAddress
);
2833 // Now walk through the related variable store.
2835 while (IsValidVariableHeader (Variable
, Volatile
, VariableGlobal
, Instance
, &VariableHeader
) &&
2836 (Variable
< GetEndPointer (VariableStoreHeaderAddress
, Volatile
, VariableGlobal
, Instance
))) {
2837 NextVariable
= GetNextVariablePtr (Variable
, Volatile
, VariableGlobal
, Instance
);
2838 VariableSize
= NextVariable
- Variable
;
2840 if (EfiAtRuntime ()) {
2842 // we don't take the state of the variables in mind
2843 // when calculating RemainingVariableStorageSize,
2844 // since the space occupied by variables not marked with
2845 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2847 if ((VariableHeader
.Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2848 HwErrVariableTotalSize
+= VariableSize
;
2850 CommonVariableTotalSize
+= VariableSize
;
2854 // Only care about Variables with State VAR_ADDED,because
2855 // the space not marked as VAR_ADDED is reclaimable now.
2857 if (VariableHeader
.State
== VAR_ADDED
) {
2858 if ((VariableHeader
.Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2859 HwErrVariableTotalSize
+= VariableSize
;
2861 CommonVariableTotalSize
+= VariableSize
;
2867 // Go to the next one
2869 Variable
= NextVariable
;
2872 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2873 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2875 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2878 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2879 *MaximumVariableSize
= 0;
2880 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2881 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2884 ReleaseLockOnlyAtBootTime (&VariableGlobal
->VariableServicesLock
);
2889 Notification function of EVT_GROUP_READY_TO_BOOT event group.
2891 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.
2892 When the Boot Manager is about to load and execute a boot option, it reclaims variable
2893 storage if free size is below the threshold.
2895 @param[in] Event Event whose notification function is being invoked.
2896 @param[in] Context Pointer to the notification function's context.
2908 UINTN CommonVariableSpace
;
2909 UINTN RemainingCommonVariableSpace
;
2910 UINTN RemainingHwErrVariableSpace
;
2912 VarSize
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
[Physical
].NonVolatileVariableBase
))->Size
;
2913 Status
= EFI_SUCCESS
;
2915 //Allowable max size of common variable storage space
2917 CommonVariableSpace
= VarSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
);
2919 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2921 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2923 // If the free area is below a threshold, then performs reclaim operation.
2925 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2926 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2927 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2929 mVariableModuleGlobal
->VariableGlobal
[Physical
].NonVolatileVariableBase
,
2930 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2933 mVariableModuleGlobal
,
2936 ASSERT_EFI_ERROR (Status
);
2941 Initializes variable store area for non-volatile and volatile variable.
2943 This function allocates and initializes memory space for global context of ESAL
2944 variable service and variable store area for non-volatile and volatile variable.
2946 @param[in] ImageHandle The Image handle of this driver.
2947 @param[in] SystemTable The pointer of EFI_SYSTEM_TABLE.
2949 @retval EFI_SUCCESS Function successfully executed.
2950 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2954 VariableCommonInitialize (
2955 IN EFI_HANDLE ImageHandle
,
2956 IN EFI_SYSTEM_TABLE
*SystemTable
2960 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
2961 EFI_PHYSICAL_ADDRESS CurrPtr
;
2962 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
2963 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2964 EFI_PHYSICAL_ADDRESS Variable
;
2965 EFI_PHYSICAL_ADDRESS NextVariable
;
2968 EFI_PHYSICAL_ADDRESS FvVolHdr
;
2969 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader
;
2970 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
2975 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2976 UINT64 VariableStoreLength
;
2977 EFI_EVENT ReadyToBootEvent
;
2981 // Allocate memory for mVariableModuleGlobal
2983 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (ESAL_VARIABLE_GLOBAL
));
2984 if (mVariableModuleGlobal
== NULL
) {
2985 return EFI_OUT_OF_RESOURCES
;
2988 mVariableModuleGlobal
->GlobalVariableGuid
[Physical
] = &gEfiGlobalVariableGuid
;
2990 mVariableModuleGlobal
->VariableName
[Physical
],
2992 sizeof (mVariableName
)
2995 EfiInitializeLock(&mVariableModuleGlobal
->VariableGlobal
[Physical
].VariableServicesLock
, TPL_NOTIFY
);
2998 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2999 // is stored with common variable in the same NV region. So the platform integrator should
3000 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
3001 // PcdFlashNvStorageVariableSize.
3003 ASSERT (PcdGet32(PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
3006 // Allocate memory for volatile variable store
3008 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
3009 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
3010 if (VolatileVariableStore
== NULL
) {
3011 FreePool (mVariableModuleGlobal
);
3012 return EFI_OUT_OF_RESOURCES
;
3015 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize