3 Implement ReadOnly Variable Services required by PEIM and install
4 PEI ReadOnly Varaiable2 PPI. These services operates the non volatile storage space.
6 Copyright (c) 2006 - 2010, 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.
24 EFI_PEI_READ_ONLY_VARIABLE2_PPI mVariablePpi
= {
26 PeiGetNextVariableName
29 EFI_PEI_PPI_DESCRIPTOR mPpiListVariable
= {
30 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
31 &gEfiPeiReadOnlyVariable2PpiGuid
,
35 EFI_GUID mEfiVariableIndexTableGuid
= EFI_VARIABLE_INDEX_TABLE_GUID
;
39 Provide the functionality of the variable services.
41 @param FileHandle Handle of the file being invoked.
42 Type EFI_PEI_FILE_HANDLE is defined in FfsFindNextFile().
43 @param PeiServices General purpose services available to every PEIM.
45 @retval EFI_SUCCESS If the interface could be successfully installed
46 @retval Others Returned from PeiServicesInstallPpi()
51 PeimInitializeVariableServices (
52 IN EFI_PEI_FILE_HANDLE FileHandle
,
53 IN CONST EFI_PEI_SERVICES
**PeiServices
56 EFI_BOOT_MODE BootMode
;
60 // Check if this is recovery boot path. If no, publish the variable access capability
61 // to other modules. If yes, the content of variable area is not reliable. Therefore,
62 // in this case we should not provide variable service to other pei modules.
64 Status
= (*PeiServices
)->GetBootMode (PeiServices
, &BootMode
);
65 ASSERT_EFI_ERROR (Status
);
67 if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
68 return EFI_UNSUPPORTED
;
71 return PeiServicesInstallPpi (&mPpiListVariable
);
77 Gets the pointer to the first variable header in given variable store area.
79 @param VarStoreHeader Pointer to the Variable Store Header.
81 @return Pointer to the first variable header
86 IN VARIABLE_STORE_HEADER
*VarStoreHeader
90 // The end of variable store
92 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
97 This code gets the pointer to the last variable memory pointer byte.
99 @param VarStoreHeader Pointer to the Variable Store Header.
101 @return VARIABLE_HEADER* pointer to last unavailable Variable Header.
106 IN VARIABLE_STORE_HEADER
*VarStoreHeader
110 // The end of variable store
112 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
117 This code checks if variable header is valid or not.
119 @param Variable Pointer to the Variable Header.
121 @retval TRUE Variable header is valid.
122 @retval FALSE Variable header is not valid.
126 IsValidVariableHeader (
127 IN VARIABLE_HEADER
*Variable
130 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
139 This code gets the size of name of variable.
141 @param Variable Pointer to the Variable Header.
143 @return Size of variable in bytes in type UINTN.
148 IN VARIABLE_HEADER
*Variable
151 if (Variable
->State
== (UINT8
) (-1) ||
152 Variable
->DataSize
== (UINT32
) (-1) ||
153 Variable
->NameSize
== (UINT32
) (-1) ||
154 Variable
->Attributes
== (UINT32
) (-1)) {
157 return (UINTN
) Variable
->NameSize
;
162 This code gets the size of data of variable.
164 @param Variable Pointer to the Variable Header.
166 @return Size of variable in bytes in type UINTN.
171 IN VARIABLE_HEADER
*Variable
174 if (Variable
->State
== (UINT8
) (-1) ||
175 Variable
->DataSize
== (UINT32
) (-1) ||
176 Variable
->NameSize
== (UINT32
) (-1) ||
177 Variable
->Attributes
== (UINT32
) (-1)) {
180 return (UINTN
) Variable
->DataSize
;
184 This code gets the pointer to the variable name.
186 @param Variable Pointer to the Variable Header.
188 @return A CHAR16* pointer to Variable Name.
193 IN VARIABLE_HEADER
*Variable
197 return (CHAR16
*) (Variable
+ 1);
202 This code gets the pointer to the variable data.
204 @param Variable Pointer to the Variable Header.
206 @return A UINT8* pointer to Variable Data.
211 IN VARIABLE_HEADER
*Variable
217 // Be careful about pad size for alignment
219 Value
= (UINTN
) GetVariableNamePtr (Variable
);
220 Value
+= NameSizeOfVariable (Variable
);
221 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
223 return (UINT8
*) Value
;
228 This code gets the pointer to the next variable header.
230 @param Variable Pointer to the Variable Header.
232 @return A VARIABLE_HEADER* pointer to next variable header.
237 IN VARIABLE_HEADER
*Variable
242 if (!IsValidVariableHeader (Variable
)) {
246 Value
= (UINTN
) GetVariableDataPtr (Variable
);
247 Value
+= DataSizeOfVariable (Variable
);
248 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
251 // Be careful about pad size for alignment
253 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
257 This code gets the pointer to the variable name.
259 @param VarStoreHeader Pointer to the Variable Store Header.
261 @retval EfiRaw Variable store is raw
262 @retval EfiValid Variable store is valid
263 @retval EfiInvalid Variable store is invalid
266 VARIABLE_STORE_STATUS
267 GetVariableStoreStatus (
268 IN VARIABLE_STORE_HEADER
*VarStoreHeader
272 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiVariableGuid
) &&
273 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
274 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
280 if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
281 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
282 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
283 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
284 VarStoreHeader
->Size
== 0xffffffff &&
285 VarStoreHeader
->Format
== 0xff &&
286 VarStoreHeader
->State
== 0xff
297 This function compares a variable with variable entries in database.
299 @param Variable Pointer to the variable in our database
300 @param VariableName Name of the variable to compare to 'Variable'
301 @param VendorGuid GUID of the variable to compare to 'Variable'
302 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
304 @retval EFI_SUCCESS Found match variable
305 @retval EFI_NOT_FOUND Variable not found
309 CompareWithValidVariable (
310 IN VARIABLE_HEADER
*Variable
,
311 IN CONST CHAR16
*VariableName
,
312 IN CONST EFI_GUID
*VendorGuid
,
313 OUT VARIABLE_POINTER_TRACK
*PtrTrack
318 if (VariableName
[0] == 0) {
319 PtrTrack
->CurrPtr
= Variable
;
323 // Don't use CompareGuid function here for performance reasons.
324 // Instead we compare the GUID a UINT32 at a time and branch
325 // on the first failed comparison.
327 if ((((INT32
*) VendorGuid
)[0] == ((INT32
*) &Variable
->VendorGuid
)[0]) &&
328 (((INT32
*) VendorGuid
)[1] == ((INT32
*) &Variable
->VendorGuid
)[1]) &&
329 (((INT32
*) VendorGuid
)[2] == ((INT32
*) &Variable
->VendorGuid
)[2]) &&
330 (((INT32
*) VendorGuid
)[3] == ((INT32
*) &Variable
->VendorGuid
)[3])
332 ASSERT (NameSizeOfVariable (Variable
) != 0);
333 Point
= (VOID
*) GetVariableNamePtr (Variable
);
334 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (Variable
)) == 0) {
335 PtrTrack
->CurrPtr
= Variable
;
341 return EFI_NOT_FOUND
;
346 This code finds variable in storage blocks (Non-Volatile).
348 @param PeiServices General purpose services available to every PEIM.
349 @param VariableName Name of the variable to be found
350 @param VendorGuid Vendor GUID to be found.
351 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
353 @retval EFI_SUCCESS Variable found successfully
354 @retval EFI_NOT_FOUND Variable not found
355 @retval EFI_INVALID_PARAMETER Invalid variable name
360 IN CONST EFI_PEI_SERVICES
**PeiServices
,
361 IN CONST CHAR16
*VariableName
,
362 IN CONST EFI_GUID
*VendorGuid
,
363 OUT VARIABLE_POINTER_TRACK
*PtrTrack
366 EFI_HOB_GUID_TYPE
*GuidHob
;
367 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
368 VARIABLE_HEADER
*Variable
;
369 VARIABLE_HEADER
*LastVariable
;
370 VARIABLE_HEADER
*MaxIndex
;
371 VARIABLE_INDEX_TABLE
*IndexTable
;
377 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
378 return EFI_INVALID_PARAMETER
;
381 // No Variable Address equals zero, so 0 as initial value is safe.
386 GuidHob
= GetFirstGuidHob (&mEfiVariableIndexTableGuid
);
387 if (GuidHob
== NULL
) {
389 // If it's the first time to access variable region in flash, create a guid hob to record
390 // VAR_ADDED type variable info.
391 // Note that as the resource of PEI phase is limited, only store the number of
392 // VARIABLE_INDEX_TABLE_VOLUME of VAR_ADDED type variables to reduce access time.
394 IndexTable
= BuildGuidHob (&mEfiVariableIndexTableGuid
, sizeof (VARIABLE_INDEX_TABLE
));
395 IndexTable
->Length
= 0;
396 IndexTable
->StartPtr
= NULL
;
397 IndexTable
->EndPtr
= NULL
;
398 IndexTable
->GoneThrough
= 0;
400 IndexTable
= GET_GUID_HOB_DATA (GuidHob
);
401 for (Offset
= 0, Count
= 0; Count
< IndexTable
->Length
; Count
++) {
403 // traverse the variable info list to look for varible.
404 // The IndexTable->Index[Count] records the distance of two neighbouring VAR_ADDED type variables.
406 ASSERT (Count
< VARIABLE_INDEX_TABLE_VOLUME
);
407 Offset
+= IndexTable
->Index
[Count
];
408 MaxIndex
= (VARIABLE_HEADER
*)((CHAR8
*)(IndexTable
->StartPtr
) + Offset
);
409 if (CompareWithValidVariable (MaxIndex
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
410 PtrTrack
->StartPtr
= IndexTable
->StartPtr
;
411 PtrTrack
->EndPtr
= IndexTable
->EndPtr
;
417 if (IndexTable
->GoneThrough
!= 0) {
418 return EFI_NOT_FOUND
;
422 // If not found in HOB, then let's start from the MaxIndex we've found.
424 if (MaxIndex
!= NULL
) {
425 Variable
= GetNextVariablePtr (MaxIndex
);
426 LastVariable
= MaxIndex
;
428 if ((IndexTable
->StartPtr
!= NULL
) || (IndexTable
->EndPtr
!= NULL
)) {
429 Variable
= IndexTable
->StartPtr
;
431 VariableBase
= (UINT8
*) (UINTN
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
432 if (VariableBase
== NULL
) {
433 VariableBase
= (UINT8
*) (UINTN
) PcdGet32 (PcdFlashNvStorageVariableBase
);
436 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (VariableBase
+ \
437 ((EFI_FIRMWARE_VOLUME_HEADER
*) (VariableBase
)) -> HeaderLength
);
439 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
440 return EFI_UNSUPPORTED
;
443 if (~VariableStoreHeader
->Size
== 0) {
444 return EFI_NOT_FOUND
;
447 // Find the variable by walk through non-volatile variable store
449 IndexTable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
450 IndexTable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
453 // Start Pointers for the variable.
454 // Actual Data Pointer where data can be written.
456 Variable
= IndexTable
->StartPtr
;
459 LastVariable
= IndexTable
->StartPtr
;
462 // Find the variable by walk through non-volatile variable store
464 PtrTrack
->StartPtr
= IndexTable
->StartPtr
;
465 PtrTrack
->EndPtr
= IndexTable
->EndPtr
;
467 while ((Variable
< IndexTable
->EndPtr
) && IsValidVariableHeader (Variable
)) {
468 if (Variable
->State
== VAR_ADDED
) {
470 // Record Variable in VariableIndex HOB
472 if (IndexTable
->Length
< VARIABLE_INDEX_TABLE_VOLUME
&& !StopRecord
) {
473 Offset
= (UINT32
)((UINTN
)Variable
- (UINTN
)LastVariable
);
475 // The distance of two neighbouring VAR_ADDED variable is larger than 2^16,
476 // which is beyond the allowable scope(UINT16) of record. In such case, need not to
477 // record the subsequent VAR_ADDED type variables again.
479 if ((Offset
& 0xFFFF0000UL
) != 0) {
484 IndexTable
->Index
[IndexTable
->Length
++] = (UINT16
) Offset
;
486 LastVariable
= Variable
;
489 if (CompareWithValidVariable (Variable
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
494 Variable
= GetNextVariablePtr (Variable
);
497 // If gone through the VariableStore, that means we never find in Firmware any more.
499 if ((IndexTable
->Length
< VARIABLE_INDEX_TABLE_VOLUME
) && (!StopRecord
)) {
500 IndexTable
->GoneThrough
= 1;
503 PtrTrack
->CurrPtr
= NULL
;
505 return EFI_NOT_FOUND
;
509 This service retrieves a variable's value using its name and GUID.
511 Read the specified variable from the UEFI variable store. If the Data
512 buffer is too small to hold the contents of the variable, the error
513 EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer
514 size to obtain the data.
516 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
517 @param VariableName A pointer to a null-terminated string that is the variable's name.
518 @param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of
519 VariableGuid and VariableName must be unique.
520 @param Attributes If non-NULL, on return, points to the variable's attributes.
521 @param DataSize On entry, points to the size in bytes of the Data buffer.
522 On return, points to the size of the data returned in Data.
523 @param Data Points to the buffer which will hold the returned variable value.
525 @retval EFI_SUCCESS The variable was read successfully.
526 @retval EFI_NOT_FOUND The variable could not be found.
527 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data.
528 DataSize is updated with the size required for
529 the specified variable.
530 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.
531 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
537 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
538 IN CONST CHAR16
*VariableName
,
539 IN CONST EFI_GUID
*VariableGuid
,
540 OUT UINT32
*Attributes
,
541 IN OUT UINTN
*DataSize
,
545 VARIABLE_POINTER_TRACK Variable
;
548 CONST EFI_PEI_SERVICES
**PeiServices
;
550 PeiServices
= GetPeiServicesTablePointer ();
551 if (VariableName
== NULL
|| VariableGuid
== NULL
|| DataSize
== NULL
) {
552 return EFI_INVALID_PARAMETER
;
555 // Find existing variable
557 Status
= FindVariable (PeiServices
, VariableName
, VariableGuid
, &Variable
);
558 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
564 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
565 if (*DataSize
>= VarDataSize
) {
567 return EFI_INVALID_PARAMETER
;
570 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
572 if (Attributes
!= NULL
) {
573 *Attributes
= Variable
.CurrPtr
->Attributes
;
576 *DataSize
= VarDataSize
;
579 *DataSize
= VarDataSize
;
580 return EFI_BUFFER_TOO_SMALL
;
585 Return the next variable name and GUID.
587 This function is called multiple times to retrieve the VariableName
588 and VariableGuid of all variables currently available in the system.
589 On each call, the previous results are passed into the interface,
590 and, on return, the interface returns the data for the next
591 interface. When the entire variable list has been returned,
592 EFI_NOT_FOUND is returned.
594 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
596 @param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.
597 On return, the size of the variable name buffer.
598 @param VariableName On entry, a pointer to a null-terminated string that is the variable's name.
599 On return, points to the next variable's null-terminated name string.
600 @param VariableGuid On entry, a pointer to an EFI_GUID that is the variable's GUID.
601 On return, a pointer to the next variable's GUID.
603 @retval EFI_SUCCESS The variable was read successfully.
604 @retval EFI_NOT_FOUND The variable could not be found.
605 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting
606 data. VariableNameSize is updated with the size
607 required for the specified variable.
608 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or
609 VariableNameSize is NULL.
610 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
615 PeiGetNextVariableName (
616 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
617 IN OUT UINTN
*VariableNameSize
,
618 IN OUT CHAR16
*VariableName
,
619 IN OUT EFI_GUID
*VariableGuid
622 VARIABLE_POINTER_TRACK Variable
;
625 CONST EFI_PEI_SERVICES
**PeiServices
;
627 PeiServices
= GetPeiServicesTablePointer ();
628 if (VariableName
== NULL
|| VariableGuid
== NULL
|| VariableNameSize
== NULL
) {
629 return EFI_INVALID_PARAMETER
;
632 Status
= FindVariable (PeiServices
, VariableName
, VariableGuid
, &Variable
);
633 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
637 if (VariableName
[0] != 0) {
639 // If variable name is not NULL, get next variable
641 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
644 while (!(Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
)) {
645 if (IsValidVariableHeader (Variable
.CurrPtr
)) {
646 if (Variable
.CurrPtr
->State
== VAR_ADDED
) {
647 ASSERT (NameSizeOfVariable (Variable
.CurrPtr
) != 0);
649 VarNameSize
= (UINTN
) NameSizeOfVariable (Variable
.CurrPtr
);
650 if (VarNameSize
<= *VariableNameSize
) {
651 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
653 CopyMem (VariableGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
655 Status
= EFI_SUCCESS
;
657 Status
= EFI_BUFFER_TOO_SMALL
;
660 *VariableNameSize
= VarNameSize
;
666 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
673 return EFI_NOT_FOUND
;