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
316 Index
= LookupPcdIndex (SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
318 fprintf (stderr
, "PCD %s.%s.%s.%s is not in database\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
321 switch (PcdList
[Index
].PcdDataType
) {
322 case PcdDataTypeBoolean
:
323 case PcdDataTypeUint8
:
324 case PcdDataTypeUint16
:
325 case PcdDataTypeUint32
:
326 case PcdDataTypeUint64
:
327 fprintf (stderr
, "PCD %s.%s.%s.%s is a value. Use PcdGet()\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
330 case PcdDataTypePointer
:
331 Value
= &PcdList
[Index
].Value
[1];
332 for (*Size
= 0, strtoul(Value
, &End
, 16); Value
!= End
; strtoul(Value
, &End
, 16), *Size
= *Size
+ 1) {
335 Buffer
= malloc(*Size
+ 1);
336 if (Buffer
== NULL
) {
340 Value
= &PcdList
[Index
].Value
[1];
341 for (*Size
= 0, Buffer
[*Size
] = (UINT8
) strtoul(Value
, &End
, 16); Value
!= End
; *Size
= *Size
+ 1, Buffer
[*Size
] = (UINT8
) strtoul(Value
, &End
, 16)) {
352 CHAR8
*SkuName OPTIONAL
,
353 CHAR8
*DefaultValueName OPTIONAL
,
354 CHAR8
*TokenSpaceGuidName
,
367 SkuName SkuName String
368 DefaultValueName DefaultValueName String
369 TokenSpaceGuidName TokenSpaceGuidName String
370 TokenName TokenName String
371 Size Size of PCD value
372 Value Pointer to the updated PCD value buffer
382 Index
= LookupPcdIndex (SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
384 fprintf (stderr
, "PCD %s.%s.%s.%s is not in database\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
387 switch (PcdList
[Index
].PcdDataType
) {
388 case PcdDataTypeBoolean
:
389 case PcdDataTypeUint8
:
390 case PcdDataTypeUint16
:
391 case PcdDataTypeUint32
:
392 case PcdDataTypeUint64
:
393 fprintf (stderr
, "PCD %s.%s.%s.%s is a value. Use PcdGet()\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
396 case PcdDataTypePointer
:
397 free(PcdList
[Index
].Value
);
398 PcdList
[Index
].Value
= malloc(Size
* 5 + 3);
399 PcdList
[Index
].Value
[0] = '{';
400 for (ValueIndex
= 0; ValueIndex
< Size
; ValueIndex
++) {
401 sprintf(&PcdList
[Index
].Value
[1 + ValueIndex
* 5], "0x%02x,", Value
[ValueIndex
]);
403 PcdList
[Index
].Value
[1 + Size
* 5 - 1] = '}';
404 PcdList
[Index
].Value
[1 + Size
* 5 ] = 0;
412 CHAR8
*InputFileName
,
420 Read the file buffer from the input file.
424 InputFileName Point to the input file name.
425 FileBuffer Point to the input file buffer.
426 FileSize Size of the file buffer.
437 // Open Input file and read file data.
439 InputFile
= fopen (InputFileName
, "rb");
440 if (InputFile
== NULL
) {
441 fprintf (stderr
, "Error opening file %s\n", InputFileName
);
446 // Go to the end so that we can determine the file size
448 if (fseek (InputFile
, 0, SEEK_END
)) {
449 fprintf (stderr
, "Error reading input file %s\n", InputFileName
);
457 *FileSize
= ftell (InputFile
);
458 if (*FileSize
== -1) {
459 fprintf (stderr
, "Error parsing the input file %s\n", InputFileName
);
467 *FileBuffer
= malloc (*FileSize
);
468 if (*FileBuffer
== NULL
) {
469 fprintf (stderr
, "Can not allocate buffer for input input file %s\n", InputFileName
);
475 // Reset to the beginning of the file
477 if (fseek (InputFile
, 0, SEEK_SET
)) {
478 fprintf (stderr
, "Error reading the input file %s\n", InputFileName
);
485 // Read all of the file contents.
487 BytesRead
= fread (*FileBuffer
, sizeof (UINT8
), *FileSize
, InputFile
);
488 if (BytesRead
!= *FileSize
* sizeof (UINT8
)) {
489 fprintf (stderr
, "Error reading the input file %s\n", InputFileName
);
511 Read the initial PCD value from the input file buffer.
515 FileBuffer Point to the input file buffer.
516 FileSize Size of the file buffer.
528 for (Index
= 0, NumLines
= 0; Index
< FileSize
; Index
++) {
529 if (FileBuffer
[Index
] == '\n') {
533 PcdList
= malloc((NumLines
+ 1) * sizeof(PcdList
[0]));
535 for (Index
= 0, TokenIndex
= 0, PcdListLength
= 0, TokenStart
= 0; Index
< FileSize
; Index
++) {
536 if (FileBuffer
[Index
] == ' ') {
539 if (FileBuffer
[Index
] == '|' || FileBuffer
[Index
] == '.' || FileBuffer
[Index
] == '\n' || FileBuffer
[Index
] == '\r') {
540 RecordToken (FileBuffer
, PcdListLength
, TokenIndex
, TokenStart
, Index
);
541 if (FileBuffer
[Index
] == '\n' || FileBuffer
[Index
] == '\r') {
542 if (TokenIndex
!= 0) {
549 TokenStart
= Index
+ 1;
553 if (Index
> TokenStart
) {
554 RecordToken (FileBuffer
, PcdListLength
, TokenIndex
, TokenStart
, Index
);
555 if (TokenIndex
!= 0) {
564 CHAR8
*OutputFileName
570 Write the updated PCD value into the output file name.
574 OutputFileName Point to the output file name.
587 OutputFile
= fopen (OutputFileName
, "wb");
588 if (OutputFile
== NULL
) {
589 fprintf (stderr
, "Error opening file %s\n", OutputFileName
);
593 for (Index
= 0; Index
< PcdListLength
; Index
++) {
596 "%s.%s.%s.%s|%s|%s\n",
597 PcdList
[Index
].SkuName
,
598 PcdList
[Index
].DefaultValueName
,
599 PcdList
[Index
].TokenSpaceGuidName
,
600 PcdList
[Index
].TokenName
,
601 PcdList
[Index
].DataType
,
607 // Done, write output file.
609 if (OutputFile
!= NULL
) {
623 Displays the utility usage syntax to STDOUT
635 fprintf (stdout
, "Usage: -i <input_file> -o <output_file>\n\n");
636 fprintf (stdout
, "optional arguments:\n");
637 fprintf (stdout
, " -h, --help Show this help message and exit\n");
638 fprintf (stdout
, " -i INPUT_FILENAME, --input INPUT_FILENAME\n\
639 PCD Database Input file name\n");
640 fprintf (stdout
, " -o OUTPUT_FILENAME, --output OUTPUT_FILENAME\n\
641 PCD Database Output file name\n");
649 CHAR8
**InputFileName
,
650 CHAR8
**OutputFileName
656 Parse the input parameters to get the input/output file name.
660 argc Number of command line parameters.
661 argv Array of pointers to parameter strings.
662 InputFileName Point to the input file name.
663 OutputFileName Point to the output file name.
671 fprintf (stderr
, "Missing options\n");
676 // Parse command line
681 if ((stricmp (argv
[0], "-h") == 0) || (stricmp (argv
[0], "--help") == 0)) {
687 if ((stricmp (argv
[0], "-i") == 0) || (stricmp (argv
[0], "--input") == 0)) {
688 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
689 fprintf (stderr
, "Invalid option value. Input File name is missing for -i option\n");
692 *InputFileName
= argv
[1];
698 if ((stricmp (argv
[0], "-o") == 0) || (stricmp (argv
[0], "--output") == 0)) {
699 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
700 fprintf (stderr
, "Invalid option value. Output File name is missing for -i option\n");
703 *OutputFileName
= argv
[1];
709 if (argv
[0][0] == '-') {
710 fprintf (stderr
, "Unknown option %s\n", argv
[0]);
718 // Check Input paramters
720 if (*InputFileName
== NULL
) {
721 fprintf (stderr
, "Missing option. Input files is not specified\n");
725 if (*OutputFileName
== NULL
) {
726 fprintf (stderr
, "Missing option. Output file is not specified\n");
740 Main function updates PCD values.
744 argc Number of command line parameters.
745 argv Array of pointers to parameter strings.
751 CHAR8
*InputFileName
;
752 CHAR8
*OutputFileName
;
756 InputFileName
= NULL
;
757 OutputFileName
= NULL
;
760 // Parse the input arguments
762 ParseArguments (argc
, argv
, &InputFileName
, &OutputFileName
);
765 // Open Input file and read file data.
767 ReadInputFile (InputFileName
, &FileBuffer
, &FileSize
);
770 // Read the initial Pcd value
772 ParseFile (FileBuffer
, FileSize
);
775 // Customize PCD values in the PCD Database
780 // Save the updated PCD value
782 WriteOutputFile (OutputFileName
);