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
,
36 Check if it runs in Recovery mode.
38 @param PeiServices General purpose services available to every PEIM.
40 @retval TRUE It's in Recovery mode.
41 @retval FALSE It's not in Recovery mode.
46 IN CONST EFI_PEI_SERVICES
**PeiServices
50 EFI_BOOT_MODE BootMode
;
52 Status
= (*PeiServices
)->GetBootMode (PeiServices
, &BootMode
);
53 ASSERT_EFI_ERROR (Status
);
55 if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
62 Provide the functionality of the variable services.
64 @param FileHandle Handle of the file being invoked.
65 Type EFI_PEI_FILE_HANDLE is defined in FfsFindNextFile().
66 @param PeiServices General purpose services available to every PEIM.
68 @retval EFI_SUCCESS If the interface could be successfully installed
69 @retval Others Returned from PeiServicesInstallPpi()
74 PeimInitializeVariableServices (
75 IN EFI_PEI_FILE_HANDLE FileHandle
,
76 IN CONST EFI_PEI_SERVICES
**PeiServices
79 return PeiServicesInstallPpi (&mPpiListVariable
);
84 Gets the pointer to the first variable header in given variable store area.
86 @param VarStoreHeader Pointer to the Variable Store Header.
88 @return Pointer to the first variable header
93 IN VARIABLE_STORE_HEADER
*VarStoreHeader
97 // The end of variable store
99 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
104 This code gets the pointer to the last variable memory pointer byte.
106 @param VarStoreHeader Pointer to the Variable Store Header.
108 @return VARIABLE_HEADER* pointer to last unavailable Variable Header.
113 IN VARIABLE_STORE_HEADER
*VarStoreHeader
117 // The end of variable store
119 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
124 This code checks if variable header is valid or not.
126 @param Variable Pointer to the Variable Header.
128 @retval TRUE Variable header is valid.
129 @retval FALSE Variable header is not valid.
133 IsValidVariableHeader (
134 IN VARIABLE_HEADER
*Variable
137 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
146 This code gets the size of name of variable.
148 @param Variable Pointer to the Variable Header.
150 @return Size of variable in bytes in type UINTN.
155 IN VARIABLE_HEADER
*Variable
158 if (Variable
->State
== (UINT8
) (-1) ||
159 Variable
->DataSize
== (UINT32
) (-1) ||
160 Variable
->NameSize
== (UINT32
) (-1) ||
161 Variable
->Attributes
== (UINT32
) (-1)) {
164 return (UINTN
) Variable
->NameSize
;
169 This code gets the size of data of variable.
171 @param Variable Pointer to the Variable Header.
173 @return Size of variable in bytes in type UINTN.
178 IN VARIABLE_HEADER
*Variable
181 if (Variable
->State
== (UINT8
) (-1) ||
182 Variable
->DataSize
== (UINT32
) (-1) ||
183 Variable
->NameSize
== (UINT32
) (-1) ||
184 Variable
->Attributes
== (UINT32
) (-1)) {
187 return (UINTN
) Variable
->DataSize
;
191 This code gets the pointer to the variable name.
193 @param Variable Pointer to the Variable Header.
195 @return A CHAR16* pointer to Variable Name.
200 IN VARIABLE_HEADER
*Variable
204 return (CHAR16
*) (Variable
+ 1);
209 This code gets the pointer to the variable data.
211 @param Variable Pointer to the Variable Header.
213 @return A UINT8* pointer to Variable Data.
218 IN VARIABLE_HEADER
*Variable
224 // Be careful about pad size for alignment
226 Value
= (UINTN
) GetVariableNamePtr (Variable
);
227 Value
+= NameSizeOfVariable (Variable
);
228 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
230 return (UINT8
*) Value
;
235 This code gets the pointer to the next variable header.
237 @param Variable Pointer to the Variable Header.
239 @return A VARIABLE_HEADER* pointer to next variable header.
244 IN VARIABLE_HEADER
*Variable
249 if (!IsValidVariableHeader (Variable
)) {
253 Value
= (UINTN
) GetVariableDataPtr (Variable
);
254 Value
+= DataSizeOfVariable (Variable
);
255 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
258 // Be careful about pad size for alignment
260 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
264 This code gets the pointer to the variable name.
266 @param VarStoreHeader Pointer to the Variable Store Header.
268 @retval EfiRaw Variable store is raw
269 @retval EfiValid Variable store is valid
270 @retval EfiInvalid Variable store is invalid
273 VARIABLE_STORE_STATUS
274 GetVariableStoreStatus (
275 IN VARIABLE_STORE_HEADER
*VarStoreHeader
279 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
) &&
280 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
281 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
287 if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
288 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
289 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
290 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
291 VarStoreHeader
->Size
== 0xffffffff &&
292 VarStoreHeader
->Format
== 0xff &&
293 VarStoreHeader
->State
== 0xff
304 This function compares a variable with variable entries in database.
306 @param Variable Pointer to the variable in our database
307 @param VariableName Name of the variable to compare to 'Variable'
308 @param VendorGuid GUID of the variable to compare to 'Variable'
309 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
311 @retval EFI_SUCCESS Found match variable
312 @retval EFI_NOT_FOUND Variable not found
316 CompareWithValidVariable (
317 IN VARIABLE_HEADER
*Variable
,
318 IN CONST CHAR16
*VariableName
,
319 IN CONST EFI_GUID
*VendorGuid
,
320 OUT VARIABLE_POINTER_TRACK
*PtrTrack
325 if (VariableName
[0] == 0) {
326 PtrTrack
->CurrPtr
= Variable
;
330 // Don't use CompareGuid function here for performance reasons.
331 // Instead we compare the GUID a UINT32 at a time and branch
332 // on the first failed comparison.
334 if ((((INT32
*) VendorGuid
)[0] == ((INT32
*) &Variable
->VendorGuid
)[0]) &&
335 (((INT32
*) VendorGuid
)[1] == ((INT32
*) &Variable
->VendorGuid
)[1]) &&
336 (((INT32
*) VendorGuid
)[2] == ((INT32
*) &Variable
->VendorGuid
)[2]) &&
337 (((INT32
*) VendorGuid
)[3] == ((INT32
*) &Variable
->VendorGuid
)[3])
339 ASSERT (NameSizeOfVariable (Variable
) != 0);
340 Point
= (VOID
*) GetVariableNamePtr (Variable
);
341 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (Variable
)) == 0) {
342 PtrTrack
->CurrPtr
= Variable
;
348 return EFI_NOT_FOUND
;
353 This code finds variable in storage blocks (Non-Volatile).
355 @param PeiServices General purpose services available to every PEIM.
356 @param VariableName Name of the variable to be found
357 @param VendorGuid Vendor GUID to be found.
358 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
360 @retval EFI_SUCCESS Variable found successfully
361 @retval EFI_NOT_FOUND Variable not found
362 @retval EFI_INVALID_PARAMETER Invalid variable name
367 IN CONST EFI_PEI_SERVICES
**PeiServices
,
368 IN CONST CHAR16
*VariableName
,
369 IN CONST EFI_GUID
*VendorGuid
,
370 OUT VARIABLE_POINTER_TRACK
*PtrTrack
373 EFI_HOB_GUID_TYPE
*GuidHob
;
374 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
375 VARIABLE_HEADER
*Variable
;
376 VARIABLE_HEADER
*LastVariable
;
377 VARIABLE_HEADER
*MaxIndex
;
378 VARIABLE_INDEX_TABLE
*IndexTable
;
384 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
385 return EFI_INVALID_PARAMETER
;
388 // No Variable Address equals zero, so 0 as initial value is safe.
393 GuidHob
= GetFirstGuidHob (&gEfiVariableIndexTableGuid
);
394 if (GuidHob
== NULL
) {
396 // If it's the first time to access variable region in flash, create a guid hob to record
397 // VAR_ADDED type variable info.
398 // Note that as the resource of PEI phase is limited, only store the number of
399 // VARIABLE_INDEX_TABLE_VOLUME of VAR_ADDED type variables to reduce access time.
401 IndexTable
= BuildGuidHob (&gEfiVariableIndexTableGuid
, sizeof (VARIABLE_INDEX_TABLE
));
402 IndexTable
->Length
= 0;
403 IndexTable
->StartPtr
= NULL
;
404 IndexTable
->EndPtr
= NULL
;
405 IndexTable
->GoneThrough
= 0;
407 IndexTable
= GET_GUID_HOB_DATA (GuidHob
);
408 for (Offset
= 0, Count
= 0; Count
< IndexTable
->Length
; Count
++) {
410 // traverse the variable info list to look for varible.
411 // The IndexTable->Index[Count] records the distance of two neighbouring VAR_ADDED type variables.
413 ASSERT (Count
< VARIABLE_INDEX_TABLE_VOLUME
);
414 Offset
+= IndexTable
->Index
[Count
];
415 MaxIndex
= (VARIABLE_HEADER
*)((CHAR8
*)(IndexTable
->StartPtr
) + Offset
);
416 if (CompareWithValidVariable (MaxIndex
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
417 PtrTrack
->StartPtr
= IndexTable
->StartPtr
;
418 PtrTrack
->EndPtr
= IndexTable
->EndPtr
;
424 if (IndexTable
->GoneThrough
!= 0) {
425 return EFI_NOT_FOUND
;
429 // If not found in HOB, then let's start from the MaxIndex we've found.
431 if (MaxIndex
!= NULL
) {
432 Variable
= GetNextVariablePtr (MaxIndex
);
433 LastVariable
= MaxIndex
;
435 if ((IndexTable
->StartPtr
!= NULL
) || (IndexTable
->EndPtr
!= NULL
)) {
436 Variable
= IndexTable
->StartPtr
;
438 VariableBase
= (UINT8
*) (UINTN
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
439 if (VariableBase
== NULL
) {
440 VariableBase
= (UINT8
*) (UINTN
) PcdGet32 (PcdFlashNvStorageVariableBase
);
443 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (VariableBase
+ \
444 ((EFI_FIRMWARE_VOLUME_HEADER
*) (VariableBase
)) -> HeaderLength
);
446 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
447 return EFI_UNSUPPORTED
;
450 if (~VariableStoreHeader
->Size
== 0) {
451 return EFI_NOT_FOUND
;
454 // Find the variable by walk through non-volatile variable store
456 IndexTable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
457 IndexTable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
460 // Start Pointers for the variable.
461 // Actual Data Pointer where data can be written.
463 Variable
= IndexTable
->StartPtr
;
466 LastVariable
= IndexTable
->StartPtr
;
469 // Find the variable by walk through non-volatile variable store
471 PtrTrack
->StartPtr
= IndexTable
->StartPtr
;
472 PtrTrack
->EndPtr
= IndexTable
->EndPtr
;
474 while ((Variable
< IndexTable
->EndPtr
) && IsValidVariableHeader (Variable
)) {
475 if (Variable
->State
== VAR_ADDED
) {
477 // Record Variable in VariableIndex HOB
479 if (IndexTable
->Length
< VARIABLE_INDEX_TABLE_VOLUME
&& !StopRecord
) {
480 Offset
= (UINT32
)((UINTN
)Variable
- (UINTN
)LastVariable
);
482 // The distance of two neighbouring VAR_ADDED variable is larger than 2^16,
483 // which is beyond the allowable scope(UINT16) of record. In such case, need not to
484 // record the subsequent VAR_ADDED type variables again.
486 if ((Offset
& 0xFFFF0000UL
) != 0) {
491 IndexTable
->Index
[IndexTable
->Length
++] = (UINT16
) Offset
;
493 LastVariable
= Variable
;
496 if (CompareWithValidVariable (Variable
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
501 Variable
= GetNextVariablePtr (Variable
);
504 // If gone through the VariableStore, that means we never find in Firmware any more.
506 if ((IndexTable
->Length
< VARIABLE_INDEX_TABLE_VOLUME
) && (!StopRecord
)) {
507 IndexTable
->GoneThrough
= 1;
510 PtrTrack
->CurrPtr
= NULL
;
512 return EFI_NOT_FOUND
;
516 This service retrieves a variable's value using its name and GUID.
518 Read the specified variable from the UEFI variable store. If the Data
519 buffer is too small to hold the contents of the variable, the error
520 EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer
521 size to obtain the data.
523 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
524 @param VariableName A pointer to a null-terminated string that is the variable's name.
525 @param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of
526 VariableGuid and VariableName must be unique.
527 @param Attributes If non-NULL, on return, points to the variable's attributes.
528 @param DataSize On entry, points to the size in bytes of the Data buffer.
529 On return, points to the size of the data returned in Data.
530 @param Data Points to the buffer which will hold the returned variable value.
532 @retval EFI_SUCCESS The variable was read successfully.
533 @retval EFI_NOT_FOUND The variable could not be found.
534 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data.
535 DataSize is updated with the size required for
536 the specified variable.
537 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.
538 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
544 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
545 IN CONST CHAR16
*VariableName
,
546 IN CONST EFI_GUID
*VariableGuid
,
547 OUT UINT32
*Attributes
,
548 IN OUT UINTN
*DataSize
,
552 VARIABLE_POINTER_TRACK Variable
;
555 CONST EFI_PEI_SERVICES
**PeiServices
;
557 PeiServices
= GetPeiServicesTablePointer ();
558 if (VariableName
== NULL
|| VariableGuid
== NULL
|| DataSize
== NULL
) {
559 return EFI_INVALID_PARAMETER
;
563 // Check if this is recovery boot path.
564 // If yes, the content of variable area is not reliable. Therefore we directly
565 // return EFI_NOT_FOUND.
567 if (IsInRecoveryMode(PeiServices
)) {
568 return EFI_NOT_FOUND
;
572 // Find existing variable
574 Status
= FindVariable (PeiServices
, VariableName
, VariableGuid
, &Variable
);
575 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
581 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
582 if (*DataSize
>= VarDataSize
) {
584 return EFI_INVALID_PARAMETER
;
587 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
589 if (Attributes
!= NULL
) {
590 *Attributes
= Variable
.CurrPtr
->Attributes
;
593 *DataSize
= VarDataSize
;
596 *DataSize
= VarDataSize
;
597 return EFI_BUFFER_TOO_SMALL
;
602 Return the next variable name and GUID.
604 This function is called multiple times to retrieve the VariableName
605 and VariableGuid of all variables currently available in the system.
606 On each call, the previous results are passed into the interface,
607 and, on return, the interface returns the data for the next
608 interface. When the entire variable list has been returned,
609 EFI_NOT_FOUND is returned.
611 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
613 @param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.
614 On return, the size of the variable name buffer.
615 @param VariableName On entry, a pointer to a null-terminated string that is the variable's name.
616 On return, points to the next variable's null-terminated name string.
617 @param VariableGuid On entry, a pointer to an EFI_GUID that is the variable's GUID.
618 On return, a pointer to the next variable's GUID.
620 @retval EFI_SUCCESS The variable was read successfully.
621 @retval EFI_NOT_FOUND The variable could not be found.
622 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting
623 data. VariableNameSize is updated with the size
624 required for the specified variable.
625 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or
626 VariableNameSize is NULL.
627 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
632 PeiGetNextVariableName (
633 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
634 IN OUT UINTN
*VariableNameSize
,
635 IN OUT CHAR16
*VariableName
,
636 IN OUT EFI_GUID
*VariableGuid
639 VARIABLE_POINTER_TRACK Variable
;
642 CONST EFI_PEI_SERVICES
**PeiServices
;
644 PeiServices
= GetPeiServicesTablePointer ();
645 if (VariableName
== NULL
|| VariableGuid
== NULL
|| VariableNameSize
== NULL
) {
646 return EFI_INVALID_PARAMETER
;
650 // Check if this is recovery boot path.
651 // If yes, the content of variable area is not reliable. Therefore we directly
652 // return EFI_NOT_FOUND.
654 if (IsInRecoveryMode(PeiServices
)) {
655 return EFI_NOT_FOUND
;
658 Status
= FindVariable (PeiServices
, VariableName
, VariableGuid
, &Variable
);
659 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
663 if (VariableName
[0] != 0) {
665 // If variable name is not NULL, get next variable
667 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
670 while (!(Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
)) {
671 if (IsValidVariableHeader (Variable
.CurrPtr
)) {
672 if (Variable
.CurrPtr
->State
== VAR_ADDED
) {
673 ASSERT (NameSizeOfVariable (Variable
.CurrPtr
) != 0);
675 VarNameSize
= (UINTN
) NameSizeOfVariable (Variable
.CurrPtr
);
676 if (VarNameSize
<= *VariableNameSize
) {
677 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
679 CopyMem (VariableGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
681 Status
= EFI_SUCCESS
;
683 Status
= EFI_BUFFER_TOO_SMALL
;
686 *VariableNameSize
= VarNameSize
;
692 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
699 return EFI_NOT_FOUND
;