pub fn parse_volume<D, T>(
    controller: &mut Controller<D, T>,
    lba_start: BlockIdx,
    num_blocks: BlockCount,
) -> Result<VolumeType, Error<D::Error>>
where
    D: BlockDevice,
    T: TimeSource,
    D::Error: core::fmt::Debug,
{
    let mut blocks = [Block::new()];
    controller
        .block_device
        .read(&mut blocks, lba_start, "read_bpb")
        .map_err(Error::DeviceError)?;
    let block = &blocks[0];
    let bpb = Bpb::create_from_bytes(&block).map_err(Error::FormatError)?;
    match bpb.fat_type {
        FatType::Fat16 => {
            // FirstDataSector = BPB_ResvdSecCnt + (BPB_NumFATs * FATSz) + RootDirSectors;
            let root_dir_blocks = ((u32::from(bpb.root_entries_count()) * OnDiskDirEntry::LEN_U32)
                + (Block::LEN_U32 - 1))
                / Block::LEN_U32;
            let fat_start = BlockCount(u32::from(bpb.reserved_block_count()));
            let first_root_dir_block =
                fat_start + BlockCount(u32::from(bpb.num_fats()) * bpb.fat_size());
            let first_data_block = first_root_dir_block + BlockCount(root_dir_blocks);
            let mut volume = Fat16Volume {
                lba_start,
                num_blocks,
                name: VolumeName { data: [0u8; 11] },
                root_entries_count: bpb.root_entries_count(),
                blocks_per_cluster: bpb.blocks_per_cluster(),
                first_root_dir_block: (first_root_dir_block),
                first_data_block: (first_data_block),
                fat_start: BlockCount(u32::from(bpb.reserved_block_count())),
                free_clusters_count: None,
                next_free_cluster: None,
                cluster_count: bpb.total_clusters(),
            };
            volume.name.data[..].copy_from_slice(bpb.volume_label());
            Ok(VolumeType::Fat16(volume))
        }
        FatType::Fat32 => {
            // FirstDataSector = BPB_ResvdSecCnt + (BPB_NumFATs * FATSz);
            let first_data_block = u32::from(bpb.reserved_block_count())
                + (u32::from(bpb.num_fats()) * bpb.fat_size());

            // Safe to unwrap since this is a Fat32 Type
            let info_location = bpb.fs_info_block().unwrap();
            let mut info_blocks = [Block::new()];
            controller
                .block_device
                .read(
                    &mut info_blocks,
                    lba_start + info_location,
                    "read_info_sector",
                )
                .map_err(Error::DeviceError)?;
            let info_block = &info_blocks[0];
            let info_sector =
                InfoSector::create_from_bytes(&info_block).map_err(Error::FormatError)?;

            let mut volume = Fat32Volume {
                lba_start,
                num_blocks,
                name: VolumeName { data: [0u8; 11] },
                blocks_per_cluster: bpb.blocks_per_cluster(),
                first_data_block: BlockCount(first_data_block),
                fat_start: BlockCount(u32::from(bpb.reserved_block_count())),
                first_root_dir_cluster: Cluster(bpb.first_root_dir_cluster()),
                free_clusters_count: info_sector.free_clusters_count(),
                next_free_cluster: info_sector.next_free_cluster(),
                cluster_count: bpb.total_clusters(),
                info_location: lba_start + info_location,
            };
            volume.name.data[..].copy_from_slice(bpb.volume_label());
            Ok(VolumeType::Fat32(volume))
        }
    }
}
/// Load the boot parameter block from the start of the given partition and
/// determine if the partition contains a valid FAT16 or FAT32 file system.
Load the boot parameter block from the start of the given partition and
determine if the partition contains a valid FAT16 or FAT32 file system.
Load the boot parameter block from the start of the given partition and determine if the partition contains a valid FAT16 or FAT32 file system .
pub fn parse_volume<D, T>(
    controller: &mut Controller<D, T>,
    lba_start: BlockIdx,
    num_blocks: BlockCount,
) -> Result<VolumeType, Error<D::Error>>
where
    D: BlockDevice,
    T: TimeSource,
    D::Error: core::fmt::Debug,
{
    let mut blocks = [Block::new()];
    controller
        .block_device
        .read(&mut blocks, lba_start, "read_bpb")
        .map_err(Error::DeviceError)?;
    let block = &blocks[0];
    let bpb = Bpb::create_from_bytes(&block).map_err(Error::FormatError)?;
    match bpb.fat_type {
        FatType::Fat16 => {
            // FirstDataSector = BPB_ResvdSecCnt + (BPB_NumFATs * FATSz) + RootDirSectors;
            let root_dir_blocks = ((u32::from(bpb.root_entries_count()) * OnDiskDirEntry::LEN_U32)
                + (Block::LEN_U32 - 1))
                / Block::LEN_U32;
            let fat_start = BlockCount(u32::from(bpb.reserved_block_count()));
            let first_root_dir_block =
                fat_start + BlockCount(u32::from(bpb.num_fats()) * bpb.fat_size());
            let first_data_block = first_root_dir_block + BlockCount(root_dir_blocks);
            let mut volume = Fat16Volume {
                lba_start,
                num_blocks,
                name: VolumeName { data: [0u8; 11] },
                root_entries_count: bpb.root_entries_count(),
                blocks_per_cluster: bpb.blocks_per_cluster(),
                first_root_dir_block: (first_root_dir_block),
                first_data_block: (first_data_block),
                fat_start: BlockCount(u32::from(bpb.reserved_block_count())),
                free_clusters_count: None,
                next_free_cluster: None,
                cluster_count: bpb.total_clusters(),
            };
            volume.name.data[..].copy_from_slice(bpb.volume_label());
            Ok(VolumeType::Fat16(volume))
        }
        FatType::Fat32 => {
            // FirstDataSector = BPB_ResvdSecCnt + (BPB_NumFATs * FATSz);
            let first_data_block = u32::from(bpb.reserved_block_count())
                + (u32::from(bpb.num_fats()) * bpb.fat_size());
            // Safe to unwrap since this is a Fat32 Type
            let info_location = bpb.fs_info_block().unwrap();
            let mut info_blocks = [Block::new()];
            controller
                .block_device
                .read(
                    &mut info_blocks,
                    lba_start + info_location,
                    "read_info_sector",
                )
                .map_err(Error::DeviceError)?;
            let info_block = &info_blocks[0];
            let info_sector =
                InfoSector::create_from_bytes(&info_block).map_err(Error::FormatError)?;
            let mut volume = Fat32Volume {
                lba_start,
                num_blocks,
                name: VolumeName { data: [0u8; 11] },
                blocks_per_cluster: bpb.blocks_per_cluster(),
                first_data_block: BlockCount(first_data_block),
                fat_start: BlockCount(u32::from(bpb.reserved_block_count())),
                first_root_dir_cluster: Cluster(bpb.first_root_dir_cluster()),
                free_clusters_count: info_sector.free_clusters_count(),
                next_free_cluster: info_sector.next_free_cluster(),
                cluster_count: bpb.total_clusters(),
                info_location: lba_start + info_location,
            };
            volume.name.data[..].copy_from_slice(bpb.volume_label());
            Ok(VolumeType::Fat32(volume))
        }
    }
}
pub fn parse_volume < D , T > ( controller : & mut Controller < D , T > , lba_start : BlockIdx , num_blocks : BlockCount , ) -> Result < VolumeType , Error < D :: Error > > where D : BlockDevice , T : TimeSource , D :: Error : core :: fmt :: Debug , { let mut blocks = [ Block :: new ( ) ] ; controller . block_device . read ( & mut blocks , lba_start , " " ) . map_err ( Error :: DeviceError ) ? ; let block = & blocks [ 0 ] ; let bpb = Bpb :: create_from_bytes ( & block ) . map_err ( Error :: FormatError ) ? ; match bpb . fat_type { FatType :: Fat16 => { // FirstDataSector = BPB_ResvdSecCnt + (BPB_NumFATs * FATSz) + RootDirSectors; let root_dir_blocks = ( ( u32 :: from ( bpb . root_entries_count ( ) ) * OnDiskDirEntry :: LEN_U32 ) + ( Block :: LEN_U32 - 1 ) ) / Block :: LEN_U32 ; let fat_start = BlockCount ( u32 :: from ( bpb . reserved_block_count ( ) ) ) ; let first_root_dir_block = fat_start + BlockCount ( u32 :: from ( bpb . num_fats ( ) ) * bpb . fat_size ( ) ) ; let first_data_block = first_root_dir_block + BlockCount ( root_dir_blocks ) ; let mut volume = Fat16Volume { lba_start , num_blocks , name : VolumeName { data : [ 0u8 ; 11 ] } , root_entries_count : bpb . root_entries_count ( ) , blocks_per_cluster : bpb . blocks_per_cluster ( ) , first_root_dir_block : ( first_root_dir_block ) , first_data_block : ( first_data_block ) , fat_start : BlockCount ( u32 :: from ( bpb . reserved_block_count ( ) ) ) , free_clusters_count : None , next_free_cluster : None , cluster_count : bpb . total_clusters ( ) , } ; volume . name . data [ .. ] . copy_from_slice ( bpb . volume_label ( ) ) ; Ok ( VolumeType :: Fat16 ( volume ) ) } FatType :: Fat32 => { // FirstDataSector = BPB_ResvdSecCnt + (BPB_NumFATs * FATSz); let first_data_block = u32 :: from ( bpb . reserved_block_count ( ) ) + ( u32 :: from ( bpb . num_fats ( ) ) * bpb . fat_size ( ) ) ; // Safe to unwrap since this is a Fat32 Type let info_location = bpb . fs_info_block ( ) . unwrap ( ) ; let mut info_blocks = [ Block :: new ( ) ] ; controller . block_device . read ( & mut info_blocks , lba_start + info_location , " " , ) . map_err ( Error :: DeviceError ) ? ; let info_block = & info_blocks [ 0 ] ; let info_sector = InfoSector :: create_from_bytes ( & info_block ) . map_err ( Error :: FormatError ) ? ; let mut volume = Fat32Volume { lba_start , num_blocks , name : VolumeName { data : [ 0u8 ; 11 ] } , blocks_per_cluster : bpb . blocks_per_cluster ( ) , first_data_block : BlockCount ( first_data_block ) , fat_start : BlockCount ( u32 :: from ( bpb . reserved_block_count ( ) ) ) , first_root_dir_cluster : Cluster ( bpb . first_root_dir_cluster ( ) ) , free_clusters_count : info_sector . free_clusters_count ( ) , next_free_cluster : info_sector . next_free_cluster ( ) , cluster_count : bpb . total_clusters ( ) , info_location : lba_start + info_location , } ; volume . name . data [ .. ] . copy_from_slice ( bpb . volume_label ( ) ) ; Ok ( VolumeType :: Fat32 ( volume ) ) } } }
Load the boot parameter block from the start of the given partition and
determine if the partition contains a valid FAT16 or FAT32 file system.
Load the boot parameter block from the start of the given partition and determine if the partition contains a valid FAT16 or FAT32 file system .
controller
&mut Controller<D, T>
lba_start
BlockIdx
num_blocks
BlockCount
controller
&mut Controller<D, T>
lba_start
BlockIdx
num_blocks
BlockCount