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)