Move ant-related code from ParserFpd function to enable single module debug for PCD...
[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.math.BigInteger;
25 import java.util.ArrayList;
26 import java.util.Comparator;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.UUID;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
35
36 import org.apache.xmlbeans.XmlException;
37 import org.apache.xmlbeans.XmlObject;
38 import org.tianocore.DynamicPcdBuildDefinitionsDocument.DynamicPcdBuildDefinitions;
39 import org.tianocore.FrameworkModulesDocument;
40 import org.tianocore.PlatformSurfaceAreaDocument;
41 import org.tianocore.PcdBuildDefinitionDocument;
42 import org.tianocore.ModuleSADocument;
43 import org.tianocore.PcdBuildDefinitionDocument.PcdBuildDefinition;
44 import org.tianocore.build.autogen.CommonDefinition;
45 import org.tianocore.build.fpd.FpdParserTask;
46 import org.tianocore.build.global.GlobalData;
47 import org.tianocore.build.id.FpdModuleIdentification;
48 import org.tianocore.build.pcd.action.ActionMessage;
49 import org.tianocore.build.pcd.entity.DynamicTokenValue;
50 import org.tianocore.build.pcd.entity.MemoryDatabaseManager;
51 import org.tianocore.build.pcd.entity.SkuInstance;
52 import org.tianocore.build.pcd.entity.Token;
53 import org.tianocore.build.pcd.entity.UsageInstance;
54 import org.tianocore.build.pcd.exception.EntityException;
55
56 /**
57 CStructTypeDeclaration
58
59 This class is used to store the declaration string, such as
60 "UINT32 PcdPlatformFlashBaseAddress", of
61 each memember in the C structure, which is a standard C language
62 feature used to implement a simple and efficient database for
63 dynamic(ex) type PCD entry.
64 **/
65
66 class CStructTypeDeclaration {
67 String key;
68 int alignmentSize;
69 String cCode;
70 boolean initTable;
71
72 public CStructTypeDeclaration (String key, int alignmentSize, String cCode, boolean initTable) {
73 this.key = key;
74 this.alignmentSize = alignmentSize;
75 this.cCode = cCode;
76 this.initTable = initTable;
77 }
78 }
79
80 /**
81 StringTable
82
83 This class is used to store the String in a PCD database.
84
85 **/
86 class StringTable {
87 private ArrayList<String> al;
88 private ArrayList<String> alComments;
89 private String phase;
90 int len;
91
92 public StringTable (String phase) {
93 this.phase = phase;
94 al = new ArrayList<String>();
95 alComments = new ArrayList<String>();
96 len = 0;
97 }
98
99 public String getSizeMacro () {
100 return String.format(PcdDatabase.StringTableSizeMacro, phase, getSize());
101 }
102
103 private int getSize () {
104 //
105 // We have at least one Unicode Character in the table.
106 //
107 return len == 0 ? 1 : len;
108 }
109
110 public String getExistanceMacro () {
111 return String.format(PcdDatabase.StringTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");
112 }
113
114 public void genCode (ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable) {
115 final String stringTable = "StringTable";
116 final String tab = "\t";
117 final String newLine = "\r\n";
118 final String commaNewLine = ",\r\n";
119
120 CStructTypeDeclaration decl;
121
122 String cDeclCode = "";
123 String cInstCode = "";
124
125 //
126 // If we have a empty StringTable
127 //
128 if (al.size() == 0) {
129 cDeclCode += String.format("%-20s%s[1]; /* StringTable is empty */", "UINT16", stringTable) + newLine;
130 decl = new CStructTypeDeclaration (
131 stringTable,
132 2,
133 cDeclCode,
134 true
135 );
136 declaList.add(decl);
137
138 cInstCode = String.format("/* %s */", stringTable) + newLine + tab + "{ 0 }";
139 instTable.put(stringTable, cInstCode);
140 } else {
141
142 //
143 // If there is any String in the StringTable
144 //
145 for (int i = 0; i < al.size(); i++) {
146 String str = al.get(i);
147 String stringTableName;
148
149 if (i == 0) {
150 //
151 // StringTable is a well-known name in the PCD DXE driver
152 //
153 stringTableName = stringTable;
154
155 } else {
156 stringTableName = String.format("%s_%d", stringTable, i);
157 cDeclCode += tab;
158 }
159 cDeclCode += String.format("%-20s%s[%d]; /* %s */", "UINT16",
160 stringTableName, str.length() + 1,
161 alComments.get(i))
162 + newLine;
163
164 if (i == 0) {
165 cInstCode = "/* StringTable */" + newLine;
166 }
167
168 cInstCode += tab + String.format("L\"%s\" /* %s */", al.get(i), alComments.get(i));
169 if (i != al.size() - 1) {
170 cInstCode += commaNewLine;
171 }
172 }
173
174 decl = new CStructTypeDeclaration (
175 stringTable,
176 2,
177 cDeclCode,
178 true
179 );
180 declaList.add(decl);
181
182 instTable.put(stringTable, cInstCode);
183 }
184 }
185
186 public int add (String inputStr, Token token) {
187 int i;
188 int pos;
189
190 String str = inputStr;
191
192 //
193 // The input can be two types:
194 // "L\"Bootmode\"" or "Bootmode".
195 // We drop the L\" and \" for the first type.
196 if (str.startsWith("L\"") && str.endsWith("\"")) {
197 str = str.substring(2, str.length() - 1);
198 }
199 //
200 // Check if StringTable has this String already.
201 // If so, return the current pos.
202 //
203 for (i = 0, pos = 0; i < al.size(); i++) {
204 String s = al.get(i);;
205
206 if (str.equals(s)) {
207 return pos;
208 }
209 pos = s.length() + 1;
210 }
211
212 i = len;
213 //
214 // Include the NULL character at the end of String
215 //
216 len += str.length() + 1;
217 al.add(str);
218 alComments.add(token.getPrimaryKeyString());
219
220 return i;
221 }
222 }
223
224 /**
225 SizeTable
226
227 This class is used to store the Size information for
228 POINTER TYPE PCD entry in a PCD database.
229
230 **/
231 class SizeTable {
232 private ArrayList<Integer> al;
233 private ArrayList<String> alComments;
234 private String phase;
235 private int len;
236
237 public SizeTable (String phase) {
238 this.phase = phase;
239 al = new ArrayList<Integer>();
240 alComments = new ArrayList<String>();
241 len = 0;
242 }
243
244 public void genCode (ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable, String phase) {
245 final String name = "SizeTable";
246
247 CStructTypeDeclaration decl;
248 String cCode;
249
250 cCode = String.format(PcdDatabase.SizeTableDeclaration, phase);
251 decl = new CStructTypeDeclaration (
252 name,
253 2,
254 cCode,
255 true
256 );
257 declaList.add(decl);
258
259
260 cCode = PcdDatabase.genInstantiationStr(getInstantiation());
261 instTable.put(name, cCode);
262 }
263
264 private ArrayList<String> getInstantiation () {
265 ArrayList<String> Output = new ArrayList<String>();
266
267 Output.add("/* SizeTable */");
268 Output.add("{");
269 if (al.size() == 0) {
270 Output.add("\t0");
271 } else {
272 for (int index = 0; index < al.size(); index++) {
273 Integer n = al.get(index);
274 String str = "\t" + n.toString();
275
276 if (index != (al.size() - 1)) {
277 str += ",";
278 }
279
280 str += " /* " + alComments.get(index) + " */";
281 Output.add(str);
282
283 }
284 }
285 Output.add("}");
286
287 return Output;
288 }
289
290 public int add (Token token) {
291 int index = len;
292
293 len++;
294 al.add(token.datumSize);
295 alComments.add(token.getPrimaryKeyString());
296
297 return index;
298 }
299
300 public int getTableLen () {
301 return al.size() == 0 ? 1 : al.size();
302 }
303
304 }
305
306 /**
307 GuidTable
308
309 This class is used to store the GUIDs in a PCD database.
310 **/
311 class GuidTable {
312 private ArrayList<UUID> al;
313 private ArrayList<String> alComments;
314 private String phase;
315 private int len;
316 private int bodyLineNum;
317
318 public GuidTable (String phase) {
319 this.phase = phase;
320 al = new ArrayList<UUID>();
321 alComments = new ArrayList<String>();
322 len = 0;
323 bodyLineNum = 0;
324 }
325
326 public String getSizeMacro () {
327 return String.format(PcdDatabase.GuidTableSizeMacro, phase, getSize());
328 }
329
330 private int getSize () {
331 return (al.size() == 0)? 1 : al.size();
332 }
333
334 public String getExistanceMacro () {
335 return String.format(PcdDatabase.GuidTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");
336 }
337
338 public void genCode (ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable, String phase) {
339 final String name = "GuidTable";
340
341 CStructTypeDeclaration decl;
342 String cCode = "";
343
344 cCode += String.format(PcdDatabase.GuidTableDeclaration, phase);
345 decl = new CStructTypeDeclaration (
346 name,
347 8,
348 cCode,
349 true
350 );
351 declaList.add(decl);
352
353
354 cCode = PcdDatabase.genInstantiationStr(getInstantiation());
355 instTable.put(name, cCode);
356 }
357
358 private String getUuidCString (UUID uuid) {
359 String[] guidStrArray;
360
361 guidStrArray =(uuid.toString()).split("-");
362
363 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}}",
364 guidStrArray[0],
365 guidStrArray[1],
366 guidStrArray[2],
367 (guidStrArray[3].substring(0, 2)),
368 (guidStrArray[3].substring(2, 4)),
369 (guidStrArray[4].substring(0, 2)),
370 (guidStrArray[4].substring(2, 4)),
371 (guidStrArray[4].substring(4, 6)),
372 (guidStrArray[4].substring(6, 8)),
373 (guidStrArray[4].substring(8, 10)),
374 (guidStrArray[4].substring(10, 12))
375 );
376 }
377
378 private ArrayList<String> getInstantiation () {
379 ArrayList<String> Output = new ArrayList<String>();
380
381 Output.add("/* GuidTable */");
382 Output.add("{");
383
384 if (al.size() == 0) {
385 Output.add("\t" + getUuidCString(new UUID(0, 0)));
386 }
387
388 for (int i = 0; i < al.size(); i++) {
389 String str = "\t" + getUuidCString(al.get(i));
390
391 str += "/* " + alComments.get(i) + " */";
392 if (i != (al.size() - 1)) {
393 str += ",";
394 }
395 Output.add(str);
396 bodyLineNum++;
397
398 }
399 Output.add("}");
400
401 return Output;
402 }
403
404 public int add (UUID uuid, String name) {
405 //
406 // Check if GuidTable has this entry already.
407 // If so, return the GuidTable index.
408 //
409 for (int i = 0; i < al.size(); i++) {
410 if (al.get(i).equals(uuid)) {
411 return i;
412 }
413 }
414
415 len++;
416 al.add(uuid);
417 alComments.add(name);
418
419 //
420 // Return the previous Table Index
421 //
422 return len - 1;
423 }
424
425 }
426
427 /**
428 SkuIdTable
429
430 This class is used to store the SKU IDs in a PCD database.
431
432 **/
433 class SkuIdTable {
434 private ArrayList<Integer[]> al;
435 private ArrayList<String> alComment;
436 private String phase;
437 private int len;
438
439 public SkuIdTable (String phase) {
440 this.phase = phase;
441 al = new ArrayList<Integer[]>();
442 alComment = new ArrayList<String>();
443 len = 0;
444 }
445
446 public String getSizeMacro () {
447 return String.format(PcdDatabase.SkuIdTableSizeMacro, phase, getSize());
448 }
449
450 private int getSize () {
451 return (len == 0)? 1 : len;
452 }
453
454 public String getExistanceMacro () {
455 return String.format(PcdDatabase.SkuTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");
456 }
457
458 public void genCode (ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable, String phase) {
459 final String name = "SkuIdTable";
460
461 CStructTypeDeclaration decl;
462 String cCode = "";
463
464 cCode += String.format(PcdDatabase.SkuIdTableDeclaration, phase);
465 decl = new CStructTypeDeclaration (
466 name,
467 1,
468 cCode,
469 true
470 );
471 declaList.add(decl);
472
473
474 cCode = PcdDatabase.genInstantiationStr(getInstantiation());
475 instTable.put(name, cCode);
476
477 //
478 // SystemSkuId is in PEI phase PCD Database
479 //
480 if (phase.equalsIgnoreCase("PEI")) {
481 decl = new CStructTypeDeclaration (
482 "SystemSkuId",
483 1,
484 String.format("%-20sSystemSkuId;\r\n", "SKU_ID"),
485 true
486 );
487 declaList.add(decl);
488
489 instTable.put("SystemSkuId", "0");
490 }
491
492 }
493
494 private ArrayList<String> getInstantiation () {
495 ArrayList<String> Output = new ArrayList<String> ();
496
497 Output.add("/* SkuIdTable */");
498 Output.add("{");
499
500 if (al.size() == 0) {
501 Output.add("\t0");
502 }
503
504 for (int index = 0; index < al.size(); index++) {
505 String str;
506
507 str = "/* " + alComment.get(index) + "*/ ";
508 str += "/* MaxSku */ ";
509
510
511 Integer[] ia = al.get(index);
512
513 str += "\t" + ia[0].toString() + ", ";
514 for (int index2 = 1; index2 < ia.length; index2++) {
515 str += ia[index2].toString();
516 if (!((index2 == ia.length - 1) && (index == al.size() - 1))) {
517 str += ", ";
518 }
519 }
520
521 Output.add(str);
522
523 }
524
525 Output.add("}");
526
527 return Output;
528 }
529
530 public int add (Token token) {
531
532 int index;
533 int pos;
534
535 //
536 // Check if this SKU_ID Array is already in the table
537 //
538 pos = 0;
539 for (Object o: al) {
540 Integer [] s = (Integer[]) o;
541 boolean different = false;
542 if (s[0] == token.getSkuIdCount()) {
543 for (index = 1; index < s.length; index++) {
544 if (s[index] != token.skuData.get(index-1).id) {
545 different = true;
546 break;
547 }
548 }
549 } else {
550 different = true;
551 }
552 if (different) {
553 pos += s[0] + 1;
554 } else {
555 return pos;
556 }
557 }
558
559 Integer [] skuIds = new Integer[token.skuData.size() + 1];
560 skuIds[0] = new Integer(token.skuData.size());
561 for (index = 1; index < skuIds.length; index++) {
562 skuIds[index] = new Integer(token.skuData.get(index - 1).id);
563 }
564
565 index = len;
566
567 len += skuIds.length;
568 al.add(skuIds);
569 alComment.add(token.getPrimaryKeyString());
570
571 return index;
572 }
573
574 }
575
576 class LocalTokenNumberTable {
577 private ArrayList<String> al;
578 private ArrayList<String> alComment;
579 private String phase;
580 private int len;
581
582 public LocalTokenNumberTable (String phase) {
583 this.phase = phase;
584 al = new ArrayList<String>();
585 alComment = new ArrayList<String>();
586
587 len = 0;
588 }
589
590 public String getSizeMacro () {
591 return String.format(PcdDatabase.LocalTokenNumberTableSizeMacro, phase, getSize())
592 + String.format(PcdDatabase.LocalTokenNumberSizeMacro, phase, al.size());
593 }
594
595 public int getSize () {
596 return (al.size() == 0)? 1 : al.size();
597 }
598
599 public String getExistanceMacro () {
600 return String.format(PcdDatabase.DatabaseExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");
601 }
602
603 public void genCode (ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable, String phase) {
604 final String name = "LocalTokenNumberTable";
605
606 CStructTypeDeclaration decl;
607 String cCode = "";
608
609 cCode += String.format(PcdDatabase.LocalTokenNumberTableDeclaration, phase);
610 decl = new CStructTypeDeclaration (
611 name,
612 4,
613 cCode,
614 true
615 );
616 declaList.add(decl);
617
618 cCode = PcdDatabase.genInstantiationStr(getInstantiation());
619 instTable.put(name, cCode);
620 }
621
622 private ArrayList<String> getInstantiation () {
623 ArrayList<String> output = new ArrayList<String>();
624
625 output.add("/* LocalTokenNumberTable */");
626 output.add("{");
627
628 if (al.size() == 0) {
629 output.add("\t0");
630 }
631
632 for (int index = 0; index < al.size(); index++) {
633 String str;
634
635 str = "\t" + (String)al.get(index);
636
637 str += " /* " + alComment.get(index) + " */ ";
638
639
640 if (index != (al.size() - 1)) {
641 str += ",";
642 }
643
644 output.add(str);
645
646 }
647
648 output.add("}");
649
650 return output;
651 }
652
653 public int add (Token token) {
654 int index = len;
655 String str;
656
657 len++;
658
659 str = String.format(PcdDatabase.offsetOfStrTemplate, phase, token.hasDefaultValue() ? "Init" : "Uninit", token.getPrimaryKeyString());
660
661 if (token.isUnicodeStringType()) {
662 str += " | PCD_TYPE_STRING";
663 }
664
665 if (token.isSkuEnable()) {
666 str += " | PCD_TYPE_SKU_ENABLED";
667 }
668
669 if (token.getDefaultSku().type == DynamicTokenValue.VALUE_TYPE.HII_TYPE) {
670 str += " | PCD_TYPE_HII";
671 }
672
673 if (token.getDefaultSku().type == DynamicTokenValue.VALUE_TYPE.VPD_TYPE) {
674 str += " | PCD_TYPE_VPD";
675 }
676
677 al.add(str);
678 alComment.add(token.getPrimaryKeyString());
679
680 return index;
681 }
682 }
683
684 /**
685 ExMapTable
686
687 This class is used to store the table of mapping information
688 between DynamicEX ID pair(Guid, TokenNumber) and
689 the local token number assigned by PcdDatabase class.
690 **/
691 class ExMapTable {
692
693 /**
694 ExTriplet
695
696 This class is used to store the mapping information
697 between DynamicEX ID pair(Guid, TokenNumber) and
698 the local token number assigned by PcdDatabase class.
699 **/
700 class ExTriplet {
701 public Integer guidTableIdx;
702 public Long exTokenNumber;
703 public Long localTokenIdx;
704
705 public ExTriplet (int guidTableIdx, long exTokenNumber, long localTokenIdx) {
706 this.guidTableIdx = new Integer(guidTableIdx);
707 this.exTokenNumber = new Long(exTokenNumber);
708 this.localTokenIdx = new Long(localTokenIdx);
709 }
710 }
711
712 private ArrayList<ExTriplet> al;
713 private ArrayList<String> alComment;
714 private String phase;
715 private int len;
716 private int bodyLineNum;
717
718 public ExMapTable (String phase) {
719 this.phase = phase;
720 al = new ArrayList<ExTriplet>();
721 alComment = new ArrayList<String>();
722 bodyLineNum = 0;
723 len = 0;
724 }
725
726 public String getSizeMacro () {
727 return String.format(PcdDatabase.ExMapTableSizeMacro, phase, getTableLen())
728 + String.format(PcdDatabase.ExTokenNumber, phase, al.size());
729 }
730
731 public String getExistanceMacro () {
732 return String.format(PcdDatabase.ExMapTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");
733 }
734
735 public void genCode (ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable, String phase) {
736 final String exMapTableName = "ExMapTable";
737
738 sortTable();
739
740 CStructTypeDeclaration decl;
741 String cCode = "";
742
743 cCode += String.format(PcdDatabase.ExMapTableDeclaration, phase);
744 decl = new CStructTypeDeclaration (
745 exMapTableName,
746 4,
747 cCode,
748 true
749 );
750 declaList.add(decl);
751
752
753 cCode = PcdDatabase.genInstantiationStr(getInstantiation());
754 instTable.put(exMapTableName, cCode);
755 }
756
757 private ArrayList<String> getInstantiation () {
758 ArrayList<String> Output = new ArrayList<String>();
759
760 Output.add("/* ExMapTable */");
761 Output.add("{");
762 if (al.size() == 0) {
763 Output.add("\t{0, 0, 0}");
764 }
765
766 int index;
767 for (index = 0; index < al.size(); index++) {
768 String str;
769
770 ExTriplet e = (ExTriplet)al.get(index);
771
772 str = "\t" + "{ " + String.format("0x%08X", e.exTokenNumber) + ", ";
773 str += e.localTokenIdx.toString() + ", ";
774 str += e.guidTableIdx.toString();
775
776 str += "}" + " /* " + alComment.get(index) + " */" ;
777
778 if (index != al.size() - 1) {
779 str += ",";
780 }
781
782 Output.add(str);
783 bodyLineNum++;
784
785 }
786
787 Output.add("}");
788
789 return Output;
790 }
791
792 public int add (int localTokenIdx, long exTokenNum, int guidTableIdx, String name) {
793 int index = len;
794
795 len++;
796 al.add(new ExTriplet(guidTableIdx, exTokenNum, localTokenIdx));
797 alComment.add(name);
798
799 return index;
800 }
801
802 private int getTableLen () {
803 return al.size() == 0 ? 1 : al.size();
804 }
805
806 //
807 // To simplify the algorithm for GetNextToken and GetNextTokenSpace in
808 // PCD PEIM/Driver, we need to sort the ExMapTable according to the
809 // following order:
810 // 1) ExGuid
811 // 2) ExTokenNumber
812 //
813 class ExTripletComp implements Comparator<ExTriplet> {
814 public int compare (ExTriplet a, ExTriplet b) {
815 if (a.guidTableIdx == b.guidTableIdx ) {
816 //
817 // exTokenNumber is long, we can't use simple substraction.
818 //
819 if (a.exTokenNumber > b.exTokenNumber) {
820 return 1;
821 } else if (a.exTokenNumber == b.exTokenNumber) {
822 return 0;
823 } else {
824 return -1;
825 }
826 }
827
828 return a.guidTableIdx - b.guidTableIdx;
829 }
830 }
831
832 private void sortTable () {
833 java.util.Comparator<ExTriplet> comparator = new ExTripletComp();
834 java.util.Collections.sort(al, comparator);
835 }
836 }
837
838 /**
839 PcdDatabase
840
841 This class is used to generate C code for Autogen.h and Autogen.c of
842 a PCD service DXE driver and PCD service PEIM.
843 **/
844 class PcdDatabase {
845
846 private final static int SkuHeadAlignmentSize = 4;
847 private final String newLine = "\r\n";
848 private final String commaNewLine = ",\r\n";
849 private final String tab = "\t";
850 public final static String ExMapTableDeclaration = "DYNAMICEX_MAPPING ExMapTable[%s_EXMAPPING_TABLE_SIZE];\r\n";
851 public final static String GuidTableDeclaration = "EFI_GUID GuidTable[%s_GUID_TABLE_SIZE];\r\n";
852 public final static String LocalTokenNumberTableDeclaration = "UINT32 LocalTokenNumberTable[%s_LOCAL_TOKEN_NUMBER_TABLE_SIZE];\r\n";
853 public final static String StringTableDeclaration = "UINT16 StringTable[%s_STRING_TABLE_SIZE];\r\n";
854 public final static String SizeTableDeclaration = "UINT16 SizeTable[%s_LOCAL_TOKEN_NUMBER_TABLE_SIZE];\r\n";
855 public final static String SkuIdTableDeclaration = "UINT8 SkuIdTable[%s_SKUID_TABLE_SIZE];\r\n";
856
857
858 public final static String ExMapTableSizeMacro = "#define %s_EXMAPPING_TABLE_SIZE %d\r\n";
859 public final static String ExTokenNumber = "#define %s_EX_TOKEN_NUMBER %d\r\n";
860 public final static String GuidTableSizeMacro = "#define %s_GUID_TABLE_SIZE %d\r\n";
861 public final static String LocalTokenNumberTableSizeMacro = "#define %s_LOCAL_TOKEN_NUMBER_TABLE_SIZE %d\r\n";
862 public final static String LocalTokenNumberSizeMacro = "#define %s_LOCAL_TOKEN_NUMBER %d\r\n";
863 public final static String StringTableSizeMacro = "#define %s_STRING_TABLE_SIZE %d\r\n";
864 public final static String SkuIdTableSizeMacro = "#define %s_SKUID_TABLE_SIZE %d\r\n";
865
866
867 public final static String ExMapTableExistenceMacro = "#define %s_EXMAP_TABLE_EMPTY %s\r\n";
868 public final static String GuidTableExistenceMacro = "#define %s_GUID_TABLE_EMPTY %s\r\n";
869 public final static String DatabaseExistenceMacro = "#define %s_DATABASE_EMPTY %s\r\n";
870 public final static String StringTableExistenceMacro = "#define %s_STRING_TABLE_EMPTY %s\r\n";
871 public final static String SkuTableExistenceMacro = "#define %s_SKUID_TABLE_EMPTY %s\r\n";
872
873 public final static String offsetOfSkuHeadStrTemplate = "offsetof(%s_PCD_DATABASE, %s.%s_SkuDataTable)";
874 public final static String offsetOfVariableEnabledDefault = "offsetof(%s_PCD_DATABASE, %s.%s_VariableDefault_%d)";
875 public final static String offsetOfStrTemplate = "offsetof(%s_PCD_DATABASE, %s.%s)";
876
877 private final static String skuDataTableTemplate = "SkuDataTable";
878
879
880 private StringTable stringTable;
881 private GuidTable guidTable;
882 private LocalTokenNumberTable localTokenNumberTable;
883 private SkuIdTable skuIdTable;
884 private SizeTable sizeTable;
885 private ExMapTable exMapTable;
886
887 private ArrayList<Token> alTokens;
888 private String phase;
889 private int assignedTokenNumber;
890
891 //
892 // Use two class global variable to store
893 // temperary
894 //
895 private String privateGlobalName;
896 private String privateGlobalCCode;
897 //
898 // After Major changes done to the PCD
899 // database generation class PcdDatabase
900 // Please increment the version and please
901 // also update the version number in PCD
902 // service PEIM and DXE driver accordingly.
903 //
904 private final int version = 2;
905
906 private String hString;
907 private String cString;
908
909 /**
910 Constructor for PcdDatabase class.
911
912 <p>We have two PCD dynamic(ex) database for the Framework implementation. One
913 for PEI phase and the other for DXE phase. </p>
914
915 @param alTokens A ArrayList of Dynamic(EX) PCD entry.
916 @param exePhase The phase to generate PCD database for: valid input
917 is "PEI" or "DXE".
918 @param startLen The starting Local Token Number for the PCD database. For
919 PEI phase, the starting Local Token Number starts from 0.
920 For DXE phase, the starting Local Token Number starts
921 from the total number of PCD entry of PEI phase.
922 @return void
923 **/
924 public PcdDatabase (ArrayList<Token> alTokens, String exePhase, int startLen) {
925 phase = exePhase;
926
927 stringTable = new StringTable(phase);
928 guidTable = new GuidTable(phase);
929 localTokenNumberTable = new LocalTokenNumberTable(phase);
930 skuIdTable = new SkuIdTable(phase);
931 sizeTable = new SizeTable(phase);
932 exMapTable = new ExMapTable(phase);
933
934 //
935 // Local token number 0 is reserved for INVALID_TOKEN_NUMBER.
936 // So we will increment 1 for the startLen passed from the
937 // constructor.
938 //
939 assignedTokenNumber = startLen + 1;
940 this.alTokens = alTokens;
941 }
942
943 private void getNonExAndExTokens (ArrayList<Token> alTokens, List<Token> nexTokens, List<Token> exTokens) {
944 for (int i = 0; i < alTokens.size(); i++) {
945 Token t = (Token)alTokens.get(i);
946 if (t.isDynamicEx()) {
947 exTokens.add(t);
948 } else {
949 nexTokens.add(t);
950 }
951 }
952
953 return;
954 }
955
956 private int getDataTypeAlignmentSize (Token token) {
957 switch (token.datumType) {
958 case UINT8:
959 return 1;
960 case UINT16:
961 return 2;
962 case UINT32:
963 return 4;
964 case UINT64:
965 return 8;
966 case POINTER:
967 return 1;
968 case BOOLEAN:
969 return 1;
970 default:
971 return 1;
972 }
973 }
974
975 private int getHiiPtrTypeAlignmentSize(Token token) {
976 switch (token.datumType) {
977 case UINT8:
978 return 1;
979 case UINT16:
980 return 2;
981 case UINT32:
982 return 4;
983 case UINT64:
984 return 8;
985 case POINTER:
986 if (token.isHiiEnable()) {
987 if (token.isHiiDefaultValueUnicodeStringType()) {
988 return 2;
989 }
990 }
991 return 1;
992 case BOOLEAN:
993 return 1;
994 default:
995 return 1;
996 }
997 }
998
999 private int getAlignmentSize (Token token) {
1000 if (token.getDefaultSku().type == DynamicTokenValue.VALUE_TYPE.HII_TYPE) {
1001 return 2;
1002 }
1003
1004 if (token.getDefaultSku().type == DynamicTokenValue.VALUE_TYPE.VPD_TYPE) {
1005 return 4;
1006 }
1007
1008 if (token.isUnicodeStringType()) {
1009 return 2;
1010 }
1011
1012 return getDataTypeAlignmentSize(token);
1013 }
1014
1015 public String getCString () {
1016 return cString;
1017 }
1018
1019 public String getHString () {
1020 return hString;
1021 }
1022
1023 private void genCodeWorker(Token t,
1024 ArrayList<CStructTypeDeclaration> declaList,
1025 HashMap<String, String> instTable, String phase)
1026 throws EntityException {
1027
1028 CStructTypeDeclaration decl;
1029
1030 //
1031 // Insert SKU_HEAD if isSkuEnable is true
1032 //
1033 if (t.isSkuEnable()) {
1034 int tableIdx;
1035 tableIdx = skuIdTable.add(t);
1036 decl = new CStructTypeDeclaration(t.getPrimaryKeyString(),
1037 SkuHeadAlignmentSize, getSkuEnabledTypeDeclaration(t), true);
1038 declaList.add(decl);
1039 instTable.put(t.getPrimaryKeyString(),
1040 getSkuEnabledTypeInstantiaion(t, tableIdx));
1041 }
1042
1043 //
1044 // Insert PCD_ENTRY declaration and instantiation
1045 //
1046 getCDeclarationString(t);
1047
1048 decl = new CStructTypeDeclaration(privateGlobalName,
1049 getAlignmentSize(t), privateGlobalCCode, t.hasDefaultValue());
1050 declaList.add(decl);
1051
1052 if (t.hasDefaultValue()) {
1053 instTable.put(privateGlobalName,
1054 getTypeInstantiation(t, declaList, instTable, phase)
1055 );
1056 }
1057
1058 }
1059
1060 private void ProcessTokens (List<Token> tokens,
1061 ArrayList<CStructTypeDeclaration> cStructDeclList,
1062 HashMap<String, String> cStructInstTable,
1063 String phase
1064 )
1065 throws EntityException {
1066
1067 for (int idx = 0; idx < tokens.size(); idx++) {
1068 Token t = tokens.get(idx);
1069
1070 genCodeWorker (t, cStructDeclList, cStructInstTable, phase);
1071
1072 sizeTable.add(t);
1073 localTokenNumberTable.add(t);
1074 t.tokenNumber = assignedTokenNumber++;
1075
1076 //
1077 // Add a mapping if this dynamic PCD entry is a EX type
1078 //
1079 if (t.isDynamicEx()) {
1080 exMapTable.add((int)t.tokenNumber,
1081 t.dynamicExTokenNumber,
1082 guidTable.add(t.tokenSpaceName, t.getPrimaryKeyString()),
1083 t.getPrimaryKeyString()
1084 );
1085 }
1086 }
1087
1088 }
1089
1090 public void genCode () throws EntityException {
1091
1092 ArrayList<CStructTypeDeclaration> cStructDeclList = new ArrayList<CStructTypeDeclaration>();
1093 HashMap<String, String> cStructInstTable = new HashMap<String, String>();
1094
1095 List<Token> nexTokens = new ArrayList<Token> ();
1096 List<Token> exTokens = new ArrayList<Token> ();
1097
1098 getNonExAndExTokens (alTokens, nexTokens, exTokens);
1099
1100 //
1101 // We have to process Non-Ex type PCD entry first. The reason is
1102 // that our optimization assumes that the Token Number of Non-Ex
1103 // PCD entry start from 1 (for PEI phase) and grows continously upwards.
1104 //
1105 // EX type token number starts from the last Non-EX PCD entry and
1106 // grows continously upwards.
1107 //
1108 ProcessTokens (nexTokens, cStructDeclList, cStructInstTable, phase);
1109 ProcessTokens (exTokens, cStructDeclList, cStructInstTable, phase);
1110
1111 stringTable.genCode(cStructDeclList, cStructInstTable);
1112 skuIdTable.genCode(cStructDeclList, cStructInstTable, phase);
1113 exMapTable.genCode(cStructDeclList, cStructInstTable, phase);
1114 localTokenNumberTable.genCode(cStructDeclList, cStructInstTable, phase);
1115 sizeTable.genCode(cStructDeclList, cStructInstTable, phase);
1116 guidTable.genCode(cStructDeclList, cStructInstTable, phase);
1117
1118 hString = genCMacroCode ();
1119
1120 HashMap <String, String> result;
1121
1122 result = genCStructCode(cStructDeclList,
1123 cStructInstTable,
1124 phase
1125 );
1126
1127 hString += result.get("initDeclStr");
1128 hString += result.get("uninitDeclStr");
1129
1130 hString += String.format("#define PCD_%s_SERVICE_DRIVER_VERSION %d", phase, version);
1131
1132 cString = newLine + newLine + result.get("initInstStr");
1133
1134 }
1135
1136 private String genCMacroCode () {
1137 String macroStr = "";
1138
1139 //
1140 // Generate size info Macro for all Tables
1141 //
1142 macroStr += guidTable.getSizeMacro();
1143 macroStr += stringTable.getSizeMacro();
1144 macroStr += skuIdTable.getSizeMacro();
1145 macroStr += localTokenNumberTable.getSizeMacro();
1146 macroStr += exMapTable.getSizeMacro();
1147
1148 //
1149 // Generate existance info Macro for all Tables
1150 //
1151 macroStr += guidTable.getExistanceMacro();
1152 macroStr += stringTable.getExistanceMacro();
1153 macroStr += skuIdTable.getExistanceMacro();
1154 macroStr += localTokenNumberTable.getExistanceMacro();
1155 macroStr += exMapTable.getExistanceMacro();
1156
1157 macroStr += newLine;
1158
1159 return macroStr;
1160 }
1161
1162 private HashMap <String, String> genCStructCode(
1163 ArrayList<CStructTypeDeclaration> declaList,
1164 HashMap<String, String> instTable,
1165 String phase
1166 ) {
1167
1168 int i;
1169 HashMap <String, String> result = new HashMap<String, String>();
1170 HashMap <Integer, ArrayList<String>> alignmentInitDecl = new HashMap<Integer, ArrayList<String>>();
1171 HashMap <Integer, ArrayList<String>> alignmentUninitDecl = new HashMap<Integer, ArrayList<String>>();
1172 HashMap <Integer, ArrayList<String>> alignmentInitInst = new HashMap<Integer, ArrayList<String>>();
1173
1174 //
1175 // Initialize the storage for each alignment
1176 //
1177 for (i = 8; i > 0; i>>=1) {
1178 alignmentInitDecl.put(new Integer(i), new ArrayList<String>());
1179 alignmentInitInst.put(new Integer(i), new ArrayList<String>());
1180 alignmentUninitDecl.put(new Integer(i), new ArrayList<String>());
1181 }
1182
1183 String initDeclStr = "typedef struct {" + newLine;
1184 String initInstStr = String.format("%s_PCD_DATABASE_INIT g%sPcdDbInit = { ", phase.toUpperCase(), phase.toUpperCase()) + newLine;
1185 String uninitDeclStr = "typedef struct {" + newLine;
1186
1187 //
1188 // Sort all C declaration and instantiation base on Alignment Size
1189 //
1190 for (Object d : declaList) {
1191 CStructTypeDeclaration decl = (CStructTypeDeclaration) d;
1192
1193 if (decl.initTable) {
1194 alignmentInitDecl.get(new Integer(decl.alignmentSize)).add(decl.cCode);
1195 alignmentInitInst.get(new Integer(decl.alignmentSize)).add(instTable.get(decl.key));
1196 } else {
1197 alignmentUninitDecl.get(new Integer(decl.alignmentSize)).add(decl.cCode);
1198 }
1199 }
1200
1201 //
1202 // Generate code for every alignment size
1203 //
1204 boolean uinitDatabaseEmpty = true;
1205 for (int align = 8; align > 0; align >>= 1) {
1206 ArrayList<String> declaListBasedOnAlignment = alignmentInitDecl.get(new Integer(align));
1207 ArrayList<String> instListBasedOnAlignment = alignmentInitInst.get(new Integer(align));
1208 for (i = 0; i < declaListBasedOnAlignment.size(); i++) {
1209 initDeclStr += tab + declaListBasedOnAlignment.get(i);
1210 initInstStr += tab + instListBasedOnAlignment.get(i);
1211
1212 //
1213 // We made a assumption that both PEI_PCD_DATABASE and DXE_PCD_DATABASE
1214 // has a least one data memember with alignment size of 1. So we can
1215 // remove the last "," in the C structure instantiation string. Luckily,
1216 // this is true as both data structure has SKUID_TABLE anyway.
1217 //
1218 if ((align == 1) && (i == declaListBasedOnAlignment.size() - 1)) {
1219 initInstStr += newLine;
1220 } else {
1221 initInstStr += commaNewLine;
1222 }
1223 }
1224
1225 declaListBasedOnAlignment = alignmentUninitDecl.get(new Integer(align));
1226
1227 if (declaListBasedOnAlignment.size() != 0) {
1228 uinitDatabaseEmpty = false;
1229 }
1230
1231 for (Object d : declaListBasedOnAlignment) {
1232 String s = (String)d;
1233 uninitDeclStr += tab + s;
1234 }
1235 }
1236
1237 if (uinitDatabaseEmpty) {
1238 uninitDeclStr += tab + String.format("%-20sdummy; /* PCD_DATABASE_UNINIT is emptry */\r\n", "UINT8");
1239 }
1240
1241 initDeclStr += String.format("} %s_PCD_DATABASE_INIT;", phase) + newLine + newLine;
1242 initInstStr += "};" + newLine;
1243 uninitDeclStr += String.format("} %s_PCD_DATABASE_UNINIT;", phase) + newLine + newLine;
1244
1245 result.put("initDeclStr", initDeclStr);
1246 result.put("initInstStr", initInstStr);
1247 result.put("uninitDeclStr", uninitDeclStr);
1248
1249 return result;
1250 }
1251
1252 public static String genInstantiationStr (ArrayList<String> alStr) {
1253 String str = "";
1254 for (int i = 0; i< alStr.size(); i++) {
1255 if (i != 0) {
1256 str += "\t";
1257 }
1258 str += alStr.get(i);
1259 if (i != alStr.size() - 1) {
1260 str += "\r\n";
1261 }
1262 }
1263
1264 return str;
1265 }
1266
1267 private String getSkuEnabledTypeDeclaration (Token token) {
1268 return String.format("%-20s%s;\r\n", "SKU_HEAD", token.getPrimaryKeyString());
1269 }
1270
1271 private String getSkuEnabledTypeInstantiaion (Token token, int SkuTableIdx) {
1272
1273 String offsetof = String.format(PcdDatabase.offsetOfSkuHeadStrTemplate, phase, token.hasDefaultValue()? "Init" : "Uninit", token.getPrimaryKeyString());
1274 return String.format("{ %s, %d } /* SKU_ENABLED: %s */", offsetof, SkuTableIdx, token.getPrimaryKeyString());
1275 }
1276
1277 private String getDataTypeInstantiationForVariableDefault (Token token, String cName, int skuId) {
1278 return String.format("%s /* %s */", token.skuData.get(skuId).value.hiiDefaultValue, cName);
1279 }
1280
1281 private String getCType (Token t)
1282 throws EntityException {
1283
1284 if (t.isHiiEnable()) {
1285 return "VARIABLE_HEAD";
1286 }
1287
1288 if (t.isVpdEnable()) {
1289 return "VPD_HEAD";
1290 }
1291
1292 if (t.isUnicodeStringType()) {
1293 return "STRING_HEAD";
1294 }
1295
1296 switch (t.datumType) {
1297 case UINT64:
1298 return "UINT64";
1299 case UINT32:
1300 return "UINT32";
1301 case UINT16:
1302 return "UINT16";
1303 case UINT8:
1304 return "UINT8";
1305 case BOOLEAN:
1306 return "BOOLEAN";
1307 case POINTER:
1308 return "UINT8";
1309 default:
1310 throw new EntityException("Unknown type in getDataTypeCDeclaration");
1311 }
1312 }
1313
1314 private void getCDeclarationString(Token t)
1315 throws EntityException {
1316
1317 if (t.isSkuEnable()) {
1318 privateGlobalName = String.format("%s_%s", t.getPrimaryKeyString(), skuDataTableTemplate);
1319 } else {
1320 privateGlobalName = t.getPrimaryKeyString();
1321 }
1322
1323 String type = getCType(t);
1324 if ((t.datumType == Token.DATUM_TYPE.POINTER) && (!t.isHiiEnable())) {
1325 int bufferSize;
1326 if (t.isASCIIStringType()) {
1327 //
1328 // Build tool will add a NULL string at the end of the ASCII string
1329 //
1330 bufferSize = t.datumSize + 1;
1331 } else {
1332 bufferSize = t.datumSize;
1333 }
1334 privateGlobalCCode = String.format("%-20s%s[%d][%d];\r\n", type, privateGlobalName, t.getSkuIdCount(), bufferSize);
1335 } else {
1336 privateGlobalCCode = String.format("%-20s%s[%d];\r\n", type, privateGlobalName, t.getSkuIdCount());
1337 }
1338 }
1339
1340 private String getDataTypeDeclarationForVariableDefault (Token token, String cName, int skuId)
1341 throws EntityException {
1342
1343 String typeStr;
1344
1345 if (token.datumType == Token.DATUM_TYPE.UINT8) {
1346 typeStr = "UINT8";
1347 } else if (token.datumType == Token.DATUM_TYPE.UINT16) {
1348 typeStr = "UINT16";
1349 } else if (token.datumType == Token.DATUM_TYPE.UINT32) {
1350 typeStr = "UINT32";
1351 } else if (token.datumType == Token.DATUM_TYPE.UINT64) {
1352 typeStr = "UINT64";
1353 } else if (token.datumType == Token.DATUM_TYPE.BOOLEAN) {
1354 typeStr = "BOOLEAN";
1355 } else if (token.datumType == Token.DATUM_TYPE.POINTER) {
1356 int size;
1357 if (token.isHiiDefaultValueUnicodeStringType()) {
1358 typeStr = "UINT16";
1359 //
1360 // Include the NULL charactor
1361 //
1362 size = token.datumSize / 2 + 1;
1363 } else {
1364 typeStr = "UINT8";
1365 if (token.isHiiDefaultValueASCIIStringType()) {
1366 //
1367 // Include the NULL charactor
1368 //
1369 size = token.datumSize + 1;
1370 } else {
1371 size = token.datumSize;
1372 }
1373 }
1374 return String.format("%-20s%s[%d];\r\n", typeStr, cName, size);
1375 } else {
1376 throw new EntityException("Unknown DATUM_TYPE type in when generating code for VARIABLE_ENABLED PCD entry");
1377 }
1378
1379 return String.format("%-20s%s;\r\n", typeStr, cName);
1380 }
1381
1382 private String getTypeInstantiation (Token t, ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable, String phase) throws EntityException {
1383
1384 int i;
1385
1386 String s;
1387 s = String.format("/* %s */", t.getPrimaryKeyString()) + newLine;
1388 s += tab + "{" + newLine;
1389
1390 for (i = 0; i < t.skuData.size(); i++) {
1391 if (t.isUnicodeStringType()) {
1392 s += tab + tab + String.format("{ %d }", stringTable.add(t.skuData.get(i).value.value, t));
1393 } else if (t.isHiiEnable()) {
1394 /* VPD_HEAD definition
1395 typedef struct {
1396 UINT16 GuidTableIndex; // Offset in Guid Table in units of GUID.
1397 UINT16 StringIndex; // Offset in String Table in units of UINT16.
1398 UINT16 Offset; // Offset in Variable
1399 UINT16 DefaultValueOffset; // Offset of the Default Value
1400 } VARIABLE_HEAD ;
1401 */
1402 String variableDefaultName = String.format("%s_VariableDefault_%d", t.getPrimaryKeyString(), i);
1403
1404 s += tab + tab + String.format("{ %d, %d, %s, %s }", guidTable.add(t.skuData.get(i).value.variableGuid, t.getPrimaryKeyString()),
1405 stringTable.add(t.skuData.get(i).value.getStringOfVariableName(), t),
1406 t.skuData.get(i).value.variableOffset,
1407 String.format("offsetof(%s_PCD_DATABASE, Init.%s)", phase, variableDefaultName)
1408 );
1409 //
1410 // We need to support the default value, so we add the declaration and
1411 // the instantiation for the default value.
1412 //
1413 CStructTypeDeclaration decl = new CStructTypeDeclaration (variableDefaultName,
1414 getHiiPtrTypeAlignmentSize(t),
1415 getDataTypeDeclarationForVariableDefault(t, variableDefaultName, i),
1416 true
1417 );
1418 declaList.add(decl);
1419 instTable.put(variableDefaultName, getDataTypeInstantiationForVariableDefault (t, variableDefaultName, i));
1420 } else if (t.isVpdEnable()) {
1421 /* typedef struct {
1422 UINT32 Offset;
1423 } VPD_HEAD;
1424 */
1425 s += tab + tab + String.format("{ %s }", t.skuData.get(i).value.vpdOffset);
1426 } else {
1427 if (t.isByteStreamType()) {
1428 //
1429 // Byte stream type input has their own "{" "}", so we won't help to insert.
1430 //
1431 s += tab + tab + String.format(" %s ", t.skuData.get(i).value.value);
1432 } else {
1433 s += tab + tab + String.format("{ %s }", t.skuData.get(i).value.value);
1434 }
1435 }
1436
1437 if (i != t.skuData.size() - 1) {
1438 s += commaNewLine;
1439 } else {
1440 s += newLine;
1441 }
1442
1443 }
1444
1445 s += tab + "}";
1446
1447 return s;
1448 }
1449
1450 public static String getPcdDatabaseCommonDefinitions ()
1451 throws EntityException {
1452
1453 String retStr = "";
1454 try {
1455 File file = new File(GlobalData.getWorkspacePath() + File.separator +
1456 "Tools" + File.separator +
1457 "Conf" + File.separator +
1458 "Pcd" + File.separator +
1459 "PcdDatabaseCommonDefinitions.sample");
1460 FileReader reader = new FileReader(file);
1461 BufferedReader in = new BufferedReader(reader);
1462 String str;
1463 while ((str = in.readLine()) != null) {
1464 retStr = retStr +"\r\n" + str;
1465 }
1466 } catch (Exception ex) {
1467 throw new EntityException("Fatal error when generating PcdDatabase Common Definitions");
1468 }
1469
1470 return retStr;
1471 }
1472
1473 public static String getPcdDxeDatabaseDefinitions ()
1474 throws EntityException {
1475
1476 String retStr = "";
1477 try {
1478 File file = new File(GlobalData.getWorkspacePath() + File.separator +
1479 "Tools" + File.separator +
1480 "Conf" + File.separator +
1481 "Pcd" + File.separator +
1482 "PcdDatabaseDxeDefinitions.sample");
1483 FileReader reader = new FileReader(file);
1484 BufferedReader in = new BufferedReader(reader);
1485 String str;
1486 while ((str = in.readLine()) != null) {
1487 retStr = retStr +"\r\n" + str;
1488 }
1489 } catch (Exception ex) {
1490 throw new EntityException("Fatal error when generating PcdDatabase Dxe Definitions");
1491 }
1492
1493 return retStr;
1494 }
1495
1496 public static String getPcdPeiDatabaseDefinitions ()
1497 throws EntityException {
1498
1499 String retStr = "";
1500 try {
1501 File file = new File(GlobalData.getWorkspacePath() + File.separator +
1502 "Tools" + File.separator +
1503 "Conf" + File.separator +
1504 "Pcd" + File.separator +
1505 "PcdDatabasePeiDefinitions.sample");
1506 FileReader reader = new FileReader(file);
1507 BufferedReader in = new BufferedReader(reader);
1508 String str;
1509 while ((str = in.readLine()) != null) {
1510 retStr = retStr +"\r\n" + str;
1511 }
1512 } catch (Exception ex) {
1513 throw new EntityException("Fatal error when generating PcdDatabase Pei Definitions");
1514 }
1515
1516 return retStr;
1517 }
1518
1519 }
1520
1521 class ModuleInfo {
1522 private String type;
1523 private FpdModuleIdentification moduleId;
1524 private PcdBuildDefinitionDocument.PcdBuildDefinition pcdBuildDef;
1525
1526
1527
1528 public ModuleInfo (FpdModuleIdentification moduleId, String type, XmlObject pcdDef) {
1529 this.moduleId = moduleId;
1530 this.type = type;
1531 this.pcdBuildDef = ((PcdBuildDefinitionDocument)pcdDef).getPcdBuildDefinition();
1532 }
1533 public String getModuleType (){
1534 return this.type;
1535 }
1536 public FpdModuleIdentification getModuleId (){
1537 return this.moduleId;
1538 }
1539 public PcdBuildDefinitionDocument.PcdBuildDefinition getPcdBuildDef(){
1540 return this.pcdBuildDef;
1541 }
1542 }
1543
1544 /** This action class is to collect PCD information from MSA, SPD, FPD xml file.
1545 This class will be used for wizard and build tools, So it can *not* inherit
1546 from buildAction or UIAction.
1547 **/
1548 public class CollectPCDAction {
1549 /// memoryDatabase hold all PCD information collected from SPD, MSA, FPD.
1550 private MemoryDatabaseManager dbManager;
1551
1552 /// Workspacepath hold the workspace information.
1553 private String workspacePath;
1554
1555 /// FPD file is the root file.
1556 private String fpdFilePath;
1557
1558 /// Message level for CollectPCDAction.
1559 private int originalMessageLevel;
1560
1561 /// Cache the fpd docment instance for private usage.
1562 private PlatformSurfaceAreaDocument fpdDocInstance;
1563
1564 /// xmlObject name
1565 private static String xmlObjectName = "PcdBuildDefinition";
1566
1567 /**
1568 Set WorkspacePath parameter for this action class.
1569
1570 @param workspacePath parameter for this action
1571 **/
1572 public void setWorkspacePath(String workspacePath) {
1573 this.workspacePath = workspacePath;
1574 }
1575
1576 /**
1577 Set action message level for CollectPcdAction tool.
1578
1579 The message should be restored when this action exit.
1580
1581 @param actionMessageLevel parameter for this action
1582 **/
1583 public void setActionMessageLevel(int actionMessageLevel) {
1584 originalMessageLevel = ActionMessage.messageLevel;
1585 ActionMessage.messageLevel = actionMessageLevel;
1586 }
1587
1588 /**
1589 Set FPDFileName parameter for this action class.
1590
1591 @param fpdFilePath fpd file path
1592 **/
1593 public void setFPDFilePath(String fpdFilePath) {
1594 this.fpdFilePath = fpdFilePath;
1595 }
1596
1597 /**
1598 Common function interface for outer.
1599
1600 @param workspacePath The path of workspace of current build or analysis.
1601 @param fpdFilePath The fpd file path of current build or analysis.
1602 @param messageLevel The message level for this Action.
1603
1604 @throws Exception The exception of this function. Because it can *not* be predict
1605 where the action class will be used. So only Exception can be throw.
1606
1607 **/
1608 public void perform(String workspacePath, String fpdFilePath,
1609 int messageLevel) throws Exception {
1610 setWorkspacePath(workspacePath);
1611 setFPDFilePath(fpdFilePath);
1612 setActionMessageLevel(messageLevel);
1613 checkParameter();
1614 execute();
1615 ActionMessage.messageLevel = originalMessageLevel;
1616 }
1617
1618 /**
1619 Core execution function for this action class.
1620
1621 This function work flows will be:
1622 1) Collect and prepocess PCD information from FPD file, all PCD
1623 information will be stored into memory database.
1624 2) Generate 3 strings for
1625 a) All modules using Dynamic(Ex) PCD entry.(Token Number)
1626 b) PEI PCDDatabase (C Structure) for PCD Service PEIM.
1627 c) DXE PCD Database (C structure) for PCD Service DXE.
1628
1629
1630 @throws EntityException Exception indicate failed to execute this action.
1631
1632 **/
1633 public void execute() throws EntityException {
1634 //
1635 // Get memoryDatabaseManager instance from GlobalData.
1636 // The memoryDatabaseManager should be initialized for whatever build
1637 // tools or wizard tools
1638 //
1639 if((dbManager = GlobalData.getPCDMemoryDBManager()) == null) {
1640 throw new EntityException("The instance of PCD memory database manager is null");
1641 }
1642
1643 //
1644 // Collect all PCD information defined in FPD file.
1645 // Evenry token defind in FPD will be created as an token into
1646 // memory database.
1647 //
1648 createTokenInDBFromFPD();
1649
1650 //
1651 // Call Private function genPcdDatabaseSourceCode (void); ComponentTypeBsDriver
1652 // 1) Generate for PEI, DXE PCD DATABASE's definition and initialization.
1653 //
1654 genPcdDatabaseSourceCode ();
1655
1656 }
1657
1658 /**
1659 This function generates source code for PCD Database.
1660
1661 @param void
1662 @throws EntityException If the token does *not* exist in memory database.
1663
1664 **/
1665 private void genPcdDatabaseSourceCode()
1666 throws EntityException {
1667 String PcdCommonHeaderString = PcdDatabase.getPcdDatabaseCommonDefinitions ();
1668
1669 ArrayList<Token> alPei = new ArrayList<Token> ();
1670 ArrayList<Token> alDxe = new ArrayList<Token> ();
1671
1672 dbManager.getTwoPhaseDynamicRecordArray(alPei, alDxe);
1673 PcdDatabase pcdPeiDatabase = new PcdDatabase (alPei, "PEI", 0);
1674 pcdPeiDatabase.genCode();
1675 MemoryDatabaseManager.PcdPeimHString = PcdCommonHeaderString + pcdPeiDatabase.getHString()
1676 + PcdDatabase.getPcdPeiDatabaseDefinitions();
1677 MemoryDatabaseManager.PcdPeimCString = pcdPeiDatabase.getCString();
1678
1679 PcdDatabase pcdDxeDatabase = new PcdDatabase (alDxe,
1680 "DXE",
1681 alPei.size()
1682 );
1683 pcdDxeDatabase.genCode();
1684 MemoryDatabaseManager.PcdDxeHString = MemoryDatabaseManager.PcdPeimHString + pcdDxeDatabase.getHString()
1685 + PcdDatabase.getPcdDxeDatabaseDefinitions();
1686 MemoryDatabaseManager.PcdDxeCString = pcdDxeDatabase.getCString();
1687 }
1688
1689 /**
1690 Get component array from FPD.
1691
1692 This function maybe provided by some Global class.
1693
1694 @return List<ModuleInfo> the component array.
1695
1696 */
1697 private List<ModuleInfo> getComponentsFromFPD()
1698 throws EntityException {
1699 List<ModuleInfo> allModules = new ArrayList<ModuleInfo>();
1700 ModuleInfo current = null;
1701 int index = 0;
1702 FrameworkModulesDocument.FrameworkModules fModules = null;
1703 ModuleSADocument.ModuleSA[] modules = null;
1704 HashMap<String, XmlObject> map = new HashMap<String, XmlObject>();
1705
1706 if (fpdDocInstance == null) {
1707 try {
1708 fpdDocInstance = (PlatformSurfaceAreaDocument)XmlObject.Factory.parse(new File(fpdFilePath));
1709 } catch(IOException ioE) {
1710 throw new EntityException("File IO error for xml file:" + fpdFilePath + "\n" + ioE.getMessage());
1711 } catch(XmlException xmlE) {
1712 throw new EntityException("Can't parse the FPD xml fle:" + fpdFilePath + "\n" + xmlE.getMessage());
1713 }
1714
1715 }
1716
1717 Map<FpdModuleIdentification,XmlObject>pcdBuildDef = GlobalData.getFpdModuleSaXmlObject(CollectPCDAction.xmlObjectName);
1718 Set<FpdModuleIdentification> pcdBuildKeySet = pcdBuildDef.keySet();
1719 Iterator item = pcdBuildKeySet.iterator();
1720 while (item.hasNext()){
1721 FpdModuleIdentification id = (FpdModuleIdentification)item.next();
1722 allModules.add(new ModuleInfo(id, id.getModule().getModuleType(),pcdBuildDef.get(id)));
1723 }
1724
1725 return allModules;
1726 }
1727
1728 /**
1729 Create token instance object into memory database, the token information
1730 comes for FPD file. Normally, FPD file will contain all token platform
1731 informations.
1732
1733 @return FrameworkPlatformDescriptionDocument The FPD document instance for furture usage.
1734
1735 @throws EntityException Failed to parse FPD xml file.
1736
1737 **/
1738 private void createTokenInDBFromFPD()
1739 throws EntityException {
1740 int index = 0;
1741 int index2 = 0;
1742 int pcdIndex = 0;
1743 List<PcdBuildDefinition.PcdData> pcdBuildDataArray = new ArrayList<PcdBuildDefinition.PcdData>();
1744 PcdBuildDefinition.PcdData pcdBuildData = null;
1745 Token token = null;
1746 List<ModuleInfo> modules = null;
1747 String primaryKey = null;
1748 String exceptionString = null;
1749 UsageInstance usageInstance = null;
1750 String primaryKey1 = null;
1751 String primaryKey2 = null;
1752 boolean isDuplicate = false;
1753 Token.PCD_TYPE pcdType = Token.PCD_TYPE.UNKNOWN;
1754 Token.DATUM_TYPE datumType = Token.DATUM_TYPE.UNKNOWN;
1755 long tokenNumber = 0;
1756 String moduleName = null;
1757 String datum = null;
1758 int maxDatumSize = 0;
1759 String[] tokenSpaceStrRet = null;
1760
1761 //
1762 // ----------------------------------------------
1763 // 1), Get all <ModuleSA> from FPD file.
1764 // ----------------------------------------------
1765 //
1766 modules = getComponentsFromFPD();
1767
1768 if (modules == null) {
1769 throw new EntityException("[FPD file error] No modules in FPD file, Please check whether there are elements in <FrameworkModules> in FPD file!");
1770 }
1771
1772 //
1773 // -------------------------------------------------------------------
1774 // 2), Loop all modules to process <PcdBuildDeclarations> for each module.
1775 // -------------------------------------------------------------------
1776 //
1777 for (index = 0; index < modules.size(); index ++) {
1778 isDuplicate = false;
1779 for (index2 = 0; index2 < index; index2 ++) {
1780 //
1781 // BUGBUG: For transition schema, we can *not* get module's version from
1782 // <ModuleSAs>, It is work around code.
1783 //
1784 primaryKey1 = UsageInstance.getPrimaryKey(modules.get(index).getModuleId().getModule().getName(),
1785 null,
1786 null,
1787 null,
1788 modules.get(index).getModuleId().getArch(),
1789 null);
1790 primaryKey2 = UsageInstance.getPrimaryKey(modules.get(index2).getModuleId().getModule().getName(),
1791 null,
1792 null,
1793 null,
1794 modules.get(index2).getModuleId().getArch(),
1795 null);
1796 if (primaryKey1.equalsIgnoreCase(primaryKey2)) {
1797 isDuplicate = true;
1798 break;
1799 }
1800 }
1801
1802 if (isDuplicate) {
1803 continue;
1804 }
1805
1806 //
1807 // It is legal for a module does not contains ANY pcd build definitions.
1808 //
1809 if (modules.get(index).getPcdBuildDef() == null) {
1810 continue;
1811 }
1812
1813 pcdBuildDataArray = modules.get(index).getPcdBuildDef().getPcdDataList();
1814
1815 moduleName = modules.get(index).getModuleId().getModule().getName();
1816
1817 //
1818 // ----------------------------------------------------------------------
1819 // 2.1), Loop all Pcd entry for a module and add it into memory database.
1820 // ----------------------------------------------------------------------
1821 //
1822 for (pcdIndex = 0; pcdIndex < pcdBuildDataArray.size(); pcdIndex ++) {
1823 pcdBuildData = pcdBuildDataArray.get(pcdIndex);
1824
1825 try {
1826 tokenSpaceStrRet = GlobalData.getGuidInfoFromCname(pcdBuildData.getTokenSpaceGuidCName());
1827 } catch ( Exception e ) {
1828 throw new EntityException ("Faile get Guid for token " + pcdBuildData.getCName() + ":" + e.getMessage());
1829 }
1830
1831 if (tokenSpaceStrRet == null) {
1832 throw new EntityException ("Fail to get Token space guid for token" + pcdBuildData.getCName());
1833 }
1834
1835 primaryKey = Token.getPrimaryKeyString(pcdBuildData.getCName(),
1836 translateSchemaStringToUUID(tokenSpaceStrRet[1]));
1837 pcdType = Token.getpcdTypeFromString(pcdBuildData.getItemType().toString());
1838 datumType = Token.getdatumTypeFromString(pcdBuildData.getDatumType().toString());
1839 tokenNumber = Long.decode(pcdBuildData.getToken().toString());
1840 if (pcdBuildData.getValue() != null) {
1841 datum = pcdBuildData.getValue().toString();
1842 } else {
1843 datum = null;
1844 }
1845 maxDatumSize = pcdBuildData.getMaxDatumSize();
1846
1847 if ((pcdType == Token.PCD_TYPE.FEATURE_FLAG) &&
1848 (datumType != Token.DATUM_TYPE.BOOLEAN)){
1849 exceptionString = String.format("[FPD file error] For PCD %s in module %s, the PCD type is FEATRUE_FLAG but "+
1850 "datum type of this PCD entry is not BOOLEAN!",
1851 pcdBuildData.getCName(),
1852 moduleName);
1853 throw new EntityException(exceptionString);
1854 }
1855
1856 //
1857 // -------------------------------------------------------------------------------------------
1858 // 2.1.1), Do some necessary checking work for FixedAtBuild, FeatureFlag and PatchableInModule
1859 // -------------------------------------------------------------------------------------------
1860 //
1861 if (!Token.isDynamic(pcdType)) {
1862 //
1863 // Value is required.
1864 //
1865 if (datum == null) {
1866 exceptionString = String.format("[FPD file error] There is no value for PCD entry %s in module %s!",
1867 pcdBuildData.getCName(),
1868 moduleName);
1869 throw new EntityException(exceptionString);
1870 }
1871
1872 //
1873 // Check whether the datum size is matched datum type.
1874 //
1875 if ((exceptionString = verifyDatum(pcdBuildData.getCName(),
1876 moduleName,
1877 datum,
1878 datumType,
1879 maxDatumSize)) != null) {
1880 throw new EntityException(exceptionString);
1881 }
1882 }
1883
1884 //
1885 // ---------------------------------------------------------------------------------
1886 // 2.1.2), Create token or update token information for current anaylized PCD data.
1887 // ---------------------------------------------------------------------------------
1888 //
1889 if (dbManager.isTokenInDatabase(primaryKey)) {
1890 //
1891 // If the token is already exist in database, do some necessary checking
1892 // and add a usage instance into this token in database
1893 //
1894 token = dbManager.getTokenByKey(primaryKey);
1895
1896 //
1897 // checking for DatumType, DatumType should be unique for one PCD used in different
1898 // modules.
1899 //
1900 if (token.datumType != datumType) {
1901 exceptionString = String.format("[FPD file error] The datum type of PCD entry %s is %s, which is different with %s defined in before!",
1902 pcdBuildData.getCName(),
1903 pcdBuildData.getDatumType().toString(),
1904 Token.getStringOfdatumType(token.datumType));
1905 throw new EntityException(exceptionString);
1906 }
1907
1908 //
1909 // Check token number is valid
1910 //
1911 if (tokenNumber != token.tokenNumber) {
1912 exceptionString = String.format("[FPD file error] The token number of PCD entry %s in module %s is different with same PCD entry in other modules!",
1913 pcdBuildData.getCName(),
1914 moduleName);
1915 throw new EntityException(exceptionString);
1916 }
1917
1918 //
1919 // For same PCD used in different modules, the PCD type should all be dynamic or non-dynamic.
1920 //
1921 if (token.isDynamicPCD != Token.isDynamic(pcdType)) {
1922 exceptionString = String.format("[FPD file error] For PCD entry %s in module %s, you define dynamic or non-dynamic PCD type which"+
1923 "is different with others module's",
1924 token.cName,
1925 moduleName);
1926 throw new EntityException(exceptionString);
1927 }
1928
1929 if (token.isDynamicPCD) {
1930 //
1931 // Check datum is equal the datum in dynamic information.
1932 // For dynamic PCD, you can do not write <Value> in sperated every <PcdBuildDefinition> in different <ModuleSA>,
1933 // But if you write, the <Value> must be same as the value in <DynamicPcdBuildDefinitions>.
1934 //
1935 if (!token.isSkuEnable() &&
1936 (token.getDefaultSku().type == DynamicTokenValue.VALUE_TYPE.DEFAULT_TYPE) &&
1937 (datum != null)) {
1938 if (!datum.equalsIgnoreCase(token.getDefaultSku().value)) {
1939 exceptionString = String.format("[FPD file error] For dynamic PCD %s in module %s, the datum in <ModuleSA> is "+
1940 "not equal to the datum in <DynamicPcdBuildDefinitions>, it is "+
1941 "illega! You could no set <Value> in <ModuleSA> for a dynamic PCD!",
1942 token.cName,
1943 moduleName);
1944 throw new EntityException(exceptionString);
1945 }
1946 }
1947
1948 if ((maxDatumSize != 0) &&
1949 (maxDatumSize != token.datumSize)){
1950 exceptionString = String.format("[FPD file error] For dynamic PCD %s in module %s, the max datum size is %d which "+
1951 "is different with <MaxDatumSize> %d defined in <DynamicPcdBuildDefinitions>!",
1952 token.cName,
1953 moduleName,
1954 maxDatumSize,
1955 token.datumSize);
1956 throw new EntityException(exceptionString);
1957 }
1958 }
1959
1960 } else {
1961 //
1962 // If the token is not in database, create a new token instance and add
1963 // a usage instance into this token in database.
1964 //
1965 try {
1966 tokenSpaceStrRet = GlobalData.getGuidInfoFromCname(pcdBuildData.getTokenSpaceGuidCName());
1967 } catch (Exception e) {
1968 throw new EntityException("Fail to get token space guid for token " + token.cName);
1969 }
1970
1971 if (tokenSpaceStrRet == null) {
1972 throw new EntityException("Fail to get token space guid for token " + token.cName);
1973 }
1974
1975 token = new Token(pcdBuildData.getCName(),
1976 translateSchemaStringToUUID(tokenSpaceStrRet[1]));
1977
1978 token.datumType = datumType;
1979 token.tokenNumber = tokenNumber;
1980 token.isDynamicPCD = Token.isDynamic(pcdType);
1981 token.datumSize = maxDatumSize;
1982
1983 if (token.isDynamicPCD) {
1984 //
1985 // For Dynamic and Dynamic Ex type, need find the dynamic information
1986 // in <DynamicPcdBuildDefinition> section in FPD file.
1987 //
1988 updateDynamicInformation(moduleName,
1989 token,
1990 datum,
1991 maxDatumSize);
1992 }
1993
1994 dbManager.addTokenToDatabase(primaryKey, token);
1995 }
1996
1997 //
1998 // -----------------------------------------------------------------------------------
1999 // 2.1.3), Add the PcdType in current module into this Pcd token's supported PCD type.
2000 // -----------------------------------------------------------------------------------
2001 //
2002 token.updateSupportPcdType(pcdType);
2003
2004 //
2005 // ------------------------------------------------
2006 // 2.1.4), Create an usage instance for this token.
2007 // ------------------------------------------------
2008 //
2009 usageInstance = new UsageInstance(token,
2010 moduleName,
2011 null,
2012 null,
2013 null,
2014 CommonDefinition.getModuleType(modules.get(index).getModuleType()),
2015 pcdType,
2016 modules.get(index).getModuleId().getArch(),
2017 null,
2018 datum,
2019 maxDatumSize);
2020 token.addUsageInstance(usageInstance);
2021 }
2022 }
2023 }
2024
2025 /**
2026 Verify the datum value according its datum size and datum type, this
2027 function maybe moved to FPD verification tools in future.
2028
2029 @param cName
2030 @param moduleName
2031 @param datum
2032 @param datumType
2033 @param maxDatumSize
2034
2035 @return String
2036 */
2037 /***/
2038 public String verifyDatum(String cName,
2039 String moduleName,
2040 String datum,
2041 Token.DATUM_TYPE datumType,
2042 int maxDatumSize) {
2043 String exceptionString = null;
2044 int value;
2045 BigInteger value64;
2046 String subStr;
2047 int index;
2048
2049 if (moduleName == null) {
2050 moduleName = "section <DynamicPcdBuildDefinitions>";
2051 } else {
2052 moduleName = "module " + moduleName;
2053 }
2054
2055 if (maxDatumSize == 0) {
2056 exceptionString = String.format("[FPD file error] You maybe miss <MaxDatumSize> for PCD %s in %s",
2057 cName,
2058 moduleName);
2059 return exceptionString;
2060 }
2061
2062 switch (datumType) {
2063 case UINT8:
2064 if (maxDatumSize != 1) {
2065 exceptionString = String.format("[FPD file error] The datum type of PCD data %s in %s "+
2066 "is UINT8, but datum size is %d, they are not matched!",
2067 cName,
2068 moduleName,
2069 maxDatumSize);
2070 return exceptionString;
2071 }
2072
2073 if (datum != null) {
2074 try {
2075 value = Integer.decode(datum);
2076 } catch (NumberFormatException nfeExp) {
2077 exceptionString = String.format("[FPD file error] The datum for PCD %s in %s is not valid "+
2078 "digital format of UINT8",
2079 cName,
2080 moduleName);
2081 return exceptionString;
2082 }
2083 if (value > 0xFF) {
2084 exceptionString = String.format("[FPD file error] The datum for PCD %s in %s is %s exceed"+
2085 " the max size of UINT8 - 0xFF",
2086 cName,
2087 moduleName,
2088 datum);
2089 return exceptionString;
2090 }
2091 }
2092 break;
2093 case UINT16:
2094 if (maxDatumSize != 2) {
2095 exceptionString = String.format("[FPD file error] The datum type of PCD data %s in %s "+
2096 "is UINT16, but datum size is %d, they are not matched!",
2097 cName,
2098 moduleName,
2099 maxDatumSize);
2100 return exceptionString;
2101 }
2102 if (datum != null) {
2103 try {
2104 value = Integer.decode(datum);
2105 } catch (NumberFormatException nfeExp) {
2106 exceptionString = String.format("[FPD file error] The datum for PCD %s in %s is "+
2107 "not valid digital of UINT16",
2108 cName,
2109 moduleName);
2110 return exceptionString;
2111 }
2112 if (value > 0xFFFF) {
2113 exceptionString = String.format("[FPD file error] The datum for PCD %s in %s is %s "+
2114 "which exceed the range of UINT16 - 0xFFFF",
2115 cName,
2116 moduleName,
2117 datum);
2118 return exceptionString;
2119 }
2120 }
2121 break;
2122 case UINT32:
2123 if (maxDatumSize != 4) {
2124 exceptionString = String.format("[FPD file error] The datum type of PCD data %s in %s "+
2125 "is UINT32, but datum size is %d, they are not matched!",
2126 cName,
2127 moduleName,
2128 maxDatumSize);
2129 return exceptionString;
2130 }
2131
2132 if (datum != null) {
2133 try {
2134 if (datum.length() > 2) {
2135 if ((datum.charAt(0) == '0') &&
2136 ((datum.charAt(1) == 'x') || (datum.charAt(1) == 'X'))){
2137 subStr = datum.substring(2, datum.length());
2138 value64 = new BigInteger(subStr, 16);
2139 } else {
2140 value64 = new BigInteger(datum);
2141 }
2142 } else {
2143 value64 = new BigInteger(datum);
2144 }
2145 } catch (NumberFormatException nfeExp) {
2146 exceptionString = String.format("[FPD file error] The datum for PCD %s in %s is not "+
2147 "valid digital of UINT32",
2148 cName,
2149 moduleName);
2150 return exceptionString;
2151 }
2152
2153 if (value64.bitLength() > 32) {
2154 exceptionString = String.format("[FPD file error] The datum for PCD %s in %s is %s which "+
2155 "exceed the range of UINT32 - 0xFFFFFFFF",
2156 cName,
2157 moduleName,
2158 datum);
2159 return exceptionString;
2160 }
2161 }
2162 break;
2163 case UINT64:
2164 if (maxDatumSize != 8) {
2165 exceptionString = String.format("[FPD file error] The datum type of PCD data %s in %s "+
2166 "is UINT64, but datum size is %d, they are not matched!",
2167 cName,
2168 moduleName,
2169 maxDatumSize);
2170 return exceptionString;
2171 }
2172
2173 if (datum != null) {
2174 try {
2175 if (datum.length() > 2) {
2176 if ((datum.charAt(0) == '0') &&
2177 ((datum.charAt(1) == 'x') || (datum.charAt(1) == 'X'))){
2178 subStr = datum.substring(2, datum.length());
2179 value64 = new BigInteger(subStr, 16);
2180 } else {
2181 value64 = new BigInteger(datum);
2182 }
2183 } else {
2184 value64 = new BigInteger(datum);
2185 }
2186 } catch (NumberFormatException nfeExp) {
2187 exceptionString = String.format("[FPD file error] The datum for PCD %s in %s is not valid"+
2188 " digital of UINT64",
2189 cName,
2190 moduleName);
2191 return exceptionString;
2192 }
2193
2194 if (value64.bitLength() > 64) {
2195 exceptionString = String.format("[FPD file error] The datum for PCD %s in %s is %s "+
2196 "exceed the range of UINT64 - 0xFFFFFFFFFFFFFFFF",
2197 cName,
2198 moduleName,
2199 datum);
2200 return exceptionString;
2201 }
2202 }
2203 break;
2204 case BOOLEAN:
2205 if (maxDatumSize != 1) {
2206 exceptionString = String.format("[FPD file error] The datum type of PCD data %s in %s "+
2207 "is BOOLEAN, but datum size is %d, they are not matched!",
2208 cName,
2209 moduleName,
2210 maxDatumSize);
2211 return exceptionString;
2212 }
2213
2214 if (datum != null) {
2215 if (!(datum.equalsIgnoreCase("TRUE") ||
2216 datum.equalsIgnoreCase("FALSE"))) {
2217 exceptionString = String.format("[FPD file error] The datum type of PCD data %s in %s "+
2218 "is BOOELAN, but value is not 'true'/'TRUE' or 'FALSE'/'false'",
2219 cName,
2220 moduleName);
2221 return exceptionString;
2222 }
2223
2224 }
2225 break;
2226 case POINTER:
2227 if (datum == null) {
2228 break;
2229 }
2230
2231 char ch = datum.charAt(0);
2232 int start, end;
2233 String strValue;
2234 //
2235 // For void* type PCD, only three datum is support:
2236 // 1) Unicode: string with start char is "L"
2237 // 2) Ansci: String start char is ""
2238 // 3) byte array: String start char "{"
2239 //
2240 if (ch == 'L') {
2241 start = datum.indexOf('\"');
2242 end = datum.lastIndexOf('\"');
2243 if ((start > end) ||
2244 (end > datum.length())||
2245 ((start == end) && (datum.length() > 0))) {
2246 exceptionString = String.format("[FPD file error] The datum type of PCD %s in %s is VOID* and datum is "+
2247 "a UNICODE string because start with L\", but format maybe"+
2248 "is not right, correct UNICODE string is L\"...\"!",
2249 cName,
2250 moduleName);
2251 return exceptionString;
2252 }
2253
2254 strValue = datum.substring(start + 1, end);
2255 if ((strValue.length() * 2) > maxDatumSize) {
2256 exceptionString = String.format("[FPD file error] The datum type of PCD %s in %s is VOID*, and datum is "+
2257 "a UNICODE string, but the datum size is %d exceed to <MaxDatumSize> : %d",
2258 cName,
2259 moduleName,
2260 strValue.length() * 2,
2261 maxDatumSize);
2262 return exceptionString;
2263 }
2264 } else if (ch == '\"'){
2265 start = datum.indexOf('\"');
2266 end = datum.lastIndexOf('\"');
2267 if ((start > end) ||
2268 (end > datum.length())||
2269 ((start == end) && (datum.length() > 0))) {
2270 exceptionString = String.format("[FPD file error] The datum type of PCD %s in %s is VOID* and datum is "+
2271 "a ANSCII string because start with \", but format maybe"+
2272 "is not right, correct ANSIC string is \"...\"!",
2273 cName,
2274 moduleName);
2275 return exceptionString;
2276 }
2277 strValue = datum.substring(start + 1, end);
2278 if ((strValue.length()) > maxDatumSize) {
2279 exceptionString = String.format("[FPD file error] The datum type of PCD %s in %s is VOID*, and datum is "+
2280 "a ANSCI string, but the datum size is %d which exceed to <MaxDatumSize> : %d",
2281 cName,
2282 moduleName,
2283 strValue.length(),
2284 maxDatumSize);
2285 return exceptionString;
2286 }
2287 } else if (ch =='{') {
2288 String[] strValueArray;
2289
2290 start = datum.indexOf('{');
2291 end = datum.lastIndexOf('}');
2292 strValue = datum.substring(start + 1, end);
2293 strValue = strValue.trim();
2294 if (strValue.length() == 0) {
2295 break;
2296 }
2297 strValueArray = strValue.split(",");
2298 for (index = 0; index < strValueArray.length; index ++) {
2299 try{
2300 value = Integer.decode(strValueArray[index].trim());
2301 } catch (NumberFormatException nfeEx) {
2302 exceptionString = String.format("[FPD file error] The datum type of PCD %s in %s is VOID*, and "+
2303 "it is byte array in fact. For every byte in array should be a valid"+
2304 "byte digital, but element %s is not a valid byte digital!",
2305 cName,
2306 moduleName,
2307 strValueArray[index]);
2308 return exceptionString;
2309 }
2310 if (value > 0xFF) {
2311 exceptionString = String.format("[FPD file error] The datum type of PCD %s in %s is VOID*, "+
2312 "it is byte array in fact. But the element of %s exceed the byte range",
2313 cName,
2314 moduleName,
2315 strValueArray[index]);
2316 return exceptionString;
2317 }
2318 }
2319
2320 if (strValueArray.length > maxDatumSize) {
2321 exceptionString = String.format("[FPD file error] The datum type of PCD %s in %s is VOID*, and datum is byte"+
2322 "array, but the number of bytes is %d which exceed to <MaxDatumSzie> : %d!",
2323 cName,
2324 moduleName,
2325 strValueArray.length,
2326 maxDatumSize);
2327 return exceptionString;
2328 }
2329 } else {
2330 exceptionString = String.format("[FPD file error] The datum type of PCD %s in %s is VOID*. For VOID* type, you have three format choise:\n "+
2331 "1) UNICODE string: like L\"xxxx\";\r\n"+
2332 "2) ANSIC string: like \"xxx\";\r\n"+
2333 "3) Byte array: like {0x2, 0x45, 0x23}\r\n"+
2334 "But the datum in seems does not following above format!",
2335 cName,
2336 moduleName);
2337 return exceptionString;
2338 }
2339 break;
2340 default:
2341 exceptionString = String.format("[FPD file error] For PCD entry %s in %s, datum type is unknown, it should be one of "+
2342 "UINT8, UINT16, UINT32, UINT64, VOID*, BOOLEAN",
2343 cName,
2344 moduleName);
2345 return exceptionString;
2346 }
2347 return null;
2348 }
2349
2350 /**
2351 Get dynamic information for a dynamic PCD from <DynamicPcdBuildDefinition> seciton in FPD file.
2352
2353 This function should be implemented in GlobalData in future.
2354
2355 @param token The token instance which has hold module's PCD information
2356 @param moduleName The name of module who will use this Dynamic PCD.
2357
2358 @return DynamicPcdBuildDefinitions.PcdBuildData
2359 */
2360 /***/
2361 private DynamicPcdBuildDefinitions.PcdBuildData getDynamicInfoFromFPD(Token token,
2362 String moduleName)
2363 throws EntityException {
2364 int index = 0;
2365 String exceptionString = null;
2366 String dynamicPrimaryKey = null;
2367 DynamicPcdBuildDefinitions dynamicPcdBuildDefinitions = null;
2368 List<DynamicPcdBuildDefinitions.PcdBuildData> dynamicPcdBuildDataArray = null;
2369 String[] tokenSpaceStrRet = null;
2370
2371 //
2372 // If FPD document is not be opened, open and initialize it.
2373 //
2374 if (fpdDocInstance == null) {
2375 try {
2376 fpdDocInstance = (PlatformSurfaceAreaDocument)XmlObject.Factory.parse(new File(fpdFilePath));
2377 } catch(IOException ioE) {
2378 throw new EntityException("File IO error for xml file:" + fpdFilePath + "\n" + ioE.getMessage());
2379 } catch(XmlException xmlE) {
2380 throw new EntityException("Can't parse the FPD xml fle:" + fpdFilePath + "\n" + xmlE.getMessage());
2381 }
2382 }
2383
2384 dynamicPcdBuildDefinitions = fpdDocInstance.getPlatformSurfaceArea().getDynamicPcdBuildDefinitions();
2385 if (dynamicPcdBuildDefinitions == null) {
2386 exceptionString = String.format("[FPD file error] There are no <PcdDynamicBuildDescriptions> in FPD file but contains Dynamic type "+
2387 "PCD entry %s in module %s!",
2388 token.cName,
2389 moduleName);
2390 throw new EntityException(exceptionString);
2391 }
2392
2393 dynamicPcdBuildDataArray = dynamicPcdBuildDefinitions.getPcdBuildDataList();
2394 for (index = 0; index < dynamicPcdBuildDataArray.size(); index ++) {
2395 //String tokenSpaceGuidString = GlobalData.getGuidInfoFromCname(dynamicPcdBuildDataArray.get(index).getTokenSpaceGuidCName())[1];
2396 String tokenSpaceGuidString = null;
2397 try {
2398 tokenSpaceStrRet = GlobalData.getGuidInfoFromCname(dynamicPcdBuildDataArray.get(index).getTokenSpaceGuidCName());
2399 } catch (Exception e) {
2400 throw new EntityException ("Fail to get token space guid for token " + dynamicPcdBuildDataArray.get(index).getCName());
2401 }
2402
2403 if (tokenSpaceStrRet == null) {
2404 throw new EntityException ("Fail to get token space guid for token " + dynamicPcdBuildDataArray.get(index).getCName());
2405 }
2406
2407 dynamicPrimaryKey = Token.getPrimaryKeyString(dynamicPcdBuildDataArray.get(index).getCName(),
2408 translateSchemaStringToUUID(tokenSpaceStrRet[1]));
2409 if (dynamicPrimaryKey.equalsIgnoreCase(token.getPrimaryKeyString())) {
2410 return dynamicPcdBuildDataArray.get(index);
2411 }
2412 }
2413
2414 return null;
2415 }
2416
2417 /**
2418 Update dynamic information for PCD entry.
2419
2420 Dynamic information is retrieved from <PcdDynamicBuildDeclarations> in
2421 FPD file.
2422
2423 @param moduleName The name of the module who use this PCD
2424 @param token The token instance
2425 @param datum The <datum> in module's PCD information
2426 @param maxDatumSize The <maxDatumSize> in module's PCD information
2427
2428 @return Token
2429 */
2430 private Token updateDynamicInformation(String moduleName,
2431 Token token,
2432 String datum,
2433 int maxDatumSize)
2434 throws EntityException {
2435 int index = 0;
2436 int offset;
2437 String exceptionString = null;
2438 DynamicTokenValue dynamicValue;
2439 SkuInstance skuInstance = null;
2440 String temp;
2441 boolean hasSkuId0 = false;
2442 Token.PCD_TYPE pcdType = Token.PCD_TYPE.UNKNOWN;
2443 long tokenNumber = 0;
2444 String hiiDefaultValue = null;
2445 String[] variableGuidString = null;
2446
2447 List<DynamicPcdBuildDefinitions.PcdBuildData.SkuInfo> skuInfoList = null;
2448 DynamicPcdBuildDefinitions.PcdBuildData dynamicInfo = null;
2449
2450 dynamicInfo = getDynamicInfoFromFPD(token, moduleName);
2451 if (dynamicInfo == null) {
2452 exceptionString = String.format("[FPD file error] For Dynamic PCD %s used by module %s, "+
2453 "there is no dynamic information in <DynamicPcdBuildDefinitions> "+
2454 "in FPD file, but it is required!",
2455 token.cName,
2456 moduleName);
2457 throw new EntityException(exceptionString);
2458 }
2459
2460 token.datumSize = dynamicInfo.getMaxDatumSize();
2461
2462 exceptionString = verifyDatum(token.cName,
2463 moduleName,
2464 null,
2465 token.datumType,
2466 token.datumSize);
2467 if (exceptionString != null) {
2468 throw new EntityException(exceptionString);
2469 }
2470
2471 if ((maxDatumSize != 0) &&
2472 (maxDatumSize != token.datumSize)) {
2473 exceptionString = String.format("FPD file error] For dynamic PCD %s, the datum size in module %s is %d, but "+
2474 "the datum size in <DynamicPcdBuildDefinitions> is %d, they are not match!",
2475 token.cName,
2476 moduleName,
2477 maxDatumSize,
2478 dynamicInfo.getMaxDatumSize());
2479 throw new EntityException(exceptionString);
2480 }
2481 tokenNumber = Long.decode(dynamicInfo.getToken().toString());
2482 if (tokenNumber != token.tokenNumber) {
2483 exceptionString = String.format("[FPD file error] For dynamic PCD %s, the token number in module %s is 0x%x, but"+
2484 "in <DynamicPcdBuildDefinictions>, the token number is 0x%x, they are not match!",
2485 token.cName,
2486 moduleName,
2487 token.tokenNumber,
2488 tokenNumber);
2489 throw new EntityException(exceptionString);
2490 }
2491
2492 pcdType = Token.getpcdTypeFromString(dynamicInfo.getItemType().toString());
2493 if (pcdType == Token.PCD_TYPE.DYNAMIC_EX) {
2494 token.dynamicExTokenNumber = tokenNumber;
2495 }
2496
2497 skuInfoList = dynamicInfo.getSkuInfoList();
2498
2499 //
2500 // Loop all sku data
2501 //
2502 for (index = 0; index < skuInfoList.size(); index ++) {
2503 skuInstance = new SkuInstance();
2504 //
2505 // Although SkuId in schema is BigInteger, but in fact, sku id is 32 bit value.
2506 //
2507 temp = skuInfoList.get(index).getSkuId().toString();
2508 skuInstance.id = Integer.decode(temp);
2509 if (skuInstance.id == 0) {
2510 hasSkuId0 = true;
2511 }
2512 //
2513 // Judge whether is DefaultGroup at first, because most case is DefautlGroup.
2514 //
2515 if (skuInfoList.get(index).getValue() != null) {
2516 skuInstance.value.setValue(skuInfoList.get(index).getValue().toString());
2517 if ((exceptionString = verifyDatum(token.cName,
2518 null,
2519 skuInfoList.get(index).getValue().toString(),
2520 token.datumType,
2521 token.datumSize)) != null) {
2522 throw new EntityException(exceptionString);
2523 }
2524
2525 token.skuData.add(skuInstance);
2526
2527 //
2528 // Judege wether is same of datum between module's information
2529 // and dynamic information.
2530 //
2531 if (datum != null) {
2532 if ((skuInstance.id == 0) &&
2533 !datum.toString().equalsIgnoreCase(skuInfoList.get(index).getValue().toString())) {
2534 exceptionString = "[FPD file error] For dynamic PCD " + token.cName + ", the value in module " + moduleName + " is " + datum.toString() + " but the "+
2535 "value of sku 0 data in <DynamicPcdBuildDefinition> is " + skuInstance.value.value + ". They are must be same!"+
2536 " or you could not define value for a dynamic PCD in every <ModuleSA>!";
2537 throw new EntityException(exceptionString);
2538 }
2539 }
2540 continue;
2541 }
2542
2543 //
2544 // Judge whether is HII group case.
2545 //
2546 if (skuInfoList.get(index).getVariableName() != null) {
2547 exceptionString = null;
2548 if (skuInfoList.get(index).getVariableGuid() == null) {
2549 exceptionString = String.format("[FPD file error] For dynamic PCD %s in <DynamicPcdBuildDefinitions> section in FPD "+
2550 "file, who use HII, but there is no <VariableGuid> defined for Sku %d data!",
2551 token.cName,
2552 index);
2553 if (exceptionString != null) {
2554 throw new EntityException(exceptionString);
2555 }
2556 }
2557
2558 if (skuInfoList.get(index).getVariableOffset() == null) {
2559 exceptionString = String.format("[FPD file error] For dynamic PCD %s in <DynamicPcdBuildDefinitions> section in FPD "+
2560 "file, who use HII, but there is no <VariableOffset> defined for Sku %d data!",
2561 token.cName,
2562 index);
2563 if (exceptionString != null) {
2564 throw new EntityException(exceptionString);
2565 }
2566 }
2567
2568 if (skuInfoList.get(index).getHiiDefaultValue() == null) {
2569 exceptionString = String.format("[FPD file error] For dynamic PCD %s in <DynamicPcdBuildDefinitions> section in FPD "+
2570 "file, who use HII, but there is no <HiiDefaultValue> defined for Sku %d data!",
2571 token.cName,
2572 index);
2573 if (exceptionString != null) {
2574 throw new EntityException(exceptionString);
2575 }
2576 }
2577
2578 if (skuInfoList.get(index).getHiiDefaultValue() != null) {
2579 hiiDefaultValue = skuInfoList.get(index).getHiiDefaultValue().toString();
2580 } else {
2581 hiiDefaultValue = null;
2582 }
2583
2584 if ((exceptionString = verifyDatum(token.cName,
2585 null,
2586 hiiDefaultValue,
2587 token.datumType,
2588 token.datumSize)) != null) {
2589 throw new EntityException(exceptionString);
2590 }
2591
2592 offset = Integer.decode(skuInfoList.get(index).getVariableOffset());
2593 if (offset > 0xFFFF) {
2594 throw new EntityException(String.format("[FPD file error] For dynamic PCD %s , the variable offset defined in sku %d data "+
2595 "exceed 64K, it is not allowed!",
2596 token.cName,
2597 index));
2598 }
2599
2600 //
2601 // Get variable guid string according to the name of guid which will be mapped into a GUID in SPD file.
2602 //
2603 variableGuidString = GlobalData.getGuidInfoFromCname(skuInfoList.get(index).getVariableGuid().toString());
2604 if (variableGuidString == null) {
2605 throw new EntityException(String.format("[GUID Error] For dynamic PCD %s, the variable guid %s can be found in all SPD file!",
2606 token.cName,
2607 skuInfoList.get(index).getVariableGuid().toString()));
2608 }
2609 String variableStr = skuInfoList.get(index).getVariableName();
2610 Pattern pattern = Pattern.compile("0x([a-fA-F0-9]){4}");
2611 Matcher matcher = pattern.matcher(variableStr);
2612 List<String> varNameList = new ArrayList<String>();
2613 while (matcher.find()){
2614 String str = variableStr.substring(matcher.start(),matcher.end());
2615 varNameList.add(str);
2616 }
2617
2618 skuInstance.value.setHiiData(varNameList,
2619 translateSchemaStringToUUID(variableGuidString[1]),
2620 skuInfoList.get(index).getVariableOffset(),
2621 skuInfoList.get(index).getHiiDefaultValue().toString());
2622 token.skuData.add(skuInstance);
2623 continue;
2624 }
2625
2626 if (skuInfoList.get(index).getVpdOffset() != null) {
2627 skuInstance.value.setVpdData(skuInfoList.get(index).getVpdOffset());
2628 token.skuData.add(skuInstance);
2629 continue;
2630 }
2631
2632 exceptionString = String.format("[FPD file error] For dynamic PCD %s, the dynamic info must "+
2633 "be one of 'DefaultGroup', 'HIIGroup', 'VpdGroup'.",
2634 token.cName);
2635 throw new EntityException(exceptionString);
2636 }
2637
2638 if (!hasSkuId0) {
2639 exceptionString = String.format("[FPD file error] For dynamic PCD %s in <DynamicPcdBuildDefinitions>, there are "+
2640 "no sku id = 0 data, which is required for every dynamic PCD",
2641 token.cName);
2642 throw new EntityException(exceptionString);
2643 }
2644
2645 return token;
2646 }
2647
2648 /**
2649 Translate the schema string to UUID instance.
2650
2651 In schema, the string of UUID is defined as following two types string:
2652 1) GuidArrayType: pattern = 0x[a-fA-F0-9]{1,8},( )*0x[a-fA-F0-9]{1,4},(
2653 )*0x[a-fA-F0-9]{1,4}(,( )*\{)?(,?( )*0x[a-fA-F0-9]{1,2}){8}( )*(\})?
2654
2655 2) GuidNamingConvention: pattern =
2656 [a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}
2657
2658 This function will convert string and create uuid instance.
2659
2660 @param uuidString UUID string in XML file
2661
2662 @return UUID UUID instance
2663 **/
2664 private UUID translateSchemaStringToUUID(String uuidString)
2665 throws EntityException {
2666 String temp;
2667 String[] splitStringArray;
2668 int index;
2669 int chIndex;
2670 int chLen;
2671
2672 if (uuidString == null) {
2673 return null;
2674 }
2675
2676 if (uuidString.length() == 0) {
2677 return null;