From 7ec9457854749823d34016361c72ff49f6b43a44 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 30 Sep 2025 00:38:19 +0800 Subject: [PATCH] cleanup the boot-wait code, and add autoboot toggle command --- bao1x-boot/boot1/src/main.rs | 37 ++++++++++++++++++++++++--------- bao1x-boot/boot1/src/repl.rs | 16 +++++++++++++- bao1x-boot/boot1/src/secboot.rs | 37 +-------------------------------- 3 files changed, 43 insertions(+), 47 deletions(-) diff --git a/bao1x-boot/boot1/src/main.rs b/bao1x-boot/boot1/src/main.rs index cd0ef8a74..8b8000803 100644 --- a/bao1x-boot/boot1/src/main.rs +++ b/bao1x-boot/boot1/src/main.rs @@ -12,7 +12,7 @@ mod uf2; use alloc::collections::VecDeque; use core::{cell::RefCell, sync::atomic::Ordering}; -use bao1x_api::BoardTypeCoding; +use bao1x_api::{BoardTypeCoding, BootWaitCoding}; use bao1x_hal::{board::KeyPress, iox::Iox, usb::driver::UsbDeviceState}; use critical_section::Mutex; use platform::*; @@ -21,6 +21,7 @@ use utralib::*; use crate::delay; use crate::platform::usb::glue; +use crate::secboot::boot_or_die; static UART_RX: Mutex>> = Mutex::new(RefCell::new(VecDeque::new())); #[allow(dead_code)] @@ -59,19 +60,35 @@ pub unsafe extern "C" fn rust_entry() -> ! { crate::println!("\n~~Boot1 up!~~\n"); let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32); - // this diverges if the conditions to boot the next stage are met, or if - // the conditions fail and the device is in a secured state. It returns - // and allows the repl loop below to run if an update is authorized. - let mut current_key = secboot::try_boot(&board_type, &iox); - // "flash" SE0 for 1 second so as to emulate a clean re-plug event, allowing the - // stack to enumerate from a coherent state. Up until now, any attempt of the - // host to talk to us just gave us nonsense - if current_key.is_none() { - crate::println!("No valid boot image found; enabling USB..."); + let mut current_key = if let Some(key) = crate::platform::get_key(&board_type, &iox) { + // TODO: on baosec v2, we should not get Invalid keys. However, as we wait for the new + // boards to come in this will be a thing. + if key != KeyPress::Invalid { + // skip boot if a key is pressed; record what key it is so we know to check that it has + // become *unpressed* before looking for a new press + Some(key) + } else { + None + } } else { + None + }; + + let one_way = bao1x_hal::acram::OneWayCounter::new(); + let boot_wait = one_way.get_decoded::().expect("internal error"); + + if boot_wait == BootWaitCoding::Disable && current_key.is_none() { + // this should diverge, rest of code is not run + boot_or_die(); + } + + if boot_wait == BootWaitCoding::Enable { + crate::println!("Boot bypassed because autoboot was disabled"); + } else if current_key.is_some() { crate::println!("Boot bypassed with keypress: {:?}", current_key); } + let (se0_port, se0_pin) = match board_type { BoardTypeCoding::Baosec => bao1x_hal::board::setup_usb_pins(&iox), _ => crate::platform::setup_dabao_se0_pin(&iox), diff --git a/bao1x-boot/boot1/src/repl.rs b/bao1x-boot/boot1/src/repl.rs index 302ec53ba..f02dce11b 100644 --- a/bao1x-boot/boot1/src/repl.rs +++ b/bao1x-boot/boot1/src/repl.rs @@ -5,6 +5,7 @@ use alloc::vec::Vec; #[allow(unused_imports)] use bao1x_api::*; +use bao1x_hal::acram::OneWayCounter; use utralib::*; pub struct Error { @@ -122,6 +123,19 @@ impl Repl { self.local_echo = false; } } + "autoboot" => { + // this toggles the autoboot flag by incrementing the one-way counter associated with + // autobooting. + let one_way = OneWayCounter::new(); + match one_way.inc_coded::() { + Ok(_) => { + let autoboot_state = + one_way.get_decoded::().expect("couldn't fetch flag"); + crate::println!("autoboot is now set to {:?}", autoboot_state); + } + Err(e) => crate::println!("Couldn't toggle autoboot: {:?}", e), + } + } #[cfg(feature = "unsafe-debug")] "peek" => { const COLUMNS: usize = 4; @@ -160,7 +174,7 @@ impl Repl { } _ => { crate::println!("Command not recognized: {}", cmd); - crate::print!("Commands include: reset, echo, boot"); + crate::print!("Commands include: reset, echo, boot, autoboot, localecho, uf2"); crate::println!(""); } } diff --git a/bao1x-boot/boot1/src/secboot.rs b/bao1x-boot/boot1/src/secboot.rs index 1cf2569d6..13e11f3b4 100644 --- a/bao1x-boot/boot1/src/secboot.rs +++ b/bao1x-boot/boot1/src/secboot.rs @@ -1,5 +1,4 @@ -use bao1x_api::{signatures::FunctionCode, *}; -use bao1x_hal::board::KeyPress; +use bao1x_api::signatures::FunctionCode; const ALLOWED_FUNCTIONS: [u32; 5] = [ FunctionCode::Baremetal as u32, @@ -8,40 +7,6 @@ const ALLOWED_FUNCTIONS: [u32; 5] = [ FunctionCode::UpdatedLoader as u32, FunctionCode::Developer as u32, ]; -pub fn try_boot(board_type: &BoardTypeCoding, iox: &T) -> Option { - if let Some(key) = crate::platform::get_key(board_type, iox) { - // TODO: on baosec v2, we should not get Invalid keys. However, as we wait for the new - // boards to come in this will be a thing. - if key != KeyPress::Invalid { - // skip boot if a key is pressed; record what key it is so we know to check that it has - // become *unpressed* before looking for a new press - return Some(key); - } - } - - let one_way = bao1x_hal::acram::OneWayCounter::new(); - if one_way.get_decoded::().expect("internal error") == BootWaitCoding::Enable { - // enter repl, but indicate that no key was pressed. - return None; - } - - // do the secure signature stuff now - seal_boot1_keys(); - // loader is at the same offset as baremetal. Accept either as valid boot. - // This diverges if the signature check is successful - match bao1x_hal::sigcheck::validate_image( - bao1x_api::LOADER_START as *const u32, - bao1x_api::BOOT1_START as *const u32, - bao1x_api::BOOT1_REVOCATION_OFFSET, - &ALLOWED_FUNCTIONS, - true, - ) { - Ok(k) => crate::println!("**should be unreachable** Booted with key {}", k), - Err(e) => crate::println!("Image did not validate: {:?}", e), - } - crate::println!("No valid loader or baremetal image found. Halting!"); - bao1x_hal::sigcheck::die_no_std(); -} fn seal_boot1_keys() { // TODO: