Skip to main content

obkrnl/config/
mod.rs

1pub use self::dipsw::*;
2pub use self::param1::*;
3
4use alloc::boxed::Box;
5use alloc::sync::Arc;
6use config::{ConsoleId, ProductId, QaFlags};
7use core::num::NonZero;
8use macros::elf_note;
9
10mod dipsw;
11mod param1;
12
13pub const PAGE_SHIFT: usize = 14; // 16K
14pub const PAGE_SIZE: NonZero<usize> = NonZero::new(1 << PAGE_SHIFT).unwrap();
15pub const PAGE_MASK: NonZero<usize> = NonZero::new(PAGE_SIZE.get() - 1).unwrap();
16
17/// Runtime configurations for the kernel populated from [`config::Config`].
18pub struct Config {
19    cpu_count: NonZero<u8>,
20    unknown_dmem1: u8, // TODO: Figure out a correct name.
21    idps: &'static ConsoleId,
22    qa: bool,
23    qa_flags: &'static QaFlags,
24    env_vars: Box<[(&'static str, &'static str)]>, // kenvp
25}
26
27impl Config {
28    pub fn new(src: &'static ::config::Config) -> Arc<Self> {
29        let env_vars = Self::load_env(src);
30
31        Arc::new(Self {
32            cpu_count: src.cpu_count,
33            unknown_dmem1: 0,
34            idps: &src.idps,
35            qa: src.qa,
36            qa_flags: &src.qa_flags,
37            env_vars,
38        })
39    }
40
41    /// See `mp_ncpus` on the Orbis for a reference.
42    ///
43    /// # Reference offsets
44    /// | Version | Offset  |
45    /// |---------|---------|
46    /// |PS4 11.00|0x2168108|
47    pub fn cpu_count(&self) -> NonZero<u8> {
48        self.cpu_count
49    }
50
51    pub fn unknown_dmem1(&self) -> u8 {
52        self.unknown_dmem1
53    }
54
55    pub fn idps(&self) -> &'static ConsoleId {
56        self.idps
57    }
58
59    /// See `getenv` on the Orbis for a reference.
60    ///
61    /// # Reference offsets
62    /// | Version | Offset |
63    /// |---------|--------|
64    /// |PS4 11.00|0x39D0A0|
65    pub fn env(&self, name: &str) -> Option<&'static str> {
66        for &(k, v) in &self.env_vars {
67            if k == name {
68                return Some(v);
69            }
70        }
71
72        None
73    }
74
75    /// See `sceSblRcMgrIsAllowDisablingAslr` on the Orbis for a reference.
76    ///
77    /// # Reference offsets
78    /// | Version | Offset |
79    /// |---------|--------|
80    /// |PS4 11.00|0x3CA8F0|
81    pub fn is_allow_disabling_aslr(&self) -> bool {
82        self.qa && self.qa_flags.internal_dev()
83    }
84
85    /// See `sceSblAIMgrIsDevKit` on the Orbis for a reference.
86    ///
87    /// # Reference offsets
88    /// | Version | Offset |
89    /// |---------|--------|
90    /// |PS4 11.00|0x078F50|
91    pub fn is_devkit(&self) -> bool {
92        self.idps.product == ProductId::DEVKIT
93    }
94
95    /// See `sceSblAIMgrIsTestKit` on the Orbis for a reference.
96    ///
97    /// # Reference offsets
98    /// | Version | Offset |
99    /// |---------|--------|
100    /// |PS4 11.00|0x0790A0|
101    pub fn is_testkit(&self) -> bool {
102        self.idps.product == ProductId::TESTKIT
103    }
104
105    /// See `sceKernelCheckDipsw` on the Orbis for a reference.
106    ///
107    /// # Reference offsets
108    /// | Version | Offset |
109    /// |---------|--------|
110    /// |PS4 11.00|0x654D70|
111    pub fn dipsw(&self, _: Dipsw) -> bool {
112        if !self.is_testkit() {
113            if !self.is_devkit() {
114                return false;
115            }
116        } else {
117            todo!()
118        }
119
120        todo!()
121    }
122
123    /// See `init_dynamic_kenv` on the Orbis for a reference.
124    ///
125    /// # Reference offsets
126    /// | Version | Offset |
127    /// |---------|--------|
128    /// |PS4 11.00|0x39DC90|
129    fn load_env(config: &'static ::config::Config) -> Box<[(&'static str, &'static str)]> {
130        config.env().collect()
131    }
132}
133
134#[elf_note(section = ".note.obkrnl.page-size", name = "obkrnl", ty = 0)]
135static NOTE_PAGE_SIZE: [u8; size_of::<usize>()] = PAGE_SIZE.get().to_ne_bytes();