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