Skip to content

Commit

Permalink
build(core): improve CPPDEFINES quoting
Browse files Browse the repository at this point in the history
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...
  • Loading branch information
matejcik committed Oct 3, 2024
1 parent 006f796 commit 52a599f
Show file tree
Hide file tree
Showing 27 changed files with 174 additions and 159 deletions.
10 changes: 6 additions & 4 deletions core/SConscript.boardloader
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 5 additions & 2 deletions core/SConscript.bootloader
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=E0602

import os
import shlex
import tools, models, ui

TREZOR_MODEL = ARGUMENTS.get('TREZOR_MODEL', 'T')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions core/SConscript.bootloader_ci
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 8 additions & 2 deletions core/SConscript.bootloader_emu
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=E0602

import os
import shlex
import tools, models, ui

TREZOR_MODEL = ARGUMENTS.get('TREZOR_MODEL', 'T')
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions core/SConscript.firmware
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# fmt: off

import os
import shlex
import tools, models, ui

BITCOIN_ONLY = ARGUMENTS.get('BITCOIN_ONLY', '0')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions core/SConscript.kernel
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 4 additions & 1 deletion core/SConscript.prodtest
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 4 additions & 2 deletions core/SConscript.reflash
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 10 additions & 4 deletions core/SConscript.unix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# fmt: off

import os
import shlex
import tools, models, ui

BITCOIN_ONLY = ARGUMENTS.get('BITCOIN_ONLY', '0')
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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', )
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions core/site_scons/models/D001/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 += [
Expand Down
12 changes: 4 additions & 8 deletions core/site_scons/models/D002/discovery2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 9 additions & 7 deletions core/site_scons/models/T2B1/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
12 changes: 7 additions & 5 deletions core/site_scons/models/T2B1/trezor_r_v10.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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"]
Expand Down
10 changes: 6 additions & 4 deletions core/site_scons/models/T2B1/trezor_r_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
10 changes: 6 additions & 4 deletions core/site_scons/models/T2B1/trezor_r_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
10 changes: 6 additions & 4 deletions core/site_scons/models/T2B1/trezor_r_v6.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
16 changes: 9 additions & 7 deletions core/site_scons/models/T2T1/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading

0 comments on commit 52a599f

Please sign in to comment.