Fix the bug that using hardcode driver name to judge whether current driver is PCD...
[mirror_edk2.git] / Tools / Source / PcdTools / org / tianocore / pcd / entity / UsageInstance.java
CommitLineData
878ddf1f 1/** @file\r
2 UsageInstance class.\r
3\r
4 This class indicate an usage instance for a PCD token. This instance maybe a module\r
5 or platform setting. When a module produce or cosume a PCD token, then this module\r
6 is an usage instance for this PCD token.\r
bc262841 7\r
878ddf1f 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
bc262841 13\r
878ddf1f 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
bc262841 17**/\r
d14ebb43 18package org.tianocore.pcd.entity;\r
878ddf1f 19\r
d14ebb43 20import org.tianocore.pcd.entity.CommonDefinition;\r
21import org.tianocore.pcd.entity.UsageIdentification;\r
878ddf1f 22\r
23/**\r
24 This class indicate an usage instance for a PCD token. This instance maybe a module\r
25 or platform setting. When a module produce or cosume a PCD token, then this module\r
26 is an usage instance for this PCD token.\r
27**/\r
28public class UsageInstance {\r
38ee8d9e 29 ///\r
30 /// This parent that this usage instance belongs to.\r
31 ///\r
eece174a 32 public Token parentToken;\r
38ee8d9e 33\r
34 ///\r
eece174a 35 /// ModuleIdentification for Usage Instance\r
bc262841 36 ///\r
d14ebb43 37 public UsageIdentification usageId;\r
38ee8d9e 38\r
39 ///\r
eece174a 40 /// Arch also is a key for a UsageInstance\r
bc262841 41 ///\r
eece174a 42 public String arch;\r
38ee8d9e 43\r
44 ///\r
bc262841 45 /// The PCD type defined for module\r
46 ///\r
eece174a 47 public Token.PCD_TYPE modulePcdType;\r
38ee8d9e 48\r
49 ///\r
bc262841 50 /// The value of the PCD in this usage instance.\r
51 ///\r
eece174a 52 public String datum;\r
38ee8d9e 53\r
54 ///\r
55 /// The maxDatumSize could be different for same PCD in different module\r
56 /// But this case is allow for FeatureFlag, FixedAtBuild, PatchableInModule\r
57 /// type.\r
bc262841 58 ///\r
eece174a 59 public int maxDatumSize;\r
38ee8d9e 60\r
61 ///\r
62 /// Autogen string for header file.\r
63 ///\r
eece174a 64 public String hAutogenStr;\r
38ee8d9e 65\r
66 ///\r
67 /// Auotgen string for C code file.\r
bc262841 68 ///\r
eece174a 69 public String cAutogenStr;\r
38ee8d9e 70\r
71 /**\r
eece174a 72 Constructure function for UsageInstance\r
bc262841 73\r
eece174a 74 @param parentToken The token instance for this usgaInstance\r
f28c0830 75 @param usageId The identification for usage instance\r
eece174a 76 @param modulePcdType The PCD type for this usage instance\r
77 @param value The value of this PCD in this usage instance\r
78 @param maxDatumSize The max datum size of this PCD in this usage\r
79 instance.\r
80 **/\r
81 public UsageInstance(Token parentToken,\r
d14ebb43 82 UsageIdentification usageId,\r
eece174a 83 Token.PCD_TYPE modulePcdType,\r
eece174a 84 String value,\r
85 int maxDatumSize) {\r
38ee8d9e 86 this.parentToken = parentToken;\r
d14ebb43 87 this.usageId = usageId;\r
38ee8d9e 88 this.modulePcdType = modulePcdType;\r
38ee8d9e 89 this.datum = value;\r
90 this.maxDatumSize = maxDatumSize;\r
91 }\r
92\r
93 /**\r
94 Get the primary key for usage instance array for every token.\r
bc262841 95\r
d14ebb43 96 @param usageId The identification of UsageInstance\r
bc262841 97\r
eece174a 98 @retval String The primary key for this usage instance\r
99 **/\r
d14ebb43 100 public static String getPrimaryKey(UsageIdentification usageId) {\r
101 return usageId.toString();\r
38ee8d9e 102 }\r
103\r
104 /**\r
105 Get primary key string for this usage instance\r
bc262841 106\r
38ee8d9e 107 @return String primary key string\r
108 **/\r
109 public String getPrimaryKey() {\r
d14ebb43 110 return UsageInstance.getPrimaryKey(usageId);\r
38ee8d9e 111 }\r
112\r
113 /**\r
114 Judget whether current module is PEI driver\r
bc262841 115\r
eece174a 116 @return boolean whether current module is PEI driver\r
117 **/\r
38ee8d9e 118 public boolean isPeiPhaseComponent() {\r
d14ebb43 119 int moduleType = CommonDefinition.getModuleType(usageId.moduleType);\r
eece174a 120\r
136adffc 121 if ((moduleType == CommonDefinition.ModuleTypePeiCore) ||\r
122 (moduleType == CommonDefinition.ModuleTypePeim)) {\r
38ee8d9e 123 return true;\r
124 }\r
125 return false;\r
126 }\r
127\r
128 /**\r
eece174a 129 Judge whether current module is DXE driver.\r
bc262841 130\r
eece174a 131 @return boolean whether current module is DXE driver\r
132 **/\r
133 public boolean isDxePhaseComponent() {\r
d14ebb43 134 int moduleType = CommonDefinition.getModuleType(usageId.moduleType);\r
eece174a 135\r
136 if ((moduleType == CommonDefinition.ModuleTypeDxeDriver) ||\r
137 (moduleType == CommonDefinition.ModuleTypeDxeRuntimeDriver) ||\r
138 (moduleType == CommonDefinition.ModuleTypeDxeSalDriver) ||\r
139 (moduleType == CommonDefinition.ModuleTypeDxeSmmDriver) ||\r
140 (moduleType == CommonDefinition.ModuleTypeUefiDriver) ||\r
141 (moduleType == CommonDefinition.ModuleTypeUefiApplication)\r
142 ) {\r
143 return true;\r
144 }\r
145 return false;\r
146 }\r
147\r
148 /**\r
149 Generate autogen string for header file and C code file.\r
bc262841 150\r
38ee8d9e 151 @param isBuildUsedLibrary whether the autogen is for library.\r
eece174a 152 **/\r
153 public void generateAutoGen(boolean isBuildUsedLibrary) {\r
51da9e80 154 String guidStringCName = null;\r
155 boolean isByteArray = false;\r
156 String printDatum = null;\r
157 String tokenNumberString = null;\r
38ee8d9e 158\r
159 hAutogenStr = "";\r
160 cAutogenStr = "";\r
161\r
162 if (this.modulePcdType == Token.PCD_TYPE.DYNAMIC_EX) {\r
eece174a 163 //\r
164 // For DYNAMIC_EX type PCD, use original token number in SPD or FPD to generate autogen\r
bc262841 165 //\r
51da9e80 166 tokenNumberString = Long.toString(parentToken.dynamicExTokenNumber, 16);\r
38ee8d9e 167 } else {\r
eece174a 168 //\r
169 // For Others type PCD, use autogenerated token number to generate autogen\r
bc262841 170 //\r
51da9e80 171 tokenNumberString = Long.toString(parentToken.tokenNumber, 16);\r
38ee8d9e 172 }\r
173\r
eece174a 174 hAutogenStr += String.format("#define _PCD_TOKEN_%s 0x%s\r\n", parentToken.cName, tokenNumberString);\r
175\r
176 //\r
177 // Judge the value of this PCD is byte array type\r
bc262841 178 //\r
2435723a 179 if (!isBuildUsedLibrary && !parentToken.isDynamicPCD) {\r
180 if (datum.trim().charAt(0) == '{') {\r
181 isByteArray = true;\r
182 }\r
183 }\r
184\r
eece174a 185 //\r
186 // "ULL" should be added to value's tail for UINT64 value\r
bc262841 187 //\r
1cf9cdcc 188 if (parentToken.datumType == Token.DATUM_TYPE.UINT64) {\r
189 printDatum = this.datum + "ULL";\r
190 } else {\r
191 printDatum = this.datum;\r
192 }\r
193\r
38ee8d9e 194 switch (modulePcdType) {\r
195 case FEATURE_FLAG:\r
bc262841 196 hAutogenStr += String.format("extern const BOOLEAN _gPcd_FixedAtBuild_%s;\r\n",\r
16d3e132 197 parentToken.cName);\r
57a38e69 198 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s _gPcd_FixedAtBuild_%s\r\n",\r
bc262841 199 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),\r
16d3e132 200 parentToken.cName,\r
201 parentToken.cName);\r
57a38e69 202 hAutogenStr += String.format("//#define _PCD_SET_MODE_%s_%s ASSERT(FALSE) If is not allowed to set value for a FEATURE_FLAG PCD\r\n",\r
bc262841 203 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),\r
57a38e69 204 parentToken.cName);\r
16d3e132 205\r
206 if (!isBuildUsedLibrary) {\r
bc262841 207 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",\r
208 parentToken.cName,\r
1cf9cdcc 209 printDatum);\r
38ee8d9e 210 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const BOOLEAN _gPcd_FixedAtBuild_%s = _PCD_VALUE_%s;\r\n",\r
211 parentToken.cName,\r
212 parentToken.cName);\r
38ee8d9e 213 }\r
214 break;\r
215 case FIXED_AT_BUILD:\r
16d3e132 216 if (isByteArray) {\r
217 hAutogenStr += String.format("extern const UINT8 _gPcd_FixedAtBuild_%s[];\r\n",\r
218 parentToken.cName);\r
bc262841 219 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s (VOID*)_gPcd_FixedAtBuild_%s\r\n",\r
16d3e132 220 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),\r
221 parentToken.cName,\r
222 parentToken.cName);\r
223 } else {\r
38ee8d9e 224 hAutogenStr += String.format("extern const %s _gPcd_FixedAtBuild_%s;\r\n",\r
225 Token.getAutogendatumTypeString(parentToken.datumType),\r
226 parentToken.cName);\r
bc262841 227 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s _gPcd_FixedAtBuild_%s\r\n",\r
38ee8d9e 228 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),\r
229 parentToken.cName,\r
230 parentToken.cName);\r
16d3e132 231 }\r
232\r
57a38e69 233 hAutogenStr += String.format("//#define _PCD_SET_MODE_%s_%s ASSERT(FALSE) // It is not allowed to set value for a FIXED_AT_BUILD PCD\r\n",\r
bc262841 234 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),\r
57a38e69 235 parentToken.cName);\r
16d3e132 236 if (!isBuildUsedLibrary) {\r
9efa2da0 237 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {\r
238 if (isByteArray) {\r
bc262841 239 hAutogenStr += String.format("#define _PCD_VALUE_%s (VOID*)_gPcd_FixedAtBuild_%s\r\n",\r
240 parentToken.cName,\r
9efa2da0 241 parentToken.cName);\r
242 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gPcd_FixedAtBuild_%s[] = %s;\r\n",\r
243 parentToken.cName,\r
244 printDatum);\r
245 } else {\r
bc262841 246 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",\r
247 parentToken.cName,\r
9efa2da0 248 printDatum);\r
249 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const %s _gPcd_FixedAtBuild_%s = _PCD_VALUE_%s;\r\n",\r
250 Token.getAutogendatumTypeString(parentToken.datumType),\r
251 parentToken.cName,\r
252 parentToken.cName);\r
253 }\r
2435723a 254 } else {\r
bc262841 255 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",\r
256 parentToken.cName,\r
9efa2da0 257 printDatum);\r
2435723a 258 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const %s _gPcd_FixedAtBuild_%s = _PCD_VALUE_%s;\r\n",\r
259 Token.getAutogendatumTypeString(parentToken.datumType),\r
260 parentToken.cName,\r
261 parentToken.cName);\r
2435723a 262 }\r
38ee8d9e 263 }\r
264 break;\r
265 case PATCHABLE_IN_MODULE:\r
16d3e132 266 if (isByteArray) {\r
267 hAutogenStr += String.format("extern UINT8 _gPcd_BinaryPatch_%s[];\r\n",\r
268 parentToken.cName);\r
57a38e69 269 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s (VOID*)_gPcd_BinaryPatch_%s\r\n",\r
16d3e132 270 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),\r
271 parentToken.cName,\r
bc262841 272 parentToken.cName);\r
16d3e132 273 } else {\r
38ee8d9e 274 hAutogenStr += String.format("extern %s _gPcd_BinaryPatch_%s;\r\n",\r
275 Token.getAutogendatumTypeString(parentToken.datumType),\r
276 parentToken.cName);\r
57a38e69 277 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s _gPcd_BinaryPatch_%s\r\n",\r
38ee8d9e 278 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),\r
279 parentToken.cName,\r
bc262841 280 parentToken.cName);\r
16d3e132 281 }\r
282\r
57a38e69 283 //\r
284 // Generate _PCD_SET_MODE_xx macro for using set BinaryPatch value via PcdSet macro\r
bc262841 285 //\r
57a38e69 286 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {\r
287 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(SizeOfBuffer, Buffer) CopyMem (_gPcd_BinaryPatch_%s, (Buffer), (SizeOfBuffer))\r\n",\r
288 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),\r
289 parentToken.cName,\r
290 parentToken.cName);\r
291 } else {\r
292 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(Value) (_gPcd_BinaryPatch_%s = (Value))\r\n",\r
293 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),\r
294 parentToken.cName,\r
295 parentToken.cName);\r
296 }\r
bc262841 297\r
16d3e132 298 if (!isBuildUsedLibrary) {\r
bc262841 299 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",\r
300 parentToken.cName,\r
1cf9cdcc 301 printDatum);\r
2435723a 302 if (isByteArray) {\r
303 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED UINT8 _gPcd_BinaryPatch_%s[] = _PCD_VALUE_%s;\r\n",\r
304 parentToken.cName,\r
305 parentToken.cName);\r
2435723a 306 } else {\r
307 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED %s _gPcd_BinaryPatch_%s = _PCD_VALUE_%s;\r\n",\r
308 Token.getAutogendatumTypeString(parentToken.datumType),\r
309 parentToken.cName,\r
310 parentToken.cName);\r
2435723a 311 }\r
38ee8d9e 312 }\r
313\r
314 break;\r
315 case DYNAMIC:\r
57a38e69 316 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s LibPcdGet%s(_PCD_TOKEN_%s)\r\n",\r
38ee8d9e 317 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),\r
318 parentToken.cName,\r
319 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),\r
320 parentToken.cName);\r
57a38e69 321 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {\r
322 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(SizeOfBuffer, Buffer) LibPcdSet%s(_PCD_TOKEN_%s, (SizeOfBuffer), (Buffer))\r\n",\r
323 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),\r
324 parentToken.cName,\r
325 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),\r
326 parentToken.cName);\r
327 } else {\r
328 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(Value) LibPcdSet%s(_PCD_TOKEN_%s, (Value))\r\n",\r
329 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),\r
330 parentToken.cName,\r
331 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),\r
332 parentToken.cName);\r
333 }\r
38ee8d9e 334 break;\r
335 case DYNAMIC_EX:\r
11e7b0f6 336 guidStringCName = "_gPcd_TokenSpaceGuid_" +\r
337 parentToken.tokenSpaceName.toString().replaceAll("-", "_");\r
57a38e69 338\r
339 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s LibPcdGetEx%s(&%s, _PCD_TOKEN_%s)\r\n",\r
38ee8d9e 340 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),\r
341 parentToken.cName,\r
342 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),\r
a49b34c0 343 guidStringCName,\r
38ee8d9e 344 parentToken.cName);\r
345\r
57a38e69 346 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {\r
347 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(SizeOfBuffer, Buffer) LibPcdSetEx%s(&%s, _PCD_TOKEN_%s, (SizeOfBuffer), (Buffer))\r\n",\r
348 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),\r
349 parentToken.cName,\r
350 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),\r
351 guidStringCName,\r
352 parentToken.cName);\r
353 } else {\r
354 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(Value) LibPcdSetEx%s(&%s, _PCD_TOKEN_%s, (Value))\r\n",\r
355 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),\r
356 parentToken.cName,\r
357 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),\r
358 guidStringCName,\r
359 parentToken.cName);\r
360\r
361 }\r
38ee8d9e 362 break;\r
363 }\r
364 }\r
365\r
366 /**\r
367 Get the autogen string for header file.\r
bc262841 368\r
38ee8d9e 369 @return The string of header file.\r
370 **/\r
371 public String getHAutogenStr() {\r
372 return hAutogenStr;\r
373 }\r
878ddf1f 374\r
38ee8d9e 375 /**\r
376 Get the autogen string for C code file.\r
bc262841 377\r
38ee8d9e 378 @return The string of C Code file.\r
379 **/\r
380 public String getCAutogenStr() {\r
381 return cAutogenStr;\r
878ddf1f 382 }\r
878ddf1f 383}\r
384\r