]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/pcd/entity/UsageInstance.java
2abcea8bdc691d860e92fda5fee6e357ae259bbb
[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 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",
263 parentToken.cName,
264 printDatum);
265 if (isByteArray) {
266 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gPcd_FixedAtBuild_%s[] = _PCD_VALUE_%s;\r\n",
267 parentToken.cName,
268 parentToken.cName);
269 } else {
270 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const %s _gPcd_FixedAtBuild_%s = _PCD_VALUE_%s;\r\n",
271 Token.getAutogendatumTypeString(parentToken.datumType),
272 parentToken.cName,
273 parentToken.cName);
274 }
275 }
276 break;
277 case PATCHABLE_IN_MODULE:
278 if (isByteArray) {
279 hAutogenStr += String.format("extern UINT8 _gPcd_BinaryPatch_%s[];\r\n",
280 parentToken.cName);
281 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s (VOID*)_gPcd_BinaryPatch_%s\r\n",
282 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
283 parentToken.cName,
284 parentToken.cName);
285 } else {
286 hAutogenStr += String.format("extern %s _gPcd_BinaryPatch_%s;\r\n",
287 Token.getAutogendatumTypeString(parentToken.datumType),
288 parentToken.cName);
289 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s _gPcd_BinaryPatch_%s\r\n",
290 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
291 parentToken.cName,
292 parentToken.cName);
293 }
294
295 //
296 // Generate _PCD_SET_MODE_xx macro for using set BinaryPatch value via PcdSet macro
297 //
298 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {
299 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(SizeOfBuffer, Buffer) CopyMem (_gPcd_BinaryPatch_%s, (Buffer), (SizeOfBuffer))\r\n",
300 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
301 parentToken.cName,
302 parentToken.cName);
303 } else {
304 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(Value) (_gPcd_BinaryPatch_%s = (Value))\r\n",
305 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
306 parentToken.cName,
307 parentToken.cName);
308 }
309
310 if (!isBuildUsedLibrary) {
311 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",
312 parentToken.cName,
313 printDatum);
314 if (isByteArray) {
315 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED UINT8 _gPcd_BinaryPatch_%s[] = _PCD_VALUE_%s;\r\n",
316 parentToken.cName,
317 parentToken.cName);
318 } else {
319 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED %s _gPcd_BinaryPatch_%s = _PCD_VALUE_%s;\r\n",
320 Token.getAutogendatumTypeString(parentToken.datumType),
321 parentToken.cName,
322 parentToken.cName);
323 }
324 }
325
326 break;
327 case DYNAMIC:
328 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s LibPcdGet%s(_PCD_TOKEN_%s)\r\n",
329 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
330 parentToken.cName,
331 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
332 parentToken.cName);
333 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {
334 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(SizeOfBuffer, Buffer) LibPcdSet%s(_PCD_TOKEN_%s, (SizeOfBuffer), (Buffer))\r\n",
335 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
336 parentToken.cName,
337 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
338 parentToken.cName);
339 } else {
340 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(Value) LibPcdSet%s(_PCD_TOKEN_%s, (Value))\r\n",
341 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
342 parentToken.cName,
343 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
344 parentToken.cName);
345 }
346 break;
347 case DYNAMIC_EX:
348 guidStringCName = "_gPcd_TokenSpaceGuid_" +
349 parentToken.tokenSpaceName.toString().replaceAll("-", "_");
350
351 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s LibPcdGetEx%s(&%s, _PCD_TOKEN_%s)\r\n",
352 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
353 parentToken.cName,
354 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
355 guidStringCName,
356 parentToken.cName);
357
358 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {
359 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(SizeOfBuffer, Buffer) LibPcdSetEx%s(&%s, _PCD_TOKEN_%s, (SizeOfBuffer), (Buffer))\r\n",
360 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
361 parentToken.cName,
362 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
363 guidStringCName,
364 parentToken.cName);
365 } else {
366 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(Value) LibPcdSetEx%s(&%s, _PCD_TOKEN_%s, (Value))\r\n",
367 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
368 parentToken.cName,
369 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
370 guidStringCName,
371 parentToken.cName);
372
373 }
374 break;
375 }
376 }
377
378 /**
379 Get the autogen string for header file.
380
381 @return The string of header file.
382 **/
383 public String getHAutogenStr() {
384 return hAutogenStr;
385 }
386
387 /**
388 Get the autogen string for C code file.
389
390 @return The string of C Code file.
391 **/
392 public String getCAutogenStr() {
393 return cAutogenStr;
394 }
395 }
396