Skip to main content

proka_exec/
header.rs

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