2017-05-15 13:48:47 +00:00
|
|
|
/*
|
2018-02-26 13:06:10 +00:00
|
|
|
* This file is part of the TREZOR project, https://trezor.io/
|
2017-05-15 13:48:47 +00:00
|
|
|
*
|
2018-02-26 13:06:10 +00:00
|
|
|
* Copyright (c) SatoshiLabs
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-05-15 13:48:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sdcard.h"
|
|
|
|
|
2018-05-16 12:42:41 +00:00
|
|
|
#include "embed/extmod/trezorobj.h"
|
|
|
|
|
2017-06-14 16:07:54 +00:00
|
|
|
/// class SDCard:
|
|
|
|
/// '''
|
|
|
|
/// '''
|
2017-05-15 13:48:47 +00:00
|
|
|
typedef struct _mp_obj_SDCard_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
} mp_obj_SDCard_t;
|
|
|
|
|
2017-06-14 16:07:54 +00:00
|
|
|
/// def __init__(self) -> None:
|
|
|
|
/// '''
|
|
|
|
/// '''
|
2017-06-14 16:47:38 +00:00
|
|
|
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) {
|
2017-05-15 13:48:47 +00:00
|
|
|
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;
|
2018-03-04 11:59:16 +00:00
|
|
|
#if defined TREZOR_MODEL_EMU
|
2017-12-15 00:00:00 +00:00
|
|
|
sdcard_init();
|
|
|
|
#endif
|
2017-05-15 13:48:47 +00:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
|
|
|
}
|
|
|
|
|
2017-06-14 16:07:54 +00:00
|
|
|
/// def present(self) -> bool:
|
|
|
|
/// '''
|
|
|
|
/// Returns True if SD card is detected, False otherwise.
|
|
|
|
/// '''
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC mp_obj_t mod_trezorio_SDCard_present(mp_obj_t self) {
|
2017-05-15 13:48:47 +00:00
|
|
|
return mp_obj_new_bool(sdcard_is_present());
|
|
|
|
}
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_SDCard_present_obj, mod_trezorio_SDCard_present);
|
2017-05-15 13:48:47 +00:00
|
|
|
|
2017-06-14 16:07:54 +00:00
|
|
|
/// def power(self, state: bool) -> bool:
|
|
|
|
/// '''
|
|
|
|
/// Power on or power off the SD card interface.
|
|
|
|
/// Returns True if in case of success, False otherwise.
|
|
|
|
/// '''
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC mp_obj_t mod_trezorio_SDCard_power(mp_obj_t self, mp_obj_t state) {
|
2017-05-15 13:48:47 +00:00
|
|
|
if (mp_obj_is_true(state)) {
|
|
|
|
return mp_obj_new_bool(sdcard_power_on());
|
|
|
|
} else {
|
2017-12-16 17:42:06 +00:00
|
|
|
sdcard_power_off();
|
|
|
|
return mp_const_true;
|
2017-05-15 13:48:47 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorio_SDCard_power_obj, mod_trezorio_SDCard_power);
|
2017-05-15 13:48:47 +00:00
|
|
|
|
2017-06-14 16:07:54 +00:00
|
|
|
/// def capacity(self) -> int:
|
|
|
|
/// '''
|
|
|
|
/// Returns capacity of the SD card in bytes, or zero if not present.
|
|
|
|
/// '''
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC mp_obj_t mod_trezorio_SDCard_capacity(mp_obj_t self) {
|
2017-05-15 13:48:47 +00:00
|
|
|
return mp_obj_new_int_from_ull(sdcard_get_capacity_in_bytes());
|
|
|
|
}
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_SDCard_capacity_obj, mod_trezorio_SDCard_capacity);
|
2017-05-15 13:48:47 +00:00
|
|
|
|
2017-06-14 16:07:54 +00:00
|
|
|
/// def read(self, block_num: int, buf: bytearray) -> bool:
|
|
|
|
/// '''
|
2017-09-05 06:04:21 +00:00
|
|
|
/// Reads blocks starting with block_num from the SD card into buf.
|
|
|
|
/// Number of bytes read is length of buf rounded down to multiply of SDCARD_BLOCK_SIZE.
|
2017-06-14 16:07:54 +00:00
|
|
|
/// Returns True if in case of success, False otherwise.
|
|
|
|
/// '''
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC mp_obj_t mod_trezorio_SDCard_read(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
|
2018-05-16 12:42:41 +00:00
|
|
|
uint32_t block = trezor_obj_get_uint(block_num);
|
2017-05-15 13:48:47 +00:00
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE);
|
2018-05-16 12:42:41 +00:00
|
|
|
return mp_obj_new_bool(sdcard_read_blocks(bufinfo.buf, block, bufinfo.len / SDCARD_BLOCK_SIZE));
|
2017-05-15 13:48:47 +00:00
|
|
|
}
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_SDCard_read_obj, mod_trezorio_SDCard_read);
|
2017-05-15 13:48:47 +00:00
|
|
|
|
2017-06-14 16:07:54 +00:00
|
|
|
/// def write(self, block_num: int, buf: bytes) -> bool:
|
|
|
|
/// '''
|
2017-09-05 06:04:21 +00:00
|
|
|
/// Writes blocks starting with block_num from buf to the SD card.
|
|
|
|
/// Number of bytes written is length of buf rounded down to multiply of SDCARD_BLOCK_SIZE.
|
2017-06-14 16:07:54 +00:00
|
|
|
/// Returns True if in case of success, False otherwise.
|
|
|
|
/// '''
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC mp_obj_t mod_trezorio_SDCard_write(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
|
2018-05-16 12:42:41 +00:00
|
|
|
uint32_t block = trezor_obj_get_uint(block_num);
|
2017-05-15 13:48:47 +00:00
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
|
2018-05-16 12:42:41 +00:00
|
|
|
return mp_obj_new_bool(sdcard_write_blocks(bufinfo.buf, block, bufinfo.len / SDCARD_BLOCK_SIZE));
|
2017-05-15 13:48:47 +00:00
|
|
|
}
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_SDCard_write_obj, mod_trezorio_SDCard_write);
|
2017-05-15 13:48:47 +00:00
|
|
|
|
2017-06-14 16:47:38 +00:00
|
|
|
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) },
|
2017-05-15 13:48:47 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(SDCARD_BLOCK_SIZE) },
|
2017-06-14 16:47:38 +00:00
|
|
|
{ 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) },
|
2017-05-15 13:48:47 +00:00
|
|
|
};
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mod_trezorio_SDCard_locals_dict, mod_trezorio_SDCard_locals_dict_table);
|
2017-05-15 13:48:47 +00:00
|
|
|
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC const mp_obj_type_t mod_trezorio_SDCard_type = {
|
2017-05-15 13:48:47 +00:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_SDCard,
|
2017-06-14 16:47:38 +00:00
|
|
|
.make_new = mod_trezorio_SDCard_make_new,
|
|
|
|
.locals_dict = (void*)&mod_trezorio_SDCard_locals_dict,
|
2017-05-15 13:48:47 +00:00
|
|
|
};
|