refactor(core/embed): remove FlashOTP module

[no changelog]
cepetr/low-power
cepetr 1 month ago
parent f495a31664
commit bd29f6ed61

@ -1,120 +0,0 @@
/*
* This file is part of the Trezor project, https://trezor.io/
*
* 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/>.
*/
#include "flash_otp.h"
#include "embed/extmod/trezorobj.h"
/// package: trezorio.__init__
/// class FlashOTP:
/// """
/// """
typedef struct _mp_obj_FlashOTP_t {
mp_obj_base_t base;
} mp_obj_FlashOTP_t;
/// def __init__(self) -> None:
/// """
/// """
STATIC mp_obj_t mod_trezorio_FlashOTP_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_FlashOTP_t *o = mp_obj_malloc(mp_obj_FlashOTP_t, type);
return MP_OBJ_FROM_PTR(o);
}
/// def write(self, block: int, offset: int, data: bytes) -> None:
/// """
/// Writes data to OTP flash
/// """
STATIC mp_obj_t mod_trezorio_FlashOTP_write(size_t n_args,
const mp_obj_t *args) {
uint8_t block = trezor_obj_get_uint8(args[1]);
uint8_t offset = trezor_obj_get_uint8(args[2]);
mp_buffer_info_t data = {0};
mp_get_buffer_raise(args[3], &data, MP_BUFFER_READ);
if (sectrue != flash_otp_write(block, offset, data.buf, data.len)) {
mp_raise_ValueError("write failed");
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorio_FlashOTP_write_obj, 4,
4, mod_trezorio_FlashOTP_write);
/// def read(self, block: int, offset: int, data: bytearray) -> None:
/// """
/// Reads data from OTP flash
/// """
STATIC mp_obj_t mod_trezorio_FlashOTP_read(size_t n_args,
const mp_obj_t *args) {
uint8_t block = trezor_obj_get_uint8(args[1]);
uint8_t offset = trezor_obj_get_uint8(args[2]);
mp_buffer_info_t data = {0};
mp_get_buffer_raise(args[3], &data, MP_BUFFER_WRITE);
if (sectrue != flash_otp_read(block, offset, data.buf, data.len)) {
mp_raise_ValueError("read failed");
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorio_FlashOTP_read_obj, 4, 4,
mod_trezorio_FlashOTP_read);
/// def lock(self, block: int) -> None:
/// """
/// Lock OTP flash block
/// """
STATIC mp_obj_t mod_trezorio_FlashOTP_lock(mp_obj_t self, mp_obj_t block) {
uint8_t b = trezor_obj_get_uint8(block);
if (sectrue != flash_otp_lock(b)) {
mp_raise_ValueError("lock failed");
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorio_FlashOTP_lock_obj,
mod_trezorio_FlashOTP_lock);
/// def is_locked(self, block: int) -> bool:
/// """
/// Is OTP flash block locked?
/// """
STATIC mp_obj_t mod_trezorio_FlashOTP_is_locked(mp_obj_t self, mp_obj_t block) {
uint8_t b = trezor_obj_get_uint8(block);
return (sectrue == flash_otp_is_locked(b)) ? mp_const_true : mp_const_false;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorio_FlashOTP_is_locked_obj,
mod_trezorio_FlashOTP_is_locked);
STATIC const mp_rom_map_elem_t mod_trezorio_FlashOTP_locals_dict_table[] = {
{MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mod_trezorio_FlashOTP_read_obj)},
{MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mod_trezorio_FlashOTP_write_obj)},
{MP_ROM_QSTR(MP_QSTR_lock), MP_ROM_PTR(&mod_trezorio_FlashOTP_lock_obj)},
{MP_ROM_QSTR(MP_QSTR_is_locked),
MP_ROM_PTR(&mod_trezorio_FlashOTP_is_locked_obj)},
};
STATIC MP_DEFINE_CONST_DICT(mod_trezorio_FlashOTP_locals_dict,
mod_trezorio_FlashOTP_locals_dict_table);
STATIC const mp_obj_type_t mod_trezorio_FlashOTP_type = {
{&mp_type_type},
.name = MP_QSTR_FlashOTP,
.make_new = mod_trezorio_FlashOTP_make_new,
.locals_dict = (void *)&mod_trezorio_FlashOTP_locals_dict,
};

@ -17,6 +17,8 @@
* 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 "embed/extmod/trezorobj.h"
/// package: trezorio.__init__ /// package: trezorio.__init__
/// class HID: /// class HID:

@ -43,7 +43,6 @@ uint32_t last_touch_sample_time = 0;
} }
// clang-format off // clang-format off
#include "modtrezorio-flash.h"
#include "modtrezorio-hid.h" #include "modtrezorio-hid.h"
#include "modtrezorio-poll.h" #include "modtrezorio-poll.h"
#include "modtrezorio-vcp.h" #include "modtrezorio-vcp.h"
@ -114,8 +113,6 @@ STATIC const mp_rom_map_elem_t mp_module_trezorio_globals_table[] = {
{MP_ROM_QSTR(MP_QSTR_BUTTON_RIGHT), MP_ROM_INT(BTN_RIGHT)}, {MP_ROM_QSTR(MP_QSTR_BUTTON_RIGHT), MP_ROM_INT(BTN_RIGHT)},
#endif #endif
{MP_ROM_QSTR(MP_QSTR_FlashOTP), MP_ROM_PTR(&mod_trezorio_FlashOTP_type)},
{MP_ROM_QSTR(MP_QSTR_USB), MP_ROM_PTR(&mod_trezorio_USB_type)}, {MP_ROM_QSTR(MP_QSTR_USB), MP_ROM_PTR(&mod_trezorio_USB_type)},
{MP_ROM_QSTR(MP_QSTR_HID), MP_ROM_PTR(&mod_trezorio_HID_type)}, {MP_ROM_QSTR(MP_QSTR_HID), MP_ROM_PTR(&mod_trezorio_HID_type)},
{MP_ROM_QSTR(MP_QSTR_VCP), MP_ROM_PTR(&mod_trezorio_VCP_type)}, {MP_ROM_QSTR(MP_QSTR_VCP), MP_ROM_PTR(&mod_trezorio_VCP_type)},

@ -1,190 +0,0 @@
import trezorio as io
import trezorui as ui
import utime
usb_vcp = io.VCP(
iface_num=0x00,
data_iface_num=0x01,
ep_in=0x01,
ep_out=0x01,
ep_cmd=0x02,
)
usb = io.USB(
vendor_id=0x1209,
product_id=0x53C1,
release_num=0x0200,
manufacturer="SatoshiLabs",
product="TREZOR",
serial_number="000000000000000000000000",
usb21_landing=False,
)
usb.add(usb_vcp)
usb.open()
d = ui.Display()
otp = io.FlashOTP()
sd = io.SDCard()
sbu = io.SBU()
def test_display(colors):
d.clear()
m = {
"R": 0xF800,
"G": 0x07E0,
"B": 0x001F,
"W": 0xFFFF,
}
w = 240 // len(colors)
for i, c in enumerate(colors):
c = m.get(c, 0x0000)
d.bar(i * w, 0, i * w + w, 240, c)
d.refresh()
print("OK")
def test_touch(v):
d.clear()
c, t = int(v[0]), int(v[1])
deadline = utime.ticks_add(utime.ticks_us(), t * 1000000)
if c == 1:
d.bar(0, 0, 120, 120, 0xFFFF)
elif c == 2:
d.bar(120, 0, 120, 120, 0xFFFF)
elif c == 3:
d.bar(120, 120, 120, 120, 0xFFFF)
else:
d.bar(0, 120, 120, 120, 0xFFFF)
d.refresh()
r = [0, 0]
# flush all events
while io.poll([io.TOUCH], r, 10000):
pass
# wait for event
touch = False
while True:
if not touch:
if (
io.poll([io.TOUCH], r, 10000)
and r[0] == io.TOUCH
and r[1][0] == io.TOUCH_START
):
touch = True
else:
if (
io.poll([io.TOUCH], r, 10000)
and r[0] == io.TOUCH
and r[1][0] == io.TOUCH_END
):
print(f"OK {r[1][1]} {r[1][2]}")
break
if utime.ticks_us() > deadline:
print("ERROR TIMEOUT")
break
# flush all events
while io.poll([io.TOUCH], r, 10000):
pass
d.clear()
d.refresh()
def test_pwm(v):
d.backlight(int(v))
d.refresh()
print("OK")
def test_sd():
if sd.present():
sd.power(True)
buf1 = bytearray(8 * 1024)
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)
try:
sd.read(0, buf2)
except OSError:
print("ERROR READING DATA")
sd.power(False)
return
if buf1 == buf2:
print("OK")
else:
print("ERROR DATA MISMATCH")
sd.power(False)
else:
print("ERROR NOCARD")
def test_sbu(v):
sbu1 = v[0] == "1"
sbu2 = v[1] == "1"
sbu.set(sbu1, sbu2)
print("OK")
def test_otp_read():
data = bytearray(32)
otp.read(0, 0, data)
data = bytes(data).rstrip(b"\x00\xff").decode()
print("OK", data)
def test_otp_write(v):
if len(v) < 32:
v = v + "\x00" * (32 - len(v))
data = v[:32].encode()
otp.write(0, 0, data)
otp.lock(0)
print("OK")
d.clear()
while True:
try:
line = input()
if line == "PING":
print("OK")
elif line.startswith("DISP "):
test_display(line[5:])
elif line.startswith("TOUCH "):
test_touch(line[6:])
elif line.startswith("PWM "):
test_pwm(line[4:])
elif line == "SD":
test_sd()
elif line.startswith("SBU "):
test_sbu(line[4:])
elif line.startswith("OTP READ"):
test_otp_read()
elif line.startswith("OTP WRITE "):
test_otp_write(line[10:])
else:
print("UNKNOWN")
except Exception as ex:
print("ERROR", ex)
Loading…
Cancel
Save