From f37ca13f1ae952f42da8d7bfb65baf427cd4175e Mon Sep 17 00:00:00 2001 From: Martin Milata Date: Thu, 13 May 2021 12:33:52 +0200 Subject: [PATCH] refactor(core): disable SD, SBU, fatfs for T1 build --- core/.changelog.d/1163.fixed | 1 + core/SConscript.firmware | 20 +++++++++++---- core/SConscript.unix | 20 +++++++++++---- core/embed/extmod/modtrezorio/modtrezorio.c | 16 ++++++------ core/src/apps/common/sdcard.py | 14 +++++------ core/src/apps/workflow_handlers.py | 5 ++-- core/src/boot.py | 1 - core/src/storage/sd_salt.py | 28 ++++++++++----------- core/src/trezor/__init__.py | 8 ------ core/src/trezor/sdcard.py | 23 ++++++++++++++--- core/tests/test_trezor.sdcard.py | 3 ++- 11 files changed, 86 insertions(+), 53 deletions(-) create mode 100644 core/.changelog.d/1163.fixed diff --git a/core/.changelog.d/1163.fixed b/core/.changelog.d/1163.fixed new file mode 100644 index 000000000..1f4b60d39 --- /dev/null +++ b/core/.changelog.d/1163.fixed @@ -0,0 +1 @@ +Disable TT features (SD card, SBU, FAT) for T1 build. diff --git a/core/SConscript.firmware b/core/SConscript.firmware index ffe262003..a9cf0bdbf 100644 --- a/core/SConscript.firmware +++ b/core/SConscript.firmware @@ -139,10 +139,13 @@ if FEATURE_FLAGS["SECP256K1_ZKP"]: # modtrezorio SOURCE_MOD += [ - 'embed/extmod/modtrezorio/ff.c', - 'embed/extmod/modtrezorio/ffunicode.c', 'embed/extmod/modtrezorio/modtrezorio.c', ] +if TREZOR_MODEL == "T": + SOURCE_MOD += [ + 'embed/extmod/modtrezorio/ff.c', + 'embed/extmod/modtrezorio/ffunicode.c', + ] # modtrezorui CPPPATH_MOD += [ @@ -340,8 +343,6 @@ SOURCE_TREZORHAL = [ 'embed/trezorhal/mpu.c', 'embed/trezorhal/random_delays.c', 'embed/trezorhal/rng.c', - 'embed/trezorhal/sbu.c', - 'embed/trezorhal/sdcard.c', 'embed/trezorhal/stm32.c', 'embed/trezorhal/systick.c', 'embed/trezorhal/touch.c', @@ -353,6 +354,11 @@ SOURCE_TREZORHAL = [ 'embed/trezorhal/util.s', 'embed/trezorhal/vectortable.s', ] +if TREZOR_MODEL == 'T': + SOURCE_TREZORHAL += [ + 'embed/trezorhal/sbu.c', + 'embed/trezorhal/sdcard.c', + ] CPPDEFINES_MOD += ['USE_SVC_SHUTDOWN'] @@ -561,7 +567,11 @@ if FROZEN: SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/common/*/*.py')) 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')) + SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/management/*.py', + exclude=[ + SOURCE_PY_DIR + 'apps/management/sd_protect.py', + ] if TREZOR_MODEL != 'T' else []) + ) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/management/*/*.py')) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/misc/*.py')) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/bitcoin/*.py')) diff --git a/core/SConscript.unix b/core/SConscript.unix index 7a48212d0..c0f75917d 100644 --- a/core/SConscript.unix +++ b/core/SConscript.unix @@ -134,10 +134,13 @@ if FEATURE_FLAGS["SECP256K1_ZKP"]: # modtrezorio SOURCE_MOD += [ - 'embed/extmod/modtrezorio/ff.c', - 'embed/extmod/modtrezorio/ffunicode.c', 'embed/extmod/modtrezorio/modtrezorio.c', ] +if TREZOR_MODEL == "T": + SOURCE_MOD += [ + 'embed/extmod/modtrezorio/ff.c', + 'embed/extmod/modtrezorio/ffunicode.c', + ] # modtrezorui CPPPATH_MOD += [ @@ -312,8 +315,6 @@ SOURCE_UNIX = [ 'embed/unix/profile.c', 'embed/unix/random_delays.c', 'embed/unix/rng.c', - 'embed/unix/sbu.c', - 'embed/unix/sdcard.c', 'embed/unix/touch.c', 'embed/unix/usb.c', 'vendor/micropython/lib/utils/gchelper_generic.c', @@ -322,6 +323,11 @@ SOURCE_UNIX = [ 'vendor/micropython/ports/unix/input.c', 'vendor/micropython/ports/unix/unix_mphal.c', ] +if TREZOR_MODEL == 'T': + SOURCE_UNIX += [ + 'embed/unix/sbu.c', + 'embed/unix/sdcard.c', + ] SOURCE_QSTR = SOURCE_MOD + SOURCE_MICROPYTHON + SOURCE_UNIX @@ -516,7 +522,11 @@ if FROZEN: SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/common/*/*.py')) 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')) + SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/management/*.py', + exclude=[ + SOURCE_PY_DIR + 'apps/management/sd_protect.py', + ] if TREZOR_MODEL != 'T' else []) + ) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/management/*/*.py')) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/misc/*.py')) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/bitcoin/*.py')) diff --git a/core/embed/extmod/modtrezorio/modtrezorio.c b/core/embed/extmod/modtrezorio/modtrezorio.c index 3b47687a2..3a8cd8994 100644 --- a/core/embed/extmod/modtrezorio/modtrezorio.c +++ b/core/embed/extmod/modtrezorio/modtrezorio.c @@ -36,16 +36,18 @@ } // clang-format off -#include "modtrezorio-fatfs.h" #include "modtrezorio-flash.h" #include "modtrezorio-hid.h" #include "modtrezorio-poll.h" -#include "modtrezorio-sbu.h" -#include "modtrezorio-sdcard.h" #include "modtrezorio-vcp.h" #include "modtrezorio-webusb.h" #include "modtrezorio-usb.h" // clang-format on +#if TREZOR_MODEL == T +#include "modtrezorio-fatfs.h" +#include "modtrezorio-sbu.h" +#include "modtrezorio-sdcard.h" +#endif /// package: trezorio.__init__ @@ -65,13 +67,13 @@ 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 TREZOR_MODEL == T {MP_ROM_QSTR(MP_QSTR_fatfs), MP_ROM_PTR(&mod_trezorio_fatfs_module)}, - - {MP_ROM_QSTR(MP_QSTR_FlashOTP), MP_ROM_PTR(&mod_trezorio_FlashOTP_type)}, - {MP_ROM_QSTR(MP_QSTR_SBU), MP_ROM_PTR(&mod_trezorio_SBU_type)}, - {MP_ROM_QSTR(MP_QSTR_sdcard), MP_ROM_PTR(&mod_trezorio_sdcard_module)}, +#endif + + {MP_ROM_QSTR(MP_QSTR_FlashOTP), MP_ROM_PTR(&mod_trezorio_FlashOTP_type)}, {MP_ROM_QSTR(MP_QSTR_USB), MP_ROM_PTR(&mod_trezorio_USB_type)}, {MP_ROM_QSTR(MP_QSTR_HID), MP_ROM_PTR(&mod_trezorio_HID_type)}, diff --git a/core/src/apps/common/sdcard.py b/core/src/apps/common/sdcard.py index cea46f302..27b51ad15 100644 --- a/core/src/apps/common/sdcard.py +++ b/core/src/apps/common/sdcard.py @@ -1,6 +1,6 @@ import storage.sd_salt from storage.sd_salt import SD_CARD_HOT_SWAPPABLE -from trezor import fatfs, sdcard, ui, wire +from trezor import io, sdcard, ui, wire from trezor.ui.layouts import confirm_action, show_error_and_raise @@ -131,8 +131,8 @@ async def ensure_sdcard( try: try: with sdcard.filesystem(mounted=False): - fatfs.mount() - except fatfs.NoFilesystem: + io.fatfs.mount() + except io.fatfs.NoFilesystem: # card not formatted. proceed out of the except clause pass else: @@ -143,9 +143,9 @@ async def ensure_sdcard( # Proceed to formatting. Failure is caught by the outside OSError handler with sdcard.filesystem(mounted=False): - fatfs.mkfs() - fatfs.mount() - fatfs.setlabel("TREZOR") + io.fatfs.mkfs() + io.fatfs.mount() + io.fatfs.setlabel("TREZOR") # format and mount succeeded return @@ -165,7 +165,7 @@ async def request_sd_salt( await ensure_sdcard(ctx, ensure_filesystem=False) try: return storage.sd_salt.load_sd_salt() - except (storage.sd_salt.WrongSdCard, fatfs.NoFilesystem): + except (storage.sd_salt.WrongSdCard, io.fatfs.NoFilesystem): await _confirm_retry_wrong_card(ctx) except OSError: # Generic problem with loading the SD salt (hardware problem, or we could diff --git a/core/src/apps/workflow_handlers.py b/core/src/apps/workflow_handlers.py index 3389b84fd..b81a6426b 100644 --- a/core/src/apps/workflow_handlers.py +++ b/core/src/apps/workflow_handlers.py @@ -46,11 +46,12 @@ def find_message_handler_module(msg_type: int) -> str: return "apps.management.apply_flags" elif msg_type == MessageType.ChangePin: return "apps.management.change_pin" - elif msg_type == MessageType.SdProtect: - return "apps.management.sd_protect" elif msg_type == MessageType.ChangeWipeCode: return "apps.management.change_wipe_code" + elif utils.MODEL == "T" and msg_type == MessageType.SdProtect: + return "apps.management.sd_protect" + # bitcoin elif msg_type == MessageType.AuthorizeCoinJoin: return "apps.bitcoin.authorize_coinjoin" diff --git a/core/src/boot.py b/core/src/boot.py index cd1f31ae3..8de0768fb 100644 --- a/core/src/boot.py +++ b/core/src/boot.py @@ -1,6 +1,5 @@ import storage import storage.device -import storage.sd_salt from trezor import config, log, loop, ui, utils, wire from trezor.pin import show_pin_timeout diff --git a/core/src/storage/sd_salt.py b/core/src/storage/sd_salt.py index 350452153..bccd8b5bc 100644 --- a/core/src/storage/sd_salt.py +++ b/core/src/storage/sd_salt.py @@ -1,7 +1,7 @@ from micropython import const import storage.device -from trezor import fatfs +from trezor import io from trezor.sdcard import with_filesystem from trezor.utils import consteq @@ -42,12 +42,12 @@ def _get_salt_path(new: bool = False) -> str: def _load_salt(auth_key: bytes, path: str) -> bytearray | None: # Load the salt file if it exists. try: - with fatfs.open(path, "r") as f: + with io.fatfs.open(path, "r") as f: salt = bytearray(SD_SALT_LEN_BYTES) stored_tag = bytearray(SD_SALT_AUTH_TAG_LEN_BYTES) f.read(salt) f.read(stored_tag) - except fatfs.FatFSError: + except io.fatfs.FatFSError: return None # Check the salt's authentication tag. @@ -81,22 +81,22 @@ def load_sd_salt() -> bytearray | None: # SD salt regeneration was interrupted earlier. Bring into consistent state. # TODO Possibly overwrite salt file with random data. try: - fatfs.unlink(salt_path) - except fatfs.FatFSError: + io.fatfs.unlink(salt_path) + except io.fatfs.FatFSError: pass - # fatfs.rename can fail with a write error, which falls through as an FatFSError. + # io.fatfs.rename can fail with a write error, which falls through as an FatFSError. # This should be handled in calling code, by allowing the user to retry. - fatfs.rename(new_salt_path, salt_path) + io.fatfs.rename(new_salt_path, salt_path) return salt @with_filesystem def set_sd_salt(salt: bytes, salt_tag: bytes, stage: bool = False) -> None: salt_path = _get_salt_path(stage) - fatfs.mkdir("/trezor", True) - fatfs.mkdir(_get_device_dir(), True) - with fatfs.open(salt_path, "w") as f: + io.fatfs.mkdir("/trezor", True) + io.fatfs.mkdir(_get_device_dir(), True) + with io.fatfs.open(salt_path, "w") as f: f.write(salt) f.write(salt_tag) @@ -107,14 +107,14 @@ def commit_sd_salt() -> None: new_salt_path = _get_salt_path(new=True) try: - fatfs.unlink(salt_path) - except fatfs.FatFSError: + io.fatfs.unlink(salt_path) + except io.fatfs.FatFSError: pass - fatfs.rename(new_salt_path, salt_path) + io.fatfs.rename(new_salt_path, salt_path) @with_filesystem def remove_sd_salt() -> None: salt_path = _get_salt_path() # TODO Possibly overwrite salt file with random data. - fatfs.unlink(salt_path) + io.fatfs.unlink(salt_path) diff --git a/core/src/trezor/__init__.py b/core/src/trezor/__init__.py index 9f7e623d8..80c7ec815 100644 --- a/core/src/trezor/__init__.py +++ b/core/src/trezor/__init__.py @@ -1,10 +1,2 @@ import trezorconfig as config # noqa: F401 import trezorio as io # noqa: F401 - -if False: - import trezorio.fatfs as fatfs -else: - # a bug in mypy causes a crash at _usage site_ of the following: - fatfs = io.fatfs - # hence the if False branch that does what mypy understands - but which doesn't - # actually work because `trezorio.fatfs` is not importable. diff --git a/core/src/trezor/sdcard.py b/core/src/trezor/sdcard.py index 01d0813ee..27cfe963e 100644 --- a/core/src/trezor/sdcard.py +++ b/core/src/trezor/sdcard.py @@ -1,4 +1,9 @@ -from trezorio import fatfs, sdcard +try: + from trezorio import fatfs, sdcard + + HAVE_SDCARD = True +except Exception: + HAVE_SDCARD = False if False: from typing import Any, Callable, TypeVar @@ -10,6 +15,9 @@ class FilesystemWrapper: _INSTANCE: "FilesystemWrapper" | None = None def __init__(self, mounted: bool) -> None: + if not HAVE_SDCARD: + raise RuntimeError + self.mounted = mounted self.counter = 0 @@ -57,5 +65,14 @@ def with_filesystem(func: T) -> T: return wrapped_func # type: ignore -is_present = sdcard.is_present -capacity = sdcard.capacity +if HAVE_SDCARD: + is_present = sdcard.is_present + capacity = sdcard.capacity + +else: + + def is_present() -> bool: + return False + + def capacity() -> int: + return 0 diff --git a/core/tests/test_trezor.sdcard.py b/core/tests/test_trezor.sdcard.py index f70853ac5..dff5e9fc1 100644 --- a/core/tests/test_trezor.sdcard.py +++ b/core/tests/test_trezor.sdcard.py @@ -1,7 +1,8 @@ from common import * -from trezor import io, fatfs, sdcard +from trezor import io, sdcard +fatfs = io.fatfs class TestTrezorSdcard(unittest.TestCase): def test_power(self):