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