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:
parent
6702df58dd
commit
bcf837694b
@ -23,30 +23,29 @@
|
|||||||
|
|
||||||
#include "librust.h"
|
#include "librust.h"
|
||||||
|
|
||||||
|
|
||||||
/// def is_version_stored() -> bool:
|
/// def is_version_stored() -> bool:
|
||||||
/// """Whether version is in storage."""
|
/// """Whether version is in storage."""
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_storagedevice_is_version_stored_obj,
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(
|
||||||
storagedevice_is_version_stored);
|
mod_trezorutils_storagedevice_is_version_stored_obj,
|
||||||
|
storagedevice_is_version_stored);
|
||||||
|
|
||||||
// /// def get_version() -> bool:
|
/// def get_version() -> bytes:
|
||||||
// /// """Get from storage."""
|
/// """Get from storage."""
|
||||||
// STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorutils_storagedevice_get_version_obj,
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_storagedevice_get_version_obj,
|
||||||
// storagedevice_get_version);
|
storagedevice_get_version);
|
||||||
|
|
||||||
/// def set_version(version: bytes) -> bool:
|
/// def set_version(version: bytes) -> bool:
|
||||||
/// """Save to storage."""
|
/// """Save to storage."""
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorutils_storagedevice_set_version_obj,
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorutils_storagedevice_set_version_obj,
|
||||||
storagedevice_set_version);
|
storagedevice_set_version);
|
||||||
|
|
||||||
|
|
||||||
STATIC const mp_rom_map_elem_t mp_module_trezorstoragedevice_globals_table[] = {
|
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___name__), MP_ROM_QSTR(MP_QSTR_trezorstoragedevice)},
|
||||||
|
|
||||||
{MP_ROM_QSTR(MP_QSTR_is_version_stored),
|
{MP_ROM_QSTR(MP_QSTR_is_version_stored),
|
||||||
MP_ROM_PTR(&mod_trezorutils_storagedevice_is_version_stored_obj)},
|
MP_ROM_PTR(&mod_trezorutils_storagedevice_is_version_stored_obj)},
|
||||||
// {MP_ROM_QSTR(MP_QSTR_get_version),
|
{MP_ROM_QSTR(MP_QSTR_get_version),
|
||||||
// MP_ROM_PTR(&mod_trezorutils_storagedevice_get_version_obj)},
|
MP_ROM_PTR(&mod_trezorutils_storagedevice_get_version_obj)},
|
||||||
{MP_ROM_QSTR(MP_QSTR_set_version),
|
{MP_ROM_QSTR(MP_QSTR_set_version),
|
||||||
MP_ROM_PTR(&mod_trezorutils_storagedevice_set_version_obj)},
|
MP_ROM_PTR(&mod_trezorutils_storagedevice_set_version_obj)},
|
||||||
};
|
};
|
||||||
|
@ -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 protobuf_encode(mp_obj_t buf, mp_obj_t obj);
|
||||||
|
|
||||||
mp_obj_t storagedevice_is_version_stored();
|
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);
|
mp_obj_t storagedevice_set_version(mp_obj_t key);
|
||||||
|
|
||||||
#ifdef TREZOR_EMULATOR
|
#ifdef TREZOR_EMULATOR
|
||||||
|
@ -8,30 +8,30 @@ use core::convert::{TryFrom, TryInto};
|
|||||||
|
|
||||||
// TODO: transfer this into a struct with field specifying data type and
|
// TODO: transfer this into a struct with field specifying data type and
|
||||||
// max_length
|
// max_length
|
||||||
const APP_DEVICE: u16 = 0x01;
|
const APP_DEVICE: u8 = 0x01;
|
||||||
|
|
||||||
const DEVICE_ID: u16 = 0x01;
|
const DEVICE_ID: u8 = 0x01;
|
||||||
const _VERSION: u16 = 0x02;
|
const _VERSION: u8 = 0x02;
|
||||||
const _MNEMONIC_SECRET: u16 = 0x03;
|
const _MNEMONIC_SECRET: u8 = 0x03;
|
||||||
const _LANGUAGE: u16 = 0x04;
|
const _LANGUAGE: u8 = 0x04;
|
||||||
const _LABEL: u16 = 0x05;
|
const _LABEL: u8 = 0x05;
|
||||||
const _USE_PASSPHRASE: u16 = 0x06;
|
const _USE_PASSPHRASE: u8 = 0x06;
|
||||||
const _HOMESCREEN: u16 = 0x07;
|
const _HOMESCREEN: u8 = 0x07;
|
||||||
const _NEEDS_BACKUP: u16 = 0x08;
|
const _NEEDS_BACKUP: u8 = 0x08;
|
||||||
const _FLAGS: u16 = 0x09;
|
const _FLAGS: u8 = 0x09;
|
||||||
const U2F_COUNTER: u16 = 0x0A;
|
const U2F_COUNTER: u8 = 0x0A;
|
||||||
const _PASSPHRASE_ALWAYS_ON_DEVICE: u16 = 0x0B;
|
const _PASSPHRASE_ALWAYS_ON_DEVICE: u8 = 0x0B;
|
||||||
const _UNFINISHED_BACKUP: u16 = 0x0C;
|
const _UNFINISHED_BACKUP: u8 = 0x0C;
|
||||||
const _AUTOLOCK_DELAY_MS: u16 = 0x0D;
|
const _AUTOLOCK_DELAY_MS: u8 = 0x0D;
|
||||||
const _NO_BACKUP: u16 = 0x0E;
|
const _NO_BACKUP: u8 = 0x0E;
|
||||||
const _BACKUP_TYPE: u16 = 0x0F;
|
const _BACKUP_TYPE: u8 = 0x0F;
|
||||||
const _ROTATION: u16 = 0x10;
|
const _ROTATION: u8 = 0x10;
|
||||||
const _SLIP39_IDENTIFIER: u16 = 0x11;
|
const _SLIP39_IDENTIFIER: u8 = 0x11;
|
||||||
const _SLIP39_ITERATION_EXPONENT: u16 = 0x12;
|
const _SLIP39_ITERATION_EXPONENT: u8 = 0x12;
|
||||||
const _SD_SALT_AUTH_KEY: u16 = 0x13;
|
const _SD_SALT_AUTH_KEY: u8 = 0x13;
|
||||||
const INITIALIZED: u16 = 0x14;
|
const INITIALIZED: u8 = 0x14;
|
||||||
const _SAFETY_CHECK_LEVEL: u16 = 0x15;
|
const _SAFETY_CHECK_LEVEL: u8 = 0x15;
|
||||||
const _EXPERIMENTAL_FEATURES: u16 = 0x16;
|
const _EXPERIMENTAL_FEATURES: u8 = 0x16;
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
// storage.h
|
// storage.h
|
||||||
@ -53,43 +53,53 @@ extern "C" {
|
|||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn storagedevice_is_version_stored() -> Obj {
|
pub extern "C" fn storagedevice_is_version_stored() -> Obj {
|
||||||
let block = || {
|
let block = || {
|
||||||
let key: u16 = 0x0102;
|
let key = _get_appkey(_VERSION);
|
||||||
let result = storagedevice_storage_has(key);
|
let result = storagedevice_storage_has(key);
|
||||||
Ok(result.into())
|
Ok(result.into())
|
||||||
};
|
};
|
||||||
unsafe { util::try_or_raise(block) }
|
unsafe { util::try_or_raise(block) }
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[no_mangle]
|
#[no_mangle]
|
||||||
// pub extern "C" fn storagedevice_get_version() -> Obj {
|
pub extern "C" fn storagedevice_get_version() -> Obj {
|
||||||
// let block = || {
|
let block = || {
|
||||||
// let key: u16 = 0x0102;
|
let key = _get_appkey(_VERSION);
|
||||||
// let result = storagedevice_storage_get(key);
|
let (buf, len) = storagedevice_storage_get(key);
|
||||||
// Ok(result.into())
|
let result = &buf[..len as usize];
|
||||||
// };
|
// let result = storagedevice_storage_get(key);
|
||||||
// unsafe { util::try_or_raise(block) }
|
result.try_into()
|
||||||
// }
|
};
|
||||||
|
unsafe { util::try_or_raise(block) }
|
||||||
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn storagedevice_set_version(version: Obj) -> Obj {
|
pub extern "C" fn storagedevice_set_version(version: Obj) -> Obj {
|
||||||
let block = || {
|
let block = || {
|
||||||
let value = Buffer::try_from(version)?;
|
let value = Buffer::try_from(version)?;
|
||||||
|
|
||||||
let key: u16 = 0x0102;
|
let key = _get_appkey(_VERSION);
|
||||||
let result = storagedevice_storage_set(key, value);
|
let result = storagedevice_storage_set(key, value);
|
||||||
Ok(result.into())
|
Ok(result.into())
|
||||||
};
|
};
|
||||||
unsafe { util::try_or_raise(block) }
|
unsafe { util::try_or_raise(block) }
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn storagedevice_storage_get(key: u16) -> BufferMut {
|
// TODO: find out how to return the real result
|
||||||
// const MAX_LEN: usize = 300;
|
// pub fn storagedevice_storage_get(key: u16) -> &'static [u8] {
|
||||||
// // let mut buf: [u8; MAX_LEN] = [0; MAX_LEN];
|
// pub fn storagedevice_storage_get(key: u16) -> [u8] {
|
||||||
// let mut buf: BufferMut;
|
pub fn storagedevice_storage_get(key: u16) -> ([u8; 300], u16) {
|
||||||
// let mut len: u16 = 0;
|
const MAX_LEN: usize = 300;
|
||||||
// unsafe { storage_get(key, &mut buf as *mut _, MAX_LEN as u16, &mut len as
|
let mut buf: [u8; MAX_LEN] = [0; MAX_LEN];
|
||||||
// *mut _) }; buf[..len as usize] as BufferMut
|
// 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 {
|
pub fn storagedevice_storage_set(key: u16, value: Buffer) -> bool {
|
||||||
let len = value.len();
|
let len = value.len();
|
||||||
@ -112,3 +122,18 @@ pub fn storagedevice_storage_delete(key: u16) -> bool {
|
|||||||
_ => false,
|
_ => 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
#include "secbool.h"
|
#include "secbool.h"
|
||||||
#include "usb.h"
|
#include "usb.h"
|
||||||
|
@ -6,6 +6,11 @@ def is_version_stored() -> bool:
|
|||||||
"""Whether version is in storage."""
|
"""Whether version is in storage."""
|
||||||
|
|
||||||
|
|
||||||
|
# extmod/rustmods/modtrezorstoragedevice.c
|
||||||
|
def get_version() -> bytes:
|
||||||
|
"""Get from storage."""
|
||||||
|
|
||||||
|
|
||||||
# extmod/rustmods/modtrezorstoragedevice.c
|
# extmod/rustmods/modtrezorstoragedevice.c
|
||||||
def set_version(version: bytes) -> bool:
|
def set_version(version: bytes) -> bool:
|
||||||
"""Save to storage."""
|
"""Save to storage."""
|
||||||
|
@ -15,7 +15,7 @@ def wipe() -> None:
|
|||||||
|
|
||||||
def init_unlocked() -> None:
|
def init_unlocked() -> None:
|
||||||
# Check for storage version upgrade.
|
# Check for storage version upgrade.
|
||||||
version = device.get_version()
|
version = trezorstoragedevice.get_version()
|
||||||
if version == common.STORAGE_VERSION_01:
|
if version == common.STORAGE_VERSION_01:
|
||||||
_migrate_from_version_01()
|
_migrate_from_version_01()
|
||||||
|
|
||||||
|
@ -66,8 +66,8 @@ SD_SALT_AUTH_KEY_LEN_BYTES = const(16)
|
|||||||
# return bool(common.get(_NAMESPACE, _VERSION))
|
# return bool(common.get(_NAMESPACE, _VERSION))
|
||||||
|
|
||||||
|
|
||||||
def get_version() -> bytes | None:
|
# def get_version() -> bytes | None:
|
||||||
return common.get(_NAMESPACE, _VERSION)
|
# return common.get(_NAMESPACE, _VERSION)
|
||||||
|
|
||||||
|
|
||||||
# def set_version(version: bytes) -> None:
|
# def set_version(version: bytes) -> None:
|
||||||
|
Loading…
Reference in New Issue
Block a user