1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-07-23 23:18:16 +00:00

WIP - flags

This commit is contained in:
grdddj 2022-03-16 15:43:57 +01:00
parent b61b4cac04
commit 9237c376dd
6 changed files with 66 additions and 20 deletions

View File

@ -47,6 +47,8 @@ static void _librust_qstrs(void) {
MP_QSTR_set_slip39_iteration_exponent;
MP_QSTR_get_autolock_delay_ms;
MP_QSTR_set_autolock_delay_ms;
MP_QSTR_get_flags;
MP_QSTR_set_flags;
MP_QSTR_set_timer_fn;
MP_QSTR_touch_event;

View File

@ -368,6 +368,32 @@ extern "C" fn storagedevice_set_autolock_delay_ms(delay_ms: Obj) -> Obj {
unsafe { util::try_or_raise(block) }
}
extern "C" fn storagedevice_get_flags() -> Obj {
let block = || {
let key = _get_appkey(_FLAGS, false);
match storagedevice_storage_get_u32(key) {
Some(flag) => flag.try_into(),
None => 0.try_into(),
}
};
unsafe { util::try_or_raise(block) }
}
extern "C" fn storagedevice_set_flags(flags: Obj) -> Obj {
let block = || {
let flags = u32::try_from(flags)?;
let key = _get_appkey(_FLAGS, false);
let old_flags = storagedevice_storage_get_u32(key).unwrap_or(0);
// Not deleting old flags
let new_flags = flags | old_flags;
Ok(storagedevice_storage_set_u32(key, new_flags).into())
};
unsafe { util::try_or_raise(block) }
}
pub fn storagedevice_storage_get(key: u16) -> Vec<u8, MAX_LEN> {
let mut buf: [u8; MAX_LEN] = [0; MAX_LEN];
let mut len: u16 = 0;
@ -619,6 +645,14 @@ pub static mp_module_trezorstoragedevice: Module = obj_module! {
/// def set_autolock_delay_ms(delay_ms: int) -> bool:
/// """Set autolock delay."""
Qstr::MP_QSTR_set_autolock_delay_ms => obj_fn_1!(storagedevice_set_autolock_delay_ms).as_obj(),
/// def get_flags() -> int:
/// """Get flags."""
Qstr::MP_QSTR_get_flags => obj_fn_0!(storagedevice_get_flags).as_obj(),
/// def set_flags(flags: int) -> bool:
/// """Set flags."""
Qstr::MP_QSTR_set_flags => obj_fn_1!(storagedevice_set_flags).as_obj(),
};
#[cfg(test)]

View File

@ -142,3 +142,13 @@ def get_autolock_delay_ms() -> int:
# rust/src/storagedevice/storage_device.rs
def set_autolock_delay_ms(delay_ms: int) -> bool:
"""Set autolock delay."""
# rust/src/storagedevice/storage_device.rs
def get_flags() -> int:
"""Get flags."""
# rust/src/storagedevice/storage_device.rs
def set_flags(flags: int) -> bool:
"""Set flags."""

View File

@ -85,7 +85,7 @@ def get_features() -> Features:
f.needs_backup = storagedevice.needs_backup()
f.unfinished_backup = storagedevice.unfinished_backup()
f.no_backup = storagedevice.no_backup()
f.flags = storage.device.get_flags()
f.flags = storagedevice.get_flags()
f.recovery_mode = storage.recovery.is_in_progress()
f.backup_type = mnemonic.get_type()
f.sd_protection = storage.sd_salt.is_enabled()

View File

@ -1,6 +1,5 @@
from typing import TYPE_CHECKING
from storage.device import set_flags
from trezor import storagedevice, wire
from trezor.messages import Success
@ -11,5 +10,5 @@ if TYPE_CHECKING:
async def apply_flags(ctx: wire.GenericContext, msg: ApplyFlags) -> Success:
if not storagedevice.is_initialized():
raise wire.NotInitialized("Device is not initialized")
set_flags(msg.flags)
storagedevice.set_flags(msg.flags)
return Success(message="Flags applied")

View File

@ -10,6 +10,8 @@ if TYPE_CHECKING:
from trezor.enums import BackupType
from typing_extensions import Literal
StorageSafetyCheckLevel = Literal[0, 1]
# Namespace:
_NAMESPACE = common.APP_DEVICE
@ -41,8 +43,6 @@ _EXPERIMENTAL_FEATURES = const(0x15) # bool (0x01 or empty)
SAFETY_CHECK_LEVEL_STRICT : Literal[0] = const(0)
SAFETY_CHECK_LEVEL_PROMPT : Literal[1] = const(1)
_DEFAULT_SAFETY_CHECK_LEVEL = SAFETY_CHECK_LEVEL_STRICT
if TYPE_CHECKING:
StorageSafetyCheckLevel = Literal[0, 1]
# fmt: on
HOMESCREEN_MAXSIZE = 16384
@ -207,23 +207,24 @@ def store_mnemonic_secret(
# common.set_bool(_NAMESPACE, _PASSPHRASE_ALWAYS_ON_DEVICE, enable)
def get_flags() -> int:
b = common.get(_NAMESPACE, _FLAGS)
if b is None:
return 0
else:
return int.from_bytes(b, "big")
# def get_flags() -> int:
# b = common.get(_NAMESPACE, _FLAGS)
# if b is None:
# return 0
# else:
# return int.from_bytes(b, "big")
def set_flags(flags: int) -> None:
b = common.get(_NAMESPACE, _FLAGS)
if b is None:
i = 0
else:
i = int.from_bytes(b, "big")
flags = (flags | i) & 0xFFFF_FFFF
if flags != i:
common.set(_NAMESPACE, _FLAGS, flags.to_bytes(4, "big"))
# def set_flags(flags: int) -> None:
# b = common.get(_NAMESPACE, _FLAGS)
# if b is None:
# i = 0
# else:
# i = int.from_bytes(b, "big")
# flags = (flags | i) & 0xFFFF_FFFF
# print("flags_mpppppp", flags)
# if flags != i:
# common.set(_NAMESPACE, _FLAGS, flags.to_bytes(4, "big"))
# def _normalize_autolock_delay(delay_ms: int) -> int: