with open('RoveConnectCTL.unpacked.unpatched', 'rb') as f:
    data = bytearray(f.read())

# fix header mess-ups
# endianness:
data[5] = 1
# machine:
data[0x12] = 0x3E

# skip 0x1000 bytes where the segment is loaded into a different place in memory
first_part = data[:0x26E8]
last_part = data[0x36E8:]

with open('fixup.bin', 'rb') as f:
    fixup_data = f.read()

data = first_part + last_part

# Set segment flags to RWX for the large data segment.
data[0x15C] = 0x7
# Change the relocation for the initialization function
data[0x848:0x84a] = [0x20, 0x40]
# Write the initialization fixup function.
# This is later overwritten by data, but
# at that point it isn't needed anymore.
data[0x3020:0x3020 + len(fixup_data)] = fixup_data

with open('RoveConnectCTL.unpacked', 'wb') as f:
    f.write(data)
