]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/PcdTools/org/tianocore/pcd/entity/UsageInstance.java
Because Pcd entity, exception and some action package are shared by Building tools...
[mirror_edk2.git] / Tools / Source / PcdTools / org / tianocore / 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.pcd.entity;
19
20
21 import java.util.UUID;
22
23 import org.tianocore.ModuleTypeDef;
24 import org.tianocore.pcd.entity.CommonDefinition;
25 import org.tianocore.pcd.entity.UsageIdentification;
26 import org.tianocore.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 UsageIdentification usageId;
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 UsageIdentification usageId,
88 Token.PCD_TYPE modulePcdType,
89 String value,
90 int maxDatumSize) {
91 this.parentToken = parentToken;
92 this.usageId = usageId;
93 this.modulePcdType = modulePcdType;
94 this.datum = value;
95 this.maxDatumSize = maxDatumSize;
96 }
97
98 /**
99 Get the primary key for usage instance array for every token.
100
101 @param usageId The identification of UsageInstance
102
103 @retval String The primary key for this usage instance
104 **/
105 public static String getPrimaryKey(UsageIdentification usageId) {
106 return usageId.toString();
107 }
108
109 /**
110 Get primary key string for this usage instance
111
112 @return String primary key string
113 **/
114 public String getPrimaryKey() {
115 return UsageInstance.getPrimaryKey(usageId);
116 }
117
118 /**
119 Judget whether current module is PEI driver
120
121 @return boolean whether current module is PEI driver
122 **/
123 public boolean isPeiPhaseComponent() {
124 int moduleType = CommonDefinition.getModuleType(usageId.moduleType);
125
126 if ((moduleType == CommonDefinition.ModuleTypePeiCore) ||
127 (moduleType == CommonDefinition.ModuleTypePeim)) {
128 return true;
129 }
130 return false;
131 }
132
133 /**
134 Judge whether current module is DXE driver.
135
136 @return boolean whether current module is DXE driver
137 **/
138 public boolean isDxePhaseComponent() {
139 int moduleType = CommonDefinition.getModuleType(usageId.moduleType);
140
141 if ((moduleType == CommonDefinition.ModuleTypeDxeDriver) ||
142 (moduleType == CommonDefinition.ModuleTypeDxeRuntimeDriver) ||
143 (moduleType == CommonDefinition.ModuleTypeDxeSalDriver) ||
144 (moduleType == CommonDefinition.ModuleTypeDxeSmmDriver) ||
145 (moduleType == CommonDefinition.ModuleTypeUefiDriver) ||
146 (moduleType == CommonDefinition.ModuleTypeUefiApplication)
147 ) {
148 return true;
149 }
150 return false;
151 }
152
153 /**
154 Generate autogen string for header file and C code file.
155
156 @param isBuildUsedLibrary whether the autogen is for library.
157 **/
158 public void generateAutoGen(boolean isBuildUsedLibrary) {
159 String guidStringCName = null;
160 boolean isByteArray = false;
161 String printDatum = null;
162 String tokenNumberString = null;
163
164 hAutogenStr = "";
165 cAutogenStr = "";
166
167 if (this.modulePcdType == Token.PCD_TYPE.DYNAMIC_EX) {
168 //
169 // For DYNAMIC_EX type PCD, use original token number in SPD or FPD to generate autogen
170 //
171 tokenNumberString = Long.toString(parentToken.dynamicExTokenNumber, 16);
172 } else {
173 //
174 // For Others type PCD, use autogenerated token number to generate autogen
175 //
176 tokenNumberString = Long.toString(parentToken.tokenNumber, 16);
177 }
178
179 hAutogenStr += String.format("#define _PCD_TOKEN_%s 0x%s\r\n", parentToken.cName, tokenNumberString);
180
181 //
182 // Judge the value of this PCD is byte array type
183 //
184 if (!isBuildUsedLibrary && !parentToken.isDynamicPCD) {
185 if (datum.trim().charAt(0) == '{') {
186 isByteArray = true;
187 }
188 }
189
190 //
191 // "ULL" should be added to value's tail for UINT64 value
192 //
193 if (parentToken.datumType == Token.DATUM_TYPE.UINT64) {
194 printDatum = this.datum + "ULL";
195 } else {
196 printDatum = this.datum;
197 }
198
199 switch (modulePcdType) {
200 case FEATURE_FLAG:
201 hAutogenStr += String.format("extern const BOOLEAN _gPcd_FixedAtBuild_%s;\r\n",
202 parentToken.cName);
203 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s _gPcd_FixedAtBuild_%s\r\n",
204 parentToken.GetAutogenDefinedatumTypeString(parentToken.datumType),
205 parentToken.cName,
206 parentToken.cName);
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",
208 parentToken.GetAutogenDefinedatumTypeString(parentToken.datumType),
209 parentToken.cName);
210
211 if (!isBuildUsedLibrary) {
212 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",
213 parentToken.cName,
214 printDatum);
215 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const BOOLEAN _gPcd_FixedAtBuild_%s = _PCD_VALUE_%s;\r\n",
216 parentToken.cName,
217 parentToken.cName);
218 }
219 break;
220 case FIXED_AT_BUILD:
221 if (isByteArray) {
222 hAutogenStr += String.format("extern const UINT8 _gPcd_FixedAtBuild_%s[];\r\n",
223 parentToken.cName);
224 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s (VOID*)_gPcd_FixedAtBuild_%s\r\n",
225 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
226 parentToken.cName,
227 parentToken.cName);
228 } else {
229 hAutogenStr += String.format("extern const %s _gPcd_FixedAtBuild_%s;\r\n",
230 Token.getAutogendatumTypeString(parentToken.datumType),
231 parentToken.cName);
232 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s _gPcd_FixedAtBuild_%s\r\n",
233 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
234 parentToken.cName,
235 parentToken.cName);
236 }
237
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",
239 parentToken.GetAutogenDefinedatumTypeString(parentToken.datumType),
240 parentToken.cName);
241 if (!isBuildUsedLibrary) {
242 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {
243 if (isByteArray) {
244 hAutogenStr += String.format("#define _PCD_VALUE_%s (VOID*)_gPcd_FixedAtBuild_%s\r\n",
245 parentToken.cName,
246 parentToken.cName);
247 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gPcd_FixedAtBuild_%s[] = %s;\r\n",
248 parentToken.cName,
249 printDatum);
250 } else {
251 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",
252 parentToken.cName,
253 printDatum);
254 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const %s _gPcd_FixedAtBuild_%s = _PCD_VALUE_%s;\r\n",
255 Token.getAutogendatumTypeString(parentToken.datumType),
256 parentToken.cName,
257 parentToken.cName);
258 }
259 } else {
260 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",
261 parentToken.cName,
262 printDatum);
263 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const %s _gPcd_FixedAtBuild_%s = _PCD_VALUE_%s;\r\n",
264 Token.getAutogendatumTypeString(parentToken.datumType),
265 parentToken.cName,
266 parentToken.cName);
267 }
268 }
269 break;
270 case PATCHABLE_IN_MODULE:
271 if (isByteArray) {
272 hAutogenStr += String.format("extern UINT8 _gPcd_BinaryPatch_%s[];\r\n",
273 parentToken.cName);
274 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s (VOID*)_gPcd_BinaryPatch_%s\r\n",
275 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
276 parentToken.cName,
277 parentToken.cName);
278 } else {
279 hAutogenStr += String.format("extern %s _gPcd_BinaryPatch_%s;\r\n",
280 Token.getAutogendatumTypeString(parentToken.datumType),
281 parentToken.cName);
282 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s _gPcd_BinaryPatch_%s\r\n",
283 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
284 parentToken.cName,
285 parentToken.cName);
286 }
287
288 //
289 // Generate _PCD_SET_MODE_xx macro for using set BinaryPatch value via PcdSet macro
290 //
291 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {
292 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(SizeOfBuffer, Buffer) CopyMem (_gPcd_BinaryPatch_%s, (Buffer), (SizeOfBuffer))\r\n",
293 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
294 parentToken.cName,
295 parentToken.cName);
296 } else {
297 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(Value) (_gPcd_BinaryPatch_%s = (Value))\r\n",
298 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
299 parentToken.cName,
300 parentToken.cName);
301 }
302
303 if (!isBuildUsedLibrary) {
304 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",
305 parentToken.cName,
306 printDatum);
307 if (isByteArray) {
308 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED UINT8 _gPcd_BinaryPatch_%s[] = _PCD_VALUE_%s;\r\n",
309 parentToken.cName,
310 parentToken.cName);
311 } else {
312 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED %s _gPcd_BinaryPatch_%s = _PCD_VALUE_%s;\r\n",
313 Token.getAutogendatumTypeString(parentToken.datumType),
314 parentToken.cName,
315 parentToken.cName);
316 }
317 }
318
319 break;
320 case DYNAMIC:
321 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s LibPcdGet%s(_PCD_TOKEN_%s)\r\n",
322 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
323 parentToken.cName,
324 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
325 parentToken.cName);
326 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {
327 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(SizeOfBuffer, Buffer) LibPcdSet%s(_PCD_TOKEN_%s, (SizeOfBuffer), (Buffer))\r\n",
328 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
329 parentToken.cName,
330 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
331 parentToken.cName);
332 } else {
333 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(Value) LibPcdSet%s(_PCD_TOKEN_%s, (Value))\r\n",
334 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
335 parentToken.cName,
336 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
337 parentToken.cName);
338 }
339 break;
340 case DYNAMIC_EX:
341 guidStringCName = "_gPcd_TokenSpaceGuid_" +
342 parentToken.tokenSpaceName.toString().replaceAll("-", "_");
343
344 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s LibPcdGetEx%s(&%s, _PCD_TOKEN_%s)\r\n",
345 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
346 parentToken.cName,
347 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
348 guidStringCName,
349 parentToken.cName);
350
351 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {
352 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(SizeOfBuffer, Buffer) LibPcdSetEx%s(&%s, _PCD_TOKEN_%s, (SizeOfBuffer), (Buffer))\r\n",
353 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
354 parentToken.cName,
355 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
356 guidStringCName,
357 parentToken.cName);
358 } else {
359 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(Value) LibPcdSetEx%s(&%s, _PCD_TOKEN_%s, (Value))\r\n",
360 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
361 parentToken.cName,
362 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
363 guidStringCName,
364 parentToken.cName);
365
366 }
367 break;
368 }
369 }
370
371 /**
372 Get the autogen string for header file.
373
374 @return The string of header file.
375 **/
376 public String getHAutogenStr() {
377 return hAutogenStr;
378 }
379
380 /**
381 Get the autogen string for C code file.
382
383 @return The string of C Code file.
384 **/
385 public String getCAutogenStr() {
386 return cAutogenStr;
387 }
388 }
389