2 CollectPCDAction 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
.util
.ArrayList
;
23 import java
.util
.HashMap
;
24 import java
.util
.List
;
26 import java
.util
.UUID
;
28 import org
.apache
.xmlbeans
.XmlException
;
29 import org
.apache
.xmlbeans
.XmlObject
;
30 import org
.tianocore
.FrameworkPlatformDescriptionDocument
;
31 import org
.tianocore
.ModuleSADocument
;
32 import org
.tianocore
.PackageSurfaceAreaDocument
;
33 import org
.tianocore
.PcdBuildDeclarationsDocument
.PcdBuildDeclarations
.PcdBuildData
;
34 import org
.tianocore
.PcdDefinitionsDocument
.PcdDefinitions
;
35 import org
.tianocore
.build
.autogen
.CommonDefinition
;
36 import org
.tianocore
.build
.global
.GlobalData
;
37 import org
.tianocore
.build
.global
.SurfaceAreaQuery
;
38 import org
.tianocore
.build
.pcd
.action
.ActionMessage
;
39 import org
.tianocore
.build
.pcd
.entity
.MemoryDatabaseManager
;
40 import org
.tianocore
.build
.pcd
.entity
.SkuInstance
;
41 import org
.tianocore
.build
.pcd
.entity
.Token
;
42 import org
.tianocore
.build
.pcd
.entity
.UsageInstance
;
43 import org
.tianocore
.build
.pcd
.exception
.EntityException
;
45 /** This action class is to collect PCD information from MSA, SPD, FPD xml file.
46 This class will be used for wizard and build tools, So it can *not* inherit
47 from buildAction or UIAction.
49 public class CollectPCDAction
{
50 /// memoryDatabase hold all PCD information collected from SPD, MSA, FPD.
51 private MemoryDatabaseManager dbManager
;
53 /// Workspacepath hold the workspace information.
54 private String workspacePath
;
56 /// FPD file is the root file.
57 private String fpdFilePath
;
59 /// Message level for CollectPCDAction.
60 private int originalMessageLevel
;
63 Set WorkspacePath parameter for this action class.
65 @param workspacePath parameter for this action
67 public void setWorkspacePath(String workspacePath
) {
68 this.workspacePath
= workspacePath
;
72 Set action message level for CollectPcdAction tool.
74 The message should be restored when this action exit.
76 @param actionMessageLevel parameter for this action
78 public void setActionMessageLevel(int actionMessageLevel
) {
79 originalMessageLevel
= ActionMessage
.messageLevel
;
80 ActionMessage
.messageLevel
= actionMessageLevel
;
84 Set FPDFileName parameter for this action class.
86 @param fpdFilePath fpd file path
88 public void setFPDFilePath(String fpdFilePath
) {
89 this.fpdFilePath
= fpdFilePath
;
93 Common function interface for outer.
95 @param workspacePath The path of workspace of current build or analysis.
96 @param fpdFilePath The fpd file path of current build or analysis.
97 @param messageLevel The message level for this Action.
99 @throws Exception The exception of this function. Because it can *not* be predict
100 where the action class will be used. So only Exception can be throw.
103 public void perform(String workspacePath
, String fpdFilePath
,
104 int messageLevel
) throws Exception
{
105 setWorkspacePath(workspacePath
);
106 setFPDFilePath(fpdFilePath
);
107 setActionMessageLevel(messageLevel
);
110 ActionMessage
.messageLevel
= originalMessageLevel
;
114 Core execution function for this action class.
116 This function work flows will be:
117 1) Get all token's platform information from FPD, and create token object into memory database.
118 2) Get all token's module information from MSA, and create usage instance for every module's PCD entry.
119 3) Get all token's inherited information from MSA's library, and create usage instance
120 for module who consume this library and create usage instance for library for building.
121 4) Collect token's package information from SPD, update these information for token in memory
124 @throws EntityException Exception indicate failed to execute this action.
127 private void execute() throws EntityException
{
128 FrameworkPlatformDescriptionDocument fpdDoc
= null;
129 Object
[][] modulePCDArray
= null;
130 Map
<String
, XmlObject
> docMap
= null;
131 ModuleSADocument
.ModuleSA
[] moduleSAs
= null;
132 UsageInstance usageInstance
= null;
133 String packageName
= null;
134 String packageFullPath
= null;
136 int libraryIndex
= 0;
137 int pcdArrayIndex
= 0;
138 List
<String
> listLibraryInstance
= null;
139 String componentTypeStr
= null;
142 // Collect all PCD information defined in FPD file.
143 // Evenry token defind in FPD will be created as an token into
146 fpdDoc
= createTokenInDBFromFPD();
149 // Searching MSA and SPD document.
150 // The information of MSA will be used to create usage instance into database.
151 // The information of SPD will be used to update the token information in database.
154 HashMap
<String
, XmlObject
> map
= new HashMap
<String
, XmlObject
>();
155 map
.put("FrameworkPlatformDescription", fpdDoc
);
156 SurfaceAreaQuery
.setDoc(map
);
158 moduleSAs
= SurfaceAreaQuery
.getFpdModules();
159 for(index
= 0; index
< moduleSAs
.length
; index
++) {
161 // Get module document and use SurfaceAreaQuery to get PCD information
163 docMap
= GlobalData
.getDoc(moduleSAs
[index
].getModuleName());
164 SurfaceAreaQuery
.setDoc(docMap
);
165 modulePCDArray
= SurfaceAreaQuery
.getModulePCDTokenArray();
166 componentTypeStr
= SurfaceAreaQuery
.getComponentType();
168 GlobalData
.getPackageNameForModule(moduleSAs
[index
].getModuleName());
169 packageFullPath
= this.workspacePath
+ File
.separator
+
170 GlobalData
.getPackagePath(packageName
) +
171 packageName
+ ".spd";
173 if(modulePCDArray
!= null) {
175 // If current MSA contains <PCDs> information, then create usage
176 // instance for PCD information from MSA
178 for(pcdArrayIndex
= 0; pcdArrayIndex
< modulePCDArray
.length
;
181 createUsageInstanceFromMSA(moduleSAs
[index
].getModuleName(),
182 modulePCDArray
[pcdArrayIndex
]);
184 if(usageInstance
== null) {
188 // Get remaining PCD information from the package which this module belongs to
190 updateTokenBySPD(usageInstance
, packageFullPath
);
195 // Get inherit PCD information which inherit from library instance of this module.
197 listLibraryInstance
=
198 SurfaceAreaQuery
.getLibraryInstance(moduleSAs
[index
].getArch().toString(),
199 CommonDefinition
.AlwaysConsumed
);
200 if(listLibraryInstance
!= null) {
201 for(libraryIndex
= 0; libraryIndex
< listLibraryInstance
.size();
203 inheritPCDFromLibraryInstance(listLibraryInstance
.get(libraryIndex
),
204 moduleSAs
[index
].getModuleName(),
213 This function will collect inherit PCD information from library for a module.
215 This function will create two usage instance for inherited PCD token, one is
216 for module and another is for library.
217 For module, if it inherited a PCD token from library, this PCD token's value
218 should be instanced in module level, and belongs to module.
219 For library, it also need a usage instance for build.
221 @param libraryName The name of library instance.
222 @param moduleName The name of module.
223 @param packageName The name of package while module belongs to.
224 @param parentcomponentType The component type of module.
226 @throws EntityException If the token does *not* exist in memory database.
229 private void inheritPCDFromLibraryInstance(String libraryName
,
232 String parentcomponentType
)
233 throws EntityException
{
234 Map
<String
, XmlObject
> docMap
= null;
235 String primaryKeyString
= null;
236 Object
[][] libPcdDataArray
= null;
237 UUID nullUUID
= new UUID(0,0);
238 UUID platformUUID
= nullUUID
;
239 UUID tokenSpaceGuid
= null;
242 Token
.PCD_TYPE pcdType
= Token
.PCD_TYPE
.UNKNOWN
;
243 UsageInstance usageInstance
= null;
244 String packageFullPath
= null;
247 // Query PCD information from library's document.
249 docMap
= GlobalData
.getDoc(libraryName
);
250 SurfaceAreaQuery
.setDoc(docMap
);
251 libPcdDataArray
= SurfaceAreaQuery
.getModulePCDTokenArray();
253 if(libPcdDataArray
== null) {
257 for(tokenIndex
= 0; tokenIndex
< libPcdDataArray
.length
; tokenIndex
++) {
258 tokenSpaceGuid
=((UUID
)libPcdDataArray
[tokenIndex
][2] == null) ?
259 nullUUID
:(UUID
)libPcdDataArray
[tokenIndex
][2];
262 // Get token from memory database. The token must be created from FPD already.
264 primaryKeyString
= Token
.getPrimaryKeyString((String
)libPcdDataArray
[tokenIndex
][0],
269 if(dbManager
.isTokenInDatabase(primaryKeyString
)) {
270 token
= dbManager
.getTokenByKey(primaryKeyString
);
272 throw new EntityException("The PCD token " + primaryKeyString
+
273 " defined in module " + moduleName
+
274 " does not exist in FPD file!");
278 // Create usage instance for module.
280 pcdType
= Token
.getpcdTypeFromString((String
)libPcdDataArray
[tokenIndex
][1]);
281 usageInstance
= new UsageInstance(token
,
282 Token
.PCD_USAGE
.ALWAYS_CONSUMED
,
284 CommonDefinition
.getComponentType(parentcomponentType
),
285 libPcdDataArray
[tokenIndex
][3],
287 (String
) libPcdDataArray
[tokenIndex
][5],
292 if(Token
.PCD_USAGE
.UNKNOWN
== token
.isUsageInstanceExist(moduleName
)) {
293 token
.addUsageInstance(usageInstance
);
295 packageFullPath
= this.workspacePath
+ File
.separator
+
296 GlobalData
.getPackagePath(packageName
) +
297 packageName
+ ".spd";
298 updateTokenBySPD(usageInstance
, packageFullPath
);
302 // We need create second usage instance for inherited case, which
303 // add library as an usage instance, because when build a module, and
304 // if module inherited from base library, then build process will build
307 if(Token
.PCD_USAGE
.UNKNOWN
== token
.isUsageInstanceExist(libraryName
)) {
308 packageName
= GlobalData
.getPackageNameForModule(libraryName
);
309 usageInstance
= new UsageInstance(token
,
310 Token
.PCD_USAGE
.ALWAYS_CONSUMED
,
312 CommonDefinition
.ComponentTypeLibrary
,
313 libPcdDataArray
[tokenIndex
][3],
315 (String
)libPcdDataArray
[tokenIndex
][5],
320 token
.addUsageInstance(usageInstance
);
326 Create usage instance for PCD token defined in MSA document
328 A PCD token maybe used by many modules, and every module is one of usage
329 instance of this token. For ALWAY_CONSUMED, SOMETIMES_CONSUMED, it is
330 consumer type usage instance of this token, and for ALWAYS_PRODUCED,
331 SOMETIMES_PRODUCED, it is produce type usage instance.
333 @param moduleName The name of module
334 @param tokenInfoInMsa The PCD token information array retrieved from MSA.
336 @return UsageInstance The usage instance created in memroy database.
338 @throws EntityException If token did not exist in database yet.
341 private UsageInstance
createUsageInstanceFromMSA(String moduleName
,
342 Object
[] tokenInfoInMsa
)
343 throws EntityException
{
344 String packageName
= null;
345 UsageInstance usageInstance
= null;
346 UUID tokenSpaceGuid
= null;
347 UUID nullUUID
= new UUID(0,0);
348 String primaryKeyString
= null;
349 UUID platformTokenSpace
= nullUUID
;
351 Token
.PCD_TYPE pcdType
= Token
.PCD_TYPE
.UNKNOWN
;
352 Token
.PCD_USAGE pcdUsage
= Token
.PCD_USAGE
.UNKNOWN
;
354 tokenSpaceGuid
=((UUID
)tokenInfoInMsa
[2] == null) ? nullUUID
:(UUID
)tokenInfoInMsa
[2];
356 primaryKeyString
= Token
.getPrimaryKeyString((String
)tokenInfoInMsa
[0],
361 // Get token object from memory database firstly.
363 if(dbManager
.isTokenInDatabase(primaryKeyString
)) {
364 token
= dbManager
.getTokenByKey(primaryKeyString
);
366 throw new EntityException("The PCD token " + primaryKeyString
+ " defined in module " +
367 moduleName
+ " does not exist in FPD file!" );
369 pcdType
= Token
.getpcdTypeFromString((String
)tokenInfoInMsa
[1]);
370 pcdUsage
= Token
.getUsageFromString((String
)tokenInfoInMsa
[4]);
372 packageName
= GlobalData
.getPackageNameForModule(moduleName
);
374 if(Token
.PCD_USAGE
.UNKNOWN
!= token
.isUsageInstanceExist(moduleName
)) {
376 // BUGBUG: It is legal that same base name exist in one FPD file. In furture
377 // we should use "Guid, Version, Package" and "Arch" to differ a module.
378 // So currently, warning should be disabled.
380 //ActionMessage.warning(this,
381 // "In module " + moduleName + " exist more than one PCD token " + token.cName
387 // BUGBUG: following code could be enabled at current schema. Because
388 // current schema does not provide usage information.
390 // For FEATRURE_FLAG, FIXED_AT_BUILD, PATCH_IN_MODULE type PCD token, his
391 // usage is always ALWAYS_CONSUMED
393 //if((pcdType != Token.PCD_TYPE.DYNAMIC) &&
394 // (pcdType != Token.PCD_TYPE.DYNAMIC_EX)) {
395 pcdUsage
= Token
.PCD_USAGE
.ALWAYS_CONSUMED
;
398 usageInstance
= new UsageInstance(token
,
401 CommonDefinition
.getComponentType(SurfaceAreaQuery
.getComponentType()),
404 (String
) tokenInfoInMsa
[5],
411 // Use default value defined in MSA to update datum of token,
412 // if datum of token does not defined in FPD file.
414 if((token
.datum
== null) &&(tokenInfoInMsa
[3] != null)) {
415 token
.datum
= tokenInfoInMsa
[3];
418 token
.addUsageInstance(usageInstance
);
420 return usageInstance
;
424 Create token instance object into memory database, the token information
425 comes for FPD file. Normally, FPD file will contain all token platform
428 This fucntion should be executed at firsly before others collection work
429 such as searching token information from MSA, SPD.
431 @return FrameworkPlatformDescriptionDocument The FPD document instance for furture usage.
433 @throws EntityException Failed to parse FPD xml file.
436 private FrameworkPlatformDescriptionDocument
createTokenInDBFromFPD()
437 throws EntityException
{
438 XmlObject doc
= null;
439 FrameworkPlatformDescriptionDocument fpdDoc
= null;
441 List
<PcdBuildData
> pcdBuildDataArray
= new ArrayList
<PcdBuildData
>();
442 PcdBuildData pcdBuildData
= null;
444 UUID nullUUID
= new UUID(0,0);
445 UUID platformTokenSpace
= nullUUID
;
446 List skuDataArray
= new ArrayList();
447 SkuInstance skuInstance
= null;
451 // Get all tokens from FPD file and create token into database.
455 doc
= XmlObject
.Factory
.parse(new File(fpdFilePath
));
456 } catch(IOException ioE
) {
457 throw new EntityException("Can't find the FPD xml fle:" + fpdFilePath
);
458 } catch(XmlException xmlE
) {
459 throw new EntityException("Can't parse the FPD xml fle:" + fpdFilePath
);
463 // Get memoryDatabaseManager instance from GlobalData.
465 if((dbManager
= GlobalData
.getPCDMemoryDBManager()) == null) {
466 throw new EntityException("The instance of PCD memory database manager is null");
469 dbManager
= new MemoryDatabaseManager();
471 if(!(doc
instanceof FrameworkPlatformDescriptionDocument
)) {
472 throw new EntityException("File " + fpdFilePath
+
473 " is not a FrameworkPlatformDescriptionDocument");
476 fpdDoc
=(FrameworkPlatformDescriptionDocument
)doc
;
479 // Add all tokens in FPD into Memory Database.
482 fpdDoc
.getFrameworkPlatformDescription().getPcdBuildDeclarations().getPcdBuildDataList();
484 index
< fpdDoc
.getFrameworkPlatformDescription().getPcdBuildDeclarations().sizeOfPcdBuildDataArray();
486 pcdBuildData
= pcdBuildDataArray
.get(index
);
487 token
= new Token(pcdBuildData
.getCName(), new UUID(0, 0), new UUID(0, 0));
489 // BUGBUG: in FPD, <defaultValue> should be defined as <Value>
491 token
.datum
= pcdBuildData
.getDefaultValue();
492 token
.tokenNumber
= Integer
.decode(pcdBuildData
.getToken().getStringValue());
493 token
.hiiEnabled
= pcdBuildData
.getHiiEnable();
494 token
.variableGuid
= Token
.getGUIDFromSchemaObject(pcdBuildData
.getVariableGuid());
495 token
.variableName
= pcdBuildData
.getVariableName();
496 token
.variableOffset
= Integer
.decode(pcdBuildData
.getDataOffset());
497 token
.skuEnabled
= pcdBuildData
.getSkuEnable();
498 token
.maxSkuCount
= Integer
.decode(pcdBuildData
.getMaxSku());
499 token
.skuId
= Integer
.decode(pcdBuildData
.getSkuId());
500 token
.skuDataArrayEnabled
= pcdBuildData
.getSkuDataArrayEnable();
501 token
.assignedtokenNumber
= Integer
.decode(pcdBuildData
.getToken().getStringValue());
502 skuDataArray
= pcdBuildData
.getSkuDataArray1();
503 token
.datumType
= Token
.getdatumTypeFromString(pcdBuildData
.getDatumType().toString());
505 if(skuDataArray
!= null) {
506 for(skuIndex
= 0; skuIndex
< skuDataArray
.size(); skuIndex
++) {
508 // BUGBUG: Now in current schema, The value is defined as String type,
509 // it is not correct, the type should be same as the datumType
511 skuInstance
= new SkuInstance(((PcdBuildData
.SkuData
)skuDataArray
.get(skuIndex
)).getId(),
512 ((PcdBuildData
.SkuData
)skuDataArray
.get(skuIndex
)).getValue());
513 token
.skuData
.add(skuInstance
);
517 if(dbManager
.isTokenInDatabase(Token
.getPrimaryKeyString(token
.cName
,
518 token
.tokenSpaceName
,
519 platformTokenSpace
))) {
521 // If found duplicate token, Should tool be hold?
523 ActionMessage
.warning(this,
524 "Token " + token
.cName
+ " exists in token database");
527 token
.pcdType
= Token
.getpcdTypeFromString(pcdBuildData
.getItemType().toString());
528 dbManager
.addTokenToDatabase(Token
.getPrimaryKeyString(token
.cName
,
529 token
.tokenSpaceName
,
538 Update PCD token in memory database by help information in SPD.
540 After create token from FPD and create usage instance from MSA, we should collect
541 PCD package level information from SPD and update token information in memory
544 @param usageInstance The usage instance defined in MSA and want to search in SPD.
545 @param packageFullPath The SPD file path.
547 @throws EntityException Failed to parse SPD xml file.
550 private void updateTokenBySPD(UsageInstance usageInstance
,
551 String packageFullPath
)
552 throws EntityException
{
553 PackageSurfaceAreaDocument pkgDoc
= null;
554 PcdDefinitions pcdDefinitions
= null;
555 List
<PcdDefinitions
.PcdEntry
> pcdEntryArray
= new ArrayList
<PcdDefinitions
.PcdEntry
>();
557 boolean isFoundInSpd
= false;
558 Token
.DATUM_TYPE datumType
= Token
.DATUM_TYPE
.UNKNOWN
;
561 pkgDoc
=(PackageSurfaceAreaDocument
)XmlObject
.Factory
.parse(new File(packageFullPath
));
562 } catch(IOException ioE
) {
563 throw new EntityException("Can't find the FPD xml fle:" + packageFullPath
);
564 } catch(XmlException xmlE
) {
565 throw new EntityException("Can't parse the FPD xml fle:" + packageFullPath
);
567 pcdDefinitions
= pkgDoc
.getPackageSurfaceArea().getPcdDefinitions();
569 // It is illege for SPD file does not contains any PCD information.
571 if (pcdDefinitions
== null) {
575 pcdEntryArray
= pcdDefinitions
.getPcdEntryList();
576 if (pcdEntryArray
== null) {
579 for(index
= 0; index
< pcdEntryArray
.size(); index
++) {
580 if(pcdEntryArray
.get(index
).getCName().equalsIgnoreCase(
581 usageInstance
.parentToken
.cName
)) {
584 // From SPD file , we can get following information.
585 // Token: Token number defined in package level.
586 // PcdItemType: This item does not single one. It means all supported item type.
587 // datumType: UINT8, UNIT16, UNIT32, UINT64, VOID*, BOOLEAN
588 // datumSize: The size of default value or maxmine size.
589 // defaultValue: This value is defined in package level.
590 // HelpText: The help text is provided in package level.
593 usageInstance
.parentToken
.tokenNumber
= Integer
.decode(pcdEntryArray
.get(index
).getToken());
595 if(pcdEntryArray
.get(index
).getDatumType() != null) {
596 datumType
= Token
.getdatumTypeFromString(
597 pcdEntryArray
.get(index
).getDatumType().toString());
598 if(usageInstance
.parentToken
.datumType
== Token
.DATUM_TYPE
.UNKNOWN
) {
599 usageInstance
.parentToken
.datumType
= datumType
;
601 if(datumType
!= usageInstance
.parentToken
.datumType
) {
602 throw new EntityException("Different datum types are defined for Token :" +
603 usageInstance
.parentToken
.cName
);
608 throw new EntityException("The datum type for token " + usageInstance
.parentToken
.cName
+
609 " is not defind in SPD file " + packageFullPath
);
612 usageInstance
.defaultValueInSPD
= pcdEntryArray
.get(index
).getDefaultValue();
613 usageInstance
.helpTextInSPD
= "Help Text in SPD";
616 // If token's datum is not valid, it indicate that datum is not provided
617 // in FPD and defaultValue is not provided in MSA, then use defaultValue
618 // in SPD as the datum of token.
620 if(usageInstance
.parentToken
.datum
== null) {
621 if(pcdEntryArray
.get(index
).getDefaultValue() != null) {
622 usageInstance
.parentToken
.datum
= pcdEntryArray
.get(index
).getDefaultValue();
624 throw new EntityException("FPD does not provide datum for token " + usageInstance
.parentToken
.cName
+
625 ", MSA and SPD also does not provide <defaultValue> for this token!");
633 check parameter for this action.
635 @throws EntityException Bad parameter.
637 private void checkParameter() throws EntityException
{
640 if((fpdFilePath
== null) ||(workspacePath
== null)) {
641 throw new EntityException("WorkspacePath and FPDFileName should be blank for CollectPCDAtion!");
644 if(fpdFilePath
.length() == 0 || workspacePath
.length() == 0) {
645 throw new EntityException("WorkspacePath and FPDFileName should be blank for CollectPCDAtion!");
648 file
= new File(workspacePath
);
650 throw new EntityException("WorkpacePath " + workspacePath
+ " does not exist!");
653 file
= new File(fpdFilePath
);
656 throw new EntityException("FPD File " + fpdFilePath
+ " does not exist!");
663 @param argv parameter from command line
665 public static void main(String argv
[]) throws EntityException
{
666 CollectPCDAction ca
= new CollectPCDAction();
667 ca
.setWorkspacePath("G:/mdk");
668 ca
.setFPDFilePath("G:/mdk/EdkNt32Pkg/build/Nt32.fpd");
669 ca
.setActionMessageLevel(ActionMessage
.MAX_MESSAGE_LEVEL
);
670 GlobalData
.initInfo("Tools" + File
.separator
+ "Conf" + File
.separator
+ "FrameworkDatabase.db",