Skip to content

Commit

Permalink
feat: mkfs support
Browse files Browse the repository at this point in the history
update readme to add no_std usage notes
  • Loading branch information
Godones committed Feb 5, 2024
1 parent 6c3c485 commit 7986f8f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[workspace]
members = [ "lwext4-rs","lwext4-sys"]
members = [ "lwext4-mkfs", "lwext4-rs","lwext4-sys"]

resolver = "2"
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,35 @@ This crate is `no_std` compatible. You can disable the default features to use i
lwext4-rs = { version = "0.1.0", default-features = false }
```

In the lwext4 configuration, debug output is enabled, so it relies on `printf/fflush/stdout` for output. In addition, it also relies on several functions:

1. `malloc` / `free` / `calloc` / `realloc`

2. `strcmp` / `strcpy` / `strncmp`

3. `qsort`

To handle these dependencies, you can define these functions manually or rely on some existing implementation.

The[ tinyrlibc](https://github.com/rust-embedded-community/tinyrlibc) library provides implementations of 1 and 2. In order to implement `printf`, you can refer to [prinf_compat](https://docs.rs/printf-compat/0.1.1/printf_compat/). [c-ward](https://github.com/sunfishcode/c-ward) provides the implementation of `qsort`, you can copy it directly from here. In the end, all we need to implement are `fflush `and `stdout`. Usually, we only need to implement these two as empty functions.

```rust
#[no_mangle]
static stdout: usize = 0;

#[no_mangle]
extern "C" fn fflush(file: *mut c_void) -> c_int{
assert!(file.is_null());
0
}
```

## mkfs

```rust
cargo run -p lwext4-mkfs -- --help
```

## Reference

[lwext4 (C)](https://github.com/gkostka/lwext4)
Expand Down
10 changes: 10 additions & 0 deletions lwext4-mkfs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "lwext4-mkfs"
version = "0.1.0"
edition = "2021"

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

[dependencies]
clap = { version = "4", features = ["cargo"] }
lwext4-rs = { path = "../lwext4-rs" }
72 changes: 72 additions & 0 deletions lwext4-mkfs/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use clap::{arg, command, value_parser};
use lwext4_rs::FsType::{Ext2, Ext3, Ext4};
use lwext4_rs::{BlockDeviceConfig, DefaultInterface, FsBuilder};
use std::fs::OpenOptions;
use std::path::PathBuf;

fn main() {
let matches = command!()
.arg(
arg!(-f --file <FILE> "img file path")
.required(true)
.value_parser(value_parser!(PathBuf)),
)
.arg(
arg!(-b --blocksize <BLOCKSIZE> "block size")
.required(false)
.value_parser(value_parser!(u32)),
)
.arg(
arg!(-l --label <LABEL> "fs label")
.required(false)
.value_parser(value_parser!(String)),
)
.arg(
arg!(-j --journal <JOURNAL> "journal")
.required(false)
.value_parser(value_parser!(bool)),
)
.arg(
arg!(-t --type <TYPE> "fs type")
.required(false)
.value_parser(value_parser!(u8)),
)
.get_matches();

let path = matches.get_one::<PathBuf>("file").unwrap();
let label = matches
.get_one::<String>("label")
.unwrap_or(&"ext4fs".to_string())
.clone();
let journal = matches.get_one::<bool>("journal").unwrap_or(&true);
let block_size = matches.get_one::<u32>("blocksize").unwrap_or(&4096);
let ty = matches.get_one::<u8>("type").unwrap_or(&2);
let ty = match ty {
2 => Ext2,
3 => Ext3,
4 => Ext4,
_ => panic!("unsupported fs type"),
};
let file = OpenOptions::new()
.read(true)
.write(true)
.open(path)
.unwrap();
let mut config = BlockDeviceConfig::default();

let meta = file.metadata().unwrap();
let bs: u64 = 512;
config.block_size = bs as u32;
config.part_size = meta.len();
config.part_offset = 0;
config.block_count = config.part_size / bs;
let blk = DefaultInterface::new_device(file, config);
let fs = FsBuilder::new()
.ty(ty)
.journal(*journal)
.block_size(*block_size)
.label(&label)
.build(blk)
.unwrap();
println!("{:#x?}", fs.fs_info().unwrap());
}
1 change: 0 additions & 1 deletion lwext4-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ fn build_for_os(lwext4: &PathBuf) {
/// and switch to the old value after completing the generation.
fn build_for_none(lwext4: &PathBuf) {
let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
println!("cargo:XXX={}", arch);
let lwext4_build = lwext4.join("build_musl-generic");
let lib_path = lwext4.join("build_musl-generic/src/liblwext4.a");
if !lwext4_build.exists() || !lib_path.exists() {
Expand Down

0 comments on commit 7986f8f

Please sign in to comment.