add routines to set/get EC readiness

The nominal purpose of this is to allow other services to delay
interactions that could interfere with an EC update.

Note that anyone could call this routine and mark the EC as not
ready, since the llio block is not secured and the API is public.

I don't think it's a security problem, but is a DoS vector.
This commit is contained in:
bunnie 2023-12-07 01:29:05 +08:00
parent ed3d1fda61
commit 5bff1f6c46
3 changed files with 38 additions and 1 deletions

View file

@ -104,7 +104,7 @@ pub(crate) enum Opcode {
AdcGpio5,
AdcGpio2,
/// partially tested -- events
/// events
EventComSubscribe, //(String<64>),
//EventRtcSubscribe, //(String<64>),
EventUsbAttachSubscribe, //(String<64>),
@ -113,6 +113,11 @@ pub(crate) enum Opcode {
EventUsbAttachEnable, //(bool),
EventActivityHappened,
/// Set EC status is ready
EventEcSetReady,
/// Query if EC status is ready
EventEcIsReady,
/// internal from handler to main loop
EventComHappened,
//EventRtcHappened,

View file

@ -190,6 +190,26 @@ impl Llio {
Message::new_scalar(Opcode::EventComEnable.to_usize().unwrap(), arg, 0, 0, 0)
).map(|_| ())
}
/// Called by the updater routine when EC is ready to run. Note: no authenticity check is done, anyone
/// could call this and troll the system.
pub fn set_ec_ready(&self, ready: bool) {
send_message(self.conn,
Message::new_scalar(Opcode::EventEcSetReady.to_usize().unwrap(), if ready {1} else {0}, 0, 0, 0)
).unwrap();
}
/// Used to poll if the EC has been set as ready by the updating routine. Domiciled in the LLIO because
/// this crate is already depended upon by most crates that need to poll this status, and we'd rather not
/// expose the COM directly to generic crates.
pub fn is_ec_ready(&self) -> bool {
let response = send_message(self.conn,
Message::new_blocking_scalar(Opcode::EventEcIsReady.to_usize().unwrap(), 0, 0, 0, 0)).unwrap();
if let xous::Result::Scalar1(val) = response {
val != 0
} else {
log::error!("LLIO: unexpected return value: {:#?}", response);
false
}
}
/// GPIO IRQ hook. When using this, ensure that the WFI power saving mode is turned off.
/// Otherwise interrupts that hit during power save mode can be missed.
pub fn hook_gpio_event_callback(&mut self, id: u32, cid: CID) -> Result<(), xous::Error> {

View file

@ -285,6 +285,12 @@ fn main() -> ! {
let mut i2c = llio::I2c::new(&xns);
let tt = ticktimer_server::Ticktimer::new().unwrap();
// a variable to track if the EC has been registered as ready. This should be set by a call from
// whatever handles the EC updating routine: it is responsible for checking if the EC is up to date,
// and once it is brought into an up to date state, it then sets this variable. That routine is
// nominally domiciled in the status server.
let mut ec_ready = false;
log::debug!("starting main loop");
loop {
let msg = xous::receive_message(llio_sid).unwrap();
@ -507,6 +513,12 @@ fn main() -> ! {
latest_activity %= period;
}
}),
Some(Opcode::EventEcSetReady) => msg_scalar_unpack!(msg, ready, _, _, _, {
ec_ready = ready != 0;
}),
Some(Opcode::EventEcIsReady) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
xous::return_scalar(msg.sender, if ec_ready {1} else {0}).ok();
}),
Some(Opcode::SetWakeupAlarm) => msg_blocking_scalar_unpack!(msg, delay, _, _, _, {
if delay > u8::MAX as usize {
log::error!("Wakeup must be no longer than {} secs in the future", u8::MAX);