|
| 1 | +// SPDX-FileCopyrightText: Copyright © 2025 Serpent OS Developers |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MPL-2.0 |
| 4 | + |
| 5 | +//! Fat32 |
| 6 | +//! |
| 7 | +//! This module implements parsing and access to the FAT32 filesystem boot sector, |
| 8 | +//! which contains critical metadata about the filesystem including: |
| 9 | +//! - Version information |
| 10 | +//! - Volume name and UUID |
| 11 | +//! - Encryption settings |
| 12 | +
|
| 13 | +use std::io; |
| 14 | + |
| 15 | +use crate::{Detection, Error}; |
| 16 | +use zerocopy::*; |
| 17 | + |
| 18 | +/// Starting position of superblock in bytes |
| 19 | +pub const START_POSITION: u64 = 0; |
| 20 | + |
| 21 | +const MAGIC: [u8; 2] = [0x55, 0xAA]; |
| 22 | + |
| 23 | +#[repr(C, packed)] |
| 24 | +#[derive(FromBytes, Unaligned, Debug)] |
| 25 | +pub struct Fat { |
| 26 | + /// Boot strap short or near jump |
| 27 | + pub ignored: [u8; 3], |
| 28 | + /// Name - can be used to special case partition manager volumes |
| 29 | + pub system_id: [u8; 8], |
| 30 | + /// Bytes per logical sector |
| 31 | + pub sector_size: U16<LittleEndian>, |
| 32 | + /// Sectors per cluster |
| 33 | + pub sec_per_clus: u8, |
| 34 | + /// Reserved sectors |
| 35 | + pub _reserved: U16<LittleEndian>, |
| 36 | + /// Number of FATs |
| 37 | + pub fats: u8, |
| 38 | + /// Root directory entries |
| 39 | + pub dir_entries: U16<LittleEndian>, |
| 40 | + /// Number of sectors |
| 41 | + pub sectors: U16<LittleEndian>, |
| 42 | + /// Media code |
| 43 | + pub media: u8, |
| 44 | + /// Sectors/FAT |
| 45 | + pub fat_length: U16<LittleEndian>, |
| 46 | + /// Sectors per track |
| 47 | + pub secs_track: U16<LittleEndian>, |
| 48 | + /// Number of heads |
| 49 | + pub heads: U16<LittleEndian>, |
| 50 | + /// Hidden sectors (unused) |
| 51 | + pub hidden: U32<LittleEndian>, |
| 52 | + /// Number of sectors (if sectors == 0) |
| 53 | + pub total_sect: U32<LittleEndian>, |
| 54 | + |
| 55 | + // Shared memory region for FAT16 and FAT32 |
| 56 | + // Best way is to use a union with zerocopy, however that requires having to use `--cfg zerocopy_derive_union_into_bytes` https://github.com/google/zerocopy/issues/1792` |
| 57 | + pub shared: [u8; 54], // The size of the union fields in bytes |
| 58 | +} |
| 59 | + |
| 60 | +#[derive(FromBytes, Unaligned)] |
| 61 | +#[repr(C, packed)] |
| 62 | +pub struct Fat16And32Fields { |
| 63 | + // Physical drive number |
| 64 | + pub drive_number: u8, |
| 65 | + // Mount state |
| 66 | + pub state: u8, |
| 67 | + // Extended boot signature |
| 68 | + pub signature: u8, |
| 69 | + // Volume ID |
| 70 | + pub vol_id: U32<LittleEndian>, |
| 71 | + // Volume label |
| 72 | + pub vol_label: [u8; 11], |
| 73 | + // File system type |
| 74 | + pub fs_type: [u8; 8], |
| 75 | +} |
| 76 | + |
| 77 | +#[derive(FromBytes, Unaligned)] |
| 78 | +#[repr(C, packed)] |
| 79 | +pub struct Fat16Fields { |
| 80 | + pub common: Fat16And32Fields, |
| 81 | +} |
| 82 | + |
| 83 | +impl Fat16Fields {} |
| 84 | + |
| 85 | +#[derive(FromBytes, Unaligned)] |
| 86 | +#[repr(C, packed)] |
| 87 | +pub struct Fat32Fields { |
| 88 | + // FAT32-specific fields |
| 89 | + /// Sectors/FAT |
| 90 | + pub fat32_length: U32<LittleEndian>, |
| 91 | + /// FAT mirroring flags |
| 92 | + pub fat32_flags: U16<LittleEndian>, |
| 93 | + /// Major, minor filesystem version |
| 94 | + pub fat32_version: [u8; 2], |
| 95 | + /// First cluster in root directory |
| 96 | + pub root_cluster: U32<LittleEndian>, |
| 97 | + /// Filesystem info sector |
| 98 | + pub info_sector: U16<LittleEndian>, |
| 99 | + /// Backup boot sector |
| 100 | + pub backup_boot: U16<LittleEndian>, |
| 101 | + /// Unused |
| 102 | + pub reserved2: [U16<LittleEndian>; 6], |
| 103 | + |
| 104 | + pub common: Fat16And32Fields, |
| 105 | +} |
| 106 | + |
| 107 | +impl Detection for Fat { |
| 108 | + type Magic = [u8; 2]; |
| 109 | + |
| 110 | + const OFFSET: u64 = START_POSITION; |
| 111 | + |
| 112 | + const MAGIC_OFFSET: u64 = 0x1FE; |
| 113 | + |
| 114 | + const SIZE: usize = std::mem::size_of::<Fat>(); |
| 115 | + |
| 116 | + fn is_valid_magic(magic: &Self::Magic) -> bool { |
| 117 | + *magic == MAGIC |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +pub enum FatType { |
| 122 | + Fat16, |
| 123 | + Fat32, |
| 124 | +} |
| 125 | + |
| 126 | +impl Fat { |
| 127 | + pub fn fat_type(&self) -> Result<FatType, Error> { |
| 128 | + // this is how the linux kernel does it in https://github.com/torvalds/linux/blob/master/fs/fat/inode.c |
| 129 | + if self.fat_length == 0 && self.fat32()?.fat32_length != 0 { |
| 130 | + Ok(FatType::Fat32) |
| 131 | + } else { |
| 132 | + Ok(FatType::Fat16) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /// Returns the filesystem id |
| 137 | + pub fn uuid(&self) -> Result<String, Error> { |
| 138 | + match self.fat_type()? { |
| 139 | + FatType::Fat16 => vol_id(self.fat16()?.common.vol_id), |
| 140 | + FatType::Fat32 => vol_id(self.fat32()?.common.vol_id), |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /// Returns the volume label |
| 145 | + pub fn label(&self) -> Result<String, Error> { |
| 146 | + match self.fat_type()? { |
| 147 | + FatType::Fat16 => vol_label(&self.fat16()?.common.vol_label), |
| 148 | + FatType::Fat32 => vol_label(&self.fat32()?.common.vol_label), |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + fn fat16(&self) -> Result<Fat16Fields, Error> { |
| 153 | + Ok(Fat16Fields::read_from_bytes(&self.shared[..size_of::<Fat16Fields>()]) |
| 154 | + .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "Error Reading FAT16 Superblock"))?) |
| 155 | + } |
| 156 | + |
| 157 | + fn fat32(&self) -> Result<Fat32Fields, Error> { |
| 158 | + Ok(Fat32Fields::read_from_bytes(&self.shared) |
| 159 | + .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "Error Reading FAT32 Superblock"))?) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +fn vol_label(vol_label: &[u8; 11]) -> Result<String, Error> { |
| 164 | + Ok(String::from_utf8_lossy(vol_label).trim_end_matches(' ').to_string()) |
| 165 | +} |
| 166 | + |
| 167 | +fn vol_id(vol_id: U32<LittleEndian>) -> Result<String, Error> { |
| 168 | + Ok(format!("{:04X}-{:04X}", vol_id >> 16, vol_id & 0xFFFF)) |
| 169 | +} |
0 commit comments