summaryrefslogtreecommitdiff
path: root/fx3parse.py
blob: 8ff08e4a31d4d32f64eae06f8b1b0e7c76e3a3a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import binascii
import struct

data = open("firmware/qhy/QHY367.img").read()

if data[0:3] == [0x43, 0x59, 0x1c, 0xb0]:
    print("looks like an fx3 image to me!")

def read_section(data, offset):
    length = struct.unpack_from("i", data, offset=offset)[0]
    addr = struct.unpack_from("i", data, offset=offset + 4)[0]
    return (data[offset + 8:offset + 8 + length * 4], addr)

offset = 4

sections = []

while True:
    (section, addr) = read_section(data, offset)
    sections.append((section, addr, offset))
    print("read section at addr {}, length {}, file offset {}".format(hex(addr), hex(len(section)), hex(offset)))
    offset = offset + 8 + len(section)
    if len(section) == 0:
        break

def offsetof(sections, addr):
    for (section, start, offset) in sections:
        print("considering section {} +{}".format(hex(start), hex(offset)))
        if addr >= start and addr < start + len(section):
            sec_addr = addr - start
            print("file offset of {}: {}".format(hex(addr), hex(sec_addr + offset + 8)))

offsetof(sections, 0x40010a30)