kernel/fs/vfs_v2/drivers/fat32/
driver.rs1use alloc::sync::Arc;
8
9use crate::{
10 device::block::BlockDevice,
11 fs::{
12 FileSystemDriver, FileSystemError, FileSystemErrorKind, FileSystemType,
13 params::FileSystemParams,
14 },
15};
16
17use super::{super::super::core::FileSystemOperations, Fat32FileSystem};
18
19pub struct Fat32Driver;
24
25impl FileSystemDriver for Fat32Driver {
26 fn name(&self) -> &'static str {
27 "fat32"
28 }
29
30 fn filesystem_type(&self) -> FileSystemType {
31 FileSystemType::Block
32 }
33
34 fn create(&self) -> Result<Arc<dyn FileSystemOperations>, FileSystemError> {
35 Err(FileSystemError::new(
37 FileSystemErrorKind::NotSupported,
38 "FAT32 filesystem requires a block device",
39 ))
40 }
41
42 fn create_from_block(
43 &self,
44 block_device: Arc<dyn BlockDevice>,
45 _block_size: usize,
46 ) -> Result<Arc<dyn FileSystemOperations>, FileSystemError> {
47 let fs = Fat32FileSystem::new(block_device)?;
49 Ok(fs as Arc<dyn FileSystemOperations>)
50 }
51
52 fn create_from_memory(
53 &self,
54 _memory_area: &crate::vm::vmem::MemoryArea,
55 ) -> Result<Arc<dyn FileSystemOperations>, FileSystemError> {
56 Err(FileSystemError::new(
58 FileSystemErrorKind::NotSupported,
59 "FAT32 filesystem does not support memory-based creation",
60 ))
61 }
62
63 fn create_from_option_string(
64 &self,
65 _options: &str,
66 ) -> Result<Arc<dyn FileSystemOperations>, FileSystemError> {
67 Err(FileSystemError::new(
69 FileSystemErrorKind::NotSupported,
70 "FAT32 filesystem requires a block device, not options",
71 ))
72 }
73
74 fn create_from_params(
75 &self,
76 _params: &dyn FileSystemParams,
77 ) -> Result<Arc<dyn FileSystemOperations>, FileSystemError> {
78 Err(FileSystemError::new(
81 FileSystemErrorKind::NotSupported,
82 "FAT32 filesystem parameter-based creation not implemented",
83 ))
84 }
85}
86
87#[cfg(test)]
88mod tests {
89 use alloc::vec;
90
91 use super::*;
92 use crate::device::block::mockblk::MockBlockDevice;
93
94 #[test_case]
95 fn test_fat32_driver_type() {
96 let driver = Fat32Driver;
97 assert_eq!(driver.name(), "fat32");
98 assert_eq!(driver.filesystem_type(), FileSystemType::Block);
99 }
100
101 #[test_case]
102 fn test_fat32_create_without_block_device_fails() {
103 let driver = Fat32Driver;
104 let result = driver.create();
105 assert!(result.is_err());
106
107 let result = driver.create_from_option_string("");
108 assert!(result.is_err());
109 }
110
111 #[test_case]
112 fn test_fat32_create_from_mock_block_device() {
113 let driver = Fat32Driver;
114
115 let mut boot_sector = vec![0u8; 512];
117
118 boot_sector[0] = 0xEB;
121 boot_sector[1] = 0x58;
122 boot_sector[2] = 0x90;
123
124 boot_sector[3..11].copy_from_slice(b"MSWIN4.1");
126
127 boot_sector[11] = 0x00;
129 boot_sector[12] = 0x02;
130
131 boot_sector[13] = 0x08;
133
134 boot_sector[14] = 0x20;
136 boot_sector[15] = 0x00;
137
138 boot_sector[16] = 0x02;
140
141 boot_sector[17] = 0x00;
143 boot_sector[18] = 0x00;
144
145 boot_sector[19] = 0x00;
147 boot_sector[20] = 0x00;
148
149 boot_sector[21] = 0xF8;
151
152 boot_sector[22] = 0x00;
154 boot_sector[23] = 0x00;
155
156 boot_sector[24] = 0x3F;
158 boot_sector[25] = 0x00;
159
160 boot_sector[26] = 0xFF;
162 boot_sector[27] = 0x00;
163
164 boot_sector[28] = 0x00;
166 boot_sector[29] = 0x00;
167 boot_sector[30] = 0x00;
168 boot_sector[31] = 0x00;
169
170 boot_sector[32] = 0x00;
172 boot_sector[33] = 0x00;
173 boot_sector[34] = 0x01;
174 boot_sector[35] = 0x00;
175
176 boot_sector[36] = 0x00;
178 boot_sector[37] = 0x02;
179 boot_sector[38] = 0x00;
180 boot_sector[39] = 0x00;
181
182 boot_sector[40] = 0x00;
184 boot_sector[41] = 0x00;
185
186 boot_sector[42] = 0x00;
188 boot_sector[43] = 0x00;
189
190 boot_sector[44] = 0x02;
192 boot_sector[45] = 0x00;
193 boot_sector[46] = 0x00;
194 boot_sector[47] = 0x00;
195
196 boot_sector[48] = 0x01;
198 boot_sector[49] = 0x00;
199
200 boot_sector[50] = 0x06;
202 boot_sector[51] = 0x00;
203
204 boot_sector[510] = 0x55;
206 boot_sector[511] = 0xAA;
207
208 let mut mock_device = MockBlockDevice::new("mock_fat32", 512, 65536);
210
211 let result = driver.create_from_block(Arc::new(mock_device), 512);
214
215 match result {
218 Ok(_fs) => {
219 }
221 Err(e) => {
222 assert!(
225 e.kind == FileSystemErrorKind::IoError
226 || e.kind == FileSystemErrorKind::InvalidData
227 );
228 }
229 }
230 }
231}