From 102b31ddea48923422bf4d2d9e486343c816e2f3 Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Mon, 27 Mar 2023 16:44:01 +0200 Subject: [PATCH] refactor(core): improve conditional compilations based on model [no changelog] --- core/SConscript.bootloader | 1 + core/SConscript.firmware | 7 +-- core/SConscript.unix | 8 ++++ .../extmod/modtrezorio/modtrezorio-poll.h | 7 ++- core/embed/extmod/modtrezorio/modtrezorio.c | 19 ++++++-- .../extmod/modtrezorutils/modtrezorutils.c | 20 +++----- core/embed/rust/Cargo.toml | 9 ++-- core/embed/unix/board-unix.h | 15 ++++++ core/embed/unix/touch/touch.c | 9 ++-- core/mocks/generated/trezorutils.pyi | 9 +--- core/site_scons/boards/trezor_1.py | 2 +- core/site_scons/boards/trezor_r_v3.py | 2 +- core/site_scons/boards/trezor_r_v4.py | 2 +- core/site_scons/boards/trezor_t.py | 2 + core/src/apps/common/request_pin.py | 47 ++++++++++++++----- core/src/storage/sd_salt.py | 6 +-- core/src/trezor/utils.py | 2 +- 17 files changed, 105 insertions(+), 62 deletions(-) diff --git a/core/SConscript.bootloader b/core/SConscript.bootloader index 3135b6de4..496d5cc7b 100644 --- a/core/SConscript.bootloader +++ b/core/SConscript.bootloader @@ -264,6 +264,7 @@ def cargo_build(): features.append("bitcoin_only") features.append("ui") features.append("bootloader") + features.extend(FEATURES_AVAILABLE) cargo_opts = [ f'--target={RUST_TARGET}', diff --git a/core/SConscript.firmware b/core/SConscript.firmware index 98a289516..2e3f85aef 100644 --- a/core/SConscript.firmware +++ b/core/SConscript.firmware @@ -166,11 +166,6 @@ if FEATURE_FLAGS["SECP256K1_ZKP"]: SOURCE_MOD += [ 'embed/extmod/modtrezorio/modtrezorio.c', ] -if TREZOR_MODEL in ('T',): - SOURCE_MOD += [ - 'embed/extmod/modtrezorio/ff.c', - 'embed/extmod/modtrezorio/ffunicode.c', - ] # modtrezorui CPPPATH_MOD += [ @@ -722,6 +717,8 @@ def cargo_build(): if DMA2D: features.append('dma2d') + features.extend(FEATURES_AVAILABLE) + cargo_opts = [ f'--target={RUST_TARGET}', f'--target-dir=../../build/firmware/rust', diff --git a/core/SConscript.unix b/core/SConscript.unix index 9f57a8abc..ecd444b55 100644 --- a/core/SConscript.unix +++ b/core/SConscript.unix @@ -686,6 +686,14 @@ def cargo_build(): if DMA2D: features.append('dma2d') + if TREZOR_MODEL in ('T',) : + features.append('touch') + if TREZOR_MODEL in ('R') : + features.append('buttons') + if TREZOR_MODEL in ('1') : + features.append('buttons') + + env.get('ENV')['TREZOR_MODEL'] = TREZOR_MODEL return f'cd embed/rust; cargo build --profile {RUST_PROFILE} --target-dir=../../build/unix/rust --no-default-features --features "{" ".join(features)}" --target {TARGET}' diff --git a/core/embed/extmod/modtrezorio/modtrezorio-poll.h b/core/embed/extmod/modtrezorio/modtrezorio-poll.h index 59fb7b737..da5b12247 100644 --- a/core/embed/extmod/modtrezorio/modtrezorio-poll.h +++ b/core/embed/extmod/modtrezorio/modtrezorio-poll.h @@ -82,7 +82,7 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref, if (false) { } -#if defined TREZOR_MODEL_T +#if defined USE_TOUCH else if (iface == TOUCH_IFACE) { const uint32_t evt = touch_read(); if (evt) { @@ -126,7 +126,8 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref, return mp_const_true; } } -#elif defined TREZOR_MODEL_1 || defined TREZOR_MODEL_R +#endif +#if USE_BUTTON else if (iface == BUTTON_IFACE) { const uint32_t evt = button_read(); if (evt & (BTN_EVT_DOWN | BTN_EVT_UP)) { @@ -143,8 +144,6 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref, return mp_const_true; } } -#else -#error Unknown Trezor model #endif else if (mode == POLL_READ) { if (sectrue == usb_hid_can_read(iface)) { diff --git a/core/embed/extmod/modtrezorio/modtrezorio.c b/core/embed/extmod/modtrezorio/modtrezorio.c index ca67f0aa4..7961ac4de 100644 --- a/core/embed/extmod/modtrezorio/modtrezorio.c +++ b/core/embed/extmod/modtrezorio/modtrezorio.c @@ -27,6 +27,7 @@ #include +#include TREZOR_BOARD #include "button.h" #include "touch/touch.h" #include "usb.h" @@ -47,9 +48,11 @@ bool usb_connected_previously = true; #include "modtrezorio-webusb.h" #include "modtrezorio-usb.h" // clang-format on -#if defined TREZOR_MODEL_T -#include "modtrezorio-fatfs.h" +#ifdef USE_SBU #include "modtrezorio-sbu.h" +#endif +#ifdef USE_SD_CARD +#include "modtrezorio-fatfs.h" #include "modtrezorio-sdcard.h" #endif @@ -77,16 +80,22 @@ bool usb_connected_previously = true; STATIC const mp_rom_map_elem_t mp_module_trezorio_globals_table[] = { {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_trezorio)}, -#if defined TREZOR_MODEL_T - {MP_ROM_QSTR(MP_QSTR_fatfs), MP_ROM_PTR(&mod_trezorio_fatfs_module)}, +#ifdef USE_SBU {MP_ROM_QSTR(MP_QSTR_SBU), MP_ROM_PTR(&mod_trezorio_SBU_type)}, +#endif + +#ifdef USE_SD_CARD + {MP_ROM_QSTR(MP_QSTR_fatfs), MP_ROM_PTR(&mod_trezorio_fatfs_module)}, {MP_ROM_QSTR(MP_QSTR_sdcard), MP_ROM_PTR(&mod_trezorio_sdcard_module)}, +#endif +#ifdef USE_TOUCH {MP_ROM_QSTR(MP_QSTR_TOUCH), MP_ROM_INT(TOUCH_IFACE)}, {MP_ROM_QSTR(MP_QSTR_TOUCH_START), MP_ROM_INT((TOUCH_START >> 24) & 0xFFU)}, {MP_ROM_QSTR(MP_QSTR_TOUCH_MOVE), MP_ROM_INT((TOUCH_MOVE >> 24) & 0xFFU)}, {MP_ROM_QSTR(MP_QSTR_TOUCH_END), MP_ROM_INT((TOUCH_END >> 24) & 0xFFU)}, -#elif defined TREZOR_MODEL_1 || defined TREZOR_MODEL_R +#endif +#ifdef USE_BUTTON {MP_ROM_QSTR(MP_QSTR_BUTTON), MP_ROM_INT(BUTTON_IFACE)}, {MP_ROM_QSTR(MP_QSTR_BUTTON_PRESSED), MP_ROM_INT((BTN_EVT_DOWN >> 24) & 0x3U)}, diff --git a/core/embed/extmod/modtrezorutils/modtrezorutils.c b/core/embed/extmod/modtrezorutils/modtrezorutils.c index 402125577..e3b00d77f 100644 --- a/core/embed/extmod/modtrezorutils/modtrezorutils.c +++ b/core/embed/extmod/modtrezorutils/modtrezorutils.c @@ -35,6 +35,7 @@ #include "common.h" #include "flash.h" #include "usb.h" +#include TREZOR_BOARD #ifndef TREZOR_EMULATOR #include "image.h" @@ -222,17 +223,6 @@ STATIC mp_obj_t mod_trezorutils_reboot_to_bootloader() { STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_reboot_to_bootloader_obj, mod_trezorutils_reboot_to_bootloader); -/// def usb_data_connected() -> bool: -/// """ -/// Returns whether USB has been enumerated/configured -/// (and is not just connected by cable without data pins) -/// """ -STATIC mp_obj_t mod_trezorutils_usb_data_connected() { - return usb_configured() == sectrue ? mp_const_true : mp_const_false; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_usb_data_connected_obj, - mod_trezorutils_usb_data_connected); - STATIC mp_obj_str_t mod_trezorutils_revision_obj = { {&mp_type_bytes}, 0, sizeof(SCM_REVISION) - 1, (const byte *)SCM_REVISION}; @@ -240,6 +230,7 @@ STATIC mp_obj_str_t mod_trezorutils_revision_obj = { /// VERSION_MAJOR: int /// VERSION_MINOR: int /// VERSION_PATCH: int +/// USE_SD_CARD: bool /// MODEL: str /// EMULATOR: bool /// BITCOIN_ONLY: bool @@ -255,14 +246,17 @@ STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = { MP_ROM_PTR(&mod_trezorutils_firmware_vendor_obj)}, {MP_ROM_QSTR(MP_QSTR_reboot_to_bootloader), MP_ROM_PTR(&mod_trezorutils_reboot_to_bootloader_obj)}, - {MP_ROM_QSTR(MP_QSTR_usb_data_connected), - MP_ROM_PTR(&mod_trezorutils_usb_data_connected_obj)}, // various built-in constants {MP_ROM_QSTR(MP_QSTR_SCM_REVISION), MP_ROM_PTR(&mod_trezorutils_revision_obj)}, {MP_ROM_QSTR(MP_QSTR_VERSION_MAJOR), MP_ROM_INT(VERSION_MAJOR)}, {MP_ROM_QSTR(MP_QSTR_VERSION_MINOR), MP_ROM_INT(VERSION_MINOR)}, {MP_ROM_QSTR(MP_QSTR_VERSION_PATCH), MP_ROM_INT(VERSION_PATCH)}, +#ifdef USE_SD_CARD + {MP_ROM_QSTR(MP_QSTR_USE_SD_CARD), mp_const_true}, +#else + {MP_ROM_QSTR(MP_QSTR_USE_SD_CARD), mp_const_false}, +#endif #if defined TREZOR_MODEL_1 {MP_ROM_QSTR(MP_QSTR_MODEL), MP_ROM_QSTR(MP_QSTR_1)}, #elif defined TREZOR_MODEL_T diff --git a/core/embed/rust/Cargo.toml b/core/embed/rust/Cargo.toml index 067866180..8ce50f85d 100644 --- a/core/embed/rust/Cargo.toml +++ b/core/embed/rust/Cargo.toml @@ -8,8 +8,8 @@ build = "build.rs" [features] default = ["model_tt"] bitcoin_only = [] -model_tt = ["touch", "jpeg"] -model_tr = ["buttons"] +model_tt = ["jpeg"] +model_tr = [] micropython = [] protobuf = ["micropython"] ui = [] @@ -22,7 +22,10 @@ touch = [] clippy = [] jpeg = [] debug = ["ui_debug"] -test = ["cc", "glob", "micropython", "protobuf", "ui", "ui_debug", "dma2d"] +sbu = [] +sdcard = [] +rgb_led = [] +test = ["cc", "glob", "micropython", "protobuf", "ui", "ui_debug", "dma2d", "touch"] [lib] crate-type = ["staticlib"] diff --git a/core/embed/unix/board-unix.h b/core/embed/unix/board-unix.h index 566d25211..32fd61a7e 100644 --- a/core/embed/unix/board-unix.h +++ b/core/embed/unix/board-unix.h @@ -1,6 +1,21 @@ #ifndef _BOARD_UNIX_H #define _BOARD_UNIX_H +#ifdef TREZOR_MODEL_T +#define USE_TOUCH 1 +#define USE_SD_CARD 1 +#define USE_SBU 1 +#endif + +#ifdef TREZOR_MODEL_1 +#define USE_BUTTON 1 +#endif + +#ifdef TREZOR_MODEL_R +#define USE_BUTTON 1 +#define USE_SBU 1 +#endif + #include "display-unix.h" #define USE_TOUCH 1 diff --git a/core/embed/unix/touch/touch.c b/core/embed/unix/touch/touch.c index e8c1f5abf..542737d6e 100644 --- a/core/embed/unix/touch/touch.c +++ b/core/embed/unix/touch/touch.c @@ -21,7 +21,8 @@ #include #include -#if defined TREZOR_MODEL_T +#include TREZOR_BOARD +#ifdef USE_TOUCH #include "common.h" #include "touch.h" @@ -85,7 +86,9 @@ void touch_power_on(void) {} uint32_t touch_is_detected(void) { return _touch_detected; } -#elif defined TREZOR_MODEL_1 || defined TREZOR_MODEL_R +#endif + +#ifdef USE_BUTTON #include "button.h" @@ -121,6 +124,4 @@ uint32_t button_read(void) { return 0; } -#else -#error Unknown Trezor model #endif diff --git a/core/mocks/generated/trezorutils.pyi b/core/mocks/generated/trezorutils.pyi index 177a42288..82afec52f 100644 --- a/core/mocks/generated/trezorutils.pyi +++ b/core/mocks/generated/trezorutils.pyi @@ -65,18 +65,11 @@ def reboot_to_bootloader() -> None: """ Reboots to bootloader. """ - - -# extmod/modtrezorutils/modtrezorutils.c -def usb_data_connected() -> bool: - """ - Returns whether USB has been enumerated/configured - (and is not just connected by cable without data pins) - """ SCM_REVISION: bytes VERSION_MAJOR: int VERSION_MINOR: int VERSION_PATCH: int +USE_SD_CARD: bool MODEL: str EMULATOR: bool BITCOIN_ONLY: bool diff --git a/core/site_scons/boards/trezor_1.py b/core/site_scons/boards/trezor_1.py index 52bc1cfe4..1cbc64a57 100644 --- a/core/site_scons/boards/trezor_1.py +++ b/core/site_scons/boards/trezor_1.py @@ -15,7 +15,7 @@ def configure(env, features_wanted, defines, sources): if "input" in features_wanted: sources += ['embed/trezorhal/button.c'] - features_available.append("button") + features_available.append("buttons") env.get('ENV')['TREZOR_BOARD'] = board diff --git a/core/site_scons/boards/trezor_r_v3.py b/core/site_scons/boards/trezor_r_v3.py index 8e4b13a50..f7e6cdcb7 100644 --- a/core/site_scons/boards/trezor_r_v3.py +++ b/core/site_scons/boards/trezor_r_v3.py @@ -15,7 +15,7 @@ def configure(env, features_wanted, defines, sources): if "input" in features_wanted: sources += ['embed/trezorhal/button.c'] - features_available.append("button") + features_available.append("buttons") if "rgb_led" in features_wanted: sources += ['embed/trezorhal/rgb_led.c'] diff --git a/core/site_scons/boards/trezor_r_v4.py b/core/site_scons/boards/trezor_r_v4.py index ef21bec5d..458d07ee8 100644 --- a/core/site_scons/boards/trezor_r_v4.py +++ b/core/site_scons/boards/trezor_r_v4.py @@ -15,7 +15,7 @@ def configure(env, features_wanted, defines, sources): if "input" in features_wanted: sources += ['embed/trezorhal/button.c'] - features_available.append("button") + features_available.append("buttons") if "sbu" in features_wanted: sources += ['embed/trezorhal/sbu.c', ] diff --git a/core/site_scons/boards/trezor_t.py b/core/site_scons/boards/trezor_t.py index 7025c9a73..5cf41fec1 100644 --- a/core/site_scons/boards/trezor_t.py +++ b/core/site_scons/boards/trezor_t.py @@ -20,6 +20,8 @@ def configure(env, features_wanted, defines, sources): if "sdcard" in features_wanted: sources += ['embed/trezorhal/sdcard.c', ] + sources += ['embed/extmod/modtrezorio/ff.c', ] + sources += ['embed/extmod/modtrezorio/ffunicode.c', ] features_available.append("sdcard") if "sbu" in features_wanted: diff --git a/core/src/apps/common/request_pin.py b/core/src/apps/common/request_pin.py index fb9c428aa..66bac810c 100644 --- a/core/src/apps/common/request_pin.py +++ b/core/src/apps/common/request_pin.py @@ -2,20 +2,46 @@ import utime from typing import TYPE_CHECKING import storage.cache as storage_cache -from trezor import config, wire - -from .sdcard import request_sd_salt +from trezor import config, utils, wire if TYPE_CHECKING: from typing import Any, NoReturn from trezor.wire import Context, GenericContext +async def _request_sd_salt( + ctx: wire.GenericContext, raise_cancelled_on_unavailable: bool = False +) -> bytearray | None: + """Helper to get SD salt in a general manner, working for all models. + + Is model-specific, because some models (like TR/T2B1) do not even + have SD card support (and we do not want to include SD-card connected code). + """ + from trezor import utils + + if not utils.USE_SD_CARD: + return None + else: + from .sdcard import request_sd_salt, SdCardUnavailable + + try: + return await request_sd_salt(ctx) + except SdCardUnavailable: + if raise_cancelled_on_unavailable: + raise wire.PinCancelled("SD salt is unavailable") + else: + raise + + def can_lock_device() -> bool: - """Return True if the device has a PIN set or SD-protect enabled.""" - import storage.sd_salt + """Return True if the device has a PIN set or SD-protect enabled (when supported).""" + # TR/T2B1 does not support SD card + if not utils.USE_SD_CARD: + return config.has_pin() + else: + import storage.sd_salt - return config.has_pin() or storage.sd_salt.is_enabled() + return config.has_pin() or storage.sd_salt.is_enabled() async def request_pin( @@ -56,7 +82,7 @@ async def request_pin_and_sd_salt( else: pin = "" - salt = await request_sd_salt(ctx) + salt = await _request_sd_salt(ctx) return pin, salt @@ -73,8 +99,6 @@ async def verify_user_pin( retry: bool = True, cache_time_ms: int = 0, ) -> None: - from .sdcard import SdCardUnavailable - # _get_last_unlock_time last_unlock = int.from_bytes( storage_cache.get(storage_cache.APP_COMMON_REQUEST_PIN_LAST_UNLOCK, b""), "big" @@ -98,10 +122,7 @@ async def verify_user_pin( else: pin = "" - try: - salt = await request_sd_salt(ctx) - except SdCardUnavailable: - raise wire.PinCancelled("SD salt is unavailable") + salt = await _request_sd_salt(ctx, raise_cancelled_on_unavailable=True) if config.unlock(pin, salt): _set_last_unlock_time() return diff --git a/core/src/storage/sd_salt.py b/core/src/storage/sd_salt.py index d1e350ef0..7fc3c43ae 100644 --- a/core/src/storage/sd_salt.py +++ b/core/src/storage/sd_salt.py @@ -2,7 +2,7 @@ from micropython import const from typing import TYPE_CHECKING import storage.device -from trezor import io +from trezor import io, utils from trezor.sdcard import with_filesystem if TYPE_CHECKING: @@ -10,8 +10,8 @@ if TYPE_CHECKING: T = TypeVar("T", bound=Callable) - -fatfs = io.fatfs # global_import_cache +if utils.USE_SD_CARD: + fatfs = io.fatfs # global_import_cache SD_CARD_HOT_SWAPPABLE = False SD_SALT_LEN_BYTES = const(32) diff --git a/core/src/trezor/utils.py b/core/src/trezor/utils.py index fcd796a4a..53814ccd0 100644 --- a/core/src/trezor/utils.py +++ b/core/src/trezor/utils.py @@ -5,6 +5,7 @@ from trezorutils import ( # noqa: F401 EMULATOR, MODEL, SCM_REVISION, + USE_SD_CARD, VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, @@ -14,7 +15,6 @@ from trezorutils import ( # noqa: F401 halt, memcpy, reboot_to_bootloader, - usb_data_connected, ) from typing import TYPE_CHECKING