]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/pcd/entity/Token.java
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@250 6f19259b...
[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
22 import org.tianocore.build.pcd.action.ActionMessage;
23
24 /** This class is to descript a PCD token object. The information of a token mainly
25 comes from MSA, SPD and setting produced by platform developer.
26 **/
27 public class Token {
28 ///
29 /// Enumeration macro defintion for PCD type.
30 /// BUGBUG: Not use upcase charater is to facility for reading. It may be changed
31 /// in coding review.
32 public enum PCD_TYPE {FEATURE_FLAG, FIXED_AT_BUILD, PATCHABLE_IN_MODULE, DYNAMIC,
33 DYNAMIC_EX, UNKNOWN}
34
35 ///
36 /// Enumeration macro definition for datum type. All type mainly comes from ProcessBind.h.
37 /// Wizard maybe expand this type as "int, unsigned int, short, unsigned short etc" in
38 /// prompt dialog.
39 ///
40 public enum DATUM_TYPE {UINT8, UINT16, UINT32, UINT64, BOOLEAN, POINTER, UNKNOWN}
41
42 ///
43 /// Enumeration macor defintion for usage of PCD
44 ///
45 public enum PCD_USAGE {ALWAYS_PRODUCED, ALWAYS_CONSUMED, SOMETIMES_PRODUCED,
46 SOMETIMES_CONSUMED, UNKNOWN}
47
48 ///
49 /// cName is to identify a PCD entry and will be used for generating autogen.h/autogen.c.
50 /// cName will be defined in MSA, SPD and FPD, can be regarded as primary key with token space guid.
51 ///
52 public String cName;
53
54 ///
55 /// Token space name is the guid defined by token itself in package or module level. This
56 /// name mainly for DynamicEx type. For other PCD type token, his token space name is the
57 /// assignedtokenSpaceName as follows.
58 /// tokenSpaceName is defined in MSA, SPD, FPD, can be regarded as primary key with cName.
59 ///
60 public UUID tokenSpaceName;
61
62 ///
63 /// tokenNumber is allocated by platform. tokenNumber indicate an index for this token in
64 /// platform token space.
65 /// tokenNumber is defined in SPD, FPD.
66 ///
67 public int tokenNumber;
68
69 ///
70 /// The token space name assigned by platform. For Non-DynamicEx driver this value is same.
71 /// assignedtokenSpaceName is defined in FPD.
72 ///
73 public UUID assignedtokenSpaceName;
74
75 ///
76 /// The token number assigned by platform. The number indiect the offset of this token in platform
77 /// token space.
78 /// AssgiendtokenNumber is defined in FPD.
79 ///
80 public int assignedtokenNumber;
81
82 ///
83 /// pcdType is the PCD item type defined by platform developer.
84 ///
85 public PCD_TYPE pcdType;
86
87 ///
88 /// PCDtype is set by platform developer. It is final PCD type of this token.
89 /// SupportedPcdType is defined in SPD.
90 ///
91 public PCD_TYPE[] supportedpcdType;
92
93 ///
94 /// datumSize is to descript the fix size or max size for this token.
95 /// datumSize is defined in SPD.
96 ///
97 public int datumSize;
98
99 ///
100 /// datum type is to descript what type can be expressed by a PCD token.
101 /// datumType is defined in SPD.
102 ///
103 public DATUM_TYPE datumType;
104
105 ///
106 /// Isplatform is to descript whether this token is defined in platform level.
107 /// If token is belong to platform level. The value can be different for every
108 /// module. All are determined by platform developer.
109 ///
110 public boolean isPlatform;
111
112 ///
113 /// hiiEnabled is to indicate whether the token support Hii functionality.
114 /// hiiEnabled is defined in FPD.
115 ///
116 public boolean hiiEnabled;
117
118 ///
119 /// variableName is valid only when this token support Hii functionality. variableName
120 /// indicates the value of token is associated with what variable.
121 /// variableName is defined in FPD.
122 ///
123 public String variableName;
124
125 ///
126 /// variableGuid is the GUID this token associated with.
127 /// variableGuid is defined in FPD.
128 ///
129 public UUID variableGuid;
130
131 ///
132 /// Variable offset indicate the associated variable's offset in NV storage.
133 /// variableOffset is defined in FPD.
134 ///
135 public int variableOffset;
136
137 ///
138 /// skuEnabled is to indicate whether the token support Sku functionality.
139 /// skuEnabled is defined in FPD.
140 ///
141 public boolean skuEnabled;
142
143 ///
144 /// skuDataArrayEnabled is to indicate wheter use the skuData array or default value.
145 ///
146 public boolean skuDataArrayEnabled;
147
148 ///
149 /// skuData contains all value for SkuNumber of token.
150 /// skuData is defined in FPD.
151 ///
152 public List<SkuInstance> skuData;
153
154 ///
155 /// maxSkuCount indicate the max count of sku data.
156 /// maxSkuCount is defined in FPD.
157 ///
158 public int maxSkuCount;
159
160 ///
161 /// SkuId is the id of current selected SKU.
162 /// SkuId is defined in FPD.
163 ///
164 public int skuId;
165
166 ///
167 /// datum is the value set by platform developer.
168 /// datum is defined in FPD.
169 ///
170 public Object datum;
171
172 ///
173 /// Default value of this token.
174 /// This default value is defined in SPD level.
175 ///
176 public Object defaultValue;
177
178 ///
179 /// BUGBUG: fix comment
180 /// vpdEnabled is defined in FPD.
181 ///
182 public boolean vpdEnabled;
183
184 ///
185 /// BUGBUG: fix comment
186 /// vpdOffset is defined in FPD.
187 ///
188 public long vpdOffset;
189
190 ///
191 /// producers array record all module private information who produce this PCD token.
192 ///
193 public List<UsageInstance> producers;
194
195 ///
196 /// consumers array record all module private information who consume this PCD token.
197 ///
198 public List<UsageInstance> consumers;
199
200 /**
201 Constructure function.
202
203 Initialize the value of token.
204
205 @param cName The cName of this token
206 @param tokenSpaceName The tokenSpaceName of this token, it is a GUID.
207 @param assignedtokenSpaceName The assignedtokenSpaceName of this token, it is a GUID.
208
209 **/
210 public Token(String cName, UUID tokenSpaceName, UUID assignedtokenSpaceName) {
211 UUID nullUUID = new UUID(0, 0);
212
213 this.cName = cName;
214 this.tokenSpaceName =(tokenSpaceName == null) ? nullUUID : tokenSpaceName;
215 this.assignedtokenSpaceName =(assignedtokenSpaceName == null) ? nullUUID : assignedtokenSpaceName;
216 this.tokenNumber = 0;
217 this.assignedtokenNumber = 0;
218 this.pcdType = PCD_TYPE.UNKNOWN;
219 this.supportedpcdType = null;
220 this.isPlatform = false;
221 this.datumType = DATUM_TYPE.UNKNOWN;
222 this.datumSize = -1;
223 this.defaultValue = null;
224 this.datum = null;
225 this.hiiEnabled = false;
226 this.variableGuid = null;
227 this.variableName = "";
228 this.variableOffset = -1;
229 this.skuEnabled = false;
230 this.skuDataArrayEnabled = false;
231 this.skuId = -1;
232 this.maxSkuCount = -1;
233 this.skuData = new ArrayList<SkuInstance>();
234 this.vpdEnabled = false;
235 this.vpdOffset = -1;
236
237 this.producers = new ArrayList<UsageInstance>();
238 this.consumers = new ArrayList<UsageInstance>();
239 }
240
241 /**
242 Use "TokencName + "-" + SpaceTokenName" as primary key when adding token into database
243
244 @param cName Token name.
245 @param tokenSpaceName The token space guid defined in MSA or SPD
246 @param platformtokenSpaceName The token space guid for current platform token space,
247
248 @return primary key for this token in token database.
249 **/
250 public static String getPrimaryKeyString(String cName, UUID tokenSpaceName,
251 UUID platformtokenSpaceName) {
252 UUID nullUUID = new UUID(0, 0);
253
254 if (platformtokenSpaceName == nullUUID) {
255 return cName + "_" + tokenSpaceName.toString().replace('-', '_');
256 } else {
257 return cName + "_" + platformtokenSpaceName.toString().replace('-', '_');
258 }
259 }
260
261 public String getPrimaryKeyString () {
262 return cName + "_" + tokenSpaceName.toString().replace('-', '_');
263 }
264
265 /**
266 Judge datumType is valid
267
268 @param type The datumType want to be judged.
269
270 @retval TRUE - The type is valid.
271 @retval FALSE - The type is invalid.
272 **/
273 public static boolean isValiddatumType(DATUM_TYPE type) {
274 if ((type.ordinal() < DATUM_TYPE.UINT8.ordinal() ) ||
275 (type.ordinal() > DATUM_TYPE.POINTER.ordinal())) {
276 return false;
277 }
278 return true;
279 }
280
281 /**
282 Judge pcdType is valid
283
284 @param type The PCdType want to be judged.
285
286 @retval TRUE - The type is valid.
287 @retval FALSE - The type is invalid.
288 **/
289 public static boolean isValidpcdType(PCD_TYPE type) {
290 if ((type.ordinal() < PCD_TYPE.FEATURE_FLAG.ordinal() ) ||
291 (type.ordinal() > PCD_TYPE.DYNAMIC_EX.ordinal())) {
292 return false;
293 }
294 return true;
295 }
296
297 /**
298 Add an usage instance for token
299
300 @param usageInstance The usage instance
301
302 @retval TRUE - Success to add usage instance.
303 @retval FALSE - Fail to add usage instance
304 **/
305 public boolean addUsageInstance(UsageInstance usageInstance) {
306 if (usageInstance.usage == PCD_USAGE.UNKNOWN) {
307 return false;
308 }
309
310 if ((usageInstance.usage == PCD_USAGE.ALWAYS_PRODUCED) ||
311 (usageInstance.usage == PCD_USAGE.SOMETIMES_PRODUCED)) {
312 producers.add(usageInstance);
313 } else {
314 consumers.add(usageInstance);
315 }
316 return true;
317 }
318
319 /**
320 Judge whether exist an usage instance for this token
321
322 @param moduleName Use xmlFilePath as keyword to search the usage instance
323
324 @retval PCD_USAGE - if UsageInstance exists.
325 @retval UNKNOWN - if UsageInstance does not exist, return UNKONW.
326 **/
327 public PCD_USAGE isUsageInstanceExist(String moduleName) {
328 int index;
329 UsageInstance usageInstance;
330
331 if (moduleName == null) {
332 ActionMessage.warning(this, "Error parameter for isUsageInstanceExist() function!");
333 return PCD_USAGE.UNKNOWN;
334 }
335
336 if (moduleName.length() == 0) {
337 return PCD_USAGE.UNKNOWN;
338 }
339
340 //
341 // Searching the usage instance in module's producer and consumer according to
342 // module's name.
343 //
344 for (index = 0; index < producers.size(); index ++) {
345 usageInstance =(UsageInstance)producers.get(index);
346 if (usageInstance.moduleName.equalsIgnoreCase(moduleName)) {
347 return usageInstance.usage;
348 }
349 }
350
351 for (index = 0; index < consumers.size(); index ++) {
352 usageInstance =(UsageInstance)consumers.get(index);
353 if (usageInstance.moduleName.equalsIgnoreCase(moduleName)) {
354 return usageInstance.usage;
355 }
356 }
357 return PCD_USAGE.UNKNOWN;
358 }
359
360 /**
361 Get usage instance according to a MSA file name
362
363 @param moduleName The file path string of MSA file.
364
365 @return usage instance object.
366 **/
367 public UsageInstance getUsageInstance(String moduleName) {
368 int usageIndex;
369 UsageInstance usageInstance;
370
371 if (moduleName == null) {
372 ActionMessage.warning(this, "Error parameter for isUsageInstanceExist() function!");
373 return null;
374 }
375
376 if (moduleName.length() == 0) {
377 return null;
378 }
379
380 if (producers.size() != 0) {
381 for (usageIndex = 0; usageIndex < producers.size(); usageIndex ++) {
382 usageInstance =(UsageInstance)producers.get(usageIndex);
383 if (usageInstance.moduleName.equalsIgnoreCase(moduleName)) {
384 return usageInstance;
385 }
386 }
387 }
388
389 if (consumers.size() != 0) {
390 for (usageIndex = 0; usageIndex < consumers.size(); usageIndex ++) {
391 usageInstance =(UsageInstance)consumers.get(usageIndex);
392 if (usageInstance.moduleName.equalsIgnoreCase(moduleName)) {
393 return usageInstance;
394 }
395 }
396 }
397 return null;
398 }
399
400 /**
401 Get the PCD_TYPE according to the string of PCD_TYPE
402
403 @param pcdTypeStr The string of PCD_TYPE
404
405 @return PCD_TYPE
406 **/
407 public static PCD_TYPE getpcdTypeFromString(String pcdTypeStr) {
408 if (pcdTypeStr == null) {
409 return PCD_TYPE.UNKNOWN;
410 }
411
412 if (pcdTypeStr.equalsIgnoreCase("FEATURE_FLAG")) {
413 return PCD_TYPE.FEATURE_FLAG;
414 } else if (pcdTypeStr.equalsIgnoreCase("FIXED_AT_BUILD")) {
415 return PCD_TYPE.FIXED_AT_BUILD;
416 } else if (pcdTypeStr.equalsIgnoreCase("PATCHABLE_IN_MODULE")) {
417 return PCD_TYPE.PATCHABLE_IN_MODULE;
418 } else if (pcdTypeStr.equalsIgnoreCase("DYNAMIC")) {
419 return PCD_TYPE.DYNAMIC;
420 } else if (pcdTypeStr.equalsIgnoreCase("DYNAMIC_EX")) {
421 return PCD_TYPE.DYNAMIC_EX;
422 } else {
423 return PCD_TYPE.UNKNOWN;
424 }
425 }
426
427 /**
428 Get the string of given datumType. This string will be used for generating autogen files
429
430 @param datumType Given datumType
431
432 @return The string of datum type.
433 **/
434 public static String getStringOfdatumType(DATUM_TYPE datumType) {
435 switch (datumType) {
436 case UINT8:
437 return "UINT8";
438 case UINT16:
439 return "UINT16";
440 case UINT32:
441 return "UINT32";
442 case UINT64:
443 return "UINT64";
444 case POINTER:
445 return "POINTER";
446 case BOOLEAN:
447 return "BOOLEAN";
448 }
449 return "UNKNOWN";
450 }
451
452 /**
453 Get the datumType according to a string.
454
455 @param datumTypeStr The string of datumType
456
457 @return DATUM_TYPE
458 **/
459 public static DATUM_TYPE getdatumTypeFromString(String datumTypeStr) {
460 if (datumTypeStr.equalsIgnoreCase("UINT8")) {
461 return DATUM_TYPE.UINT8;
462 } else if (datumTypeStr.equalsIgnoreCase("UINT16")) {
463 return DATUM_TYPE.UINT16;
464 } else if (datumTypeStr.equalsIgnoreCase("UINT32")) {
465 return DATUM_TYPE.UINT32;
466 } else if (datumTypeStr.equalsIgnoreCase("UINT64")) {
467 return DATUM_TYPE.UINT64;
468 } else if (datumTypeStr.equalsIgnoreCase("VOID*")) {
469 return DATUM_TYPE.POINTER;
470 } else if (datumTypeStr.equalsIgnoreCase("BOOLEAN")) {
471 return DATUM_TYPE.BOOLEAN;
472 }
473 return DATUM_TYPE.UNKNOWN;
474 }
475
476 /**
477 Get string of given pcdType
478
479 @param pcdType The given PcdType
480
481 @return The string of PCD_TYPE.
482 **/
483 public static String getStringOfpcdType(PCD_TYPE pcdType) {
484 switch (pcdType) {
485 case FEATURE_FLAG:
486 return "FEATURE_FLAG";
487 case FIXED_AT_BUILD:
488 return "FIXED_AT_BUILD";
489 case PATCHABLE_IN_MODULE:
490 return "PATCHABLE_IN_MODULE";
491 case DYNAMIC:
492 return "DYNAMIC";
493 case DYNAMIC_EX:
494 return "DYNAMIC_EX";
495 }
496 return "UNKNOWN";
497 }
498
499 /**
500 Get the PCD_USAGE according to a string
501
502 @param usageStr The string of PCD_USAGE
503
504 @return The PCD_USAGE
505 **/
506 public static PCD_USAGE getUsageFromString(String usageStr) {
507 if (usageStr == null) {
508 return PCD_USAGE.UNKNOWN;
509 }
510
511 if (usageStr.equalsIgnoreCase("ALWAYS_PRODUCED")) {
512 return PCD_USAGE.ALWAYS_PRODUCED;
513 } else if (usageStr.equalsIgnoreCase("SOMETIMES_PRODUCED")) {
514 return PCD_USAGE.SOMETIMES_PRODUCED;
515 } else if (usageStr.equalsIgnoreCase("ALWAYS_CONSUMED")) {
516 return PCD_USAGE.ALWAYS_CONSUMED;
517 } else if (usageStr.equalsIgnoreCase("SOMETIMES_CONSUMED")) {
518 return PCD_USAGE.SOMETIMES_CONSUMED;
519 }
520
521 return PCD_USAGE.UNKNOWN;
522 }
523
524 /**
525 Get the string of given PCD_USAGE
526
527 @param usage The given PCD_USAGE
528
529 @return The string of PDC_USAGE.
530 **/
531 public static String getStringOfUsage(PCD_USAGE usage) {
532 switch (usage) {
533 case ALWAYS_PRODUCED:
534 return "ALWAYS_PRODUCED";
535 case ALWAYS_CONSUMED:
536 return "ALWAYS_CONSUMED";
537 case SOMETIMES_PRODUCED:
538 return "SOMETIMES_PRODUCED";
539 case SOMETIMES_CONSUMED:
540 return "SOMETIMES_CONSUMED";
541 }
542 return "UNKNOWN";
543 }
544
545 /**
546 Get the Defined datumType string for autogen. The string is for generating some MACROs in Autogen.h
547
548 @param datumType The given datumType
549
550 @return string of datum type for autogen.
551 **/
552 public static String GetAutogenDefinedatumTypeString(DATUM_TYPE datumType) {
553 switch (datumType) {
554
555 case UINT8:
556 return "8";
557 case UINT16:
558 return "16";
559 case BOOLEAN:
560 return "BOOL";
561 case POINTER:
562 return "PTR";
563 case UINT32:
564 return "32";
565 case UINT64:
566 return "64";
567 default:
568 return null;
569 }
570 }
571
572 /**
573 Get the datumType String for Autogen. This string will be used for generating defintions of PCD token in autogen
574
575 @param datumType The given datumType
576
577 @return string of datum type.
578 **/
579 public static String getAutogendatumTypeString(DATUM_TYPE datumType) {
580 switch (datumType) {
581 case UINT8:
582 return "UINT8";
583 case UINT16:
584 return "UINT16";
585 case UINT32:
586 return "UINT32";
587 case UINT64:
588 return "UINT64";
589 case POINTER:
590 return "VOID*";
591 case BOOLEAN:
592 return "BOOLEAN";
593 }
594 return null;
595 }
596
597 /**
598 Get the datumType string for generating some MACROs in autogen file of Library
599
600 @param datumType The given datumType
601
602 @return String of datum for genrating bit charater.
603 **/
604 public static String getAutogenLibrarydatumTypeString(DATUM_TYPE datumType) {
605 switch (datumType) {
606 case UINT8:
607 return "8";
608 case UINT16:
609 return "16";
610 case BOOLEAN:
611 return "Bool";
612 case POINTER:
613 return "Ptr";
614 case UINT32:
615 return "32";
616 case UINT64:
617 return "64";
618 default:
619 return null;
620 }
621 }
622
623 /**
624 UUID defined in Schems is object, this function is to tranlate this object
625 to UUID data.
626
627 @param uuidObj The object comes from schema.
628
629 @return The traslated UUID instance.
630 **/
631 public static UUID getGUIDFromSchemaObject(Object uuidObj) {
632 UUID uuid;
633 if (uuidObj.toString().equalsIgnoreCase("0")) {
634 uuid = new UUID(0,0);
635 } else {
636 uuid = UUID.fromString(uuidObj.toString());
637 }
638
639 return uuid;
640 }
641
642 //
643 // BugBug: We need change this algorithm accordingly when schema is updated
644 // to support no default value.
645 //
646 public boolean hasDefaultValue () {
647
648 if (hiiEnabled) {
649 return true;
650 }
651
652 if (vpdEnabled) {
653 return true;
654 }
655
656 if (datum.toString().compareTo("NoDefault") == 0) {
657 return false;
658 }
659
660 return true;
661 }
662
663 public boolean isStringType () {
664 String str = datum.toString();
665
666 if (datumType == Token.DATUM_TYPE.POINTER &&
667 str.startsWith("L\"") &&
668 str.endsWith("\"")) {
669 return true;
670 }
671
672 return false;
673 }
674
675 public String getStringTypeString () {
676 return datum.toString().substring(2, datum.toString().length() - 1);
677 }
678 }
679
680
681
682