3 Copyright (c) 2006, 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.
25 // Don't use module globals after the SetVirtualAddress map is signaled
27 ESAL_VARIABLE_GLOBAL
*mVariableModuleGlobal
;
39 Determine the length of null terminated char16 array.
43 String Null-terminated CHAR16 array pointer.
47 UINT32 Number of bytes in the string, including the double NULL at the end;
59 while (0 != String
[Count
]) {
63 return (Count
* 2) + 2;
70 IN VARIABLE_HEADER
*Variable
76 This code gets the pointer to the variable data.
80 Variable Pointer to the Variable Header.
84 UINT8* Pointer to Variable Data
88 if (Variable
->StartId
!= VARIABLE_DATA
) {
92 // Be careful about pad size for alignment
94 return (UINT8
*) ((UINTN
) GET_VARIABLE_NAME_PTR (Variable
) + Variable
->NameSize
+ GET_PAD_SIZE (Variable
->NameSize
));
101 IN VARIABLE_HEADER
*Variable
107 This code gets the pointer to the next variable header.
111 Variable Pointer to the Variable Header.
115 VARIABLE_HEADER* Pointer to next variable header.
119 VARIABLE_HEADER
*VarHeader
;
121 if (Variable
->StartId
!= VARIABLE_DATA
) {
125 // Be careful about pad size for alignment
127 VarHeader
= (VARIABLE_HEADER
*) (GetVariableDataPtr (Variable
) + Variable
->DataSize
+ GET_PAD_SIZE (Variable
->DataSize
));
129 if (VarHeader
->StartId
!= VARIABLE_DATA
||
130 (sizeof (VARIABLE_HEADER
) + VarHeader
->DataSize
+ VarHeader
->NameSize
) > MAX_VARIABLE_SIZE
142 IN VARIABLE_STORE_HEADER
*VolHeader
148 This code gets the pointer to the last variable memory pointer byte
152 Variable Pointer to the Variable Header.
156 VARIABLE_HEADER* Pointer to last unavailable Variable Header
161 // The end of variable store
163 return (VARIABLE_HEADER
*) ((UINTN
) VolHeader
+ VolHeader
->Size
);
170 IN CHAR16
*VariableName
,
171 IN EFI_GUID
*VendorGuid
,
172 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
173 IN VARIABLE_GLOBAL
*Global
179 This code finds variable in storage blocks (Volatile or Non-Volatile)
183 VariableName Name of the variable to be found
184 VendorGuid Vendor GUID to be found.
185 PtrTrack Variable Track Pointer structure that contains
186 Variable Information.
187 Contains the pointer of Variable header.
188 Global VARIABLE_GLOBAL pointer
196 VARIABLE_HEADER
*Variable
[2];
197 VARIABLE_STORE_HEADER
*VariableStoreHeader
[2];
201 // 0: Non-Volatile, 1: Volatile
203 VariableStoreHeader
[0] = (VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->NonVolatileVariableBase
);
204 VariableStoreHeader
[1] = (VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->VolatileVariableBase
);
207 // Start Pointers for the variable.
208 // Actual Data Pointer where data can be written.
210 Variable
[0] = (VARIABLE_HEADER
*) (VariableStoreHeader
[0] + 1);
211 Variable
[1] = (VARIABLE_HEADER
*) (VariableStoreHeader
[1] + 1);
213 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
214 return EFI_INVALID_PARAMETER
;
217 // Find the variable by walk through non-volatile and volatile variable store
219 for (Index
= 0; Index
< 2; Index
++) {
220 PtrTrack
->StartPtr
= (VARIABLE_HEADER
*) (VariableStoreHeader
[Index
] + 1);
221 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
223 while ((Variable
[Index
] != NULL
) && (Variable
[Index
] <= GetEndPointer (VariableStoreHeader
[Index
]))) {
224 if (Variable
[Index
]->StartId
== VARIABLE_DATA
&& Variable
[Index
]->State
== VAR_ADDED
) {
225 if (!(EfiAtRuntime () && !(Variable
[Index
]->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
))) {
226 if (VariableName
[0] == 0) {
227 PtrTrack
->CurrPtr
= Variable
[Index
];
228 PtrTrack
->Volatile
= (BOOLEAN
) Index
;
231 if (CompareGuid (VendorGuid
, &Variable
[Index
]->VendorGuid
)) {
232 if (!CompareMem (VariableName
, GET_VARIABLE_NAME_PTR (Variable
[Index
]), ArrayLength (VariableName
))) {
233 PtrTrack
->CurrPtr
= Variable
[Index
];
234 PtrTrack
->Volatile
= (BOOLEAN
) Index
;
242 Variable
[Index
] = GetNextVariablePtr (Variable
[Index
]);
245 PtrTrack
->CurrPtr
= NULL
;
246 return EFI_NOT_FOUND
;
252 IN CHAR16
*VariableName
,
253 IN EFI_GUID
* VendorGuid
,
254 OUT UINT32
*Attributes OPTIONAL
,
255 IN OUT UINTN
*DataSize
,
257 IN VARIABLE_GLOBAL
* Global
,
264 This code finds variable in storage blocks (Volatile or Non-Volatile)
268 VariableName Name of Variable to be found
269 VendorGuid Variable vendor GUID
270 Attributes OPTIONAL Attribute value of the variable found
271 DataSize Size of Data found. If size is less than the
272 data, this value contains the required size.
274 Global Pointer to VARIABLE_GLOBAL structure
275 Instance Instance of the Firmware Volume.
283 VARIABLE_POINTER_TRACK Variable
;
287 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
288 return EFI_INVALID_PARAMETER
;
291 // Find existing variable
293 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, Global
);
295 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
301 VarDataSize
= Variable
.CurrPtr
->DataSize
;
302 if (*DataSize
>= VarDataSize
) {
304 return EFI_INVALID_PARAMETER
;
307 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
308 if (Attributes
!= NULL
) {
309 *Attributes
= Variable
.CurrPtr
->Attributes
;
312 *DataSize
= VarDataSize
;
315 *DataSize
= VarDataSize
;
316 return EFI_BUFFER_TOO_SMALL
;
322 GetNextVariableName (
323 IN OUT UINTN
*VariableNameSize
,
324 IN OUT CHAR16
*VariableName
,
325 IN OUT EFI_GUID
*VendorGuid
,
326 IN VARIABLE_GLOBAL
*Global
,
333 This code Finds the Next available variable
337 VariableNameSize Size of the variable
338 VariableName Pointer to variable name
339 VendorGuid Variable Vendor Guid
340 Global VARIABLE_GLOBAL structure pointer.
349 VARIABLE_POINTER_TRACK Variable
;
353 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
354 return EFI_INVALID_PARAMETER
;
357 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, Global
);
359 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
364 if (VariableName
[0] != 0) {
366 // If variable name is not NULL, get next variable
368 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
371 // If both volatile and non-volatile variable store are parsed,
374 if (Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
) {
375 Variable
.Volatile
= (BOOLEAN
) (Variable
.Volatile
^ ((BOOLEAN
) 0x1));
376 if (Variable
.Volatile
) {
377 Variable
.StartPtr
= (VARIABLE_HEADER
*) ((UINTN
) (Global
->VolatileVariableBase
+ sizeof (VARIABLE_STORE_HEADER
)));
378 Variable
.EndPtr
= (VARIABLE_HEADER
*) GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->VolatileVariableBase
));
383 Variable
.CurrPtr
= Variable
.StartPtr
;
384 if (Variable
.CurrPtr
->StartId
!= VARIABLE_DATA
) {
391 if (Variable
.CurrPtr
->StartId
== VARIABLE_DATA
&& Variable
.CurrPtr
->State
== VAR_ADDED
) {
392 if (!(EfiAtRuntime () && !(Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
))) {
393 VarNameSize
= Variable
.CurrPtr
->NameSize
;
394 if (VarNameSize
<= *VariableNameSize
) {
397 GET_VARIABLE_NAME_PTR (Variable
.CurrPtr
),
402 &Variable
.CurrPtr
->VendorGuid
,
405 Status
= EFI_SUCCESS
;
407 Status
= EFI_BUFFER_TOO_SMALL
;
410 *VariableNameSize
= VarNameSize
;
417 return EFI_NOT_FOUND
;
423 IN CHAR16
*VariableName
,
424 IN EFI_GUID
*VendorGuid
,
425 IN UINT32 Attributes
,
428 IN VARIABLE_GLOBAL
*Global
,
429 IN UINTN
*VolatileOffset
,
430 IN UINTN
*NonVolatileOffset
,
437 This code sets variable in storage blocks (Volatile or Non-Volatile)
441 VariableName Name of Variable to be found
442 VendorGuid Variable vendor GUID
443 Attributes Attribute value of the variable found
444 DataSize Size of Data found. If size is less than the
445 data, this value contains the required size.
447 Global Pointer to VARIABLE_GLOBAL structure
448 VolatileOffset The offset of last volatile variable
449 NonVolatileOffset The offset of last non-volatile variable
450 Instance Instance of the Firmware Volume.
458 VARIABLE_POINTER_TRACK Variable
;
460 VARIABLE_HEADER
*NextVariable
;
466 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
467 return EFI_INVALID_PARAMETER
;
470 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, Global
);
472 if (Status
== EFI_INVALID_PARAMETER
) {
474 } else if (!EFI_ERROR (Status
) && Variable
.Volatile
&& EfiAtRuntime()) {
476 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
477 // the volatile is ReadOnly, and SetVariable should be aborted and
478 // return EFI_WRITE_PROTECTED.
480 return EFI_WRITE_PROTECTED
;
481 } else if (sizeof (VARIABLE_HEADER
) + (ArrayLength (VariableName
) + DataSize
) > MAX_VARIABLE_SIZE
) {
483 // The size of the VariableName, including the Unicode Null in bytes plus
484 // the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes.
486 return EFI_INVALID_PARAMETER
;
487 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
490 // Make sure if runtime bit is set, boot service bit is set also
492 return EFI_INVALID_PARAMETER
;
493 } else if (EfiAtRuntime () && Attributes
&& !(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
495 // Runtime but Attribute is not Runtime
497 return EFI_INVALID_PARAMETER
;
498 } else if (EfiAtRuntime () && Attributes
&& !(Attributes
& EFI_VARIABLE_NON_VOLATILE
)) {
500 // Cannot set volatile variable in Runtime
502 return EFI_INVALID_PARAMETER
;
503 } else if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
505 // Setting a data variable with no access, or zero DataSize attributes
506 // specified causes it to be deleted.
508 if (!EFI_ERROR (Status
)) {
509 Variable
.CurrPtr
->State
&= VAR_DELETED
;
513 return EFI_NOT_FOUND
;
515 if (!EFI_ERROR (Status
)) {
517 // If the variable is marked valid and the same data has been passed in
518 // then return to the caller immediately.
520 if (Variable
.CurrPtr
->DataSize
== DataSize
&&
521 !CompareMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), DataSize
)
524 } else if (Variable
.CurrPtr
->State
== VAR_ADDED
) {
526 // Mark the old variable as in delete transition
528 Variable
.CurrPtr
->State
&= VAR_IN_DELETED_TRANSITION
;
532 // Create a new variable and copy the data.
534 VarNameOffset
= sizeof (VARIABLE_HEADER
);
535 VarNameSize
= ArrayLength (VariableName
);
536 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
537 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
539 if (Attributes
& EFI_VARIABLE_NON_VOLATILE
) {
540 if ((UINT32
) (VarSize
+*NonVolatileOffset
) >
541 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (Global
->NonVolatileVariableBase
)))->Size
543 return EFI_OUT_OF_RESOURCES
;
546 NextVariable
= (VARIABLE_HEADER
*) (UINT8
*) (*NonVolatileOffset
+ (UINTN
) Global
->NonVolatileVariableBase
);
547 *NonVolatileOffset
= *NonVolatileOffset
+ VarSize
;
549 if (EfiAtRuntime ()) {
550 return EFI_INVALID_PARAMETER
;
553 if ((UINT32
) (VarSize
+*VolatileOffset
) >
554 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (Global
->VolatileVariableBase
)))->Size
556 return EFI_OUT_OF_RESOURCES
;
559 NextVariable
= (VARIABLE_HEADER
*) (UINT8
*) (*VolatileOffset
+ (UINTN
) Global
->VolatileVariableBase
);
560 *VolatileOffset
= *VolatileOffset
+ VarSize
;
563 NextVariable
->StartId
= VARIABLE_DATA
;
564 NextVariable
->Attributes
= Attributes
;
565 NextVariable
->State
= VAR_ADDED
;
566 NextVariable
->Reserved
= 0;
569 // There will be pad bytes after Data, the NextVariable->NameSize and
570 // NextVariable->NameSize should not include pad size so that variable
571 // service can get actual size in GetVariable
573 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
574 NextVariable
->DataSize
= (UINT32
)DataSize
;
576 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
578 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
583 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
589 // Mark the old variable as deleted
591 if (!EFI_ERROR (Status
)) {
592 Variable
.CurrPtr
->State
&= VAR_DELETED
;
599 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)
603 IN UINT32 Attributes
,
604 OUT UINT64
*MaximumVariableStorageSize
,
605 OUT UINT64
*RemainingVariableStorageSize
,
606 OUT UINT64
*MaximumVariableSize
,
607 IN VARIABLE_GLOBAL
*Global
,
614 This code returns information about the EFI variables.
618 Attributes Attributes bitmask to specify the type of variables
619 on which to return information.
620 MaximumVariableStorageSize Pointer to the maximum size of the storage space available
621 for the EFI variables associated with the attributes specified.
622 RemainingVariableStorageSize Pointer to the remaining size of the storage space available
623 for the EFI variables associated with the attributes specified.
624 MaximumVariableSize Pointer to the maximum size of the individual EFI variables
625 associated with the attributes specified.
626 Global Pointer to VARIABLE_GLOBAL structure.
627 Instance Instance of the Firmware Volume.
632 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.
633 EFI_SUCCESS - Query successfully.
634 EFI_UNSUPPORTED - The attribute is not supported on this platform.
638 VARIABLE_HEADER
*Variable
;
639 VARIABLE_HEADER
*NextVariable
;
641 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
643 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
) {
644 return EFI_INVALID_PARAMETER
;
647 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
)) == 0) {
649 // Make sure the Attributes combination is supported by the platform.
651 return EFI_UNSUPPORTED
;
652 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
654 // Make sure if runtime bit is set, boot service bit is set also.
656 return EFI_INVALID_PARAMETER
;
657 } else if (EfiAtRuntime () && !(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
659 // Make sure RT Attribute is set if we are in Runtime phase.
661 return EFI_INVALID_PARAMETER
;
662 } else if (EfiAtRuntime () && Attributes
&& !(Attributes
& EFI_VARIABLE_NON_VOLATILE
)) {
664 // Cannot Query volatile variable in Runtime
666 return EFI_INVALID_PARAMETER
;
669 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
671 // Query is Volatile related.
673 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->VolatileVariableBase
);
676 // Query is Non-Volatile related.
678 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->NonVolatileVariableBase
);
682 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
683 // with the storage size (excluding the storage header size)
685 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
686 *RemainingVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
689 // Let *MaximumVariableSize be MAX_VARIABLE_SIZE
691 *MaximumVariableSize
= MAX_VARIABLE_SIZE
;
694 // Point to the starting address of the variables.
696 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
699 // Now walk through the related variable store.
701 while (Variable
< GetEndPointer (VariableStoreHeader
)) {
702 if (Variable
->StartId
!= VARIABLE_DATA
) {
706 NextVariable
= (VARIABLE_HEADER
*) (GetVariableDataPtr (Variable
) + Variable
->DataSize
+ GET_PAD_SIZE (Variable
->DataSize
));
707 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
709 if (Variable
->State
== VAR_ADDED
) {
710 *RemainingVariableStorageSize
-= VariableSize
;
714 // Go to the next one.
716 Variable
= NextVariable
;
726 InitializeVariableStore (
727 OUT EFI_PHYSICAL_ADDRESS
*VariableBase
,
728 OUT UINTN
*LastVariableOffset
733 This function initializes variable store
741 VARIABLE_STORE_HEADER
*VariableStore
;
744 // Allocate memory for volatile variable store
746 VariableStore
= (VARIABLE_STORE_HEADER
*) AllocateRuntimePool (
749 if (NULL
== VariableStore
) {
750 return EFI_OUT_OF_RESOURCES
;
753 SetMem (VariableStore
, VARIABLE_STORE_SIZE
, 0xff);
756 // Variable Specific Data
758 *VariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStore
;
759 *LastVariableOffset
= sizeof (VARIABLE_STORE_HEADER
);
761 VariableStore
->Signature
= VARIABLE_STORE_SIGNATURE
;
762 VariableStore
->Size
= VARIABLE_STORE_SIZE
;
763 VariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
764 VariableStore
->State
= VARIABLE_STORE_HEALTHY
;
765 VariableStore
->Reserved
= 0;
766 VariableStore
->Reserved1
= 0;
773 VariableCommonInitialize (
774 IN EFI_HANDLE ImageHandle
,
775 IN EFI_SYSTEM_TABLE
*SystemTable
780 This function does common initialization for variable services
791 // Allocate memory for mVariableModuleGlobal
793 mVariableModuleGlobal
= (ESAL_VARIABLE_GLOBAL
*) AllocateRuntimePool (
794 sizeof (ESAL_VARIABLE_GLOBAL
)
796 if (NULL
== mVariableModuleGlobal
) {
797 return EFI_OUT_OF_RESOURCES
;
800 // Intialize volatile variable store
802 Status
= InitializeVariableStore (
803 &mVariableModuleGlobal
->VariableBase
[Physical
].VolatileVariableBase
,
804 &mVariableModuleGlobal
->VolatileLastVariableOffset
807 if (EFI_ERROR (Status
)) {
811 // Intialize non volatile variable store
813 Status
= InitializeVariableStore (
814 &mVariableModuleGlobal
->VariableBase
[Physical
].NonVolatileVariableBase
,
815 &mVariableModuleGlobal
->NonVolatileLastVariableOffset