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 ASSERT (PcdDxeDb
!= NULL
);
505 CopyMem (PcdDxeDb
, mPcdDatabase
.DxeDb
, mPcdDatabase
.DxeDb
->Length
);
506 FreePool (mPcdDatabase
.DxeDb
);
507 mPcdDatabase
.DxeDb
= PcdDxeDb
;
510 // Initialized the external PCD database local variables
512 mPeiLocalTokenCount
= mPcdDatabase
.PeiDb
->LocalTokenCount
;
513 mDxeLocalTokenCount
= mPcdDatabase
.DxeDb
->LocalTokenCount
;
515 mPeiExMapppingTableSize
= mPcdDatabase
.PeiDb
->ExTokenCount
* sizeof (DYNAMICEX_MAPPING
);
516 mDxeExMapppingTableSize
= mPcdDatabase
.DxeDb
->ExTokenCount
* sizeof (DYNAMICEX_MAPPING
);
517 mPeiGuidTableSize
= mPcdDatabase
.PeiDb
->GuidTableCount
* sizeof(GUID
);
518 mDxeGuidTableSize
= mPcdDatabase
.DxeDb
->GuidTableCount
* sizeof (GUID
);
520 mPcdTotalTokenCount
= mPeiLocalTokenCount
+ mDxeLocalTokenCount
;
521 mPeiNexTokenCount
= mPeiLocalTokenCount
- mPcdDatabase
.PeiDb
->ExTokenCount
;
522 mDxeNexTokenCount
= mDxeLocalTokenCount
- mPcdDatabase
.DxeDb
->ExTokenCount
;
524 mPeiExMapTableEmpty
= (mPcdDatabase
.PeiDb
->ExTokenCount
== 0) ? TRUE
: FALSE
;
525 mDxeExMapTableEmpty
= (mPcdDatabase
.DxeDb
->ExTokenCount
== 0) ? TRUE
: FALSE
;
526 mPeiDatabaseEmpty
= (mPeiLocalTokenCount
== 0) ? TRUE
: FALSE
;
528 TmpTokenSpaceBufferCount
= mPcdDatabase
.PeiDb
->ExTokenCount
+ mPcdDatabase
.DxeDb
->ExTokenCount
;
529 TmpTokenSpaceBuffer
= (EFI_GUID
**)AllocateZeroPool(TmpTokenSpaceBufferCount
* sizeof (EFI_GUID
*));
532 // Initialized the Callback Function Table
534 mCallbackFnTable
= AllocateZeroPool (mPcdTotalTokenCount
* sizeof (LIST_ENTRY
));
535 ASSERT(mCallbackFnTable
!= NULL
);
538 // EBC compiler is very choosy. It may report warning about comparison
539 // between UINTN and 0 . So we add 1 in each size of the
542 for (Index
= 0; Index
+ 1 < mPcdTotalTokenCount
+ 1; Index
++) {
543 InitializeListHead (&mCallbackFnTable
[Index
]);
548 Get Variable which contains HII type PCD entry.
550 @param VariableGuid Variable's guid
551 @param VariableName Variable's unicode name string
552 @param VariableData Variable's data pointer,
553 @param VariableSize Variable's size.
555 @return the status of gRT->GetVariable
559 IN EFI_GUID
*VariableGuid
,
560 IN UINT16
*VariableName
,
561 OUT UINT8
**VariableData
,
562 OUT UINTN
*VariableSize
573 // Firstly get the real size of HII variable
575 Status
= gRT
->GetVariable (
576 (UINT16
*)VariableName
,
584 // Allocate buffer to hold whole variable data according to variable size.
586 if (Status
== EFI_BUFFER_TOO_SMALL
) {
587 Buffer
= (UINT8
*) AllocatePool (Size
);
589 ASSERT (Buffer
!= NULL
);
591 Status
= gRT
->GetVariable (
599 ASSERT (Status
== EFI_SUCCESS
);
600 *VariableData
= Buffer
;
601 *VariableSize
= Size
;
604 // Use Default Data only when variable is not found.
605 // For other error status, correct data can't be got, and trig ASSERT().
607 ASSERT (Status
== EFI_NOT_FOUND
);
614 Find the local token number according to system SKU ID.
616 @param LocalTokenNumber PCD token number
617 @param Size The size of PCD entry.
618 @param IsPeiDb If TRUE, the PCD entry is initialized in PEI phase.
619 If False, the PCD entry is initialized in DXE phase.
621 @return Token number according to system SKU ID.
625 GetSkuEnabledTokenNumber (
626 UINT32 LocalTokenNumber
,
638 ASSERT ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) == 0);
640 PcdDb
= IsPeiDb
? (UINT8
*) mPcdDatabase
.PeiDb
: (UINT8
*) mPcdDatabase
.DxeDb
;
642 SkuHead
= (SKU_HEAD
*) (PcdDb
+ (LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
));
643 Value
= (UINT8
*) (PcdDb
+ SkuHead
->SkuDataStartOffset
);
645 SkuIdTable
= (SKU_ID
*)(PcdDb
+ SkuHead
->SkuIdTableOffset
);
647 // Find the current system's SKU ID entry in SKU ID table.
650 for (Index
= 0; Index
< SkuIdTable
[0]; Index
++) {
651 if (mPcdDatabase
.PeiDb
->SystemSkuId
== SkuIdTable
[Index
+ 1]) {
658 // Find the default SKU ID entry in SKU ID table.
662 for (Index
= 0; Index
< SkuIdTable
[0]; Index
++) {
663 if (0 == SkuIdTable
[Index
+ 1]) {
668 ASSERT (Index
< SkuIdTable
[0]);
670 switch (LocalTokenNumber
& PCD_TYPE_ALL_SET
) {
672 Value
= (UINT8
*) &(((VPD_HEAD
*) Value
)[Index
]);
673 return (UINT32
) ((Value
- PcdDb
) | PCD_TYPE_VPD
);
676 Value
= (UINT8
*) &(((VARIABLE_HEAD
*) Value
)[Index
]);
677 return (UINT32
) ((Value
- PcdDb
) | PCD_TYPE_HII
);
679 case PCD_TYPE_HII
|PCD_TYPE_STRING
:
680 Value
= (UINT8
*) &(((VARIABLE_HEAD
*) Value
)[Index
]);
681 return (UINT32
) ((Value
- PcdDb
) | PCD_TYPE_HII
| PCD_TYPE_STRING
);
683 case PCD_TYPE_STRING
:
684 Value
= (UINT8
*) &(((STRING_HEAD
*) Value
)[Index
]);
685 return (UINT32
) ((Value
- PcdDb
) | PCD_TYPE_STRING
);
688 Value
+= Size
* Index
;
689 return (UINT32
) ((Value
- PcdDb
) | PCD_TYPE_DATA
);
702 Invoke the callback function when dynamic PCD entry was set, if this PCD entry
703 has registered callback function.
705 @param ExTokenNumber DynamicEx PCD's token number, if this PCD entry is dyanmicEx
707 @param Guid DynamicEx PCD's guid, if this PCD entry is dynamicEx type
709 @param TokenNumber PCD token number generated by build tools.
710 @param Data Value want to be set for this PCD entry
711 @param Size The size of value
715 InvokeCallbackOnSet (
716 UINT32 ExTokenNumber
,
717 CONST EFI_GUID
*Guid
, OPTIONAL
723 CALLBACK_FN_ENTRY
*FnTableEntry
;
724 LIST_ENTRY
*ListHead
;
725 LIST_ENTRY
*ListNode
;
728 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
729 // We have to decrement TokenNumber by 1 to make it usable
730 // as the array index of mCallbackFnTable[].
732 ListHead
= &mCallbackFnTable
[TokenNumber
- 1];
733 ListNode
= GetFirstNode (ListHead
);
735 while (ListNode
!= ListHead
) {
736 FnTableEntry
= CR_FNENTRY_FROM_LISTNODE (ListNode
, CALLBACK_FN_ENTRY
, Node
);
738 FnTableEntry
->CallbackFn(Guid
,
739 (Guid
== NULL
) ? TokenNumber
: ExTokenNumber
,
743 ListNode
= GetNextNode (ListHead
, ListNode
);
751 Wrapper function for setting non-pointer type value for a PCD entry.
753 @param TokenNumber Pcd token number autogenerated by build tools.
754 @param Data Value want to be set for PCD entry
755 @param Size Size of value.
757 @return status of SetWorker.
762 IN UINTN TokenNumber
,
767 return SetWorker (TokenNumber
, Data
, &Size
, FALSE
);
772 Set value for an PCD entry
774 @param TokenNumber Pcd token number autogenerated by build tools.
775 @param Data Value want to be set for PCD entry
776 @param Size Size of value.
777 @param PtrType If TRUE, the type of PCD entry's value is Pointer.
778 If False, the type of PCD entry's value is not Pointer.
780 @retval EFI_INVALID_PARAMETER If this PCD type is VPD, VPD PCD can not be set.
781 @retval EFI_INVALID_PARAMETER If Size can not be set to size table.
782 @retval EFI_INVALID_PARAMETER If Size of non-Ptr type PCD does not match the size information in PCD database.
783 @retval EFI_NOT_FOUND If value type of PCD entry is intergrate, but not in
784 range of UINT8, UINT16, UINT32, UINT64
785 @retval EFI_NOT_FOUND Can not find the PCD type according to token number.
789 IN UINTN TokenNumber
,
795 UINT32
*LocalTokenNumberTable
;
797 UINT32 LocalTokenNumber
;
802 UINTN VariableOffset
;
804 VARIABLE_HEAD
*VariableHead
;
809 UINTN TmpTokenNumber
;
812 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
813 // We have to decrement TokenNumber by 1 to make it usable
814 // as the array index.
818 TmpTokenNumber
= TokenNumber
;
821 // EBC compiler is very choosy. It may report warning about comparison
822 // between UINTN and 0 . So we add 1 in each size of the
825 ASSERT (TokenNumber
+ 1 < mPcdTotalTokenCount
+ 1);
829 // Get MaxSize first, then check new size with max buffer size.
831 GetPtrTypeSize (TokenNumber
, &MaxSize
);
832 if (*Size
> MaxSize
) {
834 return EFI_INVALID_PARAMETER
;
837 if (*Size
!= DxePcdGetSize (TokenNumber
+ 1)) {
838 return EFI_INVALID_PARAMETER
;
843 // EBC compiler is very choosy. It may report warning about comparison
844 // between UINTN and 0 . So we add 1 in each size of the
847 if ((TokenNumber
+ 1 < mPeiNexTokenCount
+ 1) ||
848 (TokenNumber
+ 1 >= mPeiLocalTokenCount
+ 1 && TokenNumber
+ 1 < (mPeiLocalTokenCount
+ mDxeNexTokenCount
+ 1))) {
849 InvokeCallbackOnSet (0, NULL
, TokenNumber
+ 1, Data
, *Size
);
853 // Aquire lock to prevent reentrance from TPL_CALLBACK level
855 EfiAcquireLock (&mPcdDatabaseLock
);
858 // EBC compiler is very choosy. It may report warning about comparison
859 // between UINTN and 0 . So we add 1 in each size of the
862 IsPeiDb
= (BOOLEAN
) ((TokenNumber
+ 1 < mPeiLocalTokenCount
+ 1) ? TRUE
: FALSE
);
864 LocalTokenNumberTable
= IsPeiDb
? (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
) :
865 (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
) ;
867 TokenNumber
= IsPeiDb
? TokenNumber
868 : TokenNumber
- mPeiLocalTokenCount
;
870 LocalTokenNumber
= LocalTokenNumberTable
[TokenNumber
];
872 if ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) == PCD_TYPE_SKU_ENABLED
) {
874 GetPtrTypeSize (TmpTokenNumber
, &MaxSize
);
878 LocalTokenNumber
= GetSkuEnabledTokenNumber (LocalTokenNumber
& ~PCD_TYPE_SKU_ENABLED
, MaxSize
, IsPeiDb
);
881 Offset
= LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
;
883 PcdDb
= IsPeiDb
? ((UINT8
*) mPcdDatabase
.PeiDb
) : ((UINT8
*) mPcdDatabase
.DxeDb
);
886 StringTable
= (UINT8
*) ((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->StringTableOffset
);
888 StringTable
= (UINT8
*) ((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->StringTableOffset
);
892 InternalData
= PcdDb
+ Offset
;
894 switch (LocalTokenNumber
& PCD_TYPE_ALL_SET
) {
897 Status
= EFI_INVALID_PARAMETER
;
900 case PCD_TYPE_STRING
:
901 if (SetPtrTypeSize (TmpTokenNumber
, Size
)) {
902 CopyMem (StringTable
+ *((STRING_HEAD
*)InternalData
), Data
, *Size
);
903 Status
= EFI_SUCCESS
;
905 Status
= EFI_INVALID_PARAMETER
;
909 case PCD_TYPE_HII
|PCD_TYPE_STRING
:
912 if (!SetPtrTypeSize (TmpTokenNumber
, Size
)) {
913 Status
= EFI_INVALID_PARAMETER
;
919 GuidTable
= (EFI_GUID
*) ((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->GuidTableOffset
);
921 GuidTable
= (EFI_GUID
*) ((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->GuidTableOffset
);
924 VariableHead
= (VARIABLE_HEAD
*) (PcdDb
+ Offset
);
926 Guid
= GuidTable
+ VariableHead
->GuidTableIndex
;
927 Name
= (UINT16
*) (StringTable
+ VariableHead
->StringIndex
);
928 VariableOffset
= VariableHead
->Offset
;
929 Status
= SetHiiVariable (Guid
, Name
, Data
, *Size
, VariableOffset
);
931 if (EFI_NOT_FOUND
== Status
) {
932 if ((LocalTokenNumber
& PCD_TYPE_ALL_SET
) == (PCD_TYPE_HII
|PCD_TYPE_STRING
)) {
934 StringTable
+ *(STRING_HEAD
*)(PcdDb
+ VariableHead
->DefaultValueOffset
),
939 CopyMem (PcdDb
+ VariableHead
->DefaultValueOffset
, Data
, *Size
);
941 Status
= EFI_SUCCESS
;
947 if (SetPtrTypeSize (TmpTokenNumber
, Size
)) {
948 CopyMem (InternalData
, Data
, *Size
);
949 Status
= EFI_SUCCESS
;
951 Status
= EFI_INVALID_PARAMETER
;
956 Status
= EFI_SUCCESS
;
959 *((UINT8
*) InternalData
) = *((UINT8
*) Data
);
963 *((UINT16
*) InternalData
) = *((UINT16
*) Data
);
967 *((UINT32
*) InternalData
) = *((UINT32
*) Data
);
971 *((UINT64
*) InternalData
) = *((UINT64
*) Data
);
976 Status
= EFI_NOT_FOUND
;
983 Status
= EFI_NOT_FOUND
;
987 EfiReleaseLock (&mPcdDatabaseLock
);
993 Wrapper function for get PCD value for dynamic-ex PCD.
995 @param Guid Token space guid for dynamic-ex PCD.
996 @param ExTokenNumber Token number for dynamic-ex PCD.
997 @param GetSize The size of dynamic-ex PCD value.
999 @return PCD entry in PCD database.
1004 IN CONST EFI_GUID
*Guid
,
1005 IN UINTN ExTokenNumber
,
1009 return GetWorker(GetExPcdTokenNumber (Guid
, (UINT32
) ExTokenNumber
), GetSize
);
1013 Wrapper function for set PCD value for non-Pointer type dynamic-ex PCD.
1015 @param ExTokenNumber Token number for dynamic-ex PCD.
1016 @param Guid Token space guid for dynamic-ex PCD.
1017 @param Data Value want to be set.
1018 @param SetSize The size of value.
1020 @return status of ExSetWorker().
1025 IN UINTN ExTokenNumber
,
1026 IN CONST EFI_GUID
*Guid
,
1031 return ExSetWorker (ExTokenNumber
, Guid
, Data
, &SetSize
, FALSE
);
1035 Set value for a dynamic-ex PCD entry.
1037 This routine find the local token number according to dynamic-ex PCD's token
1038 space guid and token number firstly, and invoke callback function if this PCD
1039 entry registered callback function. Finally, invoken general SetWorker to set
1042 @param ExTokenNumber Dynamic-ex PCD token number.
1043 @param Guid Token space guid for dynamic-ex PCD.
1044 @param Data PCD value want to be set
1045 @param SetSize Size of value.
1046 @param PtrType If TRUE, this PCD entry is pointer type.
1047 If FALSE, this PCD entry is not pointer type.
1049 @return status of SetWorker().
1054 IN UINTN ExTokenNumber
,
1055 IN CONST EFI_GUID
*Guid
,
1057 IN OUT UINTN
*SetSize
,
1063 TokenNumber
= GetExPcdTokenNumber (Guid
, (UINT32
) ExTokenNumber
);
1065 InvokeCallbackOnSet ((UINT32
) ExTokenNumber
, Guid
, TokenNumber
, Data
, *SetSize
);
1067 return SetWorker (TokenNumber
, Data
, SetSize
, PtrType
);
1072 Set value for HII-type PCD.
1074 A HII-type PCD's value is stored in a variable. Setting/Getting the value of
1075 HII-type PCD is to visit this variable.
1077 @param VariableGuid Guid of variable which stored value of a HII-type PCD.
1078 @param VariableName Unicode name of variable which stored value of a HII-type PCD.
1079 @param Data Value want to be set.
1080 @param DataSize Size of value
1081 @param Offset Value offset of HII-type PCD in variable.
1083 @return status of GetVariable()/SetVariable().
1088 IN EFI_GUID
*VariableGuid
,
1089 IN UINT16
*VariableName
,
1090 IN CONST VOID
*Data
,
1105 // Try to get original variable size information.
1107 Status
= gRT
->GetVariable (
1108 (UINT16
*)VariableName
,
1115 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1117 // Patch new PCD's value to offset in given HII variable.
1119 if (Size
>= (DataSize
+ Offset
)) {
1122 SetSize
= DataSize
+ Offset
;
1124 Buffer
= AllocatePool (SetSize
);
1125 ASSERT (Buffer
!= NULL
);
1127 Status
= gRT
->GetVariable (
1135 ASSERT_EFI_ERROR (Status
);
1137 CopyMem ((UINT8
*)Buffer
+ Offset
, Data
, DataSize
);
1139 Status
= gRT
->SetVariable (
1149 } else if (Status
== EFI_NOT_FOUND
) {
1151 // If variable does not exist, a new variable need to be created.
1154 Size
= Offset
+ DataSize
;
1156 Buffer
= AllocateZeroPool (Size
);
1157 ASSERT (Buffer
!= NULL
);
1159 CopyMem ((UINT8
*)Buffer
+ Offset
, Data
, DataSize
);
1161 Status
= gRT
->SetVariable (
1164 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1174 // If we drop to here, the value is failed to be written in to variable area
1175 // So, we will save the data in the PCD Database's volatile area.
1181 Get Token Number according to dynamic-ex PCD's {token space guid:token number}
1183 A dynamic-ex type PCD, developer must provide pair of token space guid: token number
1184 in DEC file. PCD database maintain a mapping table that translate pair of {token
1185 space guid: token number} to Token Number.
1187 @param Guid Token space guid for dynamic-ex PCD entry.
1188 @param ExTokenNumber Dynamic-ex PCD token number.
1190 @return Token Number for dynamic-ex PCD.
1194 GetExPcdTokenNumber (
1195 IN CONST EFI_GUID
*Guid
,
1196 IN UINT32 ExTokenNumber
1200 DYNAMICEX_MAPPING
*ExMap
;
1201 EFI_GUID
*GuidTable
;
1202 EFI_GUID
*MatchGuid
;
1205 if (!mPeiDatabaseEmpty
) {
1206 ExMap
= (DYNAMICEX_MAPPING
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->ExMapTableOffset
);
1207 GuidTable
= (EFI_GUID
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->GuidTableOffset
);
1209 MatchGuid
= ScanGuid (GuidTable
, mPeiGuidTableSize
, Guid
);
1211 if (MatchGuid
!= NULL
) {
1213 MatchGuidIdx
= MatchGuid
- GuidTable
;
1215 for (Index
= 0; Index
< mPeiExMapppingTableSize
; Index
++) {
1216 if ((ExTokenNumber
== ExMap
[Index
].ExTokenNumber
) &&
1217 (MatchGuidIdx
== ExMap
[Index
].ExGuidIndex
)) {
1218 return ExMap
[Index
].TokenNumber
;
1224 ExMap
= (DYNAMICEX_MAPPING
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->ExMapTableOffset
);
1225 GuidTable
= (EFI_GUID
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->GuidTableOffset
);
1227 MatchGuid
= ScanGuid (GuidTable
, mDxeGuidTableSize
, Guid
);
1229 // We need to ASSERT here. If GUID can't be found in GuidTable, this is a
1230 // error in the BUILD system.
1232 ASSERT (MatchGuid
!= NULL
);
1234 MatchGuidIdx
= MatchGuid
- GuidTable
;
1236 for (Index
= 0; Index
< mDxeExMapppingTableSize
; Index
++) {
1237 if ((ExTokenNumber
== ExMap
[Index
].ExTokenNumber
) &&
1238 (MatchGuidIdx
== ExMap
[Index
].ExGuidIndex
)) {
1239 return ExMap
[Index
].TokenNumber
;
1249 Get SKU ID table from PCD database.
1251 @param LocalTokenNumberTableIdx Index of local token number in token number table.
1252 @param IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
1253 If FALSE, the pcd entry is initialized in DXE phase.
1254 @return Pointer to SKU ID array table
1259 IN UINTN LocalTokenNumberTableIdx
,
1264 UINTN LocalTokenNumber
;
1268 LocalTokenNumber
= *((UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
) + LocalTokenNumberTableIdx
);
1269 Database
= (UINT8
*) mPcdDatabase
.PeiDb
;
1271 LocalTokenNumber
= *((UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
) + LocalTokenNumberTableIdx
);
1272 Database
= (UINT8
*) mPcdDatabase
.DxeDb
;
1275 ASSERT ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) != 0);
1277 SkuHead
= (SKU_HEAD
*) ((UINT8
*)Database
+ (LocalTokenNumber
& PCD_DATABASE_OFFSET_MASK
));
1279 return (SKU_ID
*) (Database
+ SkuHead
->SkuIdTableOffset
);
1284 Wrapper function of getting index of PCD entry in size table.
1286 @param LocalTokenNumberTableIdx Index of this PCD in local token number table.
1287 @param IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
1288 If FALSE, the pcd entry is initialized in DXE phase.
1290 @return index of PCD entry in size table.
1294 IN UINTN LocalTokenNumberTableIdx
,
1298 UINT32
*LocalTokenNumberTable
;
1299 UINTN LocalTokenNumber
;
1305 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
);
1307 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
);
1312 for (Index
= 0; Index
< LocalTokenNumberTableIdx
; Index
++) {
1313 LocalTokenNumber
= LocalTokenNumberTable
[Index
];
1315 if ((LocalTokenNumber
& PCD_DATUM_TYPE_ALL_SET
) == PCD_DATUM_TYPE_POINTER
) {
1317 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1320 if ((LocalTokenNumber
& PCD_TYPE_VPD
) != 0) {
1322 // We have only two entry for VPD enabled PCD entry:
1325 // Current size is equal to MAX size.
1329 if ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) == 0) {
1331 // We have only two entry for Non-Sku enabled PCD entry:
1338 // We have these entry for SKU enabled PCD entry
1340 // 2) Current Size for each SKU_ID (It is equal to MaxSku).
1342 SkuIdTable
= GetSkuIdArray (Index
, IsPeiDb
);
1343 SizeTableIdx
+= (UINTN
)*SkuIdTable
+ 1;
1350 return SizeTableIdx
;
1354 Get size of POINTER type PCD value.
1356 @param LocalTokenNumberTableIdx Index of local token number in local token number table.
1357 @param MaxSize Maxmium size of POINTER type PCD value.
1359 @return size of POINTER type PCD value.
1364 IN UINTN LocalTokenNumberTableIdx
,
1369 UINTN LocalTokenNumber
;
1371 SIZE_INFO
*SizeTable
;
1374 UINT32
*LocalTokenNumberTable
;
1376 // EBC compiler is very choosy. It may report warning about comparison
1377 // between UINTN and 0 . So we add 1 in each size of the
1379 IsPeiDb
= (BOOLEAN
) (LocalTokenNumberTableIdx
+ 1 < mPeiLocalTokenCount
+ 1);
1383 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
);
1384 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->SizeTableOffset
);
1386 LocalTokenNumberTableIdx
-= mPeiLocalTokenCount
;
1387 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
);
1388 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->SizeTableOffset
);
1391 LocalTokenNumber
= LocalTokenNumberTable
[LocalTokenNumberTableIdx
];
1393 ASSERT ((LocalTokenNumber
& PCD_DATUM_TYPE_ALL_SET
) == PCD_DATUM_TYPE_POINTER
);
1395 SizeTableIdx
= GetSizeTableIndex (LocalTokenNumberTableIdx
, IsPeiDb
);
1397 *MaxSize
= SizeTable
[SizeTableIdx
];
1399 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1402 if ((LocalTokenNumber
& PCD_TYPE_VPD
) != 0) {
1404 // We have only two entry for VPD enabled PCD entry:
1407 // We consider current size is equal to MAX size.
1411 if ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) == 0) {
1413 // We have only two entry for Non-Sku enabled PCD entry:
1417 return SizeTable
[SizeTableIdx
+ 1];
1420 // We have these entry for SKU enabled PCD entry
1422 // 2) Current Size for each SKU_ID (It is equal to MaxSku).
1424 SkuIdTable
= GetSkuIdArray (LocalTokenNumberTableIdx
, IsPeiDb
);
1425 for (Index
= 0; Index
< SkuIdTable
[0]; Index
++) {
1426 if (SkuIdTable
[1 + Index
] == mPcdDatabase
.PeiDb
->SystemSkuId
) {
1427 return SizeTable
[SizeTableIdx
+ 1 + Index
];
1430 return SizeTable
[SizeTableIdx
+ 1];
1436 Set size of POINTER type PCD value. The size should not exceed the maximum size
1439 @param LocalTokenNumberTableIdx Index of local token number in local token number table.
1440 @param CurrentSize Size of POINTER type PCD value.
1442 @retval TRUE Success to set size of PCD value.
1443 @retval FALSE Fail to set size of PCD value.
1447 IN UINTN LocalTokenNumberTableIdx
,
1448 IN OUT UINTN
*CurrentSize
1452 UINTN LocalTokenNumber
;
1454 SIZE_INFO
*SizeTable
;
1458 UINT32
*LocalTokenNumberTable
;
1461 // EBC compiler is very choosy. It may report warning about comparison
1462 // between UINTN and 0 . So we add 1 in each size of the
1465 IsPeiDb
= (BOOLEAN
) (LocalTokenNumberTableIdx
+ 1 < mPeiLocalTokenCount
+ 1);
1468 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->LocalTokenNumberTableOffset
);
1469 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.PeiDb
+ mPcdDatabase
.PeiDb
->SizeTableOffset
);
1471 LocalTokenNumberTableIdx
-= mPeiLocalTokenCount
;
1472 LocalTokenNumberTable
= (UINT32
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->LocalTokenNumberTableOffset
);
1473 SizeTable
= (SIZE_INFO
*)((UINT8
*)mPcdDatabase
.DxeDb
+ mPcdDatabase
.DxeDb
->SizeTableOffset
);
1476 LocalTokenNumber
= LocalTokenNumberTable
[LocalTokenNumberTableIdx
];
1478 ASSERT ((LocalTokenNumber
& PCD_DATUM_TYPE_ALL_SET
) == PCD_DATUM_TYPE_POINTER
);
1480 SizeTableIdx
= GetSizeTableIndex (LocalTokenNumberTableIdx
, IsPeiDb
);
1482 MaxSize
= SizeTable
[SizeTableIdx
];
1484 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1487 if ((LocalTokenNumber
& PCD_TYPE_VPD
) != 0) {
1489 // We shouldn't come here as we don't support SET for VPD
1494 if ((*CurrentSize
> MaxSize
) ||
1495 (*CurrentSize
== MAX_ADDRESS
)) {
1496 *CurrentSize
= MaxSize
;
1500 if ((LocalTokenNumber
& PCD_TYPE_SKU_ENABLED
) == 0) {
1502 // We have only two entry for Non-Sku enabled PCD entry:
1506 SizeTable
[SizeTableIdx
+ 1] = (SIZE_INFO
) *CurrentSize
;
1510 // We have these entry for SKU enabled PCD entry
1512 // 2) Current Size for each SKU_ID (It is equal to MaxSku).
1514 SkuIdTable
= GetSkuIdArray (LocalTokenNumberTableIdx
, IsPeiDb
);
1515 for (Index
= 0; Index
< SkuIdTable
[0]; Index
++) {
1516 if (SkuIdTable
[1 + Index
] == mPcdDatabase
.PeiDb
->SystemSkuId
) {
1517 SizeTable
[SizeTableIdx
+ 1 + Index
] = (SIZE_INFO
) *CurrentSize
;
1521 SizeTable
[SizeTableIdx
+ 1] = (SIZE_INFO
) *CurrentSize
;