3 Implement ReadOnly Variable Services required by PEIM and install
4 PI ReadOnly Varaiable2 PPI. These services operates the non volatile storage space.
6 Copyright (c) 2006 - 2008 Intel Corporation. <BR>
7 All rights reserved. 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
57 // Publish the variable capability to other modules
59 return PeiServicesInstallPpi (&mPpiListVariable
);
65 This code gets the pointer to the first variable memory pointer byte.
67 @param VarStoreHeader Pointer to the Variable Store Header.
69 @return VARIABLE_HEADER* pointer to last unavailable Variable Header.
74 IN VARIABLE_STORE_HEADER
*VarStoreHeader
78 // The end of variable store
80 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
85 This code gets the pointer to the last variable memory pointer byte.
87 @param VarStoreHeader Pointer to the Variable Store Header.
89 @return VARIABLE_HEADER* pointer to last unavailable Variable Header.
94 IN VARIABLE_STORE_HEADER
*VarStoreHeader
98 // The end of variable store
100 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
105 This code checks if variable header is valid or not.
107 @param Variable Pointer to the Variable Header.
109 @retval TRUE Variable header is valid.
110 @retval FALSE Variable header is not valid.
114 IsValidVariableHeader (
115 IN VARIABLE_HEADER
*Variable
118 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
127 This code gets the size of name of variable.
129 @param Variable Pointer to the Variable Header.
131 @return Size of variable in bytes in type UINTN.
136 IN VARIABLE_HEADER
*Variable
139 if (Variable
->State
== (UINT8
) (-1) ||
140 Variable
->DataSize
== (UINT32
) -1 ||
141 Variable
->NameSize
== (UINT32
) -1 ||
142 Variable
->Attributes
== (UINT32
) -1) {
145 return (UINTN
) Variable
->NameSize
;
150 This code gets the size of data of variable.
152 @param Variable Pointer to the Variable Header.
154 @return Size of variable in bytes in type UINTN.
159 IN VARIABLE_HEADER
*Variable
162 if (Variable
->State
== (UINT8
) -1 ||
163 Variable
->DataSize
== (UINT32
) -1 ||
164 Variable
->NameSize
== (UINT32
) -1 ||
165 Variable
->Attributes
== (UINT32
) -1) {
168 return (UINTN
) Variable
->DataSize
;
172 This code gets the pointer to the variable name.
174 @param Variable Pointer to the Variable Header.
176 @return A CHAR16* pointer to Variable Name.
181 IN VARIABLE_HEADER
*Variable
185 return (CHAR16
*) (Variable
+ 1);
190 This code gets the pointer to the variable data.
192 @param Variable Pointer to the Variable Header.
194 @return A UINT8* pointer to Variable Data.
199 IN VARIABLE_HEADER
*Variable
205 // Be careful about pad size for alignment
207 Value
= (UINTN
) GetVariableNamePtr (Variable
);
208 Value
+= NameSizeOfVariable (Variable
);
209 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
211 return (UINT8
*) Value
;
216 This code gets the pointer to the next variable header.
218 @param Variable Pointer to the Variable Header.
220 @return A VARIABLE_HEADER* pointer to next variable header.
225 IN VARIABLE_HEADER
*Variable
230 if (!IsValidVariableHeader (Variable
)) {
234 Value
= (UINTN
) GetVariableDataPtr (Variable
);
235 Value
+= DataSizeOfVariable (Variable
);
236 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
239 // Be careful about pad size for alignment
241 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
245 This code gets the pointer to the variable name.
247 @param VarStoreHeader Pointer to the Variable Store Header.
249 @retval EfiRaw Variable store is raw
250 @retval EfiValid Variable store is valid
251 @retval EfiInvalid Variable store is invalid
254 VARIABLE_STORE_STATUS
255 GetVariableStoreStatus (
256 IN VARIABLE_STORE_HEADER
*VarStoreHeader
259 if (VarStoreHeader
->Signature
== VARIABLE_STORE_SIGNATURE
&&
260 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
261 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
267 if (VarStoreHeader
->Signature
== 0xffffffff &&
268 VarStoreHeader
->Size
== 0xffffffff &&
269 VarStoreHeader
->Format
== 0xff &&
270 VarStoreHeader
->State
== 0xff
281 This function compares a variable with variable entries in database.
283 @param Variable Pointer to the variable in our database
284 @param VariableName Name of the variable to compare to 'Variable'
285 @param VendorGuid GUID of the variable to compare to 'Variable'
286 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
288 @retval EFI_SUCCESS Found match variable
289 @retval EFI_NOT_FOUND Variable not found
293 CompareWithValidVariable (
294 IN VARIABLE_HEADER
*Variable
,
295 IN CONST CHAR16
*VariableName
,
296 IN CONST EFI_GUID
*VendorGuid
,
297 OUT VARIABLE_POINTER_TRACK
*PtrTrack
302 if (VariableName
[0] == 0) {
303 PtrTrack
->CurrPtr
= Variable
;
307 // Don't use CompareGuid function here for performance reasons.
308 // Instead we compare the GUID a UINT32 at a time and branch
309 // on the first failed comparison.
311 if ((((INT32
*) VendorGuid
)[0] == ((INT32
*) &Variable
->VendorGuid
)[0]) &&
312 (((INT32
*) VendorGuid
)[1] == ((INT32
*) &Variable
->VendorGuid
)[1]) &&
313 (((INT32
*) VendorGuid
)[2] == ((INT32
*) &Variable
->VendorGuid
)[2]) &&
314 (((INT32
*) VendorGuid
)[3] == ((INT32
*) &Variable
->VendorGuid
)[3])
316 ASSERT (NameSizeOfVariable (Variable
) != 0);
317 Point
= (VOID
*) GetVariableNamePtr (Variable
);
318 if (!CompareMem (VariableName
, Point
, NameSizeOfVariable (Variable
))) {
319 PtrTrack
->CurrPtr
= Variable
;
325 return EFI_NOT_FOUND
;
330 This code finds variable in storage blocks (Non-Volatile).
332 @param PeiServices General purpose services available to every PEIM.
333 @param VariableName Name of the variable to be found
334 @param VendorGuid Vendor GUID to be found.
335 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
337 @retval EFI_SUCCESS Variable found successfully
338 @retval EFI_NOT_FOUND Variable not found
339 @retval EFI_INVALID_PARAMETER Invalid variable name
344 IN CONST EFI_PEI_SERVICES
**PeiServices
,
345 IN CONST CHAR16
*VariableName
,
346 IN CONST EFI_GUID
*VendorGuid
,
347 OUT VARIABLE_POINTER_TRACK
*PtrTrack
350 EFI_HOB_GUID_TYPE
*GuidHob
;
351 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
352 VARIABLE_HEADER
*Variable
;
353 VARIABLE_HEADER
*MaxIndex
;
354 VARIABLE_INDEX_TABLE
*IndexTable
;
358 if (VariableName
!= 0 && VendorGuid
== NULL
) {
359 return EFI_INVALID_PARAMETER
;
362 // No Variable Address equals zero, so 0 as initial value is safe.
366 GuidHob
= GetFirstGuidHob (&mEfiVariableIndexTableGuid
);
367 if (GuidHob
== NULL
) {
368 IndexTable
= BuildGuidHob (&mEfiVariableIndexTableGuid
, sizeof (VARIABLE_INDEX_TABLE
));
369 IndexTable
->Length
= 0;
370 IndexTable
->StartPtr
= NULL
;
371 IndexTable
->EndPtr
= NULL
;
372 IndexTable
->GoneThrough
= 0;
374 IndexTable
= GET_GUID_HOB_DATA (GuidHob
);
375 for (Count
= 0; Count
< IndexTable
->Length
; Count
++) {
376 MaxIndex
= GetVariableByIndex (IndexTable
, Count
);
378 if (CompareWithValidVariable (MaxIndex
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
379 PtrTrack
->StartPtr
= IndexTable
->StartPtr
;
380 PtrTrack
->EndPtr
= IndexTable
->EndPtr
;
386 if (IndexTable
->GoneThrough
!= 0) {
387 return EFI_NOT_FOUND
;
391 // If not found in HOB, then let's start from the MaxIndex we've found.
393 if (MaxIndex
!= NULL
) {
394 Variable
= GetNextVariablePtr (MaxIndex
);
396 if ((IndexTable
->StartPtr
!= NULL
) || (IndexTable
->EndPtr
!= NULL
)) {
397 Variable
= IndexTable
->StartPtr
;
399 VariableBase
= (UINT8
*) (UINTN
) PcdGet32 (PcdFlashNvStorageVariableBase
);
400 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (VariableBase
+ \
401 ((EFI_FIRMWARE_VOLUME_HEADER
*) (VariableBase
)) -> HeaderLength
);
403 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
404 return EFI_UNSUPPORTED
;
407 if (~VariableStoreHeader
->Size
== 0) {
408 return EFI_NOT_FOUND
;
411 // Find the variable by walk through non-volatile variable store
413 IndexTable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
414 IndexTable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
417 // Start Pointers for the variable.
418 // Actual Data Pointer where data can be written.
420 Variable
= IndexTable
->StartPtr
;
424 // Find the variable by walk through non-volatile variable store
426 PtrTrack
->StartPtr
= IndexTable
->StartPtr
;
427 PtrTrack
->EndPtr
= IndexTable
->EndPtr
;
429 while (IsValidVariableHeader (Variable
) && (Variable
<= IndexTable
->EndPtr
)) {
430 if (Variable
->State
== VAR_ADDED
) {
432 // Record Variable in VariableIndex HOB
434 if (IndexTable
->Length
< VARIABLE_INDEX_TABLE_VOLUME
) {
435 VariableIndexTableUpdate (IndexTable
, Variable
);
438 if (CompareWithValidVariable (Variable
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
443 Variable
= GetNextVariablePtr (Variable
);
446 // If gone through the VariableStore, that means we never find in Firmware any more.
448 if (IndexTable
->Length
< VARIABLE_INDEX_TABLE_VOLUME
) {
449 IndexTable
->GoneThrough
= 1;
452 PtrTrack
->CurrPtr
= NULL
;
454 return EFI_NOT_FOUND
;
458 This service retrieves a variable's value using its name and GUID.
460 Read the specified variable from the UEFI variable store. If the Data
461 buffer is too small to hold the contents of the variable, the error
462 EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer
463 size to obtain the data.
465 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
466 @param VariableName A pointer to a null-terminated string that is the variable's name.
467 @param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of
468 VariableGuid and VariableName must be unique.
469 @param Attributes If non-NULL, on return, points to the variable's attributes.
470 @param DataSize On entry, points to the size in bytes of the Data buffer.
471 On return, points to the size of the data returned in Data.
472 @param Data Points to the buffer which will hold the returned variable value.
474 @retval EFI_SUCCESS The variable was read successfully.
475 @retval EFI_NOT_FOUND The variable could not be found.
476 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data.
477 DataSize is updated with the size required for
478 the specified variable.
479 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.
480 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
486 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
487 IN CONST CHAR16
*VariableName
,
488 IN CONST EFI_GUID
*VariableGuid
,
489 OUT UINT32
*Attributes
,
490 IN OUT UINTN
*DataSize
,
494 VARIABLE_POINTER_TRACK Variable
;
497 CONST EFI_PEI_SERVICES
**PeiServices
;
499 PeiServices
= GetPeiServicesTablePointer ();
500 if (VariableName
== NULL
|| VariableGuid
== NULL
|| DataSize
== NULL
) {
501 return EFI_INVALID_PARAMETER
;
504 // Find existing variable
506 Status
= FindVariable (PeiServices
, VariableName
, VariableGuid
, &Variable
);
507 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
513 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
514 if (*DataSize
>= VarDataSize
) {
516 return EFI_INVALID_PARAMETER
;
519 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
521 if (Attributes
!= NULL
) {
522 *Attributes
= Variable
.CurrPtr
->Attributes
;
525 *DataSize
= VarDataSize
;
528 *DataSize
= VarDataSize
;
529 return EFI_BUFFER_TOO_SMALL
;
534 Return the next variable name and GUID.
536 This function is called multiple times to retrieve the VariableName
537 and VariableGuid of all variables currently available in the system.
538 On each call, the previous results are passed into the interface,
539 and, on return, the interface returns the data for the next
540 interface. When the entire variable list has been returned,
541 EFI_NOT_FOUND is returned.
543 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
545 @param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.
546 @param VariableName On entry, a pointer to a null-terminated string that is the variable's name.
547 On return, points to the next variable's null-terminated name string.
548 @param VariableGuid On entry, a pointer to an UEFI _GUID that is the variable's GUID.
549 On return, a pointer to the next variable's GUID.
551 @retval EFI_SUCCESS The variable was read successfully.
552 @retval EFI_NOT_FOUND The variable could not be found.
553 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting
554 data. VariableNameSize is updated with the size
555 required for the specified variable.
556 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or
557 VariableNameSize is NULL.
558 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
563 PeiGetNextVariableName (
564 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
565 IN OUT UINTN
*VariableNameSize
,
566 IN OUT CHAR16
*VariableName
,
567 IN OUT EFI_GUID
*VariableGuid
570 VARIABLE_POINTER_TRACK Variable
;
573 CONST EFI_PEI_SERVICES
**PeiServices
;
575 PeiServices
= GetPeiServicesTablePointer ();
576 if (VariableName
== NULL
|| VariableGuid
== NULL
|| VariableNameSize
== NULL
) {
577 return EFI_INVALID_PARAMETER
;
580 Status
= FindVariable (PeiServices
, VariableName
, VariableGuid
, &Variable
);
581 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
585 if (VariableName
[0] != 0) {
587 // If variable name is not NULL, get next variable
589 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
592 while (!(Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
)) {
593 if (IsValidVariableHeader (Variable
.CurrPtr
)) {
594 if (Variable
.CurrPtr
->State
== VAR_ADDED
) {
595 ASSERT (NameSizeOfVariable (Variable
.CurrPtr
) != 0);
597 VarNameSize
= (UINTN
) NameSizeOfVariable (Variable
.CurrPtr
);
598 if (VarNameSize
<= *VariableNameSize
) {
599 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
601 CopyMem (VariableGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
603 Status
= EFI_SUCCESS
;
605 Status
= EFI_BUFFER_TOO_SMALL
;
608 *VariableNameSize
= VarNameSize
;
614 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
621 return EFI_NOT_FOUND
;