Skip to content

Commit 9312031

Browse files
AmerikraniankiranchandrasekharJoseph Wilsonpgosar
authored
Ext2 (#64)
* EXT2: Added (yet untested) and incomplete implementation * Ext2: Add writing support (untested) * Ext2: Add filesys tests * EXT2: Fix directories being written improperly * EXT2: Fix tests not respecting 0 as sentinel for allocation * Obey clippy * Remove dead code * Made EXT2 Async * SD integration of EXT2 * Made SD block size for BlockIO configurable Additonally fixes wrong num_blocks paramater, along with fixing some documentation listed in comments. * Created actual filesystem image Additonally reads the superblock before setting up the allocator * Fixed formatting * Add tests for real now * Make EXT2 on Mac * Fixed spin in interrupt * Add (currently broken) tests * Passed most ext2 tests * Zero out blocks for ext2 * Ext2 code refactor * Remove unnecessary arcs * Remove fat16 --------- Co-authored-by: Kiran Chandrasekhar <[email protected]> Co-authored-by: Joseph Wilson <[email protected]> Co-authored-by: pgosar <[email protected]>
1 parent 2cb9c69 commit 9312031

27 files changed

+4825
-1575
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
*.txt
77

88
*zig*
9+
10+
/target

Makefile

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
override STORAGE_NAME := storage_test
22

3+
UNAME_S := $(shell uname -s)
4+
ifeq ($(UNAME_S),Linux)
5+
MKFS := mkfs.ext2
6+
endif
7+
ifeq ($(UNAME_S),Darwin)
8+
MKFS := $(shell brew --prefix e2fsprogs)/sbin/mkfs.ext2
9+
endif
10+
311
.PHONY: check
412
check:
513
@cd kernel && \
@@ -38,13 +46,14 @@ fmt:
3846

3947
.PHONY: objdump
4048
objdump:
41-
@cd kernel && cargo objdump --lib --release -- -d -M intel
49+
@cd kernel && cargo objdump --lib --release -- -d -M intel -S
4250

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

4756
.PHONY: clean
4857
clean:
49-
@cd kernel && rm $(STORAGE_NAME).img
58+
@cd kernel && rm -f $(STORAGE_NAME).img
5059
@cd kernel && cargo clean

kernel/Cargo.lock

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

kernel/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ arrayvec = { version = "0.7.6", default-features = false }
2626
log = { version = "0.4.25", default-features = false }
2727
async-trait = "0.1.86"
2828
fontdue = { version = "0.9.3", default-features = false, features = ["hashbrown"] }
29+
zerocopy = {version = "0.8", features = ["derive"]}
2930
pc-keyboard = "0.8.0"
3031
futures-util = { version = "0.3.31", default-features = false, features = ["alloc", "async-await", "async-await-macro", "futures-macro"] }

kernel/limage_config.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ gdb-terminal = { args = ["-nographic", "-s", "-S"] }
4040
gdb-gui = { args = ["-s", "-S"] }
4141

4242
[test]
43-
timeout_secs = 20
43+
timeout_secs = 30
4444
success_exit_code = 33
4545
no_reboot = true
4646
extra_args = [

kernel/src/devices/sd_card.rs

+70-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ use crate::{
1515
debug_println,
1616
devices::pci::write_pci_command,
1717
events::{current_running_event, futures::devices::SDCardReq, get_runner_time},
18-
filesys::{BlockDevice, FsError},
18+
filesys::{
19+
ext2::block_io::{BlockError, BlockIO, BlockResult},
20+
BlockDevice, FsError,
21+
},
1922
memory::paging,
2023
};
2124
use bitflags::bitflags;
@@ -49,7 +52,7 @@ pub struct SDCardInfo {
4952
/// Stores the block size of this sd card
5053
block_size: usize,
5154
/// Stores total blocks of this sd card
52-
total_blocks: u64,
55+
total_sectors: u64,
5356
/// Stores the relative card address. This is used as an argument in some
5457
/// sd commands
5558
reletave_card_address: u32,
@@ -195,12 +198,12 @@ const SD_SUB_CLASS: u8 = 0x5;
195198
const SD_NO_DMA_INTERFACE: u8 = 0x0;
196199
const SD_DMA_INTERFACE: u8 = 0x1;
197200
const MAX_ITERATIONS: usize = 1_000;
198-
const SD_BLOCK_SIZE: u32 = 512;
201+
const SD_SECTOR_SIZE: u32 = 512;
199202

200203
#[async_trait]
201204
impl BlockDevice for SDCardInfo {
202205
async fn read_block(&self, block_num: u64, buf: &mut [u8]) -> Result<(), FsError> {
203-
if block_num > self.total_blocks {
206+
if block_num > self.total_sectors {
204207
return Result::Err(FsError::IOError);
205208
}
206209
let data = read_sd_card(
@@ -217,7 +220,7 @@ impl BlockDevice for SDCardInfo {
217220
}
218221

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

237240
fn block_size(&self) -> usize {
238-
SD_BLOCK_SIZE.try_into().expect("To be on 64 bit system")
241+
SD_SECTOR_SIZE.try_into().expect("To be on 64 bit system")
239242
}
240243

241244
fn total_blocks(&self) -> u64 {
242-
self.total_blocks
245+
self.total_sectors
246+
}
247+
}
248+
249+
const SECTORS_PER_BLOCK: u32 = 2;
250+
const SD_BLOCK_IO_SIZE: u32 = SD_SECTOR_SIZE * SECTORS_PER_BLOCK;
251+
252+
#[async_trait]
253+
impl BlockIO for SDCardInfo {
254+
async fn read_block(&self, block_num: u64, buf: &mut [u8]) -> BlockResult<()> {
255+
if (block_num + 1) * SECTORS_PER_BLOCK as u64 > self.total_sectors {
256+
return BlockResult::Err(BlockError::InvalidBlock);
257+
}
258+
for start_block in 0..SECTORS_PER_BLOCK {
259+
let block_num_32: u32 = block_num
260+
.try_into()
261+
.expect("Total blocks is less than 32 bits long");
262+
let data = read_sd_card(self, block_num_32 * SECTORS_PER_BLOCK + start_block)
263+
.await
264+
.map_err(|_| BlockError::DeviceError)?;
265+
let start_block_size: usize = (start_block * SD_SECTOR_SIZE).try_into().unwrap();
266+
// Copy the data from buff to data (Blame clippy for this abomination)
267+
buf[start_block_size..(SD_SECTOR_SIZE as usize + start_block_size)]
268+
.copy_from_slice(&data[..(SD_SECTOR_SIZE as usize)]);
269+
}
270+
271+
Result::Ok(())
272+
}
273+
274+
async fn write_block(&self, block_num: u64, buf: &[u8]) -> BlockResult<()> {
275+
if (block_num + 1) * SECTORS_PER_BLOCK as u64 > self.total_sectors {
276+
return BlockResult::Err(BlockError::InvalidBlock);
277+
}
278+
let mut data: [u8; 512] = [0; 512];
279+
for start_block in 0..SECTORS_PER_BLOCK {
280+
let start_block_size: usize = (start_block * SD_SECTOR_SIZE).try_into().unwrap();
281+
// Copy the data from data to buff (Blame clippy for this abomination)
282+
data[..(SD_SECTOR_SIZE as usize)].copy_from_slice(
283+
&buf[start_block_size..(SD_SECTOR_SIZE as usize + start_block_size)],
284+
);
285+
let block_num_32: u32 = block_num
286+
.try_into()
287+
.expect("Total blocks is less than 32 bits long");
288+
write_sd_card(self, block_num_32 * SECTORS_PER_BLOCK + start_block, data)
289+
.await
290+
.map_err(|_| BlockError::DeviceError)?;
291+
}
292+
Result::Ok(())
293+
}
294+
295+
fn block_size(&self) -> u64 {
296+
SD_BLOCK_IO_SIZE as u64 //.try_into().expect("To be on 64 bit system")
297+
}
298+
299+
fn size_in_bytes(&self) -> u64 {
300+
self.total_sectors * SD_SECTOR_SIZE as u64
243301
}
244302
}
245303

@@ -549,7 +607,7 @@ fn get_full_sd_card_info(
549607
rca: u32,
550608
csd: u128,
551609
) -> Result<SDCardInfo, SDCardError> {
552-
// Currently only supports CSD Version 1.0
610+
// Currently only supports CSD Version 2.0
553611
let csd_structre: u32 = (csd >> 126)
554612
.try_into()
555613
.expect("Higher bits to be masked out");
@@ -564,8 +622,8 @@ fn get_full_sd_card_info(
564622
let info = SDCardInfo {
565623
internal_info: sd_card.clone(),
566624
reletave_card_address: rca,
567-
block_size: SD_BLOCK_SIZE.try_into().expect("To be on 64 bit system"),
568-
total_blocks: (c_size + 1).into(),
625+
block_size: SD_SECTOR_SIZE.try_into().expect("To be on 64 bit system"),
626+
total_sectors: ((c_size + 1) * 1024).into(),
569627
};
570628

571629
Result::Ok(info)
@@ -722,7 +780,7 @@ pub async fn read_sd_card(sd_card: &SDCardInfo, block: u32) -> Result<[u8; 512],
722780
unsafe { core::ptr::write_volatile(block_count_register_addr, 1) };
723781
sending_command_valid(internal_info)?;
724782
let argument_register_addr = (internal_info.base_address_register + 0x8) as *mut u32;
725-
unsafe { core::ptr::write_volatile(argument_register_addr, block * SD_BLOCK_SIZE) };
783+
unsafe { core::ptr::write_volatile(argument_register_addr, block) };
726784
let transfer_mode_register_adder = (internal_info.base_address_register + 0xC) as *mut u16;
727785
unsafe {
728786
core::ptr::write_volatile(
@@ -778,7 +836,7 @@ pub async fn write_sd_card(
778836
unsafe { core::ptr::write_volatile(block_count_register_addr, 1) };
779837
sending_command_valid(internal_info)?;
780838
let argument_register_addr = (internal_info.base_address_register + 0x8) as *mut u32;
781-
unsafe { core::ptr::write_volatile(argument_register_addr, block * SD_BLOCK_SIZE) };
839+
unsafe { core::ptr::write_volatile(argument_register_addr, block) };
782840
let transfer_mode_register_adder = (internal_info.base_address_register + 0xC) as *mut u16;
783841
unsafe { core::ptr::write_volatile(transfer_mode_register_adder, 0) };
784842

kernel/src/events/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,12 @@ pub fn current_running_event_priority() -> usize {
202202
pub fn inc_runner_clock() {
203203
let cpuid = x2apic::current_core_id() as u32;
204204
let runners = EVENT_RUNNERS.read();
205-
let mut runner = runners.get(&cpuid).expect("No runner found").write();
205+
unsafe {
206+
let runner = runners.get(&cpuid).expect("No runner found").as_mut_ptr();
206207

207-
runner.inc_system_clock();
208+
// Safe as long as system clock is ONLY accessed from this core
209+
(*runner).inc_system_clock();
210+
}
208211
}
209212

210213
pub fn get_runner_time(offset_nanos: u64) -> u64 {

kernel/src/filesys/block/memory.rs

-68
This file was deleted.

kernel/src/filesys/block/mod.rs

-1
This file was deleted.

0 commit comments

Comments
 (0)