]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/AutoGen/GenC.py
BaseTools: Generate macro for the size of PCD value
[mirror_edk2.git] / BaseTools / Source / Python / AutoGen / GenC.py
1 ## @file
2 # Routines for generating AutoGen.h and AutoGen.c
3 #
4 # Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
9 #
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 #
13
14 ## Import Modules
15 #
16 import string
17
18 from Common import EdkLogger
19
20 from Common.BuildToolError import *
21 from Common.DataType import *
22 from Common.Misc import *
23 from Common.String import StringToArray
24 from StrGather import *
25 from GenPcdDb import CreatePcdDatabaseCode
26
27 ## PCD type string
28 gItemTypeStringDatabase = {
29 TAB_PCDS_FEATURE_FLAG : 'FixedAtBuild',
30 TAB_PCDS_FIXED_AT_BUILD : 'FixedAtBuild',
31 TAB_PCDS_PATCHABLE_IN_MODULE: 'BinaryPatch',
32 TAB_PCDS_DYNAMIC : '',
33 TAB_PCDS_DYNAMIC_DEFAULT : '',
34 TAB_PCDS_DYNAMIC_VPD : '',
35 TAB_PCDS_DYNAMIC_HII : '',
36 TAB_PCDS_DYNAMIC_EX : '',
37 TAB_PCDS_DYNAMIC_EX_DEFAULT : '',
38 TAB_PCDS_DYNAMIC_EX_VPD : '',
39 TAB_PCDS_DYNAMIC_EX_HII : '',
40 }
41
42 ## Dynamic PCD types
43 gDynamicPcd = [TAB_PCDS_DYNAMIC, TAB_PCDS_DYNAMIC_DEFAULT, TAB_PCDS_DYNAMIC_VPD, TAB_PCDS_DYNAMIC_HII]
44
45 ## Dynamic-ex PCD types
46 gDynamicExPcd = [TAB_PCDS_DYNAMIC_EX, TAB_PCDS_DYNAMIC_EX_DEFAULT, TAB_PCDS_DYNAMIC_EX_VPD, TAB_PCDS_DYNAMIC_EX_HII]
47
48 ## Datum size
49 gDatumSizeStringDatabase = {'UINT8':'8','UINT16':'16','UINT32':'32','UINT64':'64','BOOLEAN':'BOOLEAN','VOID*':'8'}
50 gDatumSizeStringDatabaseH = {'UINT8':'8','UINT16':'16','UINT32':'32','UINT64':'64','BOOLEAN':'BOOL','VOID*':'PTR'}
51 gDatumSizeStringDatabaseLib = {'UINT8':'8','UINT16':'16','UINT32':'32','UINT64':'64','BOOLEAN':'Bool','VOID*':'Ptr'}
52
53 ## AutoGen File Header Templates
54 gAutoGenHeaderString = TemplateString("""\
55 /**
56 DO NOT EDIT
57 FILE auto-generated
58 Module name:
59 ${FileName}
60 Abstract: Auto-generated ${FileName} for building module or library.
61 **/
62 """)
63
64 gAutoGenHPrologueString = TemplateString("""
65 #ifndef _${File}_${Guid}
66 #define _${File}_${Guid}
67
68 """)
69
70 gAutoGenHCppPrologueString = """\
71 #ifdef __cplusplus
72 extern "C" {
73 #endif
74
75 """
76
77 gAutoGenHEpilogueString = """
78
79 #ifdef __cplusplus
80 }
81 #endif
82
83 #endif
84 """
85
86 ## PEI Core Entry Point Templates
87 gPeiCoreEntryPointPrototype = TemplateString("""
88 ${BEGIN}
89 VOID
90 EFIAPI
91 ${Function} (
92 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,
93 IN CONST EFI_PEI_PPI_DESCRIPTOR *PpiList,
94 IN VOID *Context
95 );
96 ${END}
97 """)
98
99 gPeiCoreEntryPointString = TemplateString("""
100 ${BEGIN}
101 VOID
102 EFIAPI
103 ProcessModuleEntryPointList (
104 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,
105 IN CONST EFI_PEI_PPI_DESCRIPTOR *PpiList,
106 IN VOID *Context
107 )
108
109 {
110 ${Function} (SecCoreData, PpiList, Context);
111 }
112 ${END}
113 """)
114
115
116 ## DXE Core Entry Point Templates
117 gDxeCoreEntryPointPrototype = TemplateString("""
118 ${BEGIN}
119 VOID
120 EFIAPI
121 ${Function} (
122 IN VOID *HobStart
123 );
124 ${END}
125 """)
126
127 gDxeCoreEntryPointString = TemplateString("""
128 ${BEGIN}
129 VOID
130 EFIAPI
131 ProcessModuleEntryPointList (
132 IN VOID *HobStart
133 )
134
135 {
136 ${Function} (HobStart);
137 }
138 ${END}
139 """)
140
141 ## PEIM Entry Point Templates
142 gPeimEntryPointPrototype = TemplateString("""
143 ${BEGIN}
144 EFI_STATUS
145 EFIAPI
146 ${Function} (
147 IN EFI_PEI_FILE_HANDLE FileHandle,
148 IN CONST EFI_PEI_SERVICES **PeiServices
149 );
150 ${END}
151 """)
152
153 gPeimEntryPointString = [
154 TemplateString("""
155 GLOBAL_REMOVE_IF_UNREFERENCED const UINT32 _gPeimRevision = ${PiSpecVersion};
156
157 EFI_STATUS
158 EFIAPI
159 ProcessModuleEntryPointList (
160 IN EFI_PEI_FILE_HANDLE FileHandle,
161 IN CONST EFI_PEI_SERVICES **PeiServices
162 )
163
164 {
165 return EFI_SUCCESS;
166 }
167 """),
168 TemplateString("""
169 GLOBAL_REMOVE_IF_UNREFERENCED const UINT32 _gPeimRevision = ${PiSpecVersion};
170 ${BEGIN}
171 EFI_STATUS
172 EFIAPI
173 ProcessModuleEntryPointList (
174 IN EFI_PEI_FILE_HANDLE FileHandle,
175 IN CONST EFI_PEI_SERVICES **PeiServices
176 )
177
178 {
179 return ${Function} (FileHandle, PeiServices);
180 }
181 ${END}
182 """),
183 TemplateString("""
184 GLOBAL_REMOVE_IF_UNREFERENCED const UINT32 _gPeimRevision = ${PiSpecVersion};
185
186 EFI_STATUS
187 EFIAPI
188 ProcessModuleEntryPointList (
189 IN EFI_PEI_FILE_HANDLE FileHandle,
190 IN CONST EFI_PEI_SERVICES **PeiServices
191 )
192
193 {
194 EFI_STATUS Status;
195 EFI_STATUS CombinedStatus;
196
197 CombinedStatus = EFI_LOAD_ERROR;
198 ${BEGIN}
199 Status = ${Function} (FileHandle, PeiServices);
200 if (!EFI_ERROR (Status) || EFI_ERROR (CombinedStatus)) {
201 CombinedStatus = Status;
202 }
203 ${END}
204 return CombinedStatus;
205 }
206 """)
207 ]
208
209 ## SMM_CORE Entry Point Templates
210 gSmmCoreEntryPointPrototype = TemplateString("""
211 ${BEGIN}
212 EFI_STATUS
213 EFIAPI
214 ${Function} (
215 IN EFI_HANDLE ImageHandle,
216 IN EFI_SYSTEM_TABLE *SystemTable
217 );
218 ${END}
219 """)
220
221 gSmmCoreEntryPointString = TemplateString("""
222 ${BEGIN}
223 const UINT32 _gUefiDriverRevision = ${UefiSpecVersion};
224 const UINT32 _gDxeRevision = ${PiSpecVersion};
225
226 EFI_STATUS
227 EFIAPI
228 ProcessModuleEntryPointList (
229 IN EFI_HANDLE ImageHandle,
230 IN EFI_SYSTEM_TABLE *SystemTable
231 )
232 {
233 return ${Function} (ImageHandle, SystemTable);
234 }
235 ${END}
236 """)
237
238 ## DXE SMM Entry Point Templates
239 gDxeSmmEntryPointPrototype = TemplateString("""
240 ${BEGIN}
241 EFI_STATUS
242 EFIAPI
243 ${Function} (
244 IN EFI_HANDLE ImageHandle,
245 IN EFI_SYSTEM_TABLE *SystemTable
246 );
247 ${END}
248 """)
249
250 gDxeSmmEntryPointString = [
251 TemplateString("""
252 const UINT32 _gUefiDriverRevision = ${UefiSpecVersion};
253 const UINT32 _gDxeRevision = ${PiSpecVersion};
254
255 EFI_STATUS
256 EFIAPI
257 ProcessModuleEntryPointList (
258 IN EFI_HANDLE ImageHandle,
259 IN EFI_SYSTEM_TABLE *SystemTable
260 )
261
262 {
263 return EFI_SUCCESS;
264 }
265 """),
266 TemplateString("""
267 const UINT32 _gUefiDriverRevision = ${UefiSpecVersion};
268 const UINT32 _gDxeRevision = ${PiSpecVersion};
269
270 static BASE_LIBRARY_JUMP_BUFFER mJumpContext;
271 static EFI_STATUS mDriverEntryPointStatus;
272
273 VOID
274 EFIAPI
275 ExitDriver (
276 IN EFI_STATUS Status
277 )
278 {
279 if (!EFI_ERROR (Status) || EFI_ERROR (mDriverEntryPointStatus)) {
280 mDriverEntryPointStatus = Status;
281 }
282 LongJump (&mJumpContext, (UINTN)-1);
283 ASSERT (FALSE);
284 }
285
286 EFI_STATUS
287 EFIAPI
288 ProcessModuleEntryPointList (
289 IN EFI_HANDLE ImageHandle,
290 IN EFI_SYSTEM_TABLE *SystemTable
291 )
292 {
293 mDriverEntryPointStatus = EFI_LOAD_ERROR;
294
295 ${BEGIN}
296 if (SetJump (&mJumpContext) == 0) {
297 ExitDriver (${Function} (ImageHandle, SystemTable));
298 ASSERT (FALSE);
299 }
300 ${END}
301
302 return mDriverEntryPointStatus;
303 }
304 """)
305 ]
306
307 ## UEFI Driver Entry Point Templates
308 gUefiDriverEntryPointPrototype = TemplateString("""
309 ${BEGIN}
310 EFI_STATUS
311 EFIAPI
312 ${Function} (
313 IN EFI_HANDLE ImageHandle,
314 IN EFI_SYSTEM_TABLE *SystemTable
315 );
316 ${END}
317 """)
318
319 gUefiDriverEntryPointString = [
320 TemplateString("""
321 const UINT32 _gUefiDriverRevision = ${UefiSpecVersion};
322 const UINT32 _gDxeRevision = ${PiSpecVersion};
323
324 EFI_STATUS
325 EFIAPI
326 ProcessModuleEntryPointList (
327 IN EFI_HANDLE ImageHandle,
328 IN EFI_SYSTEM_TABLE *SystemTable
329 )
330 {
331 return EFI_SUCCESS;
332 }
333 """),
334 TemplateString("""
335 const UINT32 _gUefiDriverRevision = ${UefiSpecVersion};
336 const UINT32 _gDxeRevision = ${PiSpecVersion};
337
338 ${BEGIN}
339 EFI_STATUS
340 EFIAPI
341 ProcessModuleEntryPointList (
342 IN EFI_HANDLE ImageHandle,
343 IN EFI_SYSTEM_TABLE *SystemTable
344 )
345
346 {
347 return ${Function} (ImageHandle, SystemTable);
348 }
349 ${END}
350 VOID
351 EFIAPI
352 ExitDriver (
353 IN EFI_STATUS Status
354 )
355 {
356 if (EFI_ERROR (Status)) {
357 ProcessLibraryDestructorList (gImageHandle, gST);
358 }
359 gBS->Exit (gImageHandle, Status, 0, NULL);
360 }
361 """),
362 TemplateString("""
363 const UINT32 _gUefiDriverRevision = ${UefiSpecVersion};
364 const UINT32 _gDxeRevision = ${PiSpecVersion};
365
366 static BASE_LIBRARY_JUMP_BUFFER mJumpContext;
367 static EFI_STATUS mDriverEntryPointStatus;
368
369 EFI_STATUS
370 EFIAPI
371 ProcessModuleEntryPointList (
372 IN EFI_HANDLE ImageHandle,
373 IN EFI_SYSTEM_TABLE *SystemTable
374 )
375 {
376 mDriverEntryPointStatus = EFI_LOAD_ERROR;
377 ${BEGIN}
378 if (SetJump (&mJumpContext) == 0) {
379 ExitDriver (${Function} (ImageHandle, SystemTable));
380 ASSERT (FALSE);
381 }
382 ${END}
383 return mDriverEntryPointStatus;
384 }
385
386 VOID
387 EFIAPI
388 ExitDriver (
389 IN EFI_STATUS Status
390 )
391 {
392 if (!EFI_ERROR (Status) || EFI_ERROR (mDriverEntryPointStatus)) {
393 mDriverEntryPointStatus = Status;
394 }
395 LongJump (&mJumpContext, (UINTN)-1);
396 ASSERT (FALSE);
397 }
398 """)
399 ]
400
401
402 ## UEFI Application Entry Point Templates
403 gUefiApplicationEntryPointPrototype = TemplateString("""
404 ${BEGIN}
405 EFI_STATUS
406 EFIAPI
407 ${Function} (
408 IN EFI_HANDLE ImageHandle,
409 IN EFI_SYSTEM_TABLE *SystemTable
410 );
411 ${END}
412 """)
413
414 gUefiApplicationEntryPointString = [
415 TemplateString("""
416 const UINT32 _gUefiDriverRevision = ${UefiSpecVersion};
417
418 EFI_STATUS
419 EFIAPI
420 ProcessModuleEntryPointList (
421 IN EFI_HANDLE ImageHandle,
422 IN EFI_SYSTEM_TABLE *SystemTable
423 )
424 {
425 return EFI_SUCCESS;
426 }
427 """),
428 TemplateString("""
429 const UINT32 _gUefiDriverRevision = ${UefiSpecVersion};
430
431 ${BEGIN}
432 EFI_STATUS
433 EFIAPI
434 ProcessModuleEntryPointList (
435 IN EFI_HANDLE ImageHandle,
436 IN EFI_SYSTEM_TABLE *SystemTable
437 )
438
439 {
440 return ${Function} (ImageHandle, SystemTable);
441 }
442 ${END}
443 VOID
444 EFIAPI
445 ExitDriver (
446 IN EFI_STATUS Status
447 )
448 {
449 if (EFI_ERROR (Status)) {
450 ProcessLibraryDestructorList (gImageHandle, gST);
451 }
452 gBS->Exit (gImageHandle, Status, 0, NULL);
453 }
454 """),
455 TemplateString("""
456 const UINT32 _gUefiDriverRevision = ${UefiSpecVersion};
457
458 EFI_STATUS
459 EFIAPI
460 ProcessModuleEntryPointList (
461 IN EFI_HANDLE ImageHandle,
462 IN EFI_SYSTEM_TABLE *SystemTable
463 )
464
465 {
466 ${BEGIN}
467 if (SetJump (&mJumpContext) == 0) {
468 ExitDriver (${Function} (ImageHandle, SystemTable));
469 ASSERT (FALSE);
470 }
471 ${END}
472 return mDriverEntryPointStatus;
473 }
474
475 static BASE_LIBRARY_JUMP_BUFFER mJumpContext;
476 static EFI_STATUS mDriverEntryPointStatus = EFI_LOAD_ERROR;
477
478 VOID
479 EFIAPI
480 ExitDriver (
481 IN EFI_STATUS Status
482 )
483 {
484 if (!EFI_ERROR (Status) || EFI_ERROR (mDriverEntryPointStatus)) {
485 mDriverEntryPointStatus = Status;
486 }
487 LongJump (&mJumpContext, (UINTN)-1);
488 ASSERT (FALSE);
489 }
490 """)
491 ]
492
493 ## UEFI Unload Image Templates
494 gUefiUnloadImagePrototype = TemplateString("""
495 ${BEGIN}
496 EFI_STATUS
497 EFIAPI
498 ${Function} (
499 IN EFI_HANDLE ImageHandle
500 );
501 ${END}
502 """)
503
504 gUefiUnloadImageString = [
505 TemplateString("""
506 GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverUnloadImageCount = ${Count};
507
508 EFI_STATUS
509 EFIAPI
510 ProcessModuleUnloadList (
511 IN EFI_HANDLE ImageHandle
512 )
513 {
514 return EFI_SUCCESS;
515 }
516 """),
517 TemplateString("""
518 GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverUnloadImageCount = ${Count};
519
520 ${BEGIN}
521 EFI_STATUS
522 EFIAPI
523 ProcessModuleUnloadList (
524 IN EFI_HANDLE ImageHandle
525 )
526 {
527 return ${Function} (ImageHandle);
528 }
529 ${END}
530 """),
531 TemplateString("""
532 GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverUnloadImageCount = ${Count};
533
534 EFI_STATUS
535 EFIAPI
536 ProcessModuleUnloadList (
537 IN EFI_HANDLE ImageHandle
538 )
539 {
540 EFI_STATUS Status;
541
542 Status = EFI_SUCCESS;
543 ${BEGIN}
544 if (EFI_ERROR (Status)) {
545 ${Function} (ImageHandle);
546 } else {
547 Status = ${Function} (ImageHandle);
548 }
549 ${END}
550 return Status;
551 }
552 """)
553 ]
554
555 gLibraryStructorPrototype = {
556 'BASE' : TemplateString("""${BEGIN}
557 RETURN_STATUS
558 EFIAPI
559 ${Function} (
560 VOID
561 );${END}
562 """),
563
564 'PEI' : TemplateString("""${BEGIN}
565 EFI_STATUS
566 EFIAPI
567 ${Function} (
568 IN EFI_PEI_FILE_HANDLE FileHandle,
569 IN CONST EFI_PEI_SERVICES **PeiServices
570 );${END}
571 """),
572
573 'DXE' : TemplateString("""${BEGIN}
574 EFI_STATUS
575 EFIAPI
576 ${Function} (
577 IN EFI_HANDLE ImageHandle,
578 IN EFI_SYSTEM_TABLE *SystemTable
579 );${END}
580 """),
581 }
582
583 gLibraryStructorCall = {
584 'BASE' : TemplateString("""${BEGIN}
585 Status = ${Function} ();
586 ASSERT_EFI_ERROR (Status);${END}
587 """),
588
589 'PEI' : TemplateString("""${BEGIN}
590 Status = ${Function} (FileHandle, PeiServices);
591 ASSERT_EFI_ERROR (Status);${END}
592 """),
593
594 'DXE' : TemplateString("""${BEGIN}
595 Status = ${Function} (ImageHandle, SystemTable);
596 ASSERT_EFI_ERROR (Status);${END}
597 """),
598 }
599
600 ## Library Constructor and Destructor Templates
601 gLibraryString = {
602 'BASE' : TemplateString("""
603 ${BEGIN}${FunctionPrototype}${END}
604
605 VOID
606 EFIAPI
607 ProcessLibrary${Type}List (
608 VOID
609 )
610 {
611 ${BEGIN} EFI_STATUS Status;
612 ${FunctionCall}${END}
613 }
614 """),
615
616 'PEI' : TemplateString("""
617 ${BEGIN}${FunctionPrototype}${END}
618
619 VOID
620 EFIAPI
621 ProcessLibrary${Type}List (
622 IN EFI_PEI_FILE_HANDLE FileHandle,
623 IN CONST EFI_PEI_SERVICES **PeiServices
624 )
625 {
626 ${BEGIN} EFI_STATUS Status;
627 ${FunctionCall}${END}
628 }
629 """),
630
631 'DXE' : TemplateString("""
632 ${BEGIN}${FunctionPrototype}${END}
633
634 VOID
635 EFIAPI
636 ProcessLibrary${Type}List (
637 IN EFI_HANDLE ImageHandle,
638 IN EFI_SYSTEM_TABLE *SystemTable
639 )
640 {
641 ${BEGIN} EFI_STATUS Status;
642 ${FunctionCall}${END}
643 }
644 """),
645 }
646
647 gBasicHeaderFile = "Base.h"
648
649 gModuleTypeHeaderFile = {
650 "BASE" : [gBasicHeaderFile],
651 "SEC" : ["PiPei.h", "Library/DebugLib.h"],
652 "PEI_CORE" : ["PiPei.h", "Library/DebugLib.h", "Library/PeiCoreEntryPoint.h"],
653 "PEIM" : ["PiPei.h", "Library/DebugLib.h", "Library/PeimEntryPoint.h"],
654 "DXE_CORE" : ["PiDxe.h", "Library/DebugLib.h", "Library/DxeCoreEntryPoint.h"],
655 "DXE_DRIVER" : ["PiDxe.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiBootServicesTableLib.h", "Library/UefiDriverEntryPoint.h"],
656 "DXE_SMM_DRIVER" : ["PiDxe.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiBootServicesTableLib.h", "Library/UefiDriverEntryPoint.h"],
657 "DXE_RUNTIME_DRIVER": ["PiDxe.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiBootServicesTableLib.h", "Library/UefiDriverEntryPoint.h"],
658 "DXE_SAL_DRIVER" : ["PiDxe.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiBootServicesTableLib.h", "Library/UefiDriverEntryPoint.h"],
659 "UEFI_DRIVER" : ["Uefi.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiBootServicesTableLib.h", "Library/UefiDriverEntryPoint.h"],
660 "UEFI_APPLICATION" : ["Uefi.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiBootServicesTableLib.h", "Library/UefiApplicationEntryPoint.h"],
661 "SMM_CORE" : ["PiDxe.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiDriverEntryPoint.h"],
662 "USER_DEFINED" : [gBasicHeaderFile]
663 }
664
665 ## Autogen internal worker macro to define DynamicEx PCD name includes both the TokenSpaceGuidName
666 # the TokenName and Guid comparison to avoid define name collisions.
667 #
668 # @param Info The ModuleAutoGen object
669 # @param AutoGenH The TemplateString object for header file
670 #
671 #
672 def DynExPcdTokenNumberMapping(Info, AutoGenH):
673 ExTokenCNameList = []
674 PcdExList = []
675 if Info.IsLibrary:
676 PcdList = Info.LibraryPcdList
677 else:
678 PcdList = Info.ModulePcdList
679 for Pcd in PcdList:
680 if Pcd.Type in gDynamicExPcd:
681 ExTokenCNameList.append(Pcd.TokenCName)
682 PcdExList.append(Pcd)
683 if len(ExTokenCNameList) == 0:
684 return
685 AutoGenH.Append('\n#define COMPAREGUID(Guid1, Guid2) (BOOLEAN)(*(CONST UINT64*)Guid1 == *(CONST UINT64*)Guid2 && *((CONST UINT64*)Guid1 + 1) == *((CONST UINT64*)Guid2 + 1))\n')
686 # AutoGen for each PCD listed in a [PcdEx] section of a Module/Lib INF file.
687 # Auto generate a macro for each TokenName that takes a Guid pointer as a parameter.
688 # Use the Guid pointer to see if it matches any of the token space GUIDs.
689 TokenCNameList = []
690 for TokenCName in ExTokenCNameList:
691 if TokenCName in TokenCNameList:
692 continue
693 Index = 0
694 Count = ExTokenCNameList.count(TokenCName)
695 for Pcd in PcdExList:
696 if Pcd.TokenCName == TokenCName:
697 Index = Index + 1
698 if Index == 1:
699 AutoGenH.Append('\n#define __PCD_%s_ADDR_CMP(GuidPtr) (' % (Pcd.TokenCName))
700 AutoGenH.Append('\\\n (GuidPtr == &%s) ? _PCD_TOKEN_%s_%s:'
701 % (Pcd.TokenSpaceGuidCName, Pcd.TokenSpaceGuidCName, Pcd.TokenCName))
702 else:
703 AutoGenH.Append('\\\n (GuidPtr == &%s) ? _PCD_TOKEN_%s_%s:'
704 % (Pcd.TokenSpaceGuidCName, Pcd.TokenSpaceGuidCName, Pcd.TokenCName))
705 if Index == Count:
706 AutoGenH.Append('0 \\\n )\n')
707 TokenCNameList.append(TokenCName)
708
709 TokenCNameList = []
710 for TokenCName in ExTokenCNameList:
711 if TokenCName in TokenCNameList:
712 continue
713 Index = 0
714 Count = ExTokenCNameList.count(TokenCName)
715 for Pcd in PcdExList:
716 if Pcd.Type in gDynamicExPcd and Pcd.TokenCName == TokenCName:
717 Index = Index + 1
718 if Index == 1:
719 AutoGenH.Append('\n#define __PCD_%s_VAL_CMP(GuidPtr) (' % (Pcd.TokenCName))
720 AutoGenH.Append('\\\n (GuidPtr == NULL) ? 0:')
721 AutoGenH.Append('\\\n COMPAREGUID (GuidPtr, &%s) ? _PCD_TOKEN_%s_%s:'
722 % (Pcd.TokenSpaceGuidCName, Pcd.TokenSpaceGuidCName, Pcd.TokenCName))
723 else:
724 AutoGenH.Append('\\\n COMPAREGUID (GuidPtr, &%s) ? _PCD_TOKEN_%s_%s:'
725 % (Pcd.TokenSpaceGuidCName, Pcd.TokenSpaceGuidCName, Pcd.TokenCName))
726 if Index == Count:
727 AutoGenH.Append('0 \\\n )\n')
728 # Autogen internal worker macro to compare GUIDs. Guid1 is a pointer to a GUID.
729 # Guid2 is a C name for a GUID. Compare pointers first because optimizing compiler
730 # can do this at build time on CONST GUID pointers and optimize away call to COMPAREGUID().
731 # COMPAREGUID() will only be used if the Guid passed in is local to the module.
732 AutoGenH.Append('#define _PCD_TOKEN_EX_%s(GuidPtr) __PCD_%s_ADDR_CMP(GuidPtr) ? __PCD_%s_ADDR_CMP(GuidPtr) : __PCD_%s_VAL_CMP(GuidPtr) \n'
733 % (Pcd.TokenCName, Pcd.TokenCName, Pcd.TokenCName, Pcd.TokenCName))
734 TokenCNameList.append(TokenCName)
735
736 def GetPcdSize(Pcd):
737 if Pcd.DatumType == 'VOID*':
738 Value = Pcd.DefaultValue
739 if Value in [None, '']:
740 return 1
741 elif Value[0] == 'L':
742 return (len(Value) - 2) * 2
743 elif Value[0] == '{':
744 return len(Value.split(','))
745 else:
746 return len(Value) - 1
747 if Pcd.DatumType == 'UINT64':
748 return 8
749 if Pcd.DatumType == 'UINT32':
750 return 4
751 if Pcd.DatumType == 'UINT16':
752 return 2
753 if Pcd.DatumType == 'UINT8':
754 return 1
755 if Pcd.DatumType == 'BOOLEAN':
756 return 1
757
758
759 ## Create code for module PCDs
760 #
761 # @param Info The ModuleAutoGen object
762 # @param AutoGenC The TemplateString object for C code
763 # @param AutoGenH The TemplateString object for header file
764 # @param Pcd The PCD object
765 #
766 def CreateModulePcdCode(Info, AutoGenC, AutoGenH, Pcd):
767 TokenSpaceGuidValue = Pcd.TokenSpaceGuidValue #Info.GuidList[Pcd.TokenSpaceGuidCName]
768 PcdTokenNumber = Info.PlatformInfo.PcdTokenNumber
769 #
770 # Write PCDs
771 #
772 PcdTokenName = '_PCD_TOKEN_' + Pcd.TokenCName
773 PatchPcdSizeTokenName = '_PCD_PATCHABLE_' + Pcd.TokenCName +'_SIZE'
774 PatchPcdSizeVariableName = '_gPcd_BinaryPatch_Size_' + Pcd.TokenCName
775 FixPcdSizeTokenName = '_PCD_SIZE_' + Pcd.TokenCName
776
777 if Pcd.Type in gDynamicExPcd:
778 TokenNumber = int(Pcd.TokenValue, 0)
779 # Add TokenSpaceGuidValue value to PcdTokenName to discriminate the DynamicEx PCDs with
780 # different Guids but same TokenCName
781 PcdExTokenName = '_PCD_TOKEN_' + Pcd.TokenSpaceGuidCName + '_' + Pcd.TokenCName
782 AutoGenH.Append('\n#define %s %dU\n' % (PcdExTokenName, TokenNumber))
783 else:
784 if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName) not in PcdTokenNumber:
785 # If one of the Source built modules listed in the DSC is not listed in FDF modules,
786 # and the INF lists a PCD can only use the PcdsDynamic access method (it is only
787 # listed in the DEC file that declares the PCD as PcdsDynamic), then build tool will
788 # report warning message notify the PI that they are attempting to build a module
789 # that must be included in a flash image in order to be functional. These Dynamic PCD
790 # will not be added into the Database unless it is used by other modules that are
791 # included in the FDF file.
792 # In this case, just assign an invalid token number to make it pass build.
793 if Pcd.Type in PCD_DYNAMIC_TYPE_LIST:
794 TokenNumber = 0
795 else:
796 EdkLogger.error("build", AUTOGEN_ERROR,
797 "No generated token number for %s.%s\n" % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
798 ExtraData="[%s]" % str(Info))
799 else:
800 TokenNumber = PcdTokenNumber[Pcd.TokenCName, Pcd.TokenSpaceGuidCName]
801 AutoGenH.Append('\n#define %s %dU\n' % (PcdTokenName, TokenNumber))
802
803 EdkLogger.debug(EdkLogger.DEBUG_3, "Creating code for " + Pcd.TokenCName + "." + Pcd.TokenSpaceGuidCName)
804 if Pcd.Type not in gItemTypeStringDatabase:
805 EdkLogger.error("build", AUTOGEN_ERROR,
806 "Unknown PCD type [%s] of PCD %s.%s" % (Pcd.Type, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
807 ExtraData="[%s]" % str(Info))
808 if Pcd.DatumType not in gDatumSizeStringDatabase:
809 EdkLogger.error("build", AUTOGEN_ERROR,
810 "Unknown datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
811 ExtraData="[%s]" % str(Info))
812
813 DatumSize = gDatumSizeStringDatabase[Pcd.DatumType]
814 DatumSizeLib = gDatumSizeStringDatabaseLib[Pcd.DatumType]
815 GetModeName = '_PCD_GET_MODE_' + gDatumSizeStringDatabaseH[Pcd.DatumType] + '_' + Pcd.TokenCName
816 SetModeName = '_PCD_SET_MODE_' + gDatumSizeStringDatabaseH[Pcd.DatumType] + '_' + Pcd.TokenCName
817 SetModeStatusName = '_PCD_SET_MODE_' + gDatumSizeStringDatabaseH[Pcd.DatumType] + '_S_' + Pcd.TokenCName
818 GetModeSizeName = '_PCD_GET_MODE_SIZE' + '_' + Pcd.TokenCName
819
820 PcdExCNameList = []
821 if Pcd.Type in gDynamicExPcd:
822 if Info.IsLibrary:
823 PcdList = Info.LibraryPcdList
824 else:
825 PcdList = Info.ModulePcdList
826 for PcdModule in PcdList:
827 if PcdModule.Type in gDynamicExPcd:
828 PcdExCNameList.append(PcdModule.TokenCName)
829 # Be compatible with the current code which using PcdToken and PcdGet/Set for DynamicEx Pcd.
830 # If only PcdToken and PcdGet/Set used in all Pcds with different CName, it should succeed to build.
831 # If PcdToken and PcdGet/Set used in the Pcds with different Guids but same CName, it should failed to build.
832 if PcdExCNameList.count(Pcd.TokenCName) > 1:
833 AutoGenH.Append('// Disabled the macros, as PcdToken and PcdGet/Set are not allowed in the case that more than one DynamicEx Pcds are different Guids but same CName.\n')
834 AutoGenH.Append('// #define %s %s\n' % (PcdTokenName, PcdExTokenName))
835 AutoGenH.Append('// #define %s LibPcdGetEx%s(&%s, %s)\n' % (GetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
836 AutoGenH.Append('#define %s LibPcdGetExSize(&%s, %s)\n' % (GetModeSizeName,Pcd.TokenSpaceGuidCName, PcdTokenName))
837 if Pcd.DatumType == 'VOID*':
838 AutoGenH.Append('// #define %s(SizeOfBuffer, Buffer) LibPcdSetEx%s(&%s, %s, (SizeOfBuffer), (Buffer))\n' % (SetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
839 AutoGenH.Append('// #define %s(SizeOfBuffer, Buffer) LibPcdSetEx%sS(&%s, %s, (SizeOfBuffer), (Buffer))\n' % (SetModeStatusName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
840 else:
841 AutoGenH.Append('// #define %s(Value) LibPcdSetEx%s(&%s, %s, (Value))\n' % (SetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
842 AutoGenH.Append('// #define %s(Value) LibPcdSetEx%sS(&%s, %s, (Value))\n' % (SetModeStatusName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
843 else:
844 AutoGenH.Append('#define %s %s\n' % (PcdTokenName, PcdExTokenName))
845 AutoGenH.Append('#define %s LibPcdGetEx%s(&%s, %s)\n' % (GetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
846 AutoGenH.Append('#define %s LibPcdGetExSize(&%s, %s)\n' % (GetModeSizeName,Pcd.TokenSpaceGuidCName, PcdTokenName))
847 if Pcd.DatumType == 'VOID*':
848 AutoGenH.Append('#define %s(SizeOfBuffer, Buffer) LibPcdSetEx%s(&%s, %s, (SizeOfBuffer), (Buffer))\n' % (SetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
849 AutoGenH.Append('#define %s(SizeOfBuffer, Buffer) LibPcdSetEx%sS(&%s, %s, (SizeOfBuffer), (Buffer))\n' % (SetModeStatusName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
850 else:
851 AutoGenH.Append('#define %s(Value) LibPcdSetEx%s(&%s, %s, (Value))\n' % (SetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
852 AutoGenH.Append('#define %s(Value) LibPcdSetEx%sS(&%s, %s, (Value))\n' % (SetModeStatusName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
853 elif Pcd.Type in gDynamicPcd:
854 AutoGenH.Append('#define %s LibPcdGet%s(%s)\n' % (GetModeName, DatumSizeLib, PcdTokenName))
855 AutoGenH.Append('#define %s LibPcdGetSize(%s)\n' % (GetModeSizeName, PcdTokenName))
856 if Pcd.DatumType == 'VOID*':
857 AutoGenH.Append('#define %s(SizeOfBuffer, Buffer) LibPcdSet%s(%s, (SizeOfBuffer), (Buffer))\n' %(SetModeName, DatumSizeLib, PcdTokenName))
858 AutoGenH.Append('#define %s(SizeOfBuffer, Buffer) LibPcdSet%sS(%s, (SizeOfBuffer), (Buffer))\n' % (SetModeStatusName, DatumSizeLib, PcdTokenName))
859 else:
860 AutoGenH.Append('#define %s(Value) LibPcdSet%s(%s, (Value))\n' % (SetModeName, DatumSizeLib, PcdTokenName))
861 AutoGenH.Append('#define %s(Value) LibPcdSet%sS(%s, (Value))\n' % (SetModeStatusName, DatumSizeLib, PcdTokenName))
862 else:
863 PcdVariableName = '_gPcd_' + gItemTypeStringDatabase[Pcd.Type] + '_' + Pcd.TokenCName
864 Const = 'const'
865 if Pcd.Type == TAB_PCDS_PATCHABLE_IN_MODULE:
866 Const = ''
867 Type = ''
868 Array = ''
869 Value = Pcd.DefaultValue
870 Unicode = False
871 ValueNumber = 0
872
873 if Pcd.DatumType == 'BOOLEAN':
874 BoolValue = Value.upper()
875 if BoolValue == 'TRUE' or BoolValue == '1':
876 Value = '1U'
877 elif BoolValue == 'FALSE' or BoolValue == '0':
878 Value = '0U'
879
880 if Pcd.DatumType in ['UINT64', 'UINT32', 'UINT16', 'UINT8']:
881 try:
882 if Value.upper().startswith('0X'):
883 ValueNumber = int (Value, 16)
884 else:
885 ValueNumber = int (Value)
886 except:
887 EdkLogger.error("build", AUTOGEN_ERROR,
888 "PCD value is not valid dec or hex number for datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
889 ExtraData="[%s]" % str(Info))
890 if Pcd.DatumType == 'UINT64':
891 if ValueNumber < 0:
892 EdkLogger.error("build", AUTOGEN_ERROR,
893 "PCD can't be set to negative value for datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
894 ExtraData="[%s]" % str(Info))
895 elif ValueNumber >= 0x10000000000000000:
896 EdkLogger.error("build", AUTOGEN_ERROR,
897 "Too large PCD value for datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
898 ExtraData="[%s]" % str(Info))
899 if not Value.endswith('ULL'):
900 Value += 'ULL'
901 elif Pcd.DatumType == 'UINT32':
902 if ValueNumber < 0:
903 EdkLogger.error("build", AUTOGEN_ERROR,
904 "PCD can't be set to negative value for datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
905 ExtraData="[%s]" % str(Info))
906 elif ValueNumber >= 0x100000000:
907 EdkLogger.error("build", AUTOGEN_ERROR,
908 "Too large PCD value for datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
909 ExtraData="[%s]" % str(Info))
910 if not Value.endswith('U'):
911 Value += 'U'
912 elif Pcd.DatumType == 'UINT16':
913 if ValueNumber < 0:
914 EdkLogger.error("build", AUTOGEN_ERROR,
915 "PCD can't be set to negative value for datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
916 ExtraData="[%s]" % str(Info))
917 elif ValueNumber >= 0x10000:
918 EdkLogger.error("build", AUTOGEN_ERROR,
919 "Too large PCD value for datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
920 ExtraData="[%s]" % str(Info))
921 if not Value.endswith('U'):
922 Value += 'U'
923 elif Pcd.DatumType == 'UINT8':
924 if ValueNumber < 0:
925 EdkLogger.error("build", AUTOGEN_ERROR,
926 "PCD can't be set to negative value for datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
927 ExtraData="[%s]" % str(Info))
928 elif ValueNumber >= 0x100:
929 EdkLogger.error("build", AUTOGEN_ERROR,
930 "Too large PCD value for datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
931 ExtraData="[%s]" % str(Info))
932 if not Value.endswith('U'):
933 Value += 'U'
934 if Pcd.DatumType == 'VOID*':
935 if Pcd.MaxDatumSize == None or Pcd.MaxDatumSize == '':
936 EdkLogger.error("build", AUTOGEN_ERROR,
937 "Unknown [MaxDatumSize] of PCD [%s.%s]" % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
938 ExtraData="[%s]" % str(Info))
939
940 ArraySize = int(Pcd.MaxDatumSize, 0)
941 if Value[0] == '{':
942 Type = '(VOID *)'
943 else:
944 if Value[0] == 'L':
945 Unicode = True
946 Value = Value.lstrip('L') #.strip('"')
947 Value = eval(Value) # translate escape character
948 NewValue = '{'
949 for Index in range(0,len(Value)):
950 if Unicode:
951 NewValue = NewValue + str(ord(Value[Index]) % 0x10000) + ', '
952 else:
953 NewValue = NewValue + str(ord(Value[Index]) % 0x100) + ', '
954 if Unicode:
955 ArraySize = ArraySize / 2;
956
957 if ArraySize < (len(Value) + 1):
958 EdkLogger.error("build", AUTOGEN_ERROR,
959 "The maximum size of VOID* type PCD '%s.%s' is less than its actual size occupied." % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
960 ExtraData="[%s]" % str(Info))
961 Value = NewValue + '0 }'
962 Array = '[%d]' % ArraySize
963 #
964 # skip casting for fixed at build since it breaks ARM assembly.
965 # Long term we need PCD macros that work in assembly
966 #
967 elif Pcd.Type != TAB_PCDS_FIXED_AT_BUILD:
968 Value = "((%s)%s)" % (Pcd.DatumType, Value)
969
970 if Pcd.Type == TAB_PCDS_PATCHABLE_IN_MODULE:
971 PcdValueName = '_PCD_PATCHABLE_VALUE_' + Pcd.TokenCName
972 else:
973 PcdValueName = '_PCD_VALUE_' + Pcd.TokenCName
974
975 if Pcd.DatumType == 'VOID*':
976 #
977 # For unicode, UINT16 array will be generated, so the alignment of unicode is guaranteed.
978 #
979 if Unicode:
980 AutoGenH.Append('#define %s %s%s\n' %(PcdValueName, Type, PcdVariableName))
981 AutoGenC.Append('GLOBAL_REMOVE_IF_UNREFERENCED %s UINT16 %s%s = %s;\n' % (Const, PcdVariableName, Array, Value))
982 AutoGenH.Append('extern %s UINT16 %s%s;\n' %(Const, PcdVariableName, Array))
983 AutoGenH.Append('#define %s %s%s\n' %(GetModeName, Type, PcdVariableName))
984 else:
985 AutoGenH.Append('#define %s %s%s\n' %(PcdValueName, Type, PcdVariableName))
986 AutoGenC.Append('GLOBAL_REMOVE_IF_UNREFERENCED %s UINT8 %s%s = %s;\n' % (Const, PcdVariableName, Array, Value))
987 AutoGenH.Append('extern %s UINT8 %s%s;\n' %(Const, PcdVariableName, Array))
988 AutoGenH.Append('#define %s %s%s\n' %(GetModeName, Type, PcdVariableName))
989
990 PcdDataSize = GetPcdSize(Pcd)
991 if Pcd.Type == TAB_PCDS_FIXED_AT_BUILD:
992 AutoGenH.Append('#define %s %s\n' % (FixPcdSizeTokenName, PcdDataSize))
993 AutoGenH.Append('#define %s %s \n' % (GetModeSizeName,FixPcdSizeTokenName))
994
995 if Pcd.Type == TAB_PCDS_PATCHABLE_IN_MODULE:
996 AutoGenH.Append('#define %s %s\n' % (PatchPcdSizeTokenName, Pcd.MaxDatumSize))
997 AutoGenH.Append('#define %s %s \n' % (GetModeSizeName,PatchPcdSizeVariableName))
998 AutoGenH.Append('extern UINTN %s; \n' % PatchPcdSizeVariableName)
999 AutoGenC.Append('GLOBAL_REMOVE_IF_UNREFERENCED UINTN %s = %s;\n' % (PatchPcdSizeVariableName,PcdDataSize))
1000 elif Pcd.Type == TAB_PCDS_PATCHABLE_IN_MODULE:
1001 AutoGenH.Append('#define %s %s\n' %(PcdValueName, Value))
1002 AutoGenC.Append('volatile %s %s %s = %s;\n' %(Const, Pcd.DatumType, PcdVariableName, PcdValueName))
1003 AutoGenH.Append('extern volatile %s %s %s%s;\n' % (Const, Pcd.DatumType, PcdVariableName, Array))
1004 AutoGenH.Append('#define %s %s%s\n' % (GetModeName, Type, PcdVariableName))
1005
1006 PcdDataSize = GetPcdSize(Pcd)
1007 AutoGenH.Append('#define %s %s\n' % (PatchPcdSizeTokenName, PcdDataSize))
1008
1009 AutoGenH.Append('#define %s %s \n' % (GetModeSizeName,PatchPcdSizeVariableName))
1010 AutoGenH.Append('extern UINTN %s; \n' % PatchPcdSizeVariableName)
1011 AutoGenC.Append('GLOBAL_REMOVE_IF_UNREFERENCED UINTN %s = %s;\n' % (PatchPcdSizeVariableName,PcdDataSize))
1012 else:
1013 PcdDataSize = GetPcdSize(Pcd)
1014 AutoGenH.Append('#define %s %s\n' % (FixPcdSizeTokenName, PcdDataSize))
1015 AutoGenH.Append('#define %s %s \n' % (GetModeSizeName,FixPcdSizeTokenName))
1016
1017 AutoGenH.Append('#define %s %s\n' %(PcdValueName, Value))
1018 AutoGenC.Append('GLOBAL_REMOVE_IF_UNREFERENCED %s %s %s = %s;\n' %(Const, Pcd.DatumType, PcdVariableName, PcdValueName))
1019 AutoGenH.Append('extern %s %s %s%s;\n' % (Const, Pcd.DatumType, PcdVariableName, Array))
1020 AutoGenH.Append('#define %s %s%s\n' % (GetModeName, Type, PcdVariableName))
1021
1022 if Pcd.Type == TAB_PCDS_PATCHABLE_IN_MODULE:
1023 if Pcd.DatumType == 'VOID*':
1024 AutoGenH.Append('#define %s(SizeOfBuffer, Buffer) LibPatchPcdSetPtr(_gPcd_BinaryPatch_%s, (UINTN)_PCD_PATCHABLE_%s_SIZE, (SizeOfBuffer), (Buffer))\n' % (SetModeName, Pcd.TokenCName, Pcd.TokenCName))
1025 AutoGenH.Append('#define %s(SizeOfBuffer, Buffer) LibPatchPcdSetPtrS(_gPcd_BinaryPatch_%s, (UINTN)_PCD_PATCHABLE_%s_SIZE, (SizeOfBuffer), (Buffer))\n' % (SetModeStatusName, Pcd.TokenCName, Pcd.TokenCName))
1026 else:
1027 AutoGenH.Append('#define %s(Value) (%s = (Value))\n' % (SetModeName, PcdVariableName))
1028 AutoGenH.Append('#define %s(Value) ((%s = (Value)), RETURN_SUCCESS) \n' % (SetModeStatusName, PcdVariableName))
1029 else:
1030 AutoGenH.Append('//#define %s ASSERT(FALSE) // It is not allowed to set value for a FIXED_AT_BUILD PCD\n' % SetModeName)
1031
1032 ## Create code for library module PCDs
1033 #
1034 # @param Info The ModuleAutoGen object
1035 # @param AutoGenC The TemplateString object for C code
1036 # @param AutoGenH The TemplateString object for header file
1037 # @param Pcd The PCD object
1038 #
1039 def CreateLibraryPcdCode(Info, AutoGenC, AutoGenH, Pcd):
1040 PcdTokenNumber = Info.PlatformInfo.PcdTokenNumber
1041 TokenSpaceGuidCName = Pcd.TokenSpaceGuidCName
1042 TokenCName = Pcd.TokenCName
1043 PcdTokenName = '_PCD_TOKEN_' + TokenCName
1044 FixPcdSizeTokenName = '_PCD_SIZE_' + Pcd.TokenCName
1045 PatchPcdSizeTokenName = '_PCD_PATCHABLE_' + Pcd.TokenCName +'_SIZE'
1046 PatchPcdSizeVariableName = '_gPcd_BinaryPatch_Size_' + Pcd.TokenCName
1047
1048 #
1049 # Write PCDs
1050 #
1051 if Pcd.Type in gDynamicExPcd:
1052 TokenNumber = int(Pcd.TokenValue, 0)
1053 else:
1054 if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName) not in PcdTokenNumber:
1055 # If one of the Source built modules listed in the DSC is not listed in FDF modules,
1056 # and the INF lists a PCD can only use the PcdsDynamic access method (it is only
1057 # listed in the DEC file that declares the PCD as PcdsDynamic), then build tool will
1058 # report warning message notify the PI that they are attempting to build a module
1059 # that must be included in a flash image in order to be functional. These Dynamic PCD
1060 # will not be added into the Database unless it is used by other modules that are
1061 # included in the FDF file.
1062 # In this case, just assign an invalid token number to make it pass build.
1063 if Pcd.Type in PCD_DYNAMIC_TYPE_LIST:
1064 TokenNumber = 0
1065 else:
1066 EdkLogger.error("build", AUTOGEN_ERROR,
1067 "No generated token number for %s.%s\n" % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
1068 ExtraData="[%s]" % str(Info))
1069 else:
1070 TokenNumber = PcdTokenNumber[Pcd.TokenCName, Pcd.TokenSpaceGuidCName]
1071
1072 if Pcd.Type not in gItemTypeStringDatabase:
1073 EdkLogger.error("build", AUTOGEN_ERROR,
1074 "Unknown PCD type [%s] of PCD %s.%s" % (Pcd.Type, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
1075 ExtraData="[%s]" % str(Info))
1076 if Pcd.DatumType not in gDatumSizeStringDatabase:
1077 EdkLogger.error("build", AUTOGEN_ERROR,
1078 "Unknown datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
1079 ExtraData="[%s]" % str(Info))
1080
1081 DatumType = Pcd.DatumType
1082 DatumSize = gDatumSizeStringDatabaseH[DatumType]
1083 DatumSizeLib= gDatumSizeStringDatabaseLib[DatumType]
1084 GetModeName = '_PCD_GET_MODE_' + DatumSize + '_' + TokenCName
1085 SetModeName = '_PCD_SET_MODE_' + DatumSize + '_' + TokenCName
1086 SetModeStatusName = '_PCD_SET_MODE_' + DatumSize + '_S_' + TokenCName
1087 GetModeSizeName = '_PCD_GET_MODE_SIZE' + '_' + Pcd.TokenCName
1088
1089 Type = ''
1090 Array = ''
1091 if Pcd.DatumType == 'VOID*':
1092 Type = '(VOID *)'
1093 Array = '[]'
1094 PcdItemType = Pcd.Type
1095 PcdExCNameList = []
1096 if PcdItemType in gDynamicExPcd:
1097 PcdExTokenName = '_PCD_TOKEN_' + TokenSpaceGuidCName + '_' + Pcd.TokenCName
1098 AutoGenH.Append('\n#define %s %dU\n' % (PcdExTokenName, TokenNumber))
1099
1100 if Info.IsLibrary:
1101 PcdList = Info.LibraryPcdList
1102 else:
1103 PcdList = Info.ModulePcdList
1104 for PcdModule in PcdList:
1105 if PcdModule.Type in gDynamicExPcd:
1106 PcdExCNameList.append(PcdModule.TokenCName)
1107 # Be compatible with the current code which using PcdGet/Set for DynamicEx Pcd.
1108 # If only PcdGet/Set used in all Pcds with different CName, it should succeed to build.
1109 # If PcdGet/Set used in the Pcds with different Guids but same CName, it should failed to build.
1110 if PcdExCNameList.count(Pcd.TokenCName) > 1:
1111 AutoGenH.Append('// Disabled the macros, as PcdToken and PcdGet/Set are not allowed in the case that more than one DynamicEx Pcds are different Guids but same CName.\n')
1112 AutoGenH.Append('// #define %s %s\n' % (PcdTokenName, PcdExTokenName))
1113 AutoGenH.Append('// #define %s LibPcdGetEx%s(&%s, %s)\n' % (GetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
1114 AutoGenH.Append('// #define %s LibPcdGetExSize(&%s, %s \n' % (GetModeSizeName,Pcd.TokenSpaceGuidCName, PcdTokenName))
1115 if Pcd.DatumType == 'VOID*':
1116 AutoGenH.Append('// #define %s(SizeOfBuffer, Buffer) LibPcdSetEx%s(&%s, %s, (SizeOfBuffer), (Buffer))\n' % (SetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
1117 AutoGenH.Append('// #define %s(SizeOfBuffer, Buffer) LibPcdSetEx%sS(&%s, %s, (SizeOfBuffer), (Buffer))\n' % (SetModeStatusName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
1118 else:
1119 AutoGenH.Append('// #define %s(Value) LibPcdSetEx%s(&%s, %s, (Value))\n' % (SetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
1120 AutoGenH.Append('// #define %s(Value) LibPcdSetEx%sS(&%s, %s, (Value))\n' % (SetModeStatusName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
1121 else:
1122 AutoGenH.Append('#define %s %s\n' % (PcdTokenName, PcdExTokenName))
1123 AutoGenH.Append('#define %s LibPcdGetEx%s(&%s, %s)\n' % (GetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
1124 AutoGenH.Append('#define %s LibPcdGetExSize(&%s, %s)\n' % (GetModeSizeName,Pcd.TokenSpaceGuidCName, PcdTokenName))
1125 if Pcd.DatumType == 'VOID*':
1126 AutoGenH.Append('#define %s(SizeOfBuffer, Buffer) LibPcdSetEx%s(&%s, %s, (SizeOfBuffer), (Buffer))\n' % (SetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
1127 AutoGenH.Append('#define %s(SizeOfBuffer, Buffer) LibPcdSetEx%sS(&%s, %s, (SizeOfBuffer), (Buffer))\n' % (SetModeStatusName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
1128 else:
1129 AutoGenH.Append('#define %s(Value) LibPcdSetEx%s(&%s, %s, (Value))\n' % (SetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
1130 AutoGenH.Append('#define %s(Value) LibPcdSetEx%sS(&%s, %s, (Value))\n' % (SetModeStatusName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
1131 else:
1132 AutoGenH.Append('#define _PCD_TOKEN_%s %dU\n' % (TokenCName, TokenNumber))
1133 if PcdItemType in gDynamicPcd:
1134 AutoGenH.Append('#define %s LibPcdGet%s(%s)\n' % (GetModeName, DatumSizeLib, PcdTokenName))
1135 AutoGenH.Append('#define %s LibPcdGetSize(%s)\n' % (GetModeSizeName, PcdTokenName))
1136 if DatumType == 'VOID*':
1137 AutoGenH.Append('#define %s(SizeOfBuffer, Buffer) LibPcdSet%s(%s, (SizeOfBuffer), (Buffer))\n' %(SetModeName, DatumSizeLib, PcdTokenName))
1138 AutoGenH.Append('#define %s(SizeOfBuffer, Buffer) LibPcdSet%sS(%s, (SizeOfBuffer), (Buffer))\n' % (SetModeStatusName, DatumSizeLib, PcdTokenName))
1139 else:
1140 AutoGenH.Append('#define %s(Value) LibPcdSet%s(%s, (Value))\n' % (SetModeName, DatumSizeLib, PcdTokenName))
1141 AutoGenH.Append('#define %s(Value) LibPcdSet%sS(%s, (Value))\n' % (SetModeStatusName, DatumSizeLib, PcdTokenName))
1142 if PcdItemType == TAB_PCDS_PATCHABLE_IN_MODULE:
1143 PcdVariableName = '_gPcd_' + gItemTypeStringDatabase[TAB_PCDS_PATCHABLE_IN_MODULE] + '_' + TokenCName
1144 AutoGenH.Append('extern volatile %s _gPcd_BinaryPatch_%s%s;\n' %(DatumType, TokenCName, Array) )
1145 AutoGenH.Append('#define %s %s_gPcd_BinaryPatch_%s\n' %(GetModeName, Type, TokenCName))
1146 AutoGenH.Append('#define %s(Value) (%s = (Value))\n' % (SetModeName, PcdVariableName))
1147 AutoGenH.Append('#define %s(Value) ((%s = (Value)), RETURN_SUCCESS)\n' % (SetModeStatusName, PcdVariableName))
1148
1149 PcdDataSize = GetPcdSize(Pcd)
1150 AutoGenH.Append('#define %s %s\n' % (PatchPcdSizeTokenName, PcdDataSize))
1151 AutoGenH.Append('#define %s %s\n' % (GetModeSizeName,PatchPcdSizeVariableName))
1152 AutoGenH.Append('extern UINTN %s; \n' % PatchPcdSizeVariableName)
1153
1154 if PcdItemType == TAB_PCDS_FIXED_AT_BUILD or PcdItemType == TAB_PCDS_FEATURE_FLAG:
1155 key = ".".join((Pcd.TokenSpaceGuidCName,Pcd.TokenCName))
1156
1157 if DatumType == 'VOID*' and Array == '[]':
1158 DatumType = ['UINT8', 'UINT16'][Pcd.DefaultValue[0] == 'L']
1159 AutoGenH.Append('extern const %s _gPcd_FixedAtBuild_%s%s;\n' %(DatumType, TokenCName, Array))
1160 AutoGenH.Append('#define %s %s_gPcd_FixedAtBuild_%s\n' %(GetModeName, Type, TokenCName))
1161 AutoGenH.Append('//#define %s ASSERT(FALSE) // It is not allowed to set value for a FIXED_AT_BUILD PCD\n' % SetModeName)
1162
1163 if PcdItemType == TAB_PCDS_FIXED_AT_BUILD and key in Info.ConstPcd:
1164 AutoGenH.Append('#define _PCD_VALUE_%s %s\n' %(TokenCName, Pcd.DefaultValue))
1165
1166 if PcdItemType == TAB_PCDS_FIXED_AT_BUILD:
1167 PcdDataSize = GetPcdSize(Pcd)
1168 AutoGenH.Append('#define %s %s\n' % (FixPcdSizeTokenName, PcdDataSize))
1169 AutoGenH.Append('#define %s %s\n' % (GetModeSizeName,FixPcdSizeTokenName))
1170
1171 ## Create code for library constructor
1172 #
1173 # @param Info The ModuleAutoGen object
1174 # @param AutoGenC The TemplateString object for C code
1175 # @param AutoGenH The TemplateString object for header file
1176 #
1177 def CreateLibraryConstructorCode(Info, AutoGenC, AutoGenH):
1178 #
1179 # Library Constructors
1180 #
1181 ConstructorPrototypeString = TemplateString()
1182 ConstructorCallingString = TemplateString()
1183 if Info.IsLibrary:
1184 DependentLibraryList = [Info.Module]
1185 else:
1186 DependentLibraryList = Info.DependentLibraryList
1187 for Lib in DependentLibraryList:
1188 if len(Lib.ConstructorList) <= 0:
1189 continue
1190 Dict = {'Function':Lib.ConstructorList}
1191 if Lib.ModuleType in ['BASE', 'SEC']:
1192 ConstructorPrototypeString.Append(gLibraryStructorPrototype['BASE'].Replace(Dict))
1193 ConstructorCallingString.Append(gLibraryStructorCall['BASE'].Replace(Dict))
1194 elif Lib.ModuleType in ['PEI_CORE','PEIM']:
1195 ConstructorPrototypeString.Append(gLibraryStructorPrototype['PEI'].Replace(Dict))
1196 ConstructorCallingString.Append(gLibraryStructorCall['PEI'].Replace(Dict))
1197 elif Lib.ModuleType in ['DXE_CORE','DXE_DRIVER','DXE_SMM_DRIVER','DXE_RUNTIME_DRIVER',
1198 'DXE_SAL_DRIVER','UEFI_DRIVER','UEFI_APPLICATION','SMM_CORE']:
1199 ConstructorPrototypeString.Append(gLibraryStructorPrototype['DXE'].Replace(Dict))
1200 ConstructorCallingString.Append(gLibraryStructorCall['DXE'].Replace(Dict))
1201
1202 if str(ConstructorPrototypeString) == '':
1203 ConstructorPrototypeList = []
1204 else:
1205 ConstructorPrototypeList = [str(ConstructorPrototypeString)]
1206 if str(ConstructorCallingString) == '':
1207 ConstructorCallingList = []
1208 else:
1209 ConstructorCallingList = [str(ConstructorCallingString)]
1210
1211 Dict = {
1212 'Type' : 'Constructor',
1213 'FunctionPrototype' : ConstructorPrototypeList,
1214 'FunctionCall' : ConstructorCallingList
1215 }
1216 if Info.IsLibrary:
1217 AutoGenH.Append("${BEGIN}${FunctionPrototype}${END}", Dict)
1218 else:
1219 if Info.ModuleType in ['BASE', 'SEC']:
1220 AutoGenC.Append(gLibraryString['BASE'].Replace(Dict))
1221 elif Info.ModuleType in ['PEI_CORE','PEIM']:
1222 AutoGenC.Append(gLibraryString['PEI'].Replace(Dict))
1223 elif Info.ModuleType in ['DXE_CORE','DXE_DRIVER','DXE_SMM_DRIVER','DXE_RUNTIME_DRIVER',
1224 'DXE_SAL_DRIVER','UEFI_DRIVER','UEFI_APPLICATION','SMM_CORE']:
1225 AutoGenC.Append(gLibraryString['DXE'].Replace(Dict))
1226
1227 ## Create code for library destructor
1228 #
1229 # @param Info The ModuleAutoGen object
1230 # @param AutoGenC The TemplateString object for C code
1231 # @param AutoGenH The TemplateString object for header file
1232 #
1233 def CreateLibraryDestructorCode(Info, AutoGenC, AutoGenH):
1234 #
1235 # Library Destructors
1236 #
1237 DestructorPrototypeString = TemplateString()
1238 DestructorCallingString = TemplateString()
1239 if Info.IsLibrary:
1240 DependentLibraryList = [Info.Module]
1241 else:
1242 DependentLibraryList = Info.DependentLibraryList
1243 for Index in range(len(DependentLibraryList)-1, -1, -1):
1244 Lib = DependentLibraryList[Index]
1245 if len(Lib.DestructorList) <= 0:
1246 continue
1247 Dict = {'Function':Lib.DestructorList}
1248 if Lib.ModuleType in ['BASE', 'SEC']:
1249 DestructorPrototypeString.Append(gLibraryStructorPrototype['BASE'].Replace(Dict))
1250 DestructorCallingString.Append(gLibraryStructorCall['BASE'].Replace(Dict))
1251 elif Lib.ModuleType in ['PEI_CORE','PEIM']:
1252 DestructorPrototypeString.Append(gLibraryStructorPrototype['PEI'].Replace(Dict))
1253 DestructorCallingString.Append(gLibraryStructorCall['PEI'].Replace(Dict))
1254 elif Lib.ModuleType in ['DXE_CORE','DXE_DRIVER','DXE_SMM_DRIVER','DXE_RUNTIME_DRIVER',
1255 'DXE_SAL_DRIVER','UEFI_DRIVER','UEFI_APPLICATION', 'SMM_CORE']:
1256 DestructorPrototypeString.Append(gLibraryStructorPrototype['DXE'].Replace(Dict))
1257 DestructorCallingString.Append(gLibraryStructorCall['DXE'].Replace(Dict))
1258
1259 if str(DestructorPrototypeString) == '':
1260 DestructorPrototypeList = []
1261 else:
1262 DestructorPrototypeList = [str(DestructorPrototypeString)]
1263 if str(DestructorCallingString) == '':
1264 DestructorCallingList = []
1265 else:
1266 DestructorCallingList = [str(DestructorCallingString)]
1267
1268 Dict = {
1269 'Type' : 'Destructor',
1270 'FunctionPrototype' : DestructorPrototypeList,
1271 'FunctionCall' : DestructorCallingList
1272 }
1273 if Info.IsLibrary:
1274 AutoGenH.Append("${BEGIN}${FunctionPrototype}${END}", Dict)
1275 else:
1276 if Info.ModuleType in ['BASE', 'SEC']:
1277 AutoGenC.Append(gLibraryString['BASE'].Replace(Dict))
1278 elif Info.ModuleType in ['PEI_CORE','PEIM']:
1279 AutoGenC.Append(gLibraryString['PEI'].Replace(Dict))
1280 elif Info.ModuleType in ['DXE_CORE','DXE_DRIVER','DXE_SMM_DRIVER','DXE_RUNTIME_DRIVER',
1281 'DXE_SAL_DRIVER','UEFI_DRIVER','UEFI_APPLICATION','SMM_CORE']:
1282 AutoGenC.Append(gLibraryString['DXE'].Replace(Dict))
1283
1284
1285 ## Create code for ModuleEntryPoint
1286 #
1287 # @param Info The ModuleAutoGen object
1288 # @param AutoGenC The TemplateString object for C code
1289 # @param AutoGenH The TemplateString object for header file
1290 #
1291 def CreateModuleEntryPointCode(Info, AutoGenC, AutoGenH):
1292 if Info.IsLibrary or Info.ModuleType in ['USER_DEFINED', 'SEC']:
1293 return
1294 #
1295 # Module Entry Points
1296 #
1297 NumEntryPoints = len(Info.Module.ModuleEntryPointList)
1298 if 'PI_SPECIFICATION_VERSION' in Info.Module.Specification:
1299 PiSpecVersion = Info.Module.Specification['PI_SPECIFICATION_VERSION']
1300 else:
1301 PiSpecVersion = '0x00000000'
1302 if 'UEFI_SPECIFICATION_VERSION' in Info.Module.Specification:
1303 UefiSpecVersion = Info.Module.Specification['UEFI_SPECIFICATION_VERSION']
1304 else:
1305 UefiSpecVersion = '0x00000000'
1306 Dict = {
1307 'Function' : Info.Module.ModuleEntryPointList,
1308 'PiSpecVersion' : PiSpecVersion + 'U',
1309 'UefiSpecVersion': UefiSpecVersion + 'U'
1310 }
1311
1312 if Info.ModuleType in ['PEI_CORE', 'DXE_CORE', 'SMM_CORE']:
1313 if Info.SourceFileList <> None and Info.SourceFileList <> []:
1314 if NumEntryPoints != 1:
1315 EdkLogger.error(
1316 "build",
1317 AUTOGEN_ERROR,
1318 '%s must have exactly one entry point' % Info.ModuleType,
1319 File=str(Info),
1320 ExtraData= ", ".join(Info.Module.ModuleEntryPointList)
1321 )
1322 if Info.ModuleType == 'PEI_CORE':
1323 AutoGenC.Append(gPeiCoreEntryPointString.Replace(Dict))
1324 AutoGenH.Append(gPeiCoreEntryPointPrototype.Replace(Dict))
1325 elif Info.ModuleType == 'DXE_CORE':
1326 AutoGenC.Append(gDxeCoreEntryPointString.Replace(Dict))
1327 AutoGenH.Append(gDxeCoreEntryPointPrototype.Replace(Dict))
1328 elif Info.ModuleType == 'SMM_CORE':
1329 AutoGenC.Append(gSmmCoreEntryPointString.Replace(Dict))
1330 AutoGenH.Append(gSmmCoreEntryPointPrototype.Replace(Dict))
1331 elif Info.ModuleType == 'PEIM':
1332 if NumEntryPoints < 2:
1333 AutoGenC.Append(gPeimEntryPointString[NumEntryPoints].Replace(Dict))
1334 else:
1335 AutoGenC.Append(gPeimEntryPointString[2].Replace(Dict))
1336 AutoGenH.Append(gPeimEntryPointPrototype.Replace(Dict))
1337 elif Info.ModuleType in ['DXE_RUNTIME_DRIVER','DXE_DRIVER','DXE_SAL_DRIVER','UEFI_DRIVER']:
1338 if NumEntryPoints < 2:
1339 AutoGenC.Append(gUefiDriverEntryPointString[NumEntryPoints].Replace(Dict))
1340 else:
1341 AutoGenC.Append(gUefiDriverEntryPointString[2].Replace(Dict))
1342 AutoGenH.Append(gUefiDriverEntryPointPrototype.Replace(Dict))
1343 elif Info.ModuleType == 'DXE_SMM_DRIVER':
1344 if NumEntryPoints == 0:
1345 AutoGenC.Append(gDxeSmmEntryPointString[0].Replace(Dict))
1346 else:
1347 AutoGenC.Append(gDxeSmmEntryPointString[1].Replace(Dict))
1348 AutoGenH.Append(gDxeSmmEntryPointPrototype.Replace(Dict))
1349 elif Info.ModuleType == 'UEFI_APPLICATION':
1350 if NumEntryPoints < 2:
1351 AutoGenC.Append(gUefiApplicationEntryPointString[NumEntryPoints].Replace(Dict))
1352 else:
1353 AutoGenC.Append(gUefiApplicationEntryPointString[2].Replace(Dict))
1354 AutoGenH.Append(gUefiApplicationEntryPointPrototype.Replace(Dict))
1355
1356 ## Create code for ModuleUnloadImage
1357 #
1358 # @param Info The ModuleAutoGen object
1359 # @param AutoGenC The TemplateString object for C code
1360 # @param AutoGenH The TemplateString object for header file
1361 #
1362 def CreateModuleUnloadImageCode(Info, AutoGenC, AutoGenH):
1363 if Info.IsLibrary or Info.ModuleType in ['USER_DEFINED', 'SEC']:
1364 return
1365 #
1366 # Unload Image Handlers
1367 #
1368 NumUnloadImage = len(Info.Module.ModuleUnloadImageList)
1369 Dict = {'Count':str(NumUnloadImage) + 'U', 'Function':Info.Module.ModuleUnloadImageList}
1370 if NumUnloadImage < 2:
1371 AutoGenC.Append(gUefiUnloadImageString[NumUnloadImage].Replace(Dict))
1372 else:
1373 AutoGenC.Append(gUefiUnloadImageString[2].Replace(Dict))
1374 AutoGenH.Append(gUefiUnloadImagePrototype.Replace(Dict))
1375
1376 ## Create code for GUID
1377 #
1378 # @param Info The ModuleAutoGen object
1379 # @param AutoGenC The TemplateString object for C code
1380 # @param AutoGenH The TemplateString object for header file
1381 #
1382 def CreateGuidDefinitionCode(Info, AutoGenC, AutoGenH):
1383 if Info.IsLibrary:
1384 return
1385
1386 if Info.ModuleType in ["USER_DEFINED", "BASE"]:
1387 GuidType = "GUID"
1388 else:
1389 GuidType = "EFI_GUID"
1390
1391 if Info.GuidList:
1392 AutoGenC.Append("\n// Guids\n")
1393 AutoGenH.Append("\n// Guids\n")
1394 #
1395 # GUIDs
1396 #
1397 for Key in Info.GuidList:
1398 AutoGenC.Append('GLOBAL_REMOVE_IF_UNREFERENCED %s %s = %s;\n' % (GuidType, Key, Info.GuidList[Key]))
1399 AutoGenH.Append('extern %s %s;\n' % (GuidType, Key))
1400
1401 ## Create code for protocol
1402 #
1403 # @param Info The ModuleAutoGen object
1404 # @param AutoGenC The TemplateString object for C code
1405 # @param AutoGenH The TemplateString object for header file
1406 #
1407 def CreateProtocolDefinitionCode(Info, AutoGenC, AutoGenH):
1408 if Info.IsLibrary:
1409 return
1410
1411 if Info.ModuleType in ["USER_DEFINED", "BASE"]:
1412 GuidType = "GUID"
1413 else:
1414 GuidType = "EFI_GUID"
1415
1416 if Info.ProtocolList:
1417 AutoGenC.Append("\n// Protocols\n")
1418 AutoGenH.Append("\n// Protocols\n")
1419 #
1420 # Protocol GUIDs
1421 #
1422 for Key in Info.ProtocolList:
1423 AutoGenC.Append('GLOBAL_REMOVE_IF_UNREFERENCED %s %s = %s;\n' % (GuidType, Key, Info.ProtocolList[Key]))
1424 AutoGenH.Append('extern %s %s;\n' % (GuidType, Key))
1425
1426 ## Create code for PPI
1427 #
1428 # @param Info The ModuleAutoGen object
1429 # @param AutoGenC The TemplateString object for C code
1430 # @param AutoGenH The TemplateString object for header file
1431 #
1432 def CreatePpiDefinitionCode(Info, AutoGenC, AutoGenH):
1433 if Info.IsLibrary:
1434 return
1435
1436 if Info.ModuleType in ["USER_DEFINED", "BASE"]:
1437 GuidType = "GUID"
1438 else:
1439 GuidType = "EFI_GUID"
1440
1441 if Info.PpiList:
1442 AutoGenC.Append("\n// PPIs\n")
1443 AutoGenH.Append("\n// PPIs\n")
1444 #
1445 # PPI GUIDs
1446 #
1447 for Key in Info.PpiList:
1448 AutoGenC.Append('GLOBAL_REMOVE_IF_UNREFERENCED %s %s = %s;\n' % (GuidType, Key, Info.PpiList[Key]))
1449 AutoGenH.Append('extern %s %s;\n' % (GuidType, Key))
1450
1451 ## Create code for PCD
1452 #
1453 # @param Info The ModuleAutoGen object
1454 # @param AutoGenC The TemplateString object for C code
1455 # @param AutoGenH The TemplateString object for header file
1456 #
1457 def CreatePcdCode(Info, AutoGenC, AutoGenH):
1458
1459 # Collect Token Space GUIDs used by DynamicEc PCDs
1460 TokenSpaceList = []
1461 for Pcd in Info.ModulePcdList:
1462 if Pcd.Type in gDynamicExPcd and Pcd.TokenSpaceGuidCName not in TokenSpaceList:
1463 TokenSpaceList += [Pcd.TokenSpaceGuidCName]
1464
1465 # Add extern declarations to AutoGen.h if one or more Token Space GUIDs were found
1466 if TokenSpaceList <> []:
1467 AutoGenH.Append("\n// Definition of PCD Token Space GUIDs used in this module\n\n")
1468 if Info.ModuleType in ["USER_DEFINED", "BASE"]:
1469 GuidType = "GUID"
1470 else:
1471 GuidType = "EFI_GUID"
1472 for Item in TokenSpaceList:
1473 AutoGenH.Append('extern %s %s;\n' % (GuidType, Item))
1474
1475 if Info.IsLibrary:
1476 if Info.ModulePcdList:
1477 AutoGenH.Append("\n// PCD definitions\n")
1478 for Pcd in Info.ModulePcdList:
1479 CreateLibraryPcdCode(Info, AutoGenC, AutoGenH, Pcd)
1480 DynExPcdTokenNumberMapping (Info, AutoGenH)
1481 else:
1482 if Info.ModulePcdList:
1483 AutoGenH.Append("\n// Definition of PCDs used in this module\n")
1484 AutoGenC.Append("\n// Definition of PCDs used in this module\n")
1485 for Pcd in Info.ModulePcdList:
1486 CreateModulePcdCode(Info, AutoGenC, AutoGenH, Pcd)
1487 DynExPcdTokenNumberMapping (Info, AutoGenH)
1488 if Info.LibraryPcdList:
1489 AutoGenH.Append("\n// Definition of PCDs used in libraries is in AutoGen.c\n")
1490 AutoGenC.Append("\n// Definition of PCDs used in libraries\n")
1491 for Pcd in Info.LibraryPcdList:
1492 CreateModulePcdCode(Info, AutoGenC, AutoGenC, Pcd)
1493 CreatePcdDatabaseCode(Info, AutoGenC, AutoGenH)
1494
1495 ## Create code for unicode string definition
1496 #
1497 # @param Info The ModuleAutoGen object
1498 # @param AutoGenC The TemplateString object for C code
1499 # @param AutoGenH The TemplateString object for header file
1500 # @param UniGenCFlag UniString is generated into AutoGen C file when it is set to True
1501 # @param UniGenBinBuffer Buffer to store uni string package data
1502 #
1503 def CreateUnicodeStringCode(Info, AutoGenC, AutoGenH, UniGenCFlag, UniGenBinBuffer):
1504 WorkingDir = os.getcwd()
1505 os.chdir(Info.WorkspaceDir)
1506
1507 IncList = [Info.MetaFile.Dir]
1508 # Get all files under [Sources] section in inf file for EDK-II module
1509 EDK2Module = True
1510 SrcList = [F for F in Info.SourceFileList]
1511 if Info.AutoGenVersion < 0x00010005:
1512 EDK2Module = False
1513 # Get all files under the module directory for EDK-I module
1514 Cwd = os.getcwd()
1515 os.chdir(Info.MetaFile.Dir)
1516 for Root, Dirs, Files in os.walk("."):
1517 if 'CVS' in Dirs:
1518 Dirs.remove('CVS')
1519 if '.svn' in Dirs:
1520 Dirs.remove('.svn')
1521 for File in Files:
1522 File = PathClass(os.path.join(Root, File), Info.MetaFile.Dir)
1523 if File in SrcList:
1524 continue
1525 SrcList.append(File)
1526 os.chdir(Cwd)
1527
1528 if 'BUILD' in Info.BuildOption and Info.BuildOption['BUILD']['FLAGS'].find('-c') > -1:
1529 CompatibleMode = True
1530 else:
1531 CompatibleMode = False
1532
1533 #
1534 # -s is a temporary option dedicated for building .UNI files with ISO 639-2 language codes of EDK Shell in EDK2
1535 #
1536 if 'BUILD' in Info.BuildOption and Info.BuildOption['BUILD']['FLAGS'].find('-s') > -1:
1537 if CompatibleMode:
1538 EdkLogger.error("build", AUTOGEN_ERROR,
1539 "-c and -s build options should be used exclusively",
1540 ExtraData="[%s]" % str(Info))
1541 ShellMode = True
1542 else:
1543 ShellMode = False
1544
1545 #RFC4646 is only for EDKII modules and ISO639-2 for EDK modules
1546 if EDK2Module:
1547 FilterInfo = [EDK2Module] + [Info.PlatformInfo.Platform.RFCLanguages]
1548 else:
1549 FilterInfo = [EDK2Module] + [Info.PlatformInfo.Platform.ISOLanguages]
1550 Header, Code = GetStringFiles(Info.UnicodeFileList, SrcList, IncList, Info.IncludePathList, ['.uni', '.inf'], Info.Name, CompatibleMode, ShellMode, UniGenCFlag, UniGenBinBuffer, FilterInfo)
1551 if CompatibleMode or UniGenCFlag:
1552 AutoGenC.Append("\n//\n//Unicode String Pack Definition\n//\n")
1553 AutoGenC.Append(Code)
1554 AutoGenC.Append("\n")
1555 AutoGenH.Append("\n//\n//Unicode String ID\n//\n")
1556 AutoGenH.Append(Header)
1557 if CompatibleMode or UniGenCFlag:
1558 AutoGenH.Append("\n#define STRING_ARRAY_NAME %sStrings\n" % Info.Name)
1559 os.chdir(WorkingDir)
1560
1561 ## Create common code
1562 #
1563 # @param Info The ModuleAutoGen object
1564 # @param AutoGenC The TemplateString object for C code
1565 # @param AutoGenH The TemplateString object for header file
1566 #
1567 def CreateHeaderCode(Info, AutoGenC, AutoGenH):
1568 # file header
1569 AutoGenH.Append(gAutoGenHeaderString.Replace({'FileName':'AutoGen.h'}))
1570 # header file Prologue
1571 AutoGenH.Append(gAutoGenHPrologueString.Replace({'File':'AUTOGENH','Guid':Info.Guid.replace('-','_')}))
1572 AutoGenH.Append(gAutoGenHCppPrologueString)
1573 if Info.AutoGenVersion >= 0x00010005:
1574 # header files includes
1575 AutoGenH.Append("#include <%s>\n" % gBasicHeaderFile)
1576 if Info.ModuleType in gModuleTypeHeaderFile \
1577 and gModuleTypeHeaderFile[Info.ModuleType][0] != gBasicHeaderFile:
1578 AutoGenH.Append("#include <%s>\n" % gModuleTypeHeaderFile[Info.ModuleType][0])
1579 #
1580 # if either PcdLib in [LibraryClasses] sections or there exist Pcd section, add PcdLib.h
1581 # As if modules only uses FixedPcd, then PcdLib is not needed in [LibraryClasses] section.
1582 #
1583 if 'PcdLib' in Info.Module.LibraryClasses or Info.Module.Pcds:
1584 AutoGenH.Append("#include <Library/PcdLib.h>\n")
1585
1586 AutoGenH.Append('\nextern GUID gEfiCallerIdGuid;')
1587 AutoGenH.Append('\nextern CHAR8 *gEfiCallerBaseName;\n\n')
1588
1589 if Info.IsLibrary:
1590 return
1591
1592 AutoGenH.Append("#define EFI_CALLER_ID_GUID \\\n %s\n" % GuidStringToGuidStructureString(Info.Guid))
1593
1594 if Info.IsLibrary:
1595 return
1596 # C file header
1597 AutoGenC.Append(gAutoGenHeaderString.Replace({'FileName':'AutoGen.c'}))
1598 if Info.AutoGenVersion >= 0x00010005:
1599 # C file header files includes
1600 if Info.ModuleType in gModuleTypeHeaderFile:
1601 for Inc in gModuleTypeHeaderFile[Info.ModuleType]:
1602 AutoGenC.Append("#include <%s>\n" % Inc)
1603 else:
1604 AutoGenC.Append("#include <%s>\n" % gBasicHeaderFile)
1605
1606 #
1607 # Publish the CallerId Guid
1608 #
1609 AutoGenC.Append('\nGLOBAL_REMOVE_IF_UNREFERENCED GUID gEfiCallerIdGuid = %s;\n' % GuidStringToGuidStructureString(Info.Guid))
1610 AutoGenC.Append('\nGLOBAL_REMOVE_IF_UNREFERENCED CHAR8 *gEfiCallerBaseName = "%s";\n' % Info.Name)
1611
1612 ## Create common code for header file
1613 #
1614 # @param Info The ModuleAutoGen object
1615 # @param AutoGenC The TemplateString object for C code
1616 # @param AutoGenH The TemplateString object for header file
1617 #
1618 def CreateFooterCode(Info, AutoGenC, AutoGenH):
1619 AutoGenH.Append(gAutoGenHEpilogueString)
1620
1621 ## Create code for a module
1622 #
1623 # @param Info The ModuleAutoGen object
1624 # @param AutoGenC The TemplateString object for C code
1625 # @param AutoGenH The TemplateString object for header file
1626 # @param UniGenCFlag UniString is generated into AutoGen C file when it is set to True
1627 # @param UniGenBinBuffer Buffer to store uni string package data
1628 #
1629 def CreateCode(Info, AutoGenC, AutoGenH, StringH, UniGenCFlag, UniGenBinBuffer):
1630 CreateHeaderCode(Info, AutoGenC, AutoGenH)
1631
1632 if Info.AutoGenVersion >= 0x00010005:
1633 CreateGuidDefinitionCode(Info, AutoGenC, AutoGenH)
1634 CreateProtocolDefinitionCode(Info, AutoGenC, AutoGenH)
1635 CreatePpiDefinitionCode(Info, AutoGenC, AutoGenH)
1636 CreatePcdCode(Info, AutoGenC, AutoGenH)
1637 CreateLibraryConstructorCode(Info, AutoGenC, AutoGenH)
1638 CreateLibraryDestructorCode(Info, AutoGenC, AutoGenH)
1639 CreateModuleEntryPointCode(Info, AutoGenC, AutoGenH)
1640 CreateModuleUnloadImageCode(Info, AutoGenC, AutoGenH)
1641
1642 if Info.UnicodeFileList:
1643 FileName = "%sStrDefs.h" % Info.Name
1644 StringH.Append(gAutoGenHeaderString.Replace({'FileName':FileName}))
1645 StringH.Append(gAutoGenHPrologueString.Replace({'File':'STRDEFS', 'Guid':Info.Guid.replace('-','_')}))
1646 CreateUnicodeStringCode(Info, AutoGenC, StringH, UniGenCFlag, UniGenBinBuffer)
1647
1648 GuidMacros = []
1649 for Guid in Info.Module.Guids:
1650 if Guid in Info.Module.GetGuidsUsedByPcd():
1651 continue
1652 GuidMacros.append('#define %s %s' % (Guid, Info.Module.Guids[Guid]))
1653 for Guid, Value in Info.Module.Protocols.items() + Info.Module.Ppis.items():
1654 GuidMacros.append('#define %s %s' % (Guid, Value))
1655 if GuidMacros:
1656 StringH.Append('\n#ifdef VFRCOMPILE\n%s\n#endif\n' % '\n'.join(GuidMacros))
1657
1658 StringH.Append("\n#endif\n")
1659 AutoGenH.Append('#include "%s"\n' % FileName)
1660
1661 CreateFooterCode(Info, AutoGenC, AutoGenH)
1662
1663 # no generation of AutoGen.c for Edk modules without unicode file
1664 if Info.AutoGenVersion < 0x00010005 and len(Info.UnicodeFileList) == 0:
1665 AutoGenC.String = ''
1666
1667 ## Create the code file
1668 #
1669 # @param FilePath The path of code file
1670 # @param Content The content of code file
1671 # @param IsBinaryFile The flag indicating if the file is binary file or not
1672 #
1673 # @retval True If file content is changed or file doesn't exist
1674 # @retval False If the file exists and the content is not changed
1675 #
1676 def Generate(FilePath, Content, IsBinaryFile):
1677 return SaveFileOnChange(FilePath, Content, IsBinaryFile)
1678