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
) > MAX_VARIABLE_SIZE
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 (
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
);
671 return EFI_NOT_FOUND
;
677 IN CHAR16
*VariableName
,
678 IN EFI_GUID
*VendorGuid
,
679 IN UINT32 Attributes
,
687 This code sets variable in storage blocks (Volatile or Non-Volatile)
691 VariableName Name of Variable to be found
692 VendorGuid Variable vendor GUID
693 Attributes Attribute value of the variable found
694 DataSize Size of Data found. If size is less than the
695 data, this value contains the required size.
700 EFI_INVALID_PARAMETER - Invalid parameter
701 EFI_SUCCESS - Set successfully
702 EFI_OUT_OF_RESOURCES - Resource not enough to set variable
703 EFI_NOT_FOUND - Not found
704 EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure
705 EFI_WRITE_PROTECTED - Variable is read-only
709 VARIABLE_POINTER_TRACK Variable
;
711 VARIABLE_HEADER
*NextVariable
;
718 VARIABLE_STORAGE_TYPE StorageType
;
723 // Check input parameters
726 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
727 return EFI_INVALID_PARAMETER
;
731 // Make sure if runtime bit is set, boot service bit is set also
733 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
734 return EFI_INVALID_PARAMETER
;
738 // The size of the VariableName, including the Unicode Null in bytes plus
739 // the DataSize is limited to maximum size of MAX_HARDWARE_ERROR_VARIABLE_SIZE (32K)
740 // bytes for HwErrRec, and MAX_VARIABLE_SIZE (1024) bytes for the others.
742 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
743 if ((DataSize
> MAX_HARDWARE_ERROR_VARIABLE_SIZE
) ||
744 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> MAX_HARDWARE_ERROR_VARIABLE_SIZE
)) {
745 return EFI_INVALID_PARAMETER
;
748 if ((DataSize
> MAX_VARIABLE_SIZE
) ||
749 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> MAX_VARIABLE_SIZE
)) {
750 return EFI_INVALID_PARAMETER
;
755 // Check whether the input variable is already existed
758 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
);
760 if (Status
== EFI_SUCCESS
&& Variable
.CurrPtr
!= NULL
) {
762 // Update/Delete existing variable
765 if (EfiAtRuntime ()) {
767 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
768 // the volatile is ReadOnly, and SetVariable should be aborted and
769 // return EFI_WRITE_PROTECTED.
771 if (Variable
.Type
== Volatile
) {
772 return EFI_WRITE_PROTECTED
;
775 // Only variable have NV attribute can be updated/deleted in Runtime
777 if (!(Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
)) {
778 return EFI_INVALID_PARAMETER
;
783 // Setting a data variable with no access, or zero DataSize attributes
784 // specified causes it to be deleted.
786 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
788 // Found this variable in storage
790 State
= Variable
.CurrPtr
->State
;
791 State
&= VAR_DELETED
;
793 Status
= mGlobal
->VariableStore
[Variable
.Type
]->Write (
794 mGlobal
->VariableStore
[Variable
.Type
],
795 VARIABLE_MEMBER_OFFSET (State
, (UINTN
) Variable
.CurrPtr
- (UINTN
) Variable
.StartPtr
),
796 sizeof (Variable
.CurrPtr
->State
),
800 // NOTE: Write operation at least can write data to memory cache
801 // Discard file writing failure here.
807 // Found this variable in storage
808 // If the variable is marked valid and the same data has been passed in
809 // then return to the caller immediately.
811 if ((Variable
.CurrPtr
->DataSize
== DataSize
) &&
812 (CompareMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), DataSize
) == 0)
815 } else if ((Variable
.CurrPtr
->State
== VAR_ADDED
) ||
816 (Variable
.CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
818 // Mark the old variable as in delete transition
820 State
= Variable
.CurrPtr
->State
;
821 State
&= VAR_IN_DELETED_TRANSITION
;
823 Status
= mGlobal
->VariableStore
[Variable
.Type
]->Write (
824 mGlobal
->VariableStore
[Variable
.Type
],
825 VARIABLE_MEMBER_OFFSET (State
, (UINTN
) Variable
.CurrPtr
- (UINTN
) Variable
.StartPtr
),
826 sizeof (Variable
.CurrPtr
->State
),
830 // NOTE: Write operation at least can write data to memory cache
831 // Discard file writing failure here.
834 } else if (Status
== EFI_NOT_FOUND
) {
836 // Create a new variable
840 // Make sure we are trying to create a new variable.
841 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
843 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
844 return EFI_NOT_FOUND
;
847 // Only variable have NV|RT attribute can be created in Runtime
849 if (EfiAtRuntime () &&
850 (!(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) || !(Attributes
& EFI_VARIABLE_NON_VOLATILE
))) {
851 return EFI_INVALID_PARAMETER
;
856 // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().
862 // Function part - create a new variable and copy the data.
863 // Both update a variable and create a variable will come here.
864 // We can firstly write all the data in memory, then write them to file
865 // This can reduce the times of write operation
868 NextVariable
= (VARIABLE_HEADER
*) mGlobal
->Scratch
;
870 NextVariable
->StartId
= VARIABLE_DATA
;
871 NextVariable
->Attributes
= Attributes
;
872 NextVariable
->State
= VAR_ADDED
;
873 NextVariable
->Reserved
= 0;
874 VarNameOffset
= sizeof (VARIABLE_HEADER
);
875 VarNameSize
= StrSize (VariableName
);
877 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
881 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
883 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
887 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
889 // There will be pad bytes after Data, the NextVariable->NameSize and
890 // NextVariable->DataSize should not include pad size so that variable
891 // service can get actual size in GetVariable
893 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
894 NextVariable
->DataSize
= (UINT32
)DataSize
;
897 // The actual size of the variable that stores in storage should
899 // VarDataOffset: offset from begin of current variable header
901 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
903 StorageType
= (Attributes
& EFI_VARIABLE_NON_VOLATILE
) ? NonVolatile
: Volatile
;
905 if ((UINT32
) (VarSize
+ mGlobal
->LastVariableOffset
[StorageType
]) >
906 ((VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[StorageType
])->Size
908 if ((StorageType
== NonVolatile
) && EfiAtRuntime ()) {
909 return EFI_OUT_OF_RESOURCES
;
912 // Perform garbage collection & reclaim operation
914 Status
= Reclaim (StorageType
, Variable
.CurrPtr
);
915 if (EFI_ERROR (Status
)) {
918 // we cannot restore to original state, fetal error, report to user
920 DEBUG ((EFI_D_ERROR
, "FSVariable: Recalim error (fetal error) - %r\n", Status
));
924 // If still no enough space, return out of resources
926 if ((UINT32
) (VarSize
+ mGlobal
->LastVariableOffset
[StorageType
]) >
927 ((VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[StorageType
])->Size
929 return EFI_OUT_OF_RESOURCES
;
934 Status
= mGlobal
->VariableStore
[StorageType
]->Write (
935 mGlobal
->VariableStore
[StorageType
],
936 mGlobal
->LastVariableOffset
[StorageType
],
941 // NOTE: Write operation at least can write data to memory cache
942 // Discard file writing failure here.
944 mGlobal
->LastVariableOffset
[StorageType
] += VarSize
;
947 // Mark the old variable as deleted
949 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
.CurrPtr
!= NULL
) {
950 State
= Variable
.CurrPtr
->State
;
951 State
&= VAR_DELETED
;
953 Status
= mGlobal
->VariableStore
[StorageType
]->Write (
954 mGlobal
->VariableStore
[StorageType
],
955 VARIABLE_MEMBER_OFFSET (State
, (UINTN
) Variable
.CurrPtr
- (UINTN
) Variable
.StartPtr
),
956 sizeof (Variable
.CurrPtr
->State
),
960 // NOTE: Write operation at least can write data to memory cache
961 // Discard file writing failure here.
971 IN UINT32 Attributes
,
972 OUT UINT64
*MaximumVariableStorageSize
,
973 OUT UINT64
*RemainingVariableStorageSize
,
974 OUT UINT64
*MaximumVariableSize
980 This code returns information about the EFI variables.
984 Attributes Attributes bitmask to specify the type of variables
985 on which to return information.
986 MaximumVariableStorageSize Pointer to the maximum size of the storage space available
987 for the EFI variables associated with the attributes specified.
988 RemainingVariableStorageSize Pointer to the remaining size of the storage space available
989 for the EFI variables associated with the attributes specified.
990 MaximumVariableSize Pointer to the maximum size of the individual EFI variables
991 associated with the attributes specified.
996 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.
997 EFI_SUCCESS - Query successfully.
998 EFI_UNSUPPORTED - The attribute is not supported on this platform.
1002 VARIABLE_HEADER
*Variable
;
1003 VARIABLE_HEADER
*NextVariable
;
1004 UINT64 VariableSize
;
1005 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1007 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
1008 return EFI_INVALID_PARAMETER
;
1011 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
1013 // Make sure the Attributes combination is supported by the platform.
1015 return EFI_UNSUPPORTED
;
1017 else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1019 // Make sure if runtime bit is set, boot service bit is set also.
1021 return EFI_INVALID_PARAMETER
;
1022 } else if (EfiAtRuntime () && !(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
1024 // Make sure RT Attribute is set if we are in Runtime phase.
1026 return EFI_INVALID_PARAMETER
;
1029 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) mGlobal
->VariableBase
[
1030 (Attributes
& EFI_VARIABLE_NON_VOLATILE
) ? NonVolatile
: Volatile
1033 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1034 // with the storage size (excluding the storage header size).
1036 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1037 *RemainingVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1040 // Let *MaximumVariableSize be MAX_VARIABLE_SIZE with the exception of the variable header size.
1042 *MaximumVariableSize
= MAX_VARIABLE_SIZE
- sizeof (VARIABLE_HEADER
);
1045 // Harware error record variable needs larger size.
1047 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1048 *MaximumVariableSize
= MAX_HARDWARE_ERROR_VARIABLE_SIZE
- sizeof (VARIABLE_HEADER
);
1052 // Point to the starting address of the variables.
1054 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
1057 // Now walk through the related variable store.
1059 while (IsValidVariableHeader (Variable
) && (Variable
< GetEndPointer (VariableStoreHeader
))) {
1060 NextVariable
= GetNextVariablePtr (Variable
);
1061 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
1063 if (EfiAtRuntime ()) {
1065 // we don't take the state of the variables in mind
1066 // when calculating RemainingVariableStorageSize,
1067 // since the space occupied by variables not marked with
1068 // VAR_ADDED is not allowed to be reclaimed in Runtime.
1070 *RemainingVariableStorageSize
-= VariableSize
;
1073 // Only care about Variables with State VAR_ADDED,because
1074 // the space not marked as VAR_ADDED is reclaimable now.
1076 if ((Variable
->State
== VAR_ADDED
) ||
1077 (Variable
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1078 *RemainingVariableStorageSize
-= VariableSize
;
1083 // Go to the next one
1085 Variable
= NextVariable
;
1088 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
1089 *MaximumVariableSize
= 0;
1090 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
1091 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
1099 VariableServiceInitialize (
1100 IN EFI_HANDLE ImageHandle
,
1101 IN EFI_SYSTEM_TABLE
*SystemTable
1105 Routine Description:
1106 This function does initialization for variable services
1110 ImageHandle - The firmware allocated handle for the EFI image.
1111 SystemTable - A pointer to the EFI System Table.
1117 EFI_NOT_FOUND - Variable store area not found.
1118 EFI_SUCCESS - Variable services successfully initialized.
1123 EFI_HANDLE NewHandle
;
1125 EFI_PEI_HOB_POINTERS GuidHob
;
1126 VARIABLE_HEADER
*NextVariable
;
1127 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1128 EFI_FLASH_MAP_FS_ENTRY_DATA
*FlashMapEntryData
;
1129 EFI_FLASH_SUBAREA_ENTRY VariableStoreEntry
;
1132 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
1134 Status
= gBS
->AllocatePool (
1135 EfiRuntimeServicesData
,
1136 (UINTN
) sizeof (VARIABLE_GLOBAL
),
1139 if (EFI_ERROR (Status
)) {
1143 GuidHob
.Raw
= GetHobList ();
1144 FlashMapEntryData
= NULL
;
1145 while ((GuidHob
.Raw
= GetNextGuidHob (&gEfiFlashMapHobGuid
, GuidHob
.Raw
)) != NULL
) {
1146 FlashMapEntryData
= (EFI_FLASH_MAP_FS_ENTRY_DATA
*) GET_GUID_HOB_DATA (GuidHob
.Guid
);
1147 if (FlashMapEntryData
->AreaType
== EFI_FLASH_AREA_EFI_VARIABLES
) {
1150 GuidHob
.Raw
= GET_NEXT_HOB (GuidHob
);
1153 if (FlashMapEntryData
== NULL
) {
1154 DEBUG ((EFI_D_ERROR
, "FSVariable: Could not find flash area for variable!\n"));
1155 Status
= EFI_NOT_FOUND
;
1160 (VOID
*)&VariableStoreEntry
,
1161 (VOID
*)&FlashMapEntryData
->Entries
[0],
1162 sizeof(EFI_FLASH_SUBAREA_ENTRY
)
1166 // Mark the variable storage region of the FLASH as RUNTIME
1168 BaseAddress
= VariableStoreEntry
.Base
& (~EFI_PAGE_MASK
);
1169 Length
= VariableStoreEntry
.Length
+ (VariableStoreEntry
.Base
- BaseAddress
);
1170 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
1171 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
1172 if (EFI_ERROR (Status
)) {
1173 Status
= EFI_UNSUPPORTED
;
1176 Status
= gDS
->SetMemorySpaceAttributes (
1179 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
1181 if (EFI_ERROR (Status
)) {
1182 Status
= EFI_UNSUPPORTED
;
1186 Status
= FileStorageConstructor (
1187 &mGlobal
->VariableStore
[NonVolatile
],
1188 &mGlobal
->GoVirtualChildEvent
[NonVolatile
],
1189 VariableStoreEntry
.Base
,
1190 (UINT32
) VariableStoreEntry
.Length
,
1191 FlashMapEntryData
->VolumeId
,
1192 FlashMapEntryData
->FilePath
1194 ASSERT_EFI_ERROR (Status
);
1199 Status
= MemStorageConstructor (
1200 &mGlobal
->VariableStore
[Volatile
],
1201 &mGlobal
->GoVirtualChildEvent
[Volatile
],
1202 VOLATILE_VARIABLE_STORE_SIZE
1204 ASSERT_EFI_ERROR (Status
);
1209 Status
= gBS
->AllocatePool (
1210 EfiRuntimeServicesData
,
1211 VARIABLE_SCRATCH_SIZE
,
1214 ASSERT_EFI_ERROR (Status
);
1219 Dev
= DEV_FROM_THIS (mGlobal
->VariableStore
[NonVolatile
]);
1220 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) VAR_DATA_PTR (Dev
);
1221 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
1222 if (~VariableStoreHeader
->Size
== 0) {
1223 VariableStoreHeader
->Size
= (UINT32
) VariableStoreEntry
.Length
;
1227 // Calculate LastVariableOffset
1229 NextVariable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
1230 while (IsValidVariableHeader (NextVariable
)) {
1231 NextVariable
= GetNextVariablePtr (NextVariable
);
1233 mGlobal
->LastVariableOffset
[NonVolatile
] = (UINTN
) NextVariable
- (UINTN
) VariableStoreHeader
;
1234 mGlobal
->VariableBase
[NonVolatile
] = VariableStoreHeader
;
1237 // Reclaim if remaining space is too small
1239 if ((VariableStoreHeader
->Size
- mGlobal
->LastVariableOffset
[NonVolatile
]) < VARIABLE_RECLAIM_THRESHOLD
) {
1240 Status
= Reclaim (NonVolatile
, NULL
);
1241 if (EFI_ERROR (Status
)) {
1244 // we cannot restore to original state
1246 DEBUG ((EFI_D_ERROR
, "FSVariable: Recalim error (fetal error) - %r\n", Status
));
1247 ASSERT_EFI_ERROR (Status
);
1252 // 2. Volatile Storage
1254 Dev
= DEV_FROM_THIS (mGlobal
->VariableStore
[Volatile
]);
1255 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) VAR_DATA_PTR (Dev
);
1256 mGlobal
->VariableBase
[Volatile
] = VAR_DATA_PTR (Dev
);
1257 mGlobal
->LastVariableOffset
[Volatile
] = sizeof (VARIABLE_STORE_HEADER
);
1259 // init store_header & body in memory.
1261 mGlobal
->VariableStore
[Volatile
]->Erase (mGlobal
->VariableStore
[Volatile
]);
1262 mGlobal
->VariableStore
[Volatile
]->Write (
1263 mGlobal
->VariableStore
[Volatile
],
1265 sizeof (VARIABLE_STORE_HEADER
),
1266 &mStoreHeaderTemplate
1270 SystemTable
->RuntimeServices
->GetVariable
= GetVariable
;
1271 SystemTable
->RuntimeServices
->GetNextVariableName
= GetNextVariableName
;
1272 SystemTable
->RuntimeServices
->SetVariable
= SetVariable
;
1274 SystemTable
->RuntimeServices
->QueryVariableInfo
= QueryVariableInfo
;
1277 // Now install the Variable Runtime Architectural Protocol on a new handle
1280 Status
= gBS
->InstallMultipleProtocolInterfaces (
1282 &gEfiVariableArchProtocolGuid
,
1284 &gEfiVariableWriteArchProtocolGuid
,
1288 ASSERT_EFI_ERROR (Status
);
1293 // EfiShutdownRuntimeDriverLib ();
1301 OnVirtualAddressChangeFsv (
1308 for (Index
= 0; Index
< MaxType
; Index
++) {
1309 mGlobal
->GoVirtualChildEvent
[Index
] (Event
, mGlobal
->VariableStore
[Index
]);
1310 EfiConvertPointer (0, &mGlobal
->VariableStore
[Index
]);
1311 EfiConvertPointer (0, &mGlobal
->VariableBase
[Index
]);
1313 EfiConvertPointer (0, &mGlobal
->Scratch
);
1314 EfiConvertPointer (0, &mGlobal
);