mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-13 19:18:56 +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/>.
|
||||
*/
|
||||
|
||||
#include "sdcard.h"
|
||||
|
||||
#include "embed/extmod/trezorobj.h"
|
||||
#include "py/mperrno.h"
|
||||
|
||||
#include "sdcard.h"
|
||||
|
||||
/// 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,
|
||||
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.
|
||||
/// 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);
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE);
|
||||
return mp_obj_new_bool(
|
||||
sdcard_read_blocks(bufinfo.buf, block, bufinfo.len / SDCARD_BLOCK_SIZE));
|
||||
if (sectrue != 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,
|
||||
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.
|
||||
/// 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);
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
|
||||
return mp_obj_new_bool(
|
||||
sdcard_write_blocks(bufinfo.buf, block, bufinfo.len / SDCARD_BLOCK_SIZE));
|
||||
if (sectrue != 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,
|
||||
mod_trezorio_SDCard_write);
|
||||
|
@ -93,10 +93,25 @@ def test_sd():
|
||||
if sd.present():
|
||||
sd.power(True)
|
||||
buf1 = bytearray(8 * 1024)
|
||||
sd.read(0, buf1)
|
||||
sd.write(0, buf1)
|
||||
try:
|
||||
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)
|
||||
sd.read(0, buf2)
|
||||
try:
|
||||
sd.read(0, buf2)
|
||||
except OSError:
|
||||
print('ERROR READING DATA')
|
||||
sd.power(False)
|
||||
return
|
||||
if buf1 == buf2:
|
||||
print('OK')
|
||||
else:
|
||||
|
@ -13,21 +13,24 @@ class TestTrezorIoSdcard(unittest.TestCase):
|
||||
sd = io.SDCard()
|
||||
x = bytearray(8 * 512)
|
||||
self.assertEqual(sd.capacity(), 0)
|
||||
self.assertFalse(sd.read(0, x))
|
||||
with self.assertRaises(OSError):
|
||||
sd.read(0, x)
|
||||
sd.power(True)
|
||||
self.assertTrue(sd.capacity() > 0)
|
||||
self.assertTrue(sd.read(0, x))
|
||||
sd.read(0, x)
|
||||
sd.power(False)
|
||||
self.assertEqual(sd.capacity(), 0)
|
||||
self.assertFalse(sd.read(0, x))
|
||||
with self.assertRaises(OSError):
|
||||
sd.read(0, x)
|
||||
|
||||
def test_read(self):
|
||||
sd = io.SDCard()
|
||||
x = bytearray(8 * 512)
|
||||
sd.power(True)
|
||||
self.assertTrue(sd.read(0, x))
|
||||
sd.read(0, x)
|
||||
sd.power(False)
|
||||
self.assertFalse(sd.read(0, x))
|
||||
with self.assertRaises(OSError):
|
||||
sd.read(0, x)
|
||||
|
||||
def test_read_write(self):
|
||||
sd = io.SDCard()
|
||||
@ -35,11 +38,11 @@ class TestTrezorIoSdcard(unittest.TestCase):
|
||||
w0 = bytearray(b'0' * (8 * 512))
|
||||
w1 = bytearray(b'1' * (8 * 512))
|
||||
sd.power(True)
|
||||
self.assertTrue(sd.write(0, w0))
|
||||
self.assertTrue(sd.read(0, r))
|
||||
sd.write(0, w0)
|
||||
sd.read(0, r)
|
||||
self.assertEqual(r, w0)
|
||||
self.assertTrue(sd.write(0, w1))
|
||||
self.assertTrue(sd.read(0, r))
|
||||
sd.write(0, w1)
|
||||
sd.read(0, r)
|
||||
self.assertEqual(r, w1)
|
||||
sd.power(False)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user