-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee9f682
commit 508a834
Showing
5 changed files
with
76 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "ps4k" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#![no_std] | ||
|
||
use self::elf::ProgramType; | ||
|
||
pub mod elf; | ||
|
||
/// Struct to access internal kernel functions and variables. | ||
pub struct Kernel { | ||
elf: &'static [u8], | ||
} | ||
|
||
impl Kernel { | ||
/// # Safety | ||
/// `base` must point to a valid address of the kernel. Behavior is undefined if format of the | ||
/// kernel is unknown. This should never happens unless the PS4 boot loader has been changed in | ||
/// the future. | ||
/// | ||
/// # Panics | ||
/// This function may panic if format of the kernel is unknown. | ||
pub unsafe fn new(base: *const u8) -> Self { | ||
// Get ELF loaded size. | ||
let e_phnum = base.add(0x38).cast::<u16>().read() as usize; | ||
let progs = core::slice::from_raw_parts(base.add(0x40), e_phnum * 0x38); | ||
let mut end = base as usize; | ||
|
||
for h in progs.chunks_exact(0x38) { | ||
// Skip non-loadable. | ||
let ty = ProgramType::new(u32::from_le_bytes(h[0x00..0x04].try_into().unwrap())); | ||
|
||
if !matches!(ty, ProgramType::PT_LOAD | ProgramType::PT_SCE_RELRO) { | ||
continue; | ||
} | ||
|
||
// Update end address. | ||
let addr = usize::from_le_bytes(h[0x10..0x18].try_into().unwrap()); | ||
let len = usize::from_le_bytes(h[0x28..0x30].try_into().unwrap()); | ||
let align = usize::from_le_bytes(h[0x30..0x38].try_into().unwrap()); | ||
|
||
assert!(addr >= end); // Just in case if Sony re-order the programs. | ||
|
||
end = addr + len.next_multiple_of(align); | ||
} | ||
|
||
// Get loaded ELF. | ||
let len = end - (base as usize); | ||
let elf = unsafe { core::slice::from_raw_parts(base, len) }; | ||
|
||
Self { elf } | ||
} | ||
|
||
/// Returns loaded ELF of the kernel. | ||
/// | ||
/// # Safety | ||
/// The returned slice can contains `PF_W` programs. That mean the memory covered by this slice | ||
/// can mutate at any time. The whole slice is guarantee to be readable. | ||
pub unsafe fn elf(&self) -> &'static [u8] { | ||
self.elf | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters