feat(core): improve flexibility of combine script, add combine fw make targets

[no changelog]
tychovrahe/u5/basic_support_revE_200ksprod
tychovrahe 3 months ago
parent 025ca9ca22
commit 14fa8d96e6

@ -44,7 +44,9 @@ OPENOCD_INTERFACE ?= stlink
OPENOCD_TRANSPORT ?= hla_swd
ifeq ($(TREZOR_MODEL),$(filter $(TREZOR_MODEL),T R DISC1))
MCU = STM32F4
OPENOCD_TARGET = target/stm32f4x.cfg
FLASH_START = 0x08000000
BOARDLOADER_START = 0x08000000
BOARDLOADER_END = 0x0800C000
BOOTLOADER_START = 0x08020000
@ -73,7 +75,9 @@ STORAGE_2_SECTOR_START = 16
STORAGE_2_SECTOR_END = 16
else ifeq ($(TREZOR_MODEL),$(filter $(TREZOR_MODEL),T3T1))
MCU = STM32U5
OPENOCD_TARGET = target/stm32u5x.cfg
FLASH_START = 0x0C000000
BOARDLOADER_START = 0x0C004000
BOARDLOADER_END = 0x0C010000
BOOTLOADER_START = 0x0C010000
@ -96,7 +100,9 @@ STORAGE_1_SECTOR_END = 0x1F
STORAGE_2_SECTOR_START = 0x20
STORAGE_2_SECTOR_END = 0x27
else ifeq ($(TREZOR_MODEL),$(filter $(TREZOR_MODEL),DISC2))
MCU = STM32U5
OPENOCD_TARGET = target/stm32u5x.cfg
FLASH_START = 0x0C000000
BOARDLOADER_START = 0x0C004000
BOARDLOADER_END = 0x0C010000
BOOTLOADER_START = 0x0C010000
@ -119,6 +125,7 @@ STORAGE_1_SECTOR_END = 0x1F
STORAGE_2_SECTOR_START = 0x20
STORAGE_2_SECTOR_END = 0x27
else ifeq ($(TREZOR_MODEL), 1)
MCU = STM32F2
OPENOCD_TARGET = target/stm32f2x.cfg
FIRMWARE_START = 0x08010000
else
@ -378,7 +385,7 @@ else
endif
flash_combine: $(PRODTEST_BUILD_DIR)/combined.bin ## flash combined using OpenOCD
$(OPENOCD) -c "init; reset halt; flash write_image erase $< $(BOARDLOADER_START); exit"
$(OPENOCD) -c "init; reset halt; flash write_image erase $< $(FLASH_START); exit"
flash_erase: ## erase all sectors in flash bank 0
$(OPENOCD) -c "init; reset halt; flash info 0; flash erase_sector 0 0 last; flash erase_check 0; exit"
@ -456,16 +463,37 @@ sizecheck: ## check sizes of binary files
test $(FIRMWARE_P2_MAXSIZE) -ge $(shell wc -c < $(FIRMWARE_BUILD_DIR)/firmware.bin.p2)
test $(FIRMWARE_MAXSIZE) -ge $(shell wc -c < $(FIRMWARE_BUILD_DIR)/firmware.bin)
ifeq ($(MCU),$(filter $(MCU),STM32F4))
combine: ## combine boardloader + bootloader + prodtest into one combined image
./tools/combine_firmware \
$(BOARDLOADER_BUILD_DIR)/boardloader.bin \
$(BOOTLOADER_BUILD_DIR)/bootloader.bin \
$(PRODTEST_BUILD_DIR)/prodtest.bin \
$(PRODTEST_BUILD_DIR)/combined.bin \
$(BOARDLOADER_START) \
$(BOARDLOADER_END) \
$(BOOTLOADER_START) \
$(PRODTEST_START)
-b $(BOARDLOADER_BUILD_DIR)/boardloader.bin $(BOARDLOADER_START) \
-b $(BOOTLOADER_BUILD_DIR)/bootloader.bin $(BOOTLOADER_START) \
-b $(PRODTEST_BUILD_DIR)/prodtest.bin $(FIRMWARE_START)
combine_fw: ## combine boardloader + bootloader + firmware into one combined image
./tools/combine_firmware \
$(PRODTEST_BUILD_DIR)/combined.bin \
-b $(BOARDLOADER_BUILD_DIR)/boardloader.bin $(BOARDLOADER_START) \
-b $(BOOTLOADER_BUILD_DIR)/bootloader.bin $(BOOTLOADER_START) \
-b $(FIRMWARE_BUILD_DIR)/firmware.bin.p1 $(FIRMWARE_START) \
-b $(FIRMWARE_BUILD_DIR)/firmware.bin.p2 $(FIRMWARE_P2_START)
else ifeq ($(MCU),$(filter $(MCU),STM32U5))
combine: ## combine boardloader + bootloader + prodtest into one combined image
./tools/combine_firmware \
$(PRODTEST_BUILD_DIR)/combined.bin \
-b $(BOARDLOADER_BUILD_DIR)/boardloader.bin $(BOARDLOADER_START) \
-b $(BOOTLOADER_BUILD_DIR)/bootloader.bin $(BOOTLOADER_START) \
-b $(PRODTEST_BUILD_DIR)/prodtest.bin $(FIRMWARE_START)
combine_fw: ## combine boardloader + bootloader + firmware into one combined image
./tools/combine_firmware \
$(PRODTEST_BUILD_DIR)/combined.bin \
-b $(BOARDLOADER_BUILD_DIR)/boardloader.bin $(BOARDLOADER_START) \
-b $(BOOTLOADER_BUILD_DIR)/bootloader.bin $(BOOTLOADER_START) \
-b $(FIRMWARE_BUILD_DIR)/firmware.bin $(FIRMWARE_START)
endif
upload: ## upload firmware using trezorctl
trezorctl firmware_update -s -f $(FIRMWARE_BUILD_DIR)/firmware.bin

