diff --git a/core/Makefile b/core/Makefile index f39fa4a68..759f5500d 100644 --- a/core/Makefile +++ b/core/Makefile @@ -42,89 +42,59 @@ OPENOCD_INTERFACE ?= stlink # OpenOCD transport default. Alternative: jtag OPENOCD_TRANSPORT ?= hla_swd -ifeq ($(TREZOR_MODEL),$(filter $(TREZOR_MODEL),T R DISC1)) -OPENOCD_TARGET = target/stm32f4x.cfg -BOARDLOADER_START = 0x08000000 -BOARDLOADER_END = 0x0800C000 -BOOTLOADER_START = 0x08020000 -FIRMWARE_START = 0x08040000 -FIRMWARE_P2_START = 0x08120000 -PRODTEST_START = 0x08040000 -STORAGE_1_OFFSET = 0x10000 -STORAGE_2_OFFSET = 0x110000 -STORAGE_SIZE = 0x10000 -BOARDLOADER_MAXSIZE = 49152 -BOOTLOADER_MAXSIZE = 131072 -FIRMWARE_P1_MAXSIZE = 786432 -FIRMWARE_P2_MAXSIZE = 917504 -FIRMWARE_MAXSIZE = 1703936 -BOARDLOADER_SECTOR_START = 0 -BOARDLOADER_SECTOR_END = 3 -BOOTLOADER_SECTOR_START = 5 -BOOTLOADER_SECTOR_END = 5 -FIRMWARE_SECTOR_START = 6 -FIRMWARE_SECTOR_END = 11 -FIRMWARE_P2_SECTOR_START = 17 -FIRMWARE_P2_SECTOR_END = 23 -STORAGE_1_SECTOR_START = 4 -STORAGE_1_SECTOR_END = 4 -STORAGE_2_SECTOR_START = 16 -STORAGE_2_SECTOR_END = 16 - -else ifeq ($(TREZOR_MODEL),$(filter $(TREZOR_MODEL),T3T1)) -OPENOCD_TARGET = target/stm32u5x.cfg -BOARDLOADER_START = 0x0C004000 -BOARDLOADER_END = 0x0C010000 -BOOTLOADER_START = 0x0C010000 -FIRMWARE_START = 0x0C050000 -PRODTEST_START = 0x0C050000 -STORAGE_1_OFFSET = 0x30000 -STORAGE_2_OFFSET = 0x50000 -STORAGE_SIZE = 0x10000 -BOARDLOADER_MAXSIZE = 49152 -BOOTLOADER_MAXSIZE = 131072 -FIRMWARE_MAXSIZE = 1703936 -BOARDLOADER_SECTOR_START = 0x2 -BOARDLOADER_SECTOR_END = 0x7 -BOOTLOADER_SECTOR_START = 0x8 -BOOTLOADER_SECTOR_END = 0x17 -FIRMWARE_SECTOR_START = 0x28 -FIRMWARE_SECTOR_END = 0xF8 -STORAGE_1_SECTOR_START = 0x18 -STORAGE_1_SECTOR_END = 0x1F -STORAGE_2_SECTOR_START = 0x20 -STORAGE_2_SECTOR_END = 0x27 -else ifeq ($(TREZOR_MODEL),$(filter $(TREZOR_MODEL),DISC2)) -OPENOCD_TARGET = target/stm32u5x.cfg -BOARDLOADER_START = 0x0C004000 -BOARDLOADER_END = 0x0C010000 -BOOTLOADER_START = 0x0C010000 -FIRMWARE_START = 0x0C050000 -PRODTEST_START = 0x0C050000 -STORAGE_1_OFFSET = 0x30000 -STORAGE_2_OFFSET = 0x50000 -STORAGE_SIZE = 0x10000 -BOARDLOADER_MAXSIZE = 49152 -BOOTLOADER_MAXSIZE = 131072 -FIRMWARE_MAXSIZE = 3735552 -BOARDLOADER_SECTOR_START = 0x2 -BOARDLOADER_SECTOR_END = 0x7 -BOOTLOADER_SECTOR_START = 0x8 -BOOTLOADER_SECTOR_END = 0x17 -FIRMWARE_SECTOR_START = 0x28 -FIRMWARE_SECTOR_END = 0x1F8 -STORAGE_1_SECTOR_START = 0x18 -STORAGE_1_SECTOR_END = 0x1F -STORAGE_2_SECTOR_START = 0x20 -STORAGE_2_SECTOR_END = 0x27 -else ifeq ($(TREZOR_MODEL), 1) +ifeq ($(TREZOR_MODEL), 1) +MCU = STM32F2 +LAYOUT_FILE = embed/models/model_T1B1.h OPENOCD_TARGET = target/stm32f2x.cfg -FIRMWARE_START = 0x08010000 +else ifeq ($(TREZOR_MODEL),$(filter $(TREZOR_MODEL),T)) +MCU = STM32F4 +LAYOUT_FILE = embed/models/model_T2T1.h +OPENOCD_TARGET = target/stm32f4x.cfg +else ifeq ($(TREZOR_MODEL),$(filter $(TREZOR_MODEL),R)) +MCU = STM32F4 +LAYOUT_FILE = embed/models/model_T2B1.h +OPENOCD_TARGET = target/stm32f4x.cfg +else ifeq ($(TREZOR_MODEL),$(filter $(TREZOR_MODEL),T3T1)) +MCU = STM32U5 +OPENOCD_TARGET = target/stm32u5x.cfg +LAYOUT_FILE = embed/models/model_T3T1.h +else ifeq ($(TREZOR_MODEL),$(filter $(TREZOR_MODEL),DISC1)) +MCU = STM32F4 +LAYOUT_FILE = embed/models/model_D001.h +else ifeq ($(TREZOR_MODEL),$(filter $(TREZOR_MODEL),DISC2)) +MCU = STM32U5 +LAYOUT_FILE = embed/models/model_D002.h +OPENOCD_TARGET = target/stm32u5x.cfg else $(error Unknown TREZOR_MODEL: $(TREZOR_MODEL)) endif +FLASH_START = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} FLASH_START) +BOARDLOADER_START = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} BOARDLOADER_START) +BOOTLOADER_START = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} BOOTLOADER_START) +FIRMWARE_START = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} FIRMWARE_START) +STORAGE_1_OFFSET = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} STORAGE_1_OFFSET) +STORAGE_2_OFFSET = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} STORAGE_2_OFFSET) +STORAGE_SIZE = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} NORCOW_SECTOR_SIZE) +BOARDLOADER_MAXSIZE = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} BOARDLOADER_IMAGE_MAXSIZE) +BOOTLOADER_MAXSIZE = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} BOOTLOADER_IMAGE_MAXSIZE) +FIRMWARE_MAXSIZE = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} FIRMWARE_IMAGE_MAXSIZE) +FIRMWARE_P1_MAXSIZE = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} FIRMWARE_P1_IMAGE_MAXSIZE) +FIRMWARE_P2_MAXSIZE = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} FIRMWARE_P2_IMAGE_MAXSIZE) +BOARDLOADER_SECTOR_START = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} BOARDLOADER_SECTOR_START) +BOARDLOADER_SECTOR_END = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} BOARDLOADER_SECTOR_END) +BOOTLOADER_SECTOR_START = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} BOOTLOADER_SECTOR_START) +BOOTLOADER_SECTOR_END = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} BOOTLOADER_SECTOR_END) +FIRMWARE_SECTOR_START = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} FIRMWARE_SECTOR_START) +FIRMWARE_SECTOR_END = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} FIRMWARE_SECTOR_END) +FIRMWARE_P2_SECTOR_START = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} FIRMWARE_P2_SECTOR_START) +FIRMWARE_P2_SECTOR_END = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} FIRMWARE_P2_SECTOR_END) +STORAGE_1_SECTOR_START = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} STORAGE_1_SECTOR_START) +STORAGE_1_SECTOR_END = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} STORAGE_1_SECTOR_END) +STORAGE_2_SECTOR_START = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} STORAGE_2_SECTOR_START) +STORAGE_2_SECTOR_END = $(shell python ./tools/layout_parser.py ${LAYOUT_FILE} STORAGE_2_SECTOR_END) + OPENOCD = openocd -f interface/$(OPENOCD_INTERFACE).cfg -c "transport select $(OPENOCD_TRANSPORT)" -f $(OPENOCD_TARGET) CFLAGS += -DSCM_REVISION='\"$(shell git rev-parse HEAD | sed 's:\(..\):\\x\1:g')\"' @@ -356,10 +326,10 @@ flash_bootloader_ci: $(BOOTLOADER_CI_BUILD_DIR)/bootloader.bin ## flash CI bootl $(OPENOCD) -c "init; reset halt; flash write_image erase $< $(BOOTLOADER_START); exit" flash_prodtest: $(PRODTEST_BUILD_DIR)/prodtest.bin ## flash prodtest using OpenOCD - $(OPENOCD) -c "init; reset halt; flash write_image erase $< $(PRODTEST_START); exit" + $(OPENOCD) -c "init; reset halt; flash write_image erase $< $(FIRMWARE_START); exit" flash_firmware: $(FIRMWARE_BUILD_DIR)/firmware.bin ## flash firmware using OpenOCD -ifdef FIRMWARE_P2_START +ifeq ($(MCU),$(filter $(MCU),STM32F4)) $(OPENOCD) -c "init; reset halt; flash write_image erase $<.p1 $(FIRMWARE_START); flash write_image erase $<.p2 $(FIRMWARE_P2_START); exit" else @@ -367,7 +337,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" @@ -376,7 +346,7 @@ flash_erase_bootloader: ## erase bootloader $(OPENOCD) -c "init; reset halt; flash info 0; flash erase_sector 0 $(BOOTLOADER_SECTOR_START) $(BOOTLOADER_SECTOR_END); exit" flash_erase_firmware: ## erase bootloader -ifdef FIRMWARE_P2_START +ifeq ($(MCU),$(filter $(MCU),STM32F4)) $(OPENOCD) -c "init; reset halt; flash info 0; flash erase_sector 0 $(FIRMWARE_SECTOR_START) $(FIRMWARE_SECTOR_END); flash erase_sector 0 $(FIRMWARE_P2_SECTOR_START) $(FIRMWARE_P2_SECTOR_END); exit" else @@ -447,14 +417,30 @@ sizecheck: ## check sizes of binary files 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 \ + $(LAYOUT_FILE) \ $(PRODTEST_BUILD_DIR)/combined.bin \ - $(BOARDLOADER_START) \ - $(BOARDLOADER_END) \ - $(BOOTLOADER_START) \ - $(PRODTEST_START) + -b BOARDLOADER $(BOARDLOADER_BUILD_DIR)/boardloader.bin \ + -b BOOTLOADER $(BOOTLOADER_BUILD_DIR)/bootloader.bin \ + -b FIRMWARE $(PRODTEST_BUILD_DIR)/prodtest.bin + +ifeq ($(MCU),$(filter $(MCU),STM32F4)) +combine_fw: ## combine boardloader + bootloader + firmware into one combined image + ./tools/combine_firmware \ + $(LAYOUT_FILE) \ + $(PRODTEST_BUILD_DIR)/combined.bin \ + -b BOARDLOADER $(BOARDLOADER_BUILD_DIR)/boardloader.bin \ + -b BOOTLOADER $(BOOTLOADER_BUILD_DIR)/bootloader.bin \ + -b FIRMWARE $(FIRMWARE_BUILD_DIR)/firmware.bin.p1 \ + -b FIRMWARE_P2 $(FIRMWARE_BUILD_DIR)/firmware.bin.p2 +else ifeq ($(MCU),$(filter $(MCU),STM32U5)) +combine_fw: ## combine boardloader + bootloader + firmware into one combined image + ./tools/combine_firmware \ + $(LAYOUT_FILE) \ + $(PRODTEST_BUILD_DIR)/combined.bin \ + -b BOARDLOADER $(BOARDLOADER_BUILD_DIR)/boardloader.bin \ + -b BOOTLOADER $(BOOTLOADER_BUILD_DIR)/bootloader.bin \ + -b FIRMWARE $(FIRMWARE_BUILD_DIR)/firmware.bin +endif upload: ## upload firmware using trezorctl trezorctl firmware_update -s -f $(FIRMWARE_BUILD_DIR)/firmware.bin diff --git a/core/embed/models/model_D001.h b/core/embed/models/model_D001.h index 6c52dbc5b..9f54bad1a 100644 --- a/core/embed/models/model_D001.h +++ b/core/embed/models/model_D001.h @@ -18,15 +18,35 @@ (const uint8_t *)"\x63\x55\x69\x1c\x17\x8a\x8f\xf9\x10\x07\xa7\x47\x8a\xfb\x95\x5e\xf7\x35\x2c\x63\xe7\xb2\x57\x03\x98\x4c\xf7\x8b\x26\xe2\x1a\x56", \ (const uint8_t *)"\xee\x93\xa4\xf6\x6f\x8d\x16\xb8\x19\xbb\x9b\xeb\x9f\xfc\xcd\xfc\xdc\x14\x12\xe8\x7f\xee\x6a\x32\x4c\x2a\x99\xa1\xe0\xe6\x71\x48", -#define BOARDLOADER_START 0x08000000 -#define BOARD_CAPABILITIES_ADDR 0x0800BF00 -#define BOOTLOADER_START 0x08020000 -#define FIRMWARE_START 0x08040000 - #define IMAGE_CHUNK_SIZE (128 * 1024) #define IMAGE_HASH_BLAKE2S -#define BOOTLOADER_IMAGE_MAXSIZE (128 * 1024 * 1) // 128 KB -#define FIRMWARE_IMAGE_MAXSIZE (128 * 1024 * 13) // 1664 KB -#define NORCOW_SECTOR_SIZE (64 * 1024) +#define BOARD_CAPABILITIES_ADDR 0x0800BF00 + +// SHARED WITH MAKEFILE +#define FLASH_START 0x08000000 +#define BOARDLOADER_START 0x08000000 +#define BOOTLOADER_START 0x08020000 +#define FIRMWARE_START 0x08040000 +#define FIRMWARE_P2_START 0x08120000 +#define STORAGE_1_OFFSET 0x10000 +#define STORAGE_2_OFFSET 0x110000 +#define NORCOW_SECTOR_SIZE (1 * 64 * 1024) // 64 kB +#define BOARDLOADER_IMAGE_MAXSIZE (3 * 16 * 1024) // 48 kB +#define BOOTLOADER_IMAGE_MAXSIZE (1 * 128 * 1024) // 128 kB +#define FIRMWARE_IMAGE_MAXSIZE (13 * 128 * 1024) // 1664 kB +#define FIRMWARE_P1_IMAGE_MAXSIZE (6 * 128 * 1024) +#define FIRMWARE_P2_IMAGE_MAXSIZE (7 * 128 * 1024) +#define BOARDLOADER_SECTOR_START 0 +#define BOARDLOADER_SECTOR_END 3 +#define BOOTLOADER_SECTOR_START 5 +#define BOOTLOADER_SECTOR_END 5 +#define FIRMWARE_SECTOR_START 6 +#define FIRMWARE_SECTOR_END 11 +#define FIRMWARE_P2_SECTOR_START 17 +#define FIRMWARE_P2_SECTOR_END 23 +#define STORAGE_1_SECTOR_START 4 +#define STORAGE_1_SECTOR_END 4 +#define STORAGE_2_SECTOR_START 16 +#define STORAGE_2_SECTOR_END 16 #endif diff --git a/core/embed/models/model_D002.h b/core/embed/models/model_D002.h index e8c9c6878..590152a62 100644 --- a/core/embed/models/model_D002.h +++ b/core/embed/models/model_D002.h @@ -1,8 +1,6 @@ #ifndef MODELS_MODEL_DISC2_H_ #define MODELS_MODEL_DISC2_H_ -#include "sizedefs.h" - #define MODEL_NAME "T" #define MODEL_FULL_NAME "Trezor Model T" #define MODEL_INTERNAL_NAME "D002" @@ -21,15 +19,30 @@ (const uint8_t *)"\x63\x55\x69\x1c\x17\x8a\x8f\xf9\x10\x07\xa7\x47\x8a\xfb\x95\x5e\xf7\x35\x2c\x63\xe7\xb2\x57\x03\x98\x4c\xf7\x8b\x26\xe2\x1a\x56", \ (const uint8_t *)"\xee\x93\xa4\xf6\x6f\x8d\x16\xb8\x19\xbb\x9b\xeb\x9f\xfc\xcd\xfc\xdc\x14\x12\xe8\x7f\xee\x6a\x32\x4c\x2a\x99\xa1\xe0\xe6\x71\x48", -#define BOARDLOADER_START 0x0C004000 -#define BOARD_CAPABILITIES_ADDR 0x0C00FF00 -#define BOOTLOADER_START 0x0C010000 -#define FIRMWARE_START 0x0C050000 - #define IMAGE_CHUNK_SIZE SIZE_256K #define IMAGE_HASH_SHA256 -#define BOOTLOADER_IMAGE_MAXSIZE SIZE_128K -#define FIRMWARE_IMAGE_MAXSIZE SIZE_3712K -#define NORCOW_SECTOR_SIZE SIZE_64K +#define BOARD_CAPABILITIES_ADDR 0x0C00FF00 + +// SHARED WITH MAKEFILE +#define FLASH_START 0x0C000000 +#define BOARDLOADER_START 0x0C004000 +#define BOOTLOADER_START 0x0C010000 +#define FIRMWARE_START 0x0C050000 +#define STORAGE_1_OFFSET 0x30000 +#define STORAGE_2_OFFSET 0x50000 +#define NORCOW_SECTOR_SIZE (8 * 8 * 1024) // 64 kB +#define BOARDLOADER_IMAGE_MAXSIZE (6 * 8 * 1024) // 48 kB +#define BOOTLOADER_IMAGE_MAXSIZE (16 * 8 * 1024) // 128 kB +#define FIRMWARE_IMAGE_MAXSIZE (464 * 8 * 1024) // 3712 kB +#define BOARDLOADER_SECTOR_START 0x2 +#define BOARDLOADER_SECTOR_END 0x7 +#define BOOTLOADER_SECTOR_START 0x8 +#define BOOTLOADER_SECTOR_END 0x17 +#define FIRMWARE_SECTOR_START 0x28 +#define FIRMWARE_SECTOR_END 0x1F7 +#define STORAGE_1_SECTOR_START 0x18 +#define STORAGE_1_SECTOR_END 0x1F +#define STORAGE_2_SECTOR_START 0x20 +#define STORAGE_2_SECTOR_END 0x27 #endif diff --git a/core/embed/models/model_D002_layout.c b/core/embed/models/model_D002_layout.c index 50f6d753d..264a625a1 100644 --- a/core/embed/models/model_D002_layout.c +++ b/core/embed/models/model_D002_layout.c @@ -6,16 +6,18 @@ const flash_area_t STORAGE_AREAS[STORAGE_AREAS_COUNT] = { .num_subareas = 1, .subarea[0] = { - .first_sector = 0x18, - .num_sectors = 8, + .first_sector = STORAGE_1_SECTOR_START, + .num_sectors = + STORAGE_1_SECTOR_END - STORAGE_1_SECTOR_START + 1, }, }, { .num_subareas = 1, .subarea[0] = { - .first_sector = 0x20, - .num_sectors = 8, + .first_sector = STORAGE_2_SECTOR_START, + .num_sectors = + STORAGE_2_SECTOR_END - STORAGE_2_SECTOR_START + 1, }, }, }; @@ -24,8 +26,9 @@ const flash_area_t BOARDLOADER_AREA = { .num_subareas = 1, .subarea[0] = { - .first_sector = 2, - .num_sectors = 6, + .first_sector = BOARDLOADER_SECTOR_START, + .num_sectors = + BOARDLOADER_SECTOR_END - BOARDLOADER_SECTOR_START + 1, }, }; @@ -33,8 +36,8 @@ const flash_area_t BOOTLOADER_AREA = { .num_subareas = 1, .subarea[0] = { - .first_sector = 0x08, - .num_sectors = 16, + .first_sector = BOOTLOADER_SECTOR_START, + .num_sectors = BOOTLOADER_SECTOR_END - BOOTLOADER_SECTOR_START + 1, }, }; @@ -42,8 +45,8 @@ const flash_area_t FIRMWARE_AREA = { .num_subareas = 1, .subarea[0] = { - .first_sector = 0x28, - .num_sectors = 464, + .first_sector = FIRMWARE_SECTOR_START.num_sectors = + FIRMWARE_SECTOR_END - FIRMWARE_SECTOR_START + 1, }, }; @@ -78,7 +81,7 @@ const flash_area_t WIPE_AREA = { .num_subareas = 1, .subarea[0] = { - .first_sector = 0x18, + .first_sector = STORAGE_1_SECTOR_START, .num_sectors = 488, }, }; @@ -87,7 +90,7 @@ const flash_area_t ALL_WIPE_AREA = { .num_subareas = 1, .subarea[0] = { - .first_sector = 0x08, + .first_sector = BOOTLOADER_SECTOR_START, .num_sectors = 504, }, }; diff --git a/core/embed/models/model_T1B1.h b/core/embed/models/model_T1B1.h index 1138d4383..c29414ea6 100644 --- a/core/embed/models/model_T1B1.h +++ b/core/embed/models/model_T1B1.h @@ -7,13 +7,24 @@ #define MODEL_INTERNAL_NAME_TOKEN T1B1 #define MODEL_INTERNAL_NAME_QSTR MP_QSTR_T1B1 -#define BOOTLOADER_START 0x08000000 -#define FIRMWARE_START 0x08010000 - #define IMAGE_CHUNK_SIZE (64 * 1024) #define IMAGE_HASH_SHA256 -#define BOOTLOADER_IMAGE_MAXSIZE (32 * 1024 * 1) // 32 KB -#define FIRMWARE_IMAGE_MAXSIZE (64 * 1024 * 15) // 960 KB -#define NORCOW_SECTOR_SIZE (16 * 1024) + +// SHARED WITH MAKEFILE +#define FLASH_START 0x08000000 +#define BOOTLOADER_START 0x08000000 +#define FIRMWARE_START 0x08010000 +#define NORCOW_SECTOR_SIZE (1 * 64 * 1024) // 64 kB +#define BOOTLOADER_IMAGE_MAXSIZE (1 * 32 * 1024) // 32 kB +#define FIRMWARE_IMAGE_MAXSIZE (15 * 64 * 1024) // 960 kB + +#define BOOTLOADER_SECTOR_START 0 +#define BOOTLOADER_SECTOR_END 2 +#define FIRMWARE_SECTOR_START 4 +#define FIRMWARE_SECTOR_END 11 +#define STORAGE_1_SECTOR_START 2 +#define STORAGE_1_SECTOR_END 2 +#define STORAGE_2_SECTOR_START 3 +#define STORAGE_2_SECTOR_END 3 #endif diff --git a/core/embed/models/model_T1B1_layout.c b/core/embed/models/model_T1B1_layout.c index 5532b2671..a5d8df0c8 100644 --- a/core/embed/models/model_T1B1_layout.c +++ b/core/embed/models/model_T1B1_layout.c @@ -6,16 +6,18 @@ const flash_area_t STORAGE_AREAS[STORAGE_AREAS_COUNT] = { .num_subareas = 1, .subarea[0] = { - .first_sector = 2, - .num_sectors = 1, + .first_sector = STORAGE_1_SECTOR_START, + .num_sectors = + STORAGE_1_SECTOR_END - STORAGE_1_SECTOR_START + 1, }, }, { .num_subareas = 1, .subarea[0] = { - .first_sector = 3, - .num_sectors = 1, + .first_sector = STORAGE_2_SECTOR_START, + .num_sectors = + STORAGE_2_SECTOR_END - STORAGE_2_SECTOR_START + 1, }, }, }; @@ -24,8 +26,8 @@ const flash_area_t BOOTLOADER_AREA = { .num_subareas = 1, .subarea[0] = { - .first_sector = 0, - .num_sectors = 2, + .first_sector = BOOTLOADER_SECTOR_START, + .num_sectors = BOOTLOADER_SECTOR_END - BOOTLOADER_SECTOR_START + 1, }, }; @@ -33,7 +35,7 @@ const flash_area_t FIRMWARE_AREA = { .num_subareas = 1, .subarea[0] = { - .first_sector = 4, - .num_sectors = 8, + .first_sector = FIRMWARE_SECTOR_START, + .num_sectors = FIRMWARE_SECTOR_END - FIRMWARE_SECTOR_START + 1, }, }; diff --git a/core/embed/models/model_T2B1.h b/core/embed/models/model_T2B1.h index cd27202be..12774b757 100644 --- a/core/embed/models/model_T2B1.h +++ b/core/embed/models/model_T2B1.h @@ -18,15 +18,35 @@ (const uint8_t *)"\xd2\xde\xf6\x91\xc1\xe9\xd8\x09\xd8\x19\x0c\xf7\xaf\x93\x5c\x10\x68\x8f\x68\x98\x34\x79\xb4\xee\x9a\xba\xc1\x91\x04\x87\x8e\xc1", \ (const uint8_t *)"\x07\xc8\x51\x34\x94\x6b\xf8\x9f\xa1\x9b\xdc\x2c\x5e\x5f\xf9\xce\x01\x29\x65\x08\xee\x08\x63\xd0\xff\x6d\x63\x33\x1d\x1a\x25\x16", -#define BOARDLOADER_START 0x08000000 -#define BOARD_CAPABILITIES_ADDR 0x0800BF00 -#define BOOTLOADER_START 0x08020000 -#define FIRMWARE_START 0x08040000 - #define IMAGE_CHUNK_SIZE (128 * 1024) #define IMAGE_HASH_BLAKE2S -#define BOOTLOADER_IMAGE_MAXSIZE (128 * 1024 * 1) // 128 KB -#define FIRMWARE_IMAGE_MAXSIZE (128 * 1024 * 13) // 1664 KB -#define NORCOW_SECTOR_SIZE (64 * 1024) +#define BOARD_CAPABILITIES_ADDR 0x0800BF00 + +// SHARED WITH MAKEFILE +#define FLASH_START 0x08000000 +#define BOARDLOADER_START 0x08000000 +#define BOOTLOADER_START 0x08020000 +#define FIRMWARE_START 0x08040000 +#define FIRMWARE_P2_START 0x08120000 +#define STORAGE_1_OFFSET 0x10000 +#define STORAGE_2_OFFSET 0x110000 +#define NORCOW_SECTOR_SIZE (1 * 64 * 1024) // 64 kB +#define BOARDLOADER_IMAGE_MAXSIZE (3 * 16 * 1024) // 48 kB +#define BOOTLOADER_IMAGE_MAXSIZE (1 * 128 * 1024) // 128 kB +#define FIRMWARE_IMAGE_MAXSIZE (13 * 128 * 1024) // 1664 kB +#define FIRMWARE_P1_IMAGE_MAXSIZE (6 * 128 * 1024) +#define FIRMWARE_P2_IMAGE_MAXSIZE (7 * 128 * 1024) +#define BOARDLOADER_SECTOR_START 0 +#define BOARDLOADER_SECTOR_END 3 +#define BOOTLOADER_SECTOR_START 5 +#define BOOTLOADER_SECTOR_END 5 +#define FIRMWARE_SECTOR_START 6 +#define FIRMWARE_SECTOR_END 11 +#define FIRMWARE_P2_SECTOR_START 17 +#define FIRMWARE_P2_SECTOR_END 23 +#define STORAGE_1_SECTOR_START 4 +#define STORAGE_1_SECTOR_END 4 +#define STORAGE_2_SECTOR_START 16 +#define STORAGE_2_SECTOR_END 16 #endif diff --git a/core/embed/models/model_T2B1_layout.c b/core/embed/models/model_T2B1_layout.c index adaf2f3d7..430f4d494 100644 --- a/core/embed/models/model_T2B1_layout.c +++ b/core/embed/models/model_T2B1_layout.c @@ -6,16 +6,18 @@ const flash_area_t STORAGE_AREAS[STORAGE_AREAS_COUNT] = { .num_subareas = 1, .subarea[0] = { - .first_sector = 4, - .num_sectors = 1, + .first_sector = STORAGE_1_SECTOR_START, + .num_sectors = + STORAGE_1_SECTOR_END - STORAGE_1_SECTOR_START + 1, }, }, { .num_subareas = 1, .subarea[0] = { - .first_sector = 16, - .num_sectors = 1, + .first_sector = STORAGE_2_SECTOR_START, + .num_sectors = + STORAGE_2_SECTOR_END - STORAGE_2_SECTOR_START + 1, }, }, }; @@ -24,8 +26,9 @@ const flash_area_t BOARDLOADER_AREA = { .num_subareas = 1, .subarea[0] = { - .first_sector = 0, - .num_sectors = 3, + .first_sector = BOARDLOADER_SECTOR_START, + .num_sectors = + BOARDLOADER_SECTOR_END - BOARDLOADER_SECTOR_START + 1, }, }; @@ -51,8 +54,8 @@ const flash_area_t BOOTLOADER_AREA = { .num_subareas = 1, .subarea[0] = { - .first_sector = 5, - .num_sectors = 1, + .first_sector = BOOTLOADER_SECTOR_START, + .num_sectors = BOOTLOADER_SECTOR_END - BOOTLOADER_SECTOR_START + 1, }, }; @@ -60,13 +63,14 @@ const flash_area_t FIRMWARE_AREA = { .num_subareas = 2, .subarea[0] = { - .first_sector = 6, - .num_sectors = 6, + .first_sector = FIRMWARE_SECTOR_START, + .num_sectors = FIRMWARE_SECTOR_END - FIRMWARE_SECTOR_START + 1, }, .subarea[1] = { - .first_sector = 17, - .num_sectors = 7, + .first_sector = FIRMWARE_P2_SECTOR_START, + .num_sectors = + FIRMWARE_P2_SECTOR_END - FIRMWARE_P2_SECTOR_START + 1, }, }; diff --git a/core/embed/models/model_T2T1.h b/core/embed/models/model_T2T1.h index 4c7452148..1af939a55 100644 --- a/core/embed/models/model_T2T1.h +++ b/core/embed/models/model_T2T1.h @@ -18,15 +18,35 @@ (const uint8_t *)"\x80\xd0\x36\xb0\x87\x39\xb8\x46\xf4\xcb\x77\x59\x30\x78\xde\xb2\x5d\xc9\x48\x7a\xed\xcf\x52\xe3\x0b\x4f\xb7\xcd\x70\x24\x17\x8a", \ (const uint8_t *)"\xb8\x30\x7a\x71\xf5\x52\xc6\x0a\x4c\xbb\x31\x7f\xf4\x8b\x82\xcd\xbf\x6b\x6b\xb5\xf0\x4c\x92\x0f\xec\x7b\xad\xf0\x17\x88\x37\x51", -#define BOARDLOADER_START 0x08000000 -#define BOARD_CAPABILITIES_ADDR 0x0800BF00 -#define BOOTLOADER_START 0x08020000 -#define FIRMWARE_START 0x08040000 - #define IMAGE_CHUNK_SIZE (128 * 1024) #define IMAGE_HASH_BLAKE2S -#define BOOTLOADER_IMAGE_MAXSIZE (128 * 1024 * 1) // 128 KB -#define FIRMWARE_IMAGE_MAXSIZE (128 * 1024 * 13) // 1664 KB -#define NORCOW_SECTOR_SIZE (64 * 1024) +#define BOARD_CAPABILITIES_ADDR 0x0800BF00 + +// SHARED WITH MAKEFILE +#define FLASH_START 0x08000000 +#define BOARDLOADER_START 0x08000000 +#define BOOTLOADER_START 0x08020000 +#define FIRMWARE_START 0x08040000 +#define FIRMWARE_P2_START 0x08120000 +#define STORAGE_1_OFFSET 0x10000 +#define STORAGE_2_OFFSET 0x110000 +#define NORCOW_SECTOR_SIZE (1 * 64 * 1024) // 64 kB +#define BOARDLOADER_IMAGE_MAXSIZE (3 * 16 * 1024) // 48 kB +#define BOOTLOADER_IMAGE_MAXSIZE (1 * 128 * 1024) // 128 kB +#define FIRMWARE_IMAGE_MAXSIZE (13 * 128 * 1024) // 1664 kB +#define FIRMWARE_P1_IMAGE_MAXSIZE (6 * 128 * 1024) +#define FIRMWARE_P2_IMAGE_MAXSIZE (7 * 128 * 1024) +#define BOARDLOADER_SECTOR_START 0 +#define BOARDLOADER_SECTOR_END 3 +#define BOOTLOADER_SECTOR_START 5 +#define BOOTLOADER_SECTOR_END 5 +#define FIRMWARE_SECTOR_START 6 +#define FIRMWARE_SECTOR_END 11 +#define FIRMWARE_P2_SECTOR_START 17 +#define FIRMWARE_P2_SECTOR_END 23 +#define STORAGE_1_SECTOR_START 4 +#define STORAGE_1_SECTOR_END 4 +#define STORAGE_2_SECTOR_START 16 +#define STORAGE_2_SECTOR_END 16 #endif diff --git a/core/embed/models/model_T2T1_layout.c b/core/embed/models/model_T2T1_layout.c index 7d26440cc..ad7265d84 100644 --- a/core/embed/models/model_T2T1_layout.c +++ b/core/embed/models/model_T2T1_layout.c @@ -6,16 +6,18 @@ const flash_area_t STORAGE_AREAS[STORAGE_AREAS_COUNT] = { .num_subareas = 1, .subarea[0] = { - .first_sector = 4, - .num_sectors = 1, + .first_sector = STORAGE_1_SECTOR_START, + .num_sectors = + STORAGE_1_SECTOR_END - STORAGE_1_SECTOR_START + 1, }, }, { .num_subareas = 1, .subarea[0] = { - .first_sector = 16, - .num_sectors = 1, + .first_sector = STORAGE_2_SECTOR_START, + .num_sectors = + STORAGE_2_SECTOR_END - STORAGE_2_SECTOR_START + 1, }, }, }; @@ -24,8 +26,9 @@ const flash_area_t BOARDLOADER_AREA = { .num_subareas = 1, .subarea[0] = { - .first_sector = 0, - .num_sectors = 3, + .first_sector = BOARDLOADER_SECTOR_START, + .num_sectors = + BOARDLOADER_SECTOR_END - BOARDLOADER_SECTOR_START + 1, }, }; @@ -42,8 +45,8 @@ const flash_area_t BOOTLOADER_AREA = { .num_subareas = 1, .subarea[0] = { - .first_sector = 5, - .num_sectors = 1, + .first_sector = BOOTLOADER_SECTOR_START, + .num_sectors = BOOTLOADER_SECTOR_END - BOOTLOADER_SECTOR_START + 1, }, }; @@ -51,13 +54,14 @@ const flash_area_t FIRMWARE_AREA = { .num_subareas = 2, .subarea[0] = { - .first_sector = 6, - .num_sectors = 6, + .first_sector = FIRMWARE_SECTOR_START, + .num_sectors = FIRMWARE_SECTOR_END - FIRMWARE_SECTOR_START + 1, }, .subarea[1] = { - .first_sector = 17, - .num_sectors = 7, + .first_sector = FIRMWARE_P2_SECTOR_START, + .num_sectors = + FIRMWARE_P2_SECTOR_END - FIRMWARE_P2_SECTOR_START + 1, }, }; diff --git a/core/embed/models/model_T3T1.h b/core/embed/models/model_T3T1.h index e4f7241a5..bd7755efe 100644 --- a/core/embed/models/model_T3T1.h +++ b/core/embed/models/model_T3T1.h @@ -20,15 +20,30 @@ (const uint8_t *)"\x63\x55\x69\x1c\x17\x8a\x8f\xf9\x10\x07\xa7\x47\x8a\xfb\x95\x5e\xf7\x35\x2c\x63\xe7\xb2\x57\x03\x98\x4c\xf7\x8b\x26\xe2\x1a\x56", \ (const uint8_t *)"\xee\x93\xa4\xf6\x6f\x8d\x16\xb8\x19\xbb\x9b\xeb\x9f\xfc\xcd\xfc\xdc\x14\x12\xe8\x7f\xee\x6a\x32\x4c\x2a\x99\xa1\xe0\xe6\x71\x48", -#define BOARDLOADER_START 0x0C004000 -#define BOARD_CAPABILITIES_ADDR 0x0C00FF00 -#define BOOTLOADER_START 0x0C010000 -#define FIRMWARE_START 0x0C050000 - #define IMAGE_CHUNK_SIZE (128 * 1024) #define IMAGE_HASH_SHA256 -#define BOOTLOADER_IMAGE_MAXSIZE (128 * 1024 * 1) // 128 KB -#define FIRMWARE_IMAGE_MAXSIZE (128 * 1024 * 13) // 1664 KB -#define NORCOW_SECTOR_SIZE (64 * 1024) +#define BOARD_CAPABILITIES_ADDR 0x0C00FF00 + +// SHARED WITH MAKEFILE +#define FLASH_START 0x0C000000 +#define BOARDLOADER_START 0x0C004000 +#define BOOTLOADER_START 0x0C010000 +#define FIRMWARE_START 0x0C050000 +#define STORAGE_1_OFFSET 0x30000 +#define STORAGE_2_OFFSET 0x50000 +#define NORCOW_SECTOR_SIZE (8 * 8 * 1024) // 64 kB +#define BOARDLOADER_IMAGE_MAXSIZE (6 * 8 * 1024) // 48 kB +#define BOOTLOADER_IMAGE_MAXSIZE (16 * 8 * 1024) // 128 kB +#define FIRMWARE_IMAGE_MAXSIZE (208 * 8 * 1024) // 1664 kB +#define BOARDLOADER_SECTOR_START 0x2 +#define BOARDLOADER_SECTOR_END 0x7 +#define BOOTLOADER_SECTOR_START 0x8 +#define BOOTLOADER_SECTOR_END 0x17 +#define FIRMWARE_SECTOR_START 0x28 +#define FIRMWARE_SECTOR_END 0xF7 +#define STORAGE_1_SECTOR_START 0x18 +#define STORAGE_1_SECTOR_END 0x1F +#define STORAGE_2_SECTOR_START 0x20 +#define STORAGE_2_SECTOR_END 0x27 #endif diff --git a/core/embed/models/model_T3T1_layout.c b/core/embed/models/model_T3T1_layout.c index 9e3cd82d3..3baae222e 100644 --- a/core/embed/models/model_T3T1_layout.c +++ b/core/embed/models/model_T3T1_layout.c @@ -6,16 +6,18 @@ const flash_area_t STORAGE_AREAS[STORAGE_AREAS_COUNT] = { .num_subareas = 1, .subarea[0] = { - .first_sector = 0x18, - .num_sectors = 8, + .first_sector = STORAGE_1_SECTOR_START, + .num_sectors = + STORAGE_1_SECTOR_END - STORAGE_1_SECTOR_START + 1, }, }, { .num_subareas = 1, .subarea[0] = { - .first_sector = 0x20, - .num_sectors = 8, + .first_sector = STORAGE_2_SECTOR_START, + .num_sectors = + STORAGE_2_SECTOR_END - STORAGE_2_SECTOR_START + 1, }, }, }; @@ -24,8 +26,9 @@ const flash_area_t BOARDLOADER_AREA = { .num_subareas = 1, .subarea[0] = { - .first_sector = 2, - .num_sectors = 6, + .first_sector = BOARDLOADER_SECTOR_START, + .num_sectors = + BOARDLOADER_SECTOR_END - BOARDLOADER_SECTOR_START + 1, }, }; @@ -33,8 +36,8 @@ const flash_area_t BOOTLOADER_AREA = { .num_subareas = 1, .subarea[0] = { - .first_sector = 0x08, - .num_sectors = 16, + .first_sector = BOOTLOADER_SECTOR_START, + .num_sectors = BOOTLOADER_SECTOR_END - BOOTLOADER_SECTOR_START + 1, }, }; @@ -42,8 +45,8 @@ const flash_area_t FIRMWARE_AREA = { .num_subareas = 1, .subarea[0] = { - .first_sector = 0x28, - .num_sectors = 208, + .first_sector = FIRMWARE_SECTOR_START, + .num_sectors = FIRMWARE_SECTOR_END - FIRMWARE_SECTOR_START + 1, }, }; @@ -78,7 +81,7 @@ const flash_area_t WIPE_AREA = { .num_subareas = 1, .subarea[0] = { - .first_sector = 0x18, + .first_sector = STORAGE_1_SECTOR_START, .num_sectors = 232, }, }; @@ -87,7 +90,7 @@ const flash_area_t ALL_WIPE_AREA = { .num_subareas = 1, .subarea[0] = { - .first_sector = 0x08, + .first_sector = BOOTLOADER_SECTOR_START, .num_sectors = 248, }, }; diff --git a/core/tools/combine_firmware b/core/tools/combine_firmware index a1ae841c8..d52d4743e 100755 --- a/core/tools/combine_firmware +++ b/core/tools/combine_firmware @@ -3,99 +3,65 @@ from __future__ import annotations import datetime import io -import sys from pathlib import Path +import os +import subprocess import click +def get_layout_params(layout: Path, name: str) -> int: + directory = os.path.dirname(os.path.realpath(__file__)) + with subprocess.Popen(args=["python", Path(directory, "layout_parser.py"), str(layout), name], + stdout=subprocess.PIPE) as script: + return int(script.stdout.read().decode().strip()) @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), + "layout", + type=click.Path(dir_okay=False, writable=True, path_type=Path), + required=True, ) @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=(str, click.Path(exists=True, dir_okay=False, readable=True, path_type=Path)), + multiple=True, ) def main( - boardloader: Path, - bootloader: Path, - firmware: Path, + layout: 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) if outfile is None: today = datetime.date.today().strftime(r"%Y-%m-%d") outfile = Path(f"combined-{today}.bin") - offset = boardloader_start + first_bin = bin[0] + (name, bin_path) = first_bin + + start_offset = get_layout_params(layout, name+ "_START") + + offset = start_offset out_bytes = io.BytesIO() - # write boardloader - offset += out_bytes.write(boardloader.read_bytes()) - if offset > boardloader_end: - raise Exception("Boardloader too big") + for (name, bin_path) in bin: + bin_start = get_layout_params(layout, name + "_START") + # zero-pad until next section: + offset += out_bytes.write(b"\x00" * (bin_start - offset)) + assert offset == bin_start - # 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 - - # write firmware - offset += out_bytes.write(firmware.read_bytes()) + # write binary + offset += out_bytes.write(bin_path.read_bytes()) # write out contents - click.echo(f"Writing {outfile} ({offset - boardloader_start} bytes)") + click.echo(f"Writing {outfile} ({offset - start_offset} bytes)") outfile.write_bytes(out_bytes.getvalue()) diff --git a/core/tools/layout_parser.py b/core/tools/layout_parser.py new file mode 100644 index 000000000..0b5adfd1f --- /dev/null +++ b/core/tools/layout_parser.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +from __future__ import annotations + + +from pathlib import Path + +import click +import ast + + +@click.command() +@click.argument( + "layout", + type=click.Path(exists=True, dir_okay=False, readable=True, path_type=Path), +) +@click.argument( + "name", + type=click.STRING, +) +def main( + layout: Path, + name: str, +) -> None: + + search = f'#define {name} ' + for line in layout.read_text().splitlines(): + + if line.startswith(search): + line = line.split(search)[1] + if line.startswith("("): + line = line.split("(")[1].split(")")[-2] + if "//" in line: + line = line.split("//")[0] + line = line.strip() + node = ast.parse(line, mode='eval') + result = eval(compile(node, '', 'eval')) + print(result) + + +if __name__ == "__main__": + main()