Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Gianmatteo Palmieri <[email protected]>
  • Loading branch information
mrgian committed Sep 9, 2023
1 parent 30253aa commit 685c826
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 48 deletions.
10 changes: 5 additions & 5 deletions bootloader/src/splash.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub const SPLASH: &'static str =
"▄ ▄ ▄
pub const SPLASH: &'static str =
"▄ ▄ ▄
██▄ ▄██
█ ▀█▄ ▄█▀▐█
█ ▀▄ ▄▀ ▐█
Expand All @@ -23,7 +23,7 @@ pub const SPLASH: &'static str =
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▀▀▀▀▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
Press any key to start...";

#[allow(dead_code)]
pub fn splash() {
for c in SPLASH.chars() {
Expand All @@ -33,8 +33,8 @@ pub fn splash() {
'▌' => print!("{}", 0xdd as char),
'▄' => print!("{}", 0xdc as char),
'█' => print!("{}", 0xdb as char),
'\n' => {},
'\n' => {}
_ => print!("{}", c),
}
}
}
}
24 changes: 10 additions & 14 deletions kernel/src/filesystem/fat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@

use crate::drivers::disk::DISK;
use core::mem;
use core::sync::atomic::AtomicBool;
use libfelix::mutex::Mutex;

//Warning! Mutable static here
pub static mut FAT_MUTEX: Mutex<FatDriver> = Mutex::new( FatDriver {
pub static mut FAT: Mutex<FatDriver> = Mutex::new(FatDriver {
header: NULL_HEADER,
entries: [NULL_ENTRY; ENTRY_COUNT],
table: [0; FAT_SIZE],
buffer: [0; 2048],
} );
});

const ENTRY_COUNT: usize = 512;
const FAT_START: u16 = 36864;

const FAT_SIZE: usize = 256;



//FAT16 header
#[derive(Copy, Clone, Debug)]
#[repr(C, packed)]
Expand Down Expand Up @@ -51,7 +47,7 @@ pub struct Header {
zero: [u8; 460], //needed to make struct 512 bytes big
}

