use esp_storage::FlashStorage;
use rusty_esp_mid_core::esp_core::error::{Error, Result};
pub const TABLE_OFFSET: u32 = 0x8000;
pub const MAX_ENTRIES: u32 = 95;
const MAGIC: [u8; 2] = [0xAA, 0x50];
const MD5_MAGIC: [u8; 2] = [0xEB, 0xEB];
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Partition {
pub kind: u8,
pub subtype: u8,
pub offset: u32,
pub len: u32,
label: [u8; 16],
}
impl Partition {
#[must_use]
pub fn label(&self) -> &str {
let end = self
.label
.iter()
.position(|&b| b == 0 || b == 0xFF)
.unwrap_or(self.label.len());
core::str::from_utf8(&self.label[..end]).unwrap_or("")
}
}
impl core::fmt::Debug for Partition {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Partition")
.field("label", &self.label())
.field("kind", &self.kind)
.field("subtype", &self.subtype)
.field("offset", &self.offset)
.field("len", &self.len)
.finish()
}
}
pub fn find_partition(storage: &mut FlashStorage<'_>, label: &str) -> Result<Option<Partition>> {
let mut entry = [0u8; 32];
for i in 0..MAX_ENTRIES {
storage
.read_nor(TABLE_OFFSET + i * 32, &mut entry)
.map_err(|_| Error::Hardware)?;
if entry[..2] == MD5_MAGIC || entry[..2] == [0xFF, 0xFF] {
return Ok(None);
}
if entry[..2] != MAGIC {
return Err(Error::Corrupt);
}
let mut name = [0u8; 16];
name.copy_from_slice(&entry[12..28]);
let p = Partition {
kind: entry[2],
subtype: entry[3],
offset: u32::from_le_bytes([entry[4], entry[5], entry[6], entry[7]]),
len: u32::from_le_bytes([entry[8], entry[9], entry[10], entry[11]]),
label: name,
};
if p.label() == label {
return Ok(Some(p));
}
}
Ok(None)
}