1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-07-21 05:58:09 +00:00

WIP - storage_get rust binding

This commit is contained in:
grdddj 2022-03-10 14:37:16 +01:00
parent 6702df58dd
commit bcf837694b
7 changed files with 86 additions and 57 deletions

View File

@ -23,30 +23,29 @@
#include "librust.h"
/// def is_version_stored() -> bool:
/// """Whether version is in storage."""
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_storagedevice_is_version_stored_obj,
storagedevice_is_version_stored);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(
mod_trezorutils_storagedevice_is_version_stored_obj,
storagedevice_is_version_stored);
// /// def get_version() -> bool:
// /// """Get from storage."""
// STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorutils_storagedevice_get_version_obj,
// storagedevice_get_version);
/// def get_version() -> bytes:
/// """Get from storage."""
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_storagedevice_get_version_obj,
storagedevice_get_version);
/// def set_version(version: bytes) -> bool:
/// """Save to storage."""
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorutils_storagedevice_set_version_obj,
storagedevice_set_version);
STATIC const mp_rom_map_elem_t mp_module_trezorstoragedevice_globals_table[] = {
{MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_trezorstoragedevice)},
{MP_ROM_QSTR(MP_QSTR_is_version_stored),
MP_ROM_PTR(&mod_trezorutils_storagedevice_is_version_stored_obj)},
// {MP_ROM_QSTR(MP_QSTR_get_version),
// MP_ROM_PTR(&mod_trezorutils_storagedevice_get_version_obj)},
{MP_ROM_QSTR(MP_QSTR_get_version),
MP_ROM_PTR(&mod_trezorutils_storagedevice_get_version_obj)},
{MP_ROM_QSTR(MP_QSTR_set_version),
MP_ROM_PTR(&mod_trezorutils_storagedevice_set_version_obj)},
};

View File

@ -10,7 +10,7 @@ mp_obj_t protobuf_len(mp_obj_t obj);
mp_obj_t protobuf_encode(mp_obj_t buf, mp_obj_t obj);
mp_obj_t storagedevice_is_version_stored();
// mp_obj_t storagedevice_get_version();
mp_obj_t storagedevice_get_version();
mp_obj_t storagedevice_set_version(mp_obj_t key);
#ifdef TREZOR_EMULATOR

View File

