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