pub static NULL_HEADER: Header = Header {
static NULL_HEADER: Header = Header {
boot_jump_instructions: [0; 3],

oem_identifier: [0; 8],
Expand Down Expand Up @@ -95,7 +91,7 @@ pub struct Entry {
size: u32,
}

pub static NULL_ENTRY: Entry = Entry {
static NULL_ENTRY: Entry = Entry {
name: [0; 11],
attributes: 0,
reserved: 0,
Expand Down Expand Up @@ -142,7 +138,7 @@ impl FatDriver {

let lba: u64 = FAT_START as u64
+ (self.header.reserved_sectors
+ self.header.sectors_per_fat * self.header.fat_count as u16) as u64;
+ self.header.sectors_per_fat * self.header.fat_count as u16) as u64;

let size: u16 = entry_size * self.header.dir_entries_count;
let sectors: u16 = size / self.header.bytes_per_sector;
Expand Down Expand Up @@ -193,12 +189,12 @@ impl FatDriver {

//read first cluster of file to buffer
pub fn read_file_to_buffer(&self, entry: &Entry) {
let target = self.buffer.as_ptr() as *mut u8;
let target = self.buffer.as_ptr() as *mut u8;

let data_lba: u64 = FAT_START as u64
+ (self.header.reserved_sectors
+ self.header.sectors_per_fat * self.header.fat_count as u16
+ 32) as u64;
+ self.header.sectors_per_fat * self.header.fat_count as u16
+ 32) as u64;
let lba: u64 = data_lba
+ ((entry.first_cluster_low - 2) * self.header.sectors_per_cluster as u16) as u64;

Expand All @@ -218,8 +214,8 @@ impl FatDriver {
loop {
let data_lba: u64 = FAT_START as u64
+ (self.header.reserved_sectors
+ self.header.sectors_per_fat * self.header.fat_count as u16
+ 32) as u64;
+ self.header.sectors_per_fat * self.header.fat_count as u16
+ 32) as u64;

let lba: u64 =
data_lba + ((next_cluster - 2) * self.header.sectors_per_cluster as u16) as u64;
Expand Down
6 changes: 3 additions & 3 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ use memory::allocator::Allocator;
use memory::paging::PAGING;
use shell::shell::SHELL;
use syscalls::print::PRINTER;
use filesystem::fat::FAT;

use multitasking::task::TASK_MANAGER;

use libfelix;
use crate::filesystem::fat::{FAT_MUTEX, FatDriver, NULL_ENTRY, NULL_HEADER};

#[global_allocator]
static ALLOCATOR: Allocator = Allocator;
Expand Down Expand Up @@ -80,11 +80,11 @@ pub extern "C" fn _start() -> ! {

//init filesystem
if DISK.enabled {
let fat = FAT_MUTEX.acquire_mut();
let fat = FAT.acquire_mut();
fat.load_header();
fat.load_table();
fat.load_entries();
FAT_MUTEX.free();
FAT.free();
}

//print name, version and copyright
Expand Down
26 changes: 12 additions & 14 deletions kernel/src/shell/shell.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//SHELL

use crate::filesystem::fat::{FAT_MUTEX};
use crate::filesystem::fat::FAT;
use crate::multitasking::task::TASK_MANAGER;
use crate::syscalls::print::PRINTER;

Expand Down Expand Up @@ -97,8 +97,8 @@ impl Shell {

//list root directory
_b if self.is_command("ls") => unsafe {
FAT_MUTEX.acquire().list_entries();
FAT_MUTEX.free();
FAT.acquire().list_entries();
FAT.free();
},

//list running tasks
Expand Down Expand Up @@ -170,15 +170,14 @@ impl Shell {
for i in 4..15 {
self.arg[i - 4] = b[i];
}
let fat_driver = FAT_MUTEX.acquire();
let fat = FAT.acquire();

let entry = fat_driver.search_file(&self.arg);
let entry = fat.search_file(&self.arg);

if entry.name[0] != 0 {
fat.read_file_to_buffer(entry);

fat_driver.read_file_to_buffer(entry);

for c in fat_driver.buffer {
for c in fat.buffer {
if c != 0 {
libfelix::print!("{}", c as char);
}
Expand All @@ -187,17 +186,17 @@ impl Shell {
} else {
libfelix::println!("File not found!");
}
FAT_MUTEX.free();
FAT.free();
}

//loads an executable as a task
pub unsafe fn run(&mut self, b: &[char]) {
for i in 4..15 {
self.arg[i - 4] = b[i];
}
let ft = FAT_MUTEX.acquire();
let fat = FAT.acquire();

let entry = ft.search_file(&self.arg);
let entry = fat.search_file(&self.arg);
if entry.name[0] != 0 {
let slot = TASK_MANAGER.get_free_slot();
let target = APP_TARGET + (slot as u32 * APP_SIZE);
Expand All @@ -206,7 +205,7 @@ impl Shell {
TABLES[8].set(target);
PAGING.set_table(8, &TABLES[8]);

ft.read_file_to_target(&entry, target as *mut u32);
fat.read_file_to_target(&entry, target as *mut u32);

unsafe {
let signature = *(target as *mut u32);
Expand All @@ -220,8 +219,7 @@ impl Shell {
} else {
libfelix::println!("Program not found!");
}
FAT_MUTEX.free();

FAT.free();
}

pub fn is_command(&self, command: &str) -> bool {
Expand Down
3 changes: 1 addition & 2 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![no_std]


pub mod print;
pub mod mutex;
pub mod print;
18 changes: 8 additions & 10 deletions lib/src/mutex.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use core::fmt;
use core::sync::atomic::{AtomicBool, Ordering};
/*
This is a oversimplified mutex created from scratch. Meant to be used for global, static definitions of objects, visible for all active threads. Once a thread acquires the target object, all other threads trying to do so will wait until it is freed.
There is plenty of room for improvements, since there are no mechanisms for e.g. creating a queue of threads that requested access to an object and giving it to the first that needs it.
TODO: Improve it
This is a oversimplified mutex created from scratch. Meant to be used for global, static definitions of objects, visible for all active threads. Once a thread acquires the target object, all other threads trying to do so will wait until it is freed.
There is plenty of room for improvements, since there are no mechanisms for e.g. creating a queue of threads that requested access to an object and giving it to the first that needs it.
TODO: Improve it
*/
pub struct Mutex<T> {
target: T,
free: AtomicBool,
target: T,
free: AtomicBool,
}

impl<T> Mutex<T> {
Expand Down Expand Up @@ -39,9 +38,8 @@ impl<T> Mutex<T> {
}
}

impl<T> Drop for Mutex<T> {
impl<T> Drop for Mutex<T> {
fn drop(&mut self) {
self.free = AtomicBool::from(true);
}
}

0 comments on commit 685c826

Please sign in to comment.