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);
820 while (Index
< mDxePcdDbSize
) {
821 SkuDelta
= (PCD_DATABASE_SKU_DELTA
*)((UINT8
*)mDxePcdDbBinary
+ Index
);
822 if ((SkuDelta
->SkuId
== SkuId
) && (SkuDelta
->SkuIdCompared
== 0)) {
826 Index
= (Index
+ SkuDelta
->Length
+ 7) & (~7);
830 // Patch the delta data into current PCD database
832 if ((Index
< mDxePcdDbSize
) && (SkuDelta
!= NULL
)) {
833 SkuDeltaData
= (PCD_DATA_DELTA
*)(SkuDelta
+ 1);
834 while ((UINT8
*)SkuDeltaData
< (UINT8
*)SkuDelta
+ SkuDelta
->Length
) {
835 *((UINT8
*)mPcdDatabase
.DxeDb
+ SkuDeltaData
->Offset
) = (UINT8
)SkuDeltaData
->Value
;
842 return EFI_NOT_FOUND
;
846 Initialize the PCD database in DXE phase.
848 PCD database in DXE phase also contains PCD database in PEI phase which is copied
853 BuildPcdDxeDataBase (
857 PEI_PCD_DATABASE
*PeiDatabase
;
858 EFI_HOB_GUID_TYPE
*GuidHob
;
865 // Assign PCD Entries with default value to PCD DATABASE
867 mPcdDatabase
.DxeDb
= LocateExPcdBinary ();
868 ASSERT (mPcdDatabase
.DxeDb
!= NULL
);
869 PcdDxeDbLen
= mPcdDatabase
.DxeDb
->Length
+ mPcdDatabase
.DxeDb
->UninitDataBaseSize
;
870 PcdDxeDb
= AllocateZeroPool (PcdDxeDbLen
);
871 ASSERT (PcdDxeDb
!= NULL
);
872 CopyMem (PcdDxeDb
, mPcdDatabase
.DxeDb
, mPcdDatabase
.DxeDb
->Length
);
873 mPcdDatabase
.DxeDb
= PcdDxeDb
;
875 GuidHob
= GetFirstGuidHob (&gPcdDataBaseHobGuid
);
876 if (GuidHob
!= NULL
) {
878 // If no PEIMs use dynamic Pcd Entry, the Pcd Service PEIM
879 // should not be included at all. So the GuidHob could
880 // be NULL. If it is NULL, we just copy over the DXE Default
881 // Value to PCD Database.
883 PeiDatabase
= (PEI_PCD_DATABASE
*)GET_GUID_HOB_DATA (GuidHob
);
886 // Get next one that stores full PEI data
888 GuidHob
= GetNextGuidHob (&gPcdDataBaseHobGuid
, GET_NEXT_HOB (GuidHob
));
889 if (GuidHob
!= NULL
) {
890 mPeiPcdDbBinary
= (PEI_PCD_DATABASE
*)GET_GUID_HOB_DATA (GuidHob
);
891 mPeiPcdDbSize
= (UINTN
)GET_GUID_HOB_DATA_SIZE (GuidHob
);
895 // Assign PCD Entries refereneced in PEI phase to PCD DATABASE
897 mPcdDatabase
.PeiDb
= PeiDatabase
;
899 // Inherit the SystemSkuId from PEI phase.
901 if (mPcdDatabase
.PeiDb
->SystemSkuId
!= 0) {
902 Status
= UpdatePcdDatabase (mPcdDatabase
.PeiDb
->SystemSkuId
, FALSE
);
903 ASSERT_EFI_ERROR (Status
);
906 mPcdDatabase
.DxeDb
->SystemSkuId
= mPcdDatabase
.PeiDb
->SystemSkuId
;
908 mPcdDatabase
.PeiDb
= AllocateZeroPool (sizeof (PEI_PCD_DATABASE
));
909 ASSERT (mPcdDatabase
.PeiDb
!= NULL
);
913 // Initialized the external PCD database local variables
915 mPeiLocalTokenCount
= mPcdDatabase
.PeiDb
->LocalTokenCount
;
916 mDxeLocalTokenCount
= mPcdDatabase
.DxeDb
->LocalTokenCount
;
918 mPeiExMapppingTableSize
= mPcdDatabase
.PeiDb
->ExTokenCount
* sizeof (DYNAMICEX_MAPPING
);
919 mDxeExMapppingTableSize
= mPcdDatabase
.DxeDb
->ExTokenCount
* sizeof (DYNAMICEX_MAPPING
);
920 mPeiGuidTableSize
= mPcdDatabase
.PeiDb
->GuidTableCount
* sizeof (GUID
);
921 mDxeGuidTableSize
= mPcdDatabase
.DxeDb
->GuidTableCount
* sizeof (GUID
);
923 mPcdTotalTokenCount
= mPeiLocalTokenCount
+ mDxeLocalTokenCount
;
924 mPeiNexTokenCount
= mPeiLocalTokenCount
- mPcdDatabase
.PeiDb
->ExTokenCount
;
925 mDxeNexTokenCount
= mDxeLocalTokenCount
- mPcdDatabase
.DxeDb
->ExTokenCount
;
927 mPeiExMapTableEmpty
= (mPcdDatabase
.PeiDb
->ExTokenCount
== 0) ? TRUE
: FALSE
;
928 mDxeExMapTableEmpty
= (mPcdDatabase
.DxeDb
->ExTokenCount
== 0) ? TRUE
: FALSE
;
929 mPeiDatabaseEmpty
= (mPeiLocalTokenCount
== 0) ? TRUE
: FALSE
;
931 TmpTokenSpaceBufferCount
= mPcdDatabase
.PeiDb
->ExTokenCount
+ mPcdDatabase
.DxeDb
->ExTokenCount
;
932 TmpTokenSpaceBuffer
= (EFI_GUID
**)AllocateZeroPool (TmpTokenSpaceBufferCount
* sizeof (EFI_GUID
*));
935 // Initialized the Callback Function Table
937 mCallbackFnTable
= AllocateZeroPool (mPcdTotalTokenCount
* sizeof (LIST_ENTRY
));
938 ASSERT (mCallbackFnTable
!= NULL
);
941 // EBC compiler is very choosy. It may report warning about comparison
942 // between UINTN and 0 . So we add 1 in each size of the
945 for (Index
= 0; Index
+ 1 < mPcdTotalTokenCount
+ 1; Index
++) {
946 InitializeListHead (&mCallbackFnTable
[Index
]);
951 Get Variable which contains HII type PCD entry.
953 @param VariableGuid Variable's guid
954 @param VariableName Variable's unicode name string
955 @param VariableData Variable's data pointer,
956 @param VariableSize Variable's size.
958 @return the status of gRT->GetVariable
962 IN EFI_GUID
*VariableGuid
,
963 IN UINT16
*VariableName
,
964 OUT UINT8
**VariableData
,
965 OUT UINTN
*VariableSize
976 // Firstly get the real size of HII variable
978 Status
= gRT
->GetVariable (
979 (UINT16
*)VariableName
,
987 // Allocate buffer to hold whole variable data according to variable size.
989 if (Status
== EFI_BUFFER_TOO_SMALL
) {
990 Buffer
= (UINT8
*)AllocatePool (Size
);
992 ASSERT (Buffer
!= NULL
);
994 Status
= gRT
->GetVariable (
1002 ASSERT (Status
== EFI_SUCCESS
);
1003 *VariableData
= Buffer
;
1004 *VariableSize
= Size
;
1007 // Use Default Data only when variable is not found.
1008 // For other error status, correct data can't be got, and trig ASSERT().
1010 ASSERT (Status
== EFI_NOT_FOUND
);
1017 Invoke the callback function when dynamic PCD entry was set, if this PCD entry
1018 has registered callback function.
1020 @param ExTokenNumber DynamicEx PCD's token number, if this PCD entry is dyanmicEx
1022 @param Guid DynamicEx PCD's guid, if this PCD entry is dynamicEx type
1024 @param TokenNumber PCD token number generated by build tools.
1025 @param Data Value want to be set for this PCD entry
1026 @param Size The size of value
1030 InvokeCallbackOnSet (
1031 UINT32 ExTokenNumber
,
1032 CONST EFI_GUID
*Guid OPTIONAL
,
1038 CALLBACK_FN_ENTRY
*FnTableEntry
;
1039 LIST_ENTRY
*ListHead
;
1040 LIST_ENTRY
*ListNode
;
1043 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
1044 // We have to decrement TokenNumber by 1 to make it usable
1045 // as the array index of mCallbackFnTable[].
1047 ListHead
= &mCallbackFnTable
[TokenNumber
- 1];
1048 ListNode
= GetFirstNode (ListHead
);
1050 while (ListNode
!= ListHead
) {
1051 FnTableEntry
= CR_FNENTRY_FROM_LISTNODE (ListNode
, CALLBACK_FN_ENTRY
, Node
);
1053 FnTableEntry
->CallbackFn (
1055 (Guid
== NULL
) ? TokenNumber
: ExTokenNumber
,
1060 ListNode
= GetNextNode (ListHead
, ListNode
);
1067 Wrapper function for setting non-pointer type value for a PCD entry.
1069 @param TokenNumber Pcd token number autogenerated by build tools.
1070 @param Data Value want to be set for PCD entry
1071 @param Size Size of value.
1073 @return status of SetWorker.
1078 IN UINTN TokenNumber
,
1083 return SetWorker (TokenNumber
, Data
, &Size
, FALSE
);
1087 Set value for an PCD entry
1089 @param TokenNumber Pcd token number autogenerated by build tools.
1090 @param Data Value want to be set for PCD entry
1091 @param Size Size of value.
1092 @param PtrType If TRUE, the type of PCD entry's value is Pointer.
1093 If False, the type of PCD entry's value is not Pointer.
1095 @retval EFI_INVALID_PARAMETER If this PCD type is VPD, VPD PCD can not be set.
1096 @retval EFI_INVALID_PARAMETER If Size can not be set to size table.
1097 @retval EFI_INVALID_PARAMETER If Size of non-Ptr type PCD does not match the size information in PCD database.
1098 @retval EFI_NOT_FOUND If value type of PCD entry is intergrate, but not in
1099 range of UINT8, UINT16, UINT32, UINT64
1100 @retval EFI_NOT_FOUND Can not find the PCD type according to token number.
1104 IN UINTN TokenNumber
,
1111 UINT32 LocalTokenNumber
;
1112 EFI_GUID
*GuidTable
;
1116 UINTN VariableOffset
;
1119 VARIABLE_HEAD
*VariableHead
;
1124 UINTN TmpTokenNumber
;
1127 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
1128 // We have to decrement TokenNumber by 1 to make it usable
1129 // as the array index.
1133 TmpTokenNumber
= TokenNumber
;
1136 // EBC compiler is very choosy. It may report warning about comparison
1137 // between UINTN and 0 . So we add 1 in each size of the
1140 ASSERT (TokenNumber
+ 1 < mPcdTotalTokenCount
+ 1);
1144 // Get MaxSize first, then check new size with max buffer size.
1146 GetPtrTypeSize (TokenNumber
, &MaxSize
);
1147 if (*Size
> MaxSize
) {
1149 return EFI_INVALID_PARAMETER
;
1152 if (*Size
!= DxePcdGetSize (TokenNumber
+ 1)) {
1153 return EFI_INVALID_PARAMETER
;
1158 // EBC compiler is very choosy. It may report warning about comparison
1159 // between UINTN and 0 . So we add 1 in each size of the
1162 if ((TokenNumber
+ 1 < mPeiNexTokenCount
+ 1) ||
1163 ((TokenNumber
+ 1 >= mPeiLocalTokenCount
+ 1) && (TokenNumber
+ 1 < (mPeiLocalTokenCount
+ mDxeNexTokenCount
+ 1))))
1165 InvokeCallbackOnSet (0, NULL
, TokenNumber
+ 1, Data
, *Size
);
1169 // Aquire lock to prevent reentrance from TPL_CALLBACK level
1171 EfiAcquireLock (&mPcdDatabaseLock
);
1174 // EBC compiler is very choosy. It may report warning about comparison
1175 // between UINTN and 0 . So we add 1 in each size of the
1178 IsPeiDb
= (BOOLEAN
)((TokenNumber
+ 1 < mPeiLocalTokenCount
+ 1) ? TRUE
: FALSE
);
1180 LocalTokenNumber
= GetLocalTokenNumber (IsPeiDb
, TokenNumber
+ 1);
1182 Offset
= LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
;
1184 PcdDb
= IsPeiDb
? ((UINT8
*)mPcdDatabase
.PeiDb
) : ((UINT8
*)mPcdDatabase
.DxeDb
);
1187 StringTable
= (UINT8
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->StringTableOffset
);
1189 StringTable
= (UINT8
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->StringTableOffset
);
1192 InternalData
= PcdDb
+ Offset
;
1194 switch (LocalTokenNumber
& PCD_TYPE_ALL_SET
) {
1197 Status
= EFI_INVALID_PARAMETER
;
1200 case PCD_TYPE_STRING
:
1201 if (SetPtrTypeSize (TmpTokenNumber
, Size
)) {
1202 CopyMem (StringTable
+ *((STRING_HEAD
*)InternalData
), Data
, *Size
);
1203 Status
= EFI_SUCCESS
;
1205 Status
= EFI_INVALID_PARAMETER
;
1210 case PCD_TYPE_HII
|PCD_TYPE_STRING
:
1213 if (!SetPtrTypeSize (TmpTokenNumber
, Size
)) {
1214 Status
= EFI_INVALID_PARAMETER
;
1220 GuidTable
= (EFI_GUID
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->GuidTableOffset
);
1222 GuidTable
= (EFI_GUID
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->GuidTableOffset
);
1225 VariableHead
= (VARIABLE_HEAD
*)(PcdDb
+ Offset
);
1227 Guid
= GuidTable
+ VariableHead
->GuidTableIndex
;
1228 Name
= (UINT16
*)(StringTable
+ VariableHead
->StringIndex
);
1229 VariableOffset
= VariableHead
->Offset
;
1230 Attributes
= VariableHead
->Attributes
;
1231 Status
= SetHiiVariable (Guid
, Name
, Attributes
, Data
, *Size
, VariableOffset
);
1236 if (SetPtrTypeSize (TmpTokenNumber
, Size
)) {
1237 CopyMem (InternalData
, Data
, *Size
);
1238 Status
= EFI_SUCCESS
;
1240 Status
= EFI_INVALID_PARAMETER
;
1246 Status
= EFI_SUCCESS
;
1248 case sizeof (UINT8
):
1249 *((UINT8
*)InternalData
) = *((UINT8
*)Data
);
1252 case sizeof (UINT16
):
1253 *((UINT16
*)InternalData
) = *((UINT16
*)Data
);
1256 case sizeof (UINT32
):
1257 *((UINT32
*)InternalData
) = *((UINT32
*)Data
);
1260 case sizeof (UINT64
):
1261 *((UINT64
*)InternalData
) = *((UINT64
*)Data
);
1266 Status
= EFI_NOT_FOUND
;
1274 Status
= EFI_NOT_FOUND
;
1278 EfiReleaseLock (&mPcdDatabaseLock
);
1284 Wrapper function for get PCD value for dynamic-ex PCD.
1286 @param Guid Token space guid for dynamic-ex PCD.
1287 @param ExTokenNumber Token number for dynamic-ex PCD.
1288 @param GetSize The size of dynamic-ex PCD value.
1290 @return PCD entry in PCD database.
1295 IN CONST EFI_GUID
*Guid
,
1296 IN UINTN ExTokenNumber
,
1300 return GetWorker (GetExPcdTokenNumber (Guid
, (UINT32
)ExTokenNumber
), GetSize
);
1304 Wrapper function for set PCD value for non-Pointer type dynamic-ex PCD.
1306 @param ExTokenNumber Token number for dynamic-ex PCD.
1307 @param Guid Token space guid for dynamic-ex PCD.
1308 @param Data Value want to be set.
1309 @param SetSize The size of value.
1311 @return status of ExSetWorker().
1316 IN UINTN ExTokenNumber
,
1317 IN CONST EFI_GUID
*Guid
,
1322 return ExSetWorker (ExTokenNumber
, Guid
, Data
, &SetSize
, FALSE
);
1326 Set value for a dynamic-ex PCD entry.
1328 This routine find the local token number according to dynamic-ex PCD's token
1329 space guid and token number firstly, and invoke callback function if this PCD
1330 entry registered callback function. Finally, invoken general SetWorker to set
1333 @param ExTokenNumber Dynamic-ex PCD token number.
1334 @param Guid Token space guid for dynamic-ex PCD.
1335 @param Data PCD value want to be set
1336 @param SetSize Size of value.
1337 @param PtrType If TRUE, this PCD entry is pointer type.
1338 If FALSE, this PCD entry is not pointer type.
1340 @return status of SetWorker().
1345 IN UINTN ExTokenNumber
,
1346 IN CONST EFI_GUID
*Guid
,
1348 IN OUT UINTN
*SetSize
,
1354 TokenNumber
= GetExPcdTokenNumber (Guid
, (UINT32
)ExTokenNumber
);
1356 InvokeCallbackOnSet ((UINT32
)ExTokenNumber
, Guid
, TokenNumber
, Data
, *SetSize
);
1358 return SetWorker (TokenNumber
, Data
, SetSize
, PtrType
);
1362 Get variable size and data from HII-type PCDs.
1364 @param[in] VariableGuid Guid of variable which stored value of a HII-type PCD.
1365 @param[in] VariableName Unicode name of variable which stored value of a HII-type PCD.
1366 @param[out] VariableSize Pointer to variable size got from HII-type PCDs.
1367 @param[out] VariableData Pointer to variable data got from HII-type PCDs.
1371 GetVariableSizeAndDataFromHiiPcd (
1372 IN EFI_GUID
*VariableGuid
,
1373 IN UINT16
*VariableName
,
1374 OUT UINTN
*VariableSize
,
1375 OUT VOID
*VariableData OPTIONAL
1379 PCD_DATABASE_INIT
*Database
;
1381 UINT32 LocalTokenNumber
;
1383 EFI_GUID
*GuidTable
;
1385 VARIABLE_HEAD
*VariableHead
;
1390 UINT8
*VaraiableDefaultBuffer
;
1391 STRING_HEAD StringTableIdx
;
1396 // Go through PCD database to find out DynamicHii PCDs.
1398 for (TokenNumber
= 1; TokenNumber
<= mPcdTotalTokenCount
; TokenNumber
++) {
1399 IsPeiDb
= (BOOLEAN
)((TokenNumber
+ 1 < mPeiLocalTokenCount
+ 1) ? TRUE
: FALSE
);
1400 Database
= IsPeiDb
? mPcdDatabase
.PeiDb
: mPcdDatabase
.DxeDb
;
1401 LocalTokenNumber
= GetLocalTokenNumber (IsPeiDb
, TokenNumber
);
1402 if ((LocalTokenNumber
& PCD_TYPE_HII
) != 0) {
1404 // Get the Variable Guid and Name pointer.
1406 Offset
= LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
;
1407 VariableHead
= (VARIABLE_HEAD
*)((UINT8
*)Database
+ Offset
);
1408 StringTable
= (UINT8
*)((UINT8
*)Database
+ Database
->StringTableOffset
);
1409 GuidTable
= (EFI_GUID
*)((UINT8
*)Database
+ Database
->GuidTableOffset
);
1410 Guid
= GuidTable
+ VariableHead
->GuidTableIndex
;
1411 Name
= (UINT16
*)(StringTable
+ VariableHead
->StringIndex
);
1412 if (CompareGuid (VariableGuid
, Guid
) && (StrCmp (VariableName
, Name
) == 0)) {
1414 // It is the matched DynamicHii PCD.
1416 PcdDataSize
= DxePcdGetSize (TokenNumber
);
1417 Size
= VariableHead
->Offset
+ PcdDataSize
;
1418 if (Size
> *VariableSize
) {
1419 *VariableSize
= Size
;
1422 if (VariableData
!= NULL
) {
1423 if ((LocalTokenNumber
& PCD_TYPE_ALL_SET
) == (PCD_TYPE_HII
|PCD_TYPE_STRING
)) {
1425 // If a HII type PCD's datum type is VOID*, the DefaultValueOffset is the index of
1426 // string array in string table.
1428 StringTableIdx
= *(STRING_HEAD
*)((UINT8
*)Database
+ VariableHead
->DefaultValueOffset
);
1429 VaraiableDefaultBuffer
= (UINT8
*)(StringTable
+ StringTableIdx
);
1431 VaraiableDefaultBuffer
= (UINT8
*)Database
+ VariableHead
->DefaultValueOffset
;
1434 CopyMem ((UINT8
*)VariableData
+ VariableHead
->Offset
, VaraiableDefaultBuffer
, PcdDataSize
);
1442 Set value for HII-type PCD.
1444 A HII-type PCD's value is stored in a variable. Setting/Getting the value of
1445 HII-type PCD is to visit this variable.
1447 @param VariableGuid Guid of variable which stored value of a HII-type PCD.
1448 @param VariableName Unicode name of variable which stored value of a HII-type PCD.
1449 @param SetAttributes Attributes bitmask to set for the variable.
1450 @param Data Value want to be set.
1451 @param DataSize Size of value
1452 @param Offset Value offset of HII-type PCD in variable.
1454 @return status of GetVariable()/SetVariable().
1459 IN EFI_GUID
*VariableGuid
,
1460 IN UINT16
*VariableName
,
1461 IN UINT32 SetAttributes
,
1462 IN CONST VOID
*Data
,
1477 // Try to get original variable size information.
1479 Status
= gRT
->GetVariable (
1480 (UINT16
*)VariableName
,
1487 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1489 // Patch new PCD's value to offset in given HII variable.
1491 if (Size
>= (DataSize
+ Offset
)) {
1494 SetSize
= DataSize
+ Offset
;
1497 Buffer
= AllocatePool (SetSize
);
1498 ASSERT (Buffer
!= NULL
);
1500 Status
= gRT
->GetVariable (
1508 ASSERT_EFI_ERROR (Status
);
1510 CopyMem ((UINT8
*)Buffer
+ Offset
, Data
, DataSize
);
1512 if (SetAttributes
== 0) {
1513 SetAttributes
= Attribute
;
1516 Status
= gRT
->SetVariable (
1526 } else if (Status
== EFI_NOT_FOUND
) {
1528 // If variable does not exist, a new variable need to be created.
1532 // Get size, allocate buffer and get data.
1534 GetVariableSizeAndDataFromHiiPcd (VariableGuid
, VariableName
, &Size
, NULL
);
1535 Buffer
= AllocateZeroPool (Size
);
1536 ASSERT (Buffer
!= NULL
);
1537 GetVariableSizeAndDataFromHiiPcd (VariableGuid
, VariableName
, &Size
, Buffer
);
1542 CopyMem ((UINT8
*)Buffer
+ Offset
, Data
, DataSize
);
1544 if (SetAttributes
== 0) {
1545 SetAttributes
= EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
;
1548 Status
= gRT
->SetVariable (
1561 // If we drop to here, the value is failed to be written in to variable area.
1567 Get Token Number according to dynamic-ex PCD's {token space guid:token number}
1569 A dynamic-ex type PCD, developer must provide pair of token space guid: token number
1570 in DEC file. PCD database maintain a mapping table that translate pair of {token
1571 space guid: token number} to Token Number.
1573 @param Guid Token space guid for dynamic-ex PCD entry.
1574 @param ExTokenNumber Dynamic-ex PCD token number.
1576 @return Token Number for dynamic-ex PCD.
1580 GetExPcdTokenNumber (
1581 IN CONST EFI_GUID
*Guid
,
1582 IN UINT32 ExTokenNumber
1586 DYNAMICEX_MAPPING
*ExMap
;
1587 EFI_GUID
*GuidTable
;
1588 EFI_GUID
*MatchGuid
;
1591 if (!mPeiDatabaseEmpty
) {
1592 ExMap
= (DYNAMICEX_MAPPING
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->ExMapTableOffset
);
1593 GuidTable
= (EFI_GUID
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->GuidTableOffset
);
1595 MatchGuid
= ScanGuid (GuidTable
, mPeiGuidTableSize
, Guid
);
1597 if (MatchGuid
!= NULL
) {
1598 MatchGuidIdx
= MatchGuid
- GuidTable
;
1600 for (Index
= 0; Index
< mPcdDatabase
.PeiDb
->ExTokenCount
; Index
++) {
1601 if ((ExTokenNumber
== ExMap
[Index
].ExTokenNumber
) &&
1602 (MatchGuidIdx
== ExMap
[Index
].ExGuidIndex
))
1604 return ExMap
[Index
].TokenNumber
;
1610 ExMap
= (DYNAMICEX_MAPPING
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->ExMapTableOffset
);
1611 GuidTable
= (EFI_GUID
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->GuidTableOffset
);
1613 MatchGuid
= ScanGuid (GuidTable
, mDxeGuidTableSize
, Guid
);
1615 // We need to ASSERT here. If GUID can't be found in GuidTable, this is a
1616 // error in the BUILD system.
1618 ASSERT (MatchGuid
!= NULL
);
1620 MatchGuidIdx
= MatchGuid
- GuidTable
;
1622 for (Index
= 0; Index
< mPcdDatabase
.DxeDb
->ExTokenCount
; Index
++) {
1623 if ((ExTokenNumber
== ExMap
[Index
].ExTokenNumber
) &&
1624 (MatchGuidIdx
== ExMap
[Index
].ExGuidIndex
))
1626 return ExMap
[Index
].TokenNumber
;
1630 DEBUG ((DEBUG_ERROR
, "%a: Failed to find PCD with GUID: %g and token number: %d\n", __FUNCTION__
, Guid
, ExTokenNumber
));
1637 Wrapper function of getting index of PCD entry in size table.
1639 @param LocalTokenNumberTableIdx Index of this PCD in local token number table.
1640 @param IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
1641 If FALSE, the pcd entry is initialized in DXE phase.
1643 @return index of PCD entry in size table.
1647 IN UINTN LocalTokenNumberTableIdx
,
1651 UINT32
*LocalTokenNumberTable
;
1652 UINTN LocalTokenNumber
;
1657 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
);
1659 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
);
1664 for (Index
= 0; Index
< LocalTokenNumberTableIdx
; Index
++) {
1665 LocalTokenNumber
= LocalTokenNumberTable
[Index
];
1667 if ((LocalTokenNumber
& PCD_DATUM_TYPE_ALL_SET
) == PCD_DATUM_TYPE_POINTER
) {
1669 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1672 if ((LocalTokenNumber
& PCD_TYPE_VPD
) != 0) {
1674 // We have only two entry for VPD enabled PCD entry:
1677 // Current size is equal to MAX size.
1682 // We have only two entry for Non-Sku enabled PCD entry:
1691 return SizeTableIdx
;
1695 Get size of POINTER type PCD value.
1697 @param LocalTokenNumberTableIdx Index of local token number in local token number table.
1698 @param MaxSize Maxmium size of POINTER type PCD value.
1700 @return size of POINTER type PCD value.
1705 IN UINTN LocalTokenNumberTableIdx
,
1710 UINTN LocalTokenNumber
;
1711 SIZE_INFO
*SizeTable
;
1713 UINT32
*LocalTokenNumberTable
;
1715 // EBC compiler is very choosy. It may report warning about comparison
1716 // between UINTN and 0 . So we add 1 in each size of the
1718 IsPeiDb
= (BOOLEAN
)(LocalTokenNumberTableIdx
+ 1 < mPeiLocalTokenCount
+ 1);
1721 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
);
1722 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->SizeTableOffset
);
1724 LocalTokenNumberTableIdx
-= mPeiLocalTokenCount
;
1725 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
);
1726 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->SizeTableOffset
);
1729 LocalTokenNumber
= LocalTokenNumberTable
[LocalTokenNumberTableIdx
];
1731 ASSERT ((LocalTokenNumber
& PCD_DATUM_TYPE_ALL_SET
) == PCD_DATUM_TYPE_POINTER
);
1733 SizeTableIdx
= GetSizeTableIndex (LocalTokenNumberTableIdx
, IsPeiDb
);
1735 *MaxSize
= SizeTable
[SizeTableIdx
];
1737 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1740 if ((LocalTokenNumber
& PCD_TYPE_VPD
) != 0) {
1742 // We have only two entry for VPD enabled PCD entry:
1745 // We consider current size is equal to MAX size.
1750 // We have only two entry for Non-Sku enabled PCD entry:
1754 return SizeTable
[SizeTableIdx
+ 1];
1759 Set size of POINTER type PCD value. The size should not exceed the maximum size
1762 @param LocalTokenNumberTableIdx Index of local token number in local token number table.
1763 @param CurrentSize Size of POINTER type PCD value.
1765 @retval TRUE Success to set size of PCD value.
1766 @retval FALSE Fail to set size of PCD value.
1770 IN UINTN LocalTokenNumberTableIdx
,
1771 IN OUT UINTN
*CurrentSize
1775 UINTN LocalTokenNumber
;
1776 SIZE_INFO
*SizeTable
;
1779 UINT32
*LocalTokenNumberTable
;
1782 // EBC compiler is very choosy. It may report warning about comparison
1783 // between UINTN and 0 . So we add 1 in each size of the
1786 IsPeiDb
= (BOOLEAN
)(LocalTokenNumberTableIdx
+ 1 < mPeiLocalTokenCount
+ 1);
1789 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
);
1790 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->SizeTableOffset
);
1792 LocalTokenNumberTableIdx
-= mPeiLocalTokenCount
;
1793 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
);
1794 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->SizeTableOffset
);
1797 LocalTokenNumber
= LocalTokenNumberTable
[LocalTokenNumberTableIdx
];
1799 ASSERT ((LocalTokenNumber
& PCD_DATUM_TYPE_ALL_SET
) == PCD_DATUM_TYPE_POINTER
);
1801 SizeTableIdx
= GetSizeTableIndex (LocalTokenNumberTableIdx
, IsPeiDb
);
1803 MaxSize
= SizeTable
[SizeTableIdx
];
1805 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1808 if ((LocalTokenNumber
& PCD_TYPE_VPD
) != 0) {
1810 // We shouldn't come here as we don't support SET for VPD
1815 if ((*CurrentSize
> MaxSize
) ||
1816 (*CurrentSize
== MAX_ADDRESS
))
1818 *CurrentSize
= MaxSize
;
1823 // We have only two entry for Non-Sku enabled PCD entry:
1827 SizeTable
[SizeTableIdx
+ 1] = (SIZE_INFO
)*CurrentSize
;
1833 VariableLock DynamicHiiPcd.
1835 @param[in] IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
1836 If FALSE, the pcd entry is initialized in DXE phase.
1837 @param[in] VariableLock Pointer to VariableLockProtocol.
1841 VariableLockDynamicHiiPcd (
1843 IN EDKII_VARIABLE_LOCK_PROTOCOL
*VariableLock
1847 PCD_DATABASE_INIT
*Database
;
1848 UINT32 LocalTokenCount
;
1850 UINT32 LocalTokenNumber
;
1852 EFI_GUID
*GuidTable
;
1854 VARIABLE_HEAD
*VariableHead
;
1858 Database
= IsPeiDb
? mPcdDatabase
.PeiDb
: mPcdDatabase
.DxeDb
;
1859 LocalTokenCount
= IsPeiDb
? mPeiLocalTokenCount
: mDxeLocalTokenCount
;
1862 // Go through PCD database to find out DynamicHii PCDs.
1864 for (TokenNumber
= 1; TokenNumber
<= LocalTokenCount
; TokenNumber
++) {
1866 LocalTokenNumber
= GetLocalTokenNumber (TRUE
, TokenNumber
);
1868 LocalTokenNumber
= GetLocalTokenNumber (FALSE
, TokenNumber
+ mPeiLocalTokenCount
);
1871 if ((LocalTokenNumber
& PCD_TYPE_HII
) != 0) {
1872 Offset
= LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
;
1873 VariableHead
= (VARIABLE_HEAD
*)((UINT8
*)Database
+ Offset
);
1875 // Why not to set property by VarCheckProtocol with Attributes and Property directly here?
1876 // It is because that set property by VarCheckProtocol will indicate the variable to
1877 // be a system variable, but the unknown max size of the variable is dangerous to
1878 // the system variable region.
1880 if ((VariableHead
->Property
& VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY
) != 0) {
1882 // DynamicHii PCD with RO property set in *.dsc.
1884 StringTable
= (UINT8
*)((UINT8
*)Database
+ Database
->StringTableOffset
);
1885 GuidTable
= (EFI_GUID
*)((UINT8
*)Database
+ Database
->GuidTableOffset
);
1886 Guid
= GuidTable
+ VariableHead
->GuidTableIndex
;
1887 Name
= (UINT16
*)(StringTable
+ VariableHead
->StringIndex
);
1888 Status
= VariableLock
->RequestToLock (VariableLock
, Name
, Guid
);
1889 ASSERT_EFI_ERROR (Status
);
1896 VariableLockProtocol callback
1897 to lock the variables referenced by DynamicHii PCDs with RO property set in *.dsc.
1899 @param[in] Event Event whose notification function is being invoked.
1900 @param[in] Context Pointer to the notification function's context.
1905 VariableLockCallBack (
1911 EDKII_VARIABLE_LOCK_PROTOCOL
*VariableLock
;
1913 Status
= gBS
->LocateProtocol (&gEdkiiVariableLockProtocolGuid
, NULL
, (VOID
**)&VariableLock
);
1914 if (!EFI_ERROR (Status
)) {
1915 VariableLockDynamicHiiPcd (TRUE
, VariableLock
);
1916 VariableLockDynamicHiiPcd (FALSE
, VariableLock
);