rust-fatfs icon indicating copy to clipboard operation
rust-fatfs copied to clipboard

Physical disks in windows

Open 1Dragoon opened this issue 2 years ago • 1 comments

Is it possible to format physical disks in Windows? Believe it or not, there is a use case here. Windows by default only allows 32GB max partition sizes for FAT32, even though the technical limitation is somewhere closer to 2TB. I'm trying to create a tool to prepare larger thumb drives to use in a 3d printer that only takes MBR/FAT32 formatted USB drives, and the only other readily available solutions for Windows that I can see are non-libre applications.

I tried specifying a raw partition for format and I just get an OS error

use std::fs;

use fatfs::{format_volume, FormatVolumeOptions, FatType, StdIoWrapper};
use fscommon::BufStream;

fn main() {
    let options = FormatVolumeOptions::new().fat_type(FatType::Fat32);
    let vol = r"\\?\usbstor#disk&ven_mass&prod_storage_device&rev__#125c20100726&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}";
    let file = fs::OpenOptions::new().read(true).write(true).open(vol).unwrap();
    let buf_file = BufStream::new(file);
    let mut iowrapper = StdIoWrapper::from(buf_file);

    format_volume(&mut iowrapper, options).unwrap();
}

That results in this:

Io(Os { code: 1, kind: Uncategorized, message: "Incorrect function." })

FWIW I've never done low level disk IO before, so I'm not sure what I'm missing. I'm guessing that there needs to be special block level handling or ioctl but I assumed the StdIoWrapper was doing something to that effect.

1Dragoon avatar Jul 10 '22 06:07 1Dragoon

I have never done low level IO access on Windows before as well. I wonder when this error is returned (read, write, seek?). Also it would be worth checking if Rust std::fs::File is capable of writing in such a manner. If it is required to write whole sectors it would probably be possible to create a modified version of BufStream that respects it.

rafalh avatar Jul 10 '22 11:07 rafalh

I've experimented with accessing raw partitions on Windows and got it working for ESP partition on my disk. You can find example code in: https://github.com/rafalh/rust-fatfs/issues/88

rafalh avatar Jan 14 '24 02:01 rafalh