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

WIP - extend trezorhal translations interface

This commit is contained in:
matejcik 2024-01-15 13:47:15 +01:00
parent f1e25c9837
commit 43ef1f29d8
4 changed files with 25 additions and 18 deletions

View File

@ -291,6 +291,9 @@ fn generate_trezorhal_bindings() {
.allowlist_function("storage_set_counter") .allowlist_function("storage_set_counter")
.allowlist_function("storage_next_counter") .allowlist_function("storage_next_counter")
.allowlist_function("translations_read") .allowlist_function("translations_read")
.allowlist_function("translations_write")
.allowlist_function("translations_erase")
.allowlist_function("translations_area_bytesize")
// display // display
.allowlist_function("display_clear") .allowlist_function("display_clear")
.allowlist_function("display_offset") .allowlist_function("display_offset")

View File

@ -1,27 +1,29 @@
use super::ffi; use super::ffi;
#[repr(C)] // SAFETY: Returned slice is valid and immutable until a call to `erase()` and/or `set_blob()`.
pub struct PointerData { // Caller is responsible for disposing of all references to the slice before touching
pub ptr: *const u8, // the flash contents.
pub len: u32, pub unsafe fn get_blob<'a>() -> &'a [u8] {
}
pub fn get_translations_blob() -> &'static [u8] {
let mut len: u32 = 0; let mut len: u32 = 0;
let ptr = unsafe { ffi::translations_read(&mut len, 0) }; let ptr = unsafe { ffi::translations_read(&mut len, 0) };
if ptr.is_null() { if ptr.is_null() {
fatal_error!("Translations read failed", ""); fatal_error!("Translations read failed", "");
} }
// SAFETY: The pointer is always valid.
unsafe { core::slice::from_raw_parts(ptr, len as usize) } unsafe { core::slice::from_raw_parts(ptr, len as usize) }
} }
pub fn get_pointer_with_offset(offset: u16, len: u16) -> PointerData { // SAFETY: This call invalidates the reference to the blob returned by `get_blob()`.
let ptr = unsafe { ffi::translations_read(&mut 0, offset.into()) }; pub unsafe fn erase() {
if ptr.is_null() { unsafe { ffi::translations_erase() };
fatal_error!("Translations read failed", ""); }
}
PointerData { pub fn area_bytesize() -> usize {
ptr, // SAFETY: Safe, no side effects.
len: len.into(), unsafe { ffi::translations_area_bytesize() as usize }
} }
// SAFETY: This call may invalidate the reference to the blob returned by `get_blob()`.
pub unsafe fn write(data: &[u8], offset: usize) {
unsafe { ffi::translations_write(data.as_ptr(), offset as u32, data.len() as u32) };
} }

View File

@ -5,9 +5,11 @@
#include "flash.h" #include "flash.h"
#include "model.h" #include "model.h"
void translations_write(uint8_t* data, uint32_t offset, uint32_t len) { void translations_write(const uint8_t* data, uint32_t offset, uint32_t len) {
// TODO maybe return errors from here?
ensure(flash_unlock_write(), "translations_write unlock"); ensure(flash_unlock_write(), "translations_write unlock");
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
// TODO optimize by writing by (quad)words
ensure(flash_area_write_byte(&TRANSLATIONS_AREA, offset + i, data[i]), ensure(flash_area_write_byte(&TRANSLATIONS_AREA, offset + i, data[i]),
"translations_write write"); "translations_write write");
} }

View File

@ -1,6 +1,6 @@
#include <stdint.h> #include <stdint.h>
void translations_write(uint8_t* data, uint32_t offset, uint32_t len); void translations_write(const uint8_t* data, uint32_t offset, uint32_t len);
const uint8_t* translations_read(uint32_t* len, uint32_t offset); const uint8_t* translations_read(uint32_t* len, uint32_t offset);