2 PlatformPcdPreprocessActionForBuilding class.
4 This action class is to collect PCD information from MSA, SPD, FPD xml file.
5 This class will be used for wizard and build tools, So it can *not* inherit
6 from buildAction or wizardAction.
8 Copyright (c) 2006, Intel Corporation
9 All rights reserved. This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 package org
.tianocore
.build
.pcd
.action
;
21 import java
.io
.IOException
;
22 import java
.math
.BigInteger
;
23 import java
.util
.ArrayList
;
24 import java
.util
.Iterator
;
25 import java
.util
.List
;
28 import org
.apache
.xmlbeans
.XmlException
;
29 import org
.apache
.xmlbeans
.XmlObject
;
30 import org
.tianocore
.DynamicPcdBuildDefinitionsDocument
.DynamicPcdBuildDefinitions
;
31 import org
.tianocore
.PlatformSurfaceAreaDocument
;
32 import org
.tianocore
.build
.fpd
.FpdParserTask
;
33 import org
.tianocore
.build
.global
.GlobalData
;
34 import org
.tianocore
.build
.id
.FpdModuleIdentification
;
35 import org
.tianocore
.pcd
.action
.ActionMessage
;
36 import org
.tianocore
.pcd
.entity
.ModulePcdInfoFromFpd
;
37 import org
.tianocore
.pcd
.entity
.MemoryDatabaseManager
;
38 import org
.tianocore
.pcd
.entity
.Token
;
39 import org
.tianocore
.pcd
.entity
.UsageIdentification
;
40 import org
.tianocore
.pcd
.exception
.EntityException
;
41 import org
.tianocore
.pcd
.action
.PlatformPcdPreprocessAction
;
44 This action class is to collect PCD information from MSA, SPD, FPD xml file.
45 This class will be used for wizard and build tools, So it can *not* inherit
46 from buildAction or UIAction.
48 public class PlatformPcdPreprocessActionForBuilding
extends PlatformPcdPreprocessAction
{
50 /// Workspacepath hold the workspace information.
52 private String workspacePath
;
55 /// FPD file is the root file.
57 private String fpdFilePath
;
60 /// Message level for CollectPCDAction.
62 private int originalMessageLevel
;
65 /// Cache the fpd docment instance for private usage.
67 private PlatformSurfaceAreaDocument fpdDocInstance
;
70 Set WorkspacePath parameter for this action class.
72 @param workspacePath parameter for this action
74 public void setWorkspacePath(String workspacePath
) {
75 this.workspacePath
= workspacePath
;
79 Set action message level for CollectPcdAction tool.
81 The message should be restored when this action exit.
83 @param actionMessageLevel parameter for this action
85 public void setActionMessageLevel(int actionMessageLevel
) {
86 originalMessageLevel
= ActionMessage
.messageLevel
;
87 ActionMessage
.messageLevel
= actionMessageLevel
;
91 Set FPDFileName parameter for this action class.
93 @param fpdFilePath fpd file path
95 public void setFPDFilePath(String fpdFilePath
) {
96 this.fpdFilePath
= fpdFilePath
;
100 Common function interface for outer.
102 @param workspacePath The path of workspace of current build or analysis.
103 @param fpdFilePath The fpd file path of current build or analysis.
104 @param messageLevel The message level for this Action.
106 @throws Exception The exception of this function. Because it can *not* be predict
107 where the action class will be used. So only Exception can be throw.
110 public void perform(String workspacePath
, String fpdFilePath
,
111 int messageLevel
) throws Exception
{
112 setWorkspacePath(workspacePath
);
113 setFPDFilePath(fpdFilePath
);
114 setActionMessageLevel(messageLevel
);
117 ActionMessage
.messageLevel
= originalMessageLevel
;
121 Core execution function for this action class.
123 This function work flows will be:
124 1) Collect and prepocess PCD information from FPD file, all PCD
125 information will be stored into memory database.
126 2) Generate 3 strings for
127 a) All modules using Dynamic(Ex) PCD entry.(Token Number)
128 b) PEI PCDDatabase (C Structure) for PCD Service PEIM.
129 c) DXE PCD Database (C structure) for PCD Service DXE.
132 @throws EntityException Exception indicate failed to execute this action.
135 public void execute() throws EntityException
{
136 MemoryDatabaseManager pcdDbManager
= null;
139 // Get memoryDatabaseManager instance from GlobalData.
140 // The memoryDatabaseManager should be initialized for whatever build
141 // tools or wizard tools
143 if((pcdDbManager
= GlobalData
.getPCDMemoryDBManager()) == null) {
144 throw new EntityException("The instance of PCD memory database manager is null");
147 this.setPcdDbManager(pcdDbManager
);
150 // Collect all PCD information defined in FPD file.
151 // Evenry token defind in FPD will be created as an token into
154 initPcdMemoryDbWithPlatformInfo();
157 // Generate for PEI, DXE PCD DATABASE's definition and initialization.
159 genPcdDatabaseSourceCode ();
164 Override function: implementate the method of get Guid string information from SPD file.
166 @param guidCName Guid CName string.
168 @return String[] Guid information from SPD file.
170 public String
[] getGuidInfoFromSpd(String guidCName
) throws EntityException
{
171 String
[] tokenSpaceStrRet
= null;
173 tokenSpaceStrRet
= GlobalData
.getGuidInfoFromCname(guidCName
);
174 } catch ( Exception e
) {
175 throw new EntityException ("Failed get Guid CName " + guidCName
+ "from SPD file!");
177 return tokenSpaceStrRet
;
181 This function generates source code for PCD Database.
184 @throws EntityException If the token does *not* exist in memory database.
187 private void genPcdDatabaseSourceCode()
188 throws EntityException
{
189 String PcdCommonHeaderString
= PcdDatabase
.getPcdDatabaseCommonDefinitions();
191 ArrayList
<Token
> alPei
= new ArrayList
<Token
> ();
192 ArrayList
<Token
> alDxe
= new ArrayList
<Token
> ();
194 getPcdDbManager().getTwoPhaseDynamicRecordArray(alPei
, alDxe
);
195 PcdDatabase pcdPeiDatabase
= new PcdDatabase (alPei
, "PEI", 0);
196 pcdPeiDatabase
.genCode();
197 MemoryDatabaseManager
.PcdPeimHString
= PcdCommonHeaderString
+ pcdPeiDatabase
.getHString() +
198 PcdDatabase
.getPcdPeiDatabaseDefinitions();
199 MemoryDatabaseManager
.PcdPeimCString
= pcdPeiDatabase
.getCString();
201 PcdDatabase pcdDxeDatabase
= new PcdDatabase(alDxe
, "DXE", alPei
.size());
202 pcdDxeDatabase
.genCode();
203 MemoryDatabaseManager
.PcdDxeHString
= MemoryDatabaseManager
.PcdPeimHString
+ pcdDxeDatabase
.getHString() +
204 PcdDatabase
.getPcdDxeDatabaseDefinitions();
205 MemoryDatabaseManager
.PcdDxeCString
= pcdDxeDatabase
.getCString();
209 Override function: Get component array from FPD.
211 This function maybe provided by some Global class.
213 @return List<ModuleInfo> the component array.
216 public List
<ModulePcdInfoFromFpd
> getComponentsFromFpd()
217 throws EntityException
{
218 List
<ModulePcdInfoFromFpd
> allModules
= new ArrayList
<ModulePcdInfoFromFpd
>();
219 Map
<FpdModuleIdentification
, XmlObject
> pcdBuildDefinitions
= null;
220 UsageIdentification usageId
= null;
222 pcdBuildDefinitions
= GlobalData
.getFpdPcdBuildDefinitions();
223 if (pcdBuildDefinitions
== null) {
228 // Loop map to retrieve all PCD build definition and Module id
230 Iterator item
= pcdBuildDefinitions
.keySet().iterator();
231 while (item
.hasNext()){
232 FpdModuleIdentification id
= (FpdModuleIdentification
) item
.next();
233 usageId
= new UsageIdentification(id
.getModule().getName(),
234 id
.getModule().getGuid(),
235 id
.getModule().getPackage().getName(),
236 id
.getModule().getPackage().getGuid(),
238 id
.getModule().getVersion(),
239 id
.getModule().getModuleType());
240 allModules
.add(new ModulePcdInfoFromFpd(usageId
, pcdBuildDefinitions
.get(id
)));
246 Override function: Verify the datum value according its datum size and datum type, this
247 function maybe moved to FPD verification tools in future.
258 public String
verifyDatum(String cName
,
261 Token
.DATUM_TYPE datumType
,
263 String exceptionString
= null;
269 if (moduleName
== null) {
270 moduleName
= "section <DynamicPcdBuildDefinitions>";
272 moduleName
= "module " + moduleName
;
275 if (maxDatumSize
== 0) {
276 exceptionString
= String
.format("[FPD file error] You maybe miss <MaxDatumSize> for PCD %s in %s",
279 return exceptionString
;
284 if (maxDatumSize
!= 1) {
285 exceptionString
= String
.format("[FPD file error] The datum type of PCD data %s in %s "+
286 "is UINT8, but datum size is %d, they are not matched!",
290 return exceptionString
;
295 value
= Integer
.decode(datum
);
296 } catch (NumberFormatException nfeExp
) {
297 exceptionString
= String
.format("[FPD file error] The datum for PCD %s in %s is not valid "+
298 "digital format of UINT8",
301 return exceptionString
;
304 exceptionString
= String
.format("[FPD file error] The datum for PCD %s in %s is %s exceed"+
305 " the max size of UINT8 - 0xFF",
309 return exceptionString
;
314 if (maxDatumSize
!= 2) {
315 exceptionString
= String
.format("[FPD file error] The datum type of PCD data %s in %s "+
316 "is UINT16, but datum size is %d, they are not matched!",
320 return exceptionString
;
324 value
= Integer
.decode(datum
);
325 } catch (NumberFormatException nfeExp
) {
326 exceptionString
= String
.format("[FPD file error] The datum for PCD %s in %s is "+
327 "not valid digital of UINT16",
330 return exceptionString
;
332 if (value
> 0xFFFF) {
333 exceptionString
= String
.format("[FPD file error] The datum for PCD %s in %s is %s "+
334 "which exceed the range of UINT16 - 0xFFFF",
338 return exceptionString
;
343 if (maxDatumSize
!= 4) {
344 exceptionString
= String
.format("[FPD file error] The datum type of PCD data %s in %s "+
345 "is UINT32, but datum size is %d, they are not matched!",
349 return exceptionString
;
354 if (datum
.length() > 2) {
355 if ((datum
.charAt(0) == '0') &&
356 ((datum
.charAt(1) == 'x') || (datum
.charAt(1) == 'X'))){
357 subStr
= datum
.substring(2, datum
.length());
358 value64
= new BigInteger(subStr
, 16);
360 value64
= new BigInteger(datum
);
363 value64
= new BigInteger(datum
);
365 } catch (NumberFormatException nfeExp
) {
366 exceptionString
= String
.format("[FPD file error] The datum for PCD %s in %s is not "+
367 "valid digital of UINT32",
370 return exceptionString
;
373 if (value64
.bitLength() > 32) {
374 exceptionString
= String
.format("[FPD file error] The datum for PCD %s in %s is %s which "+
375 "exceed the range of UINT32 - 0xFFFFFFFF",
379 return exceptionString
;
384 if (maxDatumSize
!= 8) {
385 exceptionString
= String
.format("[FPD file error] The datum type of PCD data %s in %s "+
386 "is UINT64, but datum size is %d, they are not matched!",
390 return exceptionString
;
395 if (datum
.length() > 2) {
396 if ((datum
.charAt(0) == '0') &&
397 ((datum
.charAt(1) == 'x') || (datum
.charAt(1) == 'X'))){
398 subStr
= datum
.substring(2, datum
.length());
399 value64
= new BigInteger(subStr
, 16);
401 value64
= new BigInteger(datum
);
404 value64
= new BigInteger(datum
);
406 } catch (NumberFormatException nfeExp
) {
407 exceptionString
= String
.format("[FPD file error] The datum for PCD %s in %s is not valid"+
408 " digital of UINT64",
411 return exceptionString
;
414 if (value64
.bitLength() > 64) {
415 exceptionString
= String
.format("[FPD file error] The datum for PCD %s in %s is %s "+
416 "exceed the range of UINT64 - 0xFFFFFFFFFFFFFFFF",
420 return exceptionString
;
425 if (maxDatumSize
!= 1) {
426 exceptionString
= String
.format("[FPD file error] The datum type of PCD data %s in %s "+
427 "is BOOLEAN, but datum size is %d, they are not matched!",
431 return exceptionString
;
435 if (!(datum
.equalsIgnoreCase("TRUE") ||
436 datum
.equalsIgnoreCase("FALSE"))) {
437 exceptionString
= String
.format("[FPD file error] The datum type of PCD data %s in %s "+
438 "is BOOELAN, but value is not 'true'/'TRUE' or 'FALSE'/'false'",
441 return exceptionString
;
451 char ch
= datum
.charAt(0);
455 // For void* type PCD, only three datum is support:
456 // 1) Unicode: string with start char is "L"
457 // 2) Ansci: String start char is ""
458 // 3) byte array: String start char "{"
461 start
= datum
.indexOf('\"');
462 end
= datum
.lastIndexOf('\"');
464 (end
> datum
.length())||
465 ((start
== end
) && (datum
.length() > 0))) {
466 exceptionString
= String
.format("[FPD file error] The datum type of PCD %s in %s is VOID* and datum is "+
467 "a UNICODE string because start with L\", but format maybe"+
468 "is not right, correct UNICODE string is L\"...\"!",
471 return exceptionString
;
474 strValue
= datum
.substring(start
+ 1, end
);
475 if ((strValue
.length() * 2) > maxDatumSize
) {
476 exceptionString
= String
.format("[FPD file error] The datum type of PCD %s in %s is VOID*, and datum is "+
477 "a UNICODE string, but the datum size is %d exceed to <MaxDatumSize> : %d",
480 strValue
.length() * 2,
482 return exceptionString
;
484 } else if (ch
== '\"'){
485 start
= datum
.indexOf('\"');
486 end
= datum
.lastIndexOf('\"');
488 (end
> datum
.length())||
489 ((start
== end
) && (datum
.length() > 0))) {
490 exceptionString
= String
.format("[FPD file error] The datum type of PCD %s in %s is VOID* and datum is "+
491 "a ANSCII string because start with \", but format maybe"+
492 "is not right, correct ANSIC string is \"...\"!",
495 return exceptionString
;
497 strValue
= datum
.substring(start
+ 1, end
);
498 if ((strValue
.length()) > maxDatumSize
) {
499 exceptionString
= String
.format("[FPD file error] The datum type of PCD %s in %s is VOID*, and datum is "+
500 "a ANSCI string, but the datum size is %d which exceed to <MaxDatumSize> : %d",
505 return exceptionString
;
507 } else if (ch
=='{') {
508 String
[] strValueArray
;
510 start
= datum
.indexOf('{');
511 end
= datum
.lastIndexOf('}');
512 strValue
= datum
.substring(start
+ 1, end
);
513 strValue
= strValue
.trim();
514 if (strValue
.length() == 0) {
515 exceptionString
= String
.format ("[FPD file error] The datum type of PCD %s in %s is VOID*, and "+
516 "it is byte array in fact, but '{}' is not valid for NULL datam but"+
520 return exceptionString
;
522 strValueArray
= strValue
.split(",");
523 for (index
= 0; index
< strValueArray
.length
; index
++) {
525 value
= Integer
.decode(strValueArray
[index
].trim());
526 } catch (NumberFormatException nfeEx
) {
527 exceptionString
= String
.format("[FPD file error] The datum type of PCD %s in %s is VOID*, and "+
528 "it is byte array in fact. For every byte in array should be a valid"+
529 "byte digital, but element %s is not a valid byte digital!",
532 strValueArray
[index
]);
533 return exceptionString
;
536 exceptionString
= String
.format("[FPD file error] The datum type of PCD %s in %s is VOID*, "+
537 "it is byte array in fact. But the element of %s exceed the byte range",
540 strValueArray
[index
]);
541 return exceptionString
;
545 if (strValueArray
.length
> maxDatumSize
) {
546 exceptionString
= String
.format("[FPD file error] The datum type of PCD %s in %s is VOID*, and datum is byte"+
547 "array, but the number of bytes is %d which exceed to <MaxDatumSzie> : %d!",
550 strValueArray
.length
,
552 return exceptionString
;
555 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 "+
556 "1) UNICODE string: like L\"xxxx\";\r\n"+
557 "2) ANSIC string: like \"xxx\";\r\n"+
558 "3) Byte array: like {0x2, 0x45, 0x23}\r\n"+
559 "But the datum in seems does not following above format!",
562 return exceptionString
;
566 exceptionString
= String
.format("[FPD file error] For PCD entry %s in %s, datum type is unknown, it should be one of "+
567 "UINT8, UINT16, UINT32, UINT64, VOID*, BOOLEAN",
570 return exceptionString
;
576 Override function: Get dynamic information for a dynamic PCD from <DynamicPcdBuildDefinition> seciton in FPD file.
578 This function should be implemented in GlobalData in future.
580 @param token The token instance which has hold module's PCD information
581 @param moduleName The name of module who will use this Dynamic PCD.
583 @return DynamicPcdBuildDefinitions.PcdBuildData
585 public DynamicPcdBuildDefinitions
.PcdBuildData
getDynamicInfoFromFpd(Token token
,
587 throws EntityException
{
589 String exceptionString
= null;
590 String dynamicPrimaryKey
= null;
591 DynamicPcdBuildDefinitions dynamicPcdBuildDefinitions
= null;
592 List
<DynamicPcdBuildDefinitions
.PcdBuildData
> dynamicPcdBuildDataArray
= null;
593 String
[] tokenSpaceStrRet
= null;
596 // If FPD document is not be opened, open and initialize it.
597 // BUGBUG: The code should be moved into GlobalData in future.
599 if (fpdDocInstance
== null) {
601 fpdDocInstance
= (PlatformSurfaceAreaDocument
)XmlObject
.Factory
.parse(new File(fpdFilePath
));
602 } catch(IOException ioE
) {
603 throw new EntityException("File IO error for xml file:" + fpdFilePath
+ "\n" + ioE
.getMessage());
604 } catch(XmlException xmlE
) {
605 throw new EntityException("Can't parse the FPD xml fle:" + fpdFilePath
+ "\n" + xmlE
.getMessage());
609 dynamicPcdBuildDefinitions
= fpdDocInstance
.getPlatformSurfaceArea().getDynamicPcdBuildDefinitions();
610 if (dynamicPcdBuildDefinitions
== null) {
611 exceptionString
= String
.format("[FPD file error] There are no <PcdDynamicBuildDescriptions> in FPD file but contains Dynamic type "+
612 "PCD entry %s in module %s!",
615 throw new EntityException(exceptionString
);
618 dynamicPcdBuildDataArray
= dynamicPcdBuildDefinitions
.getPcdBuildDataList();
619 for (index
= 0; index
< dynamicPcdBuildDataArray
.size(); index
++) {
621 tokenSpaceStrRet
= GlobalData
.getGuidInfoFromCname(dynamicPcdBuildDataArray
.get(index
).getTokenSpaceGuidCName());
622 } catch (Exception e
) {
623 throw new EntityException ("Fail to get token space guid for token " + dynamicPcdBuildDataArray
.get(index
).getCName());
626 if (tokenSpaceStrRet
== null) {
627 throw new EntityException ("Fail to get token space guid for token " + dynamicPcdBuildDataArray
.get(index
).getCName());
630 dynamicPrimaryKey
= Token
.getPrimaryKeyString(dynamicPcdBuildDataArray
.get(index
).getCName(),
631 tokenSpaceStrRet
[1]);
632 if (dynamicPrimaryKey
.equalsIgnoreCase(token
.getPrimaryKeyString())) {
633 return dynamicPcdBuildDataArray
.get(index
);
641 Override function: get all <DynamicPcdBuildDefinition> from FPD file.
643 @return List<DynamicPcdBuildDefinitions.PcdBuildData>
645 public List
<DynamicPcdBuildDefinitions
.PcdBuildData
>
646 getAllDynamicPcdInfoFromFpd()
647 throws EntityException
{
648 DynamicPcdBuildDefinitions dynamicPcdBuildDefinitions
= null;
651 // Open fpd document to get <DynamicPcdBuildDefinition> Section.
652 // BUGBUG: the function should be move GlobalData in furture.
654 if (fpdDocInstance
== null) {
656 fpdDocInstance
= (PlatformSurfaceAreaDocument
)XmlObject
.Factory
.parse(new File(fpdFilePath
));
657 } catch(IOException ioE
) {
658 throw new EntityException("File IO error for xml file:" + fpdFilePath
+ "\n" + ioE
.getMessage());
659 } catch(XmlException xmlE
) {
660 throw new EntityException("Can't parse the FPD xml fle:" + fpdFilePath
+ "\n" + xmlE
.getMessage());
664 dynamicPcdBuildDefinitions
= fpdDocInstance
.getPlatformSurfaceArea().getDynamicPcdBuildDefinitions();
665 if (dynamicPcdBuildDefinitions
== null) {
669 return dynamicPcdBuildDefinitions
.getPcdBuildDataList();
673 check parameter for this action.
675 @throws EntityException Bad parameter.
677 private void checkParameter() throws EntityException
{
680 if((fpdFilePath
== null) ||(workspacePath
== null)) {
681 throw new EntityException("WorkspacePath and FPDFileName should be blank for CollectPCDAtion!");
684 if(fpdFilePath
.length() == 0 || workspacePath
.length() == 0) {
685 throw new EntityException("WorkspacePath and FPDFileName should be blank for CollectPCDAtion!");
688 file
= new File(workspacePath
);
690 throw new EntityException("WorkpacePath " + workspacePath
+ " does not exist!");
693 file
= new File(fpdFilePath
);
696 throw new EntityException("FPD File " + fpdFilePath
+ " does not exist!");
703 @param argv parameter from command line
705 public static void main(String argv
[]) throws EntityException
{
706 PlatformPcdPreprocessActionForBuilding ca
= new PlatformPcdPreprocessActionForBuilding();
707 String projectDir
= "x:/edk2";
708 ca
.setWorkspacePath(projectDir
);
709 ca
.setFPDFilePath(projectDir
+ "/EdkNt32Pkg/Nt32.fpd");
710 ca
.setActionMessageLevel(ActionMessage
.MAX_MESSAGE_LEVEL
);
711 GlobalData
.initInfo("Tools" + File
.separator
+ "Conf" + File
.separator
+ "FrameworkDatabase.db",
714 System
.out
.println("After initInfo!");
715 FpdParserTask fpt
= new FpdParserTask();
716 fpt
.parseFpdFile(new File(projectDir
+ "/EdkNt32Pkg/Nt32.fpd"));