Skip to content

Ext2 #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Apr 11, 2025
Merged

Ext2 #64

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
*.txt

*zig*

/target
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
override STORAGE_NAME := storage_test

UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
MKFS := mkfs.ext2
endif
ifeq ($(UNAME_S),Darwin)
MKFS := $(shell brew --prefix e2fsprogs)/sbin/mkfs.ext2
endif

.PHONY: check
check:
@cd kernel && \
Expand Down Expand Up @@ -38,13 +46,14 @@ fmt:

.PHONY: objdump
objdump:
@cd kernel && cargo objdump --lib --release -- -d -M intel
@cd kernel && cargo objdump --lib --release -- -d -M intel -S

.PHONY: blank_drive
blank_drive:
@cd kernel && dd if=/dev/zero of=$(STORAGE_NAME).img bs=1M count=4k
@cd kernel && $(MKFS) -b 1024 -d ../resources -I 128 $(STORAGE_NAME).img 4g

.PHONY: clean
clean:
@cd kernel && rm $(STORAGE_NAME).img
@cd kernel && rm -f $(STORAGE_NAME).img
@cd kernel && cargo clean
21 changes: 21 additions & 0 deletions kernel/Cargo.lock

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

1 change: 1 addition & 0 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ arrayvec = { version = "0.7.6", default-features = false }
log = { version = "0.4.25", default-features = false }
async-trait = "0.1.86"
fontdue = { version = "0.9.3", default-features = false, features = ["hashbrown"] }
zerocopy = {version = "0.8", features = ["derive"]}
pc-keyboard = "0.8.0"
futures-util = { version = "0.3.31", default-features = false, features = ["alloc", "async-await", "async-await-macro", "futures-macro"] }
2 changes: 1 addition & 1 deletion kernel/limage_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ gdb-terminal = { args = ["-nographic", "-s", "-S"] }
gdb-gui = { args = ["-s", "-S"] }

