use crate::HEADER_SIZE;
pub const PKEX_MAGIC: u32 = 0x58454B50;
#[repr(C, packed)]
#[derive(Debug, Clone, Copy)]
pub struct Header {
pub magic: u32,
pub min: [u16; 3],
pub max: [u16; 3],
pub mode: ExecMode,
pub sections: u16,
pub entry_sec: u16,
pub entry_off: u32,
pub author: [u8; 32],
pub name: [u8; 32],
pub extended: [u8; 36],
}
impl Default for Header {
fn default() -> Self {
Self::new()
}
}
impl Header {
pub fn new() -> Self {
Self {
magic: PKEX_MAGIC,
min: [0; 3],
max: [0; 3],
mode: ExecMode::UserApp,
sections: 0,
entry_sec: 0,
entry_off: HEADER_SIZE as u32,
author: [0; 32],
name: [0; 32],
extended: [0u8; 36],
}
}
#[inline]
pub fn validate(&self) -> bool {
self.magic == PKEX_MAGIC
}
#[inline]
pub const fn to_array(&self) -> [u8; HEADER_SIZE] {
unsafe { core::ptr::read(self as *const Self as *const [u8; HEADER_SIZE]) }
}
}
#[repr(C)]
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExecMode {
#[default]
UserApp,
CoreDrv,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_header_length() {
assert_eq!(crate::HEADER_SIZE, 128);
assert_eq!(core::mem::size_of::<Header>(), 128)
}
}