1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-18 04:18:10 +00:00

feat(core/tools): make combine_firmware script a bit smarter and more opinionated

This commit is contained in:
matejcik 2023-04-21 12:03:11 +02:00 committed by matejcik
parent 3fb3f108cc
commit 9f597a6072
2 changed files with 67 additions and 20 deletions

View File

@ -321,10 +321,10 @@ sizecheck: ## check sizes of binary files
combine: ## combine boardloader + bootloader + prodtest into one combined image combine: ## combine boardloader + bootloader + prodtest into one combined image
./tools/combine_firmware \ ./tools/combine_firmware \
$(BOARDLOADER_START) $(BOARDLOADER_BUILD_DIR)/boardloader.bin \ $(BOARDLOADER_BUILD_DIR)/boardloader.bin \
$(BOOTLOADER_START) $(BOOTLOADER_BUILD_DIR)/bootloader.bin \ $(BOOTLOADER_BUILD_DIR)/bootloader.bin \
$(PRODTEST_START) $(PRODTEST_BUILD_DIR)/prodtest.bin \ $(PRODTEST_BUILD_DIR)/prodtest.bin \
> $(PRODTEST_BUILD_DIR)/combined.bin $(PRODTEST_BUILD_DIR)/combined.bin
upload: ## upload firmware using trezorctl upload: ## upload firmware using trezorctl
trezorctl firmware_update -f $(FIRMWARE_BUILD_DIR)/firmware.bin trezorctl firmware_update -f $(FIRMWARE_BUILD_DIR)/firmware.bin

View File

@ -1,26 +1,73 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from __future__ import annotations
import datetime
import io
import sys import sys
from pathlib import Path
import click
def pairwise(iterable): BOARDLOADER_START = 0x08000000
a = iter(iterable) BOARDLOADER_END = 0x0800C000
return zip(a, a) BOOTLOADER_START = 0x08020000
FIRMWARE_START = 0x08040000
files = sys.argv[1:] @click.command()
files = list(pairwise(files)) @click.argument(
"boardloader",
type=click.Path(exists=True, dir_okay=False, readable=True, path_type=Path),
)
@click.argument(
"bootloader",
type=click.Path(exists=True, dir_okay=False, readable=True, path_type=Path),
)
@click.argument(
"firmware",
type=click.Path(exists=True, dir_okay=False, readable=True, path_type=Path),
)
@click.argument(
"outfile",
type=click.Path(dir_okay=False, writable=True, path_type=Path),
required=False,
)
def main(
boardloader: Path, bootloader: Path, firmware: Path, outfile: Path | None
) -> None:
if outfile is None:
today = datetime.date.today().strftime(r"%Y-%m-%d")
outfile = Path(f"combined-{today}.bin")
offset = int(files[0][0], 16) offset = BOARDLOADER_START
out_bytes = io.BytesIO()
out = bytearray() # write boardloader
offset += out_bytes.write(boardloader.read_bytes())
if offset > BOARDLOADER_END:
raise Exception("Boardloader too big")
for addr, fn in files: # zero-pad until next section:
addr = int(addr, 16) - offset offset += out_bytes.write(b"\x00" * (BOOTLOADER_START - offset))
data = open(fn, "rb").read() assert offset == BOOTLOADER_START
if len(out) < addr:
out += b"\x00" * (addr - len(out))
if len(out) != addr:
raise Exception("Alignment failed")
out += data
sys.stdout.buffer.write(out) # write bootlaoder
offset += out_bytes.write(bootloader.read_bytes())
if offset > FIRMWARE_START:
raise Exception("Bootloader too big")
# zero-pad until next section:
offset += out_bytes.write(b"\x00" * (FIRMWARE_START - offset))
assert offset == FIRMWARE_START
# write firmware
offset += out_bytes.write(firmware.read_bytes())
# write out contents
click.echo(f"Writing {outfile} ({offset - BOARDLOADER_START} bytes)")
outfile.write_bytes(out_bytes.getvalue())
if __name__ == "__main__":
main()