From 36ea69f6df3914bdd94e790d480231129afa04a4 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 22 Mar 2022 22:16:46 +0800 Subject: [PATCH 1/4] add a one-liner API call to block execution until PDDB is mounted --- services/pddb/src/lib.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/services/pddb/src/lib.rs b/services/pddb/src/lib.rs index 78b4e0646..1ef098e70 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -47,6 +47,7 @@ pub struct Pddb { /// like this are probably OK. keys: Arc >>>, trng: trng::Trng, + tt: ticktimer_server::Ticktimer, } impl Pddb { pub fn new() -> Self { @@ -86,6 +87,7 @@ impl Pddb { cb_handle: Some(handle), keys, trng: trng::Trng::new(&xns).unwrap(), + tt: ticktimer_server::Ticktimer::new().unwrap(), } } pub fn is_mounted(&self) -> bool { @@ -98,6 +100,19 @@ impl Pddb { _ => panic!("Internal error"), } } + /// This blocks until the PDDB is mounted by the end user. If `None` is specified for the poll_interval_ms, + /// A random interval between 1 and 2 seconds is chosen for the poll wait time. Randomizing the waiting time + /// helps to level out the task scheduler in the case that many threads are waiting on the PDDB simultaneously. + pub fn is_mounted_blocking(&self, poll_interval_ms: Option) { + let interval = if let Some(i) = poll_interval_ms { + i + } else { + (self.trng.get_u32().unwrap() % 1000) + 1000 + }; + while !self.is_mounted() { + self.tt.sleep_ms(interval as usize).unwrap(); + } + } pub fn try_mount(&self) -> bool { let ret = send_message(self.conn, Message::new_blocking_scalar( Opcode::TryMount.to_usize().unwrap(), 0, 0, 0, 0)).expect("couldn't execute IsMounted query"); From 7c3601bd25acc1b390ffb388d2b85bc70670da67 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 22 Mar 2022 22:17:04 +0800 Subject: [PATCH 2/4] convert the connection manager to use the new PDDB blocking call --- services/net/src/connection_manager.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/services/net/src/connection_manager.rs b/services/net/src/connection_manager.rs index 78abd6b0b..b82d7c23c 100644 --- a/services/net/src/connection_manager.rs +++ b/services/net/src/connection_manager.rs @@ -107,9 +107,7 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc Date: Tue, 22 Mar 2022 22:17:27 +0800 Subject: [PATCH 3/4] silence a warning in the loader module about an unused macro --- Cargo.lock | 8 ++++---- loader/curve25519-dalek-loader/Cargo.toml | 2 +- loader/curve25519-dalek-loader/src/lib.rs | 2 ++ services/engine-25519/Cargo.toml | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 421e717a0..f6aef5a28 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -743,7 +743,7 @@ dependencies = [ "criterion", "digest", "engine-25519", - "engine25519-as 0.1.0 (git+https://github.com/betrusted-io/engine25519-as.git?rev=274b82899d28535ac6d4b7869e26eef2039578d5)", + "engine25519-as 0.1.0 (git+https://github.com/betrusted-io/engine25519-as.git?rev=d249c967556b02ab5439eacb5078fa00c60b93d6)", "fiat-crypto", "hex", "log", @@ -898,7 +898,7 @@ dependencies = [ name = "engine-25519" version = "0.1.0" dependencies = [ - "engine25519-as 0.1.0 (git+https://github.com/betrusted-io/engine25519-as.git?rev=274b82899d28535ac6d4b7869e26eef2039578d5)", + "engine25519-as 0.1.0 (git+https://github.com/betrusted-io/engine25519-as.git?rev=d249c967556b02ab5439eacb5078fa00c60b93d6)", "llio", "log", "log-server", @@ -916,12 +916,12 @@ dependencies = [ [[package]] name = "engine25519-as" version = "0.1.0" -source = "git+https://github.com/betrusted-io/engine25519-as.git?rev=274b82899d28535ac6d4b7869e26eef2039578d5#274b82899d28535ac6d4b7869e26eef2039578d5" +source = "git+https://github.com/betrusted-io/engine25519-as.git?rev=6681e73c1fdc4a460b5ef9f9c7c91aef546d00f3#6681e73c1fdc4a460b5ef9f9c7c91aef546d00f3" [[package]] name = "engine25519-as" version = "0.1.0" -source = "git+https://github.com/betrusted-io/engine25519-as.git?rev=6681e73c1fdc4a460b5ef9f9c7c91aef546d00f3#6681e73c1fdc4a460b5ef9f9c7c91aef546d00f3" +source = "git+https://github.com/betrusted-io/engine25519-as.git?rev=d249c967556b02ab5439eacb5078fa00c60b93d6#d249c967556b02ab5439eacb5078fa00c60b93d6" [[package]] name = "enum_dispatch" diff --git a/loader/curve25519-dalek-loader/Cargo.toml b/loader/curve25519-dalek-loader/Cargo.toml index 6e70d743d..c0a3a8cde 100644 --- a/loader/curve25519-dalek-loader/Cargo.toml +++ b/loader/curve25519-dalek-loader/Cargo.toml @@ -70,7 +70,7 @@ optional = true [dependencies.engine25519-as] git = "https://github.com/betrusted-io/engine25519-as.git" -rev = "274b82899d28535ac6d4b7869e26eef2039578d5" +rev = "d249c967556b02ab5439eacb5078fa00c60b93d6" default-features = false features = [] optional = true diff --git a/loader/curve25519-dalek-loader/src/lib.rs b/loader/curve25519-dalek-loader/src/lib.rs index 2d56e78b2..807e52c03 100644 --- a/loader/curve25519-dalek-loader/src/lib.rs +++ b/loader/curve25519-dalek-loader/src/lib.rs @@ -31,6 +31,8 @@ // https://github.com/rust-analyzer/rust-analyzer/issues/8640 #![recursion_limit="512"] +#![allow(unused_macros)] // quiets an unused macro error imported from engine25519-as. It can't be quieted by just suppressing it in the source library. + //------------------------------------------------------------------------ // External dependencies: //------------------------------------------------------------------------ diff --git a/services/engine-25519/Cargo.toml b/services/engine-25519/Cargo.toml index 67884d47e..74c033d77 100644 --- a/services/engine-25519/Cargo.toml +++ b/services/engine-25519/Cargo.toml @@ -22,7 +22,7 @@ rkyv = {version = "0.4.3", default-features = false, features = ["const_generics [dependencies.engine25519-as] git="https://github.com/betrusted-io/engine25519-as.git" -rev="274b82899d28535ac6d4b7869e26eef2039578d5" +rev="d249c967556b02ab5439eacb5078fa00c60b93d6" default-features = false features = [] From 0bcc93c78e947d726b8ef4fcf20811cc07f2b2e0 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 24 Mar 2022 03:21:00 +0800 Subject: [PATCH 4/4] attempt to add persistent storage to hosted mode PDDB This will create a 100MiB file in tools/ppdb-images/hosted.bin (sorry!!) which is persistent storage for hosted mode. Delete the file if you want to start over from scratch. It's...pretty brittle. I expect problems, but maybe it will at least get things moving. --- services/llio/src/main.rs | 1 + services/pddb/src/backend/hosted.rs | 36 +++++++++++++++++++++++++++-- services/pddb/src/main.rs | 1 - 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/services/llio/src/main.rs b/services/llio/src/main.rs index c4a80ab97..7de99bc7c 100644 --- a/services/llio/src/main.rs +++ b/services/llio/src/main.rs @@ -602,6 +602,7 @@ mod implementation { pub fn set_usb_disable(&mut self, state: bool) { self.usb_disable = state; } + #[allow(dead_code)] pub fn tts_sleep_indicate(&mut self) { } } diff --git a/services/pddb/src/backend/hosted.rs b/services/pddb/src/backend/hosted.rs index c40e76191..39c048728 100644 --- a/services/pddb/src/backend/hosted.rs +++ b/services/pddb/src/backend/hosted.rs @@ -5,7 +5,9 @@ use std::sync::Once; use std::mem::MaybeUninit; use std::fs::File; +use std::fs::OpenOptions; use std::io::prelude::*; +use std::io::SeekFrom; // This is considered bad practice for Rust to use a global singleton. // However, this hack puts the burden of emulation on the emulator, while @@ -16,6 +18,7 @@ use std::io::prelude::*; // Note that this is a concurrently accessed, unsafe, unchecked vector. struct FlashSingleton { memory: Vec::, + disk: File, } fn flashmem() -> &'static mut FlashSingleton { @@ -24,12 +27,33 @@ fn flashmem() -> &'static mut FlashSingleton { unsafe { ONCE.call_once(|| { + let mut disk = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .open("../tools/pddb-images/hosted.bin") + .expect("Can't open a PDDB image file for writing"); + let mut memory = Vec::::with_capacity(PDDB_A_LEN); - for _ in 0..PDDB_A_LEN { - memory.push(0xFF); + if disk.metadata().unwrap().len() == 0 { + for _ in 0..PDDB_A_LEN { + memory.push(0xFF); + } + disk.write(&memory).expect("couldn't create initial disk image"); + } else { + match disk.read_to_end(&mut memory) { + Ok(bytes_read) => { + assert!(bytes_read == PDDB_A_LEN, "PDDB disk image is of an incorrect size, refusing to run!"); + } + _ => { + panic!("Can't read PDDB disk image, refusing to run!"); + } + } } + let flashmem = FlashSingleton { memory, + disk, }; SINGLETON.write(flashmem); }); @@ -100,12 +124,20 @@ impl HostedSpinor { ) { *dst = src; } + flashmem().disk.seek(SeekFrom::Start(offset as u64)).expect("couldn't seek PDDB"); + flashmem().disk.write(data).expect("couldn't write PDDB"); Ok(()) } pub fn bulk_erase(&self, start: u32, len: u32) -> Result<(), xous::Error> { for b in flashmem().memory.as_mut_slice()[(start - xous::PDDB_LOC) as usize .. (start - xous::PDDB_LOC + len) as usize].iter_mut() { *b = 0xFF; } + flashmem().disk.seek(SeekFrom::Start(start as u64)).expect("couldn't seek PDDB"); + let mut blank = Vec::::with_capacity(len as usize); + for _ in 0..len { + blank.push(0xFF); + } + flashmem().disk.write(&blank).expect("couldn't write PDDB"); Ok(()) } } \ No newline at end of file diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index 98ed7d455..be2a5f013 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -468,7 +468,6 @@ fn xmain() -> ! { } }); // spawn a delayed mount command, shortly after boot. There's too much going on at boot, and it blocks other things from coming up. - #[cfg(not(any(windows, unix)))] // skip this step if running in hosted mode let _ = thread::spawn({ let my_cid = my_cid.clone(); move || {