From 2b19c4bd2162658b5a992d6fd69cb904eaf3fff5 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 16 Jan 2026 17:32:12 +0800 Subject: [PATCH] add a command for setting the bootwait flag The keystore is a sensitive server, and the one-way counters have security-critical propoerties. So instead of revealing a generic one-way counter API to the world, specific, limited requests to do things are going to be created for the keystore. In this case, the bootwait flag would be convenient to be able to set from userspace. The motivation for this requirement is actually continuous integration - a normal human could just hold down a button while turning on power, but in CI, I don't have a robotic thumb (or an alternative electrical signal) to simulate the nerve-pinch on boot. Previously I was "cheating" and simulating this by nuking the loader, which effectively forces a bootwait. However, in production, this is *not possible* because the JTAG port is sealed off. Thus, this may be the first of a series of small PRs to incorporate CI tooling that clears up tech debt like nuking the loader to get into boot wait. --- libs/keystore-api/src/gen2_api.rs | 4 ++++ services/keystore/src/lib.rs | 23 +++++++++++++++++++ .../keystore/src/platform/baosec/server.rs | 16 +++++++++++++ .../keystore/src/platform/baosec/store.rs | 18 ++++++++++++++- 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/libs/keystore-api/src/gen2_api.rs b/libs/keystore-api/src/gen2_api.rs index 87098ea4c..290c60e63 100644 --- a/libs/keystore-api/src/gen2_api.rs +++ b/libs/keystore-api/src/gen2_api.rs @@ -18,6 +18,10 @@ pub enum Opcode { /// Ephemeral secret operations EphemeralOp = 256, + // ----- below are non-cryptographic opcodes but used to manipulate sensitive state ----- + /// Set bootwait parameters + Bootwait = 4096, + /// Used to map unknown opcodes InvalidCall = 65535, } diff --git a/services/keystore/src/lib.rs b/services/keystore/src/lib.rs index ce5124010..b6d21b325 100644 --- a/services/keystore/src/lib.rs +++ b/services/keystore/src/lib.rs @@ -169,6 +169,29 @@ impl Keystore { #[inline(always)] pub(crate) fn get_dec_backend(&self) -> KeystoreDec<'_> { KeystoreDec(self) } + + /// Returns the previous bootwait state + pub fn bootwait(&self, enable: Option) -> Result { + match xous::send_message( + self.conn, + xous::Message::new_blocking_scalar( + Opcode::Bootwait.to_usize().unwrap(), + if enable.is_some() { 1 } else { 0 }, + if enable.unwrap_or(false) { 1 } else { 0 }, + 0, + 0, + ), + )? { + xous::Result::Scalar5(_, last, _, _, _) => { + if last != 0 { + Ok(true) + } else { + Ok(false) + } + } + _ => unimplemented!(), + } + } } use core::sync::atomic::{AtomicU32, Ordering}; diff --git a/services/keystore/src/platform/baosec/server.rs b/services/keystore/src/platform/baosec/server.rs index 3cd6992da..e5469e8c6 100644 --- a/services/keystore/src/platform/baosec/server.rs +++ b/services/keystore/src/platform/baosec/server.rs @@ -44,6 +44,22 @@ pub fn keystore(sid: SID) -> ! { // this will use the backup_mgr to store ephemeral secrets todo!() } + Opcode::Bootwait => { + if let Some(scalar) = msg.body.scalar_message_mut() { + let is_some = scalar.arg1 != 0; + let enable = scalar.arg2 != 0; + match store.set_bootwait(if is_some { Some(enable) } else { None }) { + Ok(last) => { + if last { + scalar.arg1 = 1; + } else { + scalar.arg1 = 0; + } + } + _ => panic!("Couldn't set bootwait"), + } + } + } Opcode::InvalidCall => { log::error!("Invalid call in keystore: {:?}", opcode); } diff --git a/services/keystore/src/platform/baosec/store.rs b/services/keystore/src/platform/baosec/store.rs index 344d146c6..a5c94f53e 100644 --- a/services/keystore/src/platform/baosec/store.rs +++ b/services/keystore/src/platform/baosec/store.rs @@ -1,7 +1,8 @@ use aes::Aes256; use aes::cipher::{BlockDecrypt, BlockEncrypt, KeyInit, generic_array::GenericArray}; use bao1x_api::{ - BOOT0_PUBKEY_FAIL, BoardTypeCoding, CP_ID, DEVELOPER_MODE, OEM_MODE, SLOT_ELEMENT_LEN_BYTES, UUID, + BOOT0_PUBKEY_FAIL, BoardTypeCoding, BootWaitCoding, CP_ID, DEVELOPER_MODE, OEM_MODE, + SLOT_ELEMENT_LEN_BYTES, UUID, }; use bao1x_hal::board::{BOOKEND_END, BOOKEND_START}; use bao1x_hal::{ @@ -356,4 +357,19 @@ impl KeyStore { }; Ok(()) } + + pub fn set_bootwait(&self, enable: Option) -> Result { + let previous = self.owc.get_decoded::().expect("couldn't fetch flag"); + if let Some(enable) = enable { + while self.owc.get_decoded::().expect("couldn't fetch flag") + != if enable { bao1x_api::BootWaitCoding::Enable } else { bao1x_api::BootWaitCoding::Disable } + { + self.owc.inc_coded::().unwrap(); + } + } + match previous { + BootWaitCoding::Enable => Ok(true), + BootWaitCoding::Disable => Ok(false), + } + } }