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 for (*Size
= 0, Byte
= (UINT8
) strtoul(Value
, &End
, 16); Value
!= End
; Byte
= (UINT8
) strtoul(Value
, &End
, 16), *Size
= *Size
+ 1) {
336 Buffer
= malloc(*Size
+ 1);
337 if (Buffer
== NULL
) {
341 Value
= &PcdList
[Index
].Value
[1];
342 for (*Size
= 0, Buffer
[*Size
] = (UINT8
) strtoul(Value
, &End
, 16); Value
!= End
; *Size
= *Size
+ 1, Buffer
[*Size
] = (UINT8
) strtoul(Value
, &End
, 16)) {
353 CHAR8
*SkuName OPTIONAL
,
354 CHAR8
*DefaultValueName OPTIONAL
,
355 CHAR8
*TokenSpaceGuidName
,
368 SkuName SkuName String
369 DefaultValueName DefaultValueName String
370 TokenSpaceGuidName TokenSpaceGuidName String
371 TokenName TokenName String
372 Size Size of PCD value
373 Value Pointer to the updated PCD value buffer
383 Index
= LookupPcdIndex (SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
385 fprintf (stderr
, "PCD %s.%s.%s.%s is not in database\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
388 switch (PcdList
[Index
].PcdDataType
) {
389 case PcdDataTypeBoolean
:
390 case PcdDataTypeUint8
:
391 case PcdDataTypeUint16
:
392 case PcdDataTypeUint32
:
393 case PcdDataTypeUint64
:
394 fprintf (stderr
, "PCD %s.%s.%s.%s is a value. Use PcdGet()\n", SkuName
, DefaultValueName
, TokenSpaceGuidName
, TokenName
);
397 case PcdDataTypePointer
:
398 free(PcdList
[Index
].Value
);
399 PcdList
[Index
].Value
= malloc(Size
* 5 + 3);
400 PcdList
[Index
].Value
[0] = '{';
401 for (ValueIndex
= 0; ValueIndex
< Size
; ValueIndex
++) {
402 sprintf(&PcdList
[Index
].Value
[1 + ValueIndex
* 5], "0x%02x,", Value
[ValueIndex
]);
404 PcdList
[Index
].Value
[1 + Size
* 5 - 1] = '}';
405 PcdList
[Index
].Value
[1 + Size
* 5 ] = 0;
413 CHAR8
*InputFileName
,
421 Read the file buffer from the input file.
425 InputFileName Point to the input file name.
426 FileBuffer Point to the input file buffer.
427 FileSize Size of the file buffer.
438 // Open Input file and read file data.
440 InputFile
= fopen (InputFileName
, "rb");
441 if (InputFile
== NULL
) {
442 fprintf (stderr
, "Error opening file %s\n", InputFileName
);
447 // Go to the end so that we can determine the file size
449 if (fseek (InputFile
, 0, SEEK_END
)) {
450 fprintf (stderr
, "Error reading input file %s\n", InputFileName
);
458 *FileSize
= ftell (InputFile
);
459 if (*FileSize
== -1) {
460 fprintf (stderr
, "Error parsing the input file %s\n", InputFileName
);
468 *FileBuffer
= malloc (*FileSize
);
469 if (*FileBuffer
== NULL
) {
470 fprintf (stderr
, "Can not allocate buffer for input input file %s\n", InputFileName
);
476 // Reset to the beginning of the file
478 if (fseek (InputFile
, 0, SEEK_SET
)) {
479 fprintf (stderr
, "Error reading the input file %s\n", InputFileName
);
486 // Read all of the file contents.
488 BytesRead
= fread (*FileBuffer
, sizeof (UINT8
), *FileSize
, InputFile
);
489 if (BytesRead
!= *FileSize
* sizeof (UINT8
)) {
490 fprintf (stderr
, "Error reading the input file %s\n", InputFileName
);
512 Read the initial PCD value from the input file buffer.
516 FileBuffer Point to the input file buffer.
517 FileSize Size of the file buffer.
529 for (Index
= 0, NumLines
= 0; Index
< FileSize
; Index
++) {
530 if (FileBuffer
[Index
] == '\n') {
534 PcdList
= malloc((NumLines
+ 1) * sizeof(PcdList
[0]));
536 for (Index
= 0, TokenIndex
= 0, PcdListLength
= 0, TokenStart
= 0; Index
< FileSize
; Index
++) {
537 if (FileBuffer
[Index
] == ' ') {
540 if (FileBuffer
[Index
] == '|' || FileBuffer
[Index
] == '.' || FileBuffer
[Index
] == '\n' || FileBuffer
[Index
] == '\r') {
541 RecordToken (FileBuffer
, PcdListLength
, TokenIndex
, TokenStart
, Index
);
542 if (FileBuffer
[Index
] == '\n' || FileBuffer
[Index
] == '\r') {
543 if (TokenIndex
!= 0) {
550 TokenStart
= Index
+ 1;
554 if (Index
> TokenStart
) {
555 RecordToken (FileBuffer
, PcdListLength
, TokenIndex
, TokenStart
, Index
);
556 if (TokenIndex
!= 0) {
565 CHAR8
*OutputFileName
571 Write the updated PCD value into the output file name.
575 OutputFileName Point to the output file name.
588 OutputFile
= fopen (OutputFileName
, "wb");
589 if (OutputFile
== NULL
) {
590 fprintf (stderr
, "Error opening file %s\n", OutputFileName
);
594 for (Index
= 0; Index
< PcdListLength
; Index
++) {
597 "%s.%s.%s.%s|%s|%s\n",
598 PcdList
[Index
].SkuName
,
599 PcdList
[Index
].DefaultValueName
,
600 PcdList
[Index
].TokenSpaceGuidName
,
601 PcdList
[Index
].TokenName
,
602 PcdList
[Index
].DataType
,
608 // Done, write output file.
610 if (OutputFile
!= NULL
) {
624 Displays the utility usage syntax to STDOUT
636 fprintf (stdout
, "Usage: -i <input_file> -o <output_file>\n\n");
637 fprintf (stdout
, "optional arguments:\n");
638 fprintf (stdout
, " -h, --help Show this help message and exit\n");
639 fprintf (stdout
, " -i INPUT_FILENAME, --input INPUT_FILENAME\n\
640 PCD Database Input file name\n");
641 fprintf (stdout
, " -o OUTPUT_FILENAME, --output OUTPUT_FILENAME\n\
642 PCD Database Output file name\n");
650 CHAR8
**InputFileName
,
651 CHAR8
**OutputFileName
657 Parse the input parameters to get the input/output file name.
661 argc Number of command line parameters.
662 argv Array of pointers to parameter strings.
663 InputFileName Point to the input file name.
664 OutputFileName Point to the output file name.
672 fprintf (stderr
, "Missing options\n");
677 // Parse command line
682 if ((stricmp (argv
[0], "-h") == 0) || (stricmp (argv
[0], "--help") == 0)) {
688 if ((stricmp (argv
[0], "-i") == 0) || (stricmp (argv
[0], "--input") == 0)) {
689 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
690 fprintf (stderr
, "Invalid option value. Input File name is missing for -i option\n");
693 *InputFileName
= argv
[1];
699 if ((stricmp (argv
[0], "-o") == 0) || (stricmp (argv
[0], "--output") == 0)) {
700 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
701 fprintf (stderr
, "Invalid option value. Output File name is missing for -i option\n");
704 *OutputFileName
= argv
[1];
710 if (argv
[0][0] == '-') {
711 fprintf (stderr
, "Unknown option %s\n", argv
[0]);
719 // Check Input paramters
721 if (*InputFileName
== NULL
) {
722 fprintf (stderr
, "Missing option. Input files is not specified\n");
726 if (*OutputFileName
== NULL
) {
727 fprintf (stderr
, "Missing option. Output file is not specified\n");
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 InputFileName
= NULL
;
758 OutputFileName
= NULL
;
761 // Parse the input arguments
763 ParseArguments (argc
, argv
, &InputFileName
, &OutputFileName
);
766 // Open Input file and read file data.
768 ReadInputFile (InputFileName
, &FileBuffer
, &FileSize
);
771 // Read the initial Pcd value
773 ParseFile (FileBuffer
, FileSize
);
776 // Customize PCD values in the PCD Database
781 // Save the updated PCD value
783 WriteOutputFile (OutputFileName
);