mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-21 23:18:13 +00:00
trezor.config: use norcow
This commit is contained in:
parent
08484d8806
commit
c6ea71901d
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -7,3 +7,6 @@
|
|||||||
[submodule "vendor/micropython"]
|
[submodule "vendor/micropython"]
|
||||||
path = vendor/micropython
|
path = vendor/micropython
|
||||||
url = https://github.com/trezor/micropython.git
|
url = https://github.com/trezor/micropython.git
|
||||||
|
[submodule "vendor/norcow"]
|
||||||
|
path = vendor/norcow
|
||||||
|
url = https://github.com/trezor/norcow.git
|
||||||
|
@ -14,16 +14,10 @@
|
|||||||
#include "py/binary.h"
|
#include "py/binary.h"
|
||||||
#include "py/objstr.h"
|
#include "py/objstr.h"
|
||||||
|
|
||||||
|
#include "norcow.h"
|
||||||
|
|
||||||
#if MICROPY_PY_TREZORCONFIG
|
#if MICROPY_PY_TREZORCONFIG
|
||||||
|
|
||||||
|
|
||||||
// temporary function stubs from norcow
|
|
||||||
void norcow_init(void) { }
|
|
||||||
bool norcow_get(uint16_t key, const void **val, uint32_t *len) { *val = "Works!"; *len = 6; return true; }
|
|
||||||
bool norcow_set(uint16_t key, const void *val, uint32_t len) { return true; }
|
|
||||||
// end
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct _mp_obj_Config_t {
|
typedef struct _mp_obj_Config_t {
|
||||||
mp_obj_base_t base;
|
mp_obj_base_t base;
|
||||||
} mp_obj_Config_t;
|
} mp_obj_Config_t;
|
||||||
@ -79,7 +73,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorConfig_Config_set_obj, 4, 4
|
|||||||
/// Erases the whole config (use with caution!)
|
/// 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_Config_wipe(mp_obj_t self) {
|
||||||
// TODO
|
norcow_wipe();
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorConfig_Config_wipe_obj, mod_TrezorConfig_Config_wipe);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorConfig_Config_wipe_obj, mod_TrezorConfig_Config_wipe);
|
||||||
|
1
extmod/modtrezorconfig/norcow.c
Symbolic link
1
extmod/modtrezorconfig/norcow.c
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../vendor/norcow/norcow.c
|
1
extmod/modtrezorconfig/norcow.h
Symbolic link
1
extmod/modtrezorconfig/norcow.h
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../vendor/norcow/norcow.h
|
10
extmod/modtrezorconfig/norcow_config.h
Normal file
10
extmod/modtrezorconfig/norcow_config.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#ifdef UNIX
|
||||||
|
#define NORCOW_UNIX 1
|
||||||
|
#define NORCOW_FILE "/var/tmp/trezor.config"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef STM32_HAL_H
|
||||||
|
// TODO: switch to native implementation when finished
|
||||||
|
#define NORCOW_UNIX 1
|
||||||
|
#define NORCOW_FILE "/sd/trezor.config"
|
||||||
|
#endif
|
@ -1,52 +1,13 @@
|
|||||||
import sys
|
from TrezorConfig import Config
|
||||||
import ustruct
|
|
||||||
|
|
||||||
# mock implementation using binary file
|
_config = Config()
|
||||||
|
|
||||||
_mock = {}
|
def get(app, key, default=None):
|
||||||
|
v = _config.get(app, key)
|
||||||
|
return v if v else default
|
||||||
|
|
||||||
if sys.platform in ['trezor', 'pyboard']: # stmhal
|
def set(app, key, value):
|
||||||
_file = '/flash/trezor.config'
|
return _config.set(app, key, value)
|
||||||
else:
|
|
||||||
_file = '/var/tmp/trezor.config'
|
|
||||||
|
|
||||||
|
|
||||||
def _load():
|
|
||||||
global _mock
|
|
||||||
try:
|
|
||||||
with open(_file, 'rb') as f:
|
|
||||||
while True:
|
|
||||||
d = f.read(4)
|
|
||||||
if len(d) != 4:
|
|
||||||
break
|
|
||||||
k, l = ustruct.unpack('<HH', d)
|
|
||||||
v = f.read(l)
|
|
||||||
_mock[k] = v
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def _save():
|
|
||||||
global _mock
|
|
||||||
with open(_file, 'wb') as f:
|
|
||||||
for k, v in _mock.items():
|
|
||||||
f.write(ustruct.pack('<HH', k, len(v)))
|
|
||||||
f.write(v)
|
|
||||||
|
|
||||||
_load()
|
|
||||||
|
|
||||||
|
|
||||||
def get(app_id, key, default=None):
|
|
||||||
global _mock
|
|
||||||
return _mock.get((app_id << 8) | key, default)
|
|
||||||
|
|
||||||
|
|
||||||
def set(app_id, key, value):
|
|
||||||
global _mock
|
|
||||||
_mock[(app_id << 8) | key] = value
|
|
||||||
_save()
|
|
||||||
|
|
||||||
def wipe():
|
def wipe():
|
||||||
global _mock
|
return _config.wipe()
|
||||||
_mock = {}
|
|
||||||
_save()
|
|
||||||
|
@ -1,13 +0,0 @@
|
|||||||
from TrezorConfig import Config
|
|
||||||
|
|
||||||
_config = Config()
|
|
||||||
|
|
||||||
def get(app, key, default=None):
|
|
||||||
v = _config.get(app, key)
|
|
||||||
return v if v else default
|
|
||||||
|
|
||||||
def set(app, key, value):
|
|
||||||
return _config.set(app, key, value)
|
|
||||||
|
|
||||||
def wipe():
|
|
||||||
return _config.wipe()
|
|
@ -22,7 +22,7 @@ class TestConfig(unittest.TestCase):
|
|||||||
|
|
||||||
def test_set_get(self):
|
def test_set_get(self):
|
||||||
config.wipe()
|
config.wipe()
|
||||||
for _ in range(128):
|
for _ in range(64):
|
||||||
appid, key = random.uniform(256), random.uniform(256)
|
appid, key = random.uniform(256), random.uniform(256)
|
||||||
value = random.bytes(128)
|
value = random.bytes(128)
|
||||||
config.set(appid, key, value)
|
config.set(appid, key, value)
|
||||||
@ -31,7 +31,7 @@ class TestConfig(unittest.TestCase):
|
|||||||
|
|
||||||
def test_get_default(self):
|
def test_get_default(self):
|
||||||
config.wipe()
|
config.wipe()
|
||||||
for _ in range(128):
|
for _ in range(64):
|
||||||
appid, key = random.uniform(256), random.uniform(256)
|
appid, key = random.uniform(256), random.uniform(256)
|
||||||
value = random.bytes(128)
|
value = random.bytes(128)
|
||||||
value2 = config.get(appid, key, value)
|
value2 = config.get(appid, key, value)
|
||||||
|
2
vendor/micropython
vendored
2
vendor/micropython
vendored
@ -1 +1 @@
|
|||||||
Subproject commit 81e11673b3b3f8b4cdc5c69585c9c61bf6dc808a
|
Subproject commit de483fcc9876fac6aaa98bfd916e5797b13ce7a8
|
1
vendor/norcow
vendored
Submodule
1
vendor/norcow
vendored
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 934b3cd1fbef79fd6b079b787e6e3225bbe5684c
|
Loading…
Reference in New Issue
Block a user