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