Skip to main content

proka_exec/
header.rs

1//! The header definitions.
2
3use crate::HEADER_SIZE;
4
5/// The magic number, fixed to 'PKEX'
6pub const PKEX_MAGIC: u32 = 0x58454B50;
7
8/// The main header struct, which contains the metadata of the PKE file.
9#[repr(C)]
10#[derive(Debug, Clone, Copy)]
11pub struct Header {
12    /// The magic number, fixed to 'PKEX'
13    pub magic: u32,
14
15    /// The minimal kernel version supported.
16    ///
17    /// # Note
18    /// As the `proka-bootloader`'s definitions, its format is similar
19    /// like `[major, minor, fix]`. See `proka-bootloader` crate for more informations.
20    pub min: [u16; 3],
21
22    /// The maximum kernel supported.
23    ///
24    /// For notes, see above.
25    pub max: [u16; 3],
26
27    /// Signates is this executable run as `userapp` or `coredrv`.
28    pub mode: ExecMode,
29
30    /// The section table count.
31    pub sections: u16,
32
33    /// The author name (max length is 32 bytes).
34    pub author: [u8; 32],
35
36    /// The executable/project name.
37    pub name: [u8; 32],
38
39    /// Extended bits for different mode parsing.
40    pub extended: [u8; 40],
41}
42
43impl Default for Header {
44    fn default() -> Self {
45        Self::new()
46    }
47}
48
49impl Header {
50    /// Create a header object.
51    pub fn new() -> Self {
52        Self {
53            magic: PKEX_MAGIC,
54            author: [0u8; 32],
55            name: [0u8; 32],
56            ..Default::default()
57        }
58    }
59
60    /// Validate is this a valid proka executable.
61    #[inline]
62    pub fn validate(&self) -> bool {
63        self.magic == PKEX_MAGIC
64    }
65
66    /// Convert this header to array
67    #[inline]
68    pub const fn to_array(&self) -> [u8; HEADER_SIZE] {
69        // SAFETY: used `#[repr(C)]`
70        unsafe { core::ptr::read(self as *const Self as *const [u8; HEADER_SIZE]) }
71    }
72}
73
74/// The executable mode.
75#[repr(C)]
76#[derive(Debug, Clone, Copy)]
77pub enum ExecMode {
78    /// Run in `userapp` mode (Ring 3).
79    UserApp,
80
81    /// Run in `coredrv` mode (Ring 0).
82    CoreDrv,
83}