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 - 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.
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 Check if it runs in Recovery mode.
41 @param PeiServices General purpose services available to every PEIM.
43 @retval TRUE It's in Recovery mode.
44 @retval FALSE It's not in Recovery mode.
49 IN CONST EFI_PEI_SERVICES
**PeiServices
53 EFI_BOOT_MODE BootMode
;
55 Status
= (*PeiServices
)->GetBootMode (PeiServices
, &BootMode
);
56 ASSERT_EFI_ERROR (Status
);
58 if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
65 Provide the functionality of the variable services.
67 @param FileHandle Handle of the file being invoked.
68 Type EFI_PEI_FILE_HANDLE is defined in FfsFindNextFile().
69 @param PeiServices General purpose services available to every PEIM.
71 @retval EFI_SUCCESS If the interface could be successfully installed
72 @retval Others Returned from PeiServicesInstallPpi()
77 PeimInitializeVariableServices (
78 IN EFI_PEI_FILE_HANDLE FileHandle
,
79 IN CONST EFI_PEI_SERVICES
**PeiServices
82 return PeiServicesInstallPpi (&mPpiListVariable
);
87 Gets the pointer to the first variable header in given variable store area.
89 @param VarStoreHeader Pointer to the Variable Store Header.
91 @return Pointer to the first variable header
96 IN VARIABLE_STORE_HEADER
*VarStoreHeader
100 // The end of variable store
102 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
107 This code gets the pointer to the last variable memory pointer byte.
109 @param VarStoreHeader Pointer to the Variable Store Header.
111 @return VARIABLE_HEADER* pointer to last unavailable Variable Header.
116 IN VARIABLE_STORE_HEADER
*VarStoreHeader
120 // The end of variable store
122 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
127 This code checks if variable header is valid or not.
129 @param Variable Pointer to the Variable Header.
131 @retval TRUE Variable header is valid.
132 @retval FALSE Variable header is not valid.
136 IsValidVariableHeader (
137 IN VARIABLE_HEADER
*Variable
140 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
149 This code gets the size of name of variable.
151 @param Variable Pointer to the Variable Header.
153 @return Size of variable in bytes in type UINTN.
158 IN VARIABLE_HEADER
*Variable
161 if (Variable
->State
== (UINT8
) (-1) ||
162 Variable
->DataSize
== (UINT32
) (-1) ||
163 Variable
->NameSize
== (UINT32
) (-1) ||
164 Variable
->Attributes
== (UINT32
) (-1)) {
167 return (UINTN
) Variable
->NameSize
;
172 This code gets the size of data of variable.
174 @param Variable Pointer to the Variable Header.
176 @return Size of variable in bytes in type UINTN.
181 IN VARIABLE_HEADER
*Variable
184 if (Variable
->State
== (UINT8
) (-1) ||
185 Variable
->DataSize
== (UINT32
) (-1) ||
186 Variable
->NameSize
== (UINT32
) (-1) ||
187 Variable
->Attributes
== (UINT32
) (-1)) {
190 return (UINTN
) Variable
->DataSize
;
194 This code gets the pointer to the variable name.
196 @param Variable Pointer to the Variable Header.
198 @return A CHAR16* pointer to Variable Name.
203 IN VARIABLE_HEADER
*Variable
207 return (CHAR16
*) (Variable
+ 1);
212 This code gets the pointer to the variable data.
214 @param Variable Pointer to the Variable Header.
216 @return A UINT8* pointer to Variable Data.
221 IN VARIABLE_HEADER
*Variable
227 // Be careful about pad size for alignment
229 Value
= (UINTN
) GetVariableNamePtr (Variable
);
230 Value
+= NameSizeOfVariable (Variable
);
231 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
233 return (UINT8
*) Value
;
238 This code gets the pointer to the next variable header.
240 @param Variable Pointer to the Variable Header.
242 @return A VARIABLE_HEADER* pointer to next variable header.
247 IN VARIABLE_HEADER
*Variable
252 if (!IsValidVariableHeader (Variable
)) {
256 Value
= (UINTN
) GetVariableDataPtr (Variable
);
257 Value
+= DataSizeOfVariable (Variable
);
258 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
261 // Be careful about pad size for alignment
263 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
267 This code gets the pointer to the variable name.
269 @param VarStoreHeader Pointer to the Variable Store Header.
271 @retval EfiRaw Variable store is raw
272 @retval EfiValid Variable store is valid
273 @retval EfiInvalid Variable store is invalid
276 VARIABLE_STORE_STATUS
277 GetVariableStoreStatus (
278 IN VARIABLE_STORE_HEADER
*VarStoreHeader
282 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiVariableGuid
) &&
283 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
284 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
290 if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
291 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
292 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
293 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
294 VarStoreHeader
->Size
== 0xffffffff &&
295 VarStoreHeader
->Format
== 0xff &&
296 VarStoreHeader
->State
== 0xff
307 This function compares a variable with variable entries in database.
309 @param Variable Pointer to the variable in our database
310 @param VariableName Name of the variable to compare to 'Variable'
311 @param VendorGuid GUID of the variable to compare to 'Variable'
312 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
314 @retval EFI_SUCCESS Found match variable
315 @retval EFI_NOT_FOUND Variable not found
319 CompareWithValidVariable (
320 IN VARIABLE_HEADER
*Variable
,
321 IN CONST CHAR16
*VariableName
,
322 IN CONST EFI_GUID
*VendorGuid
,
323 OUT VARIABLE_POINTER_TRACK
*PtrTrack
328 if (VariableName
[0] == 0) {
329 PtrTrack
->CurrPtr
= Variable
;
333 // Don't use CompareGuid function here for performance reasons.
334 // Instead we compare the GUID a UINT32 at a time and branch
335 // on the first failed comparison.
337 if ((((INT32
*) VendorGuid
)[0] == ((INT32
*) &Variable
->VendorGuid
)[0]) &&
338 (((INT32
*) VendorGuid
)[1] == ((INT32
*) &Variable
->VendorGuid
)[1]) &&
339 (((INT32
*) VendorGuid
)[2] == ((INT32
*) &Variable
->VendorGuid
)[2]) &&
340 (((INT32
*) VendorGuid
)[3] == ((INT32
*) &Variable
->VendorGuid
)[3])
342 ASSERT (NameSizeOfVariable (Variable
) != 0);
343 Point
= (VOID
*) GetVariableNamePtr (Variable
);
344 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (Variable
)) == 0) {
345 PtrTrack
->CurrPtr
= Variable
;
351 return EFI_NOT_FOUND
;
356 This code finds variable in storage blocks (Non-Volatile).
358 @param PeiServices General purpose services available to every PEIM.
359 @param VariableName Name of the variable to be found
360 @param VendorGuid Vendor GUID to be found.
361 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
363 @retval EFI_SUCCESS Variable found successfully
364 @retval EFI_NOT_FOUND Variable not found
365 @retval EFI_INVALID_PARAMETER Invalid variable name
370 IN CONST EFI_PEI_SERVICES
**PeiServices
,
371 IN CONST CHAR16
*VariableName
,
372 IN CONST EFI_GUID
*VendorGuid
,
373 OUT VARIABLE_POINTER_TRACK
*PtrTrack
376 EFI_HOB_GUID_TYPE
*GuidHob
;
377 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
378 VARIABLE_HEADER
*Variable
;
379 VARIABLE_HEADER
*LastVariable
;
380 VARIABLE_HEADER
*MaxIndex
;
381 VARIABLE_INDEX_TABLE
*IndexTable
;
387 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
388 return EFI_INVALID_PARAMETER
;
391 // No Variable Address equals zero, so 0 as initial value is safe.
396 GuidHob
= GetFirstGuidHob (&mEfiVariableIndexTableGuid
);
397 if (GuidHob
== NULL
) {
399 // If it's the first time to access variable region in flash, create a guid hob to record
400 // VAR_ADDED type variable info.
401 // Note that as the resource of PEI phase is limited, only store the number of
402 // VARIABLE_INDEX_TABLE_VOLUME of VAR_ADDED type variables to reduce access time.
404 IndexTable
= BuildGuidHob (&mEfiVariableIndexTableGuid
, sizeof (VARIABLE_INDEX_TABLE
));
405 IndexTable
->Length
= 0;
406 IndexTable
->StartPtr
= NULL
;
407 IndexTable
->EndPtr
= NULL
;
408 IndexTable
->GoneThrough
= 0;
410 IndexTable
= GET_GUID_HOB_DATA (GuidHob
);
411 for (Offset
= 0, Count
= 0; Count
< IndexTable
->Length
; Count
++) {
413 // traverse the variable info list to look for varible.
414 // The IndexTable->Index[Count] records the distance of two neighbouring VAR_ADDED type variables.
416 ASSERT (Count
< VARIABLE_INDEX_TABLE_VOLUME
);
417 Offset
+= IndexTable
->Index
[Count
];
418 MaxIndex
= (VARIABLE_HEADER
*)((CHAR8
*)(IndexTable
->StartPtr
) + Offset
);
419 if (CompareWithValidVariable (MaxIndex
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
420 PtrTrack
->StartPtr
= IndexTable
->StartPtr
;
421 PtrTrack
->EndPtr
= IndexTable
->EndPtr
;
427 if (IndexTable
->GoneThrough
!= 0) {
428 return EFI_NOT_FOUND
;
432 // If not found in HOB, then let's start from the MaxIndex we've found.
434 if (MaxIndex
!= NULL
) {
435 Variable
= GetNextVariablePtr (MaxIndex
);
436 LastVariable
= MaxIndex
;
438 if ((IndexTable
->StartPtr
!= NULL
) || (IndexTable
->EndPtr
!= NULL
)) {
439 Variable
= IndexTable
->StartPtr
;
441 VariableBase
= (UINT8
*) (UINTN
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
442 if (VariableBase
== NULL
) {
443 VariableBase
= (UINT8
*) (UINTN
) PcdGet32 (PcdFlashNvStorageVariableBase
);
446 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (VariableBase
+ \
447 ((EFI_FIRMWARE_VOLUME_HEADER
*) (VariableBase
)) -> HeaderLength
);
449 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
450 return EFI_UNSUPPORTED
;
453 if (~VariableStoreHeader
->Size
== 0) {
454 return EFI_NOT_FOUND
;
457 // Find the variable by walk through non-volatile variable store
459 IndexTable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
460 IndexTable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
463 // Start Pointers for the variable.
464 // Actual Data Pointer where data can be written.
466 Variable
= IndexTable
->StartPtr
;
469 LastVariable
= IndexTable
->StartPtr
;
472 // Find the variable by walk through non-volatile variable store
474 PtrTrack
->StartPtr
= IndexTable
->StartPtr
;
475 PtrTrack
->EndPtr
= IndexTable
->EndPtr
;
477 while ((Variable
< IndexTable
->EndPtr
) && IsValidVariableHeader (Variable
)) {
478 if (Variable
->State
== VAR_ADDED
) {
480 // Record Variable in VariableIndex HOB
482 if (IndexTable
->Length
< VARIABLE_INDEX_TABLE_VOLUME
&& !StopRecord
) {
483 Offset
= (UINT32
)((UINTN
)Variable
- (UINTN
)LastVariable
);
485 // The distance of two neighbouring VAR_ADDED variable is larger than 2^16,
486 // which is beyond the allowable scope(UINT16) of record. In such case, need not to
487 // record the subsequent VAR_ADDED type variables again.
489 if ((Offset
& 0xFFFF0000UL
) != 0) {
494 IndexTable
->Index
[IndexTable
->Length
++] = (UINT16
) Offset
;
496 LastVariable
= Variable
;
499 if (CompareWithValidVariable (Variable
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
504 Variable
= GetNextVariablePtr (Variable
);
507 // If gone through the VariableStore, that means we never find in Firmware any more.
509 if ((IndexTable
->Length
< VARIABLE_INDEX_TABLE_VOLUME
) && (!StopRecord
)) {
510 IndexTable
->GoneThrough
= 1;
513 PtrTrack
->CurrPtr
= NULL
;
515 return EFI_NOT_FOUND
;
519 This service retrieves a variable's value using its name and GUID.
521 Read the specified variable from the UEFI variable store. If the Data
522 buffer is too small to hold the contents of the variable, the error
523 EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer
524 size to obtain the data.
526 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
527 @param VariableName A pointer to a null-terminated string that is the variable's name.
528 @param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of
529 VariableGuid and VariableName must be unique.
530 @param Attributes If non-NULL, on return, points to the variable's attributes.
531 @param DataSize On entry, points to the size in bytes of the Data buffer.
532 On return, points to the size of the data returned in Data.
533 @param Data Points to the buffer which will hold the returned variable value.
535 @retval EFI_SUCCESS The variable was read successfully.
536 @retval EFI_NOT_FOUND The variable could not be found.
537 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data.
538 DataSize is updated with the size required for
539 the specified variable.
540 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.
541 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
547 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
548 IN CONST CHAR16
*VariableName
,
549 IN CONST EFI_GUID
*VariableGuid
,
550 OUT UINT32
*Attributes
,
551 IN OUT UINTN
*DataSize
,
555 VARIABLE_POINTER_TRACK Variable
;
558 CONST EFI_PEI_SERVICES
**PeiServices
;
560 PeiServices
= GetPeiServicesTablePointer ();
561 if (VariableName
== NULL
|| VariableGuid
== NULL
|| DataSize
== NULL
) {
562 return EFI_INVALID_PARAMETER
;
566 // Check if this is recovery boot path.
567 // If yes, the content of variable area is not reliable. Therefore we directly
568 // return EFI_NOT_FOUND.
570 if (IsInRecoveryMode(PeiServices
)) {
571 return EFI_NOT_FOUND
;
575 // Find existing variable
577 Status
= FindVariable (PeiServices
, VariableName
, VariableGuid
, &Variable
);
578 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
584 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
585 if (*DataSize
>= VarDataSize
) {
587 return EFI_INVALID_PARAMETER
;
590 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
592 if (Attributes
!= NULL
) {
593 *Attributes
= Variable
.CurrPtr
->Attributes
;
596 *DataSize
= VarDataSize
;
599 *DataSize
= VarDataSize
;
600 return EFI_BUFFER_TOO_SMALL
;
605 Return the next variable name and GUID.
607 This function is called multiple times to retrieve the VariableName
608 and VariableGuid of all variables currently available in the system.
609 On each call, the previous results are passed into the interface,
610 and, on return, the interface returns the data for the next
611 interface. When the entire variable list has been returned,
612 EFI_NOT_FOUND is returned.
614 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
616 @param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.
617 On return, the size of the variable name buffer.
618 @param VariableName On entry, a pointer to a null-terminated string that is the variable's name.
619 On return, points to the next variable's null-terminated name string.
620 @param VariableGuid On entry, a pointer to an EFI_GUID that is the variable's GUID.
621 On return, a pointer to the next variable's GUID.
623 @retval EFI_SUCCESS The variable was read successfully.
624 @retval EFI_NOT_FOUND The variable could not be found.
625 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting
626 data. VariableNameSize is updated with the size
627 required for the specified variable.
628 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or
629 VariableNameSize is NULL.
630 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
635 PeiGetNextVariableName (
636 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
637 IN OUT UINTN
*VariableNameSize
,
638 IN OUT CHAR16
*VariableName
,
639 IN OUT EFI_GUID
*VariableGuid
642 VARIABLE_POINTER_TRACK Variable
;
645 CONST EFI_PEI_SERVICES
**PeiServices
;
647 PeiServices
= GetPeiServicesTablePointer ();
648 if (VariableName
== NULL
|| VariableGuid
== NULL
|| VariableNameSize
== NULL
) {
649 return EFI_INVALID_PARAMETER
;
653 // Check if this is recovery boot path.
654 // If yes, the content of variable area is not reliable. Therefore we directly
655 // return EFI_NOT_FOUND.
657 if (IsInRecoveryMode(PeiServices
)) {
658 return EFI_NOT_FOUND
;
661 Status
= FindVariable (PeiServices
, VariableName
, VariableGuid
, &Variable
);
662 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
666 if (VariableName
[0] != 0) {
668 // If variable name is not NULL, get next variable
670 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
673 while (!(Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
)) {
674 if (IsValidVariableHeader (Variable
.CurrPtr
)) {
675 if (Variable
.CurrPtr
->State
== VAR_ADDED
) {
676 ASSERT (NameSizeOfVariable (Variable
.CurrPtr
) != 0);
678 VarNameSize
= (UINTN
) NameSizeOfVariable (Variable
.CurrPtr
);
679 if (VarNameSize
<= *VariableNameSize
) {
680 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
682 CopyMem (VariableGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
684 Status
= EFI_SUCCESS
;
686 Status
= EFI_BUFFER_TOO_SMALL
;
689 *VariableNameSize
= VarNameSize
;
695 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
702 return EFI_NOT_FOUND
;