@ -3,99 +3,45 @@ from __future__ import annotations
import datetime
import io
import sys
from pathlib import Path
import click
@click.command()
@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,
)
@click.argument(
"boardloader_start",
type=click.STRING,
required=False,
default="0x08000000",
)
@click.argument(
"boardloader_end",
type=click.STRING,
required=False,
default="0x0800C000",
)
@click.argument(
"bootloader_start",
type=click.STRING,
required=False,
default="0x08020000",
)
@click.argument(
"firmware_start",
type=click.STRING,
required=False,
default="0x08040000",
@click.option(
"--bin",
"-b",
type=(click.Path(exists=True, dir_okay=False, readable=True, path_type=Path), str),
multiple=True,
)
def main(
boardloader: Path,
bootloader: Path,
firmware: Path,
bin: List[Tuple[Path, str]],
outfile: Path | None,
boardloader_start: str,
boardloader_end: str,
bootloader_start: str,
firmware_start: str,
) -> None:
boardloader_start = int(boardloader_start, 0)
boardloader_end = int(boardloader_end, 0)
bootloader_start = int(bootloader_start, 0)
firmware_start = int(firmware_start, 0)
) -> None:
if outfile is None:
today = datetime.date.today().strftime(r"%Y-%m-%d")
outfile = Path(f"combined-{today}.bin")
offset = boardloader_start
offset = int(bin[0][1], 0)
out_bytes = io.BytesIO()
# write boardloader
offset += out_bytes.write(boardloader.read_bytes())
if offset > boardloader_end:
raise Exception("Boardloader too big")
# zero-pad until next section:
offset += out_bytes.write(b"\x00" * (bootloader_start - offset))
assert offset == bootloader_start
# 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
for b in bin:
# zero-pad until next section:
offset += out_bytes.write(b"\x00" * (int(b[1], 0) - offset))
assert offset == int(b[1], 0)
# write firmware
offset += out_bytes.write(firmware.read_bytes())
# write binary
offset += out_bytes.write(b[0].read_bytes())
# write out contents
click.echo(f"Writing {outfile} ({offset - boardloader_start} bytes)")
click.echo(f"Writing {outfile} ({offset - int(bin[0][1], 0)} bytes)")
outfile.write_bytes(out_bytes.getvalue())

Loading…
Cancel
Save