mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-19 22:00:28 +01:00
[scripts] Change elf2dfu to build a .dfu with 2 elements: internal &
external
This commit is contained in:
@@ -11,8 +11,8 @@ import argparse
|
||||
# arm-none-eabi-objdump -h -w file.elf
|
||||
# arm-none-eabi-objcopy -O binary -j .data file.elf file.bin
|
||||
|
||||
def loadable_sections(elf_file):
|
||||
objdump_section_headers_pattern = re.compile("^\s+\d+\s+(\.[\w\.]+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([0-9a-f]+).*LOAD", flags=re.MULTILINE)
|
||||
def loadable_sections(elf_file, section_name_appendix = ""):
|
||||
objdump_section_headers_pattern = re.compile("^\s+\d+\s+(\.[\w\.]+"+section_name_appendix+")\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([0-9a-f]+).*LOAD", flags=re.MULTILINE)
|
||||
objdump_output = subprocess.check_output(["arm-none-eabi-objdump", "-h", "-w", elf_file])
|
||||
sections = []
|
||||
for (name, size, vma, lma, offset) in re.findall(objdump_section_headers_pattern, objdump_output):
|
||||
@@ -39,23 +39,30 @@ def generate_dfu_file(targets, usb_vid_pid, dfu_file):
|
||||
data += struct.pack('<I', crc)
|
||||
open(dfu_file, 'wb').write(data)
|
||||
|
||||
def bin_file(section):
|
||||
return "section" + section['name'] + ".bin"
|
||||
def bin_file(block):
|
||||
return "firmware" + block['name'] + ".bin"
|
||||
|
||||
def print_sections(sections):
|
||||
for s in sections:
|
||||
print("%s-%s: %s, %s" % (hex(s['lma']), hex(s['lma'] + s['size'] - 1), s['name'], "{:,} bytes".format(s['size'])))
|
||||
|
||||
def elf2dfu(elf_file, usb_vid_pid, dfu_file, verbose):
|
||||
sections = loadable_sections(elf_file)
|
||||
external_block = {'name': "external", 'sections': loadable_sections(elf_file, "external")}
|
||||
internal_block = {'name': "internal", 'sections': [s for s in loadable_sections(elf_file) if s not in external_block['sections']]}
|
||||
blocks = [external_block, internal_block]
|
||||
if verbose:
|
||||
print_sections(sections)
|
||||
for section in sections:
|
||||
subprocess.call(["arm-none-eabi-objcopy", "-O", "binary", "-j", section['name'], elf_file, bin_file(section)])
|
||||
targets = map(lambda s: {'address': s['lma'], 'name': s['name'], 'data': open(bin_file(s)).read()}, sections)
|
||||
for b in blocks:
|
||||
print(b['name'])
|
||||
print_sections(b['sections'])
|
||||
targets = []
|
||||
for b in blocks:
|
||||
name = b['name']
|
||||
subprocess.call(["arm-none-eabi-objcopy", "-O", "binary"]+[item for sublist in [["-j", s['name']] for s in b['sections']] for item in sublist]+[elf_file, bin_file(b)])
|
||||
address = min([s['lma'] for s in b['sections']])
|
||||
targets.append({'address': address, 'name': name, 'data': open(bin_file(b)).read()})
|
||||
generate_dfu_file([targets], usb_vid_pid, dfu_file)
|
||||
for section in sections:
|
||||
subprocess.call(["rm", bin_file(section)])
|
||||
for b in blocks:
|
||||
subprocess.call(["rm", bin_file(b)])
|
||||
|
||||
parser = argparse.ArgumentParser(description="Convert an ELF file to DfuSe.")
|
||||
parser.add_argument('elf_file', help='Input ELF file')
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
%.dfu: %.$(EXE)
|
||||
@echo "DFUSE $@"
|
||||
$(Q) $(PYTHON) build/device/elf2dfu.py $< $@
|
||||
$(Q) $(PYTHON) scripts/device/elf2dfu.py $< $@
|
||||
|
||||
%.hex: %.$(EXE)
|
||||
@echo "OBJCOPY $@"
|
||||
|
||||
Reference in New Issue
Block a user