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