1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-22 05:10:56 +00:00

upy exposure

This commit is contained in:
tychovrahe 2024-10-01 10:27:36 +02:00
parent 66624acb6d
commit 75910b77e9
9 changed files with 300 additions and 26 deletions

View File

@ -31,8 +31,8 @@ PRODUCTION ?= 0
PYOPT ?= 1
BITCOIN_ONLY ?= 0
BOOTLOADER_QA ?= 0
BOOTLOADER_DEVEL ?= 0
TREZOR_MODEL ?= T
BOOTLOADER_DEVEL ?= 1
TREZOR_MODEL ?= T3W1
TREZOR_MEMPERF ?= 0
ADDRESS_SANITIZER ?= 0
CMAKELISTS ?= 0

View File

@ -0,0 +1,173 @@
/*
* 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 "ble/dfu.h"
// #include "ble/messages.h"
/// package: trezorio.ble
// /// def update_init(data: bytes, binsize: int) -> int:
// /// """
// /// Initializes the BLE firmware update
// /// """
// STATIC mp_obj_t mod_trezorio_BLE_update_init(mp_obj_t data, mp_obj_t binsize)
// {
// mp_buffer_info_t buffer = {0};
// mp_int_t binsize_int = mp_obj_get_int(binsize);
//
// mp_get_buffer_raise(data, &buffer, MP_BUFFER_READ);
//
// ble_set_dfu_mode(true);
//
// dfu_result_t result = dfu_update_init(buffer.buf, buffer.len, binsize_int);
// if (result == DFU_NEXT_CHUNK) {
// return mp_obj_new_int(0);
// } else if (result == DFU_SUCCESS) {
// ble_set_dfu_mode(false);
// return mp_obj_new_int(1);
// } else {
// ble_set_dfu_mode(false);
// mp_raise_msg(&mp_type_RuntimeError, "Upload failed.");
// }
// }
// STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorio_BLE_update_init_obj,
// mod_trezorio_BLE_update_init);
//
// /// def update_chunk(chunk: bytes) -> int:
// /// """
// /// Writes next chunk of BLE firmware update
// /// """
// STATIC mp_obj_t mod_trezorio_BLE_update_chunk(mp_obj_t data) {
// mp_buffer_info_t buffer = {0};
//
// mp_get_buffer_raise(data, &buffer, MP_BUFFER_READ);
//
// dfu_result_t result = dfu_update_chunk(buffer.buf, buffer.len);
//
// if (result == DFU_NEXT_CHUNK) {
// return mp_obj_new_int(0);
// } else if (result == DFU_SUCCESS) {
// ble_set_dfu_mode(false);
// return mp_obj_new_int(1);
// } else {
// ble_set_dfu_mode(false);
// mp_raise_msg(&mp_type_RuntimeError, "Upload failed.");
// }
// }
// STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_BLE_update_chunk_obj,
// mod_trezorio_BLE_update_chunk);
/// def write(self, msg: bytes) -> int:
/// """
/// Sends message over BLE
/// """
STATIC mp_obj_t mod_trezorio_BLE_write(mp_obj_t self, mp_obj_t msg) {
mp_buffer_info_t buf = {0};
mp_get_buffer_raise(msg, &buf, MP_BUFFER_READ);
bool success = ble_write(buf.buf, buf.len);
if (success) {
return MP_OBJ_NEW_SMALL_INT(buf.len);
} else {
return MP_OBJ_NEW_SMALL_INT(-1);
}
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorio_BLE_write_obj,
mod_trezorio_BLE_write);
/// def erase_bonds() -> None:
/// """
/// Erases all BLE bonds
/// """
STATIC mp_obj_t mod_trezorio_BLE_erase_bonds(void) {
ble_issue_command(BLE_ERASE_BONDS);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorio_BLE_erase_bonds_obj,
mod_trezorio_BLE_erase_bonds);
/// def start_comm() -> None:
/// """
/// Start communication with BLE chip
/// """
STATIC mp_obj_t mod_trezorio_BLE_start_comm(void) {
ble_start();
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorio_BLE_start_comm_obj,
mod_trezorio_BLE_start_comm);
/// def start_advertising(whitelist: bool) -> None:
/// """
/// Start advertising
/// """
STATIC mp_obj_t mod_trezorio_BLE_start_advertising(mp_obj_t whitelist) {
bool whitelist_bool = mp_obj_is_true(whitelist);
ble_issue_command(whitelist_bool ? BLE_SWITCH_ON : BLE_PAIRING_MODE);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_BLE_start_advertising_obj,
mod_trezorio_BLE_start_advertising);
/// def stop_advertising(whitelist: bool) -> None:
/// """
/// Stop advertising
/// """
STATIC mp_obj_t mod_trezorio_BLE_stop_advertising(void) {
ble_issue_command(BLE_SWITCH_OFF);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorio_BLE_stop_advertising_obj,
mod_trezorio_BLE_stop_advertising);
/// def disconnect() -> None:
/// """
/// Disconnect BLE
/// """
STATIC mp_obj_t mod_trezorio_BLE_disconnect(void) {
ble_issue_command(BLE_DISCONNECT);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorio_BLE_disconnect_obj,
mod_trezorio_BLE_disconnect);
STATIC const mp_rom_map_elem_t mod_trezorio_BLE_globals_table[] = {
{MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ble)},
// {MP_ROM_QSTR(MP_QSTR_update_init),
// MP_ROM_PTR(&mod_trezorio_BLE_update_init_obj)},
// {MP_ROM_QSTR(MP_QSTR_update_chunk),
// MP_ROM_PTR(&mod_trezorio_BLE_update_chunk_obj)},
{MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mod_trezorio_BLE_write_obj)},
{MP_ROM_QSTR(MP_QSTR_erase_bonds),
MP_ROM_PTR(&mod_trezorio_BLE_erase_bonds_obj)},
{MP_ROM_QSTR(MP_QSTR_start_comm),
MP_ROM_PTR(&mod_trezorio_BLE_start_comm_obj)},
{MP_ROM_QSTR(MP_QSTR_start_advertising),
MP_ROM_PTR(&mod_trezorio_BLE_start_advertising_obj)},
{MP_ROM_QSTR(MP_QSTR_stop_advertising),
MP_ROM_PTR(&mod_trezorio_BLE_stop_advertising_obj)},
{MP_ROM_QSTR(MP_QSTR_disconnect),
MP_ROM_PTR(&mod_trezorio_BLE_disconnect_obj)},
};
STATIC MP_DEFINE_CONST_DICT(mod_trezorio_BLE_globals,
mod_trezorio_BLE_globals_table);
STATIC const mp_obj_module_t mod_trezorio_BLE_module = {
.base = {&mp_type_module},
.globals = (mp_obj_dict_t *)&mod_trezorio_BLE_globals};

View File

@ -19,6 +19,7 @@
#include <trezor_types.h>
#include "ble.h"
#include "button.h"
#include "display.h"
#include "systick.h"
@ -29,10 +30,13 @@
#include "SDL.h"
#endif
#define BLE_EVENTS_IFACE (252)
#define USB_DATA_IFACE (253)
#define INPUT_IFACE (255)
#define TOUCH_INPUT_FLAG (0x400000)
#define BUTTON_INPUT_FLAG (0x800000)
#define USB_RW_IFACE_MAX (15) // 0-15 reserved for USB
#define BLE_IFACE (16)
#define POLL_READ (0x0000)
#define POLL_WRITE (0x0100)
@ -160,7 +164,8 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref,
ret->items[1] = usb_connected ? mp_const_true : mp_const_false;
return mp_const_true;
}
} else if (mode == POLL_READ) {
} else if (iface <= USB_RW_IFACE_MAX) {
if (mode == POLL_READ) {
if (sectrue == usb_hid_can_read(iface)) {
uint8_t buf[64] = {0};
int len = usb_hid_read(iface, buf, sizeof(buf));
@ -190,6 +195,37 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref,
}
}
}
#ifdef USE_BLE
else if (iface == BLE_IFACE) {
if (mode == POLL_READ) {
uint8_t buf[BLE_RX_PACKET_SIZE] = {0};
int len = ble_read(buf, sizeof(buf));
if (len > 0) {
ret->items[0] = MP_OBJ_NEW_SMALL_INT(i);
ret->items[1] = mp_obj_new_bytes(buf, len);
return mp_const_true;
}
} else if (mode == POLL_WRITE) {
if (ble_can_write()) {
ret->items[0] = MP_OBJ_NEW_SMALL_INT(i);
ret->items[1] = mp_const_none;
return mp_const_true;
}
}
} else if (iface == BLE_EVENTS_IFACE) {
ble_event_t event = {0};
bool read = ble_read_event(&event);
if (read) {
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
tuple->items[0] = MP_OBJ_NEW_SMALL_INT(event.type);
tuple->items[1] = mp_obj_new_bytes(event.data, event.data_len);
ret->items[0] = MP_OBJ_NEW_SMALL_INT(i);
ret->items[1] = MP_OBJ_FROM_PTR(tuple);
return mp_const_true;
}
}
#endif
}
if (mp_hal_ticks_ms() >= deadline) {
break;

View File

@ -33,6 +33,8 @@
// Whether USB data pins were connected on last check (USB configured)
bool usb_connected_previously = true;
uint8_t ble_connected_previously = false;
bool ble_last_internal = false;
uint32_t last_touch_sample_time = 0;
@ -48,6 +50,9 @@ uint32_t last_touch_sample_time = 0;
#include "modtrezorio-webusb.h"
#include "modtrezorio-usb.h"
// clang-format on
#ifdef USE_BLE
#include "modtrezorio-ble.h"
#endif
#ifdef USE_SD_CARD
#include "modtrezorio-fatfs.h"
#include "modtrezorio-sdcard.h"
@ -57,7 +62,7 @@ uint32_t last_touch_sample_time = 0;
#endif
/// package: trezorio.__init__
/// from . import fatfs, haptic, sdcard
/// from . import fatfs, haptic, sdcard, ble
/// POLL_READ: int # wait until interface is readable and return read data
/// POLL_WRITE: int # wait until interface is writable
@ -74,8 +79,9 @@ uint32_t last_touch_sample_time = 0;
/// BUTTON_RIGHT: int # button number of right button
/// USB_CHECK: int # interface id for check of USB data connection
/// BLE_CHECK: int # interface id for check of BLE data connection
/// WireInterface = Union[HID, WebUSB]
/// WireInterface = Union[HID, WebUSB, BleInterface]
STATIC const mp_rom_map_elem_t mp_module_trezorio_globals_table[] = {
{MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_trezorio)},
@ -89,6 +95,10 @@ STATIC const mp_rom_map_elem_t mp_module_trezorio_globals_table[] = {
{MP_ROM_QSTR(MP_QSTR_haptic), MP_ROM_PTR(&mod_trezorio_haptic_module)},
#endif
#ifdef USE_BLE
{MP_ROM_QSTR(MP_QSTR_ble), MP_ROM_PTR(&mod_trezorio_BLE_module)},
#endif
{MP_ROM_QSTR(MP_QSTR_INPUT), MP_ROM_INT(INPUT_IFACE)},
#ifdef USE_TOUCH
{MP_ROM_QSTR(MP_QSTR_TOUCH_START),
MP_ROM_INT(((TOUCH_START >> 24) & 0xFFU) | TOUCH_INPUT_FLAG)},
@ -116,6 +126,7 @@ STATIC const mp_rom_map_elem_t mp_module_trezorio_globals_table[] = {
{MP_ROM_QSTR(MP_QSTR_POLL_WRITE), MP_ROM_INT(POLL_WRITE)},
{MP_ROM_QSTR(MP_QSTR_USB_CHECK), MP_ROM_INT(USB_DATA_IFACE)},
{MP_ROM_QSTR(MP_QSTR_BLE_CHECK), MP_ROM_INT(BLE_EVENTS_IFACE)},
};
STATIC MP_DEFINE_CONST_DICT(mp_module_trezorio_globals,

View File

@ -380,6 +380,8 @@ STATIC mp_obj_tuple_t mod_trezorutils_version_obj = {
/// """Git commit hash of the firmware."""
/// VERSION: VersionTuple
/// """Firmware version as a tuple (major, minor, patch, build)."""
/// USE_BLE: bool
/// """Whether the hardware supports BLE."""
/// USE_BUTTON: bool
/// """Whether the hardware supports button."""
/// USE_SD_CARD: bool
@ -443,6 +445,11 @@ STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = {
#else
{MP_ROM_QSTR(MP_QSTR_USE_SD_CARD), mp_const_false},
#endif
#ifdef USE_BLE
{MP_ROM_QSTR(MP_QSTR_USE_BLE), mp_const_true},
#else
{MP_ROM_QSTR(MP_QSTR_USE_BLE), mp_const_false},
#endif
#ifdef USE_TOUCH
{MP_ROM_QSTR(MP_QSTR_USE_TOUCH), mp_const_true},
#else

View File

@ -148,7 +148,7 @@ class WebUSB:
"""
Sends message using USB WebUSB (device) or UDP (emulator).
"""
from . import fatfs, haptic, sdcard
from . import fatfs, haptic, sdcard, ble
POLL_READ: int # wait until interface is readable and return read data
POLL_WRITE: int # wait until interface is writable
@ -162,4 +162,5 @@ BUTTON_RELEASED: int # button up event
BUTTON_LEFT: int # button number of left button
BUTTON_RIGHT: int # button number of right button
USB_CHECK: int # interface id for check of USB data connection
WireInterface = Union[HID, WebUSB]
BLE_CHECK: int # interface id for check of BLE data connection
WireInterface = Union[HID, WebUSB, BleInterface]

View File

@ -0,0 +1,43 @@
from typing import *
# extmod/modtrezorio/modtrezorio-ble.h
def write(self, msg: bytes) -> int:
"""
Sends message over BLE
"""
# extmod/modtrezorio/modtrezorio-ble.h
def erase_bonds() -> None:
"""
Erases all BLE bonds
"""
# extmod/modtrezorio/modtrezorio-ble.h
def start_comm() -> None:
"""
Start communication with BLE chip
"""
# extmod/modtrezorio/modtrezorio-ble.h
def start_advertising(whitelist: bool) -> None:
"""
Start advertising
"""
# extmod/modtrezorio/modtrezorio-ble.h
def stop_advertising(whitelist: bool) -> None:
"""
Stop advertising
"""
# extmod/modtrezorio/modtrezorio-ble.h
def disconnect() -> None:
"""
Disconnect BLE
"""

View File

@ -122,6 +122,8 @@ SCM_REVISION: bytes
"""Git commit hash of the firmware."""
VERSION: VersionTuple
"""Firmware version as a tuple (major, minor, patch, build)."""
USE_BLE: bool
"""Whether the hardware supports BLE."""
USE_BUTTON: bool
"""Whether the hardware supports button."""
USE_SD_CARD: bool

View File

@ -11,6 +11,7 @@ from trezorutils import ( # noqa: F401
SCM_REVISION,
UI_LAYOUT,
USE_BACKLIGHT,
USE_BLE,
USE_BUTTON,
USE_HAPTIC,
USE_OPTIGA,