From 006f79660073cce2dccf0477440c2d61c62c7dc2 Mon Sep 17 00:00:00 2001 From: matejcik Date: Mon, 30 Sep 2024 15:09:48 +0200 Subject: [PATCH 1/5] build(core): introduce common definition of all makefile vars that get passed as scons vars so that we don't have to write "USE_FOO=$(USE_FOO)" 20 times every time we add such flag --- core/Makefile | 71 +++++++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 39 deletions(-) diff --git a/core/Makefile b/core/Makefile index c63a07a93f1..7d9e30d1e79 100644 --- a/core/Makefile +++ b/core/Makefile @@ -9,7 +9,6 @@ JOBS = $(shell sysctl -n hw.perflevel0.physicalcpu 2>/dev/null ||\ nproc 2>/dev/null ||\ echo 4) MAKE = make -j $(JOBS) -SCONS = scons -Q -j $(JOBS) BUILD_DIR = build BOARDLOADER_BUILD_DIR = $(BUILD_DIR)/boardloader @@ -40,6 +39,7 @@ PYTEST_TIMEOUT ?= 500 TEST_LANG ?= "en" NEW_RENDERING ?= 1 THP ?= 0 +TREZOR_EMULATOR_DEBUGGABLE ?= 0 # OpenOCD interface default. Alternative: ftdi/olimex-arm-usb-tiny-h OPENOCD_INTERFACE ?= stlink @@ -124,6 +124,24 @@ RUST_TARGET=$(shell rustc -vV | sed -n 's/host: //p') MULTICORE ?= "auto" +SCONS_VARS = \ + BITCOIN_ONLY="$(BITCOIN_ONLY)" \ + BOOTLOADER_DEVEL="$(BOOTLOADER_DEVEL)" \ + BOOTLOADER_QA="$(BOOTLOADER_QA)" \ + CFLAGS="$(CFLAGS)" \ + CMAKELISTS="$(CMAKELISTS)" \ + DISABLE_OPTIGA="$(DISABLE_OPTIGA)" \ + NEW_RENDERING="$(NEW_RENDERING)" \ + PRODUCTION="$(PRODUCTION)" \ + PYOPT="$(PYOPT)" \ + THP="$(THP)" \ + TREZOR_EMULATOR_ASAN="$(ADDRESS_SANITIZER)" \ + TREZOR_EMULATOR_DEBUGGABLE=$(TREZOR_EMULATOR_DEBUGGABLE) \ + TREZOR_MEMPERF="$(TREZOR_MEMPERF)" \ + TREZOR_MODEL="$(TREZOR_MODEL)" + +SCONS = scons -Q -j $(JOBS) $(SCONS_VARS) + ## help commands: help: ## show this help @@ -244,68 +262,43 @@ build: build_boardloader build_bootloader build_firmware build_prodtest build_un build_embed: build_boardloader build_bootloader build_firmware # build boardloader, bootloader, firmware build_boardloader: ## build boardloader - $(SCONS) CFLAGS="$(CFLAGS)" PRODUCTION="$(PRODUCTION)" TREZOR_MODEL="$(TREZOR_MODEL)" \ - CMAKELISTS="$(CMAKELISTS)" NEW_RENDERING="$(NEW_RENDERING)" $(BOARDLOADER_BUILD_DIR)/boardloader.bin + $(SCONS) $(BOARDLOADER_BUILD_DIR)/boardloader.bin build_bootloader: ## build bootloader - $(SCONS) CFLAGS="$(CFLAGS)" PRODUCTION="$(PRODUCTION)" TREZOR_MODEL="$(TREZOR_MODEL)" \ - CMAKELISTS="$(CMAKELISTS)" BOOTLOADER_QA="$(BOOTLOADER_QA)" BOOTLOADER_DEVEL="$(BOOTLOADER_DEVEL)" \ - NEW_RENDERING="$(NEW_RENDERING)" $(BOOTLOADER_BUILD_DIR)/bootloader.bin + $(SCONS) $(BOOTLOADER_BUILD_DIR)/bootloader.bin build_bootloader_ci: ## build CI device testing bootloader - $(SCONS) CFLAGS="$(CFLAGS)" PRODUCTION="$(PRODUCTION)" TREZOR_MODEL="$(TREZOR_MODEL)" \ - CMAKELISTS="$(CMAKELISTS)" $(BOOTLOADER_CI_BUILD_DIR)/bootloader.bin + $(SCONS) $(BOOTLOADER_CI_BUILD_DIR)/bootloader.bin build_bootloader_emu: ## build the unix bootloader emulator - $(SCONS) CFLAGS="$(CFLAGS)" PRODUCTION="$(PRODUCTION)" TREZOR_MODEL="$(TREZOR_MODEL)" \ - CMAKELISTS="$(CMAKELISTS)" NEW_RENDERING="$(NEW_RENDERING)" $(BOOTLOADER_EMU_BUILD_DIR)/bootloader.elf + $(SCONS) $(BOOTLOADER_EMU_BUILD_DIR)/bootloader.elf build_bootloader_emu_debug: ## build the unix bootloader emulator - $(SCONS) CFLAGS="$(CFLAGS)" PRODUCTION="$(PRODUCTION)" TREZOR_MODEL="$(TREZOR_MODEL)" \ - CMAKELISTS="$(CMAKELISTS)" NEW_RENDERING="$(NEW_RENDERING)" TREZOR_EMULATOR_DEBUGGABLE=1 \ - $(BOOTLOADER_EMU_BUILD_DIR)/bootloader.elf + $(SCONS) TREZOR_EMULATOR_DEBUGGABLE=1 $(BOOTLOADER_EMU_BUILD_DIR)/bootloader.elf build_prodtest: ## build production test firmware - $(SCONS) CFLAGS="$(CFLAGS)" PRODUCTION="$(PRODUCTION)" TREZOR_MODEL="$(TREZOR_MODEL)" \ - CMAKELISTS="$(CMAKELISTS)" BOOTLOADER_DEVEL="$(BOOTLOADER_DEVEL)" $(PRODTEST_BUILD_DIR)/prodtest.bin + $(SCONS) $(PRODTEST_BUILD_DIR)/prodtest.bin build_reflash: ## build reflash firmware + reflash image - $(SCONS) CFLAGS="$(CFLAGS)" PRODUCTION="$(PRODUCTION)" TREZOR_MODEL="$(TREZOR_MODEL)" \ - CMAKELISTS="$(CMAKELISTS)" NEW_RENDERING="$(NEW_RENDERING)" $(REFLASH_BUILD_DIR)/reflash.bin + $(SCONS) $(REFLASH_BUILD_DIR)/reflash.bin dd if=build/boardloader/boardloader.bin of=$(REFLASH_BUILD_DIR)/sdimage.bin bs=1 seek=0 dd if=build/bootloader/bootloader.bin of=$(REFLASH_BUILD_DIR)/sdimage.bin bs=1 seek=49152 build_kernel: ## build kernel image - $(SCONS) CFLAGS="$(CFLAGS)" PRODUCTION="$(PRODUCTION)" \ - TREZOR_MODEL="$(TREZOR_MODEL)" CMAKELISTS="$(CMAKELISTS)" \ - BOOTLOADER_QA="$(BOOTLOADER_QA)" BOOTLOADER_DEVEL="$(BOOTLOADER_DEVEL)" \ - $(KERNEL_BUILD_DIR)/kernel.bin + $(SCONS) $(KERNEL_BUILD_DIR)/kernel.bin build_firmware: templates build_cross build_kernel ## build firmware with frozen modules - $(SCONS) CFLAGS="$(CFLAGS)" PRODUCTION="$(PRODUCTION)" \ - TREZOR_MODEL="$(TREZOR_MODEL)" CMAKELISTS="$(CMAKELISTS)" \ - PYOPT="$(PYOPT)" BITCOIN_ONLY="$(BITCOIN_ONLY)" \ - BOOTLOADER_QA="$(BOOTLOADER_QA)" BOOTLOADER_DEVEL="$(BOOTLOADER_DEVEL)" \ - DISABLE_OPTIGA="$(DISABLE_OPTIGA)" THP="$(THP)" NEW_RENDERING="$(NEW_RENDERING)" \ - $(FIRMWARE_BUILD_DIR)/firmware.bin + $(SCONS) $(FIRMWARE_BUILD_DIR)/firmware.bin build_unix: templates ## build unix port - $(SCONS) CFLAGS="$(CFLAGS)" $(UNIX_BUILD_DIR)/trezor-emu-core $(UNIX_PORT_OPTS) \ - TREZOR_MODEL="$(TREZOR_MODEL)" CMAKELISTS="$(CMAKELISTS)" THP="$(THP)" \ - PYOPT="0" BITCOIN_ONLY="$(BITCOIN_ONLY)" TREZOR_EMULATOR_ASAN="$(ADDRESS_SANITIZER)" \ - NEW_RENDERING="$(NEW_RENDERING)" + $(SCONS) $(UNIX_BUILD_DIR)/trezor-emu-core $(UNIX_PORT_OPTS) build_unix_frozen: templates build_cross ## build unix port with frozen modules - $(SCONS) CFLAGS="$(CFLAGS)" $(UNIX_BUILD_DIR)/trezor-emu-core $(UNIX_PORT_OPTS) \ - TREZOR_MODEL="$(TREZOR_MODEL)" CMAKELISTS="$(CMAKELISTS)" \ - PYOPT="$(PYOPT)" BITCOIN_ONLY="$(BITCOIN_ONLY)" TREZOR_EMULATOR_ASAN="$(ADDRESS_SANITIZER)" \ - TREZOR_MEMPERF="$(TREZOR_MEMPERF)" TREZOR_EMULATOR_FROZEN=1 NEW_RENDERING="$(NEW_RENDERING)" + $(SCONS) $(UNIX_BUILD_DIR)/trezor-emu-core $(UNIX_PORT_OPTS) TREZOR_EMULATOR_FROZEN=1 build_unix_debug: templates ## build unix port - $(SCONS) --max-drift=1 CFLAGS="$(CFLAGS)" $(UNIX_BUILD_DIR)/trezor-emu-core $(UNIX_PORT_OPTS) \ - TREZOR_MODEL="$(TREZOR_MODEL)" CMAKELISTS="$(CMAKELISTS)" \ - BITCOIN_ONLY="$(BITCOIN_ONLY)" TREZOR_EMULATOR_ASAN=1 TREZOR_EMULATOR_DEBUGGABLE=1 \ - NEW_RENDERING="$(NEW_RENDERING)" + $(SCONS) --max-drift=1 $(UNIX_BUILD_DIR)/trezor-emu-core $(UNIX_PORT_OPTS) \ + TREZOR_EMULATOR_ASAN=1 TREZOR_EMULATOR_DEBUGGABLE=1 build_cross: ## build mpy-cross port $(MAKE) -C vendor/micropython/mpy-cross $(CROSS_PORT_OPTS) From 52a599fd4ea60f27d790657e01995db6d9e40e25 Mon Sep 17 00:00:00 2001 From: matejcik Date: Tue, 1 Oct 2024 11:32:54 +0200 Subject: [PATCH 2/5] build(core): improve CPPDEFINES quoting Here we change all FOO=VALUE defines to be tuples ("FOO", "VALUE"). Also, VALUE is always the raw string you want to end up in the C file, instead of attempting to shell-escape it while specifying. By all rights scons _should_ be using shlex.quote() on the CPPDEFINES, but it doesn't, so we hack it by specifying the define prefix as `-D'` and suffix as `'`. That way the arguments in shell are '-escaped, and we're (currently) not using ' in any argument value so this should work fine. At the same time, when passing the flags to cargo, we can shlex.quote the whole thing and get the right strings passed all the way into build.rs -- as long as no argument contains a comma, which is the split character... --- core/SConscript.boardloader | 10 +++-- core/SConscript.bootloader | 7 +++- core/SConscript.bootloader_ci | 9 ++-- core/SConscript.bootloader_emu | 10 ++++- core/SConscript.firmware | 7 +++- core/SConscript.kernel | 6 ++- core/SConscript.prodtest | 5 ++- core/SConscript.reflash | 6 ++- core/SConscript.unix | 14 +++++-- core/site_scons/models/D001/discovery.py | 6 +-- core/site_scons/models/D002/discovery2.py | 12 ++---- core/site_scons/models/T2B1/emulator.py | 16 +++---- core/site_scons/models/T2B1/trezor_r_v10.py | 12 +++--- core/site_scons/models/T2B1/trezor_r_v3.py | 10 +++-- core/site_scons/models/T2B1/trezor_r_v4.py | 10 +++-- core/site_scons/models/T2B1/trezor_r_v6.py | 10 +++-- core/site_scons/models/T2T1/emulator.py | 16 +++---- core/site_scons/models/T2T1/trezor_t.py | 42 +++++++------------ core/site_scons/models/T3B1/emulator.py | 16 +++---- .../models/T3B1/trezor_t3b1_revB.py | 14 ++++--- core/site_scons/models/T3T1/emulator.py | 22 +++++----- .../models/T3T1/trezor_t3t1_revE.py | 24 +++++------ core/site_scons/models/T3T1/trezor_t3t1_v4.py | 24 +++++------ core/site_scons/models/stm32f4_common.py | 2 +- core/site_scons/models/stm32u5_common.py | 4 +- core/site_scons/tools.py | 10 +---- core/site_scons/ui/common.py | 9 ++-- 27 files changed, 174 insertions(+), 159 deletions(-) diff --git a/core/SConscript.boardloader b/core/SConscript.boardloader index 3e2511f47b8..a700d40207a 100644 --- a/core/SConscript.boardloader +++ b/core/SConscript.boardloader @@ -84,10 +84,12 @@ else: env = Environment(ENV=os.environ, - CFLAGS='%s -DPRODUCTION=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0')), - CONSTRAINTS=["limited_util_s"], - CPPDEFINES_IMPLICIT=[] - ) + CFLAGS='%s -DPRODUCTION=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0')), + CONSTRAINTS=["limited_util_s"], + CPPDEFINES_IMPLICIT=[], + CPPDEFPREFIX="-D'", + CPPDEFSUFFIX="'", +) FEATURES_AVAILABLE = models.configure_board(TREZOR_MODEL, HW_REVISION, FEATURES_WANTED, env, CPPDEFINES_HAL, SOURCE_HAL, PATH_HAL) diff --git a/core/SConscript.bootloader b/core/SConscript.bootloader index 0c05e7838de..d9c0262b961 100644 --- a/core/SConscript.bootloader +++ b/core/SConscript.bootloader @@ -1,6 +1,7 @@ # pylint: disable=E0602 import os +import shlex import tools, models, ui TREZOR_MODEL = ARGUMENTS.get('TREZOR_MODEL', 'T') @@ -109,7 +110,9 @@ ui.init_ui(TREZOR_MODEL, "bootloader", CPPDEFINES_MOD, SOURCE_MOD, RUST_UI_FEATU env = Environment( ENV=os.environ, CFLAGS=f"{ARGUMENTS.get('CFLAGS', '')} -DPRODUCTION={int(PRODUCTION)} -DBOOTLOADER_QA={int(BOOTLOADER_QA)}", - CPPDEFINES_IMPLICIT=[] + CPPDEFINES_IMPLICIT=[], + CPPDEFPREFIX="-D'", + CPPDEFSUFFIX="'", ) FEATURES_AVAILABLE = models.configure_board(TREZOR_MODEL, HW_REVISION, FEATURES_WANTED, env, CPPDEFINES_HAL, SOURCE_HAL, PATH_HAL) @@ -232,7 +235,7 @@ def cargo_build(): bindgen_macros = tools.get_bindgen_defines(env.get("CPPDEFINES"), ALLPATHS) build_dir = str(Dir('.').abspath) - return f'export BINDGEN_MACROS=\'{bindgen_macros}\'; export BUILD_DIR=\'{build_dir}\'; cd embed/rust; cargo build {profile} ' + ' '.join(cargo_opts) + return f'export BINDGEN_MACROS={shlex.quote(bindgen_macros)}; export BUILD_DIR=\'{build_dir}\'; cd embed/rust; cargo build {profile} ' + ' '.join(cargo_opts) rust = env.Command( target=RUST_LIBPATH, diff --git a/core/SConscript.bootloader_ci b/core/SConscript.bootloader_ci index 3e11207dcda..58eadb5b62c 100644 --- a/core/SConscript.bootloader_ci +++ b/core/SConscript.bootloader_ci @@ -101,9 +101,12 @@ SOURCE_NANOPB = [ ui.init_ui(TREZOR_MODEL, "bootloader", CPPDEFINES_MOD, SOURCE_MOD, RUST_UI_FEATURES) env = Environment( - ENV=os.environ, CFLAGS='%s -DPRODUCTION=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0')), - CPPDEFINES_IMPLICIT=[] - ) + ENV=os.environ, + CFLAGS='%s -DPRODUCTION=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0')), + CPPDEFINES_IMPLICIT=[], + CPPDEFPREFIX="-D'", + CPPDEFSUFFIX="'", +) FEATURES_AVAILABLE = models.configure_board(TREZOR_MODEL, HW_REVISION, FEATURES_WANTED, env, CPPDEFINES_HAL, SOURCE_HAL, PATH_HAL) diff --git a/core/SConscript.bootloader_emu b/core/SConscript.bootloader_emu index f2c3d770fd2..c8886df84b4 100644 --- a/core/SConscript.bootloader_emu +++ b/core/SConscript.bootloader_emu @@ -1,6 +1,7 @@ # pylint: disable=E0602 import os +import shlex import tools, models, ui TREZOR_MODEL = ARGUMENTS.get('TREZOR_MODEL', 'T') @@ -151,7 +152,12 @@ SOURCE_UNIX = [ ui.init_ui(TREZOR_MODEL, "bootloader", CPPDEFINES_MOD, SOURCE_MOD, RUST_UI_FEATURES) -env = Environment(ENV=os.environ, CFLAGS='%s -DCONFIDENTIAL= -DPRODUCTION=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0'))) +env = Environment( + ENV=os.environ, + CFLAGS=ARGUMENTS.get('CFLAGS', '') + f" -DCONFIDENTIAL= -DPRODUCTION={ARGUMENTS.get('PRODUCTION', '0')}", + CPPDEFPREFIX="-D'", + CPPDEFSUFFIX="'", +) FEATURES_AVAILABLE = models.configure_board(TREZOR_MODEL, HW_REVISION, FEATURES_WANTED, env, CPPDEFINES_HAL, SOURCE_UNIX, PATH_HAL) @@ -261,7 +267,7 @@ def cargo_build(): bindgen_macros = tools.get_bindgen_defines(env.get("CPPDEFINES"), ALLPATHS) build_dir = str(Dir('.').abspath) - return f'export BINDGEN_MACROS=\'{bindgen_macros}\'; export BUILD_DIR=\'{build_dir}\'; cd embed/rust; cargo build --profile {RUST_PROFILE} ' + ' '.join(cargo_opts) + return f'export BINDGEN_MACROS={shlex.quote(bindgen_macros)}; export BUILD_DIR=\'{build_dir}\'; cd embed/rust; cargo build --profile {RUST_PROFILE} ' + ' '.join(cargo_opts) rust = env.Command( target=RUST_LIBPATH, diff --git a/core/SConscript.firmware b/core/SConscript.firmware index 63744c2bdc4..c6e74884188 100644 --- a/core/SConscript.firmware +++ b/core/SConscript.firmware @@ -2,6 +2,7 @@ # fmt: off import os +import shlex import tools, models, ui BITCOIN_ONLY = ARGUMENTS.get('BITCOIN_ONLY', '0') @@ -383,7 +384,9 @@ SOURCE_QSTR = SOURCE_MOD + SOURCE_MICROPYTHON + SOURCE_MICROPYTHON_SPEED env = Environment( ENV=os.environ, CFLAGS=f"{ARGUMENTS.get('CFLAGS', '')} -DPRODUCTION={int(PRODUCTION)} -DPYOPT={PYOPT} -DBOOTLOADER_QA={int(BOOTLOADER_QA)} -DBITCOIN_ONLY={BITCOIN_ONLY}", - CPPDEFINES_IMPLICIT=[] + CPPDEFINES_IMPLICIT=[], + CPPDEFPREFIX="-D'", + CPPDEFSUFFIX="'", ) FEATURES_AVAILABLE = models.configure_board(TREZOR_MODEL, HW_REVISION, FEATURES_WANTED, env, CPPDEFINES_HAL, SOURCE_HAL, PATH_HAL) @@ -766,7 +769,7 @@ def cargo_build(): bindgen_macros = tools.get_bindgen_defines(env.get("CPPDEFINES"), ALLPATHS) build_dir = str(Dir('.').abspath) - return f'export BINDGEN_MACROS=\'{bindgen_macros}\'; export BUILD_DIR=\'{build_dir}\'; cd embed/rust; cargo build {profile} ' + ' '.join(cargo_opts) + return f'export BINDGEN_MACROS={shlex.quote(bindgen_macros)}; export BUILD_DIR=\'{build_dir}\'; cd embed/rust; cargo build {profile} ' + ' '.join(cargo_opts) rust = env.Command( target=RUST_LIBPATH, diff --git a/core/SConscript.kernel b/core/SConscript.kernel index 61598225ee7..a761836beed 100644 --- a/core/SConscript.kernel +++ b/core/SConscript.kernel @@ -262,8 +262,10 @@ if THP: env = Environment( ENV=os.environ, CFLAGS=f"{ARGUMENTS.get('CFLAGS', '')} -DPRODUCTION={int(PRODUCTION)} -DPYOPT={PYOPT} -DBOOTLOADER_QA={int(BOOTLOADER_QA)} -DBITCOIN_ONLY={BITCOIN_ONLY}", - CPPDEFINES_IMPLICIT=[] - ) + CPPDEFINES_IMPLICIT=[], + CPPDEFPREFIX="-D'", + CPPDEFSUFFIX="'", +) FEATURES_AVAILABLE = models.configure_board(TREZOR_MODEL, HW_REVISION, FEATURES_WANTED, env, CPPDEFINES_HAL, SOURCE_HAL, PATH_HAL) diff --git a/core/SConscript.prodtest b/core/SConscript.prodtest index 0fafdc09286..a5c609d7add 100644 --- a/core/SConscript.prodtest +++ b/core/SConscript.prodtest @@ -101,7 +101,10 @@ ui.init_ui(TREZOR_MODEL, "prodtest", CPPDEFINES_MOD, SOURCE_MOD, RUST_UI_FEATURE env = Environment( ENV=os.environ, CFLAGS='%s -DPRODUCTION=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0')), - CPPDEFINES_IMPLICIT=[]) + CPPDEFINES_IMPLICIT=[], + CPPDEFPREFIX="-D'", + CPPDEFSUFFIX="'", +) FEATURES_AVAILABLE = models.configure_board(TREZOR_MODEL, HW_REVISION, FEATURES_WANTED, env, CPPDEFINES_HAL, SOURCE_HAL, PATH_HAL) diff --git a/core/SConscript.reflash b/core/SConscript.reflash index 1309f58d443..f4aad6f0612 100644 --- a/core/SConscript.reflash +++ b/core/SConscript.reflash @@ -87,8 +87,10 @@ ui.init_ui(TREZOR_MODEL, "prodtest", CPPDEFINES_MOD, SOURCE_MOD, RUST_UI_FEATURE env = Environment( ENV=os.environ, CFLAGS='%s -DPRODUCTION=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0')), - CPPDEFINES_IMPLICIT=[] - ) + CPPDEFINES_IMPLICIT=[], + CPPDEFPREFIX="-D'", + CPPDEFSUFFIX="'", +) FEATURES_AVAILABLE = models.configure_board(TREZOR_MODEL, HW_REVISION, FEATURES_WANTED, env, CPPDEFINES_HAL, SOURCE_HAL, PATH_HAL) diff --git a/core/SConscript.unix b/core/SConscript.unix index c23bad1fdb7..2a70f2f60ea 100644 --- a/core/SConscript.unix +++ b/core/SConscript.unix @@ -2,6 +2,7 @@ # fmt: off import os +import shlex import tools, models, ui BITCOIN_ONLY = ARGUMENTS.get('BITCOIN_ONLY', '0') @@ -437,7 +438,12 @@ if PYOPT == '0' or not FROZEN: else: STATIC="" -env = Environment(ENV=os.environ, CFLAGS='%s -DCONFIDENTIAL= -DPYOPT=%s -DBITCOIN_ONLY=%s %s' % (ARGUMENTS.get('CFLAGS', ''), PYOPT, BITCOIN_ONLY, STATIC)) +env = Environment( + ENV=os.environ, + CFLAGS=ARGUMENTS.get("CFLAGS", "") + f" -DCONFIDENTIAL= -DPYOPT={PYOPT} -DBITCOIN_ONLY={BITCOIN_ONLY} {STATIC}", + CPPDEFPREFIX="-D'", + CPPDEFSUFFIX="'", +) FEATURES_AVAILABLE = models.configure_board(TREZOR_MODEL, HW_REVISION, FEATURES_WANTED, env, CPPDEFINES_HAL, SOURCE_UNIX, PATH_HAL) @@ -495,7 +501,7 @@ if ARGUMENTS.get('TREZOR_EMULATOR_DEBUGGABLE', '0') == '1': if ARGUMENTS.get('TREZOR_MEMPERF', '0') == '1': CPPDEFINES_MOD += [ - ('MICROPY_TREZOR_MEMPERF', '\(1\)') + ('MICROPY_TREZOR_MEMPERF', '(1)') ] env.Replace( @@ -525,7 +531,7 @@ env.Replace( CPPDEFINES=[ 'TREZOR_EMULATOR', 'TREZOR_MODEL_'+TREZOR_MODEL, - ('MP_CONFIGFILE', '\\"embed/unix/mpconfigport.h\\"'), + ('MP_CONFIGFILE', '"embed/unix/mpconfigport.h"'), ui.get_ui_layout(TREZOR_MODEL), ] + CPPDEFINES_MOD + CPPDEFINES_HAL, ASPPFLAGS='$CFLAGS $CCFLAGS', ) @@ -835,7 +841,7 @@ def cargo_build(): bindgen_macros = tools.get_bindgen_defines(env.get("CPPDEFINES"), ALLPATHS) build_dir = str(Dir('.').abspath) - return f'export BINDGEN_MACROS=\'{bindgen_macros}\'; export BUILD_DIR=\'{build_dir}\'; cd embed/rust; cargo build --profile {RUST_PROFILE} --target-dir=../../build/unix/rust --no-default-features --features "{" ".join(features)}" --target {TARGET}' + return f'export BINDGEN_MACROS={shlex.quote(bindgen_macros)}; export BUILD_DIR=\'{build_dir}\'; cd embed/rust; cargo build --profile {RUST_PROFILE} --target-dir=../../build/unix/rust --no-default-features --features "{" ".join(features)}" --target {TARGET}' rust = env.Command( target=RUST_LIBPATH, diff --git a/core/site_scons/models/D001/discovery.py b/core/site_scons/models/D001/discovery.py index a9435390864..4e1cd7511b5 100644 --- a/core/site_scons/models/D001/discovery.py +++ b/core/site_scons/models/D001/discovery.py @@ -30,9 +30,9 @@ def configure( env.get("ENV")["RUST_TARGET"] = "thumbv7em-none-eabihf" defines += [mcu] - defines += [f'TREZOR_BOARD=\\"{board}\\"'] - defines += [f"HW_MODEL={hw_model}"] - defines += [f"HW_REVISION={hw_revision}"] + defines += [("TREZOR_BOARD", f'"{board}"')] + defines += [("HW_MODEL", str(hw_model))] + defines += [("HW_REVISION", str(hw_revision))] if "new_rendering" in features_wanted: sources += [ diff --git a/core/site_scons/models/D002/discovery2.py b/core/site_scons/models/D002/discovery2.py index 8ca76fb7bb0..158ec627aa5 100644 --- a/core/site_scons/models/D002/discovery2.py +++ b/core/site_scons/models/D002/discovery2.py @@ -30,15 +30,11 @@ def configure( ] = "-mthumb -mcpu=cortex-m33 -mfloat-abi=hard -mfpu=fpv5-sp-d16 -mtune=cortex-m33 -mcmse " env.get("ENV")["RUST_TARGET"] = "thumbv8m.main-none-eabihf" - defines += [mcu] defines += [ - f'TREZOR_BOARD=\\"{board}\\"', - ] - defines += [ - f"HW_MODEL={hw_model}", - ] - defines += [ - f"HW_REVISION={hw_revision}", + mcu, + ("TREZOR_BOARD", f'"{board}"'), + ("HW_MODEL", str(hw_model)), + ("HW_REVISION", str(hw_revision)), ] if "new_rendering" in features_wanted: diff --git a/core/site_scons/models/T2B1/emulator.py b/core/site_scons/models/T2B1/emulator.py index 16a6137d69b..7884d8e334e 100644 --- a/core/site_scons/models/T2B1/emulator.py +++ b/core/site_scons/models/T2B1/emulator.py @@ -22,13 +22,15 @@ def configure( features_available.append("xframebuffer") features_available.append("display_mono") - defines += [mcu] - defines += [f'TREZOR_BOARD=\\"{board}\\"'] - defines += [f"HW_MODEL={hw_model}"] - defines += [f"HW_REVISION={hw_revision}"] - defines += [f"MCU_TYPE={mcu}"] - defines += ["FLASH_BIT_ACCESS=1"] - defines += ["FLASH_BLOCK_WORDS=1"] + defines += [ + mcu, + ("TREZOR_BOARD", f'"{board}"'), + ("HW_MODEL", str(hw_model)), + ("HW_REVISION", str(hw_revision)), + ("MCU_TYPE", mcu), + ("FLASH_BIT_ACCESS", "1"), + ("FLASH_BLOCK_WORDS", "1"), + ] if "sbu" in features_wanted: sources += ["embed/trezorhal/unix/sbu.c"] diff --git a/core/site_scons/models/T2B1/trezor_r_v10.py b/core/site_scons/models/T2B1/trezor_r_v10.py index 2289d8fa46c..5dcfd008846 100644 --- a/core/site_scons/models/T2B1/trezor_r_v10.py +++ b/core/site_scons/models/T2B1/trezor_r_v10.py @@ -34,10 +34,12 @@ def configure( ] = "-mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mtune=cortex-m4 " env.get("ENV")["RUST_TARGET"] = "thumbv7em-none-eabihf" - defines += [mcu] - defines += [f'TREZOR_BOARD=\\"{board}\\"'] - defines += [f"HW_MODEL={hw_model}"] - defines += [f"HW_REVISION={hw_revision}"] + defines += [ + mcu, + ("TREZOR_BOARD", f'"{board}"'), + ("HW_MODEL", str(hw_model)), + ("HW_REVISION", str(hw_revision)), + ] if "new_rendering" in features_wanted: sources += ["embed/trezorhal/xdisplay_legacy.c"] @@ -73,7 +75,7 @@ def configure( features_available.append("usb") if "optiga" in features_wanted: - defines += ["USE_OPTIGA=1"] + defines += [("USE_OPTIGA", "1")] sources += ["embed/trezorhal/stm32f4/i2c_bus.c"] sources += ["embed/trezorhal/stm32f4/optiga_hal.c"] sources += ["embed/trezorhal/optiga/optiga.c"] diff --git a/core/site_scons/models/T2B1/trezor_r_v3.py b/core/site_scons/models/T2B1/trezor_r_v3.py index c72a596f3d8..2f63e90b06e 100644 --- a/core/site_scons/models/T2B1/trezor_r_v3.py +++ b/core/site_scons/models/T2B1/trezor_r_v3.py @@ -34,10 +34,12 @@ def configure( ] = "-mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mtune=cortex-m4 " env.get("ENV")["RUST_TARGET"] = "thumbv7em-none-eabihf" - defines += [mcu] - defines += [f'TREZOR_BOARD=\\"{board}\\"'] - defines += [f"HW_MODEL={hw_model}"] - defines += [f"HW_REVISION={hw_revision}"] + defines += [ + mcu, + ("TREZOR_BOARD", f'"{board}"'), + ("HW_MODEL", str(hw_model)), + ("HW_REVISION", str(hw_revision)), + ] if "new_rendering" in features_wanted: sources += ["embed/trezorhal/xdisplay_legacy.c"] diff --git a/core/site_scons/models/T2B1/trezor_r_v4.py b/core/site_scons/models/T2B1/trezor_r_v4.py index c578517c4ac..c555dd8803e 100644 --- a/core/site_scons/models/T2B1/trezor_r_v4.py +++ b/core/site_scons/models/T2B1/trezor_r_v4.py @@ -34,10 +34,12 @@ def configure( ] = "-mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mtune=cortex-m4 " env.get("ENV")["RUST_TARGET"] = "thumbv7em-none-eabihf" - defines += [mcu] - defines += [f'TREZOR_BOARD=\\"{board}\\"'] - defines += [f"HW_MODEL={hw_model}"] - defines += [f"HW_REVISION={hw_revision}"] + defines += [ + mcu, + ("TREZOR_BOARD", f'"{board}"'), + ("HW_MODEL", str(hw_model)), + ("HW_REVISION", str(hw_revision)), + ] if "new_rendering" in features_wanted: sources += ["embed/trezorhal/xdisplay_legacy.c"] diff --git a/core/site_scons/models/T2B1/trezor_r_v6.py b/core/site_scons/models/T2B1/trezor_r_v6.py index 6062ab7dc04..10b5f621a09 100644 --- a/core/site_scons/models/T2B1/trezor_r_v6.py +++ b/core/site_scons/models/T2B1/trezor_r_v6.py @@ -34,10 +34,12 @@ def configure( ] = "-mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mtune=cortex-m4 " env.get("ENV")["RUST_TARGET"] = "thumbv7em-none-eabihf" - defines += [mcu] - defines += [f'TREZOR_BOARD=\\"{board}\\"'] - defines += [f"HW_MODEL={hw_model}"] - defines += [f"HW_REVISION={hw_revision}"] + defines += [ + mcu, + ("TREZOR_BOARD", f'"{board}"'), + ("HW_MODEL", str(hw_model)), + ("HW_REVISION", str(hw_revision)), + ] if "new_rendering" in features_wanted: sources += ["embed/trezorhal/xdisplay_legacy.c"] diff --git a/core/site_scons/models/T2T1/emulator.py b/core/site_scons/models/T2T1/emulator.py index 9d10e1c6b74..d523d108fe0 100644 --- a/core/site_scons/models/T2T1/emulator.py +++ b/core/site_scons/models/T2T1/emulator.py @@ -21,13 +21,15 @@ def configure( defines += ["DISPLAY_RGB565"] features_available.append("display_rgb565") - defines += [mcu] - defines += [f'TREZOR_BOARD=\\"{board}\\"'] - defines += [f"HW_MODEL={hw_model}"] - defines += [f"HW_REVISION={hw_revision}"] - defines += [f"MCU_TYPE={mcu}"] - defines += ["FLASH_BIT_ACCESS=1"] - defines += ["FLASH_BLOCK_WORDS=1"] + defines += [ + mcu, + ("TREZOR_BOARD", f'"{board}"'), + ("HW_MODEL", str(hw_model)), + ("HW_REVISION", str(hw_revision)), + ("MCU_TYPE", mcu), + ("FLASH_BIT_ACCESS", "1"), + ("FLASH_BLOCK_WORDS", "1"), + ] if "dma2d" in features_wanted: features_available.append("dma2d") diff --git a/core/site_scons/models/T2T1/trezor_t.py b/core/site_scons/models/T2T1/trezor_t.py index b428bd05db4..4152b8764a9 100644 --- a/core/site_scons/models/T2T1/trezor_t.py +++ b/core/site_scons/models/T2T1/trezor_t.py @@ -34,10 +34,12 @@ def configure( ] = "-mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mtune=cortex-m4 " env.get("ENV")["RUST_TARGET"] = "thumbv7em-none-eabihf" - defines += [mcu] - defines += [f'TREZOR_BOARD=\\"{board}\\"'] - defines += [f"HW_MODEL={hw_model}"] - defines += [f"HW_REVISION={hw_revision}"] + defines += [ + mcu, + ("TREZOR_BOARD", f'"{board}"'), + ("HW_MODEL", str(hw_model)), + ("HW_REVISION", str(hw_revision)), + ] sources += [ "embed/models/T2T1/compat_settings.c", ] @@ -48,33 +50,17 @@ def configure( sources += ["embed/trezorhal/stm32f4/xdisplay/st-7789/display_driver.c"] sources += ["embed/trezorhal/stm32f4/xdisplay/st-7789/display_io.c"] sources += ["embed/trezorhal/stm32f4/xdisplay/st-7789/display_panel.c"] - sources += [ - "embed/trezorhal/stm32f4/xdisplay/st-7789/panels/tf15411a.c", - ] - sources += [ - "embed/trezorhal/stm32f4/xdisplay/st-7789/panels/154a.c", - ] - sources += [ - "embed/trezorhal/stm32f4/xdisplay/st-7789/panels/lx154a2411.c", - ] - sources += [ - "embed/trezorhal/stm32f4/xdisplay/st-7789/panels/lx154a2422.c", - ] + sources += ["embed/trezorhal/stm32f4/xdisplay/st-7789/panels/tf15411a.c"] + sources += ["embed/trezorhal/stm32f4/xdisplay/st-7789/panels/154a.c"] + sources += ["embed/trezorhal/stm32f4/xdisplay/st-7789/panels/lx154a2411.c"] + sources += ["embed/trezorhal/stm32f4/xdisplay/st-7789/panels/lx154a2422.c"] else: sources += [f"embed/trezorhal/stm32f4/displays/{display}"] - sources += [ - "embed/trezorhal/stm32f4/displays/panels/tf15411a.c", - ] - sources += [ - "embed/trezorhal/stm32f4/displays/panels/154a.c", - ] - sources += [ - "embed/trezorhal/stm32f4/displays/panels/lx154a2411.c", - ] - sources += [ - "embed/trezorhal/stm32f4/displays/panels/lx154a2422.c", - ] + sources += ["embed/trezorhal/stm32f4/displays/panels/tf15411a.c"] + sources += ["embed/trezorhal/stm32f4/displays/panels/154a.c"] + sources += ["embed/trezorhal/stm32f4/displays/panels/lx154a2411.c"] + sources += ["embed/trezorhal/stm32f4/displays/panels/lx154a2422.c"] sources += ["embed/trezorhal/stm32f4/backlight_pwm.c"] diff --git a/core/site_scons/models/T3B1/emulator.py b/core/site_scons/models/T3B1/emulator.py index 493c3353d5e..50f33f04251 100644 --- a/core/site_scons/models/T3B1/emulator.py +++ b/core/site_scons/models/T3B1/emulator.py @@ -22,13 +22,15 @@ def configure( features_available.append("xframebuffer") features_available.append("display_mono") - defines += [mcu] - defines += [f'TREZOR_BOARD=\\"{board}\\"'] - defines += [f"HW_MODEL={hw_model}"] - defines += [f"HW_REVISION={hw_revision}"] - defines += [f"MCU_TYPE={mcu}"] - defines += ["FLASH_BIT_ACCESS=1"] - defines += ["FLASH_BLOCK_WORDS=1"] + defines += [ + mcu, + ("TREZOR_BOARD", f'"{board}"'), + ("HW_MODEL", str(hw_model)), + ("HW_REVISION", str(hw_revision)), + ("MCU_TYPE", mcu), + ("FLASH_BIT_ACCESS", "1"), + ("FLASH_BLOCK_WORDS", "1"), + ] if "sbu" in features_wanted: sources += ["embed/trezorhal/unix/sbu.c"] diff --git a/core/site_scons/models/T3B1/trezor_t3b1_revB.py b/core/site_scons/models/T3B1/trezor_t3b1_revB.py index 3c5a5973b52..79e36b2c9e1 100644 --- a/core/site_scons/models/T3B1/trezor_t3b1_revB.py +++ b/core/site_scons/models/T3B1/trezor_t3b1_revB.py @@ -15,7 +15,7 @@ def configure( board = "T3B1/boards/trezor_t3b1_revB.h" display = "vg-2864ksweg01.c" hw_model = get_hw_model_as_number("T3B1") - hw_revision = "B" + hw_revision = ord("B") if "new_rendering" in features_wanted: defines += ["XFRAMEBUFFER"] @@ -35,10 +35,12 @@ def configure( ] = "-mthumb -mcpu=cortex-m33 -mfloat-abi=hard -mfpu=fpv5-sp-d16 -mtune=cortex-m33 -mcmse " env.get("ENV")["RUST_TARGET"] = "thumbv8m.main-none-eabihf" - defines += [mcu] - defines += [f'TREZOR_BOARD=\\"{board}\\"'] - defines += [f"HW_MODEL={hw_model}"] - defines += [f"HW_REVISION={ord(hw_revision)}"] + defines += [ + mcu, + ("TREZOR_BOARD", f'"{board}"'), + ("HW_MODEL", str(hw_model)), + ("HW_REVISION", str(hw_revision)), + ] if "new_rendering" in features_wanted: sources += ["embed/trezorhal/xdisplay_legacy.c"] @@ -69,7 +71,7 @@ def configure( features_available.append("usb") if "optiga" in features_wanted: - defines += ["USE_OPTIGA=1"] + defines += [("USE_OPTIGA", "1")] sources += ["embed/trezorhal/stm32u5/i2c_bus.c"] sources += ["embed/trezorhal/stm32u5/optiga_hal.c"] sources += ["embed/trezorhal/optiga/optiga.c"] diff --git a/core/site_scons/models/T3T1/emulator.py b/core/site_scons/models/T3T1/emulator.py index e8173defe0f..f9361078369 100644 --- a/core/site_scons/models/T3T1/emulator.py +++ b/core/site_scons/models/T3T1/emulator.py @@ -22,21 +22,21 @@ def configure( features_available.append("xframebuffer") features_available.append("display_rgb565") - defines += [mcu] - defines += [f'TREZOR_BOARD=\\"{board}\\"'] - defines += [f"HW_MODEL={hw_model}"] - defines += [f"HW_REVISION={hw_revision}"] - defines += [f"MCU_TYPE={mcu}"] - # todo change to blockwise flash when implemented in unix - defines += ["FLASH_BIT_ACCESS=1"] - defines += ["FLASH_BLOCK_WORDS=1"] + defines += [ + mcu, + ("TREZOR_BOARD", f'"{board}"'), + ("HW_MODEL", str(hw_model)), + ("HW_REVISION", str(hw_revision)), + ("MCU_TYPE", mcu), + # todo change to blockwise flash when implemented in unix + ("FLASH_BIT_ACCESS", "1"), + ("FLASH_BLOCK_WORDS", "1"), + ] if "dma2d" in features_wanted: features_available.append("dma2d") if "new_rendering" in features_wanted: - sources += [ - "embed/trezorhal/unix/dma2d_bitblt.c", - ] + sources += ["embed/trezorhal/unix/dma2d_bitblt.c"] else: sources += ["embed/lib/dma2d_emul.c"] defines += ["USE_DMA2D"] diff --git a/core/site_scons/models/T3T1/trezor_t3t1_revE.py b/core/site_scons/models/T3T1/trezor_t3t1_revE.py index 412c8f77adb..947520b66d0 100644 --- a/core/site_scons/models/T3T1/trezor_t3t1_revE.py +++ b/core/site_scons/models/T3T1/trezor_t3t1_revE.py @@ -39,10 +39,12 @@ def configure( ] = "-mthumb -mcpu=cortex-m33 -mfloat-abi=hard -mfpu=fpv5-sp-d16 -mtune=cortex-m33 -mcmse " env.get("ENV")["RUST_TARGET"] = "thumbv8m.main-none-eabihf" - defines += [mcu] - defines += [f'TREZOR_BOARD=\\"{board}\\"'] - defines += [f"HW_MODEL={hw_model}"] - defines += [f"HW_REVISION={hw_revision}"] + defines += [ + mcu, + ("TREZOR_BOARD", f'"{board}"'), + ("HW_MODEL", str(hw_model)), + ("HW_REVISION", str(hw_revision)), + ] if "new_rendering" in features_wanted: sources += ["embed/trezorhal/xdisplay_legacy.c"] @@ -50,14 +52,10 @@ def configure( sources += ["embed/trezorhal/stm32u5/xdisplay/st-7789/display_driver.c"] sources += ["embed/trezorhal/stm32u5/xdisplay/st-7789/display_io.c"] sources += ["embed/trezorhal/stm32u5/xdisplay/st-7789/display_panel.c"] - sources += [ - "embed/trezorhal/stm32u5/xdisplay/st-7789/panels/lx154a2482.c", - ] + sources += ["embed/trezorhal/stm32u5/xdisplay/st-7789/panels/lx154a2482.c"] else: sources += [f"embed/trezorhal/stm32u5/displays/{display}"] - sources += [ - "embed/trezorhal/stm32u5/displays/panels/lx154a2482.c", - ] + sources += ["embed/trezorhal/stm32u5/displays/panels/lx154a2482.c"] sources += ["embed/trezorhal/stm32u5/backlight_pwm.c"] @@ -74,9 +72,7 @@ def configure( features_available.append("touch") if "haptic" in features_wanted: - sources += [ - "embed/trezorhal/stm32u5/haptic/drv2625/drv2625.c", - ] + sources += ["embed/trezorhal/stm32u5/haptic/drv2625/drv2625.c"] sources += [ "vendor/stm32u5xx_hal_driver/Src/stm32u5xx_hal_tim.c", "vendor/stm32u5xx_hal_driver/Src/stm32u5xx_hal_tim_ex.c", @@ -118,7 +114,7 @@ def configure( features_available.append("dma2d") if "optiga" in features_wanted: - defines += ["USE_OPTIGA=1"] + defines += [("USE_OPTIGA", "1")] sources += ["embed/trezorhal/stm32u5/optiga_hal.c"] sources += ["embed/trezorhal/optiga/optiga.c"] sources += ["embed/trezorhal/optiga/optiga_commands.c"] diff --git a/core/site_scons/models/T3T1/trezor_t3t1_v4.py b/core/site_scons/models/T3T1/trezor_t3t1_v4.py index c6b712bb6bc..67457d4446c 100644 --- a/core/site_scons/models/T3T1/trezor_t3t1_v4.py +++ b/core/site_scons/models/T3T1/trezor_t3t1_v4.py @@ -39,10 +39,12 @@ def configure( ] = "-mthumb -mcpu=cortex-m33 -mfloat-abi=hard -mfpu=fpv5-sp-d16 -mtune=cortex-m33 -mcmse " env.get("ENV")["RUST_TARGET"] = "thumbv8m.main-none-eabihf" - defines += [mcu] - defines += [f'TREZOR_BOARD=\\"{board}\\"'] - defines += [f"HW_MODEL={hw_model}"] - defines += [f"HW_REVISION={hw_revision}"] + defines += [ + mcu, + ("TREZOR_BOARD", f'"{board}"'), + ("HW_MODEL", str(hw_model)), + ("HW_REVISION", str(hw_revision)), + ] sources += [ f"embed/trezorhal/stm32u5/displays/{display}", ] @@ -53,15 +55,11 @@ def configure( sources += ["embed/trezorhal/stm32u5/xdisplay/st-7789/display_driver.c"] sources += ["embed/trezorhal/stm32u5/xdisplay/st-7789/display_io.c"] sources += ["embed/trezorhal/stm32u5/xdisplay/st-7789/display_panel.c"] - sources += [ - "embed/trezorhal/stm32u5/xdisplay/st-7789/panels/lx154a2482.c", - ] + sources += ["embed/trezorhal/stm32u5/xdisplay/st-7789/panels/lx154a2482.c"] else: sources += [f"embed/trezorhal/stm32u5/displays/{display}"] - sources += [ - "embed/trezorhal/stm32u5/displays/panels/lx154a2482.c", - ] + sources += ["embed/trezorhal/stm32u5/displays/panels/lx154a2482.c"] sources += ["embed/trezorhal/stm32u5/backlight_pwm.c"] @@ -77,9 +75,7 @@ def configure( features_available.append("touch") if "haptic" in features_wanted: - sources += [ - "embed/trezorhal/stm32u5/haptic/drv2625/drv2625.c", - ] + sources += ["embed/trezorhal/stm32u5/haptic/drv2625/drv2625.c"] sources += [ "vendor/stm32u5xx_hal_driver/Src/stm32u5xx_hal_tim.c", "vendor/stm32u5xx_hal_driver/Src/stm32u5xx_hal_tim_ex.c", @@ -121,7 +117,7 @@ def configure( features_available.append("dma2d") if "optiga" in features_wanted: - defines += ["USE_OPTIGA=1"] + defines += [("USE_OPTIGA", "1")] sources += ["embed/trezorhal/stm32u5/optiga_hal.c"] sources += ["embed/trezorhal/optiga/optiga.c"] sources += ["embed/trezorhal/optiga/optiga_commands.c"] diff --git a/core/site_scons/models/stm32f4_common.py b/core/site_scons/models/stm32f4_common.py index 9e3b4cbe1a5..d4953663d3d 100644 --- a/core/site_scons/models/stm32f4_common.py +++ b/core/site_scons/models/stm32f4_common.py @@ -3,7 +3,7 @@ def stm32f4_common_files(env, defines, sources, paths): defines += [ - ("STM32_HAL_H", '""'), + ("STM32_HAL_H", ""), ("FLASH_BLOCK_WORDS", "1"), ("FLASH_BIT_ACCESS", "1"), ("CONFIDENTIAL", ""), diff --git a/core/site_scons/models/stm32u5_common.py b/core/site_scons/models/stm32u5_common.py index b69e88a3241..9d12d97c0f3 100644 --- a/core/site_scons/models/stm32u5_common.py +++ b/core/site_scons/models/stm32u5_common.py @@ -3,9 +3,9 @@ def stm32u5_common_files(env, defines, sources, paths): defines += [ - ("STM32_HAL_H", '""'), + ("STM32_HAL_H", ""), ("FLASH_BLOCK_WORDS", "4"), - ("CONFIDENTIAL", "'__attribute__((section(\".confidential\")))'"), + ("CONFIDENTIAL", '__attribute__((section(".confidential")))'), ] paths += [ diff --git a/core/site_scons/tools.py b/core/site_scons/tools.py index 2f91feb4c15..3f5226ac20e 100644 --- a/core/site_scons/tools.py +++ b/core/site_scons/tools.py @@ -63,21 +63,13 @@ def _compress(data: bytes) -> bytes: return z.compress(data) + z.flush() -def get_bindgen_defines( - defines: list[str | tuple[str, str]], paths: list[str] -) -> tuple(str, str): +def get_bindgen_defines(defines: list[str | tuple[str, str]], paths: list[str]) -> str: rest_defs = [] for d in defines: if type(d) is tuple: d = f"-D{d[0]}={d[1]}" else: d = f"-D{d}" - d = ( - d.replace('\\"', '"') - .replace("'", "'\"'\"'") - .replace('"<', "<") - .replace('>"', ">") - ) rest_defs.append(d) for d in paths: rest_defs.append(f"-I../../{d}") diff --git a/core/site_scons/ui/common.py b/core/site_scons/ui/common.py index c13b67c1ef4..dbeb5d01a0d 100644 --- a/core/site_scons/ui/common.py +++ b/core/site_scons/ui/common.py @@ -2,13 +2,16 @@ def add_font( - font_name: str, font: str | None, defines: list[str], sources: list[str] + font_name: str, + font: str | None, + defines: list[str | tuple[str, str]], + sources: list[str], ) -> None: if font is not None: font_filename = font.replace("_upper", "").lower() defines += [ - "TREZOR_FONT_" + font_name + "_ENABLE=" + font, - "TREZOR_FONT_" + font_name + '_INCLUDE=\\"' + font_filename + '.h\\"', + (f"TREZOR_FONT_{font_name}_ENABLE", font), + (f"TREZOR_FONT_{font_name}_INCLUDE", f'"{font_filename}.h"'), ] sourcefile = "embed/lib/fonts/" + font_filename + ".c" if sourcefile not in sources: From f765e11eaed869eb68546963e1e99fea085771f9 Mon Sep 17 00:00:00 2001 From: cepetr Date: Wed, 2 Oct 2024 09:38:49 +0200 Subject: [PATCH 3/5] build(core): introduce quiet mode [no changelog] --- core/Makefile | 11 +++++++++-- core/SConstruct | 3 +++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/core/Makefile b/core/Makefile index 7d9e30d1e79..6340cc5c77e 100644 --- a/core/Makefile +++ b/core/Makefile @@ -40,6 +40,7 @@ TEST_LANG ?= "en" NEW_RENDERING ?= 1 THP ?= 0 TREZOR_EMULATOR_DEBUGGABLE ?= 0 +QUIET_MODE ?= 0 # OpenOCD interface default. Alternative: ftdi/olimex-arm-usb-tiny-h OPENOCD_INTERFACE ?= stlink @@ -138,9 +139,15 @@ SCONS_VARS = \ TREZOR_EMULATOR_ASAN="$(ADDRESS_SANITIZER)" \ TREZOR_EMULATOR_DEBUGGABLE=$(TREZOR_EMULATOR_DEBUGGABLE) \ TREZOR_MEMPERF="$(TREZOR_MEMPERF)" \ - TREZOR_MODEL="$(TREZOR_MODEL)" + TREZOR_MODEL="$(TREZOR_MODEL)" \ + QUIET_MODE="$(QUIET_MODE)" -SCONS = scons -Q -j $(JOBS) $(SCONS_VARS) +SCONS_OPTS = -Q -j $(JOBS) +ifeq ($(QUIET_MODE),1) +SCONS_OPTS += --quiet +endif + +SCONS = scons $(SCONS_OPTS) $(SCONS_VARS) ## help commands: diff --git a/core/SConstruct b/core/SConstruct index 6446691cf3e..0ea3bdfb7f7 100644 --- a/core/SConstruct +++ b/core/SConstruct @@ -12,3 +12,6 @@ SConscript('SConscript.firmware', variant_dir='build/firmware', duplicate=False) SConscript('SConscript.prodtest', variant_dir='build/prodtest', duplicate=False) SConscript('SConscript.reflash', variant_dir='build/reflash', duplicate=False) SConscript('SConscript.unix', variant_dir='build/unix', duplicate=False) + +if ARGUMENTS.get('QUIET_MODE', '1'): + Progress('$TARGET\r', overwrite=True) \ No newline at end of file From 116aafc437e6ccc9885bef4d4926f3c7427aad7e Mon Sep 17 00:00:00 2001 From: Martin Milata Date: Wed, 23 Oct 2024 21:44:42 +0200 Subject: [PATCH 4/5] build(core): emulator valgrind support [no changelog] --- ci/shell.nix | 1 + core/emu.py | 38 ++++++++++++++++++++-------------- docs/SUMMARY.md | 1 + docs/core/emulator/valgrind.md | 34 ++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 16 deletions(-) create mode 100644 docs/core/emulator/valgrind.md diff --git a/ci/shell.nix b/ci/shell.nix index f90b29130bc..09c879280cd 100644 --- a/ci/shell.nix +++ b/ci/shell.nix @@ -155,6 +155,7 @@ stdenvNoCC.mkDerivation ({ shellcheck gdb openocd-stm + kcachegrind ] ++ lib.optionals (devTools && acceptJlink) [ nrfutil nrfconnect diff --git a/core/emu.py b/core/emu.py index dd183fe9c1e..0cf88a6ca91 100755 --- a/core/emu.py +++ b/core/emu.py @@ -67,27 +67,31 @@ def watch_emulator(emulator: CoreEmulator) -> int: return 0 -def run_debugger(emulator: CoreEmulator, gdb_script_file: str | Path | None) -> None: +def run_debugger(emulator: CoreEmulator, gdb_script_file: str | Path | None, valgrind: bool = False, run_command: list[str] = []) -> None: os.chdir(emulator.workdir) env = emulator.make_env() - if platform.system() == "Darwin": + if valgrind: + dbg_command = ["valgrind", "-v", "--tool=callgrind", "--read-inline-info=yes", str(emulator.executable)] + emulator.make_args() + elif platform.system() == "Darwin": env["PATH"] = "/usr/bin" - os.execvpe( - "lldb", - ["lldb", "-f", str(emulator.executable), "--"] + emulator.make_args(), - env, - ) + dbg_command = ["lldb", "-f", str(emulator.executable), "--"] + emulator.make_args() else: # Optionally run a gdb script from a file if gdb_script_file is None: - gdb = ["gdb"] + dbg_command = ["gdb"] else: - gdb = ["gdb", "-x", str(HERE / gdb_script_file)] - os.execvpe( - "gdb", - gdb + ["--args", str(emulator.executable)] + emulator.make_args(), - env, - ) + dbg_command = ["gdb", "-x", str(HERE / gdb_script_file)] + dbg_command += ["--args", str(emulator.executable)] + dbg_command += emulator.make_args() + + if not run_command: + os.execvpe(dbg_command[0], dbg_command, env) + else: + dbg_process = subprocess.Popen(dbg_command, env=env) + run_process = subprocess.Popen(run_command, env=env, shell=True) + rc = run_process.wait() + dbg_process.send_signal(signal.SIGINT) + sys.exit(rc) def _from_env(name: str) -> bool: @@ -118,6 +122,7 @@ def _from_env(name: str) -> bool: @click.option("-r", "--record-dir", help="Directory where to record screen changes") @click.option("-s", "--slip0014", is_flag=True, help="Initialize device with SLIP-14 seed (all all all...)") @click.option("-S", "--script-gdb-file", type=click.Path(exists=True, dir_okay=False), help="Run gdb with an init file") +@click.option("-V", "--valgrind", is_flag=True, help="Use valgrind instead of debugger (-D)") @click.option("-t", "--temporary-profile", is_flag=True, help="Create an empty temporary profile") @click.option("-w", "--watch", is_flag=True, help="Restart emulator if sources change") @click.option("-X", "--extra-arg", "extra_args", multiple=True, help="Extra argument to pass to micropython") @@ -144,6 +149,7 @@ def cli( record_dir: Optional[str], slip0014: bool, script_gdb_file: str | Path | None, + valgrind: bool, temporary_profile: bool, watch: bool, extra_args: list[str], @@ -261,8 +267,8 @@ def cli( if alloc_profiling: os.environ["TREZOR_MEMPERF"] = "1" - if debugger: - run_debugger(emulator, script_gdb_file) + if debugger or valgrind: + run_debugger(emulator, script_gdb_file, valgrind, command) raise RuntimeError("run_debugger should not return") emulator.start() diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index a9de23f7238..24697f5a0cc 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -6,6 +6,7 @@ - [Embedded](core/build/embedded.md) - [Emulator](core/build/emulator.md) - [Emulator](core/emulator/index.md) + - [Valgrind profiling](core/emulator/valgrind.md) - [Event Loop](core/src/event-loop.md) - [Apps](core/src/apps.md) - [Tests](core/tests/index.md) diff --git a/docs/core/emulator/valgrind.md b/docs/core/emulator/valgrind.md new file mode 100644 index 00000000000..d8bc43f6402 --- /dev/null +++ b/docs/core/emulator/valgrind.md @@ -0,0 +1,34 @@ +# Profiling emulator with Valgrind + +Sometimes it can be helpful to know which parts of your code take most of the CPU time. +[Callgrind](https://valgrind.org/docs/manual/cl-manual.html) tool from the [Valgrind](https://valgrind.org/) +instrumentation framework can generate profiling data for a run of Trezor emulator. These can then be visualized +with [KCachegrind](https://kcachegrind.github.io/). + +Bear in mind that profiling the emulator is of very limited usefulness due to: +* different CPU architecture, +* different drivers, +* & other differences from actual hardware. +Still it might be a way to get *some* insight without a [hardware debugger](../systemview/index.md) +and a development board. + +## Build + +``` +make build_unix_frozen TREZOR_EMULATOR_DEBUGGABLE=1 ADDRESS_SANITIZER=0 +``` + +With `PYOPT=0` most of the execution time is spent formatting and writing logs so it is recommended to use `PYOPT=1` +(and lose DebugLink) or get rid of logging manually. + +## Run + +Record profiling data on some device tests: +``` +./emu.py -a --debugger --valgrind -c 'sleep 10; pytest ../../tests/device_tests/ -v --other-pytest-args...' +``` + +Open profiling data in KCachegrind (file suffix is different for each emulator process): +``` +kcachegrind src/callgrind.out.$PID +``` From 59140d9ca6433933d70663b66bdf365e1cc57af5 Mon Sep 17 00:00:00 2001 From: Martin Milata Date: Thu, 24 Oct 2024 13:01:33 +0200 Subject: [PATCH 5/5] fixup! build(core): emulator valgrind support --- docs/core/emulator/valgrind.md | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/docs/core/emulator/valgrind.md b/docs/core/emulator/valgrind.md index d8bc43f6402..05ad6b3a232 100644 --- a/docs/core/emulator/valgrind.md +++ b/docs/core/emulator/valgrind.md @@ -1,28 +1,37 @@ # Profiling emulator with Valgrind -Sometimes it can be helpful to know which parts of your code take most of the CPU time. +Sometimes, it can be helpful to know which parts of your code take most of the CPU time. [Callgrind](https://valgrind.org/docs/manual/cl-manual.html) tool from the [Valgrind](https://valgrind.org/) instrumentation framework can generate profiling data for a run of Trezor emulator. These can then be visualized with [KCachegrind](https://kcachegrind.github.io/). Bear in mind that profiling the emulator is of very limited usefulness due to: * different CPU architecture, -* different drivers, +* different/mocked drivers, * & other differences from actual hardware. -Still it might be a way to get *some* insight without a [hardware debugger](../systemview/index.md) +Still, it might be a way to get *some* insight without a [hardware debugger](../systemview/index.md) and a development board. +Valgrind also currently doesn't understand MicroPython call stack so it won't help you when your code is spending +a lot of time in pure python functions that don't call out to C. It might be possible to instrument trezor-core +so that Valgrind is aware of MicroPython stack frames. + ## Build ``` make build_unix_frozen TREZOR_EMULATOR_DEBUGGABLE=1 ADDRESS_SANITIZER=0 ``` -With `PYOPT=0` most of the execution time is spent formatting and writing logs so it is recommended to use `PYOPT=1` -(and lose DebugLink) or get rid of logging manually. +With `PYOPT=0`, most of the execution time is spent formatting and writing logs, so it is recommended to use +`PYOPT=1` (and lose DebugLink) or get rid of logging manually. ## Run +If you're using Nix, you can use Valgrind and KCachegrind packages from our `shell.nix`: +``` +nix-shell --args devTools true --run "poetry shell" +``` + Record profiling data on some device tests: ``` ./emu.py -a --debugger --valgrind -c 'sleep 10; pytest ../../tests/device_tests/ -v --other-pytest-args...'