Skip to content

Commit

Permalink
add ramfs example
Browse files Browse the repository at this point in the history
  • Loading branch information
yuoo655 committed Aug 9, 2023
1 parent 27d7780 commit e2d634b
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
13 changes: 13 additions & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ members = [
"apps/helloworld",
"apps/memtest",
"apps/fs/shell",
"apps/fs/ramfs",
"apps/net/echoserver",
"apps/net/httpclient",
"apps/net/httpserver",
Expand Down
19 changes: 19 additions & 0 deletions apps/fs/ramfs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "ramfs"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]




[dependencies]
axstd = { path = "../../../ulib/axstd", optional = true, features = ["paging"] }
axtask = { path = "../../../modules/axtask" }
axalloc = { path = "../../../modules/axalloc" }
axfs = { path = "../../../modules/axfs"}
axio = { path = "../../../crates/axio", features = ["alloc"] }
axdriver = { path = "../../../modules/axdriver", features = ["block", "ramdisk"]}
driver_block = { path = "../../../crates/driver_block", features = ["ramdisk"]}
30 changes: 30 additions & 0 deletions apps/fs/ramfs/create_test_img.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

# From https://github.com/rafalh/rust-fatfs/blob/master/scripts/create-test-img.sh

CUR_DIR=`dirname $0`

echo $OUT_DIR

create_test_img() {
local name=$1
local blkcount=$2
local fatSize=$3
dd if=/dev/zero of="$name" bs=1024 count=$blkcount
mkfs.vfat -s 1 -F $fatSize -n "Test!" -i 12345678 "$name"
mkdir -p mnt
sudo mount -o loop "$name" mnt -o rw,uid=$USER,gid=$USER
for i in $(seq 1 1000); do
echo "Rust is cool!" >>"mnt/long.txt"
done
echo "Rust is cool!" >>"mnt/short.txt"
mkdir -p "mnt/very/long/path"
echo "Rust is cool!" >>"mnt/very/long/path/test.txt"
mkdir -p "mnt/very-long-dir-name"
echo "Rust is cool!" >>"mnt/very-long-dir-name/very-long-file-name.txt"

sudo umount mnt
}

create_test_img "$CUR_DIR/fat16.img" 2500 16
create_test_img "$CUR_DIR/fat32.img" 34000 32
14 changes: 14 additions & 0 deletions apps/fs/ramfs/src/link_fs.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.align 4
.section .data
.global fs_
fs_:
.quad 2
.quad fs_start

.section .data
.global fs_start
.global fs_end
fs_start:
.incbin "apps/fs/ramfs/fat32.img"
fs_end:

53 changes: 53 additions & 0 deletions apps/fs/ramfs/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#![cfg_attr(feature = "axstd", no_std)]
#![cfg_attr(feature = "axstd", no_main)]

#[cfg(feature = "axstd")]
use axstd::println;

#[macro_use]
extern crate alloc;

use core::arch::global_asm;
global_asm!(include_str!("link_fs.S"));

use axdriver::AxDeviceContainer;
use axfs::api as fs;
use axio as io;
use driver_block::ramdisk::RamDisk;

use io::Result;

fn test_read_dir() -> Result<()> {
let dir = "/././//./";
println!("list directory {:?}:", dir);
for entry in fs::read_dir(dir)? {
let entry = entry?;
println!(" {}", entry.file_name());
}
println!("test_read_dir() OK!");
Ok(())
}


#[cfg_attr(feature = "axstd", no_mangle)]
fn main() {
println!("Hello, world!");

extern "C" {
fn fs_();
fn fs_start();
fn fs_end();
}
println!("fs addr {:#x?}", fs_ as usize);
println!("fs_start {:#x?}", fs_start as usize);
println!("fs_start {:#x?}", fs_end as usize);

let fs_data: &[u8] = unsafe {
core::slice::from_raw_parts(fs_start as *mut u8, fs_end as usize - fs_start as usize)
};

let ramdisk = RamDisk::from(&fs_data);
axfs::init_filesystems(AxDeviceContainer::from_one(ramdisk));

test_read_dir().expect("test_read_dir() failed");
}

0 comments on commit e2d634b

Please sign in to comment.