1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-13 19:18:56 +00:00

trezor.config: use norcow

This commit is contained in:
Pavol Rusnak 2016-11-21 19:56:23 +01:00
parent 08484d8806
commit c6ea71901d
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
10 changed files with 30 additions and 72 deletions

3
.gitmodules vendored
View File

@ -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

View File

@ -14,16 +14,10 @@
#include "py/binary.h"
#include "py/objstr.h"
#include "norcow.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
typedef struct _mp_obj_Config_t {
mp_obj_base_t base;
} 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!)
/// '''
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);

View File

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

View File

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

View 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

View File

@ -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()

View File

@ -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()

View File

@ -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)

2
vendor/micropython vendored

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

1
vendor/norcow vendored Submodule

@ -0,0 +1 @@
Subproject commit 934b3cd1fbef79fd6b079b787e6e3225bbe5684c