Skip to content

Commit

Permalink
waf: fixed app signature in elf files
Browse files Browse the repository at this point in the history
this fixes an issue when developing for ChibiOS AP_Periph targets
where loading the elf file in gdb doesn't allow it to run as it
doesn't have the correct AP_Periph signature (crc, board type etc)

This patch modifies the elf file to fill in the signature, so when you
load in gdb the bootloader will be able to run the signature checks
and load the firmware
  • Loading branch information
tridge committed Sep 9, 2024
1 parent 9726e8e commit ab15ae5
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions Tools/ardupilotwaf/chibios.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,14 @@ def run(self):
else:
descriptor = b'\x40\xa2\xe4\xf1\x64\x68\x91\x06'

img = open(self.inputs[0].abspath(), 'rb').read()
elf_file = self.inputs[0].abspath()
bin_file = self.inputs[1].abspath()
img = open(bin_file, 'rb').read()
offset = img.find(descriptor)
if offset == -1:
Logs.info("No APP_DESCRIPTOR found")
return
offset += 8
offset += len(descriptor)
# next 8 bytes is 64 bit CRC. We set first 4 bytes to
# CRC32 of image before descriptor and 2nd 4 bytes
# to CRC32 of image after descriptor. This is very efficient
Expand Down Expand Up @@ -310,7 +312,19 @@ def run(self):
desc = struct.pack('<IIII', crc1, crc2, len(img), githash)
img = img[:offset] + desc + img[offset+desc_len:]
Logs.info("Applying APP_DESCRIPTOR %08x%08x" % (crc1, crc2))
open(self.inputs[0].abspath(), 'wb').write(img)
open(bin_file, 'wb').write(img)

elf_img = open(elf_file,'rb').read()
zero_descriptor = descriptor + struct.pack("<IIII",0,0,0,0)
elf_ofs = elf_img.find(zero_descriptor)
if elf_ofs == -1:
Logs.info("No APP_DESCRIPTOR found in elf file")
return
elf_ofs += len(descriptor)
elf_img = elf_img[:elf_ofs] + desc + elf_img[elf_ofs+desc_len:]
Logs.info("Applying APP_DESCRIPTOR %08x%08x to elf" % (crc1, crc2))
open(elf_file, 'wb').write(elf_img)


class generate_apj(Task.Task):
'''generate an apj firmware file'''
Expand Down Expand Up @@ -438,7 +452,7 @@ def chibios_firmware(self):

# we need to setup the app descriptor so the bootloader can validate the firmware
if not self.bld.env.BOOTLOADER:
app_descriptor_task = self.create_task('set_app_descriptor', src=bin_target)
app_descriptor_task = self.create_task('set_app_descriptor', src=[link_output,bin_target[0]])
app_descriptor_task.set_run_after(generate_bin_task)
generate_apj_task.set_run_after(app_descriptor_task)
if hex_task is not None:
Expand Down

0 comments on commit ab15ae5

Please sign in to comment.