def elf2xuv(offset, elf_filename, xuv_filename=None): """ Reads an elf file and dumps the loadable sections in an XUV file. The offset parameter is the number of words by which to shift the address in the XUV file. This is usually 0. """ if xuv_filename is None: xuv_filename = elf_filename.replace(".elf", ".xuv") print "Loading elf symbols from %s" % elf_filename layout_info = Kalimba32CoreInfo().layout_info elfcode = ElfCodeReader(elf_filename, layout_info) xuv_file = open(xuv_filename, "w") xuv_file.write("// Apps Firmware image dumped from ELF by %s\n" % sys.argv[0]) xuv_encoder = XUVStreamEncoder(xuv_file) for section in elfcode.sections: addr = section.paddr data = section.data name = section.name print(" %s block starting at 0x%08x, %d bytes" % (name, addr, len(data))) words = bytes_to_words(data) for value in words: xuv_encoder.address_value_pair((addr + offset) / 2, value) addr += 2 xuv_file.close() print "Done.\nCreated %s" % xuv_filename
def pack_dir(dir_to_pack, out, packfile=DEFAULT_PACKFILE, offset=DEFAULT_OFFSET, appsfs=False): """ Create a filesystem image starting at the specified address offset """ #1. Run packfile on the specified directory print("Packing directory...") handle, tmp_packed_img = tempfile.mkstemp(suffix=".fs") os.close(handle) # We close this since we need packfile to write to it first if subprocess.call([packfile, dir_to_pack, tmp_packed_img], stderr=subprocess.STDOUT) != 0: raise RuntimeError("%s failed" % packfile) #2. Translate the XUV file to the offset addresses print("Translating filesystem image by 0x%06x" % (offset)) with open(tmp_packed_img) as raw_img: decoder = XUVStreamDecoder(raw_img) encoder = XUVStreamEncoder(out) if appsfs: profs = PackfileReadOnlyFS([(a, v) for a, v in decoder.address_value_pairs]) data = loadable_to_bytes(profs.loadable_le32, verbose=True) if len(data) & 1: print("padding odd length with a single byte") data += bytearray([0xff]) addr = 0 for val in bytes_to_words(data): encoder.address_value_pair(addr + offset, val) addr += 1 else: for addr, val in decoder.address_value_pairs: encoder.address_value_pair(addr+offset, val) os.remove(tmp_packed_img)