Skip to content

Commit

Permalink
vendor fat32 rust library
Browse files Browse the repository at this point in the history
  • Loading branch information
kloenk committed Feb 1, 2022
1 parent fcb6ad2 commit 8b45c4c
Show file tree
Hide file tree
Showing 21 changed files with 2,074 additions and 0 deletions.
1 change: 1 addition & 0 deletions .cargo/config.toml
@@ -0,0 +1 @@
source = { crates-io = { replace-with = "vendored-sources" }, vendored-sources = { directory = "vendor" } }
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -132,3 +132,5 @@ licensed under the [OFL-1.1](3rdparty_licenses/LICENSE.OFL-1.1) license and copy

m1n1 embeds portions of the [dwc3 usb linux driver](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/usb/dwc3/core.h?id=7bc5a6ba369217e0137833f5955cf0b0f08b0712), which was [BSD-or-GPLv2 dual-licensed](3rdparty_licenses/LICENSE.BSD-3.dwc3) and copyright
* Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com

m1n1 embeds some rust crates. Licenses can be found in the vendor directory for every crate.
1 change: 1 addition & 0 deletions rust/Cargo.toml
Expand Up @@ -11,3 +11,4 @@ crate-type = [ "staticlib" ]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
fat32 = "0.2"
1 change: 1 addition & 0 deletions vendor/block_device/.cargo-checksum.json
@@ -0,0 +1 @@
{"files":{"Cargo.toml":"58d8a67f25e32da802a29fb749713e0f9133888da945762f2ac3aead3b90d654","LICENSE-MIT":"ec353a54e72cbe55ff1434c234f6d0ff2d590f62c8434985dcee878a50cfe2f2","README.md":"d2c7883d3261cd99bb664e2ee9f63b18e2692420a83fbd8b2140522341ea7418","src/lib.rs":"6ec24999e79d747da4a632632df970ae33f12b8d4f61d9f90700efa33749de17"},"package":"d73c55835cc76243c11580a55eff3f31250b00105a744015b8957ed4203fb8ad"}
23 changes: 23 additions & 0 deletions vendor/block_device/Cargo.toml
@@ -0,0 +1,23 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies
#
# If you believe there's an error in this file please file an
# issue against the rust-lang/cargo repository. If you're
# editing this file be aware that the upstream Cargo.toml
# will likely look very different (and much more reasonable)

[package]
edition = "2018"
name = "block_device"
version = "0.1.2"
authors = ["spxg <itsme@unsafe.me>"]
description = "BlockDevice trait"
readme = "README.md"
keywords = ["BlockDevice"]
categories = ["embedded", "no-std"]
license = "MIT"
repository = "https://github.com/Spxg/block_device"
19 changes: 19 additions & 0 deletions vendor/block_device/LICENSE-MIT
@@ -0,0 +1,19 @@
Copyright 2020 Spxg <itsme@unsafe.me>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
8 changes: 8 additions & 0 deletions vendor/block_device/README.md
@@ -0,0 +1,8 @@
# BlockDevice trait
```rust
pub trait BlockDevice {
type Error;
fn read(&self, buf: &mut [u8], address: usize, number_of_blocks: usize) -> Result<(), Self::Error>;
fn write(&self, buf: &[u8], address: usize, number_of_blocks: usize) -> Result<(), Self::Error>;
}
```
17 changes: 17 additions & 0 deletions vendor/block_device/src/lib.rs
@@ -0,0 +1,17 @@
//! # BlockDevice trait
//! ```rust
//! pub trait BlockDevice {
//! type Error;
//! fn read(&self, buf: &mut [u8], address: usize, number_of_blocks: usize) -> Result<(), Self::Error>;
//! fn write(&self, buf: &[u8], address: usize, number_of_blocks: usize) -> Result<(), Self::Error>;
//! }
//! ```

#![no_std]