@ -8,30 +8,30 @@ use core::convert::{TryFrom, TryInto};
// TODO: transfer this into a struct with field specifying data type and
// max_length
const APP_DEVICE: u16 = 0x01;
const APP_DEVICE: u8 = 0x01;
const DEVICE_ID: u16 = 0x01;
const _VERSION: u16 = 0x02;
const _MNEMONIC_SECRET: u16 = 0x03;
const _LANGUAGE: u16 = 0x04;
const _LABEL: u16 = 0x05;
const _USE_PASSPHRASE: u16 = 0x06;
const _HOMESCREEN: u16 = 0x07;
const _NEEDS_BACKUP: u16 = 0x08;
const _FLAGS: u16 = 0x09;
const U2F_COUNTER: u16 = 0x0A;
const _PASSPHRASE_ALWAYS_ON_DEVICE: u16 = 0x0B;
const _UNFINISHED_BACKUP: u16 = 0x0C;
const _AUTOLOCK_DELAY_MS: u16 = 0x0D;
const _NO_BACKUP: u16 = 0x0E;
const _BACKUP_TYPE: u16 = 0x0F;
const _ROTATION: u16 = 0x10;
const _SLIP39_IDENTIFIER: u16 = 0x11;
const _SLIP39_ITERATION_EXPONENT: u16 = 0x12;
const _SD_SALT_AUTH_KEY: u16 = 0x13;
const INITIALIZED: u16 = 0x14;
const _SAFETY_CHECK_LEVEL: u16 = 0x15;
const _EXPERIMENTAL_FEATURES: u16 = 0x16;
const DEVICE_ID: u8 = 0x01;
const _VERSION: u8 = 0x02;
const _MNEMONIC_SECRET: u8 = 0x03;
const _LANGUAGE: u8 = 0x04;
const _LABEL: u8 = 0x05;
const _USE_PASSPHRASE: u8 = 0x06;
const _HOMESCREEN: u8 = 0x07;
const _NEEDS_BACKUP: u8 = 0x08;
const _FLAGS: u8 = 0x09;
const U2F_COUNTER: u8 = 0x0A;
const _PASSPHRASE_ALWAYS_ON_DEVICE: u8 = 0x0B;
const _UNFINISHED_BACKUP: u8 = 0x0C;
const _AUTOLOCK_DELAY_MS: u8 = 0x0D;
const _NO_BACKUP: u8 = 0x0E;
const _BACKUP_TYPE: u8 = 0x0F;
const _ROTATION: u8 = 0x10;
const _SLIP39_IDENTIFIER: u8 = 0x11;
const _SLIP39_ITERATION_EXPONENT: u8 = 0x12;
const _SD_SALT_AUTH_KEY: u8 = 0x13;
const INITIALIZED: u8 = 0x14;
const _SAFETY_CHECK_LEVEL: u8 = 0x15;
const _EXPERIMENTAL_FEATURES: u8 = 0x16;
extern "C" {
// storage.h
@ -53,43 +53,53 @@ extern "C" {
#[no_mangle]
pub extern "C" fn storagedevice_is_version_stored() -> Obj {
let block = || {
let key: u16 = 0x0102;
let key = _get_appkey(_VERSION);
let result = storagedevice_storage_has(key);
Ok(result.into())
};
unsafe { util::try_or_raise(block) }
}
// #[no_mangle]
// pub extern "C" fn storagedevice_get_version() -> Obj {
// let block = || {
// let key: u16 = 0x0102;
// let result = storagedevice_storage_get(key);
// Ok(result.into())
// };
// unsafe { util::try_or_raise(block) }
// }
#[no_mangle]
pub extern "C" fn storagedevice_get_version() -> Obj {
let block = || {
let key = _get_appkey(_VERSION);
let (buf, len) = storagedevice_storage_get(key);
let result = &buf[..len as usize];
// let result = storagedevice_storage_get(key);
result.try_into()
};
unsafe { util::try_or_raise(block) }
}
#[no_mangle]
pub extern "C" fn storagedevice_set_version(version: Obj) -> Obj {
let block = || {
let value = Buffer::try_from(version)?;
let key: u16 = 0x0102;
let key = _get_appkey(_VERSION);
let result = storagedevice_storage_set(key, value);
Ok(result.into())
};
unsafe { util::try_or_raise(block) }
}
// pub fn storagedevice_storage_get(key: u16) -> BufferMut {
// const MAX_LEN: usize = 300;
// // let mut buf: [u8; MAX_LEN] = [0; MAX_LEN];
// let mut buf: BufferMut;
// let mut len: u16 = 0;
// unsafe { storage_get(key, &mut buf as *mut _, MAX_LEN as u16, &mut len as
// *mut _) }; buf[..len as usize] as BufferMut
// }
// 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] {
pub fn storagedevice_storage_get(key: u16) -> ([u8; 300], u16) {
const MAX_LEN: usize = 300;
let mut buf: [u8; MAX_LEN] = [0; MAX_LEN];
// let mut buf: BufferMut;
let mut len: u16 = 0;
unsafe { storage_get(key, &mut buf as *mut _, MAX_LEN as u16, &mut len as *mut _) };
// TODO: when the result is empty, we could return None
// Would mean having Option<XXX> as the return type
// &buf[..len as usize]
// buf[..len as usize]
(buf, len)
}
pub fn storagedevice_storage_set(key: u16, value: Buffer) -> bool {
let len = value.len();
@ -112,3 +122,18 @@ pub fn storagedevice_storage_delete(key: u16) -> bool {
_ => false,
}
}
fn _get_appkey(key: u8) -> u16 {
((APP_DEVICE as u16) << 8) | key as u16
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn get_appkey() {
let result = _get_appkey(0x11);
assert_eq!(result, 0x0111);
}
}

View File

@ -6,6 +6,11 @@ def is_version_stored() -> bool:
"""Whether version is in storage."""
# extmod/rustmods/modtrezorstoragedevice.c
def get_version() -> bytes:
"""Get from storage."""
# extmod/rustmods/modtrezorstoragedevice.c
def set_version(version: bytes) -> bool:
"""Save to storage."""

View File

@ -15,7 +15,7 @@ def wipe() -> None:
def init_unlocked() -> None:
# Check for storage version upgrade.
version = device.get_version()
version = trezorstoragedevice.get_version()
if version == common.STORAGE_VERSION_01:
_migrate_from_version_01()

View File

@ -66,8 +66,8 @@ SD_SALT_AUTH_KEY_LEN_BYTES = const(16)
# return bool(common.get(_NAMESPACE, _VERSION))
def get_version() -> bytes | None:
return common.get(_NAMESPACE, _VERSION)
# def get_version() -> bytes | None:
# return common.get(_NAMESPACE, _VERSION)
# def set_version(version: bytes) -> None: