2 Help functions used by PCD DXE driver.
4 Copyright (c) 2014, Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
6 (C) Copyright 2016-2021 Hewlett Packard Enterprise Development LP<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Library/DxeServicesLib.h>
14 PCD_DATABASE mPcdDatabase
;
16 UINT32 mPcdTotalTokenCount
;
17 UINT32 mPeiLocalTokenCount
;
18 UINT32 mDxeLocalTokenCount
;
19 UINT32 mPeiNexTokenCount
;
20 UINT32 mDxeNexTokenCount
;
21 UINT32 mPeiExMapppingTableSize
;
22 UINT32 mDxeExMapppingTableSize
;
23 UINT32 mPeiGuidTableSize
;
24 UINT32 mDxeGuidTableSize
;
26 BOOLEAN mPeiExMapTableEmpty
;
27 BOOLEAN mDxeExMapTableEmpty
;
28 BOOLEAN mPeiDatabaseEmpty
;
30 LIST_ENTRY
*mCallbackFnTable
;
31 EFI_GUID
**TmpTokenSpaceBuffer
;
32 UINTN TmpTokenSpaceBufferCount
;
34 UINTN mPeiPcdDbSize
= 0;
35 PEI_PCD_DATABASE
*mPeiPcdDbBinary
= NULL
;
36 UINTN mDxePcdDbSize
= 0;
37 DXE_PCD_DATABASE
*mDxePcdDbBinary
= NULL
;
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.
54 UINT32
*LocalTokenNumberTable
;
57 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
58 // We have to decrement TokenNumber by 1 to make it usable
59 // as the array index.
63 LocalTokenNumberTable
= IsPeiDb
? (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
) :
64 (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
);
65 TokenNumber
= IsPeiDb
? TokenNumber
: TokenNumber
- mPeiLocalTokenCount
;
67 return LocalTokenNumberTable
[TokenNumber
];
71 Get PCD type by Local Token Number.
73 @param[in] LocalTokenNumber The PCD local token number.
79 IN UINT32 LocalTokenNumber
82 switch (LocalTokenNumber
& PCD_DATUM_TYPE_ALL_SET
) {
83 case PCD_DATUM_TYPE_POINTER
:
84 return EFI_PCD_TYPE_PTR
;
85 case PCD_DATUM_TYPE_UINT8
:
86 if ((LocalTokenNumber
& PCD_DATUM_TYPE_UINT8_BOOLEAN
) == PCD_DATUM_TYPE_UINT8_BOOLEAN
) {
87 return EFI_PCD_TYPE_BOOL
;
89 return EFI_PCD_TYPE_8
;
92 case PCD_DATUM_TYPE_UINT16
:
93 return EFI_PCD_TYPE_16
;
94 case PCD_DATUM_TYPE_UINT32
:
95 return EFI_PCD_TYPE_32
;
96 case PCD_DATUM_TYPE_UINT64
:
97 return EFI_PCD_TYPE_64
;
100 return EFI_PCD_TYPE_8
;
107 @param[in] OnlyTokenSpaceName If TRUE, only need to get the TokenSpaceCName.
108 If FALSE, need to get the full PCD name.
109 @param[in] IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
110 If FALSE, the pcd entry is initialized in DXE phase.
111 @param[in] TokenNumber The PCD token number.
113 @return The TokenSpaceCName or full PCD name.
117 IN BOOLEAN OnlyTokenSpaceName
,
122 PCD_DATABASE_INIT
*Database
;
125 PCD_NAME_INDEX
*PcdNameIndex
;
126 CHAR8
*TokenSpaceName
;
131 // Return NULL when PCD name table is absent.
134 if (mPcdDatabase
.PeiDb
->PcdNameTableOffset
== 0) {
138 if (mPcdDatabase
.DxeDb
->PcdNameTableOffset
== 0) {
144 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
145 // We have to decrement TokenNumber by 1 to make it usable
146 // as the array index.
150 Database
= IsPeiDb
? mPcdDatabase
.PeiDb
: mPcdDatabase
.DxeDb
;
151 TokenNumber
= IsPeiDb
? TokenNumber
: TokenNumber
- mPeiLocalTokenCount
;
153 StringTable
= (UINT8
*)Database
+ Database
->StringTableOffset
;
156 // Get the PCD name index.
158 PcdNameIndex
= (PCD_NAME_INDEX
*)((UINT8
*)Database
+ Database
->PcdNameTableOffset
) + TokenNumber
;
159 TokenSpaceName
= (CHAR8
*)&StringTable
[PcdNameIndex
->TokenSpaceCNameIndex
];
160 PcdName
= (CHAR8
*)&StringTable
[PcdNameIndex
->PcdCNameIndex
];
162 if (OnlyTokenSpaceName
) {
164 // Only need to get the TokenSpaceCName.
166 Name
= AllocateCopyPool (AsciiStrSize (TokenSpaceName
), TokenSpaceName
);
169 // Need to get the full PCD name.
171 NameSize
= AsciiStrSize (TokenSpaceName
) + AsciiStrSize (PcdName
);
172 Name
= AllocateZeroPool (NameSize
);
173 ASSERT (Name
!= NULL
);
175 // Catenate TokenSpaceCName and PcdCName with a '.' to form the full PCD name.
177 AsciiStrCatS (Name
, NameSize
, TokenSpaceName
);
178 Name
[AsciiStrSize (TokenSpaceName
) - sizeof (CHAR8
)] = '.';
179 AsciiStrCatS (Name
, NameSize
, PcdName
);
186 Retrieve additional information associated with a PCD token.
188 This includes information such as the type of value the TokenNumber is associated with as well as possible
189 human readable name that is associated with the token.
191 @param[in] IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
192 If FALSE, the pcd entry is initialized in DXE phase.
193 @param[in] Guid The 128-bit unique value that designates the namespace from which to extract the value.
194 @param[in] TokenNumber The PCD token number.
195 @param[out] PcdInfo The returned information associated with the requested TokenNumber.
196 The caller is responsible for freeing the buffer that is allocated by callee for PcdInfo->PcdName.
198 @retval EFI_SUCCESS The PCD information was returned successfully
199 @retval EFI_NOT_FOUND The PCD service could not find the requested token number.
204 IN CONST EFI_GUID
*Guid
,
205 IN UINTN TokenNumber
,
206 OUT EFI_PCD_INFO
*PcdInfo
209 PCD_DATABASE_INIT
*Database
;
213 DYNAMICEX_MAPPING
*ExMapTable
;
215 UINT32 LocalTokenNumber
;
217 Database
= IsPeiDb
? mPcdDatabase
.PeiDb
: mPcdDatabase
.DxeDb
;
219 GuidTable
= (EFI_GUID
*)((UINT8
*)Database
+ Database
->GuidTableOffset
);
220 MatchGuid
= ScanGuid (GuidTable
, Database
->GuidTableCount
* sizeof (EFI_GUID
), Guid
);
222 if (MatchGuid
== NULL
) {
223 return EFI_NOT_FOUND
;
226 GuidTableIdx
= MatchGuid
- GuidTable
;
228 ExMapTable
= (DYNAMICEX_MAPPING
*)((UINT8
*)Database
+ Database
->ExMapTableOffset
);
231 // Find the PCD by GuidTableIdx and ExTokenNumber in ExMapTable.
233 for (Index
= 0; Index
< Database
->ExTokenCount
; Index
++) {
234 if (ExMapTable
[Index
].ExGuidIndex
== GuidTableIdx
) {
235 if (TokenNumber
== PCD_INVALID_TOKEN_NUMBER
) {
237 // TokenNumber is 0, follow spec to set PcdType to EFI_PCD_TYPE_8,
238 // PcdSize to 0 and PcdName to the null-terminated ASCII string
239 // associated with the token's namespace Guid.
241 PcdInfo
->PcdType
= EFI_PCD_TYPE_8
;
242 PcdInfo
->PcdSize
= 0;
244 // Here use one representative in the token space to get the TokenSpaceCName.
246 PcdInfo
->PcdName
= GetPcdName (TRUE
, IsPeiDb
, ExMapTable
[Index
].TokenNumber
);
248 } else if (ExMapTable
[Index
].ExTokenNumber
== TokenNumber
) {
249 PcdInfo
->PcdSize
= DxePcdGetSize (ExMapTable
[Index
].TokenNumber
);
250 LocalTokenNumber
= GetLocalTokenNumber (IsPeiDb
, ExMapTable
[Index
].TokenNumber
);
251 PcdInfo
->PcdType
= GetPcdType (LocalTokenNumber
);
252 PcdInfo
->PcdName
= GetPcdName (FALSE
, IsPeiDb
, ExMapTable
[Index
].TokenNumber
);
258 return EFI_NOT_FOUND
;
262 Retrieve additional information associated with a PCD token.
264 This includes information such as the type of value the TokenNumber is associated with as well as possible
265 human readable name that is associated with the token.
267 @param[in] Guid The 128-bit unique value that designates the namespace from which to extract the value.
268 @param[in] TokenNumber The PCD token number.
269 @param[out] PcdInfo The returned information associated with the requested TokenNumber.
270 The caller is responsible for freeing the buffer that is allocated by callee for PcdInfo->PcdName.
272 @retval EFI_SUCCESS The PCD information was returned successfully.
273 @retval EFI_NOT_FOUND The PCD service could not find the requested token number.
277 IN CONST EFI_GUID
*Guid
,
278 IN UINTN TokenNumber
,
279 OUT EFI_PCD_INFO
*PcdInfo
283 BOOLEAN PeiExMapTableEmpty
;
284 BOOLEAN DxeExMapTableEmpty
;
285 UINT32 LocalTokenNumber
;
288 ASSERT (PcdInfo
!= NULL
);
290 Status
= EFI_NOT_FOUND
;
291 PeiExMapTableEmpty
= mPeiExMapTableEmpty
;
292 DxeExMapTableEmpty
= mDxeExMapTableEmpty
;
295 if (((TokenNumber
+ 1 > mPeiNexTokenCount
+ 1) && (TokenNumber
+ 1 <= mPeiLocalTokenCount
+ 1)) ||
296 ((TokenNumber
+ 1 > (mPeiLocalTokenCount
+ mDxeNexTokenCount
+ 1))))
298 return EFI_NOT_FOUND
;
299 } else if (TokenNumber
== PCD_INVALID_TOKEN_NUMBER
) {
301 // TokenNumber is 0, follow spec to set PcdType to EFI_PCD_TYPE_8,
302 // PcdSize to 0 and PcdName to NULL for default Token Space.
304 PcdInfo
->PcdType
= EFI_PCD_TYPE_8
;
305 PcdInfo
->PcdSize
= 0;
306 PcdInfo
->PcdName
= NULL
;
308 PcdInfo
->PcdSize
= DxePcdGetSize (TokenNumber
);
310 if ((TokenNumber
+ 1 <= mPeiNexTokenCount
+ 1)) {
314 LocalTokenNumber
= GetLocalTokenNumber (IsPeiDb
, TokenNumber
);
315 PcdInfo
->PcdType
= GetPcdType (LocalTokenNumber
);
316 PcdInfo
->PcdName
= GetPcdName (FALSE
, IsPeiDb
, TokenNumber
);
322 if (PeiExMapTableEmpty
&& DxeExMapTableEmpty
) {
323 return EFI_NOT_FOUND
;
326 if (!PeiExMapTableEmpty
) {
327 Status
= ExGetPcdInfo (
335 if (Status
== EFI_SUCCESS
) {
339 if (!DxeExMapTableEmpty
) {
340 Status
= ExGetPcdInfo (
352 Get the PCD entry pointer in PCD database.
354 This routine will visit PCD database to find the PCD entry according to given
355 token number. The given token number is autogened by build tools and it will be
356 translated to local token number. Local token number contains PCD's type and
357 offset of PCD entry in PCD database.
359 @param TokenNumber Token's number, it is autogened by build tools
360 @param GetSize The size of token's value
362 @return PCD entry pointer in PCD database
367 IN UINTN TokenNumber
,
375 VARIABLE_HEAD
*VariableHead
;
376 UINT8
*VaraiableDefaultBuffer
;
381 UINTN TmpTokenNumber
;
384 UINT32 LocalTokenNumber
;
386 STRING_HEAD StringTableIdx
;
390 // Aquire lock to prevent reentrance from TPL_CALLBACK level
392 EfiAcquireLock (&mPcdDatabaseLock
);
396 ASSERT (TokenNumber
> 0);
398 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
399 // We have to decrement TokenNumber by 1 to make it usable
400 // as the array index.
404 TmpTokenNumber
= TokenNumber
;
407 // EBC compiler is very choosy. It may report warning about comparison
408 // between UINTN and 0 . So we add 1 in each size of the
411 ASSERT (TokenNumber
+ 1 < mPcdTotalTokenCount
+ 1);
413 ASSERT ((GetSize
== DxePcdGetSize (TokenNumber
+ 1)) || (GetSize
== 0));
415 // EBC compiler is very choosy. It may report warning about comparison
416 // between UINTN and 0 . So we add 1 in each size of the
418 IsPeiDb
= (BOOLEAN
)((TokenNumber
+ 1 < mPeiLocalTokenCount
+ 1) ? TRUE
: FALSE
);
420 LocalTokenNumber
= GetLocalTokenNumber (IsPeiDb
, TokenNumber
+ 1);
422 PcdDb
= IsPeiDb
? ((UINT8
*)mPcdDatabase
.PeiDb
) : ((UINT8
*)mPcdDatabase
.DxeDb
);
425 StringTable
= (UINT8
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->StringTableOffset
);
427 StringTable
= (UINT8
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->StringTableOffset
);
430 Offset
= LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
;
432 switch (LocalTokenNumber
& PCD_TYPE_ALL_SET
) {
434 VpdHead
= (VPD_HEAD
*)((UINT8
*)PcdDb
+ Offset
);
435 ASSERT (mVpdBaseAddress
!= 0);
436 RetPtr
= (VOID
*)(mVpdBaseAddress
+ VpdHead
->Offset
);
440 case PCD_TYPE_HII
|PCD_TYPE_STRING
:
443 GuidTable
= (EFI_GUID
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->GuidTableOffset
);
445 GuidTable
= (EFI_GUID
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->GuidTableOffset
);
448 VariableHead
= (VARIABLE_HEAD
*)(PcdDb
+ Offset
);
449 Guid
= GuidTable
+ VariableHead
->GuidTableIndex
;
450 Name
= (UINT16
*)(StringTable
+ VariableHead
->StringIndex
);
452 if ((LocalTokenNumber
& PCD_TYPE_ALL_SET
) == (PCD_TYPE_HII
|PCD_TYPE_STRING
)) {
454 // If a HII type PCD's datum type is VOID*, the DefaultValueOffset is the index of
455 // string array in string table.
457 StringTableIdx
= *(STRING_HEAD
*)((UINT8
*)PcdDb
+ VariableHead
->DefaultValueOffset
);
458 VaraiableDefaultBuffer
= (UINT8
*)(StringTable
+ StringTableIdx
);
460 VaraiableDefaultBuffer
= (UINT8
*)PcdDb
+ VariableHead
->DefaultValueOffset
;
463 Status
= GetHiiVariable (Guid
, Name
, &Data
, &DataSize
);
464 if (Status
== EFI_SUCCESS
) {
465 if (DataSize
>= (VariableHead
->Offset
+ GetSize
)) {
468 // It is a pointer type. So get the MaxSize reserved for
471 GetPtrTypeSize (TmpTokenNumber
, &GetSize
);
472 if (GetSize
> (DataSize
- VariableHead
->Offset
)) {
474 // Use actual valid size.
476 GetSize
= DataSize
- VariableHead
->Offset
;
481 // If the operation is successful, we copy the data
482 // to the default value buffer in the PCD Database.
483 // So that we can free the Data allocated in GetHiiVariable.
485 CopyMem (VaraiableDefaultBuffer
, Data
+ VariableHead
->Offset
, GetSize
);
491 RetPtr
= (VOID
*)VaraiableDefaultBuffer
;
494 case PCD_TYPE_STRING
:
495 StringTableIdx
= *(STRING_HEAD
*)((UINT8
*)PcdDb
+ Offset
);
496 RetPtr
= (VOID
*)(StringTable
+ StringTableIdx
);
500 RetPtr
= (VOID
*)((UINT8
*)PcdDb
+ Offset
);
508 EfiReleaseLock (&mPcdDatabaseLock
);
514 Register the callback function for a PCD entry.
516 This routine will register a callback function to a PCD entry by given token number
517 and token space guid.
519 @param TokenNumber PCD token's number, it is autogened by build tools.
520 @param Guid PCD token space's guid,
521 if not NULL, this PCD is dynamicEx type PCD.
522 @param CallBackFunction Callback function pointer
524 @return EFI_SUCCESS Always success for registering callback function.
528 DxeRegisterCallBackWorker (
529 IN UINTN TokenNumber
,
530 IN CONST EFI_GUID
*Guid OPTIONAL
,
531 IN PCD_PROTOCOL_CALLBACK CallBackFunction
534 CALLBACK_FN_ENTRY
*FnTableEntry
;
535 LIST_ENTRY
*ListHead
;
536 LIST_ENTRY
*ListNode
;
539 TokenNumber
= GetExPcdTokenNumber (Guid
, (UINT32
)TokenNumber
);
543 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
544 // We have to decrement TokenNumber by 1 to make it usable
545 // as the array index of mCallbackFnTable[].
547 ListHead
= &mCallbackFnTable
[TokenNumber
- 1];
548 ListNode
= GetFirstNode (ListHead
);
550 while (ListNode
!= ListHead
) {
551 FnTableEntry
= CR_FNENTRY_FROM_LISTNODE (ListNode
, CALLBACK_FN_ENTRY
, Node
);
553 if (FnTableEntry
->CallbackFn
== CallBackFunction
) {
555 // We only allow a Callback function to be register once
556 // for a TokenNumber. So just return EFI_SUCCESS
561 ListNode
= GetNextNode (ListHead
, ListNode
);
564 FnTableEntry
= AllocatePool (sizeof (CALLBACK_FN_ENTRY
));
565 ASSERT (FnTableEntry
!= NULL
);
567 FnTableEntry
->CallbackFn
= CallBackFunction
;
568 InsertTailList (ListHead
, &FnTableEntry
->Node
);
574 UnRegister the callback function for a PCD entry.
576 This routine will unregister a callback function to a PCD entry by given token number
577 and token space guid.
579 @param TokenNumber PCD token's number, it is autogened by build tools.
580 @param Guid PCD token space's guid.
581 if not NULL, this PCD is dynamicEx type PCD.
582 @param CallBackFunction Callback function pointer
584 @retval EFI_SUCCESS Callback function is success to be unregister.
585 @retval EFI_INVALID_PARAMETER Can not find the PCD entry by given token number.
588 DxeUnRegisterCallBackWorker (
589 IN UINTN TokenNumber
,
590 IN CONST EFI_GUID
*Guid OPTIONAL
,
591 IN PCD_PROTOCOL_CALLBACK CallBackFunction
594 CALLBACK_FN_ENTRY
*FnTableEntry
;
595 LIST_ENTRY
*ListHead
;
596 LIST_ENTRY
*ListNode
;
599 TokenNumber
= GetExPcdTokenNumber (Guid
, (UINT32
)TokenNumber
);
603 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
604 // We have to decrement TokenNumber by 1 to make it usable
605 // as the array index of mCallbackFnTable[].
607 ListHead
= &mCallbackFnTable
[TokenNumber
- 1];
608 ListNode
= GetFirstNode (ListHead
);
610 while (ListNode
!= ListHead
) {
611 FnTableEntry
= CR_FNENTRY_FROM_LISTNODE (ListNode
, CALLBACK_FN_ENTRY
, Node
);
613 if (FnTableEntry
->CallbackFn
== CallBackFunction
) {
615 // We only allow a Callback function to be register once
616 // for a TokenNumber. So we can safely remove the Node from
617 // the Link List and return EFI_SUCCESS.
619 RemoveEntryList (ListNode
);
620 FreePool (FnTableEntry
);
625 ListNode
= GetNextNode (ListHead
, ListNode
);
628 return EFI_INVALID_PARAMETER
;
632 Get next token number in given token space.
634 This routine is used for dynamicEx type PCD. It will firstly scan token space
635 table to get token space according to given token space guid. Then scan given
636 token number in found token space, if found, then return next token number in
639 @param Guid Token space guid. Next token number will be scaned in
641 @param TokenNumber Token number.
642 If PCD_INVALID_TOKEN_NUMBER, return first token number in
644 If not PCD_INVALID_TOKEN_NUMBER, return next token number
645 in token space table.
646 @param GuidTable Token space guid table. It will be used for scan token space
647 by given token space guid.
648 @param SizeOfGuidTable The size of guid table.
649 @param ExMapTable DynamicEx token number mapping table.
650 @param SizeOfExMapTable The size of dynamicEx token number mapping table.
652 @retval EFI_NOT_FOUND Can not given token space or token number.
653 @retval EFI_SUCCESS Success to get next token number.
657 ExGetNextTokeNumber (
658 IN CONST EFI_GUID
*Guid
,
659 IN OUT UINTN
*TokenNumber
,
660 IN EFI_GUID
*GuidTable
,
661 IN UINTN SizeOfGuidTable
,
662 IN DYNAMICEX_MAPPING
*ExMapTable
,
663 IN UINTN SizeOfExMapTable
670 UINTN ExMapTableCount
;
673 // Scan token space guid
675 MatchGuid
= ScanGuid (GuidTable
, SizeOfGuidTable
, Guid
);
676 if (MatchGuid
== NULL
) {
677 return EFI_NOT_FOUND
;
681 // Find the token space table in dynamicEx mapping table.
684 GuidTableIdx
= MatchGuid
- GuidTable
;
685 ExMapTableCount
= SizeOfExMapTable
/ sizeof (ExMapTable
[0]);
686 for (Index
= 0; Index
< ExMapTableCount
; Index
++) {
687 if (ExMapTable
[Index
].ExGuidIndex
== GuidTableIdx
) {
695 // If given token number is PCD_INVALID_TOKEN_NUMBER, then return the first
696 // token number in found token space.
698 if (*TokenNumber
== PCD_INVALID_TOKEN_NUMBER
) {
699 *TokenNumber
= ExMapTable
[Index
].ExTokenNumber
;
703 for ( ; Index
< ExMapTableCount
; Index
++) {
704 if ((ExMapTable
[Index
].ExTokenNumber
== *TokenNumber
) && (ExMapTable
[Index
].ExGuidIndex
== GuidTableIdx
)) {
709 while (Index
< ExMapTableCount
) {
711 if (Index
== ExMapTableCount
) {
713 // Exceed the length of ExMap Table
715 *TokenNumber
= PCD_INVALID_TOKEN_NUMBER
;
716 return EFI_NOT_FOUND
;
717 } else if (ExMapTable
[Index
].ExGuidIndex
== GuidTableIdx
) {
719 // Found the next match
721 *TokenNumber
= ExMapTable
[Index
].ExTokenNumber
;
727 return EFI_NOT_FOUND
;
731 Find the PCD database.
733 @retval The base address of external PCD database binary.
734 @retval NULL Return NULL if not find.
744 // Search the External Pcd database from one section of current FFS,
745 // and read it to memory
747 Status
= GetSectionFromFfs (
750 (VOID
**)&mDxePcdDbBinary
,
753 ASSERT_EFI_ERROR (Status
);
756 // Check the first bytes (Header Signature Guid) and build version.
758 if (!CompareGuid ((VOID
*)mDxePcdDbBinary
, &gPcdDataBaseSignatureGuid
) ||
759 (mDxePcdDbBinary
->BuildVersion
!= PCD_SERVICE_DXE_VERSION
))
764 return mDxePcdDbBinary
;
768 Update PCD database base on current SkuId
770 @param SkuId Current SkuId
771 @param IsPeiDb Whether to update PEI PCD database.
773 @retval EFI_SUCCESS Update PCD database successfully.
774 @retval EFI_NOT_FOUND Not found PCD database for current SkuId.
783 PCD_DATABASE_SKU_DELTA
*SkuDelta
;
784 PCD_DATA_DELTA
*SkuDeltaData
;
786 if (IsPeiDb
&& (mPeiPcdDbBinary
!= NULL
)) {
788 // Find the delta data for PEI DB
790 Index
= (mPcdDatabase
.PeiDb
->Length
+ 7) & (~7);
792 while (Index
< mPeiPcdDbSize
) {
793 SkuDelta
= (PCD_DATABASE_SKU_DELTA
*)((UINT8
*)mPeiPcdDbBinary
+ Index
);
794 if ((SkuDelta
->SkuId
== SkuId
) && (SkuDelta
->SkuIdCompared
== 0)) {
798 Index
= (Index
+ SkuDelta
->Length
+ 7) & (~7);
802 // Patch the delta data into current PCD database
804 if ((Index
< mPeiPcdDbSize
) && (SkuDelta
!= NULL
)) {
805 SkuDeltaData
= (PCD_DATA_DELTA
*)(SkuDelta
+ 1);
806 while ((UINT8
*)SkuDeltaData
< (UINT8
*)SkuDelta
+ SkuDelta
->Length
) {
807 *((UINT8
*)mPcdDatabase
.PeiDb
+ SkuDeltaData
->Offset
) = (UINT8
)SkuDeltaData
->Value
;
811 return EFI_NOT_FOUND
;
816 // Find the delta data for DXE DB
818 Index
= (mPcdDatabase
.DxeDb
->Length
+ 7) & (~7);
821 if (Index
== mDxePcdDbSize
) {
825 while (Index
< mDxePcdDbSize
) {
826 SkuDelta
= (PCD_DATABASE_SKU_DELTA
*)((UINT8
*)mDxePcdDbBinary
+ Index
);
827 if ((SkuDelta
->SkuId
== SkuId
) && (SkuDelta
->SkuIdCompared
== 0)) {
831 Index
= (Index
+ SkuDelta
->Length
+ 7) & (~7);
835 // Patch the delta data into current PCD database
837 if ((Index
< mDxePcdDbSize
) && (SkuDelta
!= NULL
)) {
838 SkuDeltaData
= (PCD_DATA_DELTA
*)(SkuDelta
+ 1);
839 while ((UINT8
*)SkuDeltaData
< (UINT8
*)SkuDelta
+ SkuDelta
->Length
) {
840 *((UINT8
*)mPcdDatabase
.DxeDb
+ SkuDeltaData
->Offset
) = (UINT8
)SkuDeltaData
->Value
;
847 return EFI_NOT_FOUND
;
851 Initialize the PCD database in DXE phase.
853 PCD database in DXE phase also contains PCD database in PEI phase which is copied
858 BuildPcdDxeDataBase (
862 PEI_PCD_DATABASE
*PeiDatabase
;
863 EFI_HOB_GUID_TYPE
*GuidHob
;
870 // Assign PCD Entries with default value to PCD DATABASE
872 mPcdDatabase
.DxeDb
= LocateExPcdBinary ();
873 ASSERT (mPcdDatabase
.DxeDb
!= NULL
);
874 PcdDxeDbLen
= mPcdDatabase
.DxeDb
->Length
+ mPcdDatabase
.DxeDb
->UninitDataBaseSize
;
875 PcdDxeDb
= AllocateZeroPool (PcdDxeDbLen
);
876 ASSERT (PcdDxeDb
!= NULL
);
877 CopyMem (PcdDxeDb
, mPcdDatabase
.DxeDb
, mPcdDatabase
.DxeDb
->Length
);
878 mPcdDatabase
.DxeDb
= PcdDxeDb
;
880 GuidHob
= GetFirstGuidHob (&gPcdDataBaseHobGuid
);
881 if (GuidHob
!= NULL
) {
883 // If no PEIMs use dynamic Pcd Entry, the Pcd Service PEIM
884 // should not be included at all. So the GuidHob could
885 // be NULL. If it is NULL, we just copy over the DXE Default
886 // Value to PCD Database.
888 PeiDatabase
= (PEI_PCD_DATABASE
*)GET_GUID_HOB_DATA (GuidHob
);
891 // Get next one that stores full PEI data
893 GuidHob
= GetNextGuidHob (&gPcdDataBaseHobGuid
, GET_NEXT_HOB (GuidHob
));
894 if (GuidHob
!= NULL
) {
895 mPeiPcdDbBinary
= (PEI_PCD_DATABASE
*)GET_GUID_HOB_DATA (GuidHob
);
896 mPeiPcdDbSize
= (UINTN
)GET_GUID_HOB_DATA_SIZE (GuidHob
);
900 // Assign PCD Entries refereneced in PEI phase to PCD DATABASE
902 mPcdDatabase
.PeiDb
= PeiDatabase
;
904 // Inherit the SystemSkuId from PEI phase.
906 if (mPcdDatabase
.PeiDb
->SystemSkuId
!= 0) {
907 Status
= UpdatePcdDatabase (mPcdDatabase
.PeiDb
->SystemSkuId
, FALSE
);
908 ASSERT_EFI_ERROR (Status
);
911 mPcdDatabase
.DxeDb
->SystemSkuId
= mPcdDatabase
.PeiDb
->SystemSkuId
;
913 mPcdDatabase
.PeiDb
= AllocateZeroPool (sizeof (PEI_PCD_DATABASE
));
914 ASSERT (mPcdDatabase
.PeiDb
!= NULL
);
918 // Initialized the external PCD database local variables
920 mPeiLocalTokenCount
= mPcdDatabase
.PeiDb
->LocalTokenCount
;
921 mDxeLocalTokenCount
= mPcdDatabase
.DxeDb
->LocalTokenCount
;
923 mPeiExMapppingTableSize
= mPcdDatabase
.PeiDb
->ExTokenCount
* sizeof (DYNAMICEX_MAPPING
);
924 mDxeExMapppingTableSize
= mPcdDatabase
.DxeDb
->ExTokenCount
* sizeof (DYNAMICEX_MAPPING
);
925 mPeiGuidTableSize
= mPcdDatabase
.PeiDb
->GuidTableCount
* sizeof (GUID
);
926 mDxeGuidTableSize
= mPcdDatabase
.DxeDb
->GuidTableCount
* sizeof (GUID
);
928 mPcdTotalTokenCount
= mPeiLocalTokenCount
+ mDxeLocalTokenCount
;
929 mPeiNexTokenCount
= mPeiLocalTokenCount
- mPcdDatabase
.PeiDb
->ExTokenCount
;
930 mDxeNexTokenCount
= mDxeLocalTokenCount
- mPcdDatabase
.DxeDb
->ExTokenCount
;
932 mPeiExMapTableEmpty
= (mPcdDatabase
.PeiDb
->ExTokenCount
== 0) ? TRUE
: FALSE
;
933 mDxeExMapTableEmpty
= (mPcdDatabase
.DxeDb
->ExTokenCount
== 0) ? TRUE
: FALSE
;
934 mPeiDatabaseEmpty
= (mPeiLocalTokenCount
== 0) ? TRUE
: FALSE
;
936 TmpTokenSpaceBufferCount
= mPcdDatabase
.PeiDb
->ExTokenCount
+ mPcdDatabase
.DxeDb
->ExTokenCount
;
937 TmpTokenSpaceBuffer
= (EFI_GUID
**)AllocateZeroPool (TmpTokenSpaceBufferCount
* sizeof (EFI_GUID
*));
940 // Initialized the Callback Function Table
942 mCallbackFnTable
= AllocateZeroPool (mPcdTotalTokenCount
* sizeof (LIST_ENTRY
));
943 ASSERT (mCallbackFnTable
!= NULL
);
946 // EBC compiler is very choosy. It may report warning about comparison
947 // between UINTN and 0 . So we add 1 in each size of the
950 for (Index
= 0; Index
+ 1 < mPcdTotalTokenCount
+ 1; Index
++) {
951 InitializeListHead (&mCallbackFnTable
[Index
]);
956 Get Variable which contains HII type PCD entry.
958 @param VariableGuid Variable's guid
959 @param VariableName Variable's unicode name string
960 @param VariableData Variable's data pointer,
961 @param VariableSize Variable's size.
963 @return the status of gRT->GetVariable
967 IN EFI_GUID
*VariableGuid
,
968 IN UINT16
*VariableName
,
969 OUT UINT8
**VariableData
,
970 OUT UINTN
*VariableSize
981 // Firstly get the real size of HII variable
983 Status
= gRT
->GetVariable (
984 (UINT16
*)VariableName
,
992 // Allocate buffer to hold whole variable data according to variable size.
994 if (Status
== EFI_BUFFER_TOO_SMALL
) {
995 Buffer
= (UINT8
*)AllocatePool (Size
);
997 ASSERT (Buffer
!= NULL
);
999 Status
= gRT
->GetVariable (
1007 ASSERT (Status
== EFI_SUCCESS
);
1008 *VariableData
= Buffer
;
1009 *VariableSize
= Size
;
1012 // Use Default Data only when variable is not found.
1013 // For other error status, correct data can't be got, and trig ASSERT().
1015 ASSERT (Status
== EFI_NOT_FOUND
);
1022 Invoke the callback function when dynamic PCD entry was set, if this PCD entry
1023 has registered callback function.
1025 @param ExTokenNumber DynamicEx PCD's token number, if this PCD entry is dyanmicEx
1027 @param Guid DynamicEx PCD's guid, if this PCD entry is dynamicEx type
1029 @param TokenNumber PCD token number generated by build tools.
1030 @param Data Value want to be set for this PCD entry
1031 @param Size The size of value
1035 InvokeCallbackOnSet (
1036 UINT32 ExTokenNumber
,
1037 CONST EFI_GUID
*Guid OPTIONAL
,
1043 CALLBACK_FN_ENTRY
*FnTableEntry
;
1044 LIST_ENTRY
*ListHead
;
1045 LIST_ENTRY
*ListNode
;
1048 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
1049 // We have to decrement TokenNumber by 1 to make it usable
1050 // as the array index of mCallbackFnTable[].
1052 ListHead
= &mCallbackFnTable
[TokenNumber
- 1];
1053 ListNode
= GetFirstNode (ListHead
);
1055 while (ListNode
!= ListHead
) {
1056 FnTableEntry
= CR_FNENTRY_FROM_LISTNODE (ListNode
, CALLBACK_FN_ENTRY
, Node
);
1058 FnTableEntry
->CallbackFn (
1060 (Guid
== NULL
) ? TokenNumber
: ExTokenNumber
,
1065 ListNode
= GetNextNode (ListHead
, ListNode
);
1072 Wrapper function for setting non-pointer type value for a PCD entry.
1074 @param TokenNumber Pcd token number autogenerated by build tools.
1075 @param Data Value want to be set for PCD entry
1076 @param Size Size of value.
1078 @return status of SetWorker.
1083 IN UINTN TokenNumber
,
1088 return SetWorker (TokenNumber
, Data
, &Size
, FALSE
);
1092 Set value for an PCD entry
1094 @param TokenNumber Pcd token number autogenerated by build tools.
1095 @param Data Value want to be set for PCD entry
1096 @param Size Size of value.
1097 @param PtrType If TRUE, the type of PCD entry's value is Pointer.
1098 If False, the type of PCD entry's value is not Pointer.
1100 @retval EFI_INVALID_PARAMETER If this PCD type is VPD, VPD PCD can not be set.
1101 @retval EFI_INVALID_PARAMETER If Size can not be set to size table.
1102 @retval EFI_INVALID_PARAMETER If Size of non-Ptr type PCD does not match the size information in PCD database.
1103 @retval EFI_NOT_FOUND If value type of PCD entry is intergrate, but not in
1104 range of UINT8, UINT16, UINT32, UINT64
1105 @retval EFI_NOT_FOUND Can not find the PCD type according to token number.
1109 IN UINTN TokenNumber
,
1116 UINT32 LocalTokenNumber
;
1117 EFI_GUID
*GuidTable
;
1121 UINTN VariableOffset
;
1124 VARIABLE_HEAD
*VariableHead
;
1129 UINTN TmpTokenNumber
;
1132 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
1133 // We have to decrement TokenNumber by 1 to make it usable
1134 // as the array index.
1138 TmpTokenNumber
= TokenNumber
;
1141 // EBC compiler is very choosy. It may report warning about comparison
1142 // between UINTN and 0 . So we add 1 in each size of the
1145 ASSERT (TokenNumber
+ 1 < mPcdTotalTokenCount
+ 1);
1149 // Get MaxSize first, then check new size with max buffer size.
1151 GetPtrTypeSize (TokenNumber
, &MaxSize
);
1152 if (*Size
> MaxSize
) {
1154 return EFI_INVALID_PARAMETER
;
1157 if (*Size
!= DxePcdGetSize (TokenNumber
+ 1)) {
1158 return EFI_INVALID_PARAMETER
;
1163 // EBC compiler is very choosy. It may report warning about comparison
1164 // between UINTN and 0 . So we add 1 in each size of the
1167 if ((TokenNumber
+ 1 < mPeiNexTokenCount
+ 1) ||
1168 ((TokenNumber
+ 1 >= mPeiLocalTokenCount
+ 1) && (TokenNumber
+ 1 < (mPeiLocalTokenCount
+ mDxeNexTokenCount
+ 1))))
1170 InvokeCallbackOnSet (0, NULL
, TokenNumber
+ 1, Data
, *Size
);
1174 // Aquire lock to prevent reentrance from TPL_CALLBACK level
1176 EfiAcquireLock (&mPcdDatabaseLock
);
1179 // EBC compiler is very choosy. It may report warning about comparison
1180 // between UINTN and 0 . So we add 1 in each size of the
1183 IsPeiDb
= (BOOLEAN
)((TokenNumber
+ 1 < mPeiLocalTokenCount
+ 1) ? TRUE
: FALSE
);
1185 LocalTokenNumber
= GetLocalTokenNumber (IsPeiDb
, TokenNumber
+ 1);
1187 Offset
= LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
;
1189 PcdDb
= IsPeiDb
? ((UINT8
*)mPcdDatabase
.PeiDb
) : ((UINT8
*)mPcdDatabase
.DxeDb
);
1192 StringTable
= (UINT8
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->StringTableOffset
);
1194 StringTable
= (UINT8
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->StringTableOffset
);
1197 InternalData
= PcdDb
+ Offset
;
1199 switch (LocalTokenNumber
& PCD_TYPE_ALL_SET
) {
1202 Status
= EFI_INVALID_PARAMETER
;
1205 case PCD_TYPE_STRING
:
1206 if (SetPtrTypeSize (TmpTokenNumber
, Size
)) {
1207 CopyMem (StringTable
+ *((STRING_HEAD
*)InternalData
), Data
, *Size
);
1208 Status
= EFI_SUCCESS
;
1210 Status
= EFI_INVALID_PARAMETER
;
1215 case PCD_TYPE_HII
|PCD_TYPE_STRING
:
1218 if (!SetPtrTypeSize (TmpTokenNumber
, Size
)) {
1219 Status
= EFI_INVALID_PARAMETER
;
1225 GuidTable
= (EFI_GUID
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->GuidTableOffset
);
1227 GuidTable
= (EFI_GUID
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->GuidTableOffset
);
1230 VariableHead
= (VARIABLE_HEAD
*)(PcdDb
+ Offset
);
1232 Guid
= GuidTable
+ VariableHead
->GuidTableIndex
;
1233 Name
= (UINT16
*)(StringTable
+ VariableHead
->StringIndex
);
1234 VariableOffset
= VariableHead
->Offset
;
1235 Attributes
= VariableHead
->Attributes
;
1236 Status
= SetHiiVariable (Guid
, Name
, Attributes
, Data
, *Size
, VariableOffset
);
1241 if (SetPtrTypeSize (TmpTokenNumber
, Size
)) {
1242 CopyMem (InternalData
, Data
, *Size
);
1243 Status
= EFI_SUCCESS
;
1245 Status
= EFI_INVALID_PARAMETER
;
1251 Status
= EFI_SUCCESS
;
1253 case sizeof (UINT8
):
1254 *((UINT8
*)InternalData
) = *((UINT8
*)Data
);
1257 case sizeof (UINT16
):
1258 *((UINT16
*)InternalData
) = *((UINT16
*)Data
);
1261 case sizeof (UINT32
):
1262 *((UINT32
*)InternalData
) = *((UINT32
*)Data
);
1265 case sizeof (UINT64
):
1266 *((UINT64
*)InternalData
) = *((UINT64
*)Data
);
1271 Status
= EFI_NOT_FOUND
;
1279 Status
= EFI_NOT_FOUND
;
1283 EfiReleaseLock (&mPcdDatabaseLock
);
1289 Wrapper function for get PCD value for dynamic-ex PCD.
1291 @param Guid Token space guid for dynamic-ex PCD.
1292 @param ExTokenNumber Token number for dynamic-ex PCD.
1293 @param GetSize The size of dynamic-ex PCD value.
1295 @return PCD entry in PCD database.
1300 IN CONST EFI_GUID
*Guid
,
1301 IN UINTN ExTokenNumber
,
1305 return GetWorker (GetExPcdTokenNumber (Guid
, (UINT32
)ExTokenNumber
), GetSize
);
1309 Wrapper function for set PCD value for non-Pointer type dynamic-ex PCD.
1311 @param ExTokenNumber Token number for dynamic-ex PCD.
1312 @param Guid Token space guid for dynamic-ex PCD.
1313 @param Data Value want to be set.
1314 @param SetSize The size of value.
1316 @return status of ExSetWorker().
1321 IN UINTN ExTokenNumber
,
1322 IN CONST EFI_GUID
*Guid
,
1327 return ExSetWorker (ExTokenNumber
, Guid
, Data
, &SetSize
, FALSE
);
1331 Set value for a dynamic-ex PCD entry.
1333 This routine find the local token number according to dynamic-ex PCD's token
1334 space guid and token number firstly, and invoke callback function if this PCD
1335 entry registered callback function. Finally, invoken general SetWorker to set
1338 @param ExTokenNumber Dynamic-ex PCD token number.
1339 @param Guid Token space guid for dynamic-ex PCD.
1340 @param Data PCD value want to be set
1341 @param SetSize Size of value.
1342 @param PtrType If TRUE, this PCD entry is pointer type.
1343 If FALSE, this PCD entry is not pointer type.
1345 @return status of SetWorker().
1350 IN UINTN ExTokenNumber
,
1351 IN CONST EFI_GUID
*Guid
,
1353 IN OUT UINTN
*SetSize
,
1359 TokenNumber
= GetExPcdTokenNumber (Guid
, (UINT32
)ExTokenNumber
);
1361 InvokeCallbackOnSet ((UINT32
)ExTokenNumber
, Guid
, TokenNumber
, Data
, *SetSize
);
1363 return SetWorker (TokenNumber
, Data
, SetSize
, PtrType
);
1367 Get variable size and data from HII-type PCDs.
1369 @param[in] VariableGuid Guid of variable which stored value of a HII-type PCD.
1370 @param[in] VariableName Unicode name of variable which stored value of a HII-type PCD.
1371 @param[out] VariableSize Pointer to variable size got from HII-type PCDs.
1372 @param[out] VariableData Pointer to variable data got from HII-type PCDs.
1376 GetVariableSizeAndDataFromHiiPcd (
1377 IN EFI_GUID
*VariableGuid
,
1378 IN UINT16
*VariableName
,
1379 OUT UINTN
*VariableSize
,
1380 OUT VOID
*VariableData OPTIONAL
1384 PCD_DATABASE_INIT
*Database
;
1386 UINT32 LocalTokenNumber
;
1388 EFI_GUID
*GuidTable
;
1390 VARIABLE_HEAD
*VariableHead
;
1395 UINT8
*VaraiableDefaultBuffer
;
1396 STRING_HEAD StringTableIdx
;
1401 // Go through PCD database to find out DynamicHii PCDs.
1403 for (TokenNumber
= 1; TokenNumber
<= mPcdTotalTokenCount
; TokenNumber
++) {
1404 IsPeiDb
= (BOOLEAN
)((TokenNumber
+ 1 < mPeiLocalTokenCount
+ 1) ? TRUE
: FALSE
);
1405 Database
= IsPeiDb
? mPcdDatabase
.PeiDb
: mPcdDatabase
.DxeDb
;
1406 LocalTokenNumber
= GetLocalTokenNumber (IsPeiDb
, TokenNumber
);
1407 if ((LocalTokenNumber
& PCD_TYPE_HII
) != 0) {
1409 // Get the Variable Guid and Name pointer.
1411 Offset
= LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
;
1412 VariableHead
= (VARIABLE_HEAD
*)((UINT8
*)Database
+ Offset
);
1413 StringTable
= (UINT8
*)((UINT8
*)Database
+ Database
->StringTableOffset
);
1414 GuidTable
= (EFI_GUID
*)((UINT8
*)Database
+ Database
->GuidTableOffset
);
1415 Guid
= GuidTable
+ VariableHead
->GuidTableIndex
;
1416 Name
= (UINT16
*)(StringTable
+ VariableHead
->StringIndex
);
1417 if (CompareGuid (VariableGuid
, Guid
) && (StrCmp (VariableName
, Name
) == 0)) {
1419 // It is the matched DynamicHii PCD.
1421 PcdDataSize
= DxePcdGetSize (TokenNumber
);
1422 Size
= VariableHead
->Offset
+ PcdDataSize
;
1423 if (Size
> *VariableSize
) {
1424 *VariableSize
= Size
;
1427 if (VariableData
!= NULL
) {
1428 if ((LocalTokenNumber
& PCD_TYPE_ALL_SET
) == (PCD_TYPE_HII
|PCD_TYPE_STRING
)) {
1430 // If a HII type PCD's datum type is VOID*, the DefaultValueOffset is the index of
1431 // string array in string table.
1433 StringTableIdx
= *(STRING_HEAD
*)((UINT8
*)Database
+ VariableHead
->DefaultValueOffset
);
1434 VaraiableDefaultBuffer
= (UINT8
*)(StringTable
+ StringTableIdx
);
1436 VaraiableDefaultBuffer
= (UINT8
*)Database
+ VariableHead
->DefaultValueOffset
;
1439 CopyMem ((UINT8
*)VariableData
+ VariableHead
->Offset
, VaraiableDefaultBuffer
, PcdDataSize
);
1447 Set value for HII-type PCD.
1449 A HII-type PCD's value is stored in a variable. Setting/Getting the value of
1450 HII-type PCD is to visit this variable.
1452 @param VariableGuid Guid of variable which stored value of a HII-type PCD.
1453 @param VariableName Unicode name of variable which stored value of a HII-type PCD.
1454 @param SetAttributes Attributes bitmask to set for the variable.
1455 @param Data Value want to be set.
1456 @param DataSize Size of value
1457 @param Offset Value offset of HII-type PCD in variable.
1459 @return status of GetVariable()/SetVariable().
1464 IN EFI_GUID
*VariableGuid
,
1465 IN UINT16
*VariableName
,
1466 IN UINT32 SetAttributes
,
1467 IN CONST VOID
*Data
,
1482 // Try to get original variable size information.
1484 Status
= gRT
->GetVariable (
1485 (UINT16
*)VariableName
,
1492 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1494 // Patch new PCD's value to offset in given HII variable.
1496 if (Size
>= (DataSize
+ Offset
)) {
1499 SetSize
= DataSize
+ Offset
;
1502 Buffer
= AllocatePool (SetSize
);
1503 ASSERT (Buffer
!= NULL
);
1505 Status
= gRT
->GetVariable (
1513 ASSERT_EFI_ERROR (Status
);
1515 CopyMem ((UINT8
*)Buffer
+ Offset
, Data
, DataSize
);
1517 if (SetAttributes
== 0) {
1518 SetAttributes
= Attribute
;
1521 Status
= gRT
->SetVariable (
1531 } else if (Status
== EFI_NOT_FOUND
) {
1533 // If variable does not exist, a new variable need to be created.
1537 // Get size, allocate buffer and get data.
1539 GetVariableSizeAndDataFromHiiPcd (VariableGuid
, VariableName
, &Size
, NULL
);
1540 Buffer
= AllocateZeroPool (Size
);
1541 ASSERT (Buffer
!= NULL
);
1542 GetVariableSizeAndDataFromHiiPcd (VariableGuid
, VariableName
, &Size
, Buffer
);
1547 CopyMem ((UINT8
*)Buffer
+ Offset
, Data
, DataSize
);
1549 if (SetAttributes
== 0) {
1550 SetAttributes
= EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
;
1553 Status
= gRT
->SetVariable (
1566 // If we drop to here, the value is failed to be written in to variable area.
1572 Get Token Number according to dynamic-ex PCD's {token space guid:token number}
1574 A dynamic-ex type PCD, developer must provide pair of token space guid: token number
1575 in DEC file. PCD database maintain a mapping table that translate pair of {token
1576 space guid: token number} to Token Number.
1578 @param Guid Token space guid for dynamic-ex PCD entry.
1579 @param ExTokenNumber Dynamic-ex PCD token number.
1581 @return Token Number for dynamic-ex PCD.
1585 GetExPcdTokenNumber (
1586 IN CONST EFI_GUID
*Guid
,
1587 IN UINT32 ExTokenNumber
1591 DYNAMICEX_MAPPING
*ExMap
;
1592 EFI_GUID
*GuidTable
;
1593 EFI_GUID
*MatchGuid
;
1596 if (!mPeiDatabaseEmpty
) {
1597 ExMap
= (DYNAMICEX_MAPPING
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->ExMapTableOffset
);
1598 GuidTable
= (EFI_GUID
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->GuidTableOffset
);
1600 MatchGuid
= ScanGuid (GuidTable
, mPeiGuidTableSize
, Guid
);
1602 if (MatchGuid
!= NULL
) {
1603 MatchGuidIdx
= MatchGuid
- GuidTable
;
1605 for (Index
= 0; Index
< mPcdDatabase
.PeiDb
->ExTokenCount
; Index
++) {
1606 if ((ExTokenNumber
== ExMap
[Index
].ExTokenNumber
) &&
1607 (MatchGuidIdx
== ExMap
[Index
].ExGuidIndex
))
1609 return ExMap
[Index
].TokenNumber
;
1615 ExMap
= (DYNAMICEX_MAPPING
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->ExMapTableOffset
);
1616 GuidTable
= (EFI_GUID
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->GuidTableOffset
);
1618 MatchGuid
= ScanGuid (GuidTable
, mDxeGuidTableSize
, Guid
);
1620 // We need to ASSERT here. If GUID can't be found in GuidTable, this is a
1621 // error in the BUILD system.
1623 ASSERT (MatchGuid
!= NULL
);
1625 MatchGuidIdx
= MatchGuid
- GuidTable
;
1627 for (Index
= 0; Index
< mPcdDatabase
.DxeDb
->ExTokenCount
; Index
++) {
1628 if ((ExTokenNumber
== ExMap
[Index
].ExTokenNumber
) &&
1629 (MatchGuidIdx
== ExMap
[Index
].ExGuidIndex
))
1631 return ExMap
[Index
].TokenNumber
;
1635 DEBUG ((DEBUG_ERROR
, "%a: Failed to find PCD with GUID: %g and token number: %d\n", __FUNCTION__
, Guid
, ExTokenNumber
));
1642 Wrapper function of getting index of PCD entry in size table.
1644 @param LocalTokenNumberTableIdx Index of this PCD in local token number table.
1645 @param IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
1646 If FALSE, the pcd entry is initialized in DXE phase.
1648 @return index of PCD entry in size table.
1652 IN UINTN LocalTokenNumberTableIdx
,
1656 UINT32
*LocalTokenNumberTable
;
1657 UINTN LocalTokenNumber
;
1662 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
);
1664 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
);
1669 for (Index
= 0; Index
< LocalTokenNumberTableIdx
; Index
++) {
1670 LocalTokenNumber
= LocalTokenNumberTable
[Index
];
1672 if ((LocalTokenNumber
& PCD_DATUM_TYPE_ALL_SET
) == PCD_DATUM_TYPE_POINTER
) {
1674 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1677 if ((LocalTokenNumber
& PCD_TYPE_VPD
) != 0) {
1679 // We have only two entry for VPD enabled PCD entry:
1682 // Current size is equal to MAX size.
1687 // We have only two entry for Non-Sku enabled PCD entry:
1696 return SizeTableIdx
;
1700 Get size of POINTER type PCD value.
1702 @param LocalTokenNumberTableIdx Index of local token number in local token number table.
1703 @param MaxSize Maxmium size of POINTER type PCD value.
1705 @return size of POINTER type PCD value.
1710 IN UINTN LocalTokenNumberTableIdx
,
1715 UINTN LocalTokenNumber
;
1716 SIZE_INFO
*SizeTable
;
1718 UINT32
*LocalTokenNumberTable
;
1720 // EBC compiler is very choosy. It may report warning about comparison
1721 // between UINTN and 0 . So we add 1 in each size of the
1723 IsPeiDb
= (BOOLEAN
)(LocalTokenNumberTableIdx
+ 1 < mPeiLocalTokenCount
+ 1);
1726 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
);
1727 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->SizeTableOffset
);
1729 LocalTokenNumberTableIdx
-= mPeiLocalTokenCount
;
1730 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
);
1731 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->SizeTableOffset
);
1734 LocalTokenNumber
= LocalTokenNumberTable
[LocalTokenNumberTableIdx
];
1736 ASSERT ((LocalTokenNumber
& PCD_DATUM_TYPE_ALL_SET
) == PCD_DATUM_TYPE_POINTER
);
1738 SizeTableIdx
= GetSizeTableIndex (LocalTokenNumberTableIdx
, IsPeiDb
);
1740 *MaxSize
= SizeTable
[SizeTableIdx
];
1742 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1745 if ((LocalTokenNumber
& PCD_TYPE_VPD
) != 0) {
1747 // We have only two entry for VPD enabled PCD entry:
1750 // We consider current size is equal to MAX size.
1755 // We have only two entry for Non-Sku enabled PCD entry:
1759 return SizeTable
[SizeTableIdx
+ 1];
1764 Set size of POINTER type PCD value. The size should not exceed the maximum size
1767 @param LocalTokenNumberTableIdx Index of local token number in local token number table.
1768 @param CurrentSize Size of POINTER type PCD value.
1770 @retval TRUE Success to set size of PCD value.
1771 @retval FALSE Fail to set size of PCD value.
1775 IN UINTN LocalTokenNumberTableIdx
,
1776 IN OUT UINTN
*CurrentSize
1780 UINTN LocalTokenNumber
;
1781 SIZE_INFO
*SizeTable
;
1784 UINT32
*LocalTokenNumberTable
;
1787 // EBC compiler is very choosy. It may report warning about comparison
1788 // between UINTN and 0 . So we add 1 in each size of the
1791 IsPeiDb
= (BOOLEAN
)(LocalTokenNumberTableIdx
+ 1 < mPeiLocalTokenCount
+ 1);
1794 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
);
1795 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->SizeTableOffset
);
1797 LocalTokenNumberTableIdx
-= mPeiLocalTokenCount
;
1798 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
);
1799 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->SizeTableOffset
);
1802 LocalTokenNumber
= LocalTokenNumberTable
[LocalTokenNumberTableIdx
];
1804 ASSERT ((LocalTokenNumber
& PCD_DATUM_TYPE_ALL_SET
) == PCD_DATUM_TYPE_POINTER
);
1806 SizeTableIdx
= GetSizeTableIndex (LocalTokenNumberTableIdx
, IsPeiDb
);
1808 MaxSize
= SizeTable
[SizeTableIdx
];
1810 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1813 if ((LocalTokenNumber
& PCD_TYPE_VPD
) != 0) {
1815 // We shouldn't come here as we don't support SET for VPD
1820 if ((*CurrentSize
> MaxSize
) ||
1821 (*CurrentSize
== MAX_ADDRESS
))
1823 *CurrentSize
= MaxSize
;
1828 // We have only two entry for Non-Sku enabled PCD entry:
1832 SizeTable
[SizeTableIdx
+ 1] = (SIZE_INFO
)*CurrentSize
;
1838 VariableLock DynamicHiiPcd.
1840 @param[in] IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
1841 If FALSE, the pcd entry is initialized in DXE phase.
1842 @param[in] VariableLock Pointer to VariableLockProtocol.
1846 VariableLockDynamicHiiPcd (
1848 IN EDKII_VARIABLE_LOCK_PROTOCOL
*VariableLock
1852 PCD_DATABASE_INIT
*Database
;
1853 UINT32 LocalTokenCount
;
1855 UINT32 LocalTokenNumber
;
1857 EFI_GUID
*GuidTable
;
1859 VARIABLE_HEAD
*VariableHead
;
1863 Database
= IsPeiDb
? mPcdDatabase
.PeiDb
: mPcdDatabase
.DxeDb
;
1864 LocalTokenCount
= IsPeiDb
? mPeiLocalTokenCount
: mDxeLocalTokenCount
;
1867 // Go through PCD database to find out DynamicHii PCDs.
1869 for (TokenNumber
= 1; TokenNumber
<= LocalTokenCount
; TokenNumber
++) {
1871 LocalTokenNumber
= GetLocalTokenNumber (TRUE
, TokenNumber
);
1873 LocalTokenNumber
= GetLocalTokenNumber (FALSE
, TokenNumber
+ mPeiLocalTokenCount
);
1876 if ((LocalTokenNumber
& PCD_TYPE_HII
) != 0) {
1877 Offset
= LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
;
1878 VariableHead
= (VARIABLE_HEAD
*)((UINT8
*)Database
+ Offset
);
1880 // Why not to set property by VarCheckProtocol with Attributes and Property directly here?
1881 // It is because that set property by VarCheckProtocol will indicate the variable to
1882 // be a system variable, but the unknown max size of the variable is dangerous to
1883 // the system variable region.
1885 if ((VariableHead
->Property
& VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY
) != 0) {
1887 // DynamicHii PCD with RO property set in *.dsc.
1889 StringTable
= (UINT8
*)((UINT8
*)Database
+ Database
->StringTableOffset
);
1890 GuidTable
= (EFI_GUID
*)((UINT8
*)Database
+ Database
->GuidTableOffset
);
1891 Guid
= GuidTable
+ VariableHead
->GuidTableIndex
;
1892 Name
= (UINT16
*)(StringTable
+ VariableHead
->StringIndex
);
1893 Status
= VariableLock
->RequestToLock (VariableLock
, Name
, Guid
);
1894 ASSERT_EFI_ERROR (Status
);
1901 VariableLockProtocol callback
1902 to lock the variables referenced by DynamicHii PCDs with RO property set in *.dsc.
1904 @param[in] Event Event whose notification function is being invoked.
1905 @param[in] Context Pointer to the notification function's context.
1910 VariableLockCallBack (
1916 EDKII_VARIABLE_LOCK_PROTOCOL
*VariableLock
;
1918 Status
= gBS
->LocateProtocol (&gEdkiiVariableLockProtocolGuid
, NULL
, (VOID
**)&VariableLock
);
1919 if (!EFI_ERROR (Status
)) {
1920 VariableLockDynamicHiiPcd (TRUE
, VariableLock
);
1921 VariableLockDynamicHiiPcd (FALSE
, VariableLock
);