Refine some internal functions of PCD Driver.
[mirror_edk2.git] / EdkModulePkg / Universal / PCD / Pei / Pcd.c
CommitLineData
52e1905d 1/** @file PCD PEIM\r
878ddf1f 2\r
3Copyright (c) 2006, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12\r
13Module Name: Pcd.c\r
14\r
15**/\r
16\r
878ddf1f 17#include "Service.h"\r
18\r
19\r
20PCD_PPI mPcdPpiInstance = {\r
21 PeiPcdSetSku,\r
22\r
23 PeiPcdGet8,\r
24 PeiPcdGet16, \r
25 PeiPcdGet32, \r
26 PeiPcdGet64, \r
27 PeiPcdGetPtr, \r
28 PeiPcdGetBool, \r
29 PeiPcdGetSize,\r
30\r
31 PeiPcdGet8Ex,\r
32 PeiPcdGet16Ex, \r
33 PeiPcdGet32Ex, \r
34 PeiPcdGet64Ex, \r
35 PeiPcdGetPtrEx, \r
36 PeiPcdGetBoolEx, \r
37 PeiPcdGetSizeEx,\r
38 \r
39 PeiPcdSet8,\r
40 PeiPcdSet16, \r
41 PeiPcdSet32, \r
42 PeiPcdSet64, \r
43 PeiPcdSetPtr, \r
44 PeiPcdSetBool, \r
45\r
46 PeiPcdSet8Ex,\r
47 PeiPcdSet16Ex, \r
48 PeiPcdSet32Ex, \r
49 PeiPcdSet64Ex, \r
50 PeiPcdSetPtrEx, \r
51 PeiPcdSetBoolEx,\r
52\r
4c114006 53 PeiRegisterCallBackOnSet,\r
878ddf1f 54 PcdUnRegisterCallBackOnSet,\r
55 PeiPcdGetNextToken\r
56};\r
57\r
58\r
59\r
60STATIC EFI_PEI_PPI_DESCRIPTOR mPpiPCD = {\r
61 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
62 &gPcdPpiGuid,\r
63 &mPcdPpiInstance\r
64};\r
65\r
66\r
67\r
68EFI_STATUS\r
69EFIAPI\r
70PcdPeimInit (\r
71 IN EFI_FFS_FILE_HEADER *FfsHeader,\r
72 IN EFI_PEI_SERVICES **PeiServices\r
73 )\r
74{\r
75 EFI_STATUS Status;\r
878ddf1f 76\r
52e1905d 77 BuildPcdDatabase ();\r
78 \r
878ddf1f 79 Status = PeiCoreInstallPpi (&mPpiPCD);\r
80\r
81 ASSERT_EFI_ERROR (Status);\r
82 \r
83 return EFI_SUCCESS;\r
84}\r
85\r
00b7af13 86VOID\r
878ddf1f 87EFIAPI\r
88PeiPcdSetSku (\r
00b7af13 89 IN SKU_ID SkuId\r
878ddf1f 90 )\r
91{\r
878ddf1f 92\r
00b7af13 93 GetPcdDatabase()->Init.SystemSkuId = SkuId;\r
878ddf1f 94\r
00b7af13 95 return;\r
878ddf1f 96}\r
97\r
98\r
99\r
100UINT8\r
101EFIAPI\r
102PeiPcdGet8 (\r
00b7af13 103 IN PCD_TOKEN_NUMBER TokenNumber\r
878ddf1f 104 )\r
105{\r
52e1905d 106 return *((UINT8 *) GetWorker (TokenNumber, sizeof (UINT8)));\r
878ddf1f 107}\r
108\r
109\r
110\r
111UINT16\r
112EFIAPI\r
113PeiPcdGet16 (\r
00b7af13 114 IN PCD_TOKEN_NUMBER TokenNumber\r
878ddf1f 115 )\r
116{\r
52e1905d 117 return ReadUnaligned16 (GetWorker (TokenNumber, sizeof (UINT16)));\r
878ddf1f 118}\r
119\r
120\r
121\r
122UINT32\r
123EFIAPI\r
124PeiPcdGet32 (\r
00b7af13 125 IN PCD_TOKEN_NUMBER TokenNumber\r
878ddf1f 126 )\r
127{\r
52e1905d 128 return ReadUnaligned32 (GetWorker (TokenNumber, sizeof (UINT32)));\r
878ddf1f 129}\r
130\r
131\r
132\r
133UINT64\r
134EFIAPI\r
135PeiPcdGet64 (\r
00b7af13 136 IN PCD_TOKEN_NUMBER TokenNumber\r
878ddf1f 137 )\r
138{\r
52e1905d 139 return ReadUnaligned64 (GetWorker (TokenNumber, sizeof (UINT64)));\r
878ddf1f 140}\r
141\r
142\r
143\r
144VOID *\r
145EFIAPI\r
146PeiPcdGetPtr (\r
00b7af13 147 IN PCD_TOKEN_NUMBER TokenNumber\r
878ddf1f 148 )\r
149{\r
52e1905d 150 return GetWorker (TokenNumber, 0);\r
878ddf1f 151}\r
152\r
153\r
154\r
155BOOLEAN\r
156EFIAPI\r
157PeiPcdGetBool (\r
00b7af13 158 IN PCD_TOKEN_NUMBER TokenNumber\r
878ddf1f 159 )\r
160{\r
52e1905d 161 return *((BOOLEAN *) GetWorker (TokenNumber, sizeof (BOOLEAN)));\r
878ddf1f 162}\r
163\r
164\r
165\r
166UINTN\r
167EFIAPI\r
168PeiPcdGetSize (\r
00b7af13 169 IN PCD_TOKEN_NUMBER TokenNumber\r
878ddf1f 170 )\r
171{\r
52e1905d 172 ASSERT (TokenNumber < PEI_LOCAL_TOKEN_NUMBER);\r
173\r
174 return GetPcdDatabase()->Init.SizeTable[TokenNumber];\r
878ddf1f 175}\r
176\r
177\r
178\r
179UINT8\r
180EFIAPI\r
181PeiPcdGet8Ex (\r
182 IN CONST EFI_GUID *Guid,\r
00b7af13 183 IN PCD_TOKEN_NUMBER ExTokenNumber\r
878ddf1f 184 )\r
185{\r
52e1905d 186 return *((UINT8 *) ExGetWorker (Guid, ExTokenNumber, sizeof (UINT8)));\r
878ddf1f 187}\r
188\r
189\r
190\r
191UINT16\r
192EFIAPI\r
193PeiPcdGet16Ex (\r
194 IN CONST EFI_GUID *Guid,\r
00b7af13 195 IN PCD_TOKEN_NUMBER ExTokenNumber\r
878ddf1f 196 )\r
197{\r
52e1905d 198 return ReadUnaligned16 (ExGetWorker (Guid, ExTokenNumber, sizeof (UINT16)));\r
878ddf1f 199}\r
200\r
201\r
202\r
203UINT32\r
204EFIAPI\r
205PeiPcdGet32Ex (\r
206 IN CONST EFI_GUID *Guid,\r
00b7af13 207 IN PCD_TOKEN_NUMBER ExTokenNumber\r
878ddf1f 208 )\r
209{\r
52e1905d 210 return ReadUnaligned32 (ExGetWorker (Guid, ExTokenNumber, sizeof (UINT32)));\r
878ddf1f 211}\r
212\r
213\r
214\r
215UINT64\r
216EFIAPI\r
217PeiPcdGet64Ex (\r
218 IN CONST EFI_GUID *Guid,\r
00b7af13 219 IN PCD_TOKEN_NUMBER ExTokenNumber\r
878ddf1f 220 )\r
221{\r
52e1905d 222 return ReadUnaligned64 (ExGetWorker (Guid, ExTokenNumber, sizeof (UINT64)));\r
878ddf1f 223}\r
224\r
225\r
226\r
227VOID *\r
228EFIAPI\r
229PeiPcdGetPtrEx (\r
230 IN CONST EFI_GUID *Guid,\r
00b7af13 231 IN PCD_TOKEN_NUMBER ExTokenNumber\r
878ddf1f 232 )\r
233{\r
52e1905d 234 return ExGetWorker (Guid, ExTokenNumber, 0);\r
878ddf1f 235}\r
236\r
237\r
238\r
239BOOLEAN\r
240EFIAPI\r
241PeiPcdGetBoolEx (\r
242 IN CONST EFI_GUID *Guid,\r
00b7af13 243 IN PCD_TOKEN_NUMBER ExTokenNumber\r
878ddf1f 244 )\r
245{\r
52e1905d 246 return *((BOOLEAN *) ExGetWorker (Guid, ExTokenNumber, sizeof (BOOLEAN)));\r
878ddf1f 247}\r
248\r
249\r
250\r
251UINTN\r
252EFIAPI\r
253PeiPcdGetSizeEx (\r
254 IN CONST EFI_GUID *Guid,\r
00b7af13 255 IN PCD_TOKEN_NUMBER ExTokenNumber\r
878ddf1f 256 )\r
257{\r
9d6d8b24 258 return PeiPcdGetSize (GetExPcdTokenNumber (Guid, ExTokenNumber));\r
878ddf1f 259}\r
260\r
261\r
262\r
263EFI_STATUS\r
264EFIAPI\r
265PeiPcdSet8 (\r
00b7af13 266 IN PCD_TOKEN_NUMBER TokenNumber,\r
878ddf1f 267 IN UINT8 Value\r
268 )\r
269{\r
52e1905d 270 return SetWorker (TokenNumber, &Value, sizeof (Value), FALSE);\r
878ddf1f 271}\r
272\r
273\r
274\r
275EFI_STATUS\r
276EFIAPI\r
277PeiPcdSet16 (\r
00b7af13 278 IN PCD_TOKEN_NUMBER TokenNumber,\r
878ddf1f 279 IN UINT16 Value\r
280 )\r
281{\r
52e1905d 282 return SetWorker (TokenNumber, &Value, sizeof (Value), FALSE);\r
878ddf1f 283}\r
284\r
285\r
286\r
287EFI_STATUS\r
288EFIAPI\r
289PeiPcdSet32 (\r
00b7af13 290 IN PCD_TOKEN_NUMBER TokenNumber,\r
878ddf1f 291 IN UINT32 Value\r
292 )\r
293{\r
52e1905d 294 return SetWorker (TokenNumber, &Value, sizeof (Value), FALSE);\r
878ddf1f 295}\r
296\r
297\r
298\r
299EFI_STATUS\r
300EFIAPI\r
301PeiPcdSet64 (\r
00b7af13 302 IN PCD_TOKEN_NUMBER TokenNumber,\r
878ddf1f 303 IN UINT64 Value\r
304 )\r
305{\r
52e1905d 306 return SetWorker (TokenNumber, &Value, sizeof (Value), FALSE);\r
878ddf1f 307}\r
308\r
309\r
310EFI_STATUS\r
311EFIAPI\r
312PeiPcdSetPtr (\r
00b7af13 313 IN PCD_TOKEN_NUMBER TokenNumber,\r
314 IN UINTN SizeOfBuffer,\r
315 IN VOID *Buffer\r
878ddf1f 316 )\r
317{\r
00b7af13 318 return SetWorker (TokenNumber, Buffer, SizeOfBuffer, TRUE);\r
878ddf1f 319}\r
320\r
321\r
322\r
323EFI_STATUS\r
324EFIAPI\r
325PeiPcdSetBool (\r
00b7af13 326 IN PCD_TOKEN_NUMBER TokenNumber,\r
878ddf1f 327 IN BOOLEAN Value\r
328 )\r
329{\r
52e1905d 330 return SetWorker (TokenNumber, &Value, sizeof (Value), FALSE);\r
878ddf1f 331}\r
332\r
333\r
334\r
335EFI_STATUS\r
336EFIAPI\r
337PeiPcdSet8Ex (\r
338 IN CONST EFI_GUID *Guid,\r
00b7af13 339 IN PCD_TOKEN_NUMBER ExTokenNumber,\r
878ddf1f 340 IN UINT8 Value\r
341 )\r
342{\r
52e1905d 343 return ExSetWorker(\r
344 ExTokenNumber, \r
345 Guid,\r
346 &Value, \r
347 sizeof (Value), \r
348 FALSE\r
349 );\r
878ddf1f 350}\r
351\r
352\r
353\r
354EFI_STATUS\r
355EFIAPI\r
356PeiPcdSet16Ex (\r
357 IN CONST EFI_GUID *Guid,\r
00b7af13 358 IN PCD_TOKEN_NUMBER ExTokenNumber,\r
878ddf1f 359 IN UINT16 Value\r
360 )\r
361{\r
52e1905d 362 return ExSetWorker(\r
363 ExTokenNumber, \r
364 Guid,\r
365 &Value, \r
366 sizeof (Value), \r
367 FALSE\r
368 );\r
878ddf1f 369}\r
370\r
371\r
372\r
373EFI_STATUS\r
374EFIAPI\r
375PeiPcdSet32Ex (\r
376 IN CONST EFI_GUID *Guid,\r
00b7af13 377 IN PCD_TOKEN_NUMBER ExTokenNumber,\r
878ddf1f 378 IN UINT32 Value\r
379 )\r
380{\r
52e1905d 381 return ExSetWorker(\r
382 ExTokenNumber, \r
383 Guid,\r
384 &Value, \r
385 sizeof (Value), \r
386 FALSE\r
387 );\r
878ddf1f 388}\r
389\r
390\r
391\r
392EFI_STATUS\r
393EFIAPI\r
394PeiPcdSet64Ex (\r
395 IN CONST EFI_GUID *Guid,\r
00b7af13 396 IN PCD_TOKEN_NUMBER ExTokenNumber,\r
878ddf1f 397 IN UINT64 Value\r
398 )\r
399{\r
52e1905d 400 return ExSetWorker(\r
401 ExTokenNumber, \r
402 Guid,\r
403 &Value, \r
404 sizeof (Value), \r
405 FALSE\r
406 );\r
878ddf1f 407}\r
408\r
409\r
410\r
411EFI_STATUS\r
412EFIAPI\r
413PeiPcdSetPtrEx (\r
414 IN CONST EFI_GUID *Guid,\r
00b7af13 415 IN PCD_TOKEN_NUMBER ExTokenNumber,\r
416 IN UINTN SizeOfBuffer,\r
417 IN VOID *Value\r
878ddf1f 418 )\r
419{\r
52e1905d 420 return ExSetWorker(\r
421 ExTokenNumber, \r
422 Guid,\r
00b7af13 423 Value, \r
424 SizeOfBuffer, \r
52e1905d 425 TRUE\r
426 );\r
878ddf1f 427}\r
428\r
429\r
430\r
431EFI_STATUS\r
432EFIAPI\r
433PeiPcdSetBoolEx (\r
434 IN CONST EFI_GUID *Guid,\r
00b7af13 435 IN PCD_TOKEN_NUMBER ExTokenNumber,\r
878ddf1f 436 IN BOOLEAN Value\r
437 )\r
438{\r
52e1905d 439 return ExSetWorker(\r
440 ExTokenNumber, \r
441 Guid,\r
442 &Value, \r
443 sizeof (Value), \r
444 FALSE\r
445 );\r
878ddf1f 446}\r
447\r
448\r
449\r
450\r
451EFI_STATUS\r
452EFIAPI\r
4c114006 453PeiRegisterCallBackOnSet (\r
00b7af13 454 IN PCD_TOKEN_NUMBER ExTokenNumber,\r
878ddf1f 455 IN CONST EFI_GUID *Guid, OPTIONAL\r
456 IN PCD_PPI_CALLBACK CallBackFunction\r
457 )\r
458{\r
4c114006 459 ASSERT (CallBackFunction != NULL);\r
460 \r
52e1905d 461 return PeiRegisterCallBackWorker (ExTokenNumber, Guid, CallBackFunction, TRUE);\r
878ddf1f 462}\r
463\r
464\r
465\r
466EFI_STATUS\r
467EFIAPI\r
468PcdUnRegisterCallBackOnSet (\r
00b7af13 469 IN PCD_TOKEN_NUMBER ExTokenNumber,\r
878ddf1f 470 IN CONST EFI_GUID *Guid, OPTIONAL\r
471 IN PCD_PPI_CALLBACK CallBackFunction\r
472 )\r
473{\r
4c114006 474 ASSERT (CallBackFunction != NULL);\r
475 \r
52e1905d 476 return PeiRegisterCallBackWorker (ExTokenNumber, Guid, CallBackFunction, FALSE);\r
878ddf1f 477}\r
478\r
479\r
480\r
481EFI_STATUS\r
482EFIAPI\r
483PeiPcdGetNextToken (\r
484 IN CONST EFI_GUID *Guid, OPTIONAL\r
4c114006 485 IN OUT PCD_TOKEN_NUMBER *TokenNumber\r
878ddf1f 486 )\r
487{\r
00b7af13 488 UINTN GuidTableIdx;\r
489 PEI_PCD_DATABASE *PeiPcdDb;\r
490 EFI_GUID *MatchGuid;\r
491 DYNAMICEX_MAPPING *ExMapTable;\r
492 UINTN i;\r
493 BOOLEAN Found;\r
494 \r
52e1905d 495 if (Guid == NULL) {\r
b560bb65 496 (*TokenNumber)++;\r
52e1905d 497\r
00b7af13 498 if (*TokenNumber >= PEI_NEX_TOKEN_NUMBER) {\r
0653eb89 499 *TokenNumber = PCD_INVALID_TOKEN_NUMBER;\r
52e1905d 500 }\r
00b7af13 501 \r
502 } else {\r
503\r
504 if (PEI_EXMAP_TABLE_EMPTY) {\r
0653eb89 505 *TokenNumber = PCD_INVALID_TOKEN_NUMBER;\r
00b7af13 506 return EFI_NOT_FOUND;\r
507 }\r
508 \r
509 //\r
510 // Assume PCD Database AutoGen tool is sorting the ExMap based on the following order\r
511 // 1) ExGuid\r
512 // 2) ExTokenNumber\r
513 //\r
514 PeiPcdDb = GetPcdDatabase ();\r
515 \r
516 MatchGuid = ScanGuid (PeiPcdDb->Init.GuidTable, sizeof(PeiPcdDb->Init.GuidTable), Guid);\r
517\r
518 if (MatchGuid == NULL) {\r
519 *TokenNumber = (UINTN) PCD_INVALID_TOKEN_NUMBER;\r
520 return EFI_NOT_FOUND;\r
521 }\r
522\r
523 GuidTableIdx = MatchGuid - PeiPcdDb->Init.GuidTable;\r
524\r
525 ExMapTable = PeiPcdDb->Init.ExMapTable;\r
526\r
527 Found = FALSE;\r
528 for (i = 0; i < PEI_EXMAPPING_TABLE_SIZE; i++) {\r
529 if (ExMapTable[i].ExGuidIndex == GuidTableIdx) {\r
530 Found = TRUE;\r
531 break;\r
532 }\r
533 }\r
534\r
535 if (Found) {\r
536 if (*TokenNumber == PCD_INVALID_TOKEN_NUMBER) {\r
537 *TokenNumber = ExMapTable[i].ExTokenNumber;\r
538 return EFI_SUCCESS;\r
539 }\r
540 \r
541 for ( ; ExMapTable[i].ExGuidIndex == GuidTableIdx; i++) {\r
542 if (ExMapTable[i].ExTokenNumber == *TokenNumber) {\r
543 i++;\r
544 if (ExMapTable[i].ExGuidIndex == GuidTableIdx) {\r
545 *TokenNumber = ExMapTable[i].ExTokenNumber;\r
546 return EFI_SUCCESS;\r
547 } else {\r
548 *TokenNumber = (UINTN) PCD_INVALID_TOKEN_NUMBER;\r
549 return EFI_SUCCESS;\r
550 }\r
551 }\r
552 }\r
553\r
554 return EFI_NOT_FOUND;\r
555 }\r
556 \r
557 }\r
558\r
559 return EFI_SUCCESS;\r
560}\r
561\r
562EFI_GUID *\r
563EFIAPI\r
564PeiPcdGetNextTokenSpaceGuid (\r
565 IN CONST EFI_GUID *Guid\r
566 )\r
567{\r
568 UINTN GuidTableIdx;\r
569 EFI_GUID *MatchGuid;\r
570 PEI_PCD_DATABASE *PeiPcdDb;\r
571 DYNAMICEX_MAPPING *ExMapTable;\r
572 UINTN i;\r
573 BOOLEAN Found;\r
574\r
575 if (PEI_EXMAP_TABLE_EMPTY) {\r
576 return NULL;\r
52e1905d 577 }\r
578\r
579 //\r
00b7af13 580 // Assume PCD Database AutoGen tool is sorting the ExMap based on the following order\r
581 // 1) ExGuid\r
582 // 2) ExTokenNumber\r
52e1905d 583 //\r
00b7af13 584 PeiPcdDb = GetPcdDatabase ();\r
52e1905d 585\r
00b7af13 586 MatchGuid = ScanGuid (PeiPcdDb->Init.GuidTable, sizeof(PeiPcdDb->Init.GuidTable), Guid);\r
587\r
588 if (MatchGuid == NULL) {\r
589 return NULL;\r
590 }\r
591 \r
592 GuidTableIdx = MatchGuid - PeiPcdDb->Init.GuidTable;\r
878ddf1f 593\r
00b7af13 594 ExMapTable = PeiPcdDb->Init.ExMapTable;\r
595\r
596 Found = FALSE;\r
597 for (i = 0; i < PEI_EXMAPPING_TABLE_SIZE; i++) {\r
598 if (ExMapTable[i].ExGuidIndex == GuidTableIdx) {\r
599 Found = TRUE;\r
600 break;\r
601 }\r
602 }\r
603\r
604 if (Found) {\r
605 for ( ; i < PEI_EXMAPPING_TABLE_SIZE; i++ ) {\r
606 if (ExMapTable[i].ExGuidIndex != GuidTableIdx ) {\r
607 if (i < PEI_EXMAPPING_TABLE_SIZE) {\r
608 return &PeiPcdDb->Init.GuidTable[ExMapTable[i].ExGuidIndex];\r
609 } else {\r
610 return NULL;\r
611 }\r
612 }\r
613 }\r
614 }\r
615\r
616 return NULL;\r
617\r
618}\r
878ddf1f 619\r