1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-12 18:49:07 +00:00

remove trezor.debug module, add trezor.io module

This commit is contained in:
Pavol Rusnak 2017-05-15 15:48:47 +02:00
parent b98c98e6e8
commit 3765fb734b
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
12 changed files with 119 additions and 111 deletions

View File

@ -25,7 +25,7 @@ BUILD_HDR = $(BUILD)/genhdr
MICROPY_PY_TREZORCONFIG = 1
MICROPY_PY_TREZORCRYPTO = 1
MICROPY_PY_TREZORDEBUG = 1
MICROPY_PY_TREZORIO = 1
MICROPY_PY_TREZORMSG = 1
MICROPY_PY_TREZORUI = 1
MICROPY_PY_TREZORUTILS = 1
@ -77,10 +77,10 @@ OBJ_MOD += $(addprefix $(BUILD_FW)/,\
)
endif
# OBJ micropython/extmod/modtrezordebug
ifeq ($(MICROPY_PY_TREZORDEBUG),1)
# OBJ micropython/extmod/modtrezorio
ifeq ($(MICROPY_PY_TREZORIO),1)
OBJ_MOD += $(addprefix $(BUILD_FW)/,\
extmod/modtrezordebug/modtrezordebug.o \
extmod/modtrezorio/modtrezorio.o \
)
endif

View File

@ -1,68 +0,0 @@
/*
* Copyright (c) Pavol Rusnak, SatoshiLabs
*
* Licensed under TREZOR License
* see LICENSE file for details
*/
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "py/runtime.h"
#include "py/objstr.h"
#if MICROPY_PY_TREZORDEBUG
typedef struct _mp_obj_Debug_t {
mp_obj_base_t base;
} mp_obj_Debug_t;
STATIC mp_obj_t mod_TrezorDebug_Debug_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_Debug_t *o = m_new_obj(mp_obj_Debug_t);
o->base.type = type;
return MP_OBJ_FROM_PTR(o);
}
/// def trezor.debug.memaccess(address: int, length: int) -> bytes:
/// '''
/// Creates a bytes object that can be used to access certain memory location.
/// '''
STATIC mp_obj_t mod_TrezorDebug_Debug_memaccess(mp_obj_t self, mp_obj_t address, mp_obj_t length) {
uint32_t addr = mp_obj_get_int(address);
uint32_t len = mp_obj_get_int(length);
mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
o->base.type = &mp_type_bytes;
o->len = len;
o->hash = 0;
o->data = (byte *)(uintptr_t)addr;
return MP_OBJ_FROM_PTR(o);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorDebug_Debug_memaccess_obj, mod_TrezorDebug_Debug_memaccess);
STATIC const mp_rom_map_elem_t mod_TrezorDebug_Debug_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_memaccess), MP_ROM_PTR(&mod_TrezorDebug_Debug_memaccess_obj) },
};
STATIC MP_DEFINE_CONST_DICT(mod_TrezorDebug_Debug_locals_dict, mod_TrezorDebug_Debug_locals_dict_table);
STATIC const mp_obj_type_t mod_TrezorDebug_Debug_type = {
{ &mp_type_type },
.name = MP_QSTR_Debug,
.make_new = mod_TrezorDebug_Debug_make_new,
.locals_dict = (void*)&mod_TrezorDebug_Debug_locals_dict,
};
STATIC const mp_rom_map_elem_t mp_module_TrezorDebug_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_TrezorDebug) },
{ MP_ROM_QSTR(MP_QSTR_Debug), MP_ROM_PTR(&mod_TrezorDebug_Debug_type) },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_TrezorDebug_globals, mp_module_TrezorDebug_globals_table);
const mp_obj_module_t mp_module_TrezorDebug = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_TrezorDebug_globals,
};
#endif // MICROPY_PY_TREZORDEBUG

View File

