2 This file contains the PcdValue structure definition.
4 Copyright (c) 2017, 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
;
115 CHAR8
*SkuName OPTIONAL
,
116 CHAR8
*DefaultValueName OPTIONAL
,
117 CHAR8
*TokenSpaceGuidName
,
124 Get PCD index in Pcd database
128 SkuName SkuName String
129 DefaultValueName DefaultValueName String
130 TokenSpaceGuidName TokenSpaceGuidName String
131 TokenName TokenName String
135 Index of PCD in Pcd database
140 if (SkuName
== NULL
) {
143 if (DefaultValueName
== NULL
) {
144 DefaultValueName
= "DEFAULT";
146 for (Index
= 0; Index
< PcdListLength
; Index
++) {
147 if (strcmp(PcdList
[Index
].TokenSpaceGuidName
, TokenSpaceGuidName
) != 0) {
150 if (strcmp(PcdList
[Index
].TokenName
, TokenName
) != 0) {
153 if (strcmp(PcdList
[Index
].SkuName
, SkuName
) != 0) {
156 if (strcmp(PcdList
[Index
].DefaultValueName
, DefaultValueName
) != 0) {
166 CHAR8
*SkuName OPTIONAL
,
167 CHAR8
*DefaultValueName OPTIONAL
,
168 CHAR8
*TokenSpaceGuidName
,
179 SkuName SkuName String
180 DefaultValueName DefaultValueName String
181 TokenSpaceGuidName TokenSpaceGuidName String
182 TokenName TokenName String
192 Index
= LookupPcdIndex (SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
194 fprintf (stderr
, "PCD %s.%s.%s.%s is not in database\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
197 switch (PcdList
[Index
].PcdDataType
) {
198 case PcdDataTypeBoolean
:
199 case PcdDataTypeUint8
:
200 case PcdDataTypeUint16
:
201 case PcdDataTypeUint32
:
202 return (UINT64
)strtoul(PcdList
[Index
].Value
, &End
, 16);
204 case PcdDataTypeUint64
:
205 return (UINT64
)strtoul(PcdList
[Index
].Value
, &End
, 16);
207 case PcdDataTypePointer
:
208 fprintf (stderr
, "PCD %s.%s.%s.%s is structure. Use PcdGetPtr()\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
217 CHAR8
*SkuName OPTIONAL
,
218 CHAR8
*DefaultValueName OPTIONAL
,
219 CHAR8
*TokenSpaceGuidName
,
231 SkuName SkuName String
232 DefaultValueName DefaultValueName String
233 TokenSpaceGuidName TokenSpaceGuidName String
234 TokenName TokenName String
235 Value PCD value to be set
244 Index
= LookupPcdIndex (SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
246 fprintf (stderr
, "PCD %s.%s.%s.%s is not in database\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
249 free(PcdList
[Index
].Value
);
250 PcdList
[Index
].Value
= malloc(20);
251 switch (PcdList
[Index
].PcdDataType
) {
252 case PcdDataTypeBoolean
:
254 strcpy (PcdList
[Index
].Value
, "0x00");
256 strcpy (PcdList
[Index
].Value
, "0x01");
259 case PcdDataTypeUint8
:
260 sprintf(PcdList
[Index
].Value
, "0x%02x", (UINT8
)(Value
& 0xff));
262 case PcdDataTypeUint16
:
263 sprintf(PcdList
[Index
].Value
, "0x%04x", (UINT16
)(Value
& 0xffff));
265 case PcdDataTypeUint32
:
266 sprintf(PcdList
[Index
].Value
, "0x%08x", (UINT32
)(Value
& 0xffffffff));
268 case PcdDataTypeUint64
:
270 sprintf(PcdList
[Index
].Value
, "0x%016lx", Value
);
272 sprintf(PcdList
[Index
].Value
, "0x%016llx", Value
);
275 case PcdDataTypePointer
:
276 fprintf (stderr
, "PCD %s.%s.%s.%s is structure. Use PcdSetPtr()\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
284 CHAR8
*SkuName OPTIONAL
,
285 CHAR8
*DefaultValueName OPTIONAL
,
286 CHAR8
*TokenSpaceGuidName
,
298 SkuName SkuName String
299 DefaultValueName DefaultValueName String
300 TokenSpaceGuidName TokenSpaceGuidName String
301 TokenName TokenName String
302 Size Size of PCD value buffer
315 Index
= LookupPcdIndex (SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
317 fprintf (stderr
, "PCD %s.%s.%s.%s is not in database\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
320 switch (PcdList
[Index
].PcdDataType
) {
321 case PcdDataTypeBoolean
:
322 case PcdDataTypeUint8
:
323 case PcdDataTypeUint16
:
324 case PcdDataTypeUint32
:
325 case PcdDataTypeUint64
:
326 fprintf (stderr
, "PCD %s.%s.%s.%s is a value. Use PcdGet()\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
329 case PcdDataTypePointer
:
330 Value
= &PcdList
[Index
].Value
[1];
331 printf ("Value = %s\n", PcdList
[Index
].Value
);
332 for (*Size
= 0, Byte
= (UINT8
) strtoul(Value
, &End
, 16); Value
!= End
; Byte
= (UINT8
) strtoul(Value
, &End
, 16), *Size
= *Size
+ 1) {
333 printf("%x\n", Byte
);
336 Buffer
= malloc(*Size
);
337 Value
= &PcdList
[Index
].Value
[1];
338 for (*Size
= 0, Buffer
[*Size
] = (UINT8
) strtoul(Value
, &End
, 16); Value
!= End
; *Size
= *Size
+ 1, Buffer
[*Size
] = (UINT8
) strtoul(Value
, &End
, 16)) {
349 CHAR8
*SkuName OPTIONAL
,
350 CHAR8
*DefaultValueName OPTIONAL
,
351 CHAR8
*TokenSpaceGuidName
,
364 SkuName SkuName String
365 DefaultValueName DefaultValueName String
366 TokenSpaceGuidName TokenSpaceGuidName String
367 TokenName TokenName String
368 Size Size of PCD value
369 Value Pointer to the updated PCD value buffer
379 Index
= LookupPcdIndex (SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
381 fprintf (stderr
, "PCD %s.%s.%s.%s is not in database\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
384 switch (PcdList
[Index
].PcdDataType
) {
385 case PcdDataTypeBoolean
:
386 case PcdDataTypeUint8
:
387 case PcdDataTypeUint16
:
388 case PcdDataTypeUint32
:
389 case PcdDataTypeUint64
:
390 fprintf (stderr
, "PCD %s.%s.%s.%s is a value. Use PcdGet()\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
393 case PcdDataTypePointer
:
394 free(PcdList
[Index
].Value
);
395 PcdList
[Index
].Value
= malloc(Size
* 5 + 3);
396 PcdList
[Index
].Value
[0] = '{';
397 for (ValueIndex
= 0; ValueIndex
< Size
; ValueIndex
++) {
398 printf("Value[%d] = %02x\n", ValueIndex
, Value
[ValueIndex
]);
399 sprintf(&PcdList
[Index
].Value
[1 + ValueIndex
* 5], "0x%02x,", Value
[ValueIndex
]);
401 PcdList
[Index
].Value
[1 + Size
* 5 - 1] = '}';
402 PcdList
[Index
].Value
[1 + Size
* 5 ] = 0;
410 CHAR8
*InputFileName
,
418 Read the file buffer from the input file.
422 InputFileName Point to the input file name.
423 FileBuffer Point to the input file buffer.
424 FileSize Size of the file buffer.
435 // Open Input file and read file data.
437 InputFile
= fopen (InputFileName
, "rb");
438 if (InputFile
== NULL
) {
439 fprintf (stderr
, "Error opening file %s\n", InputFileName
);
444 // Go to the end so that we can determine the file size
446 if (fseek (InputFile
, 0, SEEK_END
)) {
447 fprintf (stderr
, "Error reading input file %s\n", InputFileName
);
455 *FileSize
= ftell (InputFile
);
456 if (*FileSize
== -1) {
457 fprintf (stderr
, "Error parsing the input file %s\n", InputFileName
);
465 *FileBuffer
= malloc (*FileSize
);
466 if (*FileBuffer
== NULL
) {
467 fprintf (stderr
, "Can not allocate buffer for input input file %s\n", InputFileName
);
473 // Reset to the beginning of the file
475 if (fseek (InputFile
, 0, SEEK_SET
)) {
476 fprintf (stderr
, "Error reading the input file %s\n", InputFileName
);
483 // Read all of the file contents.
485 BytesRead
= fread (*FileBuffer
, sizeof (UINT8
), *FileSize
, InputFile
);
486 if (BytesRead
!= *FileSize
* sizeof (UINT8
)) {
487 fprintf (stderr
, "Error reading the input file %s\n", InputFileName
);
509 Read the initial PCD value from the input file buffer.
513 FileBuffer Point to the input file buffer.
514 FileSize Size of the file buffer.
526 for (Index
= 0, NumLines
= 0; Index
< FileSize
; Index
++) {
527 if (FileBuffer
[Index
] == '\n') {
531 PcdList
= malloc((NumLines
+ 1) * sizeof(PcdList
[0]));
533 for (Index
= 0, TokenIndex
= 0, PcdListLength
= 0, TokenStart
= 0; Index
< FileSize
; Index
++) {
534 if (FileBuffer
[Index
] == ' ') {
537 if (FileBuffer
[Index
] == '|' || FileBuffer
[Index
] == '.' || FileBuffer
[Index
] == '\n' || FileBuffer
[Index
] == '\r') {
538 RecordToken (FileBuffer
, PcdListLength
, TokenIndex
, TokenStart
, Index
);
539 if (FileBuffer
[Index
] == '\n' || FileBuffer
[Index
] == '\r') {
540 if (TokenIndex
!= 0) {
547 TokenStart
= Index
+ 1;
551 if (Index
> TokenStart
) {
552 RecordToken (FileBuffer
, PcdListLength
, TokenIndex
, TokenStart
, Index
);
553 if (TokenIndex
!= 0) {
562 CHAR8
*OutputFileName
568 Write the updated PCD value into the output file name.
572 OutputFileName Point to the output file name.
585 OutputFile
= fopen (OutputFileName
, "wb");
586 if (OutputFile
== NULL
) {
587 fprintf (stderr
, "Error opening file %s\n", OutputFileName
);
591 for (Index
= 0; Index
< PcdListLength
; Index
++) {
594 "%s.%s.%s.%s|%s|%s\n",
595 PcdList
[Index
].SkuName
,
596 PcdList
[Index
].DefaultValueName
,
597 PcdList
[Index
].TokenSpaceGuidName
,
598 PcdList
[Index
].TokenName
,
599 PcdList
[Index
].DataType
,
605 // Done, write output file.
607 if (OutputFile
!= NULL
) {
621 Displays the utility usage syntax to STDOUT
633 fprintf (stdout
, "Usage: -i <input_file> -o <output_file>\n\n");
634 fprintf (stdout
, "optional arguments:\n");
635 fprintf (stdout
, " -h, --help Show this help message and exit\n");
636 fprintf (stdout
, " -i INPUT_FILENAME, --input INPUT_FILENAME\n\
637 PCD Database Input file name\n");
638 fprintf (stdout
, " -o OUTPUT_FILENAME, --output OUTPUT_FILENAME\n\
639 PCD Database Output file name\n");
647 CHAR8
**InputFileName
,
648 CHAR8
**OutputFileName
654 Parse the input parameters to get the input/output file name.
658 argc Number of command line parameters.
659 argv Array of pointers to parameter strings.
660 InputFileName Point to the input file name.
661 OutputFileName Point to the output file name.
669 fprintf (stderr
, "Missing options\n");
674 // Parse command line
679 if ((stricmp (argv
[0], "-h") == 0) || (stricmp (argv
[0], "--help") == 0)) {
685 if ((stricmp (argv
[0], "-i") == 0) || (stricmp (argv
[0], "--input") == 0)) {
686 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
687 fprintf (stderr
, "Invalid option value. Input File name is missing for -i option\n");
690 *InputFileName
= argv
[1];
696 if ((stricmp (argv
[0], "-o") == 0) || (stricmp (argv
[0], "--output") == 0)) {
697 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
698 fprintf (stderr
, "Invalid option value. Output File name is missing for -i option\n");
701 *OutputFileName
= argv
[1];
707 if (argv
[0][0] == '-') {
708 fprintf (stderr
, "Unknown option %s\n", argv
[0]);
716 // Check Input paramters
718 if (*InputFileName
== NULL
) {
719 fprintf (stderr
, "Missing option. Input files is not specified\n");
722 printf ("Input file name is %s\n", *InputFileName
);
725 if (*OutputFileName
== NULL
) {
726 fprintf (stderr
, "Missing option. Output file is not specified\n");
729 printf ("Output file name is %s\n", *OutputFileName
);
742 Main function updates PCD values.
746 argc Number of command line parameters.
747 argv Array of pointers to parameter strings.
753 CHAR8
*InputFileName
;
754 CHAR8
*OutputFileName
;
758 printf ("PCD tool start.\n");
759 InputFileName
= NULL
;
760 OutputFileName
= NULL
;
763 // Parse the input arguments
765 ParseArguments (argc
, argv
, &InputFileName
, &OutputFileName
);
768 // Open Input file and read file data.
770 ReadInputFile (InputFileName
, &FileBuffer
, &FileSize
);
773 // Read the initial Pcd value
775 ParseFile (FileBuffer
, FileSize
);
778 // Customize PCD values in the PCD Database
783 // Save the updated PCD value
785 WriteOutputFile (OutputFileName
);
787 printf ("PCD tool done.\n");