diff --git a/micropython/extmod/modtrezorconfig/modtrezorconfig.c b/micropython/extmod/modtrezorconfig/modtrezorconfig.c index 065a0542f5..4fd8232e60 100644 --- a/micropython/extmod/modtrezorconfig/modtrezorconfig.c +++ b/micropython/extmod/modtrezorconfig/modtrezorconfig.c @@ -5,44 +5,38 @@ * see LICENSE file for details */ -#include -#include -#include - #include "py/runtime.h" -#include "norcow.h" - #if MICROPY_PY_TREZORCONFIG -/// class Config: -/// ''' -/// Persistent key-value storage, with 16-bit keys and bytes values. -/// ''' -typedef struct _mp_obj_Config_t { - mp_obj_base_t base; -} mp_obj_Config_t; +#include +#include +#include "norcow.h" -/// def __init__(self): +static bool initialized = false; + +/// def init(self) -> None: /// ''' -/// Initializes the storage. +/// Initializes the storage. Must be called before any other method is called from this module! /// ''' -STATIC mp_obj_t mod_trezorconfig_Config_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { - mp_arg_check_num(n_args, n_kw, 0, 0, false); - mp_obj_Config_t *o = m_new_obj(mp_obj_Config_t); - o->base.type = type; +STATIC mp_obj_t mod_trezorconfig_init(void) { bool r = norcow_init(); if (!r) { - mp_raise_msg(&mp_type_RuntimeError, "Could not initialize storage"); + mp_raise_msg(&mp_type_RuntimeError, "Could not initialize config module"); } - return MP_OBJ_FROM_PTR(o); + initialized = true; + return mp_const_none; } +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorconfig_init_obj, mod_trezorconfig_init); -/// def get(self, app: int, key: int) -> bytes: +/// def get(app: int, key: int) -> bytes: /// ''' /// Gets a value of given key for given app (or empty bytes if not set). /// ''' -STATIC mp_obj_t mod_trezorconfig_Config_get(mp_obj_t self, mp_obj_t app, mp_obj_t key) { +STATIC mp_obj_t mod_trezorconfig_get(mp_obj_t app, mp_obj_t key) { + if (!initialized) { + mp_raise_msg(&mp_type_RuntimeError, "Config module not initialized"); + } uint8_t a = mp_obj_get_int(app); uint8_t k = mp_obj_get_int(key); uint16_t appkey = a << 8 | k, len; @@ -56,57 +50,51 @@ STATIC mp_obj_t mod_trezorconfig_Config_get(mp_obj_t self, mp_obj_t app, mp_obj_ memcpy(vstr.buf, val, len); return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } -STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorconfig_Config_get_obj, mod_trezorconfig_Config_get); +STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorconfig_get_obj, mod_trezorconfig_get); -/// def set(self, app: int, key: int, value: bytes) -> None: +/// def set(app: int, key: int, value: bytes) -> None: /// ''' /// Sets a value of given key for given app. -/// Returns True on success. /// ''' -STATIC mp_obj_t mod_trezorconfig_Config_set(size_t n_args, const mp_obj_t *args) { - uint8_t a = mp_obj_get_int(args[1]); - uint8_t k = mp_obj_get_int(args[2]); +STATIC mp_obj_t mod_trezorconfig_set(mp_obj_t app, mp_obj_t key, mp_obj_t value) { + if (!initialized) { + mp_raise_msg(&mp_type_RuntimeError, "Config module not initialized"); + } + uint8_t a = mp_obj_get_int(app); + uint8_t k = mp_obj_get_int(key); uint16_t appkey = a << 8 | k; - mp_buffer_info_t value; - mp_get_buffer_raise(args[3], &value, MP_BUFFER_READ); - bool r = norcow_set(appkey, value.buf, value.len); + mp_buffer_info_t v; + mp_get_buffer_raise(value, &v, MP_BUFFER_READ); + bool r = norcow_set(appkey, v.buf, v.len); if (!r) { mp_raise_msg(&mp_type_RuntimeError, "Could not save value"); } return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorconfig_Config_set_obj, 4, 4, mod_trezorconfig_Config_set); +STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorconfig_set_obj, mod_trezorconfig_set); /// def wipe(self) -> None: /// ''' /// Erases the whole config. Use with caution! /// ''' -STATIC mp_obj_t mod_trezorconfig_Config_wipe(mp_obj_t self) { +STATIC mp_obj_t mod_trezorconfig_wipe(void) { + if (!initialized) { + mp_raise_msg(&mp_type_RuntimeError, "Config module not initialized"); + } bool r = norcow_wipe(); if (!r) { mp_raise_msg(&mp_type_RuntimeError, "Could not wipe storage"); } return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorconfig_Config_wipe_obj, mod_trezorconfig_Config_wipe); - -STATIC const mp_rom_map_elem_t mod_trezorconfig_Config_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&mod_trezorconfig_Config_get_obj) }, - { MP_ROM_QSTR(MP_QSTR_set), MP_ROM_PTR(&mod_trezorconfig_Config_set_obj) }, - { MP_ROM_QSTR(MP_QSTR_wipe), MP_ROM_PTR(&mod_trezorconfig_Config_wipe_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(mod_trezorconfig_Config_locals_dict, mod_trezorconfig_Config_locals_dict_table); - -STATIC const mp_obj_type_t mod_trezorconfig_Config_type = { - { &mp_type_type }, - .name = MP_QSTR_Config, - .make_new = mod_trezorconfig_Config_make_new, - .locals_dict = (void*)&mod_trezorconfig_Config_locals_dict, -}; +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorconfig_wipe_obj, mod_trezorconfig_wipe); STATIC const mp_rom_map_elem_t mp_module_trezorconfig_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_trezorconfig) }, - { MP_ROM_QSTR(MP_QSTR_Config), MP_ROM_PTR(&mod_trezorconfig_Config_type) }, + { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&mod_trezorconfig_init_obj) }, + { MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&mod_trezorconfig_get_obj) }, + { MP_ROM_QSTR(MP_QSTR_set), MP_ROM_PTR(&mod_trezorconfig_set_obj) }, + { MP_ROM_QSTR(MP_QSTR_wipe), MP_ROM_PTR(&mod_trezorconfig_wipe_obj) }, }; STATIC MP_DEFINE_CONST_DICT(mp_module_trezorconfig_globals, mp_module_trezorconfig_globals_table); diff --git a/mocks/generated/trezorconfig.py b/mocks/generated/trezorconfig.py index 7df40decf6..0881917d88 100644 --- a/mocks/generated/trezorconfig.py +++ b/mocks/generated/trezorconfig.py @@ -1,28 +1,25 @@ from typing import * # extmod/modtrezorconfig/modtrezorconfig.c -class Config: +def init(self) -> None: ''' - Persistent key-value storage, with 16-bit keys and bytes values. + Initializes the storage. Must be called before any other method is called from this module! ''' - def __init__(self): - ''' - Initializes the storage. - ''' +# extmod/modtrezorconfig/modtrezorconfig.c +def get(app: int, key: int) -> bytes: + ''' + Gets a value of given key for given app (or empty bytes if not set). + ''' - def get(self, app: int, key: int) -> bytes: - ''' - Gets a value of given key for given app (or empty bytes if not set). - ''' +# extmod/modtrezorconfig/modtrezorconfig.c +def set(app: int, key: int, value: bytes) -> None: + ''' + Sets a value of given key for given app. + ''' - def set(self, app: int, key: int, value: bytes) -> None: - ''' - Sets a value of given key for given app. - Returns True on success. - ''' - - def wipe(self) -> None: - ''' - Erases the whole config. Use with caution! - ''' +# extmod/modtrezorconfig/modtrezorconfig.c +def wipe(self) -> None: + ''' + Erases the whole config. Use with caution! + ''' diff --git a/src/main.py b/src/main.py index fc5466e4e1..af45999182 100644 --- a/src/main.py +++ b/src/main.py @@ -1,10 +1,13 @@ from micropython import const import trezor.main +from trezor import config from trezor import msg from trezor import ui from trezor import wire +config.init() + # Load all applications if __debug__: from apps import debug diff --git a/src/trezor/__init__.py b/src/trezor/__init__.py index e69de29bb2..8fc42297a5 100644 --- a/src/trezor/__init__.py +++ b/src/trezor/__init__.py @@ -0,0 +1 @@ +import trezorconfig as config diff --git a/src/trezor/config.py b/src/trezor/config.py deleted file mode 100644 index dd743803d8..0000000000 --- a/src/trezor/config.py +++ /dev/null @@ -1,15 +0,0 @@ -from trezorconfig import Config - -_config = Config() - - -def get(app: int, key: int) -> bytes: - return _config.get(app, key) - - -def set(app: int, key: int, value: bytes): - return _config.set(app, key, value) - - -def wipe(): - return _config.wipe() diff --git a/tests/test_trezor.config.py b/tests/test_trezor.config.py index 2730f76f69..57639e6343 100644 --- a/tests/test_trezor.config.py +++ b/tests/test_trezor.config.py @@ -6,7 +6,13 @@ from trezor import config class TestConfig(unittest.TestCase): + def test_init(self): + config.init() + config.init() + config.init() + def test_wipe(self): + config.init() config.wipe() config.set(0, 0, b'hello') config.set(1, 1, b'world') @@ -21,6 +27,7 @@ class TestConfig(unittest.TestCase): self.assertEqual(v1, bytes()) def test_set_get(self): + config.init() config.wipe() for _ in range(64): appid, key = random.uniform(256), random.uniform(256) @@ -30,6 +37,7 @@ class TestConfig(unittest.TestCase): self.assertEqual(value, value2) def test_get_default(self): + config.init() config.wipe() for _ in range(64): appid, key = random.uniform(256), random.uniform(256)