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 author: [0u8; 32],
57 name: [0u8; 32],
58 ..Default::default()
59 }
60 }
61
62 /// Validate is this a valid proka executable.
63 #[inline]
64 pub fn validate(&self) -> bool {
65 self.magic == PKEX_MAGIC
66 }
67
68 /// Convert this header to array
69 #[inline]
70 pub const fn to_array(&self) -> [u8; HEADER_SIZE] {
71 // SAFETY: used `#[repr(C)]`
72 unsafe { core::ptr::read(self as *const Self as *const [u8; HEADER_SIZE]) }
73 }
74}
75
76/// The executable mode.
77#[repr(C)]
78#[derive(Debug, Clone, Copy, PartialEq, Eq)]
79pub enum ExecMode {
80 /// Run in `userapp` mode (Ring 3).
81 UserApp,
82
83 /// Run in `coredrv` mode (Ring 0).
84 CoreDrv,
85}
86
87// Tests
88#[cfg(test)]
89mod tests {
90 use super::*;
91
92 #[test]
93 fn test_header_length() {
94 assert_eq!(crate::HEADER_SIZE, 128);
95 assert_eq!(core::mem::size_of::<Header>(), 128)
96 }
97}