Skip to content

Commit 49509fc

Browse files
committed
Make ioctls unsafe, and add some defines.
1 parent 88b28f0 commit 49509fc

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ authors = ["Jonathan 'theJPster' Pallant <neotron@thejpster.org.uk>"]
99
[dependencies]
1010
neotron-ffi = "0.1"
1111
neotron-api = "0.2"
12+
neotron-common-bios = "0.12"
1213

1314
[target.'cfg(unix)'.dependencies]
1415
crossterm = "0.26"

src/ioctls/gfx.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//! Graphics ioctl constants
2+
3+
/// Clear the screen
4+
///
5+
/// The corresponding value is the fill colour, which is taken modulo the number of on-screen colours.
6+
pub const COMMAND_CLEAR_SCREEN: u64 = 0;
7+
8+
/// Plot a chunky pixel
9+
///
10+
/// The command contains [ x | y | mode | colour ].
11+
///
12+
/// * `x` is 16 bits and marks the horizontal position (0 is left)
13+
/// * `y` is 16 bits and marks the vertical position (0 is top)
14+
/// * `mode` is 8 bits and is currently ignored
15+
/// * `colour` is 24 bits, and is taken modulo the number of on-screen colours
16+
///
17+
/// Use [`chunky_plot_value`] to create a suitable value for this ioctl command.
18+
pub const COMMAND_CHUNKY_PLOT: u64 = 1;
19+
20+
/// Change graphics mode
21+
///
22+
/// The command contains the video mode in the upper 32 bits and a pointer to a
23+
/// framebuffer in the lower 32 bits.
24+
///
25+
/// The framebuffer pointer must point to a 32-bit aligned region of memory
26+
/// that is large enough for the selected mode. If you pass `null`, then the OS
27+
/// will attempt to allocate a framebuffer for you.
28+
///
29+
/// Use [`change_mode_value`] to construct a value.
30+
pub const COMMAND_CHANGE_MODE: u64 = 2;
31+
32+
/// Calculate a 64-bit value argument for a gfx ioctl
33+
pub fn chunky_plot_value(x: u16, y: u16, colour: u32) -> u64 {
34+
(x as u64) << 48 | (y as u64) << 32 | (colour & 0xFFFFFF) as u64
35+
}
36+
37+
/// Calculate a 64-bit value argument for a gfx ioctl
38+
pub fn change_mode_value(mode: crate::VideoMode, fb_ptr: *mut u32) -> u64 {
39+
let fb_ptr = fb_ptr as usize as u64;
40+
let mode = mode.as_u8() as u64;
41+
mode << 32 | fb_ptr
42+
}
43+
44+
// End of file

src/ioctls/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//! A collection of ioctl constants
2+
3+
pub mod gfx;

src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ pub use neotron_ffi::{FfiBuffer, FfiByteSlice, FfiString};
2727

2828
pub use neotron_api::{file::Flags, path, Api, Error};
2929

30+
pub use neotron_common_bios::video::Mode as VideoMode;
31+
3032
use neotron_api as api;
3133

3234
pub mod console;
3335

36+
pub mod ioctls;
37+
3438
#[cfg(not(target_os = "none"))]
3539
mod fake_os_api;
3640

@@ -204,8 +208,14 @@ impl File {
204208

205209
/// Perform a special I/O control operation.
206210
///
207-
/// The allowed values of `command` and `value` are TBD.
208-
pub fn ioctl(&self, command: u64, value: u64) -> Result<u64> {
211+
/// The allowed values of `command` and `value` are defined in the
212+
/// [`ioctls`] module.
213+
///
214+
/// # Safety
215+
///
216+
/// Refer to the documentation for the ioctl you are using. Raw pointers may
217+
/// be involved.
218+
pub unsafe fn ioctl(&self, command: u64, value: u64) -> Result<u64> {
209219
let api = get_api();
210220
match (api.ioctl)(self.0, command, value) {
211221
neotron_ffi::FfiResult::Ok(output) => Ok(output),

0 commit comments

Comments
 (0)