## @file
# Patch value into the binary file.
#
# Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution. The full text of the license may be found at
#
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#
##
# Import Modules
#
import os
import sys
import re
from Common.BuildToolError import *
import array
# Version and Copyright
## PatchBinaryFile method
#
# This method mainly patches the data into binary file.
#
# @param FileName File path of the binary file
# @param ValueOffset Offset value
# @param TypeName DataType Name
# @param Value Value String
# @param MaxSize MaxSize value
#
# @retval 0 File is updated successfully.
# @retval not 0 File is updated failed.
#
#
# Length of Binary File
#
#
# Unify string to upper string
#
#
# Get PCD value data length
#
ValueLength = 0
if TypeName == 'BOOLEAN':
ValueLength = 1
elif TypeName == 'UINT8':
ValueLength = 1
elif TypeName == 'UINT16':
ValueLength = 2
elif TypeName == 'UINT32':
ValueLength = 4
elif TypeName == 'UINT64':
ValueLength = 8
elif TypeName == 'VOID*':
if MaxSize == 0:
return OPTION_MISSING, "PcdMaxSize is not specified for VOID* type PCD."
else:
#
# Check PcdValue is in the input binary file.
#
return PARAMETER_INVALID, "PcdOffset + PcdMaxSize(DataType) is larger than the input file size."
#
# Read binary file into array
#
#
# Clear the data in file
#
#
# Patch value into offset
#
ValueNumber = 0
if TypeName == 'BOOLEAN':
#
# Get PCD value for BOOLEAN data type
#
try:
if ValueString == 'TRUE':
ValueNumber = 1
elif ValueString == 'FALSE':
ValueNumber = 0
else:
if ValueNumber != 0:
ValueNumber = 1
except:
#
# Set PCD value into binary data
#
#
# Get PCD value for UINT* data type
#
try:
else:
except:
#
# Set PCD value into binary data
#
elif TypeName == 'VOID*':
#
# Patch Unicode String
#
Index = 0
#
# Reserve zero as unicode tail
#
break
#
# Set string value one by one
#
#
# Patch {0x1, 0x2, ...} byte by byte
#
Index = 0
try:
for ByteString in ValueList:
else:
if Index >= ValueLength:
break
except:
else:
#
# Patch ascii string
#
Index = 0
for ByteString in ValueString:
#
# Reserve zero as string tail
#
break
#
# Set string value one by one
#
#
# Update new data into input file.
#
if ByteList != OrigByteList:
## Parse command line options
#
# Using standard Python module optparse to parse command line option of this tool.
#
# @retval Options A optparse.Values object containing the parsed options
# @retval InputFile Path of file to be trimmed
#
def Options():
OptionList = [
help="Start offset to the image is used to store PCD value."),
help="PCD value will be updated into the image."),
help="The name of PCD data type may be one of VOID*,BOOLEAN, UINT8, UINT16, UINT32, UINT64."),
help="Max size of data buffer is taken by PCD value.It must be set when PCD type is VOID*."),
help="Run verbosely"),
help="Run with debug information"),
help="Run quietly"),
]
# use clearer usage to override default usage message
UsageString = "%prog -f Offset -u Value -t Type [-s MaxSize] <input_file>"
Parser = OptionParser(description=__copyright__, version=__version__, option_list=OptionList, usage=UsageString)
# error check
## Entrance method
#
# This method mainly dispatch specific methods per the command line options.
# If no error found, return zero value so the caller of this tool can know
# if it's executed successfully or not.
#
# @retval 0 Tool was successful
# @retval 1 Tool failed
#
def Main():
try:
#
# Check input parameter
#
else:
return 1
if CommandOptions.PcdOffset == None or CommandOptions.PcdValue == None or CommandOptions.PcdTypeName == None:
EdkLogger.error("PatchPcdValue", OPTION_MISSING, ExtraData="PcdOffset or PcdValue of PcdTypeName is not specified.")
return 1
if CommandOptions.PcdTypeName.upper() not in ["BOOLEAN", "UINT8", "UINT16", "UINT32", "UINT64", "VOID*"]:
EdkLogger.error("PatchPcdValue", PARAMETER_INVALID, ExtraData="PCD type %s is not valid." %(CommandOptions.PcdTypeName))
return 1
EdkLogger.error("PatchPcdValue", OPTION_MISSING, ExtraData="PcdMaxSize is not specified for VOID* type PCD.")
return 1
#
# Patch value into binary image.
#
ReturnValue, ErrorInfo = PatchBinaryFile (InputFile, CommandOptions.PcdOffset, CommandOptions.PcdTypeName, CommandOptions.PcdValue, CommandOptions.PcdMaxSize)
if ReturnValue != 0:
return 1
return 0
except:
return 1
if __name__ == '__main__':
r = Main()