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