[test]
timeout_secs = 20
timeout_secs = 30
success_exit_code = 33
no_reboot = true
extra_args = [
Expand Down
82 changes: 70 additions & 12 deletions kernel/src/devices/sd_card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use crate::{
debug_println,
devices::pci::write_pci_command,
events::{current_running_event, futures::devices::SDCardReq, get_runner_time},
filesys::{BlockDevice, FsError},
filesys::{
ext2::block_io::{BlockError, BlockIO, BlockResult},
BlockDevice, FsError,
},
memory::paging,
};
use bitflags::bitflags;
Expand Down Expand Up @@ -49,7 +52,7 @@ pub struct SDCardInfo {
/// Stores the block size of this sd card
block_size: usize,
/// Stores total blocks of this sd card
total_blocks: u64,
total_sectors: u64,
/// Stores the relative card address. This is used as an argument in some
/// sd commands
reletave_card_address: u32,
Expand Down Expand Up @@ -195,12 +198,12 @@ const SD_SUB_CLASS: u8 = 0x5;
const SD_NO_DMA_INTERFACE: u8 = 0x0;
const SD_DMA_INTERFACE: u8 = 0x1;
const MAX_ITERATIONS: usize = 1_000;
const SD_BLOCK_SIZE: u32 = 512;
const SD_SECTOR_SIZE: u32 = 512;

#[async_trait]
impl BlockDevice for SDCardInfo {
async fn read_block(&self, block_num: u64, buf: &mut [u8]) -> Result<(), FsError> {
if block_num > self.total_blocks {
if block_num > self.total_sectors {
return Result::Err(FsError::IOError);
}
let data = read_sd_card(
Expand All @@ -217,7 +220,7 @@ impl BlockDevice for SDCardInfo {
}

async fn write_block(&mut self, block_num: u64, buf: &[u8]) -> Result<(), FsError> {
if block_num > self.total_blocks {
if block_num > self.total_sectors {
return Result::Err(FsError::IOError);
}
let mut data: [u8; 512] = [0; 512];
Expand All @@ -235,11 +238,66 @@ impl BlockDevice for SDCardInfo {
}

fn block_size(&self) -> usize {
SD_BLOCK_SIZE.try_into().expect("To be on 64 bit system")
SD_SECTOR_SIZE.try_into().expect("To be on 64 bit system")
}

fn total_blocks(&self) -> u64 {
self.total_blocks
self.total_sectors
}
}

const SECTORS_PER_BLOCK: u32 = 2;
const SD_BLOCK_IO_SIZE: u32 = SD_SECTOR_SIZE * SECTORS_PER_BLOCK;

#[async_trait]
impl BlockIO for SDCardInfo {
async fn read_block(&self, block_num: u64, buf: &mut [u8]) -> BlockResult<()> {
if (block_num + 1) * SECTORS_PER_BLOCK as u64 > self.total_sectors {
return BlockResult::Err(BlockError::InvalidBlock);
}
for start_block in 0..SECTORS_PER_BLOCK {
let block_num_32: u32 = block_num
.try_into()
.expect("Total blocks is less than 32 bits long");
let data = read_sd_card(self, block_num_32 * SECTORS_PER_BLOCK + start_block)
.await
.map_err(|_| BlockError::DeviceError)?;
let start_block_size: usize = (start_block * SD_SECTOR_SIZE).try_into().unwrap();
// Copy the data from buff to data (Blame clippy for this abomination)
buf[start_block_size..(SD_SECTOR_SIZE as usize + start_block_size)]
.copy_from_slice(&data[..(SD_SECTOR_SIZE as usize)]);
}

Result::Ok(())
}

async fn write_block(&self, block_num: u64, buf: &[u8]) -> BlockResult<()> {
if (block_num + 1) * SECTORS_PER_BLOCK as u64 > self.total_sectors {
return BlockResult::Err(BlockError::InvalidBlock);
}
let mut data: [u8; 512] = [0; 512];
for start_block in 0..SECTORS_PER_BLOCK {
let start_block_size: usize = (start_block * SD_SECTOR_SIZE).try_into().unwrap();
// Copy the data from data to buff (Blame clippy for this abomination)
data[..(SD_SECTOR_SIZE as usize)].copy_from_slice(
&buf[start_block_size..(SD_SECTOR_SIZE as usize + start_block_size)],
);
let block_num_32: u32 = block_num
.try_into()
.expect("Total blocks is less than 32 bits long");
write_sd_card(self, block_num_32 * SECTORS_PER_BLOCK + start_block, data)
.await
.map_err(|_| BlockError::DeviceError)?;
}
Result::Ok(())
}

fn block_size(&self) -> u64 {
SD_BLOCK_IO_SIZE as u64 //.try_into().expect("To be on 64 bit system")
}

fn size_in_bytes(&self) -> u64 {
self.total_sectors * SD_SECTOR_SIZE as u64
}
}

Expand Down Expand Up @@ -549,7 +607,7 @@ fn get_full_sd_card_info(
rca: u32,
csd: u128,
) -> Result<SDCardInfo, SDCardError> {
// Currently only supports CSD Version 1.0
// Currently only supports CSD Version 2.0
let csd_structre: u32 = (csd >> 126)
.try_into()
.expect("Higher bits to be masked out");
Expand All @@ -564,8 +622,8 @@ fn get_full_sd_card_info(
let info = SDCardInfo {
internal_info: sd_card.clone(),
reletave_card_address: rca,
block_size: SD_BLOCK_SIZE.try_into().expect("To be on 64 bit system"),
total_blocks: (c_size + 1).into(),
block_size: SD_SECTOR_SIZE.try_into().expect("To be on 64 bit system"),
total_sectors: ((c_size + 1) * 1024).into(),
};

Result::Ok(info)
Expand Down Expand Up @@ -722,7 +780,7 @@ pub async fn read_sd_card(sd_card: &SDCardInfo, block: u32) -> Result<[u8; 512],
unsafe { core::ptr::write_volatile(block_count_register_addr, 1) };
sending_command_valid(internal_info)?;
let argument_register_addr = (internal_info.base_address_register + 0x8) as *mut u32;
unsafe { core::ptr::write_volatile(argument_register_addr, block * SD_BLOCK_SIZE) };
unsafe { core::ptr::write_volatile(argument_register_addr, block) };
let transfer_mode_register_adder = (internal_info.base_address_register + 0xC) as *mut u16;
unsafe {
core::ptr::write_volatile(
Expand Down Expand Up @@ -778,7 +836,7 @@ pub async fn write_sd_card(
unsafe { core::ptr::write_volatile(block_count_register_addr, 1) };
sending_command_valid(internal_info)?;
let argument_register_addr = (internal_info.base_address_register + 0x8) as *mut u32;
unsafe { core::ptr::write_volatile(argument_register_addr, block * SD_BLOCK_SIZE) };
unsafe { core::ptr::write_volatile(argument_register_addr, block) };
let transfer_mode_register_adder = (internal_info.base_address_register + 0xC) as *mut u16;
unsafe { core::ptr::write_volatile(transfer_mode_register_adder, 0) };

Expand Down
7 changes: 5 additions & 2 deletions kernel/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,12 @@ pub fn current_running_event_priority() -> usize {
pub fn inc_runner_clock() {
let cpuid = x2apic::current_core_id() as u32;
let runners = EVENT_RUNNERS.read();
let mut runner = runners.get(&cpuid).expect("No runner found").write();
unsafe {
let runner = runners.get(&cpuid).expect("No runner found").as_mut_ptr();

runner.inc_system_clock();
// Safe as long as system clock is ONLY accessed from this core
(*runner).inc_system_clock();
}
}

pub fn get_runner_time(offset_nanos: u64) -> u64 {
Expand Down
68 changes: 0 additions & 68 deletions kernel/src/filesys/block/memory.rs

This file was deleted.

1 change: 0 additions & 1 deletion kernel/src/filesys/block/mod.rs

This file was deleted.

Loading
Loading