use crate::rom_data::otp_access;
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
InvalidIndex,
InvalidPermissions,
UnsupportedModification,
Overflow,
UnexpectedFailure(i32),
}
pub const OTP_DATA_BASE: *const u32 = 0x4013_0000 as *const u32;
pub const OTP_DATA_RAW_BASE: *const u32 = 0x4013_4000 as *const u32;
pub const NUM_PAGES: usize = 64;
pub const NUM_ROWS_PER_PAGE: usize = 64;
pub const NUM_ROWS: usize = NUM_PAGES * NUM_ROWS_PER_PAGE;
pub const RAW_WRITE_BIT_MASK: u32 = 0x00FF_FFFF;
pub fn read_ecc_word(row: usize) -> Result<u16, Error> {
if row >= NUM_ROWS {
return Err(Error::InvalidIndex);
}
let _ = read_raw_word(row)?;
let offset = row >> 1;
let value = unsafe { OTP_DATA_BASE.add(offset).read() };
if (row & 1) == 0 {
Ok(value as u16)
} else {
Ok((value >> 16) as u16)
}
}
pub fn read_raw_word(row: usize) -> Result<u32, Error> {
if row >= NUM_ROWS {
return Err(Error::InvalidIndex);
}
let value = unsafe { OTP_DATA_RAW_BASE.add(row).read() };
if value == 0xFFFF_FFFF {
Err(Error::InvalidPermissions)
} else {
Ok(value)
}
}
pub fn write_raw_word(row: usize, data: u32) -> Result<(), Error> {
if data > RAW_WRITE_BIT_MASK {
return Err(Error::Overflow);
}
if row >= NUM_ROWS {
return Err(Error::InvalidIndex);
}
let row_with_write_bit = row | 0x00010000;
let result = unsafe { otp_access(data.to_le_bytes().as_mut_ptr(), 4, row_with_write_bit as u32) };
if result == 0 {
Ok(())
} else {
let error = match result {
-4 => Error::InvalidPermissions,
-18 => Error::UnsupportedModification,
_ => Error::UnexpectedFailure(result),
};
Err(error)
}
}
pub fn write_ecc_word(row: usize, data: u16) -> Result<(), Error> {
if row >= NUM_ROWS {
return Err(Error::InvalidIndex);
}
let row_with_write_and_ecc_bit = row | 0x00030000;
let result = unsafe { otp_access(data.to_le_bytes().as_mut_ptr(), 2, row_with_write_and_ecc_bit as u32) };
if result == 0 {
Ok(())
} else {
let error = match result {
-4 => Error::InvalidPermissions,
-18 => Error::UnsupportedModification,
_ => Error::UnexpectedFailure(result),
};
Err(error)
}
}
pub fn get_chipid() -> Result<u64, Error> {
let w0 = read_ecc_word(0x000)?.to_be_bytes();
let w1 = read_ecc_word(0x001)?.to_be_bytes();
let w2 = read_ecc_word(0x002)?.to_be_bytes();
let w3 = read_ecc_word(0x003)?.to_be_bytes();
Ok(u64::from_be_bytes([
w3[0], w3[1], w2[0], w2[1], w1[0], w1[1], w0[0], w0[1],
]))
}
pub fn get_private_random_number() -> Result<u128, Error> {
let w0 = read_ecc_word(0x004)?.to_be_bytes();
let w1 = read_ecc_word(0x005)?.to_be_bytes();
let w2 = read_ecc_word(0x006)?.to_be_bytes();
let w3 = read_ecc_word(0x007)?.to_be_bytes();
let w4 = read_ecc_word(0x008)?.to_be_bytes();
let w5 = read_ecc_word(0x009)?.to_be_bytes();
let w6 = read_ecc_word(0x00a)?.to_be_bytes();
let w7 = read_ecc_word(0x00b)?.to_be_bytes();
Ok(u128::from_be_bytes([
w7[0], w7[1], w6[0], w6[1], w5[0], w5[1], w4[0], w4[1], w3[0], w3[1], w2[0], w2[1], w1[0], w1[1], w0[0], w0[1],
]))
}