]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/PCD/Dxe/Service.c
MdeModulePkg Pcd(DXE): Use correct TokenNumber to call GetPtrTypeSize () when SKU...
[mirror_edk2.git] / MdeModulePkg / Universal / PCD / Dxe / Service.c
1 /** @file
2 Help functions used by PCD DXE driver.
3
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
9
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.
12
13 **/
14
15 #include "Service.h"
16 #include <Library/DxeServicesLib.h>
17
18 PCD_DATABASE mPcdDatabase;
19
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;
29
30 BOOLEAN mPeiExMapTableEmpty;
31 BOOLEAN mDxeExMapTableEmpty;
32 BOOLEAN mPeiDatabaseEmpty;
33
34 LIST_ENTRY *mCallbackFnTable;
35 EFI_GUID **TmpTokenSpaceBuffer;
36 UINTN TmpTokenSpaceBufferCount;
37
38 /**
39 Get Local Token Number by Token Number.
40
41 @param[in] IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
42 If FALSE, the pcd entry is initialized in DXE phase.
43 @param[in] TokenNumber The PCD token number.
44
45 @return Local Token Number.
46 **/
47 UINT32
48 GetLocalTokenNumber (
49 IN BOOLEAN IsPeiDb,
50 IN UINTN TokenNumber
51 )
52 {
53 UINTN TmpTokenNumber;
54 UINT32 *LocalTokenNumberTable;
55 UINT32 LocalTokenNumber;
56 UINTN Size;
57 UINTN MaxSize;
58
59 //
60 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
61 // We have to decrement TokenNumber by 1 to make it usable
62 // as the array index.
63 //
64 TokenNumber--;
65
66 //
67 // Backup the TokenNumber passed in as GetPtrTypeSize need the original TokenNumber
68 //
69 TmpTokenNumber = TokenNumber;
70
71 LocalTokenNumberTable = IsPeiDb ? (UINT32 *)((UINT8 *)mPcdDatabase.PeiDb + mPcdDatabase.PeiDb->LocalTokenNumberTableOffset) :
72 (UINT32 *)((UINT8 *)mPcdDatabase.DxeDb + mPcdDatabase.DxeDb->LocalTokenNumberTableOffset);
73 TokenNumber = IsPeiDb ? TokenNumber : TokenNumber - mPeiLocalTokenCount;
74
75 LocalTokenNumber = LocalTokenNumberTable[TokenNumber];
76
77 Size = (LocalTokenNumber & PCD_DATUM_TYPE_ALL_SET) >> PCD_DATUM_TYPE_SHIFT;
78
79 if ((LocalTokenNumber & PCD_TYPE_SKU_ENABLED) == PCD_TYPE_SKU_ENABLED) {
80 if (Size == 0) {
81 GetPtrTypeSize (TmpTokenNumber, &MaxSize);
82 } else {
83 MaxSize = Size;
84 }
85 LocalTokenNumber = GetSkuEnabledTokenNumber (LocalTokenNumber & ~PCD_TYPE_SKU_ENABLED, MaxSize, IsPeiDb);
86 }
87
88 return LocalTokenNumber;
89 }
90
91 /**
92 Get PCD type by Local Token Number.
93
94 @param[in] LocalTokenNumber The PCD local token number.
95
96 @return PCD type.
97 **/
98 EFI_PCD_TYPE
99 GetPcdType (
100 IN UINT32 LocalTokenNumber
101 )
102 {
103 switch (LocalTokenNumber & PCD_DATUM_TYPE_ALL_SET) {
104 case PCD_DATUM_TYPE_POINTER:
105 return EFI_PCD_TYPE_PTR;
106 case PCD_DATUM_TYPE_UINT8:
107 if ((LocalTokenNumber & PCD_DATUM_TYPE_UINT8_BOOLEAN) == PCD_DATUM_TYPE_UINT8_BOOLEAN) {
108 return EFI_PCD_TYPE_BOOL;
109 } else {
110 return EFI_PCD_TYPE_8;
111 }
112 case PCD_DATUM_TYPE_UINT16:
113 return EFI_PCD_TYPE_16;
114 case PCD_DATUM_TYPE_UINT32:
115 return EFI_PCD_TYPE_32;
116 case PCD_DATUM_TYPE_UINT64:
117 return EFI_PCD_TYPE_64;
118 default:
119 ASSERT (FALSE);
120 return EFI_PCD_TYPE_8;
121 }
122 }
123
124 /**
125 Get PCD name.
126
127 @param[in] OnlyTokenSpaceName If TRUE, only need to get the TokenSpaceCName.
128 If FALSE, need to get the full PCD name.
129 @param[in] IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
130 If FALSE, the pcd entry is initialized in DXE phase.
131 @param[in] TokenNumber The PCD token number.
132
133 @return The TokenSpaceCName or full PCD name.
134 **/
135 CHAR8 *
136 GetPcdName (
137 IN BOOLEAN OnlyTokenSpaceName,
138 IN BOOLEAN IsPeiDb,
139 IN UINTN TokenNumber
140 )
141 {
142 PCD_DATABASE_INIT *Database;
143 UINT8 *StringTable;
144 PCD_NAME_INDEX *PcdNameIndex;
145 CHAR8 *TokenSpaceName;
146 CHAR8 *PcdName;
147 CHAR8 *Name;
148
149 //
150 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
151 // We have to decrement TokenNumber by 1 to make it usable
152 // as the array index.
153 //
154 TokenNumber--;
155
156 Database = IsPeiDb ? mPcdDatabase.PeiDb: mPcdDatabase.DxeDb;
157 TokenNumber = IsPeiDb ? TokenNumber : TokenNumber - mPeiLocalTokenCount;
158
159 StringTable = (UINT8 *) Database + Database->StringTableOffset;
160
161 //
162 // Get the PCD name index.
163 //
164 PcdNameIndex = (PCD_NAME_INDEX *)((UINT8 *) Database + Database->PcdNameTableOffset) + TokenNumber;
165 TokenSpaceName = (CHAR8 *)&StringTable[PcdNameIndex->TokenSpaceCNameIndex];
166 PcdName = (CHAR8 *)&StringTable[PcdNameIndex->PcdCNameIndex];
167
168 if (OnlyTokenSpaceName) {
169 //
170 // Only need to get the TokenSpaceCName.
171 //
172 Name = AllocateCopyPool (AsciiStrSize (TokenSpaceName), TokenSpaceName);
173 } else {
174 //
175 // Need to get the full PCD name.
176 //
177 Name = AllocateZeroPool (AsciiStrSize (TokenSpaceName) + AsciiStrSize (PcdName));
178 ASSERT (Name != NULL);
179 //
180 // Catenate TokenSpaceCName and PcdCName with a '.' to form the full PCD name.
181 //
182 AsciiStrCat (Name, TokenSpaceName);
183 Name[AsciiStrSize (TokenSpaceName) - sizeof (CHAR8)] = '.';
184 AsciiStrCat (Name, PcdName);
185 }
186
187 return Name;
188 }
189
190 /**
191 Retrieve additional information associated with a PCD token.
192
193 This includes information such as the type of value the TokenNumber is associated with as well as possible
194 human readable name that is associated with the token.
195
196 @param[in] IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
197 If FALSE, the pcd entry is initialized in DXE phase.
198 @param[in] Guid The 128-bit unique value that designates the namespace from which to extract the value.
199 @param[in] TokenNumber The PCD token number.
200 @param[out] PcdInfo The returned information associated with the requested TokenNumber.
201 The caller is responsible for freeing the buffer that is allocated by callee for PcdInfo->PcdName.
202
203 @retval EFI_SUCCESS The PCD information was returned successfully
204 @retval EFI_NOT_FOUND The PCD service could not find the requested token number.
205 **/
206 EFI_STATUS
207 ExGetPcdInfo (
208 IN BOOLEAN IsPeiDb,
209 IN CONST EFI_GUID *Guid,
210 IN UINTN TokenNumber,
211 OUT EFI_PCD_INFO *PcdInfo
212 )
213 {
214 PCD_DATABASE_INIT *Database;
215 UINTN GuidTableIdx;
216 EFI_GUID *MatchGuid;
217 EFI_GUID *GuidTable;
218 DYNAMICEX_MAPPING *ExMapTable;
219 UINTN Index;
220 UINT32 LocalTokenNumber;
221
222 Database = IsPeiDb ? mPcdDatabase.PeiDb: mPcdDatabase.DxeDb;
223
224 GuidTable = (EFI_GUID *)((UINT8 *)Database + Database->GuidTableOffset);
225 MatchGuid = ScanGuid (GuidTable, Database->GuidTableCount * sizeof(EFI_GUID), Guid);
226
227 if (MatchGuid == NULL) {
228 return EFI_NOT_FOUND;
229 }
230
231 GuidTableIdx = MatchGuid - GuidTable;
232
233 ExMapTable = (DYNAMICEX_MAPPING *)((UINT8 *)Database + Database->ExMapTableOffset);
234
235 //
236 // Find the PCD by GuidTableIdx and ExTokenNumber in ExMapTable.
237 //
238 for (Index = 0; Index < Database->ExTokenCount; Index++) {
239 if (ExMapTable[Index].ExGuidIndex == GuidTableIdx) {
240 if (TokenNumber == PCD_INVALID_TOKEN_NUMBER) {
241 //
242 // TokenNumber is 0, follow spec to set PcdType to EFI_PCD_TYPE_8,
243 // PcdSize to 0 and PcdName to the null-terminated ASCII string
244 // associated with the token's namespace Guid.
245 //
246 PcdInfo->PcdType = EFI_PCD_TYPE_8;
247 PcdInfo->PcdSize = 0;
248 //
249 // Here use one representative in the token space to get the TokenSpaceCName.
250 //
251 PcdInfo->PcdName = GetPcdName (TRUE, IsPeiDb, ExMapTable[Index].TokenNumber);
252 return EFI_SUCCESS;
253 } else if (ExMapTable[Index].ExTokenNumber == TokenNumber) {
254 PcdInfo->PcdSize = DxePcdGetSize (ExMapTable[Index].TokenNumber);
255 LocalTokenNumber = GetLocalTokenNumber (IsPeiDb, ExMapTable[Index].TokenNumber);
256 PcdInfo->PcdType = GetPcdType (LocalTokenNumber);
257 PcdInfo->PcdName = GetPcdName (FALSE, IsPeiDb, ExMapTable[Index].TokenNumber);
258 return EFI_SUCCESS;
259 }
260 }
261 }
262
263 return EFI_NOT_FOUND;
264 }
265
266 /**
267 Retrieve additional information associated with a PCD token.
268
269 This includes information such as the type of value the TokenNumber is associated with as well as possible
270 human readable name that is associated with the token.
271
272 @param[in] Guid The 128-bit unique value that designates the namespace from which to extract the value.
273 @param[in] TokenNumber The PCD token number.
274 @param[out] PcdInfo The returned information associated with the requested TokenNumber.
275 The caller is responsible for freeing the buffer that is allocated by callee for PcdInfo->PcdName.
276
277 @retval EFI_SUCCESS The PCD information was returned successfully.
278 @retval EFI_NOT_FOUND The PCD service could not find the requested token number.
279 **/
280 EFI_STATUS
281 DxeGetPcdInfo (
282 IN CONST EFI_GUID *Guid,
283 IN UINTN TokenNumber,
284 OUT EFI_PCD_INFO *PcdInfo
285 )
286 {
287 EFI_STATUS Status;
288 BOOLEAN PeiExMapTableEmpty;
289 BOOLEAN DxeExMapTableEmpty;
290 UINT32 LocalTokenNumber;
291 BOOLEAN IsPeiDb;
292
293 ASSERT (PcdInfo != NULL);
294
295 Status = EFI_NOT_FOUND;
296 PeiExMapTableEmpty = mPeiExMapTableEmpty;
297 DxeExMapTableEmpty = mDxeExMapTableEmpty;
298
299 if (Guid == NULL) {
300 if (((TokenNumber + 1 > mPeiNexTokenCount + 1) && (TokenNumber + 1 <= mPeiLocalTokenCount + 1)) ||
301 ((TokenNumber + 1 > (mPeiLocalTokenCount + mDxeNexTokenCount + 1)))) {
302 return EFI_NOT_FOUND;
303 } else if (TokenNumber == PCD_INVALID_TOKEN_NUMBER) {
304 //
305 // TokenNumber is 0, follow spec to set PcdType to EFI_PCD_TYPE_8,
306 // PcdSize to 0 and PcdName to NULL for default Token Space.
307 //
308 PcdInfo->PcdType = EFI_PCD_TYPE_8;
309 PcdInfo->PcdSize = 0;
310 PcdInfo->PcdName = NULL;
311 } else {
312 PcdInfo->PcdSize = DxePcdGetSize (TokenNumber);
313 IsPeiDb = FALSE;
314 if ((TokenNumber + 1 <= mPeiNexTokenCount + 1)) {
315 IsPeiDb = TRUE;
316 }
317 LocalTokenNumber = GetLocalTokenNumber (IsPeiDb, TokenNumber);
318 PcdInfo->PcdType = GetPcdType (LocalTokenNumber);
319 PcdInfo->PcdName = GetPcdName (FALSE, IsPeiDb, TokenNumber);
320 }
321 return EFI_SUCCESS;
322 }
323
324 if (PeiExMapTableEmpty && DxeExMapTableEmpty) {
325 return EFI_NOT_FOUND;
326 }
327
328 if (!PeiExMapTableEmpty) {
329 Status = ExGetPcdInfo (
330 TRUE,
331 Guid,
332 TokenNumber,
333 PcdInfo
334 );
335 }
336
337 if (Status == EFI_SUCCESS) {
338 return Status;
339 }
340
341 if (!DxeExMapTableEmpty) {
342 Status = ExGetPcdInfo (
343 FALSE,
344 Guid,
345 TokenNumber,
346 PcdInfo
347 );
348 }
349
350 return Status;
351 }
352
353 /**
354 Get the PCD entry pointer in PCD database.
355
356 This routine will visit PCD database to find the PCD entry according to given
357 token number. The given token number is autogened by build tools and it will be
358 translated to local token number. Local token number contains PCD's type and
359 offset of PCD entry in PCD database.
360
361 @param TokenNumber Token's number, it is autogened by build tools
362 @param GetSize The size of token's value
363
364 @return PCD entry pointer in PCD database
365
366 **/
367 VOID *
368 GetWorker (
369 IN UINTN TokenNumber,
370 IN UINTN GetSize
371 )
372 {
373 EFI_GUID *GuidTable;
374 UINT8 *StringTable;
375 EFI_GUID *Guid;
376 UINT16 *Name;
377 VARIABLE_HEAD *VariableHead;
378 UINT8 *VaraiableDefaultBuffer;
379 UINT8 *Data;
380 VPD_HEAD *VpdHead;
381 UINT8 *PcdDb;
382 VOID *RetPtr;
383 UINTN TmpTokenNumber;
384 UINTN DataSize;
385 EFI_STATUS Status;
386 UINT32 LocalTokenNumber;
387 UINT32 Offset;
388 STRING_HEAD StringTableIdx;
389 BOOLEAN IsPeiDb;
390
391 //
392 // Aquire lock to prevent reentrance from TPL_CALLBACK level
393 //
394 EfiAcquireLock (&mPcdDatabaseLock);
395
396 RetPtr = NULL;
397
398 ASSERT (TokenNumber > 0);
399 //
400 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
401 // We have to decrement TokenNumber by 1 to make it usable
402 // as the array index.
403 //
404 TokenNumber--;
405
406 TmpTokenNumber = TokenNumber;
407
408 //
409 // EBC compiler is very choosy. It may report warning about comparison
410 // between UINTN and 0 . So we add 1 in each size of the
411 // comparison.
412 //
413 ASSERT (TokenNumber + 1 < mPcdTotalTokenCount + 1);
414
415 ASSERT ((GetSize == DxePcdGetSize (TokenNumber + 1)) || (GetSize == 0));
416
417 // EBC compiler is very choosy. It may report warning about comparison
418 // between UINTN and 0 . So we add 1 in each size of the
419 // comparison.
420 IsPeiDb = (BOOLEAN) ((TokenNumber + 1 < mPeiLocalTokenCount + 1) ? TRUE : FALSE);
421
422 LocalTokenNumber = GetLocalTokenNumber (IsPeiDb, TokenNumber + 1);
423
424 PcdDb = IsPeiDb ? ((UINT8 *) mPcdDatabase.PeiDb) : ((UINT8 *) mPcdDatabase.DxeDb);
425
426 if (IsPeiDb) {
427 StringTable = (UINT8 *) ((UINT8 *)mPcdDatabase.PeiDb + mPcdDatabase.PeiDb->StringTableOffset);
428 } else {
429 StringTable = (UINT8 *) ((UINT8 *)mPcdDatabase.DxeDb + mPcdDatabase.DxeDb->StringTableOffset);
430 }
431
432
433 Offset = LocalTokenNumber & PCD_DATABASE_OFFSET_MASK;
434
435 switch (LocalTokenNumber & PCD_TYPE_ALL_SET) {
436 case PCD_TYPE_VPD:
437 VpdHead = (VPD_HEAD *) ((UINT8 *) PcdDb + Offset);
438 RetPtr = (VOID *) (UINTN) (PcdGet32 (PcdVpdBaseAddress) + VpdHead->Offset);
439
440 break;
441
442 case PCD_TYPE_HII|PCD_TYPE_STRING:
443 case PCD_TYPE_HII:
444 if (IsPeiDb) {
445 GuidTable = (EFI_GUID *) ((UINT8 *)mPcdDatabase.PeiDb + mPcdDatabase.PeiDb->GuidTableOffset);
446 } else {
447 GuidTable = (EFI_GUID *) ((UINT8 *)mPcdDatabase.DxeDb + mPcdDatabase.DxeDb->GuidTableOffset);
448 }
449
450 VariableHead = (VARIABLE_HEAD *) (PcdDb + Offset);
451 Guid = GuidTable + VariableHead->GuidTableIndex;
452 Name = (UINT16*)(StringTable + VariableHead->StringIndex);
453
454 if ((LocalTokenNumber & PCD_TYPE_ALL_SET) == (PCD_TYPE_HII|PCD_TYPE_STRING)) {
455 //
456 // If a HII type PCD's datum type is VOID*, the DefaultValueOffset is the index of
457 // string array in string table.
458 //
459 StringTableIdx = *(STRING_HEAD*)((UINT8 *) PcdDb + VariableHead->DefaultValueOffset);
460 VaraiableDefaultBuffer = (VOID *) (StringTable + StringTableIdx);
461 } else {
462 VaraiableDefaultBuffer = (UINT8 *) PcdDb + VariableHead->DefaultValueOffset;
463 }
464 Status = GetHiiVariable (Guid, Name, &Data, &DataSize);
465 if (Status == EFI_SUCCESS) {
466 if (GetSize == 0) {
467 //
468 // It is a pointer type. So get the MaxSize reserved for
469 // this PCD entry.
470 //
471 GetPtrTypeSize (TmpTokenNumber, &GetSize);
472 }
473 //
474 // If the operation is successful, we copy the data
475 // to the default value buffer in the PCD Database.
476 // So that we can free the Data allocated in GetHiiVariable.
477 //
478 CopyMem (VaraiableDefaultBuffer, Data + VariableHead->Offset, GetSize);
479 FreePool (Data);
480 }
481 RetPtr = (VOID *) VaraiableDefaultBuffer;
482 break;
483
484 case PCD_TYPE_STRING:
485 StringTableIdx = *(STRING_HEAD*)((UINT8 *) PcdDb + Offset);
486 RetPtr = (VOID *) (StringTable + StringTableIdx);
487 break;
488
489 case PCD_TYPE_DATA:
490 RetPtr = (VOID *) ((UINT8 *) PcdDb + Offset);
491 break;
492
493 default:
494 ASSERT (FALSE);
495 break;
496
497 }
498
499 EfiReleaseLock (&mPcdDatabaseLock);
500
501 return RetPtr;
502
503 }
504
505 /**
506 Register the callback function for a PCD entry.
507
508 This routine will register a callback function to a PCD entry by given token number
509 and token space guid.
510
511 @param TokenNumber PCD token's number, it is autogened by build tools.
512 @param Guid PCD token space's guid,
513 if not NULL, this PCD is dynamicEx type PCD.
514 @param CallBackFunction Callback function pointer
515
516 @return EFI_SUCCESS Always success for registering callback function.
517
518 **/
519 EFI_STATUS
520 DxeRegisterCallBackWorker (
521 IN UINTN TokenNumber,
522 IN CONST EFI_GUID *Guid, OPTIONAL
523 IN PCD_PROTOCOL_CALLBACK CallBackFunction
524 )
525 {
526 CALLBACK_FN_ENTRY *FnTableEntry;
527 LIST_ENTRY *ListHead;
528 LIST_ENTRY *ListNode;
529
530 if (Guid != NULL) {
531 TokenNumber = GetExPcdTokenNumber (Guid, (UINT32) TokenNumber);
532 }
533
534 //
535 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
536 // We have to decrement TokenNumber by 1 to make it usable
537 // as the array index of mCallbackFnTable[].
538 //
539 ListHead = &mCallbackFnTable[TokenNumber - 1];
540 ListNode = GetFirstNode (ListHead);
541
542 while (ListNode != ListHead) {
543 FnTableEntry = CR_FNENTRY_FROM_LISTNODE(ListNode, CALLBACK_FN_ENTRY, Node);
544
545 if (FnTableEntry->CallbackFn == CallBackFunction) {
546 //
547 // We only allow a Callback function to be register once
548 // for a TokenNumber. So just return EFI_SUCCESS
549 //
550 return EFI_SUCCESS;
551 }
552 ListNode = GetNextNode (ListHead, ListNode);
553 }
554
555 FnTableEntry = AllocatePool (sizeof(CALLBACK_FN_ENTRY));
556 ASSERT (FnTableEntry != NULL);
557
558 FnTableEntry->CallbackFn = CallBackFunction;
559 InsertTailList (ListHead, &FnTableEntry->Node);
560
561 return EFI_SUCCESS;
562 }
563
564 /**
565 UnRegister the callback function for a PCD entry.
566
567 This routine will unregister a callback function to a PCD entry by given token number
568 and token space guid.
569
570 @param TokenNumber PCD token's number, it is autogened by build tools.
571 @param Guid PCD token space's guid.
572 if not NULL, this PCD is dynamicEx type PCD.
573 @param CallBackFunction Callback function pointer
574
575 @retval EFI_SUCCESS Callback function is success to be unregister.
576 @retval EFI_INVALID_PARAMETER Can not find the PCD entry by given token number.
577 **/
578 EFI_STATUS
579 DxeUnRegisterCallBackWorker (
580 IN UINTN TokenNumber,
581 IN CONST EFI_GUID *Guid, OPTIONAL
582 IN PCD_PROTOCOL_CALLBACK CallBackFunction
583 )
584 {
585 CALLBACK_FN_ENTRY *FnTableEntry;
586 LIST_ENTRY *ListHead;
587 LIST_ENTRY *ListNode;
588
589 if (Guid != NULL) {
590 TokenNumber = GetExPcdTokenNumber (Guid, (UINT32) TokenNumber);
591 }
592
593 //
594 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
595 // We have to decrement TokenNumber by 1 to make it usable
596 // as the array index of mCallbackFnTable[].
597 //
598 ListHead = &mCallbackFnTable[TokenNumber - 1];
599 ListNode = GetFirstNode (ListHead);
600
601 while (ListNode != ListHead) {
602 FnTableEntry = CR_FNENTRY_FROM_LISTNODE(ListNode, CALLBACK_FN_ENTRY, Node);
603
604 if (FnTableEntry->CallbackFn == CallBackFunction) {
605 //
606 // We only allow a Callback function to be register once
607 // for a TokenNumber. So we can safely remove the Node from
608 // the Link List and return EFI_SUCCESS.
609 //
610 RemoveEntryList (ListNode);
611 FreePool (FnTableEntry);
612
613 return EFI_SUCCESS;
614 }
615 ListNode = GetNextNode (ListHead, ListNode);
616 }
617
618 return EFI_INVALID_PARAMETER;
619 }
620
621 /**
622 Get next token number in given token space.
623
624 This routine is used for dynamicEx type PCD. It will firstly scan token space
625 table to get token space according to given token space guid. Then scan given
626 token number in found token space, if found, then return next token number in
627 this token space.
628
629 @param Guid Token space guid. Next token number will be scaned in
630 this token space.
631 @param TokenNumber Token number.
632 If PCD_INVALID_TOKEN_NUMBER, return first token number in
633 token space table.
634 If not PCD_INVALID_TOKEN_NUMBER, return next token number
635 in token space table.
636 @param GuidTable Token space guid table. It will be used for scan token space
637 by given token space guid.
638 @param SizeOfGuidTable The size of guid table.
639 @param ExMapTable DynamicEx token number mapping table.
640 @param SizeOfExMapTable The size of dynamicEx token number mapping table.
641
642 @retval EFI_NOT_FOUND Can not given token space or token number.
643 @retval EFI_SUCCESS Success to get next token number.
644
645 **/
646 EFI_STATUS
647 ExGetNextTokeNumber (
648 IN CONST EFI_GUID *Guid,
649 IN OUT UINTN *TokenNumber,
650 IN EFI_GUID *GuidTable,
651 IN UINTN SizeOfGuidTable,
652 IN DYNAMICEX_MAPPING *ExMapTable,
653 IN UINTN SizeOfExMapTable
654 )
655 {
656 EFI_GUID *MatchGuid;
657 UINTN Index;
658 UINTN GuidTableIdx;
659 BOOLEAN Found;
660 UINTN ExMapTableCount;
661
662 //
663 // Scan token space guid
664 //
665 MatchGuid = ScanGuid (GuidTable, SizeOfGuidTable, Guid);
666 if (MatchGuid == NULL) {
667 return EFI_NOT_FOUND;
668 }
669
670 //
671 // Find the token space table in dynamicEx mapping table.
672 //
673 Found = FALSE;
674 GuidTableIdx = MatchGuid - GuidTable;
675 ExMapTableCount = SizeOfExMapTable / sizeof(ExMapTable[0]);
676 for (Index = 0; Index < ExMapTableCount; Index++) {
677 if (ExMapTable[Index].ExGuidIndex == GuidTableIdx) {
678 Found = TRUE;
679 break;
680 }
681 }
682
683 if (Found) {
684 //
685 // If given token number is PCD_INVALID_TOKEN_NUMBER, then return the first
686 // token number in found token space.
687 //
688 if (*TokenNumber == PCD_INVALID_TOKEN_NUMBER) {
689 *TokenNumber = ExMapTable[Index].ExTokenNumber;
690 return EFI_SUCCESS;
691 }
692
693 for ( ; Index < ExMapTableCount; Index++) {
694 if (ExMapTable[Index].ExTokenNumber == *TokenNumber) {
695 break;
696 }
697 }
698
699 while (Index < ExMapTableCount) {
700 Index++;
701 if (Index == ExMapTableCount) {
702 //
703 // Exceed the length of ExMap Table
704 //
705 *TokenNumber = PCD_INVALID_TOKEN_NUMBER;
706 return EFI_NOT_FOUND;
707 } else if (ExMapTable[Index].ExGuidIndex == GuidTableIdx) {
708 //
709 // Found the next match
710 //
711 *TokenNumber = ExMapTable[Index].ExTokenNumber;
712 return EFI_SUCCESS;
713 }
714 }
715 }
716
717 return EFI_NOT_FOUND;
718 }
719
720 /**
721 Find the PCD database.
722
723 @retval The base address of external PCD database binary.
724 @retval NULL Return NULL if not find.
725 **/
726 DXE_PCD_DATABASE *
727 LocateExPcdBinary (
728 VOID
729 )
730 {
731 DXE_PCD_DATABASE *DxePcdDbBinary;
732 UINTN DxePcdDbSize;
733 EFI_STATUS Status;
734
735 DxePcdDbBinary = NULL;
736 //
737 // Search the External Pcd database from one section of current FFS,
738 // and read it to memory
739 //
740 Status = GetSectionFromFfs (
741 EFI_SECTION_RAW,
742 0,
743 (VOID **) &DxePcdDbBinary,
744 &DxePcdDbSize
745 );
746 ASSERT_EFI_ERROR (Status);
747
748 //
749 // Check the first bytes (Header Signature Guid) and build version.
750 //
751 if (!CompareGuid ((VOID *)DxePcdDbBinary, &gPcdDataBaseSignatureGuid) ||
752 (DxePcdDbBinary->BuildVersion != PCD_SERVICE_DXE_VERSION)) {
753 ASSERT (FALSE);
754 }
755
756 return DxePcdDbBinary;
757 }
758
759 /**
760 Initialize the PCD database in DXE phase.
761
762 PCD database in DXE phase also contains PCD database in PEI phase which is copied
763 from GUID Hob.
764
765 **/
766 VOID
767 BuildPcdDxeDataBase (
768 VOID
769 )
770 {
771 PEI_PCD_DATABASE *PeiDatabase;
772 EFI_HOB_GUID_TYPE *GuidHob;
773 UINTN Index;
774 UINT32 PcdDxeDbLen;
775 VOID *PcdDxeDb;
776
777 GuidHob = GetFirstGuidHob (&gPcdDataBaseHobGuid);
778 if (GuidHob != NULL) {
779
780 //
781 // If no PEIMs use dynamic Pcd Entry, the Pcd Service PEIM
782 // should not be included at all. So the GuidHob could
783 // be NULL. If it is NULL, we just copy over the DXE Default
784 // Value to PCD Database.
785 //
786
787 PeiDatabase = (PEI_PCD_DATABASE *) GET_GUID_HOB_DATA (GuidHob);
788 //
789 // Assign PCD Entries refereneced in PEI phase to PCD DATABASE
790 //
791 mPcdDatabase.PeiDb = PeiDatabase;
792 }
793
794 //
795 // Assign PCD Entries with default value to PCD DATABASE
796 //
797 mPcdDatabase.DxeDb = LocateExPcdBinary ();
798 ASSERT(mPcdDatabase.DxeDb != NULL);
799 PcdDxeDbLen = mPcdDatabase.DxeDb->Length + mPcdDatabase.DxeDb->UninitDataBaseSize;
800 PcdDxeDb = AllocateZeroPool (PcdDxeDbLen);
801 ASSERT (PcdDxeDb != NULL);
802 CopyMem (PcdDxeDb, mPcdDatabase.DxeDb, mPcdDatabase.DxeDb->Length);
803 FreePool (mPcdDatabase.DxeDb);
804 mPcdDatabase.DxeDb = PcdDxeDb;
805
806 //
807 // Initialized the external PCD database local variables
808 //
809 mPeiLocalTokenCount = mPcdDatabase.PeiDb->LocalTokenCount;
810 mDxeLocalTokenCount = mPcdDatabase.DxeDb->LocalTokenCount;
811
812 mPeiExMapppingTableSize = mPcdDatabase.PeiDb->ExTokenCount * sizeof (DYNAMICEX_MAPPING);
813 mDxeExMapppingTableSize = mPcdDatabase.DxeDb->ExTokenCount * sizeof (DYNAMICEX_MAPPING);
814 mPeiGuidTableSize = mPcdDatabase.PeiDb->GuidTableCount * sizeof(GUID);
815 mDxeGuidTableSize = mPcdDatabase.DxeDb->GuidTableCount * sizeof (GUID);
816
817 mPcdTotalTokenCount = mPeiLocalTokenCount + mDxeLocalTokenCount;
818 mPeiNexTokenCount = mPeiLocalTokenCount - mPcdDatabase.PeiDb->ExTokenCount;
819 mDxeNexTokenCount = mDxeLocalTokenCount - mPcdDatabase.DxeDb->ExTokenCount;
820
821 mPeiExMapTableEmpty = (mPcdDatabase.PeiDb->ExTokenCount == 0) ? TRUE : FALSE;
822 mDxeExMapTableEmpty = (mPcdDatabase.DxeDb->ExTokenCount == 0) ? TRUE : FALSE;
823 mPeiDatabaseEmpty = (mPeiLocalTokenCount == 0) ? TRUE : FALSE;
824
825 TmpTokenSpaceBufferCount = mPcdDatabase.PeiDb->ExTokenCount + mPcdDatabase.DxeDb->ExTokenCount;
826 TmpTokenSpaceBuffer = (EFI_GUID **)AllocateZeroPool(TmpTokenSpaceBufferCount * sizeof (EFI_GUID *));
827
828 //
829 // Initialized the Callback Function Table
830 //
831 mCallbackFnTable = AllocateZeroPool (mPcdTotalTokenCount * sizeof (LIST_ENTRY));
832 ASSERT(mCallbackFnTable != NULL);
833
834 //
835 // EBC compiler is very choosy. It may report warning about comparison
836 // between UINTN and 0 . So we add 1 in each size of the
837 // comparison.
838 //
839 for (Index = 0; Index + 1 < mPcdTotalTokenCount + 1; Index++) {
840 InitializeListHead (&mCallbackFnTable[Index]);
841 }
842 }
843
844 /**
845 Get Variable which contains HII type PCD entry.
846
847 @param VariableGuid Variable's guid
848 @param VariableName Variable's unicode name string
849 @param VariableData Variable's data pointer,
850 @param VariableSize Variable's size.
851
852 @return the status of gRT->GetVariable
853 **/
854 EFI_STATUS
855 GetHiiVariable (
856 IN EFI_GUID *VariableGuid,
857 IN UINT16 *VariableName,
858 OUT UINT8 **VariableData,
859 OUT UINTN *VariableSize
860 )
861 {
862 UINTN Size;
863 EFI_STATUS Status;
864 UINT8 *Buffer;
865
866 Size = 0;
867 Buffer = NULL;
868
869 //
870 // Firstly get the real size of HII variable
871 //
872 Status = gRT->GetVariable (
873 (UINT16 *)VariableName,
874 VariableGuid,
875 NULL,
876 &Size,
877 Buffer
878 );
879
880 //
881 // Allocate buffer to hold whole variable data according to variable size.
882 //
883 if (Status == EFI_BUFFER_TOO_SMALL) {
884 Buffer = (UINT8 *) AllocatePool (Size);
885
886 ASSERT (Buffer != NULL);
887
888 Status = gRT->GetVariable (
889 VariableName,
890 VariableGuid,
891 NULL,
892 &Size,
893 Buffer
894 );
895
896 ASSERT (Status == EFI_SUCCESS);
897 *VariableData = Buffer;
898 *VariableSize = Size;
899 } else {
900 //
901 // Use Default Data only when variable is not found.
902 // For other error status, correct data can't be got, and trig ASSERT().
903 //
904 ASSERT (Status == EFI_NOT_FOUND);
905 }
906
907 return Status;
908 }
909
910 /**
911 Find the local token number according to system SKU ID.
912
913 @param LocalTokenNumber PCD token number
914 @param Size The size of PCD entry.
915 @param IsPeiDb If TRUE, the PCD entry is initialized in PEI phase.
916 If False, the PCD entry is initialized in DXE phase.
917
918 @return Token number according to system SKU ID.
919
920 **/
921 UINT32
922 GetSkuEnabledTokenNumber (
923 UINT32 LocalTokenNumber,
924 UINTN Size,
925 BOOLEAN IsPeiDb
926 )
927 {
928 SKU_HEAD *SkuHead;
929 SKU_ID *SkuIdTable;
930 INTN Index;
931 UINT8 *Value;
932 UINT8 *PcdDb;
933 BOOLEAN FoundSku;
934
935 ASSERT ((LocalTokenNumber & PCD_TYPE_SKU_ENABLED) == 0);
936
937 PcdDb = IsPeiDb ? (UINT8 *) mPcdDatabase.PeiDb : (UINT8 *) mPcdDatabase.DxeDb;
938
939 SkuHead = (SKU_HEAD *) (PcdDb + (LocalTokenNumber & PCD_DATABASE_OFFSET_MASK));
940 Value = (UINT8 *) (PcdDb + SkuHead->SkuDataStartOffset);
941
942 SkuIdTable = (SKU_ID *)(PcdDb + SkuHead->SkuIdTableOffset);
943 //
944 // Find the current system's SKU ID entry in SKU ID table.
945 //
946 FoundSku = FALSE;
947 for (Index = 0; Index < SkuIdTable[0]; Index++) {
948 if (mPcdDatabase.PeiDb->SystemSkuId == SkuIdTable[Index + 1]) {
949 FoundSku = TRUE;
950 break;
951 }
952 }
953
954 //
955 // Find the default SKU ID entry in SKU ID table.
956 //
957
958 if(!FoundSku) {
959 for (Index = 0; Index < SkuIdTable[0]; Index++) {
960 if (0 == SkuIdTable[Index + 1]) {
961 break;
962 }
963 }
964 }
965 ASSERT (Index < SkuIdTable[0]);
966
967 switch (LocalTokenNumber & PCD_TYPE_ALL_SET) {
968 case PCD_TYPE_VPD:
969 Value = (UINT8 *) &(((VPD_HEAD *) Value)[Index]);
970 return (UINT32) ((Value - PcdDb) | PCD_TYPE_VPD);
971
972 case PCD_TYPE_HII:
973 Value = (UINT8 *) &(((VARIABLE_HEAD *) Value)[Index]);
974 return (UINT32) ((Value - PcdDb) | PCD_TYPE_HII);
975
976 case PCD_TYPE_HII|PCD_TYPE_STRING:
977 Value = (UINT8 *) &(((VARIABLE_HEAD *) Value)[Index]);
978 return (UINT32) ((Value - PcdDb) | PCD_TYPE_HII | PCD_TYPE_STRING);
979
980 case PCD_TYPE_STRING:
981 Value = (UINT8 *) &(((STRING_HEAD *) Value)[Index]);
982 return (UINT32) ((Value - PcdDb) | PCD_TYPE_STRING);
983
984 case PCD_TYPE_DATA:
985 Value += Size * Index;
986 return (UINT32) ((Value - PcdDb) | PCD_TYPE_DATA);
987
988 default:
989 ASSERT (FALSE);
990 }
991
992 ASSERT (FALSE);
993
994 return 0;
995
996 }
997
998 /**
999 Invoke the callback function when dynamic PCD entry was set, if this PCD entry
1000 has registered callback function.
1001
1002 @param ExTokenNumber DynamicEx PCD's token number, if this PCD entry is dyanmicEx
1003 type PCD.
1004 @param Guid DynamicEx PCD's guid, if this PCD entry is dynamicEx type
1005 PCD.
1006 @param TokenNumber PCD token number generated by build tools.
1007 @param Data Value want to be set for this PCD entry
1008 @param Size The size of value
1009
1010 **/
1011 VOID
1012 InvokeCallbackOnSet (
1013 UINT32 ExTokenNumber,
1014 CONST EFI_GUID *Guid, OPTIONAL
1015 UINTN TokenNumber,
1016 VOID *Data,
1017 UINTN Size
1018 )
1019 {
1020 CALLBACK_FN_ENTRY *FnTableEntry;
1021 LIST_ENTRY *ListHead;
1022 LIST_ENTRY *ListNode;
1023
1024 //
1025 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
1026 // We have to decrement TokenNumber by 1 to make it usable
1027 // as the array index of mCallbackFnTable[].
1028 //
1029 ListHead = &mCallbackFnTable[TokenNumber - 1];
1030 ListNode = GetFirstNode (ListHead);
1031
1032 while (ListNode != ListHead) {
1033 FnTableEntry = CR_FNENTRY_FROM_LISTNODE (ListNode, CALLBACK_FN_ENTRY, Node);
1034
1035 FnTableEntry->CallbackFn(Guid,
1036 (Guid == NULL) ? TokenNumber : ExTokenNumber,
1037 Data,
1038 Size);
1039
1040 ListNode = GetNextNode (ListHead, ListNode);
1041 }
1042
1043 return;
1044 }
1045
1046
1047 /**
1048 Wrapper function for setting non-pointer type value for a PCD entry.
1049
1050 @param TokenNumber Pcd token number autogenerated by build tools.
1051 @param Data Value want to be set for PCD entry
1052 @param Size Size of value.
1053
1054 @return status of SetWorker.
1055
1056 **/
1057 EFI_STATUS
1058 SetValueWorker (
1059 IN UINTN TokenNumber,
1060 IN VOID *Data,
1061 IN UINTN Size
1062 )
1063 {
1064 return SetWorker (TokenNumber, Data, &Size, FALSE);
1065 }
1066
1067
1068 /**
1069 Set value for an PCD entry
1070
1071 @param TokenNumber Pcd token number autogenerated by build tools.
1072 @param Data Value want to be set for PCD entry
1073 @param Size Size of value.
1074 @param PtrType If TRUE, the type of PCD entry's value is Pointer.
1075 If False, the type of PCD entry's value is not Pointer.
1076
1077 @retval EFI_INVALID_PARAMETER If this PCD type is VPD, VPD PCD can not be set.
1078 @retval EFI_INVALID_PARAMETER If Size can not be set to size table.
1079 @retval EFI_INVALID_PARAMETER If Size of non-Ptr type PCD does not match the size information in PCD database.
1080 @retval EFI_NOT_FOUND If value type of PCD entry is intergrate, but not in
1081 range of UINT8, UINT16, UINT32, UINT64
1082 @retval EFI_NOT_FOUND Can not find the PCD type according to token number.
1083 **/
1084 EFI_STATUS
1085 SetWorker (
1086 IN UINTN TokenNumber,
1087 IN VOID *Data,
1088 IN OUT UINTN *Size,
1089 IN BOOLEAN PtrType
1090 )
1091 {
1092 BOOLEAN IsPeiDb;
1093 UINT32 LocalTokenNumber;
1094 EFI_GUID *GuidTable;
1095 UINT8 *StringTable;
1096 EFI_GUID *Guid;
1097 UINT16 *Name;
1098 UINTN VariableOffset;
1099 VOID *InternalData;
1100 VARIABLE_HEAD *VariableHead;
1101 UINTN Offset;
1102 UINT8 *PcdDb;
1103 EFI_STATUS Status;
1104 UINTN MaxSize;
1105 UINTN TmpTokenNumber;
1106
1107 //
1108 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
1109 // We have to decrement TokenNumber by 1 to make it usable
1110 // as the array index.
1111 //
1112 TokenNumber--;
1113
1114 TmpTokenNumber = TokenNumber;
1115
1116 //
1117 // EBC compiler is very choosy. It may report warning about comparison
1118 // between UINTN and 0 . So we add 1 in each size of the
1119 // comparison.
1120 //
1121 ASSERT (TokenNumber + 1 < mPcdTotalTokenCount + 1);
1122
1123 if (PtrType) {
1124 //
1125 // Get MaxSize first, then check new size with max buffer size.
1126 //
1127 GetPtrTypeSize (TokenNumber, &MaxSize);
1128 if (*Size > MaxSize) {
1129 *Size = MaxSize;
1130 return EFI_INVALID_PARAMETER;
1131 }
1132 } else {
1133 if (*Size != DxePcdGetSize (TokenNumber + 1)) {
1134 return EFI_INVALID_PARAMETER;
1135 }
1136 }
1137
1138 //
1139 // EBC compiler is very choosy. It may report warning about comparison
1140 // between UINTN and 0 . So we add 1 in each size of the
1141 // comparison.
1142 //
1143 if ((TokenNumber + 1 < mPeiNexTokenCount + 1) ||
1144 (TokenNumber + 1 >= mPeiLocalTokenCount + 1 && TokenNumber + 1 < (mPeiLocalTokenCount + mDxeNexTokenCount + 1))) {
1145 InvokeCallbackOnSet (0, NULL, TokenNumber + 1, Data, *Size);
1146 }
1147
1148 //
1149 // Aquire lock to prevent reentrance from TPL_CALLBACK level
1150 //
1151 EfiAcquireLock (&mPcdDatabaseLock);
1152
1153 //
1154 // EBC compiler is very choosy. It may report warning about comparison
1155 // between UINTN and 0 . So we add 1 in each size of the
1156 // comparison.
1157 //
1158 IsPeiDb = (BOOLEAN) ((TokenNumber + 1 < mPeiLocalTokenCount + 1) ? TRUE : FALSE);
1159
1160 LocalTokenNumber = GetLocalTokenNumber (IsPeiDb, TokenNumber + 1);
1161
1162 Offset = LocalTokenNumber & PCD_DATABASE_OFFSET_MASK;
1163
1164 PcdDb = IsPeiDb ? ((UINT8 *) mPcdDatabase.PeiDb) : ((UINT8 *) mPcdDatabase.DxeDb);
1165
1166 if (IsPeiDb) {
1167 StringTable = (UINT8 *) ((UINT8 *)mPcdDatabase.PeiDb + mPcdDatabase.PeiDb->StringTableOffset);
1168 } else {
1169 StringTable = (UINT8 *) ((UINT8 *)mPcdDatabase.DxeDb + mPcdDatabase.DxeDb->StringTableOffset);
1170 }
1171
1172
1173 InternalData = PcdDb + Offset;
1174
1175 switch (LocalTokenNumber & PCD_TYPE_ALL_SET) {
1176 case PCD_TYPE_VPD:
1177 ASSERT (FALSE);
1178 Status = EFI_INVALID_PARAMETER;
1179 break;
1180
1181 case PCD_TYPE_STRING:
1182 if (SetPtrTypeSize (TmpTokenNumber, Size)) {
1183 CopyMem (StringTable + *((STRING_HEAD *)InternalData), Data, *Size);
1184 Status = EFI_SUCCESS;
1185 } else {
1186 Status = EFI_INVALID_PARAMETER;
1187 }
1188 break;
1189
1190 case PCD_TYPE_HII|PCD_TYPE_STRING:
1191 case PCD_TYPE_HII:
1192 if (PtrType) {
1193 if (!SetPtrTypeSize (TmpTokenNumber, Size)) {
1194 Status = EFI_INVALID_PARAMETER;
1195 break;
1196 }
1197 }
1198
1199 if (IsPeiDb) {
1200 GuidTable = (EFI_GUID *) ((UINT8 *)mPcdDatabase.PeiDb + mPcdDatabase.PeiDb->GuidTableOffset);
1201 } else {
1202 GuidTable = (EFI_GUID *) ((UINT8 *)mPcdDatabase.DxeDb + mPcdDatabase.DxeDb->GuidTableOffset);
1203 }
1204
1205 VariableHead = (VARIABLE_HEAD *) (PcdDb + Offset);
1206
1207 Guid = GuidTable + VariableHead->GuidTableIndex;
1208 Name = (UINT16*) (StringTable + VariableHead->StringIndex);
1209 VariableOffset = VariableHead->Offset;
1210 Status = SetHiiVariable (Guid, Name, Data, *Size, VariableOffset);
1211
1212 if (EFI_NOT_FOUND == Status) {
1213 if ((LocalTokenNumber & PCD_TYPE_ALL_SET) == (PCD_TYPE_HII|PCD_TYPE_STRING)) {
1214 CopyMem (
1215 StringTable + *(STRING_HEAD *)(PcdDb + VariableHead->DefaultValueOffset),
1216 Data,
1217 *Size
1218 );
1219 } else {
1220 CopyMem (PcdDb + VariableHead->DefaultValueOffset, Data, *Size);
1221 }
1222 Status = EFI_SUCCESS;
1223 }
1224 break;
1225
1226 case PCD_TYPE_DATA:
1227 if (PtrType) {
1228 if (SetPtrTypeSize (TmpTokenNumber, Size)) {
1229 CopyMem (InternalData, Data, *Size);
1230 Status = EFI_SUCCESS;
1231 } else {
1232 Status = EFI_INVALID_PARAMETER;
1233 }
1234 break;
1235 }
1236
1237 Status = EFI_SUCCESS;
1238 switch (*Size) {
1239 case sizeof(UINT8):
1240 *((UINT8 *) InternalData) = *((UINT8 *) Data);
1241 break;
1242
1243 case sizeof(UINT16):
1244 *((UINT16 *) InternalData) = *((UINT16 *) Data);
1245 break;
1246
1247 case sizeof(UINT32):
1248 *((UINT32 *) InternalData) = *((UINT32 *) Data);
1249 break;
1250
1251 case sizeof(UINT64):
1252 *((UINT64 *) InternalData) = *((UINT64 *) Data);
1253 break;
1254
1255 default:
1256 ASSERT (FALSE);
1257 Status = EFI_NOT_FOUND;
1258 break;
1259 }
1260 break;
1261
1262 default:
1263 ASSERT (FALSE);
1264 Status = EFI_NOT_FOUND;
1265 break;
1266 }
1267
1268 EfiReleaseLock (&mPcdDatabaseLock);
1269
1270 return Status;
1271 }
1272
1273 /**
1274 Wrapper function for get PCD value for dynamic-ex PCD.
1275
1276 @param Guid Token space guid for dynamic-ex PCD.
1277 @param ExTokenNumber Token number for dynamic-ex PCD.
1278 @param GetSize The size of dynamic-ex PCD value.
1279
1280 @return PCD entry in PCD database.
1281
1282 **/
1283 VOID *
1284 ExGetWorker (
1285 IN CONST EFI_GUID *Guid,
1286 IN UINTN ExTokenNumber,
1287 IN UINTN GetSize
1288 )
1289 {
1290 return GetWorker(GetExPcdTokenNumber (Guid, (UINT32) ExTokenNumber), GetSize);
1291 }
1292
1293 /**
1294 Wrapper function for set PCD value for non-Pointer type dynamic-ex PCD.
1295
1296 @param ExTokenNumber Token number for dynamic-ex PCD.
1297 @param Guid Token space guid for dynamic-ex PCD.
1298 @param Data Value want to be set.
1299 @param SetSize The size of value.
1300
1301 @return status of ExSetWorker().
1302
1303 **/
1304 EFI_STATUS
1305 ExSetValueWorker (
1306 IN UINTN ExTokenNumber,
1307 IN CONST EFI_GUID *Guid,
1308 IN VOID *Data,
1309 IN UINTN SetSize
1310 )
1311 {
1312 return ExSetWorker (ExTokenNumber, Guid, Data, &SetSize, FALSE);
1313 }
1314
1315 /**
1316 Set value for a dynamic-ex PCD entry.
1317
1318 This routine find the local token number according to dynamic-ex PCD's token
1319 space guid and token number firstly, and invoke callback function if this PCD
1320 entry registered callback function. Finally, invoken general SetWorker to set
1321 PCD value.
1322
1323 @param ExTokenNumber Dynamic-ex PCD token number.
1324 @param Guid Token space guid for dynamic-ex PCD.
1325 @param Data PCD value want to be set
1326 @param SetSize Size of value.
1327 @param PtrType If TRUE, this PCD entry is pointer type.
1328 If FALSE, this PCD entry is not pointer type.
1329
1330 @return status of SetWorker().
1331
1332 **/
1333 EFI_STATUS
1334 ExSetWorker (
1335 IN UINTN ExTokenNumber,
1336 IN CONST EFI_GUID *Guid,
1337 IN VOID *Data,
1338 IN OUT UINTN *SetSize,
1339 IN BOOLEAN PtrType
1340 )
1341 {
1342 UINTN TokenNumber;
1343
1344 TokenNumber = GetExPcdTokenNumber (Guid, (UINT32) ExTokenNumber);
1345
1346 InvokeCallbackOnSet ((UINT32) ExTokenNumber, Guid, TokenNumber, Data, *SetSize);
1347
1348 return SetWorker (TokenNumber, Data, SetSize, PtrType);
1349
1350 }
1351
1352 /**
1353 Set value for HII-type PCD.
1354
1355 A HII-type PCD's value is stored in a variable. Setting/Getting the value of
1356 HII-type PCD is to visit this variable.
1357
1358 @param VariableGuid Guid of variable which stored value of a HII-type PCD.
1359 @param VariableName Unicode name of variable which stored value of a HII-type PCD.
1360 @param Data Value want to be set.
1361 @param DataSize Size of value
1362 @param Offset Value offset of HII-type PCD in variable.
1363
1364 @return status of GetVariable()/SetVariable().
1365
1366 **/
1367 EFI_STATUS
1368 SetHiiVariable (
1369 IN EFI_GUID *VariableGuid,
1370 IN UINT16 *VariableName,
1371 IN CONST VOID *Data,
1372 IN UINTN DataSize,
1373 IN UINTN Offset
1374 )
1375 {
1376 UINTN Size;
1377 VOID *Buffer;
1378 EFI_STATUS Status;
1379 UINT32 Attribute;
1380 UINTN SetSize;
1381
1382 Size = 0;
1383 SetSize = 0;
1384
1385 //
1386 // Try to get original variable size information.
1387 //
1388 Status = gRT->GetVariable (
1389 (UINT16 *)VariableName,
1390 VariableGuid,
1391 NULL,
1392 &Size,
1393 NULL
1394 );
1395
1396 if (Status == EFI_BUFFER_TOO_SMALL) {
1397 //
1398 // Patch new PCD's value to offset in given HII variable.
1399 //
1400 if (Size >= (DataSize + Offset)) {
1401 SetSize = Size;
1402 } else {
1403 SetSize = DataSize + Offset;
1404 }
1405 Buffer = AllocatePool (SetSize);
1406 ASSERT (Buffer != NULL);
1407
1408 Status = gRT->GetVariable (
1409 VariableName,
1410 VariableGuid,
1411 &Attribute,
1412 &Size,
1413 Buffer
1414 );
1415
1416 ASSERT_EFI_ERROR (Status);
1417
1418 CopyMem ((UINT8 *)Buffer + Offset, Data, DataSize);
1419
1420 Status = gRT->SetVariable (
1421 VariableName,
1422 VariableGuid,
1423 Attribute,
1424 SetSize,
1425 Buffer
1426 );
1427
1428 FreePool (Buffer);
1429 return Status;
1430 } else if (Status == EFI_NOT_FOUND) {
1431 //
1432 // If variable does not exist, a new variable need to be created.
1433 //
1434
1435 Size = Offset + DataSize;
1436
1437 Buffer = AllocateZeroPool (Size);
1438 ASSERT (Buffer != NULL);
1439
1440 CopyMem ((UINT8 *)Buffer + Offset, Data, DataSize);
1441
1442 Status = gRT->SetVariable (
1443 VariableName,
1444 VariableGuid,
1445 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
1446 Size,
1447 Buffer
1448 );
1449
1450 FreePool (Buffer);
1451 return Status;
1452 }
1453
1454 //
1455 // If we drop to here, the value is failed to be written in to variable area
1456 // So, we will save the data in the PCD Database's volatile area.
1457 //
1458 return Status;
1459 }
1460
1461 /**
1462 Get Token Number according to dynamic-ex PCD's {token space guid:token number}
1463
1464 A dynamic-ex type PCD, developer must provide pair of token space guid: token number
1465 in DEC file. PCD database maintain a mapping table that translate pair of {token
1466 space guid: token number} to Token Number.
1467
1468 @param Guid Token space guid for dynamic-ex PCD entry.
1469 @param ExTokenNumber Dynamic-ex PCD token number.
1470
1471 @return Token Number for dynamic-ex PCD.
1472
1473 **/
1474 UINTN
1475 GetExPcdTokenNumber (
1476 IN CONST EFI_GUID *Guid,
1477 IN UINT32 ExTokenNumber
1478 )
1479 {
1480 UINT32 Index;
1481 DYNAMICEX_MAPPING *ExMap;
1482 EFI_GUID *GuidTable;
1483 EFI_GUID *MatchGuid;
1484 UINTN MatchGuidIdx;
1485
1486 if (!mPeiDatabaseEmpty) {
1487 ExMap = (DYNAMICEX_MAPPING *)((UINT8 *)mPcdDatabase.PeiDb + mPcdDatabase.PeiDb->ExMapTableOffset);
1488 GuidTable = (EFI_GUID *)((UINT8 *)mPcdDatabase.PeiDb + mPcdDatabase.PeiDb->GuidTableOffset);
1489
1490 MatchGuid = ScanGuid (GuidTable, mPeiGuidTableSize, Guid);
1491
1492 if (MatchGuid != NULL) {
1493
1494 MatchGuidIdx = MatchGuid - GuidTable;
1495
1496 for (Index = 0; Index < mPeiExMapppingTableSize; Index++) {
1497 if ((ExTokenNumber == ExMap[Index].ExTokenNumber) &&
1498 (MatchGuidIdx == ExMap[Index].ExGuidIndex)) {
1499 return ExMap[Index].TokenNumber;
1500 }
1501 }
1502 }
1503 }
1504
1505 ExMap = (DYNAMICEX_MAPPING *)((UINT8 *)mPcdDatabase.DxeDb + mPcdDatabase.DxeDb->ExMapTableOffset);
1506 GuidTable = (EFI_GUID *)((UINT8 *)mPcdDatabase.DxeDb + mPcdDatabase.DxeDb->GuidTableOffset);
1507
1508 MatchGuid = ScanGuid (GuidTable, mDxeGuidTableSize, Guid);
1509 //
1510 // We need to ASSERT here. If GUID can't be found in GuidTable, this is a
1511 // error in the BUILD system.
1512 //
1513 ASSERT (MatchGuid != NULL);
1514
1515 MatchGuidIdx = MatchGuid - GuidTable;
1516
1517 for (Index = 0; Index < mDxeExMapppingTableSize; Index++) {
1518 if ((ExTokenNumber == ExMap[Index].ExTokenNumber) &&
1519 (MatchGuidIdx == ExMap[Index].ExGuidIndex)) {
1520 return ExMap[Index].TokenNumber;
1521 }
1522 }
1523
1524 ASSERT (FALSE);
1525
1526 return 0;
1527 }
1528
1529 /**
1530 Get SKU ID table from PCD database.
1531
1532 @param LocalTokenNumberTableIdx Index of local token number in token number table.
1533 @param IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
1534 If FALSE, the pcd entry is initialized in DXE phase.
1535 @return Pointer to SKU ID array table
1536
1537 **/
1538 SKU_ID *
1539 GetSkuIdArray (
1540 IN UINTN LocalTokenNumberTableIdx,
1541 IN BOOLEAN IsPeiDb
1542 )
1543 {
1544 SKU_HEAD *SkuHead;
1545 UINTN LocalTokenNumber;
1546 UINT8 *Database;
1547
1548 if (IsPeiDb) {
1549 LocalTokenNumber = *((UINT32 *)((UINT8 *)mPcdDatabase.PeiDb + mPcdDatabase.PeiDb->LocalTokenNumberTableOffset) + LocalTokenNumberTableIdx);
1550 Database = (UINT8 *) mPcdDatabase.PeiDb;
1551 } else {
1552 LocalTokenNumber = *((UINT32 *)((UINT8 *)mPcdDatabase.DxeDb + mPcdDatabase.DxeDb->LocalTokenNumberTableOffset) + LocalTokenNumberTableIdx);
1553 Database = (UINT8 *) mPcdDatabase.DxeDb;
1554 }
1555
1556 ASSERT ((LocalTokenNumber & PCD_TYPE_SKU_ENABLED) != 0);
1557
1558 SkuHead = (SKU_HEAD *) ((UINT8 *)Database + (LocalTokenNumber & PCD_DATABASE_OFFSET_MASK));
1559
1560 return (SKU_ID *) (Database + SkuHead->SkuIdTableOffset);
1561
1562 }
1563
1564 /**
1565 Wrapper function of getting index of PCD entry in size table.
1566
1567 @param LocalTokenNumberTableIdx Index of this PCD in local token number table.
1568 @param IsPeiDb If TRUE, the pcd entry is initialized in PEI phase,
1569 If FALSE, the pcd entry is initialized in DXE phase.
1570
1571 @return index of PCD entry in size table.
1572 **/
1573 UINTN
1574 GetSizeTableIndex (
1575 IN UINTN LocalTokenNumberTableIdx,
1576 IN BOOLEAN IsPeiDb
1577 )
1578 {
1579 UINT32 *LocalTokenNumberTable;
1580 UINTN LocalTokenNumber;
1581 UINTN Index;
1582 UINTN SizeTableIdx;
1583 SKU_ID *SkuIdTable;
1584
1585 if (IsPeiDb) {
1586 LocalTokenNumberTable = (UINT32 *)((UINT8 *)mPcdDatabase.PeiDb + mPcdDatabase.PeiDb->LocalTokenNumberTableOffset);
1587 } else {
1588 LocalTokenNumberTable = (UINT32 *)((UINT8 *)mPcdDatabase.DxeDb + mPcdDatabase.DxeDb->LocalTokenNumberTableOffset);
1589 }
1590
1591 SizeTableIdx = 0;
1592
1593 for (Index = 0; Index < LocalTokenNumberTableIdx; Index ++) {
1594 LocalTokenNumber = LocalTokenNumberTable[Index];
1595
1596 if ((LocalTokenNumber & PCD_DATUM_TYPE_ALL_SET) == PCD_DATUM_TYPE_POINTER) {
1597 //
1598 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1599 // PCD entry.
1600 //
1601 if ((LocalTokenNumber & PCD_TYPE_VPD) != 0) {
1602 //
1603 // We have only two entry for VPD enabled PCD entry:
1604 // 1) MAX Size.
1605 // 2) Current Size
1606 // Current size is equal to MAX size.
1607 //
1608 SizeTableIdx += 2;
1609 } else {
1610 if ((LocalTokenNumber & PCD_TYPE_SKU_ENABLED) == 0) {
1611 //
1612 // We have only two entry for Non-Sku enabled PCD entry:
1613 // 1) MAX SIZE
1614 // 2) Current Size
1615 //
1616 SizeTableIdx += 2;
1617 } else {
1618 //
1619 // We have these entry for SKU enabled PCD entry
1620 // 1) MAX SIZE
1621 // 2) Current Size for each SKU_ID (It is equal to MaxSku).
1622 //
1623 SkuIdTable = GetSkuIdArray (Index, IsPeiDb);
1624 SizeTableIdx += (UINTN)*SkuIdTable + 1;
1625 }
1626 }
1627 }
1628
1629 }
1630
1631 return SizeTableIdx;
1632 }
1633
1634 /**
1635 Get size of POINTER type PCD value.
1636
1637 @param LocalTokenNumberTableIdx Index of local token number in local token number table.
1638 @param MaxSize Maxmium size of POINTER type PCD value.
1639
1640 @return size of POINTER type PCD value.
1641
1642 **/
1643 UINTN
1644 GetPtrTypeSize (
1645 IN UINTN LocalTokenNumberTableIdx,
1646 OUT UINTN *MaxSize
1647 )
1648 {
1649 INTN SizeTableIdx;
1650 UINTN LocalTokenNumber;
1651 SKU_ID *SkuIdTable;
1652 SIZE_INFO *SizeTable;
1653 UINTN Index;
1654 BOOLEAN IsPeiDb;
1655 UINT32 *LocalTokenNumberTable;
1656
1657 // EBC compiler is very choosy. It may report warning about comparison
1658 // between UINTN and 0 . So we add 1 in each size of the
1659 // comparison.
1660 IsPeiDb = (BOOLEAN) (LocalTokenNumberTableIdx + 1 < mPeiLocalTokenCount + 1);
1661
1662
1663 if (IsPeiDb) {
1664 LocalTokenNumberTable = (UINT32 *)((UINT8 *)mPcdDatabase.PeiDb + mPcdDatabase.PeiDb->LocalTokenNumberTableOffset);
1665 SizeTable = (SIZE_INFO *)((UINT8 *)mPcdDatabase.PeiDb + mPcdDatabase.PeiDb->SizeTableOffset);
1666 } else {
1667 LocalTokenNumberTableIdx -= mPeiLocalTokenCount;
1668 LocalTokenNumberTable = (UINT32 *)((UINT8 *)mPcdDatabase.DxeDb + mPcdDatabase.DxeDb->LocalTokenNumberTableOffset);
1669 SizeTable = (SIZE_INFO *)((UINT8 *)mPcdDatabase.DxeDb + mPcdDatabase.DxeDb->SizeTableOffset);
1670 }
1671
1672 LocalTokenNumber = LocalTokenNumberTable[LocalTokenNumberTableIdx];
1673
1674 ASSERT ((LocalTokenNumber & PCD_DATUM_TYPE_ALL_SET) == PCD_DATUM_TYPE_POINTER);
1675
1676 SizeTableIdx = GetSizeTableIndex (LocalTokenNumberTableIdx, IsPeiDb);
1677
1678 *MaxSize = SizeTable[SizeTableIdx];
1679 //
1680 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1681 // PCD entry.
1682 //
1683 if ((LocalTokenNumber & PCD_TYPE_VPD) != 0) {
1684 //
1685 // We have only two entry for VPD enabled PCD entry:
1686 // 1) MAX Size.
1687 // 2) Current Size
1688 // We consider current size is equal to MAX size.
1689 //
1690 return *MaxSize;
1691 } else {
1692 if ((LocalTokenNumber & PCD_TYPE_SKU_ENABLED) == 0) {
1693 //
1694 // We have only two entry for Non-Sku enabled PCD entry:
1695 // 1) MAX SIZE
1696 // 2) Current Size
1697 //
1698 return SizeTable[SizeTableIdx + 1];
1699 } else {
1700 //
1701 // We have these entry for SKU enabled PCD entry
1702 // 1) MAX SIZE
1703 // 2) Current Size for each SKU_ID (It is equal to MaxSku).
1704 //
1705 SkuIdTable = GetSkuIdArray (LocalTokenNumberTableIdx, IsPeiDb);
1706 for (Index = 0; Index < SkuIdTable[0]; Index++) {
1707 if (SkuIdTable[1 + Index] == mPcdDatabase.PeiDb->SystemSkuId) {
1708 return SizeTable[SizeTableIdx + 1 + Index];
1709 }
1710 }
1711 return SizeTable[SizeTableIdx + 1];
1712 }
1713 }
1714 }
1715
1716 /**
1717 Set size of POINTER type PCD value. The size should not exceed the maximum size
1718 of this PCD value.
1719
1720 @param LocalTokenNumberTableIdx Index of local token number in local token number table.
1721 @param CurrentSize Size of POINTER type PCD value.
1722
1723 @retval TRUE Success to set size of PCD value.
1724 @retval FALSE Fail to set size of PCD value.
1725 **/
1726 BOOLEAN
1727 SetPtrTypeSize (
1728 IN UINTN LocalTokenNumberTableIdx,
1729 IN OUT UINTN *CurrentSize
1730 )
1731 {
1732 INTN SizeTableIdx;
1733 UINTN LocalTokenNumber;
1734 SKU_ID *SkuIdTable;
1735 SIZE_INFO *SizeTable;
1736 UINTN Index;
1737 UINTN MaxSize;
1738 BOOLEAN IsPeiDb;
1739 UINT32 *LocalTokenNumberTable;
1740
1741 //
1742 // EBC compiler is very choosy. It may report warning about comparison
1743 // between UINTN and 0 . So we add 1 in each size of the
1744 // comparison.
1745 //
1746 IsPeiDb = (BOOLEAN) (LocalTokenNumberTableIdx + 1 < mPeiLocalTokenCount + 1);
1747
1748 if (IsPeiDb) {
1749 LocalTokenNumberTable = (UINT32 *)((UINT8 *)mPcdDatabase.PeiDb + mPcdDatabase.PeiDb->LocalTokenNumberTableOffset);
1750 SizeTable = (SIZE_INFO *)((UINT8 *)mPcdDatabase.PeiDb + mPcdDatabase.PeiDb->SizeTableOffset);
1751 } else {
1752 LocalTokenNumberTableIdx -= mPeiLocalTokenCount;
1753 LocalTokenNumberTable = (UINT32 *)((UINT8 *)mPcdDatabase.DxeDb + mPcdDatabase.DxeDb->LocalTokenNumberTableOffset);
1754 SizeTable = (SIZE_INFO *)((UINT8 *)mPcdDatabase.DxeDb + mPcdDatabase.DxeDb->SizeTableOffset);
1755 }
1756
1757 LocalTokenNumber = LocalTokenNumberTable[LocalTokenNumberTableIdx];
1758
1759 ASSERT ((LocalTokenNumber & PCD_DATUM_TYPE_ALL_SET) == PCD_DATUM_TYPE_POINTER);
1760
1761 SizeTableIdx = GetSizeTableIndex (LocalTokenNumberTableIdx, IsPeiDb);
1762
1763 MaxSize = SizeTable[SizeTableIdx];
1764 //
1765 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1766 // PCD entry.
1767 //
1768 if ((LocalTokenNumber & PCD_TYPE_VPD) != 0) {
1769 //
1770 // We shouldn't come here as we don't support SET for VPD
1771 //
1772 ASSERT (FALSE);
1773 return FALSE;
1774 } else {
1775 if ((*CurrentSize > MaxSize) ||
1776 (*CurrentSize == MAX_ADDRESS)) {
1777 *CurrentSize = MaxSize;
1778 return FALSE;
1779 }
1780
1781 if ((LocalTokenNumber & PCD_TYPE_SKU_ENABLED) == 0) {
1782 //
1783 // We have only two entry for Non-Sku enabled PCD entry:
1784 // 1) MAX SIZE
1785 // 2) Current Size
1786 //
1787 SizeTable[SizeTableIdx + 1] = (SIZE_INFO) *CurrentSize;
1788 return TRUE;
1789 } else {
1790 //
1791 // We have these entry for SKU enabled PCD entry
1792 // 1) MAX SIZE
1793 // 2) Current Size for each SKU_ID (It is equal to MaxSku).
1794 //
1795 SkuIdTable = GetSkuIdArray (LocalTokenNumberTableIdx, IsPeiDb);
1796 for (Index = 0; Index < SkuIdTable[0]; Index++) {
1797 if (SkuIdTable[1 + Index] == mPcdDatabase.PeiDb->SystemSkuId) {
1798 SizeTable[SizeTableIdx + 1 + Index] = (SIZE_INFO) *CurrentSize;
1799 return TRUE;
1800 }
1801 }
1802 SizeTable[SizeTableIdx + 1] = (SIZE_INFO) *CurrentSize;
1803 return TRUE;
1804 }
1805 }
1806 }