2 Help functions used by PCD DXE driver.
4 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/DxeServicesLib.h>
18 PCD_DATABASE mPcdDatabase
;
20 UINT32 mPcdTotalTokenCount
;
21 UINT32 mPeiLocalTokenCount
;
22 UINT32 mDxeLocalTokenCount
;
23 UINT32 mPeiNexTokenCount
;
24 UINT32 mDxeNexTokenCount
;
25 UINT32 mPeiExMapppingTableSize
;
26 UINT32 mDxeExMapppingTableSize
;
27 UINT32 mPeiGuidTableSize
;
28 UINT32 mDxeGuidTableSize
;
30 BOOLEAN mPeiExMapTableEmpty
;
31 BOOLEAN mDxeExMapTableEmpty
;
32 BOOLEAN mPeiDatabaseEmpty
;
34 LIST_ENTRY
*mCallbackFnTable
;
35 EFI_GUID
**TmpTokenSpaceBuffer
;
36 UINTN TmpTokenSpaceBufferCount
;
40 Get the PCD entry pointer in PCD database.
42 This routine will visit PCD database to find the PCD entry according to given
43 token number. The given token number is autogened by build tools and it will be
44 translated to local token number. Local token number contains PCD's type and
45 offset of PCD entry in PCD database.
47 @param TokenNumber Token's number, it is autogened by build tools
48 @param GetSize The size of token's value
50 @return PCD entry pointer in PCD database
59 UINT32
*LocalTokenNumberTable
;
64 VARIABLE_HEAD
*VariableHead
;
65 UINT8
*VaraiableDefaultBuffer
;
74 UINT32 LocalTokenNumber
;
76 STRING_HEAD StringTableIdx
;
80 // Aquire lock to prevent reentrance from TPL_CALLBACK level
82 EfiAcquireLock (&mPcdDatabaseLock
);
86 ASSERT (TokenNumber
> 0);
88 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
89 // We have to decrement TokenNumber by 1 to make it usable
90 // as the array index.
94 TmpTokenNumber
= TokenNumber
;
97 // EBC compiler is very choosy. It may report warning about comparison
98 // between UINTN and 0 . So we add 1 in each size of the
101 ASSERT (TokenNumber
+ 1 < mPcdTotalTokenCount
+ 1);
103 ASSERT ((GetSize
== DxePcdGetSize (TokenNumber
+ 1)) || (GetSize
== 0));
105 // EBC compiler is very choosy. It may report warning about comparison
106 // between UINTN and 0 . So we add 1 in each size of the
108 IsPeiDb
= (BOOLEAN
) ((TokenNumber
+ 1 < mPeiLocalTokenCount
+ 1) ? TRUE
: FALSE
);
110 LocalTokenNumberTable
= IsPeiDb
? (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
) :
111 (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
);
113 TokenNumber
= IsPeiDb
? TokenNumber
:
114 TokenNumber
- mPeiLocalTokenCount
;
116 LocalTokenNumber
= LocalTokenNumberTable
[TokenNumber
];
118 if ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) == PCD_TYPE_SKU_ENABLED
) {
120 GetPtrTypeSize (TmpTokenNumber
, &MaxSize
);
124 LocalTokenNumber
= GetSkuEnabledTokenNumber (LocalTokenNumber
& ~PCD_TYPE_SKU_ENABLED
, MaxSize
, IsPeiDb
);
127 PcdDb
= IsPeiDb
? ((UINT8
*) mPcdDatabase
.PeiDb
) : ((UINT8
*) mPcdDatabase
.DxeDb
);
130 StringTable
= (UINT8
*) ((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->StringTableOffset
);
132 StringTable
= (UINT8
*) ((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->StringTableOffset
);
136 Offset
= LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
;
138 switch (LocalTokenNumber
& PCD_TYPE_ALL_SET
) {
140 VpdHead
= (VPD_HEAD
*) ((UINT8
*) PcdDb
+ Offset
);
141 RetPtr
= (VOID
*) (UINTN
) (PcdGet32 (PcdVpdBaseAddress
) + VpdHead
->Offset
);
145 case PCD_TYPE_HII
|PCD_TYPE_STRING
:
148 GuidTable
= (EFI_GUID
*) ((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->GuidTableOffset
);
150 GuidTable
= (EFI_GUID
*) ((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->GuidTableOffset
);
153 VariableHead
= (VARIABLE_HEAD
*) (PcdDb
+ Offset
);
154 Guid
= GuidTable
+ VariableHead
->GuidTableIndex
;
155 Name
= (UINT16
*)(StringTable
+ VariableHead
->StringIndex
);
157 if ((LocalTokenNumber
& PCD_TYPE_ALL_SET
) == (PCD_TYPE_HII
|PCD_TYPE_STRING
)) {
159 // If a HII type PCD's datum type is VOID*, the DefaultValueOffset is the index of
160 // string array in string table.
162 StringTableIdx
= *(STRING_HEAD
*)((UINT8
*) PcdDb
+ VariableHead
->DefaultValueOffset
);
163 VaraiableDefaultBuffer
= (VOID
*) (StringTable
+ StringTableIdx
);
165 VaraiableDefaultBuffer
= (UINT8
*) PcdDb
+ VariableHead
->DefaultValueOffset
;
167 Status
= GetHiiVariable (Guid
, Name
, &Data
, &DataSize
);
168 if (Status
== EFI_SUCCESS
) {
171 // It is a pointer type. So get the MaxSize reserved for
174 GetPtrTypeSize (TmpTokenNumber
, &GetSize
);
177 // If the operation is successful, we copy the data
178 // to the default value buffer in the PCD Database.
179 // So that we can free the Data allocated in GetHiiVariable.
181 CopyMem (VaraiableDefaultBuffer
, Data
+ VariableHead
->Offset
, GetSize
);
184 RetPtr
= (VOID
*) VaraiableDefaultBuffer
;
187 case PCD_TYPE_STRING
:
188 StringTableIdx
= *(STRING_HEAD
*)((UINT8
*) PcdDb
+ Offset
);
189 RetPtr
= (VOID
*) (StringTable
+ StringTableIdx
);
193 RetPtr
= (VOID
*) ((UINT8
*) PcdDb
+ Offset
);
202 EfiReleaseLock (&mPcdDatabaseLock
);
209 Register the callback function for a PCD entry.
211 This routine will register a callback function to a PCD entry by given token number
212 and token space guid.
214 @param TokenNumber PCD token's number, it is autogened by build tools.
215 @param Guid PCD token space's guid,
216 if not NULL, this PCD is dynamicEx type PCD.
217 @param CallBackFunction Callback function pointer
219 @return EFI_SUCCESS Always success for registering callback function.
223 DxeRegisterCallBackWorker (
224 IN UINTN TokenNumber
,
225 IN CONST EFI_GUID
*Guid
, OPTIONAL
226 IN PCD_PROTOCOL_CALLBACK CallBackFunction
229 CALLBACK_FN_ENTRY
*FnTableEntry
;
230 LIST_ENTRY
*ListHead
;
231 LIST_ENTRY
*ListNode
;
234 TokenNumber
= GetExPcdTokenNumber (Guid
, (UINT32
) TokenNumber
);
238 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
239 // We have to decrement TokenNumber by 1 to make it usable
240 // as the array index of mCallbackFnTable[].
242 ListHead
= &mCallbackFnTable
[TokenNumber
- 1];
243 ListNode
= GetFirstNode (ListHead
);
245 while (ListNode
!= ListHead
) {
246 FnTableEntry
= CR_FNENTRY_FROM_LISTNODE(ListNode
, CALLBACK_FN_ENTRY
, Node
);
248 if (FnTableEntry
->CallbackFn
== CallBackFunction
) {
250 // We only allow a Callback function to be register once
251 // for a TokenNumber. So just return EFI_SUCCESS
255 ListNode
= GetNextNode (ListHead
, ListNode
);
258 FnTableEntry
= AllocatePool (sizeof(CALLBACK_FN_ENTRY
));
259 ASSERT (FnTableEntry
!= NULL
);
261 FnTableEntry
->CallbackFn
= CallBackFunction
;
262 InsertTailList (ListHead
, &FnTableEntry
->Node
);
268 UnRegister the callback function for a PCD entry.
270 This routine will unregister a callback function to a PCD entry by given token number
271 and token space guid.
273 @param TokenNumber PCD token's number, it is autogened by build tools.
274 @param Guid PCD token space's guid.
275 if not NULL, this PCD is dynamicEx type PCD.
276 @param CallBackFunction Callback function pointer
278 @retval EFI_SUCCESS Callback function is success to be unregister.
279 @retval EFI_INVALID_PARAMETER Can not find the PCD entry by given token number.
282 DxeUnRegisterCallBackWorker (
283 IN UINTN TokenNumber
,
284 IN CONST EFI_GUID
*Guid
, OPTIONAL
285 IN PCD_PROTOCOL_CALLBACK CallBackFunction
288 CALLBACK_FN_ENTRY
*FnTableEntry
;
289 LIST_ENTRY
*ListHead
;
290 LIST_ENTRY
*ListNode
;
293 TokenNumber
= GetExPcdTokenNumber (Guid
, (UINT32
) TokenNumber
);
297 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
298 // We have to decrement TokenNumber by 1 to make it usable
299 // as the array index of mCallbackFnTable[].
301 ListHead
= &mCallbackFnTable
[TokenNumber
- 1];
302 ListNode
= GetFirstNode (ListHead
);
304 while (ListNode
!= ListHead
) {
305 FnTableEntry
= CR_FNENTRY_FROM_LISTNODE(ListNode
, CALLBACK_FN_ENTRY
, Node
);
307 if (FnTableEntry
->CallbackFn
== CallBackFunction
) {
309 // We only allow a Callback function to be register once
310 // for a TokenNumber. So we can safely remove the Node from
311 // the Link List and return EFI_SUCCESS.
313 RemoveEntryList (ListNode
);
314 FreePool (FnTableEntry
);
318 ListNode
= GetNextNode (ListHead
, ListNode
);
321 return EFI_INVALID_PARAMETER
;
325 Get next token number in given token space.
327 This routine is used for dynamicEx type PCD. It will firstly scan token space
328 table to get token space according to given token space guid. Then scan given
329 token number in found token space, if found, then return next token number in
332 @param Guid Token space guid. Next token number will be scaned in
334 @param TokenNumber Token number.
335 If PCD_INVALID_TOKEN_NUMBER, return first token number in
337 If not PCD_INVALID_TOKEN_NUMBER, return next token number
338 in token space table.
339 @param GuidTable Token space guid table. It will be used for scan token space
340 by given token space guid.
341 @param SizeOfGuidTable The size of guid table.
342 @param ExMapTable DynamicEx token number mapping table.
343 @param SizeOfExMapTable The size of dynamicEx token number mapping table.
345 @retval EFI_NOT_FOUND Can not given token space or token number.
346 @retval EFI_SUCCESS Success to get next token number.
350 ExGetNextTokeNumber (
351 IN CONST EFI_GUID
*Guid
,
352 IN OUT UINTN
*TokenNumber
,
353 IN EFI_GUID
*GuidTable
,
354 IN UINTN SizeOfGuidTable
,
355 IN DYNAMICEX_MAPPING
*ExMapTable
,
356 IN UINTN SizeOfExMapTable
363 UINTN ExMapTableCount
;
366 // Scan token space guid
368 MatchGuid
= ScanGuid (GuidTable
, SizeOfGuidTable
, Guid
);
369 if (MatchGuid
== NULL
) {
370 return EFI_NOT_FOUND
;
374 // Find the token space table in dynamicEx mapping table.
377 GuidTableIdx
= MatchGuid
- GuidTable
;
378 ExMapTableCount
= SizeOfExMapTable
/ sizeof(ExMapTable
[0]);
379 for (Index
= 0; Index
< ExMapTableCount
; Index
++) {
380 if (ExMapTable
[Index
].ExGuidIndex
== GuidTableIdx
) {
388 // If given token number is PCD_INVALID_TOKEN_NUMBER, then return the first
389 // token number in found token space.
391 if (*TokenNumber
== PCD_INVALID_TOKEN_NUMBER
) {
392 *TokenNumber
= ExMapTable
[Index
].ExTokenNumber
;
396 for ( ; Index
< ExMapTableCount
; Index
++) {
397 if (ExMapTable
[Index
].ExTokenNumber
== *TokenNumber
) {
402 while (Index
< ExMapTableCount
) {
404 if (Index
== ExMapTableCount
) {
406 // Exceed the length of ExMap Table
408 *TokenNumber
= PCD_INVALID_TOKEN_NUMBER
;
409 return EFI_NOT_FOUND
;
410 } else if (ExMapTable
[Index
].ExGuidIndex
== GuidTableIdx
) {
412 // Found the next match
414 *TokenNumber
= ExMapTable
[Index
].ExTokenNumber
;
420 return EFI_NOT_FOUND
;
424 Find the PCD database.
426 @retval The base address of external PCD database binary.
427 @retval NULL Return NULL if not find.
434 DXE_PCD_DATABASE
*DxePcdDbBinary
;
438 DxePcdDbBinary
= NULL
;
440 // Search the External Pcd database from one section of current FFS,
441 // and read it to memory
443 Status
= GetSectionFromFfs (
446 (VOID
**) &DxePcdDbBinary
,
449 ASSERT_EFI_ERROR (Status
);
452 // Check the first bytes (Header Signature Guid) and build version.
454 if (!CompareGuid ((VOID
*)DxePcdDbBinary
, &gPcdDataBaseSignatureGuid
) ||
455 (DxePcdDbBinary
->BuildVersion
!= PCD_SERVICE_DXE_VERSION
)) {
459 return DxePcdDbBinary
;
463 Initialize the PCD database in DXE phase.
465 PCD database in DXE phase also contains PCD database in PEI phase which is copied
470 BuildPcdDxeDataBase (
474 PEI_PCD_DATABASE
*PeiDatabase
;
475 EFI_HOB_GUID_TYPE
*GuidHob
;
480 GuidHob
= GetFirstGuidHob (&gPcdDataBaseHobGuid
);
481 if (GuidHob
!= NULL
) {
484 // If no PEIMs use dynamic Pcd Entry, the Pcd Service PEIM
485 // should not be included at all. So the GuidHob could
486 // be NULL. If it is NULL, we just copy over the DXE Default
487 // Value to PCD Database.
490 PeiDatabase
= (PEI_PCD_DATABASE
*) GET_GUID_HOB_DATA (GuidHob
);
492 // Assign PCD Entries refereneced in PEI phase to PCD DATABASE
494 mPcdDatabase
.PeiDb
= PeiDatabase
;
498 // Assign PCD Entries with default value to PCD DATABASE
500 mPcdDatabase
.DxeDb
= LocateExPcdBinary ();
501 ASSERT(mPcdDatabase
.DxeDb
!= NULL
);
502 PcdDxeDbLen
= mPcdDatabase
.DxeDb
->Length
+ mPcdDatabase
.DxeDb
->UninitDataBaseSize
;
503 PcdDxeDb
= AllocateZeroPool (PcdDxeDbLen
);
504 CopyMem (PcdDxeDb
, mPcdDatabase
.DxeDb
, mPcdDatabase
.DxeDb
->Length
);
505 FreePool (mPcdDatabase
.DxeDb
);
506 mPcdDatabase
.DxeDb
= PcdDxeDb
;
509 // Initialized the external PCD database local variables
511 mPeiLocalTokenCount
= mPcdDatabase
.PeiDb
->LocalTokenCount
;
512 mDxeLocalTokenCount
= mPcdDatabase
.DxeDb
->LocalTokenCount
;
514 mPeiExMapppingTableSize
= mPcdDatabase
.PeiDb
->ExTokenCount
* sizeof (DYNAMICEX_MAPPING
);
515 mDxeExMapppingTableSize
= mPcdDatabase
.DxeDb
->ExTokenCount
* sizeof (DYNAMICEX_MAPPING
);
516 mPeiGuidTableSize
= mPcdDatabase
.PeiDb
->GuidTableCount
* sizeof(GUID
);
517 mDxeGuidTableSize
= mPcdDatabase
.DxeDb
->GuidTableCount
* sizeof (GUID
);
519 mPcdTotalTokenCount
= mPeiLocalTokenCount
+ mDxeLocalTokenCount
;
520 mPeiNexTokenCount
= mPeiLocalTokenCount
- mPcdDatabase
.PeiDb
->ExTokenCount
;
521 mDxeNexTokenCount
= mDxeLocalTokenCount
- mPcdDatabase
.DxeDb
->ExTokenCount
;
523 mPeiExMapTableEmpty
= (mPcdDatabase
.PeiDb
->ExTokenCount
== 0) ? TRUE
: FALSE
;
524 mDxeExMapTableEmpty
= (mPcdDatabase
.DxeDb
->ExTokenCount
== 0) ? TRUE
: FALSE
;
525 mPeiDatabaseEmpty
= (mPeiLocalTokenCount
== 0) ? TRUE
: FALSE
;
527 TmpTokenSpaceBufferCount
= mPcdDatabase
.PeiDb
->ExTokenCount
+ mPcdDatabase
.DxeDb
->ExTokenCount
;
528 TmpTokenSpaceBuffer
= (EFI_GUID
**)AllocateZeroPool(TmpTokenSpaceBufferCount
* sizeof (EFI_GUID
*));
531 // Initialized the Callback Function Table
533 mCallbackFnTable
= AllocateZeroPool (mPcdTotalTokenCount
* sizeof (LIST_ENTRY
));
534 ASSERT(mCallbackFnTable
!= NULL
);
537 // EBC compiler is very choosy. It may report warning about comparison
538 // between UINTN and 0 . So we add 1 in each size of the
541 for (Index
= 0; Index
+ 1 < mPcdTotalTokenCount
+ 1; Index
++) {
542 InitializeListHead (&mCallbackFnTable
[Index
]);
547 Get Variable which contains HII type PCD entry.
549 @param VariableGuid Variable's guid
550 @param VariableName Variable's unicode name string
551 @param VariableData Variable's data pointer,
552 @param VariableSize Variable's size.
554 @return the status of gRT->GetVariable
558 IN EFI_GUID
*VariableGuid
,
559 IN UINT16
*VariableName
,
560 OUT UINT8
**VariableData
,
561 OUT UINTN
*VariableSize
572 // Firstly get the real size of HII variable
574 Status
= gRT
->GetVariable (
575 (UINT16
*)VariableName
,
583 // Allocate buffer to hold whole variable data according to variable size.
585 if (Status
== EFI_BUFFER_TOO_SMALL
) {
586 Buffer
= (UINT8
*) AllocatePool (Size
);
588 ASSERT (Buffer
!= NULL
);
590 Status
= gRT
->GetVariable (
598 ASSERT (Status
== EFI_SUCCESS
);
599 *VariableData
= Buffer
;
600 *VariableSize
= Size
;
603 // Use Default Data only when variable is not found.
604 // For other error status, correct data can't be got, and trig ASSERT().
606 ASSERT (Status
== EFI_NOT_FOUND
);
613 Find the local token number according to system SKU ID.
615 @param LocalTokenNumber PCD token number
616 @param Size The size of PCD entry.
617 @param IsPeiDb If TRUE, the PCD entry is initialized in PEI phase.
618 If False, the PCD entry is initialized in DXE phase.
620 @return Token number according to system SKU ID.
624 GetSkuEnabledTokenNumber (
625 UINT32 LocalTokenNumber
,
637 ASSERT ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) == 0);
639 PcdDb
= IsPeiDb
? (UINT8
*) mPcdDatabase
.PeiDb
: (UINT8
*) mPcdDatabase
.DxeDb
;
641 SkuHead
= (SKU_HEAD
*) (PcdDb
+ (LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
));
642 Value
= (UINT8
*) (PcdDb
+ SkuHead
->SkuDataStartOffset
);
644 SkuIdTable
= (SKU_ID
*)(PcdDb
+ SkuHead
->SkuIdTableOffset
);
646 // Find the current system's SKU ID entry in SKU ID table.
649 for (Index
= 0; Index
< SkuIdTable
[0]; Index
++) {
650 if (mPcdDatabase
.PeiDb
->SystemSkuId
== SkuIdTable
[Index
+ 1]) {
657 // Find the default SKU ID entry in SKU ID table.
661 for (Index
= 0; Index
< SkuIdTable
[0]; Index
++) {
662 if (0 == SkuIdTable
[Index
+ 1]) {
667 ASSERT (Index
< SkuIdTable
[0]);
669 switch (LocalTokenNumber
& PCD_TYPE_ALL_SET
) {
671 Value
= (UINT8
*) &(((VPD_HEAD
*) Value
)[Index
]);
672 return (UINT32
) ((Value
- PcdDb
) | PCD_TYPE_VPD
);
675 Value
= (UINT8
*) &(((VARIABLE_HEAD
*) Value
)[Index
]);
676 return (UINT32
) ((Value
- PcdDb
) | PCD_TYPE_HII
);
678 case PCD_TYPE_HII
|PCD_TYPE_STRING
:
679 Value
= (UINT8
*) &(((VARIABLE_HEAD
*) Value
)[Index
]);
680 return (UINT32
) ((Value
- PcdDb
) | PCD_TYPE_HII
| PCD_TYPE_STRING
);
682 case PCD_TYPE_STRING
:
683 Value
= (UINT8
*) &(((STRING_HEAD
*) Value
)[Index
]);
684 return (UINT32
) ((Value
- PcdDb
) | PCD_TYPE_STRING
);
687 Value
+= Size
* Index
;
688 return (UINT32
) ((Value
- PcdDb
) | PCD_TYPE_DATA
);
701 Invoke the callback function when dynamic PCD entry was set, if this PCD entry
702 has registered callback function.
704 @param ExTokenNumber DynamicEx PCD's token number, if this PCD entry is dyanmicEx
706 @param Guid DynamicEx PCD's guid, if this PCD entry is dynamicEx type
708 @param TokenNumber PCD token number generated by build tools.
709 @param Data Value want to be set for this PCD entry
710 @param Size The size of value
714 InvokeCallbackOnSet (
715 UINT32 ExTokenNumber
,
716 CONST EFI_GUID
*Guid
, OPTIONAL
722 CALLBACK_FN_ENTRY
*FnTableEntry
;
723 LIST_ENTRY
*ListHead
;
724 LIST_ENTRY
*ListNode
;
727 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
728 // We have to decrement TokenNumber by 1 to make it usable
729 // as the array index of mCallbackFnTable[].
731 ListHead
= &mCallbackFnTable
[TokenNumber
- 1];
732 ListNode
= GetFirstNode (ListHead
);
734 while (ListNode
!= ListHead
) {
735 FnTableEntry
= CR_FNENTRY_FROM_LISTNODE (ListNode
, CALLBACK_FN_ENTRY
, Node
);
737 FnTableEntry
->CallbackFn(Guid
,
738 (Guid
== NULL
) ? TokenNumber
: ExTokenNumber
,
742 ListNode
= GetNextNode (ListHead
, ListNode
);
750 Wrapper function for setting non-pointer type value for a PCD entry.
752 @param TokenNumber Pcd token number autogenerated by build tools.
753 @param Data Value want to be set for PCD entry
754 @param Size Size of value.
756 @return status of SetWorker.
761 IN UINTN TokenNumber
,
766 return SetWorker (TokenNumber
, Data
, &Size
, FALSE
);
771 Set value for an PCD entry
773 @param TokenNumber Pcd token number autogenerated by build tools.
774 @param Data Value want to be set for PCD entry
775 @param Size Size of value.
776 @param PtrType If TRUE, the type of PCD entry's value is Pointer.
777 If False, the type of PCD entry's value is not Pointer.
779 @retval EFI_INVALID_PARAMETER If this PCD type is VPD, VPD PCD can not be set.
780 @retval EFI_INVALID_PARAMETER If Size can not be set to size table.
781 @retval EFI_INVALID_PARAMETER If Size of non-Ptr type PCD does not match the size information in PCD database.
782 @retval EFI_NOT_FOUND If value type of PCD entry is intergrate, but not in
783 range of UINT8, UINT16, UINT32, UINT64
784 @retval EFI_NOT_FOUND Can not find the PCD type according to token number.
788 IN UINTN TokenNumber
,
794 UINT32
*LocalTokenNumberTable
;
796 UINT32 LocalTokenNumber
;
801 UINTN VariableOffset
;
803 VARIABLE_HEAD
*VariableHead
;
808 UINTN TmpTokenNumber
;
811 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
812 // We have to decrement TokenNumber by 1 to make it usable
813 // as the array index.
817 TmpTokenNumber
= TokenNumber
;
820 // EBC compiler is very choosy. It may report warning about comparison
821 // between UINTN and 0 . So we add 1 in each size of the
824 ASSERT (TokenNumber
+ 1 < mPcdTotalTokenCount
+ 1);
828 // Get MaxSize first, then check new size with max buffer size.
830 GetPtrTypeSize (TokenNumber
, &MaxSize
);
831 if (*Size
> MaxSize
) {
833 return EFI_INVALID_PARAMETER
;
836 if (*Size
!= DxePcdGetSize (TokenNumber
+ 1)) {
837 return EFI_INVALID_PARAMETER
;
842 // EBC compiler is very choosy. It may report warning about comparison
843 // between UINTN and 0 . So we add 1 in each size of the
846 if ((TokenNumber
+ 1 < mPeiNexTokenCount
+ 1) ||
847 (TokenNumber
+ 1 >= mPeiLocalTokenCount
+ 1 && TokenNumber
+ 1 < (mPeiLocalTokenCount
+ mDxeNexTokenCount
+ 1))) {
848 InvokeCallbackOnSet (0, NULL
, TokenNumber
+ 1, Data
, *Size
);
852 // Aquire lock to prevent reentrance from TPL_CALLBACK level
854 EfiAcquireLock (&mPcdDatabaseLock
);
857 // EBC compiler is very choosy. It may report warning about comparison
858 // between UINTN and 0 . So we add 1 in each size of the
861 IsPeiDb
= (BOOLEAN
) ((TokenNumber
+ 1 < mPeiLocalTokenCount
+ 1) ? TRUE
: FALSE
);
863 LocalTokenNumberTable
= IsPeiDb
? (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
) :
864 (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
) ;
866 TokenNumber
= IsPeiDb
? TokenNumber
867 : TokenNumber
- mPeiLocalTokenCount
;
869 LocalTokenNumber
= LocalTokenNumberTable
[TokenNumber
];
871 if ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) == PCD_TYPE_SKU_ENABLED
) {
873 GetPtrTypeSize (TmpTokenNumber
, &MaxSize
);
877 LocalTokenNumber
= GetSkuEnabledTokenNumber (LocalTokenNumber
& ~PCD_TYPE_SKU_ENABLED
, MaxSize
, IsPeiDb
);
880 Offset
= LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
;
882 PcdDb
= IsPeiDb
? ((UINT8
*) mPcdDatabase
.PeiDb
) : ((UINT8
*) mPcdDatabase
.DxeDb
);
885 StringTable
= (UINT8
*) ((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->StringTableOffset
);
887 StringTable
= (UINT8
*) ((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->StringTableOffset
);
891 InternalData
= PcdDb
+ Offset
;
893 switch (LocalTokenNumber
& PCD_TYPE_ALL_SET
) {
896 Status
= EFI_INVALID_PARAMETER
;
899 case PCD_TYPE_STRING
:
900 if (SetPtrTypeSize (TmpTokenNumber
, Size
)) {
901 CopyMem (StringTable
+ *((STRING_HEAD
*)InternalData
), Data
, *Size
);
902 Status
= EFI_SUCCESS
;
904 Status
= EFI_INVALID_PARAMETER
;
908 case PCD_TYPE_HII
|PCD_TYPE_STRING
:
911 if (!SetPtrTypeSize (TmpTokenNumber
, Size
)) {
912 Status
= EFI_INVALID_PARAMETER
;
918 GuidTable
= (EFI_GUID
*) ((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->GuidTableOffset
);
920 GuidTable
= (EFI_GUID
*) ((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->GuidTableOffset
);
923 VariableHead
= (VARIABLE_HEAD
*) (PcdDb
+ Offset
);
925 Guid
= GuidTable
+ VariableHead
->GuidTableIndex
;
926 Name
= (UINT16
*) (StringTable
+ VariableHead
->StringIndex
);
927 VariableOffset
= VariableHead
->Offset
;
928 Status
= SetHiiVariable (Guid
, Name
, Data
, *Size
, VariableOffset
);
930 if (EFI_NOT_FOUND
== Status
) {
931 if ((LocalTokenNumber
& PCD_TYPE_ALL_SET
) == (PCD_TYPE_HII
|PCD_TYPE_STRING
)) {
933 StringTable
+ *(STRING_HEAD
*)(PcdDb
+ VariableHead
->DefaultValueOffset
),
938 CopyMem (PcdDb
+ VariableHead
->DefaultValueOffset
, Data
, *Size
);
940 Status
= EFI_SUCCESS
;
946 if (SetPtrTypeSize (TmpTokenNumber
, Size
)) {
947 CopyMem (InternalData
, Data
, *Size
);
948 Status
= EFI_SUCCESS
;
950 Status
= EFI_INVALID_PARAMETER
;
955 Status
= EFI_SUCCESS
;
958 *((UINT8
*) InternalData
) = *((UINT8
*) Data
);
962 *((UINT16
*) InternalData
) = *((UINT16
*) Data
);
966 *((UINT32
*) InternalData
) = *((UINT32
*) Data
);
970 *((UINT64
*) InternalData
) = *((UINT64
*) Data
);
975 Status
= EFI_NOT_FOUND
;
982 Status
= EFI_NOT_FOUND
;
986 EfiReleaseLock (&mPcdDatabaseLock
);
992 Wrapper function for get PCD value for dynamic-ex PCD.
994 @param Guid Token space guid for dynamic-ex PCD.
995 @param ExTokenNumber Token number for dynamic-ex PCD.
996 @param GetSize The size of dynamic-ex PCD value.
998 @return PCD entry in PCD database.
1003 IN CONST EFI_GUID
*Guid
,
1004 IN UINTN ExTokenNumber
,
1008 return GetWorker(GetExPcdTokenNumber (Guid
, (UINT32
) ExTokenNumber
), GetSize
);
1012 Wrapper function for set PCD value for non-Pointer type dynamic-ex PCD.
1014 @param ExTokenNumber Token number for dynamic-ex PCD.
1015 @param Guid Token space guid for dynamic-ex PCD.
1016 @param Data Value want to be set.
1017 @param SetSize The size of value.
1019 @return status of ExSetWorker().
1024 IN UINTN ExTokenNumber
,
1025 IN CONST EFI_GUID
*Guid
,
1030 return ExSetWorker (ExTokenNumber
, Guid
, Data
, &SetSize
, FALSE
);
1034 Set value for a dynamic-ex PCD entry.
1036 This routine find the local token number according to dynamic-ex PCD's token
1037 space guid and token number firstly, and invoke callback function if this PCD
1038 entry registered callback function. Finally, invoken general SetWorker to set
1041 @param ExTokenNumber Dynamic-ex PCD token number.
1042 @param Guid Token space guid for dynamic-ex PCD.
1043 @param Data PCD value want to be set
1044 @param SetSize Size of value.
1045 @param PtrType If TRUE, this PCD entry is pointer type.
1046 If FALSE, this PCD entry is not pointer type.
1048 @return status of SetWorker().
1053 IN UINTN ExTokenNumber
,
1054 IN CONST EFI_GUID
*Guid
,
1056 IN OUT UINTN
*SetSize
,
1062 TokenNumber
= GetExPcdTokenNumber (Guid
, (UINT32
) ExTokenNumber
);
1064 InvokeCallbackOnSet ((UINT32
) ExTokenNumber
, Guid
, TokenNumber
, Data
, *SetSize
);
1066 return SetWorker (TokenNumber
, Data
, SetSize
, PtrType
);
1071 Set value for HII-type PCD.
1073 A HII-type PCD's value is stored in a variable. Setting/Getting the value of
1074 HII-type PCD is to visit this variable.
1076 @param VariableGuid Guid of variable which stored value of a HII-type PCD.
1077 @param VariableName Unicode name of variable which stored value of a HII-type PCD.
1078 @param Data Value want to be set.
1079 @param DataSize Size of value
1080 @param Offset Value offset of HII-type PCD in variable.
1082 @return status of GetVariable()/SetVariable().
1087 IN EFI_GUID
*VariableGuid
,
1088 IN UINT16
*VariableName
,
1089 IN CONST VOID
*Data
,
1104 // Try to get original variable size information.
1106 Status
= gRT
->GetVariable (
1107 (UINT16
*)VariableName
,
1114 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1116 // Patch new PCD's value to offset in given HII variable.
1118 if (Size
>= (DataSize
+ Offset
)) {
1121 SetSize
= DataSize
+ Offset
;
1123 Buffer
= AllocatePool (SetSize
);
1124 ASSERT (Buffer
!= NULL
);
1126 Status
= gRT
->GetVariable (
1134 ASSERT_EFI_ERROR (Status
);
1136 CopyMem ((UINT8
*)Buffer
+ Offset
, Data
, DataSize
);
1138 Status
= gRT
->SetVariable (
1148 } else if (Status
== EFI_NOT_FOUND
) {
1150 // If variable does not exist, a new variable need to be created.
1153 Size
= Offset
+ DataSize
;
1155 Buffer
= AllocateZeroPool (Size
);
1156 ASSERT (Buffer
!= NULL
);
1158 CopyMem ((UINT8
*)Buffer
+ Offset
, Data
, DataSize
);
1160 Status
= gRT
->SetVariable (
1163 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1173 // If we drop to here, the value is failed to be written in to variable area
1174 // So, we will save the data in the PCD Database's volatile area.
1180 Get Token Number according to dynamic-ex PCD's {token space guid:token number}
1182 A dynamic-ex type PCD, developer must provide pair of token space guid: token number
1183 in DEC file. PCD database maintain a mapping table that translate pair of {token
1184 space guid: token number} to Token Number.
1186 @param Guid Token space guid for dynamic-ex PCD entry.
1187 @param ExTokenNumber Dynamic-ex PCD token number.
1189 @return Token Number for dynamic-ex PCD.
1193 GetExPcdTokenNumber (
1194 IN CONST EFI_GUID
*Guid
,
1195 IN UINT32 ExTokenNumber
1199 DYNAMICEX_MAPPING
*ExMap
;
1200 EFI_GUID
*GuidTable
;
1201 EFI_GUID
*MatchGuid
;
1204 if (!mPeiDatabaseEmpty
) {
1205 ExMap
= (DYNAMICEX_MAPPING
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->ExMapTableOffset
);
1206 GuidTable
= (EFI_GUID
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->GuidTableOffset
);
1208 MatchGuid
= ScanGuid (GuidTable
, mPeiGuidTableSize
, Guid
);
1210 if (MatchGuid
!= NULL
) {
1212 MatchGuidIdx
= MatchGuid
- GuidTable
;
1214 for (Index
= 0; Index
< mPeiExMapppingTableSize
; Index
++) {
1215 if ((ExTokenNumber
== ExMap
[Index
].ExTokenNumber
) &&
1216 (MatchGuidIdx
== ExMap
[Index
].ExGuidIndex
)) {
1217 return ExMap
[Index
].TokenNumber
;
1223 ExMap
= (DYNAMICEX_MAPPING
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->ExMapTableOffset
);
1224 GuidTable
= (EFI_GUID
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->GuidTableOffset
);
1226 MatchGuid
= ScanGuid (GuidTable
, mDxeGuidTableSize
, Guid
);
1228 // We need to ASSERT here. If GUID can't be found in GuidTable, this is a
1229 // error in the BUILD system.
1231 ASSERT (MatchGuid
!= NULL
);
1233 MatchGuidIdx
= MatchGuid
- GuidTable
;
1235 for (Index
= 0; Index
< mDxeExMapppingTableSize
; Index
++) {
1236 if ((ExTokenNumber
== ExMap
[Index
].ExTokenNumber
) &&
1237 (MatchGuidIdx
== ExMap
[Index
].ExGuidIndex
)) {
1238 return ExMap
[Index
].TokenNumber
;
1248 Get SKU ID table from PCD database.
1250 @param LocalTokenNumberTableIdx Index of local token number in token number table.
1251 @param IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
1252 If FALSE, the pcd entry is initialized in DXE phase.
1253 @return Pointer to SKU ID array table
1258 IN UINTN LocalTokenNumberTableIdx
,
1263 UINTN LocalTokenNumber
;
1267 LocalTokenNumber
= *((UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
) + LocalTokenNumberTableIdx
);
1268 Database
= (UINT8
*) mPcdDatabase
.PeiDb
;
1270 LocalTokenNumber
= *((UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
) + LocalTokenNumberTableIdx
);
1271 Database
= (UINT8
*) mPcdDatabase
.DxeDb
;
1274 ASSERT ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) != 0);
1276 SkuHead
= (SKU_HEAD
*) ((UINT8
*)Database
+ (LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
));
1278 return (SKU_ID
*) (Database
+ SkuHead
->SkuIdTableOffset
);
1283 Wrapper function of getting index of PCD entry in size table.
1285 @param LocalTokenNumberTableIdx Index of this PCD in local token number table.
1286 @param IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
1287 If FALSE, the pcd entry is initialized in DXE phase.
1289 @return index of PCD entry in size table.
1293 IN UINTN LocalTokenNumberTableIdx
,
1297 UINT32
*LocalTokenNumberTable
;
1298 UINTN LocalTokenNumber
;
1304 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
);
1306 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
);
1311 for (Index
= 0; Index
< LocalTokenNumberTableIdx
; Index
++) {
1312 LocalTokenNumber
= LocalTokenNumberTable
[Index
];
1314 if ((LocalTokenNumber
& PCD_DATUM_TYPE_ALL_SET
) == PCD_DATUM_TYPE_POINTER
) {
1316 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1319 if ((LocalTokenNumber
& PCD_TYPE_VPD
) != 0) {
1321 // We have only two entry for VPD enabled PCD entry:
1324 // Current size is equal to MAX size.
1328 if ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) == 0) {
1330 // We have only two entry for Non-Sku enabled PCD entry:
1337 // We have these entry for SKU enabled PCD entry
1339 // 2) Current Size for each SKU_ID (It is equal to MaxSku).
1341 SkuIdTable
= GetSkuIdArray (Index
, IsPeiDb
);
1342 SizeTableIdx
+= (UINTN
)*SkuIdTable
+ 1;
1349 return SizeTableIdx
;
1353 Get size of POINTER type PCD value.
1355 @param LocalTokenNumberTableIdx Index of local token number in local token number table.
1356 @param MaxSize Maxmium size of POINTER type PCD value.
1358 @return size of POINTER type PCD value.
1363 IN UINTN LocalTokenNumberTableIdx
,
1368 UINTN LocalTokenNumber
;
1370 SIZE_INFO
*SizeTable
;
1373 UINT32
*LocalTokenNumberTable
;
1375 // EBC compiler is very choosy. It may report warning about comparison
1376 // between UINTN and 0 . So we add 1 in each size of the
1378 IsPeiDb
= (BOOLEAN
) (LocalTokenNumberTableIdx
+ 1 < mPeiLocalTokenCount
+ 1);
1382 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
);
1383 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->SizeTableOffset
);
1385 LocalTokenNumberTableIdx
-= mPeiLocalTokenCount
;
1386 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
);
1387 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->SizeTableOffset
);
1390 LocalTokenNumber
= LocalTokenNumberTable
[LocalTokenNumberTableIdx
];
1392 ASSERT ((LocalTokenNumber
& PCD_DATUM_TYPE_ALL_SET
) == PCD_DATUM_TYPE_POINTER
);
1394 SizeTableIdx
= GetSizeTableIndex (LocalTokenNumberTableIdx
, IsPeiDb
);
1396 *MaxSize
= SizeTable
[SizeTableIdx
];
1398 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1401 if ((LocalTokenNumber
& PCD_TYPE_VPD
) != 0) {
1403 // We have only two entry for VPD enabled PCD entry:
1406 // We consider current size is equal to MAX size.
1410 if ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) == 0) {
1412 // We have only two entry for Non-Sku enabled PCD entry:
1416 return SizeTable
[SizeTableIdx
+ 1];
1419 // We have these entry for SKU enabled PCD entry
1421 // 2) Current Size for each SKU_ID (It is equal to MaxSku).
1423 SkuIdTable
= GetSkuIdArray (LocalTokenNumberTableIdx
, IsPeiDb
);
1424 for (Index
= 0; Index
< SkuIdTable
[0]; Index
++) {
1425 if (SkuIdTable
[1 + Index
] == mPcdDatabase
.PeiDb
->SystemSkuId
) {
1426 return SizeTable
[SizeTableIdx
+ 1 + Index
];
1429 return SizeTable
[SizeTableIdx
+ 1];
1435 Set size of POINTER type PCD value. The size should not exceed the maximum size
1438 @param LocalTokenNumberTableIdx Index of local token number in local token number table.
1439 @param CurrentSize Size of POINTER type PCD value.
1441 @retval TRUE Success to set size of PCD value.
1442 @retval FALSE Fail to set size of PCD value.
1446 IN UINTN LocalTokenNumberTableIdx
,
1447 IN OUT UINTN
*CurrentSize
1451 UINTN LocalTokenNumber
;
1453 SIZE_INFO
*SizeTable
;
1457 UINT32
*LocalTokenNumberTable
;
1460 // EBC compiler is very choosy. It may report warning about comparison
1461 // between UINTN and 0 . So we add 1 in each size of the
1464 IsPeiDb
= (BOOLEAN
) (LocalTokenNumberTableIdx
+ 1 < mPeiLocalTokenCount
+ 1);
1467 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
);
1468 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->SizeTableOffset
);
1470 LocalTokenNumberTableIdx
-= mPeiLocalTokenCount
;
1471 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
);
1472 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->SizeTableOffset
);
1475 LocalTokenNumber
= LocalTokenNumberTable
[LocalTokenNumberTableIdx
];
1477 ASSERT ((LocalTokenNumber
& PCD_DATUM_TYPE_ALL_SET
) == PCD_DATUM_TYPE_POINTER
);
1479 SizeTableIdx
= GetSizeTableIndex (LocalTokenNumberTableIdx
, IsPeiDb
);
1481 MaxSize
= SizeTable
[SizeTableIdx
];
1483 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1486 if ((LocalTokenNumber
& PCD_TYPE_VPD
) != 0) {
1488 // We shouldn't come here as we don't support SET for VPD
1493 if ((*CurrentSize
> MaxSize
) ||
1494 (*CurrentSize
== MAX_ADDRESS
)) {
1495 *CurrentSize
= MaxSize
;
1499 if ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) == 0) {
1501 // We have only two entry for Non-Sku enabled PCD entry:
1505 SizeTable
[SizeTableIdx
+ 1] = (SIZE_INFO
) *CurrentSize
;
1509 // We have these entry for SKU enabled PCD entry
1511 // 2) Current Size for each SKU_ID (It is equal to MaxSku).
1513 SkuIdTable
= GetSkuIdArray (LocalTokenNumberTableIdx
, IsPeiDb
);
1514 for (Index
= 0; Index
< SkuIdTable
[0]; Index
++) {
1515 if (SkuIdTable
[1 + Index
] == mPcdDatabase
.PeiDb
->SystemSkuId
) {
1516 SizeTable
[SizeTableIdx
+ 1 + Index
] = (SIZE_INFO
) *CurrentSize
;
1520 SizeTable
[SizeTableIdx
+ 1] = (SIZE_INFO
) *CurrentSize
;