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