From 611d374bbd787f53a5e14537b0a941d4072ba3d1 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Sun, 4 Mar 2018 12:59:16 +0100 Subject: [PATCH] embed: rename TREZOR_STM32/UNIX to TREZOR_MODEL_T/EMU, introduce trezor.utils.model() --- SConscript.boardloader | 2 +- SConscript.bootloader | 2 +- SConscript.firmware | 2 +- SConscript.prodtest | 2 +- SConscript.reflash | 2 +- SConscript.unix | 2 +- embed/extmod/modtrezorio/modtrezorio-sdcard.h | 2 +- embed/extmod/modtrezorui/display.c | 6 +++--- embed/extmod/modtrezorutils/modtrezorutils.c | 18 +++++++++++++++++- src/apps/homescreen/__init__.py | 5 +++-- src/trezor/ui/__init__.py | 4 ++-- src/trezor/utils.py | 2 +- tests/unittest.py | 1 + 13 files changed, 34 insertions(+), 16 deletions(-) diff --git a/SConscript.boardloader b/SConscript.boardloader index 786557a3..6d1289f0 100644 --- a/SConscript.boardloader +++ b/SConscript.boardloader @@ -121,7 +121,7 @@ env.Replace( 'vendor/micropython/lib/cmsis/inc', ] + CPPPATH_MOD, CPPDEFINES=[ - 'TREZOR_STM32', + 'TREZOR_MODEL_T', 'MCU_SERIES_F4', 'STM32F427xx', 'USE_HAL_DRIVER', diff --git a/SConscript.bootloader b/SConscript.bootloader index 07fe7ef7..9ae742d3 100644 --- a/SConscript.bootloader +++ b/SConscript.bootloader @@ -143,7 +143,7 @@ env.Replace( 'vendor/micropython/lib/cmsis/inc', ] + CPPPATH_MOD, CPPDEFINES=[ - 'TREZOR_STM32', + 'TREZOR_MODEL_T', 'MCU_SERIES_F4', 'STM32F427xx', 'USE_HAL_DRIVER', diff --git a/SConscript.firmware b/SConscript.firmware index 3db755a1..805a9837 100644 --- a/SConscript.firmware +++ b/SConscript.firmware @@ -328,7 +328,7 @@ env.Replace( 'vendor/micropython/ports/stm32', ] + CPPPATH_MOD, CPPDEFINES=[ - 'TREZOR_STM32', + 'TREZOR_MODEL_T', 'MCU_SERIES_F4', 'STM32F427xx', 'USE_HAL_DRIVER', diff --git a/SConscript.prodtest b/SConscript.prodtest index 161c43e0..449dec48 100644 --- a/SConscript.prodtest +++ b/SConscript.prodtest @@ -110,7 +110,7 @@ env.Replace( 'vendor/micropython/ports/stm32', ] + CPPPATH_MOD, CPPDEFINES=[ - 'TREZOR_STM32', + 'TREZOR_MODEL_T', 'MCU_SERIES_F4', 'STM32F427xx', ('STM32_HAL_H', '""'), diff --git a/SConscript.reflash b/SConscript.reflash index e81c938d..57c4acae 100644 --- a/SConscript.reflash +++ b/SConscript.reflash @@ -110,7 +110,7 @@ env.Replace( 'vendor/micropython/ports/stm32', ] + CPPPATH_MOD, CPPDEFINES=[ - 'TREZOR_STM32', + 'TREZOR_MODEL_T', 'MCU_SERIES_F4', 'STM32F427xx', ('STM32_HAL_H', '""'), diff --git a/SConscript.unix b/SConscript.unix index 429c6441..6e6a71a3 100644 --- a/SConscript.unix +++ b/SConscript.unix @@ -261,7 +261,7 @@ env.Replace( ] + CPPPATH_MOD, CPPDEFINES=[ 'UNIX', - 'TREZOR_UNIX', + 'TREZOR_MODEL_EMU', 'MICROPY_USE_READLINE', ('MP_CONFIGFILE', '\\"embed/unix/mpconfigport.h\\"'), ] + CPPDEFINES_MOD, diff --git a/embed/extmod/modtrezorio/modtrezorio-sdcard.h b/embed/extmod/modtrezorio/modtrezorio-sdcard.h index d66c6661..635b7853 100644 --- a/embed/extmod/modtrezorio/modtrezorio-sdcard.h +++ b/embed/extmod/modtrezorio/modtrezorio-sdcard.h @@ -33,7 +33,7 @@ STATIC mp_obj_t mod_trezorio_SDCard_make_new(const mp_obj_type_t *type, size_t n mp_arg_check_num(n_args, n_kw, 0, 0, false); mp_obj_SDCard_t *o = m_new_obj(mp_obj_SDCard_t); o->base.type = type; -#if defined TREZOR_UNIX +#if defined TREZOR_MODEL_EMU sdcard_init(); #endif return MP_OBJ_FROM_PTR(o); diff --git a/embed/extmod/modtrezorui/display.c b/embed/extmod/modtrezorui/display.c index 389d9701..83d5c102 100644 --- a/embed/extmod/modtrezorui/display.c +++ b/embed/extmod/modtrezorui/display.c @@ -44,9 +44,9 @@ static struct { int x, y; } DISPLAY_OFFSET; -#if defined TREZOR_STM32 +#if defined TREZOR_MODEL_T #include "display-stm32.h" -#elif defined TREZOR_UNIX +#elif defined TREZOR_MODEL_EMU #include "display-unix.h" #else #error Unsupported TREZOR port. Only STM32 and UNIX ports are supported. @@ -385,7 +385,7 @@ void display_print(const char *text, int textlen) display_refresh(); } -#ifdef TREZOR_UNIX +#ifdef TREZOR_MODEL_EMU #define mini_vsnprintf vsnprintf #include #else diff --git a/embed/extmod/modtrezorutils/modtrezorutils.c b/embed/extmod/modtrezorutils/modtrezorutils.c index a017ba69..d609c031 100644 --- a/embed/extmod/modtrezorutils/modtrezorutils.c +++ b/embed/extmod/modtrezorutils/modtrezorutils.c @@ -113,7 +113,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorutils_halt_obj, 0, 1, mod_t /// Set unprivileged mode. /// ''' STATIC mp_obj_t mod_trezorutils_set_mode_unprivileged(void) { -#if defined TREZOR_STM32 +#if defined TREZOR_MODEL_T __asm__ volatile("msr control, %0" :: "r" (0x1)); __asm__ volatile("isb"); #endif @@ -146,6 +146,21 @@ STATIC mp_obj_t mod_trezorutils_symbol(mp_obj_t name) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorutils_symbol_obj, mod_trezorutils_symbol); +/// def model() -> str: +/// ''' +/// Return which hardware model we are running on. +/// ''' +STATIC mp_obj_t mod_trezorutils_model(void) { + const char *model = NULL; +#if defined TREZOR_MODEL_T + model = "T"; +#elif defined TREZOR_MODEL_EMU + model = "EMU"; +#endif + return model ? mp_obj_new_str(model, strlen(model), false) : mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_model_obj, mod_trezorutils_model); + STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_trezorutils) }, { MP_ROM_QSTR(MP_QSTR_consteq), MP_ROM_PTR(&mod_trezorutils_consteq_obj) }, @@ -153,6 +168,7 @@ STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_halt), MP_ROM_PTR(&mod_trezorutils_halt_obj) }, { MP_ROM_QSTR(MP_QSTR_set_mode_unprivileged), MP_ROM_PTR(&mod_trezorutils_set_mode_unprivileged_obj) }, { MP_ROM_QSTR(MP_QSTR_symbol), MP_ROM_PTR(&mod_trezorutils_symbol_obj) }, + { MP_ROM_QSTR(MP_QSTR_model), MP_ROM_PTR(&mod_trezorutils_model_obj) }, }; STATIC MP_DEFINE_CONST_DICT(mp_module_trezorutils_globals, mp_module_trezorutils_globals_table); diff --git a/src/apps/homescreen/__init__.py b/src/apps/homescreen/__init__.py index 08018d41..7cb3fd80 100644 --- a/src/apps/homescreen/__init__.py +++ b/src/apps/homescreen/__init__.py @@ -1,5 +1,5 @@ from trezor import config -from trezor.utils import unimport, symbol +from trezor.utils import unimport, symbol, model from trezor.wire import register, protobuf_workflow from trezor.messages import wire_types from trezor.messages.Features import Features @@ -31,7 +31,8 @@ async def respond_Features(ctx, msg): f.passphrase_cached = cache.has_passphrase() f.needs_backup = storage.needs_backup() f.flags = storage.get_flags() - f.model = 'T' + if model() in ['T', 'EMU']: # emulator currently emulates model T + f.model = 'T' return f diff --git a/src/trezor/ui/__init__.py b/src/trezor/ui/__init__.py index dac6ef78..eac273cd 100644 --- a/src/trezor/ui/__init__.py +++ b/src/trezor/ui/__init__.py @@ -1,6 +1,5 @@ from micropython import const -import sys import math import utime @@ -10,11 +9,12 @@ from trezor import io from trezor import loop from trezor import res from trezor import workflow +from trezor.utils import model display = Display() # for desktop platforms, we need to refresh the display after each frame -if sys.platform != 'trezor': +if model() == 'EMU': loop.after_step_hook = display.refresh # import constants from modtrezorui diff --git a/src/trezor/utils.py b/src/trezor/utils.py index c6f87014..ca2eab05 100644 --- a/src/trezor/utils.py +++ b/src/trezor/utils.py @@ -1,7 +1,7 @@ import sys import gc -from trezorutils import halt, memcpy, set_mode_unprivileged, symbol # noqa: F401 +from trezorutils import halt, memcpy, set_mode_unprivileged, symbol, model # noqa: F401 def unimport(genfunc): diff --git a/tests/unittest.py b/tests/unittest.py index c065c36a..dad09497 100644 --- a/tests/unittest.py +++ b/tests/unittest.py @@ -1,5 +1,6 @@ from trezor.utils import ensure + class SkipTest(Exception): pass