#!/usr/bin/python
import sys
mtdfilename = sys.argv[1]
device_code = int(sys.argv[2][2:],16)
device_code_lower = (device_code & 0xFF)
device_code_upper = (device_code >> 8)
# Open file for writing
mtdfile = open(mtdfilename, "r+b")
try:
# Write the language hex value 0x01 at position 0x10
mtdfile.seek(0x10,0)
byte_to_be_written=[0x01]
bytearrayObj=bytearray(byte_to_be_written)
mtdfile.write(bytearrayObj)
# Write the device code at 0x64 and 0x65
mtdfile.seek(0x64,0)
byte_to_be_written=[device_code_upper]
bytearrayObj=bytearray(byte_to_be_written)
mtdfile.write(bytearrayObj)
byte_to_be_written=[device_code_lower]
bytearrayObj=bytearray(byte_to_be_written)
mtdfile.write(bytearrayObj)
# Calculate the simple checksum-16
mtdfile.seek(0x09,0)
counter=0;
for bytecounter in range (0,244):
counter = int(ord(mtdfile.read(1))) + counter
counter_lower = (counter & 0xFF)
counter_upper = (counter >> 8) & 0xFF
# Write the checksum to 0x04 and 0x05
mtdfile.seek(0x04,0)
byte_to_be_written=[counter_lower]
bytearrayObj=bytearray(byte_to_be_written)
mtdfile.write(bytearrayObj)
byte_to_be_written=[counter_upper]
bytearrayObj=bytearray(byte_to_be_written)
mtdfile.write(bytearrayObj)
finally:
mtdfile.close()