From 937e2b898d20e8fb95184e863256d89ab1b92c66 Mon Sep 17 00:00:00 2001 From: Niklas Dusenlund Date: Fri, 12 Jun 2026 14:54:58 +0200 Subject: [PATCH 1/9] da14531: Only use embedded-swd when needed --- src/da14531/da14531_protocol.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/da14531/da14531_protocol.c b/src/da14531/da14531_protocol.c index e6d991f3fe..101333e125 100644 --- a/src/da14531/da14531_protocol.c +++ b/src/da14531/da14531_protocol.c @@ -8,7 +8,7 @@ #include #include #include -#ifndef TESTING +#if (FACTORYSETUP == 1 || !defined(NDEBUG)) && !defined(TESTING) #include "dap.h" #endif From b7446888cdb85ee87d9fd199e61133734020b711 Mon Sep 17 00:00:00 2001 From: Niklas Dusenlund Date: Fri, 12 Jun 2026 15:02:36 +0200 Subject: [PATCH 2/9] cmake: Add BOOTLOADER_LOCKED --- CMakeLists.txt | 2 ++ Makefile | 35 ++++++++++++++++++++++------------- src/CMakeLists.txt | 26 +++----------------------- 3 files changed, 27 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 434a34d3d8..af98723732 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,6 +63,7 @@ option(COVERAGE "Compile with test coverage flags." OFF) option(SANITIZE_ADDRESS "Compile with asan." OFF) option(SANITIZE_UNDEFINED "Compile with ubsan." OFF) option(CMAKE_VERBOSE_MAKEFILE "Verbose build." OFF) +option(BOOTLOADER_LOCKED "Build bootloaders that lock the bootloader area." OFF) # Generate compile_command.json (for tidy and other tools) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) @@ -215,6 +216,7 @@ message(STATUS "Default EXE LDFLAGS: ${CMAKE_EXE_LINKER_FLAGS}") message(STATUS " - Options -") message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") message(STATUS "Hardware version: ${HARDWARE}") +message(STATUS "Bootloader locked: ${BOOTLOADER_LOCKED}") message(STATUS "Verbose: ${CMAKE_VERBOSE_MAKEFILE}") message(STATUS "Coverage flags: ${COVERAGE}") message(STATUS "\n=============================================\n\n") diff --git a/Makefile b/Makefile index 252e778c10..3f11a8fba9 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,12 @@ build-debug/Makefile: cd build-debug && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DCMAKE_BUILD_TYPE=DEBUG .. $(MAKE) -C py/bitbox02 +build-bootloader-locked/Makefile: + ./scripts/bootstrap-cargo-config + mkdir -p build-bootloader-locked + cd build-bootloader-locked && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBOOTLOADER_LOCKED=ON .. + $(MAKE) -C py/bitbox02 + build-build/Makefile: ./scripts/bootstrap-cargo-config mkdir -p build-build @@ -42,6 +48,9 @@ build: build/Makefile # Directory for building debug build for "host" machine according to gcc convention build-debug: build-debug/Makefile +# Directory for building locked bootloaders +build-bootloader-locked: build-bootloader-locked/Makefile + # Directory for building for "build" machine according to gcc convention build-build: build-build/Makefile @@ -61,10 +70,10 @@ bootloader: | build $(MAKE) -C build bb02-bl-multi.elf bootloader-development: | build $(MAKE) -C build bb02-bl-multi-development.elf -bootloader-development-locked: | build - $(MAKE) -C build bb02-bl-multi-development-locked.elf -bootloader-production: | build - $(MAKE) -C build bb02-bl-multi-production.elf +bootloader-development-locked: | build-bootloader-locked + $(MAKE) -C build-bootloader-locked bb02-bl-multi-development.elf +bootloader-production: | build-bootloader-locked + $(MAKE) -C build-bootloader-locked bb02-bl-multi.elf bootloader-debug: | build-debug $(MAKE) -C build-debug bb02-bl-multi-development.elf @@ -72,15 +81,15 @@ bootloader-btc: | build $(MAKE) -C build bb02-bl-btconly.elf bootloader-btc-development: | build $(MAKE) -C build bb02-bl-btconly-development.elf -bootloader-btc-production: | build - $(MAKE) -C build bb02-bl-btconly-production.elf +bootloader-btc-production: | build-bootloader-locked + $(MAKE) -C build-bootloader-locked bb02-bl-btconly.elf bootloader-plus: | build $(MAKE) -C build bb02p-bl-multi.elf bootloader-plus-development: | build $(MAKE) -C build bb02p-bl-multi-development.elf -bootloader-plus-production: | build - $(MAKE) -C build bb02p-bl-multi-production.elf +bootloader-plus-production: | build-bootloader-locked + $(MAKE) -C build-bootloader-locked bb02p-bl-multi.elf bootloader-plus-debug: | build-debug $(MAKE) -C build-debug bb02p-bl-multi-development.elf @@ -88,8 +97,8 @@ bootloader-plus-btc: | build $(MAKE) -C build bb02p-bl-btconly.elf bootloader-plus-btc-development: | build $(MAKE) -C build bb02p-bl-btconly-development.elf -bootloader-plus-btc-production: | build - $(MAKE) -C build bb02p-bl-btconly-production.elf +bootloader-plus-btc-production: | build-bootloader-locked + $(MAKE) -C build-bootloader-locked bb02p-bl-btconly.elf factory-setup: | build $(MAKE) -C build factory-setup.elf @@ -135,8 +144,8 @@ jlink-flash-bootloader-plus-development: | build JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bb02p-bl-multi-development.jlink jlink-flash-bootloader-btc-plus-development: | build JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bb02p-bl-btconly-development.jlink -jlink-flash-bootloader-development-locked: | build - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bb02-bl-multi-development-locked.jlink +jlink-flash-bootloader-development-locked: | build-bootloader-locked + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-locked/scripts/bb02-bl-multi-development.jlink jlink-flash-bootloader: | build JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bb02-bl-multi.jlink jlink-flash-bootloader-btc-development: | build @@ -193,7 +202,7 @@ prepare-tidy: | build build-build $(MAKE) -C build rust-cbindgen $(MAKE) -C build-build rust-cbindgen clean: - rm -rf build build-build build-debug build-build-noasan src/rust/target + rm -rf build build-bootloader-locked build-build build-debug build-build-noasan src/rust/target # When you vendor rust libs avoid duplicates vendor-rust-deps: diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8626a98121..2042633967 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -290,20 +290,14 @@ endif() set(BOOTLOADERS-BITBOX02 bb02-bl-multi # Runs signed firmware bb02-bl-multi-development # Runs signed/unsigned firmware and has bootloader menu - bb02-bl-multi-development-locked # Runs signed/unsigned firmware, has bootloader menu and bootloader is locked - bb02-bl-multi-production # Runs signed firmware and bootloader is locked bb02-bl-btconly # Runs signed (btc-only keys) firmware bb02-bl-btconly-development # Runs signed/unsigned firmware and has bootloader menu - bb02-bl-btconly-production # Runs signed (btc-only keys) firmware and bootloader is locked ) set(BOOTLOADERS-BITBOX02PLUS bb02p-bl-multi # Runs signed firmware bb02p-bl-multi-development # Runs signed/unsigned firmware and has bootloader menu - bb02p-bl-multi-development-locked # Runs signed/unsigned firmware, has bootloader menu and bootloader is locked - bb02p-bl-multi-production # Runs signed firmware and bootloader is locked bb02p-bl-btconly # Runs signed (btc-only keys) firmware bb02p-bl-btconly-development # Runs signed/unsigned firmware and has bootloader menu - bb02p-bl-btconly-production # Runs signed (btc-only keys) firmware and bootloader is locked ) set(BOOTLOADERS ${BOOTLOADERS-BITBOX02} @@ -314,7 +308,6 @@ set(BOOTLOADERS ${BOOTLOADERS} PARENT_SCOPE) # Used to add QTouch to development bootloaders of bb02 bootloaders set(DEVDEVICE-BOOTLOADERS bb02-bl-multi-development - bb02-bl-multi-development-locked bb02-bl-btconly-development ) @@ -423,6 +416,9 @@ if(CMAKE_CROSSCOMPILING) target_link_libraries(${elf} PRIVATE c asf4-drivers-min samd51a-ds -Wl,-u,exception_table) target_include_directories(${elf} PRIVATE ${INCLUDES}) target_compile_definitions(${elf} PRIVATE BOOTLOADER "APP_U2F=0") + if(BOOTLOADER_LOCKED) + target_compile_definitions(${elf} PRIVATE BOOTLOADER_PRODUCTION) + endif() # needed to find version.h target_include_directories(${elf} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) # needed to find bootloader_version.h @@ -456,33 +452,17 @@ if(CMAKE_CROSSCOMPILING) target_compile_definitions(bb02-bl-multi.elf PRIVATE PRODUCT_BITBOX_MULTI) target_compile_definitions(bb02-bl-multi-development.elf PRIVATE PRODUCT_BITBOX_MULTI BOOTLOADER_DEVDEVICE) - target_compile_definitions(bb02-bl-multi-development-locked.elf PRIVATE PRODUCT_BITBOX_MULTI BOOTLOADER_DEVDEVICE BOOTLOADER_PRODUCTION) - set_property(TARGET bb02-bl-multi-development-locked.elf PROPERTY EXCLUDE_FROM_ALL ON) - - - target_compile_definitions(bb02-bl-multi-production.elf PRIVATE PRODUCT_BITBOX_MULTI BOOTLOADER_PRODUCTION) - set_property(TARGET bb02-bl-multi-production.elf PROPERTY EXCLUDE_FROM_ALL ON) target_compile_definitions(bb02-bl-btconly.elf PRIVATE PRODUCT_BITBOX_BTCONLY) target_compile_definitions(bb02-bl-btconly-development.elf PRIVATE PRODUCT_BITBOX_BTCONLY BOOTLOADER_DEVDEVICE) - target_compile_definitions(bb02-bl-btconly-production.elf PRIVATE PRODUCT_BITBOX_BTCONLY BOOTLOADER_PRODUCTION) - set_property(TARGET bb02-bl-btconly-production.elf PROPERTY EXCLUDE_FROM_ALL ON) # BB02PLUS definitions target_compile_definitions(bb02p-bl-multi.elf PRIVATE PRODUCT_BITBOX_PLUS_MULTI) target_compile_definitions(bb02p-bl-multi-development.elf PRIVATE PRODUCT_BITBOX_PLUS_MULTI BOOTLOADER_DEVDEVICE) - target_compile_definitions(bb02p-bl-multi-development-locked.elf PRIVATE PRODUCT_BITBOX_PLUS_MULTI BOOTLOADER_DEVDEVICE BOOTLOADER_PRODUCTION) - set_property(TARGET bb02p-bl-multi-development-locked.elf PROPERTY EXCLUDE_FROM_ALL ON) - - - target_compile_definitions(bb02p-bl-multi-production.elf PRIVATE PRODUCT_BITBOX_PLUS_MULTI BOOTLOADER_PRODUCTION) - set_property(TARGET bb02p-bl-multi-production.elf PROPERTY EXCLUDE_FROM_ALL ON) target_compile_definitions(bb02p-bl-btconly.elf PRIVATE PRODUCT_BITBOX_PLUS_BTCONLY) target_compile_definitions(bb02p-bl-btconly-development.elf PRIVATE PRODUCT_BITBOX_PLUS_BTCONLY BOOTLOADER_DEVDEVICE) - target_compile_definitions(bb02p-bl-btconly-production.elf PRIVATE PRODUCT_BITBOX_PLUS_BTCONLY BOOTLOADER_PRODUCTION) - set_property(TARGET bb02p-bl-btconly-production.elf PROPERTY EXCLUDE_FROM_ALL ON) foreach(firmware ${FIRMWARES}) set(elf ${firmware}.elf) From df07025335271187fc260373f1d832fa950ee2bf Mon Sep 17 00:00:00 2001 From: Niklas Dusenlund Date: Fri, 12 Jun 2026 15:08:07 +0200 Subject: [PATCH 3/9] cmake: Add BOOTLOADER_DEVDEVICE --- CMakeLists.txt | 2 ++ Makefile | 79 +++++++++++++++++++++++++++++++--------------- src/CMakeLists.txt | 32 ++++++++----------- 3 files changed, 68 insertions(+), 45 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index af98723732..2916c0d19a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,6 +64,7 @@ option(SANITIZE_ADDRESS "Compile with asan." OFF) option(SANITIZE_UNDEFINED "Compile with ubsan." OFF) option(CMAKE_VERBOSE_MAKEFILE "Verbose build." OFF) option(BOOTLOADER_LOCKED "Build bootloaders that lock the bootloader area." OFF) +option(BOOTLOADER_DEVDEVICE "Build bootloaders with the development device menu." OFF) # Generate compile_command.json (for tidy and other tools) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) @@ -217,6 +218,7 @@ message(STATUS " - Options -") message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") message(STATUS "Hardware version: ${HARDWARE}") message(STATUS "Bootloader locked: ${BOOTLOADER_LOCKED}") +message(STATUS "Bootloader devdevice: ${BOOTLOADER_DEVDEVICE}") message(STATUS "Verbose: ${CMAKE_VERBOSE_MAKEFILE}") message(STATUS "Coverage flags: ${COVERAGE}") message(STATUS "\n=============================================\n\n") diff --git a/Makefile b/Makefile index 3f11a8fba9..8ab64b52f9 100644 --- a/Makefile +++ b/Makefile @@ -23,12 +23,30 @@ build-debug/Makefile: cd build-debug && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DCMAKE_BUILD_TYPE=DEBUG .. $(MAKE) -C py/bitbox02 +build-bootloader-development/Makefile: + ./scripts/bootstrap-cargo-config + mkdir -p build-bootloader-development + cd build-bootloader-development && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBOOTLOADER_DEVDEVICE=ON .. + $(MAKE) -C py/bitbox02 + build-bootloader-locked/Makefile: ./scripts/bootstrap-cargo-config mkdir -p build-bootloader-locked cd build-bootloader-locked && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBOOTLOADER_LOCKED=ON .. $(MAKE) -C py/bitbox02 +build-bootloader-development-locked/Makefile: + ./scripts/bootstrap-cargo-config + mkdir -p build-bootloader-development-locked + cd build-bootloader-development-locked && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBOOTLOADER_DEVDEVICE=ON -DBOOTLOADER_LOCKED=ON .. + $(MAKE) -C py/bitbox02 + +build-bootloader-development-debug/Makefile: + ./scripts/bootstrap-cargo-config + mkdir -p build-bootloader-development-debug + cd build-bootloader-development-debug && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DCMAKE_BUILD_TYPE=DEBUG -DBOOTLOADER_DEVDEVICE=ON .. + $(MAKE) -C py/bitbox02 + build-build/Makefile: ./scripts/bootstrap-cargo-config mkdir -p build-build @@ -48,9 +66,18 @@ build: build/Makefile # Directory for building debug build for "host" machine according to gcc convention build-debug: build-debug/Makefile +# Directory for building development bootloaders +build-bootloader-development: build-bootloader-development/Makefile + # Directory for building locked bootloaders build-bootloader-locked: build-bootloader-locked/Makefile +# Directory for building development locked bootloaders +build-bootloader-development-locked: build-bootloader-development-locked/Makefile + +# Directory for building development debug bootloaders +build-bootloader-development-debug: build-bootloader-development-debug/Makefile + # Directory for building for "build" machine according to gcc convention build-build: build-build/Makefile @@ -68,35 +95,35 @@ firmware-debug: | build-debug bootloader: | build $(MAKE) -C build bb02-bl-multi.elf -bootloader-development: | build - $(MAKE) -C build bb02-bl-multi-development.elf -bootloader-development-locked: | build-bootloader-locked - $(MAKE) -C build-bootloader-locked bb02-bl-multi-development.elf +bootloader-development: | build-bootloader-development + $(MAKE) -C build-bootloader-development bb02-bl-multi.elf +bootloader-development-locked: | build-bootloader-development-locked + $(MAKE) -C build-bootloader-development-locked bb02-bl-multi.elf bootloader-production: | build-bootloader-locked $(MAKE) -C build-bootloader-locked bb02-bl-multi.elf -bootloader-debug: | build-debug - $(MAKE) -C build-debug bb02-bl-multi-development.elf +bootloader-debug: | build-bootloader-development-debug + $(MAKE) -C build-bootloader-development-debug bb02-bl-multi.elf bootloader-btc: | build $(MAKE) -C build bb02-bl-btconly.elf -bootloader-btc-development: | build - $(MAKE) -C build bb02-bl-btconly-development.elf +bootloader-btc-development: | build-bootloader-development + $(MAKE) -C build-bootloader-development bb02-bl-btconly.elf bootloader-btc-production: | build-bootloader-locked $(MAKE) -C build-bootloader-locked bb02-bl-btconly.elf bootloader-plus: | build $(MAKE) -C build bb02p-bl-multi.elf -bootloader-plus-development: | build - $(MAKE) -C build bb02p-bl-multi-development.elf +bootloader-plus-development: | build-bootloader-development + $(MAKE) -C build-bootloader-development bb02p-bl-multi.elf bootloader-plus-production: | build-bootloader-locked $(MAKE) -C build-bootloader-locked bb02p-bl-multi.elf -bootloader-plus-debug: | build-debug - $(MAKE) -C build-debug bb02p-bl-multi-development.elf +bootloader-plus-debug: | build-bootloader-development-debug + $(MAKE) -C build-bootloader-development-debug bb02p-bl-multi.elf bootloader-plus-btc: | build $(MAKE) -C build bb02p-bl-btconly.elf -bootloader-plus-btc-development: | build - $(MAKE) -C build bb02p-bl-btconly-development.elf +bootloader-plus-btc-development: | build-bootloader-development + $(MAKE) -C build-bootloader-development bb02p-bl-btconly.elf bootloader-plus-btc-production: | build-bootloader-locked $(MAKE) -C build-bootloader-locked bb02p-bl-btconly.elf @@ -138,18 +165,18 @@ run-valgrind-on-unit-tests: bash -ec 'for exe in build-build/bin/test_*; do valgrind --leak-check=yes --track-origins=yes --error-exitcode=1 --exit-on-first-error=yes $$exe; done' flash-dev-firmware: ./py/load_firmware.py build/bin/firmware.bin --debug -jlink-flash-bootloader-development: | build - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bb02-bl-multi-development.jlink -jlink-flash-bootloader-plus-development: | build - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bb02p-bl-multi-development.jlink -jlink-flash-bootloader-btc-plus-development: | build - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bb02p-bl-btconly-development.jlink -jlink-flash-bootloader-development-locked: | build-bootloader-locked - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-locked/scripts/bb02-bl-multi-development.jlink +jlink-flash-bootloader-development: | build-bootloader-development + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-development/scripts/bb02-bl-multi.jlink +jlink-flash-bootloader-plus-development: | build-bootloader-development + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-development/scripts/bb02p-bl-multi.jlink +jlink-flash-bootloader-btc-plus-development: | build-bootloader-development + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-development/scripts/bb02p-bl-btconly.jlink +jlink-flash-bootloader-development-locked: | build-bootloader-development-locked + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-development-locked/scripts/bb02-bl-multi.jlink jlink-flash-bootloader: | build JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bb02-bl-multi.jlink -jlink-flash-bootloader-btc-development: | build - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bb02-bl-btconly-development.jlink +jlink-flash-bootloader-btc-development: | build-bootloader-development + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-development/scripts/bb02-bl-btconly.jlink jlink-flash-bootloader-btc: | build JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bb02-bl-btc.jlink jlink-flash-firmware: | build @@ -181,7 +208,7 @@ rtt-client: run-debug: arm-none-eabi-gdb -x scripts/jlink.gdb build-debug/bin/firmware.elf run-bootloader: - arm-none-eabi-gdb -x scripts/jlink-bootloader.gdb build/bin/bb02p-bl-multi-development.elf + arm-none-eabi-gdb -x scripts/jlink-bootloader.gdb build-bootloader-development/bin/bb02p-bl-multi.elf run-factory-setup-debug: arm-none-eabi-gdb -x scripts/jlink.gdb build-debug/bin/factory-setup.elf dockerinit: @@ -202,7 +229,7 @@ prepare-tidy: | build build-build $(MAKE) -C build rust-cbindgen $(MAKE) -C build-build rust-cbindgen clean: - rm -rf build build-bootloader-locked build-build build-debug build-build-noasan src/rust/target + rm -rf build build-bootloader-development build-bootloader-locked build-bootloader-development-locked build-bootloader-development-debug build-build build-debug build-build-noasan src/rust/target # When you vendor rust libs avoid duplicates vendor-rust-deps: diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2042633967..6d9d2ca183 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -286,18 +286,15 @@ if(NOT CMAKE_CROSSCOMPILING) add_dependencies(rust-clippy rust-cbindgen) endif() +# BOOTLOADER_LOCKED and BOOTLOADER_DEVDEVICE are configured in separate build directories. # If a bootloader that locks the bootloader is flashed the bootloader area is permanently read-only. set(BOOTLOADERS-BITBOX02 - bb02-bl-multi # Runs signed firmware - bb02-bl-multi-development # Runs signed/unsigned firmware and has bootloader menu - bb02-bl-btconly # Runs signed (btc-only keys) firmware - bb02-bl-btconly-development # Runs signed/unsigned firmware and has bootloader menu + bb02-bl-multi + bb02-bl-btconly ) set(BOOTLOADERS-BITBOX02PLUS - bb02p-bl-multi # Runs signed firmware - bb02p-bl-multi-development # Runs signed/unsigned firmware and has bootloader menu - bb02p-bl-btconly # Runs signed (btc-only keys) firmware - bb02p-bl-btconly-development # Runs signed/unsigned firmware and has bootloader menu + bb02p-bl-multi + bb02p-bl-btconly ) set(BOOTLOADERS ${BOOTLOADERS-BITBOX02} @@ -305,12 +302,6 @@ set(BOOTLOADERS ) set(BOOTLOADERS ${BOOTLOADERS} PARENT_SCOPE) -# Used to add QTouch to development bootloaders of bb02 bootloaders -set(DEVDEVICE-BOOTLOADERS - bb02-bl-multi-development - bb02-bl-btconly-development -) - set(FIRMWARES firmware # Firmware MULTI firmware-btc # Firmware BTC-ONLY @@ -419,6 +410,9 @@ if(CMAKE_CROSSCOMPILING) if(BOOTLOADER_LOCKED) target_compile_definitions(${elf} PRIVATE BOOTLOADER_PRODUCTION) endif() + if(BOOTLOADER_DEVDEVICE) + target_compile_definitions(${elf} PRIVATE BOOTLOADER_DEVDEVICE) + endif() # needed to find version.h target_include_directories(${elf} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) # needed to find bootloader_version.h @@ -436,7 +430,11 @@ if(CMAKE_CROSSCOMPILING) target_link_libraries(${elf} PRIVATE -Wl,--print-memory-usage) endforeach(bootloader) - foreach(bootloader ${DEVDEVICE-BOOTLOADERS} ${BOOTLOADERS-BITBOX02PLUS}) + set(QTOUCH-BOOTLOADERS ${BOOTLOADERS-BITBOX02PLUS}) + if(BOOTLOADER_DEVDEVICE) + list(APPEND QTOUCH-BOOTLOADERS ${BOOTLOADERS-BITBOX02}) + endif() + foreach(bootloader ${QTOUCH-BOOTLOADERS}) set(elf ${bootloader}.elf) target_link_libraries(${elf} PRIVATE ${QTOUCHLIB_A} ${QTOUCHLIB_B} ${QTOUCHLIB_T}) target_sources(${elf} PRIVATE ${QTOUCH-SOURCES}) @@ -451,18 +449,14 @@ if(CMAKE_CROSSCOMPILING) # BB02 definitions target_compile_definitions(bb02-bl-multi.elf PRIVATE PRODUCT_BITBOX_MULTI) - target_compile_definitions(bb02-bl-multi-development.elf PRIVATE PRODUCT_BITBOX_MULTI BOOTLOADER_DEVDEVICE) target_compile_definitions(bb02-bl-btconly.elf PRIVATE PRODUCT_BITBOX_BTCONLY) - target_compile_definitions(bb02-bl-btconly-development.elf PRIVATE PRODUCT_BITBOX_BTCONLY BOOTLOADER_DEVDEVICE) # BB02PLUS definitions target_compile_definitions(bb02p-bl-multi.elf PRIVATE PRODUCT_BITBOX_PLUS_MULTI) - target_compile_definitions(bb02p-bl-multi-development.elf PRIVATE PRODUCT_BITBOX_PLUS_MULTI BOOTLOADER_DEVDEVICE) target_compile_definitions(bb02p-bl-btconly.elf PRIVATE PRODUCT_BITBOX_PLUS_BTCONLY) - target_compile_definitions(bb02p-bl-btconly-development.elf PRIVATE PRODUCT_BITBOX_PLUS_BTCONLY BOOTLOADER_DEVDEVICE) foreach(firmware ${FIRMWARES}) set(elf ${firmware}.elf) From a2e88310bb8d57a0d6d6885d24c0699c55f09406 Mon Sep 17 00:00:00 2001 From: Niklas Dusenlund Date: Fri, 12 Jun 2026 15:17:02 +0200 Subject: [PATCH 4/9] cmake: Add BITBOX02_EDITION, drop rust target-* features --- CMakeLists.txt | 6 ++ Makefile | 101 ++++++++++++++++++---------- src/CMakeLists.txt | 58 ++++++++++------ src/rust/bitbox02-rust-c/Cargo.toml | 42 ++---------- 4 files changed, 114 insertions(+), 93 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2916c0d19a..ccc5c7fae1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,6 +65,11 @@ option(SANITIZE_UNDEFINED "Compile with ubsan." OFF) option(CMAKE_VERBOSE_MAKEFILE "Verbose build." OFF) option(BOOTLOADER_LOCKED "Build bootloaders that lock the bootloader area." OFF) option(BOOTLOADER_DEVDEVICE "Build bootloaders with the development device menu." OFF) +set(BITBOX02_EDITION "multi" CACHE STRING "Build BitBox02 edition.") +set_property(CACHE BITBOX02_EDITION PROPERTY STRINGS "multi" "btconly") +if(NOT BITBOX02_EDITION STREQUAL "multi" AND NOT BITBOX02_EDITION STREQUAL "btconly") + message(FATAL_ERROR "Invalid BITBOX02_EDITION ${BITBOX02_EDITION}") +endif() # Generate compile_command.json (for tidy and other tools) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) @@ -217,6 +222,7 @@ message(STATUS "Default EXE LDFLAGS: ${CMAKE_EXE_LINKER_FLAGS}") message(STATUS " - Options -") message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") message(STATUS "Hardware version: ${HARDWARE}") +message(STATUS "BitBox02 edition: ${BITBOX02_EDITION}") message(STATUS "Bootloader locked: ${BOOTLOADER_LOCKED}") message(STATUS "Bootloader devdevice: ${BOOTLOADER_DEVDEVICE}") message(STATUS "Verbose: ${CMAKE_VERBOSE_MAKEFILE}") diff --git a/Makefile b/Makefile index 8ab64b52f9..f6bc847a0c 100644 --- a/Makefile +++ b/Makefile @@ -23,18 +23,36 @@ build-debug/Makefile: cd build-debug && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DCMAKE_BUILD_TYPE=DEBUG .. $(MAKE) -C py/bitbox02 +build-btconly/Makefile: + ./scripts/bootstrap-cargo-config + mkdir -p build-btconly + cd build-btconly && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBITBOX02_EDITION=btconly .. + $(MAKE) -C py/bitbox02 + build-bootloader-development/Makefile: ./scripts/bootstrap-cargo-config mkdir -p build-bootloader-development cd build-bootloader-development && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBOOTLOADER_DEVDEVICE=ON .. $(MAKE) -C py/bitbox02 +build-bootloader-btconly-development/Makefile: + ./scripts/bootstrap-cargo-config + mkdir -p build-bootloader-btconly-development + cd build-bootloader-btconly-development && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBITBOX02_EDITION=btconly -DBOOTLOADER_DEVDEVICE=ON .. + $(MAKE) -C py/bitbox02 + build-bootloader-locked/Makefile: ./scripts/bootstrap-cargo-config mkdir -p build-bootloader-locked cd build-bootloader-locked && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBOOTLOADER_LOCKED=ON .. $(MAKE) -C py/bitbox02 +build-bootloader-btconly-locked/Makefile: + ./scripts/bootstrap-cargo-config + mkdir -p build-bootloader-btconly-locked + cd build-bootloader-btconly-locked && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBITBOX02_EDITION=btconly -DBOOTLOADER_LOCKED=ON .. + $(MAKE) -C py/bitbox02 + build-bootloader-development-locked/Makefile: ./scripts/bootstrap-cargo-config mkdir -p build-bootloader-development-locked @@ -66,12 +84,21 @@ build: build/Makefile # Directory for building debug build for "host" machine according to gcc convention build-debug: build-debug/Makefile +# Directory for building BTC-only firmware and bootloaders +build-btconly: build-btconly/Makefile + # Directory for building development bootloaders build-bootloader-development: build-bootloader-development/Makefile +# Directory for building BTC-only development bootloaders +build-bootloader-btconly-development: build-bootloader-btconly-development/Makefile + # Directory for building locked bootloaders build-bootloader-locked: build-bootloader-locked/Makefile +# Directory for building BTC-only locked bootloaders +build-bootloader-btconly-locked: build-bootloader-btconly-locked/Makefile + # Directory for building development locked bootloaders build-bootloader-development-locked: build-bootloader-development-locked/Makefile @@ -88,44 +115,44 @@ build-build-noasan: build-build-noasan/Makefile firmware: | build $(MAKE) -C build firmware.elf -firmware-btc: | build - $(MAKE) -C build firmware-btc.elf +firmware-btc: | build-btconly + $(MAKE) -C build-btconly firmware.elf firmware-debug: | build-debug $(MAKE) -C build-debug firmware.elf bootloader: | build - $(MAKE) -C build bb02-bl-multi.elf + $(MAKE) -C build bb02-bl.elf bootloader-development: | build-bootloader-development - $(MAKE) -C build-bootloader-development bb02-bl-multi.elf + $(MAKE) -C build-bootloader-development bb02-bl.elf bootloader-development-locked: | build-bootloader-development-locked - $(MAKE) -C build-bootloader-development-locked bb02-bl-multi.elf + $(MAKE) -C build-bootloader-development-locked bb02-bl.elf bootloader-production: | build-bootloader-locked - $(MAKE) -C build-bootloader-locked bb02-bl-multi.elf + $(MAKE) -C build-bootloader-locked bb02-bl.elf bootloader-debug: | build-bootloader-development-debug - $(MAKE) -C build-bootloader-development-debug bb02-bl-multi.elf + $(MAKE) -C build-bootloader-development-debug bb02-bl.elf -bootloader-btc: | build - $(MAKE) -C build bb02-bl-btconly.elf -bootloader-btc-development: | build-bootloader-development - $(MAKE) -C build-bootloader-development bb02-bl-btconly.elf -bootloader-btc-production: | build-bootloader-locked - $(MAKE) -C build-bootloader-locked bb02-bl-btconly.elf +bootloader-btc: | build-btconly + $(MAKE) -C build-btconly bb02-bl.elf +bootloader-btc-development: | build-bootloader-btconly-development + $(MAKE) -C build-bootloader-btconly-development bb02-bl.elf +bootloader-btc-production: | build-bootloader-btconly-locked + $(MAKE) -C build-bootloader-btconly-locked bb02-bl.elf bootloader-plus: | build - $(MAKE) -C build bb02p-bl-multi.elf + $(MAKE) -C build bb02p-bl.elf bootloader-plus-development: | build-bootloader-development - $(MAKE) -C build-bootloader-development bb02p-bl-multi.elf + $(MAKE) -C build-bootloader-development bb02p-bl.elf bootloader-plus-production: | build-bootloader-locked - $(MAKE) -C build-bootloader-locked bb02p-bl-multi.elf + $(MAKE) -C build-bootloader-locked bb02p-bl.elf bootloader-plus-debug: | build-bootloader-development-debug - $(MAKE) -C build-bootloader-development-debug bb02p-bl-multi.elf + $(MAKE) -C build-bootloader-development-debug bb02p-bl.elf -bootloader-plus-btc: | build - $(MAKE) -C build bb02p-bl-btconly.elf -bootloader-plus-btc-development: | build-bootloader-development - $(MAKE) -C build-bootloader-development bb02p-bl-btconly.elf -bootloader-plus-btc-production: | build-bootloader-locked - $(MAKE) -C build-bootloader-locked bb02p-bl-btconly.elf +bootloader-plus-btc: | build-btconly + $(MAKE) -C build-btconly bb02p-bl.elf +bootloader-plus-btc-development: | build-bootloader-btconly-development + $(MAKE) -C build-bootloader-btconly-development bb02p-bl.elf +bootloader-plus-btc-production: | build-bootloader-btconly-locked + $(MAKE) -C build-bootloader-btconly-locked bb02p-bl.elf factory-setup: | build $(MAKE) -C build factory-setup.elf @@ -166,23 +193,23 @@ run-valgrind-on-unit-tests: flash-dev-firmware: ./py/load_firmware.py build/bin/firmware.bin --debug jlink-flash-bootloader-development: | build-bootloader-development - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-development/scripts/bb02-bl-multi.jlink + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-development/scripts/bb02-bl.jlink jlink-flash-bootloader-plus-development: | build-bootloader-development - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-development/scripts/bb02p-bl-multi.jlink -jlink-flash-bootloader-btc-plus-development: | build-bootloader-development - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-development/scripts/bb02p-bl-btconly.jlink + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-development/scripts/bb02p-bl.jlink +jlink-flash-bootloader-btc-plus-development: | build-bootloader-btconly-development + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-btconly-development/scripts/bb02p-bl.jlink jlink-flash-bootloader-development-locked: | build-bootloader-development-locked - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-development-locked/scripts/bb02-bl-multi.jlink + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-development-locked/scripts/bb02-bl.jlink jlink-flash-bootloader: | build - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bb02-bl-multi.jlink -jlink-flash-bootloader-btc-development: | build-bootloader-development - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-development/scripts/bb02-bl-btconly.jlink -jlink-flash-bootloader-btc: | build - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bb02-bl-btc.jlink + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bb02-bl.jlink +jlink-flash-bootloader-btc-development: | build-bootloader-btconly-development + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-btconly-development/scripts/bb02-bl.jlink +jlink-flash-bootloader-btc: | build-btconly + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-btconly/scripts/bb02-bl.jlink jlink-flash-firmware: | build JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/firmware.jlink -jlink-flash-firmware-btc: | build - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/firmware-btc.jlink +jlink-flash-firmware-btc: | build-btconly + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-btconly/scripts/firmware.jlink jlink-flash-factory-setup: | build JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/factory-setup.jlink jlink-flash-firmware-debug: | build @@ -208,7 +235,7 @@ rtt-client: run-debug: arm-none-eabi-gdb -x scripts/jlink.gdb build-debug/bin/firmware.elf run-bootloader: - arm-none-eabi-gdb -x scripts/jlink-bootloader.gdb build-bootloader-development/bin/bb02p-bl-multi.elf + arm-none-eabi-gdb -x scripts/jlink-bootloader.gdb build-bootloader-development/bin/bb02p-bl.elf run-factory-setup-debug: arm-none-eabi-gdb -x scripts/jlink.gdb build-debug/bin/factory-setup.elf dockerinit: @@ -229,7 +256,7 @@ prepare-tidy: | build build-build $(MAKE) -C build rust-cbindgen $(MAKE) -C build-build rust-cbindgen clean: - rm -rf build build-bootloader-development build-bootloader-locked build-bootloader-development-locked build-bootloader-development-debug build-build build-debug build-build-noasan src/rust/target + rm -rf build build-btconly build-bootloader-development build-bootloader-btconly-development build-bootloader-locked build-bootloader-btconly-locked build-bootloader-development-locked build-bootloader-development-debug build-build build-debug build-build-noasan src/rust/target # When you vendor rust libs avoid duplicates vendor-rust-deps: diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6d9d2ca183..3a285747ad 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -289,12 +289,10 @@ endif() # BOOTLOADER_LOCKED and BOOTLOADER_DEVDEVICE are configured in separate build directories. # If a bootloader that locks the bootloader is flashed the bootloader area is permanently read-only. set(BOOTLOADERS-BITBOX02 - bb02-bl-multi - bb02-bl-btconly + bb02-bl ) set(BOOTLOADERS-BITBOX02PLUS - bb02p-bl-multi - bb02p-bl-btconly + bb02p-bl ) set(BOOTLOADERS ${BOOTLOADERS-BITBOX02} @@ -303,8 +301,7 @@ set(BOOTLOADERS set(BOOTLOADERS ${BOOTLOADERS} PARENT_SCOPE) set(FIRMWARES - firmware # Firmware MULTI - firmware-btc # Firmware BTC-ONLY + firmware factory-setup # Factory-setup ) set(FIRMWARES ${FIRMWARES} PARENT_SCOPE) @@ -319,7 +316,6 @@ endif() # * c-unit-tests_rust_c # or # * firmware_rust_c -# * firmware-btc_rust_c # * factory-setup_rust_c foreach(type ${RUST_LIBS}) # We build the rust standard libs so that everything from the rust runtime @@ -327,6 +323,23 @@ foreach(type ${RUST_LIBS}) if(type STREQUAL "c-unit-tests") set(RUST_CARGO_FLAGS ${RUST_CARGO_FLAGS} -Zbuild-std=std,panic_abort) endif() + if(type STREQUAL "c-unit-tests") + set(rust_features "app-bitcoin,app-litecoin,app-ethereum,app-cardano,app-u2f,firmware,c-unit-testing") + elseif(type STREQUAL "firmware") + if(BITBOX02_EDITION STREQUAL "btconly") + set(rust_features "firmware,platform-bitbox02,app-bitcoin") + else() + set(rust_features "firmware,platform-bitbox02,app-bitcoin,app-litecoin,app-ethereum,app-u2f,app-cardano") + endif() + elseif(type STREQUAL "factory-setup") + set(rust_features "factory-setup") + elseif(type STREQUAL "bb02-bl") + set(rust_features "bootloader,platform-bitbox02") + elseif(type STREQUAL "bb02p-bl") + set(rust_features "bootloader,platform-bitbox02plus") + else() + message(FATAL_ERROR "No Rust features configured for ${type}") + endif() set(lib ${RUST_BINARY_DIR}/feature-${type}/${RUST_TARGET_ARCH_DIR}/${RUST_PROFILE}/libbitbox02_rust_c.a) # The dummy output is to always trigger rebuild (cargo is clever enough to # only rebuild if something changed) @@ -346,7 +359,7 @@ foreach(type ${RUST_LIBS}) $<$:-vv> --offline $ENV{CARGOFLAGS} - --features target-${type}$<$,$>:,rtt> + --features ${rust_features}$<$,$>:,rtt> --manifest-path ${LIBBITBOX02_RUST_SOURCE_DIR}/Cargo.toml --target-dir ${RUST_BINARY_DIR}/feature-${type} ${RUST_CARGO_FLAGS} @@ -448,15 +461,19 @@ if(CMAKE_CROSSCOMPILING) # BB02 definitions - target_compile_definitions(bb02-bl-multi.elf PRIVATE PRODUCT_BITBOX_MULTI) - - target_compile_definitions(bb02-bl-btconly.elf PRIVATE PRODUCT_BITBOX_BTCONLY) + if(BITBOX02_EDITION STREQUAL "btconly") + target_compile_definitions(bb02-bl.elf PRIVATE PRODUCT_BITBOX_BTCONLY) + else() + target_compile_definitions(bb02-bl.elf PRIVATE PRODUCT_BITBOX_MULTI) + endif() # BB02PLUS definitions - target_compile_definitions(bb02p-bl-multi.elf PRIVATE PRODUCT_BITBOX_PLUS_MULTI) - - target_compile_definitions(bb02p-bl-btconly.elf PRIVATE PRODUCT_BITBOX_PLUS_BTCONLY) + if(BITBOX02_EDITION STREQUAL "btconly") + target_compile_definitions(bb02p-bl.elf PRIVATE PRODUCT_BITBOX_PLUS_BTCONLY) + else() + target_compile_definitions(bb02p-bl.elf PRIVATE PRODUCT_BITBOX_PLUS_MULTI) + endif() foreach(firmware ${FIRMWARES}) set(elf ${firmware}.elf) @@ -504,12 +521,13 @@ if(CMAKE_CROSSCOMPILING) endforeach(firmware) target_sources(firmware.elf PRIVATE firmware.c) - target_compile_definitions(firmware.elf PRIVATE PRODUCT_BITBOX_MULTI "APP_U2F=1") - target_sources(firmware.elf PRIVATE ${FIRMWARE-U2F-SOURCES} ${FIRMWARE-U2F-DRIVER-SOURCES} ${PLATFORM-BITBOX02-SOURCES}) - - target_sources(firmware-btc.elf PRIVATE firmware.c) - target_compile_definitions(firmware-btc.elf PRIVATE PRODUCT_BITBOX_BTCONLY "APP_U2F=0") - target_sources(firmware-btc.elf PRIVATE ${PLATFORM-BITBOX02-SOURCES}) + if(BITBOX02_EDITION STREQUAL "btconly") + target_compile_definitions(firmware.elf PRIVATE PRODUCT_BITBOX_BTCONLY "APP_U2F=0") + target_sources(firmware.elf PRIVATE ${PLATFORM-BITBOX02-SOURCES}) + else() + target_compile_definitions(firmware.elf PRIVATE PRODUCT_BITBOX_MULTI "APP_U2F=1") + target_sources(firmware.elf PRIVATE ${FIRMWARE-U2F-SOURCES} ${FIRMWARE-U2F-DRIVER-SOURCES} ${PLATFORM-BITBOX02-SOURCES}) + endif() target_sources(factory-setup.elf PRIVATE factorysetup.c) target_compile_definitions(factory-setup.elf PRIVATE PRODUCT_BITBOX02_FACTORYSETUP "APP_U2F=0") diff --git a/src/rust/bitbox02-rust-c/Cargo.toml b/src/rust/bitbox02-rust-c/Cargo.toml index 16491f7863..56d116dcc6 100644 --- a/src/rust/bitbox02-rust-c/Cargo.toml +++ b/src/rust/bitbox02-rust-c/Cargo.toml @@ -29,41 +29,6 @@ bip39 = { workspace = true } zeroize = { workspace = true } [features] -# Only one of the "target-" should be activated, which in turn defines/activates the dependent features. -target-bb02-bl-multi = ["bootloader", "platform-bitbox02"] -target-bb02-bl-multi-development = ["bootloader", "platform-bitbox02"] -target-bb02-bl-multi-development-locked = ["bootloader", "platform-bitbox02"] -target-bb02-bl-multi-production = ["bootloader", "platform-bitbox02"] -target-bb02-bl-btconly = ["bootloader", "platform-bitbox02"] -target-bb02-bl-btconly-development = ["bootloader", "platform-bitbox02"] -target-bb02-bl-btconly-production = ["bootloader", "platform-bitbox02"] -target-bb02p-bl-multi = ["bootloader", "platform-bitbox02plus"] -target-bb02p-bl-multi-development = ["bootloader", "platform-bitbox02plus"] -target-bb02p-bl-multi-development-locked = ["bootloader", "platform-bitbox02plus"] -target-bb02p-bl-multi-production = ["bootloader", "platform-bitbox02plus"] -target-bb02p-bl-btconly = ["bootloader", "platform-bitbox02plus"] -target-bb02p-bl-btconly-development = ["bootloader", "platform-bitbox02plus"] -target-bb02p-bl-btconly-production = ["bootloader", "platform-bitbox02plus"] -target-firmware = ["firmware", "platform-bitbox02", "app-bitcoin", "app-litecoin", "app-ethereum", "app-u2f", "app-cardano"] -target-firmware-btc = ["firmware", "platform-bitbox02", "app-bitcoin"] -target-factory-setup = [ - # enable these features - "factory-setup", - "firmware", - "platform-bitbox02", - "dep:bitcoin", -] -# add Rust features which are called in the C unit tests (currently there is only one target for C tests). -target-c-unit-tests = [ - "app-bitcoin", - "app-litecoin", - "app-ethereum", - "app-cardano", - "app-u2f", - "firmware", - "c-unit-testing", -] - platform-bitbox02 = [] platform-bitbox02plus = ["util/sha2", "bitbox-noise", "bitbox02-rust"] @@ -118,6 +83,11 @@ app-cardano = [ "bitbox02-rust/app-cardano", ] -factory-setup = ["bitbox02/factory-setup"] +factory-setup = [ + "firmware", + "platform-bitbox02", + "dep:bitcoin", + "bitbox02/factory-setup", +] rtt = ["util/rtt"] From 223a79666d391ba3b1f6c64e510efb693977611c Mon Sep 17 00:00:00 2001 From: Niklas Dusenlund Date: Fri, 12 Jun 2026 15:34:00 +0200 Subject: [PATCH 5/9] cmake: Add BITBOX02_PLATFORM --- CMakeLists.txt | 8 ++ Makefile | 133 +++++++++++++++++------- scripts/CMakeLists.txt | 10 +- src/CMakeLists.txt | 154 ++++++++++++++-------------- src/rust/bitbox02-rust-c/Cargo.toml | 2 +- src/rust/bitbox02-rust-c/src/lib.rs | 6 +- 6 files changed, 190 insertions(+), 123 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ccc5c7fae1..608e7c6295 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,6 +70,13 @@ set_property(CACHE BITBOX02_EDITION PROPERTY STRINGS "multi" "btconly") if(NOT BITBOX02_EDITION STREQUAL "multi" AND NOT BITBOX02_EDITION STREQUAL "btconly") message(FATAL_ERROR "Invalid BITBOX02_EDITION ${BITBOX02_EDITION}") endif() +set(BITBOX02_PLATFORM "classic" CACHE STRING "Build BitBox02 platform.") +set_property(CACHE BITBOX02_PLATFORM PROPERTY STRINGS "classic" "nova") +if(NOT BITBOX02_PLATFORM STREQUAL "classic" AND NOT BITBOX02_PLATFORM STREQUAL "nova") + message(FATAL_ERROR "Invalid BITBOX02_PLATFORM ${BITBOX02_PLATFORM}") +endif() +set(FIRMWARE_TARGET "firmware") +set(BOOTLOADER_TARGET "bootloader") # Generate compile_command.json (for tidy and other tools) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) @@ -223,6 +230,7 @@ message(STATUS " - Options -") message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") message(STATUS "Hardware version: ${HARDWARE}") message(STATUS "BitBox02 edition: ${BITBOX02_EDITION}") +message(STATUS "BitBox02 platform: ${BITBOX02_PLATFORM}") message(STATUS "Bootloader locked: ${BOOTLOADER_LOCKED}") message(STATUS "Bootloader devdevice: ${BOOTLOADER_DEVDEVICE}") message(STATUS "Verbose: ${CMAKE_VERBOSE_MAKEFILE}") diff --git a/Makefile b/Makefile index f6bc847a0c..70046a4998 100644 --- a/Makefile +++ b/Makefile @@ -29,30 +29,66 @@ build-btconly/Makefile: cd build-btconly && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBITBOX02_EDITION=btconly .. $(MAKE) -C py/bitbox02 +build-nova/Makefile: + ./scripts/bootstrap-cargo-config + mkdir -p build-nova + cd build-nova && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBITBOX02_PLATFORM=nova .. + $(MAKE) -C py/bitbox02 + +build-nova-btconly/Makefile: + ./scripts/bootstrap-cargo-config + mkdir -p build-nova-btconly + cd build-nova-btconly && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBITBOX02_PLATFORM=nova -DBITBOX02_EDITION=btconly .. + $(MAKE) -C py/bitbox02 + build-bootloader-development/Makefile: ./scripts/bootstrap-cargo-config mkdir -p build-bootloader-development cd build-bootloader-development && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBOOTLOADER_DEVDEVICE=ON .. $(MAKE) -C py/bitbox02 +build-bootloader-nova-development/Makefile: + ./scripts/bootstrap-cargo-config + mkdir -p build-bootloader-nova-development + cd build-bootloader-nova-development && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBITBOX02_PLATFORM=nova -DBOOTLOADER_DEVDEVICE=ON .. + $(MAKE) -C py/bitbox02 + build-bootloader-btconly-development/Makefile: ./scripts/bootstrap-cargo-config mkdir -p build-bootloader-btconly-development cd build-bootloader-btconly-development && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBITBOX02_EDITION=btconly -DBOOTLOADER_DEVDEVICE=ON .. $(MAKE) -C py/bitbox02 +build-bootloader-nova-btconly-development/Makefile: + ./scripts/bootstrap-cargo-config + mkdir -p build-bootloader-nova-btconly-development + cd build-bootloader-nova-btconly-development && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBITBOX02_PLATFORM=nova -DBITBOX02_EDITION=btconly -DBOOTLOADER_DEVDEVICE=ON .. + $(MAKE) -C py/bitbox02 + build-bootloader-locked/Makefile: ./scripts/bootstrap-cargo-config mkdir -p build-bootloader-locked cd build-bootloader-locked && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBOOTLOADER_LOCKED=ON .. $(MAKE) -C py/bitbox02 +build-bootloader-nova-locked/Makefile: + ./scripts/bootstrap-cargo-config + mkdir -p build-bootloader-nova-locked + cd build-bootloader-nova-locked && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBITBOX02_PLATFORM=nova -DBOOTLOADER_LOCKED=ON .. + $(MAKE) -C py/bitbox02 + build-bootloader-btconly-locked/Makefile: ./scripts/bootstrap-cargo-config mkdir -p build-bootloader-btconly-locked cd build-bootloader-btconly-locked && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBITBOX02_EDITION=btconly -DBOOTLOADER_LOCKED=ON .. $(MAKE) -C py/bitbox02 +build-bootloader-nova-btconly-locked/Makefile: + ./scripts/bootstrap-cargo-config + mkdir -p build-bootloader-nova-btconly-locked + cd build-bootloader-nova-btconly-locked && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DBITBOX02_PLATFORM=nova -DBITBOX02_EDITION=btconly -DBOOTLOADER_LOCKED=ON .. + $(MAKE) -C py/bitbox02 + build-bootloader-development-locked/Makefile: ./scripts/bootstrap-cargo-config mkdir -p build-bootloader-development-locked @@ -65,6 +101,12 @@ build-bootloader-development-debug/Makefile: cd build-bootloader-development-debug && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DCMAKE_BUILD_TYPE=DEBUG -DBOOTLOADER_DEVDEVICE=ON .. $(MAKE) -C py/bitbox02 +build-bootloader-nova-development-debug/Makefile: + ./scripts/bootstrap-cargo-config + mkdir -p build-bootloader-nova-development-debug + cd build-bootloader-nova-development-debug && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DCMAKE_BUILD_TYPE=DEBUG -DBITBOX02_PLATFORM=nova -DBOOTLOADER_DEVDEVICE=ON .. + $(MAKE) -C py/bitbox02 + build-build/Makefile: ./scripts/bootstrap-cargo-config mkdir -p build-build @@ -87,24 +129,45 @@ build-debug: build-debug/Makefile # Directory for building BTC-only firmware and bootloaders build-btconly: build-btconly/Makefile +# Directory for building Nova bootloaders +build-nova: build-nova/Makefile + +# Directory for building BTC-only Nova bootloaders +build-nova-btconly: build-nova-btconly/Makefile + # Directory for building development bootloaders build-bootloader-development: build-bootloader-development/Makefile +# Directory for building Nova development bootloaders +build-bootloader-nova-development: build-bootloader-nova-development/Makefile + # Directory for building BTC-only development bootloaders build-bootloader-btconly-development: build-bootloader-btconly-development/Makefile +# Directory for building BTC-only Nova development bootloaders +build-bootloader-nova-btconly-development: build-bootloader-nova-btconly-development/Makefile + # Directory for building locked bootloaders build-bootloader-locked: build-bootloader-locked/Makefile +# Directory for building Nova locked bootloaders +build-bootloader-nova-locked: build-bootloader-nova-locked/Makefile + # Directory for building BTC-only locked bootloaders build-bootloader-btconly-locked: build-bootloader-btconly-locked/Makefile +# Directory for building BTC-only Nova locked bootloaders +build-bootloader-nova-btconly-locked: build-bootloader-nova-btconly-locked/Makefile + # Directory for building development locked bootloaders build-bootloader-development-locked: build-bootloader-development-locked/Makefile # Directory for building development debug bootloaders build-bootloader-development-debug: build-bootloader-development-debug/Makefile +# Directory for building Nova development debug bootloaders +build-bootloader-nova-development-debug: build-bootloader-nova-development-debug/Makefile + # Directory for building for "build" machine according to gcc convention build-build: build-build/Makefile @@ -121,38 +184,38 @@ firmware-debug: | build-debug $(MAKE) -C build-debug firmware.elf bootloader: | build - $(MAKE) -C build bb02-bl.elf + $(MAKE) -C build bootloader.elf bootloader-development: | build-bootloader-development - $(MAKE) -C build-bootloader-development bb02-bl.elf + $(MAKE) -C build-bootloader-development bootloader.elf bootloader-development-locked: | build-bootloader-development-locked - $(MAKE) -C build-bootloader-development-locked bb02-bl.elf + $(MAKE) -C build-bootloader-development-locked bootloader.elf bootloader-production: | build-bootloader-locked - $(MAKE) -C build-bootloader-locked bb02-bl.elf + $(MAKE) -C build-bootloader-locked bootloader.elf bootloader-debug: | build-bootloader-development-debug - $(MAKE) -C build-bootloader-development-debug bb02-bl.elf + $(MAKE) -C build-bootloader-development-debug bootloader.elf bootloader-btc: | build-btconly - $(MAKE) -C build-btconly bb02-bl.elf + $(MAKE) -C build-btconly bootloader.elf bootloader-btc-development: | build-bootloader-btconly-development - $(MAKE) -C build-bootloader-btconly-development bb02-bl.elf + $(MAKE) -C build-bootloader-btconly-development bootloader.elf bootloader-btc-production: | build-bootloader-btconly-locked - $(MAKE) -C build-bootloader-btconly-locked bb02-bl.elf - -bootloader-plus: | build - $(MAKE) -C build bb02p-bl.elf -bootloader-plus-development: | build-bootloader-development - $(MAKE) -C build-bootloader-development bb02p-bl.elf -bootloader-plus-production: | build-bootloader-locked - $(MAKE) -C build-bootloader-locked bb02p-bl.elf -bootloader-plus-debug: | build-bootloader-development-debug - $(MAKE) -C build-bootloader-development-debug bb02p-bl.elf - -bootloader-plus-btc: | build-btconly - $(MAKE) -C build-btconly bb02p-bl.elf -bootloader-plus-btc-development: | build-bootloader-btconly-development - $(MAKE) -C build-bootloader-btconly-development bb02p-bl.elf -bootloader-plus-btc-production: | build-bootloader-btconly-locked - $(MAKE) -C build-bootloader-btconly-locked bb02p-bl.elf + $(MAKE) -C build-bootloader-btconly-locked bootloader.elf + +bootloader-plus: | build-nova + $(MAKE) -C build-nova bootloader.elf +bootloader-plus-development: | build-bootloader-nova-development + $(MAKE) -C build-bootloader-nova-development bootloader.elf +bootloader-plus-production: | build-bootloader-nova-locked + $(MAKE) -C build-bootloader-nova-locked bootloader.elf +bootloader-plus-debug: | build-bootloader-nova-development-debug + $(MAKE) -C build-bootloader-nova-development-debug bootloader.elf + +bootloader-plus-btc: | build-nova-btconly + $(MAKE) -C build-nova-btconly bootloader.elf +bootloader-plus-btc-development: | build-bootloader-nova-btconly-development + $(MAKE) -C build-bootloader-nova-btconly-development bootloader.elf +bootloader-plus-btc-production: | build-bootloader-nova-btconly-locked + $(MAKE) -C build-bootloader-nova-btconly-locked bootloader.elf factory-setup: | build $(MAKE) -C build factory-setup.elf @@ -193,19 +256,19 @@ run-valgrind-on-unit-tests: flash-dev-firmware: ./py/load_firmware.py build/bin/firmware.bin --debug jlink-flash-bootloader-development: | build-bootloader-development - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-development/scripts/bb02-bl.jlink -jlink-flash-bootloader-plus-development: | build-bootloader-development - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-development/scripts/bb02p-bl.jlink -jlink-flash-bootloader-btc-plus-development: | build-bootloader-btconly-development - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-btconly-development/scripts/bb02p-bl.jlink + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-development/scripts/bootloader.jlink +jlink-flash-bootloader-plus-development: | build-bootloader-nova-development + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-nova-development/scripts/bootloader.jlink +jlink-flash-bootloader-btc-plus-development: | build-bootloader-nova-btconly-development + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-nova-btconly-development/scripts/bootloader.jlink jlink-flash-bootloader-development-locked: | build-bootloader-development-locked - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-development-locked/scripts/bb02-bl.jlink + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-development-locked/scripts/bootloader.jlink jlink-flash-bootloader: | build - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bb02-bl.jlink + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/bootloader.jlink jlink-flash-bootloader-btc-development: | build-bootloader-btconly-development - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-btconly-development/scripts/bb02-bl.jlink + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-bootloader-btconly-development/scripts/bootloader.jlink jlink-flash-bootloader-btc: | build-btconly - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-btconly/scripts/bb02-bl.jlink + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-btconly/scripts/bootloader.jlink jlink-flash-firmware: | build JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/firmware.jlink jlink-flash-firmware-btc: | build-btconly @@ -235,7 +298,7 @@ rtt-client: run-debug: arm-none-eabi-gdb -x scripts/jlink.gdb build-debug/bin/firmware.elf run-bootloader: - arm-none-eabi-gdb -x scripts/jlink-bootloader.gdb build-bootloader-development/bin/bb02p-bl.elf + arm-none-eabi-gdb -x scripts/jlink-bootloader.gdb build-bootloader-nova-development/bin/bootloader.elf run-factory-setup-debug: arm-none-eabi-gdb -x scripts/jlink.gdb build-debug/bin/factory-setup.elf dockerinit: @@ -256,7 +319,7 @@ prepare-tidy: | build build-build $(MAKE) -C build rust-cbindgen $(MAKE) -C build-build rust-cbindgen clean: - rm -rf build build-btconly build-bootloader-development build-bootloader-btconly-development build-bootloader-locked build-bootloader-btconly-locked build-bootloader-development-locked build-bootloader-development-debug build-build build-debug build-build-noasan src/rust/target + rm -rf build build-btconly build-nova build-nova-btconly build-bootloader-development build-bootloader-nova-development build-bootloader-btconly-development build-bootloader-nova-btconly-development build-bootloader-locked build-bootloader-nova-locked build-bootloader-btconly-locked build-bootloader-nova-btconly-locked build-bootloader-development-locked build-bootloader-development-debug build-bootloader-nova-development-debug build-build build-debug build-build-noasan src/rust/target # When you vendor rust libs avoid duplicates vendor-rust-deps: diff --git a/scripts/CMakeLists.txt b/scripts/CMakeLists.txt index ab8e6672d1..e5fa5d0535 100644 --- a/scripts/CMakeLists.txt +++ b/scripts/CMakeLists.txt @@ -10,10 +10,8 @@ if(PYTHONINTERP_FOUND) endforeach() # This template flashes without an offset - foreach(target ${BOOTLOADERS}) - execute_process( - COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/expand_template ${CMAKE_CURRENT_SOURCE_DIR}/template-bootloader.jlink file=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${target}.bin -o ${target}.jlink - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} - ) - endforeach() + execute_process( + COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/expand_template ${CMAKE_CURRENT_SOURCE_DIR}/template-bootloader.jlink file=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${BOOTLOADER_TARGET}.bin -o ${BOOTLOADER_TARGET}.jlink + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + ) endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3a285747ad..b358307649 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -288,26 +288,15 @@ endif() # BOOTLOADER_LOCKED and BOOTLOADER_DEVDEVICE are configured in separate build directories. # If a bootloader that locks the bootloader is flashed the bootloader area is permanently read-only. -set(BOOTLOADERS-BITBOX02 - bb02-bl - ) -set(BOOTLOADERS-BITBOX02PLUS - bb02p-bl -) -set(BOOTLOADERS - ${BOOTLOADERS-BITBOX02} - ${BOOTLOADERS-BITBOX02PLUS} - ) -set(BOOTLOADERS ${BOOTLOADERS} PARENT_SCOPE) set(FIRMWARES - firmware + ${FIRMWARE_TARGET} factory-setup # Factory-setup ) set(FIRMWARES ${FIRMWARES} PARENT_SCOPE) if(CMAKE_CROSSCOMPILING) - set(RUST_LIBS ${FIRMWARES} ${BOOTLOADERS}) + set(RUST_LIBS ${FIRMWARES} ${BOOTLOADER_TARGET}) else() set(RUST_LIBS c-unit-tests) endif() @@ -325,7 +314,7 @@ foreach(type ${RUST_LIBS}) endif() if(type STREQUAL "c-unit-tests") set(rust_features "app-bitcoin,app-litecoin,app-ethereum,app-cardano,app-u2f,firmware,c-unit-testing") - elseif(type STREQUAL "firmware") + elseif(type STREQUAL "${FIRMWARE_TARGET}") if(BITBOX02_EDITION STREQUAL "btconly") set(rust_features "firmware,platform-bitbox02,app-bitcoin") else() @@ -333,10 +322,12 @@ foreach(type ${RUST_LIBS}) endif() elseif(type STREQUAL "factory-setup") set(rust_features "factory-setup") - elseif(type STREQUAL "bb02-bl") - set(rust_features "bootloader,platform-bitbox02") - elseif(type STREQUAL "bb02p-bl") - set(rust_features "bootloader,platform-bitbox02plus") + elseif(type STREQUAL "${BOOTLOADER_TARGET}") + if(BITBOX02_PLATFORM STREQUAL "nova") + set(rust_features "bootloader,platform-bitbox02-nova") + else() + set(rust_features "bootloader,platform-bitbox02") + endif() else() message(FATAL_ERROR "No Rust features configured for ${type}") endif() @@ -413,66 +404,57 @@ if(CMAKE_CROSSCOMPILING) set(HEAP_SIZE "0x18000" CACHE STRING "Specify heap size for bootloader/firmware") set(HEAP_SIZE ${HEAP_SIZE} PARENT_SCOPE) - foreach(bootloader ${BOOTLOADERS}) - set(elf ${bootloader}.elf) - add_executable(${elf} ${BOOTLOADER-SOURCES} ${PLATFORM-BITBOX02-SOURCES}) - target_compile_options(${elf} PRIVATE -fno-lto) - target_link_libraries(${elf} PRIVATE c asf4-drivers-min samd51a-ds -Wl,-u,exception_table) - target_include_directories(${elf} PRIVATE ${INCLUDES}) - target_compile_definitions(${elf} PRIVATE BOOTLOADER "APP_U2F=0") - if(BOOTLOADER_LOCKED) - target_compile_definitions(${elf} PRIVATE BOOTLOADER_PRODUCTION) - endif() - if(BOOTLOADER_DEVDEVICE) - target_compile_definitions(${elf} PRIVATE BOOTLOADER_DEVDEVICE) - endif() - # needed to find version.h - target_include_directories(${elf} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) - # needed to find bootloader_version.h - target_include_directories(${elf} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/bootloader) - target_link_libraries(${elf} PRIVATE "-Wl,-Map=\"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${bootloader}.map\" -T\"${CMAKE_SOURCE_DIR}/bootloader.ld\"") - target_link_libraries(${elf} PRIVATE -Wl,--defsym=STACK_SIZE=${STACK_SIZE} -Wl,-defsym=HEAP_SIZE=${HEAP_SIZE}) - - target_link_libraries(${elf} PRIVATE ${bootloader}_rust_c) - - # Select the smaller version of libc called nano. - target_compile_options(${elf} PRIVATE --specs=nano.specs) - target_link_libraries(${elf} PRIVATE --specs=nano.specs) - target_compile_options(${elf} PRIVATE --specs=nosys.specs) - target_link_libraries(${elf} PRIVATE --specs=nosys.specs) - target_link_libraries(${elf} PRIVATE -Wl,--print-memory-usage) - endforeach(bootloader) - - set(QTOUCH-BOOTLOADERS ${BOOTLOADERS-BITBOX02PLUS}) + add_executable(${BOOTLOADER_TARGET}.elf ${BOOTLOADER-SOURCES} ${PLATFORM-BITBOX02-SOURCES}) + target_compile_options(${BOOTLOADER_TARGET}.elf PRIVATE -fno-lto) + target_link_libraries(${BOOTLOADER_TARGET}.elf PRIVATE c asf4-drivers-min samd51a-ds -Wl,-u,exception_table) + target_include_directories(${BOOTLOADER_TARGET}.elf PRIVATE ${INCLUDES}) + target_compile_definitions(${BOOTLOADER_TARGET}.elf PRIVATE BOOTLOADER "APP_U2F=0") + if(BOOTLOADER_LOCKED) + target_compile_definitions(${BOOTLOADER_TARGET}.elf PRIVATE BOOTLOADER_PRODUCTION) + endif() if(BOOTLOADER_DEVDEVICE) - list(APPEND QTOUCH-BOOTLOADERS ${BOOTLOADERS-BITBOX02}) + target_compile_definitions(${BOOTLOADER_TARGET}.elf PRIVATE BOOTLOADER_DEVDEVICE) endif() - foreach(bootloader ${QTOUCH-BOOTLOADERS}) - set(elf ${bootloader}.elf) - target_link_libraries(${elf} PRIVATE ${QTOUCHLIB_A} ${QTOUCHLIB_B} ${QTOUCHLIB_T}) - target_sources(${elf} PRIVATE ${QTOUCH-SOURCES}) - endforeach(bootloader) - - foreach(bootloader ${BOOTLOADERS-BITBOX02PLUS}) - set(elf ${bootloader}.elf) - target_sources(${elf} PRIVATE ${PLATFORM-BITBOX02-PLUS-SOURCES}) - target_link_libraries(${bootloader}.elf PRIVATE embedded-swd) - endforeach(bootloader) - - # BB02 definitions - - if(BITBOX02_EDITION STREQUAL "btconly") - target_compile_definitions(bb02-bl.elf PRIVATE PRODUCT_BITBOX_BTCONLY) - else() - target_compile_definitions(bb02-bl.elf PRIVATE PRODUCT_BITBOX_MULTI) + # needed to find version.h + target_include_directories(${BOOTLOADER_TARGET}.elf PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) + # needed to find bootloader_version.h + target_include_directories(${BOOTLOADER_TARGET}.elf PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/bootloader) + target_link_libraries(${BOOTLOADER_TARGET}.elf PRIVATE "-Wl,-Map=\"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${BOOTLOADER_TARGET}.map\" -T\"${CMAKE_SOURCE_DIR}/bootloader.ld\"") + target_link_libraries(${BOOTLOADER_TARGET}.elf PRIVATE -Wl,--defsym=STACK_SIZE=${STACK_SIZE} -Wl,-defsym=HEAP_SIZE=${HEAP_SIZE}) + + target_link_libraries(${BOOTLOADER_TARGET}.elf PRIVATE ${BOOTLOADER_TARGET}_rust_c) + + # Select the smaller version of libc called nano. + target_compile_options(${BOOTLOADER_TARGET}.elf PRIVATE --specs=nano.specs) + target_link_libraries(${BOOTLOADER_TARGET}.elf PRIVATE --specs=nano.specs) + target_compile_options(${BOOTLOADER_TARGET}.elf PRIVATE --specs=nosys.specs) + target_link_libraries(${BOOTLOADER_TARGET}.elf PRIVATE --specs=nosys.specs) + target_link_libraries(${BOOTLOADER_TARGET}.elf PRIVATE -Wl,--print-memory-usage) + + if(BITBOX02_PLATFORM STREQUAL "nova" OR BOOTLOADER_DEVDEVICE) + target_link_libraries(${BOOTLOADER_TARGET}.elf PRIVATE ${QTOUCHLIB_A} ${QTOUCHLIB_B} ${QTOUCHLIB_T}) + target_sources(${BOOTLOADER_TARGET}.elf PRIVATE ${QTOUCH-SOURCES}) endif() - # BB02PLUS definitions + if(BITBOX02_PLATFORM STREQUAL "nova") + target_sources(${BOOTLOADER_TARGET}.elf PRIVATE ${PLATFORM-BITBOX02-PLUS-SOURCES}) + if(CMAKE_BUILD_TYPE STREQUAL "DEBUG") + target_link_libraries(${BOOTLOADER_TARGET}.elf PRIVATE embedded-swd) + endif() + endif() if(BITBOX02_EDITION STREQUAL "btconly") - target_compile_definitions(bb02p-bl.elf PRIVATE PRODUCT_BITBOX_PLUS_BTCONLY) + if(BITBOX02_PLATFORM STREQUAL "nova") + target_compile_definitions(${BOOTLOADER_TARGET}.elf PRIVATE PRODUCT_BITBOX_PLUS_BTCONLY) + else() + target_compile_definitions(${BOOTLOADER_TARGET}.elf PRIVATE PRODUCT_BITBOX_BTCONLY) + endif() else() - target_compile_definitions(bb02p-bl.elf PRIVATE PRODUCT_BITBOX_PLUS_MULTI) + if(BITBOX02_PLATFORM STREQUAL "nova") + target_compile_definitions(${BOOTLOADER_TARGET}.elf PRIVATE PRODUCT_BITBOX_PLUS_MULTI) + else() + target_compile_definitions(${BOOTLOADER_TARGET}.elf PRIVATE PRODUCT_BITBOX_MULTI) + endif() endif() foreach(firmware ${FIRMWARES}) @@ -490,7 +472,6 @@ if(CMAKE_CROSSCOMPILING) target_link_libraries(${elf} PRIVATE optiga - embedded-swd cryptoauthlib c ${firmware}_rust_c @@ -498,6 +479,9 @@ if(CMAKE_CROSSCOMPILING) asf4-drivers-min asf4-drivers -Wl,-u,exception_table) + if(firmware STREQUAL "factory-setup" OR CMAKE_BUILD_TYPE STREQUAL "DEBUG") + target_link_libraries(${elf} PRIVATE embedded-swd) + endif() target_include_directories(${elf} PRIVATE ${INCLUDES}) # needed to find version.h @@ -520,13 +504,13 @@ if(CMAKE_CROSSCOMPILING) target_compile_definitions(${elf} PRIVATE OPTIGA_LIB_EXTERNAL="optiga_config.h") endforeach(firmware) - target_sources(firmware.elf PRIVATE firmware.c) + target_sources(${FIRMWARE_TARGET}.elf PRIVATE firmware.c) if(BITBOX02_EDITION STREQUAL "btconly") - target_compile_definitions(firmware.elf PRIVATE PRODUCT_BITBOX_BTCONLY "APP_U2F=0") - target_sources(firmware.elf PRIVATE ${PLATFORM-BITBOX02-SOURCES}) + target_compile_definitions(${FIRMWARE_TARGET}.elf PRIVATE PRODUCT_BITBOX_BTCONLY "APP_U2F=0") + target_sources(${FIRMWARE_TARGET}.elf PRIVATE ${PLATFORM-BITBOX02-SOURCES}) else() - target_compile_definitions(firmware.elf PRIVATE PRODUCT_BITBOX_MULTI "APP_U2F=1") - target_sources(firmware.elf PRIVATE ${FIRMWARE-U2F-SOURCES} ${FIRMWARE-U2F-DRIVER-SOURCES} ${PLATFORM-BITBOX02-SOURCES}) + target_compile_definitions(${FIRMWARE_TARGET}.elf PRIVATE PRODUCT_BITBOX_MULTI "APP_U2F=1") + target_sources(${FIRMWARE_TARGET}.elf PRIVATE ${FIRMWARE-U2F-SOURCES} ${FIRMWARE-U2F-DRIVER-SOURCES} ${PLATFORM-BITBOX02-SOURCES}) endif() target_sources(factory-setup.elf PRIVATE factorysetup.c) @@ -551,7 +535,21 @@ if(CMAKE_CROSSCOMPILING) target_link_libraries(factory-setup.elf PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/bitbox-da14531-firmware.o) - foreach(name ${BOOTLOADERS} ${FIRMWARES}) + add_custom_command( + TARGET ${BOOTLOADER_TARGET}.elf POST_BUILD + COMMAND ${CMAKE_SIZE} ${BOOTLOADER_TARGET}.elf + COMMAND ${CMAKE_OBJCOPY} -O binary ${BOOTLOADER_TARGET}.elf ${BOOTLOADER_TARGET}.bin + WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} + COMMENT "\nGenerating binary ${BOOTLOADER_TARGET}.bin" + ) + # Fail on warnings for our release builds + if(NOT CMAKE_BUILD_TYPE STREQUAL "DEBUG") + target_compile_options(${BOOTLOADER_TARGET}.elf PRIVATE "-Werror") + endif() + + target_link_libraries(${BOOTLOADER_TARGET}.elf PRIVATE -Wl,--print-memory-usage) + + foreach(name ${FIRMWARES}) add_custom_command( TARGET ${name}.elf POST_BUILD COMMAND ${CMAKE_SIZE} ${name}.elf diff --git a/src/rust/bitbox02-rust-c/Cargo.toml b/src/rust/bitbox02-rust-c/Cargo.toml index 56d116dcc6..0c60b25cc8 100644 --- a/src/rust/bitbox02-rust-c/Cargo.toml +++ b/src/rust/bitbox02-rust-c/Cargo.toml @@ -30,7 +30,7 @@ zeroize = { workspace = true } [features] platform-bitbox02 = [] -platform-bitbox02plus = ["util/sha2", "bitbox-noise", "bitbox02-rust"] +platform-bitbox02-nova = ["util/sha2", "bitbox-noise", "bitbox02-rust"] bootloader = [] firmware = [ diff --git a/src/rust/bitbox02-rust-c/src/lib.rs b/src/rust/bitbox02-rust-c/src/lib.rs index 88aebbd10c..b7a2d5f557 100644 --- a/src/rust/bitbox02-rust-c/src/lib.rs +++ b/src/rust/bitbox02-rust-c/src/lib.rs @@ -14,7 +14,7 @@ extern crate alloc; pub mod async_usb; #[cfg(any( feature = "firmware", - all(feature = "bootloader", feature = "platform-bitbox02plus") + all(feature = "bootloader", feature = "platform-bitbox02-nova") ))] mod communication_mode; #[cfg(feature = "firmware")] @@ -40,7 +40,7 @@ extern crate bitbox_aes; // Enable for firmware and for Nova bootloader (BitBox02 bootloader currently does not need it). #[cfg(any( feature = "firmware", - all(feature = "bootloader", feature = "platform-bitbox02plus") + all(feature = "bootloader", feature = "platform-bitbox02-nova") ))] extern crate bitbox02_rust; @@ -61,7 +61,7 @@ extern crate util; #[allow(unused)] #[cfg(any( feature = "firmware", - all(feature = "bootloader", feature = "platform-bitbox02plus") + all(feature = "bootloader", feature = "platform-bitbox02-nova") ))] type HalImpl = bitbox02::hal::BitBox02Hal; From 55441295ca5f6ecaf08a186b4bce84e8d3a61cf3 Mon Sep 17 00:00:00 2001 From: Niklas Dusenlund Date: Fri, 12 Jun 2026 15:56:14 +0200 Subject: [PATCH 6/9] cmake: Add FACTORY_SETUP --- CMakeLists.txt | 2 + Makefile | 34 ++++++-- scripts/CMakeLists.txt | 10 +-- src/CMakeLists.txt | 174 +++++++++++++++++++---------------------- 4 files changed, 113 insertions(+), 107 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 608e7c6295..c195b6c8ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,6 +65,7 @@ option(SANITIZE_UNDEFINED "Compile with ubsan." OFF) option(CMAKE_VERBOSE_MAKEFILE "Verbose build." OFF) option(BOOTLOADER_LOCKED "Build bootloaders that lock the bootloader area." OFF) option(BOOTLOADER_DEVDEVICE "Build bootloaders with the development device menu." OFF) +option(FACTORY_SETUP "Build factory setup firmware." OFF) set(BITBOX02_EDITION "multi" CACHE STRING "Build BitBox02 edition.") set_property(CACHE BITBOX02_EDITION PROPERTY STRINGS "multi" "btconly") if(NOT BITBOX02_EDITION STREQUAL "multi" AND NOT BITBOX02_EDITION STREQUAL "btconly") @@ -233,6 +234,7 @@ message(STATUS "BitBox02 edition: ${BITBOX02_EDITION}") message(STATUS "BitBox02 platform: ${BITBOX02_PLATFORM}") message(STATUS "Bootloader locked: ${BOOTLOADER_LOCKED}") message(STATUS "Bootloader devdevice: ${BOOTLOADER_DEVDEVICE}") +message(STATUS "Factory setup: ${FACTORY_SETUP}") message(STATUS "Verbose: ${CMAKE_VERBOSE_MAKEFILE}") message(STATUS "Coverage flags: ${COVERAGE}") message(STATUS "\n=============================================\n\n") diff --git a/Makefile b/Makefile index 70046a4998..dcb454258d 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,18 @@ build-debug/Makefile: cd build-debug && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DCMAKE_BUILD_TYPE=DEBUG .. $(MAKE) -C py/bitbox02 +build-factory-setup/Makefile: + ./scripts/bootstrap-cargo-config + mkdir -p build-factory-setup + cd build-factory-setup && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DFACTORY_SETUP=ON .. + $(MAKE) -C py/bitbox02 + +build-factory-setup-debug/Makefile: + ./scripts/bootstrap-cargo-config + mkdir -p build-factory-setup-debug + cd build-factory-setup-debug && cmake -DCMAKE_TOOLCHAIN_FILE=arm.cmake -DCMAKE_BUILD_TYPE=DEBUG -DFACTORY_SETUP=ON .. + $(MAKE) -C py/bitbox02 + build-btconly/Makefile: ./scripts/bootstrap-cargo-config mkdir -p build-btconly @@ -126,6 +138,12 @@ build: build/Makefile # Directory for building debug build for "host" machine according to gcc convention build-debug: build-debug/Makefile +# Directory for building factory setup firmware +build-factory-setup: build-factory-setup/Makefile + +# Directory for building debug factory setup firmware +build-factory-setup-debug: build-factory-setup-debug/Makefile + # Directory for building BTC-only firmware and bootloaders build-btconly: build-btconly/Makefile @@ -217,10 +235,10 @@ bootloader-plus-btc-development: | build-bootloader-nova-btconly-development bootloader-plus-btc-production: | build-bootloader-nova-btconly-locked $(MAKE) -C build-bootloader-nova-btconly-locked bootloader.elf -factory-setup: | build - $(MAKE) -C build factory-setup.elf -factory-setup-debug: | build-debug - $(MAKE) -C build-debug factory-setup.elf +factory-setup: | build-factory-setup + $(MAKE) -C build-factory-setup firmware.elf +factory-setup-debug: | build-factory-setup-debug + $(MAKE) -C build-factory-setup-debug firmware.elf docs: | build $(MAKE) -C build doc rust-docs: | build @@ -273,8 +291,8 @@ jlink-flash-firmware: | build JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/firmware.jlink jlink-flash-firmware-btc: | build-btconly JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-btconly/scripts/firmware.jlink -jlink-flash-factory-setup: | build - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build/scripts/factory-setup.jlink +jlink-flash-factory-setup: | build-factory-setup + JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-factory-setup/scripts/firmware.jlink jlink-flash-firmware-debug: | build JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-debug/scripts/firmware.jlink jlink-flash-set-new-screen: @@ -300,7 +318,7 @@ run-debug: run-bootloader: arm-none-eabi-gdb -x scripts/jlink-bootloader.gdb build-bootloader-nova-development/bin/bootloader.elf run-factory-setup-debug: - arm-none-eabi-gdb -x scripts/jlink.gdb build-debug/bin/factory-setup.elf + arm-none-eabi-gdb -x scripts/jlink.gdb build-factory-setup-debug/bin/firmware.elf dockerinit: ./scripts/container.sh build --pull -t shiftcrypto/firmware_v2:$(shell cat .containerversion) . dockerpull: @@ -319,7 +337,7 @@ prepare-tidy: | build build-build $(MAKE) -C build rust-cbindgen $(MAKE) -C build-build rust-cbindgen clean: - rm -rf build build-btconly build-nova build-nova-btconly build-bootloader-development build-bootloader-nova-development build-bootloader-btconly-development build-bootloader-nova-btconly-development build-bootloader-locked build-bootloader-nova-locked build-bootloader-btconly-locked build-bootloader-nova-btconly-locked build-bootloader-development-locked build-bootloader-development-debug build-bootloader-nova-development-debug build-build build-debug build-build-noasan src/rust/target + rm -rf build build-btconly build-nova build-nova-btconly build-factory-setup build-factory-setup-debug build-bootloader-development build-bootloader-nova-development build-bootloader-btconly-development build-bootloader-nova-btconly-development build-bootloader-locked build-bootloader-nova-locked build-bootloader-btconly-locked build-bootloader-nova-btconly-locked build-bootloader-development-locked build-bootloader-development-debug build-bootloader-nova-development-debug build-build build-debug build-build-noasan src/rust/target # When you vendor rust libs avoid duplicates vendor-rust-deps: diff --git a/scripts/CMakeLists.txt b/scripts/CMakeLists.txt index e5fa5d0535..1a79fa7561 100644 --- a/scripts/CMakeLists.txt +++ b/scripts/CMakeLists.txt @@ -2,12 +2,10 @@ if(PYTHONINTERP_FOUND) # This template flashes with an offset - foreach(target ${FIRMWARES}) - execute_process( - COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/expand_template ${CMAKE_CURRENT_SOURCE_DIR}/template-firmware.jlink file=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${target}.bin -o ${target}.jlink - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} - ) - endforeach() + execute_process( + COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/expand_template ${CMAKE_CURRENT_SOURCE_DIR}/template-firmware.jlink file=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${FIRMWARE_TARGET}.bin -o ${FIRMWARE_TARGET}.jlink + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + ) # This template flashes without an offset execute_process( diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b358307649..2ce6b33d74 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -289,14 +289,8 @@ endif() # BOOTLOADER_LOCKED and BOOTLOADER_DEVDEVICE are configured in separate build directories. # If a bootloader that locks the bootloader is flashed the bootloader area is permanently read-only. -set(FIRMWARES - ${FIRMWARE_TARGET} - factory-setup # Factory-setup - ) -set(FIRMWARES ${FIRMWARES} PARENT_SCOPE) - if(CMAKE_CROSSCOMPILING) - set(RUST_LIBS ${FIRMWARES} ${BOOTLOADER_TARGET}) + set(RUST_LIBS ${FIRMWARE_TARGET} ${BOOTLOADER_TARGET}) else() set(RUST_LIBS c-unit-tests) endif() @@ -305,7 +299,6 @@ endif() # * c-unit-tests_rust_c # or # * firmware_rust_c -# * factory-setup_rust_c foreach(type ${RUST_LIBS}) # We build the rust standard libs so that everything from the rust runtime # is included (like eh_personality). @@ -315,13 +308,13 @@ foreach(type ${RUST_LIBS}) if(type STREQUAL "c-unit-tests") set(rust_features "app-bitcoin,app-litecoin,app-ethereum,app-cardano,app-u2f,firmware,c-unit-testing") elseif(type STREQUAL "${FIRMWARE_TARGET}") - if(BITBOX02_EDITION STREQUAL "btconly") + if(FACTORY_SETUP) + set(rust_features "factory-setup") + elseif(BITBOX02_EDITION STREQUAL "btconly") set(rust_features "firmware,platform-bitbox02,app-bitcoin") else() set(rust_features "firmware,platform-bitbox02,app-bitcoin,app-litecoin,app-ethereum,app-u2f,app-cardano") endif() - elseif(type STREQUAL "factory-setup") - set(rust_features "factory-setup") elseif(type STREQUAL "${BOOTLOADER_TARGET}") if(BITBOX02_PLATFORM STREQUAL "nova") set(rust_features "bootloader,platform-bitbox02-nova") @@ -350,7 +343,7 @@ foreach(type ${RUST_LIBS}) $<$:-vv> --offline $ENV{CARGOFLAGS} - --features ${rust_features}$<$,$>:,rtt> + --features ${rust_features}$<$,$>:,rtt> --manifest-path ${LIBBITBOX02_RUST_SOURCE_DIR}/Cargo.toml --target-dir ${RUST_BINARY_DIR}/feature-${type} ${RUST_CARGO_FLAGS} @@ -457,83 +450,80 @@ if(CMAKE_CROSSCOMPILING) endif() endif() - foreach(firmware ${FIRMWARES}) - set(elf ${firmware}.elf) - add_executable(${elf} ${FIRMWARE-SOURCES}) - # Static libraries are LTO-capable for firmware size, but only firmware images do the LTO link. - if(firmware STREQUAL "factory-setup") - target_compile_options(${elf} PRIVATE -fno-lto) - target_link_libraries(${elf} PRIVATE -fno-lto) - else() - target_compile_options(${elf} PRIVATE -flto -ffat-lto-objects) - target_link_libraries(${elf} PRIVATE -flto) - endif() - # Must manually link against C so that malloc can find _sbrk - target_link_libraries(${elf} - PRIVATE - optiga - cryptoauthlib - c - ${firmware}_rust_c - samd51a-ds - asf4-drivers-min - asf4-drivers - -Wl,-u,exception_table) - if(firmware STREQUAL "factory-setup" OR CMAKE_BUILD_TYPE STREQUAL "DEBUG") - target_link_libraries(${elf} PRIVATE embedded-swd) - endif() - target_include_directories(${elf} PRIVATE ${INCLUDES}) - - # needed to find version.h - target_include_directories(${elf} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) - target_link_libraries(${elf} PRIVATE "-Wl,-Map=\"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${firmware}.map\" -T\"${CMAKE_SOURCE_DIR}/firmware.ld\"") - target_link_libraries(${elf} PRIVATE -Wl,--defsym=STACK_SIZE=${STACK_SIZE} -Wl,-defsym=HEAP_SIZE=${HEAP_SIZE}) + set(firmware_elf ${FIRMWARE_TARGET}.elf) + add_executable(${firmware_elf} ${FIRMWARE-SOURCES}) + # Static libraries are LTO-capable for firmware size, but only firmware images do the LTO link. + if(FACTORY_SETUP) + target_compile_options(${firmware_elf} PRIVATE -fno-lto) + target_link_libraries(${firmware_elf} PRIVATE -fno-lto) + else() + target_compile_options(${firmware_elf} PRIVATE -flto -ffat-lto-objects) + target_link_libraries(${firmware_elf} PRIVATE -flto) + endif() + # Must manually link against C so that malloc can find _sbrk + target_link_libraries(${firmware_elf} + PRIVATE + optiga + cryptoauthlib + c + ${FIRMWARE_TARGET}_rust_c + samd51a-ds + asf4-drivers-min + asf4-drivers + -Wl,-u,exception_table) + if(FACTORY_SETUP OR CMAKE_BUILD_TYPE STREQUAL "DEBUG") + target_link_libraries(${firmware_elf} PRIVATE embedded-swd) + endif() + target_include_directories(${firmware_elf} PRIVATE ${INCLUDES}) - target_link_libraries(${elf} PRIVATE ${QTOUCHLIB_A} ${QTOUCHLIB_B} ${QTOUCHLIB_T}) + # needed to find version.h + target_include_directories(${firmware_elf} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) + target_link_libraries(${firmware_elf} PRIVATE "-Wl,-Map=\"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${FIRMWARE_TARGET}.map\" -T\"${CMAKE_SOURCE_DIR}/firmware.ld\"") + target_link_libraries(${firmware_elf} PRIVATE -Wl,--defsym=STACK_SIZE=${STACK_SIZE} -Wl,-defsym=HEAP_SIZE=${HEAP_SIZE}) - # Select the smaller version of libc called nano. - target_compile_options(${elf} PRIVATE --specs=nano.specs) - target_link_libraries(${elf} PRIVATE --specs=nano.specs) - target_compile_options(${elf} PRIVATE --specs=nosys.specs) - target_link_libraries(${elf} PRIVATE --specs=nosys.specs) + target_link_libraries(${firmware_elf} PRIVATE ${QTOUCHLIB_A} ${QTOUCHLIB_B} ${QTOUCHLIB_T}) - # Optiga config must be defined both when compiling the optiga lib, and also when compiling our - # sources. We can verify this include is indeed used by our sources - when you point it to a - # non-existing file, compilation fails. - # This definition is also added in external/CMakeLists.txt for the optiga lib. - target_compile_definitions(${elf} PRIVATE OPTIGA_LIB_EXTERNAL="optiga_config.h") - endforeach(firmware) + # Select the smaller version of libc called nano. + target_compile_options(${firmware_elf} PRIVATE --specs=nano.specs) + target_link_libraries(${firmware_elf} PRIVATE --specs=nano.specs) + target_compile_options(${firmware_elf} PRIVATE --specs=nosys.specs) + target_link_libraries(${firmware_elf} PRIVATE --specs=nosys.specs) + + # Optiga config must be defined both when compiling the optiga lib, and also when compiling our + # sources. We can verify this include is indeed used by our sources - when you point it to a + # non-existing file, compilation fails. + # This definition is also added in external/CMakeLists.txt for the optiga lib. + target_compile_definitions(${firmware_elf} PRIVATE OPTIGA_LIB_EXTERNAL="optiga_config.h") + + if(FACTORY_SETUP) + target_sources(${firmware_elf} PRIVATE factorysetup.c) + target_compile_definitions(${firmware_elf} PRIVATE PRODUCT_BITBOX02_FACTORYSETUP "APP_U2F=0") + target_sources(${firmware_elf} PRIVATE ${PLATFORM-BITBOX02-SOURCES}) + else() + target_sources(${firmware_elf} PRIVATE firmware.c) + endif() - target_sources(${FIRMWARE_TARGET}.elf PRIVATE firmware.c) - if(BITBOX02_EDITION STREQUAL "btconly") + if(NOT FACTORY_SETUP AND BITBOX02_EDITION STREQUAL "btconly") target_compile_definitions(${FIRMWARE_TARGET}.elf PRIVATE PRODUCT_BITBOX_BTCONLY "APP_U2F=0") target_sources(${FIRMWARE_TARGET}.elf PRIVATE ${PLATFORM-BITBOX02-SOURCES}) - else() + elseif(NOT FACTORY_SETUP) target_compile_definitions(${FIRMWARE_TARGET}.elf PRIVATE PRODUCT_BITBOX_MULTI "APP_U2F=1") target_sources(${FIRMWARE_TARGET}.elf PRIVATE ${FIRMWARE-U2F-SOURCES} ${FIRMWARE-U2F-DRIVER-SOURCES} ${PLATFORM-BITBOX02-SOURCES}) endif() - target_sources(factory-setup.elf PRIVATE factorysetup.c) - target_compile_definitions(factory-setup.elf PRIVATE PRODUCT_BITBOX02_FACTORYSETUP "APP_U2F=0") - target_sources(factory-setup.elf PRIVATE ${PLATFORM-BITBOX02-SOURCES}) - - # Copy the binary file to the output directory because the symbol names in - # the object file depend on the path to the binary file - add_custom_command( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/bitbox-da14531-firmware.o - COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/bitbox-da14531-firmware.bin ${CMAKE_CURRENT_BINARY_DIR} - COMMAND ${CMAKE_OBJCOPY} -Ibinary -Oelf32-littlearm --rename-section .data=.rodata,alloc,load,readonly,data,contents bitbox-da14531-firmware.bin bitbox-da14531-firmware.o - DEPENDS ${CMAKE_SOURCE_DIR}/bitbox-da14531-firmware.bin - COMMENT "Convert da14531 firmware to object file using ${CMAKE_OBJCOPY}") - - add_custom_target( - da14531-firmware-object - DEPENDS bitbox-da14531-firmware.o - ) - add_dependencies(factory-setup.elf da14531-firmware-object) - - target_link_libraries(factory-setup.elf PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/bitbox-da14531-firmware.o) - + if(FACTORY_SETUP) + # Copy the binary file to the output directory because the symbol names in + # the object file depend on the path to the binary file. + set(DA14531_FIRMWARE_OBJECT ${CMAKE_CURRENT_BINARY_DIR}/bitbox-da14531-firmware.o) + add_custom_command( + OUTPUT ${DA14531_FIRMWARE_OBJECT} + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/bitbox-da14531-firmware.bin ${CMAKE_CURRENT_BINARY_DIR} + COMMAND ${CMAKE_OBJCOPY} -Ibinary -Oelf32-littlearm --rename-section .data=.rodata,alloc,load,readonly,data,contents bitbox-da14531-firmware.bin bitbox-da14531-firmware.o + DEPENDS ${CMAKE_SOURCE_DIR}/bitbox-da14531-firmware.bin + COMMENT "Convert da14531 firmware to object file using ${CMAKE_OBJCOPY}") + set_source_files_properties(${DA14531_FIRMWARE_OBJECT} PROPERTIES GENERATED TRUE EXTERNAL_OBJECT TRUE) + target_sources(${firmware_elf} PRIVATE ${DA14531_FIRMWARE_OBJECT}) + endif() add_custom_command( TARGET ${BOOTLOADER_TARGET}.elf POST_BUILD @@ -549,19 +539,17 @@ if(CMAKE_CROSSCOMPILING) target_link_libraries(${BOOTLOADER_TARGET}.elf PRIVATE -Wl,--print-memory-usage) - foreach(name ${FIRMWARES}) - add_custom_command( - TARGET ${name}.elf POST_BUILD - COMMAND ${CMAKE_SIZE} ${name}.elf - COMMAND ${CMAKE_OBJCOPY} -O binary ${name}.elf ${name}.bin - WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} - COMMENT "\nGenerating binary ${name}.bin" - ) - # Fail on warnings for our release builds - if(NOT CMAKE_BUILD_TYPE STREQUAL "DEBUG") - target_compile_options(${name}.elf PRIVATE "-Werror") - endif() + add_custom_command( + TARGET ${FIRMWARE_TARGET}.elf POST_BUILD + COMMAND ${CMAKE_SIZE} ${FIRMWARE_TARGET}.elf + COMMAND ${CMAKE_OBJCOPY} -O binary ${FIRMWARE_TARGET}.elf ${FIRMWARE_TARGET}.bin + WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} + COMMENT "\nGenerating binary ${FIRMWARE_TARGET}.bin" + ) + # Fail on warnings for our release builds + if(NOT CMAKE_BUILD_TYPE STREQUAL "DEBUG") + target_compile_options(${FIRMWARE_TARGET}.elf PRIVATE "-Werror") + endif() - target_link_libraries(${name}.elf PRIVATE -Wl,--print-memory-usage) - endforeach() + target_link_libraries(${FIRMWARE_TARGET}.elf PRIVATE -Wl,--print-memory-usage) endif() From 6599535b8feffbfd47d7923760e44818b104fe16 Mon Sep 17 00:00:00 2001 From: Niklas Dusenlund Date: Fri, 12 Jun 2026 16:59:31 +0200 Subject: [PATCH 7/9] cmake/rust: simplify target selection --- src/CMakeLists.txt | 62 ++++++++++------------------- src/rust/bitbox02-rust-c/Cargo.toml | 14 +++++-- src/rust/bitbox02-rust-c/src/lib.rs | 6 +-- 3 files changed, 36 insertions(+), 46 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2ce6b33d74..774403ca0b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -286,47 +286,17 @@ if(NOT CMAKE_CROSSCOMPILING) add_dependencies(rust-clippy rust-cbindgen) endif() -# BOOTLOADER_LOCKED and BOOTLOADER_DEVDEVICE are configured in separate build directories. -# If a bootloader that locks the bootloader is flashed the bootloader area is permanently read-only. - -if(CMAKE_CROSSCOMPILING) - set(RUST_LIBS ${FIRMWARE_TARGET} ${BOOTLOADER_TARGET}) -else() - set(RUST_LIBS c-unit-tests) -endif() - -# Create static libraries from rust code. Either: -# * c-unit-tests_rust_c -# or -# * firmware_rust_c -foreach(type ${RUST_LIBS}) +function(add_rust_static_library type rust_features) + set(rust_cargo_flags ${RUST_CARGO_FLAGS}) # We build the rust standard libs so that everything from the rust runtime # is included (like eh_personality). if(type STREQUAL "c-unit-tests") - set(RUST_CARGO_FLAGS ${RUST_CARGO_FLAGS} -Zbuild-std=std,panic_abort) - endif() - if(type STREQUAL "c-unit-tests") - set(rust_features "app-bitcoin,app-litecoin,app-ethereum,app-cardano,app-u2f,firmware,c-unit-testing") - elseif(type STREQUAL "${FIRMWARE_TARGET}") - if(FACTORY_SETUP) - set(rust_features "factory-setup") - elseif(BITBOX02_EDITION STREQUAL "btconly") - set(rust_features "firmware,platform-bitbox02,app-bitcoin") - else() - set(rust_features "firmware,platform-bitbox02,app-bitcoin,app-litecoin,app-ethereum,app-u2f,app-cardano") - endif() - elseif(type STREQUAL "${BOOTLOADER_TARGET}") - if(BITBOX02_PLATFORM STREQUAL "nova") - set(rust_features "bootloader,platform-bitbox02-nova") - else() - set(rust_features "bootloader,platform-bitbox02") - endif() - else() - message(FATAL_ERROR "No Rust features configured for ${type}") + list(APPEND rust_cargo_flags -Zbuild-std=std,panic_abort) endif() + set(lib ${RUST_BINARY_DIR}/feature-${type}/${RUST_TARGET_ARCH_DIR}/${RUST_PROFILE}/libbitbox02_rust_c.a) # The dummy output is to always trigger rebuild (cargo is clever enough to - # only rebuild if something changed) + # only rebuild if something changed). add_custom_command( OUTPUT ${lib} ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/lib${type}_rust_c.a dummy COMMAND @@ -346,12 +316,12 @@ foreach(type ${RUST_LIBS}) --features ${rust_features}$<$,$>:,rtt> --manifest-path ${LIBBITBOX02_RUST_SOURCE_DIR}/Cargo.toml --target-dir ${RUST_BINARY_DIR}/feature-${type} - ${RUST_CARGO_FLAGS} + ${rust_cargo_flags} ${RUST_TARGET_ARCH_ARG} --crate-type=staticlib COMMAND ${CMAKE_COMMAND} -E copy_if_different ${lib} ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/lib${type}_rust_c.a - # DEPFILES are only supported with the Ninja build tool + # DEPFILES are only supported with the Ninja build tool. #DEPFILE ${RUST_BINARY_DIR}/feature-${type}/${RUST_TARGET_ARCH_DIR}/${RUST_PROFILE}/libbitbox02_rust_c.d WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/rust @@ -362,10 +332,10 @@ foreach(type ${RUST_LIBS}) add_library(${type}_rust_c STATIC IMPORTED GLOBAL) add_dependencies(${type}_rust_c ${type}-rust-target) set_property(TARGET ${type}_rust_c PROPERTY IMPORTED_LOCATION ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/lib${type}_rust_c.a) - # Add the include directory to find rust/rust.h + # Add the include directory to find rust/rust.h. target_include_directories(${type}_rust_c INTERFACE ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/rust/fatfs-sys/depend/fatfs/source) if (NOT CMAKE_CROSSCOMPILING) - # Needs to match the definitions in `bitbox02-sys/build.rs` + # Needs to match the definitions in `bitbox02-sys/build.rs`. target_compile_definitions(${type}_rust_c INTERFACE TESTING _UNIT_TEST_ @@ -378,7 +348,19 @@ foreach(type ${RUST_LIBS}) ${CMAKE_CURRENT_SOURCE_DIR}/../test/hardware-fakes/include ) endif() -endforeach() +endfunction() + +if(CMAKE_CROSSCOMPILING) + if(FACTORY_SETUP) + add_rust_static_library(${FIRMWARE_TARGET} "factory-setup,bitbox02-platform-${BITBOX02_PLATFORM}") + else() + add_rust_static_library(${FIRMWARE_TARGET} "firmware,bitbox02-platform-${BITBOX02_PLATFORM},bitbox02-edition-${BITBOX02_EDITION}") + endif() + + add_rust_static_library(${BOOTLOADER_TARGET} "bootloader,bitbox02-platform-${BITBOX02_PLATFORM}") +else() + add_rust_static_library(c-unit-tests "bitbox02-edition-multi,firmware,c-unit-testing") +endif() if(CMAKE_CROSSCOMPILING) diff --git a/src/rust/bitbox02-rust-c/Cargo.toml b/src/rust/bitbox02-rust-c/Cargo.toml index 0c60b25cc8..2810e3bf9a 100644 --- a/src/rust/bitbox02-rust-c/Cargo.toml +++ b/src/rust/bitbox02-rust-c/Cargo.toml @@ -29,8 +29,17 @@ bip39 = { workspace = true } zeroize = { workspace = true } [features] -platform-bitbox02 = [] -platform-bitbox02-nova = ["util/sha2", "bitbox-noise", "bitbox02-rust"] +bitbox02-platform-classic = [] +bitbox02-platform-nova = ["util/sha2", "bitbox-noise", "bitbox02-rust"] + +bitbox02-edition-btconly = ["app-bitcoin"] +bitbox02-edition-multi = [ + "app-bitcoin", + "app-litecoin", + "app-ethereum", + "app-u2f", + "app-cardano", +] bootloader = [] firmware = [ @@ -85,7 +94,6 @@ app-cardano = [ factory-setup = [ "firmware", - "platform-bitbox02", "dep:bitcoin", "bitbox02/factory-setup", ] diff --git a/src/rust/bitbox02-rust-c/src/lib.rs b/src/rust/bitbox02-rust-c/src/lib.rs index b7a2d5f557..dba8765720 100644 --- a/src/rust/bitbox02-rust-c/src/lib.rs +++ b/src/rust/bitbox02-rust-c/src/lib.rs @@ -14,7 +14,7 @@ extern crate alloc; pub mod async_usb; #[cfg(any( feature = "firmware", - all(feature = "bootloader", feature = "platform-bitbox02-nova") + all(feature = "bootloader", feature = "bitbox02-platform-nova") ))] mod communication_mode; #[cfg(feature = "firmware")] @@ -40,7 +40,7 @@ extern crate bitbox_aes; // Enable for firmware and for Nova bootloader (BitBox02 bootloader currently does not need it). #[cfg(any( feature = "firmware", - all(feature = "bootloader", feature = "platform-bitbox02-nova") + all(feature = "bootloader", feature = "bitbox02-platform-nova") ))] extern crate bitbox02_rust; @@ -61,7 +61,7 @@ extern crate util; #[allow(unused)] #[cfg(any( feature = "firmware", - all(feature = "bootloader", feature = "platform-bitbox02-nova") + all(feature = "bootloader", feature = "bitbox02-platform-nova") ))] type HalImpl = bitbox02::hal::BitBox02Hal; From e247bb465ad9b6ddfe3988f1636a19b46ac5340c Mon Sep 17 00:00:00 2001 From: Niklas Dusenlund Date: Fri, 12 Jun 2026 17:02:47 +0200 Subject: [PATCH 8/9] cmake: simplify jlink script --- scripts/CMakeLists.txt | 6 ++---- scripts/template-firmware.jlink | 3 --- scripts/{template-bootloader.jlink => template.jlink} | 0 3 files changed, 2 insertions(+), 7 deletions(-) delete mode 100644 scripts/template-firmware.jlink rename scripts/{template-bootloader.jlink => template.jlink} (100%) diff --git a/scripts/CMakeLists.txt b/scripts/CMakeLists.txt index 1a79fa7561..91c53d29d3 100644 --- a/scripts/CMakeLists.txt +++ b/scripts/CMakeLists.txt @@ -1,15 +1,13 @@ # SPDX-License-Identifier: Apache-2.0 if(PYTHONINTERP_FOUND) - # This template flashes with an offset execute_process( - COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/expand_template ${CMAKE_CURRENT_SOURCE_DIR}/template-firmware.jlink file=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${FIRMWARE_TARGET}.bin -o ${FIRMWARE_TARGET}.jlink + COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/expand_template ${CMAKE_CURRENT_SOURCE_DIR}/template.jlink file=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${FIRMWARE_TARGET}.elf -o ${FIRMWARE_TARGET}.jlink WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} ) - # This template flashes without an offset execute_process( - COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/expand_template ${CMAKE_CURRENT_SOURCE_DIR}/template-bootloader.jlink file=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${BOOTLOADER_TARGET}.bin -o ${BOOTLOADER_TARGET}.jlink + COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/expand_template ${CMAKE_CURRENT_SOURCE_DIR}/template.jlink file=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${BOOTLOADER_TARGET}.elf -o ${BOOTLOADER_TARGET}.jlink WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} ) endif() diff --git a/scripts/template-firmware.jlink b/scripts/template-firmware.jlink deleted file mode 100644 index b21f83548a..0000000000 --- a/scripts/template-firmware.jlink +++ /dev/null @@ -1,3 +0,0 @@ -loadbin $file 0x10000 -r -q diff --git a/scripts/template-bootloader.jlink b/scripts/template.jlink similarity index 100% rename from scripts/template-bootloader.jlink rename to scripts/template.jlink From 217b02cc3f8d72f7ad807a0e235dab399c6c970f Mon Sep 17 00:00:00 2001 From: Niklas Dusenlund Date: Fri, 12 Jun 2026 17:07:20 +0200 Subject: [PATCH 9/9] cmake: remove ununsed alias --- Makefile | 2 -- 1 file changed, 2 deletions(-) diff --git a/Makefile b/Makefile index dcb454258d..3f72c9da1a 100644 --- a/Makefile +++ b/Makefile @@ -293,8 +293,6 @@ jlink-flash-firmware-btc: | build-btconly JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-btconly/scripts/firmware.jlink jlink-flash-factory-setup: | build-factory-setup JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-factory-setup/scripts/firmware.jlink -jlink-flash-firmware-debug: | build - JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./build-debug/scripts/firmware.jlink jlink-flash-set-new-screen: JLinkExe -NoGui 1 -if SWD -device ATSAMD51J20 -speed 4000 -autoconnect 1 -CommanderScript ./scripts/set-new-screen.jlink jlink-flash-set-original-screen: