use crate::bootrom::{self, BootRomError};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
Unsupported,
Rom(BootRomError),
}
impl From<BootRomError> for Error {
fn from(value: BootRomError) -> Self {
Self::Rom(value)
}
}
const FLAG_CHIP_INFO: u32 = 0x0001;
const FLAG_CRITICAL: u32 = 0x0002;
const FLAG_CPU_INFO: u32 = 0x0004;
const FLAG_FLASH_DEVICE_INFO: u32 = 0x0008;
const FLAG_BOOT_RANDOM: u32 = 0x0010;
const FLAG_BOOT_INFO: u32 = 0x0040;
fn block(flag: u32, out: &mut [u32]) -> Result<&[u32], Error> {
let words = bootrom::get_sys_info(out, flag);
if words < 0 {
return Err(Error::Rom(BootRomError::from(words)));
}
if words == 0 || out[0] & flag == 0 {
return Err(Error::Unsupported);
}
let end = (words as usize).min(out.len());
Ok(&out[1..end])
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ChipInfo {
package_sel: u32,
device_id: u32,
wafer_id: u32,
}
impl ChipInfo {
pub fn package_sel(&self) -> u32 {
self.package_sel
}
pub fn device_id(&self) -> u32 {
self.device_id
}
pub fn wafer_id(&self) -> u32 {
self.wafer_id
}
}
pub fn chip_info() -> Result<ChipInfo, Error> {
let mut words = [0u32; 4];
let info = block(FLAG_CHIP_INFO, &mut words)?;
if info.len() < 3 {
return Err(Error::Unsupported);
}
Ok(ChipInfo {
package_sel: info[0],
device_id: info[1],
wafer_id: info[2],
})
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum CpuArch {
Arm,
RiscV,
}
impl From<u32> for CpuArch {
fn from(value: u32) -> Self {
if value == 0 { Self::Arm } else { Self::RiscV }
}
}
pub fn cpu_arch() -> Result<CpuArch, Error> {
let mut words = [0u32; 2];
let payload = block(FLAG_CPU_INFO, &mut words)?;
payload
.first()
.copied()
.map(CpuArch::from)
.ok_or(Error::Unsupported)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct CriticalRegister(u32);
impl CriticalRegister {
pub fn raw(&self) -> u32 {
self.0
}
pub fn secure_boot_enabled(&self) -> bool {
self.0 & 0x1 != 0
}
pub fn secure_debug_disabled(&self) -> bool {
self.0 & 0x2 != 0
}
pub fn debug_disabled(&self) -> bool {
self.0 & 0x4 != 0
}
pub fn default_arch(&self) -> CpuArch {
CpuArch::from((self.0 >> 3) & 1)
}
pub fn glitch_detector_enabled(&self) -> bool {
self.0 & 0x10 != 0
}
pub fn glitch_detector_sensitivity(&self) -> u8 {
((self.0 >> 5) & 0x3) as u8
}
pub fn arm_disabled(&self) -> bool {
self.0 & 0x1_0000 != 0
}
pub fn risc_disabled(&self) -> bool {
self.0 & 0x2_0000 != 0
}
}
pub fn critical_register() -> Result<CriticalRegister, Error> {
let mut words = [0u32; 2];
let payload = block(FLAG_CRITICAL, &mut words)?;
payload
.first()
.copied()
.map(CriticalRegister)
.ok_or(Error::Unsupported)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct FlashDeviceInfo(u32);
impl FlashDeviceInfo {
pub fn raw(&self) -> u16 {
self.0 as u16
}
pub fn cs0_size_bytes(&self) -> Option<u32> {
device_size((self.0 >> 8) & 0xf)
}
pub fn cs1_size_bytes(&self) -> Option<u32> {
device_size((self.0 >> 12) & 0xf)
}
pub fn cs1_gpio(&self) -> Option<u8> {
self.cs1_size_bytes()?;
Some((self.0 & 0x3f) as u8)
}
pub fn d8h_erase_supported(&self) -> bool {
self.0 & 0x80 != 0
}
}
fn device_size(code: u32) -> Option<u32> {
if code == 0 || code > 0xc {
return None;
}
Some((4 * 1024) << code)
}
pub fn flash_device_info() -> Result<FlashDeviceInfo, Error> {
let mut words = [0u32; 2];
let payload = block(FLAG_FLASH_DEVICE_INFO, &mut words)?;
payload
.first()
.copied()
.map(FlashDeviceInfo)
.ok_or(Error::Unsupported)
}
pub fn boot_random() -> Result<u128, Error> {
let mut words = [0u32; 5];
let payload = block(FLAG_BOOT_RANDOM, &mut words)?;
if payload.len() < 4 {
return Err(Error::Unsupported);
}
let mut value = 0u128;
for word in &payload[..4] {
value = (value << 32) | *word as u128;
}
Ok(value)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum DiagnosticPartition {
Partition(u8),
None,
Slot0,
Slot1,
Window,
Unknown,
}
impl From<i8> for DiagnosticPartition {
fn from(value: i8) -> Self {
match value {
-1 => Self::None,
-2 => Self::Slot0,
-3 => Self::Slot1,
-4 => Self::Window,
0..=15 => Self::Partition(value as u8),
_ => Self::Unknown,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum BootType {
Normal,
BootSel,
RamImage,
FlashUpdate,
PcSp,
Unknown,
}
impl From<u8> for BootType {
fn from(value: u8) -> Self {
match value {
0 => Self::Normal,
2 => Self::BootSel,
3 => Self::RamImage,
4 => Self::FlashUpdate,
0xd => Self::PcSp,
_ => Self::Unknown,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u16)]
pub enum DiagnosticFlag {
RegionSearched = 0x0001,
InvalidBlockLoop = 0x0002,
ValidBlockLoop = 0x0004,
ValidImageDef = 0x0008,
HasPartitionTable = 0x0010,
Considered = 0x0020,
Chosen = 0x0040,
PartitionTableMatchingKeyForVerify = 0x0080,
PartitionTableHashForVerify = 0x0100,
PartitionTableVerifiedOk = 0x0200,
ImageDefMatchingKeyForVerify = 0x0400,
ImageDefHashForVerify = 0x0800,
ImageDefVerifiedOk = 0x1000,
LoadMapEntriesLoaded = 0x2000,
ImageLaunched = 0x4000,
ImageConditionFailure = 0x8000,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct BootInfo {
boot_type: BootType,
chained: bool,
partition: i8,
diagnostic_partition: DiagnosticPartition,
tbyb_update: u8,
diagnostic: u32,
reboot_params: [u32; 2],
}
impl BootInfo {
pub fn boot_type(&self) -> BootType {
self.boot_type
}
pub fn chained(&self) -> bool {
self.chained
}
pub fn partition(&self) -> i8 {
self.partition
}
pub fn diagnostic_partition(&self) -> DiagnosticPartition {
self.diagnostic_partition
}
pub fn tbyb_update(&self) -> u8 {
self.tbyb_update
}
pub fn diagnostic_raw(&self) -> u32 {
self.diagnostic
}
pub fn reboot_params(&self) -> [u32; 2] {
self.reboot_params
}
pub fn slot0(&self, flag: DiagnosticFlag) -> bool {
self.diagnostic as u16 & flag as u16 != 0
}
pub fn slot1(&self, flag: DiagnosticFlag) -> bool {
(self.diagnostic >> 16) as u16 & flag as u16 != 0
}
}
pub fn boot_info() -> Result<BootInfo, Error> {
let mut words = [0u32; 5];
let payload = block(FLAG_BOOT_INFO, &mut words)?;
if payload.len() < 4 {
return Err(Error::Unsupported);
}
let boot_word = payload[0];
let boot_byte = (boot_word >> 8) as u8;
Ok(BootInfo {
boot_type: BootType::from(boot_byte & 0x7f),
chained: boot_byte & 0x80 != 0,
partition: (boot_word >> 16) as u8 as i8,
diagnostic_partition: DiagnosticPartition::from((boot_word & 0xff) as i8),
tbyb_update: (boot_word >> 24) as u8,
diagnostic: payload[1],
reboot_params: [payload[2], payload[3]],
})
}