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