Skip to content

Commit

Permalink
The disk changes works now
Browse files Browse the repository at this point in the history
The fat buffer still must be aligned for sector size
  • Loading branch information
alloncm committed Oct 3, 2023
1 parent d570013 commit ec8825e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 3 additions & 1 deletion rpi/src/drivers/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl Disk{
// early return if the buffer is aligned
if buffer_len_reminder == 0 {return};
}
log::warn!("Triggered unaligned read");
// handle the case buffer length is not aligned for block size
let mut temp_buffer:[u8;Self::BLOCK_SIZE as usize] = [0;Self::BLOCK_SIZE as usize];
self.emmc.seek(((block_index + (max_aligned_buffer_len as u32 / Self::BLOCK_SIZE)) * Self::BLOCK_SIZE) as u64);
Expand All @@ -92,9 +93,10 @@ impl Disk{
// early return since the buffer is aligned
if buffer_len_reminder == 0 {return};
}
log::warn!("Triggered unaligned write: len: {}", buffer.len());
// handle the case buffer length is not aligned for block size
let mut temp_buffer:[u8;Self::BLOCK_SIZE as usize] = [0;Self::BLOCK_SIZE as usize];
temp_buffer[max_aligned_buffer_len..].copy_from_slice(&buffer[..buffer_len_reminder]);
temp_buffer[..buffer_len_reminder].copy_from_slice(&buffer[max_aligned_buffer_len..]);
self.emmc.seek(((block_index + (max_aligned_buffer_len as u32 / Self::BLOCK_SIZE)) * Self::BLOCK_SIZE) as u64);
self.emmc_write(&temp_buffer);
}
Expand Down
8 changes: 5 additions & 3 deletions rpi/src/drivers/fat32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,16 @@ struct FatBuffer<const FBS:usize = FAT_BUFFER_SIZE>{
}

impl<const FBS:usize> FatBuffer<FBS>{
// The buffer Im reading will be the same buffer that Im writing back
// so it must be aligned in order to not corrupt the fat table
fn new(fat_info:FatInfo, first_cluster_index:usize, entries_count: Option<usize>, disk: &mut Disk)->Self{
let entries_count = entries_count.unwrap_or(FBS / FAT_ENTRY_SIZE);
let entries_count = entries_count.unwrap_or((FBS - SECTOR_SIZE) / FAT_ENTRY_SIZE); // The max size is smaller cause I need some padding space for alignment
let mut buffer = [0; FBS];
let fat_offset = first_cluster_index * FAT_ENTRY_SIZE;
let fat_index = FatIndex{ sector_number: (fat_info.first_fat_start_sector + (fat_offset / SECTOR_SIZE)) as u32, sector_offset: fat_offset % SECTOR_SIZE };

// The emmc can't read between sectors so align the start of the read to a sector
let fat_end_read = fat_index.sector_offset + (entries_count * FAT_ENTRY_SIZE);
// Align the end read to SECTOR_SIZE, since the FAT table is not aligned we need to read exactly X sectors in order to be able to write them back later
let fat_end_read = (entries_count * FAT_ENTRY_SIZE) + (SECTOR_SIZE - ((entries_count * FAT_ENTRY_SIZE) % SECTOR_SIZE));
if fat_end_read > FBS{
core::panic!("Error fat entries count is too much: expected:{}, actual: {}", FBS / FAT_ENTRY_SIZE, entries_count);
}
Expand Down

0 comments on commit ec8825e

Please sign in to comment.