keenanlaws / ford_vbf_to_hex Goto Github PK
View Code? Open in Web Editor NEWPython script that converts Ford .VBF flash file to .hex file.
Python script that converts Ford .VBF flash file to .hex file.
It looks like that this script doesn't work as it should in Python 3.9.xx
I'm C/C++ engineer, not familiar in Python, put I'd propose such modifications to this script. The main modifications was to add or value == b'{':
and or value == b'}':
at lines looking for header. Also I changed flags for opening output HEX file to open it as text, not binary. Without that conversion were failing with a stacktrace out_file.write("\n") TypeError: a bytes-like object is required, not 'str'
Full source code below
import os
import struct
import ctypes
import argparse
def convert_vbf2hex(input_vbf_file, output_hex_file):
# does the file exists
if os.path.exists(input_vbf_file) == False:
print(input_vbf_file + " does no exist");
return -1;
else:
print("Input VBF file: " + input_vbf_file);
# check file size
in_file_size = os.path.getsize(input_vbf_file)
if in_file_size == 0:
print("Input file is empty");
return -2;
else:
print("Input file is " + str(in_file_size) + " bytes long");
# open input file
in_file = open(input_vbf_file, 'rb')
# find last } in the script file
count_open = count_close = 0
found_last_one = False
while in_file_size:
value = in_file.read(1)
#print (value)
if value == '{' or value == b'{':
count_open += 1
elif value == '}' or value == b'}':
count_close += 1
if count_open > 0 and count_open == count_close:
found_last_one = True
break
in_file_size -= 1
#
if found_last_one == False:
in_file.close()
return 0
# base
base = struct.unpack(">I", in_file.read(4))[0]
print("VBF base address = 0x%X" % base);
# binary blob size
size = struct.unpack(">I", in_file.read(4))[0]
print("VBF data size = 0x%X" % size);
# create output file
out_file = open(output_hex_file, 'w')
out_file.write("\n")
out_file.write("VBF source file = %s\n" % input_vbf_file)
out_file.write("VBF base address = 0x%X\n" % base)
out_file.write("VBF data size = 0x%X\n" % size)
out_file.write("\n")
ul_byte = 0
block_address = base
ul_record_checksum_total = 0
us_section_counter = 0
us_start_string = True
line = ""
block_open = False
finished_line = False
hex_chars = "0123456789ABCDEF"
file_items = []
while size:
ul_hex_address = ul_byte + block_address
# read one unsigned byte
value = struct.unpack("B", in_file.read(1))[0]
# section
if (ul_byte & 0xFFFF) == 0:
block_open = True
line = ":"
line += "02000004"
line += "%04X" % (ul_hex_address >> 16)
ul_record_checksum = (((ul_hex_address >> 24) & 0xFF) + ((ul_hex_address >> 16) & 0xFF) + 6)
ul_extrecord_checksum = - ( ((ul_hex_address >> 24) & 0xFF) + ((ul_hex_address >> 16) & 0xFF) + 6)
#print("%08X %08X + %08X + 6 = %08X" % (ul_hex_address, hibyte(ul_hex_address), byte2(ul_hex_address), ctypes.c_uint8(ul_extrecord_checksum).value) )
line += "%02X" % ctypes.c_uint8(ul_extrecord_checksum).value
line += "\n"
us_section_counter = ul_hex_address & 0xFFFF
file_items.append(line)
line = ""
# new line prefix
if us_start_string is True:
block_open = True
line = ":"
line += hex_chars[2]
line += hex_chars[0]
line += hex_chars[us_section_counter >> 12]
line += hex_chars[(us_section_counter >> 8) & 0xF]
line += hex_chars[(us_section_counter >> 4) & 0xF]
line += hex_chars[us_section_counter & 0xF]
line += hex_chars[0]
line += hex_chars[0]
us_start_string = False
file_items.append(line)
line = ""
# char from the blob
line += hex_chars[(value >> 4) & 0x0F] + hex_chars[value & 0x0F]
# update checksum with the current byte
ul_record_checksum_total += value
finished_line = False
# line is finished, attach checksum
if ((ul_byte & 0x1F) == 31):
finished_line = True
block_open = False
ul_record_checksum_total += 32
ul_record_checksum_total += (us_section_counter & 0xFF)
ul_record_checksum_total += (us_section_counter >> 8) & 0xFF
ul_record_checksum_2 = - ul_record_checksum_total
line += hex_chars[(ul_record_checksum_2 >> 4) & 0xF] + hex_chars[ul_record_checksum_2 & 0xF]
line += "\n"
file_items.append(line)
ul_record_checksum_total = 0
us_start_string = True
us_section_counter = (us_section_counter + 32) & 0xFFFF
# update counters
size -= 1
ul_byte += 1
#print(block_open)
#print(finished_line)
if block_open is True and finished_line is False:
file_items.pop()
file_items.append(":00000001FF\n")
for item in file_items:
out_file.write(item)
out_file.close()
print("Output file " + output_hex_file + " was created");
ret = 1
return ret
#
# dd big_endian_base
# dd big_endian_size
# db data
# dw big_endian_crc
#
# Example below
# result = convert_vbf2hex("E:\\dev\\work\\ford\\ford\\HK62-12K532-PMD.vbf", "E:\\dev\\work\\ford\\ford\\HK62-12K532-PMD.vbf.hex")
# print result
# result = convert_vbf2hex("Input File", "Output file")
# print result
def main():
parser = argparse.ArgumentParser(description='Convert VBF to Intel HEX')
parser.add_argument('-i', help='Input VBF file', required=True)
parser.add_argument('-o', help='Output HEX file', required=False)
args = parser.parse_args()
if args.o is not None:
convert_vbf2hex(args.i, args.o)
else:
convert_vbf2hex(args.i, (args.i + ".hex"))
if __name__ == '__main__':
main()
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.