trezor.config: use norcow

pull/25/head
Pavol Rusnak 8 years ago
parent 08484d8806
commit c6ea71901d
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

3
.gitmodules vendored

@ -7,3 +7,6 @@
[submodule "vendor/micropython"]
path = vendor/micropython
url = https://github.com/trezor/micropython.git
[submodule "vendor/norcow"]
path = vendor/norcow
url = https://github.com/trezor/norcow.git

@ -14,15 +14,9 @@
#include "py/binary.h"
#include "py/objstr.h"
#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
#include "norcow.h"
#if MICROPY_PY_TREZORCONFIG
typedef struct _mp_obj_Config_t {
mp_obj_base_t base;
@ -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!)
/// '''
STATIC mp_obj_t mod_TrezorConfig_Config_wipe(mp_obj_t self) {
// TODO
norcow_wipe();
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorConfig_Config_wipe_obj, mod_TrezorConfig_Config_wipe);

@ -0,0 +1 @@
../../vendor/norcow/norcow.c

@ -0,0 +1 @@
../../vendor/norcow/norcow.h

@ -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
import ustruct
from TrezorConfig import Config
# 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
_file = '/flash/trezor.config'
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 set(app, key, value):
return _config.set(app, key, value)
def wipe():
global _mock
_mock = {}
_save()
return _config.wipe()

@ -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):
config.wipe()
for _ in range(128):
for _ in range(64):
appid, key = random.uniform(256), random.uniform(256)
value = random.bytes(128)
config.set(appid, key, value)
@ -31,7 +31,7 @@ class TestConfig(unittest.TestCase):
def test_get_default(self):
config.wipe()
for _ in range(128):
for _ in range(64):
appid, key = random.uniform(256), random.uniform(256)
value = random.bytes(128)
value2 = config.get(appid, key, value)

@ -1 +1 @@
Subproject commit 81e11673b3b3f8b4cdc5c69585c9c61bf6dc808a
Subproject commit de483fcc9876fac6aaa98bfd916e5797b13ce7a8

1
vendor/norcow vendored

@ -0,0 +1 @@
Subproject commit 934b3cd1fbef79fd6b079b787e6e3225bbe5684c
Loading…
Cancel
Save