Merge pull request #782 from betrusted-io/bootwait

Add bootwait to test console
This commit is contained in:
bunnie 2026-01-16 17:59:17 +08:00 committed by GitHub
commit af5a28ebc0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 291 additions and 212 deletions

1
Cargo.lock generated
View file

@ -363,6 +363,7 @@ dependencies = [
"base45",
"bytemuck",
"chrono",
"keystore",
"log",
"modals",
"num-derive 0.4.2",

Binary file not shown.

View file

@ -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,
}

View file

@ -25,6 +25,7 @@ pddb = { path = "../pddb", optional = true }
xous-swapper = { path = "../xous-swapper", optional = true }
bytemuck = "1.24.0"
susres = { package = "xous-api-susres", version = "0.9.68" }
keystore = { path = "../keystore" }
# pddb testing
rand = { version = "0.8.5", features = ["getrandom"], optional = true }

View file

@ -15,242 +15,260 @@ impl<'a> ShellCmdApi<'a> for Test {
let mut ret = String::new();
#[allow(unused_variables)]
let helpstring = "test [proc] [freemem] [interrupts]; see code for other test commands.";
let helpstring = "test [proc] [freemem] [interrupts] [bootwait]; see code for other test commands.";
#[cfg(feature = "bmp180")]
let helpstring = "Usage:
temp - reads temperature from bmp180.";
let mut tokens = args.split(' ');
let mut parts = args.split_whitespace();
let cmd = parts.next().unwrap_or("").to_string();
let args: Vec<String> = parts.map(|s| s.to_string()).collect();
if let Some(sub_cmd) = tokens.next() {
match sub_cmd {
"timer" => {
let start = _env.ticktimer.elapsed_ms();
log::info!("Starting test");
let mut seconds = 0;
loop {
let elapsed = _env.ticktimer.elapsed_ms() - start;
if elapsed > seconds * 1000 {
log::info!("{} s", seconds);
seconds += 1;
}
match cmd.as_str() {
"bootwait" => {
let keystore = keystore::Keystore::new(&_env.xns);
if args.len() != 1 {
write!(ret, "bootwait [check | enable | disable]").ok();
return Ok(Some(ret));
}
if args[0] == "check" {
write!(ret, "bootwait is {:?}", keystore.bootwait(None).unwrap()).ok();
} else if args[0] == "enable" {
keystore.bootwait(Some(true)).unwrap();
write!(ret, "bootwait enabled").ok();
log::info!(
"{}BOOTWAIT.ENABLED,{}",
bao1x_hal::board::BOOKEND_START,
bao1x_hal::board::BOOKEND_END
);
} else if args[0] == "disable" {
keystore.bootwait(Some(false)).unwrap();
write!(ret, "bootwait disabled").ok();
} else {
write!(ret, "bootwait [check | enable | disable]").ok();
}
}
"timer" => {
let start = _env.ticktimer.elapsed_ms();
log::info!("Starting test");
let mut seconds = 0;
loop {
let elapsed = _env.ticktimer.elapsed_ms() - start;
if elapsed > seconds * 1000 {
log::info!("{} s", seconds);
seconds += 1;
}
}
"time" => {
use chrono::{Local, Utc};
let systime = std::time::SystemTime::now();
let epoch = systime.duration_since(std::time::SystemTime::UNIX_EPOCH).unwrap();
log::info!("Seconds since epoch: {}", epoch.as_secs());
log::info!("Systime: {:?}", systime);
let now = Utc::now();
log::info!("UTC now {}", now.format("%Y-%m-%d %H:%M:%S UTC"));
let local_now = Local::now();
log::info!("Local time: {}", local_now.format("%Y-%m-%d %H:%M:%S %Z"));
}
"time" => {
use chrono::{Local, Utc};
let systime = std::time::SystemTime::now();
let epoch = systime.duration_since(std::time::SystemTime::UNIX_EPOCH).unwrap();
log::info!("Seconds since epoch: {}", epoch.as_secs());
log::info!("Systime: {:?}", systime);
let now = Utc::now();
log::info!("UTC now {}", now.format("%Y-%m-%d %H:%M:%S UTC"));
let local_now = Local::now();
log::info!("Local time: {}", local_now.format("%Y-%m-%d %H:%M:%S %Z"));
log::info!("Waiting 3 seconds");
std::thread::sleep(std::time::Duration::from_secs(3));
log::info!("Waiting 3 seconds");
std::thread::sleep(std::time::Duration::from_secs(3));
let systime = std::time::SystemTime::now();
let epoch = systime.duration_since(std::time::SystemTime::UNIX_EPOCH).unwrap();
log::info!("Seconds since epoch: {}", epoch.as_secs());
log::info!("Systime: {:?}", systime);
let now = Utc::now();
log::info!("UTC now {}", now.format("%Y-%m-%d %H:%M:%S UTC"));
let local_now = Local::now();
log::info!("Local time: {}", local_now.format("%Y-%m-%d %H:%M:%S %Z"));
}
#[cfg(feature = "bmp180")]
"temp" => {
use bao1x_hal::bmp180::Bmp180;
use bao1x_hal::i2c::I2c;
let mut i2c = I2c::new();
let systime = std::time::SystemTime::now();
let epoch = systime.duration_since(std::time::SystemTime::UNIX_EPOCH).unwrap();
log::info!("Seconds since epoch: {}", epoch.as_secs());
log::info!("Systime: {:?}", systime);
let now = Utc::now();
log::info!("UTC now {}", now.format("%Y-%m-%d %H:%M:%S UTC"));
let local_now = Local::now();
log::info!("Local time: {}", local_now.format("%Y-%m-%d %H:%M:%S %Z"));
}
#[cfg(feature = "bmp180")]
"temp" => {
use bao1x_hal::bmp180::Bmp180;
use bao1x_hal::i2c::I2c;
let mut i2c = I2c::new();
match Bmp180::new(&mut i2c) {
Ok(sensor) => match sensor.read_temperature(&mut i2c) {
Ok(temp) => {
write!(ret, "BMP180 Temperature: {:.1}°C", temp).unwrap();
}
Err(e) => {
write!(ret, "Failed to read temperature: {:?}", e).unwrap();
}
},
match Bmp180::new(&mut i2c) {
Ok(sensor) => match sensor.read_temperature(&mut i2c) {
Ok(temp) => {
write!(ret, "BMP180 Temperature: {:.1}°C", temp).unwrap();
}
Err(e) => {
write!(ret, "Failed to initialize BMP180 sensor: {:?}", e).unwrap();
write!(ret, "Failed to read temperature: {:?}", e).unwrap();
}
},
Err(e) => {
write!(ret, "Failed to initialize BMP180 sensor: {:?}", e).unwrap();
}
}
}
#[cfg(not(feature = "hosted-baosec"))]
"shipmode" => {
use bao1x_hal::i2c::I2c;
let mut i2c = I2c::new();
let mut axp2101 = bao1x_hal::axp2101::Axp2101::new(&mut i2c).expect("couldn't get AXP2101");
log::info!("sending shipmode to axp2101 pmic...in four seconds");
let tt = ticktimer::Ticktimer::new().unwrap();
tt.sleep_ms(4000).ok();
// shut down BLDO1 - this should disconnect the battery
axp2101.set_ldo(&mut i2c, None, bao1x_hal::axp2101::WhichLdo::Bldo1).ok();
// shut down PMIC
axp2101.powerdown(&mut i2c).ok();
log::info!("sent shutdown to axp2101");
}
#[cfg(not(feature = "hosted-baosec"))]
"deepsleep" => {
let gfx = ux_api::service::gfx::Gfx::new(&_env.xns).unwrap();
log::info!("turn off display");
gfx.set_power(false).unwrap();
log::info!("display off");
use num_traits::*;
// monkey patch over the standard xous IP
let conn = _env
.xns
.request_connection_blocking(susres::api::SERVER_NAME_SUSRES)
.expect("Can't connect to SUSRES");
match xous::send_message(
conn,
xous::Message::new_blocking_scalar(
susres::api::Opcode::SuspendRequest.to_usize().unwrap(),
1, // this is a monkeypatch
0,
0,
0,
),
) {
Ok(xous::Result::Scalar1(result)) => {
if result == 1 {
log::info!("Should be in deep sleep!");
} else {
log::error!("Couldn't initiate deep sleep")
}
}
_ => panic!("Couldn't send deep sleep message to susres"),
}
#[cfg(not(feature = "hosted-baosec"))]
"shipmode" => {
use bao1x_hal::i2c::I2c;
let mut i2c = I2c::new();
}
"seed" => {
let (_, value) = std::env::vars().find(|(key, _value)| key == "SEED").unwrap();
log::info!("Seed: {:?}", value);
}
#[cfg(not(feature = "hosted-baosec"))]
"wfi" => {
let gfx = ux_api::service::gfx::Gfx::new(&_env.xns).unwrap();
log::info!("turn off display");
gfx.set_power(false).unwrap();
log::info!("display off");
let susres = susres::Susres::new_without_hook(&_env.xns).unwrap();
log::info!("initiating wfi from test shell...");
susres.initiate_suspend().unwrap();
log::info!("waiting after WFI return (system will be in WFI)");
_env.ticktimer.sleep_ms(100).ok();
log::info!("turn on display");
gfx.set_power(true).unwrap();
}
"proc" => {
// hard coded - debug feature - if the platform ABI changes its name or opcode map this
// can break, but also this routine is not meant for public
// consumption and coding it here avoids breaking dependencies to the Xous API crate.
let page_buf = xous::PageBuf::new();
xous::rsyscall(xous::SysCall::PlatformSpecific(2, page_buf.as_ptr(), 0, 0, 0, 0, 0)).unwrap();
let mut axp2101 =
bao1x_hal::axp2101::Axp2101::new(&mut i2c).expect("couldn't get AXP2101");
log::info!("sending shipmode to axp2101 pmic...in four seconds");
let tt = ticktimer::Ticktimer::new().unwrap();
tt.sleep_ms(4000).ok();
// shut down BLDO1 - this should disconnect the battery
axp2101.set_ldo(&mut i2c, None, bao1x_hal::axp2101::WhichLdo::Bldo1).ok();
// shut down PMIC
axp2101.powerdown(&mut i2c).ok();
log::info!("sent shutdown to axp2101");
log::info!("Process listing:");
for line in page_buf.as_str().lines() {
log::info!("{}", line);
}
#[cfg(not(feature = "hosted-baosec"))]
"deepsleep" => {
let gfx = ux_api::service::gfx::Gfx::new(&_env.xns).unwrap();
log::info!("turn off display");
gfx.set_power(false).unwrap();
log::info!("display off");
use num_traits::*;
// monkey patch over the standard xous IP
let conn = _env
.xns
.request_connection_blocking(susres::api::SERVER_NAME_SUSRES)
.expect("Can't connect to SUSRES");
match xous::send_message(
conn,
xous::Message::new_blocking_scalar(
susres::api::Opcode::SuspendRequest.to_usize().unwrap(),
1, // this is a monkeypatch
0,
0,
0,
),
) {
Ok(xous::Result::Scalar1(result)) => {
if result == 1 {
log::info!("Should be in deep sleep!");
} else {
log::error!("Couldn't initiate deep sleep")
}
}
"freemem" => {
// hard coded - debug feature - if the platform ABI changes its name or opcode map this
// can break, but also this routine is not meant for public
// consumption and coding it here avoids breaking dependencies to the Xous API crate.
let page_buf = xous::PageBuf::new();
xous::rsyscall(xous::SysCall::PlatformSpecific(1, page_buf.as_ptr(), 0, 0, 0, 0, 0)).unwrap();
log::info!("RAM usage:");
for line in page_buf.as_str().lines() {
log::info!("{}", line);
}
}
"interrupts" => {
// hard coded - debug feature - if the platform ABI changes its name or opcode map this
// can break, but also this routine is not meant for public
// consumption and coding it here avoids breaking dependencies to the Xous API crate.
let page_buf = xous::PageBuf::new();
xous::rsyscall(xous::SysCall::PlatformSpecific(3, page_buf.as_ptr(), 0, 0, 0, 0, 0)).unwrap();
log::info!("Interrupt handlers:");
for line in page_buf.as_str().lines() {
log::info!("{}", line);
}
}
#[cfg(feature = "board-baosec")]
"qrshow" => {
// note that 40 bytes gives 320 bits which fits nicely into a version 3 code,
// which allows 4 pixels per module rendering.
// if we have to move to 3 pixels per module, the next code up that optimally
// uses the full screen is version 6. This would give 102 bytes of transfer
// in a single scan.
//
// The equation for capacity is:
// `binary_bytes = floor((alphanumeric_capacity / 3) × 2)`
//
// where `alphameric_capacity` is the capacity of the QR code version
// per spec lookup table.
let modals = modals::Modals::new(&_env.xns).unwrap();
let mut test_data = [0u8; 40];
for (i, d) in test_data.iter_mut().enumerate() {
*d = i as u8;
}
let encoded = base45::encode(&test_data);
modals.show_notification("", Some(&encoded)).ok();
}
#[cfg(feature = "board-baosec")]
"qrget" => {
let gfx = ux_api::service::gfx::Gfx::new(&_env.xns).unwrap();
match gfx.acquire_qr() {
Ok(qr_data) => {
if let Some(meta) = qr_data.meta {
log::info!("QR code metadata: {}", meta);
}
_ => panic!("Couldn't send deep sleep message to susres"),
}
}
"seed" => {
let (_, value) = std::env::vars().find(|(key, _value)| key == "SEED").unwrap();
log::info!("Seed: {:?}", value);
}
#[cfg(not(feature = "hosted-baosec"))]
"wfi" => {
let gfx = ux_api::service::gfx::Gfx::new(&_env.xns).unwrap();
log::info!("turn off display");
gfx.set_power(false).unwrap();
log::info!("display off");
let susres = susres::Susres::new_without_hook(&_env.xns).unwrap();
log::info!("initiating wfi from test shell...");
susres.initiate_suspend().unwrap();
log::info!("waiting after WFI return (system will be in WFI)");
_env.ticktimer.sleep_ms(100).ok();
log::info!("turn on display");
gfx.set_power(true).unwrap();
}
"proc" => {
// hard coded - debug feature - if the platform ABI changes its name or opcode map this
// can break, but also this routine is not meant for public
// consumption and coding it here avoids breaking dependencies to the Xous API crate.
let page_buf = xous::PageBuf::new();
xous::rsyscall(xous::SysCall::PlatformSpecific(2, page_buf.as_ptr(), 0, 0, 0, 0, 0))
.unwrap();
log::info!("Process listing:");
for line in page_buf.as_str().lines() {
log::info!("{}", line);
}
}
"freemem" => {
// hard coded - debug feature - if the platform ABI changes its name or opcode map this
// can break, but also this routine is not meant for public
// consumption and coding it here avoids breaking dependencies to the Xous API crate.
let page_buf = xous::PageBuf::new();
xous::rsyscall(xous::SysCall::PlatformSpecific(1, page_buf.as_ptr(), 0, 0, 0, 0, 0))
.unwrap();
log::info!("RAM usage:");
for line in page_buf.as_str().lines() {
log::info!("{}", line);
}
}
"interrupts" => {
// hard coded - debug feature - if the platform ABI changes its name or opcode map this
// can break, but also this routine is not meant for public
// consumption and coding it here avoids breaking dependencies to the Xous API crate.
let page_buf = xous::PageBuf::new();
xous::rsyscall(xous::SysCall::PlatformSpecific(3, page_buf.as_ptr(), 0, 0, 0, 0, 0))
.unwrap();
log::info!("Interrupt handlers:");
for line in page_buf.as_str().lines() {
log::info!("{}", line);
}
}
#[cfg(feature = "board-baosec")]
"qrshow" => {
// note that 40 bytes gives 320 bits which fits nicely into a version 3 code,
// which allows 4 pixels per module rendering.
// if we have to move to 3 pixels per module, the next code up that optimally
// uses the full screen is version 6. This would give 102 bytes of transfer
// in a single scan.
//
// The equation for capacity is:
// `binary_bytes = floor((alphanumeric_capacity / 3) × 2)`
//
// where `alphameric_capacity` is the capacity of the QR code version
// per spec lookup table.
let modals = modals::Modals::new(&_env.xns).unwrap();
let mut test_data = [0u8; 40];
for (i, d) in test_data.iter_mut().enumerate() {
*d = i as u8;
}
let encoded = base45::encode(&test_data);
modals.show_notification("", Some(&encoded)).ok();
}
#[cfg(feature = "board-baosec")]
"qrget" => {
let gfx = ux_api::service::gfx::Gfx::new(&_env.xns).unwrap();
match gfx.acquire_qr() {
Ok(qr_data) => {
if let Some(meta) = qr_data.meta {
log::info!("QR code metadata: {}", meta);
}
if let Some(coded) = qr_data.content {
match base45::decode(&coded) {
Ok(data) => {
log::info!("Recovered: {:x?}", data);
}
Err(e) => {
log::info!("Base45 decode err: {:?}", e);
}
if let Some(coded) = qr_data.content {
match base45::decode(&coded) {
Ok(data) => {
log::info!("Recovered: {:x?}", data);
}
Err(e) => {
log::info!("Base45 decode err: {:?}", e);
}
}
}
Err(e) => {
log::info!("QR error: {:?}", e);
}
}
}
/* // leave this around in case we have more swap bugs to debug
// needs to have `swaptest1`` & `swaptest2` added to the image for this to work.
"swap" => {
log::info!("starting swap test");
let swaptest1 = _env.xns.request_connection("swaptest1").unwrap();
let swaptest2 = _env.xns.request_connection("swaptest2").unwrap();
for i in 1..4 {
log::info!("iter {}", i);
xous::send_message(swaptest1, xous::Message::new_scalar(0, i, 0, 0, 0)).unwrap();
xous::send_message(swaptest2, xous::Message::new_scalar(0, i + 1, 0, 0, 0)).unwrap();
Err(e) => {
log::info!("QR error: {:?}", e);
}
log::info!("swaptest done");
}
*/
_ => {
write!(ret, "{}", helpstring).unwrap();
}
}
} else {
write!(ret, "{}", helpstring).unwrap();
/* // leave this around in case we have more swap bugs to debug
// needs to have `swaptest1`` & `swaptest2` added to the image for this to work.
"swap" => {
log::info!("starting swap test");
let swaptest1 = _env.xns.request_connection("swaptest1").unwrap();
let swaptest2 = _env.xns.request_connection("swaptest2").unwrap();
for i in 1..4 {
log::info!("iter {}", i);
xous::send_message(swaptest1, xous::Message::new_scalar(0, i, 0, 0, 0)).unwrap();
xous::send_message(swaptest2, xous::Message::new_scalar(0, i + 1, 0, 0, 0)).unwrap();
}
log::info!("swaptest done");
}
*/
_ => {
write!(ret, "{}", helpstring).unwrap();
}
}
Ok(Some(ret))
}
}

View file

@ -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<bool>) -> Result<bool, xous::Error> {
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};

View file

@ -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);
}

View file

@ -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<bool>) -> Result<bool, xous::Error> {
let previous = self.owc.get_decoded::<bao1x_api::BootWaitCoding>().expect("couldn't fetch flag");
if let Some(enable) = enable {
while self.owc.get_decoded::<bao1x_api::BootWaitCoding>().expect("couldn't fetch flag")
!= if enable { bao1x_api::BootWaitCoding::Enable } else { bao1x_api::BootWaitCoding::Disable }
{
self.owc.inc_coded::<bao1x_api::BootWaitCoding>().unwrap();
}
}
match previous {
BootWaitCoding::Enable => Ok(true),
BootWaitCoding::Disable => Ok(false),
}
}
}

View file

@ -136,7 +136,7 @@ pub fn keystore(sid: SID) -> ! {
Opcode::EphemeralOp => {
todo!()
}
Opcode::InvalidCall => {
_ => {
log::error!("Invalid call in keystore: {:?}", opcode);
}
}