Skip to content
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

Files save on baremetal RPI #137

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open

Files save on baremetal RPI #137

wants to merge 23 commits into from

Conversation

alloncm
Copy link
Owner

@alloncm alloncm commented Sep 9, 2023

This will mark the save files option in #123

@alloncm alloncm changed the title Feature - Files save on baremetal RPI Files save on baremetal RPI Sep 10, 2023
@alloncm alloncm added the enhancement New feature or request label Sep 10, 2023
hope the little optimization I dd there is fast enough,
cause I failed with the actual one
The file apears in root dir but is corrupted (at least in windows)
rpi/src/drivers/fat32.rs Outdated Show resolved Hide resolved
rpi/src/drivers/fat32.rs Outdated Show resolved Hide resolved
rpi/src/drivers/fat32.rs Outdated Show resolved Hide resolved
rpi/src/peripherals/emmc.rs Outdated Show resolved Hide resolved
rpi/src/drivers/disk.rs Outdated Show resolved Hide resolved
@alloncm alloncm marked this pull request as ready for review September 25, 2023 22:43
@@ -12,6 +12,7 @@ magenboy_common = {path = "../common"}
log = "0.4"
cfg-if = "1"
bitfield-struct = "0.5"
arrayvec = {version = "0.7", default-features = false}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why arrayvec? consider also tinyvec and smallvec or heapless to replace this and the StackString later on

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arrayvec is more popular then heapless and the rest dont also offer a Stack String struct which Ill need in the near future

@@ -1,7 +1,7 @@
/* Place _start procedure at the entry address for RPI */
__rpi_32_phys_binary_load_addr = 0x8000;
__isr_table_addr = 0;
__stack_size = 0x100000; /* 1MB stack */
__stack_size = 0x300000; /* 3MB stack */
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See if I can reduce this size (just for fun though 3MB ram is a lot on RPI's)

rpi/src/peripherals/emmc.rs Show resolved Hide resolved
Cargo.toml Show resolved Hide resolved
rpi/src/bin/baremetal/main.rs Show resolved Hide resolved
if let Some(existing_entry) = self.root_dir_cache.as_mut_slice().into_iter().find(|d|d.file_name == name && d.file_extension == extension){
if (existing_entry.size as usize) < content.len(){
existing_entry.file_name[0] = DELETED_DIR_ENTRY_PREFIX;
// TODO: mark the fat entries as free
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to do this, think how to test this

rpi/src/drivers/fat32.rs Outdated Show resolved Hide resolved
rpi/src/drivers/fat32.rs Outdated Show resolved Hide resolved
rpi/src/drivers/fat32.rs Outdated Show resolved Hide resolved
rpi/src/drivers/fat32.rs Outdated Show resolved Hide resolved
let buffer = &mut *core::ptr::slice_from_raw_parts_mut(t as *mut T as *mut _, core::mem::size_of::<T>());
return buffer;
pub(self) fn as_mut_buffer<'a, T>(t:&'a mut T)->&'a mut [u8]{
unsafe {&mut *core::ptr::slice_from_raw_parts_mut(t as *mut T as *mut _, core::mem::size_of::<T>())}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add safety comment

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified that this is correct using Miri

Cargo.toml Show resolved Hide resolved
}

fn arrayvec_as_buffer<'a, T, const CAP:usize>(vec:&'a ArrayVec<T, CAP>)->&'a [u8]{
unsafe{core::slice::from_raw_parts(vec.as_ptr() as *const u8, vec.len() * core::mem::size_of::<T>())}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add safety comment

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified that this is correct using Miri


#[derive(Clone, Debug)]
pub(super) struct FatIndex{
sector_number:u32,
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to sector index

let buffer = &mut *core::ptr::slice_from_raw_parts_mut(t as *mut T as *mut _, core::mem::size_of::<T>());
return buffer;
pub(crate) fn as_mut_buffer<'a, T>(t:&'a mut T)->&'a mut [u8]{
unsafe{&mut *core::ptr::slice_from_raw_parts_mut(t as *mut T as *mut _, core::mem::size_of::<T>())}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add safety comment

Comment on lines +109 to +113
self.fat_internal_index.sector_offset += FAT_ENTRY_SIZE;
if self.fat_internal_index.sector_offset >= SECTOR_SIZE{
self.fat_internal_index.sector_number += 1;
self.fat_internal_index.sector_offset = 0;
}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract those to another function

let buffer = &mut *core::ptr::slice_from_raw_parts_mut(t as *mut T as *mut _, core::mem::size_of::<T>());
return buffer;
pub(self) fn as_mut_buffer<'a, T>(t:&'a mut T)->&'a mut [u8]{
unsafe {&mut *core::ptr::slice_from_raw_parts_mut(t as *mut T as *mut _, core::mem::size_of::<T>())}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified that this is correct using Miri

}

fn arrayvec_as_buffer<'a, T, const CAP:usize>(vec:&'a ArrayVec<T, CAP>)->&'a [u8]{
unsafe{core::slice::from_raw_parts(vec.as_ptr() as *const u8, vec.len() * core::mem::size_of::<T>())}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified that this is correct using Miri

Comment on lines +15 to +58
pub enum FatSegmentState{
Free,
Allocated,
AllocatedEof,
Reserved,
Bad,
}

impl From<u32> for FatSegmentState{
fn from(value: u32) -> Self {
match value{
0 => Self::Free,
2..=0xFFF_FFF5 => Self::Allocated,
0xFFF_FFFF => Self::AllocatedEof,
0xFFF_FFF7 => Self::Bad,
_ => Self::Reserved
}
}
}

impl FatSegmentState{
/// Checks whether a value should be part of this segment or not
pub fn should_continue_segment(&self, other: &Self)->bool{
// AllocatedEof is should never continue segment
// otherwise fallback to check raw values of the enum
if *self == Self::AllocatedEof || *other == Self::AllocatedEof{
return false;
}
return self == other;
}
}

#[derive(Clone, Debug)]
pub struct FatSegment{
pub state:FatSegmentState,
pub len:u32,
pub start_index:u32,
}

impl FatSegment{
pub fn new(value:u32, start_index:u32)->Self{
Self { state: value.into(), len: 1, start_index}
}
}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return this to the fat module

Comment on lines +133 to +134
let mut root_dir_offset = 0;
const FILES_PER_LIST:usize = 20;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the const to the start of the function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant