]> git.proxmox.com Git - mirror_edk2.git/blob - EdkNt32Pkg/Pei/PcdEmulator/PcdEmulator.c
1. Rename PeiCoreLib to PeiServicesLib and rename all the interfaces from PeiCoreXXX...
[mirror_edk2.git] / EdkNt32Pkg / Pei / PcdEmulator / PcdEmulator.c
1 /*++
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 Module Name:
13 PcdEmulator.c
14
15 Abstract:
16 Platform Configuration Database (PCD) Service PEIM
17
18 --*/
19
20 #include <PcdEmulator.h>
21
22 //
23 // BugBug: PEI early phase does not support global variable!!!
24 // This is only a temperary solution.
25 //
26
27 UINTN mSkuId = 0;
28
29
30 STATIC EMULATED_PCD_DATABASE_EX *
31 GetPcdDataBaseEx (
32 VOID
33 ) {
34 EFI_HOB_GUID_TYPE *GuidHob;
35 EMULATED_PCD_DATABASE_EX *EmulatedPcdDatabaseEx;
36
37 GuidHob = GetFirstGuidHob (&gPcdHobGuid);
38 EmulatedPcdDatabaseEx = (EMULATED_PCD_DATABASE_EX *) GET_GUID_HOB_DATA(GuidHob);
39
40 return EmulatedPcdDatabaseEx;
41 }
42
43 STATIC UINTN
44 GetPcdDataBaseExEntryCount (
45 EMULATED_PCD_DATABASE_EX * Database
46 ) {
47 return Database->Count;
48 }
49
50 STATIC UINTN
51 GetPcdDataBaseExSize (
52 EMULATED_PCD_DATABASE_EX * Database
53 ) {
54 UINTN Size;
55
56 Size = sizeof (Database->Count)
57 + (sizeof (Database->Entry[0]) * Database->Count);
58
59 return Size;
60 }
61
62 EFI_STATUS
63 EFIAPI
64 PcdEmulatorSetSku (
65 IN UINTN SkuId
66 )
67 {
68 mSkuId = SkuId;
69 return EFI_SUCCESS;
70 }
71
72 UINT8
73 EFIAPI
74 PcdEmulatorGet8 (
75 IN UINTN TokenNumber
76 )
77 {
78 EMULATED_PCD_ENTRY_EX *Pcd;
79
80 Pcd = GetPcdEntry (TokenNumber);
81 ASSERT (Pcd != NULL);
82 ASSERT (Pcd->DatumSize == 1);
83
84 return (UINT8)Pcd->Datum;
85 }
86
87 UINT16
88 EFIAPI
89 PcdEmulatorGet16 (
90 IN UINTN TokenNumber
91 )
92 {
93 EMULATED_PCD_ENTRY_EX *Pcd;
94
95 Pcd = GetPcdEntry (TokenNumber);
96 ASSERT (Pcd != NULL);
97 ASSERT (Pcd->DatumSize == 2);
98
99 return (UINT16)Pcd->Datum;
100 }
101
102 UINT32
103 EFIAPI
104 PcdEmulatorGet32 (
105 IN UINTN TokenNumber
106 )
107 {
108 EMULATED_PCD_ENTRY_EX *Pcd;
109
110 Pcd = GetPcdEntry (TokenNumber);
111 ASSERT (Pcd != NULL);
112 ASSERT (Pcd->DatumSize == 4);
113
114 return (UINT32)Pcd->Datum;
115 }
116
117 UINT64
118 EFIAPI
119 PcdEmulatorGet64 (
120 IN UINTN TokenNumber
121 )
122 {
123 EMULATED_PCD_ENTRY_EX *Pcd;
124
125 Pcd = GetPcdEntry (TokenNumber);
126 ASSERT (Pcd != NULL);
127 ASSERT (Pcd->DatumSize == sizeof (UINT64));
128
129 return (UINT64)Pcd->Datum;
130 }
131
132 VOID *
133 EFIAPI
134 PcdEmulatorGetPtr (
135 IN UINTN TokenNumber
136 )
137 {
138 EMULATED_PCD_ENTRY_EX *Pcd;
139
140 Pcd = GetPcdEntry (TokenNumber);
141 ASSERT (Pcd != NULL);
142
143 return (VOID *)(UINTN)Pcd->ExtendedData;
144 }
145
146 BOOLEAN
147 EFIAPI
148 PcdEmulatorGetBoolean (
149 IN UINTN TokenNumber
150 )
151 {
152 EMULATED_PCD_ENTRY_EX *Pcd;
153
154 Pcd = GetPcdEntry (TokenNumber);
155 ASSERT (Pcd != NULL);
156 ASSERT (Pcd->DatumSize == 1);
157
158 return (BOOLEAN)Pcd->Datum;
159 }
160
161 UINTN
162 EFIAPI
163 PcdEmulatorGetSize (
164 IN UINTN TokenNumber
165 )
166 {
167 EMULATED_PCD_ENTRY_EX *Pcd;
168
169 Pcd = GetPcdEntry (TokenNumber);
170 ASSERT (Pcd != NULL);
171 return Pcd->DatumSize;
172 }
173
174 UINT8
175 EFIAPI
176 PcdEmulatorGet8Ex (
177 IN CONST EFI_GUID *PcdDataBaseName,
178 IN UINTN TokenNumber
179 )
180 {
181 ASSERT (FALSE);
182 return 0;
183 }
184
185 UINT16
186 EFIAPI
187 PcdEmulatorGet16Ex (
188 IN CONST EFI_GUID *PcdDataBaseName,
189 IN UINTN TokenNumber
190 )
191 {
192 ASSERT (FALSE);
193 return 0;
194 }
195
196 UINT32
197 EFIAPI
198 PcdEmulatorGet32Ex (
199 IN CONST EFI_GUID *PcdDataBaseName,
200 IN UINTN TokenNumber
201 )
202 {
203 ASSERT (FALSE);
204 return 0;
205 }
206
207 UINT64
208 EFIAPI
209 PcdEmulatorGet64Ex (
210 IN CONST EFI_GUID *PcdDataBaseName,
211 IN UINTN TokenNumber
212 )
213 {
214 ASSERT (FALSE);
215 return 0;
216 }
217
218 VOID *
219 EFIAPI
220 PcdEmulatorGetPtrEx (
221 IN CONST EFI_GUID *PcdDataBaseName,
222 IN UINTN TokenNumber
223 )
224 {
225 ASSERT (FALSE);
226 return 0;
227 }
228
229 BOOLEAN
230 EFIAPI
231 PcdEmulatorGetBooleanEx (
232 IN CONST EFI_GUID *PcdDataBaseName,
233 IN UINTN TokenNumber
234 )
235 {
236 ASSERT (FALSE);
237 return 0;
238 }
239
240 UINTN
241 EFIAPI
242 PcdEmulatorGetSizeEx (
243 IN CONST EFI_GUID *PcdDataBaseName,
244 IN UINTN TokenNumber
245 )
246 {
247 EMULATED_PCD_ENTRY_EX *Pcd;
248
249 Pcd = GetPcdEntry (TokenNumber);
250 ASSERT (Pcd != NULL);
251 return Pcd->DatumSize;
252 }
253
254
255 EFI_STATUS
256 EFIAPI
257 PcdEmulatorSet8 (
258 IN UINTN TokenNumber,
259 IN UINT8 Value
260 )
261 {
262
263 EMULATED_PCD_ENTRY_EX *Pcd;
264
265 Pcd = GetPcdEntry (TokenNumber);
266 ASSERT (Pcd != NULL);
267
268 ASSERT (Pcd->DatumSize == sizeof (UINT8));
269
270 Pcd->Datum = Value;
271
272 return EFI_SUCCESS;
273 }
274
275 EFI_STATUS
276 EFIAPI
277 PcdEmulatorSet16 (
278 IN UINTN TokenNumber,
279 IN UINT16 Value
280 )
281 {
282
283 ASSERT (FALSE);
284
285 return EFI_SUCCESS;
286 }
287
288 EFI_STATUS
289 EFIAPI
290 PcdEmulatorSet32 (
291 IN UINTN TokenNumber,
292 IN UINT32 Value
293 )
294 {
295
296 EMULATED_PCD_ENTRY_EX *Pcd;
297
298 Pcd = GetPcdEntry (TokenNumber);
299 ASSERT (Pcd != NULL);
300
301 ASSERT (Pcd->DatumSize == sizeof (UINT32));
302
303 Pcd->Datum = Value;
304
305 return EFI_SUCCESS;
306 }
307
308 EFI_STATUS
309 EFIAPI
310 PcdEmulatorSet64 (
311 IN UINTN TokenNumber,
312 IN UINT64 Value
313 )
314 {
315
316 ASSERT (FALSE);
317
318 return EFI_SUCCESS;
319 }
320
321 EFI_STATUS
322 EFIAPI
323 PcdEmulatorSetPtr (
324 IN UINTN TokenNumber,
325 IN CONST VOID *Value
326 )
327 {
328
329 ASSERT (FALSE);
330
331 return EFI_SUCCESS;
332 }
333
334 EFI_STATUS
335 EFIAPI
336 PcdEmulatorSetBoolean (
337 IN UINTN TokenNumber,
338 IN BOOLEAN Value
339 )
340 {
341
342 ASSERT (FALSE);
343
344 return EFI_SUCCESS;
345 }
346
347 EFI_STATUS
348 EFIAPI
349 PcdEmulatorSet8Ex (
350 IN CONST EFI_GUID *Guid,
351 IN UINTN TokenNumber,
352 IN UINT8 Value
353 )
354 {
355
356 ASSERT (FALSE);
357
358 return EFI_SUCCESS;
359 }
360
361 EFI_STATUS
362 EFIAPI
363 PcdEmulatorSet16Ex (
364 IN CONST EFI_GUID *Guid,
365 IN UINTN TokenNumber,
366 IN UINT16 Value
367 )
368 {
369
370 ASSERT (FALSE);
371
372 return EFI_SUCCESS;
373 }
374
375 EFI_STATUS
376 EFIAPI
377 PcdEmulatorSet32Ex (
378 IN CONST EFI_GUID *Guid,
379 IN UINTN TokenNumber,
380 IN UINT32 Value
381 )
382 {
383
384 ASSERT (FALSE);
385
386 return EFI_SUCCESS;
387 }
388
389 EFI_STATUS
390 EFIAPI
391 PcdEmulatorSet64Ex (
392 IN CONST EFI_GUID *Guid,
393 IN UINTN TokenNumber,
394 IN UINT64 Value
395 )
396 {
397
398 ASSERT (FALSE);
399
400 return EFI_SUCCESS;
401 }
402
403 EFI_STATUS
404 EFIAPI
405 PcdEmulatorSetPtrEx (
406 IN CONST EFI_GUID *Guid,
407 IN UINTN TokenNumber,
408 IN CONST VOID *Value
409 )
410 {
411
412 ASSERT (FALSE);
413
414 return EFI_SUCCESS;
415 }
416
417 EFI_STATUS
418 EFIAPI
419 PcdEmulatorSetBooleanEx (
420 IN CONST EFI_GUID *Guid,
421 IN UINTN TokenNumber,
422 IN BOOLEAN Value
423 )
424 {
425
426 ASSERT (FALSE);
427
428 return EFI_SUCCESS;
429 }
430
431 EFI_STATUS
432 EFIAPI
433 PcdEmulatorCallBackOnSet (
434 IN UINTN TokenNumber,
435 IN CONST EFI_GUID *Guid, OPTIONAL
436 IN PCD_PPI_CALLBACK CallBackFunction
437 )
438 {
439 EMULATED_PCD_ENTRY_EX *Pcd;
440
441 Pcd = GetPcdEntry (TokenNumber);
442 ASSERT (Pcd != NULL);
443
444 if (Pcd->CallBackListSize == Pcd->CallBackEntries) {
445 return EFI_OUT_OF_RESOURCES;
446 }
447
448 Pcd->CallBackList[Pcd->CallBackEntries++] = CallBackFunction;
449
450 return EFI_SUCCESS;
451 }
452
453 EFI_STATUS
454 EFIAPI
455 PcdEmulatorUnregisterCallBackOnSet (
456 IN UINTN TokenNumber,
457 IN CONST EFI_GUID *Guid, OPTIONAL
458 IN PCD_PPI_CALLBACK CallBackfunction
459 )
460 {
461 EMULATED_PCD_ENTRY_EX *Pcd;
462 UINT32 Index;
463
464 Pcd = GetPcdEntry (TokenNumber);
465 ASSERT (Pcd != NULL);
466
467 for (Index = 0; Index < Pcd->CallBackListSize; Index++) {
468 if (Pcd->CallBackList[Index] == CallBackfunction) {
469 Pcd->CallBackList[Index] = NULL;
470 return EFI_SUCCESS;
471 }
472 }
473
474 return EFI_NOT_FOUND;
475 }
476
477 EFI_STATUS
478 EFIAPI
479 PcdEmulatorGetNextToken (
480 IN CONST EFI_GUID *Guid, OPTIONAL
481 IN UINTN *Token
482 )
483 {
484 EMULATED_PCD_ENTRY_EX *Pcd;
485 EMULATED_PCD_ENTRY_EX *LastPcdEntry;
486 EMULATED_PCD_DATABASE_EX *PcdDatabase;
487 EMULATED_PCD_ENTRY_EX *PcdEntry;
488
489 PcdDatabase = GetPcdDataBaseEx ();
490 PcdEntry = PcdDatabase->Entry;
491
492 if (*Token == PCD_INVALID_TOKEN) {
493 //
494 // BugBug: Due to variable size array, ensure we convert this to a reasonable database
495 // that can accomodate array references for simplicity's sake
496 *Token = PcdEntry[0].Token;
497 return EFI_SUCCESS;
498 }
499
500 Pcd = GetPcdEntry (*Token);
501 if (Pcd == NULL) {
502 return EFI_NOT_FOUND;
503 }
504
505 LastPcdEntry = PcdEntry + GetPcdDataBaseExEntryCount (PcdDatabase);
506 if (++Pcd >= LastPcdEntry) {
507 return EFI_NOT_FOUND;
508 }
509
510 *Token = Pcd->Token;
511 return EFI_SUCCESS;
512 }
513
514 PCD_PPI mPcdPpiInstance = {
515 PcdEmulatorSetSku,
516
517 PcdEmulatorGet8,
518 PcdEmulatorGet16,
519 PcdEmulatorGet32,
520 PcdEmulatorGet64,
521 PcdEmulatorGetPtr,
522 PcdEmulatorGetBoolean,
523 PcdEmulatorGetSize,
524
525 PcdEmulatorGet8Ex,
526 PcdEmulatorGet16Ex,
527 PcdEmulatorGet32Ex,
528 PcdEmulatorGet64Ex,
529 PcdEmulatorGetPtrEx,
530 PcdEmulatorGetBooleanEx,
531 PcdEmulatorGetSizeEx,
532
533 PcdEmulatorSet8,
534 PcdEmulatorSet16,
535 PcdEmulatorSet32,
536 PcdEmulatorSet64,
537 PcdEmulatorSetPtr,
538 PcdEmulatorSetBoolean,
539
540 PcdEmulatorSet8Ex,
541 PcdEmulatorSet16Ex,
542 PcdEmulatorSet32Ex,
543 PcdEmulatorSet64Ex,
544 PcdEmulatorSetPtrEx,
545 PcdEmulatorSetBooleanEx,
546
547 PcdEmulatorCallBackOnSet,
548 PcdEmulatorUnregisterCallBackOnSet,
549 PcdEmulatorGetNextToken
550 };
551
552 STATIC EFI_PEI_PPI_DESCRIPTOR mPpiPCD = {
553 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
554 &gPcdPpiGuid,
555 &mPcdPpiInstance
556 };
557
558 EFI_STATUS
559 EFIAPI
560 PeimPcdEmulatorEntry (
561 IN EFI_FFS_FILE_HEADER *FfsHeader,
562 IN EFI_PEI_SERVICES **PeiServices
563 )
564 {
565 EFI_STATUS Status;
566 UINTN Index;
567 UINTN Count;
568 UINTN Calculation;
569 UINT8 *AllocatedBuffer;
570 EMULATED_PCD_DATABASE_EX *EmulatedPcdDatabaseEx;
571 EMULATED_PCD_ENTRY_EX *EmulatedPcdEntryEx;
572
573 //
574 // BugBug: Normally, we would read an FFS file for this data
575 // We need to remember, that when we read the FFS file, items such as the VariableName will not be encoded as a pointer
576 // but as an array of content. In this emulation, our init is encoding this data as a pointer.
577 // In the FFS version, we will depend on the proper Entry Count in the FFS data since the structures will
578 // now be variable length.
579 //
580 //
581
582 //
583 // We should now read from the FFS file into the cache - for now, we fake this.
584 //
585 Count = GetPcdDataBaseSize () / sizeof (EMULATED_PCD_ENTRY);
586
587 //
588 // Let's now determine how big of a buffer we need for our database
589 // For the FFS version, we need to calculate/consider the VariableName/ExtendedData size!!!
590 //
591 Calculation = sizeof (UINTN) + (Count * sizeof (EMULATED_PCD_ENTRY_EX));
592
593 EmulatedPcdDatabaseEx = (EMULATED_PCD_DATABASE_EX *) BuildGuidHob (&gPcdHobGuid, Calculation);
594
595 EmulatedPcdDatabaseEx->Count = Count;
596 EmulatedPcdEntryEx = EmulatedPcdDatabaseEx->Entry;
597
598 AllocatedBuffer = AllocatePool (Count * sizeof (PCD_PPI_CALLBACK) * MAX_PCD_CALLBACK);
599 ASSERT (AllocatedBuffer != NULL);
600
601 for (Index = 0; Index < Count; Index++) {
602 //
603 // Copy from source to our own allocated buffer - normally an FFS read
604 //
605 (*PeiServices)->CopyMem (
606 (VOID *) (EmulatedPcdEntryEx + Index),
607 (VOID *) (gEmulatedPcdEntry + Index),
608 sizeof (EMULATED_PCD_ENTRY)
609 );
610
611 //
612 // All the CallBackList worker functions refer to this CallBackList as CallBackList[CallbackEntry]
613 // so we seed the same buffer address here.
614 //
615 EmulatedPcdEntryEx[Index].CallBackList = (PCD_PPI_CALLBACK *)AllocatedBuffer;
616 AllocatedBuffer+= (sizeof (PCD_PPI_CALLBACK) * MAX_PCD_CALLBACK);
617 EmulatedPcdEntryEx[Index].CallBackEntries = 0;
618 EmulatedPcdEntryEx[Index].CallBackListSize = MAX_PCD_CALLBACK;
619 }
620
621 //
622 // Install PCD service PPI
623 //
624 Status = PeiServicesInstallPpi (&mPpiPCD);
625
626 ASSERT_EFI_ERROR (Status);
627 return Status;
628 }
629
630
631 EMULATED_PCD_ENTRY_EX *
632 GetPcdEntry (
633 IN UINTN TokenNumber
634 )
635 {
636 UINTN Index;
637 UINTN Count;
638 EMULATED_PCD_DATABASE_EX *EmulatedPcdDatabaseEx;
639 EMULATED_PCD_ENTRY_EX *EmulatedPcdEntryEx;
640
641 CpuBreakpoint ();
642
643 EmulatedPcdDatabaseEx = GetPcdDataBaseEx ();
644 //
645 // BugBug: This Count will change when we flip over to FFS version
646 //
647 Count = EmulatedPcdDatabaseEx->Count;
648 EmulatedPcdEntryEx = EmulatedPcdDatabaseEx->Entry;
649 for (Index = 0; Index < Count; Index++) {
650 if (EmulatedPcdEntryEx[Index].Token == TokenNumber) {
651 return &EmulatedPcdEntryEx[Index];
652 }
653 }
654 return NULL;
655 }
656
657