]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/GenBuild/org/tianocore/build/pcd/action/CollectPCDAction.java
Fix some bugs in PCD tools:
[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
24import java.util.ArrayList;\r
99d2c3c4 25import java.util.Collections;\r
26import java.util.Comparator;\r
878ddf1f 27import java.util.HashMap;\r
28import java.util.List;\r
29import java.util.Map;\r
30import java.util.UUID;\r
31\r
32import org.apache.xmlbeans.XmlException;\r
33import org.apache.xmlbeans.XmlObject;\r
6ff7a41c 34import org.tianocore.DynamicPcdBuildDefinitionsDocument;\r
35import org.tianocore.DynamicPcdBuildDefinitionsDocument.DynamicPcdBuildDefinitions;\r
36import org.tianocore.DynamicPcdBuildDefinitionsDocument.DynamicPcdBuildDefinitions.PcdBuildData.SkuInfo;\r
37import org.tianocore.DynamicPcdBuildDefinitionsDocument.DynamicPcdBuildDefinitions.PcdBuildData;\r
8840ad58 38import org.tianocore.FrameworkModulesDocument;\r
878ddf1f 39import org.tianocore.FrameworkPlatformDescriptionDocument;\r
8840ad58 40import org.tianocore.FrameworkPlatformDescriptionDocument.FrameworkPlatformDescription;\r
878ddf1f 41import org.tianocore.ModuleSADocument;\r
8840ad58 42import org.tianocore.ModuleSADocument.ModuleSA;\r
878ddf1f 43import org.tianocore.PackageSurfaceAreaDocument;\r
6ff7a41c 44import org.tianocore.PcdBuildDefinitionDocument.PcdBuildDefinition;\r
878ddf1f 45import org.tianocore.build.global.GlobalData;\r
46import org.tianocore.build.global.SurfaceAreaQuery;\r
47import org.tianocore.build.pcd.action.ActionMessage;\r
6ff7a41c 48import org.tianocore.build.pcd.entity.DynamicTokenValue;\r
878ddf1f 49import org.tianocore.build.pcd.entity.MemoryDatabaseManager;\r
50import org.tianocore.build.pcd.entity.SkuInstance;\r
51import org.tianocore.build.pcd.entity.Token;\r
52import org.tianocore.build.pcd.entity.UsageInstance;\r
53import org.tianocore.build.pcd.exception.EntityException;\r
54\r
99d2c3c4 55class StringTable {\r
56 private ArrayList<String> al; \r
32648c62 57 private ArrayList<String> alComments;\r
99d2c3c4 58 private String phase;\r
59 int len; \r
32648c62 60 int bodyStart;\r
61 int bodyLineNum;\r
99d2c3c4 62\r
63 public StringTable (String phase) {\r
64 this.phase = phase;\r
65 al = new ArrayList<String>();\r
32648c62 66 alComments = new ArrayList<String>();\r
99d2c3c4 67 len = 0;\r
32648c62 68 bodyStart = 0;\r
69 bodyLineNum = 0;\r
99d2c3c4 70 }\r
71\r
72 public String getSizeMacro () {\r
73 return String.format(PcdDatabase.StringTableSizeMacro, phase, getSize());\r
74 }\r
75\r
76 private int getSize () {\r
32648c62 77 //\r
78 // We have at least one Unicode Character in the table.\r
79 //\r
99d2c3c4 80 return len == 0 ? 1 : len;\r
81 }\r
82\r
32648c62 83 public int getTableLen () {\r
84 return al.size() == 0 ? 1 : al.size();\r
85 }\r
99d2c3c4 86\r
87 public String getExistanceMacro () {\r
88 return String.format(PcdDatabase.StringTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");\r
89 }\r
90\r
91 public String getTypeDeclaration () {\r
92\r
32648c62 93 String output;\r
99d2c3c4 94\r
32648c62 95 final String stringTable = "StringTable";\r
96 final String tab = "\t";\r
97 final String newLine = ";\r\n";\r
99d2c3c4 98\r
32648c62 99 output = "/* StringTable */\r\n";\r
99d2c3c4 100\r
32648c62 101 if (al.size() == 0) {\r
102 output += tab + String.format("UINT16 %s[1] /* StringTable is Empty */", stringTable) + newLine;\r
103 }\r
99d2c3c4 104\r
32648c62 105 for (int i = 0; i < al.size(); i++) {\r
106 String str = al.get(i);\r
99d2c3c4 107\r
32648c62 108 if (i == 0) {\r
109 //\r
110 // StringTable is a well-known name in the PCD DXE driver\r
111 //\r
112 output += tab + String.format("UINT16 %s[%d] /* %s */", stringTable, str.length() + 1, alComments.get(i)) + newLine;\r
113 } else {\r
114 output += tab + String.format("UINT16 %s_%d[%d] /* %s */", stringTable, i, str.length() + 1, alComments.get(i)) + newLine;\r
115 }\r
116 }\r
99d2c3c4 117\r
32648c62 118 return output;\r
99d2c3c4 119\r
120 }\r
121\r
122 public ArrayList<String> getInstantiation () {\r
32648c62 123 ArrayList<String> output = new ArrayList<String>();\r
99d2c3c4 124\r
32648c62 125 output.add("/* StringTable */"); \r
99d2c3c4 126\r
32648c62 127 if (al.size() == 0) {\r
128 output.add("{ 0 }");\r
129 } else {\r
130 String str;\r
99d2c3c4 131\r
32648c62 132 for (int i = 0; i < al.size(); i++) {\r
133 str = String.format("L\"%s\" /* %s */", al.get(i), alComments.get(i));\r
134 if (i != al.size() - 1) {\r
135 str += ",";\r
136 }\r
137 output.add(str);\r
138 }\r
139 }\r
99d2c3c4 140\r
141 return output;\r
142 }\r
143\r
144 public int add (String str, Token token) {\r
145 int i;\r
146\r
147 i = len;\r
148 //\r
149 // Include the NULL character at the end of String\r
150 //\r
151 len += str.length() + 1; \r
152 al.add(str);\r
32648c62 153 alComments.add(token.getPrimaryKeyString());\r
99d2c3c4 154\r
155 return i;\r
156 }\r
157}\r
158\r
159class SizeTable {\r
160 private ArrayList<Integer> al;\r
32648c62 161 private ArrayList<String> alComments;\r
99d2c3c4 162 private String phase;\r
163 private int len;\r
32648c62 164 private int bodyStart;\r
165 private int bodyLineNum;\r
99d2c3c4 166\r
167 public SizeTable (String phase) {\r
168 this.phase = phase;\r
169 al = new ArrayList<Integer>();\r
32648c62 170 alComments = new ArrayList<String>();\r
99d2c3c4 171 len = 0;\r
32648c62 172 bodyStart = 0;\r
173 bodyLineNum = 0;\r
99d2c3c4 174 }\r
175\r
176 public String getTypeDeclaration () {\r
177 return String.format(PcdDatabase.SizeTableDeclaration, phase);\r
178 }\r
179\r
180 public ArrayList<String> getInstantiation () {\r
32648c62 181 ArrayList<String> Output = new ArrayList<String>();\r
99d2c3c4 182\r
183 Output.add("/* SizeTable */");\r
184 Output.add("{");\r
32648c62 185 bodyStart = 2;\r
186\r
187 if (al.size() == 0) {\r
188 Output.add("0");\r
189 } else {\r
190 for (int index = 0; index < al.size(); index++) {\r
191 Integer n = al.get(index);\r
192 String str = n.toString();\r
193\r
194 if (index != (al.size() - 1)) {\r
195 str += ",";\r
196 }\r
197\r
198 str += " /* " + alComments.get(index) + " */"; \r
199 Output.add(str);\r
200 bodyLineNum++;\r
201 \r
202 }\r
203 }\r
204 Output.add("}");\r
99d2c3c4 205\r
206 return Output;\r
207 }\r
208\r
32648c62 209 public int getBodyStart() {\r
210 return bodyStart;\r
211 }\r
99d2c3c4 212\r
32648c62 213 public int getBodyLineNum () {\r
214 return bodyLineNum;\r
215 }\r
99d2c3c4 216\r
217 public int add (Token token) {\r
218 int index = len;\r
219\r
220 len++; \r
221 al.add(token.datumSize);\r
32648c62 222 alComments.add(token.getPrimaryKeyString());\r
99d2c3c4 223\r
224 return index;\r
225 }\r
4acf8ce7 226 \r
32648c62 227 private int getDatumSize(Token token) {\r
228 /*\r
229 switch (token.datumType) {\r
230 case Token.DATUM_TYPE.UINT8:\r
231 return 1;\r
232 default:\r
233 return 0;\r
234 }\r
235 */\r
236 return 0;\r
237 }\r
99d2c3c4 238\r
32648c62 239 public int getTableLen () {\r
240 return al.size() == 0 ? 1 : al.size();\r
241 }\r
99d2c3c4 242\r
243}\r
244\r
245class GuidTable {\r
246 private ArrayList<UUID> al;\r
32648c62 247 private ArrayList<String> alComments;\r
99d2c3c4 248 private String phase;\r
249 private int len;\r
32648c62 250 private int bodyStart;\r
251 private int bodyLineNum;\r
99d2c3c4 252\r
253 public GuidTable (String phase) {\r
254 this.phase = phase;\r
255 al = new ArrayList<UUID>();\r
32648c62 256 alComments = new ArrayList<String>();\r
99d2c3c4 257 len = 0;\r
32648c62 258 bodyStart = 0;\r
259 bodyLineNum = 0;\r
99d2c3c4 260 }\r
261\r
262 public String getSizeMacro () {\r
263 return String.format(PcdDatabase.GuidTableSizeMacro, phase, getSize());\r
264 }\r
265\r
266 private int getSize () {\r
267 return (al.size() == 0)? 1 : al.size();\r
268 }\r
269\r
270 public String getExistanceMacro () {\r
271 return String.format(PcdDatabase.GuidTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");\r
272 }\r
273\r
274 public String getTypeDeclaration () {\r
275 return String.format(PcdDatabase.GuidTableDeclaration, phase);\r
276 }\r
277\r
32648c62 278 private String getUuidCString (UUID uuid) {\r
279 String[] guidStrArray;\r
280\r
281 guidStrArray =(uuid.toString()).split("-");\r
282\r
283 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
284 guidStrArray[0],\r
285 guidStrArray[1],\r
286 guidStrArray[2],\r
287 (guidStrArray[3].substring(0, 2)),\r
288 (guidStrArray[3].substring(2, 4)),\r
289 (guidStrArray[4].substring(0, 2)),\r
290 (guidStrArray[4].substring(2, 4)),\r
291 (guidStrArray[4].substring(4, 6)),\r
292 (guidStrArray[4].substring(6, 8)),\r
293 (guidStrArray[4].substring(8, 10)),\r
294 (guidStrArray[4].substring(10, 12))\r
295 );\r
296 }\r
99d2c3c4 297\r
298 public ArrayList<String> getInstantiation () {\r
32648c62 299 ArrayList<String> Output = new ArrayList<String>();\r
99d2c3c4 300\r
301 Output.add("/* GuidTable */");\r
302 Output.add("{");\r
32648c62 303 bodyStart = 2;\r
99d2c3c4 304\r
32648c62 305 if (al.size() == 0) {\r
306 Output.add(getUuidCString(new UUID(0, 0)));\r
307 }\r
99d2c3c4 308 \r
309 for (Object u : al) {\r
310 UUID uuid = (UUID)u;\r
32648c62 311 String str = getUuidCString(uuid);\r
99d2c3c4 312\r
32648c62 313 if (al.indexOf(u) != (al.size() - 1)) {\r
314 str += ",";\r
315 }\r
99d2c3c4 316 Output.add(str);\r
32648c62 317 bodyLineNum++;\r
99d2c3c4 318\r
319 }\r
32648c62 320 Output.add("}");\r
99d2c3c4 321\r
322 return Output;\r
323 }\r
324\r
32648c62 325 public int getBodyStart() {\r
326 return bodyStart;\r
327 }\r
99d2c3c4 328\r
32648c62 329 public int getBodyLineNum () {\r
330 return bodyLineNum;\r
331 }\r
99d2c3c4 332\r
333 public int add (UUID uuid, String name) {\r
334 int index = len;\r
335 //\r
336 // Include the NULL character at the end of String\r
337 //\r
338 len++; \r
339 al.add(uuid);\r
340\r
341 return index;\r
342 }\r
343\r
32648c62 344 public int getTableLen () {\r
345 return al.size() == 0 ? 0 : al.size();\r
346 }\r
99d2c3c4 347\r
348}\r
349\r
350class SkuIdTable {\r
351 private ArrayList<Integer[]> al;\r
32648c62 352 private ArrayList<String> alComment;\r
99d2c3c4 353 private String phase;\r
354 private int len;\r
32648c62 355 private int bodyStart;\r
356 private int bodyLineNum;\r
99d2c3c4 357\r
358 public SkuIdTable (String phase) {\r
359 this.phase = phase;\r
360 al = new ArrayList<Integer[]>();\r
32648c62 361 alComment = new ArrayList<String>();\r
362 bodyStart = 0;\r
363 bodyLineNum = 0;\r
99d2c3c4 364 len = 0;\r
365 }\r
366\r
367 public String getSizeMacro () {\r
368 return String.format(PcdDatabase.SkuIdTableSizeMacro, phase, getSize());\r
369 }\r
370\r
371 private int getSize () {\r
372 return (al.size() == 0)? 1 : al.size();\r
373 }\r
374\r
375 public String getExistanceMacro () {\r
376 return String.format(PcdDatabase.SkuTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");\r
377 }\r
378\r
379 public String getTypeDeclaration () {\r
380 return String.format(PcdDatabase.SkuIdTableDeclaration, phase);\r
381 }\r
382\r
383 public ArrayList<String> getInstantiation () {\r
32648c62 384 ArrayList<String> Output = new ArrayList<String> ();\r
99d2c3c4 385\r
386 Output.add("/* SkuIdTable */");\r
387 Output.add("{");\r
32648c62 388 bodyStart = 2;\r
99d2c3c4 389\r
32648c62 390 if (al.size() == 0) {\r
391 Output.add("0");\r
392 }\r
99d2c3c4 393 \r
394 for (int index = 0; index < al.size(); index++) {\r
32648c62 395 String str;\r
99d2c3c4 396\r
32648c62 397 str = "/* " + alComment.get(index) + "*/ ";\r
398 str += "/* MaxSku */ ";\r
99d2c3c4 399\r
400\r
32648c62 401 Integer[] ia = al.get(index);\r
99d2c3c4 402\r
32648c62 403 str += ia[0].toString() + ", ";\r
404 for (int index2 = 1; index2 < ia.length; index2++) {\r
405 str += ia[index2].toString();\r
406 if (index != al.size() - 1) {\r
407 str += ", ";\r
408 }\r
409 }\r
99d2c3c4 410\r
411 Output.add(str);\r
32648c62 412 bodyLineNum++;\r
99d2c3c4 413\r
414 }\r
415\r
32648c62 416 Output.add("}");\r
99d2c3c4 417\r
418 return Output;\r
419 }\r
420\r
421 public int add (Token token) {\r
422\r
32648c62 423 int index;\r
99d2c3c4 424\r
6ff7a41c 425 Integer [] skuIds = new Integer[token.skuData.size() + 1];\r
426 skuIds[0] = new Integer(token.skuData.size());\r
32648c62 427 for (index = 1; index < skuIds.length; index++) {\r
428 skuIds[index] = new Integer(token.skuData.get(index - 1).id);\r
429 }\r
99d2c3c4 430\r
431 index = len;\r
432\r
433 len += skuIds.length; \r
434 al.add(skuIds);\r
32648c62 435 alComment.add(token.getPrimaryKeyString());\r
99d2c3c4 436\r
437 return index;\r
438 }\r
439\r
32648c62 440 public int getTableLen () {\r
441 return al.size() == 0 ? 1 : al.size();\r
442 }\r
99d2c3c4 443\r
444}\r
445\r
446class LocalTokenNumberTable {\r
447 private ArrayList<String> al;\r
32648c62 448 private ArrayList<String> alComment;\r
99d2c3c4 449 private String phase;\r
450 private int len;\r
99d2c3c4 451\r
452 public LocalTokenNumberTable (String phase) {\r
453 this.phase = phase;\r
454 al = new ArrayList<String>();\r
32648c62 455 alComment = new ArrayList<String>();\r
99d2c3c4 456\r
457 len = 0;\r
458 }\r
459\r
460 public String getSizeMacro () {\r
3496595d 461 return String.format(PcdDatabase.LocalTokenNumberTableSizeMacro, phase, getSize())\r
462 + String.format(PcdDatabase.LocalTokenNumberSizeMacro, phase, al.size());\r
99d2c3c4 463 }\r
464\r
465 public int getSize () {\r
466 return (al.size() == 0)? 1 : al.size();\r
467 }\r
468\r
469 public String getExistanceMacro () {\r
470 return String.format(PcdDatabase.DatabaseExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");\r
471 }\r
472\r
473 public String getTypeDeclaration () {\r
474 return String.format(PcdDatabase.LocalTokenNumberTableDeclaration, phase);\r
475 }\r
476\r
477 public ArrayList<String> getInstantiation () {\r
32648c62 478 ArrayList<String> output = new ArrayList<String>();\r
99d2c3c4 479\r
480 output.add("/* LocalTokenNumberTable */");\r
481 output.add("{");\r
99d2c3c4 482\r
32648c62 483 if (al.size() == 0) {\r
484 output.add("0");\r
485 }\r
99d2c3c4 486 \r
487 for (int index = 0; index < al.size(); index++) {\r
32648c62 488 String str;\r
99d2c3c4 489\r
32648c62 490 str = (String)al.get(index);\r
99d2c3c4 491\r
32648c62 492 str += " /* " + alComment.get(index) + " */ ";\r
99d2c3c4 493\r
494\r
32648c62 495 if (index != (al.size() - 1)) {\r
496 str += ",";\r
497 }\r
99d2c3c4 498\r
499 output.add(str);\r
500\r
501 }\r
502\r
32648c62 503 output.add("}");\r
99d2c3c4 504\r
505 return output;\r
506 }\r
507\r
508 public int add (Token token) {\r
509 int index = len;\r
32648c62 510 String str;\r
99d2c3c4 511\r
512 len++; \r
513\r
32648c62 514 str = String.format(PcdDatabase.offsetOfStrTemplate, phase, token.hasDefaultValue() ? "Init" : "Uninit", token.getPrimaryKeyString());\r
99d2c3c4 515\r
32648c62 516 if (token.isStringType()) {\r
517 str += " | PCD_TYPE_STRING";\r
518 }\r
99d2c3c4 519\r
6ff7a41c 520 if (token.isSkuEnable()) {\r
32648c62 521 str += " | PCD_TYPE_SKU_ENABLED";\r
522 }\r
99d2c3c4 523\r
6ff7a41c 524 if (token.getDefaultSku().type == DynamicTokenValue.VALUE_TYPE.HII_TYPE) {\r
32648c62 525 str += " | PCD_TYPE_HII";\r
526 }\r
99d2c3c4 527\r
6ff7a41c 528 if (token.getDefaultSku().type == DynamicTokenValue.VALUE_TYPE.VPD_TYPE) {\r
32648c62 529 str += " | PCD_TYPE_VPD";\r
530 }\r
531 \r
99d2c3c4 532 al.add(str);\r
32648c62 533 alComment.add(token.getPrimaryKeyString());\r
99d2c3c4 534\r
535 return index;\r
536 }\r
537}\r
538\r
539class ExMapTable {\r
540\r
32648c62 541 class ExTriplet {\r
542 public Integer guidTableIdx;\r
543 public Long exTokenNumber;\r
544 public Long localTokenIdx;\r
545 \r
546 public ExTriplet (int guidTableIdx, long exTokenNumber, long localTokenIdx) {\r
547 this.guidTableIdx = new Integer(guidTableIdx);\r
548 this.exTokenNumber = new Long(exTokenNumber);\r
549 this.localTokenIdx = new Long(localTokenIdx);\r
550 }\r
551 }\r
99d2c3c4 552\r
553 private ArrayList<ExTriplet> al;\r
32648c62 554 private ArrayList<String> alComment;\r
99d2c3c4 555 private String phase;\r
556 private int len;\r
32648c62 557 private int bodyStart;\r
558 private int bodyLineNum;\r
559 private int base;\r
99d2c3c4 560\r
561 public ExMapTable (String phase) {\r
562 this.phase = phase;\r
563 al = new ArrayList<ExTriplet>();\r
32648c62 564 alComment = new ArrayList<String>();\r
565 bodyStart = 0;\r
566 bodyLineNum = 0;\r
99d2c3c4 567 len = 0;\r
568 }\r
569\r
570 public String getSizeMacro () {\r
571 return String.format(PcdDatabase.ExMapTableSizeMacro, phase, getTableLen())\r
32648c62 572 + String.format(PcdDatabase.ExTokenNumber, phase, al.size());\r
99d2c3c4 573 }\r
574\r
575 private int getSize () {\r
576 return (al.size() == 0)? 1 : al.size();\r
577 }\r
578\r
579 public String getExistanceMacro () {\r
580 return String.format(PcdDatabase.ExMapTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");\r
581 }\r
582\r
583 public String getTypeDeclaration () {\r
584 return String.format(PcdDatabase.ExMapTableDeclaration, phase);\r
585 }\r
586\r
587 public ArrayList<String> getInstantiation () {\r
32648c62 588 ArrayList<String> Output = new ArrayList<String>();\r
99d2c3c4 589\r
590 Output.add("/* ExMapTable */");\r
591 Output.add("{");\r
32648c62 592 bodyStart = 2;\r
99d2c3c4 593\r
32648c62 594 if (al.size() == 0) {\r
595 Output.add("{0, 0, 0}");\r
596 }\r
99d2c3c4 597 \r
32648c62 598 int index;\r
99d2c3c4 599 for (index = 0; index < al.size(); index++) {\r
32648c62 600 String str;\r
99d2c3c4 601\r
32648c62 602 ExTriplet e = (ExTriplet)al.get(index);\r
99d2c3c4 603\r
32648c62 604 str = "{ " + e.exTokenNumber.toString() + ", ";\r
605 str += e.localTokenIdx.toString() + ", ";\r
606 str += e.guidTableIdx.toString();\r
99d2c3c4 607\r
32648c62 608 str += " /* " + alComment.get(index) + " */";\r
99d2c3c4 609\r
32648c62 610 if (index != al.size() - 1) {\r
611 str += ",";\r
612 }\r
99d2c3c4 613\r
614 Output.add(str);\r
32648c62 615 bodyLineNum++;\r
99d2c3c4 616\r
617 }\r
618\r
32648c62 619 Output.add("}");\r
99d2c3c4 620\r
621 return Output;\r
622 }\r
623\r
624 public int add (int localTokenIdx, long exTokenNum, int guidTableIdx, String name) {\r
625 int index = len;\r
626\r
627 len++; \r
628 al.add(new ExTriplet(guidTableIdx, exTokenNum, localTokenIdx));\r
32648c62 629 alComment.add(name);\r
99d2c3c4 630\r
631 return index;\r
632 }\r
633\r
32648c62 634 public int getTableLen () {\r
635 return al.size() == 0 ? 1 : al.size();\r
636 }\r
99d2c3c4 637\r
638}\r
639\r
640class PcdDatabase {\r
641\r
642 public final static String ExMapTableDeclaration = "DYNAMICEX_MAPPING ExMapTable[%s_EXMAPPING_TABLE_SIZE];\r\n";\r
643 public final static String GuidTableDeclaration = "EFI_GUID GuidTable[%s_GUID_TABLE_SIZE];\r\n";\r
3496595d 644 public final static String LocalTokenNumberTableDeclaration = "UINT32 LocalTokenNumberTable[%s_LOCAL_TOKEN_NUMBER_TABLE_SIZE];\r\n";\r
99d2c3c4 645 public final static String StringTableDeclaration = "UINT16 StringTable[%s_STRING_TABLE_SIZE];\r\n";\r
3496595d 646 public final static String SizeTableDeclaration = "UINT16 SizeTable[%s_LOCAL_TOKEN_NUMBER_TABLE_SIZE];\r\n";\r
99d2c3c4 647 public final static String SkuIdTableDeclaration = "UINT8 SkuIdTable[%s_SKUID_TABLE_SIZE];\r\n";\r
648\r
649\r
650 public final static String ExMapTableSizeMacro = "#define %s_EXMAPPING_TABLE_SIZE %d\r\n";\r
32648c62 651 public final static String ExTokenNumber = "#define %s_EX_TOKEN_NUMBER %d\r\n";\r
99d2c3c4 652 public final static String GuidTableSizeMacro = "#define %s_GUID_TABLE_SIZE %d\r\n";\r
3496595d 653 public final static String LocalTokenNumberTableSizeMacro = "#define %s_LOCAL_TOKEN_NUMBER_TABLE_SIZE %d\r\n";\r
654 public final static String LocalTokenNumberSizeMacro = "#define %s_LOCAL_TOKEN_NUMBER %d\r\n";\r
99d2c3c4 655 public final static String StringTableSizeMacro = "#define %s_STRING_TABLE_SIZE %d\r\n";\r
656 public final static String SkuIdTableSizeMacro = "#define %s_SKUID_TABLE_SIZE %d\r\n";\r
657\r
658\r
659 public final static String ExMapTableExistenceMacro = "#define %s_EXMAP_TABLE_EMPTY %s\r\n"; \r
660 public final static String GuidTableExistenceMacro = "#define %s_GUID_TABLE_EMPTY %s\r\n";\r
661 public final static String DatabaseExistenceMacro = "#define %s_DATABASE_EMPTY %s\r\n";\r
662 public final static String StringTableExistenceMacro = "#define %s_STRING_TABLE_EMPTY %s\r\n";\r
663 public final static String SkuTableExistenceMacro = "#define %s_SKUID_TABLE_EMPTY %s\r\n";\r
664\r
32648c62 665 public final static String offsetOfSkuHeadStrTemplate = "offsetof(%s_PCD_DATABASE, %s.%s_SkuDataTable)";\r
666 public final static String offsetOfStrTemplate = "offsetof(%s_PCD_DATABASE, %s.%s)";\r
99d2c3c4 667\r
32648c62 668 private StringTable stringTable;\r
669 private GuidTable guidTable;\r
670 private LocalTokenNumberTable localTokenNumberTable;\r
671 private SkuIdTable skuIdTable;\r
672 private SizeTable sizeTable;\r
673 private ExMapTable exMapTable;\r
99d2c3c4 674\r
32648c62 675 private ArrayList<Token> alTokens;\r
676 private String phase;\r
677 private int assignedTokenNumber;\r
4acf8ce7 678 \r
679 //\r
680 // After Major changes done to the PCD\r
681 // database generation class PcdDatabase\r
682 // Please increment the version and please\r
683 // also update the version number in PCD\r
684 // service PEIM and DXE driver accordingly.\r
685 //\r
686 private final int version = 1;\r
99d2c3c4 687\r
32648c62 688 private String hString;\r
689 private String cString;\r
99d2c3c4 690\r
691\r
32648c62 692 class AlignmentSizeComp implements Comparator<Token> {\r
99d2c3c4 693 public int compare (Token a, Token b) {\r
32648c62 694 return getAlignmentSize(b) \r
695 - getAlignmentSize(a);\r
696 }\r
697 }\r
698\r
699 public PcdDatabase (ArrayList<Token> alTokens, String exePhase, int startLen) {\r
700 phase = exePhase;\r
701\r
702 stringTable = new StringTable(phase);\r
703 guidTable = new GuidTable(phase);\r
704 localTokenNumberTable = new LocalTokenNumberTable(phase);\r
705 skuIdTable = new SkuIdTable(phase);\r
706 sizeTable = new SizeTable(phase);\r
707 exMapTable = new ExMapTable(phase); \r
708\r
709 assignedTokenNumber = startLen;\r
710 this.alTokens = alTokens;\r
711 }\r
712\r
713 private void getTwoGroupsOfTokens (ArrayList<Token> alTokens, List<Token> initTokens, List<Token> uninitTokens) {\r
714 for (int i = 0; i < alTokens.size(); i++) {\r
8840ad58 715 Token t = (Token)alTokens.get(i);\r
32648c62 716 if (t.hasDefaultValue()) {\r
717 initTokens.add(t);\r
718 } else {\r
719 uninitTokens.add(t);\r
720 }\r
721 }\r
722\r
723 return;\r
724 }\r
725\r
726 private int getAlignmentSize (Token token) {\r
6ff7a41c 727 if (token.getDefaultSku().type == DynamicTokenValue.VALUE_TYPE.HII_TYPE) {\r
32648c62 728 return 2;\r
729 }\r
730\r
6ff7a41c 731 if (token.getDefaultSku().type == DynamicTokenValue.VALUE_TYPE.VPD_TYPE) {\r
32648c62 732 return 4;\r
733 }\r
734\r
735 if (token.isStringType()) {\r
736 return 2;\r
737 }\r
738\r
739 switch (token.datumType) {\r
740 case UINT8:\r
741 return 1;\r
742 case UINT16:\r
743 return 2;\r
744 case UINT32:\r
745 return 4;\r
746 case UINT64:\r
747 return 8;\r
748 case POINTER:\r
749 return 1;\r
750 case BOOLEAN:\r
751 return 1;\r
752 }\r
753 return 1;\r
754 }\r
755\r
756 public String getCString () {\r
757 return cString;\r
758 }\r
759\r
760 public String getHString () {\r
761 return hString;\r
762 }\r
99d2c3c4 763\r
6ff7a41c 764 public void genCode () \r
765 throws EntityException {\r
99d2c3c4 766\r
32648c62 767 final String newLine = "\r\n";\r
32648c62 768 final String declNewLine = ";\r\n";\r
769 final String tab = "\t";\r
32648c62 770 final String commaNewLine = ", \r\n";\r
771\r
772 int i;\r
773 ArrayList<String> decla;\r
774 ArrayList<String> inst;\r
775\r
776 String macroStr = "";\r
777 String initDeclStr = "";\r
778 String initInstStr = "";\r
779 String uninitDeclStr = "";\r
780\r
781 List<Token> initTokens = new ArrayList<Token> ();\r
782 List<Token> uninitTokens = new ArrayList<Token> ();\r
783 \r
784 HashMap <String, ArrayList<String>> initCode = new HashMap<String, ArrayList<String>> ();\r
785 HashMap <String, ArrayList<String>> uninitCode = new HashMap<String, ArrayList<String>> ();\r
786\r
787 getTwoGroupsOfTokens (alTokens, initTokens, uninitTokens);\r
788\r
789 //\r
790 // Generate Structure Declaration for PcdTokens without Default Value\r
791 // PEI_PCD_DATABASE_INIT\r
792 //\r
6a4cae58 793 java.util.Comparator<Token> comparator = new AlignmentSizeComp();\r
4c114006 794 java.util.Collections.sort(initTokens, comparator);\r
32648c62 795 initCode = processTokens(initTokens);\r
796\r
797 //\r
798 // Generate Structure Declaration for PcdTokens without Default Value\r
799 // PEI_PCD_DATABASE_UNINIT\r
800 //\r
4c114006 801 java.util.Collections.sort(uninitTokens, comparator);\r
32648c62 802 uninitCode = processTokens(uninitTokens);\r
803\r
804 //\r
805 // Generate size info Macro for all Tables\r
806 //\r
807 macroStr += guidTable.getSizeMacro();\r
808 macroStr += stringTable.getSizeMacro();\r
809 macroStr += skuIdTable.getSizeMacro();\r
810 macroStr += localTokenNumberTable.getSizeMacro();\r
811 macroStr += exMapTable.getSizeMacro();\r
812\r
813 //\r
814 // Generate existance info Macro for all Tables\r
815 //\r
816 macroStr += guidTable.getExistanceMacro();\r
817 macroStr += stringTable.getExistanceMacro();\r
818 macroStr += skuIdTable.getExistanceMacro();\r
819 macroStr += localTokenNumberTable.getExistanceMacro();\r
820 macroStr += exMapTable.getExistanceMacro();\r
821\r
822 //\r
823 // Generate Structure Declaration for PcdTokens with Default Value\r
824 // for example PEI_PCD_DATABASE_INIT\r
825 //\r
826 initDeclStr += "typedef struct {" + newLine;\r
827 {\r
828 initDeclStr += tab + exMapTable.getTypeDeclaration();\r
829 initDeclStr += tab + guidTable.getTypeDeclaration();\r
830 initDeclStr += tab + localTokenNumberTable.getTypeDeclaration();\r
831 initDeclStr += tab + stringTable.getTypeDeclaration();\r
832 initDeclStr += tab + sizeTable.getTypeDeclaration();\r
833 initDeclStr += tab + skuIdTable.getTypeDeclaration();\r
834 if (phase.equalsIgnoreCase("PEI")) {\r
835 initDeclStr += tab + "SKU_ID SystemSkuId;" + newLine;\r
836 }\r
837\r
838 decla = initCode.get(new String("Declaration"));\r
839 for (i = 0; i < decla.size(); i++) {\r
840 initDeclStr += tab + decla.get(i) + declNewLine;\r
841 }\r
842\r
843 //\r
844 // Generate Structure Declaration for PcdToken with SkuEnabled\r
845 //\r
846 decla = initCode.get("DeclarationForSku");\r
847\r
848 for (i = 0; i < decla.size(); i++) {\r
849 initDeclStr += tab + decla.get(i) + declNewLine;\r
850 }\r
851 }\r
852 initDeclStr += String.format("} %s_PCD_DATABASE_INIT;\r\n\r\n", phase);\r
853\r
854 //\r
855 // Generate MACRO for structure intialization of PCDTokens with Default Value\r
856 // The sequence must match the sequence of declaration of the memembers in the structure\r
857 String tmp = String.format("%s_PCD_DATABASE_INIT g%sPcdDbInit = { ", phase.toUpperCase(), phase.toUpperCase());\r
858 initInstStr += tmp + newLine;\r
859 initInstStr += tab + genInstantiationStr(exMapTable.getInstantiation()) + commaNewLine;\r
860 initInstStr += tab + genInstantiationStr(guidTable.getInstantiation()) + commaNewLine;\r
861 initInstStr += tab + genInstantiationStr(localTokenNumberTable.getInstantiation()) + commaNewLine; \r
32648c62 862 initInstStr += tab + genInstantiationStr(stringTable.getInstantiation()) + commaNewLine;\r
863 initInstStr += tab + genInstantiationStr(sizeTable.getInstantiation()) + commaNewLine;\r
864 initInstStr += tab + genInstantiationStr(skuIdTable.getInstantiation()) + commaNewLine;\r
865 //\r
866 // For SystemSkuId\r
867 //\r
868 if (phase.equalsIgnoreCase("PEI")) {\r
869 initInstStr += tab + "0" + tab + "/* SystemSkuId */" + commaNewLine;\r
870 }\r
871\r
872 inst = initCode.get("Instantiation");\r
873 for (i = 0; i < inst.size(); i++) {\r
874 initInstStr += tab + inst.get(i) + commaNewLine;\r
875 }\r
876\r
877 inst = initCode.get("InstantiationForSku");\r
878 for (i = 0; i < inst.size(); i++) {\r
879 initInstStr += tab + inst.get(i);\r
880 if (i != inst.size() - 1) {\r
881 initInstStr += commaNewLine;\r
882 }\r
883 }\r
884\r
885 initInstStr += "};";\r
886\r
887 uninitDeclStr += "typedef struct {" + newLine;\r
888 {\r
889 decla = uninitCode.get("Declaration");\r
890 if (decla.size() == 0) {\r
891 uninitDeclStr += "UINT8 dummy /* The UINT struct is empty */" + declNewLine;\r
892 } else {\r
893 \r
894 for (i = 0; i < decla.size(); i++) {\r
895 uninitDeclStr += tab + decla.get(i) + declNewLine;\r
896 }\r
897 \r
898 decla = uninitCode.get("DeclarationForSku");\r
899 \r
900 for (i = 0; i < decla.size(); i++) {\r
901 uninitDeclStr += tab + decla.get(i) + declNewLine;\r
902 }\r
903 }\r
904 }\r
905 uninitDeclStr += String.format("} %s_PCD_DATABASE_UNINIT;\r\n\r\n", phase);\r
906\r
907 cString = initInstStr + newLine;\r
908 hString = macroStr + newLine \r
909 + initDeclStr + newLine\r
910 + uninitDeclStr + newLine\r
911 + newLine;\r
4acf8ce7 912 \r
913 hString += String.format("#define PCD_%s_SERVICE_DRIVER_VERSION %d", phase, version);\r
32648c62 914\r
915 }\r
916\r
917 private String genInstantiationStr (ArrayList<String> alStr) {\r
918 String str = "";\r
919 for (int i = 0; i< alStr.size(); i++) {\r
920 str += "\t" + alStr.get(i);\r
921 if (i != alStr.size() - 1) {\r
922 str += "\r\n";\r
923 }\r
924 }\r
925\r
926 return str;\r
927 }\r
928\r
6ff7a41c 929 private HashMap<String, ArrayList<String>> processTokens (List<Token> alToken) \r
930 throws EntityException {\r
32648c62 931\r
32648c62 932 HashMap <String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();\r
933\r
934 ArrayList<String> decl = new ArrayList<String>();\r
935 ArrayList<String> declForSkuEnableType = new ArrayList<String>();\r
936 ArrayList<String> inst = new ArrayList<String>();\r
937 ArrayList<String> instForSkuEnableType = new ArrayList<String>();\r
938\r
939 for (int index = 0; index < alToken.size(); index++) {\r
940 Token token = alToken.get(index);\r
941\r
6ff7a41c 942 if (token.isSkuEnable()) {\r
32648c62 943 //\r
944 // BugBug: Schema only support Data type now\r
945 //\r
946 int tableIdx;\r
947\r
948 tableIdx = skuIdTable.add(token);\r
949\r
950 decl.add(getSkuEnabledTypeDeclaration(token));\r
951 if (token.hasDefaultValue()) {\r
952 inst.add(getSkuEnabledTypeInstantiaion(token, tableIdx)); \r
953 }\r
954\r
955 declForSkuEnableType.add(getDataTypeDeclarationForSkuEnabled(token));\r
956 if (token.hasDefaultValue()) {\r
957 instForSkuEnableType.add(getDataTypeInstantiationForSkuEnabled(token));\r
958 }\r
959\r
960 } else {\r
6ff7a41c 961 if (token.getDefaultSku().type == DynamicTokenValue.VALUE_TYPE.HII_TYPE) {\r
32648c62 962 decl.add(getVariableEnableTypeDeclaration(token));\r
963 inst.add(getVariableEnableInstantiation(token));\r
6ff7a41c 964 } else if (token.getDefaultSku().type == DynamicTokenValue.VALUE_TYPE.VPD_TYPE) {\r
32648c62 965 decl.add(getVpdEnableTypeDeclaration(token));\r
966 inst.add(getVpdEnableTypeInstantiation(token));\r
967 } else if (token.isStringType()) {\r
968 decl.add(getStringTypeDeclaration(token));\r
969 inst.add(getStringTypeInstantiation(stringTable.add(token.getStringTypeString(), token), token));\r
970 }\r
971 else {\r
972 decl.add(getDataTypeDeclaration(token));\r
973 if (token.hasDefaultValue()) {\r
974 inst.add(getDataTypeInstantiation(token));\r
975 }\r
976 }\r
977 }\r
978\r
979 sizeTable.add(token);\r
980 localTokenNumberTable.add(token);\r
8840ad58 981 token.tokenNumber = assignedTokenNumber++;\r
32648c62 982\r
983 }\r
984\r
985 map.put("Declaration", decl);\r
986 map.put("DeclarationForSku", declForSkuEnableType);\r
987 map.put("Instantiation", inst);\r
988 map.put("InstantiationForSku", instForSkuEnableType);\r
989\r
990 return map;\r
991 }\r
992\r
993 private String getSkuEnabledTypeDeclaration (Token token) {\r
994 return String.format("SKU_HEAD %s;\r\n", token.getPrimaryKeyString());\r
995 }\r
996\r
997 private String getSkuEnabledTypeInstantiaion (Token token, int SkuTableIdx) {\r
998\r
999 String offsetof = String.format(PcdDatabase.offsetOfSkuHeadStrTemplate, phase, token.hasDefaultValue()? "Init" : "Uninit", token.getPrimaryKeyString());\r
1000 return String.format("{ %s, %d }", offsetof, SkuTableIdx);\r
1001 }\r
1002\r
1003 private String getDataTypeDeclarationForSkuEnabled (Token token) {\r
1004 String typeStr = "";\r
1005\r
1006 if (token.datumType == Token.DATUM_TYPE.UINT8) {\r
1007 typeStr = "UINT8 %s_%s[%d];\r\n";\r
1008 } else if (token.datumType == Token.DATUM_TYPE.UINT16) {\r
1009 typeStr = "UINT16 %s_%s[%d];\r\n";\r
1010 } else if (token.datumType == Token.DATUM_TYPE.UINT32) {\r
1011 typeStr = "UINT32 %s_%s[%d];\r\n";\r
1012 } else if (token.datumType == Token.DATUM_TYPE.UINT64) {\r
1013 typeStr = "UINT64 %s_%s[%d];\r\n";\r
1014 } else if (token.datumType == Token.DATUM_TYPE.BOOLEAN) {\r
1015 typeStr = "BOOLEAN %s_%s[%d];\r\n";\r
1016 } else if (token.datumType == Token.DATUM_TYPE.POINTER) {\r
6ff7a41c 1017 return String.format("UINT8 %s_%s[%d];\r\n", token.getPrimaryKeyString(), "SkuDataTable", token.datumSize * token.skuData.size());\r
32648c62 1018 } \r
1019\r
6ff7a41c 1020 return String.format(typeStr, token.getPrimaryKeyString(), "SkuDataTable", token.skuData.size());\r
32648c62 1021\r
1022 }\r
1023\r
1024 private String getDataTypeInstantiationForSkuEnabled (Token token) {\r
1025 String str = "";\r
1026\r
1027 if (token.datumType == Token.DATUM_TYPE.POINTER) {\r
6ff7a41c 1028 return String.format("UINT8 %s_%s[%d]", token.getPrimaryKeyString(), "SkuDataTable", token.datumSize * token.skuData.size());\r
32648c62 1029 } else {\r
1030 str = "{ ";\r
6ff7a41c 1031 for (int idx = 0; idx < token.skuData.size(); idx++) {\r
32648c62 1032 str += token.skuData.get(idx).toString();\r
6ff7a41c 1033 if (idx != token.skuData.size() - 1) {\r
32648c62 1034 str += ", ";\r
1035 }\r
1036 }\r
1037 str += "}";\r
1038\r
1039 return str;\r
1040 }\r
1041\r
1042 }\r
1043\r
1044 private String getDataTypeInstantiation (Token token) {\r
1045\r
32648c62 1046 if (token.datumType == Token.DATUM_TYPE.POINTER) {\r
6ff7a41c 1047 return String.format("%s /* %s */", token.getDefaultSku().value, token.getPrimaryKeyString());\r
32648c62 1048 } else {\r
6ff7a41c 1049 return String.format("%s /* %s */", token.getDefaultSku().value, token.getPrimaryKeyString());\r
32648c62 1050 }\r
1051 }\r
1052\r
1053\r
1054 private String getDataTypeDeclaration (Token token) {\r
1055\r
1056 String typeStr = "";\r
1057\r
1058 if (token.datumType == Token.DATUM_TYPE.UINT8) {\r
1059 typeStr = "UINT8";\r
1060 } else if (token.datumType == Token.DATUM_TYPE.UINT16) {\r
1061 typeStr = "UINT16";\r
1062 } else if (token.datumType == Token.DATUM_TYPE.UINT32) {\r
1063 typeStr = "UINT32";\r
1064 } else if (token.datumType == Token.DATUM_TYPE.UINT64) {\r
1065 typeStr = "UINT64";\r
1066 } else if (token.datumType == Token.DATUM_TYPE.BOOLEAN) {\r
1067 typeStr = "BOOLEAN";\r
1068 } else if (token.datumType == Token.DATUM_TYPE.POINTER) {\r
1069 return String.format("UINT8 %s[%d]", token.getPrimaryKeyString(), token.datumSize);\r
1070 } else {\r
1071 }\r
1072\r
1073 return String.format("%s %s", typeStr, token.getPrimaryKeyString());\r
1074 }\r
1075\r
1076 private String getVpdEnableTypeDeclaration (Token token) {\r
1077 return String.format("VPD_HEAD %s", token.getPrimaryKeyString());\r
1078 }\r
1079\r
1080 private String getVpdEnableTypeInstantiation (Token token) {\r
6ff7a41c 1081 return String.format("{ %s } /* %s */", token.getDefaultSku().vpdOffset,\r
32648c62 1082 token.getPrimaryKeyString());\r
1083 }\r
1084\r
1085 private String getStringTypeDeclaration (Token token) {\r
1086 return String.format("UINT16 %s", token.getPrimaryKeyString());\r
1087 }\r
1088\r
1089 private String getStringTypeInstantiation (int StringTableIdx, Token token) {\r
1090 return String.format ("%d /* %s */", StringTableIdx,\r
1091 token.getPrimaryKeyString()); \r
1092 }\r
1093\r
1094\r
1095 private String getVariableEnableTypeDeclaration (Token token) {\r
1096 return String.format("VARIABLE_HEAD %s", token.getPrimaryKeyString());\r
1097 }\r
1098\r
6ff7a41c 1099 private String getVariableEnableInstantiation (Token token) \r
1100 throws EntityException {\r
1101 //\r
1102 // Need scott fix\r
1103 // \r
1104 return String.format("{ %d, %d, %s } /* %s */", guidTable.add(token.getDefaultSku().variableGuid, token.getPrimaryKeyString()),\r
1105 stringTable.add(token.getDefaultSku().getStringOfVariableName(), token),\r
1106 token.getDefaultSku().variableOffset, \r
32648c62 1107 token.getPrimaryKeyString());\r
1108 }\r
1109\r
1110 public int getTotalTokenNumber () {\r
1111 return sizeTable.getTableLen();\r
1112 }\r
99d2c3c4 1113\r
1114 public static String getPcdDatabaseCommonDefinitions () \r
1115 throws EntityException {\r
1116\r
1117 String retStr = "";\r
1118 try {\r
32648c62 1119 File file = new File(GlobalData.getWorkspacePath() + File.separator + \r
1120 "Tools" + File.separator + \r
1121 "Conf" + File.separator +\r
1122 "Pcd" + File.separator +\r
1123 "PcdDatabaseCommonDefinitions.sample");\r
99d2c3c4 1124 FileReader reader = new FileReader(file);\r
1125 BufferedReader in = new BufferedReader(reader);\r
1126 String str;\r
1127 while ((str = in.readLine()) != null) {\r
1128 retStr = retStr +"\r\n" + str;\r
1129 }\r
1130 } catch (Exception ex) {\r
1131 throw new EntityException("Fatal error when generating PcdDatabase Common Definitions");\r
1132 }\r
1133\r
1134 return retStr;\r
1135 }\r
1136\r
32648c62 1137 public static String getPcdDxeDatabaseDefinitions () \r
1138 throws EntityException {\r
1139\r
1140 String retStr = "";\r
1141 try {\r
1142 File file = new File(GlobalData.getWorkspacePath() + File.separator + \r
1143 "Tools" + File.separator + \r
1144 "Conf" + File.separator +\r
1145 "Pcd" + File.separator +\r
1146 "PcdDatabaseDxeDefinitions.sample");\r
1147 FileReader reader = new FileReader(file);\r
1148 BufferedReader in = new BufferedReader(reader);\r
1149 String str;\r
1150 while ((str = in.readLine()) != null) {\r
1151 retStr = retStr +"\r\n" + str;\r
1152 }\r
1153 } catch (Exception ex) {\r
1154 throw new EntityException("Fatal error when generating PcdDatabase Dxe Definitions");\r
1155 }\r
1156\r
1157 return retStr;\r
1158 }\r
1159\r
1160 public static String getPcdPeiDatabaseDefinitions () \r
1161 throws EntityException {\r
1162\r
1163 String retStr = "";\r
1164 try {\r
1165 File file = new File(GlobalData.getWorkspacePath() + File.separator + \r
1166 "Tools" + File.separator + \r
1167 "Conf" + File.separator +\r
1168 "Pcd" + File.separator +\r
1169 "PcdDatabasePeiDefinitions.sample");\r
1170 FileReader reader = new FileReader(file);\r
1171 BufferedReader in = new BufferedReader(reader);\r
1172 String str;\r
1173 while ((str = in.readLine()) != null) {\r
1174 retStr = retStr +"\r\n" + str;\r
1175 }\r
1176 } catch (Exception ex) {\r
1177 throw new EntityException("Fatal error when generating PcdDatabase Pei Definitions");\r
1178 }\r
1179\r
1180 return retStr;\r
1181 }\r
99d2c3c4 1182\r
1183}\r
1184\r
8840ad58 1185class ModuleInfo {\r
1186 public ModuleSADocument.ModuleSA module;\r
1187 public UsageInstance.MODULE_TYPE type;\r
1188\r
1189 public ModuleInfo (ModuleSADocument.ModuleSA module, UsageInstance.MODULE_TYPE type) {\r
1190 this.module = module;\r
1191 this.type = type;\r
1192 }\r
1193}\r
1194\r
878ddf1f 1195/** This action class is to collect PCD information from MSA, SPD, FPD xml file.\r
1196 This class will be used for wizard and build tools, So it can *not* inherit\r
1197 from buildAction or UIAction.\r
1198**/\r
1199public class CollectPCDAction {\r
1200 /// memoryDatabase hold all PCD information collected from SPD, MSA, FPD.\r
1201 private MemoryDatabaseManager dbManager;\r
1202\r
1203 /// Workspacepath hold the workspace information.\r
1204 private String workspacePath;\r
1205\r
1206 /// FPD file is the root file. \r
1207 private String fpdFilePath;\r
1208\r
1209 /// Message level for CollectPCDAction.\r
1210 private int originalMessageLevel;\r
1211\r
8840ad58 1212 /// Cache the fpd docment instance for private usage.\r
1213 private FrameworkPlatformDescriptionDocument fpdDocInstance;\r
1214\r
878ddf1f 1215 /**\r
1216 Set WorkspacePath parameter for this action class.\r
1217\r
1218 @param workspacePath parameter for this action\r
1219 **/\r
1220 public void setWorkspacePath(String workspacePath) {\r
1221 this.workspacePath = workspacePath;\r
1222 }\r
1223\r
1224 /**\r
1225 Set action message level for CollectPcdAction tool.\r
1226\r
1227 The message should be restored when this action exit.\r
1228\r
1229 @param actionMessageLevel parameter for this action\r
1230 **/\r
1231 public void setActionMessageLevel(int actionMessageLevel) {\r
1232 originalMessageLevel = ActionMessage.messageLevel;\r
1233 ActionMessage.messageLevel = actionMessageLevel;\r
1234 }\r
1235\r
1236 /**\r
1237 Set FPDFileName parameter for this action class.\r
1238\r
1239 @param fpdFilePath fpd file path\r
1240 **/\r
1241 public void setFPDFilePath(String fpdFilePath) {\r
1242 this.fpdFilePath = fpdFilePath;\r
1243 }\r
1244\r
1245 /**\r
1246 Common function interface for outer.\r
1247 \r
1248 @param workspacePath The path of workspace of current build or analysis.\r
1249 @param fpdFilePath The fpd file path of current build or analysis.\r
1250 @param messageLevel The message level for this Action.\r
1251 \r
1252 @throws Exception The exception of this function. Because it can *not* be predict\r
1253 where the action class will be used. So only Exception can be throw.\r
1254 \r
1255 **/\r
1256 public void perform(String workspacePath, String fpdFilePath, \r
1257 int messageLevel) throws Exception {\r
1258 setWorkspacePath(workspacePath);\r
1259 setFPDFilePath(fpdFilePath);\r
1260 setActionMessageLevel(messageLevel);\r
1261 checkParameter();\r
1262 execute();\r
1263 ActionMessage.messageLevel = originalMessageLevel;\r
1264 }\r
1265\r
1266 /**\r
1267 Core execution function for this action class.\r
1268 \r
1269 This function work flows will be:\r
8840ad58 1270 1) Collect and prepocess PCD information from FPD file, all PCD\r
1271 information will be stored into memory database.\r
1272 2) Generate 3 strings for\r
1273 a) All modules using Dynamic(Ex) PCD entry.(Token Number)\r
1274 b) PEI PCDDatabase (C Structure) for PCD Service PEIM.\r
1275 c) DXE PCD Database (C structure) for PCD Service DXE.\r
99d2c3c4 1276 \r
878ddf1f 1277 \r
1278 @throws EntityException Exception indicate failed to execute this action.\r
1279 \r
1280 **/\r
1281 private void execute() throws EntityException {\r
8840ad58 1282 //\r
1283 // Get memoryDatabaseManager instance from GlobalData.\r
1284 // The memoryDatabaseManager should be initialized for whatever build\r
1285 // tools or wizard tools\r
1286 //\r
1287 if((dbManager = GlobalData.getPCDMemoryDBManager()) == null) {\r
1288 throw new EntityException("The instance of PCD memory database manager is null");\r
1289 }\r
878ddf1f 1290\r
1291 //\r
1292 // Collect all PCD information defined in FPD file.\r
1293 // Evenry token defind in FPD will be created as an token into \r
1294 // memory database.\r
1295 //\r
8840ad58 1296 createTokenInDBFromFPD();\r
99d2c3c4 1297 \r
1298 //\r
1299 // Call Private function genPcdDatabaseSourceCode (void); ComponentTypeBsDriver\r
1300 // 1) Generate for PEI, DXE PCD DATABASE's definition and initialization.\r
32648c62 1301 //\r
1302 genPcdDatabaseSourceCode ();\r
1303 \r
878ddf1f 1304 }\r
1305\r
32648c62 1306 /**\r
1307 This function generates source code for PCD Database.\r
1308 \r
1309 @param void\r
1310 @throws EntityException If the token does *not* exist in memory database.\r
99d2c3c4 1311\r
32648c62 1312 **/\r
8840ad58 1313 private void genPcdDatabaseSourceCode()\r
1314 throws EntityException {\r
32648c62 1315 String PcdCommonHeaderString = PcdDatabase.getPcdDatabaseCommonDefinitions ();\r
99d2c3c4 1316\r
32648c62 1317 ArrayList<Token> alPei = new ArrayList<Token> ();\r
1318 ArrayList<Token> alDxe = new ArrayList<Token> ();\r
99d2c3c4 1319\r
32648c62 1320 dbManager.getTwoPhaseDynamicRecordArray(alPei, alDxe);\r
99d2c3c4 1321 PcdDatabase pcdPeiDatabase = new PcdDatabase (alPei, "PEI", 0);\r
32648c62 1322 pcdPeiDatabase.genCode();\r
1323 dbManager.PcdPeimHString = PcdCommonHeaderString + pcdPeiDatabase.getHString()\r
1324 + PcdDatabase.getPcdPeiDatabaseDefinitions();\r
1325 dbManager.PcdPeimCString = pcdPeiDatabase.getCString();\r
99d2c3c4 1326\r
1327 PcdDatabase pcdDxeDatabase = new PcdDatabase (alDxe, \r
32648c62 1328 "DXE",\r
1329 alPei.size()\r
1330 );\r
1331 pcdDxeDatabase.genCode();\r
1332 dbManager.PcdDxeHString = dbManager.PcdPeimHString + pcdDxeDatabase.getHString()\r
1333 + PcdDatabase.getPcdDxeDatabaseDefinitions();\r
1334 dbManager.PcdDxeCString = pcdDxeDatabase.getCString();\r
1335 }\r
1336\r
1337 /**\r
8840ad58 1338 Get component array from FPD.\r
878ddf1f 1339 \r
8840ad58 1340 This function maybe provided by some Global class.\r
878ddf1f 1341 \r
8840ad58 1342 @return List<ModuleInfo> the component array.\r
878ddf1f 1343 \r
8840ad58 1344 */\r
1345 private List<ModuleInfo> getComponentsFromFPD() \r
878ddf1f 1346 throws EntityException {\r
8840ad58 1347 HashMap<String, XmlObject> map = new HashMap<String, XmlObject>();\r
1348 List<ModuleInfo> allModules = new ArrayList<ModuleInfo>();\r
1349 ModuleInfo current = null;\r
1350 int index = 0;\r
1351 org.tianocore.Components components = null;\r
1352 FrameworkModulesDocument.FrameworkModules fModules = null;\r
1353 java.util.List<ModuleSADocument.ModuleSA> modules = null;\r
1354 \r
878ddf1f 1355\r
8840ad58 1356 if (fpdDocInstance == null) {\r
1357 try {\r
1358 fpdDocInstance = (FrameworkPlatformDescriptionDocument)XmlObject.Factory.parse(new File(fpdFilePath));\r
1359 } catch(IOException ioE) {\r
1360 throw new EntityException("File IO error for xml file:" + fpdFilePath + "\n" + ioE.getMessage());\r
1361 } catch(XmlException xmlE) {\r
1362 throw new EntityException("Can't parse the FPD xml fle:" + fpdFilePath + "\n" + xmlE.getMessage());\r
1363 }\r
878ddf1f 1364\r
878ddf1f 1365 }\r
1366\r
8840ad58 1367 //\r
1368 // Check whether FPD contians <FramworkModules>\r
1369 // \r
1370 fModules = fpdDocInstance.getFrameworkPlatformDescription().getFrameworkModules();\r
1371 if (fModules == null) {\r
1372 return null;\r
1373 }\r
878ddf1f 1374\r
8840ad58 1375 //\r
1376 // BUGBUG: The following is work around code, the final component type should be get from\r
1377 // GlobalData class.\r
1378 // \r
1379 components = fModules.getSEC();\r
1380 if (components != null) {\r
1381 modules = components.getModuleSAList();\r
1382 for (index = 0; index < modules.size(); index ++) {\r
1383 allModules.add(new ModuleInfo(modules.get(index), UsageInstance.MODULE_TYPE.SEC));\r
878ddf1f 1384 }\r
8840ad58 1385 }\r
878ddf1f 1386\r
8840ad58 1387 components = fModules.getPEICORE();\r
1388 if (components != null) {\r
1389 modules = components.getModuleSAList();\r
1390 for (index = 0; index < modules.size(); index ++) {\r
1391 allModules.add(new ModuleInfo(modules.get(index), UsageInstance.MODULE_TYPE.PEI_CORE));\r
878ddf1f 1392 }\r
1393 }\r
878ddf1f 1394\r
8840ad58 1395 components = fModules.getPEIM();\r
1396 if (components != null) {\r
1397 modules = components.getModuleSAList();\r
1398 for (index = 0; index < modules.size(); index ++) {\r
1399 allModules.add(new ModuleInfo(modules.get(index), UsageInstance.MODULE_TYPE.PEIM));\r
1400 }\r
878ddf1f 1401 }\r
878ddf1f 1402\r
8840ad58 1403 components = fModules.getDXECORE();\r
1404 if (components != null) {\r
1405 modules = components.getModuleSAList();\r
1406 for (index = 0; index < modules.size(); index ++) {\r
1407 allModules.add(new ModuleInfo(modules.get(index), UsageInstance.MODULE_TYPE.DXE_CORE));\r
1408 }\r
878ddf1f 1409 }\r
1410\r
8840ad58 1411 components = fModules.getDXEDRIVERS();\r
1412 if (components != null) {\r
1413 modules = components.getModuleSAList();\r
1414 for (index = 0; index < modules.size(); index ++) {\r
1415 allModules.add(new ModuleInfo(modules.get(index), UsageInstance.MODULE_TYPE.DXE_DRIVERS));\r
1416 }\r
878ddf1f 1417 }\r
1418\r
8840ad58 1419 components = fModules.getOTHERCOMPONENTS();\r
1420 if (components != null) {\r
1421 modules = components.getModuleSAList();\r
1422 for (index = 0; index < modules.size(); index ++) {\r
1423 allModules.add(new ModuleInfo(modules.get(index), UsageInstance.MODULE_TYPE.OTHER_COMPONENTS));\r
1424 }\r
1425 }\r
1426 \r
1427 return allModules;\r
878ddf1f 1428 }\r
1429\r
1430 /**\r
1431 Create token instance object into memory database, the token information\r
1432 comes for FPD file. Normally, FPD file will contain all token platform \r
1433 informations.\r
878ddf1f 1434 \r
1435 @return FrameworkPlatformDescriptionDocument The FPD document instance for furture usage.\r
1436 \r
1437 @throws EntityException Failed to parse FPD xml file.\r
1438 \r
1439 **/\r
8840ad58 1440 private void createTokenInDBFromFPD() \r
878ddf1f 1441 throws EntityException {\r
8840ad58 1442 int index = 0;\r
1443 int index2 = 0;\r
1444 int pcdIndex = 0;\r
6ff7a41c 1445 List<PcdBuildDefinition.PcdData> pcdBuildDataArray = new ArrayList<PcdBuildDefinition.PcdData>();\r
1446 PcdBuildDefinition.PcdData pcdBuildData = null;\r
8840ad58 1447 Token token = null;\r
8840ad58 1448 SkuInstance skuInstance = null;\r
1449 int skuIndex = 0;\r
1450 List<ModuleInfo> modules = null;\r
1451 String primaryKey = null;\r
8840ad58 1452 String exceptionString = null;\r
1453 UsageInstance usageInstance = null;\r
1454 String primaryKey1 = null;\r
1455 String primaryKey2 = null;\r
1456 boolean isDuplicate = false;\r
6ff7a41c 1457 Token.PCD_TYPE pcdType = Token.PCD_TYPE.UNKNOWN;\r
1458 Token.DATUM_TYPE datumType = Token.DATUM_TYPE.UNKNOWN;\r
1459 int tokenNumber = 0;\r
1460 String moduleName = null;\r
1461 String datum = null;\r
1462 int maxDatumSize = 0;\r
878ddf1f 1463\r
1464 //\r
6ff7a41c 1465 // ----------------------------------------------\r
1466 // 1), Get all <ModuleSA> from FPD file.\r
1467 // ----------------------------------------------\r
878ddf1f 1468 // \r
8840ad58 1469 modules = getComponentsFromFPD();\r
878ddf1f 1470\r
8840ad58 1471 if (modules == null) {\r
7db4ab70 1472 throw new EntityException("[FPD file error] No modules in FPD file, Please check whether there are elements in <FrameworkModules> in FPD file!");\r
878ddf1f 1473 }\r
1474\r
1475 //\r
6ff7a41c 1476 // -------------------------------------------------------------------\r
1477 // 2), Loop all modules to process <PcdBuildDeclarations> for each module.\r
1478 // -------------------------------------------------------------------\r
8840ad58 1479 // \r
1480 for (index = 0; index < modules.size(); index ++) {\r
1481 isDuplicate = false;\r
1482 for (index2 = 0; index2 < index; index2 ++) {\r
1483 //\r
1484 // BUGBUG: For transition schema, we can *not* get module's version from \r
1485 // <ModuleSAs>, It is work around code.\r
1486 // \r
1487 primaryKey1 = UsageInstance.getPrimaryKey(modules.get(index).module.getModuleName(), \r
1488 translateSchemaStringToUUID(modules.get(index).module.getModuleGuid()),\r
1489 modules.get(index).module.getPackageName(), \r
1490 translateSchemaStringToUUID(modules.get(index).module.getPackageGuid()), \r
1491 modules.get(index).module.getArch().toString(),\r
1492 null);\r
1493 primaryKey2 = UsageInstance.getPrimaryKey(modules.get(index2).module.getModuleName(), \r
1494 translateSchemaStringToUUID(modules.get(index2).module.getModuleGuid()), \r
1495 modules.get(index2).module.getPackageName(), \r
1496 translateSchemaStringToUUID(modules.get(index2).module.getPackageGuid()), \r
1497 modules.get(index2).module.getArch().toString(), \r
1498 null);\r
1499 if (primaryKey1.equalsIgnoreCase(primaryKey2)) {\r
1500 isDuplicate = true;\r
1501 break;\r
1502 }\r
1503 }\r
878ddf1f 1504\r
8840ad58 1505 if (isDuplicate) {\r
1506 continue;\r
1507 }\r
878ddf1f 1508\r
6ff7a41c 1509 //\r
1510 // It is legal for a module does not contains ANY pcd build definitions.\r
1511 // \r
1512 if (modules.get(index).module.getPcdBuildDefinition() == null) {\r
8840ad58 1513 continue;\r
6ff7a41c 1514 }\r
1515 \r
1516 pcdBuildDataArray = modules.get(index).module.getPcdBuildDefinition().getPcdDataList();\r
1517\r
1518 moduleName = modules.get(index).module.getModuleName();\r
878ddf1f 1519\r
878ddf1f 1520 //\r
6ff7a41c 1521 // ----------------------------------------------------------------------\r
1522 // 2.1), Loop all Pcd entry for a module and add it into memory database.\r
1523 // ----------------------------------------------------------------------\r
8840ad58 1524 // \r
1525 for (pcdIndex = 0; pcdIndex < pcdBuildDataArray.size(); pcdIndex ++) {\r
1526 pcdBuildData = pcdBuildDataArray.get(pcdIndex);\r
1527 primaryKey = Token.getPrimaryKeyString(pcdBuildData.getCName(),\r
1528 translateSchemaStringToUUID(pcdBuildData.getTokenSpaceGuid()));\r
6ff7a41c 1529 pcdType = Token.getpcdTypeFromString(pcdBuildData.getItemType().toString());\r
1530 datumType = Token.getdatumTypeFromString(pcdBuildData.getDatumType().toString());\r
1531 tokenNumber = Integer.decode(pcdBuildData.getToken().toString());\r
1532 datum = pcdBuildData.getValue();\r
1533 maxDatumSize = pcdBuildData.getMaxDatumSize();\r
8840ad58 1534\r
6ff7a41c 1535 //\r
1536 // -------------------------------------------------------------------------------------------\r
1537 // 2.1.1), Do some necessary checking work for FixedAtBuild, FeatureFlag and PatchableInModule\r
1538 // -------------------------------------------------------------------------------------------\r
1539 // \r
1540 if (!Token.isDynamic(pcdType)) {\r
1541 //\r
1542 // Value is required.\r
1543 // \r
1544 if (datum == null) {\r
7db4ab70 1545 exceptionString = String.format("[FPD file error] There is no value for PCD entry %s in module %s!",\r
6ff7a41c 1546 pcdBuildData.getCName(),\r
1547 moduleName);\r
1548 throw new EntityException(exceptionString);\r
1549 }\r
1550\r
1551 //\r
1552 // Check whether the datum size is matched datum type.\r
1553 // \r
1554 if ((exceptionString = verifyDatumSize(pcdBuildData.getCName(), \r
1555 moduleName,\r
1556 maxDatumSize, \r
1557 datumType)) != null) {\r
1558 throw new EntityException(exceptionString);\r
1559 }\r
1560 }\r
8840ad58 1561\r
6ff7a41c 1562 //\r
1563 // ---------------------------------------------------------------------------------\r
1564 // 2.1.2), Create token or update token information for current anaylized PCD data.\r
1565 // ---------------------------------------------------------------------------------\r
1566 // \r
8840ad58 1567 if (dbManager.isTokenInDatabase(primaryKey)) {\r
1568 //\r
1569 // If the token is already exist in database, do some necessary checking\r
1570 // and add a usage instance into this token in database\r
1571 // \r
1572 token = dbManager.getTokenByKey(primaryKey);\r
6ff7a41c 1573 \r
1574 //\r
1575 // checking for DatumType, DatumType should be unique for one PCD used in different\r
1576 // modules.\r
1577 // \r
1578 if (token.datumType != datumType) {\r
7db4ab70 1579 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 1580 pcdBuildData.getCName(), \r
1581 pcdBuildData.getDatumType().toString(), \r
1582 Token.getStringOfdatumType(token.datumType));\r
1583 throw new EntityException(exceptionString);\r
1584 }\r
8840ad58 1585\r
878ddf1f 1586 //\r
6ff7a41c 1587 // Check token number is valid\r
8840ad58 1588 // \r
6ff7a41c 1589 if (tokenNumber != token.tokenNumber) {\r
7db4ab70 1590 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 1591 pcdBuildData.getCName(),\r
1592 moduleName);\r
8840ad58 1593 throw new EntityException(exceptionString);\r
1594 }\r
1595\r
878ddf1f 1596 //\r
6ff7a41c 1597 // For same PCD used in different modules, the PCD type should all be dynamic or non-dynamic.\r
8840ad58 1598 // \r
6ff7a41c 1599 if (token.isDynamicPCD != Token.isDynamic(pcdType)) {\r
7db4ab70 1600 exceptionString = String.format("[FPD file error] For PCD entry %s in module %s, you define dynamic or non-dynamic PCD type which"+\r
6ff7a41c 1601 "is different with others module's",\r
1602 token.cName,\r
1603 moduleName);\r
8840ad58 1604 throw new EntityException(exceptionString);\r
1605 }\r
6ff7a41c 1606\r
1607 if (token.isDynamicPCD) {\r
1608 //\r
1609 // Check datum is equal the datum in dynamic information.\r
1610 // For dynamic PCD, you can do not write <Value> in sperated every <PcdBuildDefinition> in different <ModuleSA>,\r
1611 // But if you write, the <Value> must be same as the value in <DynamicPcdBuildDefinitions>.\r
1612 // \r
1613 if (!token.isSkuEnable() && \r
7db4ab70 1614 (token.getDefaultSku().type == DynamicTokenValue.VALUE_TYPE.DEFAULT_TYPE) &&\r
1615 (datum != null)) {\r
1616 if (!datum.equalsIgnoreCase(token.getDefaultSku().value)) {\r
1617 exceptionString = String.format("[FPD file error] For dynamic PCD %s in module %s, the datum in <ModuleSA> is "+\r
6ff7a41c 1618 "not equal to the datum in <DynamicPcdBuildDefinitions>, it is "+\r
7db4ab70 1619 "illega! You could no set <Value> in <ModuleSA> for a dynamic PCD!",\r
6ff7a41c 1620 token.cName,\r
1621 moduleName);\r
7db4ab70 1622 throw new EntityException(exceptionString);\r
6ff7a41c 1623 }\r
1624 }\r
7db4ab70 1625\r
1626 if ((maxDatumSize != 0) &&\r
1627 (maxDatumSize != token.datumSize)){\r
1628 exceptionString = String.format("[FPD file error] For dynamic PCD %s in module %s, the max datum size is %d which "+\r
1629 "is different with <MaxDatumSize> %d defined in <DynamicPcdBuildDefinitions>!",\r
1630 token.cName,\r
1631 moduleName,\r
1632 maxDatumSize,\r
1633 token.datumSize);\r
1634 throw new EntityException(exceptionString);\r
1635 }\r
6ff7a41c 1636 }\r
1637 \r
8840ad58 1638 } else {\r
1639 //\r
1640 // If the token is not in database, create a new token instance and add\r
1641 // a usage instance into this token in database.\r
1642 // \r
1643 token = new Token(pcdBuildData.getCName(), \r
1644 translateSchemaStringToUUID(pcdBuildData.getTokenSpaceGuid()));\r
6ff7a41c 1645 \r
1646 token.datumType = datumType;\r
1647 token.tokenNumber = tokenNumber;\r
1648 token.isDynamicPCD = Token.isDynamic(pcdType);\r
1649 token.datumSize = maxDatumSize;\r
1650 \r
1651 if (token.isDynamicPCD) {\r
1652 //\r
1653 // For Dynamic and Dynamic Ex type, need find the dynamic information\r
1654 // in <DynamicPcdBuildDefinition> section in FPD file.\r
1655 // \r
1656 updateDynamicInformation(moduleName, \r
1657 token,\r
1658 datum,\r
1659 maxDatumSize);\r
8840ad58 1660 }\r
6ff7a41c 1661 \r
8840ad58 1662 dbManager.addTokenToDatabase(primaryKey, token);\r
878ddf1f 1663 }\r
878ddf1f 1664\r
878ddf1f 1665 //\r
6ff7a41c 1666 // -----------------------------------------------------------------------------------\r
1667 // 2.1.3), Add the PcdType in current module into this Pcd token's supported PCD type.\r
1668 // -----------------------------------------------------------------------------------\r
1669 // \r
1670 token.updateSupportPcdType(pcdType);\r
1671\r
1672 //\r
1673 // ------------------------------------------------\r
1674 // 2.1.4), Create an usage instance for this token.\r
1675 // ------------------------------------------------\r
8840ad58 1676 // \r
1677 usageInstance = new UsageInstance(token, \r
6ff7a41c 1678 moduleName, \r
8840ad58 1679 translateSchemaStringToUUID(modules.get(index).module.getModuleGuid()),\r
1680 modules.get(index).module.getPackageName(),\r
1681 translateSchemaStringToUUID(modules.get(index).module.getPackageGuid()),\r
1682 modules.get(index).type, \r
6ff7a41c 1683 pcdType,\r
8840ad58 1684 modules.get(index).module.getArch().toString(), \r
1685 null,\r
6ff7a41c 1686 datum,\r
1687 maxDatumSize);\r
8840ad58 1688 token.addUsageInstance(usageInstance);\r
878ddf1f 1689 }\r
878ddf1f 1690 }\r
878ddf1f 1691 }\r
1692\r
1693 /**\r
6ff7a41c 1694 Get dynamic information for a dynamic PCD from <DynamicPcdBuildDefinition> seciton in FPD file.\r
8840ad58 1695 \r
6ff7a41c 1696 This function should be implemented in GlobalData in future.\r
8840ad58 1697 \r
6ff7a41c 1698 @param token The token instance which has hold module's PCD information\r
1699 @param moduleName The name of module who will use this Dynamic PCD.\r
8840ad58 1700 \r
6ff7a41c 1701 @return DynamicPcdBuildDefinitions.PcdBuildData\r
1702 */\r
1703 /***/\r
1704 private DynamicPcdBuildDefinitions.PcdBuildData getDynamicInfoFromFPD(Token token,\r
1705 String moduleName)\r
878ddf1f 1706 throws EntityException {\r
6ff7a41c 1707 int index = 0;\r
1708 String exceptionString = null;\r
1709 String dynamicPrimaryKey = null;\r
1710 DynamicPcdBuildDefinitions dynamicPcdBuildDefinitions = null;\r
1711 List<DynamicPcdBuildDefinitions.PcdBuildData> dynamicPcdBuildDataArray = null;\r
878ddf1f 1712\r
3d52de13 1713 //\r
8840ad58 1714 // If FPD document is not be opened, open and initialize it.\r
1715 // \r
1716 if (fpdDocInstance == null) {\r
1717 try {\r
1718 fpdDocInstance = (FrameworkPlatformDescriptionDocument)XmlObject.Factory.parse(new File(fpdFilePath));\r
1719 } catch(IOException ioE) {\r
1720 throw new EntityException("File IO error for xml file:" + fpdFilePath + "\n" + ioE.getMessage());\r
1721 } catch(XmlException xmlE) {\r
1722 throw new EntityException("Can't parse the FPD xml fle:" + fpdFilePath + "\n" + xmlE.getMessage());\r
1723 }\r
3d52de13 1724 }\r
878ddf1f 1725\r
6ff7a41c 1726 dynamicPcdBuildDefinitions = fpdDocInstance.getFrameworkPlatformDescription().getDynamicPcdBuildDefinitions();\r
1727 if (dynamicPcdBuildDefinitions == null) {\r
7db4ab70 1728 exceptionString = String.format("[FPD file error] There are no <PcdDynamicBuildDescriptions> in FPD file but contains Dynamic type "+\r
6ff7a41c 1729 "PCD entry %s in module %s!",\r
1730 token.cName,\r
1731 moduleName);\r
1732 throw new EntityException(exceptionString);\r
3d52de13 1733 }\r
8840ad58 1734\r
6ff7a41c 1735 dynamicPcdBuildDataArray = dynamicPcdBuildDefinitions.getPcdBuildDataList();\r
1736 for (index = 0; index < dynamicPcdBuildDataArray.size(); index ++) {\r
1737 dynamicPrimaryKey = Token.getPrimaryKeyString(dynamicPcdBuildDataArray.get(index).getCName(),\r
1738 translateSchemaStringToUUID(dynamicPcdBuildDataArray.get(index).getTokenSpaceGuid()));\r
1739 if (dynamicPrimaryKey.equalsIgnoreCase(token.getPrimaryKeyString())) {\r
1740 return dynamicPcdBuildDataArray.get(index);\r
1741 }\r
8840ad58 1742 }\r
1743\r
6ff7a41c 1744 return null;\r
1745 }\r
1746\r
1747 /**\r
1748 Verify the maxDatumSize for a PCD data is matched to Datum type.\r
1749 \r
1750 @param token The token instance\r
1751 @param moduleName The module name who use this PCD data.\r
1752 @param maxDatumSize The value of max datum size in FPD file\r
1753 @param datumType The datum type\r
1754 \r
1755 @return String if is unmatched, set the exception information\r
1756 as return value, otherwice is null.\r
1757 **/\r
1758 private String verifyDatumSize(String cName, \r
1759 String moduleName,\r
1760 int maxDatumSize, \r
1761 Token.DATUM_TYPE datumType) {\r
1762 String exceptionString = null;\r
7db4ab70 1763\r
1764 if (maxDatumSize == 0) {\r
1765 exceptionString = String.format("[FPD file error] You maybe miss <MaxDatumSize> for PCD %s in module %s",\r
1766 cName,\r
1767 moduleName);\r
1768 return exceptionString;\r
1769 }\r
1770\r
6ff7a41c 1771 switch (datumType) {\r
1772 case UINT8:\r
1773 if (maxDatumSize != 1) {\r
7db4ab70 1774 exceptionString = String.format("[FPD file error] The datum type of PCD data %s in module %s "+\r
6ff7a41c 1775 "is UINT8, but datum size is %d, they are not matched!",\r
1776 cName,\r
1777 moduleName,\r
1778 maxDatumSize);\r
1779 }\r
1780 break;\r
1781 case UINT16:\r
1782 if (maxDatumSize != 2) {\r
7db4ab70 1783 exceptionString = String.format("[FPD file error] The datum type of PCD data %s in module %s "+\r
6ff7a41c 1784 "is UINT16, but datum size is %d, they are not matched!",\r
1785 cName,\r
1786 moduleName,\r
1787 maxDatumSize);\r
1788 }\r
1789 break;\r
1790 case UINT32:\r
1791 if (maxDatumSize != 4) {\r
7db4ab70 1792 exceptionString = String.format("[FPD file error] the datum type of PCD data %s in module %s "+\r
6ff7a41c 1793 "is UINT32, but datum size is %d, they are not matched!",\r
1794 cName,\r
1795 moduleName,\r
1796 maxDatumSize);\r
8840ad58 1797 }\r
6ff7a41c 1798 break;\r
1799 case UINT64:\r
1800 if (maxDatumSize != 8) {\r
7db4ab70 1801 exceptionString = String.format("[FPD file error] the datum type of PCD data %s in module %s "+\r
6ff7a41c 1802 "is UINT64, but datum size is %d, they are not matched!",\r
1803 cName,\r
1804 moduleName,\r
1805 maxDatumSize);\r
1806 }\r
1807 break;\r
1808 case BOOLEAN:\r
1809 if (maxDatumSize != 1) {\r
7db4ab70 1810 exceptionString = String.format("[FPD file error] the datum type of PCD data %s in module %s "+\r
6ff7a41c 1811 "is BOOLEAN, but datum size is %d, they are not matched!",\r
1812 cName,\r
1813 moduleName,\r
1814 maxDatumSize);\r
1815 }\r
1816 break;\r
1817 }\r
8840ad58 1818\r
6ff7a41c 1819 return exceptionString;\r
1820 }\r
8840ad58 1821\r
6ff7a41c 1822 /**\r
1823 Update dynamic information for PCD entry.\r
1824 \r
1825 Dynamic information is retrieved from <PcdDynamicBuildDeclarations> in\r
1826 FPD file.\r
1827 \r
1828 @param moduleName The name of the module who use this PCD\r
1829 @param token The token instance\r
1830 @param datum The <datum> in module's PCD information\r
1831 @param maxDatumSize The <maxDatumSize> in module's PCD information\r
1832 \r
1833 @return Token\r
1834 */\r
1835 private Token updateDynamicInformation(String moduleName, \r
1836 Token token,\r
1837 String datum,\r
1838 int maxDatumSize) \r
1839 throws EntityException {\r
1840 int index = 0;\r
1841 int offset;\r
1842 String exceptionString = null;\r
1843 DynamicTokenValue dynamicValue;\r
1844 SkuInstance skuInstance = null;\r
1845 String temp;\r
1846 boolean hasSkuId0 = false;\r
1847\r
1848 List<DynamicPcdBuildDefinitions.PcdBuildData.SkuInfo> skuInfoList = null;\r
1849 DynamicPcdBuildDefinitions.PcdBuildData dynamicInfo = null;\r
1850\r
1851 dynamicInfo = getDynamicInfoFromFPD(token, moduleName);\r
1852 if (dynamicInfo == null) {\r
7db4ab70 1853 exceptionString = String.format("[FPD file error] For Dynamic PCD %s used by module %s, "+\r
6ff7a41c 1854 "there is no dynamic information in <DynamicPcdBuildDefinitions> "+\r
1855 "in FPD file, but it is required!",\r
1856 token.cName,\r
1857 moduleName);\r
1858 throw new EntityException(exceptionString);\r
1859 }\r
878ddf1f 1860\r
7db4ab70 1861 token.datumSize = dynamicInfo.getMaxDatumSize();\r
1862\r
1863 exceptionString = verifyDatumSize(token.cName, moduleName, token.datumSize, token.datumType);\r
1864 if (exceptionString != null) {\r
1865 throw new EntityException(exceptionString);\r
1866 }\r
1867\r
1868 if ((maxDatumSize != 0) && \r
1869 (maxDatumSize != token.datumSize)) {\r
1870 exceptionString = String.format("FPD file error] For dynamic PCD %s, the datum size in module %s is %d, but "+\r
1871 "the datum size in <DynamicPcdBuildDefinitions> is %d, they are not match!",\r
1872 token.cName,\r
1873 moduleName, \r
1874 maxDatumSize,\r
1875 dynamicInfo.getMaxDatumSize());\r
1876 throw new EntityException(exceptionString);\r
1877 }\r
1878\r
6ff7a41c 1879 skuInfoList = dynamicInfo.getSkuInfoList();\r
878ddf1f 1880\r
6ff7a41c 1881 //\r
1882 // Loop all sku data \r
1883 // \r
1884 for (index = 0; index < skuInfoList.size(); index ++) {\r
1885 skuInstance = new SkuInstance();\r
1886 //\r
1887 // Although SkuId in schema is BigInteger, but in fact, sku id is 32 bit value.\r
1888 // \r
1889 temp = skuInfoList.get(index).getSkuId().toString();\r
1890 skuInstance.id = Integer.decode(temp);\r
6ff7a41c 1891 if (skuInstance.id == 0) {\r
1892 hasSkuId0 = true;\r
1893 }\r
1894 //\r
1895 // Judge whether is DefaultGroup at first, because most case is DefautlGroup.\r
1896 // \r
1897 if (skuInfoList.get(index).getValue() != null) {\r
1898 skuInstance.value.setValue(skuInfoList.get(index).getValue());\r
1899 token.skuData.add(skuInstance);\r
8840ad58 1900\r
878ddf1f 1901 //\r
6ff7a41c 1902 // Judege wether is same of datum between module's information\r
1903 // and dynamic information.\r
8840ad58 1904 // \r
6ff7a41c 1905 if (datum != null) {\r
1906 if ((skuInstance.id == 0) &&\r
1907 !datum.equalsIgnoreCase(skuInfoList.get(index).getValue())) {\r
7db4ab70 1908 exceptionString = "[FPD file error] For dynamic PCD " + token.cName + ", the value in module is " + datum.toString() + " but the "+\r
1909 "value of sku 0 data in <DynamicPcdBuildDefinition> is " + skuInstance.value.value + ". They are must be same!"+\r
1910 " or you could not define value for a dynamic PCD in every <ModuleSA>!"; \r
8840ad58 1911 throw new EntityException(exceptionString);\r
1912 }\r
6ff7a41c 1913 }\r
1914 continue;\r
1915 }\r
8840ad58 1916\r
6ff7a41c 1917 //\r
1918 // Judge whether is HII group case.\r
1919 // \r
1920 if (skuInfoList.get(index).getVariableName() != null) {\r
1921 exceptionString = null;\r
1922 if (skuInfoList.get(index).getVariableGuid() == null) {\r
7db4ab70 1923 exceptionString = String.format("[FPD file error] For dynamic PCD %s in <DynamicPcdBuildDefinitions> section in FPD "+\r
6ff7a41c 1924 "file, who use HII, but there is no <VariableGuid> defined for Sku %d data!",\r
1925 token.cName,\r
1926 index);\r
1927 \r
1928 }\r
1929\r
1930 if (skuInfoList.get(index).getVariableOffset() == null) {\r
7db4ab70 1931 exceptionString = String.format("[FPD file error] For dynamic PCD %s in <DynamicPcdBuildDefinitions> section in FPD "+\r
6ff7a41c 1932 "file, who use HII, but there is no <VariableOffset> defined for Sku %d data!",\r
1933 token.cName,\r
1934 index);\r
1935 }\r
1936\r
1937 if (skuInfoList.get(index).getHiiDefaultValue() == null) {\r
7db4ab70 1938 exceptionString = String.format("[FPD file error] For dynamic PCD %s in <DynamicPcdBuildDefinitions> section in FPD "+\r
6ff7a41c 1939 "file, who use HII, but there is no <HiiDefaultValue> defined for Sku %d data!",\r
1940 token.cName,\r
1941 index);\r
878ddf1f 1942 }\r
6ff7a41c 1943\r
1944 if (exceptionString != null) {\r
1945 throw new EntityException(exceptionString);\r
1946 }\r
1947 offset = Integer.decode(skuInfoList.get(index).getVariableOffset());\r
1948 if (offset > 0xFFFF) {\r
7db4ab70 1949 throw new EntityException(String.format("[FPD file error] For dynamic PCD %s , the variable offset defined in sku %d data "+\r
6ff7a41c 1950 "exceed 64K, it is not allowed!",\r
1951 token.cName,\r
1952 index));\r
1953 }\r
1954\r
1955 skuInstance.value.setHiiData(skuInfoList.get(index).getVariableName(),\r
1956 translateSchemaStringToUUID(skuInfoList.get(index).getVariableGuid().toString()),\r
1957 skuInfoList.get(index).getVariableOffset(),\r
1958 skuInfoList.get(index).getHiiDefaultValue());\r
1959 token.skuData.add(skuInstance);\r
1960 continue;\r
878ddf1f 1961 }\r
6ff7a41c 1962\r
1963 if (skuInfoList.get(index).getVpdOffset() != null) {\r
1964 skuInstance.value.setVpdData(skuInfoList.get(index).getVpdOffset());\r
1965 token.skuData.add(skuInstance);\r
1966 continue;\r
1967 }\r
1968\r
7db4ab70 1969 exceptionString = String.format("[FPD file error] For dynamic PCD %s, the dynamic info must "+\r
6ff7a41c 1970 "be one of 'DefaultGroup', 'HIIGroup', 'VpdGroup'.",\r
1971 token.cName);\r
8840ad58 1972 throw new EntityException(exceptionString);\r
1973 }\r
1974\r
6ff7a41c 1975 if (!hasSkuId0) {\r
7db4ab70 1976 exceptionString = String.format("[FPD file error] For dynamic PCD %s in <DynamicPcdBuildDefinitions>, there are "+\r
6ff7a41c 1977 "no sku id = 0 data, which is required for every dynamic PCD",\r
1978 token.cName);\r
7db4ab70 1979 throw new EntityException(exceptionString);\r
6ff7a41c 1980 }\r
1981\r
8840ad58 1982 return token;\r
1983 }\r
1984\r
1985 /**\r
1986 Translate the schema string to UUID instance.\r
1987 \r
1988 In schema, the string of UUID is defined as following two types string:\r
1989 1) GuidArrayType: pattern = 0x[a-fA-F0-9]{1,8},( )*0x[a-fA-F0-9]{1,4},(\r
1990 )*0x[a-fA-F0-9]{1,4}(,( )*\{)?(,?( )*0x[a-fA-F0-9]{1,2}){8}( )*(\})?\r
1991 \r
1992 2) GuidNamingConvention: pattern =\r
1993 [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
1994 \r
1995 This function will convert string and create uuid instance.\r
1996 \r
1997 @param uuidString UUID string in XML file\r
1998 \r
1999 @return UUID UUID instance\r
2000 **/\r
2001 private UUID translateSchemaStringToUUID(String uuidString) \r
2002 throws EntityException {\r
2003 String temp;\r
2004 String[] splitStringArray;\r
2005 int index;\r
2006 int chIndex;\r
2007 int chLen;\r
2008\r
2009 if (uuidString == null) {\r
2010 return null;\r
2011 }\r
2012\r
2013 if (uuidString.length() == 0) {\r
2014 return null;\r
2015 }\r
2016\r
6ff7a41c 2017 if (uuidString.equals("0") ||\r
2018 uuidString.equalsIgnoreCase("0x0")) {\r
2019 return new UUID(0, 0);\r
2020 }\r
2021\r
8840ad58 2022 //\r
2023 // If the UUID schema string is GuidArrayType type then need translate \r
2024 // to GuidNamingConvention type at first.\r
2025 // \r
2026 if ((uuidString.charAt(0) == '0') && ((uuidString.charAt(1) == 'x') || (uuidString.charAt(1) == 'X'))) {\r
2027 splitStringArray = uuidString.split("," );\r
2028 if (splitStringArray.length != 11) {\r
7db4ab70 2029 throw new EntityException ("[FPD file error] Wrong format for UUID string: " + uuidString);\r
8840ad58 2030 }\r
2031\r
2032 //\r
2033 // Remove blank space from these string and remove header string "0x"\r
2034 // \r
2035 for (index = 0; index < 11; index ++) {\r
2036 splitStringArray[index] = splitStringArray[index].trim();\r
2037 splitStringArray[index] = splitStringArray[index].substring(2, splitStringArray[index].length());\r
2038 }\r
2039\r
2040 //\r
2041 // Add heading '0' to normalize the string length\r
2042 // \r
2043 for (index = 3; index < 11; index ++) {\r
2044 chLen = splitStringArray[index].length();\r
2045 for (chIndex = 0; chIndex < 2 - chLen; chIndex ++) {\r
2046 splitStringArray[index] = "0" + splitStringArray[index];\r
2047 }\r
2048 }\r
2049\r
2050 //\r
2051 // construct the final GuidNamingConvention string\r
2052 // \r
2053 temp = String.format("%s-%s-%s-%s%s-%s%s%s%s%s%s",\r
2054 splitStringArray[0],\r
2055 splitStringArray[1],\r
2056 splitStringArray[2],\r
2057 splitStringArray[3],\r
2058 splitStringArray[4],\r
2059 splitStringArray[5],\r
2060 splitStringArray[6],\r
2061 splitStringArray[7],\r
2062 splitStringArray[8],\r
2063 splitStringArray[9],\r
2064 splitStringArray[10]);\r
2065 uuidString = temp;\r
2066 }\r
2067\r
2068 return UUID.fromString(uuidString);\r
878ddf1f 2069 }\r
2070\r
2071 /**\r
2072 check parameter for this action.\r
2073 \r
2074 @throws EntityException Bad parameter.\r
2075 **/\r
2076 private void checkParameter() throws EntityException {\r
2077 File file = null;\r
2078\r
2079 if((fpdFilePath == null) ||(workspacePath == null)) {\r
2080 throw new EntityException("WorkspacePath and FPDFileName should be blank for CollectPCDAtion!");\r
2081 }\r
2082\r
2083 if(fpdFilePath.length() == 0 || workspacePath.length() == 0) {\r
2084 throw new EntityException("WorkspacePath and FPDFileName should be blank for CollectPCDAtion!");\r
2085 }\r
2086\r
2087 file = new File(workspacePath);\r
2088 if(!file.exists()) {\r
2089 throw new EntityException("WorkpacePath " + workspacePath + " does not exist!");\r
2090 }\r
2091\r
2092 file = new File(fpdFilePath);\r
2093\r
2094 if(!file.exists()) {\r
2095 throw new EntityException("FPD File " + fpdFilePath + " does not exist!");\r
2096 }\r
2097 }\r
2098\r
2099 /**\r
2100 Test case function\r
2101\r
2102 @param argv parameter from command line\r
2103 **/\r
2104 public static void main(String argv[]) throws EntityException {\r
2105 CollectPCDAction ca = new CollectPCDAction();\r
6ff7a41c 2106 ca.setWorkspacePath("m:/tianocore/edk2");\r
2107 ca.setFPDFilePath("m:/tianocore/edk2/EdkNt32Pkg/Nt32.fpd");\r
878ddf1f 2108 ca.setActionMessageLevel(ActionMessage.MAX_MESSAGE_LEVEL);\r
2109 GlobalData.initInfo("Tools" + File.separator + "Conf" + File.separator + "FrameworkDatabase.db",\r
6ff7a41c 2110 "m:/tianocore/edk2");\r
878ddf1f 2111 ca.execute();\r
2112 }\r
2113}\r