git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@250 6f19259b...
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / pcd / action / CollectPCDAction.java
1 /** @file
2 CollectPCDAction class.
3
4 This action class is to collect PCD information from MSA, SPD, FPD xml file.
5 This class will be used for wizard and build tools, So it can *not* inherit
6 from buildAction or wizardAction.
7
8 Copyright (c) 2006, Intel Corporation
9 All rights reserved. This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
13
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16
17 **/
18 package org.tianocore.build.pcd.action;
19
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.FileReader;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Comparator;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.UUID;
31
32 import org.apache.xmlbeans.XmlException;
33 import org.apache.xmlbeans.XmlObject;
34 import org.tianocore.FrameworkPlatformDescriptionDocument;
35 import org.tianocore.ModuleSADocument;
36 import org.tianocore.PackageSurfaceAreaDocument;
37 import org.tianocore.PcdBuildDeclarationsDocument.PcdBuildDeclarations.PcdBuildData;
38 import org.tianocore.PcdDefinitionsDocument.PcdDefinitions;
39 import org.tianocore.build.autogen.CommonDefinition;
40 import org.tianocore.build.global.GlobalData;
41 import org.tianocore.build.global.SurfaceAreaQuery;
42 import org.tianocore.build.pcd.action.ActionMessage;
43 import org.tianocore.build.pcd.entity.MemoryDatabaseManager;
44 import org.tianocore.build.pcd.entity.SkuInstance;
45 import org.tianocore.build.pcd.entity.Token;
46 import org.tianocore.build.pcd.entity.UsageInstance;
47 import org.tianocore.build.pcd.exception.EntityException;
48
49 class StringTable {
50 private ArrayList<String> al;
51 private ArrayList<String> alComments;
52 private String phase;
53 int len;
54 int bodyStart;
55 int bodyLineNum;
56
57 public StringTable (String phase) {
58 this.phase = phase;
59 al = new ArrayList<String>();
60 alComments = new ArrayList<String>();
61 len = 0;
62 bodyStart = 0;
63 bodyLineNum = 0;
64 }
65
66 public String getSizeMacro () {
67 return String.format(PcdDatabase.StringTableSizeMacro, phase, getSize());
68 }
69
70 private int getSize () {
71 //
72 // We have at least one Unicode Character in the table.
73 //
74 return len == 0 ? 1 : len;
75 }
76
77 public int getTableLen () {
78 return al.size() == 0 ? 1 : al.size();
79 }
80
81 public String getExistanceMacro () {
82 return String.format(PcdDatabase.StringTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");
83 }
84
85 public String getTypeDeclaration () {
86
87 String output;
88
89 final String stringTable = "StringTable";
90 final String tab = "\t";
91 final String newLine = ";\r\n";
92
93 output = "/* StringTable */\r\n";
94
95 if (al.size() == 0) {
96 output += tab + String.format("UINT16 %s[1] /* StringTable is Empty */", stringTable) + newLine;
97 }
98
99 for (int i = 0; i < al.size(); i++) {
100 String str = al.get(i);
101
102 if (i == 0) {
103 //
104 // StringTable is a well-known name in the PCD DXE driver
105 //
106 output += tab + String.format("UINT16 %s[%d] /* %s */", stringTable, str.length() + 1, alComments.get(i)) + newLine;
107 } else {
108 output += tab + String.format("UINT16 %s_%d[%d] /* %s */", stringTable, i, str.length() + 1, alComments.get(i)) + newLine;
109 }
110 }
111
112 return output;
113
114 }
115
116 public ArrayList<String> getInstantiation () {
117 ArrayList<String> output = new ArrayList<String>();
118
119 output.add("/* StringTable */");
120
121 if (al.size() == 0) {
122 output.add("{ 0 }");
123 } else {
124 String str;
125
126 for (int i = 0; i < al.size(); i++) {
127 str = String.format("L\"%s\" /* %s */", al.get(i), alComments.get(i));
128 if (i != al.size() - 1) {
129 str += ",";
130 }
131 output.add(str);
132 }
133 }
134
135 return output;
136 }
137
138 public int add (String str, Token token) {
139 int i;
140
141 i = len;
142 //
143 // Include the NULL character at the end of String
144 //
145 len += str.length() + 1;
146 al.add(str);
147 alComments.add(token.getPrimaryKeyString());
148
149 return i;
150 }
151 }
152
153 class SizeTable {
154 private ArrayList<Integer> al;
155 private ArrayList<String> alComments;
156 private String phase;
157 private int len;
158 private int bodyStart;
159 private int bodyLineNum;
160
161 public SizeTable (String phase) {
162 this.phase = phase;
163 al = new ArrayList<Integer>();
164 alComments = new ArrayList<String>();
165 len = 0;
166 bodyStart = 0;
167 bodyLineNum = 0;
168 }
169
170 public String getTypeDeclaration () {
171 return String.format(PcdDatabase.SizeTableDeclaration, phase);
172 }
173
174 public ArrayList<String> getInstantiation () {
175 ArrayList<String> Output = new ArrayList<String>();
176
177 Output.add("/* SizeTable */");
178 Output.add("{");
179 bodyStart = 2;
180
181 if (al.size() == 0) {
182 Output.add("0");
183 } else {
184 for (int index = 0; index < al.size(); index++) {
185 Integer n = al.get(index);
186 String str = n.toString();
187
188 if (index != (al.size() - 1)) {
189 str += ",";
190 }
191
192 str += " /* " + alComments.get(index) + " */";
193 Output.add(str);
194 bodyLineNum++;
195
196 }
197 }
198 Output.add("}");
199
200 return Output;
201 }
202
203 public int getBodyStart() {
204 return bodyStart;
205 }
206
207 public int getBodyLineNum () {
208 return bodyLineNum;
209 }
210
211 public int add (Token token) {
212 int index = len;
213
214 len++;
215 al.add(token.datumSize);
216 alComments.add(token.getPrimaryKeyString());
217
218 return index;
219 }
220
221 private int getDatumSize(Token token) {
222 /*
223 switch (token.datumType) {
224 case Token.DATUM_TYPE.UINT8:
225 return 1;
226 default:
227 return 0;
228 }
229 */
230 return 0;
231 }
232
233 public int getTableLen () {
234 return al.size() == 0 ? 1 : al.size();
235 }
236
237 }
238
239 class GuidTable {
240 private ArrayList<UUID> al;
241 private ArrayList<String> alComments;
242 private String phase;
243 private int len;
244 private int bodyStart;
245 private int bodyLineNum;
246
247 public GuidTable (String phase) {
248 this.phase = phase;
249 al = new ArrayList<UUID>();
250 alComments = new ArrayList<String>();
251 len = 0;
252 bodyStart = 0;
253 bodyLineNum = 0;
254 }
255
256 public String getSizeMacro () {
257 return String.format(PcdDatabase.GuidTableSizeMacro, phase, getSize());
258 }
259
260 private int getSize () {
261 return (al.size() == 0)? 1 : al.size();
262 }
263
264 public String getExistanceMacro () {
265 return String.format(PcdDatabase.GuidTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");
266 }
267
268 public String getTypeDeclaration () {
269 return String.format(PcdDatabase.GuidTableDeclaration, phase);
270 }
271
272 private String getUuidCString (UUID uuid) {
273 String[] guidStrArray;
274
275 guidStrArray =(uuid.toString()).split("-");
276
277 return String.format("{ 0x%s, 0x%s, 0x%s, { 0x%s, 0x%s, 0x%s, 0x%s, 0x%s, 0x%s, 0x%s, 0x%s } }",
278 guidStrArray[0],
279 guidStrArray[1],
280 guidStrArray[2],
281 (guidStrArray[3].substring(0, 2)),
282 (guidStrArray[3].substring(2, 4)),
283 (guidStrArray[4].substring(0, 2)),
284 (guidStrArray[4].substring(2, 4)),
285 (guidStrArray[4].substring(4, 6)),
286 (guidStrArray[4].substring(6, 8)),
287 (guidStrArray[4].substring(8, 10)),
288 (guidStrArray[4].substring(10, 12))
289 );
290 }
291
292 public ArrayList<String> getInstantiation () {
293 ArrayList<String> Output = new ArrayList<String>();
294
295 Output.add("/* GuidTable */");
296 Output.add("{");
297 bodyStart = 2;
298
299 if (al.size() == 0) {
300 Output.add(getUuidCString(new UUID(0, 0)));
301 }
302
303 for (Object u : al) {
304 UUID uuid = (UUID)u;
305 String str = getUuidCString(uuid);
306
307 if (al.indexOf(u) != (al.size() - 1)) {
308 str += ",";
309 }
310 Output.add(str);
311 bodyLineNum++;
312
313 }
314 Output.add("}");
315
316 return Output;
317 }
318
319 public int getBodyStart() {
320 return bodyStart;
321 }
322
323 public int getBodyLineNum () {
324 return bodyLineNum;
325 }
326
327 public int add (UUID uuid, String name) {
328 int index = len;
329 //
330 // Include the NULL character at the end of String
331 //
332 len++;
333 al.add(uuid);
334
335 return index;
336 }
337
338 public int getTableLen () {
339 return al.size() == 0 ? 0 : al.size();
340 }
341
342 }
343
344 class SkuIdTable {
345 private ArrayList<Integer[]> al;
346 private ArrayList<String> alComment;
347 private String phase;
348 private int len;
349 private int bodyStart;
350 private int bodyLineNum;
351
352 public SkuIdTable (String phase) {
353 this.phase = phase;
354 al = new ArrayList<Integer[]>();
355 alComment = new ArrayList<String>();
356 bodyStart = 0;
357 bodyLineNum = 0;
358 len = 0;
359 }
360
361 public String getSizeMacro () {
362 return String.format(PcdDatabase.SkuIdTableSizeMacro, phase, getSize());
363 }
364
365 private int getSize () {
366 return (al.size() == 0)? 1 : al.size();
367 }
368
369 public String getExistanceMacro () {
370 return String.format(PcdDatabase.SkuTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");
371 }
372
373 public String getTypeDeclaration () {
374 return String.format(PcdDatabase.SkuIdTableDeclaration, phase);
375 }
376
377 public ArrayList<String> getInstantiation () {
378 ArrayList<String> Output = new ArrayList<String> ();
379
380 Output.add("/* SkuIdTable */");
381 Output.add("{");
382 bodyStart = 2;
383
384 if (al.size() == 0) {
385 Output.add("0");
386 }
387
388 for (int index = 0; index < al.size(); index++) {
389 String str;
390
391 str = "/* " + alComment.get(index) + "*/ ";
392 str += "/* MaxSku */ ";
393
394
395 Integer[] ia = al.get(index);
396
397 str += ia[0].toString() + ", ";
398 for (int index2 = 1; index2 < ia.length; index2++) {
399 str += ia[index2].toString();
400 if (index != al.size() - 1) {
401 str += ", ";
402 }
403 }
404
405 Output.add(str);
406 bodyLineNum++;
407
408 }
409
410 Output.add("}");
411
412 return Output;
413 }
414
415 public int add (Token token) {
416
417 int index;
418
419 Integer [] skuIds = new Integer[token.maxSkuCount + 1];
420 skuIds[0] = new Integer(token.maxSkuCount);
421 for (index = 1; index < skuIds.length; index++) {
422 skuIds[index] = new Integer(token.skuData.get(index - 1).id);
423 }
424
425 index = len;
426
427 len += skuIds.length;
428 al.add(skuIds);
429 alComment.add(token.getPrimaryKeyString());
430
431 return index;
432 }
433
434 public int getTableLen () {
435 return al.size() == 0 ? 1 : al.size();
436 }
437
438 }
439
440 class LocalTokenNumberTable {
441 private ArrayList<String> al;
442 private ArrayList<String> alComment;
443 private String phase;
444 private int len;
445 private int bodyStart;
446 private int bodyLineNum;
447
448 public LocalTokenNumberTable (String phase) {
449 this.phase = phase;
450 al = new ArrayList<String>();
451 alComment = new ArrayList<String>();
452 bodyStart = 0;
453 bodyLineNum = 0;
454
455 len = 0;
456 }
457
458 public String getSizeMacro () {
459 return String.format(PcdDatabase.LocalTokenNumberTableSizeMacro, phase, getSize());
460 }
461
462 public int getSize () {
463 return (al.size() == 0)? 1 : al.size();
464 }
465
466 public String getExistanceMacro () {
467 return String.format(PcdDatabase.DatabaseExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");
468 }
469
470 public String getTypeDeclaration () {
471 return String.format(PcdDatabase.LocalTokenNumberTableDeclaration, phase);
472 }
473
474 public ArrayList<String> getInstantiation () {
475 ArrayList<String> output = new ArrayList<String>();
476
477 output.add("/* LocalTokenNumberTable */");
478 output.add("{");
479 bodyStart = 2;
480
481 if (al.size() == 0) {
482 output.add("0");
483 }
484
485 for (int index = 0; index < al.size(); index++) {
486 String str;
487
488 str = (String)al.get(index);
489
490 str += " /* " + alComment.get(index) + " */ ";
491
492
493 if (index != (al.size() - 1)) {
494 str += ",";
495 }
496
497 output.add(str);
498
499 }
500
501 bodyLineNum = al.size();
502
503 output.add("}");
504
505 return output;
506 }
507
508 public int add (Token token) {
509 int index = len;
510 String str;
511
512 len++;
513
514 str = String.format(PcdDatabase.offsetOfStrTemplate, phase, token.hasDefaultValue() ? "Init" : "Uninit", token.getPrimaryKeyString());
515
516 if (token.isStringType()) {
517 str += " | PCD_TYPE_STRING";
518 }
519
520 if (token.skuEnabled) {
521 str += " | PCD_TYPE_SKU_ENABLED";
522 }
523
524 if (token.hiiEnabled) {
525 str += " | PCD_TYPE_HII";
526 }
527
528 if (token.vpdEnabled) {
529 str += " | PCD_TYPE_VPD";
530 }
531
532 al.add(str);
533 alComment.add(token.getPrimaryKeyString());
534
535 return index;
536 }
537 }
538
539 class ExMapTable {
540
541 class ExTriplet {
542 public Integer guidTableIdx;
543 public Long exTokenNumber;
544 public Long localTokenIdx;
545
546 public ExTriplet (int guidTableIdx, long exTokenNumber, long localTokenIdx) {
547 this.guidTableIdx = new Integer(guidTableIdx);
548 this.exTokenNumber = new Long(exTokenNumber);
549 this.localTokenIdx = new Long(localTokenIdx);
550 }
551 }
552
553 private ArrayList<ExTriplet> al;
554 private ArrayList<String> alComment;
555 private String phase;
556 private int len;
557 private int bodyStart;
558 private int bodyLineNum;
559 private int base;
560
561 public ExMapTable (String phase) {
562 this.phase = phase;
563 al = new ArrayList<ExTriplet>();
564 alComment = new ArrayList<String>();
565 bodyStart = 0;
566 bodyLineNum = 0;
567 len = 0;
568 }
569
570 public String getSizeMacro () {
571 return String.format(PcdDatabase.ExMapTableSizeMacro, phase, getTableLen())
572 + String.format(PcdDatabase.ExTokenNumber, phase, al.size());
573 }
574
575 private int getSize () {
576 return (al.size() == 0)? 1 : al.size();
577 }
578
579 public String getExistanceMacro () {
580 return String.format(PcdDatabase.ExMapTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");
581 }
582
583 public String getTypeDeclaration () {
584 return String.format(PcdDatabase.ExMapTableDeclaration, phase);
585 }
586
587 public ArrayList<String> getInstantiation () {
588 ArrayList<String> Output = new ArrayList<String>();
589
590 Output.add("/* ExMapTable */");
591 Output.add("{");
592 bodyStart = 2;
593
594 if (al.size() == 0) {
595 Output.add("{0, 0, 0}");
596 }
597
598 int index;
599 for (index = 0; index < al.size(); index++) {
600 String str;
601
602 ExTriplet e = (ExTriplet)al.get(index);
603
604 str = "{ " + e.exTokenNumber.toString() + ", ";
605 str += e.localTokenIdx.toString() + ", ";
606 str += e.guidTableIdx.toString();
607
608 str += " /* " + alComment.get(index) + " */";
609
610 if (index != al.size() - 1) {
611 str += ",";
612 }
613
614 Output.add(str);
615 bodyLineNum++;
616
617 }
618
619 Output.add("}");
620
621 return Output;
622 }
623
624 public int add (int localTokenIdx, long exTokenNum, int guidTableIdx, String name) {
625 int index = len;
626
627 len++;
628 al.add(new ExTriplet(guidTableIdx, exTokenNum, localTokenIdx));
629 alComment.add(name);
630
631 return index;
632 }
633
634 public int getTableLen () {
635 return al.size() == 0 ? 1 : al.size();
636 }
637
638 }
639
640 class PcdDatabase {
641
642 public final static String ExMapTableDeclaration = "DYNAMICEX_MAPPING ExMapTable[%s_EXMAPPING_TABLE_SIZE];\r\n";
643 public final static String GuidTableDeclaration = "EFI_GUID GuidTable[%s_GUID_TABLE_SIZE];\r\n";
644 public final static String LocalTokenNumberTableDeclaration = "UINT32 LocalTokenNumberTable[%s_LOCAL_TOKEN_NUMBER];\r\n";
645 public final static String StringTableDeclaration = "UINT16 StringTable[%s_STRING_TABLE_SIZE];\r\n";
646 public final static String SizeTableDeclaration = "UINT16 SizeTable[%s_LOCAL_TOKEN_NUMBER];\r\n";
647 public final static String SkuIdTableDeclaration = "UINT8 SkuIdTable[%s_SKUID_TABLE_SIZE];\r\n";
648
649
650 public final static String ExMapTableSizeMacro = "#define %s_EXMAPPING_TABLE_SIZE %d\r\n";
651 public final static String ExTokenNumber = "#define %s_EX_TOKEN_NUMBER %d\r\n";
652 public final static String GuidTableSizeMacro = "#define %s_GUID_TABLE_SIZE %d\r\n";
653 public final static String LocalTokenNumberTableSizeMacro = "#define %s_LOCAL_TOKEN_NUMBER %d\r\n";
654 public final static String StringTableSizeMacro = "#define %s_STRING_TABLE_SIZE %d\r\n";
655 public final static String SkuIdTableSizeMacro = "#define %s_SKUID_TABLE_SIZE %d\r\n";
656
657
658 public final static String ExMapTableExistenceMacro = "#define %s_EXMAP_TABLE_EMPTY %s\r\n";
659 public final static String GuidTableExistenceMacro = "#define %s_GUID_TABLE_EMPTY %s\r\n";
660 public final static String DatabaseExistenceMacro = "#define %s_DATABASE_EMPTY %s\r\n";
661 public final static String StringTableExistenceMacro = "#define %s_STRING_TABLE_EMPTY %s\r\n";
662 public final static String SkuTableExistenceMacro = "#define %s_SKUID_TABLE_EMPTY %s\r\n";
663
664 public final static String offsetOfSkuHeadStrTemplate = "offsetof(%s_PCD_DATABASE, %s.%s_SkuDataTable)";
665 public final static String offsetOfStrTemplate = "offsetof(%s_PCD_DATABASE, %s.%s)";
666
667 private StringTable stringTable;
668 private GuidTable guidTable;
669 private LocalTokenNumberTable localTokenNumberTable;
670 private SkuIdTable skuIdTable;
671 private SizeTable sizeTable;
672 private ExMapTable exMapTable;
673
674 private ArrayList<Token> alTokens;
675 private String phase;
676 private int assignedTokenNumber;
677
678 private String hString;
679 private String cString;
680
681
682 class AlignmentSizeComp implements Comparator<Token> {
683 public int compare (Token a, Token b) {
684 return getAlignmentSize(b)
685 - getAlignmentSize(a);
686 }
687 }
688
689 public PcdDatabase (ArrayList<Token> alTokens, String exePhase, int startLen) {
690 phase = exePhase;
691
692 stringTable = new StringTable(phase);
693 guidTable = new GuidTable(phase);
694 localTokenNumberTable = new LocalTokenNumberTable(phase);
695 skuIdTable = new SkuIdTable(phase);
696 sizeTable = new SizeTable(phase);
697 exMapTable = new ExMapTable(phase);
698
699 assignedTokenNumber = startLen;
700 this.alTokens = alTokens;
701 }
702
703 private void getTwoGroupsOfTokens (ArrayList<Token> alTokens, List<Token> initTokens, List<Token> uninitTokens) {
704 for (int i = 0; i < alTokens.size(); i++) {
705 Token t = (Token)alTokens.get(i);
706 if (t.hasDefaultValue()) {
707 initTokens.add(t);
708 } else {
709 uninitTokens.add(t);
710 }
711 }
712
713 return;
714 }
715
716 private int getAlignmentSize (Token token) {
717 if (token.hiiEnabled) {
718 return 2;
719 }
720
721 if (token.vpdEnabled) {
722 return 4;
723 }
724
725 if (token.isStringType()) {
726 return 2;
727 }
728
729 switch (token.datumType) {
730 case UINT8:
731 return 1;
732 case UINT16:
733 return 2;
734 case UINT32:
735 return 4;
736 case UINT64:
737 return 8;
738 case POINTER:
739 return 1;
740 case BOOLEAN:
741 return 1;
742 }
743 return 1;
744 }
745
746 public String getCString () {
747 return cString;
748 }
749
750 public String getHString () {
751 return hString;
752 }
753
754 public void genCode () {
755
756 final String newLine = "\r\n";
757 final String instNewLine = "\\\r\n";
758 final String declNewLine = ";\r\n";
759 final String tab = "\t";
760 final String commaInstNewLine = "\t,\\\r\n";
761 final String commaNewLine = ", \r\n";
762
763 int i;
764 ArrayList<String> decla;
765 ArrayList<String> inst;
766
767 String macroStr = "";
768 String initDeclStr = "";
769 String initInstStr = "";
770 String uninitDeclStr = "";
771
772 List<Token> initTokens = new ArrayList<Token> ();
773 List<Token> uninitTokens = new ArrayList<Token> ();
774
775 HashMap <String, ArrayList<String>> initCode = new HashMap<String, ArrayList<String>> ();
776 HashMap <String, ArrayList<String>> uninitCode = new HashMap<String, ArrayList<String>> ();
777
778 getTwoGroupsOfTokens (alTokens, initTokens, uninitTokens);
779
780 //
781 // Generate Structure Declaration for PcdTokens without Default Value
782 // PEI_PCD_DATABASE_INIT
783 //
784 java.util.Comparator comparator = new AlignmentSizeComp();
785 List<Token> list = initTokens;
786 java.util.Collections.sort(list, comparator);
787 initCode = processTokens(initTokens);
788
789 //
790 // Generate Structure Declaration for PcdTokens without Default Value
791 // PEI_PCD_DATABASE_UNINIT
792 //
793 java.util.Collections.sort(uninitTokens, comparator);
794 uninitCode = processTokens(uninitTokens);
795
796 //
797 // Generate size info Macro for all Tables
798 //
799 macroStr += guidTable.getSizeMacro();
800 macroStr += stringTable.getSizeMacro();
801 macroStr += skuIdTable.getSizeMacro();
802 macroStr += localTokenNumberTable.getSizeMacro();
803 macroStr += exMapTable.getSizeMacro();
804
805 //
806 // Generate existance info Macro for all Tables
807 //
808 macroStr += guidTable.getExistanceMacro();
809 macroStr += stringTable.getExistanceMacro();
810 macroStr += skuIdTable.getExistanceMacro();
811 macroStr += localTokenNumberTable.getExistanceMacro();
812 macroStr += exMapTable.getExistanceMacro();
813
814 //
815 // Generate Structure Declaration for PcdTokens with Default Value
816 // for example PEI_PCD_DATABASE_INIT
817 //
818 initDeclStr += "typedef struct {" + newLine;
819 {
820 initDeclStr += tab + exMapTable.getTypeDeclaration();
821 initDeclStr += tab + guidTable.getTypeDeclaration();
822 initDeclStr += tab + localTokenNumberTable.getTypeDeclaration();
823 initDeclStr += tab + stringTable.getTypeDeclaration();
824 initDeclStr += tab + sizeTable.getTypeDeclaration();
825 initDeclStr += tab + skuIdTable.getTypeDeclaration();
826 if (phase.equalsIgnoreCase("PEI")) {
827 initDeclStr += tab + "SKU_ID SystemSkuId;" + newLine;
828 }
829
830 decla = initCode.get(new String("Declaration"));
831 for (i = 0; i < decla.size(); i++) {
832 initDeclStr += tab + decla.get(i) + declNewLine;
833 }
834
835 //
836 // Generate Structure Declaration for PcdToken with SkuEnabled
837 //
838 decla = initCode.get("DeclarationForSku");
839
840 for (i = 0; i < decla.size(); i++) {
841 initDeclStr += tab + decla.get(i) + declNewLine;
842 }
843 }
844 initDeclStr += String.format("} %s_PCD_DATABASE_INIT;\r\n\r\n", phase);
845
846 //
847 // Generate MACRO for structure intialization of PCDTokens with Default Value
848 // The sequence must match the sequence of declaration of the memembers in the structure
849 String tmp = String.format("%s_PCD_DATABASE_INIT g%sPcdDbInit = { ", phase.toUpperCase(), phase.toUpperCase());
850 initInstStr += tmp + newLine;
851 initInstStr += tab + genInstantiationStr(exMapTable.getInstantiation()) + commaNewLine;
852 initInstStr += tab + genInstantiationStr(guidTable.getInstantiation()) + commaNewLine;
853 initInstStr += tab + genInstantiationStr(localTokenNumberTable.getInstantiation()) + commaNewLine;
854 /*
855 inst = stringTable.getInstantiation();
856 for (i = 0; i < inst.size(); i++ ) {
857 initInstStr += tab + inst.get(i) + commaNewLine;
858 }
859 */
860 initInstStr += tab + genInstantiationStr(stringTable.getInstantiation()) + commaNewLine;
861 initInstStr += tab + genInstantiationStr(sizeTable.getInstantiation()) + commaNewLine;
862 initInstStr += tab + genInstantiationStr(skuIdTable.getInstantiation()) + commaNewLine;
863 //
864 // For SystemSkuId
865 //
866 if (phase.equalsIgnoreCase("PEI")) {
867 initInstStr += tab + "0" + tab + "/* SystemSkuId */" + commaNewLine;
868 }
869
870 inst = initCode.get("Instantiation");
871 for (i = 0; i < inst.size(); i++) {
872 initInstStr += tab + inst.get(i) + commaNewLine;
873 }
874
875 inst = initCode.get("InstantiationForSku");
876 for (i = 0; i < inst.size(); i++) {
877 initInstStr += tab + inst.get(i);
878 if (i != inst.size() - 1) {
879 initInstStr += commaNewLine;
880 }
881 }
882
883 initInstStr += "};";
884
885 uninitDeclStr += "typedef struct {" + newLine;
886 {
887 decla = uninitCode.get("Declaration");
888 if (decla.size() == 0) {
889 uninitDeclStr += "UINT8 dummy /* The UINT struct is empty */" + declNewLine;
890 } else {
891
892 for (i = 0; i < decla.size(); i++) {
893 uninitDeclStr += tab + decla.get(i) + declNewLine;
894 }
895
896 decla = uninitCode.get("DeclarationForSku");
897
898 for (i = 0; i < decla.size(); i++) {
899 uninitDeclStr += tab + decla.get(i) + declNewLine;
900 }
901 }
902 }
903 uninitDeclStr += String.format("} %s_PCD_DATABASE_UNINIT;\r\n\r\n", phase);
904
905 cString = initInstStr + newLine;
906 hString = macroStr + newLine
907 + initDeclStr + newLine
908 + uninitDeclStr + newLine
909 + newLine;
910
911 }
912
913 private String genInstantiationStr (ArrayList<String> alStr) {
914 String str = "";
915 for (int i = 0; i< alStr.size(); i++) {
916 str += "\t" + alStr.get(i);
917 if (i != alStr.size() - 1) {
918 str += "\r\n";
919 }
920 }
921
922 return str;
923 }
924
925 private HashMap<String, ArrayList<String>> processTokens (List<Token> alToken) {
926
927 ArrayList[] output = new ArrayList[4];
928 HashMap <String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
929
930 ArrayList<String> decl = new ArrayList<String>();
931 ArrayList<String> declForSkuEnableType = new ArrayList<String>();
932 ArrayList<String> inst = new ArrayList<String>();
933 ArrayList<String> instForSkuEnableType = new ArrayList<String>();
934
935 for (int index = 0; index < alToken.size(); index++) {
936 Token token = alToken.get(index);
937
938 if (token.skuEnabled) {
939 //
940 // BugBug: Schema only support Data type now
941 //
942 int tableIdx;
943
944 tableIdx = skuIdTable.add(token);
945
946 decl.add(getSkuEnabledTypeDeclaration(token));
947 if (token.hasDefaultValue()) {
948 inst.add(getSkuEnabledTypeInstantiaion(token, tableIdx));
949 }
950
951 declForSkuEnableType.add(getDataTypeDeclarationForSkuEnabled(token));
952 if (token.hasDefaultValue()) {
953 instForSkuEnableType.add(getDataTypeInstantiationForSkuEnabled(token));
954 }
955
956 } else {
957 if (token.hiiEnabled) {
958 decl.add(getVariableEnableTypeDeclaration(token));
959 inst.add(getVariableEnableInstantiation(token));
960 } else if (token.vpdEnabled) {
961 decl.add(getVpdEnableTypeDeclaration(token));
962 inst.add(getVpdEnableTypeInstantiation(token));
963 } else if (token.isStringType()) {
964 decl.add(getStringTypeDeclaration(token));
965 inst.add(getStringTypeInstantiation(stringTable.add(token.getStringTypeString(), token), token));
966 }
967 else {
968 decl.add(getDataTypeDeclaration(token));
969 if (token.hasDefaultValue()) {
970 inst.add(getDataTypeInstantiation(token));
971 }
972 }
973 }
974
975 sizeTable.add(token);
976 localTokenNumberTable.add(token);
977 token.assignedtokenNumber = assignedTokenNumber++;
978
979 }
980
981 map.put("Declaration", decl);
982 map.put("DeclarationForSku", declForSkuEnableType);
983 map.put("Instantiation", inst);
984 map.put("InstantiationForSku", instForSkuEnableType);
985
986 return map;
987 }
988
989 private String getSkuEnabledTypeDeclaration (Token token) {
990 return String.format("SKU_HEAD %s;\r\n", token.getPrimaryKeyString());
991 }
992
993 private String getSkuEnabledTypeInstantiaion (Token token, int SkuTableIdx) {
994
995 String offsetof = String.format(PcdDatabase.offsetOfSkuHeadStrTemplate, phase, token.hasDefaultValue()? "Init" : "Uninit", token.getPrimaryKeyString());
996 return String.format("{ %s, %d }", offsetof, SkuTableIdx);
997 }
998
999 private String getDataTypeDeclarationForSkuEnabled (Token token) {
1000 String typeStr = "";
1001
1002 if (token.datumType == Token.DATUM_TYPE.UINT8) {
1003 typeStr = "UINT8 %s_%s[%d];\r\n";
1004 } else if (token.datumType == Token.DATUM_TYPE.UINT16) {
1005 typeStr = "UINT16 %s_%s[%d];\r\n";
1006 } else if (token.datumType == Token.DATUM_TYPE.UINT32) {
1007 typeStr = "UINT32 %s_%s[%d];\r\n";
1008 } else if (token.datumType == Token.DATUM_TYPE.UINT64) {
1009 typeStr = "UINT64 %s_%s[%d];\r\n";
1010 } else if (token.datumType == Token.DATUM_TYPE.BOOLEAN) {
1011 typeStr = "BOOLEAN %s_%s[%d];\r\n";
1012 } else if (token.datumType == Token.DATUM_TYPE.POINTER) {
1013 return String.format("UINT8 %s_s[%d];\r\n", token.getPrimaryKeyString(), "SkuDataTable", token.datumSize * token.maxSkuCount);
1014 }
1015
1016 return String.format(typeStr, token.getPrimaryKeyString(), "SkuDataTable", token.maxSkuCount);
1017
1018 }
1019
1020 private String getDataTypeInstantiationForSkuEnabled (Token token) {
1021 String str = "";
1022
1023 if (token.datumType == Token.DATUM_TYPE.POINTER) {
1024 return String.format("UINT8 %s_s[%d]", token.getPrimaryKeyString(), "SkuDataTable", token.datumSize * token.maxSkuCount);
1025 } else {
1026 str = "{ ";
1027 for (int idx = 0; idx < token.maxSkuCount; idx++) {
1028 str += token.skuData.get(idx).toString();
1029 if (idx != token.maxSkuCount - 1) {
1030 str += ", ";
1031 }
1032 }
1033 str += "}";
1034
1035 return str;
1036 }
1037
1038 }
1039
1040 private String getDataTypeInstantiation (Token token) {
1041
1042 String typeStr = "";
1043
1044 if (token.datumType == Token.DATUM_TYPE.POINTER) {
1045 return String.format("%s /* %s */", token.datum.toString(), token.getPrimaryKeyString());
1046 } else {
1047 return String.format("%s /* %s */", token.datum.toString(), token.getPrimaryKeyString());
1048 }
1049 }
1050
1051
1052 private String getDataTypeDeclaration (Token token) {
1053
1054 String typeStr = "";
1055
1056 if (token.datumType == Token.DATUM_TYPE.UINT8) {
1057 typeStr = "UINT8";
1058 } else if (token.datumType == Token.DATUM_TYPE.UINT16) {
1059 typeStr = "UINT16";
1060 } else if (token.datumType == Token.DATUM_TYPE.UINT32) {
1061 typeStr = "UINT32";
1062 } else if (token.datumType == Token.DATUM_TYPE.UINT64) {
1063 typeStr = "UINT64";
1064 } else if (token.datumType == Token.DATUM_TYPE.BOOLEAN) {
1065 typeStr = "BOOLEAN";
1066 } else if (token.datumType == Token.DATUM_TYPE.POINTER) {
1067 return String.format("UINT8 %s[%d]", token.getPrimaryKeyString(), token.datumSize);
1068 } else {
1069 }
1070
1071 return String.format("%s %s", typeStr, token.getPrimaryKeyString());
1072 }
1073
1074 private String getVpdEnableTypeDeclaration (Token token) {
1075 return String.format("VPD_HEAD %s", token.getPrimaryKeyString());
1076 }
1077
1078 private String getVpdEnableTypeInstantiation (Token token) {
1079 return String.format("{ %d } /* %s */", token.vpdOffset,
1080 token.getPrimaryKeyString());
1081 }
1082
1083 private String getStringTypeDeclaration (Token token) {
1084 return String.format("UINT16 %s", token.getPrimaryKeyString());
1085 }
1086
1087 private String getStringTypeInstantiation (int StringTableIdx, Token token) {
1088 return String.format ("%d /* %s */", StringTableIdx,
1089 token.getPrimaryKeyString());
1090 }
1091
1092
1093 private String getVariableEnableTypeDeclaration (Token token) {
1094 return String.format("VARIABLE_HEAD %s", token.getPrimaryKeyString());
1095 }
1096
1097 private String getVariableEnableInstantiation (Token token) {
1098 return String.format("{ %d, %d, %d } /* %s */", guidTable.add(token.variableGuid, token.getPrimaryKeyString()),
1099 stringTable.add(token.variableName, token),
1100 token.variableOffset,
1101 token.getPrimaryKeyString());
1102 }
1103
1104 public int getTotalTokenNumber () {
1105 return sizeTable.getTableLen();
1106 }
1107
1108 public static String getPcdDatabaseCommonDefinitions ()
1109 throws EntityException {
1110
1111 String retStr = "";
1112 try {
1113 File file = new File(GlobalData.getWorkspacePath() + File.separator +
1114 "Tools" + File.separator +
1115 "Conf" + File.separator +
1116 "Pcd" + File.separator +
1117 "PcdDatabaseCommonDefinitions.sample");
1118 System.out.println(GlobalData.getWorkspacePath());
1119 FileReader reader = new FileReader(file);
1120 BufferedReader in = new BufferedReader(reader);
1121 String str;
1122 while ((str = in.readLine()) != null) {
1123 retStr = retStr +"\r\n" + str;
1124 }
1125 } catch (Exception ex) {
1126 throw new EntityException("Fatal error when generating PcdDatabase Common Definitions");
1127 }
1128
1129 return retStr;
1130 }
1131
1132 public static String getPcdDxeDatabaseDefinitions ()
1133 throws EntityException {
1134
1135 String retStr = "";
1136 try {
1137 File file = new File(GlobalData.getWorkspacePath() + File.separator +
1138 "Tools" + File.separator +
1139 "Conf" + File.separator +
1140 "Pcd" + File.separator +
1141 "PcdDatabaseDxeDefinitions.sample");
1142 FileReader reader = new FileReader(file);
1143 BufferedReader in = new BufferedReader(reader);
1144 String str;
1145 while ((str = in.readLine()) != null) {
1146 retStr = retStr +"\r\n" + str;
1147 }
1148 } catch (Exception ex) {
1149 throw new EntityException("Fatal error when generating PcdDatabase Dxe Definitions");
1150 }
1151
1152 return retStr;
1153 }
1154
1155 public static String getPcdPeiDatabaseDefinitions ()
1156 throws EntityException {
1157
1158 String retStr = "";
1159 try {
1160 File file = new File(GlobalData.getWorkspacePath() + File.separator +
1161 "Tools" + File.separator +
1162 "Conf" + File.separator +
1163 "Pcd" + File.separator +
1164 "PcdDatabasePeiDefinitions.sample");
1165 FileReader reader = new FileReader(file);
1166 BufferedReader in = new BufferedReader(reader);
1167 String str;
1168 while ((str = in.readLine()) != null) {
1169 retStr = retStr +"\r\n" + str;
1170 }
1171 } catch (Exception ex) {
1172 throw new EntityException("Fatal error when generating PcdDatabase Pei Definitions");
1173 }
1174
1175 return retStr;
1176 }
1177
1178 }
1179
1180 /** This action class is to collect PCD information from MSA, SPD, FPD xml file.
1181 This class will be used for wizard and build tools, So it can *not* inherit
1182 from buildAction or UIAction.
1183 **/
1184 public class CollectPCDAction {
1185 /// memoryDatabase hold all PCD information collected from SPD, MSA, FPD.
1186 private MemoryDatabaseManager dbManager;
1187
1188 /// Workspacepath hold the workspace information.
1189 private String workspacePath;
1190
1191 /// FPD file is the root file.
1192 private String fpdFilePath;
1193
1194 /// Message level for CollectPCDAction.
1195 private int originalMessageLevel;
1196
1197 /**
1198 Set WorkspacePath parameter for this action class.
1199
1200 @param workspacePath parameter for this action
1201 **/
1202 public void setWorkspacePath(String workspacePath) {
1203 this.workspacePath = workspacePath;
1204 }
1205
1206 /**
1207 Set action message level for CollectPcdAction tool.
1208
1209 The message should be restored when this action exit.
1210
1211 @param actionMessageLevel parameter for this action
1212 **/
1213 public void setActionMessageLevel(int actionMessageLevel) {
1214 originalMessageLevel = ActionMessage.messageLevel;
1215 ActionMessage.messageLevel = actionMessageLevel;
1216 }
1217
1218 /**
1219 Set FPDFileName parameter for this action class.
1220
1221 @param fpdFilePath fpd file path
1222 **/
1223 public void setFPDFilePath(String fpdFilePath) {
1224 this.fpdFilePath = fpdFilePath;
1225 }
1226
1227 /**
1228 Common function interface for outer.
1229
1230 @param workspacePath The path of workspace of current build or analysis.
1231 @param fpdFilePath The fpd file path of current build or analysis.
1232 @param messageLevel The message level for this Action.
1233
1234 @throws Exception The exception of this function. Because it can *not* be predict
1235 where the action class will be used. So only Exception can be throw.
1236
1237 **/
1238 public void perform(String workspacePath, String fpdFilePath,
1239 int messageLevel) throws Exception {
1240 setWorkspacePath(workspacePath);
1241 setFPDFilePath(fpdFilePath);
1242 setActionMessageLevel(messageLevel);
1243 checkParameter();
1244 execute();
1245 ActionMessage.messageLevel = originalMessageLevel;
1246 }
1247
1248 /**
1249 Core execution function for this action class.
1250
1251 This function work flows will be:
1252 1) Get all token's platform information from FPD, and create token object into memory database.
1253 2) Get all token's module information from MSA, and create usage instance for every module's PCD entry.
1254 3) Get all token's inherited information from MSA's library, and create usage instance
1255 for module who consume this library and create usage instance for library for building.
1256 4) Collect token's package information from SPD, update these information for token in memory
1257 database.
1258 5) Generate 3 strings for a) All modules using Dynamic(Ex) PCD entry. (Token Number)
1259 b) PEI PCD Database (C Structure) for PCD Service PEIM
1260 c) DXE PCD Database (C structure) for PCD Service DXE
1261
1262
1263 @throws EntityException Exception indicate failed to execute this action.
1264
1265 **/
1266 private void execute() throws EntityException {
1267 FrameworkPlatformDescriptionDocument fpdDoc = null;
1268 Object[][] modulePCDArray = null;
1269 Map<String, XmlObject> docMap = null;
1270 ModuleSADocument.ModuleSA[] moduleSAs = null;
1271 UsageInstance usageInstance = null;
1272 String packageName = null;
1273 String packageFullPath = null;
1274 int index = 0;
1275 int libraryIndex = 0;
1276 int pcdArrayIndex = 0;
1277 List<String> listLibraryInstance = null;
1278 String componentTypeStr = null;
1279
1280 //
1281 // Collect all PCD information defined in FPD file.
1282 // Evenry token defind in FPD will be created as an token into
1283 // memory database.
1284 //
1285 fpdDoc = createTokenInDBFromFPD();
1286
1287 //
1288 // Searching MSA and SPD document.
1289 // The information of MSA will be used to create usage instance into database.
1290 // The information of SPD will be used to update the token information in database.
1291 //
1292
1293 HashMap<String, XmlObject> map = new HashMap<String, XmlObject>();
1294 map.put("FrameworkPlatformDescription", fpdDoc);
1295 SurfaceAreaQuery.setDoc(map);
1296
1297 moduleSAs = SurfaceAreaQuery.getFpdModules();
1298 for(index = 0; index < moduleSAs.length; index ++) {
1299 //
1300 // Get module document and use SurfaceAreaQuery to get PCD information
1301 //
1302 docMap = GlobalData.getDoc(moduleSAs[index].getModuleName());
1303 SurfaceAreaQuery.setDoc(docMap);
1304 modulePCDArray = SurfaceAreaQuery.getModulePCDTokenArray();
1305 componentTypeStr = SurfaceAreaQuery.getComponentType();
1306 packageName =
1307 GlobalData.getPackageNameForModule(moduleSAs[index].getModuleName());
1308 packageFullPath = this.workspacePath + File.separator +
1309 GlobalData.getPackagePath(packageName) +
1310 packageName + ".spd";
1311
1312 if(modulePCDArray != null) {
1313 //
1314 // If current MSA contains <PCDs> information, then create usage
1315 // instance for PCD information from MSA
1316 //
1317 for(pcdArrayIndex = 0; pcdArrayIndex < modulePCDArray.length;
1318 pcdArrayIndex ++) {
1319 usageInstance =
1320 createUsageInstanceFromMSA(moduleSAs[index].getModuleName(),
1321 modulePCDArray[pcdArrayIndex]);
1322
1323 if(usageInstance == null) {
1324 continue;
1325 }
1326 //
1327 // Get remaining PCD information from the package which this module belongs to
1328 //
1329 updateTokenBySPD(usageInstance, packageFullPath);
1330 }
1331 }
1332
1333 //
1334 // Get inherit PCD information which inherit from library instance of this module.
1335 //
1336 listLibraryInstance =
1337 SurfaceAreaQuery.getLibraryInstance(moduleSAs[index].getArch().toString(),
1338 CommonDefinition.AlwaysConsumed);
1339 if(listLibraryInstance != null) {
1340 for(libraryIndex = 0; libraryIndex < listLibraryInstance.size();
1341 libraryIndex ++) {
1342 inheritPCDFromLibraryInstance(listLibraryInstance.get(libraryIndex),
1343 moduleSAs[index].getModuleName(),
1344 packageName,
1345 componentTypeStr);
1346 }
1347 }
1348 }
1349
1350 //
1351 // Call Private function genPcdDatabaseSourceCode (void); ComponentTypeBsDriver
1352 // 1) Generate for PEI, DXE PCD DATABASE's definition and initialization.
1353 //
1354 genPcdDatabaseSourceCode ();
1355
1356 }
1357
1358 /**
1359 This function generates source code for PCD Database.
1360
1361 @param void
1362 @throws EntityException If the token does *not* exist in memory database.
1363
1364 **/
1365
1366 private void genPcdDatabaseSourceCode ()
1367 throws EntityException {
1368 String PcdCommonHeaderString = PcdDatabase.getPcdDatabaseCommonDefinitions ();
1369
1370 ArrayList<Token> alPei = new ArrayList<Token> ();
1371 ArrayList<Token> alDxe = new ArrayList<Token> ();
1372
1373 dbManager.getTwoPhaseDynamicRecordArray(alPei, alDxe);
1374 PcdDatabase pcdPeiDatabase = new PcdDatabase (alPei, "PEI", 0);
1375 pcdPeiDatabase.genCode();
1376 dbManager.PcdPeimHString = PcdCommonHeaderString + pcdPeiDatabase.getHString()
1377 + PcdDatabase.getPcdPeiDatabaseDefinitions();
1378 dbManager.PcdPeimCString = pcdPeiDatabase.getCString();
1379
1380 PcdDatabase pcdDxeDatabase = new PcdDatabase (alDxe,
1381 "DXE",
1382 alPei.size()
1383 );
1384 pcdDxeDatabase.genCode();
1385 dbManager.PcdDxeHString = dbManager.PcdPeimHString + pcdDxeDatabase.getHString()
1386 + PcdDatabase.getPcdDxeDatabaseDefinitions();
1387 dbManager.PcdDxeCString = pcdDxeDatabase.getCString();
1388 }
1389
1390 /**
1391 This function will collect inherit PCD information from library for a module.
1392
1393 This function will create two usage instance for inherited PCD token, one is
1394 for module and another is for library.
1395 For module, if it inherited a PCD token from library, this PCD token's value
1396 should be instanced in module level, and belongs to module.
1397 For library, it also need a usage instance for build.
1398
1399 @param libraryName The name of library instance.
1400 @param moduleName The name of module.
1401 @param packageName The name of package while module belongs to.
1402 @param parentcomponentType The component type of module.
1403
1404 @throws EntityException If the token does *not* exist in memory database.
1405
1406 **/
1407 private void inheritPCDFromLibraryInstance(String libraryName,
1408 String moduleName,
1409 String packageName,
1410 String parentcomponentType)
1411 throws EntityException {
1412 Map<String, XmlObject> docMap = null;
1413 String primaryKeyString = null;
1414 Object[][] libPcdDataArray = null;
1415 UUID nullUUID = new UUID(0,0);
1416 UUID platformUUID = nullUUID;
1417 UUID tokenSpaceGuid = null;
1418 int tokenIndex = 0;
1419 Token token = null;
1420 Token.PCD_TYPE pcdType = Token.PCD_TYPE.UNKNOWN;
1421 UsageInstance usageInstance = null;
1422 String packageFullPath = null;
1423
1424 //
1425 // Query PCD information from library's document.
1426 //
1427 docMap = GlobalData.getDoc(libraryName);
1428 SurfaceAreaQuery.setDoc(docMap);
1429 libPcdDataArray = SurfaceAreaQuery.getModulePCDTokenArray();
1430
1431 if(libPcdDataArray == null) {
1432 return;
1433 }
1434
1435 for(tokenIndex = 0; tokenIndex < libPcdDataArray.length; tokenIndex ++) {
1436 tokenSpaceGuid =((UUID)libPcdDataArray[tokenIndex][2] == null) ?
1437 nullUUID :(UUID)libPcdDataArray[tokenIndex][2];
1438
1439 //
1440 // Get token from memory database. The token must be created from FPD already.
1441 //
1442 primaryKeyString = Token.getPrimaryKeyString((String)libPcdDataArray[tokenIndex][0],
1443 tokenSpaceGuid,
1444 platformUUID
1445 );
1446
1447 if(dbManager.isTokenInDatabase(primaryKeyString)) {
1448 token = dbManager.getTokenByKey(primaryKeyString);
1449 } else {
1450 throw new EntityException("The PCD token " + primaryKeyString +
1451 " defined in module " + moduleName +
1452 " does not exist in FPD file!");
1453 }
1454
1455 //
1456 // Create usage instance for module.
1457 //
1458 pcdType = Token.getpcdTypeFromString((String)libPcdDataArray[tokenIndex][1]);
1459 usageInstance = new UsageInstance(token,
1460 Token.PCD_USAGE.ALWAYS_CONSUMED,
1461 pcdType,
1462 CommonDefinition.getComponentType(parentcomponentType),
1463 libPcdDataArray[tokenIndex][3],
1464 null,
1465 (String) libPcdDataArray[tokenIndex][5],
1466 "",
1467 moduleName,
1468 packageName,
1469 true);
1470 if(Token.PCD_USAGE.UNKNOWN == token.isUsageInstanceExist(moduleName)) {
1471 token.addUsageInstance(usageInstance);
1472
1473 packageFullPath = this.workspacePath + File.separator +
1474 GlobalData.getPackagePath(packageName) +
1475 packageName + ".spd";
1476 updateTokenBySPD(usageInstance, packageFullPath);
1477 }
1478
1479 //
1480 // We need create second usage instance for inherited case, which
1481 // add library as an usage instance, because when build a module, and
1482 // if module inherited from base library, then build process will build
1483 // library at first.
1484 //
1485 if(Token.PCD_USAGE.UNKNOWN == token.isUsageInstanceExist(libraryName)) {
1486 packageName = GlobalData.getPackageNameForModule(libraryName);
1487 usageInstance = new UsageInstance(token,
1488 Token.PCD_USAGE.ALWAYS_CONSUMED,
1489 pcdType,
1490 CommonDefinition.ComponentTypeLibrary,
1491 libPcdDataArray[tokenIndex][3],
1492 null,
1493 (String)libPcdDataArray[tokenIndex][5],
1494 "",
1495 libraryName,
1496 packageName,
1497 false);
1498 token.addUsageInstance(usageInstance);
1499 }
1500 }
1501 }
1502
1503 /**
1504 Create usage instance for PCD token defined in MSA document
1505
1506 A PCD token maybe used by many modules, and every module is one of usage
1507 instance of this token. For ALWAY_CONSUMED, SOMETIMES_CONSUMED, it is
1508 consumer type usage instance of this token, and for ALWAYS_PRODUCED,
1509 SOMETIMES_PRODUCED, it is produce type usage instance.
1510
1511 @param moduleName The name of module
1512 @param tokenInfoInMsa The PCD token information array retrieved from MSA.
1513
1514 @return UsageInstance The usage instance created in memroy database.
1515
1516 @throws EntityException If token did not exist in database yet.
1517
1518 **/
1519 private UsageInstance createUsageInstanceFromMSA(String moduleName,
1520 Object[] tokenInfoInMsa)
1521 throws EntityException {
1522 String packageName = null;
1523 UsageInstance usageInstance = null;
1524 UUID tokenSpaceGuid = null;
1525 UUID nullUUID = new UUID(0,0);
1526 String primaryKeyString = null;
1527 UUID platformTokenSpace = nullUUID;
1528 Token token = null;
1529 Token.PCD_TYPE pcdType = Token.PCD_TYPE.UNKNOWN;
1530 Token.PCD_USAGE pcdUsage = Token.PCD_USAGE.UNKNOWN;
1531
1532 tokenSpaceGuid =((UUID)tokenInfoInMsa[2] == null) ? nullUUID :(UUID)tokenInfoInMsa[2];
1533
1534 primaryKeyString = Token.getPrimaryKeyString((String)tokenInfoInMsa[0],
1535 tokenSpaceGuid,
1536 platformTokenSpace);
1537
1538 //
1539 // Get token object from memory database firstly.
1540 //
1541 if(dbManager.isTokenInDatabase(primaryKeyString)) {
1542 token = dbManager.getTokenByKey(primaryKeyString);
1543 } else {
1544 throw new EntityException("The PCD token " + primaryKeyString + " defined in module " +
1545 moduleName + " does not exist in FPD file!" );
1546 }
1547 pcdType = Token.getpcdTypeFromString((String)tokenInfoInMsa[1]);
1548 pcdUsage = Token.getUsageFromString((String)tokenInfoInMsa[4]);
1549
1550 packageName = GlobalData.getPackageNameForModule(moduleName);
1551
1552 if(Token.PCD_USAGE.UNKNOWN != token.isUsageInstanceExist(moduleName)) {
1553 //
1554 // BUGBUG: It is legal that same base name exist in one FPD file. In furture
1555 // we should use "Guid, Version, Package" and "Arch" to differ a module.
1556 // So currently, warning should be disabled.
1557 //
1558 //ActionMessage.warning(this,
1559 // "In module " + moduleName + " exist more than one PCD token " + token.cName
1560 // );
1561 return null;
1562 }
1563
1564 //
1565 // BUGBUG: following code could be enabled at current schema. Because
1566 // current schema does not provide usage information.
1567 //
1568 // For FEATRURE_FLAG, FIXED_AT_BUILD, PATCH_IN_MODULE type PCD token, his
1569 // usage is always ALWAYS_CONSUMED
1570 //
1571 //if((pcdType != Token.PCD_TYPE.DYNAMIC) &&
1572 // (pcdType != Token.PCD_TYPE.DYNAMIC_EX)) {
1573 pcdUsage = Token.PCD_USAGE.ALWAYS_CONSUMED;
1574 //}
1575
1576 usageInstance = new UsageInstance(token,
1577 pcdUsage,
1578 pcdType,
1579 CommonDefinition.getComponentType(SurfaceAreaQuery.getComponentType()),
1580 tokenInfoInMsa[3],
1581 null,
1582 (String) tokenInfoInMsa[5],
1583 "",
1584 moduleName,
1585 packageName,
1586 false);
1587
1588 //
1589 // Use default value defined in MSA to update datum of token,
1590 // if datum of token does not defined in FPD file.
1591 //
1592 if((token.datum == null) &&(tokenInfoInMsa[3] != null)) {
1593 token.datum = tokenInfoInMsa[3];
1594 }
1595
1596 token.addUsageInstance(usageInstance);
1597
1598 return usageInstance;
1599 }
1600
1601 /**
1602 Create token instance object into memory database, the token information
1603 comes for FPD file. Normally, FPD file will contain all token platform
1604 informations.
1605
1606 This fucntion should be executed at firsly before others collection work
1607 such as searching token information from MSA, SPD.
1608
1609 @return FrameworkPlatformDescriptionDocument The FPD document instance for furture usage.
1610
1611 @throws EntityException Failed to parse FPD xml file.
1612
1613 **/
1614 private FrameworkPlatformDescriptionDocument createTokenInDBFromFPD()
1615 throws EntityException {
1616 XmlObject doc = null;
1617 FrameworkPlatformDescriptionDocument fpdDoc = null;
1618 int index = 0;
1619 List<PcdBuildData> pcdBuildDataArray = new ArrayList<PcdBuildData>();
1620 PcdBuildData pcdBuildData = null;
1621 Token token = null;
1622 UUID nullUUID = new UUID(0,0);
1623 UUID platformTokenSpace= nullUUID;
1624 List skuDataArray = new ArrayList();
1625 SkuInstance skuInstance = null;
1626 int skuIndex = 0;
1627
1628 //
1629 // Get all tokens from FPD file and create token into database.
1630 //
1631
1632 try {
1633 doc = XmlObject.Factory.parse(new File(fpdFilePath));
1634 } catch(IOException ioE) {
1635 throw new EntityException("Can't find the FPD xml fle:" + fpdFilePath);
1636 } catch(XmlException xmlE) {
1637 throw new EntityException("Can't parse the FPD xml fle:" + fpdFilePath);
1638 }
1639
1640 //
1641 // Get memoryDatabaseManager instance from GlobalData.
1642 //
1643 if((dbManager = GlobalData.getPCDMemoryDBManager()) == null) {
1644 throw new EntityException("The instance of PCD memory database manager is null");
1645 }
1646
1647 dbManager = new MemoryDatabaseManager();
1648
1649 if(!(doc instanceof FrameworkPlatformDescriptionDocument)) {
1650 throw new EntityException("File " + fpdFilePath +
1651 " is not a FrameworkPlatformDescriptionDocument");
1652 }
1653
1654 fpdDoc =(FrameworkPlatformDescriptionDocument)doc;
1655
1656 //
1657 // Add all tokens in FPD into Memory Database.
1658 //
1659 pcdBuildDataArray =
1660 fpdDoc.getFrameworkPlatformDescription().getPcdBuildDeclarations().getPcdBuildDataList();
1661 for(index = 0;
1662 index < fpdDoc.getFrameworkPlatformDescription().getPcdBuildDeclarations().sizeOfPcdBuildDataArray();
1663 index ++) {
1664 pcdBuildData = pcdBuildDataArray.get(index);
1665 token = new Token(pcdBuildData.getCName(), new UUID(0, 0), new UUID(0, 0));
1666 //
1667 // BUGBUG: in FPD, <defaultValue> should be defined as <Value>
1668 //
1669 token.datum = pcdBuildData.getDefaultValue();
1670 token.tokenNumber = Integer.decode(pcdBuildData.getToken().getStringValue());
1671 token.hiiEnabled = pcdBuildData.getHiiEnable();
1672 token.variableGuid = Token.getGUIDFromSchemaObject(pcdBuildData.getVariableGuid());
1673 token.variableName = pcdBuildData.getVariableName();
1674 token.variableOffset = Integer.decode(pcdBuildData.getDataOffset());
1675 token.skuEnabled = pcdBuildData.getSkuEnable();
1676 token.maxSkuCount = Integer.decode(pcdBuildData.getMaxSku());
1677 token.skuId = Integer.decode(pcdBuildData.getSkuId());
1678 token.skuDataArrayEnabled = pcdBuildData.getSkuDataArrayEnable();
1679 token.assignedtokenNumber = Integer.decode(pcdBuildData.getToken().getStringValue());
1680 skuDataArray = pcdBuildData.getSkuDataArray1();
1681 token.datumType = Token.getdatumTypeFromString(pcdBuildData.getDatumType().toString());
1682 token.datumSize = pcdBuildData.getDatumSize();
1683
1684 if(skuDataArray != null) {
1685 for(skuIndex = 0; skuIndex < skuDataArray.size(); skuIndex ++) {
1686 //
1687 // BUGBUG: Now in current schema, The value is defined as String type,
1688 // it is not correct, the type should be same as the datumType
1689 //
1690 skuInstance = new SkuInstance(((PcdBuildData.SkuData)skuDataArray.get(skuIndex)).getId(),
1691 ((PcdBuildData.SkuData)skuDataArray.get(skuIndex)).getValue());
1692 token.skuData.add(skuInstance);
1693 }
1694 }
1695
1696 if(dbManager.isTokenInDatabase(Token.getPrimaryKeyString(token.cName,
1697 token.tokenSpaceName,
1698 platformTokenSpace))) {
1699 //
1700 // If found duplicate token, Should tool be hold?
1701 //
1702 ActionMessage.warning(this,
1703 "Token " + token.cName + " exists in token database");
1704 continue;
1705 }
1706 token.pcdType = Token.getpcdTypeFromString(pcdBuildData.getItemType().toString());
1707 dbManager.addTokenToDatabase(Token.getPrimaryKeyString(token.cName,
1708 token.tokenSpaceName,
1709 platformTokenSpace),
1710 token);
1711 }
1712
1713 return fpdDoc;
1714 }
1715
1716 /**
1717 Update PCD token in memory database by help information in SPD.
1718
1719 After create token from FPD and create usage instance from MSA, we should collect
1720 PCD package level information from SPD and update token information in memory
1721 database.
1722
1723 @param usageInstance The usage instance defined in MSA and want to search in SPD.
1724 @param packageFullPath The SPD file path.
1725
1726 @throws EntityException Failed to parse SPD xml file.
1727
1728 **/
1729 private void updateTokenBySPD(UsageInstance usageInstance,
1730 String packageFullPath)
1731 throws EntityException {
1732 PackageSurfaceAreaDocument pkgDoc = null;
1733 PcdDefinitions pcdDefinitions = null;
1734 List<PcdDefinitions.PcdEntry> pcdEntryArray = new ArrayList<PcdDefinitions.PcdEntry>();
1735 int index = 0;
1736 boolean isFoundInSpd = false;
1737 Token.DATUM_TYPE datumType = Token.DATUM_TYPE.UNKNOWN;
1738
1739 try {
1740 pkgDoc =(PackageSurfaceAreaDocument)XmlObject.Factory.parse(new File(packageFullPath));
1741 } catch(IOException ioE) {
1742 throw new EntityException("Can't find the FPD xml fle:" + packageFullPath);
1743 } catch(XmlException xmlE) {
1744 throw new EntityException("Can't parse the FPD xml fle:" + packageFullPath);
1745 }
1746 pcdDefinitions = pkgDoc.getPackageSurfaceArea().getPcdDefinitions();
1747 //
1748 // It is illege for SPD file does not contains any PCD information.
1749 //
1750 if (pcdDefinitions == null) {
1751 return;
1752 }
1753
1754 pcdEntryArray = pcdDefinitions.getPcdEntryList();
1755 if (pcdEntryArray == null) {
1756 return;
1757 }
1758 for(index = 0; index < pcdEntryArray.size(); index ++) {
1759 if(pcdEntryArray.get(index).getCName().equalsIgnoreCase(
1760 usageInstance.parentToken.cName)) {
1761 isFoundInSpd = true;
1762 //
1763 // From SPD file , we can get following information.
1764 // Token: Token number defined in package level.
1765 // PcdItemType: This item does not single one. It means all supported item type.
1766 // datumType: UINT8, UNIT16, UNIT32, UINT64, VOID*, BOOLEAN
1767 // datumSize: The size of default value or maxmine size.
1768 // defaultValue: This value is defined in package level.
1769 // HelpText: The help text is provided in package level.
1770 //
1771
1772 usageInstance.parentToken.tokenNumber = Integer.decode(pcdEntryArray.get(index).getToken());
1773
1774 if(pcdEntryArray.get(index).getDatumType() != null) {
1775 datumType = Token.getdatumTypeFromString(
1776 pcdEntryArray.get(index).getDatumType().toString());
1777 if(usageInstance.parentToken.datumType == Token.DATUM_TYPE.UNKNOWN) {
1778 usageInstance.parentToken.datumType = datumType;
1779 } else {
1780 if(datumType != usageInstance.parentToken.datumType) {
1781 throw new EntityException("Different datum types are defined for Token :" +
1782 usageInstance.parentToken.cName);
1783 }
1784 }
1785
1786 } else {
1787 throw new EntityException("The datum type for token " + usageInstance.parentToken.cName +
1788 " is not defind in SPD file " + packageFullPath);
1789 }
1790
1791 usageInstance.defaultValueInSPD = pcdEntryArray.get(index).getDefaultValue();
1792 usageInstance.helpTextInSPD = "Help Text in SPD";
1793
1794 //
1795 // If token's datum is not valid, it indicate that datum is not provided
1796 // in FPD and defaultValue is not provided in MSA, then use defaultValue
1797 // in SPD as the datum of token.
1798 //
1799 if(usageInstance.parentToken.datum == null) {
1800 if(pcdEntryArray.get(index).getDefaultValue() != null) {
1801 usageInstance.parentToken.datum = pcdEntryArray.get(index).getDefaultValue();
1802 } else {
1803 throw new EntityException("FPD does not provide datum for token " + usageInstance.parentToken.cName +
1804 ", MSA and SPD also does not provide <defaultValue> for this token!");
1805 }
1806 }
1807 }
1808 }
1809 }
1810
1811 /**
1812 check parameter for this action.
1813
1814 @throws EntityException Bad parameter.
1815 **/
1816 private void checkParameter() throws EntityException {
1817 File file = null;
1818
1819 if((fpdFilePath == null) ||(workspacePath == null)) {
1820 throw new EntityException("WorkspacePath and FPDFileName should be blank for CollectPCDAtion!");
1821 }
1822
1823 if(fpdFilePath.length() == 0 || workspacePath.length() == 0) {
1824 throw new EntityException("WorkspacePath and FPDFileName should be blank for CollectPCDAtion!");
1825 }
1826
1827 file = new File(workspacePath);
1828 if(!file.exists()) {
1829 throw new EntityException("WorkpacePath " + workspacePath + " does not exist!");
1830 }
1831
1832 file = new File(fpdFilePath);
1833
1834 if(!file.exists()) {
1835 throw new EntityException("FPD File " + fpdFilePath + " does not exist!");
1836 }
1837 }
1838
1839 /**
1840 Test case function
1841
1842 @param argv parameter from command line
1843 **/
1844 public static void main(String argv[]) throws EntityException {
1845 CollectPCDAction ca = new CollectPCDAction();
1846 ca.setWorkspacePath("G:/mdk");
1847 ca.setFPDFilePath("G:/mdk/EdkNt32Pkg/build/Nt32.fpd");
1848 ca.setActionMessageLevel(ActionMessage.MAX_MESSAGE_LEVEL);
1849 GlobalData.initInfo("Tools" + File.separator + "Conf" + File.separator + "FrameworkDatabase.db",
1850 "G:/mdk");
1851 ca.execute();
1852 }
1853 }