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