refactor(core): disable SD, SBU, fatfs for T1 build

pull/1777/head
Martin Milata 3 years ago committed by matejcik
parent 0bff220fd6
commit f37ca13f1a

@ -0,0 +1 @@
Disable TT features (SD card, SBU, FAT) for T1 build.

@ -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'))

@ -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'))

@ -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)},

@ -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

@ -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"

@ -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

@ -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)

@ -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.

@ -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

@ -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):

Loading…
Cancel
Save