Skip to content

Instantly share code, notes, and snippets.

@qiayuanl
Created March 21, 2024 20:54
Show Gist options
  • Save qiayuanl/29ff2e56f42db9c6ca079f9736497224 to your computer and use it in GitHub Desktop.
Save qiayuanl/29ff2e56f42db9c6ca079f9736497224 to your computer and use it in GitHub Desktop.
Intel hex modifly
def calculate_checksum(record):
total = sum(record)
checksum = ((~total + 1) & 0xFF)
return checksum
def verify_and_parse_record(line):
line = line.strip()[1:]
byte_count = int(line[0:2], 16)
address = int(line[2:6], 16)
record_type = int(line[6:8], 16)
data = [int(line[i:i+2], 16) for i in range(8, 8 + byte_count * 2, 2)]
checksum = int(line[-2:], 16)
return byte_count, address, record_type, data, checksum
def verify_record(byte_count, address, record_type, data, checksum):
record_bytes = [byte_count, (address >> 8) & 0xFF, address & 0xFF, record_type] + data
calculated_checksum = calculate_checksum(record_bytes)
return calculated_checksum == checksum
def modify_record(records, address, new_value):
for i, (byte_count, record_address, record_type, data, checksum) in enumerate(records):
if record_address <= address < (record_address + byte_count):
offset = address - record_address
data[offset] = new_value
record_bytes = [byte_count, (record_address >> 8) & 0xFF, record_address & 0xFF, record_type] + data
records[i] = (byte_count, record_address, record_type, data, calculate_checksum(record_bytes))
return True
return False
def read_hex_file(file_path):
records = []
with open(file_path, 'r') as file:
for line in file:
byte_count, address, record_type, data, checksum = verify_and_parse_record(line)
if verify_record(byte_count, address, record_type, data, checksum):
records.append((byte_count, address, record_type, data, checksum))
else:
print(f"Checksum mismatch in record: {line.strip()}")
return None
return records
def user_interface(records):
while True:
address = input("Enter the address to modify (or 'quit' to exit): ")
if address.lower() == 'quit':
break
value = input("Enter the new value (hex format, e.g., 1A): ")
if not modify_record(records, int(address, 16), int(value, 16)):
print(f"Address {address} not found or modification failed.")
else:
print(f"Value at address {address} modified to {value}.")
def format_record(byte_count, address, record_type, data, checksum):
record_format = ":{:02X}{:04X}{:02X}".format(byte_count, address, record_type)
data_format = ''.join("{:02X}".format(byte) for byte in data)
full_record = record_format + data_format + "{:02X}".format(checksum)
return full_record
def write_hex_file(records, output_path):
with open(output_path, 'w') as file:
for byte_count, address, record_type, data, checksum in records:
record_line = format_record(byte_count, address, record_type, data, checksum)
file.write(record_line + '\n')
# Write the end-of-file record
file.write(":00000001FF\n")
def main():
file_path = '/home/qiayuanl/Project/SOEM/build/test/linux/eepromtool/eeprom.hex' # Change to your HEX file path
output_path = '/home/qiayuanl/Project/SOEM/build/test/linux/eepromtool/eeprom_mod.hex' # Change to your desired output file path
records = read_hex_file(file_path)
if records is not None:
print("File verified successfully. Entering modification mode.")
user_interface(records)
print("Exiting modification mode. Writing modifications to the output file.")
write_hex_file(records, output_path)
print(f"Modifications have been written to {output_path}.")
else:
print("File verification failed.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment