From e3607156d88b05ad9666615178f5cc62fe1d4f94 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Fri, 31 Aug 2018 15:52:26 +0200 Subject: [PATCH] embed: make model and emulator orthogonal, update macros to match this logic --- SConscript.boardloader | 2 +- SConscript.bootloader | 2 +- SConscript.firmware | 2 +- SConscript.prodtest | 2 +- SConscript.reflash | 2 +- SConscript.unix | 4 +-- embed/extmod/modtrezorio/modtrezorio-sdcard.h | 2 +- embed/extmod/modtrezorui/display.c | 8 ++--- embed/extmod/modtrezorutils/modtrezorutils.c | 32 ++++++++----------- src/apps/homescreen/__init__.py | 4 +-- src/trezor/ui/__init__.py | 5 ++- src/trezor/utils.py | 2 +- 12 files changed, 28 insertions(+), 39 deletions(-) diff --git a/SConscript.boardloader b/SConscript.boardloader index 91dda463f..77f03ad20 100644 --- a/SConscript.boardloader +++ b/SConscript.boardloader @@ -109,7 +109,7 @@ env.Replace( 'vendor/micropython/lib/cmsis/inc', ] + CPPPATH_MOD, CPPDEFINES=[ - 'TREZOR_MODEL_T', + ('TREZOR_MODEL', 'T'), 'STM32F427xx', 'USE_HAL_DRIVER', ('STM32_HAL_H', '""'), diff --git a/SConscript.bootloader b/SConscript.bootloader index ab5ad5d86..79c5befb2 100644 --- a/SConscript.bootloader +++ b/SConscript.bootloader @@ -131,7 +131,7 @@ env.Replace( 'vendor/micropython/lib/cmsis/inc', ] + CPPPATH_MOD, CPPDEFINES=[ - 'TREZOR_MODEL_T', + ('TREZOR_MODEL', 'T'), 'STM32F427xx', 'USE_HAL_DRIVER', ('STM32_HAL_H', '""'), diff --git a/SConscript.firmware b/SConscript.firmware index 4966b2525..619059813 100644 --- a/SConscript.firmware +++ b/SConscript.firmware @@ -324,7 +324,7 @@ env.Replace( 'vendor/micropython/ports/stm32', ] + CPPPATH_MOD, CPPDEFINES=[ - 'TREZOR_MODEL_T', + ('TREZOR_MODEL', 'T'), 'STM32F427xx', 'USE_HAL_DRIVER', ('STM32_HAL_H', '""'), diff --git a/SConscript.prodtest b/SConscript.prodtest index ed726790d..415ae8514 100644 --- a/SConscript.prodtest +++ b/SConscript.prodtest @@ -98,7 +98,7 @@ env.Replace( 'vendor/micropython/ports/stm32', ] + CPPPATH_MOD, CPPDEFINES=[ - 'TREZOR_MODEL_T', + ('TREZOR_MODEL', 'T'), 'STM32F427xx', ('STM32_HAL_H', '""'), ] + CPPDEFINES_MOD, diff --git a/SConscript.reflash b/SConscript.reflash index ed6a082e8..be2d88fc6 100644 --- a/SConscript.reflash +++ b/SConscript.reflash @@ -98,7 +98,7 @@ env.Replace( 'vendor/micropython/ports/stm32', ] + CPPPATH_MOD, CPPDEFINES=[ - 'TREZOR_MODEL_T', + ('TREZOR_MODEL', 'T'), 'STM32F427xx', ('STM32_HAL_H', '""'), ] + CPPDEFINES_MOD, diff --git a/SConscript.unix b/SConscript.unix index 720c699f3..e85aacfcf 100644 --- a/SConscript.unix +++ b/SConscript.unix @@ -265,8 +265,8 @@ env.Replace( 'vendor/micropython/lib/mp-readline', ] + CPPPATH_MOD, CPPDEFINES=[ - 'UNIX', - 'TREZOR_MODEL_EMU', + 'TREZOR_EMULATOR', + ('TREZOR_MODEL', 'T'), '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 b5ef5af19..3a2c1d4d3 100644 --- a/embed/extmod/modtrezorio/modtrezorio-sdcard.h +++ b/embed/extmod/modtrezorio/modtrezorio-sdcard.h @@ -35,7 +35,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_MODEL_EMU +#ifdef TREZOR_EMULATOR sdcard_init(); #endif return MP_OBJ_FROM_PTR(o); diff --git a/embed/extmod/modtrezorui/display.c b/embed/extmod/modtrezorui/display.c index eebf4e18f..afafdc109 100644 --- a/embed/extmod/modtrezorui/display.c +++ b/embed/extmod/modtrezorui/display.c @@ -47,12 +47,10 @@ static struct { int x, y; } DISPLAY_OFFSET; -#if defined TREZOR_MODEL_T -#include "display-stm32.h" -#elif defined TREZOR_MODEL_EMU +#ifdef TREZOR_EMULATOR #include "display-unix.h" #else -#error Unsupported TREZOR port. Only STM32 and UNIX ports are supported. +#include "display-stm32.h" #endif // common display functions @@ -392,7 +390,7 @@ void display_print(const char *text, int textlen) display_refresh(); } -#ifdef TREZOR_MODEL_EMU +#ifdef TREZOR_EMULATOR #define mini_vsnprintf vsnprintf #include #else diff --git a/embed/extmod/modtrezorutils/modtrezorutils.c b/embed/extmod/modtrezorutils/modtrezorutils.c index 13f752553..c106a6f0c 100644 --- a/embed/extmod/modtrezorutils/modtrezorutils.c +++ b/embed/extmod/modtrezorutils/modtrezorutils.c @@ -107,7 +107,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_MODEL_T +#ifndef TREZOR_EMULATOR __asm__ volatile("msr control, %0" :: "r" (0x1)); __asm__ volatile("isb"); #endif @@ -120,11 +120,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_set_mode_unprivileged_obj, mod_ /// Retrieve internal symbol. /// ''' STATIC mp_obj_t mod_trezorutils_symbol(mp_obj_t name) { +#define XSTR(s) STR(s) +#define STR(s) #s mp_buffer_info_t str; mp_get_buffer_raise(name, &str, MP_BUFFER_READ); if (0 == strncmp(str.buf, "GITREV", str.len)) { -#define XSTR(s) STR(s) -#define STR(s) #s return mp_obj_new_str(XSTR(GITREV), strlen(XSTR(GITREV))); } if (0 == strncmp(str.buf, "VERSION_MAJOR", str.len)) { @@ -136,25 +136,20 @@ STATIC mp_obj_t mod_trezorutils_symbol(mp_obj_t name) { if (0 == strncmp(str.buf, "VERSION_PATCH", str.len)) { return mp_obj_new_int(VERSION_PATCH); } + if (0 == strncmp(str.buf, "MODEL", str.len)) { + return mp_obj_new_str(XSTR(TREZOR_MODEL), strlen(XSTR(TREZOR_MODEL))); + } + if (0 == strncmp(str.buf, "EMULATOR", str.len)) { +#ifdef TREZOR_EMULATOR + return mp_const_true; +#else + return mp_const_false; +#endif + } return mp_const_none; } 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)) : 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) }, @@ -162,7 +157,6 @@ 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 ac3466b3d..b99612e28 100644 --- a/src/apps/homescreen/__init__.py +++ b/src/apps/homescreen/__init__.py @@ -15,9 +15,7 @@ def get_features(): f.minor_version = utils.symbol("VERSION_MINOR") f.patch_version = utils.symbol("VERSION_PATCH") f.revision = utils.symbol("GITREV") - f.model = utils.model() - if f.model == "EMU": - f.model = "T" # emulator currently emulates model T + f.model = utils.symbol("MODEL") f.device_id = storage.get_device_id() f.label = storage.get_label() f.initialized = storage.is_initialized() diff --git a/src/trezor/ui/__init__.py b/src/trezor/ui/__init__.py index c87e0a0d7..9b91064a0 100644 --- a/src/trezor/ui/__init__.py +++ b/src/trezor/ui/__init__.py @@ -3,8 +3,7 @@ import utime from micropython import const from trezorui import Display -from trezor import io, loop, res, workflow -from trezor.utils import model +from trezor import io, loop, res, workflow, utils display = Display() @@ -18,7 +17,7 @@ if __debug__: loop.after_step_hook = debug_display_refresh # in both debug and production, emulator needs to draw the screen explicitly -elif model() == "EMU": +elif utils.symbol("EMULATOR"): loop.after_step_hook = display.refresh # re-export constants from modtrezorui diff --git a/src/trezor/utils.py b/src/trezor/utils.py index 69c57d7ac..d85b8c218 100644 --- a/src/trezor/utils.py +++ b/src/trezor/utils.py @@ -1,6 +1,6 @@ import gc import sys -from trezorutils import halt, memcpy, model, set_mode_unprivileged, symbol # noqa: F401 +from trezorutils import halt, memcpy, set_mode_unprivileged, symbol # noqa: F401 def unimport_begin():