e4ecfc034c2da25384f4a14f9a680aceaa49efb9
[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();
256 } else {
257 return cName + "-" + platformtokenSpaceName.toString();
258 }
259 }
260
261 /**
262 Judge datumType is valid
263
264 @param type The datumType want to be judged.
265
266 @retval TRUE - The type is valid.
267 @retval FALSE - The type is invalid.
268 **/
269 public static boolean isValiddatumType(DATUM_TYPE type) {
270 if ((type.ordinal() < DATUM_TYPE.UINT8.ordinal() ) ||
271 (type.ordinal() > DATUM_TYPE.POINTER.ordinal())) {
272 return false;
273 }
274 return true;
275 }
276
277 /**
278 Judge pcdType is valid
279
280 @param type The PCdType want to be judged.
281
282 @retval TRUE - The type is valid.
283 @retval FALSE - The type is invalid.
284 **/
285 public static boolean isValidpcdType(PCD_TYPE type) {
286 if ((type.ordinal() < PCD_TYPE.FEATURE_FLAG.ordinal() ) ||
287 (type.ordinal() > PCD_TYPE.DYNAMIC_EX.ordinal())) {
288 return false;
289 }
290 return true;
291 }
292
293 /**
294 Add an usage instance for token
295
296 @param usageInstance The usage instance
297
298 @retval TRUE - Success to add usage instance.
299 @retval FALSE - Fail to add usage instance
300 **/
301 public boolean addUsageInstance(UsageInstance usageInstance) {
302 if (usageInstance.usage == PCD_USAGE.UNKNOWN) {
303 return false;
304 }
305
306 if ((usageInstance.usage == PCD_USAGE.ALWAYS_PRODUCED) ||
307 (usageInstance.usage == PCD_USAGE.SOMETIMES_PRODUCED)) {
308 producers.add(usageInstance);
309 } else {
310 consumers.add(usageInstance);
311 }
312 return true;
313 }
314
315 /**
316 Judge whether exist an usage instance for this token
317
318 @param moduleName Use xmlFilePath as keyword to search the usage instance
319
320 @retval PCD_USAGE - if UsageInstance exists.
321 @retval UNKNOWN - if UsageInstance does not exist, return UNKONW.
322 **/
323 public PCD_USAGE isUsageInstanceExist(String moduleName) {
324 int index;
325 UsageInstance usageInstance;
326
327 if (moduleName == null) {
328 ActionMessage.warning(this, "Error parameter for isUsageInstanceExist() function!");
329 return PCD_USAGE.UNKNOWN;
330 }
331
332 if (moduleName.length() == 0) {
333 return PCD_USAGE.UNKNOWN;
334 }
335
336 //
337 // Searching the usage instance in module's producer and consumer according to
338 // module's name.
339 //
340 for (index = 0; index < producers.size(); index ++) {
341 usageInstance =(UsageInstance)producers.get(index);
342 if (usageInstance.moduleName.equalsIgnoreCase(moduleName)) {
343 return usageInstance.usage;
344 }
345 }
346
347 for (index = 0; index < consumers.size(); index ++) {
348 usageInstance =(UsageInstance)consumers.get(index);
349 if (usageInstance.moduleName.equalsIgnoreCase(moduleName)) {
350 return usageInstance.usage;
351 }
352 }
353 return PCD_USAGE.UNKNOWN;
354 }
355
356 /**
357 Get usage instance according to a MSA file name
358
359 @param moduleName The file path string of MSA file.
360
361 @return usage instance object.
362 **/
363 public UsageInstance getUsageInstance(String moduleName) {
364 int usageIndex;
365 UsageInstance usageInstance;
366
367 if (moduleName == null) {
368 ActionMessage.warning(this, "Error parameter for isUsageInstanceExist() function!");
369 return null;
370 }
371
372 if (moduleName.length() == 0) {
373 return null;
374 }
375
376 if (producers.size() != 0) {
377 for (usageIndex = 0; usageIndex < producers.size(); usageIndex ++) {
378 usageInstance =(UsageInstance)producers.get(usageIndex);
379 if (usageInstance.moduleName.equalsIgnoreCase(moduleName)) {
380 return usageInstance;
381 }
382 }
383 }
384
385 if (consumers.size() != 0) {
386 for (usageIndex = 0; usageIndex < consumers.size(); usageIndex ++) {
387 usageInstance =(UsageInstance)consumers.get(usageIndex);
388 if (usageInstance.moduleName.equalsIgnoreCase(moduleName)) {
389 return usageInstance;
390 }
391 }
392 }
393 return null;
394 }
395
396 /**
397 Get the PCD_TYPE according to the string of PCD_TYPE
398
399 @param pcdTypeStr The string of PCD_TYPE
400
401 @return PCD_TYPE
402 **/
403 public static PCD_TYPE getpcdTypeFromString(String pcdTypeStr) {
404 if (pcdTypeStr == null) {
405 return PCD_TYPE.UNKNOWN;
406 }
407
408 if (pcdTypeStr.equalsIgnoreCase("FEATURE_FLAG")) {
409 return PCD_TYPE.FEATURE_FLAG;
410 } else if (pcdTypeStr.equalsIgnoreCase("FIXED_AT_BUILD")) {
411 return PCD_TYPE.FIXED_AT_BUILD;
412 } else if (pcdTypeStr.equalsIgnoreCase("PATCHABLE_IN_MODULE")) {
413 return PCD_TYPE.PATCHABLE_IN_MODULE;
414 } else if (pcdTypeStr.equalsIgnoreCase("DYNAMIC")) {
415 return PCD_TYPE.DYNAMIC;
416 } else if (pcdTypeStr.equalsIgnoreCase("DYNAMIC_EX")) {
417 return PCD_TYPE.DYNAMIC_EX;
418 } else {
419 return PCD_TYPE.UNKNOWN;
420 }
421 }
422
423 /**
424 Get the string of given datumType. This string will be used for generating autogen files
425
426 @param datumType Given datumType
427
428 @return The string of datum type.
429 **/
430 public static String getStringOfdatumType(DATUM_TYPE datumType) {
431 switch (datumType) {
432 case UINT8:
433 return "UINT8";
434 case UINT16:
435 return "UINT16";
436 case UINT32:
437 return "UINT32";
438 case UINT64:
439 return "UINT64";
440 case POINTER:
441 return "POINTER";
442 case BOOLEAN:
443 return "BOOLEAN";
444 }
445 return "UNKNOWN";
446 }
447
448 /**
449 Get the datumType according to a string.
450
451 @param datumTypeStr The string of datumType
452
453 @return DATUM_TYPE
454 **/
455 public static DATUM_TYPE getdatumTypeFromString(String datumTypeStr) {
456 if (datumTypeStr.equalsIgnoreCase("UINT8")) {
457 return DATUM_TYPE.UINT8;
458 } else if (datumTypeStr.equalsIgnoreCase("UINT16")) {
459 return DATUM_TYPE.UINT16;
460 } else if (datumTypeStr.equalsIgnoreCase("UINT32")) {
461 return DATUM_TYPE.UINT32;
462 } else if (datumTypeStr.equalsIgnoreCase("UINT64")) {
463 return DATUM_TYPE.UINT64;
464 } else if (datumTypeStr.equalsIgnoreCase("VOID*")) {
465 return DATUM_TYPE.POINTER;
466 } else if (datumTypeStr.equalsIgnoreCase("BOOLEAN")) {
467 return DATUM_TYPE.BOOLEAN;
468 }
469 return DATUM_TYPE.UNKNOWN;
470 }
471
472 /**
473 Get string of given pcdType
474
475 @param pcdType The given PcdType
476
477 @return The string of PCD_TYPE.
478 **/
479 public static String getStringOfpcdType(PCD_TYPE pcdType) {
480 switch (pcdType) {
481 case FEATURE_FLAG:
482 return "FEATURE_FLAG";
483 case FIXED_AT_BUILD:
484 return "FIXED_AT_BUILD";
485 case PATCHABLE_IN_MODULE:
486 return "PATCHABLE_IN_MODULE";
487 case DYNAMIC:
488 return "DYNAMIC";
489 case DYNAMIC_EX:
490 return "DYNAMIC_EX";
491 }
492 return "UNKNOWN";
493 }
494
495 /**
496 Get the PCD_USAGE according to a string
497
498 @param usageStr The string of PCD_USAGE
499
500 @return The PCD_USAGE
501 **/
502 public static PCD_USAGE getUsageFromString(String usageStr) {
503 if (usageStr == null) {
504 return PCD_USAGE.UNKNOWN;
505 }
506
507 if (usageStr.equalsIgnoreCase("ALWAYS_PRODUCED")) {
508 return PCD_USAGE.ALWAYS_PRODUCED;
509 } else if (usageStr.equalsIgnoreCase("SOMETIMES_PRODUCED")) {
510 return PCD_USAGE.SOMETIMES_PRODUCED;
511 } else if (usageStr.equalsIgnoreCase("ALWAYS_CONSUMED")) {
512 return PCD_USAGE.ALWAYS_CONSUMED;
513 } else if (usageStr.equalsIgnoreCase("SOMETIMES_CONSUMED")) {
514 return PCD_USAGE.SOMETIMES_CONSUMED;
515 }
516
517 return PCD_USAGE.UNKNOWN;
518 }
519
520 /**
521 Get the string of given PCD_USAGE
522
523 @param usage The given PCD_USAGE
524
525 @return The string of PDC_USAGE.
526 **/
527 public static String getStringOfUsage(PCD_USAGE usage) {
528 switch (usage) {
529 case ALWAYS_PRODUCED:
530 return "ALWAYS_PRODUCED";
531 case ALWAYS_CONSUMED:
532 return "ALWAYS_CONSUMED";
533 case SOMETIMES_PRODUCED:
534 return "SOMETIMES_PRODUCED";
535 case SOMETIMES_CONSUMED:
536 return "SOMETIMES_CONSUMED";
537 }
538 return "UNKNOWN";
539 }
540
541 /**
542 Get the Defined datumType string for autogen. The string is for generating some MACROs in Autogen.h
543
544 @param datumType The given datumType
545
546 @return string of datum type for autogen.
547 **/
548 public static String GetAutogenDefinedatumTypeString(DATUM_TYPE datumType) {
549 switch (datumType) {
550
551 case UINT8:
552 return "8";
553 case UINT16:
554 return "16";
555 case BOOLEAN:
556 return "BOOL";
557 case POINTER:
558 return "PTR";
559 case UINT32:
560 return "32";
561 case UINT64:
562 return "64";
563 default:
564 return null;
565 }
566 }
567
568 /**
569 Get the datumType String for Autogen. This string will be used for generating defintions of PCD token in autogen
570
571 @param datumType The given datumType
572
573 @return string of datum type.
574 **/
575 public static String getAutogendatumTypeString(DATUM_TYPE datumType) {
576 switch (datumType) {
577 case UINT8:
578 return "UINT8";
579 case UINT16:
580 return "UINT16";
581 case UINT32:
582 return "UINT32";
583 case UINT64:
584 return "UINT64";
585 case POINTER:
586 return "VOID*";
587 case BOOLEAN:
588 return "BOOLEAN";
589 }
590 return null;
591 }
592
593 /**
594 Get the datumType string for generating some MACROs in autogen file of Library
595
596 @param datumType The given datumType
597
598 @return String of datum for genrating bit charater.
599 **/
600 public static String getAutogenLibrarydatumTypeString(DATUM_TYPE datumType) {
601 switch (datumType) {
602 case UINT8:
603 return "8";
604 case UINT16:
605 return "16";
606 case BOOLEAN:
607 return "Bool";
608 case POINTER:
609 return "Ptr";
610 case UINT32:
611 return "32";
612 case UINT64:
613 return "64";
614 default:
615 return null;
616 }
617 }
618
619 /**
620 UUID defined in Schems is object, this function is to tranlate this object
621 to UUID data.
622
623 @param uuidObj The object comes from schema.
624
625 @return The traslated UUID instance.
626 **/
627 public static UUID getGUIDFromSchemaObject(Object uuidObj) {
628 UUID uuid;
629 if (uuidObj.toString().equalsIgnoreCase("0")) {
630 uuid = new UUID(0,0);
631 } else {
632 uuid = UUID.fromString(uuidObj.toString());
633 }
634
635 return uuid;
636 }
637 }
638
639
640
641