From 7986f8fbaeba3e01e01f235dbca4d6b0e18449e6 Mon Sep 17 00:00:00 2001 From: Godones <1925466036@qq.com> Date: Mon, 5 Feb 2024 22:32:25 +0800 Subject: [PATCH] feat: mkfs support update readme to add no_std usage notes --- Cargo.toml | 2 +- README.md | 29 +++++++++++++++++ lwext4-mkfs/Cargo.toml | 10 ++++++ lwext4-mkfs/src/main.rs | 72 +++++++++++++++++++++++++++++++++++++++++ lwext4-sys/build.rs | 1 - 5 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 lwext4-mkfs/Cargo.toml create mode 100644 lwext4-mkfs/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index b0cbe4a..3573b7d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,4 @@ [workspace] -members = [ "lwext4-rs","lwext4-sys"] +members = [ "lwext4-mkfs", "lwext4-rs","lwext4-sys"] resolver = "2" \ No newline at end of file diff --git a/README.md b/README.md index dd027b1..44cc985 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/lwext4-mkfs/Cargo.toml b/lwext4-mkfs/Cargo.toml new file mode 100644 index 0000000..df15945 --- /dev/null +++ b/lwext4-mkfs/Cargo.toml @@ -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" } \ No newline at end of file diff --git a/lwext4-mkfs/src/main.rs b/lwext4-mkfs/src/main.rs new file mode 100644 index 0000000..5eef171 --- /dev/null +++ b/lwext4-mkfs/src/main.rs @@ -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 "img file path") + .required(true) + .value_parser(value_parser!(PathBuf)), + ) + .arg( + arg!(-b --blocksize "block size") + .required(false) + .value_parser(value_parser!(u32)), + ) + .arg( + arg!(-l --label