1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-23 14:58:09 +00:00

refactor(core): improve conditional compilations based on model

[no changelog]
This commit is contained in:
tychovrahe 2023-03-27 16:44:01 +02:00 committed by TychoVrahe
parent eb72fb4241
commit 83500487b8
21 changed files with 118 additions and 77 deletions

View File

@ -18,7 +18,7 @@ if TREZOR_MODEL in ('1', ):
)
Return()
FEATURES_WANTED = ["sdcard"]
FEATURES_WANTED = ["sd_card"]
CCFLAGS_MOD = ''
CPPPATH_MOD = []

View File

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

View File

@ -19,7 +19,7 @@ FEATURE_FLAGS = {
"SYSTEM_VIEW": False,
}
FEATURES_WANTED = ["input", "sbu", "sdcard", "rgb_led"]
FEATURES_WANTED = ["input", "sbu", "sd_card", "rgb_led"]
CCFLAGS_MOD = ''
CPPPATH_MOD = []
@ -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 += [
@ -619,7 +614,7 @@ if FROZEN:
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 'sd_card' not in FEATURES_AVAILABLE else [])
)
SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/management/*/*.py'))
SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/misc/*.py'))
@ -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',

View File

@ -6,7 +6,7 @@ import tools
TREZOR_MODEL = ARGUMENTS.get('TREZOR_MODEL', 'T')
CMAKELISTS = int(ARGUMENTS.get('CMAKELISTS', 0))
FEATURES_WANTED = ["input", "sbu", "sdcard", "rdb_led"]
FEATURES_WANTED = ["input", "sbu", "sd_card", "rdb_led"]
CCFLAGS_MOD = ''
CPPPATH_MOD = []

View File

@ -6,7 +6,7 @@ import tools
TREZOR_MODEL = ARGUMENTS.get('TREZOR_MODEL', 'T')
CMAKELISTS = int(ARGUMENTS.get('CMAKELISTS', 0))
FEATURES_WANTED = ["input", "sdcard"]
FEATURES_WANTED = ["input", "sd_card"]
CCFLAGS_MOD = ''
CPPPATH_MOD = []

View File

@ -686,6 +686,12 @@ def cargo_build():
if DMA2D:
features.append('dma2d')
if TREZOR_MODEL in ('T',):
features.append('touch')
features.append('sd_card')
if TREZOR_MODEL in ('R', '1'):
features.append('button')
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}'

View File

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

View File

@ -27,6 +27,7 @@
#include <unistd.h>
#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)},
{MP_ROM_QSTR(MP_QSTR_sdcard), MP_ROM_PTR(&mod_trezorio_sdcard_module)},
#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)},

View File

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

View File

@ -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 = []
@ -17,12 +17,15 @@ dma2d = []
ui_debug = []
ui_bounds = []
bootloader = []
buttons = []
button = []
touch = []
clippy = []
jpeg = []
debug = ["ui_debug"]
test = ["cc", "glob", "micropython", "protobuf", "ui", "ui_debug", "dma2d"]
sbu = []
sd_card = []
rgb_led = []
test = ["cc", "glob", "micropython", "protobuf", "ui", "ui_debug", "dma2d", "touch"]
[lib]
crate-type = ["staticlib"]

View File

@ -1,6 +1,6 @@
use super::ffi;
#[cfg(feature = "buttons")]
#[cfg(feature = "button")]
pub use super::ffi::{BTN_EVT_DOWN, BTN_EVT_UP, BTN_LEFT, BTN_RIGHT};
#[cfg(feature = "touch")]
@ -8,7 +8,7 @@ pub fn io_touch_read() -> u32 {
unsafe { ffi::touch_read() }
}
#[cfg(feature = "buttons")]
#[cfg(feature = "button")]
pub fn io_button_read() -> u32 {
unsafe { ffi::button_read() }
}

View File

@ -11,7 +11,7 @@ use crate::{
},
};
#[cfg(feature = "buttons")]
#[cfg(feature = "button")]
use crate::ui::event::ButtonEvent;
#[cfg(feature = "touch")]
use crate::ui::event::TouchEvent;
@ -347,7 +347,7 @@ where
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum Event<'a> {
#[cfg(feature = "buttons")]
#[cfg(feature = "button")]
Button(ButtonEvent),
#[cfg(feature = "touch")]
Touch(TouchEvent),

View File

@ -23,7 +23,7 @@ use crate::{
},
};
#[cfg(feature = "buttons")]
#[cfg(feature = "button")]
use crate::ui::event::ButtonEvent;
#[cfg(feature = "touch")]
use crate::ui::event::TouchEvent;
@ -400,7 +400,7 @@ extern "C" fn ui_layout_touch_event(_n_args: usize, _args: *const Obj) -> Obj {
Obj::const_none()
}
#[cfg(feature = "buttons")]
#[cfg(feature = "button")]
extern "C" fn ui_layout_button_event(n_args: usize, args: *const Obj) -> Obj {
let block = |args: &[Obj], _kwargs: &Map| {
if args.len() != 3 {
@ -414,7 +414,7 @@ extern "C" fn ui_layout_button_event(n_args: usize, args: *const Obj) -> Obj {
unsafe { util::try_with_args_and_kwargs(n_args, args, &Map::EMPTY, block) }
}
#[cfg(not(feature = "buttons"))]
#[cfg(not(feature = "button"))]
extern "C" fn ui_layout_button_event(_n_args: usize, _args: *const Obj) -> Obj {
Obj::const_none()
}

View File

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

View File

@ -21,7 +21,8 @@
#include <stdbool.h>
#include <stdint.h>
#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

View File

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

View File

@ -18,9 +18,11 @@ def configure(env, features_wanted, defines, sources):
sources += ['embed/trezorhal/touch/ft6x36.c', ]
features_available.append("touch")
if "sdcard" in features_wanted:
if "sd_card" in features_wanted:
sources += ['embed/trezorhal/sdcard.c', ]
features_available.append("sdcard")
sources += ['embed/extmod/modtrezorio/ff.c', ]
sources += ['embed/extmod/modtrezorio/ffunicode.c', ]
features_available.append("sd_card")
if "sbu" in features_wanted:
sources += ['embed/trezorhal/sbu.c', ]

View File

@ -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
def can_lock_device() -> bool:
"""Return True if the device has a PIN set or SD-protect enabled."""
import storage.sd_salt
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.
return config.has_pin() or storage.sd_salt.is_enabled()
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 (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()
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

View File

@ -52,7 +52,7 @@ def _find_message_handler_module(msg_type: int) -> str:
elif msg_type == MessageType.RebootToBootloader:
return "apps.management.reboot_to_bootloader"
if utils.MODEL in ("T",) and msg_type == MessageType.SdProtect:
if utils.USE_SD_CARD and msg_type == MessageType.SdProtect:
return "apps.management.sd_protect"
# bitcoin

View File

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

View File

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