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 author name (max length is 32 bytes).
33    pub author: [u8; 32],
34
35    /// The executable/project name.
36    pub name: [u8; 32],
37
38    /// Extended bits for different mode parsing.
39    pub extended: [u8; 42],
40}
41
42impl Default for Header {
43    fn default() -> Self {
44        Self::new()
45    }
46}
47
48impl Header {
49    /// Create a header object.
50    pub fn new() -> Self {
51        Self {
52            magic: PKEX_MAGIC,
53            author: [0u8; 32],
54            name: [0u8; 32],
55            ..Default::default()
56        }
57    }
58
59    /// Validate is this a valid proka executable.
60    #[inline]
61    pub fn validate(&self) -> bool {
62        self.magic == PKEX_MAGIC
63    }
64
65    /// Convert this header to array
66    #[inline]
67    pub const fn to_array(&self) -> [u8; HEADER_SIZE] {
68        // SAFETY: used `#[repr(C)]`
69        unsafe { core::ptr::read(self as *const Self as *const [u8; HEADER_SIZE]) }
70    }
71}
72
73/// The executable mode.
74#[repr(C)]
75#[derive(Debug, Clone, Copy)]
76pub enum ExecMode {
77    /// Run in `userapp` mode (Ring 3).
78    UserApp,
79
80    /// Run in `coredrv` mode (Ring 0).
81    CoreDrv,
82}