From 3f1ba5e27972021cad1e135a9565e1ee0b193021 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 13 Sep 2021 23:28:54 +0800 Subject: [PATCH] add com trng seed, uptime on EC; EC reset test commands --- Cargo.lock | 3 ++- imports/com_rs-ref/Cargo.toml | 2 +- services/com/Cargo.toml | 1 + services/com/src/api.rs | 6 +++++ services/com/src/lib.rs | 16 ++++++++++++++ services/com/src/main.rs | 30 +++++++++++++++++++++++++ services/shellchat/src/cmds/test.rs | 34 +++++++++++++++++++++++++++-- 7 files changed, 88 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 86cd8c41f..355087972 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -418,6 +418,7 @@ dependencies = [ "rkyv 0.4.3", "susres", "ticktimer-server", + "trng", "typenum", "utralib", "xous", @@ -428,7 +429,7 @@ dependencies = [ [[package]] name = "com_rs" version = "0.1.0" -source = "git+https://github.com/betrusted-io/com_rs.git?rev=ebbe15a5285cc4b9de7edf279949f034e82709dd#ebbe15a5285cc4b9de7edf279949f034e82709dd" +source = "git+https://github.com/betrusted-io/com_rs.git?rev=5338f9959572d57f1014d04aa129804da5b4d339#5338f9959572d57f1014d04aa129804da5b4d339" [[package]] name = "com_rs-ref" diff --git a/imports/com_rs-ref/Cargo.toml b/imports/com_rs-ref/Cargo.toml index 61fd164cd..80c2df69c 100644 --- a/imports/com_rs-ref/Cargo.toml +++ b/imports/com_rs-ref/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" description = "com_rs import package" [dependencies] -com_rs = { git = "https://github.com/betrusted-io/com_rs.git", rev = "ebbe15a5285cc4b9de7edf279949f034e82709dd" } +com_rs = { git = "https://github.com/betrusted-io/com_rs.git", rev = "5338f9959572d57f1014d04aa129804da5b4d339" } #com_rs = { path = "../../../betrusted-ec/sw/com_rs" } diff --git a/services/com/Cargo.toml b/services/com/Cargo.toml index f4f7a5a47..823e74ca8 100644 --- a/services/com/Cargo.toml +++ b/services/com/Cargo.toml @@ -15,6 +15,7 @@ typenum = "1.12" xous = {path = "../../xous-rs"} xous-ipc = {path = "../../xous-ipc"} xous-names = {path = "../xous-names"} +trng = {path = "../trng"} num-derive = {version = "0.3", default-features = false} num-traits = {version = "0.2", default-features = false} diff --git a/services/com/src/api.rs b/services/com/src/api.rs index ebe79a82f..e83c4229e 100644 --- a/services/com/src/api.rs +++ b/services/com/src/api.rs @@ -61,6 +61,12 @@ pub struct FlashRecord { #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] pub(crate) enum Opcode { + /// Refresh the TRNG seed for the EC + ReseedTrng, + + /// Fetch the uptime of the EC + GetUptime, + /// Battery stats BattStats, diff --git a/services/com/src/lib.rs b/services/com/src/lib.rs index 961c8e5b5..cbc3db964 100644 --- a/services/com/src/lib.rs +++ b/services/com/src/lib.rs @@ -78,6 +78,22 @@ impl Com { } } + pub fn reseed_ec_trng(&self) -> Result<(), xous::Error> { + send_message(self.conn, + Message::new_scalar(Opcode::ReseedTrng.to_usize().unwrap(), 0, 0, 0, 0) + ).map(|_| ()) + } + pub fn get_ec_uptime(&self) -> Result { + let response = send_message(self.conn, + Message::new_blocking_scalar(Opcode::GetUptime.to_usize().unwrap(), 0, 0, 0, 0) + )?; + if let xous::Result::Scalar2(lsb, msb) = response { + Ok( lsb as u64 | (msb as u64) << 32) + } else { + Err(xous::Error::InternalError) + } + } + pub fn get_wf200_fw_rev(&self) -> Result<(u8, u8, u8), xous::Error> { let response = send_message(self.conn, Message::new_blocking_scalar(Opcode::Wf200Rev.to_usize().unwrap(), 0, 0, 0, 0))?; diff --git a/services/com/src/main.rs b/services/com/src/main.rs index a24cdb2b3..ea2e4b700 100644 --- a/services/com/src/main.rs +++ b/services/com/src/main.rs @@ -331,6 +331,17 @@ fn xmain() -> ! { const FLASH_LEN: u32 = 0x10_0000; const FLASH_TIMEOUT: u32 = 250; + // initial seed of the COM trng + let trng = trng::Trng::new(&xns).expect("couldn't connect to TRNG"); + com.txrx(ComState::TRNG_SEED.verb); + for _ in 0..2 { + let mut rng = trng.get_u64().expect("couldn't fetch rngs"); + for _ in 0..4 { + com.txrx(rng as u16); + rng >>= 16; + } + } + trace!("starting main loop"); loop { let mut msg = xous::receive_message(com_sid).unwrap(); @@ -347,6 +358,25 @@ fn xmain() -> ! { com.txrx(ComState::BL_START.verb | (bl_main as u16) & 0x1f | (((bl_sec as u16) & 0x1f) << 5)); } }), + Some(Opcode::ReseedTrng) => xous::msg_scalar_unpack!(msg, _, _, _, _, { + com.txrx(ComState::TRNG_SEED.verb); + for _ in 0..2 { + let mut rng = trng.get_u64().expect("couldn't fetch rngs"); + for _ in 0..4 { + com.txrx(rng as u16); + rng >>= 16; + } + } + }), + Some(Opcode::GetUptime) => xous::msg_blocking_scalar_unpack!(msg, _, _, _, _, { + let mut uptime: u64 = 0; + com.txrx(ComState::UPTIME.verb); + for _ in 0..4 { + uptime >>= 16; + uptime |= (com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)) as u64) << 48; + } + xous::return_scalar2(msg.sender, uptime as usize, (uptime >> 32) as usize).expect("couldn't return uptime"); + }), Some(Opcode::FlashAcquire) => msg_blocking_scalar_unpack!(msg, id0, id1, id2, id3, { let acquired = if flash_id.is_none() { flash_id = Some([id0 as u32, id1 as u32, id2 as u32, id3 as u32]); diff --git a/services/shellchat/src/cmds/test.rs b/services/shellchat/src/cmds/test.rs index b0993761c..1463175c5 100644 --- a/services/shellchat/src/cmds/test.rs +++ b/services/shellchat/src/cmds/test.rs @@ -76,7 +76,7 @@ impl<'a> ShellCmdApi<'a> for Test { self.state += 1; let mut ret = String::<1024>::new(); - write!(ret, "Test has run {} times.\n", self.state).unwrap(); + write!(ret, "Test has run {} times.", self.state).unwrap(); let mut tokens = args.as_str().unwrap().split(' '); @@ -189,7 +189,10 @@ impl<'a> ShellCmdApi<'a> for Test { ); } - env.ticktimer.sleep_ms(3000).unwrap(); // wait so we have some realistic delta on the datetime function + let ut = env.com.get_ec_uptime().unwrap(); + env.llio.ec_reset().unwrap(); + + env.ticktimer.sleep_ms(4000).unwrap(); // wait so we have some realistic delta on the datetime function self.end_elapsed = Some(env.ticktimer.elapsed_ms()); self.end_time = rtc_get(&mut env.llio); @@ -224,6 +227,14 @@ impl<'a> ShellCmdApi<'a> for Test { log::info!("{}|RTC|FAIL|NO_END|", SENTINEL); } + let ut_after = env.com.get_ec_uptime().unwrap(); + + if ut < ut_after || ut_after > 4200 { + log::info!("{}|ECRESET|FAIL|{}|{}|", SENTINEL, ut, ut_after); + } else { + log::info!("{}|ECRESET|PASS|{}|{}|", SENTINEL, ut, ut_after); + } + log::info!("{}|DONE|", SENTINEL); write!(ret, "Factory test script has run, check serial terminal for output").unwrap(); env.llio.wfi_override(false).unwrap(); @@ -247,6 +258,25 @@ impl<'a> ShellCmdApi<'a> for Test { env.llio.wfi_override(false).unwrap(); log::info!("{}|WFIRESET|", SENTINEL); } + "wfioff" => { + env.llio.wfi_override(true).unwrap(); + log::info!("{}|WFIOFF|", SENTINEL); + } + "vibe" => { + env.llio.vibe(llio::VibePattern::Long).unwrap(); + log::info!("{}|VIBE|", SENTINEL); + } + "booston" => { + env.llio.boost_on(true).unwrap(); + env.com.set_boost(true).unwrap(); + log::info!("{}|BOOSTON|", SENTINEL); + } + "boostoff" => { + env.com.set_boost(false).unwrap(); + env.ticktimer.sleep_ms(50).unwrap(); + env.llio.boost_on(false).unwrap(); + log::info!("{}|BOOSTOFF|", SENTINEL); + } "astart" => { self.freq = if let Some(freq_str) = tokens.next() { match freq_str.parse::() {