]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/pcd/entity/UsageInstance.java
20a58ba5f5b6763673db3d6400a59d4000f0dff3
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / pcd / entity / UsageInstance.java
1 /** @file
2 UsageInstance class.
3
4 This class indicate an usage instance for a PCD token. This instance maybe a module
5 or platform setting. When a module produce or cosume a PCD token, then this module
6 is an usage instance for this PCD token.
7
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
13
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.
16
17 **/
18 package org.tianocore.build.pcd.entity;
19
20
21 import java.util.UUID;
22 import org.tianocore.ModuleTypeDef;
23 import org.tianocore.build.autogen.CommonDefinition;
24 import org.tianocore.build.pcd.exception.EntityException;
25
26 /**
27 This class indicate an usage instance for a PCD token. This instance maybe a module
28 or platform setting. When a module produce or cosume a PCD token, then this module
29 is an usage instance for this PCD token.
30 **/
31 public class UsageInstance {
32 ///
33 /// This parent that this usage instance belongs to.
34 ///
35 public Token parentToken;
36
37 ///
38 /// The name of the module who contains this PCD.
39 ///
40 public String moduleName;
41
42 ///
43 /// The GUID of the module who contains this PCD.
44 ///
45 public UUID moduleGUID;
46
47 ///
48 /// The name of the package whose module contains this PCD.
49 ///
50 public String packageName;
51
52 ///
53 /// The GUID of the package whose module contains this PCD.
54 ///
55 public UUID packageGUID;
56
57 ///
58 /// The PCD type defined for module
59 ///
60 public Token.PCD_TYPE modulePcdType;
61
62 ///
63 /// The arch string of module contains this PCD
64 ///
65 public String arch;
66
67 ///
68 /// The version of module contains this PCD
69 ///
70 public String version;
71
72 ///
73 /// The module type for this usage instance.
74 ///
75 public int moduleType;
76
77 ///
78 /// The value of the PCD in this usage instance.
79 ///
80 public String datum;
81
82 ///
83 /// The maxDatumSize could be different for same PCD in different module
84 /// But this case is allow for FeatureFlag, FixedAtBuild, PatchableInModule
85 /// type.
86 ///
87 public int maxDatumSize;
88
89 ///
90 /// Autogen string for header file.
91 ///
92 public String hAutogenStr;
93
94 ///
95 /// Auotgen string for C code file.
96 ///
97 public String cAutogenStr;
98
99 /**
100 Constructure function
101
102 @param parentToken Member variable.
103 @param moduleName Member variable.
104 @param moduleGUID Member variable.
105 @param packageName Member variable.
106 @param packageGUID Member variable.
107 @param moduleType Member variable.
108 @param modulePcdType Member variable.
109 @param arch Member variable.
110 @param version Member variable.
111 @param value Member variable.
112 @param maxDatumSize Member variable.
113 */
114 public UsageInstance (Token parentToken,
115 String moduleName,
116 UUID moduleGUID,
117 String packageName,
118 UUID packageGUID,
119 int moduleType,
120 Token.PCD_TYPE modulePcdType,
121 String arch,
122 String version,
123 String value,
124 int maxDatumSize) {
125 this.parentToken = parentToken;
126 this.moduleName = moduleName;
127 this.moduleGUID = moduleGUID;
128 this.packageName = packageName;
129 this.packageGUID = packageGUID;
130 this.moduleType = moduleType;
131 this.modulePcdType = modulePcdType;
132 this.arch = arch;
133 this.version = version;
134 this.datum = value;
135 this.maxDatumSize = maxDatumSize;
136 }
137
138 /**
139 Get the primary key for usage instance array for every token.
140
141 @param moduleName the name of module
142 @param moduleGUID the GUID name of module
143 @param packageName the name of package who contains this module
144 @param packageGUID the GUID name of package
145 @param arch the archtecture string
146 @param version the version of this module
147
148 @return String primary key
149 */
150 public static String getPrimaryKey(String moduleName,
151 UUID moduleGUID,
152 String packageName,
153 UUID packageGUID,
154 String arch,
155 String version) {
156 //
157 // Because currently transition schema not require write moduleGuid, package Name, Packge GUID in
158 // <ModuleSA> section, So currently no expect all paramter must be valid.
159 return(moduleName + "_" +
160 ((moduleGUID != null) ? moduleGUID.toString() : "NullModuleGuid") + "_" +
161 ((packageName != null) ? packageName : "NullPackageName") + "_" +
162 ((packageGUID != null) ? packageGUID.toString() : "NullPackageGuid") + "_" +
163 ((arch != null) ? arch : "NullArch") + "_" +
164 ((version != null) ? version : "NullVersion"));
165 }
166
167 /**
168 Get primary key string for this usage instance
169
170 @return String primary key string
171 **/
172 public String getPrimaryKey() {
173 return UsageInstance.getPrimaryKey(moduleName, moduleGUID, packageName, packageGUID, arch, version);
174 }
175
176 /**
177 Judget whether current module is PEI driver
178
179 @return boolean
180 */
181 public boolean isPeiPhaseComponent() {
182 if ((moduleType == CommonDefinition.ModuleTypePeiCore) ||
183 (moduleType == CommonDefinition.ModuleTypePeim)) {
184 return true;
185 }
186 return false;
187 }
188
189 public boolean isDxePhaseComponent() {
190 //
191 // BugBug: May need confirmation on which type of module can
192 // make use of Dynamic(EX) PCD entry.
193 //
194 if ((moduleType == CommonDefinition.ModuleTypeDxeDriver) ||
195 (moduleType == CommonDefinition.ModuleTypeDxeRuntimeDriver) ||
196 (moduleType == CommonDefinition.ModuleTypeDxeSalDriver) ||
197 (moduleType == CommonDefinition.ModuleTypeDxeSmmDriver) ||
198 (moduleType == CommonDefinition.ModuleTypeUefiDriver) ||
199 (moduleType == CommonDefinition.ModuleTypeUefiApplication)
200 ) {
201 return true;
202 }
203 return false;
204 }
205
206 /**
207 Generate autogen string for header file and C code file.
208
209 @throws EntityException Fail to generate.
210
211 @param isBuildUsedLibrary whether the autogen is for library.
212 */
213 public void generateAutoGen(boolean isBuildUsedLibrary)
214 throws EntityException {
215 String guidStringCName = null;
216 boolean isByteArray = false;
217 String printDatum = null;
218 String tokenNumberString = null;
219
220 hAutogenStr = "";
221 cAutogenStr = "";
222
223 if (this.modulePcdType == Token.PCD_TYPE.DYNAMIC_EX) {
224 tokenNumberString = Long.toString(parentToken.dynamicExTokenNumber, 16);
225 } else {
226 tokenNumberString = Long.toString(parentToken.tokenNumber, 16);
227 }
228
229 hAutogenStr += String.format("#define _PCD_TOKEN_%s 0x%s\r\n",
230 parentToken.cName, tokenNumberString);
231
232 if (!isBuildUsedLibrary && !parentToken.isDynamicPCD) {
233 if (datum.trim().charAt(0) == '{') {
234 isByteArray = true;
235 }
236 }
237
238 if (parentToken.datumType == Token.DATUM_TYPE.UINT64) {
239 printDatum = this.datum + "ULL";
240 } else {
241 printDatum = this.datum;
242 }
243
244 switch (modulePcdType) {
245 case FEATURE_FLAG:
246 hAutogenStr += String.format("extern const BOOLEAN _gPcd_FixedAtBuild_%s;\r\n",
247 parentToken.cName);
248 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s _gPcd_FixedAtBuild_%s\r\n",
249 parentToken.GetAutogenDefinedatumTypeString(parentToken.datumType),
250 parentToken.cName,
251 parentToken.cName);
252 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",
253 parentToken.GetAutogenDefinedatumTypeString(parentToken.datumType),
254 parentToken.cName);
255
256 if (!isBuildUsedLibrary) {
257 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",
258 parentToken.cName,
259 printDatum);
260 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const BOOLEAN _gPcd_FixedAtBuild_%s = _PCD_VALUE_%s;\r\n",
261 parentToken.cName,
262 parentToken.cName);
263 }
264 break;
265 case FIXED_AT_BUILD:
266 if (isByteArray) {
267 hAutogenStr += String.format("extern const UINT8 _gPcd_FixedAtBuild_%s[];\r\n",
268 parentToken.cName);
269 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s (VOID*)_gPcd_FixedAtBuild_%s\r\n",
270 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
271 parentToken.cName,
272 parentToken.cName);
273 } else {
274 hAutogenStr += String.format("extern const %s _gPcd_FixedAtBuild_%s;\r\n",
275 Token.getAutogendatumTypeString(parentToken.datumType),
276 parentToken.cName);
277 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s _gPcd_FixedAtBuild_%s\r\n",
278 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
279 parentToken.cName,
280 parentToken.cName);
281 }
282
283 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",
284 parentToken.GetAutogenDefinedatumTypeString(parentToken.datumType),
285 parentToken.cName);
286 if (!isBuildUsedLibrary) {
287 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",
288 parentToken.cName,
289 printDatum);
290 if (isByteArray) {
291 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gPcd_FixedAtBuild_%s[] = _PCD_VALUE_%s;\r\n",
292 parentToken.cName,
293 parentToken.cName);
294 } else {
295 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const %s _gPcd_FixedAtBuild_%s = _PCD_VALUE_%s;\r\n",
296 Token.getAutogendatumTypeString(parentToken.datumType),
297 parentToken.cName,
298 parentToken.cName);
299 }
300 }
301 break;
302 case PATCHABLE_IN_MODULE:
303 if (isByteArray) {
304 hAutogenStr += String.format("extern UINT8 _gPcd_BinaryPatch_%s[];\r\n",
305 parentToken.cName);
306 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s (VOID*)_gPcd_BinaryPatch_%s\r\n",
307 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
308 parentToken.cName,
309 parentToken.cName);
310 } else {
311 hAutogenStr += String.format("extern %s _gPcd_BinaryPatch_%s;\r\n",
312 Token.getAutogendatumTypeString(parentToken.datumType),
313 parentToken.cName);
314 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s _gPcd_BinaryPatch_%s\r\n",
315 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
316 parentToken.cName,
317 parentToken.cName);
318 }
319
320 //
321 // Generate _PCD_SET_MODE_xx macro for using set BinaryPatch value via PcdSet macro
322 //
323 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {
324 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(SizeOfBuffer, Buffer) CopyMem (_gPcd_BinaryPatch_%s, (Buffer), (SizeOfBuffer))\r\n",
325 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
326 parentToken.cName,
327 parentToken.cName);
328 } else {
329 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(Value) (_gPcd_BinaryPatch_%s = (Value))\r\n",
330 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
331 parentToken.cName,
332 parentToken.cName);
333 }
334
335 if (!isBuildUsedLibrary) {
336 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",
337 parentToken.cName,
338 printDatum);
339 if (isByteArray) {
340 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED UINT8 _gPcd_BinaryPatch_%s[] = _PCD_VALUE_%s;\r\n",
341 parentToken.cName,
342 parentToken.cName);
343 } else {
344 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED %s _gPcd_BinaryPatch_%s = _PCD_VALUE_%s;\r\n",
345 Token.getAutogendatumTypeString(parentToken.datumType),
346 parentToken.cName,
347 parentToken.cName);
348 }
349 }
350
351 break;
352 case DYNAMIC:
353 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s LibPcdGet%s(_PCD_TOKEN_%s)\r\n",
354 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
355 parentToken.cName,
356 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
357 parentToken.cName);
358 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {
359 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(SizeOfBuffer, Buffer) LibPcdSet%s(_PCD_TOKEN_%s, (SizeOfBuffer), (Buffer))\r\n",
360 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
361 parentToken.cName,
362 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
363 parentToken.cName);
364 } else {
365 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(Value) LibPcdSet%s(_PCD_TOKEN_%s, (Value))\r\n",
366 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
367 parentToken.cName,
368 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
369 parentToken.cName);
370 }
371 break;
372 case DYNAMIC_EX:
373 guidStringCName = "_gPcd_TokenSpaceGuid_" +
374 parentToken.tokenSpaceName.toString().replaceAll("-", "_");
375
376 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s LibPcdGetEx%s(&%s, _PCD_TOKEN_%s)\r\n",
377 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
378 parentToken.cName,
379 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
380 guidStringCName,
381 parentToken.cName);
382
383 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {
384 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(SizeOfBuffer, Buffer) LibPcdSetEx%s(&%s, _PCD_TOKEN_%s, (SizeOfBuffer), (Buffer))\r\n",
385 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
386 parentToken.cName,
387 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
388 guidStringCName,
389 parentToken.cName);
390 } else {
391 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(Value) LibPcdSetEx%s(&%s, _PCD_TOKEN_%s, (Value))\r\n",
392 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
393 parentToken.cName,
394 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
395 guidStringCName,
396 parentToken.cName);
397
398 }
399 break;
400 }
401 }
402
403 /**
404 Get the autogen string for header file.
405
406 @return The string of header file.
407 **/
408 public String getHAutogenStr() {
409 return hAutogenStr;
410 }
411
412 /**
413 Get the autogen string for C code file.
414
415 @return The string of C Code file.
416 **/
417 public String getCAutogenStr() {
418 return cAutogenStr;
419 }
420 }
421