Skip to content

Commit

Permalink
Implements write for direct dumping
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon committed May 18, 2024
1 parent 74db237 commit 2df4746
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
4 changes: 1 addition & 3 deletions korbis-1100/src/uio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ pub struct Uio {
td: *mut Thread,
}

impl korbis::uio::Uio for Uio {
type Kernel = Kernel;

impl korbis::uio::Uio<Kernel> for Uio {
unsafe fn new(
td: *mut Thread,
op: UioRw,
Expand Down
2 changes: 1 addition & 1 deletion korbis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub mod uio;
pub trait Kernel: Copy + Send + Sync + 'static {
type File: File;
type Thread: Thread;
type Uio: Uio;
type Uio: Uio<Self>;

/// # Safety
/// `base` must point to a valid address of the kernel. Behavior is undefined if format of the
Expand Down
6 changes: 2 additions & 4 deletions korbis/src/uio/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use crate::Kernel;

/// Represents `uio` structure.
pub trait Uio: Sized {
type Kernel: Kernel;

pub trait Uio<K: Kernel>: Sized {
/// Returns [`None`] if `len` is geater than [`Uio::vec_max()`] or total length of `iov` is
/// greater than [`Uio::io_max()`].
///
/// # Safety
/// - `td` cannot be null.
/// - `iov` cannot be null and must be valid up to `len`.
unsafe fn new(
td: *mut <Self::Kernel as Kernel>::Thread,
td: *mut K::Thread,
op: UioRw,
seg: UioSeg,
iov: *mut IoVec,
Expand Down
29 changes: 16 additions & 13 deletions src/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use crate::DumpMethod;
use core::ffi::{c_int, CStr};
use core::num::NonZeroI32;
use korbis::thread::Thread;
use korbis::uio::UioSeg;
use korbis::uio::{IoVec, Uio, UioRw, UioSeg};
use korbis::Kernel;
use x86_64::registers::control::Cr0;

/// Implementation of [`DumpMethod`] using internal kernel functions.
///
Expand All @@ -15,17 +14,7 @@ pub struct DirectMethod<K> {
}

impl<K: Kernel> DirectMethod<K> {
#[cfg(fw = "1100")]
pub fn new(kernel: K) -> Self {
// Restore kmem_alloc patch done by PPPwn.
let base = unsafe { kernel.elf().as_ptr().cast_mut() };
let cr0 = Cr0::read_raw();

unsafe { Cr0::write_raw(cr0 & !(1 << 16)) };
unsafe { base.add(0x245EDC).write(3) };
unsafe { base.add(0x245EE4).write(3) };
unsafe { Cr0::write_raw(cr0) };

Self { kernel }
}
}
Expand Down Expand Up @@ -56,7 +45,21 @@ impl<K: Kernel> DumpMethod for DirectMethod<K> {
}

fn write(&self, fd: c_int, buf: *const u8, len: usize) -> Result<usize, NonZeroI32> {
Ok(len)
// Setup iovec.
let mut iov = IoVec {
ptr: buf.cast_mut(),
len,
};

// Write.
let td = Thread::current();
let mut io = unsafe { Uio::new(td, UioRw::Write, UioSeg::Kernel, &mut iov, 1).unwrap() };
let errno = unsafe { self.kernel.kern_writev(td, fd, &mut io) };

match NonZeroI32::new(errno) {
Some(v) => Err(v),
None => Ok(unsafe { (*td).ret(0) }),
}
}

fn fsync(&self, fd: c_int) -> Result<(), NonZeroI32> {
Expand Down

0 comments on commit 2df4746

Please sign in to comment.