diff --git a/bao1x-boot/boot0/src/platform/bao1x/bao1x.rs b/bao1x-boot/boot0/src/platform/bao1x/bao1x.rs index 15944de51..bef13a2a3 100644 --- a/bao1x-boot/boot0/src/platform/bao1x/bao1x.rs +++ b/bao1x-boot/boot0/src/platform/bao1x/bao1x.rs @@ -9,10 +9,8 @@ use utralib::utra; use utralib::utra::sysctrl; #[cfg(feature = "unsafe-dev")] -use crate::platform::{ - debug::setup_rx, - irq::{enable_irq, irq_setup}, -}; +use crate::platform::debug::setup_rx; +use crate::platform::irq::{enable_irq, irq_setup}; #[global_allocator] static ALLOCATOR: linked_list_allocator::LockedHeap = linked_list_allocator::LockedHeap::empty(); @@ -149,6 +147,31 @@ pub fn early_init() -> Csprng { let mut csprng = Csprng::new(); csprng.random_delay(); + irq_setup(); + // set up the reactive sensors + bao1x_api::bollard!(4); + bao1x_hal::hardening::reset_sensors(); + bao1x_api::bollard!(4); + bollard!(die, 4); + csprng.random_delay(); + // ensure paranoid mode is respected + if paranoid2 != 0 { + bollard!(die, 4); + paranoid_mode(); + } + // set up IRQ to respond to all possible sensor errors + let mut irq13 = CSR::new(utra::irqarray13::HW_IRQARRAY13_BASE as *mut u32); + irq13.wo(utra::irqarray13::EV_EDGE_TRIGGERED, 0xFFFF_FFFF); + irq13.wo(utra::irqarray13::EV_POLARITY, 0xFFFF_FFFF); + irq13.wo(utra::irqarray13::EV_PENDING, 0xFFFF_FFFF); + irq13.wo(utra::irqarray13::EV_ENABLE, 0xFFFF_FFFF); + enable_irq(utra::irqarray13::IRQARRAY13_IRQ); + csprng.random_delay(); + if paranoid1 != 0 { + bollard!(die, 4); + paranoid_mode(); + } + // setup heap alloc // glitch_safety: failing to initialize this will generally cause heap allocs to hang setup_alloc(); @@ -193,7 +216,6 @@ pub fn early_init() -> Csprng { #[cfg(feature = "unsafe-dev")] let mut udma_uart = { let mut udma_uart = setup_rx(perclk); - irq_setup(); enable_irq(utra::irqarray5::IRQARRAY5_IRQ); udma_uart }; diff --git a/bao1x-boot/boot0/src/platform/bao1x/irq.rs b/bao1x-boot/boot0/src/platform/bao1x/irq.rs index 2e2af1706..76f2a284e 100644 --- a/bao1x-boot/boot0/src/platform/bao1x/irq.rs +++ b/bao1x-boot/boot0/src/platform/bao1x/irq.rs @@ -1,13 +1,12 @@ -#[cfg(feature = "unsafe-dev")] use riscv::register::mstatus; use riscv::register::{mcause, mie}; -#[cfg(feature = "unsafe-dev")] use vexriscv::register::vexriscv::mim; use vexriscv::register::vexriscv::mip; use crate::*; -#[cfg(feature = "unsafe-dev")] +static ATTACKS_SINCE_BOOT: core::sync::atomic::AtomicU32 = core::sync::atomic::AtomicU32::new(0); + pub fn irq_setup() { unsafe { #[rustfmt::skip] @@ -28,7 +27,6 @@ pub fn irq_setup() { unsafe { mie::set_mext() }; } -#[cfg(feature = "unsafe-dev")] pub fn enable_irq(irq_no: usize) { // Note that the vexriscv "IRQ Mask" register is inverse-logic -- // that is, setting a bit in the "mask" register unmasks (i.e. enables) it. @@ -195,12 +193,17 @@ pub extern "C" fn trap_handler( } else if (irqs_pending & (1 << utra::irqarray5::IRQARRAY5_IRQ)) != 0 { #[cfg(feature = "unsafe-dev")] crate::uart_irq_handler(); - } else if (irqs_pending & (1 << utra::irqarray2::IRQARRAY2_IRQ)) != 0 { - // just clear it for now - let mut irqarray2 = CSR::new(utralib::HW_IRQARRAY2_BASE as *mut u32); - let pending = irqarray2.r(utra::irqarray2::EV_PENDING); - crate::println!("keypress interrupt {:x}", pending); - irqarray2.wo(utra::irqarray2::EV_PENDING, pending); + } else if (irqs_pending & (1 << utra::irqarray13::IRQARRAY13_IRQ)) != 0 { + // irq13 -> {aoramerr, secirq, ifsuberr, sceerr, coresuberr} + // this IRQ will trigger an attack response if any of the sensors trigger *or* + // if the ECC-protected memory banks trigger an ECC error + bao1x_hal::hardening::glitch_handler( + ATTACKS_SINCE_BOOT.fetch_add(1, core::sync::atomic::Ordering::SeqCst), + ); + } else if (irqs_pending & (1 << utra::irqarray15::IRQARRAY15_IRQ)) != 0 { + // irq15 -> {meshirq, sensorcirq, glcirq} + // those one is kind of redundant with irq13 in this scenario, because secirq is the OR + // of all three IRQs going into this array. So, we handle this simply in irqarray13. } } diff --git a/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs b/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs index aeb0d2c1c..998cac84c 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs @@ -148,6 +148,16 @@ pub fn early_init(mut board_type: bao1x_api::BoardTypeCoding) -> (bao1x_api::Boa crate::println!("backup region is clean!"); } + irq_setup(); + // all sensors & IRQs setup already by boot0, but it doesn't hurt to re-write these registers + // in case they were defeated somehow previously + let mut irq13 = CSR::new(utra::irqarray13::HW_IRQARRAY13_BASE as *mut u32); + irq13.wo(utra::irqarray13::EV_EDGE_TRIGGERED, 0xFFFF_FFFF); + irq13.wo(utra::irqarray13::EV_POLARITY, 0xFFFF_FFFF); + irq13.wo(utra::irqarray13::EV_ENABLE, 0xFFFF_FFFF); + // enable the IRQ because it was disabled by the previous stage's exit + enable_irq(utra::irqarray13::IRQARRAY13_IRQ); + let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32); // setup board-specific I/Os - early boot set. These are items that have to be @@ -343,7 +353,6 @@ pub fn early_init(mut board_type: bao1x_api::BoardTypeCoding) -> (bao1x_api::Boa // Rx setup let _udma_uart = setup_console(&board_type, &iox, perclk); - irq_setup(); enable_irq(utra::irqarray5::IRQARRAY5_IRQ); crate::debug::USE_CONSOLE.store(true, core::sync::atomic::Ordering::SeqCst); diff --git a/bao1x-boot/boot1/src/platform/bao1x/irq.rs b/bao1x-boot/boot1/src/platform/bao1x/irq.rs index 728b75b47..38092a06f 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/irq.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/irq.rs @@ -6,6 +6,8 @@ use vexriscv::register::vexriscv::{mim, mip}; use crate::usb::USB; use crate::*; +static ATTACKS_SINCE_BOOT: core::sync::atomic::AtomicU32 = core::sync::atomic::AtomicU32::new(0); + pub fn irq_setup() { unsafe { #[rustfmt::skip] @@ -212,12 +214,17 @@ pub extern "C" fn trap_handler( timer0.wfo(utra::timer0::RELOAD_RELOAD, (DEFAULT_FCLK_FREQUENCY / 1_000) * ms); } else if (irqs_pending & (1 << utra::irqarray5::IRQARRAY5_IRQ)) != 0 { crate::uart_irq_handler(); - } else if (irqs_pending & (1 << utra::irqarray2::IRQARRAY2_IRQ)) != 0 { - // just clear it for now - let mut irqarray2 = CSR::new(utralib::HW_IRQARRAY2_BASE as *mut u32); - let pending = irqarray2.r(utra::irqarray2::EV_PENDING); - crate::println!("keypress interrupt {:x}", pending); - irqarray2.wo(utra::irqarray2::EV_PENDING, pending); + } else if (irqs_pending & (1 << utra::irqarray13::IRQARRAY13_IRQ)) != 0 { + // irq13 -> {aoramerr, secirq, ifsuberr, sceerr, coresuberr} + // this IRQ will trigger an attack response if any of the sensors trigger *or* + // if the ECC-protected memory banks trigger an ECC error + bao1x_hal::hardening::glitch_handler( + ATTACKS_SINCE_BOOT.fetch_add(1, core::sync::atomic::Ordering::SeqCst), + ); + } else if (irqs_pending & (1 << utra::irqarray15::IRQARRAY15_IRQ)) != 0 { + // irq15 -> {meshirq, sensorcirq, glcirq} + // those one is kind of redundant with irq13 in this scenario, because secirq is the OR + // of all three IRQs going into this array. So, we handle this simply in irqarray13. } if (irqs_pending & (1 << utralib::utra::irqarray1::IRQARRAY1_IRQ)) != 0 { // handle USB interrupt