@ -0,0 +1,71 @@
/*
* Copyright (c) Pavol Rusnak, SatoshiLabs
*
* Licensed under TREZOR License
* see LICENSE file for details
*/
#include "sdcard.h"
typedef struct _mp_obj_SDCard_t {
mp_obj_base_t base;
} mp_obj_SDCard_t;
STATIC mp_obj_t mod_TrezorIO_SDCard_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_SDCard_t *o = m_new_obj(mp_obj_SDCard_t);
o->base.type = type;
return MP_OBJ_FROM_PTR(o);
}
STATIC mp_obj_t mod_TrezorIO_SDCard_present(mp_obj_t self) {
return mp_obj_new_bool(sdcard_is_present());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorIO_SDCard_present_obj, mod_TrezorIO_SDCard_present);
STATIC mp_obj_t mod_TrezorIO_SDCard_power(mp_obj_t self, mp_obj_t state) {
if (mp_obj_is_true(state)) {
return mp_obj_new_bool(sdcard_power_on());
} else {
return mp_obj_new_bool(sdcard_power_off());
}
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorIO_SDCard_power_obj, mod_TrezorIO_SDCard_power);
STATIC mp_obj_t mod_TrezorIO_SDCard_capacity(mp_obj_t self) {
return mp_obj_new_int_from_ull(sdcard_get_capacity_in_bytes());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorIO_SDCard_capacity_obj, mod_TrezorIO_SDCard_capacity);
STATIC mp_obj_t mod_TrezorIO_SDCard_read(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE);
mp_uint_t ret = sdcard_read_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SDCARD_BLOCK_SIZE);
return mp_obj_new_bool(ret == 0);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorIO_SDCard_read_obj, mod_TrezorIO_SDCard_read);
STATIC mp_obj_t mod_TrezorIO_SDCard_write(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
mp_uint_t ret = sdcard_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SDCARD_BLOCK_SIZE);
return mp_obj_new_bool(ret == 0);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorIO_SDCard_write_obj, mod_TrezorIO_SDCard_write);
STATIC const mp_rom_map_elem_t mod_TrezorIO_SDCard_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_present), MP_ROM_PTR(&mod_TrezorIO_SDCard_present_obj) },
{ MP_ROM_QSTR(MP_QSTR_power), MP_ROM_PTR(&mod_TrezorIO_SDCard_power_obj) },
{ MP_ROM_QSTR(MP_QSTR_capacity), MP_ROM_PTR(&mod_TrezorIO_SDCard_capacity_obj) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(SDCARD_BLOCK_SIZE) },
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mod_TrezorIO_SDCard_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mod_TrezorIO_SDCard_write_obj) },
};
STATIC MP_DEFINE_CONST_DICT(mod_TrezorIO_SDCard_locals_dict, mod_TrezorIO_SDCard_locals_dict_table);
STATIC const mp_obj_type_t mod_TrezorIO_SDCard_type = {
{ &mp_type_type },
.name = MP_QSTR_SDCard,
.make_new = mod_TrezorIO_SDCard_make_new,
.locals_dict = (void*)&mod_TrezorIO_SDCard_locals_dict,
};

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) Pavol Rusnak, SatoshiLabs
*
* Licensed under TREZOR License
* see LICENSE file for details
*/
#include "py/runtime.h"
#if MICROPY_PY_TREZORIO
#include "modtrezorio-sdcard.h"
STATIC const mp_rom_map_elem_t mp_module_TrezorIO_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_TrezorIO) },
{ MP_ROM_QSTR(MP_QSTR_SDCard), MP_ROM_PTR(&mod_TrezorIO_SDCard_type) },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_TrezorIO_globals, mp_module_TrezorIO_globals_table);
const mp_obj_module_t mp_module_TrezorIO = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_TrezorIO_globals,
};
#endif // MICROPY_PY_TREZORIO

View File

@ -5,13 +5,11 @@
* see LICENSE file for details
*/
#include <stdio.h>
#include <string.h>
#include "py/runtime.h"
#if MICROPY_PY_TREZORUTILS
#include <string.h>
#include "common.h"
/// def trezor.utils.memcpy(dst: bytearray, dst_ofs: int,

View File

@ -104,7 +104,7 @@
#define MICROPY_PY_UTIMEQ (1)
#define MICROPY_PY_TREZORCONFIG (1)
#define MICROPY_PY_TREZORCRYPTO (1)
#define MICROPY_PY_TREZORDEBUG (1)
#define MICROPY_PY_TREZORIO (1)
#define MICROPY_PY_TREZORMSG (1)
#define MICROPY_PY_TREZORUI (1)
#define MICROPY_PY_TREZORUTILS (1)
@ -151,7 +151,7 @@ static inline mp_uint_t disable_irq(void) {
extern const struct _mp_obj_module_t mp_module_utime;
extern const struct _mp_obj_module_t mp_module_TrezorConfig;
extern const struct _mp_obj_module_t mp_module_TrezorCrypto;
extern const struct _mp_obj_module_t mp_module_TrezorDebug;
extern const struct _mp_obj_module_t mp_module_TrezorIO;
extern const struct _mp_obj_module_t mp_module_TrezorMsg;
extern const struct _mp_obj_module_t mp_module_TrezorUi;
extern const struct _mp_obj_module_t mp_module_TrezorUtils;
@ -159,7 +159,7 @@ extern const struct _mp_obj_module_t mp_module_TrezorUtils;
{ MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&mp_module_utime }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_TrezorConfig), (mp_obj_t)&mp_module_TrezorConfig }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_TrezorCrypto), (mp_obj_t)&mp_module_TrezorCrypto }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_TrezorDebug), (mp_obj_t)&mp_module_TrezorDebug }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_TrezorIO), (mp_obj_t)&mp_module_TrezorIO }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_TrezorMsg), (mp_obj_t)&mp_module_TrezorMsg }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_TrezorUi), (mp_obj_t)&mp_module_TrezorUi }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_TrezorUtils), (mp_obj_t)&mp_module_TrezorUtils },

