def load_elf(soc, elffile, imemory, dmemory): DATA_SECTIONS = [".data", ".sdata", ".rodata", ".srodata"] e = elf.ELFObject() f = open(elffile) e.fromFile(f) offset = 0 if e.e_machine != 0xf3: raise TypeError, "This is not a RISCV32 executable: %x" % (e.e_machine) for p in e.sections: if p.sh_flags & (elf.ELFSection.SHF_WRITE | elf.ELFSection.SHF_EXECINSTR | elf.ELFSection.SHF_ALLOC): if p.name[:5] == ".text" or p.name == ".init": # print "Writing program '%s' at %08x, len: %d" % (p.name, p.sh_addr, len(p.data)) write_imem(soc, imemory, p.data, p.sh_addr) elif p.name in DATA_SECTIONS: # print "Writing data '%s' at %08x, len: %d" % (p.name, p.sh_addr, len(p.data)) write_dmem(soc, dmemory, p.data, p.sh_addr) else: print "Dropping '%s' at %08x, len: %d" % (p.name, p.sh_addr, len(p.data)) return e.parseSymbols()
def main(argv): # Collect arguments if len(sys.argv) != 4: print 'usage: create_fl_image.py <elffile> <binfile> <cfile>' sys.exit(2) elfFilename = argv[0] binFilename = argv[1] cFilename = argv[2] # Open files try: binFile = open(binFilename, 'rb') except IOError: print 'cannot open file %s' % binFilename sys.exit(2) try: cFile = open(cFilename, 'w') except IOError: print 'cannot open file %s' % cFilename sys.exit(2) elfData = elf.ELFObject() try: with open(elfFilename, 'rb') as elfFile: elfData.fromFile(elfFile) if elfData.e_type != elf.ELFObject.ET_EXEC: raise Exception("No executable") resetHandler = elfData.getSymbol("Reset_Handler") vectors = elfData.getSymbol("__isr_vector") stack = elfData.getSymbol("__StackTop") except: print 'cannot process file %s' % elfFilename sys.exit(2) # Print header print >> cFile, '/* Created by create_fl_image.py, do not edit */\n' print >> cFile, '#include "bootloader_common.h"\n' print >> cFile, 'const uint8_t g_flashloaderImage[] = {' # Print byte data totalBytes = 0 while True: data = binFile.read(16) dataLen = len(data) if dataLen == 0: break totalBytes += dataLen cFile.write(' ') for i in range(dataLen): cFile.write('0x%02x, ' % ord(data[i])) print >> cFile print >> cFile, '};\n' # Print size and other info cFile.write('const uint32_t g_flashloaderSize = %dU;\n' % totalBytes) cFile.write('const uint32_t g_flashloaderBase = 0x%x;\n' % vectors.st_value) cFile.write('const uint32_t g_flashloaderEntry = 0x%x;\n' % resetHandler.st_value) cFile.write('const uint32_t g_flashloaderStack = 0x%x;\n' % stack.st_value)
def loadELF(self, file): """load data from a (opened) file in ELF object format. File must be seekable""" obj = elf.ELFObject() obj.fromFile(file) if obj.e_type != elf.ELFObject.ET_EXEC: raise Exception("No executable") for section in obj.getSections(): if DEBUG: sys.stderr.write("ELF section %s at 0x%04x %d bytes\n" % (section.name, section.lma, len(section.data))) if len(section.data): self.segments.append( Segment(section.lma, section.data) )
def loadELF(self, file): """load data from a (opened) file in ELF object format. File must be seekable""" obj = elf.ELFObject() obj.fromFile(file) if obj.e_type != elf.ELFObject.ET_EXEC: raise Exception("No executable") data_size = 0 for section in obj.getSections(): data_size += len(section.data) if options.debug: sys.stderr.write( "+- ELF section %s at 0x%04x %d bytes\n" % (section.name, section.lma, len(section.data))) if len(section.data): self.segments.append(Segment(section.lma, section.data)) data_perc = float(data_size / 32000) sys.stderr.write("+- Firmware: %d/32000 bytes.\n" % data_size)
def open_elf(infile): e = elf.ELFObject() f = open(infile) e.fromFile(f) f.close() return e