diff --git a/Tools/ardupilotwaf/chibios.py b/Tools/ardupilotwaf/chibios.py index c92acde02b..c0fb8e43bb 100644 --- a/Tools/ardupilotwaf/chibios.py +++ b/Tools/ardupilotwaf/chibios.py @@ -13,6 +13,8 @@ import shutil import sys import re import pickle +import zlib +import struct _dynamic_env_data = {} def _load_dynamic_env_data(bld): @@ -93,6 +95,46 @@ class generate_bin(Task.Task): def __str__(self): return self.outputs[0].path_from(self.generator.bld.bldnode) +def to_unsigned(i): + '''convert a possibly signed integer to unsigned''' + if i < 0: + i += 2**32 + return i + +class set_app_descriptor(Task.Task): + '''setup app descriptor in bin file''' + color='BLUE' + always_run = True + def keyword(self): + return "app_descriptor" + def run(self): + if not 'APP_DESCRIPTOR' in self.env: + return + if self.env.APP_DESCRIPTOR == 'MissionPlanner': + descriptor = b'\x40\xa2\xe4\xf1\x64\x68\x91\x06' + else: + Logs.error("Bad APP_DESCRIPTOR %s" % self.env.APP_DESCRIPTOR) + return + img = open(self.inputs[0].abspath(), 'rb').read() + offset = img.find(descriptor) + if offset == -1: + Logs.error("Failed to find %s APP_DESCRIPTOR" % self.env.APP_DESCRIPTOR) + return + offset += 8 + # 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 + # for bootloader to calculate + # after CRC comes image length and 32 bit git hash + desc_len = 16 + crc1 = to_unsigned(zlib.crc32(img[:offset])) + crc2 = to_unsigned(zlib.crc32(img[offset+desc_len:])) + githash = to_unsigned(int('0x' + self.generator.bld.git_head_hash(short=True),16)) + desc = struct.pack('