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
,
37 Check if it runs in Recovery mode.
39 @param PeiServices General purpose services available to every PEIM.
41 @retval TRUE It's in Recovery mode.
42 @retval FALSE It's not in Recovery mode.
47 IN CONST EFI_PEI_SERVICES
**PeiServices
51 EFI_BOOT_MODE BootMode
;
53 Status
= (*PeiServices
)->GetBootMode (PeiServices
, &BootMode
);
54 ASSERT_EFI_ERROR (Status
);
56 if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
63 Provide the functionality of the variable services.
65 @param FileHandle Handle of the file being invoked.
66 Type EFI_PEI_FILE_HANDLE is defined in FfsFindNextFile().
67 @param PeiServices General purpose services available to every PEIM.
69 @retval EFI_SUCCESS If the interface could be successfully installed
70 @retval Others Returned from PeiServicesInstallPpi()
75 PeimInitializeVariableServices (
76 IN EFI_PEI_FILE_HANDLE FileHandle
,
77 IN CONST EFI_PEI_SERVICES
**PeiServices
80 return PeiServicesInstallPpi (&mPpiListVariable
);
85 Gets the pointer to the first variable header in given variable store area.
87 @param VarStoreHeader Pointer to the Variable Store Header.
89 @return Pointer to the first variable header
94 IN VARIABLE_STORE_HEADER
*VarStoreHeader
98 // The end of variable store
100 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
105 This code gets the pointer to the last variable memory pointer byte.
107 @param VarStoreHeader Pointer to the Variable Store Header.
109 @return VARIABLE_HEADER* pointer to last unavailable Variable Header.
114 IN VARIABLE_STORE_HEADER
*VarStoreHeader
118 // The end of variable store
120 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
125 This code checks if variable header is valid or not.
127 @param Variable Pointer to the Variable Header.
129 @retval TRUE Variable header is valid.
130 @retval FALSE Variable header is not valid.
134 IsValidVariableHeader (
135 IN VARIABLE_HEADER
*Variable
138 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
147 This code gets the size of name of variable.
149 @param Variable Pointer to the Variable Header.
151 @return Size of variable in bytes in type UINTN.
156 IN VARIABLE_HEADER
*Variable
159 if (Variable
->State
== (UINT8
) (-1) ||
160 Variable
->DataSize
== (UINT32
) (-1) ||
161 Variable
->NameSize
== (UINT32
) (-1) ||
162 Variable
->Attributes
== (UINT32
) (-1)) {
165 return (UINTN
) Variable
->NameSize
;
170 This code gets the size of data of variable.
172 @param Variable Pointer to the Variable Header.
174 @return Size of variable in bytes in type UINTN.
179 IN VARIABLE_HEADER
*Variable
182 if (Variable
->State
== (UINT8
) (-1) ||
183 Variable
->DataSize
== (UINT32
) (-1) ||
184 Variable
->NameSize
== (UINT32
) (-1) ||
185 Variable
->Attributes
== (UINT32
) (-1)) {
188 return (UINTN
) Variable
->DataSize
;
192 This code gets the pointer to the variable name.
194 @param Variable Pointer to the Variable Header.
196 @return A CHAR16* pointer to Variable Name.
201 IN VARIABLE_HEADER
*Variable
205 return (CHAR16
*) (Variable
+ 1);
210 This code gets the pointer to the variable data.
212 @param Variable Pointer to the Variable Header.
214 @return A UINT8* pointer to Variable Data.
219 IN VARIABLE_HEADER
*Variable
225 // Be careful about pad size for alignment
227 Value
= (UINTN
) GetVariableNamePtr (Variable
);
228 Value
+= NameSizeOfVariable (Variable
);
229 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
231 return (UINT8
*) Value
;
236 This code gets the pointer to the next variable header.
238 @param Variable Pointer to the Variable Header.
240 @return A VARIABLE_HEADER* pointer to next variable header.
245 IN VARIABLE_HEADER
*Variable
250 if (!IsValidVariableHeader (Variable
)) {
254 Value
= (UINTN
) GetVariableDataPtr (Variable
);
255 Value
+= DataSizeOfVariable (Variable
);
256 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
259 // Be careful about pad size for alignment
261 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
265 This code gets the pointer to the variable name.
267 @param VarStoreHeader Pointer to the Variable Store Header.
269 @retval EfiRaw Variable store is raw
270 @retval EfiValid Variable store is valid
271 @retval EfiInvalid Variable store is invalid
274 VARIABLE_STORE_STATUS
275 GetVariableStoreStatus (
276 IN VARIABLE_STORE_HEADER
*VarStoreHeader
280 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiVariableGuid
) &&
281 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
282 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
288 if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
289 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
290 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
291 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
292 VarStoreHeader
->Size
== 0xffffffff &&
293 VarStoreHeader
->Format
== 0xff &&
294 VarStoreHeader
->State
== 0xff
305 This function compares a variable with variable entries in database.
307 @param Variable Pointer to the variable in our database
308 @param VariableName Name of the variable to compare to 'Variable'
309 @param VendorGuid GUID of the variable to compare to 'Variable'
310 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
312 @retval EFI_SUCCESS Found match variable
313 @retval EFI_NOT_FOUND Variable not found
317 CompareWithValidVariable (
318 IN VARIABLE_HEADER
*Variable
,
319 IN CONST CHAR16
*VariableName
,
320 IN CONST EFI_GUID
*VendorGuid
,
321 OUT VARIABLE_POINTER_TRACK
*PtrTrack
326 if (VariableName
[0] == 0) {
327 PtrTrack
->CurrPtr
= Variable
;
331 // Don't use CompareGuid function here for performance reasons.
332 // Instead we compare the GUID a UINT32 at a time and branch
333 // on the first failed comparison.
335 if ((((INT32
*) VendorGuid
)[0] == ((INT32
*) &Variable
->VendorGuid
)[0]) &&
336 (((INT32
*) VendorGuid
)[1] == ((INT32
*) &Variable
->VendorGuid
)[1]) &&
337 (((INT32
*) VendorGuid
)[2] == ((INT32
*) &Variable
->VendorGuid
)[2]) &&
338 (((INT32
*) VendorGuid
)[3] == ((INT32
*) &Variable
->VendorGuid
)[3])
340 ASSERT (NameSizeOfVariable (Variable
) != 0);
341 Point
= (VOID
*) GetVariableNamePtr (Variable
);
342 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (Variable
)) == 0) {
343 PtrTrack
->CurrPtr
= Variable
;
349 return EFI_NOT_FOUND
;
354 This code finds variable in storage blocks (Non-Volatile).
356 @param PeiServices General purpose services available to every PEIM.
357 @param VariableName Name of the variable to be found
358 @param VendorGuid Vendor GUID to be found.
359 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
361 @retval EFI_SUCCESS Variable found successfully
362 @retval EFI_NOT_FOUND Variable not found
363 @retval EFI_INVALID_PARAMETER Invalid variable name
368 IN CONST EFI_PEI_SERVICES
**PeiServices
,
369 IN CONST CHAR16
*VariableName
,
370 IN CONST EFI_GUID
*VendorGuid
,
371 OUT VARIABLE_POINTER_TRACK
*PtrTrack
374 EFI_HOB_GUID_TYPE
*GuidHob
;
375 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
376 VARIABLE_HEADER
*Variable
;
377 VARIABLE_HEADER
*LastVariable
;
378 VARIABLE_HEADER
*MaxIndex
;
379 VARIABLE_INDEX_TABLE
*IndexTable
;
385 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
386 return EFI_INVALID_PARAMETER
;
389 // No Variable Address equals zero, so 0 as initial value is safe.
394 GuidHob
= GetFirstGuidHob (&gEfiVariableIndexTableGuid
);
395 if (GuidHob
== NULL
) {
397 // If it's the first time to access variable region in flash, create a guid hob to record
398 // VAR_ADDED type variable info.
399 // Note that as the resource of PEI phase is limited, only store the number of
400 // VARIABLE_INDEX_TABLE_VOLUME of VAR_ADDED type variables to reduce access time.
402 IndexTable
= BuildGuidHob (&gEfiVariableIndexTableGuid
, sizeof (VARIABLE_INDEX_TABLE
));
403 IndexTable
->Length
= 0;
404 IndexTable
->StartPtr
= NULL
;
405 IndexTable
->EndPtr
= NULL
;
406 IndexTable
->GoneThrough
= 0;
408 IndexTable
= GET_GUID_HOB_DATA (GuidHob
);
409 for (Offset
= 0, Count
= 0; Count
< IndexTable
->Length
; Count
++) {
411 // traverse the variable info list to look for varible.
412 // The IndexTable->Index[Count] records the distance of two neighbouring VAR_ADDED type variables.
414 ASSERT (Count
< VARIABLE_INDEX_TABLE_VOLUME
);
415 Offset
+= IndexTable
->Index
[Count
];
416 MaxIndex
= (VARIABLE_HEADER
*)((CHAR8
*)(IndexTable
->StartPtr
) + Offset
);
417 if (CompareWithValidVariable (MaxIndex
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
418 PtrTrack
->StartPtr
= IndexTable
->StartPtr
;
419 PtrTrack
->EndPtr
= IndexTable
->EndPtr
;
425 if (IndexTable
->GoneThrough
!= 0) {
426 return EFI_NOT_FOUND
;
430 // If not found in HOB, then let's start from the MaxIndex we've found.
432 if (MaxIndex
!= NULL
) {
433 Variable
= GetNextVariablePtr (MaxIndex
);
434 LastVariable
= MaxIndex
;
436 if ((IndexTable
->StartPtr
!= NULL
) || (IndexTable
->EndPtr
!= NULL
)) {
437 Variable
= IndexTable
->StartPtr
;
439 VariableBase
= (UINT8
*) (UINTN
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
440 if (VariableBase
== NULL
) {
441 VariableBase
= (UINT8
*) (UINTN
) PcdGet32 (PcdFlashNvStorageVariableBase
);
444 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (VariableBase
+ \
445 ((EFI_FIRMWARE_VOLUME_HEADER
*) (VariableBase
)) -> HeaderLength
);
447 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
448 return EFI_UNSUPPORTED
;
451 if (~VariableStoreHeader
->Size
== 0) {
452 return EFI_NOT_FOUND
;
455 // Find the variable by walk through non-volatile variable store
457 IndexTable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
458 IndexTable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
461 // Start Pointers for the variable.
462 // Actual Data Pointer where data can be written.
464 Variable
= IndexTable
->StartPtr
;
467 LastVariable
= IndexTable
->StartPtr
;
470 // Find the variable by walk through non-volatile variable store
472 PtrTrack
->StartPtr
= IndexTable
->StartPtr
;
473 PtrTrack
->EndPtr
= IndexTable
->EndPtr
;
475 while ((Variable
< IndexTable
->EndPtr
) && IsValidVariableHeader (Variable
)) {
476 if (Variable
->State
== VAR_ADDED
) {
478 // Record Variable in VariableIndex HOB
480 if (IndexTable
->Length
< VARIABLE_INDEX_TABLE_VOLUME
&& !StopRecord
) {
481 Offset
= (UINT32
)((UINTN
)Variable
- (UINTN
)LastVariable
);
483 // The distance of two neighbouring VAR_ADDED variable is larger than 2^16,
484 // which is beyond the allowable scope(UINT16) of record. In such case, need not to
485 // record the subsequent VAR_ADDED type variables again.
487 if ((Offset
& 0xFFFF0000UL
) != 0) {
492 IndexTable
->Index
[IndexTable
->Length
++] = (UINT16
) Offset
;
494 LastVariable
= Variable
;
497 if (CompareWithValidVariable (Variable
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
502 Variable
= GetNextVariablePtr (Variable
);
505 // If gone through the VariableStore, that means we never find in Firmware any more.
507 if ((IndexTable
->Length
< VARIABLE_INDEX_TABLE_VOLUME
) && (!StopRecord
)) {
508 IndexTable
->GoneThrough
= 1;
511 PtrTrack
->CurrPtr
= NULL
;
513 return EFI_NOT_FOUND
;
517 This service retrieves a variable's value using its name and GUID.
519 Read the specified variable from the UEFI variable store. If the Data
520 buffer is too small to hold the contents of the variable, the error
521 EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer
522 size to obtain the data.
524 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
525 @param VariableName A pointer to a null-terminated string that is the variable's name.
526 @param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of
527 VariableGuid and VariableName must be unique.
528 @param Attributes If non-NULL, on return, points to the variable's attributes.
529 @param DataSize On entry, points to the size in bytes of the Data buffer.
530 On return, points to the size of the data returned in Data.
531 @param Data Points to the buffer which will hold the returned variable value.
533 @retval EFI_SUCCESS The variable was read successfully.
534 @retval EFI_NOT_FOUND The variable could not be found.
535 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data.
536 DataSize is updated with the size required for
537 the specified variable.
538 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.
539 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
545 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
546 IN CONST CHAR16
*VariableName
,
547 IN CONST EFI_GUID
*VariableGuid
,
548 OUT UINT32
*Attributes
,
549 IN OUT UINTN
*DataSize
,
553 VARIABLE_POINTER_TRACK Variable
;
556 CONST EFI_PEI_SERVICES
**PeiServices
;
558 PeiServices
= GetPeiServicesTablePointer ();
559 if (VariableName
== NULL
|| VariableGuid
== NULL
|| DataSize
== NULL
) {
560 return EFI_INVALID_PARAMETER
;
564 // Check if this is recovery boot path.
565 // If yes, the content of variable area is not reliable. Therefore we directly
566 // return EFI_NOT_FOUND.
568 if (IsInRecoveryMode(PeiServices
)) {
569 return EFI_NOT_FOUND
;
573 // Find existing variable
575 Status
= FindVariable (PeiServices
, VariableName
, VariableGuid
, &Variable
);
576 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
582 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
583 if (*DataSize
>= VarDataSize
) {
585 return EFI_INVALID_PARAMETER
;
588 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
590 if (Attributes
!= NULL
) {
591 *Attributes
= Variable
.CurrPtr
->Attributes
;
594 *DataSize
= VarDataSize
;
597 *DataSize
= VarDataSize
;
598 return EFI_BUFFER_TOO_SMALL
;
603 Return the next variable name and GUID.
605 This function is called multiple times to retrieve the VariableName
606 and VariableGuid of all variables currently available in the system.
607 On each call, the previous results are passed into the interface,
608 and, on return, the interface returns the data for the next
609 interface. When the entire variable list has been returned,
610 EFI_NOT_FOUND is returned.
612 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
614 @param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.
615 On return, the size of the variable name buffer.
616 @param VariableName On entry, a pointer to a null-terminated string that is the variable's name.
617 On return, points to the next variable's null-terminated name string.
618 @param VariableGuid On entry, a pointer to an EFI_GUID that is the variable's GUID.
619 On return, a pointer to the next variable's GUID.
621 @retval EFI_SUCCESS The variable was read successfully.
622 @retval EFI_NOT_FOUND The variable could not be found.
623 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting
624 data. VariableNameSize is updated with the size
625 required for the specified variable.
626 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or
627 VariableNameSize is NULL.
628 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
633 PeiGetNextVariableName (
634 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
635 IN OUT UINTN
*VariableNameSize
,
636 IN OUT CHAR16
*VariableName
,
637 IN OUT EFI_GUID
*VariableGuid
640 VARIABLE_POINTER_TRACK Variable
;
643 CONST EFI_PEI_SERVICES
**PeiServices
;
645 PeiServices
= GetPeiServicesTablePointer ();
646 if (VariableName
== NULL
|| VariableGuid
== NULL
|| VariableNameSize
== NULL
) {
647 return EFI_INVALID_PARAMETER
;
651 // Check if this is recovery boot path.
652 // If yes, the content of variable area is not reliable. Therefore we directly
653 // return EFI_NOT_FOUND.
655 if (IsInRecoveryMode(PeiServices
)) {
656 return EFI_NOT_FOUND
;
659 Status
= FindVariable (PeiServices
, VariableName
, VariableGuid
, &Variable
);
660 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
664 if (VariableName
[0] != 0) {
666 // If variable name is not NULL, get next variable
668 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
671 while (!(Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
)) {
672 if (IsValidVariableHeader (Variable
.CurrPtr
)) {
673 if (Variable
.CurrPtr
->State
== VAR_ADDED
) {
674 ASSERT (NameSizeOfVariable (Variable
.CurrPtr
) != 0);
676 VarNameSize
= (UINTN
) NameSizeOfVariable (Variable
.CurrPtr
);
677 if (VarNameSize
<= *VariableNameSize
) {
678 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
680 CopyMem (VariableGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
682 Status
= EFI_SUCCESS
;
684 Status
= EFI_BUFFER_TOO_SMALL
;
687 *VariableNameSize
= VarNameSize
;
693 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
700 return EFI_NOT_FOUND
;