/// BlockDevice trait
pub trait BlockDevice {
type Error;
fn read(&self, buf: &mut [u8], address: usize, number_of_blocks: usize) -> Result<(), Self::Error>;
fn write(&self, buf: &[u8], address: usize, number_of_blocks: usize) -> Result<(), Self::Error>;
}
1 change: 1 addition & 0 deletions vendor/fat32/.cargo-checksum.json
@@ -0,0 +1 @@
{"files":{"Cargo.toml":"84ed06e6b77f0583bf4118cf12c8f93d536eb56da58ea4e040a0de1c2b231f2a","LICENSE-MIT":"ec353a54e72cbe55ff1434c234f6d0ff2d590f62c8434985dcee878a50cfe2f2","README.md":"dd0b0f23776f94c44a5b5f637273e8ab2b92563d36bbfd44670af48e138a5ece","src/bpb.rs":"05e6ae46b9eb9c9f47d7ec2f53925f3264ffed40691b74de06b9f24e31b1d1e9","src/dir.rs":"7f2d4c8e961ec88863f2fbf1df46a07db29fe6200dd7de42d408971f8e5984f5","src/directory_item.rs":"e0ed365dd61143404f3d7b1c4b023d654a40acafa1ea9f38fd555e16ffbee35e","src/fat.rs":"efa563e7b451f248ba5243a8bb637f7206f755d9b6a1c278122d4a517984c911","src/file.rs":"46e1a902713057ce125ae922ac0a3cb724682368f6b921cb12dda37148e4a056","src/lib.rs":"7f96089c18a56711d7827f7ef0ebd9b56b708c7a08f2fd1c29a4cd2f4d137f15","src/tool.rs":"9dff97ad7042fd2de8171c4334cb71bf152834b48b6e1ab21b63a4395a3fd8e2","src/volume.rs":"a9d186850cbbef7e642cef25773eb37a6cbd9aba20c13b833836616e4647a4fe"},"package":"04ade4c8dc650037c07d4d1d3329851b8bdbab0da8d2b83ac5d7114174702c17"}
35 changes: 35 additions & 0 deletions vendor/fat32/Cargo.toml
@@ -0,0 +1,35 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies
#
# If you believe there's an error in this file please file an
# issue against the rust-lang/cargo repository. If you're
# editing this file be aware that the upstream Cargo.toml
# will likely look very different (and much more reasonable)

[package]
edition = "2018"
name = "fat32"
version = "0.2.0"
authors = ["spxg <itsme@unsafe.me>"]
description = "FAT32 FileSystem Library"
readme = "README.md"
keywords = ["sdcard", "fat", "embedded", "stm32f407", "filesystem"]
categories = ["embedded", "no-std"]
license = "MIT"
repository = "https://github.com/Spxg/fat32"
[dependencies.block_device]
version = "0.1"

[features]
1024 = []
2048 = []
4096 = []
512 = []
default = ["512"]
[target."cfg(windows)".dev-dependencies.winapi]
version = "0.3"
features = ["fileapi", "winioctl", "ioapiset"]
19 changes: 19 additions & 0 deletions vendor/fat32/LICENSE-MIT
@@ -0,0 +1,19 @@
Copyright 2020 Spxg <itsme@unsafe.me>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
102 changes: 102 additions & 0 deletions vendor/fat32/README.md
@@ -0,0 +1,102 @@
# FAT32 FileSystem Library

This is FAT32 FileSystem Library, which is `#![no_std]` and does not use `alloc`.

