From bf0340742615d1938c37226b30c0097a44912fb4 Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Thu, 24 Aug 2023 12:46:10 +0200 Subject: [PATCH] refactor(core): setup single-place layout selection [no changelog] --- core/SConscript.firmware | 32 ++++++++++++----- core/SConscript.unix | 35 ++++++++++++++----- .../extmod/modtrezorutils/modtrezorutils.c | 8 +++++ core/mocks/generated/trezorutils.pyi | 1 + .../site_tools/micropython/__init__.py | 4 +++ core/src/trezor/ui/layouts/__init__.py | 6 ++-- core/src/trezor/ui/layouts/fido.py | 4 +-- core/src/trezor/ui/layouts/homescreen.py | 4 +-- core/src/trezor/ui/layouts/progress.py | 4 +-- core/src/trezor/ui/layouts/recovery.py | 4 +-- core/src/trezor/ui/layouts/reset.py | 4 +-- core/src/trezor/utils.py | 1 + 12 files changed, 77 insertions(+), 30 deletions(-) diff --git a/core/SConscript.firmware b/core/SConscript.firmware index f8782ab35..42f2725a7 100644 --- a/core/SConscript.firmware +++ b/core/SConscript.firmware @@ -378,6 +378,19 @@ env = Environment(ENV=os.environ, CFLAGS=f"{ARGUMENTS.get('CFLAGS', '')} -DPRODU FEATURES_AVAILABLE = tools.configure_board(TREZOR_MODEL, FEATURES_WANTED, env, CPPDEFINES_HAL, SOURCE_HAL, PATH_HAL) + +if TREZOR_MODEL in ('T', 'DISC1'): + LAYOUT = 'LAYOUT_TTV2' +elif TREZOR_MODEL in ('1', 'R'): + LAYOUT = 'LAYOUT_TR' +else: + raise ValueError('Unknown Trezor model') + +if 'sd_card' in FEATURES_AVAILABLE: + SDCARD = True +else: + SDCARD = False + env.Tool('micropython') env.Replace( @@ -428,6 +441,7 @@ env.Replace( 'FIRMWARE', 'TREZOR_MODEL_'+TREZOR_MODEL, 'USE_HAL_DRIVER', + LAYOUT, ] + CPPDEFINES_MOD + CPPDEFINES_HAL, ASFLAGS=env.get('ENV')['CPU_ASFLAGS'], ASPPFLAGS='$CFLAGS $CCFLAGS', @@ -482,6 +496,7 @@ env.Ignore(qstr_collected, qstr_generated) moduledefs_collected = env.CollectModules( target='genhdr/moduledefs.collected.h', source=SOURCE_QSTR) + hdr_moduledefs = env.Command( target='genhdr/moduledefs.h', source=moduledefs_collected, @@ -516,7 +531,7 @@ if FROZEN: SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/*.py', exclude=[ SOURCE_PY_DIR + 'trezor/sdcard.py', - ] if TREZOR_MODEL not in ('T',) else [] + ] if not SDCARD else [] )) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/crypto/*.py')) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/ui/*.py')) @@ -527,27 +542,27 @@ if FROZEN: SOURCE_PY_DIR + 'trezor/ui/layouts/fido.py', ] if not EVERYTHING else [] )) - if TREZOR_MODEL in ('T', 'DISC1'): + if LAYOUT == 'LAYOUT_TTV2': SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/ui/layouts/tt_v2/*.py', exclude=[ SOURCE_PY_DIR + 'trezor/ui/layouts/tt_v2/fido.py', ] if not EVERYTHING else [] )) - elif TREZOR_MODEL in ('1', 'R'): + elif LAYOUT == 'LAYOUT_TR': SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/ui/layouts/tr/*.py', exclude=[ SOURCE_PY_DIR + 'trezor/ui/layouts/tr/fido.py', ] if not EVERYTHING else [] )) else: - raise ValueError('Unknown Trezor model') + raise ValueError('Unknown layout') SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/wire/*.py')) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'storage/*.py', exclude=[ SOURCE_PY_DIR + 'storage/sd_salt.py', - ] if "sd_card" not in FEATURES_AVAILABLE else [] + ] if not SDCARD else [] )) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/messages/__init__.py')) @@ -571,14 +586,14 @@ if FROZEN: SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/common/*.py', exclude=[ SOURCE_PY_DIR + 'apps/common/sdcard.py', - ] if "sd_card" not in FEATURES_AVAILABLE else [] + ] if not SDCARD else [] )) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/debug/*.py')) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/homescreen/*.py')) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/management/*.py', exclude=[ SOURCE_PY_DIR + 'apps/management/sd_protect.py', - ] if 'sd_card' not in FEATURES_AVAILABLE else []) + ] if not SDCARD else []) ) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/management/*/*.py')) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/misc/*.py')) @@ -639,7 +654,8 @@ if FROZEN: source=SOURCE_PY, source_dir=SOURCE_PY_DIR, bitcoin_only=BITCOIN_ONLY, - backlight='backlight' in FEATURES_AVAILABLE + backlight='backlight' in FEATURES_AVAILABLE, + layout=LAYOUT, ) source_mpyc = env.FrozenCFile( diff --git a/core/SConscript.unix b/core/SConscript.unix index 2ec3ef33a..1fe590556 100644 --- a/core/SConscript.unix +++ b/core/SConscript.unix @@ -387,7 +387,6 @@ SOURCE_UNIX = [ if TREZOR_MODEL in ('T', 'R'): SOURCE_UNIX += [ 'embed/trezorhal/unix/sbu.c', - 'embed/trezorhal/unix/sdcard.c', ] if DMA2D: @@ -416,6 +415,22 @@ else: env = Environment(ENV=os.environ, CFLAGS='%s -DPYOPT=%s -DBITCOIN_ONLY=%s %s' % (ARGUMENTS.get('CFLAGS', ''), PYOPT, BITCOIN_ONLY, STATIC)) + +if TREZOR_MODEL in ('T',): + LAYOUT = 'LAYOUT_TTV2' +elif TREZOR_MODEL in ('1', 'R'): + LAYOUT = 'LAYOUT_TR' +else: + raise ValueError('Unknown Trezor model') + +if TREZOR_MODEL in ('T',): + SDCARD = True + SOURCE_UNIX += [ + 'embed/trezorhal/unix/sdcard.c', + ] +else: + SDCARD = False + env.Tool('micropython') env.Replace( @@ -500,6 +515,7 @@ env.Replace( 'TREZOR_BOARD=\\"boards/board-unix.h\\"', 'MCU_TYPE='+CPU_MODEL, ('MP_CONFIGFILE', '\\"embed/unix/mpconfigport.h\\"'), + LAYOUT, ] + CPPDEFINES_MOD, ASPPFLAGS='$CFLAGS $CCFLAGS', ) @@ -591,7 +607,7 @@ if FROZEN: SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/*.py', exclude=[ SOURCE_PY_DIR + 'trezor/sdcard.py', - ] if TREZOR_MODEL not in ('T',) else [] + ] if not SDCARD else [] )) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/crypto/*.py')) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/ui/*.py')) @@ -602,27 +618,27 @@ if FROZEN: SOURCE_PY_DIR + 'trezor/ui/layouts/fido.py', ] if not EVERYTHING else [] )) - if TREZOR_MODEL in ('T',): + if LAYOUT == 'LAYOUT_TTV2': SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/ui/layouts/tt_v2/*.py', exclude=[ SOURCE_PY_DIR + 'trezor/ui/layouts/tt_v2/fido.py', ] if not EVERYTHING else [] )) - elif TREZOR_MODEL in ('R'): + elif LAYOUT == 'LAYOUT_TR': SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/ui/layouts/tr/*.py', exclude=[ SOURCE_PY_DIR + 'trezor/ui/layouts/tr/fido.py', ] if not EVERYTHING else [] )) else: - raise ValueError('Unknown Trezor model') + raise ValueError('Unknown layout') SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/wire/*.py')) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'storage/*.py', exclude=[ SOURCE_PY_DIR + 'storage/sd_salt.py', - ] if TREZOR_MODEL not in ('T',) else [] + ] if not SDCARD else [] )) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/messages/__init__.py')) @@ -646,14 +662,14 @@ if FROZEN: SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/common/*.py', exclude=[ SOURCE_PY_DIR + 'apps/common/sdcard.py', - ] if TREZOR_MODEL not in ('T',) else [] + ] if not SDCARD else [] )) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/debug/*.py')) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/homescreen/*.py')) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/management/*.py', exclude=[ SOURCE_PY_DIR + 'apps/management/sd_protect.py', - ] if TREZOR_MODEL not in ('T',) else []) + ] if not SDCARD else []) ) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/management/*/*.py')) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/misc/*.py')) @@ -714,7 +730,8 @@ if FROZEN: source=SOURCE_PY, source_dir=SOURCE_PY_DIR, bitcoin_only=BITCOIN_ONLY, - backlight=TREZOR_MODEL in ('T',) + backlight=TREZOR_MODEL in ('T',), + layout=LAYOUT, ) source_mpyc = env.FrozenCFile( diff --git a/core/embed/extmod/modtrezorutils/modtrezorutils.c b/core/embed/extmod/modtrezorutils/modtrezorutils.c index 8274ba659..17529f15c 100644 --- a/core/embed/extmod/modtrezorutils/modtrezorutils.c +++ b/core/embed/extmod/modtrezorutils/modtrezorutils.c @@ -269,6 +269,7 @@ STATIC mp_obj_str_t mod_trezorutils_revision_obj = { /// INTERNAL_MODEL: str /// EMULATOR: bool /// BITCOIN_ONLY: bool +/// LAYOUT: str STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = { {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_trezorutils)}, @@ -325,6 +326,13 @@ STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = { #else {MP_ROM_QSTR(MP_QSTR_BITCOIN_ONLY), mp_const_false}, #endif +#ifdef LAYOUT_TTV2 + {MP_ROM_QSTR(MP_QSTR_LAYOUT), MP_ROM_QSTR(MP_QSTR_TTV2)}, +#elif LAYOUT_TR + {MP_ROM_QSTR(MP_QSTR_LAYOUT), MP_ROM_QSTR(MP_QSTR_TR)}, +#else +#error Unknown layout +#endif }; STATIC MP_DEFINE_CONST_DICT(mp_module_trezorutils_globals, diff --git a/core/mocks/generated/trezorutils.pyi b/core/mocks/generated/trezorutils.pyi index 737addd8d..56f676a33 100644 --- a/core/mocks/generated/trezorutils.pyi +++ b/core/mocks/generated/trezorutils.pyi @@ -91,3 +91,4 @@ MODEL: str INTERNAL_MODEL: str EMULATOR: bool BITCOIN_ONLY: bool +LAYOUT: str diff --git a/core/site_scons/site_tools/micropython/__init__.py b/core/site_scons/site_tools/micropython/__init__.py index 00355e45a..cb71da4b0 100644 --- a/core/site_scons/site_tools/micropython/__init__.py +++ b/core/site_scons/site_tools/micropython/__init__.py @@ -37,11 +37,15 @@ def generate(env): # so the compiler can optimize out the things we don't want btc_only = env["bitcoin_only"] == "1" backlight = env["backlight"] + layout_ttv2 = env["layout"] == "LAYOUT_TTV2" + layout_tr = env["layout"] == "LAYOUT_TR" interim = f"{target[:-4]}.i" # replace .mpy with .i sed_scripts = " ".join( [ rf"-e 's/utils\.BITCOIN_ONLY/{btc_only}/g'", rf"-e 's/utils\.USE_BACKLIGHT/{backlight}/g'", + rf"-e 's/utils\.LAYOUT == \"TTV2\"/{layout_ttv2}/g'", + rf"-e 's/utils\.LAYOUT == \"TR\"/{layout_tr}/g'", r"-e 's/if TYPE_CHECKING/if False/'", r"-e 's/import typing/# \0/'", r"-e '/from typing import (/,/^\s*)/ {s/^/# /}'", diff --git a/core/src/trezor/ui/layouts/__init__.py b/core/src/trezor/ui/layouts/__init__.py index fe191482b..8960b32b9 100644 --- a/core/src/trezor/ui/layouts/__init__.py +++ b/core/src/trezor/ui/layouts/__init__.py @@ -4,9 +4,9 @@ from .common import * # noqa: F401,F403 # NOTE: using any import magic probably causes mypy not to check equivalence of # layout type signatures across models -if utils.INTERNAL_MODEL in ("T1B1", "T2B1"): +if utils.LAYOUT == "TR": from .tr import * # noqa: F401,F403 -elif utils.INTERNAL_MODEL in ("T2T1", "D001"): +elif utils.LAYOUT == "TTV2": from .tt_v2 import * # noqa: F401,F403 else: - raise ValueError("Unknown Trezor model") + raise ValueError("Unknown layout") diff --git a/core/src/trezor/ui/layouts/fido.py b/core/src/trezor/ui/layouts/fido.py index 706697bf9..67fd3218f 100644 --- a/core/src/trezor/ui/layouts/fido.py +++ b/core/src/trezor/ui/layouts/fido.py @@ -1,6 +1,6 @@ from trezor import utils -if utils.INTERNAL_MODEL in ("T2T1", "D001"): +if utils.LAYOUT == "TTV2": from .tt_v2.fido import * # noqa: F401,F403 -elif utils.INTERNAL_MODEL in ("T2B1",): +elif utils.LAYOUT == "TR": from .tr.fido import * # noqa: F401,F403 diff --git a/core/src/trezor/ui/layouts/homescreen.py b/core/src/trezor/ui/layouts/homescreen.py index 9223736b4..02ed5df5e 100644 --- a/core/src/trezor/ui/layouts/homescreen.py +++ b/core/src/trezor/ui/layouts/homescreen.py @@ -1,6 +1,6 @@ from trezor import utils -if utils.INTERNAL_MODEL in ("T2T1", "D001"): +if utils.LAYOUT == "TTV2": from .tt_v2.homescreen import * # noqa: F401,F403 -elif utils.INTERNAL_MODEL in ("T2B1",): +elif utils.LAYOUT == "TR": from .tr.homescreen import * # noqa: F401,F403 diff --git a/core/src/trezor/ui/layouts/progress.py b/core/src/trezor/ui/layouts/progress.py index 9316c920d..fa7b2f03b 100644 --- a/core/src/trezor/ui/layouts/progress.py +++ b/core/src/trezor/ui/layouts/progress.py @@ -1,6 +1,6 @@ from trezor import utils -if utils.INTERNAL_MODEL in ("T2T1", "D001"): +if utils.LAYOUT == "TTV2": from .tt_v2.progress import * # noqa: F401,F403 -elif utils.INTERNAL_MODEL in ("T2B1",): +elif utils.LAYOUT == "TR": from .tr.progress import * # noqa: F401,F403 diff --git a/core/src/trezor/ui/layouts/recovery.py b/core/src/trezor/ui/layouts/recovery.py index 39512a3e1..93bbc1db9 100644 --- a/core/src/trezor/ui/layouts/recovery.py +++ b/core/src/trezor/ui/layouts/recovery.py @@ -1,6 +1,6 @@ from trezor import utils -if utils.INTERNAL_MODEL in ("T2T1", "D001"): +if utils.LAYOUT == "TTV2": from .tt_v2.recovery import * # noqa: F401,F403 -elif utils.INTERNAL_MODEL in ("T2B1",): +elif utils.LAYOUT == "TR": from .tr.recovery import * # noqa: F401,F403 diff --git a/core/src/trezor/ui/layouts/reset.py b/core/src/trezor/ui/layouts/reset.py index 597e0d11a..b852fc843 100644 --- a/core/src/trezor/ui/layouts/reset.py +++ b/core/src/trezor/ui/layouts/reset.py @@ -1,6 +1,6 @@ from trezor import utils -if utils.INTERNAL_MODEL in ("T2T1", "D001"): +if utils.LAYOUT == "TTV2": from .tt_v2.reset import * # noqa: F401,F403 -elif utils.INTERNAL_MODEL in ("T2B1",): +elif utils.LAYOUT == "TR": from .tr.reset import * # noqa: F401,F403 diff --git a/core/src/trezor/utils.py b/core/src/trezor/utils.py index eac363a59..597f015d4 100644 --- a/core/src/trezor/utils.py +++ b/core/src/trezor/utils.py @@ -4,6 +4,7 @@ from trezorutils import ( # noqa: F401 BITCOIN_ONLY, EMULATOR, INTERNAL_MODEL, + LAYOUT, MODEL, SCM_REVISION, USE_BACKLIGHT,