View File

@ -120,12 +120,13 @@ error:
return false;
}
void sdcard_power_off(void) {
bool sdcard_power_off(void) {
if (!sd_handle.Instance) {
return;
return true;
}
HAL_SD_DeInit(&sd_handle);
sd_handle.Instance = NULL;
return true;
}
uint64_t sdcard_get_capacity_in_bytes(void) {

View File

@ -35,7 +35,7 @@
int sdcard_init(void);
bool sdcard_is_present(void);
bool sdcard_power_on(void);
void sdcard_power_off(void);
bool sdcard_power_off(void);
uint64_t sdcard_get_capacity_in_bytes(void);
// these return 0 on success, non-zero on error

View File

@ -2,7 +2,7 @@ MICROPY_PY_TREZORCONFIG = 1
MICROPY_PY_TREZORCRYPTO = 1
MICROPY_PY_TREZORDEBUG = 1
MICROPY_PY_TREZORIO = 0
MICROPY_PY_TREZORMSG = 1
@ -58,9 +58,9 @@ SRC_MOD += \
$(EXTMOD_DIR)/modtrezorcrypto/trezor-crypto/sha3.c
endif
# OBJ micropython/extmod/modtrezordebug
ifeq ($(MICROPY_PY_TREZORDEBUG),1)
SRC_MOD += $(EXTMOD_DIR)/modtrezordebug/modtrezordebug.c
# OBJ micropython/extmod/modtrezorio
ifeq ($(MICROPY_PY_TREZORIO),1)
SRC_MOD += $(EXTMOD_DIR)/modtrezorio/modtrezorio.c
endif
# OBJ micropython/extmod/modtrezormsg

View File

@ -134,7 +134,7 @@
#define MICROPY_PY_TREZORCONFIG (1)
#define MICROPY_PY_TREZORCRYPTO (1)
#define MICROPY_PY_TREZORDEBUG (1)
#define MICROPY_PY_TREZORIO (0)
#define MICROPY_PY_TREZORMSG (1)
#define MICROPY_PY_TREZORUI (1)
#define MICROPY_PY_TREZORUTILS (1)
@ -228,10 +228,10 @@ extern const struct _mp_obj_module_t mp_module_TrezorUtils;
#else
#define MICROPY_PY_TREZORCRYPTO_DEF
#endif
#if MICROPY_PY_TREZORDEBUG
#define MICROPY_PY_TREZORDEBUG_DEF { MP_ROM_QSTR(MP_QSTR_TrezorDebug), MP_ROM_PTR(&mp_module_TrezorDebug) },
#if MICROPY_PY_TREZORIO
#define MICROPY_PY_TREZORIO_DEF { MP_ROM_QSTR(MP_QSTR_TrezorIO), MP_ROM_PTR(&mp_module_TrezorIO) },
#else
#define MICROPY_PY_TREZORDEBUGI_DEF
#define MICROPY_PY_TREZORIO_DEF
#endif
#if MICROPY_PY_TREZORMSG
#define MICROPY_PY_TREZORMSG_DEF { MP_ROM_QSTR(MP_QSTR_TrezorMsg), MP_ROM_PTR(&mp_module_TrezorMsg) },
@ -261,7 +261,7 @@ extern const struct _mp_obj_module_t mp_module_TrezorUtils;
MICROPY_PY_TERMIOS_DEF \
MICROPY_PY_TREZORCONFIG_DEF \
MICROPY_PY_TREZORCRYPTO_DEF \
MICROPY_PY_TREZORDEBUG_DEF \
MICROPY_PY_TREZORIO_DEF \
MICROPY_PY_TREZORMSG_DEF \
MICROPY_PY_TREZORUI_DEF \
MICROPY_PY_TREZORUTILS_DEF \

View File

@ -1,21 +0,0 @@
from micropython import const
if not __debug__:
raise ImportError('This module can be loaded only in DEBUG mode')
from TrezorDebug import Debug
_debug = Debug()
MEM_FLASH_BASE = const(0x08000000)
MEM_FLASH_SIZE = const(1024 * 1024)
MEM_FLASH_END = const(MEM_FLASH_BASE + MEM_FLASH_SIZE - 1)
MEM_CCM_BASE = const(0x10000000)
MEM_CCM_SIZE = const(64 * 1024)
MEM_CCM_END = const(MEM_CCM_BASE + MEM_CCM_SIZE - 1)
MEM_SRAM_BASE = const(0x20000000)
MEM_SRAM_SIZE = const(128 * 1024)
MEM_SRAM_END = const(MEM_SRAM_BASE + MEM_SRAM_SIZE - 1)
def memaccess(address, length):
return _debug.memaccess(address, length)

1
src/trezor/io.py Normal file
View File

@ -0,0 +1 @@
from TrezorIO import SDCard