2 This file contains the PcdValue structure definition.
4 Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include "CommonLib.h"
14 #include "PcdValueCommon.h"
27 CHAR8
*DefaultValueName
;
28 CHAR8
*TokenSpaceGuidName
;
32 PCD_DATA_TYPE PcdDataType
;
51 Record new token information
55 FileBuffer File Buffer to be record
56 PcdIndex Index of PCD in database
57 TokenIndex Index of Token
58 TokenStart Start of Token
68 Token
= malloc (TokenEnd
- TokenStart
+ 1);
72 memcpy (Token
, &FileBuffer
[TokenStart
], TokenEnd
- TokenStart
);
73 Token
[TokenEnd
- TokenStart
] = 0;
76 PcdList
[PcdIndex
].SkuName
= Token
;
79 PcdList
[PcdIndex
].DefaultValueName
= Token
;
82 PcdList
[PcdIndex
].TokenSpaceGuidName
= Token
;
85 PcdList
[PcdIndex
].TokenName
= Token
;
88 PcdList
[PcdIndex
].DataType
= Token
;
89 if (strcmp (Token
, "BOOLEAN") == 0) {
90 PcdList
[PcdIndex
].PcdDataType
= PcdDataTypeBoolean
;
91 } else if (strcmp (Token
, "UINT8") == 0) {
92 PcdList
[PcdIndex
].PcdDataType
= PcdDataTypeUint8
;
93 } else if (strcmp (Token
, "UINT16") == 0) {
94 PcdList
[PcdIndex
].PcdDataType
= PcdDataTypeUint16
;
95 } else if (strcmp (Token
, "UINT32") == 0) {
96 PcdList
[PcdIndex
].PcdDataType
= PcdDataTypeUint32
;
97 } else if (strcmp (Token
, "UINT64") == 0) {
98 PcdList
[PcdIndex
].PcdDataType
= PcdDataTypeUint64
;
100 PcdList
[PcdIndex
].PcdDataType
= PcdDataTypePointer
;
104 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
:
269 sprintf(PcdList
[Index
].Value
, "0x%016llx", (unsigned long long)Value
);
271 case PcdDataTypePointer
:
272 fprintf (stderr
, "PCD %s.%s.%s.%s is structure. Use PcdSetPtr()\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
280 CHAR8
*SkuName OPTIONAL
,
281 CHAR8
*DefaultValueName OPTIONAL
,
282 CHAR8
*TokenSpaceGuidName
,
294 SkuName SkuName String
295 DefaultValueName DefaultValueName String
296 TokenSpaceGuidName TokenSpaceGuidName String
297 TokenName TokenName String
298 Size Size of PCD value buffer
310 Index
= LookupPcdIndex (SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
312 fprintf (stderr
, "PCD %s.%s.%s.%s is not in database\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
315 switch (PcdList
[Index
].PcdDataType
) {
316 case PcdDataTypeBoolean
:
317 case PcdDataTypeUint8
:
318 case PcdDataTypeUint16
:
319 case PcdDataTypeUint32
:
320 case PcdDataTypeUint64
:
321 fprintf (stderr
, "PCD %s.%s.%s.%s is a value. Use PcdGet()\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
324 case PcdDataTypePointer
:
325 Value
= &PcdList
[Index
].Value
[1];
326 for (*Size
= 0, strtoul(Value
, &End
, 16); Value
!= End
; strtoul(Value
, &End
, 16), *Size
= *Size
+ 1) {
329 Buffer
= malloc(*Size
+ 1);
330 if (Buffer
== NULL
) {
334 Value
= &PcdList
[Index
].Value
[1];
335 for (*Size
= 0, Buffer
[*Size
] = (UINT8
) strtoul(Value
, &End
, 16); Value
!= End
; *Size
= *Size
+ 1, Buffer
[*Size
] = (UINT8
) strtoul(Value
, &End
, 16)) {
346 CHAR8
*SkuName OPTIONAL
,
347 CHAR8
*DefaultValueName OPTIONAL
,
348 CHAR8
*TokenSpaceGuidName
,
361 SkuName SkuName String
362 DefaultValueName DefaultValueName String
363 TokenSpaceGuidName TokenSpaceGuidName String
364 TokenName TokenName String
365 Size Size of PCD value
366 Value Pointer to the updated PCD value buffer
376 Index
= LookupPcdIndex (SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
378 fprintf (stderr
, "PCD %s.%s.%s.%s is not in database\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
381 switch (PcdList
[Index
].PcdDataType
) {
382 case PcdDataTypeBoolean
:
383 case PcdDataTypeUint8
:
384 case PcdDataTypeUint16
:
385 case PcdDataTypeUint32
:
386 case PcdDataTypeUint64
:
387 fprintf (stderr
, "PCD %s.%s.%s.%s is a value. Use PcdGet()\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
390 case PcdDataTypePointer
:
391 free(PcdList
[Index
].Value
);
392 PcdList
[Index
].Value
= malloc(Size
* 5 + 3);
393 PcdList
[Index
].Value
[0] = '{';
394 for (ValueIndex
= 0; ValueIndex
< Size
; ValueIndex
++) {
395 sprintf(&PcdList
[Index
].Value
[1 + ValueIndex
* 5], "0x%02x,", Value
[ValueIndex
]);
397 PcdList
[Index
].Value
[1 + Size
* 5 - 1] = '}';
398 PcdList
[Index
].Value
[1 + Size
* 5 ] = 0;
406 CHAR8
*InputFileName
,
414 Read the file buffer from the input file.
418 InputFileName Point to the input file name.
419 FileBuffer Point to the input file buffer.
420 FileSize Size of the file buffer.
431 // Open Input file and read file data.
433 InputFile
= fopen (InputFileName
, "rb");
434 if (InputFile
== NULL
) {
435 fprintf (stderr
, "Error opening file %s\n", InputFileName
);
440 // Go to the end so that we can determine the file size
442 if (fseek (InputFile
, 0, SEEK_END
)) {
443 fprintf (stderr
, "Error reading input file %s\n", InputFileName
);
451 *FileSize
= ftell (InputFile
);
452 if (*FileSize
== -1) {
453 fprintf (stderr
, "Error parsing the input file %s\n", InputFileName
);
461 *FileBuffer
= malloc (*FileSize
);
462 if (*FileBuffer
== NULL
) {
463 fprintf (stderr
, "Can not allocate buffer for input input file %s\n", InputFileName
);
469 // Reset to the beginning of the file
471 if (fseek (InputFile
, 0, SEEK_SET
)) {
472 fprintf (stderr
, "Error reading the input file %s\n", InputFileName
);
479 // Read all of the file contents.
481 BytesRead
= fread (*FileBuffer
, sizeof (UINT8
), *FileSize
, InputFile
);
482 if (BytesRead
!= *FileSize
* sizeof (UINT8
)) {
483 fprintf (stderr
, "Error reading the input file %s\n", InputFileName
);
505 Read the initial PCD value from the input file buffer.
509 FileBuffer Point to the input file buffer.
510 FileSize Size of the file buffer.
522 for (Index
= 0, NumLines
= 0; Index
< FileSize
; Index
++) {
523 if (FileBuffer
[Index
] == '\n') {
527 PcdList
= malloc((NumLines
+ 1) * sizeof(PcdList
[0]));
529 for (Index
= 0, TokenIndex
= 0, PcdListLength
= 0, TokenStart
= 0; Index
< FileSize
; Index
++) {
530 if (FileBuffer
[Index
] == ' ') {
533 if (FileBuffer
[Index
] == '|' || FileBuffer
[Index
] == '.' || FileBuffer
[Index
] == '\n' || FileBuffer
[Index
] == '\r') {
534 RecordToken (FileBuffer
, PcdListLength
, TokenIndex
, TokenStart
, Index
);
535 if (FileBuffer
[Index
] == '\n' || FileBuffer
[Index
] == '\r') {
536 if (TokenIndex
!= 0) {
543 TokenStart
= Index
+ 1;
547 if (Index
> TokenStart
) {
548 RecordToken (FileBuffer
, PcdListLength
, TokenIndex
, TokenStart
, Index
);
549 if (TokenIndex
!= 0) {
558 CHAR8
*OutputFileName
564 Write the updated PCD value into the output file name.
568 OutputFileName Point to the output file name.
581 OutputFile
= fopen (OutputFileName
, "wb");
582 if (OutputFile
== NULL
) {
583 fprintf (stderr
, "Error opening file %s\n", OutputFileName
);
587 for (Index
= 0; Index
< PcdListLength
; Index
++) {
590 "%s.%s.%s.%s|%s|%s\n",
591 PcdList
[Index
].SkuName
,
592 PcdList
[Index
].DefaultValueName
,
593 PcdList
[Index
].TokenSpaceGuidName
,
594 PcdList
[Index
].TokenName
,
595 PcdList
[Index
].DataType
,
601 // Done, write output file.
603 if (OutputFile
!= NULL
) {
617 Displays the utility usage syntax to STDOUT
629 fprintf (stdout
, "Usage: -i <input_file> -o <output_file>\n\n");
630 fprintf (stdout
, "optional arguments:\n");
631 fprintf (stdout
, " -h, --help Show this help message and exit\n");
632 fprintf (stdout
, " -i INPUT_FILENAME, --input INPUT_FILENAME\n\
633 PCD Database Input file name\n");
634 fprintf (stdout
, " -o OUTPUT_FILENAME, --output OUTPUT_FILENAME\n\
635 PCD Database Output file name\n");
643 CHAR8
**InputFileName
,
644 CHAR8
**OutputFileName
650 Parse the input parameters to get the input/output file name.
654 argc Number of command line parameters.
655 argv Array of pointers to parameter strings.
656 InputFileName Point to the input file name.
657 OutputFileName Point to the output file name.
665 fprintf (stderr
, "Missing options\n");
670 // Parse command line
675 if ((stricmp (argv
[0], "-h") == 0) || (stricmp (argv
[0], "--help") == 0)) {
681 if ((stricmp (argv
[0], "-i") == 0) || (stricmp (argv
[0], "--input") == 0)) {
682 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
683 fprintf (stderr
, "Invalid option value. Input File name is missing for -i option\n");
686 *InputFileName
= argv
[1];
692 if ((stricmp (argv
[0], "-o") == 0) || (stricmp (argv
[0], "--output") == 0)) {
693 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
694 fprintf (stderr
, "Invalid option value. Output File name is missing for -i option\n");
697 *OutputFileName
= argv
[1];
703 if (argv
[0][0] == '-') {
704 fprintf (stderr
, "Unknown option %s\n", argv
[0]);
712 // Check Input parameters
714 if (*InputFileName
== NULL
) {
715 fprintf (stderr
, "Missing option. Input files is not specified\n");
719 if (*OutputFileName
== NULL
) {
720 fprintf (stderr
, "Missing option. Output file is not specified\n");
734 Main function updates PCD values.
738 argc Number of command line parameters.
739 argv Array of pointers to parameter strings.
745 CHAR8
*InputFileName
;
746 CHAR8
*OutputFileName
;
750 InputFileName
= NULL
;
751 OutputFileName
= NULL
;
754 // Parse the input arguments
756 ParseArguments (argc
, argv
, &InputFileName
, &OutputFileName
);
759 // Open Input file and read file data.
761 ReadInputFile (InputFileName
, &FileBuffer
, &FileSize
);
764 // Read the initial Pcd value
766 ParseFile (FileBuffer
, FileSize
);
769 // Customize PCD values in the PCD Database
774 // Save the updated PCD value
776 WriteOutputFile (OutputFileName
);