From 52a599fd4ea60f27d790657e01995db6d9e40e25 Mon Sep 17 00:00:00 2001 From: matejcik Date: Tue, 1 Oct 2024 11:32:54 +0200 Subject: [PATCH] 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 3e2511f47b..a700d40207 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 0c05e7838d..d9c0262b96 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 3e11207dcd..58eadb5b62 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 f2c3d770fd..c8886df84b 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 63744c2bdc..c6e7488418 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 61598225ee..a761836bee 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 0fafdc0928..a5c609d7ad 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 1309f58d44..f4aad6f061 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 c23bad1fdb..2a70f2f60e 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 a943539086..4e1cd7511b 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 8ca76fb7bb..158ec627aa 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 16a6137d69..7884d8e334 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 2289d8fa46..5dcfd00884 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 c72a596f3d..2f63e90b06 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 c578517c4a..c555dd8803 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 6062ab7dc0..10b5f621a0 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 9d10e1c6b7..d523d108fe 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 b428bd05db..4152b8764a 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 493c3353d5..50f33f0425 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 3c5a5973b5..79e36b2c9e 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 e8173defe0..f936107836 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 412c8f77ad..947520b66d 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 c6b712bb6b..67457d4446 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 9e3b4cbe1a..d4953663d3 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 b69e88a324..9d12d97c0f 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 2f91feb4c1..3f5226ac20 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 c13b67c1ef..dbeb5d01a0 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: