PCD tools update:
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / pcd / entity / Token.java
1 /** @file
2 Token class.
3
4 This module contains all classes releted to PCD token.
5
6 Copyright (c) 2006, Intel Corporation
7 All rights reserved. This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16 package org.tianocore.build.pcd.entity;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.UUID;
21 import java.util.Map;
22 import java.util.HashMap;
23
24 import org.tianocore.build.pcd.action.ActionMessage;
25 import org.tianocore.build.pcd.exception.EntityException;
26
27 /** This class is to descript a PCD token object. The information of a token mainly
28 comes from MSA, SPD and setting produced by platform developer.
29 **/
30 public class Token {
31 ///
32 /// Enumeration macro defintion for PCD type.
33 /// BUGBUG: Not use upcase charater is to facility for reading. It may be changed
34 /// in coding review.
35 public enum PCD_TYPE {FEATURE_FLAG, FIXED_AT_BUILD, PATCHABLE_IN_MODULE, DYNAMIC,
36 DYNAMIC_EX, UNKNOWN}
37
38 ///
39 /// Enumeration macro definition for datum type. All type mainly comes from ProcessBind.h.
40 /// Wizard maybe expand this type as "int, unsigned int, short, unsigned short etc" in
41 /// prompt dialog.
42 ///
43 public enum DATUM_TYPE {UINT8, UINT16, UINT32, UINT64, BOOLEAN, POINTER, UNKNOWN}
44
45 ///
46 /// Enumeration macor defintion for usage of PCD
47 ///
48 public enum PCD_USAGE {ALWAYS_PRODUCED, ALWAYS_CONSUMED, SOMETIMES_PRODUCED,
49 SOMETIMES_CONSUMED, UNKNOWN}
50
51 ///
52 /// cName is to identify a PCD entry and will be used for generating autogen.h/autogen.c.
53 /// cName will be defined in MSA, SPD and FPD, can be regarded as primary key with token space guid.
54 ///
55 public String cName;
56
57 ///
58 /// Token space name is the guid defined by token itself in package or module level. This
59 /// name mainly for DynamicEx type. For other PCD type token, his token space name is the
60 /// assignedtokenSpaceName as follows.
61 /// tokenSpaceName is defined in MSA, SPD, FPD, can be regarded as primary key with cName.
62 ///
63 public UUID tokenSpaceName;
64
65 ///
66 /// tokenNumber is allocated by platform. tokenNumber indicate an index for this token in
67 /// platform token space.
68 /// tokenNumber is defined in SPD, FPD.
69 ///
70 public int tokenNumber;
71
72 ///
73 /// pcdType is the PCD item type defined by platform developer.
74 ///
75 public PCD_TYPE pcdType;
76
77 ///
78 /// datumSize is to descript the fix size or max size for this token.
79 /// datumSize is defined in SPD.
80 ///
81 public int datumSize;
82
83 ///
84 /// datum type is to descript what type can be expressed by a PCD token.
85 /// datumType is defined in SPD.
86 ///
87 public DATUM_TYPE datumType;
88
89 ///
90 /// hiiEnabled is to indicate whether the token support Hii functionality.
91 /// hiiEnabled is defined in FPD.
92 ///
93 public boolean hiiEnabled;
94
95 ///
96 /// variableName is valid only when this token support Hii functionality. variableName
97 /// indicates the value of token is associated with what variable.
98 /// variableName is defined in FPD.
99 ///
100 public String variableName;
101
102 ///
103 /// variableGuid is the GUID this token associated with.
104 /// variableGuid is defined in FPD.
105 ///
106 public UUID variableGuid;
107
108 ///
109 /// Variable offset indicate the associated variable's offset in NV storage.
110 /// variableOffset is defined in FPD.
111 ///
112 public int variableOffset;
113
114 ///
115 /// skuEnabled is to indicate whether the token support Sku functionality.
116 /// skuEnabled is defined in FPD.
117 ///
118 public boolean skuEnabled;
119
120 ///
121 /// skuData contains all value for SkuNumber of token.
122 /// skuData is defined in FPD.
123 ///
124 public List<SkuInstance> skuData;
125
126 ///
127 /// maxSkuCount indicate the max count of sku data.
128 /// maxSkuCount is defined in FPD.
129 ///
130 public int maxSkuCount;
131
132 ///
133 /// SkuId is the id of current selected SKU.
134 /// SkuId is defined in FPD.
135 ///
136 public int skuId;
137
138 ///
139 /// datum is the value set by platform developer.
140 /// datum is defined in FPD.
141 ///
142 public Object datum;
143
144 ///
145 /// BUGBUG: fix comment
146 /// vpdEnabled is defined in FPD.
147 ///
148 public boolean vpdEnabled;
149
150 ///
151 /// BUGBUG: fix comment
152 /// vpdOffset is defined in FPD.
153 ///
154 public long vpdOffset;
155
156 ///
157 /// consumers array record all module private information who consume this PCD token.
158 ///
159 public Map<String, UsageInstance> consumers;
160
161 public Token(String cName, UUID tokenSpaceName) {
162 UUID nullUUID = new UUID(0, 0);
163
164 this.cName = cName;
165 this.tokenSpaceName = (tokenSpaceName == null) ? nullUUID : tokenSpaceName;
166 this.tokenNumber = 0;
167 this.pcdType = PCD_TYPE.UNKNOWN;
168 this.datumType = DATUM_TYPE.UNKNOWN;
169 this.datumSize = -1;
170 this.datum = null;
171 this.hiiEnabled = false;
172 this.variableGuid = null;
173 this.variableName = "";
174 this.variableOffset = -1;
175 this.skuEnabled = false;
176 this.skuId = -1;
177 this.maxSkuCount = -1;
178 this.skuData = new ArrayList<SkuInstance>();
179 this.vpdEnabled = false;
180 this.vpdOffset = -1;
181
182 this.consumers = new HashMap<String, UsageInstance>();
183 }
184
185 /**
186 Use "TokencName + "-" + SpaceTokenName" as primary key when adding token into database
187
188 @param cName Token name.
189 @param tokenSpaceName The token space guid defined in MSA or SPD
190 @param platformtokenSpaceName The token space guid for current platform token space,
191
192 @return primary key for this token in token database.
193 **/
194 public static String getPrimaryKeyString(String cName, UUID tokenSpaceName) {
195 UUID nullUUID = new UUID(0, 0);
196
197 if (tokenSpaceName == null) {
198 return cName + "_" + nullUUID.toString().replace('-', '_');
199 } else {
200 return cName + "_" + tokenSpaceName.toString().replace('-', '_');
201 }
202 }
203
204 /**
205 Get the token primary key in token database.
206
207 @return String
208 */
209 public String getPrimaryKeyString () {
210 return Token.getPrimaryKeyString(cName, tokenSpaceName);
211 }
212
213 /**
214 Judge datumType is valid
215
216 @param type The datumType want to be judged.
217
218 @retval TRUE - The type is valid.
219 @retval FALSE - The type is invalid.
220 **/
221 public static boolean isValiddatumType(DATUM_TYPE type) {
222 if ((type.ordinal() < DATUM_TYPE.UINT8.ordinal() ) ||
223 (type.ordinal() > DATUM_TYPE.POINTER.ordinal())) {
224 return false;
225 }
226 return true;
227 }
228
229 /**
230 Judge pcdType is valid
231
232 @param type The PCdType want to be judged.
233
234 @retval TRUE - The type is valid.
235 @retval FALSE - The type is invalid.
236 **/
237 public static boolean isValidpcdType(PCD_TYPE type) {
238 if ((type.ordinal() < PCD_TYPE.FEATURE_FLAG.ordinal() ) ||
239 (type.ordinal() > PCD_TYPE.DYNAMIC_EX.ordinal())) {
240 return false;
241 }
242 return true;
243 }
244
245 /**
246 Add an usage instance for token
247
248 @param usageInstance The usage instance
249
250 @retval TRUE - Success to add usage instance.
251 @retval FALSE - Fail to add usage instance
252 **/
253 public boolean addUsageInstance(UsageInstance usageInstance)
254 throws EntityException {
255 String exceptionStr;
256
257 if (isUsageInstanceExist(usageInstance.moduleName,
258 usageInstance.moduleGUID,
259 usageInstance.packageName,
260 usageInstance.packageGUID,
261 usageInstance.arch,
262 usageInstance.version)) {
263 exceptionStr = String.format("PCD %s for module %s has already exist in database, Please check all PCD build entries "+
264 "in modules PcdPeim in <ModuleSA> to make sure no duplicated definitions!",
265 usageInstance.parentToken.cName,
266 usageInstance.moduleName);
267 throw new EntityException(exceptionStr);
268 }
269
270 consumers.put(usageInstance.getPrimaryKey(), usageInstance);
271 return true;
272 }
273
274 /**
275 Judge whether exist an usage instance for this token
276
277 @param moduleName the name of module
278 @param moduleGuid the GUID name of modules
279 @param packageName the name of package contains this module
280 @param packageGuid the GUID name of package contains this module
281 @param arch the architecture string
282 @param version the version string
283
284 @return boolean whether exist an usage instance for this token.
285 */
286 public boolean isUsageInstanceExist(String moduleName,
287 UUID moduleGuid,
288 String packageName,
289 UUID packageGuid,
290 String arch,
291 String version) {
292 String keyStr = UsageInstance.getPrimaryKey(moduleName,
293 moduleGuid,
294 packageName,
295 packageGuid,
296 arch,
297 version);
298 return (consumers.get(keyStr) != null);
299 }
300
301 /**
302 Get the PCD_TYPE according to the string of PCD_TYPE
303
304 @param pcdTypeStr The string of PCD_TYPE
305
306 @return PCD_TYPE
307 **/
308 public static PCD_TYPE getpcdTypeFromString(String pcdTypeStr) {
309 if (pcdTypeStr == null) {
310 return PCD_TYPE.UNKNOWN;
311 }
312
313 if (pcdTypeStr.equalsIgnoreCase("FEATURE_FLAG")) {
314 return PCD_TYPE.FEATURE_FLAG;
315 } else if (pcdTypeStr.equalsIgnoreCase("FIXED_AT_BUILD")) {
316 return PCD_TYPE.FIXED_AT_BUILD;
317 } else if (pcdTypeStr.equalsIgnoreCase("PATCHABLE_IN_MODULE")) {
318 return PCD_TYPE.PATCHABLE_IN_MODULE;
319 } else if (pcdTypeStr.equalsIgnoreCase("DYNAMIC")) {
320 return PCD_TYPE.DYNAMIC;
321 } else if (pcdTypeStr.equalsIgnoreCase("DYNAMIC_EX")) {
322 return PCD_TYPE.DYNAMIC_EX;
323 } else {
324 return PCD_TYPE.UNKNOWN;
325 }
326 }
327
328 /**
329 Get the string of given datumType. This string will be used for generating autogen files
330
331 @param datumType Given datumType
332
333 @return The string of datum type.
334 **/
335 public static String getStringOfdatumType(DATUM_TYPE datumType) {
336 switch (datumType) {
337 case UINT8:
338 return "UINT8";
339 case UINT16:
340 return "UINT16";
341 case UINT32:
342 return "UINT32";
343 case UINT64:
344 return "UINT64";
345 case POINTER:
346 return "POINTER";
347 case BOOLEAN:
348 return "BOOLEAN";
349 }
350 return "UNKNOWN";
351 }
352
353 /**
354 Get the datumType according to a string.
355
356 @param datumTypeStr The string of datumType
357
358 @return DATUM_TYPE
359 **/
360 public static DATUM_TYPE getdatumTypeFromString(String datumTypeStr) {
361 if (datumTypeStr.equalsIgnoreCase("UINT8")) {
362 return DATUM_TYPE.UINT8;
363 } else if (datumTypeStr.equalsIgnoreCase("UINT16")) {
364 return DATUM_TYPE.UINT16;
365 } else if (datumTypeStr.equalsIgnoreCase("UINT32")) {
366 return DATUM_TYPE.UINT32;
367 } else if (datumTypeStr.equalsIgnoreCase("UINT64")) {
368 return DATUM_TYPE.UINT64;
369 } else if (datumTypeStr.equalsIgnoreCase("VOID*")) {
370 return DATUM_TYPE.POINTER;
371 } else if (datumTypeStr.equalsIgnoreCase("BOOLEAN")) {
372 return DATUM_TYPE.BOOLEAN;
373 }
374 return DATUM_TYPE.UNKNOWN;
375 }
376
377 /**
378 Get string of given pcdType
379
380 @param pcdType The given PcdType
381
382 @return The string of PCD_TYPE.
383 **/
384 public static String getStringOfpcdType(PCD_TYPE pcdType) {
385 switch (pcdType) {
386 case FEATURE_FLAG:
387 return "FEATURE_FLAG";
388 case FIXED_AT_BUILD:
389 return "FIXED_AT_BUILD";
390 case PATCHABLE_IN_MODULE:
391 return "PATCHABLE_IN_MODULE";
392 case DYNAMIC:
393 return "DYNAMIC";
394 case DYNAMIC_EX:
395 return "DYNAMIC_EX";
396 }
397 return "UNKNOWN";
398 }
399
400 /**
401 Get the PCD_USAGE according to a string
402
403 @param usageStr The string of PCD_USAGE
404
405 @return The PCD_USAGE
406 **/
407 public static PCD_USAGE getUsageFromString(String usageStr) {
408 if (usageStr == null) {
409 return PCD_USAGE.UNKNOWN;
410 }
411
412 if (usageStr.equalsIgnoreCase("ALWAYS_PRODUCED")) {
413 return PCD_USAGE.ALWAYS_PRODUCED;
414 } else if (usageStr.equalsIgnoreCase("SOMETIMES_PRODUCED")) {
415 return PCD_USAGE.SOMETIMES_PRODUCED;
416 } else if (usageStr.equalsIgnoreCase("ALWAYS_CONSUMED")) {
417 return PCD_USAGE.ALWAYS_CONSUMED;
418 } else if (usageStr.equalsIgnoreCase("SOMETIMES_CONSUMED")) {
419 return PCD_USAGE.SOMETIMES_CONSUMED;
420 }
421
422 return PCD_USAGE.UNKNOWN;
423 }
424
425 /**
426 Get the string of given PCD_USAGE
427
428 @param usage The given PCD_USAGE
429
430 @return The string of PDC_USAGE.
431 **/
432 public static String getStringOfUsage(PCD_USAGE usage) {
433 switch (usage) {
434 case ALWAYS_PRODUCED:
435 return "ALWAYS_PRODUCED";
436 case ALWAYS_CONSUMED:
437 return "ALWAYS_CONSUMED";
438 case SOMETIMES_PRODUCED:
439 return "SOMETIMES_PRODUCED";
440 case SOMETIMES_CONSUMED:
441 return "SOMETIMES_CONSUMED";
442 }
443 return "UNKNOWN";
444 }
445
446 /**
447 Get the Defined datumType string for autogen. The string is for generating some MACROs in Autogen.h
448
449 @param datumType The given datumType
450
451 @return string of datum type for autogen.
452 **/
453 public static String GetAutogenDefinedatumTypeString(DATUM_TYPE datumType) {
454 switch (datumType) {
455
456 case UINT8:
457 return "8";
458 case UINT16:
459 return "16";
460 case BOOLEAN:
461 return "BOOL";
462 case POINTER:
463 return "PTR";
464 case UINT32:
465 return "32";
466 case UINT64:
467 return "64";
468 default:
469 return null;
470 }
471 }
472
473 /**
474 Get the datumType String for Autogen. This string will be used for generating defintions of PCD token in autogen
475
476 @param datumType The given datumType
477
478 @return string of datum type.
479 **/
480 public static String getAutogendatumTypeString(DATUM_TYPE datumType) {
481 switch (datumType) {
482 case UINT8:
483 return "UINT8";
484 case UINT16:
485 return "UINT16";
486 case UINT32:
487 return "UINT32";
488 case UINT64:
489 return "UINT64";
490 case POINTER:
491 return "VOID*";
492 case BOOLEAN:
493 return "BOOLEAN";
494 }
495 return null;
496 }
497
498 /**
499 Get the datumType string for generating some MACROs in autogen file of Library
500
501 @param datumType The given datumType
502
503 @return String of datum for genrating bit charater.
504 **/
505 public static String getAutogenLibrarydatumTypeString(DATUM_TYPE datumType) {
506 switch (datumType) {
507 case UINT8:
508 return "8";
509 case UINT16:
510 return "16";
511 case BOOLEAN:
512 return "Bool";
513 case POINTER:
514 return "Ptr";
515 case UINT32:
516 return "32";
517 case UINT64:
518 return "64";
519 default:
520 return null;
521 }
522 }
523
524 /**
525 UUID defined in Schems is object, this function is to tranlate this object
526 to UUID data.
527
528 @param uuidObj The object comes from schema.
529
530 @return The traslated UUID instance.
531 **/
532 public static UUID getGUIDFromSchemaObject(Object uuidObj) {
533 UUID uuid;
534 if (uuidObj.toString().equalsIgnoreCase("0")) {
535 uuid = new UUID(0,0);
536 } else {
537 uuid = UUID.fromString(uuidObj.toString());
538 }
539
540 return uuid;
541 }
542
543 //
544 // BugBug: We need change this algorithm accordingly when schema is updated
545 // to support no default value.
546 //
547 public boolean hasDefaultValue () {
548
549 if (hiiEnabled) {
550 return true;
551 }
552
553 if (vpdEnabled) {
554 return true;
555 }
556
557 if (datum.toString().compareTo("NoDefault") == 0) {
558 return false;
559 }
560
561 return true;
562 }
563
564 public boolean isStringType () {
565 String str = datum.toString();
566
567 if (datumType == Token.DATUM_TYPE.POINTER &&
568 str.startsWith("L\"") &&
569 str.endsWith("\"")) {
570 return true;
571 }
572
573 return false;
574 }
575
576 public String getStringTypeString () {
577 return datum.toString().substring(2, datum.toString().length() - 1);
578 }
579 }
580
581
582
583