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 ASSERT (Entry
->Data
!= NULL
);
850 Entry
->DataSize
= DataSize
;
851 CopyMem (Entry
->Data
, Data
, DataSize
);
860 Search the cache to check if the variable is in it.
862 This function searches the variable cache. If the variable to find exists, return its data
865 @param[in] VariableName A Null-terminated Unicode string that is the name of the vendor's
866 variable. Each VariableName is unique for each VendorGuid.
867 @param[in] VendorGuid A unique identifier for the vendor
868 @param[out] Attributes Pointer to the attributes bitmask of the variable for output.
869 @param[in, out] DataSize On input, size of the buffer of Data.
870 On output, size of the variable's data.
871 @param[out] Data Pointer to the data buffer for output.
873 @retval EFI_SUCCESS VariableGuid & VariableName data was returned.
874 @retval EFI_NOT_FOUND No matching variable found in cache.
875 @retval EFI_BUFFER_TOO_SMALL *DataSize is smaller than size of the variable's data to return.
879 FindVariableInCache (
880 IN CHAR16
*VariableName
,
881 IN EFI_GUID
*VendorGuid
,
882 OUT UINT32
*Attributes OPTIONAL
,
883 IN OUT UINTN
*DataSize
,
887 VARIABLE_CACHE_ENTRY
*Entry
;
890 if (EfiAtRuntime ()) {
892 // Don't use the cache at runtime
894 return EFI_NOT_FOUND
;
898 // Searches cache for the variable
900 for (Index
= 0, Entry
= mVariableCache
; Index
< sizeof (mVariableCache
)/sizeof (VARIABLE_CACHE_ENTRY
); Index
++, Entry
++) {
901 if (CompareGuid (VendorGuid
, Entry
->Guid
)) {
902 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
903 if (Entry
->DataSize
== 0) {
905 // Variable has been deleted so return EFI_NOT_FOUND
907 return EFI_NOT_FOUND
;
908 } else if (Entry
->DataSize
> *DataSize
) {
910 // If buffer is too small, return the size needed and EFI_BUFFER_TOO_SMALL
912 *DataSize
= Entry
->DataSize
;
913 return EFI_BUFFER_TOO_SMALL
;
916 // If buffer is large enough, return the data
918 *DataSize
= Entry
->DataSize
;
919 CopyMem (Data
, Entry
->Data
, Entry
->DataSize
);
921 // If Attributes is not NULL, return the variable's attribute.
923 if (Attributes
!= NULL
) {
924 *Attributes
= Entry
->Attributes
;
932 return EFI_NOT_FOUND
;
936 Finds variable in volatile and non-volatile storage areas.
938 This code finds variable in volatile and non-volatile storage areas.
939 If VariableName is an empty string, then we just return the first
940 qualified variable without comparing VariableName and VendorGuid.
941 Otherwise, VariableName and VendorGuid are compared.
943 @param[in] VariableName Name of the variable to be found.
944 @param[in] VendorGuid Vendor GUID to be found.
945 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,
946 including the range searched and the target position.
947 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including
948 base of volatile variable storage area, base of
949 NV variable storage area, and a lock.
950 @param[in] Instance Instance of FV Block services.
952 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
954 @retval EFI_SUCCESS Variable successfully found.
955 @retval EFI_INVALID_PARAMETER Variable not found.
960 IN CHAR16
*VariableName
,
961 IN EFI_GUID
*VendorGuid
,
962 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
963 IN VARIABLE_GLOBAL
*Global
,
967 EFI_PHYSICAL_ADDRESS Variable
[2];
968 EFI_PHYSICAL_ADDRESS InDeletedVariable
;
969 EFI_PHYSICAL_ADDRESS VariableStoreHeader
[2];
970 UINTN InDeletedStorageIndex
;
972 CHAR16 LocalVariableName
[MAX_NAME_SIZE
];
974 VARIABLE_HEADER VariableHeader
;
977 // 0: Volatile, 1: Non-Volatile
978 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
979 // make use of this mapping to implement search algorithme.
981 VariableStoreHeader
[0] = Global
->VolatileVariableBase
;
982 VariableStoreHeader
[1] = Global
->NonVolatileVariableBase
;
985 // Start Pointers for the variable.
986 // Actual Data Pointer where data can be written.
988 Variable
[0] = GetStartPointer (VariableStoreHeader
[0]);
989 Variable
[1] = GetStartPointer (VariableStoreHeader
[1]);
991 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
992 return EFI_INVALID_PARAMETER
;
996 // Find the variable by walk through volatile and then non-volatile variable store
998 InDeletedVariable
= 0x0;
999 InDeletedStorageIndex
= 0;
1001 for (Index
= 0; Index
< 2; Index
++) {
1005 while (IsValidVariableHeader (Variable
[Index
], Volatile
, Global
, Instance
, &VariableHeader
)) {
1006 if (VariableHeader
.State
== VAR_ADDED
||
1007 VariableHeader
.State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
1009 if (!EfiAtRuntime () || ((VariableHeader
.Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
1010 if (VariableName
[0] == 0) {
1012 // If VariableName is an empty string, then we just find the first qualified variable
1013 // without comparing VariableName and VendorGuid
1015 if (VariableHeader
.State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1017 // If variable is in delete transition, record it.
1019 InDeletedVariable
= Variable
[Index
];
1020 InDeletedStorageIndex
= Index
;
1023 // If variable is not in delete transition, return it.
1025 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
1026 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
], Volatile
, Global
, Instance
);
1027 PtrTrack
->CurrPtr
= Variable
[Index
];
1028 PtrTrack
->Volatile
= Volatile
;
1034 // If VariableName is not an empty string, then VariableName and VendorGuid are compared.
1036 if (CompareGuid (VendorGuid
, &VariableHeader
.VendorGuid
)) {
1037 GetVariableNamePtr (
1045 ASSERT (NameSizeOfVariable (&VariableHeader
) != 0);
1046 if (CompareMem (VariableName
, LocalVariableName
, NameSizeOfVariable (&VariableHeader
)) == 0) {
1047 if (VariableHeader
.State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1049 // If variable is in delete transition, record it.
1050 // We will use if only no VAR_ADDED variable is found.
1052 InDeletedVariable
= Variable
[Index
];
1053 InDeletedStorageIndex
= Index
;
1056 // If variable is not in delete transition, return it.
1058 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
1059 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
], Volatile
, Global
, Instance
);
1060 PtrTrack
->CurrPtr
= Variable
[Index
];
1061 PtrTrack
->Volatile
= Volatile
;
1071 Variable
[Index
] = GetNextVariablePtr (
1078 if (InDeletedVariable
!= 0x0) {
1080 // If no VAR_ADDED variable is found, and only variable in delete transition, then use this one.
1082 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[InDeletedStorageIndex
]);
1083 PtrTrack
->EndPtr
= GetEndPointer (
1084 VariableStoreHeader
[InDeletedStorageIndex
],
1085 (BOOLEAN
)(InDeletedStorageIndex
== 0),
1089 PtrTrack
->CurrPtr
= InDeletedVariable
;
1090 PtrTrack
->Volatile
= (BOOLEAN
)(InDeletedStorageIndex
== 0);
1094 PtrTrack
->CurrPtr
= 0x0;
1095 return EFI_NOT_FOUND
;
1099 Variable store garbage collection and reclaim operation.
1101 @param[in] VariableBase Base address of variable store area.
1102 @param[out] LastVariableOffset Offset of last variable.
1103 @param[in] IsVolatile The variable store is volatile or not,
1104 if it is non-volatile, need FTW.
1105 @param[in] VirtualMode Current calling mode for this function.
1106 @param[in] Global Context of this Extended SAL Variable Services Class call.
1107 @param[in] UpdatingVariable Pointer to header of the variable that is being updated.
1109 @retval EFI_SUCCESS Variable store successfully reclaimed.
1110 @retval EFI_OUT_OF_RESOURCES Fail to allocate memory buffer to hold all valid variables.
1115 IN EFI_PHYSICAL_ADDRESS VariableBase
,
1116 OUT UINTN
*LastVariableOffset
,
1117 IN BOOLEAN IsVolatile
,
1118 IN BOOLEAN VirtualMode
,
1119 IN ESAL_VARIABLE_GLOBAL
*Global
,
1120 IN EFI_PHYSICAL_ADDRESS UpdatingVariable
1123 EFI_PHYSICAL_ADDRESS Variable
;
1124 EFI_PHYSICAL_ADDRESS AddedVariable
;
1125 EFI_PHYSICAL_ADDRESS NextVariable
;
1126 EFI_PHYSICAL_ADDRESS NextAddedVariable
;
1127 VARIABLE_STORE_HEADER VariableStoreHeader
;
1128 VARIABLE_HEADER VariableHeader
;
1129 VARIABLE_HEADER AddedVariableHeader
;
1130 CHAR16 VariableName
[MAX_NAME_SIZE
];
1131 CHAR16 AddedVariableName
[MAX_NAME_SIZE
];
1133 UINTN MaximumBufferSize
;
1139 VARIABLE_GLOBAL
*VariableGlobal
;
1142 VariableGlobal
= &Global
->VariableGlobal
[VirtualMode
];
1143 Instance
= Global
->FvbInstance
;
1145 GetVarStoreHeader (VariableBase
, IsVolatile
, VariableGlobal
, Instance
, &VariableStoreHeader
);
1147 // recaluate the total size of Common/HwErr type variables in non-volatile area.
1150 Global
->CommonVariableTotalSize
= 0;
1151 Global
->HwErrVariableTotalSize
= 0;
1155 // Calculate the size of buffer needed to gather all valid variables
1157 Variable
= GetStartPointer (VariableBase
);
1158 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
1160 while (IsValidVariableHeader (Variable
, IsVolatile
, VariableGlobal
, Instance
, &VariableHeader
)) {
1161 NextVariable
= GetNextVariablePtr (Variable
, IsVolatile
, VariableGlobal
, Instance
);
1163 // Collect VAR_ADDED variables, and variables in delete transition status.
1165 if (VariableHeader
.State
== VAR_ADDED
||
1166 VariableHeader
.State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
1168 VariableSize
= NextVariable
- Variable
;
1169 MaximumBufferSize
+= VariableSize
;
1172 Variable
= NextVariable
;
1176 // Reserve the 1 Bytes with Oxff to identify the
1177 // end of the variable buffer.
1179 MaximumBufferSize
+= 1;
1180 ValidBuffer
= AllocatePool (MaximumBufferSize
);
1181 if (ValidBuffer
== NULL
) {
1182 return EFI_OUT_OF_RESOURCES
;
1185 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
1188 // Copy variable store header
1190 CopyMem (ValidBuffer
, &VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
1191 CurrPtr
= (UINT8
*) GetStartPointer ((EFI_PHYSICAL_ADDRESS
) ValidBuffer
);
1194 // Reinstall all ADDED variables
1196 Variable
= GetStartPointer (VariableBase
);
1197 while (IsValidVariableHeader (Variable
, IsVolatile
, VariableGlobal
, Instance
, &VariableHeader
)) {
1198 NextVariable
= GetNextVariablePtr (Variable
, IsVolatile
, VariableGlobal
, Instance
);
1199 if (VariableHeader
.State
== VAR_ADDED
) {
1200 VariableSize
= NextVariable
- Variable
;
1201 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
1202 CurrPtr
+= VariableSize
;
1203 if ((!IsVolatile
) && ((((VARIABLE_HEADER
*)Variable
)->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
1204 Global
->HwErrVariableTotalSize
+= VariableSize
;
1205 } else if ((!IsVolatile
) && ((((VARIABLE_HEADER
*)Variable
)->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
1206 Global
->CommonVariableTotalSize
+= VariableSize
;
1209 Variable
= NextVariable
;
1212 // Reinstall in delete transition variables
1214 Variable
= GetStartPointer (VariableBase
);
1215 while (IsValidVariableHeader (Variable
, IsVolatile
, VariableGlobal
, Instance
, &VariableHeader
)) {
1216 NextVariable
= GetNextVariablePtr (Variable
, IsVolatile
, VariableGlobal
, Instance
);
1217 if (VariableHeader
.State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1220 // Buffer has cached all ADDED variable.
1221 // Per IN_DELETED variable, we have to guarantee that
1222 // no ADDED one in previous buffer.
1225 AddedVariable
= GetStartPointer ((EFI_PHYSICAL_ADDRESS
) ValidBuffer
);
1226 while (IsValidVariableHeader (AddedVariable
, IsVolatile
, VariableGlobal
, Instance
, &AddedVariableHeader
)) {
1227 NextAddedVariable
= GetNextVariablePtr (AddedVariable
, IsVolatile
, VariableGlobal
, Instance
);
1228 NameSize
= NameSizeOfVariable (&AddedVariableHeader
);
1229 if (CompareGuid (&AddedVariableHeader
.VendorGuid
, &VariableHeader
.VendorGuid
) &&
1230 NameSize
== NameSizeOfVariable (&VariableHeader
)
1232 GetVariableNamePtr (Variable
, IsVolatile
, VariableGlobal
, Instance
, VariableName
);
1233 GetVariableNamePtr (AddedVariable
, IsVolatile
, VariableGlobal
, Instance
, AddedVariableName
);
1234 if (CompareMem (VariableName
, AddedVariableName
, NameSize
) == 0) {
1236 // If ADDED variable with the same name and vender GUID has been reinstalled,
1237 // then discard this IN_DELETED copy.
1243 AddedVariable
= NextAddedVariable
;
1246 // Add IN_DELETE variables that have not been added to buffer
1249 VariableSize
= NextVariable
- Variable
;
1250 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
1251 if (Variable
!= UpdatingVariable
) {
1253 // Make this IN_DELETE instance valid if:
1254 // 1. No valid instance of this variable exists.
1255 // 2. It is not the variable that is going to be updated.
1257 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
1259 CurrPtr
+= VariableSize
;
1260 if ((!IsVolatile
) && ((((VARIABLE_HEADER
*)Variable
)->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
1261 Global
->HwErrVariableTotalSize
+= VariableSize
;
1262 } else if ((!IsVolatile
) && ((((VARIABLE_HEADER
*)Variable
)->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
1263 Global
->CommonVariableTotalSize
+= VariableSize
;
1267 Variable
= NextVariable
;
1272 // If volatile variable store, just copy valid buffer
1274 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
.Size
, 0xff);
1275 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
));
1276 Status
= EFI_SUCCESS
;
1279 // If non-volatile variable store, perform FTW here.
1280 // Write ValidBuffer to destination specified by VariableBase.
1282 Status
= FtwVariableSpace (
1285 (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
)
1288 if (!EFI_ERROR (Status
)) {
1289 *LastVariableOffset
= (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
);
1291 *LastVariableOffset
= 0;
1294 FreePool (ValidBuffer
);
1300 Get index from supported language codes according to language string.
1302 This code is used to get corresponding index in supported language codes. It can handle
1303 RFC4646 and ISO639 language tags.
1304 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
1305 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
1308 SupportedLang = "engfraengfra"
1310 Iso639Language = TRUE
1311 The return value is "0".
1313 SupportedLang = "en;fr;en-US;fr-FR"
1315 Iso639Language = FALSE
1316 The return value is "3".
1318 @param[in] SupportedLang Platform supported language codes.
1319 @param[in] Lang Configured language.
1320 @param[in] Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1322 @return The index of language in the language codes.
1326 GetIndexFromSupportedLangCodes(
1327 IN CHAR8
*SupportedLang
,
1329 IN BOOLEAN Iso639Language
1333 UINTN CompareLength
;
1334 UINTN LanguageLength
;
1336 if (Iso639Language
) {
1337 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1338 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
1339 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
1341 // Successfully find the index of Lang string in SupportedLang string.
1343 Index
= Index
/ CompareLength
;
1351 // Compare RFC4646 language code
1354 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
1356 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
1358 // Skip ';' characters in SupportedLang
1360 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
1362 // Determine the length of the next language code in SupportedLang
1364 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
1366 if ((CompareLength
== LanguageLength
) &&
1367 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
1369 // Successfully find the index of Lang string in SupportedLang string.
1380 Get language string from supported language codes according to index.
1382 This code is used to get corresponding language string in supported language codes. It can handle
1383 RFC4646 and ISO639 language tags.
1384 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
1385 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
1388 SupportedLang = "engfraengfra"
1390 Iso639Language = TRUE
1391 The return value is "fra".
1393 SupportedLang = "en;fr;en-US;fr-FR"
1395 Iso639Language = FALSE
1396 The return value is "fr".
1398 @param[in] SupportedLang Platform supported language codes.
1399 @param[in] Index the index in supported language codes.
1400 @param[in] Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1401 @param[in] VirtualMode Current calling mode for this function.
1402 @param[in] Global Context of this Extended SAL Variable Services Class call.
1404 @return The language string in the language codes.
1408 GetLangFromSupportedLangCodes (
1409 IN CHAR8
*SupportedLang
,
1411 IN BOOLEAN Iso639Language
,
1412 IN BOOLEAN VirtualMode
,
1413 IN ESAL_VARIABLE_GLOBAL
*Global
1417 UINTN CompareLength
;
1421 Supported
= SupportedLang
;
1422 if (Iso639Language
) {
1424 // according to the index of Lang string in SupportedLang string to get the language.
1425 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1426 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1428 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1429 Global
->Lang
[CompareLength
] = '\0';
1430 return CopyMem (Global
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
1435 // take semicolon as delimitation, sequentially traverse supported language codes.
1437 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
1440 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
1442 // Have completed the traverse, but not find corrsponding string.
1443 // This case is not allowed to happen.
1448 if (SubIndex
== Index
) {
1450 // according to the index of Lang string in SupportedLang string to get the language.
1451 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1452 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1454 Global
->PlatformLang
[VirtualMode
][CompareLength
] = '\0';
1455 return CopyMem (Global
->PlatformLang
[VirtualMode
], Supported
- CompareLength
, CompareLength
);
1460 // Skip ';' characters in Supported
1462 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1468 Returns a pointer to an allocated buffer that contains the best matching language
1469 from a set of supported languages.
1471 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1472 code types may not be mixed in a single call to this function. This function
1473 supports a variable argument list that allows the caller to pass in a prioritized
1474 list of language codes to test against all the language codes in SupportedLanguages.
1476 If SupportedLanguages is NULL, then ASSERT().
1478 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1479 contains a set of language codes in the format
1480 specified by Iso639Language.
1481 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1482 in ISO 639-2 format. If FALSE, then all language
1483 codes are assumed to be in RFC 4646 language format.
1484 @param[in] VirtualMode Current calling mode for this function.
1485 @param[in] ... A variable argument list that contains pointers to
1486 Null-terminated ASCII strings that contain one or more
1487 language codes in the format specified by Iso639Language.
1488 The first language code from each of these language
1489 code lists is used to determine if it is an exact or
1490 close match to any of the language codes in
1491 SupportedLanguages. Close matches only apply to RFC 4646
1492 language codes, and the matching algorithm from RFC 4647
1493 is used to determine if a close match is present. If
1494 an exact or close match is found, then the matching
1495 language code from SupportedLanguages is returned. If
1496 no matches are found, then the next variable argument
1497 parameter is evaluated. The variable argument list
1498 is terminated by a NULL.
1500 @retval NULL The best matching language could not be found in SupportedLanguages.
1501 @retval NULL There are not enough resources available to return the best matching
1503 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1504 language in SupportedLanguages.
1508 VariableGetBestLanguage (
1509 IN CONST CHAR8
*SupportedLanguages
,
1510 IN BOOLEAN Iso639Language
,
1511 IN BOOLEAN VirtualMode
,
1517 UINTN CompareLength
;
1518 UINTN LanguageLength
;
1519 CONST CHAR8
*Supported
;
1522 ASSERT (SupportedLanguages
!= NULL
);
1524 VA_START (Args
, VirtualMode
);
1525 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1527 // Default to ISO 639-2 mode
1530 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1533 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1535 if (!Iso639Language
) {
1536 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1540 // Trim back the length of Language used until it is empty
1542 while (LanguageLength
> 0) {
1544 // Loop through all language codes in SupportedLanguages
1546 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1548 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1550 if (!Iso639Language
) {
1552 // Skip ';' characters in Supported
1554 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1556 // Determine the length of the next language code in Supported
1558 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1560 // If Language is longer than the Supported, then skip to the next language
1562 if (LanguageLength
> CompareLength
) {
1567 // See if the first LanguageLength characters in Supported match Language
1569 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1572 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
[VirtualMode
];
1573 Buffer
[CompareLength
] = '\0';
1574 return CopyMem (Buffer
, Supported
, CompareLength
);
1578 if (Iso639Language
) {
1580 // If ISO 639 mode, then each language can only be tested once
1585 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1587 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1594 // No matches were found
1600 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1602 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1603 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1604 and are read-only. Therefore, in variable driver, only store the original value for other use.
1606 @param[in] VariableName Name of variable.
1607 @param[in] Data Variable data.
1608 @param[in] DataSize Size of data. 0 means delete.
1609 @param[in] VirtualMode Current calling mode for this function.
1610 @param[in] Global Context of this Extended SAL Variable Services Class call.
1614 AutoUpdateLangVariable(
1615 IN CHAR16
*VariableName
,
1618 IN BOOLEAN VirtualMode
,
1619 IN ESAL_VARIABLE_GLOBAL
*Global
1623 CHAR8
*BestPlatformLang
;
1627 VARIABLE_POINTER_TRACK Variable
;
1628 BOOLEAN SetLanguageCodes
;
1629 CHAR16
**PredefinedVariableName
;
1630 VARIABLE_GLOBAL
*VariableGlobal
;
1634 // Don't do updates for delete operation
1636 if (DataSize
== 0) {
1640 SetLanguageCodes
= FALSE
;
1641 VariableGlobal
= &Global
->VariableGlobal
[VirtualMode
];
1642 Instance
= Global
->FvbInstance
;
1645 PredefinedVariableName
= &Global
->VariableName
[VirtualMode
][0];
1646 if (StrCmp (VariableName
, PredefinedVariableName
[VAR_PLATFORM_LANG_CODES
]) == 0) {
1648 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1650 if (EfiAtRuntime ()) {
1654 SetLanguageCodes
= TRUE
;
1657 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1658 // Therefore, in variable driver, only store the original value for other use.
1660 if (Global
->PlatformLangCodes
[VirtualMode
] != NULL
) {
1661 FreePool (Global
->PlatformLangCodes
[VirtualMode
]);
1663 Global
->PlatformLangCodes
[VirtualMode
] = AllocateRuntimeCopyPool (DataSize
, Data
);
1664 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
[VirtualMode
] != NULL
);
1667 // PlatformLang holds a single language from PlatformLangCodes,
1668 // so the size of PlatformLangCodes is enough for the PlatformLang.
1670 if (Global
->PlatformLang
[VirtualMode
] != NULL
) {
1671 FreePool (Global
->PlatformLang
[VirtualMode
]);
1673 Global
->PlatformLang
[VirtualMode
] = AllocateRuntimePool (DataSize
);
1674 ASSERT (Global
->PlatformLang
[VirtualMode
] != NULL
);
1676 } else if (StrCmp (VariableName
, PredefinedVariableName
[VAR_LANG_CODES
]) == 0) {
1678 // LangCodes is a volatile variable, so it can not be updated at runtime.
1680 if (EfiAtRuntime ()) {
1684 SetLanguageCodes
= TRUE
;
1687 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1688 // Therefore, in variable driver, only store the original value for other use.
1690 if (Global
->LangCodes
[VirtualMode
] != NULL
) {
1691 FreePool (Global
->LangCodes
[VirtualMode
]);
1693 Global
->LangCodes
[VirtualMode
] = AllocateRuntimeCopyPool (DataSize
, Data
);
1694 ASSERT (Global
->LangCodes
[VirtualMode
] != NULL
);
1697 if (SetLanguageCodes
1698 && (Global
->PlatformLangCodes
[VirtualMode
] != NULL
)
1699 && (Global
->LangCodes
[VirtualMode
] != NULL
)) {
1701 // Update Lang if PlatformLang is already set
1702 // Update PlatformLang if Lang is already set
1704 Status
= FindVariable (PredefinedVariableName
[VAR_PLATFORM_LANG
], Global
->GlobalVariableGuid
[VirtualMode
], &Variable
, VariableGlobal
, Instance
);
1705 if (!EFI_ERROR (Status
)) {
1709 VariableName
= PredefinedVariableName
[VAR_PLATFORM_LANG
];
1711 Status
= FindVariable (PredefinedVariableName
[VAR_LANG
], Global
->GlobalVariableGuid
[VirtualMode
], &Variable
, VariableGlobal
, Instance
);
1712 if (!EFI_ERROR (Status
)) {
1714 // Update PlatformLang
1716 VariableName
= PredefinedVariableName
[VAR_LANG
];
1719 // Neither PlatformLang nor Lang is set, directly return
1724 Data
= (VOID
*) GetEndPointer (VariableGlobal
->VolatileVariableBase
, TRUE
, VariableGlobal
, Instance
);
1725 GetVariableDataPtr ((EFI_PHYSICAL_ADDRESS
) Variable
.CurrPtr
, Variable
.Volatile
, VariableGlobal
, Instance
, (CHAR16
*) Data
);
1727 Status
= AccessVariableStore (
1732 (UINTN
) &(((VARIABLE_HEADER
*)Variable
.CurrPtr
)->DataSize
),
1736 ASSERT_EFI_ERROR (Status
);
1740 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1742 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1744 if (StrCmp (VariableName
, PredefinedVariableName
[VAR_PLATFORM_LANG
]) == 0) {
1746 // Update Lang when PlatformLangCodes/LangCodes were set.
1748 if ((Global
->PlatformLangCodes
[VirtualMode
] != NULL
) && (Global
->LangCodes
[VirtualMode
] != NULL
)) {
1750 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1752 BestPlatformLang
= VariableGetBestLanguage (Global
->PlatformLangCodes
[VirtualMode
], FALSE
, VirtualMode
, Data
, NULL
);
1753 if (BestPlatformLang
!= NULL
) {
1755 // Get the corresponding index in language codes.
1757 Index
= GetIndexFromSupportedLangCodes (Global
->PlatformLangCodes
[VirtualMode
], BestPlatformLang
, FALSE
);
1760 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1762 BestLang
= GetLangFromSupportedLangCodes (Global
->LangCodes
[VirtualMode
], Index
, TRUE
, VirtualMode
, Global
);
1765 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1767 FindVariable (PredefinedVariableName
[VAR_LANG
], Global
->GlobalVariableGuid
[VirtualMode
], &Variable
, VariableGlobal
, Instance
);
1769 Status
= UpdateVariable (
1770 PredefinedVariableName
[VAR_LANG
],
1771 Global
->GlobalVariableGuid
[VirtualMode
],
1773 ISO_639_2_ENTRY_SIZE
+ 1,
1782 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
1784 ASSERT_EFI_ERROR (Status
);
1788 } else if (StrCmp (VariableName
, PredefinedVariableName
[VAR_LANG
]) == 0) {
1790 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1792 if ((Global
->PlatformLangCodes
[VirtualMode
] != NULL
) && (Global
->LangCodes
[VirtualMode
] != NULL
)) {
1794 // When setting Lang, firstly get most matched language string from supported language codes.
1796 BestLang
= VariableGetBestLanguage (Global
->LangCodes
[VirtualMode
], TRUE
, VirtualMode
, Data
, NULL
);
1797 if (BestLang
!= NULL
) {
1799 // Get the corresponding index in language codes.
1801 Index
= GetIndexFromSupportedLangCodes (Global
->LangCodes
[VirtualMode
], BestLang
, TRUE
);
1804 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1806 BestPlatformLang
= GetLangFromSupportedLangCodes (Global
->PlatformLangCodes
[VirtualMode
], Index
, FALSE
, VirtualMode
, Global
);
1809 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1811 FindVariable (PredefinedVariableName
[VAR_PLATFORM_LANG
], Global
->GlobalVariableGuid
[VirtualMode
], &Variable
, VariableGlobal
, Instance
);
1813 Status
= UpdateVariable (
1814 PredefinedVariableName
[VAR_PLATFORM_LANG
],
1815 Global
->GlobalVariableGuid
[VirtualMode
],
1817 AsciiStrSize (BestPlatformLang
),
1826 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
1827 ASSERT_EFI_ERROR (Status
);
1834 Update the variable region with Variable information. These are the same
1835 arguments as the EFI Variable services.
1837 @param[in] VariableName Name of variable.
1838 @param[in] VendorGuid Guid of variable.
1839 @param[in] Data Variable data.
1840 @param[in] DataSize Size of data. 0 means delete.
1841 @param[in] Attributes Attributes of the variable.
1842 @param[in] KeyIndex Index of associated public key.
1843 @param[in] MonotonicCount Value of associated monotonic count.
1844 @param[in] VirtualMode Current calling mode for this function.
1845 @param[in] Global Context of this Extended SAL Variable Services Class call.
1846 @param[in] Variable The variable information which is used to keep track of variable usage.
1848 @retval EFI_SUCCESS The update operation is success.
1849 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1855 IN CHAR16
*VariableName
,
1856 IN EFI_GUID
*VendorGuid
,
1859 IN UINT32 Attributes OPTIONAL
,
1860 IN UINT32 KeyIndex OPTIONAL
,
1861 IN UINT64 MonotonicCount OPTIONAL
,
1862 IN BOOLEAN VirtualMode
,
1863 IN ESAL_VARIABLE_GLOBAL
*Global
,
1864 IN VARIABLE_POINTER_TRACK
*Variable
1868 VARIABLE_HEADER
*NextVariable
;
1869 UINTN VarNameOffset
;
1870 UINTN VarDataOffset
;
1875 VARIABLE_HEADER VariableHeader
;
1876 VARIABLE_HEADER
*NextVariableHeader
;
1879 VARIABLE_STORE_HEADER VariableStoreHeader
;
1881 VARIABLE_GLOBAL
*VariableGlobal
;
1884 VariableGlobal
= &Global
->VariableGlobal
[VirtualMode
];
1885 Instance
= Global
->FvbInstance
;
1889 if (Variable
->CurrPtr
!= 0) {
1891 Valid
= IsValidVariableHeader (Variable
->CurrPtr
, Variable
->Volatile
, VariableGlobal
, Instance
, &VariableHeader
);
1893 Status
= EFI_NOT_FOUND
;
1898 // Update/Delete existing variable
1900 Volatile
= Variable
->Volatile
;
1902 if (EfiAtRuntime ()) {
1904 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
1905 // the volatile is ReadOnly, and SetVariable should be aborted and
1906 // return EFI_WRITE_PROTECTED.
1908 if (Variable
->Volatile
) {
1909 Status
= EFI_WRITE_PROTECTED
;
1913 // Only variable have NV attribute can be updated/deleted in Runtime
1915 if ((VariableHeader
.Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1916 Status
= EFI_INVALID_PARAMETER
;
1921 // Setting a data variable with no access, or zero DataSize attributes
1922 // specified causes it to be deleted.
1924 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1925 State
= VariableHeader
.State
;
1926 State
&= VAR_DELETED
;
1928 Status
= AccessVariableStore (
1933 (UINTN
) &(((VARIABLE_HEADER
*)Variable
->CurrPtr
)->State
),
1937 if (!EFI_ERROR (Status
)) {
1938 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1939 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1944 // Logic comes here to update variable.
1945 // If the variable is marked valid and the same data has been passed in
1946 // then return to the caller immediately.
1948 if (DataSizeOfVariable (&VariableHeader
) == DataSize
) {
1949 NextVariable
= (VARIABLE_HEADER
*)GetEndPointer (VariableGlobal
->VolatileVariableBase
, TRUE
, VariableGlobal
, Instance
);
1950 GetVariableDataPtr (Variable
->CurrPtr
, Variable
->Volatile
, VariableGlobal
, Instance
, (CHAR16
*) NextVariable
);
1951 if (CompareMem (Data
, (VOID
*) NextVariable
, DataSize
) == 0) {
1952 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1953 Status
= EFI_SUCCESS
;
1957 if ((VariableHeader
.State
== VAR_ADDED
) ||
1958 (VariableHeader
.State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1960 // If new data is different from the old one, mark the old one as VAR_IN_DELETED_TRANSITION.
1961 // It will be deleted if new variable is successfully written.
1963 State
= VariableHeader
.State
;
1964 State
&= VAR_IN_DELETED_TRANSITION
;
1966 Status
= AccessVariableStore (
1971 (UINTN
) &(((VARIABLE_HEADER
*)Variable
->CurrPtr
)->State
),
1975 if (EFI_ERROR (Status
)) {
1981 // Create a new variable
1985 // Make sure we are trying to create a new variable.
1986 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
1988 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1989 Status
= EFI_NOT_FOUND
;
1994 // Only variable have NV|RT attribute can be created in Runtime
1996 if (EfiAtRuntime () &&
1997 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1998 Status
= EFI_INVALID_PARAMETER
;
2004 // Function part - create a new variable and copy the data.
2005 // Both update a variable and create a variable will come here.
2007 // Tricky part: Use scratch data area at the end of volatile variable store
2008 // as a temporary storage.
2010 NextVariable
= (VARIABLE_HEADER
*)GetEndPointer (VariableGlobal
->VolatileVariableBase
, TRUE
, VariableGlobal
, Instance
);
2011 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
2012 NextVariableHeader
= (VARIABLE_HEADER
*) NextVariable
;
2014 SetMem (NextVariableHeader
, ScratchSize
, 0xff);
2016 NextVariableHeader
->StartId
= VARIABLE_DATA
;
2017 NextVariableHeader
->Attributes
= Attributes
;
2018 NextVariableHeader
->PubKeyIndex
= KeyIndex
;
2019 NextVariableHeader
->MonotonicCount
= MonotonicCount
;
2020 NextVariableHeader
->Reserved
= 0;
2021 VarNameOffset
= sizeof (VARIABLE_HEADER
);
2022 VarNameSize
= StrSize (VariableName
);
2024 (UINT8
*) ((UINTN
)NextVariable
+ VarNameOffset
),
2028 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
2030 (UINT8
*) ((UINTN
)NextVariable
+ VarDataOffset
),
2034 CopyMem (&NextVariableHeader
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
2036 // There will be pad bytes after Data, the NextVariable->NameSize and
2037 // NextVariable->DataSize should not include pad size so that variable
2038 // service can get actual size in GetVariable.
2040 NextVariableHeader
->NameSize
= (UINT32
)VarNameSize
;
2041 NextVariableHeader
->DataSize
= (UINT32
)DataSize
;
2044 // The actual size of the variable that stores in storage should
2045 // include pad size.
2047 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
2048 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2050 // Create a nonvolatile variable
2054 GetVarStoreHeader (VariableGlobal
->NonVolatileVariableBase
, FALSE
, VariableGlobal
, Instance
, &VariableStoreHeader
);
2055 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
2056 && ((HEADER_ALIGN (VarSize
) + Global
->HwErrVariableTotalSize
) > PcdGet32(PcdHwErrStorageSize
)))
2057 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
2058 && ((HEADER_ALIGN (VarSize
) + Global
->CommonVariableTotalSize
) > VariableStoreHeader
.Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
)))) {
2059 if (EfiAtRuntime ()) {
2060 Status
= EFI_OUT_OF_RESOURCES
;
2064 // Perform garbage collection & reclaim operation
2066 Status
= Reclaim (VariableGlobal
->NonVolatileVariableBase
, &(Global
->NonVolatileLastVariableOffset
), FALSE
, VirtualMode
, Global
, Variable
->CurrPtr
);
2067 if (EFI_ERROR (Status
)) {
2073 // If still no enough space, return out of resources
2075 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
2076 && ((HEADER_ALIGN (VarSize
) + Global
->HwErrVariableTotalSize
) > PcdGet32(PcdHwErrStorageSize
)))
2077 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
2078 && ((HEADER_ALIGN (VarSize
) + Global
->CommonVariableTotalSize
) > VariableStoreHeader
.Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
)))) {
2079 Status
= EFI_OUT_OF_RESOURCES
;
2085 // 1. Write variable header
2086 // 2. Set variable state to header valid
2087 // 3. Write variable data
2088 // 4. Set variable state to valid
2093 Status
= AccessVariableStore (
2098 VariableGlobal
->NonVolatileVariableBase
+ Global
->NonVolatileLastVariableOffset
,
2099 sizeof (VARIABLE_HEADER
),
2100 (UINT8
*) NextVariable
2103 if (EFI_ERROR (Status
)) {
2110 NextVariableHeader
->State
= VAR_HEADER_VALID_ONLY
;
2111 Status
= AccessVariableStore (
2116 VariableGlobal
->NonVolatileVariableBase
+ Global
->NonVolatileLastVariableOffset
,
2117 sizeof (VARIABLE_HEADER
),
2118 (UINT8
*) NextVariable
2121 if (EFI_ERROR (Status
)) {
2127 Status
= AccessVariableStore (
2132 VariableGlobal
->NonVolatileVariableBase
+ Global
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
2133 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
2134 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
2137 if (EFI_ERROR (Status
)) {
2143 NextVariableHeader
->State
= VAR_ADDED
;
2144 Status
= AccessVariableStore (
2149 VariableGlobal
->NonVolatileVariableBase
+ Global
->NonVolatileLastVariableOffset
,
2150 sizeof (VARIABLE_HEADER
),
2151 (UINT8
*) NextVariable
2154 if (EFI_ERROR (Status
)) {
2158 Global
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2160 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
2161 Global
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2163 Global
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2167 // Create a volatile variable
2171 if ((UINT32
) (HEADER_ALIGN(VarSize
) + Global
->VolatileLastVariableOffset
) >
2172 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (VariableGlobal
->VolatileVariableBase
)))->Size
) {
2174 // Perform garbage collection & reclaim operation
2176 Status
= Reclaim (VariableGlobal
->VolatileVariableBase
, &Global
->VolatileLastVariableOffset
, TRUE
, VirtualMode
, Global
, Variable
->CurrPtr
);
2177 if (EFI_ERROR (Status
)) {
2181 // If still no enough space, return out of resources
2183 if ((UINT32
) (HEADER_ALIGN (VarSize
) + Global
->VolatileLastVariableOffset
) >
2184 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (VariableGlobal
->VolatileVariableBase
)))->Size
2186 Status
= EFI_OUT_OF_RESOURCES
;
2192 NextVariableHeader
->State
= VAR_ADDED
;
2193 Status
= AccessVariableStore (
2198 VariableGlobal
->VolatileVariableBase
+ Global
->VolatileLastVariableOffset
,
2200 (UINT8
*) NextVariable
2203 if (EFI_ERROR (Status
)) {
2207 Global
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2210 // Mark the old variable as deleted
2211 // If storage has just been reclaimed, the old variable marked as VAR_IN_DELETED_TRANSITION
2212 // has already been eliminated, so no need to delete it.
2214 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
->CurrPtr
!= 0) {
2215 State
= ((VARIABLE_HEADER
*)Variable
->CurrPtr
)->State
;
2216 State
&= VAR_DELETED
;
2218 Status
= AccessVariableStore (
2223 (UINTN
) &(((VARIABLE_HEADER
*)Variable
->CurrPtr
)->State
),
2229 if (!EFI_ERROR (Status
)) {
2230 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
2231 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
2239 Implements EsalGetVariable function of Extended SAL Variable Services Class.
2241 This function implements EsalGetVariable function of Extended SAL Variable Services Class.
2242 It is equivalent in functionality to the EFI Runtime Service GetVariable().
2244 @param[in] VariableName A Null-terminated Unicode string that is the name of
2245 the vendor's variable.
2246 @param[in] VendorGuid A unique identifier for the vendor.
2247 @param[out] Attributes If not NULL, a pointer to the memory location to return the
2248 attributes bitmask for the variable.
2249 @param[in, out] DataSize Size of Data found. If size is less than the
2250 data, this value contains the required size.
2251 @param[out] Data On input, the size in bytes of the return Data buffer.
2252 On output, the size of data returned in Data.
2253 @param[in] VirtualMode Current calling mode for this function.
2254 @param[in] Global Context of this Extended SAL Variable Services Class call.
2256 @retval EFI_SUCCESS The function completed successfully.
2257 @retval EFI_NOT_FOUND The variable was not found.
2258 @retval EFI_BUFFER_TOO_SMALL DataSize is too small for the result. DataSize has
2259 been updated with the size needed to complete the request.
2260 @retval EFI_INVALID_PARAMETER VariableName is NULL.
2261 @retval EFI_INVALID_PARAMETER VendorGuid is NULL.
2262 @retval EFI_INVALID_PARAMETER DataSize is NULL.
2263 @retval EFI_INVALID_PARAMETER DataSize is not too small and Data is NULL.
2264 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
2265 @retval EFI_SECURITY_VIOLATION The variable could not be retrieved due to an authentication failure.
2271 IN CHAR16
*VariableName
,
2272 IN EFI_GUID
*VendorGuid
,
2273 OUT UINT32
*Attributes OPTIONAL
,
2274 IN OUT UINTN
*DataSize
,
2276 IN BOOLEAN VirtualMode
,
2277 IN ESAL_VARIABLE_GLOBAL
*Global
2280 VARIABLE_POINTER_TRACK Variable
;
2283 VARIABLE_HEADER VariableHeader
;
2285 VARIABLE_GLOBAL
*VariableGlobal
;
2288 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
2289 return EFI_INVALID_PARAMETER
;
2292 VariableGlobal
= &Global
->VariableGlobal
[VirtualMode
];
2293 Instance
= Global
->FvbInstance
;
2295 AcquireLockOnlyAtBootTime(&VariableGlobal
->VariableServicesLock
);
2298 // Check if this variable exists in cache.
2300 Status
= FindVariableInCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
2301 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_SUCCESS
)){
2303 // If variable exists in cache, just update statistical information for it and finish.
2304 // Here UpdateVariableInfo() has already retrieved data & attributes for output.
2306 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, TRUE
, FALSE
, FALSE
, TRUE
);
2310 // If variable does not exist in cache, search for it in variable storage area.
2312 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, VariableGlobal
, Instance
);
2313 if (Variable
.CurrPtr
== 0x0 || EFI_ERROR (Status
)) {
2315 // If it cannot be found in variable storage area, goto Done.
2320 Valid
= IsValidVariableHeader (Variable
.CurrPtr
, Variable
.Volatile
, VariableGlobal
, Instance
, &VariableHeader
);
2322 Status
= EFI_NOT_FOUND
;
2326 // If variable exists, but not in cache, get its data and attributes, update
2327 // statistical information, and update cache.
2329 VarDataSize
= DataSizeOfVariable (&VariableHeader
);
2330 ASSERT (VarDataSize
!= 0);
2332 if (*DataSize
>= VarDataSize
) {
2334 Status
= EFI_INVALID_PARAMETER
;
2338 GetVariableDataPtr (
2345 if (Attributes
!= NULL
) {
2346 *Attributes
= VariableHeader
.Attributes
;
2349 *DataSize
= VarDataSize
;
2350 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
2351 UpdateVariableCache (VariableName
, VendorGuid
, VariableHeader
.Attributes
, VarDataSize
, Data
);
2353 Status
= EFI_SUCCESS
;
2357 // If DataSize is too small for the result, return EFI_BUFFER_TOO_SMALL.
2359 *DataSize
= VarDataSize
;
2360 Status
= EFI_BUFFER_TOO_SMALL
;
2365 ReleaseLockOnlyAtBootTime (&VariableGlobal
->VariableServicesLock
);
2370 Implements EsalGetNextVariableName function of Extended SAL Variable Services Class.
2372 This function implements EsalGetNextVariableName function of Extended SAL Variable Services Class.
2373 It is equivalent in functionality to the EFI Runtime Service GetNextVariableName().
2375 @param[in, out] VariableNameSize Size of the variable
2376 @param[in, out] VariableName On input, supplies the last VariableName that was returned by GetNextVariableName().
2377 On output, returns the Null-terminated Unicode string of the current variable.
2378 @param[in, out] VendorGuid On input, supplies the last VendorGuid that was returned by GetNextVariableName().
2379 On output, returns the VendorGuid of the current variable.
2380 @param[in] VirtualMode Current calling mode for this function.
2381 @param[in] Global Context of this Extended SAL Variable Services Class call.
2383 @retval EFI_SUCCESS The function completed successfully.
2384 @retval EFI_NOT_FOUND The next variable was not found.
2385 @retval EFI_BUFFER_TOO_SMALL VariableNameSize is too small for the result.
2386 VariableNameSize has been updated with the size needed to complete the request.
2387 @retval EFI_INVALID_PARAMETER VariableNameSize is NULL.
2388 @retval EFI_INVALID_PARAMETER VariableName is NULL.
2389 @retval EFI_INVALID_PARAMETER VendorGuid is NULL.
2390 @retval EFI_DEVICE_ERROR The variable name could not be retrieved due to a hardware error.
2395 EsalGetNextVariableName (
2396 IN OUT UINTN
*VariableNameSize
,
2397 IN OUT CHAR16
*VariableName
,
2398 IN OUT EFI_GUID
*VendorGuid
,
2399 IN BOOLEAN VirtualMode
,
2400 IN ESAL_VARIABLE_GLOBAL
*Global
2403 VARIABLE_POINTER_TRACK Variable
;
2406 VARIABLE_HEADER VariableHeader
;
2407 VARIABLE_GLOBAL
*VariableGlobal
;
2410 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
2411 return EFI_INVALID_PARAMETER
;
2414 VariableGlobal
= &Global
->VariableGlobal
[VirtualMode
];
2415 Instance
= Global
->FvbInstance
;
2417 AcquireLockOnlyAtBootTime(&VariableGlobal
->VariableServicesLock
);
2419 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, VariableGlobal
, Instance
);
2421 // If the variable does not exist, goto Done and return.
2423 if (Variable
.CurrPtr
== 0x0 || EFI_ERROR (Status
)) {
2427 if (VariableName
[0] != 0) {
2429 // If variable name is not NULL, get next variable
2431 Variable
.CurrPtr
= GetNextVariablePtr (
2440 if (Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== 0x0) {
2442 // If fail to find a variable in current area, reverse the volatile attribute of area to search.
2444 Variable
.Volatile
= (BOOLEAN
) (Variable
.Volatile
^ ((BOOLEAN
) 0x1));
2446 // Here we depend on the searching sequence of FindVariable().
2447 // It first searches volatile area, then NV area.
2448 // So if the volatile attribute after switching is non-volatile, it means that we have finished searching volatile area,
2449 // and EFI_NOT_FOUND is returnd.
2450 // Otherwise, it means that we have finished searchig non-volatile area, and we will continue to search volatile area.
2452 if (!Variable
.Volatile
) {
2453 Variable
.StartPtr
= GetStartPointer (VariableGlobal
->NonVolatileVariableBase
);
2454 Variable
.EndPtr
= GetEndPointer (VariableGlobal
->NonVolatileVariableBase
, FALSE
, VariableGlobal
, Instance
);
2456 Status
= EFI_NOT_FOUND
;
2460 Variable
.CurrPtr
= Variable
.StartPtr
;
2461 if (!IsValidVariableHeader (Variable
.CurrPtr
, Variable
.Volatile
, VariableGlobal
, Instance
, NULL
)) {
2466 // Variable is found
2468 if (IsValidVariableHeader (Variable
.CurrPtr
, Variable
.Volatile
, VariableGlobal
, Instance
, &VariableHeader
)) {
2469 if ((VariableHeader
.State
== VAR_ADDED
) &&
2470 (!(EfiAtRuntime () && ((VariableHeader
.Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)))) {
2471 VarNameSize
= NameSizeOfVariable (&VariableHeader
);
2472 ASSERT (VarNameSize
!= 0);
2474 if (VarNameSize
<= *VariableNameSize
) {
2475 GetVariableNamePtr (
2484 &VariableHeader
.VendorGuid
,
2487 Status
= EFI_SUCCESS
;
2489 Status
= EFI_BUFFER_TOO_SMALL
;
2492 *VariableNameSize
= VarNameSize
;
2497 Variable
.CurrPtr
= GetNextVariablePtr (
2506 ReleaseLockOnlyAtBootTime (&VariableGlobal
->VariableServicesLock
);
2511 Implements EsalSetVariable function of Extended SAL Variable Services Class.
2513 This function implements EsalSetVariable function of Extended SAL Variable Services Class.
2514 It is equivalent in functionality to the EFI Runtime Service SetVariable().
2516 @param[in] VariableName A Null-terminated Unicode string that is the name of the vendor's
2517 variable. Each VariableName is unique for each
2518 VendorGuid. VariableName must contain 1 or more
2519 Unicode characters. If VariableName is an empty Unicode
2520 string, then EFI_INVALID_PARAMETER is returned.
2521 @param[in] VendorGuid A unique identifier for the vendor.
2522 @param[in] Attributes Attributes bitmask to set for the variable.
2523 @param[in] DataSize The size in bytes of the Data buffer. A size of zero causes the
2524 variable to be deleted.
2525 @param[in] Data The contents for the variable.
2526 @param[in] VirtualMode Current calling mode for this function.
2527 @param[in] Global Context of this Extended SAL Variable Services Class call.
2529 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
2530 defined by the Attributes.
2531 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied, or the
2532 DataSize exceeds the maximum allowed.
2533 @retval EFI_INVALID_PARAMETER VariableName is an empty Unicode string.
2534 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
2535 @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.
2536 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
2537 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
2538 @retval EFI_SECURITY_VIOLATION The variable could not be retrieved due to an authentication failure.
2539 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
2545 IN CHAR16
*VariableName
,
2546 IN EFI_GUID
*VendorGuid
,
2547 IN UINT32 Attributes
,
2550 IN BOOLEAN VirtualMode
,
2551 IN ESAL_VARIABLE_GLOBAL
*Global
2554 VARIABLE_POINTER_TRACK Variable
;
2556 EFI_PHYSICAL_ADDRESS NextVariable
;
2557 EFI_PHYSICAL_ADDRESS Point
;
2558 VARIABLE_GLOBAL
*VariableGlobal
;
2561 UINT64 MonotonicCount
;
2565 // Check input parameters
2567 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2568 return EFI_INVALID_PARAMETER
;
2571 if (DataSize
!= 0 && Data
== NULL
) {
2572 return EFI_INVALID_PARAMETER
;
2576 // EFI_VARIABLE_RUNTIME_ACCESS bit cannot be set without EFI_VARIABLE_BOOTSERVICE_ACCESS bit.
2578 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2579 return EFI_INVALID_PARAMETER
;
2582 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) {
2583 if (DataSize
< AUTHINFO_SIZE
) {
2585 // Try to write Authencated Variable without AuthInfo
2587 return EFI_SECURITY_VIOLATION
;
2589 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
2591 PayloadSize
= DataSize
;
2594 VariableGlobal
= &Global
->VariableGlobal
[VirtualMode
];
2595 Instance
= Global
->FvbInstance
;
2597 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2599 // For variable for hardware error record, the size of the VariableName, including the Unicode Null
2600 // in bytes plus the DataSize is limited to maximum size of PcdGet32(PcdMaxHardwareErrorVariableSize) bytes.
2602 if ((PayloadSize
> PcdGet32(PcdMaxHardwareErrorVariableSize
)) ||
2603 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32(PcdMaxHardwareErrorVariableSize
))) {
2604 return EFI_INVALID_PARAMETER
;
2607 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"
2609 if (StrnCmp (VariableName
, \
2610 Global
->VariableName
[VirtualMode
][VAR_HW_ERR_REC
], \
2611 StrLen(Global
->VariableName
[VirtualMode
][VAR_HW_ERR_REC
])) != 0) {
2612 return EFI_INVALID_PARAMETER
;
2616 // For variable not for hardware error record, the size of the VariableName, including the
2617 // Unicode Null in bytes plus the DataSize is limited to maximum size of PcdGet32(PcdMaxVariableSize) bytes.
2619 if ((PayloadSize
> PcdGet32(PcdMaxVariableSize
)) ||
2620 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32(PcdMaxVariableSize
))) {
2621 return EFI_INVALID_PARAMETER
;
2625 AcquireLockOnlyAtBootTime(&VariableGlobal
->VariableServicesLock
);
2628 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated;
2630 if (InterlockedIncrement (&Global
->ReentrantState
) > 1) {
2631 Point
= VariableGlobal
->NonVolatileVariableBase
;;
2633 // Parse non-volatile variable data and get last variable offset
2635 NextVariable
= GetStartPointer (Point
);
2636 while (IsValidVariableHeader (NextVariable
, FALSE
, VariableGlobal
, Instance
, NULL
)) {
2637 NextVariable
= GetNextVariablePtr (NextVariable
, FALSE
, VariableGlobal
, Instance
);
2639 Global
->NonVolatileLastVariableOffset
= NextVariable
- Point
;
2643 // Check whether the input variable exists
2646 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, VariableGlobal
, Instance
);
2649 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang
2651 AutoUpdateLangVariable (VariableName
, Data
, PayloadSize
, VirtualMode
, Global
);
2654 // Process PK, KEK, Sigdb seperately
2656 if (CompareGuid (VendorGuid
, Global
->GlobalVariableGuid
[VirtualMode
]) && (StrCmp (VariableName
, Global
->VariableName
[VirtualMode
][VAR_PLATFORM_KEY
]) == 0)) {
2657 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, VirtualMode
, Global
, &Variable
, Attributes
, TRUE
);
2658 } else if (CompareGuid (VendorGuid
, Global
->GlobalVariableGuid
[VirtualMode
]) && (StrCmp (VariableName
, Global
->VariableName
[VirtualMode
][VAR_KEY_EXCHANGE_KEY
]) == 0)) {
2659 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, VirtualMode
, Global
, &Variable
, Attributes
, FALSE
);
2660 } else if (CompareGuid (VendorGuid
, Global
->ImageSecurityDatabaseGuid
[VirtualMode
])) {
2661 Status
= ProcessVarWithKek (VariableName
, VendorGuid
, Data
, DataSize
, VirtualMode
, Global
, &Variable
, Attributes
);
2663 Status
= VerifyVariable (Data
, DataSize
, VirtualMode
, Global
, &Variable
, Attributes
, &KeyIndex
, &MonotonicCount
);
2664 if (!EFI_ERROR(Status
)) {
2666 // Verification pass
2668 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2670 // Cut the certificate size before set
2672 Status
= UpdateVariable (
2675 (UINT8
*)Data
+ AUTHINFO_SIZE
,
2676 DataSize
- AUTHINFO_SIZE
,
2686 // Update variable as usual
2688 Status
= UpdateVariable (
2704 InterlockedDecrement (&Global
->ReentrantState
);
2705 ReleaseLockOnlyAtBootTime (&VariableGlobal
->VariableServicesLock
);
2710 Implements EsalQueryVariableInfo function of Extended SAL Variable Services Class.
2712 This function implements EsalQueryVariableInfo function of Extended SAL Variable Services Class.
2713 It is equivalent in functionality to the EFI Runtime Service QueryVariableInfo().
2715 @param[in] Attributes Attributes bitmask to specify the type of variables
2716 on which to return information.
2717 @param[out] MaximumVariableStorageSize On output the maximum size of the storage space available for
2718 the EFI variables associated with the attributes specified.
2719 @param[out] RemainingVariableStorageSize Returns the remaining size of the storage space available for EFI
2720 variables associated with the attributes specified.
2721 @param[out] MaximumVariableSize Returns the maximum size of an individual EFI variable
2722 associated with the attributes specified.
2723 @param[in] VirtualMode Current calling mode for this function
2724 @param[in] Global Context of this Extended SAL Variable Services Class call
2726 @retval EFI_SUCCESS Valid answer returned.
2727 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2728 @retval EFI_UNSUPPORTED The attribute is not supported on this platform, and the
2729 MaximumVariableStorageSize, RemainingVariableStorageSize,
2730 MaximumVariableSize are undefined.
2734 EsalQueryVariableInfo (
2735 IN UINT32 Attributes
,
2736 OUT UINT64
*MaximumVariableStorageSize
,
2737 OUT UINT64
*RemainingVariableStorageSize
,
2738 OUT UINT64
*MaximumVariableSize
,
2739 IN BOOLEAN VirtualMode
,
2740 IN ESAL_VARIABLE_GLOBAL
*Global
2743 EFI_PHYSICAL_ADDRESS Variable
;
2744 EFI_PHYSICAL_ADDRESS NextVariable
;
2745 UINT64 VariableSize
;
2746 EFI_PHYSICAL_ADDRESS VariableStoreHeaderAddress
;
2748 VARIABLE_STORE_HEADER VarStoreHeader
;
2749 VARIABLE_HEADER VariableHeader
;
2750 UINT64 CommonVariableTotalSize
;
2751 UINT64 HwErrVariableTotalSize
;
2752 VARIABLE_GLOBAL
*VariableGlobal
;
2755 CommonVariableTotalSize
= 0;
2756 HwErrVariableTotalSize
= 0;
2758 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2759 return EFI_INVALID_PARAMETER
;
2762 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2764 // Make sure the Attributes combination is supported by the platform.
2766 return EFI_UNSUPPORTED
;
2767 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2769 // Make sure if runtime bit is set, boot service bit is set also.
2771 return EFI_INVALID_PARAMETER
;
2772 } else if (EfiAtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2774 // Make sure RT Attribute is set if we are in Runtime phase.
2776 return EFI_INVALID_PARAMETER
;
2777 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2779 // Make sure Hw Attribute is set with NV.
2781 return EFI_INVALID_PARAMETER
;
2784 VariableGlobal
= &Global
->VariableGlobal
[VirtualMode
];
2785 Instance
= Global
->FvbInstance
;
2787 AcquireLockOnlyAtBootTime(&VariableGlobal
->VariableServicesLock
);
2789 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2791 // Query is Volatile related.
2794 VariableStoreHeaderAddress
= VariableGlobal
->VolatileVariableBase
;
2797 // Query is Non-Volatile related.
2800 VariableStoreHeaderAddress
= VariableGlobal
->NonVolatileVariableBase
;
2804 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2805 // with the storage size (excluding the storage header size).
2807 GetVarStoreHeader (VariableStoreHeaderAddress
, Volatile
, VariableGlobal
, Instance
, &VarStoreHeader
);
2809 *MaximumVariableStorageSize
= VarStoreHeader
.Size
- sizeof (VARIABLE_STORE_HEADER
);
2811 // Harware error record variable needs larger size.
2813 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2814 *MaximumVariableStorageSize
= PcdGet32(PcdHwErrStorageSize
);
2815 *MaximumVariableSize
= PcdGet32(PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2817 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2818 ASSERT (PcdGet32(PcdHwErrStorageSize
) < VarStoreHeader
.Size
);
2819 *MaximumVariableStorageSize
= VarStoreHeader
.Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
);
2823 // Let *MaximumVariableSize be PcdGet32(PcdMaxVariableSize) with the exception of the variable header size.
2825 *MaximumVariableSize
= PcdGet32(PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2829 // Point to the starting address of the variables.
2831 Variable
= GetStartPointer (VariableStoreHeaderAddress
);
2834 // Now walk through the related variable store.
2836 while (IsValidVariableHeader (Variable
, Volatile
, VariableGlobal
, Instance
, &VariableHeader
) &&
2837 (Variable
< GetEndPointer (VariableStoreHeaderAddress
, Volatile
, VariableGlobal
, Instance
))) {
2838 NextVariable
= GetNextVariablePtr (Variable
, Volatile
, VariableGlobal
, Instance
);
2839 VariableSize
= NextVariable
- Variable
;
2841 if (EfiAtRuntime ()) {
2843 // we don't take the state of the variables in mind
2844 // when calculating RemainingVariableStorageSize,
2845 // since the space occupied by variables not marked with
2846 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2848 if ((VariableHeader
.Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2849 HwErrVariableTotalSize
+= VariableSize
;
2851 CommonVariableTotalSize
+= VariableSize
;
2855 // Only care about Variables with State VAR_ADDED,because
2856 // the space not marked as VAR_ADDED is reclaimable now.
2858 if (VariableHeader
.State
== VAR_ADDED
) {
2859 if ((VariableHeader
.Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2860 HwErrVariableTotalSize
+= VariableSize
;
2862 CommonVariableTotalSize
+= VariableSize
;
2868 // Go to the next one
2870 Variable
= NextVariable
;
2873 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2874 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2876 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2879 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2880 *MaximumVariableSize
= 0;
2881 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2882 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2885 ReleaseLockOnlyAtBootTime (&VariableGlobal
->VariableServicesLock
);
2890 Notification function of EVT_GROUP_READY_TO_BOOT event group.
2892 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.
2893 When the Boot Manager is about to load and execute a boot option, it reclaims variable
2894 storage if free size is below the threshold.
2896 @param[in] Event Event whose notification function is being invoked.
2897 @param[in] Context Pointer to the notification function's context.
2909 UINTN CommonVariableSpace
;
2910 UINTN RemainingCommonVariableSpace
;
2911 UINTN RemainingHwErrVariableSpace
;
2913 VarSize
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
[Physical
].NonVolatileVariableBase
))->Size
;
2914 Status
= EFI_SUCCESS
;
2916 //Allowable max size of common variable storage space
2918 CommonVariableSpace
= VarSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
);
2920 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2922 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2924 // If the free area is below a threshold, then performs reclaim operation.
2926 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2927 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2928 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2930 mVariableModuleGlobal
->VariableGlobal
[Physical
].NonVolatileVariableBase
,
2931 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2934 mVariableModuleGlobal
,
2937 ASSERT_EFI_ERROR (Status
);
2942 Flush the HOB variable to NV variable storage.
2951 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2952 VARIABLE_HEADER
*VariableHeader
;
2954 // Get HOB variable store.
2956 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
2957 if (GuidHob
!= NULL
) {
2958 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) GET_GUID_HOB_DATA (GuidHob
);
2959 if (CompareGuid (&VariableStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
) &&
2960 (VariableStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
) &&
2961 (VariableStoreHeader
->State
== VARIABLE_STORE_HEALTHY
)
2963 DEBUG ((EFI_D_INFO
, "HOB Variable Store appears to be valid.\n"));
2965 // Flush the HOB variable to NV Variable storage.
2967 for ( VariableHeader
= (VARIABLE_HEADER
*) HEADER_ALIGN (VariableStoreHeader
+ 1)
2968 ; (VariableHeader
< (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VariableStoreHeader
+ VariableStoreHeader
->Size
)
2970 (VariableHeader
->StartId
== VARIABLE_DATA
))
2971 ; VariableHeader
= (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) (VariableHeader
+ 1)
2972 + VariableHeader
->NameSize
+ GET_PAD_SIZE (VariableHeader
->NameSize
)
2973 + VariableHeader
->DataSize
+ GET_PAD_SIZE (VariableHeader
->DataSize
)
2976 ASSERT (VariableHeader
->State
== VAR_ADDED
);
2977 ASSERT ((VariableHeader
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
2978 Status
= EsalSetVariable (
2979 (CHAR16
*) (VariableHeader
+ 1),
2980 &VariableHeader
->VendorGuid
,
2981 VariableHeader
->Attributes
,
2982 VariableHeader
->DataSize
,
2983 (UINT8
*) (VariableHeader
+ 1) + VariableHeader
->NameSize
+ GET_PAD_SIZE (VariableHeader
->NameSize
),
2985 mVariableModuleGlobal
2987 ASSERT_EFI_ERROR (Status
);
2994 Initializes variable store area for non-volatile and volatile variable.
2996 This function allocates and initializes memory space for global context of ESAL
2997 variable service and variable store area for non-volatile and volatile variable.
2999 @param[in] ImageHandle The Image handle of this driver.
3000 @param[in] SystemTable The pointer of EFI_SYSTEM_TABLE.
3002 @retval EFI_SUCCESS Function successfully executed.
3003 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3007 VariableCommonInitialize (
3008 IN EFI_HANDLE ImageHandle
,
3009 IN EFI_SYSTEM_TABLE
*SystemTable