2016-05-23 13:44:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Pavol Rusnak, SatoshiLabs
|
|
|
|
*
|
2016-05-28 12:37:32 +00:00
|
|
|
* Licensed under TREZOR License
|
|
|
|
* see LICENSE file for details
|
2016-05-23 13:44:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
2016-11-21 18:56:23 +00:00
|
|
|
#include "norcow.h"
|
2016-10-14 16:40:30 +00:00
|
|
|
|
2016-11-21 18:56:23 +00:00
|
|
|
#if MICROPY_PY_TREZORCONFIG
|
2016-10-14 16:40:30 +00:00
|
|
|
|
2017-06-14 15:40:50 +00:00
|
|
|
/// class Config:
|
|
|
|
/// '''
|
|
|
|
/// Persistent key-value storage, with 16-bit keys and bytes values.
|
|
|
|
/// '''
|
2016-05-23 13:44:09 +00:00
|
|
|
typedef struct _mp_obj_Config_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
} mp_obj_Config_t;
|
|
|
|
|
2017-06-14 15:40:50 +00:00
|
|
|
/// def __init__(self):
|
|
|
|
/// '''
|
|
|
|
/// Initializes the storage.
|
|
|
|
/// '''
|
2017-06-14 16:47:38 +00:00
|
|
|
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) {
|
2016-05-23 13:44:09 +00:00
|
|
|
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;
|
2016-11-21 20:15:57 +00:00
|
|
|
bool r = norcow_init();
|
|
|
|
if (!r) {
|
|
|
|
mp_raise_msg(&mp_type_RuntimeError, "Could not initialize storage");
|
|
|
|
}
|
2016-05-23 13:44:09 +00:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
|
|
|
}
|
|
|
|
|
2017-06-14 15:40:50 +00:00
|
|
|
/// def get(self, app: int, key: int) -> bytes:
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2016-11-23 13:45:58 +00:00
|
|
|
/// Gets a value of given key for given app (or empty bytes if not set).
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC mp_obj_t mod_trezorconfig_Config_get(mp_obj_t self, mp_obj_t app, mp_obj_t key) {
|
2016-10-14 16:40:30 +00:00
|
|
|
uint8_t a = mp_obj_get_int(app);
|
|
|
|
uint8_t k = mp_obj_get_int(key);
|
2017-03-30 19:40:36 +00:00
|
|
|
uint16_t appkey = a << 8 | k, len;
|
2016-10-14 16:40:30 +00:00
|
|
|
const void *val;
|
|
|
|
bool r = norcow_get(appkey, &val, &len);
|
2016-11-23 13:45:58 +00:00
|
|
|
if (!r || len == 0) {
|
|
|
|
return mp_const_empty_bytes;
|
2016-11-21 20:15:57 +00:00
|
|
|
}
|
2016-10-14 16:40:30 +00:00
|
|
|
vstr_t vstr;
|
|
|
|
vstr_init_len(&vstr, len);
|
|
|
|
memcpy(vstr.buf, val, len);
|
|
|
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
2016-05-23 13:44:09 +00:00
|
|
|
}
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorconfig_Config_get_obj, mod_trezorconfig_Config_get);
|
2016-05-23 13:44:09 +00:00
|
|
|
|
2017-06-14 15:40:50 +00:00
|
|
|
/// def set(self, app: int, key: int, value: bytes) -> None:
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
|
|
|
/// Sets a value of given key for given app.
|
|
|
|
/// Returns True on success.
|
|
|
|
/// '''
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC mp_obj_t mod_trezorconfig_Config_set(size_t n_args, const mp_obj_t *args) {
|
2016-10-14 16:40:30 +00:00
|
|
|
uint8_t a = mp_obj_get_int(args[1]);
|
|
|
|
uint8_t k = mp_obj_get_int(args[2]);
|
|
|
|
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);
|
|
|
|
if (!r) {
|
2016-11-21 20:15:57 +00:00
|
|
|
mp_raise_msg(&mp_type_RuntimeError, "Could not save value");
|
2016-10-14 16:40:30 +00:00
|
|
|
}
|
2016-05-23 13:44:09 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorconfig_Config_set_obj, 4, 4, mod_trezorconfig_Config_set);
|
2016-05-23 13:44:09 +00:00
|
|
|
|
2017-06-14 15:40:50 +00:00
|
|
|
/// def wipe(self) -> None:
|
2016-11-06 11:38:33 +00:00
|
|
|
/// '''
|
2017-06-14 15:40:50 +00:00
|
|
|
/// Erases the whole config. Use with caution!
|
2016-11-06 11:38:33 +00:00
|
|
|
/// '''
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC mp_obj_t mod_trezorconfig_Config_wipe(mp_obj_t self) {
|
2016-11-21 20:15:57 +00:00
|
|
|
bool r = norcow_wipe();
|
|
|
|
if (!r) {
|
|
|
|
mp_raise_msg(&mp_type_RuntimeError, "Could not wipe storage");
|
|
|
|
}
|
2016-11-06 11:38:33 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorconfig_Config_wipe_obj, mod_trezorconfig_Config_wipe);
|
2016-11-06 11:38:33 +00:00
|
|
|
|
2017-06-14 16:47:38 +00:00
|
|
|
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) },
|
2016-05-23 13:44:09 +00:00
|
|
|
};
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mod_trezorconfig_Config_locals_dict, mod_trezorconfig_Config_locals_dict_table);
|
2016-05-23 13:44:09 +00:00
|
|
|
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC const mp_obj_type_t mod_trezorconfig_Config_type = {
|
2016-05-23 13:44:09 +00:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_Config,
|
2017-06-14 16:47:38 +00:00
|
|
|
.make_new = mod_trezorconfig_Config_make_new,
|
|
|
|
.locals_dict = (void*)&mod_trezorconfig_Config_locals_dict,
|
2016-05-23 13:44:09 +00:00
|
|
|
};
|
|
|
|
|
2017-06-14 16:47:38 +00:00
|
|
|
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) },
|
2016-05-23 13:44:09 +00:00
|
|
|
};
|
|
|
|
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_trezorconfig_globals, mp_module_trezorconfig_globals_table);
|
2016-05-23 13:44:09 +00:00
|
|
|
|
2017-06-14 16:47:38 +00:00
|
|
|
const mp_obj_module_t mp_module_trezorconfig = {
|
2016-05-23 13:44:09 +00:00
|
|
|
.base = { &mp_type_module },
|
2017-06-14 16:47:38 +00:00
|
|
|
.globals = (mp_obj_dict_t*)&mp_module_trezorconfig_globals,
|
2016-05-23 13:44:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MICROPY_PY_TREZORCONFIG
|