2 Implement ReadOnly Variable Services required by PEIM and install PEI
3 ReadOnly Varaiable2 PPI. These services operates the non-volatile
6 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 EFI_PEI_READ_ONLY_VARIABLE2_PPI mVariablePpi
= {
25 PeiGetNextVariableName
28 EFI_PEI_PPI_DESCRIPTOR mPpiListVariable
= {
29 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
30 &gEfiPeiReadOnlyVariable2PpiGuid
,
35 Provide the functionality of the variable services.
37 @param FileHandle Handle of the file being invoked.
38 Type EFI_PEI_FILE_HANDLE is defined in FfsFindNextFile().
39 @param PeiServices General purpose services available to every PEIM.
41 @retval EFI_SUCCESS If the interface could be successfully installed
42 @retval Others Returned from PeiServicesInstallPpi()
46 PeimInitializeVariableServices (
47 IN EFI_PEI_FILE_HANDLE FileHandle
,
48 IN CONST EFI_PEI_SERVICES
**PeiServices
51 return PeiServicesInstallPpi (&mPpiListVariable
);
56 Gets the pointer to the first variable header in given variable store area.
58 @param VarStoreHeader Pointer to the Variable Store Header.
60 @return Pointer to the first variable header
65 IN VARIABLE_STORE_HEADER
*VarStoreHeader
69 // The end of variable store
71 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
76 This code gets the pointer to the last variable memory pointer byte.
78 @param VarStoreHeader Pointer to the Variable Store Header.
80 @return VARIABLE_HEADER* pointer to last unavailable Variable Header.
85 IN VARIABLE_STORE_HEADER
*VarStoreHeader
89 // The end of variable store
91 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
96 This code checks if variable header is valid or not.
98 @param Variable Pointer to the Variable Header.
100 @retval TRUE Variable header is valid.
101 @retval FALSE Variable header is not valid.
105 IsValidVariableHeader (
106 IN VARIABLE_HEADER
*Variable
109 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
118 This code gets the size of name of variable.
120 @param Variable Pointer to the Variable Header.
122 @return Size of variable in bytes in type UINTN.
127 IN VARIABLE_HEADER
*Variable
130 if (Variable
->State
== (UINT8
) (-1) ||
131 Variable
->DataSize
== (UINT32
) (-1) ||
132 Variable
->NameSize
== (UINT32
) (-1) ||
133 Variable
->Attributes
== (UINT32
) (-1)) {
136 return (UINTN
) Variable
->NameSize
;
141 This code gets the size of data of variable.
143 @param Variable Pointer to the Variable Header.
145 @return Size of variable in bytes in type UINTN.
150 IN VARIABLE_HEADER
*Variable
153 if (Variable
->State
== (UINT8
) (-1) ||
154 Variable
->DataSize
== (UINT32
) (-1) ||
155 Variable
->NameSize
== (UINT32
) (-1) ||
156 Variable
->Attributes
== (UINT32
) (-1)) {
159 return (UINTN
) Variable
->DataSize
;
163 This code gets the pointer to the variable name.
165 @param Variable Pointer to the Variable Header.
167 @return A CHAR16* pointer to Variable Name.
172 IN VARIABLE_HEADER
*Variable
176 return (CHAR16
*) (Variable
+ 1);
181 This code gets the pointer to the variable data.
183 @param Variable Pointer to the Variable Header.
185 @return A UINT8* pointer to Variable Data.
190 IN VARIABLE_HEADER
*Variable
196 // Be careful about pad size for alignment
198 Value
= (UINTN
) GetVariableNamePtr (Variable
);
199 Value
+= NameSizeOfVariable (Variable
);
200 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
202 return (UINT8
*) Value
;
207 This code gets the pointer to the next variable header.
209 @param Variable Pointer to the Variable Header.
211 @return A VARIABLE_HEADER* pointer to next variable header.
216 IN VARIABLE_HEADER
*Variable
221 if (!IsValidVariableHeader (Variable
)) {
225 Value
= (UINTN
) GetVariableDataPtr (Variable
);
226 Value
+= DataSizeOfVariable (Variable
);
227 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
230 // Be careful about pad size for alignment
232 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
236 This code gets the pointer to the variable name.
238 @param VarStoreHeader Pointer to the Variable Store Header.
240 @retval EfiRaw Variable store is raw
241 @retval EfiValid Variable store is valid
242 @retval EfiInvalid Variable store is invalid
245 VARIABLE_STORE_STATUS
246 GetVariableStoreStatus (
247 IN VARIABLE_STORE_HEADER
*VarStoreHeader
251 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
) &&
252 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
253 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
259 if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
260 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
261 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
262 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
263 VarStoreHeader
->Size
== 0xffffffff &&
264 VarStoreHeader
->Format
== 0xff &&
265 VarStoreHeader
->State
== 0xff
276 This function compares a variable with variable entries in database.
278 @param Variable Pointer to the variable in our database
279 @param VariableName Name of the variable to compare to 'Variable'
280 @param VendorGuid GUID of the variable to compare to 'Variable'
281 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
283 @retval EFI_SUCCESS Found match variable
284 @retval EFI_NOT_FOUND Variable not found
288 CompareWithValidVariable (
289 IN VARIABLE_HEADER
*Variable
,
290 IN CONST CHAR16
*VariableName
,
291 IN CONST EFI_GUID
*VendorGuid
,
292 OUT VARIABLE_POINTER_TRACK
*PtrTrack
297 if (VariableName
[0] == 0) {
298 PtrTrack
->CurrPtr
= Variable
;
302 // Don't use CompareGuid function here for performance reasons.
303 // Instead we compare the GUID a UINT32 at a time and branch
304 // on the first failed comparison.
306 if ((((INT32
*) VendorGuid
)[0] == ((INT32
*) &Variable
->VendorGuid
)[0]) &&
307 (((INT32
*) VendorGuid
)[1] == ((INT32
*) &Variable
->VendorGuid
)[1]) &&
308 (((INT32
*) VendorGuid
)[2] == ((INT32
*) &Variable
->VendorGuid
)[2]) &&
309 (((INT32
*) VendorGuid
)[3] == ((INT32
*) &Variable
->VendorGuid
)[3])
311 ASSERT (NameSizeOfVariable (Variable
) != 0);
312 Point
= (VOID
*) GetVariableNamePtr (Variable
);
313 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (Variable
)) == 0) {
314 PtrTrack
->CurrPtr
= Variable
;
320 return EFI_NOT_FOUND
;
324 Return the variable store header and the index table based on the Index.
326 @param Type The type of the variable store.
327 @param IndexTable Return the index table.
329 @return Pointer to the variable store header.
331 VARIABLE_STORE_HEADER
*
333 IN VARIABLE_STORE_TYPE Type
,
334 OUT VARIABLE_INDEX_TABLE
**IndexTable OPTIONAL
337 EFI_HOB_GUID_TYPE
*GuidHob
;
338 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
339 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
341 if (IndexTable
!= NULL
) {
344 VariableStoreHeader
= NULL
;
346 case VariableStoreTypeHob
:
347 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
348 if (GuidHob
!= NULL
) {
349 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) GET_GUID_HOB_DATA (GuidHob
);
353 case VariableStoreTypeNv
:
354 if (GetBootModeHob () != BOOT_IN_RECOVERY_MODE
) {
356 // The content of NV storage for variable is not reliable in recovery boot mode.
358 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) (PcdGet64 (PcdFlashNvStorageVariableBase64
) != 0 ?
359 PcdGet64 (PcdFlashNvStorageVariableBase64
) :
360 PcdGet32 (PcdFlashNvStorageVariableBase
)
362 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINT8
*) FvHeader
+ FvHeader
->HeaderLength
);
364 if (IndexTable
!= NULL
) {
365 GuidHob
= GetFirstGuidHob (&gEfiVariableIndexTableGuid
);
366 if (GuidHob
!= NULL
) {
367 *IndexTable
= GET_GUID_HOB_DATA (GuidHob
);
370 // If it's the first time to access variable region in flash, create a guid hob to record
371 // VAR_ADDED type variable info.
372 // Note that as the resource of PEI phase is limited, only store the limited number of
373 // VAR_ADDED type variables to reduce access time.
375 *IndexTable
= BuildGuidHob (&gEfiVariableIndexTableGuid
, sizeof (VARIABLE_INDEX_TABLE
));
376 (*IndexTable
)->Length
= 0;
377 (*IndexTable
)->StartPtr
= GetStartPointer (VariableStoreHeader
);
378 (*IndexTable
)->EndPtr
= GetEndPointer (VariableStoreHeader
);
379 (*IndexTable
)->GoneThrough
= 0;
390 return VariableStoreHeader
;
394 Find the variable in the specified variable store.
396 @param VariableStoreHeader Pointer to the variable store header.
397 @param IndexTable Pointer to the index table.
398 @param VariableName Name of the variable to be found
399 @param VendorGuid Vendor GUID to be found.
400 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
402 @retval EFI_SUCCESS Variable found successfully
403 @retval EFI_NOT_FOUND Variable not found
404 @retval EFI_INVALID_PARAMETER Invalid variable name
409 IN VARIABLE_STORE_HEADER
*VariableStoreHeader
,
410 IN VARIABLE_INDEX_TABLE
*IndexTable
,
411 IN CONST CHAR16
*VariableName
,
412 IN CONST EFI_GUID
*VendorGuid
,
413 OUT VARIABLE_POINTER_TRACK
*PtrTrack
416 VARIABLE_HEADER
*Variable
;
417 VARIABLE_HEADER
*LastVariable
;
418 VARIABLE_HEADER
*MaxIndex
;
423 if (VariableStoreHeader
== NULL
) {
424 return EFI_INVALID_PARAMETER
;
427 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
428 return EFI_UNSUPPORTED
;
431 if (~VariableStoreHeader
->Size
== 0) {
432 return EFI_NOT_FOUND
;
435 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
);
436 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
);
439 // No Variable Address equals zero, so 0 as initial value is safe.
443 if (IndexTable
!= NULL
) {
445 // traverse the variable index table to look for varible.
446 // The IndexTable->Index[Index] records the distance of two neighbouring VAR_ADDED type variables.
448 for (Offset
= 0, Index
= 0; Index
< IndexTable
->Length
; Index
++) {
449 ASSERT (Index
< sizeof (IndexTable
->Index
) / sizeof (IndexTable
->Index
[0]));
450 Offset
+= IndexTable
->Index
[Index
];
451 MaxIndex
= (VARIABLE_HEADER
*) ((UINT8
*) IndexTable
->StartPtr
+ Offset
);
452 if (CompareWithValidVariable (MaxIndex
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
457 if (IndexTable
->GoneThrough
!= 0) {
459 // If the table has all the existing variables indexed and we still cannot find it.
461 return EFI_NOT_FOUND
;
465 if (MaxIndex
!= NULL
) {
467 // HOB exists but the variable cannot be found in HOB
468 // If not found in HOB, then let's start from the MaxIndex we've found.
470 Variable
= GetNextVariablePtr (MaxIndex
);
471 LastVariable
= MaxIndex
;
474 // Start Pointers for the variable.
475 // Actual Data Pointer where data can be written.
477 Variable
= PtrTrack
->StartPtr
;
478 LastVariable
= PtrTrack
->StartPtr
;
482 // Find the variable by walk through non-volatile variable store
485 while ((Variable
< PtrTrack
->EndPtr
) && IsValidVariableHeader (Variable
)) {
486 if (Variable
->State
== VAR_ADDED
) {
488 // Record Variable in VariableIndex HOB
490 if ((IndexTable
!= NULL
) && !StopRecord
) {
491 Offset
= (UINTN
) Variable
- (UINTN
) LastVariable
;
492 if ((Offset
> 0x0FFFF) || (IndexTable
->Length
== sizeof (IndexTable
->Index
) / sizeof (IndexTable
->Index
[0]))) {
494 // Stop to record if the distance of two neighbouring VAR_ADDED variable is larger than the allowable scope(UINT16),
495 // or the record buffer is full.
499 IndexTable
->Index
[IndexTable
->Length
++] = (UINT16
) Offset
;
500 LastVariable
= Variable
;
504 if (CompareWithValidVariable (Variable
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
509 Variable
= GetNextVariablePtr (Variable
);
512 // If gone through the VariableStore, that means we never find in Firmware any more.
514 if ((IndexTable
!= NULL
) && !StopRecord
) {
515 IndexTable
->GoneThrough
= 1;
518 PtrTrack
->CurrPtr
= NULL
;
520 return EFI_NOT_FOUND
;
524 Find the variable in HOB and Non-Volatile variable storages.
526 @param VariableName Name of the variable to be found
527 @param VendorGuid Vendor GUID to be found.
528 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
530 @retval EFI_SUCCESS Variable found successfully
531 @retval EFI_NOT_FOUND Variable not found
532 @retval EFI_INVALID_PARAMETER Invalid variable name
536 IN CONST CHAR16
*VariableName
,
537 IN CONST EFI_GUID
*VendorGuid
,
538 OUT VARIABLE_POINTER_TRACK
*PtrTrack
542 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
543 VARIABLE_INDEX_TABLE
*IndexTable
;
544 VARIABLE_STORE_TYPE Type
;
546 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
547 return EFI_INVALID_PARAMETER
;
550 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
551 VariableStoreHeader
= GetVariableStore (Type
, &IndexTable
);
552 Status
= FindVariableEx (
559 if (!EFI_ERROR (Status
)) {
564 return EFI_NOT_FOUND
;
568 This service retrieves a variable's value using its name and GUID.
570 Read the specified variable from the UEFI variable store. If the Data
571 buffer is too small to hold the contents of the variable, the error
572 EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer
573 size to obtain the data.
575 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
576 @param VariableName A pointer to a null-terminated string that is the variable's name.
577 @param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of
578 VariableGuid and VariableName must be unique.
579 @param Attributes If non-NULL, on return, points to the variable's attributes.
580 @param DataSize On entry, points to the size in bytes of the Data buffer.
581 On return, points to the size of the data returned in Data.
582 @param Data Points to the buffer which will hold the returned variable value.
584 @retval EFI_SUCCESS The variable was read successfully.
585 @retval EFI_NOT_FOUND The variable could not be found.
586 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data.
587 DataSize is updated with the size required for
588 the specified variable.
589 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.
590 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
596 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
597 IN CONST CHAR16
*VariableName
,
598 IN CONST EFI_GUID
*VariableGuid
,
599 OUT UINT32
*Attributes
,
600 IN OUT UINTN
*DataSize
,
604 VARIABLE_POINTER_TRACK Variable
;
608 if (VariableName
== NULL
|| VariableGuid
== NULL
|| DataSize
== NULL
) {
609 return EFI_INVALID_PARAMETER
;
613 // Find existing variable
615 Status
= FindVariable (VariableName
, VariableGuid
, &Variable
);
616 if (EFI_ERROR (Status
)) {
622 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
623 if (*DataSize
>= VarDataSize
) {
625 return EFI_INVALID_PARAMETER
;
628 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
630 if (Attributes
!= NULL
) {
631 *Attributes
= Variable
.CurrPtr
->Attributes
;
634 *DataSize
= VarDataSize
;
637 *DataSize
= VarDataSize
;
638 return EFI_BUFFER_TOO_SMALL
;
643 Return the next variable name and GUID.
645 This function is called multiple times to retrieve the VariableName
646 and VariableGuid of all variables currently available in the system.
647 On each call, the previous results are passed into the interface,
648 and, on return, the interface returns the data for the next
649 interface. When the entire variable list has been returned,
650 EFI_NOT_FOUND is returned.
652 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
654 @param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.
655 On return, the size of the variable name buffer.
656 @param VariableName On entry, a pointer to a null-terminated string that is the variable's name.
657 On return, points to the next variable's null-terminated name string.
658 @param VariableGuid On entry, a pointer to an EFI_GUID that is the variable's GUID.
659 On return, a pointer to the next variable's GUID.
661 @retval EFI_SUCCESS The variable was read successfully.
662 @retval EFI_NOT_FOUND The variable could not be found.
663 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting
664 data. VariableNameSize is updated with the size
665 required for the specified variable.
666 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or
667 VariableNameSize is NULL.
668 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
673 PeiGetNextVariableName (
674 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
675 IN OUT UINTN
*VariableNameSize
,
676 IN OUT CHAR16
*VariableName
,
677 IN OUT EFI_GUID
*VariableGuid
680 VARIABLE_STORE_TYPE Type
;
681 VARIABLE_POINTER_TRACK Variable
;
682 VARIABLE_POINTER_TRACK VariableInHob
;
685 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
687 if (VariableName
== NULL
|| VariableGuid
== NULL
|| VariableNameSize
== NULL
) {
688 return EFI_INVALID_PARAMETER
;
691 Status
= FindVariable (VariableName
, VariableGuid
, &Variable
);
692 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
696 if (VariableName
[0] != 0) {
698 // If variable name is not NULL, get next variable
700 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
703 VariableStoreHeader
[VariableStoreTypeHob
] = GetVariableStore (VariableStoreTypeHob
, NULL
);
704 VariableStoreHeader
[VariableStoreTypeNv
] = GetVariableStore (VariableStoreTypeNv
, NULL
);
708 // Switch from HOB to Non-Volatile.
710 while ((Variable
.CurrPtr
>= Variable
.EndPtr
) ||
711 (Variable
.CurrPtr
== NULL
) ||
712 !IsValidVariableHeader (Variable
.CurrPtr
)
715 // Find current storage index
717 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
718 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
722 ASSERT (Type
< VariableStoreTypeMax
);
724 // Switch to next storage
726 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
727 if (VariableStoreHeader
[Type
] != NULL
) {
732 // Capture the case that
733 // 1. current storage is the last one, or
734 // 2. no further storage
736 if (Type
== VariableStoreTypeMax
) {
737 return EFI_NOT_FOUND
;
739 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
740 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
741 Variable
.CurrPtr
= Variable
.StartPtr
;
744 if (Variable
.CurrPtr
->State
== VAR_ADDED
) {
747 // Don't return NV variable when HOB overrides it
749 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
750 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
752 Status
= FindVariableEx (
753 VariableStoreHeader
[VariableStoreTypeHob
],
755 GetVariableNamePtr (Variable
.CurrPtr
),
756 &Variable
.CurrPtr
->VendorGuid
,
759 if (!EFI_ERROR (Status
)) {
760 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
765 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
766 ASSERT (VarNameSize
!= 0);
768 if (VarNameSize
<= *VariableNameSize
) {
769 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
771 CopyMem (VariableGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
773 Status
= EFI_SUCCESS
;
775 Status
= EFI_BUFFER_TOO_SMALL
;
778 *VariableNameSize
= VarNameSize
;
784 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);