2 This file contains the PcdValue structure definition.
4 Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include "CommonLib.h"
20 #include "PcdValueCommon.h"
33 CHAR8
*DefaultValueName
;
34 CHAR8
*TokenSpaceGuidName
;
38 PCD_DATA_TYPE PcdDataType
;
57 Record new token information
61 FileBuffer File Buffer to be record
62 PcdIndex Index of PCD in database
63 TokenIndex Index of Token
64 TokenStart Start of Token
74 Token
= malloc (TokenEnd
- TokenStart
+ 1);
78 memcpy (Token
, &FileBuffer
[TokenStart
], TokenEnd
- TokenStart
);
79 Token
[TokenEnd
- TokenStart
] = 0;
82 PcdList
[PcdIndex
].SkuName
= Token
;
85 PcdList
[PcdIndex
].DefaultValueName
= Token
;
88 PcdList
[PcdIndex
].TokenSpaceGuidName
= Token
;
91 PcdList
[PcdIndex
].TokenName
= Token
;
94 PcdList
[PcdIndex
].DataType
= Token
;
95 if (strcmp (Token
, "BOOLEAN") == 0) {
96 PcdList
[PcdIndex
].PcdDataType
= PcdDataTypeBoolean
;
97 } else if (strcmp (Token
, "UINT8") == 0) {
98 PcdList
[PcdIndex
].PcdDataType
= PcdDataTypeUint8
;
99 } else if (strcmp (Token
, "UINT16") == 0) {
100 PcdList
[PcdIndex
].PcdDataType
= PcdDataTypeUint16
;
101 } else if (strcmp (Token
, "UINT32") == 0) {
102 PcdList
[PcdIndex
].PcdDataType
= PcdDataTypeUint32
;
103 } else if (strcmp (Token
, "UINT64") == 0) {
104 PcdList
[PcdIndex
].PcdDataType
= PcdDataTypeUint64
;
106 PcdList
[PcdIndex
].PcdDataType
= PcdDataTypePointer
;
110 PcdList
[PcdIndex
].Value
= Token
;
121 CHAR8
*SkuName OPTIONAL
,
122 CHAR8
*DefaultValueName OPTIONAL
,
123 CHAR8
*TokenSpaceGuidName
,
130 Get PCD index in Pcd database
134 SkuName SkuName String
135 DefaultValueName DefaultValueName String
136 TokenSpaceGuidName TokenSpaceGuidName String
137 TokenName TokenName String
141 Index of PCD in Pcd database
146 if (SkuName
== NULL
) {
149 if (DefaultValueName
== NULL
) {
150 DefaultValueName
= "DEFAULT";
152 for (Index
= 0; Index
< PcdListLength
; Index
++) {
153 if (strcmp(PcdList
[Index
].TokenSpaceGuidName
, TokenSpaceGuidName
) != 0) {
156 if (strcmp(PcdList
[Index
].TokenName
, TokenName
) != 0) {
159 if (strcmp(PcdList
[Index
].SkuName
, SkuName
) != 0) {
162 if (strcmp(PcdList
[Index
].DefaultValueName
, DefaultValueName
) != 0) {
172 CHAR8
*SkuName OPTIONAL
,
173 CHAR8
*DefaultValueName OPTIONAL
,
174 CHAR8
*TokenSpaceGuidName
,
185 SkuName SkuName String
186 DefaultValueName DefaultValueName String
187 TokenSpaceGuidName TokenSpaceGuidName String
188 TokenName TokenName String
198 Index
= LookupPcdIndex (SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
200 fprintf (stderr
, "PCD %s.%s.%s.%s is not in database\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
203 switch (PcdList
[Index
].PcdDataType
) {
204 case PcdDataTypeBoolean
:
205 case PcdDataTypeUint8
:
206 case PcdDataTypeUint16
:
207 case PcdDataTypeUint32
:
208 return (UINT64
)strtoul(PcdList
[Index
].Value
, &End
, 16);
210 case PcdDataTypeUint64
:
211 return (UINT64
)strtoul(PcdList
[Index
].Value
, &End
, 16);
213 case PcdDataTypePointer
:
214 fprintf (stderr
, "PCD %s.%s.%s.%s is structure. Use PcdGetPtr()\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
223 CHAR8
*SkuName OPTIONAL
,
224 CHAR8
*DefaultValueName OPTIONAL
,
225 CHAR8
*TokenSpaceGuidName
,
237 SkuName SkuName String
238 DefaultValueName DefaultValueName String
239 TokenSpaceGuidName TokenSpaceGuidName String
240 TokenName TokenName String
241 Value PCD value to be set
250 Index
= LookupPcdIndex (SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
252 fprintf (stderr
, "PCD %s.%s.%s.%s is not in database\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
255 free(PcdList
[Index
].Value
);
256 PcdList
[Index
].Value
= malloc(20);
257 switch (PcdList
[Index
].PcdDataType
) {
258 case PcdDataTypeBoolean
:
260 strcpy (PcdList
[Index
].Value
, "0x00");
262 strcpy (PcdList
[Index
].Value
, "0x01");
265 case PcdDataTypeUint8
:
266 sprintf(PcdList
[Index
].Value
, "0x%02x", (UINT8
)(Value
& 0xff));
268 case PcdDataTypeUint16
:
269 sprintf(PcdList
[Index
].Value
, "0x%04x", (UINT16
)(Value
& 0xffff));
271 case PcdDataTypeUint32
:
272 sprintf(PcdList
[Index
].Value
, "0x%08x", (UINT32
)(Value
& 0xffffffff));
274 case PcdDataTypeUint64
:
275 sprintf(PcdList
[Index
].Value
, "0x%016llx", (unsigned long long)Value
);
277 case PcdDataTypePointer
:
278 fprintf (stderr
, "PCD %s.%s.%s.%s is structure. Use PcdSetPtr()\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
286 CHAR8
*SkuName OPTIONAL
,
287 CHAR8
*DefaultValueName OPTIONAL
,
288 CHAR8
*TokenSpaceGuidName
,
300 SkuName SkuName String
301 DefaultValueName DefaultValueName String
302 TokenSpaceGuidName TokenSpaceGuidName String
303 TokenName TokenName String
304 Size Size of PCD value buffer
317 Index
= LookupPcdIndex (SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
319 fprintf (stderr
, "PCD %s.%s.%s.%s is not in database\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
322 switch (PcdList
[Index
].PcdDataType
) {
323 case PcdDataTypeBoolean
:
324 case PcdDataTypeUint8
:
325 case PcdDataTypeUint16
:
326 case PcdDataTypeUint32
:
327 case PcdDataTypeUint64
:
328 fprintf (stderr
, "PCD %s.%s.%s.%s is a value. Use PcdGet()\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
331 case PcdDataTypePointer
:
332 Value
= &PcdList
[Index
].Value
[1];
333 printf ("Value = %s\n", PcdList
[Index
].Value
);
334 for (*Size
= 0, Byte
= (UINT8
) strtoul(Value
, &End
, 16); Value
!= End
; Byte
= (UINT8
) strtoul(Value
, &End
, 16), *Size
= *Size
+ 1) {
335 printf("%x\n", Byte
);
338 Buffer
= malloc(*Size
);
339 if (Buffer
== NULL
) {
343 Value
= &PcdList
[Index
].Value
[1];
344 for (*Size
= 0, Buffer
[*Size
] = (UINT8
) strtoul(Value
, &End
, 16); Value
!= End
; *Size
= *Size
+ 1, Buffer
[*Size
] = (UINT8
) strtoul(Value
, &End
, 16)) {
355 CHAR8
*SkuName OPTIONAL
,
356 CHAR8
*DefaultValueName OPTIONAL
,
357 CHAR8
*TokenSpaceGuidName
,
370 SkuName SkuName String
371 DefaultValueName DefaultValueName String
372 TokenSpaceGuidName TokenSpaceGuidName String
373 TokenName TokenName String
374 Size Size of PCD value
375 Value Pointer to the updated PCD value buffer
385 Index
= LookupPcdIndex (SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
387 fprintf (stderr
, "PCD %s.%s.%s.%s is not in database\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
390 switch (PcdList
[Index
].PcdDataType
) {
391 case PcdDataTypeBoolean
:
392 case PcdDataTypeUint8
:
393 case PcdDataTypeUint16
:
394 case PcdDataTypeUint32
:
395 case PcdDataTypeUint64
:
396 fprintf (stderr
, "PCD %s.%s.%s.%s is a value. Use PcdGet()\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
399 case PcdDataTypePointer
:
400 free(PcdList
[Index
].Value
);
401 PcdList
[Index
].Value
= malloc(Size
* 5 + 3);
402 PcdList
[Index
].Value
[0] = '{';
403 for (ValueIndex
= 0; ValueIndex
< Size
; ValueIndex
++) {
404 printf("Value[%d] = %02x\n", ValueIndex
, Value
[ValueIndex
]);
405 sprintf(&PcdList
[Index
].Value
[1 + ValueIndex
* 5], "0x%02x,", Value
[ValueIndex
]);
407 PcdList
[Index
].Value
[1 + Size
* 5 - 1] = '}';
408 PcdList
[Index
].Value
[1 + Size
* 5 ] = 0;
416 CHAR8
*InputFileName
,
424 Read the file buffer from the input file.
428 InputFileName Point to the input file name.
429 FileBuffer Point to the input file buffer.
430 FileSize Size of the file buffer.
441 // Open Input file and read file data.
443 InputFile
= fopen (InputFileName
, "rb");
444 if (InputFile
== NULL
) {
445 fprintf (stderr
, "Error opening file %s\n", InputFileName
);
450 // Go to the end so that we can determine the file size
452 if (fseek (InputFile
, 0, SEEK_END
)) {
453 fprintf (stderr
, "Error reading input file %s\n", InputFileName
);
461 *FileSize
= ftell (InputFile
);
462 if (*FileSize
== -1) {
463 fprintf (stderr
, "Error parsing the input file %s\n", InputFileName
);
471 *FileBuffer
= malloc (*FileSize
);
472 if (*FileBuffer
== NULL
) {
473 fprintf (stderr
, "Can not allocate buffer for input input file %s\n", InputFileName
);
479 // Reset to the beginning of the file
481 if (fseek (InputFile
, 0, SEEK_SET
)) {
482 fprintf (stderr
, "Error reading the input file %s\n", InputFileName
);
489 // Read all of the file contents.
491 BytesRead
= fread (*FileBuffer
, sizeof (UINT8
), *FileSize
, InputFile
);
492 if (BytesRead
!= *FileSize
* sizeof (UINT8
)) {
493 fprintf (stderr
, "Error reading the input file %s\n", InputFileName
);
515 Read the initial PCD value from the input file buffer.
519 FileBuffer Point to the input file buffer.
520 FileSize Size of the file buffer.
532 for (Index
= 0, NumLines
= 0; Index
< FileSize
; Index
++) {
533 if (FileBuffer
[Index
] == '\n') {
537 PcdList
= malloc((NumLines
+ 1) * sizeof(PcdList
[0]));
539 for (Index
= 0, TokenIndex
= 0, PcdListLength
= 0, TokenStart
= 0; Index
< FileSize
; Index
++) {
540 if (FileBuffer
[Index
] == ' ') {
543 if (FileBuffer
[Index
] == '|' || FileBuffer
[Index
] == '.' || FileBuffer
[Index
] == '\n' || FileBuffer
[Index
] == '\r') {
544 RecordToken (FileBuffer
, PcdListLength
, TokenIndex
, TokenStart
, Index
);
545 if (FileBuffer
[Index
] == '\n' || FileBuffer
[Index
] == '\r') {
546 if (TokenIndex
!= 0) {
553 TokenStart
= Index
+ 1;
557 if (Index
> TokenStart
) {
558 RecordToken (FileBuffer
, PcdListLength
, TokenIndex
, TokenStart
, Index
);
559 if (TokenIndex
!= 0) {
568 CHAR8
*OutputFileName
574 Write the updated PCD value into the output file name.
578 OutputFileName Point to the output file name.
591 OutputFile
= fopen (OutputFileName
, "wb");
592 if (OutputFile
== NULL
) {
593 fprintf (stderr
, "Error opening file %s\n", OutputFileName
);
597 for (Index
= 0; Index
< PcdListLength
; Index
++) {
600 "%s.%s.%s.%s|%s|%s\n",
601 PcdList
[Index
].SkuName
,
602 PcdList
[Index
].DefaultValueName
,
603 PcdList
[Index
].TokenSpaceGuidName
,
604 PcdList
[Index
].TokenName
,
605 PcdList
[Index
].DataType
,
611 // Done, write output file.
613 if (OutputFile
!= NULL
) {
627 Displays the utility usage syntax to STDOUT
639 fprintf (stdout
, "Usage: -i <input_file> -o <output_file>\n\n");
640 fprintf (stdout
, "optional arguments:\n");
641 fprintf (stdout
, " -h, --help Show this help message and exit\n");
642 fprintf (stdout
, " -i INPUT_FILENAME, --input INPUT_FILENAME\n\
643 PCD Database Input file name\n");
644 fprintf (stdout
, " -o OUTPUT_FILENAME, --output OUTPUT_FILENAME\n\
645 PCD Database Output file name\n");
653 CHAR8
**InputFileName
,
654 CHAR8
**OutputFileName
660 Parse the input parameters to get the input/output file name.
664 argc Number of command line parameters.
665 argv Array of pointers to parameter strings.
666 InputFileName Point to the input file name.
667 OutputFileName Point to the output file name.
675 fprintf (stderr
, "Missing options\n");
680 // Parse command line
685 if ((stricmp (argv
[0], "-h") == 0) || (stricmp (argv
[0], "--help") == 0)) {
691 if ((stricmp (argv
[0], "-i") == 0) || (stricmp (argv
[0], "--input") == 0)) {
692 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
693 fprintf (stderr
, "Invalid option value. Input File name is missing for -i option\n");
696 *InputFileName
= argv
[1];
702 if ((stricmp (argv
[0], "-o") == 0) || (stricmp (argv
[0], "--output") == 0)) {
703 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
704 fprintf (stderr
, "Invalid option value. Output File name is missing for -i option\n");
707 *OutputFileName
= argv
[1];
713 if (argv
[0][0] == '-') {
714 fprintf (stderr
, "Unknown option %s\n", argv
[0]);
722 // Check Input paramters
724 if (*InputFileName
== NULL
) {
725 fprintf (stderr
, "Missing option. Input files is not specified\n");
728 printf ("Input file name is %s\n", *InputFileName
);
731 if (*OutputFileName
== NULL
) {
732 fprintf (stderr
, "Missing option. Output file is not specified\n");
735 printf ("Output file name is %s\n", *OutputFileName
);
748 Main function updates PCD values.
752 argc Number of command line parameters.
753 argv Array of pointers to parameter strings.
759 CHAR8
*InputFileName
;
760 CHAR8
*OutputFileName
;
764 printf ("PCD tool start.\n");
765 InputFileName
= NULL
;
766 OutputFileName
= NULL
;
769 // Parse the input arguments
771 ParseArguments (argc
, argv
, &InputFileName
, &OutputFileName
);
774 // Open Input file and read file data.
776 ReadInputFile (InputFileName
, &FileBuffer
, &FileSize
);
779 // Read the initial Pcd value
781 ParseFile (FileBuffer
, FileSize
);
784 // Customize PCD values in the PCD Database
789 // Save the updated PCD value
791 WriteOutputFile (OutputFileName
);
793 printf ("PCD tool done.\n");