2 Framework PEIM to provide the Variable functionality
4 Copyright (c) 2006 - 2008 Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 EFI_PEI_READ_ONLY_VARIABLE_PPI mVariablePpi
= {
24 PeiGetNextVariableName
27 EFI_PEI_READ_ONLY_VARIABLE2_PPI mVariable2Ppi
= {
29 PeiGetNextVariableName2
32 EFI_PEI_PPI_DESCRIPTOR mPpiListVariable
[] = {
34 (EFI_PEI_PPI_DESCRIPTOR_PPI
),
35 &gEfiPeiReadOnlyVariable2PpiGuid
,
39 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
40 &gEfiPeiReadOnlyVariablePpiGuid
,
45 EFI_GUID mEfiVariableIndexTableGuid
= EFI_VARIABLE_INDEX_TABLE_GUID
;
48 Provide the functionality of the variable services.
50 @param FileHandle Handle of the file being invoked.
51 @param PeiServices Describes the list of possible PEI Services.
53 @return EFI_SUCCESS If the interface could be successfully installed.
54 @return EFI_UNSUPPORTED If current boot path is in recovery mode, then does not
55 install this interface.
60 PeimInitializeVariableServices (
61 IN EFI_PEI_FILE_HANDLE FileHandle
,
62 IN CONST EFI_PEI_SERVICES
**PeiServices
65 EFI_BOOT_MODE BootMode
;
69 // Check if this is recovery boot path. If no, publish the variable access capability
70 // to other modules. If yes, the content of variable area is not reliable. Therefore,
71 // in this case we should not provide variable service to other pei modules.
73 Status
= (*PeiServices
)->GetBootMode (PeiServices
, &BootMode
);
74 ASSERT_EFI_ERROR (Status
);
76 if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
77 return EFI_UNSUPPORTED
;
80 return (**PeiServices
).InstallPpi (PeiServices
, &mPpiListVariable
[0]);
85 This code gets the pointer to the first 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 (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
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.
135 IsValidVariableHeader (
136 IN VARIABLE_HEADER
*Variable
139 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 UINTN Size of variable in bytes
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
;
169 This code gets the size of name of variable.
171 @param Variable Pointer to the Variable Header.
173 @return UINTN Size of variable in bytes
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 CHAR16* Pointer to Variable Name
200 IN VARIABLE_HEADER
*Variable
205 return (CHAR16
*) (Variable
+ 1);
209 This code gets the pointer to the variable data.
211 @param Variable Pointer to the Variable Header.
213 @return 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
;
234 This code gets the pointer to the next variable header.
236 @param Variable Pointer to the Variable Header.
238 @return VARIABLE_HEADER* Pointer to next variable header.
243 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
275 GetVariableStoreStatus (
276 IN VARIABLE_STORE_HEADER
*VarStoreHeader
280 if (VarStoreHeader
->Signature
== VARIABLE_STORE_SIGNATURE
&&
281 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
282 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
288 if (VarStoreHeader
->Signature
== 0xffffffff &&
289 VarStoreHeader
->Size
== 0xffffffff &&
290 VarStoreHeader
->Format
== 0xff &&
291 VarStoreHeader
->State
== 0xff
301 This function compares a variable with variable entries in database
303 @param Variable - Pointer to the variable in our database
304 @param VariableName - Name of the variable to compare to 'Variable'
305 @param VendorGuid - GUID of the variable to compare to 'Variable'
306 @param PtrTrack - Variable Track Pointer structure that contains
307 Variable Information.
309 @retval EFI_SUCCESS - Found match variable
310 @retval EFI_NOT_FOUND - Variable not found
314 CompareWithValidVariable (
315 IN VARIABLE_HEADER
*Variable
,
316 IN CONST CHAR16
*VariableName
,
317 IN CONST EFI_GUID
*VendorGuid
,
318 OUT VARIABLE_POINTER_TRACK
*PtrTrack
324 if (VariableName
[0] == 0) {
325 PtrTrack
->CurrPtr
= Variable
;
329 // Don't use CompareGuid function here for performance reasons.
330 // Instead we compare the GUID a UINT32 at a time and branch
331 // on the first failed comparison.
333 if ((((INT32
*) VendorGuid
)[0] == ((INT32
*) &Variable
->VendorGuid
)[0]) &&
334 (((INT32
*) VendorGuid
)[1] == ((INT32
*) &Variable
->VendorGuid
)[1]) &&
335 (((INT32
*) VendorGuid
)[2] == ((INT32
*) &Variable
->VendorGuid
)[2]) &&
336 (((INT32
*) VendorGuid
)[3] == ((INT32
*) &Variable
->VendorGuid
)[3])
338 ASSERT (NameSizeOfVariable (Variable
) != 0);
339 Point
= (VOID
*) GetVariableNamePtr (Variable
);
340 if (!CompareMem (VariableName
, Point
, NameSizeOfVariable (Variable
))) {
341 PtrTrack
->CurrPtr
= Variable
;
347 return EFI_NOT_FOUND
;
351 This code finds variable in storage blocks (Non-Volatile)
353 @param PeiServices - General purpose services available to every PEIM.
354 @param VariableName - Name of the variable to be found
355 @param VendorGuid - Vendor GUID to be found.
356 @param PtrTrack - Variable Track Pointer structure that contains
357 Variable Information.
359 @retval EFI_SUCCESS - Variable found successfully
360 @retval EFI_NOT_FOUND - Variable not found
361 @retval EFI_INVALID_PARAMETER - Invalid variable name
367 IN EFI_PEI_SERVICES
**PeiServices
,
368 IN CONST CHAR16
*VariableName
,
369 IN CONST EFI_GUID
*VendorGuid
,
370 OUT VARIABLE_POINTER_TRACK
*PtrTrack
374 EFI_HOB_GUID_TYPE
*GuidHob
;
375 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
376 VARIABLE_HEADER
*Variable
;
377 VARIABLE_HEADER
*MaxIndex
;
378 VARIABLE_INDEX_TABLE
*IndexTable
;
382 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
383 return EFI_INVALID_PARAMETER
;
386 // No Variable Address equals zero, so 0 as initial value is safe.
390 GuidHob
= GetFirstGuidHob (&mEfiVariableIndexTableGuid
);
391 if (GuidHob
== NULL
) {
392 IndexTable
= BuildGuidHob (&mEfiVariableIndexTableGuid
, sizeof (VARIABLE_INDEX_TABLE
));
393 IndexTable
->Length
= 0;
394 IndexTable
->StartPtr
= NULL
;
395 IndexTable
->EndPtr
= NULL
;
396 IndexTable
->GoneThrough
= 0;
398 IndexTable
= GET_GUID_HOB_DATA (GuidHob
);
399 for (Count
= 0; Count
< IndexTable
->Length
; Count
++)
401 MaxIndex
= GetVariableByIndex (IndexTable
, Count
);
403 if (CompareWithValidVariable (MaxIndex
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
404 PtrTrack
->StartPtr
= IndexTable
->StartPtr
;
405 PtrTrack
->EndPtr
= IndexTable
->EndPtr
;
411 if (IndexTable
->GoneThrough
) {
412 return EFI_NOT_FOUND
;
416 // If not found in HOB, then let's start from the MaxIndex we've found.
418 if (MaxIndex
!= NULL
) {
419 Variable
= GetNextVariablePtr (MaxIndex
);
421 if (IndexTable
->StartPtr
|| IndexTable
->EndPtr
) {
422 Variable
= IndexTable
->StartPtr
;
424 VariableBase
= (UINT8
*) (UINTN
) PcdGet32 (PcdFlashNvStorageVariableBase
);
425 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (VariableBase
+ \
426 ((EFI_FIRMWARE_VOLUME_HEADER
*) (VariableBase
)) -> HeaderLength
);
428 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
429 return EFI_UNSUPPORTED
;
432 if (~VariableStoreHeader
->Size
== 0) {
433 return EFI_NOT_FOUND
;
436 // Find the variable by walk through non-volatile variable store
438 IndexTable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
439 IndexTable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
442 // Start Pointers for the variable.
443 // Actual Data Pointer where data can be written.
445 Variable
= IndexTable
->StartPtr
;
449 // Find the variable by walk through non-volatile variable store
451 PtrTrack
->StartPtr
= IndexTable
->StartPtr
;
452 PtrTrack
->EndPtr
= IndexTable
->EndPtr
;
454 while (IsValidVariableHeader (Variable
) && (Variable
<= IndexTable
->EndPtr
)) {
455 if (Variable
->State
== VAR_ADDED
) {
457 // Record Variable in VariableIndex HOB
459 if (IndexTable
->Length
< VARIABLE_INDEX_TABLE_VOLUME
)
461 VariableIndexTableUpdate (IndexTable
, Variable
);
464 if (CompareWithValidVariable (Variable
, VariableName
, VendorGuid
, PtrTrack
) == EFI_SUCCESS
) {
469 Variable
= GetNextVariablePtr (Variable
);
472 // If gone through the VariableStore, that means we never find in Firmware any more.
474 if (IndexTable
->Length
< VARIABLE_INDEX_TABLE_VOLUME
) {
475 IndexTable
->GoneThrough
= 1;
478 PtrTrack
->CurrPtr
= NULL
;
480 return EFI_NOT_FOUND
;
484 Provide the read variable functionality of the variable services.
486 @param PeiServices - General purpose services available to every PEIM.
488 @param VariableName - The variable name
490 @param VendorGuid - The vendor's GUID
492 @param Attributes - Pointer to the attribute
494 @param DataSize - Size of data
496 @param Data - Pointer to data
498 @retval EFI_SUCCESS - The interface could be successfully installed
500 @retval EFI_NOT_FOUND - The variable could not be discovered
502 @retval EFI_BUFFER_TOO_SMALL - The caller buffer is not large enough
508 IN EFI_PEI_SERVICES
**PeiServices
,
509 IN CHAR16
*VariableName
,
510 IN EFI_GUID
* VendorGuid
,
511 OUT UINT32
*Attributes OPTIONAL
,
512 IN OUT UINTN
*DataSize
,
517 VARIABLE_POINTER_TRACK Variable
;
521 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
522 return EFI_INVALID_PARAMETER
;
525 // Find existing variable
527 Status
= FindVariable (PeiServices
, VariableName
, VendorGuid
, &Variable
);
529 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
535 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
536 if (*DataSize
>= VarDataSize
) {
538 // PO-TKW: Address one checking in this place
541 return EFI_INVALID_PARAMETER
;
544 (*PeiServices
)->CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
546 if (Attributes
!= NULL
) {
547 *Attributes
= Variable
.CurrPtr
->Attributes
;
550 *DataSize
= VarDataSize
;
553 *DataSize
= VarDataSize
;
554 return EFI_BUFFER_TOO_SMALL
;
559 Provide the read variable functionality of the variable services.
561 @param PeiServices - General purpose services available to every PEIM.
563 @param VariableName - The variable name
565 @param VendorGuid - The vendor's GUID
567 @param Attributes - Pointer to the attribute
569 @param DataSize - Size of data
571 @param Data - Pointer to data
573 @retval EFI_SUCCESS - The interface could be successfully installed
575 @retval EFI_NOT_FOUND - The variable could not be discovered
577 @retval EFI_BUFFER_TOO_SMALL - The caller buffer is not large enough
583 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
584 IN CONST CHAR16
*VariableName
,
585 IN CONST EFI_GUID
*VariableGuid
,
586 OUT UINT32
*Attributes
,
587 IN OUT UINTN
*DataSize
,
592 return PeiGetVariable (
593 (EFI_PEI_SERVICES
**) GetPeiServicesTablePointer (),
594 (CHAR16
*)VariableName
,
595 (EFI_GUID
*)VariableGuid
,
603 Provide the get next variable functionality of the variable services.
605 @param PeiServices - General purpose services available to every PEIM.
606 @param VariabvleNameSize - The variable name's size.
607 @param VariableName - A pointer to the variable's name.
608 @param VendorGuid - A pointer to the EFI_GUID structure.
610 @param VariableNameSize - Size of the variable name
612 @param VariableName - The variable name
614 @param VendorGuid - The vendor's GUID
616 @retval EFI_SUCCESS - The interface could be successfully installed
618 @retval EFI_NOT_FOUND - The variable could not be discovered
623 PeiGetNextVariableName (
624 IN EFI_PEI_SERVICES
**PeiServices
,
625 IN OUT UINTN
*VariableNameSize
,
626 IN OUT CHAR16
*VariableName
,
627 IN OUT EFI_GUID
*VendorGuid
631 VARIABLE_POINTER_TRACK Variable
;
635 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
636 return EFI_INVALID_PARAMETER
;
639 Status
= FindVariable (PeiServices
, VariableName
, VendorGuid
, &Variable
);
641 if (Variable
.CurrPtr
== NULL
|| Status
!= EFI_SUCCESS
) {
645 if (VariableName
[0] != 0) {
647 // If variable name is not NULL, get next variable
649 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
652 while (!(Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
)) {
653 if (IsValidVariableHeader (Variable
.CurrPtr
)) {
654 if (Variable
.CurrPtr
->State
== VAR_ADDED
) {
655 ASSERT (NameSizeOfVariable (Variable
.CurrPtr
) != 0);
657 VarNameSize
= (UINTN
) NameSizeOfVariable (Variable
.CurrPtr
);
658 if (VarNameSize
<= *VariableNameSize
) {
659 (*PeiServices
)->CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
661 (*PeiServices
)->CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
663 Status
= EFI_SUCCESS
;
665 Status
= EFI_BUFFER_TOO_SMALL
;
668 *VariableNameSize
= VarNameSize
;
674 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
681 return EFI_NOT_FOUND
;
685 Provide the get next variable functionality of the variable services.
687 @param PeiServices - General purpose services available to every PEIM.
688 @param VariabvleNameSize - The variable name's size.
689 @param VariableName - A pointer to the variable's name.
690 @param VariableGuid - A pointer to the EFI_GUID structure.
692 @param VariableNameSize - Size of the variable name
694 @param VariableName - The variable name
696 @param VendorGuid - The vendor's GUID
699 @retval EFI_SUCCESS - The interface could be successfully installed
701 @retval EFI_NOT_FOUND - The variable could not be discovered
706 PeiGetNextVariableName2 (
707 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI
*This
,
708 IN OUT UINTN
*VariableNameSize
,
709 IN OUT CHAR16
*VariableName
,
710 IN OUT EFI_GUID
*VariableGuid
714 return PeiGetNextVariableName (
715 (EFI_PEI_SERVICES
**) GetPeiServicesTablePointer (),