From 1617200cb647ead5cd07304f4ad0a728b0b6c531 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 27 Mar 2022 02:56:41 +0800 Subject: [PATCH] remove the RTC "reach-around" cheat inside shellchat's test routine The test routine for shellchat "cheats" and accesses the I2C block to test the RTC during factory operations. This is not aesthetic and is also potentially a security hole since we don't want less trusted stuff willy-nilly playing with i2c. Rewrite to 1) remove i2c references from shellchat 2) replace RTC calls with the newer non-dangerous llio calls that are limited to read out the initialized time variables. --- services/shellchat/src/cmds.rs | 2 - services/shellchat/src/cmds/test.rs | 131 +++------------------------- 2 files changed, 14 insertions(+), 119 deletions(-) diff --git a/services/shellchat/src/cmds.rs b/services/shellchat/src/cmds.rs index 93356b5c6..1f4428d7f 100644 --- a/services/shellchat/src/cmds.rs +++ b/services/shellchat/src/cmds.rs @@ -40,7 +40,6 @@ use trng::*; /////////////////////////// Command shell integration pub struct CommonEnv { llio: llio::Llio, - i2c: llio::I2c, com: com::Com, ticktimer: ticktimer_server::Ticktimer, gam: gam::Gam, @@ -157,7 +156,6 @@ impl CmdEnv { let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer"); let mut common = CommonEnv { llio: llio::Llio::new(&xns), - i2c: llio::I2c::new(&xns), com: com::Com::new(&xns).expect("could't connect to COM"), ticktimer, gam: gam::Gam::new(&xns).expect("couldn't connect to GAM"), diff --git a/services/shellchat/src/cmds/test.rs b/services/shellchat/src/cmds/test.rs index c3d666544..33acf70f7 100644 --- a/services/shellchat/src/cmds/test.rs +++ b/services/shellchat/src/cmds/test.rs @@ -2,18 +2,16 @@ use crate::oqc_test::OqcOp; use crate::{ShellCmdApi,CommonEnv}; use xous_ipc::String; use xous::{MessageEnvelope, Message}; -use llio::I2cStatus; use codec::*; use spectrum_analyzer::{FrequencyLimit, FrequencySpectrum, samples_fft_to_spectrum}; use spectrum_analyzer::windows::hann_window; -use llio::{DateTime, Weekday}; use core::fmt::Write; use core::sync::atomic::{AtomicBool, Ordering, AtomicU32}; use std::sync::Arc; use num_traits::*; -use std::time::{Instant, Duration}; +use std::time::Instant; static AUDIO_OQC: AtomicBool = AtomicBool::new(false); @@ -32,8 +30,8 @@ pub struct Test { right_play: bool, speaker_play: bool, freq: f32, - start_time: Option, - end_time: Option, + start_time: Option, + end_time: Option, start_elapsed: Option, end_elapsed: Option, oqc_cid: Option, @@ -116,13 +114,14 @@ impl<'a> ShellCmdApi<'a> for Test { write!(ret, "end elapsed_ms {}\n", env.ticktimer.elapsed_ms()).unwrap(); } "factory" => { - // force a specified time to make sure the elapsed time computation later on works - if !rtc_set(&mut env.i2c, 0, 0, 10, 1, 6, 21) { - log::info!("{}|RTC|FAIL|SET|", SENTINEL); - } - + self.start_time = match env.llio.get_rtc_secs() { + Ok(s) => Some(s), + _ => { + log::info!("{}|RTC|FAIL|SET|", SENTINEL); + None + }, + }; self.start_elapsed = Some(env.ticktimer.elapsed_ms()); - self.start_time = rtc_get(&mut env.i2c); // set uart MUX, and turn off WFI so UART reports are "clean" (no stuck characters when CPU is in WFI) env.llio.set_uart_mux(llio::UartType::Log).unwrap(); @@ -227,25 +226,12 @@ impl<'a> ShellCmdApi<'a> for Test { env.ticktimer.sleep_ms(6000).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.i2c); + self.end_time = env.llio.get_rtc_secs().ok(); let exact_time_secs = ((self.end_elapsed.unwrap() - self.start_elapsed.unwrap()) / 1000) as i32; - if let Some(end_dt) = self.end_time { - if let Some(start_dt) = self.start_time { - // this method of computation fails if the test was started just before midnight - // on the last day of the month and completes just after. we set the time so we ensure this - // can't happen. - let end_secs = - end_dt.days as i32 * 24 * 60 * 60 + - end_dt.hours as i32 * 60 * 60 + - end_dt.minutes as i32 * 60 + - end_dt.seconds as i32; - let start_secs = - start_dt.days as i32 * 24 * 60 * 60 + - start_dt.hours as i32 * 60 * 60 + - start_dt.minutes as i32 * 60 + - start_dt.seconds as i32; - let elapsed_secs = end_secs - start_secs; + if let Some(end_secs) = self.end_time { + if let Some(start_secs) = self.start_time { + let elapsed_secs = (end_secs - start_secs) as i32; let delta = exact_time_secs - elapsed_secs; if delta.abs() > 2 { @@ -789,95 +775,6 @@ fn asciiplot(fs: &FrequencySpectrum) { log::debug!("max freq: {}, max_val: {}", max_f, max_val); } - -/// convert binary to BCD -fn to_bcd(binary: u8) -> u8 { - let mut lsd: u8 = binary % 10; - if lsd > 9 { - lsd = 9; - } - let mut msd: u8 = binary / 10; - if msd > 9 { - msd = 9; - } - - (msd << 4) | lsd -} - -fn to_binary(bcd: u8) -> u8 { - (bcd & 0xf) + ((bcd >> 4) * 10) -} - -fn to_weekday(bcd: u8) -> Weekday { - match bcd { - 0 => Weekday::Sunday, - 1 => Weekday::Monday, - 2 => Weekday::Tuesday, - 3 => Weekday::Wednesday, - 4 => Weekday::Thursday, - 5 => Weekday::Friday, - 6 => Weekday::Saturday, - _ => Weekday::Sunday, - } -} -const ABRTCMC_I2C_ADR: u8 = 0x68; -const ABRTCMC_CONTROL3: u8 = 0x02; -const ABRTCMC_SECONDS: u8 = 0x3; - -// vendor in the RTC code -- the RTC system was not architected to do our test sets -// in particular we need a synchronous callback on the date/time, which is not terribly useful in most other contexts -// so instead of burdening the OS with it, we just incorprate it specifically into this test function -fn rtc_set(i2c: &mut llio::I2c, secs: u8, mins: u8, hours: u8, days: u8, months: u8, years: u8) -> bool { - let mut txbuf: [u8; 8] = [0; 8]; - - // convert enum to bitfields - let d = 1; - - // make sure battery switchover is enabled, otherwise we won't keep time when power goes off - txbuf[0] = 0; - txbuf[1] = to_bcd(secs); - txbuf[2] = to_bcd(mins); - txbuf[3] = to_bcd(hours); - txbuf[4] = to_bcd(days); - txbuf[5] = to_bcd(d); - txbuf[6] = to_bcd(months); - txbuf[7] = to_bcd(years); - - match i2c.i2c_write(ABRTCMC_I2C_ADR, ABRTCMC_CONTROL3, &txbuf) { - Ok(status) => { - match status { - I2cStatus::ResponseWriteOk => true, - I2cStatus::ResponseBusy => {log::error!("i2c server busy on RTC test function"); false}, - _ => {log::error!("try_send_i2c unhandled response: {:?}", status); false}, - } - } - _ => {log::error!("try_send_i2c unhandled error"); false} - } -} -fn rtc_get(i2c: &mut llio::I2c) -> Option { - let mut rxbuf = [0; 7]; - match i2c.i2c_read(ABRTCMC_I2C_ADR, ABRTCMC_SECONDS, &mut rxbuf, None) { - Ok(status) => { - match status { - I2cStatus::ResponseReadOk => { - let dt = DateTime { - seconds: to_binary(rxbuf[0] & 0x7f), - minutes: to_binary(rxbuf[1] & 0x7f), - hours: to_binary(rxbuf[2] & 0x3f), - days: to_binary(rxbuf[3] & 0x3f), - weekday: to_weekday(rxbuf[4] & 0x7f), - months: to_binary(rxbuf[5] & 0x1f), - years: to_binary(rxbuf[6]), - }; - Some(dt) - }, - _ => None, - } - } - _ => None - } -} - fn oqc_status(conn: xous::CID) -> Option { // None if still running or not yet run; Some(true) if pass; Some(false) if fail let result = xous::send_message(conn, xous::Message::new_blocking_scalar(OqcOp::Status.to_usize().unwrap(), 0, 0, 0, 0)