Skip to content

Commit

Permalink
Adds fget_write for 11.00
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon committed May 11, 2024
1 parent 431fdf1 commit 69da418
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
11 changes: 10 additions & 1 deletion korbis-1100/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use korbis::uio::UioSeg;
mod file;
mod thread;

/// Implementation of [`ps4k::Kernel`] for 11.00.
/// Implementation of [`korbis::Kernel`] for 11.00.
#[derive(Clone, Copy)]
pub struct Kernel(&'static [u8]);

Expand All @@ -25,6 +25,15 @@ impl korbis::Kernel for Kernel {
self.0
}

#[offset(0x4191C0)]
unsafe fn fget_write(
self,
td: *mut Self::Thread,
fd: c_int,
unused: c_int,
fp: *mut *mut Self::File,
) -> c_int;

#[offset(0x4161B0)]
unsafe fn fdrop(self, fp: *mut Self::File, td: *mut Self::Thread) -> c_int;

Expand Down
9 changes: 9 additions & 0 deletions korbis/src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ pub struct OwnedFile<K: Kernel> {
file: *mut K::File,
}

impl<K: Kernel> OwnedFile<K> {
/// # Safety
/// `file` cannot be null and the caller must own a strong reference to it. This method do
/// **not** increase the reference count of this file.
pub unsafe fn new(kernel: K, file: *mut K::File) -> Self {
Self { kernel, file }
}
}

impl<K: Kernel> Drop for OwnedFile<K> {
fn drop(&mut self) {
// See Drop implementation on Arc how this thing work.
Expand Down
37 changes: 36 additions & 1 deletion korbis/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#![no_std]

use self::elf::ProgramType;
use self::file::File;
use self::file::{File, OwnedFile};
use self::thread::Thread;
use self::uio::UioSeg;
use core::ffi::{c_char, c_int};
use core::num::NonZeroI32;
use core::ptr::null_mut;

pub use korbis_macros::*;

Expand All @@ -14,6 +16,10 @@ pub mod thread;
pub mod uio;

/// Provides methods to access the PS4 kernel for a specific version.
///
/// Most methods here are a direct call to the kernel so most of them are unsafe. A safe wrapper for
/// those methods are provides by [`KernelExt`], which is automatically implemented for any type
/// that implement [`Kernel`].
pub trait Kernel: Copy + Send + Sync + 'static {
type File: File;
type Thread: Thread;
Expand All @@ -33,6 +39,16 @@ pub trait Kernel: Copy + Send + Sync + 'static {
/// can mutate at any time. The whole slice is guarantee to be readable.
unsafe fn elf(self) -> &'static [u8];

/// # Safety
/// `fp` cannot be null.
unsafe fn fget_write(
self,
td: *mut Self::Thread,
fd: c_int,
unused: c_int,
fp: *mut *mut Self::File,
) -> c_int;

/// # Panics
/// If [`File::refcnt()`] of `fp` is not zero.
///
Expand Down Expand Up @@ -93,3 +109,22 @@ pub trait Kernel: Copy + Send + Sync + 'static {
core::slice::from_raw_parts(base, len)
}
}

/// Provides wrapper methods for methods on [`Kernel`].
///
/// This trait is automatically implemented for any type that implement [`Kernel`].
pub trait KernelExt: Kernel {
fn fget_write(self, td: *mut Self::Thread, fd: c_int) -> Result<OwnedFile<Self>, NonZeroI32>;
}

impl<T: Kernel> KernelExt for T {
fn fget_write(self, td: *mut Self::Thread, fd: c_int) -> Result<OwnedFile<Self>, NonZeroI32> {
let mut fp = null_mut();
let errno = unsafe { self.fget_write(td, fd, 0, &mut fp) };

match NonZeroI32::new(errno) {
Some(v) => Err(v),
None => Ok(unsafe { OwnedFile::new(self, fp) }),
}
}
}

0 comments on commit 69da418

Please sign in to comment.