]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/PCD/Pei/Service.c
MdeModulePkg PCD: Fix PCD driver to return default data if size mismatch.
[mirror_edk2.git] / MdeModulePkg / Universal / PCD / Pei / Service.c
1 /** @file
2 The driver internal functions are implmented here.
3 They build Pei PCD database, and provide access service to PCD database.
4
5 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "Service.h"
17
18 /**
19 Get Local Token Number by Token Number.
20
21 @param[in] Database PCD database.
22 @param[in] TokenNumber The PCD token number.
23
24 @return Local Token Number.
25 **/
26 UINT32
27 GetLocalTokenNumber (
28 IN PEI_PCD_DATABASE *Database,
29 IN UINTN TokenNumber
30 )
31 {
32 UINT32 LocalTokenNumber;
33 UINTN Size;
34 UINTN MaxSize;
35
36 //
37 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
38 // We have to decrement TokenNumber by 1 to make it usable
39 // as the array index.
40 //
41 TokenNumber--;
42
43 LocalTokenNumber = *((UINT32 *)((UINT8 *)Database + Database->LocalTokenNumberTableOffset) + TokenNumber);
44
45 Size = (LocalTokenNumber & PCD_DATUM_TYPE_ALL_SET) >> PCD_DATUM_TYPE_SHIFT;
46
47 if ((LocalTokenNumber & PCD_TYPE_SKU_ENABLED) == PCD_TYPE_SKU_ENABLED) {
48 if (Size == 0) {
49 GetPtrTypeSize (TokenNumber, &MaxSize, Database);
50 } else {
51 MaxSize = Size;
52 }
53 LocalTokenNumber = GetSkuEnabledTokenNumber (LocalTokenNumber & ~PCD_TYPE_SKU_ENABLED, MaxSize);
54 }
55
56 return LocalTokenNumber;
57 }
58
59 /**
60 Get PCD type by Local Token Number.
61
62 @param[in] LocalTokenNumber The PCD local token number.
63
64 @return PCD type.
65 **/
66 EFI_PCD_TYPE
67 GetPcdType (
68 IN UINT32 LocalTokenNumber
69 )
70 {
71 switch (LocalTokenNumber & PCD_DATUM_TYPE_ALL_SET) {
72 case PCD_DATUM_TYPE_POINTER:
73 return EFI_PCD_TYPE_PTR;
74 case PCD_DATUM_TYPE_UINT8:
75 if ((LocalTokenNumber & PCD_DATUM_TYPE_UINT8_BOOLEAN) == PCD_DATUM_TYPE_UINT8_BOOLEAN) {
76 return EFI_PCD_TYPE_BOOL;
77 } else {
78 return EFI_PCD_TYPE_8;
79 }
80 case PCD_DATUM_TYPE_UINT16:
81 return EFI_PCD_TYPE_16;
82 case PCD_DATUM_TYPE_UINT32:
83 return EFI_PCD_TYPE_32;
84 case PCD_DATUM_TYPE_UINT64:
85 return EFI_PCD_TYPE_64;
86 default:
87 ASSERT (FALSE);
88 return EFI_PCD_TYPE_8;
89 }
90 }
91
92 /**
93 Get PCD name.
94
95 @param[in] OnlyTokenSpaceName If TRUE, only need to get the TokenSpaceCName.
96 If FALSE, need to get the full PCD name.
97 @param[in] Database PCD database.
98 @param[in] TokenNumber The PCD token number.
99
100 @return The TokenSpaceCName or full PCD name.
101 **/
102 CHAR8 *
103 GetPcdName (
104 IN BOOLEAN OnlyTokenSpaceName,
105 IN PEI_PCD_DATABASE *Database,
106 IN UINTN TokenNumber
107 )
108 {
109 UINT8 *StringTable;
110 PCD_NAME_INDEX *PcdNameIndex;
111 CHAR8 *TokenSpaceName;
112 CHAR8 *PcdName;
113 CHAR8 *Name;
114
115 //
116 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
117 // We have to decrement TokenNumber by 1 to make it usable
118 // as the array index.
119 //
120 TokenNumber--;
121
122 StringTable = (UINT8 *) Database + Database->StringTableOffset;
123
124 //
125 // Get the PCD name index.
126 //
127 PcdNameIndex = (PCD_NAME_INDEX *)((UINT8 *) Database + Database->PcdNameTableOffset) + TokenNumber;
128 TokenSpaceName = (CHAR8 *)&StringTable[PcdNameIndex->TokenSpaceCNameIndex];
129 PcdName = (CHAR8 *)&StringTable[PcdNameIndex->PcdCNameIndex];
130
131 if (OnlyTokenSpaceName) {
132 //
133 // Only need to get the TokenSpaceCName.
134 //
135 Name = AllocateCopyPool (AsciiStrSize (TokenSpaceName), TokenSpaceName);
136 } else {
137 //
138 // Need to get the full PCD name.
139 //
140 Name = AllocateZeroPool (AsciiStrSize (TokenSpaceName) + AsciiStrSize (PcdName));
141 ASSERT (Name != NULL);
142 //
143 // Catenate TokenSpaceCName and PcdCName with a '.' to form the full PCD name.
144 //
145 AsciiStrCat (Name, TokenSpaceName);
146 Name[AsciiStrSize (TokenSpaceName) - sizeof (CHAR8)] = '.';
147 AsciiStrCat (Name, PcdName);
148 }
149
150 return Name;
151 }
152
153 /**
154 Retrieve additional information associated with a PCD token.
155
156 This includes information such as the type of value the TokenNumber is associated with as well as possible
157 human readable name that is associated with the token.
158
159 @param[in] Database PCD database.
160 @param[in] Guid The 128-bit unique value that designates the namespace from which to extract the value.
161 @param[in] TokenNumber The PCD token number.
162 @param[out] PcdInfo The returned information associated with the requested TokenNumber.
163 The caller is responsible for freeing the buffer that is allocated by callee for PcdInfo->PcdName.
164
165 @retval EFI_SUCCESS The PCD information was returned successfully
166 @retval EFI_NOT_FOUND The PCD service could not find the requested token number.
167 **/
168 EFI_STATUS
169 ExGetPcdInfo (
170 IN PEI_PCD_DATABASE *Database,
171 IN CONST EFI_GUID *Guid,
172 IN UINTN TokenNumber,
173 OUT EFI_PCD_INFO *PcdInfo
174 )
175 {
176 UINTN GuidTableIdx;
177 EFI_GUID *MatchGuid;
178 EFI_GUID *GuidTable;
179 DYNAMICEX_MAPPING *ExMapTable;
180 UINTN Index;
181 UINT32 LocalTokenNumber;
182
183 GuidTable = (EFI_GUID *)((UINT8 *)Database + Database->GuidTableOffset);
184 MatchGuid = ScanGuid (GuidTable, Database->GuidTableCount * sizeof(EFI_GUID), Guid);
185
186 if (MatchGuid == NULL) {
187 return EFI_NOT_FOUND;
188 }
189
190 GuidTableIdx = MatchGuid - GuidTable;
191
192 ExMapTable = (DYNAMICEX_MAPPING *)((UINT8 *)Database + Database->ExMapTableOffset);
193
194 //
195 // Find the PCD by GuidTableIdx and ExTokenNumber in ExMapTable.
196 //
197 for (Index = 0; Index < Database->ExTokenCount; Index++) {
198 if (ExMapTable[Index].ExGuidIndex == GuidTableIdx) {
199 if (TokenNumber == PCD_INVALID_TOKEN_NUMBER) {
200 //
201 // TokenNumber is 0, follow spec to set PcdType to EFI_PCD_TYPE_8,
202 // PcdSize to 0 and PcdName to the null-terminated ASCII string
203 // associated with the token's namespace Guid.
204 //
205 PcdInfo->PcdType = EFI_PCD_TYPE_8;
206 PcdInfo->PcdSize = 0;
207 //
208 // Here use one representative in the token space to get the TokenSpaceCName.
209 //
210 PcdInfo->PcdName = GetPcdName (TRUE, Database, ExMapTable[Index].TokenNumber);
211 return EFI_SUCCESS;
212 } else if (ExMapTable[Index].ExTokenNumber == TokenNumber) {
213 PcdInfo->PcdSize = PeiPcdGetSize (ExMapTable[Index].TokenNumber);
214 LocalTokenNumber = GetLocalTokenNumber (Database, ExMapTable[Index].TokenNumber);
215 PcdInfo->PcdType = GetPcdType (LocalTokenNumber);
216 PcdInfo->PcdName = GetPcdName (FALSE, Database, ExMapTable[Index].TokenNumber);
217 return EFI_SUCCESS;
218 }
219 }
220 }
221
222 return EFI_NOT_FOUND;
223 }
224
225 /**
226 Retrieve additional information associated with a PCD token.
227
228 This includes information such as the type of value the TokenNumber is associated with as well as possible
229 human readable name that is associated with the token.
230
231 @param[in] Guid The 128-bit unique value that designates the namespace from which to extract the value.
232 @param[in] TokenNumber The PCD token number.
233 @param[out] PcdInfo The returned information associated with the requested TokenNumber.
234 The caller is responsible for freeing the buffer that is allocated by callee for PcdInfo->PcdName.
235
236 @retval EFI_SUCCESS The PCD information was returned successfully.
237 @retval EFI_NOT_FOUND The PCD service could not find the requested token number.
238 **/
239 EFI_STATUS
240 PeiGetPcdInfo (
241 IN CONST EFI_GUID *Guid,
242 IN UINTN TokenNumber,
243 OUT EFI_PCD_INFO *PcdInfo
244 )
245 {
246 PEI_PCD_DATABASE *PeiPcdDb;
247 BOOLEAN PeiExMapTableEmpty;
248 UINTN PeiNexTokenNumber;
249 UINT32 LocalTokenNumber;
250
251 ASSERT (PcdInfo != NULL);
252
253 PeiPcdDb = GetPcdDatabase ();
254 PeiNexTokenNumber = PeiPcdDb->LocalTokenCount - PeiPcdDb->ExTokenCount;
255
256 if (PeiPcdDb->ExTokenCount == 0) {
257 PeiExMapTableEmpty = TRUE;
258 } else {
259 PeiExMapTableEmpty = FALSE;
260 }
261
262 if (Guid == NULL) {
263 if (TokenNumber > PeiNexTokenNumber) {
264 return EFI_NOT_FOUND;
265 } else if (TokenNumber == PCD_INVALID_TOKEN_NUMBER) {
266 //
267 // TokenNumber is 0, follow spec to set PcdType to EFI_PCD_TYPE_8,
268 // PcdSize to 0 and PcdName to NULL for default Token Space.
269 //
270 PcdInfo->PcdType = EFI_PCD_TYPE_8;
271 PcdInfo->PcdSize = 0;
272 PcdInfo->PcdName = NULL;
273 } else {
274 PcdInfo->PcdSize = PeiPcdGetSize (TokenNumber);
275 LocalTokenNumber = GetLocalTokenNumber (PeiPcdDb, TokenNumber);
276 PcdInfo->PcdType = GetPcdType (LocalTokenNumber);
277 PcdInfo->PcdName = GetPcdName (FALSE, PeiPcdDb, TokenNumber);
278 }
279 return EFI_SUCCESS;
280 } else {
281 if (PeiExMapTableEmpty) {
282 return EFI_NOT_FOUND;
283 }
284 return ExGetPcdInfo (
285 PeiPcdDb,
286 Guid,
287 TokenNumber,
288 PcdInfo
289 );
290 }
291 }
292
293 /**
294 The function registers the CallBackOnSet fucntion
295 according to TokenNumber and EFI_GUID space.
296
297 @param ExTokenNumber The token number.
298 @param Guid The GUID space.
299 @param CallBackFunction The Callback function to be registered.
300 @param Register To register or unregister the callback function.
301
302 @retval EFI_SUCCESS If the Callback function is registered.
303 @retval EFI_NOT_FOUND If the PCD Entry is not found according to Token Number and GUID space.
304 @retval EFI_OUT_OF_RESOURCES If the callback function can't be registered because there is not free
305 slot left in the CallbackFnTable.
306 @retval EFI_INVALID_PARAMETER If the callback function want to be de-registered can not be found.
307 **/
308 EFI_STATUS
309 PeiRegisterCallBackWorker (
310 IN UINTN ExTokenNumber,
311 IN CONST EFI_GUID *Guid, OPTIONAL
312 IN PCD_PPI_CALLBACK CallBackFunction,
313 IN BOOLEAN Register
314 )
315 {
316 EFI_HOB_GUID_TYPE *GuidHob;
317 PCD_PPI_CALLBACK *CallbackTable;
318 PCD_PPI_CALLBACK Compare;
319 PCD_PPI_CALLBACK Assign;
320 UINT32 LocalTokenNumber;
321 UINT32 LocalTokenCount;
322 UINTN PeiNexTokenNumber;
323 UINTN TokenNumber;
324 UINTN Idx;
325 PEI_PCD_DATABASE *PeiPcdDb;
326
327 PeiPcdDb = GetPcdDatabase();
328 LocalTokenCount = PeiPcdDb->LocalTokenCount;
329 PeiNexTokenNumber = PeiPcdDb->LocalTokenCount - PeiPcdDb->ExTokenCount;
330
331 if (Guid == NULL) {
332 TokenNumber = ExTokenNumber;
333 //
334 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
335 // We have to decrement TokenNumber by 1 to make it usable
336 // as the array index.
337 //
338 TokenNumber--;
339 ASSERT (TokenNumber + 1 < (PeiNexTokenNumber + 1));
340 } else {
341 TokenNumber = GetExPcdTokenNumber (Guid, ExTokenNumber);
342 if (TokenNumber == PCD_INVALID_TOKEN_NUMBER) {
343 return EFI_NOT_FOUND;
344 }
345 //
346 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
347 // We have to decrement TokenNumber by 1 to make it usable
348 // as the array index.
349 //
350 TokenNumber--;
351 // EBC compiler is very choosy. It may report warning about comparison
352 // between UINTN and 0 . So we add 1 in each size of the
353 // comparison.
354 ASSERT ((TokenNumber + 1) < (LocalTokenCount + 1));
355 }
356
357
358 LocalTokenNumber = *((UINT32 *)((UINT8 *)PeiPcdDb + PeiPcdDb->LocalTokenNumberTableOffset) + TokenNumber);
359
360 //
361 // We don't support SET for HII and VPD type PCD entry in PEI phase.
362 // So we will assert if any register callback for such PCD entry.
363 //
364 ASSERT ((LocalTokenNumber & PCD_TYPE_HII) == 0);
365 ASSERT ((LocalTokenNumber & PCD_TYPE_VPD) == 0);
366
367 GuidHob = GetFirstGuidHob (&gEfiCallerIdGuid);
368 ASSERT (GuidHob != NULL);
369
370 CallbackTable = GET_GUID_HOB_DATA (GuidHob);
371 CallbackTable = CallbackTable + (TokenNumber * PcdGet32 (PcdMaxPeiPcdCallBackNumberPerPcdEntry));
372
373 Compare = Register? NULL: CallBackFunction;
374 Assign = Register? CallBackFunction: NULL;
375
376
377 for (Idx = 0; Idx < PcdGet32 (PcdMaxPeiPcdCallBackNumberPerPcdEntry); Idx++) {
378 if (CallbackTable[Idx] == Compare) {
379 CallbackTable[Idx] = Assign;
380 return EFI_SUCCESS;
381 }
382 }
383
384 return Register? EFI_OUT_OF_RESOURCES : EFI_INVALID_PARAMETER;
385
386 }
387
388
389 /**
390 Find the Pcd database.
391
392 @param FileHandle Handle of the file the external PCD database binary located.
393
394 @retval The base address of external PCD database binary.
395 @retval NULL Return NULL if not find.
396 **/
397 VOID *
398 LocateExPcdBinary (
399 IN EFI_PEI_FILE_HANDLE FileHandle
400 )
401 {
402 EFI_STATUS Status;
403 VOID *PcdDb;
404
405 PcdDb = NULL;
406
407 ASSERT (FileHandle != NULL);
408
409 Status = PeiServicesFfsFindSectionData (EFI_SECTION_RAW, FileHandle, &PcdDb);
410 ASSERT_EFI_ERROR (Status);
411
412 //
413 // Check the first bytes (Header Signature Guid) and build version.
414 //
415 if (!CompareGuid (PcdDb, &gPcdDataBaseSignatureGuid) ||
416 (((PEI_PCD_DATABASE *) PcdDb)->BuildVersion != PCD_SERVICE_PEIM_VERSION)) {
417 ASSERT (FALSE);
418 }
419 return PcdDb;
420 }
421
422
423 /**
424 The function builds the PCD database.
425
426 @param FileHandle Handle of the file the external PCD database binary located.
427
428 @return Pointer to PCD database.
429 **/
430 PEI_PCD_DATABASE *
431 BuildPcdDatabase (
432 IN EFI_PEI_FILE_HANDLE FileHandle
433 )
434 {
435 PEI_PCD_DATABASE *Database;
436 PEI_PCD_DATABASE *PeiPcdDbBinary;
437 VOID *CallbackFnTable;
438 UINTN SizeOfCallbackFnTable;
439
440 //
441 // Locate the external PCD database binary for one section of current FFS
442 //
443 PeiPcdDbBinary = LocateExPcdBinary (FileHandle);
444
445 ASSERT(PeiPcdDbBinary != NULL);
446
447 Database = BuildGuidHob (&gPcdDataBaseHobGuid, PeiPcdDbBinary->Length + PeiPcdDbBinary->UninitDataBaseSize);
448
449 ZeroMem (Database, PeiPcdDbBinary->Length + PeiPcdDbBinary->UninitDataBaseSize);
450
451 //
452 // PeiPcdDbBinary is smaller than Database
453 //
454 CopyMem (Database, PeiPcdDbBinary, PeiPcdDbBinary->Length);
455
456 SizeOfCallbackFnTable = Database->LocalTokenCount * sizeof (PCD_PPI_CALLBACK) * PcdGet32 (PcdMaxPeiPcdCallBackNumberPerPcdEntry);
457
458 CallbackFnTable = BuildGuidHob (&gEfiCallerIdGuid, SizeOfCallbackFnTable);
459
460 ZeroMem (CallbackFnTable, SizeOfCallbackFnTable);
461
462 return Database;
463 }
464
465 /**
466 The function is provided by PCD PEIM and PCD DXE driver to
467 do the work of reading a HII variable from variable service.
468
469 @param VariableGuid The Variable GUID.
470 @param VariableName The Variable Name.
471 @param VariableData The output data.
472 @param VariableSize The size of the variable.
473
474 @retval EFI_SUCCESS Operation successful.
475 @retval EFI_NOT_FOUND Variablel not found.
476 **/
477 EFI_STATUS
478 GetHiiVariable (
479 IN CONST EFI_GUID *VariableGuid,
480 IN UINT16 *VariableName,
481 OUT VOID **VariableData,
482 OUT UINTN *VariableSize
483 )
484 {
485 UINTN Size;
486 EFI_STATUS Status;
487 VOID *Buffer;
488 EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariablePpi;
489
490 Status = PeiServicesLocatePpi (&gEfiPeiReadOnlyVariable2PpiGuid, 0, NULL, (VOID **) &VariablePpi);
491 ASSERT_EFI_ERROR (Status);
492
493 Size = 0;
494 Status = VariablePpi->GetVariable (
495 VariablePpi,
496 VariableName,
497 (EFI_GUID *) VariableGuid,
498 NULL,
499 &Size,
500 NULL
501 );
502
503 if (Status == EFI_BUFFER_TOO_SMALL) {
504 Status = PeiServicesAllocatePool (Size, &Buffer);
505 ASSERT_EFI_ERROR (Status);
506
507 Status = VariablePpi->GetVariable (
508 VariablePpi,
509 (UINT16 *) VariableName,
510 (EFI_GUID *) VariableGuid,
511 NULL,
512 &Size,
513 Buffer
514 );
515 ASSERT_EFI_ERROR (Status);
516
517 *VariableSize = Size;
518 *VariableData = Buffer;
519
520 return EFI_SUCCESS;
521 }
522
523 return EFI_NOT_FOUND;
524 }
525
526 /**
527 Find the local token number according to system SKU ID.
528
529 @param LocalTokenNumber PCD token number
530 @param Size The size of PCD entry.
531
532 @return Token number according to system SKU ID.
533
534 **/
535 UINT32
536 GetSkuEnabledTokenNumber (
537 UINT32 LocalTokenNumber,
538 UINTN Size
539 )
540 {
541 PEI_PCD_DATABASE *PeiPcdDb;
542 SKU_HEAD *SkuHead;
543 SKU_ID *SkuIdTable;
544 INTN Index;
545 UINT8 *Value;
546 BOOLEAN FoundSku;
547
548 PeiPcdDb = GetPcdDatabase ();
549
550 ASSERT ((LocalTokenNumber & PCD_TYPE_SKU_ENABLED) == 0);
551
552 SkuHead = (SKU_HEAD *) ((UINT8 *)PeiPcdDb + (LocalTokenNumber & PCD_DATABASE_OFFSET_MASK));
553 Value = (UINT8 *) ((UINT8 *)PeiPcdDb + (SkuHead->SkuDataStartOffset));
554 SkuIdTable = (SKU_ID *) ((UINT8 *)PeiPcdDb + (SkuHead->SkuIdTableOffset));
555
556 //
557 // Find the current system's SKU ID entry in SKU ID table.
558 //
559 FoundSku = FALSE;
560 for (Index = 0; Index < SkuIdTable[0]; Index++) {
561 if (PeiPcdDb->SystemSkuId == SkuIdTable[Index + 1]) {
562 FoundSku = TRUE;
563 break;
564 }
565 }
566
567 //
568 // Find the default SKU ID entry in SKU ID table.
569 //
570 if(!FoundSku) {
571 for (Index = 0; Index < SkuIdTable[0]; Index++) {
572 if (0 == SkuIdTable[Index + 1]) {
573 break;
574 }
575 }
576 }
577 ASSERT (Index < SkuIdTable[0]);
578
579 switch (LocalTokenNumber & PCD_TYPE_ALL_SET) {
580 case PCD_TYPE_VPD:
581 Value = (UINT8 *) &(((VPD_HEAD *) Value)[Index]);
582 return (UINT32) ((Value - (UINT8 *) PeiPcdDb) | PCD_TYPE_VPD);
583
584 case PCD_TYPE_HII:
585 Value = (UINT8 *) &(((VARIABLE_HEAD *) Value)[Index]);
586 return (UINT32) ((Value - (UINT8 *) PeiPcdDb) | PCD_TYPE_HII);
587
588 case PCD_TYPE_HII|PCD_TYPE_STRING:
589 Value = (UINT8 *) &(((VARIABLE_HEAD *) Value)[Index]);
590 return (UINT32) ((Value - (UINT8 *) PeiPcdDb) | PCD_TYPE_HII | PCD_TYPE_STRING);
591
592 case PCD_TYPE_STRING:
593 Value = (UINT8 *) &(((STRING_HEAD *) Value)[Index]);
594 return (UINT32) ((Value - (UINT8 *) PeiPcdDb) | PCD_TYPE_STRING);
595
596 case PCD_TYPE_DATA:
597 Value += Size * Index;
598 return (UINT32) ((Value - (UINT8 *) PeiPcdDb) | PCD_TYPE_DATA);
599
600 default:
601 ASSERT (FALSE);
602 }
603
604 ASSERT (FALSE);
605
606 return 0;
607 }
608
609 /**
610 Invoke the callback function when dynamic PCD entry was set, if this PCD entry
611 has registered callback function.
612
613 @param ExTokenNumber DynamicEx PCD's token number, if this PCD entry is dyanmicEx
614 type PCD.
615 @param Guid DynamicEx PCD's guid, if this PCD entry is dynamicEx type
616 PCD.
617 @param TokenNumber PCD token number generated by build tools.
618 @param Data Value want to be set for this PCD entry
619 @param Size The size of value
620
621 **/
622 VOID
623 InvokeCallbackOnSet (
624 UINTN ExTokenNumber,
625 CONST EFI_GUID *Guid, OPTIONAL
626 UINTN TokenNumber,
627 VOID *Data,
628 UINTN Size
629 )
630 {
631 EFI_HOB_GUID_TYPE *GuidHob;
632 PCD_PPI_CALLBACK *CallbackTable;
633 UINTN Idx;
634 PEI_PCD_DATABASE *PeiPcdDb;
635 UINT32 LocalTokenCount;
636
637 //
638 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
639 // We have to decrement TokenNumber by 1 to make it usable
640 // as the array index.
641 //
642 TokenNumber--;
643
644 PeiPcdDb = GetPcdDatabase ();
645 LocalTokenCount = PeiPcdDb->LocalTokenCount;
646
647 if (Guid == NULL) {
648 // EBC compiler is very choosy. It may report warning about comparison
649 // between UINTN and 0 . So we add 1 in each size of the
650 // comparison.
651 ASSERT (TokenNumber + 1 < (LocalTokenCount + 1));
652 }
653
654 GuidHob = GetFirstGuidHob (&gEfiCallerIdGuid);
655 ASSERT (GuidHob != NULL);
656
657 CallbackTable = GET_GUID_HOB_DATA (GuidHob);
658
659 CallbackTable += (TokenNumber * PcdGet32 (PcdMaxPeiPcdCallBackNumberPerPcdEntry));
660
661 for (Idx = 0; Idx < PcdGet32 (PcdMaxPeiPcdCallBackNumberPerPcdEntry); Idx++) {
662 if (CallbackTable[Idx] != NULL) {
663 CallbackTable[Idx] (Guid,
664 (Guid == NULL) ? (TokenNumber + 1) : ExTokenNumber,
665 Data,
666 Size
667 );
668 }
669 }
670 }
671
672 /**
673 Wrapper function for setting non-pointer type value for a PCD entry.
674
675 @param TokenNumber Pcd token number autogenerated by build tools.
676 @param Data Value want to be set for PCD entry
677 @param Size Size of value.
678
679 @return status of SetWorker.
680
681 **/
682 EFI_STATUS
683 SetValueWorker (
684 IN UINTN TokenNumber,
685 IN VOID *Data,
686 IN UINTN Size
687 )
688 {
689 return SetWorker (TokenNumber, Data, &Size, FALSE);
690 }
691
692 /**
693 Set value for an PCD entry
694
695 @param TokenNumber Pcd token number autogenerated by build tools.
696 @param Data Value want to be set for PCD entry
697 @param Size Size of value.
698 @param PtrType If TRUE, the type of PCD entry's value is Pointer.
699 If False, the type of PCD entry's value is not Pointer.
700
701 @retval EFI_INVALID_PARAMETER If this PCD type is VPD, VPD PCD can not be set.
702 @retval EFI_INVALID_PARAMETER If Size can not be set to size table.
703 @retval EFI_INVALID_PARAMETER If Size of non-Ptr type PCD does not match the size information in PCD database.
704 @retval EFI_NOT_FOUND If value type of PCD entry is intergrate, but not in
705 range of UINT8, UINT16, UINT32, UINT64
706 @retval EFI_NOT_FOUND Can not find the PCD type according to token number.
707 **/
708 EFI_STATUS
709 SetWorker (
710 IN UINTN TokenNumber,
711 IN VOID *Data,
712 IN OUT UINTN *Size,
713 IN BOOLEAN PtrType
714 )
715 {
716 UINT32 LocalTokenNumber;
717 UINTN PeiNexTokenNumber;
718 PEI_PCD_DATABASE *PeiPcdDb;
719 STRING_HEAD StringTableIdx;
720 UINTN Offset;
721 VOID *InternalData;
722 UINTN MaxSize;
723 UINT32 LocalTokenCount;
724
725 if (!FeaturePcdGet(PcdPeiFullPcdDatabaseEnable)) {
726 return EFI_UNSUPPORTED;
727 }
728
729 //
730 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
731 // We have to decrement TokenNumber by 1 to make it usable
732 // as the array index.
733 //
734 TokenNumber--;
735 PeiPcdDb = GetPcdDatabase ();
736 LocalTokenCount = PeiPcdDb->LocalTokenCount;
737
738 // EBC compiler is very choosy. It may report warning about comparison
739 // between UINTN and 0 . So we add 1 in each size of the
740 // comparison.
741 ASSERT (TokenNumber + 1 < (LocalTokenCount + 1));
742
743 if (PtrType) {
744 //
745 // Get MaxSize first, then check new size with max buffer size.
746 //
747 GetPtrTypeSize (TokenNumber, &MaxSize, PeiPcdDb);
748 if (*Size > MaxSize) {
749 *Size = MaxSize;
750 return EFI_INVALID_PARAMETER;
751 }
752 } else {
753 if (*Size != PeiPcdGetSize (TokenNumber + 1)) {
754 return EFI_INVALID_PARAMETER;
755 }
756 }
757
758 //
759 // We only invoke the callback function for Dynamic Type PCD Entry.
760 // For Dynamic EX PCD entry, we have invoked the callback function for Dynamic EX
761 // type PCD entry in ExSetWorker.
762 //
763 PeiNexTokenNumber = PeiPcdDb->LocalTokenCount - PeiPcdDb->ExTokenCount;
764 if (TokenNumber + 1 < PeiNexTokenNumber + 1) {
765 InvokeCallbackOnSet (0, NULL, TokenNumber + 1, Data, *Size);
766 }
767
768 LocalTokenNumber = GetLocalTokenNumber (PeiPcdDb, TokenNumber + 1);
769
770 Offset = LocalTokenNumber & PCD_DATABASE_OFFSET_MASK;
771 InternalData = (VOID *) ((UINT8 *) PeiPcdDb + Offset);
772
773 switch (LocalTokenNumber & PCD_TYPE_ALL_SET) {
774 case PCD_TYPE_VPD:
775 case PCD_TYPE_HII:
776 case PCD_TYPE_HII|PCD_TYPE_STRING:
777 {
778 ASSERT (FALSE);
779 return EFI_INVALID_PARAMETER;
780 }
781
782 case PCD_TYPE_STRING:
783 if (SetPtrTypeSize (TokenNumber, Size, PeiPcdDb)) {
784 StringTableIdx = *((STRING_HEAD *)InternalData);
785 CopyMem ((UINT8 *)PeiPcdDb + PeiPcdDb->StringTableOffset + StringTableIdx, Data, *Size);
786 return EFI_SUCCESS;
787 } else {
788 return EFI_INVALID_PARAMETER;
789 }
790
791 case PCD_TYPE_DATA:
792 {
793 if (PtrType) {
794 if (SetPtrTypeSize (TokenNumber, Size, PeiPcdDb)) {
795 CopyMem (InternalData, Data, *Size);
796 return EFI_SUCCESS;
797 } else {
798 return EFI_INVALID_PARAMETER;
799 }
800 }
801
802 switch (*Size) {
803 case sizeof(UINT8):
804 *((UINT8 *) InternalData) = *((UINT8 *) Data);
805 return EFI_SUCCESS;
806
807 case sizeof(UINT16):
808 *((UINT16 *) InternalData) = *((UINT16 *) Data);
809 return EFI_SUCCESS;
810
811 case sizeof(UINT32):
812 *((UINT32 *) InternalData) = *((UINT32 *) Data);
813 return EFI_SUCCESS;
814
815 case sizeof(UINT64):
816 *((UINT64 *) InternalData) = *((UINT64 *) Data);
817 return EFI_SUCCESS;
818
819 default:
820 ASSERT (FALSE);
821 return EFI_NOT_FOUND;
822 }
823 }
824
825 }
826
827 ASSERT (FALSE);
828 return EFI_NOT_FOUND;
829
830 }
831
832 /**
833 Wrapper function for set PCD value for non-Pointer type dynamic-ex PCD.
834
835 @param ExTokenNumber Token number for dynamic-ex PCD.
836 @param Guid Token space guid for dynamic-ex PCD.
837 @param Data Value want to be set.
838 @param SetSize The size of value.
839
840 @return status of ExSetWorker().
841
842 **/
843 EFI_STATUS
844 ExSetValueWorker (
845 IN UINTN ExTokenNumber,
846 IN CONST EFI_GUID *Guid,
847 IN VOID *Data,
848 IN UINTN Size
849 )
850 {
851 return ExSetWorker (ExTokenNumber, Guid, Data, &Size, FALSE);
852 }
853
854 /**
855 Set value for a dynamic-ex PCD entry.
856
857 This routine find the local token number according to dynamic-ex PCD's token
858 space guid and token number firstly, and invoke callback function if this PCD
859 entry registered callback function. Finally, invoken general SetWorker to set
860 PCD value.
861
862 @param ExTokenNumber Dynamic-ex PCD token number.
863 @param Guid Token space guid for dynamic-ex PCD.
864 @param Data PCD value want to be set
865 @param SetSize Size of value.
866 @param PtrType If TRUE, this PCD entry is pointer type.
867 If FALSE, this PCD entry is not pointer type.
868
869 @return status of SetWorker().
870
871 **/
872 EFI_STATUS
873 ExSetWorker (
874 IN UINTN ExTokenNumber,
875 IN CONST EFI_GUID *Guid,
876 IN VOID *Data,
877 IN OUT UINTN *Size,
878 IN BOOLEAN PtrType
879 )
880 {
881 UINTN TokenNumber;
882
883 if (!FeaturePcdGet(PcdPeiFullPcdDatabaseEnable)) {
884 return EFI_UNSUPPORTED;
885 }
886
887 TokenNumber = GetExPcdTokenNumber (Guid, ExTokenNumber);
888 if (TokenNumber == PCD_INVALID_TOKEN_NUMBER) {
889 return EFI_NOT_FOUND;
890 }
891
892 InvokeCallbackOnSet (ExTokenNumber, Guid, TokenNumber, Data, *Size);
893
894 return SetWorker (TokenNumber, Data, Size, PtrType);
895
896 }
897
898 /**
899 Wrapper function for get PCD value for dynamic-ex PCD.
900
901 @param Guid Token space guid for dynamic-ex PCD.
902 @param ExTokenNumber Token number for dyanmic-ex PCD.
903 @param GetSize The size of dynamic-ex PCD value.
904
905 @return PCD entry in PCD database.
906
907 **/
908 VOID *
909 ExGetWorker (
910 IN CONST EFI_GUID *Guid,
911 IN UINTN ExTokenNumber,
912 IN UINTN GetSize
913 )
914 {
915 return GetWorker (GetExPcdTokenNumber (Guid, ExTokenNumber), GetSize);
916 }
917
918 /**
919 Get the PCD entry pointer in PCD database.
920
921 This routine will visit PCD database to find the PCD entry according to given
922 token number. The given token number is autogened by build tools and it will be
923 translated to local token number. Local token number contains PCD's type and
924 offset of PCD entry in PCD database.
925
926 @param TokenNumber Token's number, it is autogened by build tools
927 @param GetSize The size of token's value
928
929 @return PCD entry pointer in PCD database
930
931 **/
932 VOID *
933 GetWorker (
934 IN UINTN TokenNumber,
935 IN UINTN GetSize
936 )
937 {
938 UINT32 Offset;
939 EFI_GUID *Guid;
940 UINT16 *Name;
941 VARIABLE_HEAD *VariableHead;
942 EFI_STATUS Status;
943 UINTN DataSize;
944 VOID *Data;
945 UINT8 *StringTable;
946 STRING_HEAD StringTableIdx;
947 PEI_PCD_DATABASE *PeiPcdDb;
948 UINT32 LocalTokenNumber;
949 UINT32 LocalTokenCount;
950 UINT8 *VaraiableDefaultBuffer;
951
952 //
953 // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.
954 // We have to decrement TokenNumber by 1 to make it usable
955 // as the array index.
956 //
957 TokenNumber--;
958
959 PeiPcdDb = GetPcdDatabase ();
960 LocalTokenCount = PeiPcdDb->LocalTokenCount;
961
962 // EBC compiler is very choosy. It may report warning about comparison
963 // between UINTN and 0 . So we add 1 in each size of the
964 // comparison.
965 ASSERT (TokenNumber + 1 < (LocalTokenCount + 1));
966
967 ASSERT ((GetSize == PeiPcdGetSize(TokenNumber + 1)) || (GetSize == 0));
968
969 LocalTokenNumber = GetLocalTokenNumber (PeiPcdDb, TokenNumber + 1);
970
971 Offset = LocalTokenNumber & PCD_DATABASE_OFFSET_MASK;
972 StringTable = (UINT8 *)PeiPcdDb + PeiPcdDb->StringTableOffset;
973
974 switch (LocalTokenNumber & PCD_TYPE_ALL_SET) {
975 case PCD_TYPE_VPD:
976 {
977 VPD_HEAD *VpdHead;
978 VpdHead = (VPD_HEAD *) ((UINT8 *)PeiPcdDb + Offset);
979 return (VOID *) (UINTN) (PcdGet32 (PcdVpdBaseAddress) + VpdHead->Offset);
980 }
981
982 case PCD_TYPE_HII|PCD_TYPE_STRING:
983 case PCD_TYPE_HII:
984 {
985 VariableHead = (VARIABLE_HEAD *) ((UINT8 *)PeiPcdDb + Offset);
986
987 Guid = (EFI_GUID *) ((UINT8 *)PeiPcdDb + PeiPcdDb->GuidTableOffset) + VariableHead->GuidTableIndex;
988 Name = (UINT16*)&StringTable[VariableHead->StringIndex];
989
990 if ((LocalTokenNumber & PCD_TYPE_ALL_SET) == (PCD_TYPE_HII|PCD_TYPE_STRING)) {
991 //
992 // If a HII type PCD's datum type is VOID*, the DefaultValueOffset is the index of
993 // string array in string table.
994 //
995 VaraiableDefaultBuffer = (UINT8 *) &StringTable[*(STRING_HEAD*)((UINT8*) PeiPcdDb + VariableHead->DefaultValueOffset)];
996 } else {
997 VaraiableDefaultBuffer = (UINT8 *) PeiPcdDb + VariableHead->DefaultValueOffset;
998 }
999 Status = GetHiiVariable (Guid, Name, &Data, &DataSize);
1000 if ((Status == EFI_SUCCESS) && (DataSize >= (VariableHead->Offset + GetSize))) {
1001 if (GetSize == 0) {
1002 //
1003 // It is a pointer type. So get the MaxSize reserved for
1004 // this PCD entry.
1005 //
1006 GetPtrTypeSize (TokenNumber, &GetSize, PeiPcdDb);
1007 if (GetSize > (DataSize - VariableHead->Offset)) {
1008 //
1009 // Use actual valid size.
1010 //
1011 GetSize = DataSize - VariableHead->Offset;
1012 }
1013 }
1014 //
1015 // If the operation is successful, we copy the data
1016 // to the default value buffer in the PCD Database.
1017 //
1018 CopyMem (VaraiableDefaultBuffer, (UINT8 *) Data + VariableHead->Offset, GetSize);
1019 }
1020 return (VOID *) VaraiableDefaultBuffer;
1021 }
1022
1023 case PCD_TYPE_DATA:
1024 return (VOID *) ((UINT8 *)PeiPcdDb + Offset);
1025
1026 case PCD_TYPE_STRING:
1027 StringTableIdx = * (STRING_HEAD*) ((UINT8 *) PeiPcdDb + Offset);
1028 return (VOID *) (&StringTable[StringTableIdx]);
1029
1030 default:
1031 ASSERT (FALSE);
1032 break;
1033
1034 }
1035
1036 ASSERT (FALSE);
1037
1038 return NULL;
1039
1040 }
1041
1042 /**
1043 Get Token Number according to dynamic-ex PCD's {token space guid:token number}
1044
1045 A dynamic-ex type PCD, developer must provide pair of token space guid: token number
1046 in DEC file. PCD database maintain a mapping table that translate pair of {token
1047 space guid: token number} to Token Number.
1048
1049 @param Guid Token space guid for dynamic-ex PCD entry.
1050 @param ExTokenNumber Dynamic-ex PCD token number.
1051
1052 @return Token Number for dynamic-ex PCD.
1053
1054 **/
1055 UINTN
1056 GetExPcdTokenNumber (
1057 IN CONST EFI_GUID *Guid,
1058 IN UINTN ExTokenNumber
1059 )
1060 {
1061 UINT32 Index;
1062 DYNAMICEX_MAPPING *ExMap;
1063 EFI_GUID *GuidTable;
1064 EFI_GUID *MatchGuid;
1065 UINTN MatchGuidIdx;
1066 PEI_PCD_DATABASE *PeiPcdDb;
1067
1068 PeiPcdDb = GetPcdDatabase();
1069
1070 ExMap = (DYNAMICEX_MAPPING *)((UINT8 *)PeiPcdDb + PeiPcdDb->ExMapTableOffset);
1071 GuidTable = (EFI_GUID *)((UINT8 *)PeiPcdDb + PeiPcdDb->GuidTableOffset);
1072
1073 MatchGuid = ScanGuid (GuidTable, PeiPcdDb->GuidTableCount * sizeof(EFI_GUID), Guid);
1074 //
1075 // We need to ASSERT here. If GUID can't be found in GuidTable, this is a
1076 // error in the BUILD system.
1077 //
1078 ASSERT (MatchGuid != NULL);
1079
1080 MatchGuidIdx = MatchGuid - GuidTable;
1081
1082 for (Index = 0; Index < PeiPcdDb->ExTokenCount; Index++) {
1083 if ((ExTokenNumber == ExMap[Index].ExTokenNumber) &&
1084 (MatchGuidIdx == ExMap[Index].ExGuidIndex)) {
1085 return ExMap[Index].TokenNumber;
1086 }
1087 }
1088
1089 return PCD_INVALID_TOKEN_NUMBER;
1090 }
1091
1092 /**
1093 Get PCD database from GUID HOB in PEI phase.
1094
1095 @return Pointer to PCD database.
1096
1097 **/
1098 PEI_PCD_DATABASE *
1099 GetPcdDatabase (
1100 VOID
1101 )
1102 {
1103 EFI_HOB_GUID_TYPE *GuidHob;
1104
1105 GuidHob = GetFirstGuidHob (&gPcdDataBaseHobGuid);
1106 ASSERT (GuidHob != NULL);
1107
1108 return (PEI_PCD_DATABASE *) GET_GUID_HOB_DATA (GuidHob);
1109 }
1110
1111 /**
1112 Get SKU ID table from PCD database.
1113
1114 @param LocalTokenNumberTableIdx Index of local token number in token number table.
1115 @param Database PCD database.
1116
1117 @return Pointer to SKU ID array table
1118
1119 **/
1120 SKU_ID *
1121 GetSkuIdArray (
1122 IN UINTN LocalTokenNumberTableIdx,
1123 IN PEI_PCD_DATABASE *Database
1124 )
1125 {
1126 SKU_HEAD *SkuHead;
1127 UINTN LocalTokenNumber;
1128
1129 LocalTokenNumber = *((UINT32 *)((UINT8 *)Database + Database->LocalTokenNumberTableOffset) + LocalTokenNumberTableIdx);
1130
1131 ASSERT ((LocalTokenNumber & PCD_TYPE_SKU_ENABLED) != 0);
1132
1133 SkuHead = (SKU_HEAD *) ((UINT8 *)Database + (LocalTokenNumber & PCD_DATABASE_OFFSET_MASK));
1134
1135 return (SKU_ID *) ((UINT8 *)Database + SkuHead->SkuIdTableOffset);
1136
1137 }
1138
1139 /**
1140 Get index of PCD entry in size table.
1141
1142 @param LocalTokenNumberTableIdx Index of this PCD in local token number table.
1143 @param Database Pointer to PCD database in PEI phase.
1144
1145 @return index of PCD entry in size table.
1146
1147 **/
1148 UINTN
1149 GetSizeTableIndex (
1150 IN UINTN LocalTokenNumberTableIdx,
1151 IN PEI_PCD_DATABASE *Database
1152 )
1153 {
1154 UINTN Index;
1155 UINTN SizeTableIdx;
1156 UINTN LocalTokenNumber;
1157 SKU_ID *SkuIdTable;
1158
1159 SizeTableIdx = 0;
1160
1161 for (Index = 0; Index < LocalTokenNumberTableIdx; Index++) {
1162 LocalTokenNumber = *((UINT32 *)((UINT8 *)Database + Database->LocalTokenNumberTableOffset) + Index);
1163
1164 if ((LocalTokenNumber & PCD_DATUM_TYPE_ALL_SET) == PCD_DATUM_TYPE_POINTER) {
1165 //
1166 // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type
1167 // PCD entry.
1168 //
1169 if ((LocalTokenNumber & PCD_TYPE_VPD) != 0) {
1170 //
1171 // We have only two entry for VPD enabled PCD entry:
1172 // 1) MAX Size.
1173 // 2) Current Size
1174 // Current size is equal to MAX size.
1175 //
1176 SizeTableIdx += 2;
1177 } else {
1178 if ((LocalTokenNumber & PCD_TYPE_SKU_ENABLED) == 0) {
1179 //
1180 // We have only two entry for Non-Sku enabled PCD entry:
1181 // 1) MAX SIZE
1182 // 2) Current Size
1183 //
1184 SizeTableIdx += 2;
1185 } else {
1186 //
1187 // We have these entry for SKU enabled PCD entry
1188 // 1) MAX SIZE
1189 // 2) Current Size for each SKU_ID (It is equal to MaxSku).
1190 //
1191 SkuIdTable = GetSkuIdArray (Index, Database);
1192 SizeTableIdx += (UINTN)*SkuIdTable + 1;
1193 }
1194 }
1195 }
1196
1197 }
1198
1199 return SizeTableIdx;
1200 }