#!/usr/bin/env python3 """Extract the first loadable section of a TI GSP COFF file as raw bytes. GSPLNK writes a COFF whose section header carries the FULL 32-bit bit address in `paddr`, so placement does not depend on gsprom's hex output (whose addresses wrap at 64 KB -- documented in reference/tms34010-sdk.md). Usage: cofextract.py hello.out payload.bin Prints: origin= size= file_offset= """ import struct import sys # TI TMS34010 COFF: 20-byte file header, optional header size in the header. # Section header: name[8] paddr(4) vaddr(4) size(4) scnptr(4) relptr(4) # lnnoptr(4) nreloc(2) nlnno(2) flags(4) = 40 bytes def main(out_path, bin_path): d = open(out_path, "rb").read() if len(d) < 20: sys.exit("cofextract: file too short") magic, nscns, _timdat, _symptr, _nsyms, opthdr, _flags = struct.unpack_from( "> 3. nbytes = size >> 3 payload = d[scnptr:scnptr + nbytes] if len(payload) != nbytes: sys.exit("cofextract: truncated section (%d of %d bytes)" % (len(payload), nbytes)) open(bin_path, "wb").write(payload) print("origin=%08X size=%d file_offset=%#x md5=%s" % ( paddr, nbytes, scnptr, __import__("hashlib").md5(payload).hexdigest())) if __name__ == "__main__": if len(sys.argv) != 3: sys.exit("usage: cofextract.py hello.out payload.bin") main(sys.argv[1], sys.argv[2])