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