2 Framework PEIM to provide the Variable functionality
4 Copyright (c) 2006 - 2008 Intel Corporation. <BR>
5 All rights reserved. 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.
22 static EFI_PEI_READ_ONLY_VARIABLE_PPI mVariablePpi
= {
24 PeiGetNextVariableName
27 static EFI_PEI_READ_ONLY_VARIABLE2_PPI mVariable2Ppi
= {
29 PeiGetNextVariableName2
32 static EFI_PEI_PPI_DESCRIPTOR mPpiListVariable
[] = {
34 (EFI_PEI_PPI_DESCRIPTOR_PPI
),
35 &gEfiPeiReadOnlyVariable2PpiGuid
,
39 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
40 &gEfiPeiReadOnlyVariablePpiGuid
,
45 EFI_GUID mEfiVariableIndexTableGuid
= EFI_VARIABLE_INDEX_TABLE_GUID
;
48 Provide the functionality of the variable services.
50 @param FfsHeadher - The FFS file header
51 @param PeiServices - General purpose services available to every PEIM.
53 @return Status - EFI_SUCCESS if the interface could be successfully
59 PeimInitializeVariableServices (
60 IN EFI_FFS_FILE_HEADER
*FfsHeader
,
61 IN EFI_PEI_SERVICES
**PeiServices
65 // Publish the variable capability to other modules
67 return (**PeiServices
).InstallPpi ((CONST EFI_PEI_SERVICES
**) PeiServices
, &mPpiListVariable
[0]);
72 This code gets the pointer to the first variable memory pointer byte
74 @param VarStoreHeader Pointer to the Variable Store Header.
76 @return VARIABLE_HEADER* Pointer to last unavailable Variable Header
81 IN VARIABLE_STORE_HEADER
*VarStoreHeader
85 // The end of variable store
87 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
91 This code gets the pointer to the last variable memory pointer byte
93 @param VarStoreHeader Pointer to the Variable Store Header.
95 @return VARIABLE_HEADER* Pointer to last unavailable Variable Header
100 IN VARIABLE_STORE_HEADER
*VarStoreHeader
105 // The end of variable store
107 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
112 This code checks if variable header is valid or not.
114 @param Variable Pointer to the Variable Header.
116 @retval TRUE Variable header is valid.
117 @retval FALSE Variable header is not valid.
123 IsValidVariableHeader (
124 IN VARIABLE_HEADER
*Variable
127 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
135 This code gets the size of name of variable.
137 @param Variable Pointer to the Variable Header.
139 @return UINTN Size of variable in bytes
144 IN VARIABLE_HEADER
*Variable
147 if (Variable
->State
== (UINT8
) (-1) ||
148 Variable
->DataSize
== (UINT32
) -1 ||
149 Variable
->NameSize
== (UINT32
) -1 ||
150 Variable
->Attributes
== (UINT32
) -1) {
153 return (UINTN
) Variable
->NameSize
;
157 This code gets the size of name of variable.
159 @param Variable Pointer to the Variable Header.
161 @return UINTN Size of variable in bytes
166 IN VARIABLE_HEADER
*Variable
169 if (Variable
->State
== (UINT8
) -1 ||
170 Variable
->DataSize
== (UINT32
) -1 ||
171 Variable
->NameSize
== (UINT32
) -1 ||
172 Variable
->Attributes
== (UINT32
) -1) {
175 return (UINTN
) Variable
->DataSize
;
179 This code gets the pointer to the variable name.
181 @param Variable Pointer to the Variable Header.
183 @return CHAR16* Pointer to Variable Name
188 IN VARIABLE_HEADER
*Variable
193 return (CHAR16
*) (Variable
+ 1);
197 This code gets the pointer to the variable data.
199 @param Variable Pointer to the Variable Header.
201 @return UINT8* Pointer to Variable Data
206 IN VARIABLE_HEADER
*Variable
212 // Be careful about pad size for alignment
214 Value
= (UINTN
) GetVariableNamePtr (Variable
);
215 Value
+= NameSizeOfVariable (Variable
);
216 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
218 return (UINT8
*) Value
;
222 This code gets the pointer to the next variable header.
224 @param Variable Pointer to the Variable Header.
226 @return VARIABLE_HEADER* Pointer to next variable header.
231 IN VARIABLE_HEADER
*Variable
237 if (!IsValidVariableHeader (Variable
)) {
241 Value
= (UINTN
) GetVariableDataPtr (Variable
);
242 Value
+= DataSizeOfVariable (Variable
);
243 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
246 // Be careful about pad size for alignment
248 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
252 This code gets the pointer to the variable name.
254 @param VarStoreHeader Pointer to the Variable Store Header.
256 @retval EfiRaw Variable store is raw
257 @retval EfiValid Variable store is valid
258 @retval EfiInvalid Variable store is invalid
262 VARIABLE_STORE_STATUS
264 GetVariableStoreStatus (
265 IN VARIABLE_STORE_HEADER
*VarStoreHeader
269 if (VarStoreHeader
->Signature
== VARIABLE_STORE_SIGNATURE
&&
270 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
271 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
277 if (VarStoreHeader
->Signature
== 0xffffffff &&
278 VarStoreHeader
->Size
== 0xffffffff &&
279 VarStoreHeader
->Format
== 0xff &&
280 VarStoreHeader
->State
== 0xff
290 This function compares a variable with variable entries in database
292 @param Variable - Pointer to the variable in our database
293 @param VariableName - Name of the variable to compare to 'Variable'
294 @param VendorGuid - GUID of the variable to compare to 'Variable'
295 @param PtrTrack - Variable Track Pointer structure that contains
296 Variable Information.
298 @retval EFI_SUCCESS - Found match variable
299 @retval EFI_NOT_FOUND - Variable not found
304 CompareWithValidVariable (
305 IN VARIABLE_HEADER
*Variable
,
306 IN CONST CHAR16
*VariableName
,
307 IN CONST EFI_GUID
*VendorGuid
,
308 OUT VARIABLE_POINTER_TRACK
*PtrTrack
314 if (VariableName
[0] == 0) {
315 PtrTrack
->CurrPtr
= Variable
;
319 // Don't use CompareGuid function here for performance reasons.
320 // Instead we compare the GUID a UINT32 at a time and branch
321 // on the first failed comparison.
323 if ((((INT32
*) VendorGuid
)[0] == ((INT32
*) &Variable
->VendorGuid
)[0]) &&
324 (((INT32
*) VendorGuid
)[1] == ((INT32
*) &Variable
->VendorGuid
)[1]) &&
325 (((INT32
*) VendorGuid
)[2] == ((INT32
*) &Variable
->VendorGuid
)[2]) &&
326 (((INT32
*) VendorGuid
)[3] == ((INT32
*) &Variable
->VendorGuid
)[3])
328 ASSERT (NameSizeOfVariable (Variable
) != 0);
329 Point
= (VOID
*) GetVariableNamePtr (Variable
);
330 if (!CompareMem (VariableName
, Point
, NameSizeOfVariable (Variable
))) {
331 PtrTrack
->CurrPtr
= Variable
;
337 return EFI_NOT_FOUND
;
341 This code finds variable in storage blocks (Non-Volatile)
343 @param PeiServices - General purpose services available to every PEIM.
344 @param VariableName - Name of the variable to be found
345 @param VendorGuid - Vendor GUID to be found.
346 @param PtrTrack - Variable Track Pointer structure that contains
347 Variable Information.
349 @retval EFI_SUCCESS - Variable found successfully
350 @retval EFI_NOT_FOUND - Variable not found
351 @retval EFI_INVALID_PARAMETER - Invalid variable name
358 IN EFI_PEI_SERVICES
**PeiServices
,
359 IN CONST CHAR16
*VariableName
,
360 IN CONST EFI_GUID
*VendorGuid
,
361 OUT VARIABLE_POINTER_TRACK
*PtrTrack
365 EFI_HOB_GUID_TYPE
*GuidHob
;
366 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
367 VARIABLE_HEADER
*Variable
;
368 VARIABLE_HEADER
*MaxIndex
;
369 VARIABLE_INDEX_TABLE
*IndexTable
;
373 if (VariableName
!= 0 && VendorGuid
== NULL
) {
374 return EFI_INVALID_PARAMETER
;
377 // No Variable Address equals zero, so 0 as initial value is safe.
381 GuidHob
= GetFirstGuidHob (&mEfiVariableIndexTableGuid
);
382 if (GuidHob
== NULL
) {
383 IndexTable
= BuildGuidHob (&mEfiVariableIndexTableGuid
, sizeof (VARIABLE_INDEX_TABLE
));
384 IndexTable
->Length
= 0;
385 IndexTable
->StartPtr
= NULL
;
386 IndexTable
->EndPtr
= NULL
;
387 IndexTable
->GoneThrough
= 0;
389 IndexTable
= GET_GUID_HOB_DATA (GuidHob
);
390 for (Count
= 0; Count
< IndexTable
->Length
; Count
++)
392 MaxIndex
= GetVariableByIndex (IndexTable
, Count
);
394 if (CompareWithValidVariable (MaxIndex
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
395 PtrTrack
->StartPtr
= IndexTable
->StartPtr
;
396 PtrTrack
->EndPtr
= IndexTable
->EndPtr
;
402 if (IndexTable
->GoneThrough
) {
403 return EFI_NOT_FOUND
;
407 // If not found in HOB, then let's start from the MaxIndex we've found.
409 if (MaxIndex
!= NULL
) {
410 Variable
= GetNextVariablePtr (MaxIndex
);
412 if (IndexTable
->StartPtr
|| IndexTable
->EndPtr
) {
413 Variable
= IndexTable
->StartPtr
;
415 VariableBase
= (UINT8
*) (UINTN
) PcdGet32 (PcdFlashNvStorageVariableBase
);
416 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (VariableBase
+ \
417 ((EFI_FIRMWARE_VOLUME_HEADER
*) (VariableBase
)) -> HeaderLength
);
419 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
420 return EFI_UNSUPPORTED
;
423 if (~VariableStoreHeader
->Size
== 0) {
424 return EFI_NOT_FOUND
;
427 // Find the variable by walk through non-volatile variable store
429 IndexTable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
430 IndexTable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
433 // Start Pointers for the variable.
434 // Actual Data Pointer where data can be written.
436 Variable
= IndexTable
->StartPtr
;
440 // Find the variable by walk through non-volatile variable store
442 PtrTrack
->StartPtr
= IndexTable
->StartPtr
;
443 PtrTrack
->EndPtr
= IndexTable
->EndPtr
;
445 while (IsValidVariableHeader (Variable
) && (Variable
<= IndexTable
->EndPtr
)) {
446 if (Variable
->State
== VAR_ADDED
) {
448 // Record Variable in VariableIndex HOB
450 if (IndexTable
->Length
< VARIABLE_INDEX_TABLE_VOLUME
)
452 VariableIndexTableUpdate (IndexTable
, Variable
);
455 if (CompareWithValidVariable (Variable
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
460 Variable
= GetNextVariablePtr (Variable
);
463 // If gone through the VariableStore, that means we never find in Firmware any more.
465 if (IndexTable
->Length
< VARIABLE_INDEX_TABLE_VOLUME
) {
466 IndexTable
->GoneThrough
= 1;
469 PtrTrack
->CurrPtr
= NULL
;
471 return EFI_NOT_FOUND
;
475 Provide the read variable functionality of the variable services.
477 @param PeiServices - General purpose services available to every PEIM.
479 @param VariableName - The variable name
481 @param VendorGuid - The vendor's GUID
483 @param Attributes - Pointer to the attribute
485 @param DataSize - Size of data
487 @param Data - Pointer to data
489 @retval EFI_SUCCESS - The interface could be successfully installed
491 @retval EFI_NOT_FOUND - The variable could not be discovered
493 @retval EFI_BUFFER_TOO_SMALL - The caller buffer is not large enough
499 IN EFI_PEI_SERVICES
**PeiServices
,
500 IN CHAR16
*VariableName
,
501 IN EFI_GUID
* VendorGuid
,
502 OUT UINT32
*Attributes OPTIONAL
,
503 IN OUT UINTN
*DataSize
,
508 VARIABLE_POINTER_TRACK Variable
;
512 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
513 return EFI_INVALID_PARAMETER
;
516 // Find existing variable
518 Status
= FindVariable (PeiServices
, VariableName
, VendorGuid
, &Variable
);
520 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
526 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
527 if (*DataSize
>= VarDataSize
) {
529 // PO-TKW: Address one checking in this place
532 return EFI_INVALID_PARAMETER
;
535 (*PeiServices
)->CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
537 if (Attributes
!= NULL
) {
538 *Attributes
= Variable
.CurrPtr
->Attributes
;
541 *DataSize
= VarDataSize
;
544 *DataSize
= VarDataSize
;
545 return EFI_BUFFER_TOO_SMALL
;
550 Provide the read variable functionality of the variable services.
552 @param PeiServices - General purpose services available to every PEIM.
554 @param VariableName - The variable name
556 @param VendorGuid - The vendor's GUID
558 @param Attributes - Pointer to the attribute
560 @param DataSize - Size of data
562 @param Data - Pointer to data
564 @retval EFI_SUCCESS - The interface could be successfully installed
566 @retval EFI_NOT_FOUND - The variable could not be discovered
568 @retval EFI_BUFFER_TOO_SMALL - The caller buffer is not large enough
574 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
575 IN CONST CHAR16
*VariableName
,
576 IN CONST EFI_GUID
*VariableGuid
,
577 OUT UINT32
*Attributes
,
578 IN OUT UINTN
*DataSize
,
583 return PeiGetVariable (
584 GetPeiServicesTablePointer (),
585 (CHAR16
*)VariableName
,
586 (EFI_GUID
*)VariableGuid
,
594 Provide the get next variable functionality of the variable services.
596 @param PeiServices - General purpose services available to every PEIM.
597 @param VariabvleNameSize - The variable name's size.
598 @param VariableName - A pointer to the variable's name.
599 @param VendorGuid - A pointer to the EFI_GUID structure.
601 @param VariableNameSize - Size of the variable name
603 @param VariableName - The variable name
605 @param VendorGuid - The vendor's GUID
607 @retval EFI_SUCCESS - The interface could be successfully installed
609 @retval EFI_NOT_FOUND - The variable could not be discovered
614 PeiGetNextVariableName (
615 IN EFI_PEI_SERVICES
**PeiServices
,
616 IN OUT UINTN
*VariableNameSize
,
617 IN OUT CHAR16
*VariableName
,
618 IN OUT EFI_GUID
*VendorGuid
622 VARIABLE_POINTER_TRACK Variable
;
626 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
627 return EFI_INVALID_PARAMETER
;
630 Status
= FindVariable (PeiServices
, VariableName
, VendorGuid
, &Variable
);
632 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
636 if (VariableName
[0] != 0) {
638 // If variable name is not NULL, get next variable
640 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
643 while (!(Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
)) {
644 if (IsValidVariableHeader (Variable
.CurrPtr
)) {
645 if (Variable
.CurrPtr
->State
== VAR_ADDED
) {
646 ASSERT (NameSizeOfVariable (Variable
.CurrPtr
) != 0);
648 VarNameSize
= (UINTN
) NameSizeOfVariable (Variable
.CurrPtr
);
649 if (VarNameSize
<= *VariableNameSize
) {
650 (*PeiServices
)->CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
652 (*PeiServices
)->CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
654 Status
= EFI_SUCCESS
;
656 Status
= EFI_BUFFER_TOO_SMALL
;
659 *VariableNameSize
= VarNameSize
;
665 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
672 return EFI_NOT_FOUND
;
676 Provide the get next variable functionality of the variable services.
678 @param PeiServices - General purpose services available to every PEIM.
679 @param VariabvleNameSize - The variable name's size.
680 @param VariableName - A pointer to the variable's name.
681 @param VariableGuid - A pointer to the EFI_GUID structure.
683 @param VariableNameSize - Size of the variable name
685 @param VariableName - The variable name
687 @param VendorGuid - The vendor's GUID
690 @retval EFI_SUCCESS - The interface could be successfully installed
692 @retval EFI_NOT_FOUND - The variable could not be discovered
697 PeiGetNextVariableName2 (
698 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
699 IN OUT UINTN
*VariableNameSize
,
700 IN OUT CHAR16
*VariableName
,
701 IN OUT EFI_GUID
*VariableGuid
705 return PeiGetNextVariableName (
706 GetPeiServicesTablePointer (),