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);
75 memcpy (Token
, &FileBuffer
[TokenStart
], TokenEnd
- TokenStart
);
76 Token
[TokenEnd
- TokenStart
] = 0;
79 PcdList
[PcdIndex
].SkuName
= Token
;
82 PcdList
[PcdIndex
].DefaultValueName
= Token
;
85 PcdList
[PcdIndex
].TokenSpaceGuidName
= Token
;
88 PcdList
[PcdIndex
].TokenName
= Token
;
91 PcdList
[PcdIndex
].DataType
= Token
;
92 if (strcmp (Token
, "BOOLEAN") == 0) {
93 PcdList
[PcdIndex
].PcdDataType
= PcdDataTypeBoolean
;
94 } else if (strcmp (Token
, "UINT8") == 0) {
95 PcdList
[PcdIndex
].PcdDataType
= PcdDataTypeUint8
;
96 } else if (strcmp (Token
, "UINT16") == 0) {
97 PcdList
[PcdIndex
].PcdDataType
= PcdDataTypeUint16
;
98 } else if (strcmp (Token
, "UINT32") == 0) {
99 PcdList
[PcdIndex
].PcdDataType
= PcdDataTypeUint32
;
100 } else if (strcmp (Token
, "UINT64") == 0) {
101 PcdList
[PcdIndex
].PcdDataType
= PcdDataTypeUint64
;
103 PcdList
[PcdIndex
].PcdDataType
= PcdDataTypePointer
;
107 PcdList
[PcdIndex
].Value
= Token
;
118 CHAR8
*SkuName OPTIONAL
,
119 CHAR8
*DefaultValueName OPTIONAL
,
120 CHAR8
*TokenSpaceGuidName
,
127 Get PCD index in Pcd database
131 SkuName SkuName String
132 DefaultValueName DefaultValueName String
133 TokenSpaceGuidName TokenSpaceGuidName String
134 TokenName TokenName String
138 Index of PCD in Pcd database
143 if (SkuName
== NULL
) {
146 if (DefaultValueName
== NULL
) {
147 DefaultValueName
= "DEFAULT";
149 for (Index
= 0; Index
< PcdListLength
; Index
++) {
150 if (strcmp(PcdList
[Index
].TokenSpaceGuidName
, TokenSpaceGuidName
) != 0) {
153 if (strcmp(PcdList
[Index
].TokenName
, TokenName
) != 0) {
156 if (strcmp(PcdList
[Index
].SkuName
, SkuName
) != 0) {
159 if (strcmp(PcdList
[Index
].DefaultValueName
, DefaultValueName
) != 0) {
169 CHAR8
*SkuName OPTIONAL
,
170 CHAR8
*DefaultValueName OPTIONAL
,
171 CHAR8
*TokenSpaceGuidName
,
182 SkuName SkuName String
183 DefaultValueName DefaultValueName String
184 TokenSpaceGuidName TokenSpaceGuidName String
185 TokenName TokenName String
195 Index
= LookupPcdIndex (SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
197 fprintf (stderr
, "PCD %s.%s.%s.%s is not in database\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
200 switch (PcdList
[Index
].PcdDataType
) {
201 case PcdDataTypeBoolean
:
202 case PcdDataTypeUint8
:
203 case PcdDataTypeUint16
:
204 case PcdDataTypeUint32
:
205 return (UINT64
)strtoul(PcdList
[Index
].Value
, &End
, 16);
207 case PcdDataTypeUint64
:
208 return (UINT64
)strtoul(PcdList
[Index
].Value
, &End
, 16);
210 case PcdDataTypePointer
:
211 fprintf (stderr
, "PCD %s.%s.%s.%s is structure. Use PcdGetPtr()\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
220 CHAR8
*SkuName OPTIONAL
,
221 CHAR8
*DefaultValueName OPTIONAL
,
222 CHAR8
*TokenSpaceGuidName
,
234 SkuName SkuName String
235 DefaultValueName DefaultValueName String
236 TokenSpaceGuidName TokenSpaceGuidName String
237 TokenName TokenName String
238 Value PCD value to be set
247 Index
= LookupPcdIndex (SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
249 fprintf (stderr
, "PCD %s.%s.%s.%s is not in database\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
252 free(PcdList
[Index
].Value
);
253 PcdList
[Index
].Value
= malloc(20);
254 switch (PcdList
[Index
].PcdDataType
) {
255 case PcdDataTypeBoolean
:
257 strcpy (PcdList
[Index
].Value
, "0x00");
259 strcpy (PcdList
[Index
].Value
, "0x01");
262 case PcdDataTypeUint8
:
263 sprintf(PcdList
[Index
].Value
, "0x%02x", (UINT8
)(Value
& 0xff));
265 case PcdDataTypeUint16
:
266 sprintf(PcdList
[Index
].Value
, "0x%04x", (UINT16
)(Value
& 0xffff));
268 case PcdDataTypeUint32
:
269 sprintf(PcdList
[Index
].Value
, "0x%08x", (UINT32
)(Value
& 0xffffffff));
271 case PcdDataTypeUint64
:
272 sprintf(PcdList
[Index
].Value
, "0x%016llx", (unsigned long long)Value
);
274 case PcdDataTypePointer
:
275 fprintf (stderr
, "PCD %s.%s.%s.%s is structure. Use PcdSetPtr()\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
283 CHAR8
*SkuName OPTIONAL
,
284 CHAR8
*DefaultValueName OPTIONAL
,
285 CHAR8
*TokenSpaceGuidName
,
297 SkuName SkuName String
298 DefaultValueName DefaultValueName String
299 TokenSpaceGuidName TokenSpaceGuidName String
300 TokenName TokenName String
301 Size Size of PCD value buffer
314 Index
= LookupPcdIndex (SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
316 fprintf (stderr
, "PCD %s.%s.%s.%s is not in database\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
319 switch (PcdList
[Index
].PcdDataType
) {
320 case PcdDataTypeBoolean
:
321 case PcdDataTypeUint8
:
322 case PcdDataTypeUint16
:
323 case PcdDataTypeUint32
:
324 case PcdDataTypeUint64
:
325 fprintf (stderr
, "PCD %s.%s.%s.%s is a value. Use PcdGet()\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
328 case PcdDataTypePointer
:
329 Value
= &PcdList
[Index
].Value
[1];
330 printf ("Value = %s\n", PcdList
[Index
].Value
);
331 for (*Size
= 0, Byte
= (UINT8
) strtoul(Value
, &End
, 16); Value
!= End
; Byte
= (UINT8
) strtoul(Value
, &End
, 16), *Size
= *Size
+ 1) {
332 printf("%x\n", Byte
);
335 Buffer
= malloc(*Size
);
336 Value
= &PcdList
[Index
].Value
[1];
337 for (*Size
= 0, Buffer
[*Size
] = (UINT8
) strtoul(Value
, &End
, 16); Value
!= End
; *Size
= *Size
+ 1, Buffer
[*Size
] = (UINT8
) strtoul(Value
, &End
, 16)) {
348 CHAR8
*SkuName OPTIONAL
,
349 CHAR8
*DefaultValueName OPTIONAL
,
350 CHAR8
*TokenSpaceGuidName
,
363 SkuName SkuName String
364 DefaultValueName DefaultValueName String
365 TokenSpaceGuidName TokenSpaceGuidName String
366 TokenName TokenName String
367 Size Size of PCD value
368 Value Pointer to the updated PCD value buffer
378 Index
= LookupPcdIndex (SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
380 fprintf (stderr
, "PCD %s.%s.%s.%s is not in database\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
383 switch (PcdList
[Index
].PcdDataType
) {
384 case PcdDataTypeBoolean
:
385 case PcdDataTypeUint8
:
386 case PcdDataTypeUint16
:
387 case PcdDataTypeUint32
:
388 case PcdDataTypeUint64
:
389 fprintf (stderr
, "PCD %s.%s.%s.%s is a value. Use PcdGet()\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
392 case PcdDataTypePointer
:
393 free(PcdList
[Index
].Value
);
394 PcdList
[Index
].Value
= malloc(Size
* 5 + 3);
395 PcdList
[Index
].Value
[0] = '{';
396 for (ValueIndex
= 0; ValueIndex
< Size
; ValueIndex
++) {
397 printf("Value[%d] = %02x\n", ValueIndex
, Value
[ValueIndex
]);
398 sprintf(&PcdList
[Index
].Value
[1 + ValueIndex
* 5], "0x%02x,", Value
[ValueIndex
]);
400 PcdList
[Index
].Value
[1 + Size
* 5 - 1] = '}';
401 PcdList
[Index
].Value
[1 + Size
* 5 ] = 0;
409 CHAR8
*InputFileName
,
417 Read the file buffer from the input file.
421 InputFileName Point to the input file name.
422 FileBuffer Point to the input file buffer.
423 FileSize Size of the file buffer.
434 // Open Input file and read file data.
436 InputFile
= fopen (InputFileName
, "rb");
437 if (InputFile
== NULL
) {
438 fprintf (stderr
, "Error opening file %s\n", InputFileName
);
443 // Go to the end so that we can determine the file size
445 if (fseek (InputFile
, 0, SEEK_END
)) {
446 fprintf (stderr
, "Error reading input file %s\n", InputFileName
);
454 *FileSize
= ftell (InputFile
);
455 if (*FileSize
== -1) {
456 fprintf (stderr
, "Error parsing the input file %s\n", InputFileName
);
464 *FileBuffer
= malloc (*FileSize
);
465 if (*FileBuffer
== NULL
) {
466 fprintf (stderr
, "Can not allocate buffer for input input file %s\n", InputFileName
);
472 // Reset to the beginning of the file
474 if (fseek (InputFile
, 0, SEEK_SET
)) {
475 fprintf (stderr
, "Error reading the input file %s\n", InputFileName
);
482 // Read all of the file contents.
484 BytesRead
= fread (*FileBuffer
, sizeof (UINT8
), *FileSize
, InputFile
);
485 if (BytesRead
!= *FileSize
* sizeof (UINT8
)) {
486 fprintf (stderr
, "Error reading the input file %s\n", InputFileName
);
508 Read the initial PCD value from the input file buffer.
512 FileBuffer Point to the input file buffer.
513 FileSize Size of the file buffer.
525 for (Index
= 0, NumLines
= 0; Index
< FileSize
; Index
++) {
526 if (FileBuffer
[Index
] == '\n') {
530 PcdList
= malloc((NumLines
+ 1) * sizeof(PcdList
[0]));
532 for (Index
= 0, TokenIndex
= 0, PcdListLength
= 0, TokenStart
= 0; Index
< FileSize
; Index
++) {
533 if (FileBuffer
[Index
] == ' ') {
536 if (FileBuffer
[Index
] == '|' || FileBuffer
[Index
] == '.' || FileBuffer
[Index
] == '\n' || FileBuffer
[Index
] == '\r') {
537 RecordToken (FileBuffer
, PcdListLength
, TokenIndex
, TokenStart
, Index
);
538 if (FileBuffer
[Index
] == '\n' || FileBuffer
[Index
] == '\r') {
539 if (TokenIndex
!= 0) {
546 TokenStart
= Index
+ 1;
550 if (Index
> TokenStart
) {
551 RecordToken (FileBuffer
, PcdListLength
, TokenIndex
, TokenStart
, Index
);
552 if (TokenIndex
!= 0) {
561 CHAR8
*OutputFileName
567 Write the updated PCD value into the output file name.
571 OutputFileName Point to the output file name.
584 OutputFile
= fopen (OutputFileName
, "wb");
585 if (OutputFile
== NULL
) {
586 fprintf (stderr
, "Error opening file %s\n", OutputFileName
);
590 for (Index
= 0; Index
< PcdListLength
; Index
++) {
593 "%s.%s.%s.%s|%s|%s\n",
594 PcdList
[Index
].SkuName
,
595 PcdList
[Index
].DefaultValueName
,
596 PcdList
[Index
].TokenSpaceGuidName
,
597 PcdList
[Index
].TokenName
,
598 PcdList
[Index
].DataType
,
604 // Done, write output file.
606 if (OutputFile
!= NULL
) {
620 Displays the utility usage syntax to STDOUT
632 fprintf (stdout
, "Usage: -i <input_file> -o <output_file>\n\n");
633 fprintf (stdout
, "optional arguments:\n");
634 fprintf (stdout
, " -h, --help Show this help message and exit\n");
635 fprintf (stdout
, " -i INPUT_FILENAME, --input INPUT_FILENAME\n\
636 PCD Database Input file name\n");
637 fprintf (stdout
, " -o OUTPUT_FILENAME, --output OUTPUT_FILENAME\n\
638 PCD Database Output file name\n");
646 CHAR8
**InputFileName
,
647 CHAR8
**OutputFileName
653 Parse the input parameters to get the input/output file name.
657 argc Number of command line parameters.
658 argv Array of pointers to parameter strings.
659 InputFileName Point to the input file name.
660 OutputFileName Point to the output file name.
668 fprintf (stderr
, "Missing options\n");
673 // Parse command line
678 if ((stricmp (argv
[0], "-h") == 0) || (stricmp (argv
[0], "--help") == 0)) {
684 if ((stricmp (argv
[0], "-i") == 0) || (stricmp (argv
[0], "--input") == 0)) {
685 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
686 fprintf (stderr
, "Invalid option value. Input File name is missing for -i option\n");
689 *InputFileName
= argv
[1];
695 if ((stricmp (argv
[0], "-o") == 0) || (stricmp (argv
[0], "--output") == 0)) {
696 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
697 fprintf (stderr
, "Invalid option value. Output File name is missing for -i option\n");
700 *OutputFileName
= argv
[1];
706 if (argv
[0][0] == '-') {
707 fprintf (stderr
, "Unknown option %s\n", argv
[0]);
715 // Check Input paramters
717 if (*InputFileName
== NULL
) {
718 fprintf (stderr
, "Missing option. Input files is not specified\n");
721 printf ("Input file name is %s\n", *InputFileName
);
724 if (*OutputFileName
== NULL
) {
725 fprintf (stderr
, "Missing option. Output file is not specified\n");
728 printf ("Output file name is %s\n", *OutputFileName
);
741 Main function updates PCD values.
745 argc Number of command line parameters.
746 argv Array of pointers to parameter strings.
752 CHAR8
*InputFileName
;
753 CHAR8
*OutputFileName
;
757 printf ("PCD tool start.\n");
758 InputFileName
= NULL
;
759 OutputFileName
= NULL
;
762 // Parse the input arguments
764 ParseArguments (argc
, argv
, &InputFileName
, &OutputFileName
);
767 // Open Input file and read file data.
769 ReadInputFile (InputFileName
, &FileBuffer
, &FileSize
);
772 // Read the initial Pcd value
774 ParseFile (FileBuffer
, FileSize
);
777 // Customize PCD values in the PCD Database
782 // Save the updated PCD value
784 WriteOutputFile (OutputFileName
);
786 printf ("PCD tool done.\n");