From d8ad636c0968bc9cb45fc0c365973eeb4011371c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Prokopi=C4=8D?= Date: Sun, 1 Dec 2024 14:21:13 +0100 Subject: [PATCH 1/3] [core] Allow calling UploadContext.collect_data multiple times --- uf2tool/models/context.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/uf2tool/models/context.py b/uf2tool/models/context.py index e8851e7..b938ce1 100644 --- a/uf2tool/models/context.py +++ b/uf2tool/models/context.py @@ -91,6 +91,9 @@ def get_offset(self, part: str, offs: int) -> Optional[int]: return None return start + offs + def rewind(self) -> None: + self.seq = 0 + def read_next(self, scheme: OTAScheme) -> Optional[Tuple[str, int, bytes]]: """ Read next available data block for the specified OTA scheme. @@ -152,6 +155,10 @@ def collect_data(self, scheme: OTAScheme) -> Optional[Dict[int, BytesIO]]: if not self.board_name: raise ValueError("This UF2 is not readable, since no board name is present") + # rewind to the beginning of the UF2 so collect_data can be called + # multiple times + self.rewind() + out: Dict[int, BytesIO] = {} while True: ret = self.read_next(scheme) From fe6885acf086bca640f26e79374277639db139fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Prokopi=C4=8D?= Date: Mon, 2 Dec 2024 20:53:26 +0100 Subject: [PATCH 2/3] [ambz2] Generate patched image for OTA --- ltchiptool/soc/ambz2/binary.py | 11 +++++++++++ ltchiptool/soc/ambz2/util/models/images.py | 2 ++ ltchiptool/soc/ambz2/util/ota.py | 10 ++++++++++ 3 files changed, 23 insertions(+) create mode 100644 ltchiptool/soc/ambz2/util/ota.py diff --git a/ltchiptool/soc/ambz2/binary.py b/ltchiptool/soc/ambz2/binary.py index e0e6b67..f7a1957 100644 --- a/ltchiptool/soc/ambz2/binary.py +++ b/ltchiptool/soc/ambz2/binary.py @@ -28,6 +28,7 @@ Section, ) from .util.models.utils import FF_32 +from .util.ota import patch_firmware_for_ota class AmebaZ2Binary(SocInterface, ABC): @@ -110,6 +111,12 @@ def elf2bin(self, input: str, ota_idx: int) -> List[FirmwareBinary]: description="Firmware partition image for direct flashing", public=True, ) + out_ota1_ota = FirmwareBinary( + location=input, + name="firmware_is_ota", + offset=ota1_offset, + title="Application Image for OTA", + ) out_ptab = FirmwareBinary( location=input, name="part_table", @@ -215,6 +222,10 @@ def elf2bin(self, input: str, ota_idx: int) -> List[FirmwareBinary]: with out_ota1.write() as f: ota1 = data[ota1_offset:ota1_end] f.write(ota1) + with out_ota1_ota.write() as f: + ota1 = data[ota1_offset:ota1_end] + ota1_ota = patch_firmware_for_ota(ota1) + f.write(ota1_ota) return output.group() def detect_file_type( diff --git a/ltchiptool/soc/ambz2/util/models/images.py b/ltchiptool/soc/ambz2/util/models/images.py index da6804a..9bfa764 100644 --- a/ltchiptool/soc/ambz2/util/models/images.py +++ b/ltchiptool/soc/ambz2/util/models/images.py @@ -29,6 +29,8 @@ FLASH_CALIBRATION = b"\x99\x99\x96\x96\x3F\xCC\x66\xFC\xC0\x33\xCC\x03\xE5\xDC\x31\x62" +IMAGE_PUBLIC_KEY_OFFSET = 32 + @dataclass class Image(DataStruct): diff --git a/ltchiptool/soc/ambz2/util/ota.py b/ltchiptool/soc/ambz2/util/ota.py new file mode 100644 index 0000000..12ed7a4 --- /dev/null +++ b/ltchiptool/soc/ambz2/util/ota.py @@ -0,0 +1,10 @@ +# Copyright (c) Martin Prokopič 2024-12-02 + +from .models.images import IMAGE_PUBLIC_KEY_OFFSET + + +def patch_firmware_for_ota(data: bytes) -> bytes: + copy = bytearray(data) + copy[0] ^= 0xff # negate first signature byte + copy[IMAGE_PUBLIC_KEY_OFFSET] ^= 0xff # negate first pubkey byte + return bytes(copy) From d272762e9638872bb0b059c20e09770cdaeb56c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Prokopi=C4=8D?= Date: Tue, 3 Dec 2024 21:48:26 +0100 Subject: [PATCH 3/3] [ambz2] Fix CI --- ltchiptool/soc/ambz2/util/models/images.py | 1 + ltchiptool/soc/ambz2/util/ota.py | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ltchiptool/soc/ambz2/util/models/images.py b/ltchiptool/soc/ambz2/util/models/images.py index 9bfa764..031b752 100644 --- a/ltchiptool/soc/ambz2/util/models/images.py +++ b/ltchiptool/soc/ambz2/util/models/images.py @@ -29,6 +29,7 @@ FLASH_CALIBRATION = b"\x99\x99\x96\x96\x3F\xCC\x66\xFC\xC0\x33\xCC\x03\xE5\xDC\x31\x62" +IMAGE_SIGNATURE_OFFSET = 0 IMAGE_PUBLIC_KEY_OFFSET = 32 diff --git a/ltchiptool/soc/ambz2/util/ota.py b/ltchiptool/soc/ambz2/util/ota.py index 12ed7a4..fdff561 100644 --- a/ltchiptool/soc/ambz2/util/ota.py +++ b/ltchiptool/soc/ambz2/util/ota.py @@ -1,10 +1,10 @@ # Copyright (c) Martin Prokopič 2024-12-02 -from .models.images import IMAGE_PUBLIC_KEY_OFFSET +from .models.images import IMAGE_PUBLIC_KEY_OFFSET, IMAGE_SIGNATURE_OFFSET def patch_firmware_for_ota(data: bytes) -> bytes: copy = bytearray(data) - copy[0] ^= 0xff # negate first signature byte - copy[IMAGE_PUBLIC_KEY_OFFSET] ^= 0xff # negate first pubkey byte + copy[IMAGE_SIGNATURE_OFFSET] ^= 0xFF # negate first signature byte + copy[IMAGE_PUBLIC_KEY_OFFSET] ^= 0xFF # negate first pubkey byte return bytes(copy)