3 Copyright (c) 2006 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Provide support functions for variable services.
22 #include "FSVariable.h"
24 VARIABLE_STORE_HEADER mStoreHeaderTemplate
= {
25 VARIABLE_STORE_SIGNATURE
,
26 VOLATILE_VARIABLE_STORE_SIZE
,
27 VARIABLE_STORE_FORMATTED
,
28 VARIABLE_STORE_HEALTHY
,
34 // Don't use module globals after the SetVirtualAddress map is signaled
36 VARIABLE_GLOBAL
*mGlobal
;
40 OnVirtualAddressChangeFsv (
47 OnSimpleFileSystemInstall (
53 IsValidVariableHeader (
54 IN VARIABLE_HEADER
*Variable
60 This code checks if variable header is valid or not.
63 Variable Pointer to the Variable Header.
66 TRUE Variable header is valid.
67 FALSE Variable header is not valid.
71 if (Variable
== NULL
||
72 Variable
->StartId
!= VARIABLE_DATA
||
73 (sizeof (VARIABLE_HEADER
) + Variable
->NameSize
+ Variable
->DataSize
) > FixedPcdGet32(PcdMaxVariableSize
)
82 GetVariableStoreStatus (
83 IN VARIABLE_STORE_HEADER
*VarStoreHeader
89 This code gets the current status of Variable Store.
93 VarStoreHeader Pointer to the Variable Store Header.
97 EfiRaw Variable store status is raw
98 EfiValid Variable store status is valid
99 EfiInvalid Variable store status is invalid
103 if ((VarStoreHeader
->Signature
== mStoreHeaderTemplate
.Signature
) &&
104 (VarStoreHeader
->Format
== mStoreHeaderTemplate
.Format
) &&
105 (VarStoreHeader
->State
== mStoreHeaderTemplate
.State
)
108 } else if (VarStoreHeader
->Signature
== VAR_DEFAULT_VALUE_32
&&
109 VarStoreHeader
->Size
== VAR_DEFAULT_VALUE_32
&&
110 VarStoreHeader
->Format
== VAR_DEFAULT_VALUE
&&
111 VarStoreHeader
->State
== VAR_DEFAULT_VALUE
122 IN VARIABLE_HEADER
*Variable
128 This code gets the pointer to the variable data.
132 Variable Pointer to the Variable Header.
136 UINT8* Pointer to Variable Data
141 // Be careful about pad size for alignment
143 return (UINT8
*) ((UINTN
) GET_VARIABLE_NAME_PTR (Variable
) + Variable
->NameSize
+ GET_PAD_SIZE (Variable
->NameSize
));
148 IN VARIABLE_HEADER
*Variable
154 This code gets the pointer to the next variable header.
158 Variable Pointer to the Variable Header.
162 VARIABLE_HEADER* Pointer to next variable header.
166 if (!IsValidVariableHeader (Variable
)) {
170 // Be careful about pad size for alignment
172 return (VARIABLE_HEADER
*) ((UINTN
) GetVariableDataPtr (Variable
) + Variable
->DataSize
+ GET_PAD_SIZE (Variable
->DataSize
));
177 IN VARIABLE_STORE_HEADER
*VarStoreHeader
183 This code gets the pointer to the last variable memory pointer byte
187 VarStoreHeader Pointer to the Variable Store Header.
191 VARIABLE_HEADER* Pointer to last unavailable Variable Header
196 // The end of variable store
198 return (VARIABLE_HEADER
*) ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
203 IN VARIABLE_HEADER
*Variable
209 Check if exist newer variable when doing reclaim
213 Variable Pointer to start position
217 TRUE - Exists another variable, which is newer than the current one
218 FALSE - Doesn't exist another vairable which is newer than the current one
222 VARIABLE_HEADER
*NextVariable
;
223 CHAR16
*VariableName
;
224 EFI_GUID
*VendorGuid
;
226 VendorGuid
= &Variable
->VendorGuid
;
227 VariableName
= GET_VARIABLE_NAME_PTR(Variable
);
229 NextVariable
= GetNextVariablePtr (Variable
);
230 while (IsValidVariableHeader (NextVariable
)) {
231 if ((NextVariable
->State
== VAR_ADDED
) || (NextVariable
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
233 // If match Guid and Name
235 if (CompareGuid (VendorGuid
, &NextVariable
->VendorGuid
)) {
236 if (CompareMem (VariableName
, GET_VARIABLE_NAME_PTR (NextVariable
), StrSize (VariableName
)) == 0) {
241 NextVariable
= GetNextVariablePtr (NextVariable
);
248 IN VARIABLE_STORAGE_TYPE StorageType
,
249 IN VARIABLE_HEADER
*CurrentVariable OPTIONAL
255 Variable store garbage collection and reclaim operation
259 IsVolatile The variable store is volatile or not,
260 if it is non-volatile, need FTW
261 CurrentVairable If it is not NULL, it means not to process
262 current variable for Reclaim.
270 VARIABLE_HEADER
*Variable
;
271 VARIABLE_HEADER
*NextVariable
;
272 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
274 UINTN ValidBufferSize
;
279 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[StorageType
];
282 // Start Pointers for the variable.
284 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
288 // To make the reclaim, here we just allocate a memory that equal to the original memory
290 ValidBufferSize
= sizeof (VARIABLE_STORE_HEADER
) + VariableStoreHeader
->Size
;
292 Status
= gBS
->AllocatePool (
295 (VOID
**) &ValidBuffer
297 if (EFI_ERROR (Status
)) {
301 CurrPtr
= ValidBuffer
;
304 // Copy variable store header
306 CopyMem (CurrPtr
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
307 CurrPtr
+= sizeof (VARIABLE_STORE_HEADER
);
310 // Start Pointers for the variable.
312 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
315 ValidBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
316 while (IsValidVariableHeader (Variable
)) {
317 NextVariable
= GetNextVariablePtr (Variable
);
319 // State VAR_ADDED or VAR_IN_DELETED_TRANSITION are to kept,
320 // The CurrentVariable, is also saved, as SetVariable may fail due to lack of space
322 if (Variable
->State
== VAR_ADDED
) {
323 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
324 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
325 ValidBufferSize
+= VariableSize
;
326 CurrPtr
+= VariableSize
;
327 } else if (Variable
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
)) {
329 // As variables that with the same guid and name may exist in NV due to power failure during SetVariable,
330 // we will only save the latest valid one
332 if (!ExistNewerVariable(Variable
)) {
333 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
334 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
336 // If CurrentVariable == Variable, mark as VAR_IN_DELETED_TRANSITION
338 if (Variable
!= CurrentVariable
){
339 ((VARIABLE_HEADER
*)CurrPtr
)->State
= VAR_ADDED
;
341 CurrPtr
+= VariableSize
;
342 ValidBufferSize
+= VariableSize
;
345 Variable
= NextVariable
;
349 // TODO: cannot restore to original state, basic FTW needed
351 Status
= mGlobal
->VariableStore
[StorageType
]->Erase (
352 mGlobal
->VariableStore
[StorageType
]
354 Status
= mGlobal
->VariableStore
[StorageType
]->Write (
355 mGlobal
->VariableStore
[StorageType
],
361 // ASSERT_EFI_ERROR (Status);
363 mGlobal
->LastVariableOffset
[StorageType
] = ValidBufferSize
;
364 gBS
->FreePool (ValidBuffer
);
371 IN CHAR16
*VariableName
,
372 IN EFI_GUID
*VendorGuid
,
373 OUT VARIABLE_POINTER_TRACK
*PtrTrack
379 This code finds variable in storage blocks (Volatile or Non-Volatile)
383 VariableName Name of the variable to be found
384 VendorGuid Vendor GUID to be found.
385 PtrTrack Variable Track Pointer structure that contains
386 Variable Information.
387 Contains the pointer of Variable header.
391 EFI_INVALID_PARAMETER - Invalid parameter
392 EFI_SUCCESS - Find the specified variable
393 EFI_NOT_FOUND - Not found
397 VARIABLE_HEADER
*Variable
;
398 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
400 VARIABLE_HEADER
*InDeleteVariable
;
402 VARIABLE_HEADER
*InDeleteStartPtr
;
403 VARIABLE_HEADER
*InDeleteEndPtr
;
405 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
406 return EFI_INVALID_PARAMETER
;
409 InDeleteVariable
= NULL
;
410 InDeleteIndex
= (UINTN
)-1;
411 InDeleteStartPtr
= NULL
;
412 InDeleteEndPtr
= NULL
;
414 for (Index
= 0; Index
< MaxType
; Index
++) {
416 // 0: Non-Volatile, 1: Volatile
418 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[Index
];
421 // Start Pointers for the variable.
422 // Actual Data Pointer where data can be written.
424 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
427 // Find the variable by walk through non-volatile and volatile variable store
429 PtrTrack
->StartPtr
= Variable
;
430 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
);
432 while (IsValidVariableHeader (Variable
) && (Variable
< PtrTrack
->EndPtr
)) {
433 if (Variable
->State
== VAR_ADDED
) {
434 if (!EfiAtRuntime () || (Variable
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
435 if (VariableName
[0] == 0) {
436 PtrTrack
->CurrPtr
= Variable
;
437 PtrTrack
->Type
= (VARIABLE_STORAGE_TYPE
) Index
;
440 if (CompareGuid (VendorGuid
, &Variable
->VendorGuid
)) {
441 if (!CompareMem (VariableName
, GET_VARIABLE_NAME_PTR (Variable
), StrSize (VariableName
))) {
442 PtrTrack
->CurrPtr
= Variable
;
443 PtrTrack
->Type
= (VARIABLE_STORAGE_TYPE
) Index
;
449 } else if (Variable
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
)) {
451 // VAR_IN_DELETED_TRANSITION should also be checked.
453 if (!EfiAtRuntime () || (Variable
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
454 if (VariableName
[0] == 0) {
455 InDeleteVariable
= Variable
;
456 InDeleteIndex
= Index
;
457 InDeleteStartPtr
= PtrTrack
->StartPtr
;
458 InDeleteEndPtr
= PtrTrack
->EndPtr
;
460 if (CompareGuid (VendorGuid
, &Variable
->VendorGuid
)) {
461 if (!CompareMem (VariableName
, GET_VARIABLE_NAME_PTR (Variable
), StrSize (VariableName
))) {
462 InDeleteVariable
= Variable
;
463 InDeleteIndex
= Index
;
464 InDeleteStartPtr
= PtrTrack
->StartPtr
;
465 InDeleteEndPtr
= PtrTrack
->EndPtr
;
472 Variable
= GetNextVariablePtr (Variable
);
483 // if VAR_IN_DELETED_TRANSITION found, and VAR_ADDED not found,
486 if (InDeleteVariable
!= NULL
) {
487 PtrTrack
->CurrPtr
= InDeleteVariable
;
488 PtrTrack
->Type
= (VARIABLE_STORAGE_TYPE
) InDeleteIndex
;
489 PtrTrack
->StartPtr
= InDeleteStartPtr
;
490 PtrTrack
->EndPtr
= InDeleteEndPtr
;
494 PtrTrack
->CurrPtr
= NULL
;
495 return EFI_NOT_FOUND
;
501 IN CHAR16
*VariableName
,
502 IN EFI_GUID
*VendorGuid
,
503 OUT UINT32
*Attributes OPTIONAL
,
504 IN OUT UINTN
*DataSize
,
511 This code finds variable in storage blocks (Volatile or Non-Volatile)
515 VariableName Name of Variable to be found
516 VendorGuid Variable vendor GUID
517 Attributes OPTIONAL Attribute value of the variable found
518 DataSize Size of Data found. If size is less than the
519 data, this value contains the required size.
528 VARIABLE_POINTER_TRACK Variable
;
532 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
533 return EFI_INVALID_PARAMETER
;
537 // Find existing variable
539 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
);
541 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
547 VarDataSize
= Variable
.CurrPtr
->DataSize
;
548 if (*DataSize
>= VarDataSize
) {
550 return EFI_INVALID_PARAMETER
;
552 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
554 if (Attributes
!= NULL
) {
555 *Attributes
= Variable
.CurrPtr
->Attributes
;
558 *DataSize
= VarDataSize
;
562 *DataSize
= VarDataSize
;
563 return EFI_BUFFER_TOO_SMALL
;
569 GetNextVariableName (
570 IN OUT UINTN
*VariableNameSize
,
571 IN OUT CHAR16
*VariableName
,
572 IN OUT EFI_GUID
*VendorGuid
578 This code Finds the Next available variable
582 VariableNameSize Size of the variable
583 VariableName Pointer to variable name
584 VendorGuid Variable Vendor Guid
592 VARIABLE_POINTER_TRACK Variable
;
596 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
597 return EFI_INVALID_PARAMETER
;
600 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
);
602 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
606 if (VariableName
[0] != 0) {
608 // If variable name is not NULL, get next variable
610 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
615 // The order we find variable is: 1). NonVolatile; 2). Volatile
616 // If both volatile and non-volatile variable store are parsed,
619 if (Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
) {
620 if (Variable
.Type
== Volatile
) {
622 // Since we met the end of Volatile storage, we have parsed all the stores.
624 return EFI_NOT_FOUND
;
628 // End of NonVolatile, continue to parse Volatile
630 Variable
.Type
= Volatile
;
631 Variable
.StartPtr
= (VARIABLE_HEADER
*) ((VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[Volatile
] + 1);
632 Variable
.EndPtr
= (VARIABLE_HEADER
*) GetEndPointer ((VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[Volatile
]);
634 Variable
.CurrPtr
= Variable
.StartPtr
;
635 if (!IsValidVariableHeader (Variable
.CurrPtr
)) {
642 if (IsValidVariableHeader (Variable
.CurrPtr
) &&
643 ((Variable
.CurrPtr
->State
== VAR_ADDED
) ||
644 (Variable
.CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
)))) {
645 if (!EfiAtRuntime () || (Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
646 VarNameSize
= Variable
.CurrPtr
->NameSize
;
647 if (VarNameSize
<= *VariableNameSize
) {
650 GET_VARIABLE_NAME_PTR (Variable
.CurrPtr
),
655 &Variable
.CurrPtr
->VendorGuid
,
658 Status
= EFI_SUCCESS
;
660 Status
= EFI_BUFFER_TOO_SMALL
;
663 *VariableNameSize
= VarNameSize
;
668 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
675 IN CHAR16
*VariableName
,
676 IN EFI_GUID
*VendorGuid
,
677 IN UINT32 Attributes
,
685 This code sets variable in storage blocks (Volatile or Non-Volatile)
689 VariableName Name of Variable to be found
690 VendorGuid Variable vendor GUID
691 Attributes Attribute value of the variable found
692 DataSize Size of Data found. If size is less than the
693 data, this value contains the required size.
698 EFI_INVALID_PARAMETER - Invalid parameter
699 EFI_SUCCESS - Set successfully
700 EFI_OUT_OF_RESOURCES - Resource not enough to set variable
701 EFI_NOT_FOUND - Not found
702 EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure
703 EFI_WRITE_PROTECTED - Variable is read-only
707 VARIABLE_POINTER_TRACK Variable
;
709 VARIABLE_HEADER
*NextVariable
;
716 VARIABLE_STORAGE_TYPE StorageType
;
721 // Check input parameters
724 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
725 return EFI_INVALID_PARAMETER
;
729 // Make sure if runtime bit is set, boot service bit is set also
731 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
732 return EFI_INVALID_PARAMETER
;
736 // The size of the VariableName, including the Unicode Null in bytes plus
737 // the DataSize is limited to maximum size of FixedPcdGet32(PcdMaxHardwareErrorVariableSize)
738 // bytes for HwErrRec, and FixedPcdGet32(PcdMaxVariableSize) bytes for the others.
740 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
741 if ((DataSize
> FixedPcdGet32(PcdMaxHardwareErrorVariableSize
)) ||
742 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> FixedPcdGet32(PcdMaxHardwareErrorVariableSize
))) {
743 return EFI_INVALID_PARAMETER
;
746 if ((DataSize
> FixedPcdGet32(PcdMaxVariableSize
)) ||
747 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> FixedPcdGet32(PcdMaxVariableSize
))) {
748 return EFI_INVALID_PARAMETER
;
753 // Check whether the input variable is already existed
756 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
);
758 if (Status
== EFI_SUCCESS
&& Variable
.CurrPtr
!= NULL
) {
760 // Update/Delete existing variable
763 if (EfiAtRuntime ()) {
765 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
766 // the volatile is ReadOnly, and SetVariable should be aborted and
767 // return EFI_WRITE_PROTECTED.
769 if (Variable
.Type
== Volatile
) {
770 return EFI_WRITE_PROTECTED
;
773 // Only variable have NV attribute can be updated/deleted in Runtime
775 if (!(Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
)) {
776 return EFI_INVALID_PARAMETER
;
781 // Setting a data variable with no access, or zero DataSize attributes
782 // specified causes it to be deleted.
784 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
786 // Found this variable in storage
788 State
= Variable
.CurrPtr
->State
;
789 State
&= VAR_DELETED
;
791 Status
= mGlobal
->VariableStore
[Variable
.Type
]->Write (
792 mGlobal
->VariableStore
[Variable
.Type
],
793 VARIABLE_MEMBER_OFFSET (State
, (UINTN
) Variable
.CurrPtr
- (UINTN
) Variable
.StartPtr
),
794 sizeof (Variable
.CurrPtr
->State
),
798 // NOTE: Write operation at least can write data to memory cache
799 // Discard file writing failure here.
805 // Found this variable in storage
806 // If the variable is marked valid and the same data has been passed in
807 // then return to the caller immediately.
809 if ((Variable
.CurrPtr
->DataSize
== DataSize
) &&
810 (CompareMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), DataSize
) == 0)
813 } else if ((Variable
.CurrPtr
->State
== VAR_ADDED
) ||
814 (Variable
.CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
816 // Mark the old variable as in delete transition
818 State
= Variable
.CurrPtr
->State
;
819 State
&= VAR_IN_DELETED_TRANSITION
;
821 Status
= mGlobal
->VariableStore
[Variable
.Type
]->Write (
822 mGlobal
->VariableStore
[Variable
.Type
],
823 VARIABLE_MEMBER_OFFSET (State
, (UINTN
) Variable
.CurrPtr
- (UINTN
) Variable
.StartPtr
),
824 sizeof (Variable
.CurrPtr
->State
),
828 // NOTE: Write operation at least can write data to memory cache
829 // Discard file writing failure here.
832 } else if (Status
== EFI_NOT_FOUND
) {
834 // Create a new variable
838 // Make sure we are trying to create a new variable.
839 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
841 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
842 return EFI_NOT_FOUND
;
845 // Only variable have NV|RT attribute can be created in Runtime
847 if (EfiAtRuntime () &&
848 (!(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) || !(Attributes
& EFI_VARIABLE_NON_VOLATILE
))) {
849 return EFI_INVALID_PARAMETER
;
854 // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().
860 // Function part - create a new variable and copy the data.
861 // Both update a variable and create a variable will come here.
862 // We can firstly write all the data in memory, then write them to file
863 // This can reduce the times of write operation
866 NextVariable
= (VARIABLE_HEADER
*) mGlobal
->Scratch
;
868 NextVariable
->StartId
= VARIABLE_DATA
;
869 NextVariable
->Attributes
= Attributes
;
870 NextVariable
->State
= VAR_ADDED
;
871 NextVariable
->Reserved
= 0;
872 VarNameOffset
= sizeof (VARIABLE_HEADER
);
873 VarNameSize
= StrSize (VariableName
);
875 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
879 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
881 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
885 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
887 // There will be pad bytes after Data, the NextVariable->NameSize and
888 // NextVariable->DataSize should not include pad size so that variable
889 // service can get actual size in GetVariable
891 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
892 NextVariable
->DataSize
= (UINT32
)DataSize
;
895 // The actual size of the variable that stores in storage should
897 // VarDataOffset: offset from begin of current variable header
899 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
901 StorageType
= (Attributes
& EFI_VARIABLE_NON_VOLATILE
) ? NonVolatile
: Volatile
;
903 if ((UINT32
) (VarSize
+ mGlobal
->LastVariableOffset
[StorageType
]) >
904 ((VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[StorageType
])->Size
906 if ((StorageType
== NonVolatile
) && EfiAtRuntime ()) {
907 return EFI_OUT_OF_RESOURCES
;
910 // Perform garbage collection & reclaim operation
912 Status
= Reclaim (StorageType
, Variable
.CurrPtr
);
913 if (EFI_ERROR (Status
)) {
916 // we cannot restore to original state, fetal error, report to user
918 DEBUG ((EFI_D_ERROR
, "FSVariable: Recalim error (fetal error) - %r\n", Status
));
922 // If still no enough space, return out of resources
924 if ((UINT32
) (VarSize
+ mGlobal
->LastVariableOffset
[StorageType
]) >
925 ((VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[StorageType
])->Size
927 return EFI_OUT_OF_RESOURCES
;
932 Status
= mGlobal
->VariableStore
[StorageType
]->Write (
933 mGlobal
->VariableStore
[StorageType
],
934 mGlobal
->LastVariableOffset
[StorageType
],
939 // NOTE: Write operation at least can write data to memory cache
940 // Discard file writing failure here.
942 mGlobal
->LastVariableOffset
[StorageType
] += VarSize
;
945 // Mark the old variable as deleted
947 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
.CurrPtr
!= NULL
) {
948 State
= Variable
.CurrPtr
->State
;
949 State
&= VAR_DELETED
;
951 Status
= mGlobal
->VariableStore
[StorageType
]->Write (
952 mGlobal
->VariableStore
[StorageType
],
953 VARIABLE_MEMBER_OFFSET (State
, (UINTN
) Variable
.CurrPtr
- (UINTN
) Variable
.StartPtr
),
954 sizeof (Variable
.CurrPtr
->State
),
958 // NOTE: Write operation at least can write data to memory cache
959 // Discard file writing failure here.
969 IN UINT32 Attributes
,
970 OUT UINT64
*MaximumVariableStorageSize
,
971 OUT UINT64
*RemainingVariableStorageSize
,
972 OUT UINT64
*MaximumVariableSize
978 This code returns information about the EFI variables.
982 Attributes Attributes bitmask to specify the type of variables
983 on which to return information.
984 MaximumVariableStorageSize Pointer to the maximum size of the storage space available
985 for the EFI variables associated with the attributes specified.
986 RemainingVariableStorageSize Pointer to the remaining size of the storage space available
987 for the EFI variables associated with the attributes specified.
988 MaximumVariableSize Pointer to the maximum size of the individual EFI variables
989 associated with the attributes specified.
994 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.
995 EFI_SUCCESS - Query successfully.
996 EFI_UNSUPPORTED - The attribute is not supported on this platform.
1000 VARIABLE_HEADER
*Variable
;
1001 VARIABLE_HEADER
*NextVariable
;
1002 UINT64 VariableSize
;
1003 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1005 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
1006 return EFI_INVALID_PARAMETER
;
1009 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
1011 // Make sure the Attributes combination is supported by the platform.
1013 return EFI_UNSUPPORTED
;
1015 else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1017 // Make sure if runtime bit is set, boot service bit is set also.
1019 return EFI_INVALID_PARAMETER
;
1020 } else if (EfiAtRuntime () && !(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
1022 // Make sure RT Attribute is set if we are in Runtime phase.
1024 return EFI_INVALID_PARAMETER
;
1027 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[
1028 (Attributes
& EFI_VARIABLE_NON_VOLATILE
) ? NonVolatile
: Volatile
1031 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1032 // with the storage size (excluding the storage header size).
1034 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1035 *RemainingVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1038 // Let *MaximumVariableSize be FixedPcdGet32(PcdMaxVariableSize) with the exception of the variable header size.
1040 *MaximumVariableSize
= FixedPcdGet32(PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
1043 // Harware error record variable needs larger size.
1045 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1046 *MaximumVariableSize
= FixedPcdGet32(PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
1050 // Point to the starting address of the variables.
1052 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
1055 // Now walk through the related variable store.
1057 while (IsValidVariableHeader (Variable
) && (Variable
< GetEndPointer (VariableStoreHeader
))) {
1058 NextVariable
= GetNextVariablePtr (Variable
);
1059 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
1061 if (EfiAtRuntime ()) {
1063 // we don't take the state of the variables in mind
1064 // when calculating RemainingVariableStorageSize,
1065 // since the space occupied by variables not marked with
1066 // VAR_ADDED is not allowed to be reclaimed in Runtime.
1068 *RemainingVariableStorageSize
-= VariableSize
;
1071 // Only care about Variables with State VAR_ADDED,because
1072 // the space not marked as VAR_ADDED is reclaimable now.
1074 if ((Variable
->State
== VAR_ADDED
) ||
1075 (Variable
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1076 *RemainingVariableStorageSize
-= VariableSize
;
1081 // Go to the next one
1083 Variable
= NextVariable
;
1086 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
1087 *MaximumVariableSize
= 0;
1088 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
1089 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
1097 VariableServiceInitialize (
1098 IN EFI_HANDLE ImageHandle
,
1099 IN EFI_SYSTEM_TABLE
*SystemTable
1103 Routine Description:
1104 This function does initialization for variable services
1108 ImageHandle - The firmware allocated handle for the EFI image.
1109 SystemTable - A pointer to the EFI System Table.
1115 EFI_NOT_FOUND - Variable store area not found.
1116 EFI_SUCCESS - Variable services successfully initialized.
1121 EFI_HANDLE NewHandle
;
1123 EFI_PEI_HOB_POINTERS GuidHob
;
1124 VARIABLE_HEADER
*NextVariable
;
1125 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1126 EFI_FLASH_MAP_FS_ENTRY_DATA
*FlashMapEntryData
;
1127 EFI_FLASH_SUBAREA_ENTRY VariableStoreEntry
;
1130 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
1132 Status
= gBS
->AllocatePool (
1133 EfiRuntimeServicesData
,
1134 (UINTN
) sizeof (VARIABLE_GLOBAL
),
1137 if (EFI_ERROR (Status
)) {
1141 GuidHob
.Raw
= GetHobList ();
1142 FlashMapEntryData
= NULL
;
1143 while ((GuidHob
.Raw
= GetNextGuidHob (&gEfiFlashMapHobGuid
, GuidHob
.Raw
)) != NULL
) {
1144 FlashMapEntryData
= (EFI_FLASH_MAP_FS_ENTRY_DATA
*) GET_GUID_HOB_DATA (GuidHob
.Guid
);
1145 if (FlashMapEntryData
->AreaType
== EFI_FLASH_AREA_EFI_VARIABLES
) {
1148 GuidHob
.Raw
= GET_NEXT_HOB (GuidHob
);
1151 if (FlashMapEntryData
== NULL
) {
1152 DEBUG ((EFI_D_ERROR
, "FSVariable: Could not find flash area for variable!\n"));
1153 Status
= EFI_NOT_FOUND
;
1158 (VOID
*)&VariableStoreEntry
,
1159 (VOID
*)&FlashMapEntryData
->Entries
[0],
1160 sizeof(EFI_FLASH_SUBAREA_ENTRY
)
1164 // Mark the variable storage region of the FLASH as RUNTIME
1166 BaseAddress
= VariableStoreEntry
.Base
& (~EFI_PAGE_MASK
);
1167 Length
= VariableStoreEntry
.Length
+ (VariableStoreEntry
.Base
- BaseAddress
);
1168 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
1169 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
1170 if (EFI_ERROR (Status
)) {
1171 Status
= EFI_UNSUPPORTED
;
1174 Status
= gDS
->SetMemorySpaceAttributes (
1177 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
1179 if (EFI_ERROR (Status
)) {
1180 Status
= EFI_UNSUPPORTED
;
1184 Status
= FileStorageConstructor (
1185 &mGlobal
->VariableStore
[NonVolatile
],
1186 &mGlobal
->GoVirtualChildEvent
[NonVolatile
],
1187 VariableStoreEntry
.Base
,
1188 (UINT32
) VariableStoreEntry
.Length
,
1189 FlashMapEntryData
->VolumeId
,
1190 FlashMapEntryData
->FilePath
1192 ASSERT_EFI_ERROR (Status
);
1197 Status
= MemStorageConstructor (
1198 &mGlobal
->VariableStore
[Volatile
],
1199 &mGlobal
->GoVirtualChildEvent
[Volatile
],
1200 VOLATILE_VARIABLE_STORE_SIZE
1202 ASSERT_EFI_ERROR (Status
);
1207 Status
= gBS
->AllocatePool (
1208 EfiRuntimeServicesData
,
1209 VARIABLE_SCRATCH_SIZE
,
1212 ASSERT_EFI_ERROR (Status
);
1217 Dev
= DEV_FROM_THIS (mGlobal
->VariableStore
[NonVolatile
]);
1218 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) VAR_DATA_PTR (Dev
);
1219 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
1220 if (~VariableStoreHeader
->Size
== 0) {
1221 VariableStoreHeader
->Size
= (UINT32
) VariableStoreEntry
.Length
;
1225 // Calculate LastVariableOffset
1227 NextVariable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
1228 while (IsValidVariableHeader (NextVariable
)) {
1229 NextVariable
= GetNextVariablePtr (NextVariable
);
1231 mGlobal
->LastVariableOffset
[NonVolatile
] = (UINTN
) NextVariable
- (UINTN
) VariableStoreHeader
;
1232 mGlobal
->VariableBase
[NonVolatile
] = VariableStoreHeader
;
1235 // Reclaim if remaining space is too small
1237 if ((VariableStoreHeader
->Size
- mGlobal
->LastVariableOffset
[NonVolatile
]) < VARIABLE_RECLAIM_THRESHOLD
) {
1238 Status
= Reclaim (NonVolatile
, NULL
);
1239 if (EFI_ERROR (Status
)) {
1242 // we cannot restore to original state
1244 DEBUG ((EFI_D_ERROR
, "FSVariable: Recalim error (fetal error) - %r\n", Status
));
1245 ASSERT_EFI_ERROR (Status
);
1250 // 2. Volatile Storage
1252 Dev
= DEV_FROM_THIS (mGlobal
->VariableStore
[Volatile
]);
1253 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) VAR_DATA_PTR (Dev
);
1254 mGlobal
->VariableBase
[Volatile
] = VAR_DATA_PTR (Dev
);
1255 mGlobal
->LastVariableOffset
[Volatile
] = sizeof (VARIABLE_STORE_HEADER
);
1257 // init store_header & body in memory.
1259 mGlobal
->VariableStore
[Volatile
]->Erase (mGlobal
->VariableStore
[Volatile
]);
1260 mGlobal
->VariableStore
[Volatile
]->Write (
1261 mGlobal
->VariableStore
[Volatile
],
1263 sizeof (VARIABLE_STORE_HEADER
),
1264 &mStoreHeaderTemplate
1268 SystemTable
->RuntimeServices
->GetVariable
= GetVariable
;
1269 SystemTable
->RuntimeServices
->GetNextVariableName
= GetNextVariableName
;
1270 SystemTable
->RuntimeServices
->SetVariable
= SetVariable
;
1272 SystemTable
->RuntimeServices
->QueryVariableInfo
= QueryVariableInfo
;
1275 // Now install the Variable Runtime Architectural Protocol on a new handle
1278 Status
= gBS
->InstallMultipleProtocolInterfaces (
1280 &gEfiVariableArchProtocolGuid
,
1282 &gEfiVariableWriteArchProtocolGuid
,
1286 ASSERT_EFI_ERROR (Status
);
1291 // EfiShutdownRuntimeDriverLib ();
1299 OnVirtualAddressChangeFsv (
1306 for (Index
= 0; Index
< MaxType
; Index
++) {
1307 mGlobal
->GoVirtualChildEvent
[Index
] (Event
, mGlobal
->VariableStore
[Index
]);
1308 EfiConvertPointer (0, (VOID
**) &mGlobal
->VariableStore
[Index
]);
1309 EfiConvertPointer (0, &mGlobal
->VariableBase
[Index
]);
1311 EfiConvertPointer (0, &mGlobal
->Scratch
);
1312 EfiConvertPointer (0, (VOID
**) &mGlobal
);