Add in GetNextToken and Register Callback Function funtionality for DXE 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
52e1905d 258 EX_PCD_ENTRY_ATTRIBUTE Attr;\r
259\r
260 GetExPcdTokenAttributes (Guid, ExTokenNumber, &Attr);\r
261 \r
262 return Attr.Size;\r
878ddf1f 263}\r
264\r
265\r
266\r
267EFI_STATUS\r
268EFIAPI\r
269PeiPcdSet8 (\r
00b7af13 270 IN PCD_TOKEN_NUMBER TokenNumber,\r
878ddf1f 271 IN UINT8 Value\r
272 )\r
273{\r
52e1905d 274 return SetWorker (TokenNumber, &Value, sizeof (Value), FALSE);\r
878ddf1f 275}\r
276\r
277\r
278\r
279EFI_STATUS\r
280EFIAPI\r
281PeiPcdSet16 (\r
00b7af13 282 IN PCD_TOKEN_NUMBER TokenNumber,\r
878ddf1f 283 IN UINT16 Value\r
284 )\r
285{\r
52e1905d 286 return SetWorker (TokenNumber, &Value, sizeof (Value), FALSE);\r
878ddf1f 287}\r
288\r
289\r
290\r
291EFI_STATUS\r
292EFIAPI\r
293PeiPcdSet32 (\r
00b7af13 294 IN PCD_TOKEN_NUMBER TokenNumber,\r
878ddf1f 295 IN UINT32 Value\r
296 )\r
297{\r
52e1905d 298 return SetWorker (TokenNumber, &Value, sizeof (Value), FALSE);\r
878ddf1f 299}\r
300\r
301\r
302\r
303EFI_STATUS\r
304EFIAPI\r
305PeiPcdSet64 (\r
00b7af13 306 IN PCD_TOKEN_NUMBER TokenNumber,\r
878ddf1f 307 IN UINT64 Value\r
308 )\r
309{\r
52e1905d 310 return SetWorker (TokenNumber, &Value, sizeof (Value), FALSE);\r
878ddf1f 311}\r
312\r
313\r
314EFI_STATUS\r
315EFIAPI\r
316PeiPcdSetPtr (\r
00b7af13 317 IN PCD_TOKEN_NUMBER TokenNumber,\r
318 IN UINTN SizeOfBuffer,\r
319 IN VOID *Buffer\r
878ddf1f 320 )\r
321{\r
00b7af13 322 return SetWorker (TokenNumber, Buffer, SizeOfBuffer, TRUE);\r
878ddf1f 323}\r
324\r
325\r
326\r
327EFI_STATUS\r
328EFIAPI\r
329PeiPcdSetBool (\r
00b7af13 330 IN PCD_TOKEN_NUMBER TokenNumber,\r
878ddf1f 331 IN BOOLEAN Value\r
332 )\r
333{\r
52e1905d 334 return SetWorker (TokenNumber, &Value, sizeof (Value), FALSE);\r
878ddf1f 335}\r
336\r
337\r
338\r
339EFI_STATUS\r
340EFIAPI\r
341PeiPcdSet8Ex (\r
342 IN CONST EFI_GUID *Guid,\r
00b7af13 343 IN PCD_TOKEN_NUMBER ExTokenNumber,\r
878ddf1f 344 IN UINT8 Value\r
345 )\r
346{\r
52e1905d 347 return ExSetWorker(\r
348 ExTokenNumber, \r
349 Guid,\r
350 &Value, \r
351 sizeof (Value), \r
352 FALSE\r
353 );\r
878ddf1f 354}\r
355\r
356\r
357\r
358EFI_STATUS\r
359EFIAPI\r
360PeiPcdSet16Ex (\r
361 IN CONST EFI_GUID *Guid,\r
00b7af13 362 IN PCD_TOKEN_NUMBER ExTokenNumber,\r
878ddf1f 363 IN UINT16 Value\r
364 )\r
365{\r
52e1905d 366 return ExSetWorker(\r
367 ExTokenNumber, \r
368 Guid,\r
369 &Value, \r
370 sizeof (Value), \r
371 FALSE\r
372 );\r
878ddf1f 373}\r
374\r
375\r
376\r
377EFI_STATUS\r
378EFIAPI\r
379PeiPcdSet32Ex (\r
380 IN CONST EFI_GUID *Guid,\r
00b7af13 381 IN PCD_TOKEN_NUMBER ExTokenNumber,\r
878ddf1f 382 IN UINT32 Value\r
383 )\r
384{\r
52e1905d 385 return ExSetWorker(\r
386 ExTokenNumber, \r
387 Guid,\r
388 &Value, \r
389 sizeof (Value), \r
390 FALSE\r
391 );\r
878ddf1f 392}\r
393\r
394\r
395\r
396EFI_STATUS\r
397EFIAPI\r
398PeiPcdSet64Ex (\r
399 IN CONST EFI_GUID *Guid,\r
00b7af13 400 IN PCD_TOKEN_NUMBER ExTokenNumber,\r
878ddf1f 401 IN UINT64 Value\r
402 )\r
403{\r
52e1905d 404 return ExSetWorker(\r
405 ExTokenNumber, \r
406 Guid,\r
407 &Value, \r
408 sizeof (Value), \r
409 FALSE\r
410 );\r
878ddf1f 411}\r
412\r
413\r
414\r
415EFI_STATUS\r
416EFIAPI\r
417PeiPcdSetPtrEx (\r
418 IN CONST EFI_GUID *Guid,\r
00b7af13 419 IN PCD_TOKEN_NUMBER ExTokenNumber,\r
420 IN UINTN SizeOfBuffer,\r
421 IN VOID *Value\r
878ddf1f 422 )\r
423{\r
52e1905d 424 return ExSetWorker(\r
425 ExTokenNumber, \r
426 Guid,\r
00b7af13 427 Value, \r
428 SizeOfBuffer, \r
52e1905d 429 TRUE\r
430 );\r
878ddf1f 431}\r
432\r
433\r
434\r
435EFI_STATUS\r
436EFIAPI\r
437PeiPcdSetBoolEx (\r
438 IN CONST EFI_GUID *Guid,\r
00b7af13 439 IN PCD_TOKEN_NUMBER ExTokenNumber,\r
878ddf1f 440 IN BOOLEAN Value\r
441 )\r
442{\r
52e1905d 443 return ExSetWorker(\r
444 ExTokenNumber, \r
445 Guid,\r
446 &Value, \r
447 sizeof (Value), \r
448 FALSE\r
449 );\r
878ddf1f 450}\r
451\r
452\r
453\r
454\r
455EFI_STATUS\r
456EFIAPI\r
4c114006 457PeiRegisterCallBackOnSet (\r
00b7af13 458 IN PCD_TOKEN_NUMBER ExTokenNumber,\r
878ddf1f 459 IN CONST EFI_GUID *Guid, OPTIONAL\r
460 IN PCD_PPI_CALLBACK CallBackFunction\r
461 )\r
462{\r
4c114006 463 ASSERT (CallBackFunction != NULL);\r
464 \r
52e1905d 465 return PeiRegisterCallBackWorker (ExTokenNumber, Guid, CallBackFunction, TRUE);\r
878ddf1f 466}\r
467\r
468\r
469\r
470EFI_STATUS\r
471EFIAPI\r
472PcdUnRegisterCallBackOnSet (\r
00b7af13 473 IN PCD_TOKEN_NUMBER ExTokenNumber,\r
878ddf1f 474 IN CONST EFI_GUID *Guid, OPTIONAL\r
475 IN PCD_PPI_CALLBACK CallBackFunction\r
476 )\r
477{\r
4c114006 478 ASSERT (CallBackFunction != NULL);\r
479 \r
52e1905d 480 return PeiRegisterCallBackWorker (ExTokenNumber, Guid, CallBackFunction, FALSE);\r
878ddf1f 481}\r
482\r
483\r
484\r
485EFI_STATUS\r
486EFIAPI\r
487PeiPcdGetNextToken (\r
488 IN CONST EFI_GUID *Guid, OPTIONAL\r
4c114006 489 IN OUT PCD_TOKEN_NUMBER *TokenNumber\r
878ddf1f 490 )\r
491{\r
00b7af13 492 UINTN GuidTableIdx;\r
493 PEI_PCD_DATABASE *PeiPcdDb;\r
494 EFI_GUID *MatchGuid;\r
495 DYNAMICEX_MAPPING *ExMapTable;\r
496 UINTN i;\r
497 BOOLEAN Found;\r
498 \r
52e1905d 499 if (Guid == NULL) {\r
b560bb65 500 (*TokenNumber)++;\r
52e1905d 501\r
00b7af13 502 if (*TokenNumber >= PEI_NEX_TOKEN_NUMBER) {\r
52e1905d 503 *TokenNumber = 0;\r
504 }\r
00b7af13 505 \r
506 } else {\r
507\r
508 if (PEI_EXMAP_TABLE_EMPTY) {\r
509 *TokenNumber = (UINTN) PCD_INVALID_TOKEN_NUMBER;\r
510 return EFI_NOT_FOUND;\r
511 }\r
512 \r
513 //\r
514 // Assume PCD Database AutoGen tool is sorting the ExMap based on the following order\r
515 // 1) ExGuid\r
516 // 2) ExTokenNumber\r
517 //\r
518 PeiPcdDb = GetPcdDatabase ();\r
519 \r
520 MatchGuid = ScanGuid (PeiPcdDb->Init.GuidTable, sizeof(PeiPcdDb->Init.GuidTable), Guid);\r
521\r
522 if (MatchGuid == NULL) {\r
523 *TokenNumber = (UINTN) PCD_INVALID_TOKEN_NUMBER;\r
524 return EFI_NOT_FOUND;\r
525 }\r
526\r
527 GuidTableIdx = MatchGuid - PeiPcdDb->Init.GuidTable;\r
528\r
529 ExMapTable = PeiPcdDb->Init.ExMapTable;\r
530\r
531 Found = FALSE;\r
532 for (i = 0; i < PEI_EXMAPPING_TABLE_SIZE; i++) {\r
533 if (ExMapTable[i].ExGuidIndex == GuidTableIdx) {\r
534 Found = TRUE;\r
535 break;\r
536 }\r
537 }\r
538\r
539 if (Found) {\r
540 if (*TokenNumber == PCD_INVALID_TOKEN_NUMBER) {\r
541 *TokenNumber = ExMapTable[i].ExTokenNumber;\r
542 return EFI_SUCCESS;\r
543 }\r
544 \r
545 for ( ; ExMapTable[i].ExGuidIndex == GuidTableIdx; i++) {\r
546 if (ExMapTable[i].ExTokenNumber == *TokenNumber) {\r
547 i++;\r
548 if (ExMapTable[i].ExGuidIndex == GuidTableIdx) {\r
549 *TokenNumber = ExMapTable[i].ExTokenNumber;\r
550 return EFI_SUCCESS;\r
551 } else {\r
552 *TokenNumber = (UINTN) PCD_INVALID_TOKEN_NUMBER;\r
553 return EFI_SUCCESS;\r
554 }\r
555 }\r
556 }\r
557\r
558 return EFI_NOT_FOUND;\r
559 }\r
560 \r
561 }\r
562\r
563 return EFI_SUCCESS;\r
564}\r
565\r
566EFI_GUID *\r
567EFIAPI\r
568PeiPcdGetNextTokenSpaceGuid (\r
569 IN CONST EFI_GUID *Guid\r
570 )\r
571{\r
572 UINTN GuidTableIdx;\r
573 EFI_GUID *MatchGuid;\r
574 PEI_PCD_DATABASE *PeiPcdDb;\r
575 DYNAMICEX_MAPPING *ExMapTable;\r
576 UINTN i;\r
577 BOOLEAN Found;\r
578\r
579 if (PEI_EXMAP_TABLE_EMPTY) {\r
580 return NULL;\r
52e1905d 581 }\r
582\r
583 //\r
00b7af13 584 // Assume PCD Database AutoGen tool is sorting the ExMap based on the following order\r
585 // 1) ExGuid\r
586 // 2) ExTokenNumber\r
52e1905d 587 //\r
00b7af13 588 PeiPcdDb = GetPcdDatabase ();\r
52e1905d 589\r
00b7af13 590 MatchGuid = ScanGuid (PeiPcdDb->Init.GuidTable, sizeof(PeiPcdDb->Init.GuidTable), Guid);\r
591\r
592 if (MatchGuid == NULL) {\r
593 return NULL;\r
594 }\r
595 \r
596 GuidTableIdx = MatchGuid - PeiPcdDb->Init.GuidTable;\r
878ddf1f 597\r
00b7af13 598 ExMapTable = PeiPcdDb->Init.ExMapTable;\r
599\r
600 Found = FALSE;\r
601 for (i = 0; i < PEI_EXMAPPING_TABLE_SIZE; i++) {\r
602 if (ExMapTable[i].ExGuidIndex == GuidTableIdx) {\r
603 Found = TRUE;\r
604 break;\r
605 }\r
606 }\r
607\r
608 if (Found) {\r
609 for ( ; i < PEI_EXMAPPING_TABLE_SIZE; i++ ) {\r
610 if (ExMapTable[i].ExGuidIndex != GuidTableIdx ) {\r
611 if (i < PEI_EXMAPPING_TABLE_SIZE) {\r
612 return &PeiPcdDb->Init.GuidTable[ExMapTable[i].ExGuidIndex];\r
613 } else {\r
614 return NULL;\r
615 }\r
616 }\r
617 }\r
618 }\r
619\r
620 return NULL;\r
621\r
622}\r
878ddf1f 623\r