diff --git a/core/embed/rust/librust_qstr.h b/core/embed/rust/librust_qstr.h index 3038eb0c2c..4dd00b46dc 100644 --- a/core/embed/rust/librust_qstr.h +++ b/core/embed/rust/librust_qstr.h @@ -25,6 +25,8 @@ static void _librust_qstrs(void) { MP_QSTR_is_initialized; MP_QSTR_get_version; MP_QSTR_set_version; + MP_QSTR_get_rotation; + MP_QSTR_set_rotation; MP_QSTR_set_timer_fn; MP_QSTR_touch_event; diff --git a/core/embed/rust/src/storagedevice/storagedevice.rs b/core/embed/rust/src/storagedevice/storagedevice.rs index 5c050b00b0..17845bba06 100644 --- a/core/embed/rust/src/storagedevice/storagedevice.rs +++ b/core/embed/rust/src/storagedevice/storagedevice.rs @@ -1,9 +1,12 @@ use crate::{ + error::Error, micropython::{buffer::Buffer, map::Map, module::Module, obj::Obj, qstr::Qstr}, trezorhal::secbool, util, }; +use cstr_core::cstr; + use core::convert::{TryFrom, TryInto}; const FLAG_PUBLIC: u8 = 0x80; @@ -73,9 +76,9 @@ extern "C" fn storagedevice_get_version() -> Obj { unsafe { util::try_or_raise(block) } } -extern "C" fn storagedevice_set_version(version: Obj) -> Obj { +extern "C" fn storagedevice_set_version(value: Obj) -> Obj { let block = || { - let value = Buffer::try_from(version)?; + let value = Buffer::try_from(value)?; let key = _get_appkey(_VERSION, false); let result = storagedevice_storage_set(key, value); @@ -94,6 +97,33 @@ extern "C" fn storagedevice_is_initialized() -> Obj { unsafe { util::try_or_raise(block) } } +extern "C" fn storagedevice_get_rotation() -> Obj { + let block = || { + let key = _get_appkey(_ROTATION, true); + let (buf, len) = storagedevice_storage_get(key); + // TODO: how to convert unknown size buff into int? + let result = (buf[0] as u16) << 8 + (buf[1] as u16); + Ok(result.into()) + }; + unsafe { util::try_or_raise(block) } +} + +extern "C" fn storagedevice_set_rotation(value: Obj) -> Obj { + let block = || { + // TODO: how to raise a micropython exception? + if ![0, 90, 180, 270].contains(&u16::try_from(value)?) { + // return Error::ValueError(cstr!("Not valid rotation")); + } + + let value = Buffer::try_from(value)?; + + let key = _get_appkey(_ROTATION, true); + let result = storagedevice_storage_set(key, value); + Ok(result.into()) + }; + unsafe { util::try_or_raise(block) } +} + // TODO: find out how to return the real result // pub fn storagedevice_storage_get(key: u16) -> &'static [u8] { // pub fn storagedevice_storage_get(key: u16) -> [u8] { @@ -157,12 +187,20 @@ pub static mp_module_trezorstoragedevice: Module = obj_module! { Qstr::MP_QSTR_is_initialized => obj_fn_0!(storagedevice_is_initialized).as_obj(), /// def get_version() -> bytes: - /// """Get from storage.""" + /// """Get version.""" Qstr::MP_QSTR_get_version => obj_fn_0!(storagedevice_get_version).as_obj(), - /// def set_version(version: bytes) -> bool: - /// """Save to storage.""" + /// def set_version(value: bytes) -> bool: + /// """Set version.""" Qstr::MP_QSTR_set_version => obj_fn_1!(storagedevice_set_version).as_obj(), + + /// def get_rotation() -> int: + /// """Get rotation.""" + Qstr::MP_QSTR_get_rotation => obj_fn_0!(storagedevice_get_rotation).as_obj(), + + /// def set_rotation(value: int) -> bool: + /// """Set rotation.""" + Qstr::MP_QSTR_set_rotation => obj_fn_1!(storagedevice_set_rotation).as_obj(), }; #[cfg(test)] diff --git a/core/src/apps/base.py b/core/src/apps/base.py index 59ac30e8f5..1de5bbb741 100644 --- a/core/src/apps/base.py +++ b/core/src/apps/base.py @@ -94,7 +94,7 @@ def get_features() -> Features: f.passphrase_always_on_device = storage.device.get_passphrase_always_on_device() f.safety_checks = safety_checks.read_setting() f.auto_lock_delay_ms = storage.device.get_autolock_delay_ms() - f.display_rotation = storage.device.get_rotation() + f.display_rotation = trezorstoragedevice.get_rotation() f.experimental_features = storage.device.get_experimental_features() return f @@ -279,7 +279,7 @@ def reload_settings_from_storage() -> None: storage.device.get_autolock_delay_ms(), lock_device_if_unlocked ) wire.experimental_enabled = storage.device.get_experimental_features() - ui.display.orientation(storage.device.get_rotation()) + ui.display.orientation(trezorstoragedevice.get_rotation()) def boot() -> None: diff --git a/core/src/boot.py b/core/src/boot.py index 8de0768fbb..33aa76b88f 100644 --- a/core/src/boot.py +++ b/core/src/boot.py @@ -1,5 +1,6 @@ import storage import storage.device +from storage import trezorstoragedevice from trezor import config, log, loop, ui, utils, wire from trezor.pin import show_pin_timeout @@ -9,7 +10,7 @@ from apps.homescreen.lockscreen import Lockscreen async def bootscreen() -> None: lockscreen = Lockscreen(bootscreen=True) - ui.display.orientation(storage.device.get_rotation()) + ui.display.orientation(trezorstoragedevice.get_rotation()) while True: try: if can_lock_device(): diff --git a/core/src/storage/device.py b/core/src/storage/device.py index 33ebad8acd..edae72db4a 100644 --- a/core/src/storage/device.py +++ b/core/src/storage/device.py @@ -92,17 +92,17 @@ def get_device_id() -> str: return dev_id.decode() -def get_rotation() -> int: - rotation = common.get(_NAMESPACE, _ROTATION, public=True) - if not rotation: - return 0 - return int.from_bytes(rotation, "big") +# def get_rotation() -> int: +# rotation = common.get(_NAMESPACE, _ROTATION, public=True) +# if not rotation: +# return 0 +# return int.from_bytes(rotation, "big") -def set_rotation(value: int) -> None: - if value not in (0, 90, 180, 270): - raise ValueError # unsupported display rotation - common.set(_NAMESPACE, _ROTATION, value.to_bytes(2, "big"), True) # public +# def set_rotation(value: int) -> None: +# if value not in (0, 90, 180, 270): +# raise ValueError # unsupported display rotation +# common.set(_NAMESPACE, _ROTATION, value.to_bytes(2, "big"), True) # public def get_label() -> str | None: