Skip to content

Commit ab2b715

Browse files
authored
Merge pull request #132 from rust-wii/feat/ios/fs
✨ IOS Filesystem Ioctls and Helpers
2 parents 29d3bc9 + 1ee8511 commit ab2b715

5 files changed

Lines changed: 510 additions & 49 deletions

File tree

examples/ios/Cargo.lock

Lines changed: 18 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/ios/src/main.rs

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,31 @@
11
#![no_std]
2-
#![feature(start)]
2+
#![no_main]
33

44
use alloc::vec;
55
use ogc_rs::{
66
ios::{self, Mode, SeekMode},
77
print, println,
88
};
9-
109
extern crate alloc;
1110

12-
#[start]
13-
fn main(_argc: isize, _argv: *const *const u8) -> isize {
14-
// Try to open SYSCONF
11+
#[no_mangle]
12+
extern "C" fn main() {
1513
if let Ok(fd) = ios::open(c"/shared2/sys/SYSCONF", Mode::Read) {
16-
// Try to grab size or default to 0;
17-
const GET_FILE_STATS: i32 = 11;
18-
let mut out_buf = [0u8; 8];
19-
let (size, seek_pos) = if ios::ioctl(fd, GET_FILE_STATS, &[], &mut out_buf).is_ok() {
20-
(
21-
usize::try_from(u32::from_be_bytes(out_buf[0..4].try_into().unwrap())).unwrap(),
22-
usize::try_from(u32::from_be_bytes(out_buf[4..8].try_into().unwrap())).unwrap(),
23-
)
24-
} else {
25-
(0usize, 0usize)
26-
};
27-
println!("{:?}, {:?}", size, seek_pos);
28-
29-
if seek_pos != 0 {
30-
// Try to seek to the start
31-
let _ = ios::seek(fd, 0, SeekMode::Start);
32-
}
14+
if let Ok(metadata) = ios::fs::get_file_stats_from_fd(fd) {
15+
if metadata.offset() != 0 {
16+
let _ = ios::seek(fd, 0, SeekMode::Start);
17+
}
3318

34-
let mut bytes = vec![0; size];
35-
if let Ok(bytes_read) = ios::read(fd, &mut bytes) {
36-
// SAFETY: I read this much bytes
37-
unsafe { bytes.set_len(bytes_read.try_into().unwrap()) };
38-
};
19+
let mut bytes = vec![0; metadata.size()];
20+
if let Ok(bytes_read) = ios::read(fd, &mut bytes) {
21+
unsafe { bytes.set_len(bytes_read.try_into().unwrap()) };
22+
}
3923

40-
println!("{:?}", bytes);
24+
println!("{:?}", bytes);
4125

42-
let _ = ios::close(fd);
26+
let _ = ios::close(fd);
27+
}
4328
}
44-
4529
loop {
4630
core::hint::spin_loop();
4731
}

src/ios.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ use core::{ffi::CStr, fmt::Display};
99
/// This is only on the system when running the Dolphin Emulator
1010
pub mod dolphin;
1111

12+
/// Filesystem IOS Device
13+
///
14+
/// '/dev/fs'' device helper functions.
15+
pub mod fs;
16+
1217
#[repr(u32)]
1318
/// Interprocess Control / IOS File Mode
1419
pub enum Mode {
@@ -35,6 +40,30 @@ impl From<Mode> for u32 {
3540
}
3641
}
3742

43+
impl From<Mode> for u8 {
44+
fn from(value: Mode) -> Self {
45+
match value {
46+
Mode::None => 0,
47+
Mode::Read => 1,
48+
Mode::Write => 2,
49+
Mode::ReadWrite => 3,
50+
}
51+
}
52+
}
53+
54+
impl TryFrom<u8> for Mode {
55+
type Error = ();
56+
fn try_from(value: u8) -> Result<Self, Self::Error> {
57+
match value {
58+
0 => Ok(Mode::None),
59+
1 => Ok(Mode::Read),
60+
2 => Ok(Mode::Write),
61+
3 => Ok(Mode::ReadWrite),
62+
_ => Err(()),
63+
}
64+
}
65+
}
66+
3867
#[repr(i32)]
3968
#[derive(Copy, Clone, Debug)]
4069
/// Interprocess Control / IOS Errors

0 commit comments

Comments
 (0)