Skip to content

Commit

Permalink
♥️ - OS template 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
INeddHelp committed May 7, 2023
1 parent 77b79fc commit 5baaa26
Show file tree
Hide file tree
Showing 31 changed files with 1,736 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "os-template"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
13 changes: 13 additions & 0 deletions src/arch/armv7.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![no_std]

#[no_mangle]
pub extern "C" fn kmain() -> ! {
// Set up some initial values
let mut x = 0;
let y = 1;

// Enter an infinite loop
loop {
x += y;
}
}
60 changes: 60 additions & 0 deletions src/arch/x86_64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Simple x86_64 implementation

#![no_std]

// GDT descriptor
#[repr(C)]
struct GdtDescriptor {
limit_low: u16,
base_low: u16,
base_middle: u8,
access: u8,
granularity: u8,
base_high: u8,
}

// GDT table
static GDT: [GdtDescriptor; 3] = [
// Null descriptor
GdtDescriptor {
limit_low: 0,
base_low: 0,
base_middle: 0,
access: 0,
granularity: 0,
base_high: 0,
},
// Code descriptor
GdtDescriptor {
limit_low: 0xFFFF,
base_low: 0,
base_middle: 0,
access: 0x9A,
granularity: 0xCF,
base_high: 0,
},
// Data descriptor
GdtDescriptor {
limit_low: 0xFFFF,
base_low: 0,
base_middle: 0,
access: 0x92,
granularity: 0xCF,
base_high: 0,
},
];

#[no_mangle]
pub extern "C" fn kmain() -> ! {
// Load the GDT
unsafe {
let descriptor = GdtDescriptorPointer {
size: (core::mem::size_of::<[GdtDescriptor; 3]>() - 1) as u16,
offset: &GDT as *const _ as u64,
};
asm!("lgdt [{0}]", in(reg) &descriptor);
}

// Enter an infinite loop
loop {}
}
44 changes: 44 additions & 0 deletions src/drivers/keyboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Keyboard driver implementation for x86_64 architecture
// Requires a PS/2 keyboard

use x86_64::instructions::port::Port;

// PS/2 keyboard constants
const PS2_DATA_PORT: u16 = 0x60;
const PS2_STATUS_PORT: u16 = 0x64;
const PS2_ACK: u8 = 0xFA;

// Keyboard driver struct
pub struct Keyboard {
status_port: Port<u8>,
data_port: Port<u8>,
}

impl Keyboard {
// Initialize the keyboard driver
pub fn new() -> Keyboard {
Keyboard {
status_port: Port::new(PS2_STATUS_PORT),
data_port: Port::new(PS2_DATA_PORT),
}
}

// Check if a key is currently pressed
fn is_key_pressed(&mut self) -> bool {
self.status_port.read() & 0x01 != 0
}

// Read a key press from the keyboard
pub fn read_key(&mut self) -> Option<u8> {
if self.is_key_pressed() {
let scancode = self.data_port.read();

// Send an ACK to the keyboard controller
self.data_port.write(PS2_ACK);

Some(scancode)
} else {
None
}
}
}
49 changes: 49 additions & 0 deletions src/drivers/network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Network driver implementation for x86_64 architecture
// Requires a compatible network interface card (NIC)

use x86_64::instructions::port::{Port, PortWriteOnly};

// Network card constants
const NIC_DATA_PORT: u16 = 0x300;
const NIC_COMMAND_PORT: u16 = 0x301;

// Network driver struct
pub struct Network {
data_port: Port<u8>,
command_port: PortWriteOnly<u8>,
}

impl Network {
// Initialize the network driver
pub fn new() -> Network {
Network {
data_port: Port::new(NIC_DATA_PORT),
command_port: PortWriteOnly::new(NIC_COMMAND_PORT),
}
}

// Read data from the network card
pub fn read(&mut self, buffer: &mut [u8]) {
for byte in buffer {
*byte = self.data_port.read();
}
}

// Write data to the network card
pub fn write(&mut self, buffer: &[u8]) {
for byte in buffer {
self.data_port.write(*byte);
}
}

// Reset the network card
pub fn reset(&mut self) {
self.command_port.write(0x80);
self.command_port.write(0x00);
}

// Configure the network card
pub fn configure(&mut self) {
// Set up the network card here
}
}
60 changes: 60 additions & 0 deletions src/drivers/storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Storage driver implementation for x86_64 architecture
// Requires a compatible storage device

use x86_64::instructions::port::{Port, PortWriteOnly};

// Storage device constants
const STORAGE_DATA_PORT: u16 = 0x1F0;
const STORAGE_COMMAND_PORT: u16 = 0x1F7;

// Storage driver struct
pub struct Storage {
data_port: Port<u8>,
command_port: PortWriteOnly<u8>,
}

impl Storage {
// Initialize the storage driver
pub fn new() -> Storage {
Storage {
data_port: Port::new(STORAGE_DATA_PORT),
command_port: PortWriteOnly::new(STORAGE_COMMAND_PORT),
}
}

// Read data from the storage device
pub fn read(&mut self, buffer: &mut [u8]) {
for sector in buffer.chunks_exact_mut(512) {
self.read_sector(sector);
}
}

// Write data to the storage device
pub fn write(&mut self, buffer: &[u8]) {
for sector in buffer.chunks_exact(512) {
self.write_sector(sector);
}
}

// Read a sector from the storage device
fn read_sector(&mut self, sector: &mut [u8]) {
// Issue a read command to the storage device
self.command_port.write(0x20);

// Read the sector data
for byte in sector.iter_mut() {
*byte = self.data_port.read();
}
}

// Write a sector to the storage device
fn write_sector(&mut self, sector: &[u8]) {
// Issue a write command to the storage device
self.command_port.write(0x30);

// Write the sector data
for byte in sector.iter() {
self.data_port.write(*byte);
}
}
}
Loading

0 comments on commit 5baaa26

Please sign in to comment.