123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779 |
- /** @file
- This file contains the PcdValue structure definition.
- Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.<BR>
- SPDX-License-Identifier: BSD-2-Clause-Patent
- **/
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "CommonLib.h"
- #include "PcdValueCommon.h"
- typedef enum {
- PcdDataTypeBoolean,
- PcdDataTypeUint8,
- PcdDataTypeUint16,
- PcdDataTypeUint32,
- PcdDataTypeUint64,
- PcdDataTypePointer
- } PCD_DATA_TYPE;
- typedef struct {
- CHAR8 *SkuName;
- CHAR8 *DefaultValueName;
- CHAR8 *TokenSpaceGuidName;
- CHAR8 *TokenName;
- CHAR8 *DataType;
- CHAR8 *Value;
- PCD_DATA_TYPE PcdDataType;
- } PCD_ENTRY;
- PCD_ENTRY *PcdList;
- UINT32 PcdListLength;
- VOID
- STATIC
- RecordToken (
- UINT8 *FileBuffer,
- UINT32 PcdIndex,
- UINT32 TokenIndex,
- UINT32 TokenStart,
- UINT32 TokenEnd
- )
- /*++
- Routine Description:
- Record new token information
- Arguments:
- FileBuffer File Buffer to be record
- PcdIndex Index of PCD in database
- TokenIndex Index of Token
- TokenStart Start of Token
- TokenEnd End of Token
- Returns:
- None
- --*/
- {
- CHAR8 *Token;
- Token = malloc (TokenEnd - TokenStart + 1);
- if (Token == NULL) {
- return;
- }
- memcpy (Token, &FileBuffer[TokenStart], TokenEnd - TokenStart);
- Token[TokenEnd - TokenStart] = 0;
- switch (TokenIndex) {
- case 0:
- PcdList[PcdIndex].SkuName = Token;
- break;
- case 1:
- PcdList[PcdIndex].DefaultValueName = Token;
- break;
- case 2:
- PcdList[PcdIndex].TokenSpaceGuidName = Token;
- break;
- case 3:
- PcdList[PcdIndex].TokenName = Token;
- break;
- case 4:
- PcdList[PcdIndex].DataType = Token;
- if (strcmp (Token, "BOOLEAN") == 0) {
- PcdList[PcdIndex].PcdDataType = PcdDataTypeBoolean;
- } else if (strcmp (Token, "UINT8") == 0) {
- PcdList[PcdIndex].PcdDataType = PcdDataTypeUint8;
- } else if (strcmp (Token, "UINT16") == 0) {
- PcdList[PcdIndex].PcdDataType = PcdDataTypeUint16;
- } else if (strcmp (Token, "UINT32") == 0) {
- PcdList[PcdIndex].PcdDataType = PcdDataTypeUint32;
- } else if (strcmp (Token, "UINT64") == 0) {
- PcdList[PcdIndex].PcdDataType = PcdDataTypeUint64;
- } else {
- PcdList[PcdIndex].PcdDataType = PcdDataTypePointer;
- }
- break;
- case 5:
- PcdList[PcdIndex].Value = Token;
- break;
- default:
- free (Token);
- break;
- }
- }
- int
- STATIC
- LookupPcdIndex (
- CHAR8 *SkuName OPTIONAL,
- CHAR8 *DefaultValueName OPTIONAL,
- CHAR8 *TokenSpaceGuidName,
- CHAR8 *TokenName
- )
- /*++
- Routine Description:
- Get PCD index in Pcd database
- Arguments:
- SkuName SkuName String
- DefaultValueName DefaultValueName String
- TokenSpaceGuidName TokenSpaceGuidName String
- TokenName TokenName String
- Returns:
- Index of PCD in Pcd database
- --*/
- {
- UINT32 Index;
- if (SkuName == NULL) {
- SkuName = "DEFAULT";
- }
- if (DefaultValueName == NULL) {
- DefaultValueName = "DEFAULT";
- }
- for (Index = 0; Index < PcdListLength; Index++) {
- if (strcmp(PcdList[Index].TokenSpaceGuidName, TokenSpaceGuidName) != 0) {
- continue;
- }
- if (strcmp(PcdList[Index].TokenName, TokenName) != 0) {
- continue;
- }
- if (strcmp(PcdList[Index].SkuName, SkuName) != 0) {
- continue;
- }
- if (strcmp(PcdList[Index].DefaultValueName, DefaultValueName) != 0) {
- continue;
- }
- return Index;
- }
- return -1;
- }
- UINT64
- __PcdGet (
- CHAR8 *SkuName OPTIONAL,
- CHAR8 *DefaultValueName OPTIONAL,
- CHAR8 *TokenSpaceGuidName,
- CHAR8 *TokenName
- )
- /*++
- Routine Description:
- Get PCD value
- Arguments:
- SkuName SkuName String
- DefaultValueName DefaultValueName String
- TokenSpaceGuidName TokenSpaceGuidName String
- TokenName TokenName String
- Returns:
- PCD value
- --*/
- {
- int Index;
- CHAR8 *End;
- Index = LookupPcdIndex (SkuName, DefaultValueName, TokenSpaceGuidName, TokenName);
- if (Index < 0) {
- fprintf (stderr, "PCD %s.%s.%s.%s is not in database\n", SkuName, DefaultValueName, TokenSpaceGuidName, TokenName);
- exit (EXIT_FAILURE);
- }
- switch (PcdList[Index].PcdDataType) {
- case PcdDataTypeBoolean:
- case PcdDataTypeUint8:
- case PcdDataTypeUint16:
- case PcdDataTypeUint32:
- return (UINT64)strtoul(PcdList[Index].Value, &End, 16);
- break;
- case PcdDataTypeUint64:
- return (UINT64)strtoul(PcdList[Index].Value, &End, 16);
- break;
- case PcdDataTypePointer:
- fprintf (stderr, "PCD %s.%s.%s.%s is structure. Use PcdGetPtr()\n", SkuName, DefaultValueName, TokenSpaceGuidName, TokenName);
- exit (EXIT_FAILURE);
- break;
- }
- return 0;
- }
- VOID
- __PcdSet (
- CHAR8 *SkuName OPTIONAL,
- CHAR8 *DefaultValueName OPTIONAL,
- CHAR8 *TokenSpaceGuidName,
- CHAR8 *TokenName,
- UINT64 Value
- )
- /*++
- Routine Description:
- Set PCD value
- Arguments:
- SkuName SkuName String
- DefaultValueName DefaultValueName String
- TokenSpaceGuidName TokenSpaceGuidName String
- TokenName TokenName String
- Value PCD value to be set
- Returns:
- None
- --*/
- {
- int Index;
- Index = LookupPcdIndex (SkuName, DefaultValueName, TokenSpaceGuidName, TokenName);
- if (Index < 0) {
- fprintf (stderr, "PCD %s.%s.%s.%s is not in database\n", SkuName, DefaultValueName, TokenSpaceGuidName, TokenName);
- exit (EXIT_FAILURE);
- }
- free(PcdList[Index].Value);
- PcdList[Index].Value = malloc(20);
- switch (PcdList[Index].PcdDataType) {
- case PcdDataTypeBoolean:
- if (Value == 0) {
- strcpy (PcdList[Index].Value, "0x00");
- } else {
- strcpy (PcdList[Index].Value, "0x01");
- }
- break;
- case PcdDataTypeUint8:
- sprintf(PcdList[Index].Value, "0x%02x", (UINT8)(Value & 0xff));
- break;
- case PcdDataTypeUint16:
- sprintf(PcdList[Index].Value, "0x%04x", (UINT16)(Value & 0xffff));
- break;
- case PcdDataTypeUint32:
- sprintf(PcdList[Index].Value, "0x%08x", (UINT32)(Value & 0xffffffff));
- break;
- case PcdDataTypeUint64:
- sprintf(PcdList[Index].Value, "0x%016llx", (unsigned long long)Value);
- break;
- case PcdDataTypePointer:
- fprintf (stderr, "PCD %s.%s.%s.%s is structure. Use PcdSetPtr()\n", SkuName, DefaultValueName, TokenSpaceGuidName, TokenName);
- exit (EXIT_FAILURE);
- break;
- }
- }
- VOID *
- __PcdGetPtr (
- CHAR8 *SkuName OPTIONAL,
- CHAR8 *DefaultValueName OPTIONAL,
- CHAR8 *TokenSpaceGuidName,
- CHAR8 *TokenName,
- UINT32 *Size
- )
- /*++
- Routine Description:
- Get PCD value buffer
- Arguments:
- SkuName SkuName String
- DefaultValueName DefaultValueName String
- TokenSpaceGuidName TokenSpaceGuidName String
- TokenName TokenName String
- Size Size of PCD value buffer
- Returns:
- PCD value buffer
- --*/
- {
- int Index;
- CHAR8 *Value;
- UINT8 *Buffer;
- CHAR8 *End;
- Index = LookupPcdIndex (SkuName, DefaultValueName, TokenSpaceGuidName, TokenName);
- if (Index < 0) {
- fprintf (stderr, "PCD %s.%s.%s.%s is not in database\n", SkuName, DefaultValueName, TokenSpaceGuidName, TokenName);
- exit (EXIT_FAILURE);
- }
- switch (PcdList[Index].PcdDataType) {
- case PcdDataTypeBoolean:
- case PcdDataTypeUint8:
- case PcdDataTypeUint16:
- case PcdDataTypeUint32:
- case PcdDataTypeUint64:
- fprintf (stderr, "PCD %s.%s.%s.%s is a value. Use PcdGet()\n", SkuName, DefaultValueName, TokenSpaceGuidName, TokenName);
- exit (EXIT_FAILURE);
- break;
- case PcdDataTypePointer:
- Value = &PcdList[Index].Value[1];
- for (*Size = 0, strtoul(Value, &End, 16); Value != End; strtoul(Value, &End, 16), *Size = *Size + 1) {
- Value = End + 1;
- }
- Buffer = malloc(*Size + 1);
- if (Buffer == NULL) {
- *Size = 0;
- return NULL;
- }
- Value = &PcdList[Index].Value[1];
- for (*Size = 0, Buffer[*Size] = (UINT8) strtoul(Value, &End, 16); Value != End; *Size = *Size + 1, Buffer[*Size] = (UINT8) strtoul(Value, &End, 16)) {
- Value = End + 1;
- }
- return Buffer;
- }
- *Size = 0;
- return 0;
- }
- VOID
- __PcdSetPtr (
- CHAR8 *SkuName OPTIONAL,
- CHAR8 *DefaultValueName OPTIONAL,
- CHAR8 *TokenSpaceGuidName,
- CHAR8 *TokenName,
- UINT32 Size,
- UINT8 *Value
- )
- /*++
- Routine Description:
- Set PCD value buffer
- Arguments:
- SkuName SkuName String
- DefaultValueName DefaultValueName String
- TokenSpaceGuidName TokenSpaceGuidName String
- TokenName TokenName String
- Size Size of PCD value
- Value Pointer to the updated PCD value buffer
- Returns:
- None
- --*/
- {
- int Index;
- UINT32 ValueIndex;
- Index = LookupPcdIndex (SkuName, DefaultValueName, TokenSpaceGuidName, TokenName);
- if (Index < 0) {
- fprintf (stderr, "PCD %s.%s.%s.%s is not in database\n", SkuName, DefaultValueName, TokenSpaceGuidName, TokenName);
- exit (EXIT_FAILURE);
- }
- switch (PcdList[Index].PcdDataType) {
- case PcdDataTypeBoolean:
- case PcdDataTypeUint8:
- case PcdDataTypeUint16:
- case PcdDataTypeUint32:
- case PcdDataTypeUint64:
- fprintf (stderr, "PCD %s.%s.%s.%s is a value. Use PcdGet()\n", SkuName, DefaultValueName, TokenSpaceGuidName, TokenName);
- exit (EXIT_FAILURE);
- break;
- case PcdDataTypePointer:
- free(PcdList[Index].Value);
- PcdList[Index].Value = malloc(Size * 5 + 3);
- PcdList[Index].Value[0] = '{';
- for (ValueIndex = 0; ValueIndex < Size; ValueIndex++) {
- sprintf(&PcdList[Index].Value[1 + ValueIndex * 5], "0x%02x,", Value[ValueIndex]);
- }
- PcdList[Index].Value[1 + Size * 5 - 1] = '}';
- PcdList[Index].Value[1 + Size * 5 ] = 0;
- break;
- }
- }
- VOID
- STATIC
- ReadInputFile (
- CHAR8 *InputFileName,
- UINT8 **FileBuffer,
- UINT32 *FileSize
- )
- /*++
- Routine Description:
- Read the file buffer from the input file.
- Arguments:
- InputFileName Point to the input file name.
- FileBuffer Point to the input file buffer.
- FileSize Size of the file buffer.
- Returns:
- None
- --*/
- {
- FILE *InputFile;
- UINT32 BytesRead;
- //
- // Open Input file and read file data.
- //
- InputFile = fopen (InputFileName, "rb");
- if (InputFile == NULL) {
- fprintf (stderr, "Error opening file %s\n", InputFileName);
- exit (EXIT_FAILURE);
- }
- //
- // Go to the end so that we can determine the file size
- //
- if (fseek (InputFile, 0, SEEK_END)) {
- fprintf (stderr, "Error reading input file %s\n", InputFileName);
- fclose (InputFile);
- exit (EXIT_FAILURE);
- }
- //
- // Get the file size
- //
- *FileSize = ftell (InputFile);
- if (*FileSize == -1) {
- fprintf (stderr, "Error parsing the input file %s\n", InputFileName);
- fclose (InputFile);
- exit (EXIT_FAILURE);
- }
- //
- // Allocate a buffer
- //
- *FileBuffer = malloc (*FileSize);
- if (*FileBuffer == NULL) {
- fprintf (stderr, "Can not allocate buffer for input input file %s\n", InputFileName);
- fclose (InputFile);
- exit (EXIT_FAILURE);
- }
- //
- // Reset to the beginning of the file
- //
- if (fseek (InputFile, 0, SEEK_SET)) {
- fprintf (stderr, "Error reading the input file %s\n", InputFileName);
- fclose (InputFile);
- free (*FileBuffer);
- exit (EXIT_FAILURE);
- }
- //
- // Read all of the file contents.
- //
- BytesRead = fread (*FileBuffer, sizeof (UINT8), *FileSize, InputFile);
- if (BytesRead != *FileSize * sizeof (UINT8)) {
- fprintf (stderr, "Error reading the input file %s\n", InputFileName);
- fclose (InputFile);
- free (*FileBuffer);
- exit (EXIT_FAILURE);
- }
- //
- // Close the file
- //
- fclose (InputFile);
- }
- VOID
- STATIC
- ParseFile (
- UINT8 *FileBuffer,
- UINT32 FileSize
- )
- /*++
- Routine Description:
- Read the initial PCD value from the input file buffer.
- Arguments:
- FileBuffer Point to the input file buffer.
- FileSize Size of the file buffer.
- Returns:
- None
- --*/
- {
- UINT32 Index;
- UINT32 NumLines;
- UINT32 TokenIndex;
- UINT32 TokenStart;
- for (Index = 0, NumLines = 0; Index < FileSize; Index++) {
- if (FileBuffer[Index] == '\n') {
- NumLines++;
- }
- }
- PcdList = malloc((NumLines + 1) * sizeof(PcdList[0]));
- for (Index = 0, TokenIndex = 0, PcdListLength = 0, TokenStart = 0; Index < FileSize; Index++) {
- if (FileBuffer[Index] == ' ') {
- continue;
- }
- if (FileBuffer[Index] == '|' || FileBuffer[Index] == '.' || FileBuffer[Index] == '\n' || FileBuffer[Index] == '\r') {
- RecordToken (FileBuffer, PcdListLength, TokenIndex, TokenStart, Index);
- if (FileBuffer[Index] == '\n' || FileBuffer[Index] == '\r') {
- if (TokenIndex != 0) {
- PcdListLength++;
- TokenIndex = 0;
- }
- } else {
- TokenIndex++;
- }
- TokenStart = Index + 1;
- continue;
- }
- }
- if (Index > TokenStart) {
- RecordToken (FileBuffer, PcdListLength, TokenIndex, TokenStart, Index);
- if (TokenIndex != 0) {
- PcdListLength++;
- }
- }
- }
- VOID
- STATIC
- WriteOutputFile (
- CHAR8 *OutputFileName
- )
- /*++
- Routine Description:
- Write the updated PCD value into the output file name.
- Arguments:
- OutputFileName Point to the output file name.
- Returns:
- None
- --*/
- {
- FILE *OutputFile;
- UINT32 Index;
- //
- // Open output file
- //
- OutputFile = fopen (OutputFileName, "wb");
- if (OutputFile == NULL) {
- fprintf (stderr, "Error opening file %s\n", OutputFileName);
- exit (EXIT_FAILURE);
- }
- for (Index = 0; Index < PcdListLength; Index++) {
- fprintf (
- OutputFile,
- "%s.%s.%s.%s|%s|%s\n",
- PcdList[Index].SkuName,
- PcdList[Index].DefaultValueName,
- PcdList[Index].TokenSpaceGuidName,
- PcdList[Index].TokenName,
- PcdList[Index].DataType,
- PcdList[Index].Value
- );
- }
- //
- // Done, write output file.
- //
- if (OutputFile != NULL) {
- fclose (OutputFile);
- }
- }
- VOID
- STATIC
- Usage (
- VOID
- )
- /*++
- Routine Description:
- Displays the utility usage syntax to STDOUT
- Arguments:
- None
- Returns:
- None
- --*/
- {
- fprintf (stdout, "Usage: -i <input_file> -o <output_file>\n\n");
- fprintf (stdout, "optional arguments:\n");
- fprintf (stdout, " -h, --help Show this help message and exit\n");
- fprintf (stdout, " -i INPUT_FILENAME, --input INPUT_FILENAME\n\
- PCD Database Input file name\n");
- fprintf (stdout, " -o OUTPUT_FILENAME, --output OUTPUT_FILENAME\n\
- PCD Database Output file name\n");
- }
- VOID
- STATIC
- ParseArguments (
- int argc,
- char *argv[],
- CHAR8 **InputFileName,
- CHAR8 **OutputFileName
- )
- /*++
- Routine Description:
- Parse the input parameters to get the input/output file name.
- Arguments:
- argc Number of command line parameters.
- argv Array of pointers to parameter strings.
- InputFileName Point to the input file name.
- OutputFileName Point to the output file name.
- Returns:
- None
- --*/
- {
- if (argc == 1) {
- fprintf (stderr, "Missing options\n");
- exit (EXIT_FAILURE);
- }
- //
- // Parse command line
- //
- argc--;
- argv++;
- if ((stricmp (argv[0], "-h") == 0) || (stricmp (argv[0], "--help") == 0)) {
- Usage ();
- exit (EXIT_SUCCESS);
- }
- while (argc > 0) {
- if ((stricmp (argv[0], "-i") == 0) || (stricmp (argv[0], "--input") == 0)) {
- if (argv[1] == NULL || argv[1][0] == '-') {
- fprintf (stderr, "Invalid option value. Input File name is missing for -i option\n");
- exit (EXIT_FAILURE);
- }
- *InputFileName = argv[1];
- argc -= 2;
- argv += 2;
- continue;
- }
- if ((stricmp (argv[0], "-o") == 0) || (stricmp (argv[0], "--output") == 0)) {
- if (argv[1] == NULL || argv[1][0] == '-') {
- fprintf (stderr, "Invalid option value. Output File name is missing for -i option\n");
- exit (EXIT_FAILURE);
- }
- *OutputFileName = argv[1];
- argc -= 2;
- argv += 2;
- continue;
- }
- if (argv[0][0] == '-') {
- fprintf (stderr, "Unknown option %s\n", argv[0]);
- exit (EXIT_FAILURE);
- }
- argc --;
- argv ++;
- }
- //
- // Check Input parameters
- //
- if (*InputFileName == NULL) {
- fprintf (stderr, "Missing option. Input files is not specified\n");
- exit (EXIT_FAILURE);
- }
- if (*OutputFileName == NULL) {
- fprintf (stderr, "Missing option. Output file is not specified\n");
- exit (EXIT_FAILURE);
- }
- }
- int
- PcdValueMain (
- int argc,
- char *argv[]
- )
- /*++
- Routine Description:
- Main function updates PCD values.
- Arguments:
- argc Number of command line parameters.
- argv Array of pointers to parameter strings.
- Returns:
- EXIT_SUCCESS
- --*/
- {
- CHAR8 *InputFileName;
- CHAR8 *OutputFileName;
- UINT8 *FileBuffer;
- UINT32 FileSize;
- InputFileName = NULL;
- OutputFileName = NULL;
- //
- // Parse the input arguments
- //
- ParseArguments (argc, argv, &InputFileName, &OutputFileName);
- //
- // Open Input file and read file data.
- //
- ReadInputFile (InputFileName, &FileBuffer, &FileSize);
- //
- // Read the initial Pcd value
- //
- ParseFile (FileBuffer, FileSize);
- //
- // Customize PCD values in the PCD Database
- //
- PcdEntryPoint ();
- //
- // Save the updated PCD value
- //
- WriteOutputFile (OutputFileName);
- exit (EXIT_SUCCESS);
- }
|