mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-06-01 21:58:45 +00:00
core/sdcard: read/write now don't return bool, but rather throw an Exception instead
This commit is contained in:
parent
4938fb5461
commit
e432c37df0
@ -17,9 +17,10 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "sdcard.h"
|
|
||||||
|
|
||||||
#include "embed/extmod/trezorobj.h"
|
#include "embed/extmod/trezorobj.h"
|
||||||
|
#include "py/mperrno.h"
|
||||||
|
|
||||||
|
#include "sdcard.h"
|
||||||
|
|
||||||
/// class SDCard:
|
/// class SDCard:
|
||||||
/// """
|
/// """
|
||||||
@ -79,7 +80,7 @@ STATIC mp_obj_t mod_trezorio_SDCard_capacity(mp_obj_t self) {
|
|||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_SDCard_capacity_obj,
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_SDCard_capacity_obj,
|
||||||
mod_trezorio_SDCard_capacity);
|
mod_trezorio_SDCard_capacity);
|
||||||
|
|
||||||
/// def read(self, block_num: int, buf: bytearray) -> bool:
|
/// def read(self, block_num: int, buf: bytearray) -> None:
|
||||||
/// """
|
/// """
|
||||||
/// Reads blocks starting with block_num from the SD card into buf.
|
/// 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
|
/// Number of bytes read is length of buf rounded down to multiply of
|
||||||
@ -90,13 +91,15 @@ STATIC mp_obj_t mod_trezorio_SDCard_read(mp_obj_t self, mp_obj_t block_num,
|
|||||||
uint32_t block = trezor_obj_get_uint(block_num);
|
uint32_t block = trezor_obj_get_uint(block_num);
|
||||||
mp_buffer_info_t bufinfo;
|
mp_buffer_info_t bufinfo;
|
||||||
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE);
|
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE);
|
||||||
return mp_obj_new_bool(
|
if (sectrue != sdcard_read_blocks(bufinfo.buf, block, bufinfo.len / SDCARD_BLOCK_SIZE)) {
|
||||||
sdcard_read_blocks(bufinfo.buf, block, bufinfo.len / SDCARD_BLOCK_SIZE));
|
mp_raise_OSError(MP_EIO);
|
||||||
|
}
|
||||||
|
return mp_const_none;
|
||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_SDCard_read_obj,
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_SDCard_read_obj,
|
||||||
mod_trezorio_SDCard_read);
|
mod_trezorio_SDCard_read);
|
||||||
|
|
||||||
/// def write(self, block_num: int, buf: bytes) -> bool:
|
/// def write(self, block_num: int, buf: bytes) -> None:
|
||||||
/// """
|
/// """
|
||||||
/// Writes blocks starting with block_num from buf to the SD card.
|
/// 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
|
/// Number of bytes written is length of buf rounded down to multiply of
|
||||||
@ -107,8 +110,10 @@ STATIC mp_obj_t mod_trezorio_SDCard_write(mp_obj_t self, mp_obj_t block_num,
|
|||||||
uint32_t block = trezor_obj_get_uint(block_num);
|
uint32_t block = trezor_obj_get_uint(block_num);
|
||||||
mp_buffer_info_t bufinfo;
|
mp_buffer_info_t bufinfo;
|
||||||
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
|
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
|
||||||
return mp_obj_new_bool(
|
if (sectrue != sdcard_write_blocks(bufinfo.buf, block, bufinfo.len / SDCARD_BLOCK_SIZE)) {
|
||||||
sdcard_write_blocks(bufinfo.buf, block, bufinfo.len / SDCARD_BLOCK_SIZE));
|
mp_raise_OSError(MP_EIO);
|
||||||
|
}
|
||||||
|
return mp_const_none;
|
||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_SDCard_write_obj,
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_SDCard_write_obj,
|
||||||
mod_trezorio_SDCard_write);
|
mod_trezorio_SDCard_write);
|
||||||
|
@ -93,10 +93,25 @@ def test_sd():
|
|||||||
if sd.present():
|
if sd.present():
|
||||||
sd.power(True)
|
sd.power(True)
|
||||||
buf1 = bytearray(8 * 1024)
|
buf1 = bytearray(8 * 1024)
|
||||||
sd.read(0, buf1)
|
try:
|
||||||
sd.write(0, buf1)
|
sd.read(0, buf1)
|
||||||
|
except OSError:
|
||||||
|
print('ERROR READING DATA')
|
||||||
|
sd.power(False)
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
sd.write(0, buf1)
|
||||||
|
except OSError:
|
||||||
|
print('ERROR WRITING DATA')
|
||||||
|
sd.power(False)
|
||||||
|
return
|
||||||
buf2 = bytearray(8 * 1024)
|
buf2 = bytearray(8 * 1024)
|
||||||
sd.read(0, buf2)
|
try:
|
||||||
|
sd.read(0, buf2)
|
||||||
|
except OSError:
|
||||||
|
print('ERROR READING DATA')
|
||||||
|
sd.power(False)
|
||||||
|
return
|
||||||
if buf1 == buf2:
|
if buf1 == buf2:
|
||||||
print('OK')
|
print('OK')
|
||||||
else:
|
else:
|
||||||
|
@ -13,21 +13,24 @@ class TestTrezorIoSdcard(unittest.TestCase):
|
|||||||
sd = io.SDCard()
|
sd = io.SDCard()
|
||||||
x = bytearray(8 * 512)
|
x = bytearray(8 * 512)
|
||||||
self.assertEqual(sd.capacity(), 0)
|
self.assertEqual(sd.capacity(), 0)
|
||||||
self.assertFalse(sd.read(0, x))
|
with self.assertRaises(OSError):
|
||||||
|
sd.read(0, x)
|
||||||
sd.power(True)
|
sd.power(True)
|
||||||
self.assertTrue(sd.capacity() > 0)
|
self.assertTrue(sd.capacity() > 0)
|
||||||
self.assertTrue(sd.read(0, x))
|
sd.read(0, x)
|
||||||
sd.power(False)
|
sd.power(False)
|
||||||
self.assertEqual(sd.capacity(), 0)
|
self.assertEqual(sd.capacity(), 0)
|
||||||
self.assertFalse(sd.read(0, x))
|
with self.assertRaises(OSError):
|
||||||
|
sd.read(0, x)
|
||||||
|
|
||||||
def test_read(self):
|
def test_read(self):
|
||||||
sd = io.SDCard()
|
sd = io.SDCard()
|
||||||
x = bytearray(8 * 512)
|
x = bytearray(8 * 512)
|
||||||
sd.power(True)
|
sd.power(True)
|
||||||
self.assertTrue(sd.read(0, x))
|
sd.read(0, x)
|
||||||
sd.power(False)
|
sd.power(False)
|
||||||
self.assertFalse(sd.read(0, x))
|
with self.assertRaises(OSError):
|
||||||
|
sd.read(0, x)
|
||||||
|
|
||||||
def test_read_write(self):
|
def test_read_write(self):
|
||||||
sd = io.SDCard()
|
sd = io.SDCard()
|
||||||
@ -35,11 +38,11 @@ class TestTrezorIoSdcard(unittest.TestCase):
|
|||||||
w0 = bytearray(b'0' * (8 * 512))
|
w0 = bytearray(b'0' * (8 * 512))
|
||||||
w1 = bytearray(b'1' * (8 * 512))
|
w1 = bytearray(b'1' * (8 * 512))
|
||||||
sd.power(True)
|
sd.power(True)
|
||||||
self.assertTrue(sd.write(0, w0))
|
sd.write(0, w0)
|
||||||
self.assertTrue(sd.read(0, r))
|
sd.read(0, r)
|
||||||
self.assertEqual(r, w0)
|
self.assertEqual(r, w0)
|
||||||
self.assertTrue(sd.write(0, w1))
|
sd.write(0, w1)
|
||||||
self.assertTrue(sd.read(0, r))
|
sd.read(0, r)
|
||||||
self.assertEqual(r, w1)
|
self.assertEqual(r, w1)
|
||||||
sd.power(False)
|
sd.power(False)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user