Test passed with [sdio_sdhc](https://github.com/play-stm32/sdio_sdhc) and WindowsAPI.

## Supported Features
- [x] Read
- [x] Create File AND Dir
- [x] Write(OverWritten and Append)
- [x] Delete File AND DIR

## Questions
### My Device Support `std`, Can I Use This Crate?
Of course you can, but I don't recommend it. You should use `std::fs::File` OR other crates.

### Why Do You Write This Crate?
In order to support devices and environment which don't have `std`, like
* Embedded Device
* Bootloader

### Have More Examples?
* [Embedded Device's Bootloader](https://github.com/play-stm32/bootloader)

## How To Test (Only Windows)
* EDIT mount() function in lib.rs, change disk like `\\\\.\\E:`
* `cargo test`

## How To Use
You need make your library implement [`BlockDevice` trait](https://github.com/Spxg/block_device):

```rust
pub trait BlockDevice {
type Error;
fn read(&self, buf: &mut [u8], address: usize, number_of_blocks: usize) -> Result<(), Self::Error>;
fn write(&self, buf: &[u8], address: usize, number_of_blocks: usize) -> Result<(), Self::Error>;
}
```

For example, I use my another library [sdio_sdhc](https://github.com/play-stm32/sdio_sdhc) to implement:

```rust
impl BlockDevice for Card {
type Error = CmdError;

fn read(&self, buf: &mut [u8], address: usize, number_of_blocks: usize) -> Result<(), Self::Error> {
if number_of_blocks == 1 {
self.read_block(buf, address as u32)?
} else {
self.read_multi_blocks(buf, address as u32, number_of_blocks as u32)?
}

Ok(())
}

fn write(&self, buf: &[u8], address: usize, number_of_blocks: usize) -> Result<(), Self::Error> {
if number_of_blocks == 1 {
self.write_block(buf, address as u32)?
} else {
self.write_multi_blocks(buf, address as u32, number_of_blocks as u32)?
}

Ok(())
}
}
```

Now [sdio_sdhc](https://github.com/play-stm32/sdio_sdhc) library supported fat32 filesystem.
Then, add fat32 library to your application

```
# if no feature config, the BUFFER_SIZE is 512 Bytes
fat32 = "0.2"
```

If your card block is other size, like 1024 Bytes

```
[dependencies.fat32]
version = "0.2"
default-features = false
features = ["1024"]
```

Then, you can do some tests

```rust
// Card from sdio_sdhc crate
let card = Card::init().unwrap();
// Volume from fat32 crate
let cont = Volume::new(card);
// cd root dir
let root = cont.root_dir();
// create file named test.txt
root.create_file("test.txt").unwrap();
// open file
let mut file = root.open_file("test.txt").unwrap();
// write buffer to file
file.write(&[80; 1234]).unwrap();
```

If all goes well, the file was created with 1234 Bytes in root dir.
34 changes: 34 additions & 0 deletions vendor/fat32/src/bpb.rs
@@ -0,0 +1,34 @@
/// Define BIOS Parameters
#[derive(Debug, Copy, Clone)]
pub struct BIOSParameterBlock {
pub(crate) byte_per_sector: u16,
pub(crate) sector_per_cluster: u8,
pub(crate) reserved_sector: u16,
pub(crate) num_fat: u8,
pub(crate) total_sector: u32,
pub(crate) sector_per_fat: u32,
pub(crate) root_cluster: u32,
pub(crate) id: u32,
pub(crate) volume_label: [u8; 11],
pub(crate) file_system: [u8; 8],
}

impl BIOSParameterBlock {
/// Get the first sector offset bytes of the cluster from the cluster number
pub(crate) fn offset(&self, cluster: u32) -> usize {
((self.reserved_sector as usize)
+ (self.num_fat as usize) * (self.sector_per_fat as usize)
+ (cluster as usize - 2) * (self.sector_per_cluster as usize))
* (self.byte_per_sector as usize)
}

/// Get FAT1 Offset
pub(crate) fn fat1(&self) -> usize {
(self.reserved_sector as usize) * (self.byte_per_sector as usize)
}

/// Get sector_per_cluster_usize as usize value
pub(crate) fn sector_per_cluster_usize(&self) -> usize {
self.sector_per_cluster as usize
}
}

0 comments on commit 8b45c4c

Please sign in to comment.