+def extract_fx3_firmware(elf, symname, filename):
+ index = 0
+ blobs = []
+ while True:
+ sym = elf.dynsym.get(symname + '_' + str(index))
+ if not sym:
+ break
+ index += 1
+ hex = bytes.decode(elf.load_symbol(sym))
+ if hex[-1] == '\0':
+ hex = hex[:-1]
+ for part in hex.split(';'):
+ if not part:
+ continue
+ if part[0] != ':':
+ raise Exception('ihex line does not start with ":"')
+ blobs.append(bytes.fromhex(part[1:]))
+ if not blobs:
+ return
+ with open(filename, 'wb') as f:
+ for blob in blobs:
+ f.write(blob)
+ print("saved %d bytes from %d blobs to %s" % (sum(map(len, blobs)),
+ len(blobs), filename))
+