2 Help functions used by PCD DXE driver.
4 Copyright (c) 2014, Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Library/DxeServicesLib.h>
19 PCD_DATABASE mPcdDatabase
;
21 UINT32 mPcdTotalTokenCount
;
22 UINT32 mPeiLocalTokenCount
;
23 UINT32 mDxeLocalTokenCount
;
24 UINT32 mPeiNexTokenCount
;
25 UINT32 mDxeNexTokenCount
;
26 UINT32 mPeiExMapppingTableSize
;
27 UINT32 mDxeExMapppingTableSize
;
28 UINT32 mPeiGuidTableSize
;
29 UINT32 mDxeGuidTableSize
;
31 BOOLEAN mPeiExMapTableEmpty
;
32 BOOLEAN mDxeExMapTableEmpty
;
33 BOOLEAN mPeiDatabaseEmpty
;
35 LIST_ENTRY
*mCallbackFnTable
;
36 EFI_GUID
**TmpTokenSpaceBuffer
;
37 UINTN TmpTokenSpaceBufferCount
;
40 Get Local Token Number by Token Number.
42 @param[in] IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
43 If FALSE, the pcd entry is initialized in DXE phase.
44 @param[in] TokenNumber The PCD token number.
46 @return Local Token Number.
55 UINT32
*LocalTokenNumberTable
;
56 UINT32 LocalTokenNumber
;
61 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
62 // We have to decrement TokenNumber by 1 to make it usable
63 // as the array index.
68 // Backup the TokenNumber passed in as GetPtrTypeSize need the original TokenNumber
70 TmpTokenNumber
= TokenNumber
;
72 LocalTokenNumberTable
= IsPeiDb
? (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
) :
73 (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
);
74 TokenNumber
= IsPeiDb
? TokenNumber
: TokenNumber
- mPeiLocalTokenCount
;
76 LocalTokenNumber
= LocalTokenNumberTable
[TokenNumber
];
78 Size
= (LocalTokenNumber
& PCD_DATUM_TYPE_ALL_SET
) >> PCD_DATUM_TYPE_SHIFT
;
80 if ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) == PCD_TYPE_SKU_ENABLED
) {
82 GetPtrTypeSize (TmpTokenNumber
, &MaxSize
);
86 LocalTokenNumber
= GetSkuEnabledTokenNumber (LocalTokenNumber
& ~PCD_TYPE_SKU_ENABLED
, MaxSize
, IsPeiDb
);
89 return LocalTokenNumber
;
93 Get PCD type by Local Token Number.
95 @param[in] LocalTokenNumber The PCD local token number.
101 IN UINT32 LocalTokenNumber
104 switch (LocalTokenNumber
& PCD_DATUM_TYPE_ALL_SET
) {
105 case PCD_DATUM_TYPE_POINTER
:
106 return EFI_PCD_TYPE_PTR
;
107 case PCD_DATUM_TYPE_UINT8
:
108 if ((LocalTokenNumber
& PCD_DATUM_TYPE_UINT8_BOOLEAN
) == PCD_DATUM_TYPE_UINT8_BOOLEAN
) {
109 return EFI_PCD_TYPE_BOOL
;
111 return EFI_PCD_TYPE_8
;
113 case PCD_DATUM_TYPE_UINT16
:
114 return EFI_PCD_TYPE_16
;
115 case PCD_DATUM_TYPE_UINT32
:
116 return EFI_PCD_TYPE_32
;
117 case PCD_DATUM_TYPE_UINT64
:
118 return EFI_PCD_TYPE_64
;
121 return EFI_PCD_TYPE_8
;
128 @param[in] OnlyTokenSpaceName If TRUE, only need to get the TokenSpaceCName.
129 If FALSE, need to get the full PCD name.
130 @param[in] IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
131 If FALSE, the pcd entry is initialized in DXE phase.
132 @param[in] TokenNumber The PCD token number.
134 @return The TokenSpaceCName or full PCD name.
138 IN BOOLEAN OnlyTokenSpaceName
,
143 PCD_DATABASE_INIT
*Database
;
146 PCD_NAME_INDEX
*PcdNameIndex
;
147 CHAR8
*TokenSpaceName
;
152 // Return NULL when PCD name table is absent.
155 if (mPcdDatabase
.PeiDb
->PcdNameTableOffset
== 0) {
159 if (mPcdDatabase
.DxeDb
->PcdNameTableOffset
== 0) {
165 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
166 // We have to decrement TokenNumber by 1 to make it usable
167 // as the array index.
171 Database
= IsPeiDb
? mPcdDatabase
.PeiDb
: mPcdDatabase
.DxeDb
;
172 TokenNumber
= IsPeiDb
? TokenNumber
: TokenNumber
- mPeiLocalTokenCount
;
174 StringTable
= (UINT8
*) Database
+ Database
->StringTableOffset
;
177 // Get the PCD name index.
179 PcdNameIndex
= (PCD_NAME_INDEX
*)((UINT8
*) Database
+ Database
->PcdNameTableOffset
) + TokenNumber
;
180 TokenSpaceName
= (CHAR8
*)&StringTable
[PcdNameIndex
->TokenSpaceCNameIndex
];
181 PcdName
= (CHAR8
*)&StringTable
[PcdNameIndex
->PcdCNameIndex
];
183 if (OnlyTokenSpaceName
) {
185 // Only need to get the TokenSpaceCName.
187 Name
= AllocateCopyPool (AsciiStrSize (TokenSpaceName
), TokenSpaceName
);
190 // Need to get the full PCD name.
192 NameSize
= AsciiStrSize (TokenSpaceName
) + AsciiStrSize (PcdName
);
193 Name
= AllocateZeroPool (NameSize
);
194 ASSERT (Name
!= NULL
);
196 // Catenate TokenSpaceCName and PcdCName with a '.' to form the full PCD name.
198 AsciiStrCatS (Name
, NameSize
, TokenSpaceName
);
199 Name
[AsciiStrSize (TokenSpaceName
) - sizeof (CHAR8
)] = '.';
200 AsciiStrCatS (Name
, NameSize
, PcdName
);
207 Retrieve additional information associated with a PCD token.
209 This includes information such as the type of value the TokenNumber is associated with as well as possible
210 human readable name that is associated with the token.
212 @param[in] IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
213 If FALSE, the pcd entry is initialized in DXE phase.
214 @param[in] Guid The 128-bit unique value that designates the namespace from which to extract the value.
215 @param[in] TokenNumber The PCD token number.
216 @param[out] PcdInfo The returned information associated with the requested TokenNumber.
217 The caller is responsible for freeing the buffer that is allocated by callee for PcdInfo->PcdName.
219 @retval EFI_SUCCESS The PCD information was returned successfully
220 @retval EFI_NOT_FOUND The PCD service could not find the requested token number.
225 IN CONST EFI_GUID
*Guid
,
226 IN UINTN TokenNumber
,
227 OUT EFI_PCD_INFO
*PcdInfo
230 PCD_DATABASE_INIT
*Database
;
234 DYNAMICEX_MAPPING
*ExMapTable
;
236 UINT32 LocalTokenNumber
;
238 Database
= IsPeiDb
? mPcdDatabase
.PeiDb
: mPcdDatabase
.DxeDb
;
240 GuidTable
= (EFI_GUID
*)((UINT8
*)Database
+ Database
->GuidTableOffset
);
241 MatchGuid
= ScanGuid (GuidTable
, Database
->GuidTableCount
* sizeof(EFI_GUID
), Guid
);
243 if (MatchGuid
== NULL
) {
244 return EFI_NOT_FOUND
;
247 GuidTableIdx
= MatchGuid
- GuidTable
;
249 ExMapTable
= (DYNAMICEX_MAPPING
*)((UINT8
*)Database
+ Database
->ExMapTableOffset
);
252 // Find the PCD by GuidTableIdx and ExTokenNumber in ExMapTable.
254 for (Index
= 0; Index
< Database
->ExTokenCount
; Index
++) {
255 if (ExMapTable
[Index
].ExGuidIndex
== GuidTableIdx
) {
256 if (TokenNumber
== PCD_INVALID_TOKEN_NUMBER
) {
258 // TokenNumber is 0, follow spec to set PcdType to EFI_PCD_TYPE_8,
259 // PcdSize to 0 and PcdName to the null-terminated ASCII string
260 // associated with the token's namespace Guid.
262 PcdInfo
->PcdType
= EFI_PCD_TYPE_8
;
263 PcdInfo
->PcdSize
= 0;
265 // Here use one representative in the token space to get the TokenSpaceCName.
267 PcdInfo
->PcdName
= GetPcdName (TRUE
, IsPeiDb
, ExMapTable
[Index
].TokenNumber
);
269 } else if (ExMapTable
[Index
].ExTokenNumber
== TokenNumber
) {
270 PcdInfo
->PcdSize
= DxePcdGetSize (ExMapTable
[Index
].TokenNumber
);
271 LocalTokenNumber
= GetLocalTokenNumber (IsPeiDb
, ExMapTable
[Index
].TokenNumber
);
272 PcdInfo
->PcdType
= GetPcdType (LocalTokenNumber
);
273 PcdInfo
->PcdName
= GetPcdName (FALSE
, IsPeiDb
, ExMapTable
[Index
].TokenNumber
);
279 return EFI_NOT_FOUND
;
283 Retrieve additional information associated with a PCD token.
285 This includes information such as the type of value the TokenNumber is associated with as well as possible
286 human readable name that is associated with the token.
288 @param[in] Guid The 128-bit unique value that designates the namespace from which to extract the value.
289 @param[in] TokenNumber The PCD token number.
290 @param[out] PcdInfo The returned information associated with the requested TokenNumber.
291 The caller is responsible for freeing the buffer that is allocated by callee for PcdInfo->PcdName.
293 @retval EFI_SUCCESS The PCD information was returned successfully.
294 @retval EFI_NOT_FOUND The PCD service could not find the requested token number.
298 IN CONST EFI_GUID
*Guid
,
299 IN UINTN TokenNumber
,
300 OUT EFI_PCD_INFO
*PcdInfo
304 BOOLEAN PeiExMapTableEmpty
;
305 BOOLEAN DxeExMapTableEmpty
;
306 UINT32 LocalTokenNumber
;
309 ASSERT (PcdInfo
!= NULL
);
311 Status
= EFI_NOT_FOUND
;
312 PeiExMapTableEmpty
= mPeiExMapTableEmpty
;
313 DxeExMapTableEmpty
= mDxeExMapTableEmpty
;
316 if (((TokenNumber
+ 1 > mPeiNexTokenCount
+ 1) && (TokenNumber
+ 1 <= mPeiLocalTokenCount
+ 1)) ||
317 ((TokenNumber
+ 1 > (mPeiLocalTokenCount
+ mDxeNexTokenCount
+ 1)))) {
318 return EFI_NOT_FOUND
;
319 } else if (TokenNumber
== PCD_INVALID_TOKEN_NUMBER
) {
321 // TokenNumber is 0, follow spec to set PcdType to EFI_PCD_TYPE_8,
322 // PcdSize to 0 and PcdName to NULL for default Token Space.
324 PcdInfo
->PcdType
= EFI_PCD_TYPE_8
;
325 PcdInfo
->PcdSize
= 0;
326 PcdInfo
->PcdName
= NULL
;
328 PcdInfo
->PcdSize
= DxePcdGetSize (TokenNumber
);
330 if ((TokenNumber
+ 1 <= mPeiNexTokenCount
+ 1)) {
333 LocalTokenNumber
= GetLocalTokenNumber (IsPeiDb
, TokenNumber
);
334 PcdInfo
->PcdType
= GetPcdType (LocalTokenNumber
);
335 PcdInfo
->PcdName
= GetPcdName (FALSE
, IsPeiDb
, TokenNumber
);
340 if (PeiExMapTableEmpty
&& DxeExMapTableEmpty
) {
341 return EFI_NOT_FOUND
;
344 if (!PeiExMapTableEmpty
) {
345 Status
= ExGetPcdInfo (
353 if (Status
== EFI_SUCCESS
) {
357 if (!DxeExMapTableEmpty
) {
358 Status
= ExGetPcdInfo (
370 Get the PCD entry pointer in PCD database.
372 This routine will visit PCD database to find the PCD entry according to given
373 token number. The given token number is autogened by build tools and it will be
374 translated to local token number. Local token number contains PCD's type and
375 offset of PCD entry in PCD database.
377 @param TokenNumber Token's number, it is autogened by build tools
378 @param GetSize The size of token's value
380 @return PCD entry pointer in PCD database
385 IN UINTN TokenNumber
,
393 VARIABLE_HEAD
*VariableHead
;
394 UINT8
*VaraiableDefaultBuffer
;
399 UINTN TmpTokenNumber
;
402 UINT32 LocalTokenNumber
;
404 STRING_HEAD StringTableIdx
;
408 // Aquire lock to prevent reentrance from TPL_CALLBACK level
410 EfiAcquireLock (&mPcdDatabaseLock
);
414 ASSERT (TokenNumber
> 0);
416 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
417 // We have to decrement TokenNumber by 1 to make it usable
418 // as the array index.
422 TmpTokenNumber
= TokenNumber
;
425 // EBC compiler is very choosy. It may report warning about comparison
426 // between UINTN and 0 . So we add 1 in each size of the
429 ASSERT (TokenNumber
+ 1 < mPcdTotalTokenCount
+ 1);
431 ASSERT ((GetSize
== DxePcdGetSize (TokenNumber
+ 1)) || (GetSize
== 0));
433 // EBC compiler is very choosy. It may report warning about comparison
434 // between UINTN and 0 . So we add 1 in each size of the
436 IsPeiDb
= (BOOLEAN
) ((TokenNumber
+ 1 < mPeiLocalTokenCount
+ 1) ? TRUE
: FALSE
);
438 LocalTokenNumber
= GetLocalTokenNumber (IsPeiDb
, TokenNumber
+ 1);
440 PcdDb
= IsPeiDb
? ((UINT8
*) mPcdDatabase
.PeiDb
) : ((UINT8
*) mPcdDatabase
.DxeDb
);
443 StringTable
= (UINT8
*) ((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->StringTableOffset
);
445 StringTable
= (UINT8
*) ((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->StringTableOffset
);
449 Offset
= LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
;
451 switch (LocalTokenNumber
& PCD_TYPE_ALL_SET
) {
453 VpdHead
= (VPD_HEAD
*) ((UINT8
*) PcdDb
+ Offset
);
454 RetPtr
= (VOID
*) (UINTN
) (PcdGet32 (PcdVpdBaseAddress
) + VpdHead
->Offset
);
458 case PCD_TYPE_HII
|PCD_TYPE_STRING
:
461 GuidTable
= (EFI_GUID
*) ((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->GuidTableOffset
);
463 GuidTable
= (EFI_GUID
*) ((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->GuidTableOffset
);
466 VariableHead
= (VARIABLE_HEAD
*) (PcdDb
+ Offset
);
467 Guid
= GuidTable
+ VariableHead
->GuidTableIndex
;
468 Name
= (UINT16
*)(StringTable
+ VariableHead
->StringIndex
);
470 if ((LocalTokenNumber
& PCD_TYPE_ALL_SET
) == (PCD_TYPE_HII
|PCD_TYPE_STRING
)) {
472 // If a HII type PCD's datum type is VOID*, the DefaultValueOffset is the index of
473 // string array in string table.
475 StringTableIdx
= *(STRING_HEAD
*)((UINT8
*) PcdDb
+ VariableHead
->DefaultValueOffset
);
476 VaraiableDefaultBuffer
= (UINT8
*) (StringTable
+ StringTableIdx
);
478 VaraiableDefaultBuffer
= (UINT8
*) PcdDb
+ VariableHead
->DefaultValueOffset
;
480 Status
= GetHiiVariable (Guid
, Name
, &Data
, &DataSize
);
481 if (Status
== EFI_SUCCESS
) {
482 if (DataSize
>= (VariableHead
->Offset
+ GetSize
)) {
485 // It is a pointer type. So get the MaxSize reserved for
488 GetPtrTypeSize (TmpTokenNumber
, &GetSize
);
489 if (GetSize
> (DataSize
- VariableHead
->Offset
)) {
491 // Use actual valid size.
493 GetSize
= DataSize
- VariableHead
->Offset
;
497 // If the operation is successful, we copy the data
498 // to the default value buffer in the PCD Database.
499 // So that we can free the Data allocated in GetHiiVariable.
501 CopyMem (VaraiableDefaultBuffer
, Data
+ VariableHead
->Offset
, GetSize
);
505 RetPtr
= (VOID
*) VaraiableDefaultBuffer
;
508 case PCD_TYPE_STRING
:
509 StringTableIdx
= *(STRING_HEAD
*)((UINT8
*) PcdDb
+ Offset
);
510 RetPtr
= (VOID
*) (StringTable
+ StringTableIdx
);
514 RetPtr
= (VOID
*) ((UINT8
*) PcdDb
+ Offset
);
523 EfiReleaseLock (&mPcdDatabaseLock
);
530 Register the callback function for a PCD entry.
532 This routine will register a callback function to a PCD entry by given token number
533 and token space guid.
535 @param TokenNumber PCD token's number, it is autogened by build tools.
536 @param Guid PCD token space's guid,
537 if not NULL, this PCD is dynamicEx type PCD.
538 @param CallBackFunction Callback function pointer
540 @return EFI_SUCCESS Always success for registering callback function.
544 DxeRegisterCallBackWorker (
545 IN UINTN TokenNumber
,
546 IN CONST EFI_GUID
*Guid
, OPTIONAL
547 IN PCD_PROTOCOL_CALLBACK CallBackFunction
550 CALLBACK_FN_ENTRY
*FnTableEntry
;
551 LIST_ENTRY
*ListHead
;
552 LIST_ENTRY
*ListNode
;
555 TokenNumber
= GetExPcdTokenNumber (Guid
, (UINT32
) TokenNumber
);
559 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
560 // We have to decrement TokenNumber by 1 to make it usable
561 // as the array index of mCallbackFnTable[].
563 ListHead
= &mCallbackFnTable
[TokenNumber
- 1];
564 ListNode
= GetFirstNode (ListHead
);
566 while (ListNode
!= ListHead
) {
567 FnTableEntry
= CR_FNENTRY_FROM_LISTNODE(ListNode
, CALLBACK_FN_ENTRY
, Node
);
569 if (FnTableEntry
->CallbackFn
== CallBackFunction
) {
571 // We only allow a Callback function to be register once
572 // for a TokenNumber. So just return EFI_SUCCESS
576 ListNode
= GetNextNode (ListHead
, ListNode
);
579 FnTableEntry
= AllocatePool (sizeof(CALLBACK_FN_ENTRY
));
580 ASSERT (FnTableEntry
!= NULL
);
582 FnTableEntry
->CallbackFn
= CallBackFunction
;
583 InsertTailList (ListHead
, &FnTableEntry
->Node
);
589 UnRegister the callback function for a PCD entry.
591 This routine will unregister a callback function to a PCD entry by given token number
592 and token space guid.
594 @param TokenNumber PCD token's number, it is autogened by build tools.
595 @param Guid PCD token space's guid.
596 if not NULL, this PCD is dynamicEx type PCD.
597 @param CallBackFunction Callback function pointer
599 @retval EFI_SUCCESS Callback function is success to be unregister.
600 @retval EFI_INVALID_PARAMETER Can not find the PCD entry by given token number.
603 DxeUnRegisterCallBackWorker (
604 IN UINTN TokenNumber
,
605 IN CONST EFI_GUID
*Guid
, OPTIONAL
606 IN PCD_PROTOCOL_CALLBACK CallBackFunction
609 CALLBACK_FN_ENTRY
*FnTableEntry
;
610 LIST_ENTRY
*ListHead
;
611 LIST_ENTRY
*ListNode
;
614 TokenNumber
= GetExPcdTokenNumber (Guid
, (UINT32
) TokenNumber
);
618 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
619 // We have to decrement TokenNumber by 1 to make it usable
620 // as the array index of mCallbackFnTable[].
622 ListHead
= &mCallbackFnTable
[TokenNumber
- 1];
623 ListNode
= GetFirstNode (ListHead
);
625 while (ListNode
!= ListHead
) {
626 FnTableEntry
= CR_FNENTRY_FROM_LISTNODE(ListNode
, CALLBACK_FN_ENTRY
, Node
);
628 if (FnTableEntry
->CallbackFn
== CallBackFunction
) {
630 // We only allow a Callback function to be register once
631 // for a TokenNumber. So we can safely remove the Node from
632 // the Link List and return EFI_SUCCESS.
634 RemoveEntryList (ListNode
);
635 FreePool (FnTableEntry
);
639 ListNode
= GetNextNode (ListHead
, ListNode
);
642 return EFI_INVALID_PARAMETER
;
646 Get next token number in given token space.
648 This routine is used for dynamicEx type PCD. It will firstly scan token space
649 table to get token space according to given token space guid. Then scan given
650 token number in found token space, if found, then return next token number in
653 @param Guid Token space guid. Next token number will be scaned in
655 @param TokenNumber Token number.
656 If PCD_INVALID_TOKEN_NUMBER, return first token number in
658 If not PCD_INVALID_TOKEN_NUMBER, return next token number
659 in token space table.
660 @param GuidTable Token space guid table. It will be used for scan token space
661 by given token space guid.
662 @param SizeOfGuidTable The size of guid table.
663 @param ExMapTable DynamicEx token number mapping table.
664 @param SizeOfExMapTable The size of dynamicEx token number mapping table.
666 @retval EFI_NOT_FOUND Can not given token space or token number.
667 @retval EFI_SUCCESS Success to get next token number.
671 ExGetNextTokeNumber (
672 IN CONST EFI_GUID
*Guid
,
673 IN OUT UINTN
*TokenNumber
,
674 IN EFI_GUID
*GuidTable
,
675 IN UINTN SizeOfGuidTable
,
676 IN DYNAMICEX_MAPPING
*ExMapTable
,
677 IN UINTN SizeOfExMapTable
684 UINTN ExMapTableCount
;
687 // Scan token space guid
689 MatchGuid
= ScanGuid (GuidTable
, SizeOfGuidTable
, Guid
);
690 if (MatchGuid
== NULL
) {
691 return EFI_NOT_FOUND
;
695 // Find the token space table in dynamicEx mapping table.
698 GuidTableIdx
= MatchGuid
- GuidTable
;
699 ExMapTableCount
= SizeOfExMapTable
/ sizeof(ExMapTable
[0]);
700 for (Index
= 0; Index
< ExMapTableCount
; Index
++) {
701 if (ExMapTable
[Index
].ExGuidIndex
== GuidTableIdx
) {
709 // If given token number is PCD_INVALID_TOKEN_NUMBER, then return the first
710 // token number in found token space.
712 if (*TokenNumber
== PCD_INVALID_TOKEN_NUMBER
) {
713 *TokenNumber
= ExMapTable
[Index
].ExTokenNumber
;
717 for ( ; Index
< ExMapTableCount
; Index
++) {
718 if (ExMapTable
[Index
].ExTokenNumber
== *TokenNumber
) {
723 while (Index
< ExMapTableCount
) {
725 if (Index
== ExMapTableCount
) {
727 // Exceed the length of ExMap Table
729 *TokenNumber
= PCD_INVALID_TOKEN_NUMBER
;
730 return EFI_NOT_FOUND
;
731 } else if (ExMapTable
[Index
].ExGuidIndex
== GuidTableIdx
) {
733 // Found the next match
735 *TokenNumber
= ExMapTable
[Index
].ExTokenNumber
;
741 return EFI_NOT_FOUND
;
745 Find the PCD database.
747 @retval The base address of external PCD database binary.
748 @retval NULL Return NULL if not find.
755 DXE_PCD_DATABASE
*DxePcdDbBinary
;
759 DxePcdDbBinary
= NULL
;
761 // Search the External Pcd database from one section of current FFS,
762 // and read it to memory
764 Status
= GetSectionFromFfs (
767 (VOID
**) &DxePcdDbBinary
,
770 ASSERT_EFI_ERROR (Status
);
773 // Check the first bytes (Header Signature Guid) and build version.
775 if (!CompareGuid ((VOID
*)DxePcdDbBinary
, &gPcdDataBaseSignatureGuid
) ||
776 (DxePcdDbBinary
->BuildVersion
!= PCD_SERVICE_DXE_VERSION
)) {
780 return DxePcdDbBinary
;
784 Initialize the PCD database in DXE phase.
786 PCD database in DXE phase also contains PCD database in PEI phase which is copied
791 BuildPcdDxeDataBase (
795 PEI_PCD_DATABASE
*PeiDatabase
;
796 EFI_HOB_GUID_TYPE
*GuidHob
;
802 // Assign PCD Entries with default value to PCD DATABASE
804 mPcdDatabase
.DxeDb
= LocateExPcdBinary ();
805 ASSERT(mPcdDatabase
.DxeDb
!= NULL
);
806 PcdDxeDbLen
= mPcdDatabase
.DxeDb
->Length
+ mPcdDatabase
.DxeDb
->UninitDataBaseSize
;
807 PcdDxeDb
= AllocateZeroPool (PcdDxeDbLen
);
808 ASSERT (PcdDxeDb
!= NULL
);
809 CopyMem (PcdDxeDb
, mPcdDatabase
.DxeDb
, mPcdDatabase
.DxeDb
->Length
);
810 FreePool (mPcdDatabase
.DxeDb
);
811 mPcdDatabase
.DxeDb
= PcdDxeDb
;
813 GuidHob
= GetFirstGuidHob (&gPcdDataBaseHobGuid
);
814 if (GuidHob
!= NULL
) {
817 // If no PEIMs use dynamic Pcd Entry, the Pcd Service PEIM
818 // should not be included at all. So the GuidHob could
819 // be NULL. If it is NULL, we just copy over the DXE Default
820 // Value to PCD Database.
823 PeiDatabase
= (PEI_PCD_DATABASE
*) GET_GUID_HOB_DATA (GuidHob
);
825 // Assign PCD Entries refereneced in PEI phase to PCD DATABASE
827 mPcdDatabase
.PeiDb
= PeiDatabase
;
829 // Inherit the SystemSkuId from PEI phase.
831 mPcdDatabase
.DxeDb
->SystemSkuId
= mPcdDatabase
.PeiDb
->SystemSkuId
;
833 mPcdDatabase
.PeiDb
= AllocateZeroPool (sizeof (PEI_PCD_DATABASE
));
834 ASSERT(mPcdDatabase
.PeiDb
!= NULL
);
838 // Initialized the external PCD database local variables
840 mPeiLocalTokenCount
= mPcdDatabase
.PeiDb
->LocalTokenCount
;
841 mDxeLocalTokenCount
= mPcdDatabase
.DxeDb
->LocalTokenCount
;
843 mPeiExMapppingTableSize
= mPcdDatabase
.PeiDb
->ExTokenCount
* sizeof (DYNAMICEX_MAPPING
);
844 mDxeExMapppingTableSize
= mPcdDatabase
.DxeDb
->ExTokenCount
* sizeof (DYNAMICEX_MAPPING
);
845 mPeiGuidTableSize
= mPcdDatabase
.PeiDb
->GuidTableCount
* sizeof(GUID
);
846 mDxeGuidTableSize
= mPcdDatabase
.DxeDb
->GuidTableCount
* sizeof (GUID
);
848 mPcdTotalTokenCount
= mPeiLocalTokenCount
+ mDxeLocalTokenCount
;
849 mPeiNexTokenCount
= mPeiLocalTokenCount
- mPcdDatabase
.PeiDb
->ExTokenCount
;
850 mDxeNexTokenCount
= mDxeLocalTokenCount
- mPcdDatabase
.DxeDb
->ExTokenCount
;
852 mPeiExMapTableEmpty
= (mPcdDatabase
.PeiDb
->ExTokenCount
== 0) ? TRUE
: FALSE
;
853 mDxeExMapTableEmpty
= (mPcdDatabase
.DxeDb
->ExTokenCount
== 0) ? TRUE
: FALSE
;
854 mPeiDatabaseEmpty
= (mPeiLocalTokenCount
== 0) ? TRUE
: FALSE
;
856 TmpTokenSpaceBufferCount
= mPcdDatabase
.PeiDb
->ExTokenCount
+ mPcdDatabase
.DxeDb
->ExTokenCount
;
857 TmpTokenSpaceBuffer
= (EFI_GUID
**)AllocateZeroPool(TmpTokenSpaceBufferCount
* sizeof (EFI_GUID
*));
860 // Initialized the Callback Function Table
862 mCallbackFnTable
= AllocateZeroPool (mPcdTotalTokenCount
* sizeof (LIST_ENTRY
));
863 ASSERT(mCallbackFnTable
!= NULL
);
866 // EBC compiler is very choosy. It may report warning about comparison
867 // between UINTN and 0 . So we add 1 in each size of the
870 for (Index
= 0; Index
+ 1 < mPcdTotalTokenCount
+ 1; Index
++) {
871 InitializeListHead (&mCallbackFnTable
[Index
]);
876 Get Variable which contains HII type PCD entry.
878 @param VariableGuid Variable's guid
879 @param VariableName Variable's unicode name string
880 @param VariableData Variable's data pointer,
881 @param VariableSize Variable's size.
883 @return the status of gRT->GetVariable
887 IN EFI_GUID
*VariableGuid
,
888 IN UINT16
*VariableName
,
889 OUT UINT8
**VariableData
,
890 OUT UINTN
*VariableSize
901 // Firstly get the real size of HII variable
903 Status
= gRT
->GetVariable (
904 (UINT16
*)VariableName
,
912 // Allocate buffer to hold whole variable data according to variable size.
914 if (Status
== EFI_BUFFER_TOO_SMALL
) {
915 Buffer
= (UINT8
*) AllocatePool (Size
);
917 ASSERT (Buffer
!= NULL
);
919 Status
= gRT
->GetVariable (
927 ASSERT (Status
== EFI_SUCCESS
);
928 *VariableData
= Buffer
;
929 *VariableSize
= Size
;
932 // Use Default Data only when variable is not found.
933 // For other error status, correct data can't be got, and trig ASSERT().
935 ASSERT (Status
== EFI_NOT_FOUND
);
942 Find the local token number according to system SKU ID.
944 @param LocalTokenNumber PCD token number
945 @param Size The size of PCD entry.
946 @param IsPeiDb If TRUE, the PCD entry is initialized in PEI phase.
947 If False, the PCD entry is initialized in DXE phase.
949 @return Token number according to system SKU ID.
953 GetSkuEnabledTokenNumber (
954 UINT32 LocalTokenNumber
,
966 ASSERT ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) == 0);
968 PcdDb
= IsPeiDb
? (UINT8
*) mPcdDatabase
.PeiDb
: (UINT8
*) mPcdDatabase
.DxeDb
;
970 SkuHead
= (SKU_HEAD
*) (PcdDb
+ (LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
));
971 Value
= (UINT8
*) (PcdDb
+ SkuHead
->SkuDataStartOffset
);
973 SkuIdTable
= (SKU_ID
*)(PcdDb
+ SkuHead
->SkuIdTableOffset
);
975 // Find the current system's SKU ID entry in SKU ID table.
978 for (Index
= 0; Index
< SkuIdTable
[0]; Index
++) {
979 if (mPcdDatabase
.DxeDb
->SystemSkuId
== SkuIdTable
[Index
+ 1]) {
986 // Find the default SKU ID entry in SKU ID table.
990 for (Index
= 0; Index
< SkuIdTable
[0]; Index
++) {
991 if (0 == SkuIdTable
[Index
+ 1]) {
996 ASSERT (Index
< SkuIdTable
[0]);
998 switch (LocalTokenNumber
& PCD_TYPE_ALL_SET
) {
1000 Value
= (UINT8
*) &(((VPD_HEAD
*) Value
)[Index
]);
1001 return (UINT32
) ((Value
- PcdDb
) | PCD_TYPE_VPD
);
1004 Value
= (UINT8
*) &(((VARIABLE_HEAD
*) Value
)[Index
]);
1005 return (UINT32
) ((Value
- PcdDb
) | PCD_TYPE_HII
);
1007 case PCD_TYPE_HII
|PCD_TYPE_STRING
:
1008 Value
= (UINT8
*) &(((VARIABLE_HEAD
*) Value
)[Index
]);
1009 return (UINT32
) ((Value
- PcdDb
) | PCD_TYPE_HII
| PCD_TYPE_STRING
);
1011 case PCD_TYPE_STRING
:
1012 Value
= (UINT8
*) &(((STRING_HEAD
*) Value
)[Index
]);
1013 return (UINT32
) ((Value
- PcdDb
) | PCD_TYPE_STRING
);
1016 Value
+= Size
* Index
;
1017 return (UINT32
) ((Value
- PcdDb
) | PCD_TYPE_DATA
);
1030 Invoke the callback function when dynamic PCD entry was set, if this PCD entry
1031 has registered callback function.
1033 @param ExTokenNumber DynamicEx PCD's token number, if this PCD entry is dyanmicEx
1035 @param Guid DynamicEx PCD's guid, if this PCD entry is dynamicEx type
1037 @param TokenNumber PCD token number generated by build tools.
1038 @param Data Value want to be set for this PCD entry
1039 @param Size The size of value
1043 InvokeCallbackOnSet (
1044 UINT32 ExTokenNumber
,
1045 CONST EFI_GUID
*Guid
, OPTIONAL
1051 CALLBACK_FN_ENTRY
*FnTableEntry
;
1052 LIST_ENTRY
*ListHead
;
1053 LIST_ENTRY
*ListNode
;
1056 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
1057 // We have to decrement TokenNumber by 1 to make it usable
1058 // as the array index of mCallbackFnTable[].
1060 ListHead
= &mCallbackFnTable
[TokenNumber
- 1];
1061 ListNode
= GetFirstNode (ListHead
);
1063 while (ListNode
!= ListHead
) {
1064 FnTableEntry
= CR_FNENTRY_FROM_LISTNODE (ListNode
, CALLBACK_FN_ENTRY
, Node
);
1066 FnTableEntry
->CallbackFn(Guid
,
1067 (Guid
== NULL
) ? TokenNumber
: ExTokenNumber
,
1071 ListNode
= GetNextNode (ListHead
, ListNode
);
1079 Wrapper function for setting non-pointer type value for a PCD entry.
1081 @param TokenNumber Pcd token number autogenerated by build tools.
1082 @param Data Value want to be set for PCD entry
1083 @param Size Size of value.
1085 @return status of SetWorker.
1090 IN UINTN TokenNumber
,
1095 return SetWorker (TokenNumber
, Data
, &Size
, FALSE
);
1100 Set value for an PCD entry
1102 @param TokenNumber Pcd token number autogenerated by build tools.
1103 @param Data Value want to be set for PCD entry
1104 @param Size Size of value.
1105 @param PtrType If TRUE, the type of PCD entry's value is Pointer.
1106 If False, the type of PCD entry's value is not Pointer.
1108 @retval EFI_INVALID_PARAMETER If this PCD type is VPD, VPD PCD can not be set.
1109 @retval EFI_INVALID_PARAMETER If Size can not be set to size table.
1110 @retval EFI_INVALID_PARAMETER If Size of non-Ptr type PCD does not match the size information in PCD database.
1111 @retval EFI_NOT_FOUND If value type of PCD entry is intergrate, but not in
1112 range of UINT8, UINT16, UINT32, UINT64
1113 @retval EFI_NOT_FOUND Can not find the PCD type according to token number.
1117 IN UINTN TokenNumber
,
1124 UINT32 LocalTokenNumber
;
1125 EFI_GUID
*GuidTable
;
1129 UINTN VariableOffset
;
1132 VARIABLE_HEAD
*VariableHead
;
1137 UINTN TmpTokenNumber
;
1140 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
1141 // We have to decrement TokenNumber by 1 to make it usable
1142 // as the array index.
1146 TmpTokenNumber
= TokenNumber
;
1149 // EBC compiler is very choosy. It may report warning about comparison
1150 // between UINTN and 0 . So we add 1 in each size of the
1153 ASSERT (TokenNumber
+ 1 < mPcdTotalTokenCount
+ 1);
1157 // Get MaxSize first, then check new size with max buffer size.
1159 GetPtrTypeSize (TokenNumber
, &MaxSize
);
1160 if (*Size
> MaxSize
) {
1162 return EFI_INVALID_PARAMETER
;
1165 if (*Size
!= DxePcdGetSize (TokenNumber
+ 1)) {
1166 return EFI_INVALID_PARAMETER
;
1171 // EBC compiler is very choosy. It may report warning about comparison
1172 // between UINTN and 0 . So we add 1 in each size of the
1175 if ((TokenNumber
+ 1 < mPeiNexTokenCount
+ 1) ||
1176 (TokenNumber
+ 1 >= mPeiLocalTokenCount
+ 1 && TokenNumber
+ 1 < (mPeiLocalTokenCount
+ mDxeNexTokenCount
+ 1))) {
1177 InvokeCallbackOnSet (0, NULL
, TokenNumber
+ 1, Data
, *Size
);
1181 // Aquire lock to prevent reentrance from TPL_CALLBACK level
1183 EfiAcquireLock (&mPcdDatabaseLock
);
1186 // EBC compiler is very choosy. It may report warning about comparison
1187 // between UINTN and 0 . So we add 1 in each size of the
1190 IsPeiDb
= (BOOLEAN
) ((TokenNumber
+ 1 < mPeiLocalTokenCount
+ 1) ? TRUE
: FALSE
);
1192 LocalTokenNumber
= GetLocalTokenNumber (IsPeiDb
, TokenNumber
+ 1);
1194 Offset
= LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
;
1196 PcdDb
= IsPeiDb
? ((UINT8
*) mPcdDatabase
.PeiDb
) : ((UINT8
*) mPcdDatabase
.DxeDb
);
1199 StringTable
= (UINT8
*) ((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->StringTableOffset
);
1201 StringTable
= (UINT8
*) ((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->StringTableOffset
);
1205 InternalData
= PcdDb
+ Offset
;
1207 switch (LocalTokenNumber
& PCD_TYPE_ALL_SET
) {
1210 Status
= EFI_INVALID_PARAMETER
;
1213 case PCD_TYPE_STRING
:
1214 if (SetPtrTypeSize (TmpTokenNumber
, Size
)) {
1215 CopyMem (StringTable
+ *((STRING_HEAD
*)InternalData
), Data
, *Size
);
1216 Status
= EFI_SUCCESS
;
1218 Status
= EFI_INVALID_PARAMETER
;
1222 case PCD_TYPE_HII
|PCD_TYPE_STRING
:
1225 if (!SetPtrTypeSize (TmpTokenNumber
, Size
)) {
1226 Status
= EFI_INVALID_PARAMETER
;
1232 GuidTable
= (EFI_GUID
*) ((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->GuidTableOffset
);
1234 GuidTable
= (EFI_GUID
*) ((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->GuidTableOffset
);
1237 VariableHead
= (VARIABLE_HEAD
*) (PcdDb
+ Offset
);
1239 Guid
= GuidTable
+ VariableHead
->GuidTableIndex
;
1240 Name
= (UINT16
*) (StringTable
+ VariableHead
->StringIndex
);
1241 VariableOffset
= VariableHead
->Offset
;
1242 Attributes
= VariableHead
->Attributes
;
1243 Status
= SetHiiVariable (Guid
, Name
, Attributes
, Data
, *Size
, VariableOffset
);
1248 if (SetPtrTypeSize (TmpTokenNumber
, Size
)) {
1249 CopyMem (InternalData
, Data
, *Size
);
1250 Status
= EFI_SUCCESS
;
1252 Status
= EFI_INVALID_PARAMETER
;
1257 Status
= EFI_SUCCESS
;
1260 *((UINT8
*) InternalData
) = *((UINT8
*) Data
);
1263 case sizeof(UINT16
):
1264 *((UINT16
*) InternalData
) = *((UINT16
*) Data
);
1267 case sizeof(UINT32
):
1268 *((UINT32
*) InternalData
) = *((UINT32
*) Data
);
1271 case sizeof(UINT64
):
1272 *((UINT64
*) InternalData
) = *((UINT64
*) Data
);
1277 Status
= EFI_NOT_FOUND
;
1284 Status
= EFI_NOT_FOUND
;
1288 EfiReleaseLock (&mPcdDatabaseLock
);
1294 Wrapper function for get PCD value for dynamic-ex PCD.
1296 @param Guid Token space guid for dynamic-ex PCD.
1297 @param ExTokenNumber Token number for dynamic-ex PCD.
1298 @param GetSize The size of dynamic-ex PCD value.
1300 @return PCD entry in PCD database.
1305 IN CONST EFI_GUID
*Guid
,
1306 IN UINTN ExTokenNumber
,
1310 return GetWorker(GetExPcdTokenNumber (Guid
, (UINT32
) ExTokenNumber
), GetSize
);
1314 Wrapper function for set PCD value for non-Pointer type dynamic-ex PCD.
1316 @param ExTokenNumber Token number for dynamic-ex PCD.
1317 @param Guid Token space guid for dynamic-ex PCD.
1318 @param Data Value want to be set.
1319 @param SetSize The size of value.
1321 @return status of ExSetWorker().
1326 IN UINTN ExTokenNumber
,
1327 IN CONST EFI_GUID
*Guid
,
1332 return ExSetWorker (ExTokenNumber
, Guid
, Data
, &SetSize
, FALSE
);
1336 Set value for a dynamic-ex PCD entry.
1338 This routine find the local token number according to dynamic-ex PCD's token
1339 space guid and token number firstly, and invoke callback function if this PCD
1340 entry registered callback function. Finally, invoken general SetWorker to set
1343 @param ExTokenNumber Dynamic-ex PCD token number.
1344 @param Guid Token space guid for dynamic-ex PCD.
1345 @param Data PCD value want to be set
1346 @param SetSize Size of value.
1347 @param PtrType If TRUE, this PCD entry is pointer type.
1348 If FALSE, this PCD entry is not pointer type.
1350 @return status of SetWorker().
1355 IN UINTN ExTokenNumber
,
1356 IN CONST EFI_GUID
*Guid
,
1358 IN OUT UINTN
*SetSize
,
1364 TokenNumber
= GetExPcdTokenNumber (Guid
, (UINT32
) ExTokenNumber
);
1366 InvokeCallbackOnSet ((UINT32
) ExTokenNumber
, Guid
, TokenNumber
, Data
, *SetSize
);
1368 return SetWorker (TokenNumber
, Data
, SetSize
, PtrType
);
1373 Get variable size and data from HII-type PCDs.
1375 @param[in] VariableGuid Guid of variable which stored value of a HII-type PCD.
1376 @param[in] VariableName Unicode name of variable which stored value of a HII-type PCD.
1377 @param[out] VariableSize Pointer to variable size got from HII-type PCDs.
1378 @param[out] VariableData Pointer to variable data got from HII-type PCDs.
1382 GetVariableSizeAndDataFromHiiPcd (
1383 IN EFI_GUID
*VariableGuid
,
1384 IN UINT16
*VariableName
,
1385 OUT UINTN
*VariableSize
,
1386 OUT VOID
*VariableData OPTIONAL
1390 PCD_DATABASE_INIT
*Database
;
1392 UINT32 LocalTokenNumber
;
1394 EFI_GUID
*GuidTable
;
1396 VARIABLE_HEAD
*VariableHead
;
1401 UINT8
*VaraiableDefaultBuffer
;
1402 STRING_HEAD StringTableIdx
;
1407 // Go through PCD database to find out DynamicHii PCDs.
1409 for (TokenNumber
= 1; TokenNumber
<= mPcdTotalTokenCount
; TokenNumber
++) {
1410 IsPeiDb
= (BOOLEAN
) ((TokenNumber
+ 1 < mPeiLocalTokenCount
+ 1) ? TRUE
: FALSE
);
1411 Database
= IsPeiDb
? mPcdDatabase
.PeiDb
: mPcdDatabase
.DxeDb
;
1412 LocalTokenNumber
= GetLocalTokenNumber (IsPeiDb
, TokenNumber
);
1413 if ((LocalTokenNumber
& PCD_TYPE_HII
) != 0) {
1415 // Get the Variable Guid and Name pointer.
1417 Offset
= LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
;
1418 VariableHead
= (VARIABLE_HEAD
*) ((UINT8
*) Database
+ Offset
);
1419 StringTable
= (UINT8
*) ((UINT8
*) Database
+ Database
->StringTableOffset
);
1420 GuidTable
= (EFI_GUID
*) ((UINT8
*) Database
+ Database
->GuidTableOffset
);
1421 Guid
= GuidTable
+ VariableHead
->GuidTableIndex
;
1422 Name
= (UINT16
*) (StringTable
+ VariableHead
->StringIndex
);
1423 if (CompareGuid (VariableGuid
, Guid
) && (StrCmp (VariableName
, Name
) == 0)) {
1425 // It is the matched DynamicHii PCD.
1427 PcdDataSize
= DxePcdGetSize (TokenNumber
);
1428 Size
= VariableHead
->Offset
+ PcdDataSize
;
1429 if (Size
> *VariableSize
) {
1430 *VariableSize
= Size
;
1432 if (VariableData
!= NULL
) {
1433 if ((LocalTokenNumber
& PCD_TYPE_ALL_SET
) == (PCD_TYPE_HII
|PCD_TYPE_STRING
)) {
1435 // If a HII type PCD's datum type is VOID*, the DefaultValueOffset is the index of
1436 // string array in string table.
1438 StringTableIdx
= *(STRING_HEAD
*) ((UINT8
*) Database
+ VariableHead
->DefaultValueOffset
);
1439 VaraiableDefaultBuffer
= (UINT8
*) (StringTable
+ StringTableIdx
);
1441 VaraiableDefaultBuffer
= (UINT8
*) Database
+ VariableHead
->DefaultValueOffset
;
1443 CopyMem ((UINT8
*) VariableData
+ VariableHead
->Offset
, VaraiableDefaultBuffer
, PcdDataSize
);
1451 Set value for HII-type PCD.
1453 A HII-type PCD's value is stored in a variable. Setting/Getting the value of
1454 HII-type PCD is to visit this variable.
1456 @param VariableGuid Guid of variable which stored value of a HII-type PCD.
1457 @param VariableName Unicode name of variable which stored value of a HII-type PCD.
1458 @param SetAttributes Attributes bitmask to set for the variable.
1459 @param Data Value want to be set.
1460 @param DataSize Size of value
1461 @param Offset Value offset of HII-type PCD in variable.
1463 @return status of GetVariable()/SetVariable().
1468 IN EFI_GUID
*VariableGuid
,
1469 IN UINT16
*VariableName
,
1470 IN UINT32 SetAttributes
,
1471 IN CONST VOID
*Data
,
1486 // Try to get original variable size information.
1488 Status
= gRT
->GetVariable (
1489 (UINT16
*)VariableName
,
1496 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1498 // Patch new PCD's value to offset in given HII variable.
1500 if (Size
>= (DataSize
+ Offset
)) {
1503 SetSize
= DataSize
+ Offset
;
1505 Buffer
= AllocatePool (SetSize
);
1506 ASSERT (Buffer
!= NULL
);
1508 Status
= gRT
->GetVariable (
1516 ASSERT_EFI_ERROR (Status
);
1518 CopyMem ((UINT8
*)Buffer
+ Offset
, Data
, DataSize
);
1520 if (SetAttributes
== 0) {
1521 SetAttributes
= Attribute
;
1524 Status
= gRT
->SetVariable (
1534 } else if (Status
== EFI_NOT_FOUND
) {
1536 // If variable does not exist, a new variable need to be created.
1540 // Get size, allocate buffer and get data.
1542 GetVariableSizeAndDataFromHiiPcd (VariableGuid
, VariableName
, &Size
, NULL
);
1543 Buffer
= AllocateZeroPool (Size
);
1544 ASSERT (Buffer
!= NULL
);
1545 GetVariableSizeAndDataFromHiiPcd (VariableGuid
, VariableName
, &Size
, Buffer
);
1550 CopyMem ((UINT8
*)Buffer
+ Offset
, Data
, DataSize
);
1552 if (SetAttributes
== 0) {
1553 SetAttributes
= EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
;
1556 Status
= gRT
->SetVariable (
1569 // If we drop to here, the value is failed to be written in to variable area.
1575 Get Token Number according to dynamic-ex PCD's {token space guid:token number}
1577 A dynamic-ex type PCD, developer must provide pair of token space guid: token number
1578 in DEC file. PCD database maintain a mapping table that translate pair of {token
1579 space guid: token number} to Token Number.
1581 @param Guid Token space guid for dynamic-ex PCD entry.
1582 @param ExTokenNumber Dynamic-ex PCD token number.
1584 @return Token Number for dynamic-ex PCD.
1588 GetExPcdTokenNumber (
1589 IN CONST EFI_GUID
*Guid
,
1590 IN UINT32 ExTokenNumber
1594 DYNAMICEX_MAPPING
*ExMap
;
1595 EFI_GUID
*GuidTable
;
1596 EFI_GUID
*MatchGuid
;
1599 if (!mPeiDatabaseEmpty
) {
1600 ExMap
= (DYNAMICEX_MAPPING
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->ExMapTableOffset
);
1601 GuidTable
= (EFI_GUID
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->GuidTableOffset
);
1603 MatchGuid
= ScanGuid (GuidTable
, mPeiGuidTableSize
, Guid
);
1605 if (MatchGuid
!= NULL
) {
1607 MatchGuidIdx
= MatchGuid
- GuidTable
;
1609 for (Index
= 0; Index
< mPcdDatabase
.PeiDb
->ExTokenCount
; Index
++) {
1610 if ((ExTokenNumber
== ExMap
[Index
].ExTokenNumber
) &&
1611 (MatchGuidIdx
== ExMap
[Index
].ExGuidIndex
)) {
1612 return ExMap
[Index
].TokenNumber
;
1618 ExMap
= (DYNAMICEX_MAPPING
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->ExMapTableOffset
);
1619 GuidTable
= (EFI_GUID
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->GuidTableOffset
);
1621 MatchGuid
= ScanGuid (GuidTable
, mDxeGuidTableSize
, Guid
);
1623 // We need to ASSERT here. If GUID can't be found in GuidTable, this is a
1624 // error in the BUILD system.
1626 ASSERT (MatchGuid
!= NULL
);
1628 MatchGuidIdx
= MatchGuid
- GuidTable
;
1630 for (Index
= 0; Index
< mPcdDatabase
.DxeDb
->ExTokenCount
; Index
++) {
1631 if ((ExTokenNumber
== ExMap
[Index
].ExTokenNumber
) &&
1632 (MatchGuidIdx
== ExMap
[Index
].ExGuidIndex
)) {
1633 return ExMap
[Index
].TokenNumber
;
1643 Get SKU ID table from PCD database.
1645 @param LocalTokenNumberTableIdx Index of local token number in token number table.
1646 @param IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
1647 If FALSE, the pcd entry is initialized in DXE phase.
1648 @return Pointer to SKU ID array table
1653 IN UINTN LocalTokenNumberTableIdx
,
1658 UINTN LocalTokenNumber
;
1662 LocalTokenNumber
= *((UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
) + LocalTokenNumberTableIdx
);
1663 Database
= (UINT8
*) mPcdDatabase
.PeiDb
;
1665 LocalTokenNumber
= *((UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
) + LocalTokenNumberTableIdx
);
1666 Database
= (UINT8
*) mPcdDatabase
.DxeDb
;
1669 ASSERT ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) != 0);
1671 SkuHead
= (SKU_HEAD
*) ((UINT8
*)Database
+ (LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
));
1673 return (SKU_ID
*) (Database
+ SkuHead
->SkuIdTableOffset
);
1678 Wrapper function of getting index of PCD entry in size table.
1680 @param LocalTokenNumberTableIdx Index of this PCD in local token number table.
1681 @param IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
1682 If FALSE, the pcd entry is initialized in DXE phase.
1684 @return index of PCD entry in size table.
1688 IN UINTN LocalTokenNumberTableIdx
,
1692 UINT32
*LocalTokenNumberTable
;
1693 UINTN LocalTokenNumber
;
1699 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
);
1701 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
);
1706 for (Index
= 0; Index
< LocalTokenNumberTableIdx
; Index
++) {
1707 LocalTokenNumber
= LocalTokenNumberTable
[Index
];
1709 if ((LocalTokenNumber
& PCD_DATUM_TYPE_ALL_SET
) == PCD_DATUM_TYPE_POINTER
) {
1711 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1714 if ((LocalTokenNumber
& PCD_TYPE_VPD
) != 0) {
1716 // We have only two entry for VPD enabled PCD entry:
1719 // Current size is equal to MAX size.
1723 if ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) == 0) {
1725 // We have only two entry for Non-Sku enabled PCD entry:
1732 // We have these entry for SKU enabled PCD entry
1734 // 2) Current Size for each SKU_ID (It is equal to MaxSku).
1736 SkuIdTable
= GetSkuIdArray (Index
, IsPeiDb
);
1737 SizeTableIdx
+= (UINTN
)*SkuIdTable
+ 1;
1744 return SizeTableIdx
;
1748 Get size of POINTER type PCD value.
1750 @param LocalTokenNumberTableIdx Index of local token number in local token number table.
1751 @param MaxSize Maxmium size of POINTER type PCD value.
1753 @return size of POINTER type PCD value.
1758 IN UINTN LocalTokenNumberTableIdx
,
1763 UINTN LocalTokenNumber
;
1765 SIZE_INFO
*SizeTable
;
1768 UINT32
*LocalTokenNumberTable
;
1770 // EBC compiler is very choosy. It may report warning about comparison
1771 // between UINTN and 0 . So we add 1 in each size of the
1773 IsPeiDb
= (BOOLEAN
) (LocalTokenNumberTableIdx
+ 1 < mPeiLocalTokenCount
+ 1);
1777 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
);
1778 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->SizeTableOffset
);
1780 LocalTokenNumberTableIdx
-= mPeiLocalTokenCount
;
1781 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
);
1782 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->SizeTableOffset
);
1785 LocalTokenNumber
= LocalTokenNumberTable
[LocalTokenNumberTableIdx
];
1787 ASSERT ((LocalTokenNumber
& PCD_DATUM_TYPE_ALL_SET
) == PCD_DATUM_TYPE_POINTER
);
1789 SizeTableIdx
= GetSizeTableIndex (LocalTokenNumberTableIdx
, IsPeiDb
);
1791 *MaxSize
= SizeTable
[SizeTableIdx
];
1793 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1796 if ((LocalTokenNumber
& PCD_TYPE_VPD
) != 0) {
1798 // We have only two entry for VPD enabled PCD entry:
1801 // We consider current size is equal to MAX size.
1805 if ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) == 0) {
1807 // We have only two entry for Non-Sku enabled PCD entry:
1811 return SizeTable
[SizeTableIdx
+ 1];
1814 // We have these entry for SKU enabled PCD entry
1816 // 2) Current Size for each SKU_ID (It is equal to MaxSku).
1818 SkuIdTable
= GetSkuIdArray (LocalTokenNumberTableIdx
, IsPeiDb
);
1819 for (Index
= 0; Index
< SkuIdTable
[0]; Index
++) {
1820 if (SkuIdTable
[1 + Index
] == mPcdDatabase
.DxeDb
->SystemSkuId
) {
1821 return SizeTable
[SizeTableIdx
+ 1 + Index
];
1824 return SizeTable
[SizeTableIdx
+ 1];
1830 Set size of POINTER type PCD value. The size should not exceed the maximum size
1833 @param LocalTokenNumberTableIdx Index of local token number in local token number table.
1834 @param CurrentSize Size of POINTER type PCD value.
1836 @retval TRUE Success to set size of PCD value.
1837 @retval FALSE Fail to set size of PCD value.
1841 IN UINTN LocalTokenNumberTableIdx
,
1842 IN OUT UINTN
*CurrentSize
1846 UINTN LocalTokenNumber
;
1848 SIZE_INFO
*SizeTable
;
1852 UINT32
*LocalTokenNumberTable
;
1855 // EBC compiler is very choosy. It may report warning about comparison
1856 // between UINTN and 0 . So we add 1 in each size of the
1859 IsPeiDb
= (BOOLEAN
) (LocalTokenNumberTableIdx
+ 1 < mPeiLocalTokenCount
+ 1);
1862 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
);
1863 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->SizeTableOffset
);
1865 LocalTokenNumberTableIdx
-= mPeiLocalTokenCount
;
1866 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
);
1867 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->SizeTableOffset
);
1870 LocalTokenNumber
= LocalTokenNumberTable
[LocalTokenNumberTableIdx
];
1872 ASSERT ((LocalTokenNumber
& PCD_DATUM_TYPE_ALL_SET
) == PCD_DATUM_TYPE_POINTER
);
1874 SizeTableIdx
= GetSizeTableIndex (LocalTokenNumberTableIdx
, IsPeiDb
);
1876 MaxSize
= SizeTable
[SizeTableIdx
];
1878 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1881 if ((LocalTokenNumber
& PCD_TYPE_VPD
) != 0) {
1883 // We shouldn't come here as we don't support SET for VPD
1888 if ((*CurrentSize
> MaxSize
) ||
1889 (*CurrentSize
== MAX_ADDRESS
)) {
1890 *CurrentSize
= MaxSize
;
1894 if ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) == 0) {
1896 // We have only two entry for Non-Sku enabled PCD entry:
1900 SizeTable
[SizeTableIdx
+ 1] = (SIZE_INFO
) *CurrentSize
;
1904 // We have these entry for SKU enabled PCD entry
1906 // 2) Current Size for each SKU_ID (It is equal to MaxSku).
1908 SkuIdTable
= GetSkuIdArray (LocalTokenNumberTableIdx
, IsPeiDb
);
1909 for (Index
= 0; Index
< SkuIdTable
[0]; Index
++) {
1910 if (SkuIdTable
[1 + Index
] == mPcdDatabase
.DxeDb
->SystemSkuId
) {
1911 SizeTable
[SizeTableIdx
+ 1 + Index
] = (SIZE_INFO
) *CurrentSize
;
1915 SizeTable
[SizeTableIdx
+ 1] = (SIZE_INFO
) *CurrentSize
;
1922 VariableLock DynamicHiiPcd.
1924 @param[in] IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
1925 If FALSE, the pcd entry is initialized in DXE phase.
1926 @param[in] VariableLock Pointer to VariableLockProtocol.
1930 VariableLockDynamicHiiPcd (
1932 IN EDKII_VARIABLE_LOCK_PROTOCOL
*VariableLock
1936 PCD_DATABASE_INIT
*Database
;
1937 UINT32 LocalTokenCount
;
1939 UINT32 LocalTokenNumber
;
1941 EFI_GUID
*GuidTable
;
1943 VARIABLE_HEAD
*VariableHead
;
1947 Database
= IsPeiDb
? mPcdDatabase
.PeiDb
: mPcdDatabase
.DxeDb
;
1948 LocalTokenCount
= IsPeiDb
? mPeiLocalTokenCount
: mDxeLocalTokenCount
;
1951 // Go through PCD database to find out DynamicHii PCDs.
1953 for (TokenNumber
= 1; TokenNumber
<= LocalTokenCount
; TokenNumber
++) {
1955 LocalTokenNumber
= GetLocalTokenNumber (TRUE
, TokenNumber
);
1957 LocalTokenNumber
= GetLocalTokenNumber (FALSE
, TokenNumber
+ mPeiLocalTokenCount
);
1959 if ((LocalTokenNumber
& PCD_TYPE_HII
) != 0) {
1960 Offset
= LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
;
1961 VariableHead
= (VARIABLE_HEAD
*) ((UINT8
*) Database
+ Offset
);
1963 // Why not to set property by VarCheckProtocol with Attributes and Property directly here?
1964 // It is because that set property by VarCheckProtocol will indicate the variable to
1965 // be a system variable, but the unknown max size of the variable is dangerous to
1966 // the system variable region.
1968 if ((VariableHead
->Property
& VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY
) != 0) {
1970 // DynamicHii PCD with RO property set in *.dsc.
1972 StringTable
= (UINT8
*) ((UINT8
*) Database
+ Database
->StringTableOffset
);
1973 GuidTable
= (EFI_GUID
*) ((UINT8
*) Database
+ Database
->GuidTableOffset
);
1974 Guid
= GuidTable
+ VariableHead
->GuidTableIndex
;
1975 Name
= (UINT16
*) (StringTable
+ VariableHead
->StringIndex
);
1976 Status
= VariableLock
->RequestToLock (VariableLock
, Name
, Guid
);
1977 ASSERT_EFI_ERROR (Status
);
1984 VariableLockProtocol callback
1985 to lock the variables referenced by DynamicHii PCDs with RO property set in *.dsc.
1987 @param[in] Event Event whose notification function is being invoked.
1988 @param[in] Context Pointer to the notification function's context.
1993 VariableLockCallBack (
1999 EDKII_VARIABLE_LOCK_PROTOCOL
*VariableLock
;
2001 Status
= gBS
->LocateProtocol (&gEdkiiVariableLockProtocolGuid
, NULL
, (VOID
**) &VariableLock
);
2002 if (!EFI_ERROR (Status
)) {
2003 VariableLockDynamicHiiPcd (TRUE
, VariableLock
);
2004 VariableLockDynamicHiiPcd (FALSE
, VariableLock
);