03499e6ee1fbe7fd575ca3bf3264a05e52c32913
[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 import org.tianocore.pcd.entity.CommonDefinition;
21 import org.tianocore.pcd.entity.UsageIdentification;
22
23 /**
24 This class indicate an usage instance for a PCD token. This instance maybe a module
25 or platform setting. When a module produce or cosume a PCD token, then this module
26 is an usage instance for this PCD token.
27 **/
28 public class UsageInstance {
29 ///
30 /// This parent that this usage instance belongs to.
31 ///
32 public Token parentToken;
33
34 ///
35 /// ModuleIdentification for Usage Instance
36 ///
37 public UsageIdentification usageId;
38
39 ///
40 /// Arch also is a key for a UsageInstance
41 ///
42 public String arch;
43
44 ///
45 /// The PCD type defined for module
46 ///
47 public Token.PCD_TYPE modulePcdType;
48
49 ///
50 /// The value of the PCD in this usage instance.
51 ///
52 public String datum;
53
54 ///
55 /// The maxDatumSize could be different for same PCD in different module
56 /// But this case is allow for FeatureFlag, FixedAtBuild, PatchableInModule
57 /// type.
58 ///
59 public int maxDatumSize;
60
61 ///
62 /// Autogen string for header file.
63 ///
64 public String hAutogenStr;
65
66 ///
67 /// Auotgen string for C code file.
68 ///
69 public String cAutogenStr;
70
71 /**
72 Constructure function for UsageInstance
73
74 @param parentToken The token instance for this usgaInstance
75 @param usageId The identification for usage instance
76 @param modulePcdType The PCD type for this usage instance
77 @param value The value of this PCD in this usage instance
78 @param maxDatumSize The max datum size of this PCD in this usage
79 instance.
80 **/
81 public UsageInstance(Token parentToken,
82 UsageIdentification usageId,
83 Token.PCD_TYPE modulePcdType,
84 String value,
85 int maxDatumSize) {
86 this.parentToken = parentToken;
87 this.usageId = usageId;
88 this.modulePcdType = modulePcdType;
89 this.datum = value;
90 this.maxDatumSize = maxDatumSize;
91 }
92
93 /**
94 Get the primary key for usage instance array for every token.
95
96 @param usageId The identification of UsageInstance
97
98 @retval String The primary key for this usage instance
99 **/
100 public static String getPrimaryKey(UsageIdentification usageId) {
101 return usageId.toString();
102 }
103
104 /**
105 Get primary key string for this usage instance
106
107 @return String primary key string
108 **/
109 public String getPrimaryKey() {
110 return UsageInstance.getPrimaryKey(usageId);
111 }
112
113 /**
114 Judget whether current module is PEI driver
115
116 @return boolean whether current module is PEI driver
117 **/
118 public boolean isPeiPhaseComponent() {
119 int moduleType = CommonDefinition.getModuleType(usageId.moduleType);
120
121 if ((moduleType == CommonDefinition.ModuleTypePeiCore) ||
122 (moduleType == CommonDefinition.ModuleTypePeim)) {
123 return true;
124 }
125 return false;
126 }
127
128 /**
129 Judge whether current module is DXE driver.
130
131 @return boolean whether current module is DXE driver
132 **/
133 public boolean isDxePhaseComponent() {
134 int moduleType = CommonDefinition.getModuleType(usageId.moduleType);
135
136 if ((moduleType == CommonDefinition.ModuleTypeDxeDriver) ||
137 (moduleType == CommonDefinition.ModuleTypeDxeRuntimeDriver) ||
138 (moduleType == CommonDefinition.ModuleTypeDxeSalDriver) ||
139 (moduleType == CommonDefinition.ModuleTypeDxeSmmDriver) ||
140 (moduleType == CommonDefinition.ModuleTypeUefiDriver) ||
141 (moduleType == CommonDefinition.ModuleTypeUefiApplication)
142 ) {
143 return true;
144 }
145 return false;
146 }
147
148 /**
149 Generate autogen string for header file and C code file.
150
151 @param isBuildUsedLibrary whether the autogen is for library.
152 **/
153 public void generateAutoGen(boolean isBuildUsedLibrary) {
154 String guidStringCName = null;
155 boolean isByteArray = false;
156 String printDatum = null;
157 String tokenNumberString = null;
158
159 hAutogenStr = "";
160 cAutogenStr = "";
161
162 if (this.modulePcdType == Token.PCD_TYPE.DYNAMIC_EX) {
163 //
164 // For DYNAMIC_EX type PCD, use original token number in SPD or FPD to generate autogen
165 //
166 tokenNumberString = Long.toString(parentToken.dynamicExTokenNumber, 16);
167 } else {
168 //
169 // For Others type PCD, use autogenerated token number to generate autogen
170 //
171 tokenNumberString = Long.toString(parentToken.tokenNumber, 16);
172 }
173
174 hAutogenStr += String.format("#define _PCD_TOKEN_%s 0x%s\r\n", parentToken.cName, tokenNumberString);
175
176 //
177 // Judge the value of this PCD is byte array type
178 //
179 if (!isBuildUsedLibrary && !parentToken.isDynamicPCD) {
180 if (datum.trim().charAt(0) == '{') {
181 isByteArray = true;
182 }
183 }
184
185 //
186 // "ULL" should be added to value's tail for UINT64 value
187 //
188 if (parentToken.datumType == Token.DATUM_TYPE.UINT64) {
189 printDatum = this.datum + "ULL";
190 } else {
191 printDatum = this.datum;
192 }
193
194 switch (modulePcdType) {
195 case FEATURE_FLAG:
196 //
197 // Example autogen string for following generation:
198 // "extern const BOOLEAN _gPcd_FixedAtBuild_PcdSampleToken";
199 //
200 hAutogenStr += String.format("extern const BOOLEAN _gPcd_FixedAtBuild_%s;\r\n",
201 parentToken.cName);
202 //
203 // Example autogen string for following generation:
204 // "#define _PCD_GET_MODE_8_PcdSampleToken _gPcd_FixedAtBuild_PcdSampleToken";
205 //
206 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s _gPcd_FixedAtBuild_%s\r\n",
207 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
208 parentToken.cName,
209 parentToken.cName);
210 //
211 // Example autogen string for following generation:
212 // "//#define _PCD_SET_MODE_8_PcdSampleToken ASSERT(FALSE) If is not allowed to set value...";
213 //
214 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",
215 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
216 parentToken.cName);
217
218 if (!isBuildUsedLibrary) {
219 //
220 // Example autogen string for following generation:
221 // "#define _PCD_VALUE_PcdSampleToken 0x1000"
222 //
223 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",
224 parentToken.cName,
225 printDatum);
226 //
227 // Example autogen string for following generation:
228 // "GLOBAL_REMOVE_IF_UNREFERENCED const BOOLEAN _gPcd_FixedAtBuild_PcdSampleToken = _PCD_VALUE_PcdSampleToken;"
229 //
230 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const BOOLEAN _gPcd_FixedAtBuild_%s = _PCD_VALUE_%s;\r\n",
231 parentToken.cName,
232 parentToken.cName);
233 }
234 break;
235 case FIXED_AT_BUILD:
236 if (isByteArray) {
237 //
238 // Example autogen string for following generation:
239 // "extern const BOOLEAN _gPcd_FixedAtBuild_PcdSampleToken";
240 //
241 hAutogenStr += String.format("extern const UINT8 _gPcd_FixedAtBuild_%s[];\r\n",
242 parentToken.cName);
243 //
244 // Example autogen string for following generation:
245 // "#define _PCD_GET_MODE_8_PcdSampleToken (VOID*)_gPcd_FixedAtBuild_PcdSampleToken";
246 //
247 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s (VOID*)_gPcd_FixedAtBuild_%s\r\n",
248 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
249 parentToken.cName,
250 parentToken.cName);
251 } else {
252 //
253 // Example autogen string for following generation:
254 // "extern const UINT8 _gPcd_FixedAtBuild_PcdSampleToken";
255 //
256 hAutogenStr += String.format("extern const %s _gPcd_FixedAtBuild_%s;\r\n",
257 Token.getAutogendatumTypeString(parentToken.datumType),
258 parentToken.cName);
259 //
260 // Example autogen string for following generation:
261 // "#define _PCD_GET_MODE_8_PcdSampleToken _gPcd_FixedAtBuild_PcdSampleToken";
262 //
263 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s _gPcd_FixedAtBuild_%s\r\n",
264 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
265 parentToken.cName,
266 parentToken.cName);
267 }
268
269 //
270 // Example autogen string for following generation:
271 // "//#define _PCD_SET_MODE_8_PcdSampleToken ASSERT(FALSE) If is not allowed to set value...";
272 //
273 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",
274 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
275 parentToken.cName);
276 if (!isBuildUsedLibrary) {
277 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {
278 if (isByteArray) {
279 //
280 // Example autogen string for following generation:
281 // "#define _PCD_VALUE_PcdSampleToken (VOID*)_gPcd_FixedAtBuild_PcdSampleToken"
282 //
283 hAutogenStr += String.format("#define _PCD_VALUE_%s (VOID*)_gPcd_FixedAtBuild_%s\r\n",
284 parentToken.cName,
285 parentToken.cName);
286 //
287 // Example autogen string for following generation:
288 // "GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gPcd_FixedAtBuild_PcdSampleToken[] = 'dfdf';"
289 //
290 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gPcd_FixedAtBuild_%s[] = %s;\r\n",
291 parentToken.cName,
292 printDatum);
293 } else {
294 //
295 // Example autogen string for following generation:
296 // "#define _PCD_VALUE_PcdSampleToken 0x222"
297 //
298 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",
299 parentToken.cName,
300 printDatum);
301 //
302 // Example autogen string for following generation:
303 // "GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gPcd_FixedAtBuild_PcdSampleToken[] = _PCD_VALUE_PcdSampleToken;"
304 //
305 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const %s _gPcd_FixedAtBuild_%s = _PCD_VALUE_%s;\r\n",
306 Token.getAutogendatumTypeString(parentToken.datumType),
307 parentToken.cName,
308 parentToken.cName);
309 }
310 } else {
311 //
312 // Example autogen string for following generation:
313 // "#define _PCD_VALUE_PcdSampleToken 0x222"
314 //
315 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",
316 parentToken.cName,
317 printDatum);
318 //
319 // Example autogen string for following generation:
320 // "GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gPcd_FixedAtBuild_PcdSampleToken[] = _PCD_VALUE_PcdSampleToken;"
321 //
322 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const %s _gPcd_FixedAtBuild_%s = _PCD_VALUE_%s;\r\n",
323 Token.getAutogendatumTypeString(parentToken.datumType),
324 parentToken.cName,
325 parentToken.cName);
326 }
327 }
328 break;
329 case PATCHABLE_IN_MODULE:
330 if (isByteArray) {
331 //
332 // Example autogen string for following generation:
333 // "extern UINT8 _gPcd_BinaryPatch_PcdSampleToken[];"
334 //
335 hAutogenStr += String.format("extern UINT8 _gPcd_BinaryPatch_%s[];\r\n",
336 parentToken.cName);
337 //
338 // Example autogen string for following generation:
339 // "#define _PCD_GET_MODE_8_PcdSampleToken (VOID*)_gPcd_BinaryPatch_PcdSampleToken"
340 //
341 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s (VOID*)_gPcd_BinaryPatch_%s\r\n",
342 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
343 parentToken.cName,
344 parentToken.cName);
345 } else {
346 //
347 // Example autogen string for following generation:
348 // "extern UINT8 _gPcd_BinaryPatch_PcdSampleToken;"
349 //
350 hAutogenStr += String.format("extern %s _gPcd_BinaryPatch_%s;\r\n",
351 Token.getAutogendatumTypeString(parentToken.datumType),
352 parentToken.cName);
353 //
354 // Example autogen string for following generation:
355 // "#define _PCD_GET_MODE_8_PcdSampleToken _gPcd_BinaryPatch_PcdSampleToken"
356 //
357 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s _gPcd_BinaryPatch_%s\r\n",
358 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
359 parentToken.cName,
360 parentToken.cName);
361 }
362
363 //
364 // Generate _PCD_SET_MODE_xx macro for using set BinaryPatch value via PcdSet macro
365 //
366 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {
367 //
368 // Example autogen string for following generation:
369 // "#define _PCD_SET_MODE_8_PcdSampleToken(SizeOfBuffer, Buffer) CopyMem (_gPcd_BinaryPatch_PcdSampleToken, (Buffer), (SizeOfBuffer))"
370 //
371 hAutogenStr += String.format("#define _PCD_PATCHABLE_%s_SIZE %d\r\n",
372 parentToken.cName,
373 parentToken.datumSize);
374 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(SizeOfBuffer, Buffer) "+
375 "LibPatchPcdSetPtr (_gPcd_BinaryPatch_%s, (UINTN)_PCD_PATCHABLE_%s_SIZE, "+
376 "(SizeOfBuffer), (Buffer))\r\n",
377 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
378 parentToken.cName,
379 parentToken.cName,
380 parentToken.cName);
381 } else {
382 //
383 // Example autogen string for following generation:
384 // "#define _PCD_SET_MODE_8_PcdSampleToken(Value) (_gPcd_BinaryPatch_PcdSampleToken = (Value))"
385 //
386 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(Value) (_gPcd_BinaryPatch_%s = (Value))\r\n",
387 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
388 parentToken.cName,
389 parentToken.cName);
390 }
391
392 if (!isBuildUsedLibrary) {
393 //
394 // Example autogen string for following generation:
395 // "#define _PCD_VALUE_PcdSampleToken 0x111"
396 //
397 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",
398 parentToken.cName,
399 printDatum);
400 if (isByteArray) {
401 //
402 // Example autogen string for following generation:
403 // "GLOBAL_REMOVE_IF_UNREFERENCED UINT8 _gPcd_BinaryPatch_PcdSampleToken[] = _PCD_VALUE_PcdSampleToken;"
404 //
405 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED UINT8 _gPcd_BinaryPatch_%s[%d] = _PCD_VALUE_%s;\r\n",
406 parentToken.cName,
407 parentToken.datumSize,
408 parentToken.cName);
409 } else {
410 //
411 // Example autogen string for following generation:
412 // "GLOBAL_REMOVE_IF_UNREFERENCED UINT8 _gPcd_BinaryPatch_PcdSampleToken[] = _PCD_VALUE_PcdSampleToken;"
413 //
414 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED %s _gPcd_BinaryPatch_%s = _PCD_VALUE_%s;\r\n",
415 Token.getAutogendatumTypeString(parentToken.datumType),
416 parentToken.cName,
417 parentToken.cName);
418 }
419 }
420
421 break;
422 case DYNAMIC:
423 //
424 // Example autogen string for following generation:
425 // "#define _PCD_GET_MODE_8_PcdSampleToken LibPcdGet%s(_PCD_TOKEN_PcdSampleToken)"
426 //
427 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s LibPcdGet%s(_PCD_TOKEN_%s)\r\n",
428 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
429 parentToken.cName,
430 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
431 parentToken.cName);
432 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {
433 //
434 // Example autogen string for following generation:
435 // "#define _PCD_SET_MODE_8_PcdSampleToken(SizeOfBuffer, Buffer) LibPcdSet%s(_PCD_TOKEN_PcdSampleToken, (SizeOfBuffer), (Buffer))"
436 //
437 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(SizeOfBuffer, Buffer) LibPcdSet%s(_PCD_TOKEN_%s, (SizeOfBuffer), (Buffer))\r\n",
438 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
439 parentToken.cName,
440 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
441 parentToken.cName);
442 } else {
443 //
444 // Example autogen string for following generation:
445 // "#define _PCD_SET_MODE_8_PcdSampleToken(Value) LibPcdSet%s(_PCD_TOKEN_PcdSampleToken, (Value))"
446 //
447 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(Value) LibPcdSet%s(_PCD_TOKEN_%s, (Value))\r\n",
448 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
449 parentToken.cName,
450 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
451 parentToken.cName);
452 }
453 break;
454 case DYNAMIC_EX:
455 guidStringCName = "_gPcd_TokenSpaceGuid_" +
456 parentToken.tokenSpaceName.toString().replaceAll("-", "_");
457
458 //
459 // Example autogen string for following generation:
460 // "#define _PCD_GET_MODE_8_PcdSampleToken LibPcdGetEx%s(&_gPcd_TokenSpaceGuid_00_00_00, _PCD_TOKEN_PcdSampleToken)"
461 //
462 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s LibPcdGetEx%s(&%s, _PCD_TOKEN_%s)\r\n",
463 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
464 parentToken.cName,
465 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
466 guidStringCName,
467 parentToken.cName);
468
469 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {
470 //
471 // Example autogen string for following generation:
472 // "#define _PCD_SET_MODE_8_PcdSampleToken(SizeOfBuffer, Buffer) LibPcdSetEx%s(&_gPcd_TokenSpaceGuid_00_00_00, _PCD_TOKEN_PcdSampleToken, (SizeOfBuffer), (Buffer))"
473 //
474 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(SizeOfBuffer, Buffer) LibPcdSetEx%s(&%s, _PCD_TOKEN_%s, (SizeOfBuffer), (Buffer))\r\n",
475 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
476 parentToken.cName,
477 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
478 guidStringCName,
479 parentToken.cName);
480 } else {
481 //
482 // Example autogen string for following generation:
483 // "#define _PCD_SET_MODE_8_PcdSampleToken(Value) LibPcdSetEx%s(&_gPcd_TokenSpaceGuid_00_00_00, _PCD_TOKEN_PcdSampleToken, (Value))"
484 //
485 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(Value) LibPcdSetEx%s(&%s, _PCD_TOKEN_%s, (Value))\r\n",
486 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
487 parentToken.cName,
488 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
489 guidStringCName,
490 parentToken.cName);
491
492 }
493 break;
494 }
495 }
496
497 /**
498 Get the autogen string for header file.
499
500 @return The string of header file.
501 **/
502 public String getHAutogenStr() {
503 return hAutogenStr;
504 }
505
506 /**
507 Get the autogen string for C code file.
508
509 @return The string of C Code file.
510 **/
511 public String getCAutogenStr() {
512 return cAutogenStr;
513 }
514 }
515