fat_fs_types 0.3.1

low-level definitions for FAT12/16/32 and exFAT file systems
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! exFAT.

pub use crate::fat::is_valid_long_char as is_valid_char;

use core::hash::Hasher;

use bitflags::bitflags;
#[cfg(feature = "bytemuck")]
use bytemuck::{Pod, Zeroable};
#[cfg(feature = "zerocopy")]
use zerocopy::{AsBytes, FromBytes, FromZeroes, Unaligned};

use crate::{ClusterIdx, Guid, U16Le, U32Le, U64Le};

pub const MAX_CLUSTER_SIZE: usize = 0x0200_0000;
/// Maximum FAT length.
///
/// **Note:** This is not the same as the cluster count; this is the number of
/// entries in a FAT and not the number of clusters on the heap.
/// It can also never indicate the maximum valid cluster index for exFAT file
/// systems. This value is one larger than the maximum valid cluster index could
/// ever be.
pub const MAX_FAT_LEN: ClusterIdx = 0xFFFF_FFF7;
pub const MAX_NAME_LEN: usize = 0xFF;
/// Special value in the upcase table that indicates that the next n values
/// are all identity mappings, where n is the next 2-byte word.
///
/// Note that this value can also show up at the end of the table and there
/// it should be interpreted literally as `0xFFFF`.
pub const UPCASE_TABLE_MASS_IDENTITY_MARK: u16 = 0xFFFF;

pub const FAT_ENTRY0: ClusterIdx = 0xFFFF_FFF8;
pub const FAT_ENTRY1: ClusterIdx = 0xFFFF_FFFF;
pub const BAD_CLUSTER_MARK: ClusterIdx = 0xFFFF_FFF7;
pub const END_OF_ALLOC_MARK: ClusterIdx = 0xFFFF_FFFF;

#[derive(Clone)]
#[cfg_attr(feature = "bytemuck", derive(Copy, Pod, Zeroable))]
#[cfg_attr(
	feature = "zerocopy",
	derive(AsBytes, FromZeroes, FromBytes, Unaligned)
)]
#[repr(C)]
pub struct BootRecord {
	pub jump_boot: [u8; 3],
	pub fs_name: [u8; 8],
	pub _zero: [u8; 0x35],
	pub partition_lba_offset: U64Le,
	pub volume_lba_size: U64Le,
	pub fat_base_lba: U32Le,
	pub fat_lba_size: U32Le,
	pub heap_base_lba: U32Le,
	pub heap_lba_size: U32Le,
	pub first_root_cluster: U32Le,
	pub volume_serial_number: U32Le,
	pub version: [u8; 2],
	pub flags: VolumeFlags,
	pub blk_size_log2: u8,
	pub cluster_lba_size_log2: u8,
	pub fat_count: u8,
	pub drive_num: u8,
	pub percent_occupied: u8,
	pub _reserved: [u8; 7],
	pub boot_code: [u8; 0x0186],
	pub signature: [u8; 2],
}

impl BootRecord {
	// offsets of struct fields
	const FLAGS_OFFSET: usize = 0x6A;
	const BLK_SIZE_LOG2_OFFSET: usize = 0x6C;
	const PERCENT_OCCUPIED_OFFSET: usize = 0x70;
	const RESERVED_OFFSET: usize = 0x71;

	pub const JUMP_BOOT: [u8; 3] = [0xEB, 0x76, 0x90];
	pub const FS_NAME: [u8; 8] = *b"EXFAT   ";
	pub const SIGNATURE: [u8; 2] = 0xAA55_u16.to_le_bytes();
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "bytemuck", derive(Pod, Zeroable))]
#[cfg_attr(
	feature = "zerocopy",
	derive(AsBytes, FromZeroes, FromBytes, Unaligned)
)]
#[repr(C, packed)]
pub struct VolumeFlags(u16);

bitflags! {
	impl VolumeFlags: u16 {
		const FAT1_ACTIVE = 1_u16.to_le();
		const DIRTY = 2_u16.to_le();
		const IO_ERROR = 4_u16.to_le();
		const CLEAR_BEFORE_WRITE = 8_u16.to_le();
	}
}

#[derive(Clone, Default)]
#[cfg_attr(feature = "bytemuck", derive(Copy, Pod, Zeroable))]
#[cfg_attr(
	feature = "zerocopy",
	derive(AsBytes, FromZeroes, FromBytes, Unaligned)
)]
#[repr(C)]
pub struct GenericParams {
	pub guid: Guid,
	pub _custom: [u8; 0x20],
}

impl GenericParams {
	pub const NULL: Self = Self {
		guid: [0; 0x10],
		_custom: [0; 0x20],
	};
}

#[derive(Clone)]
#[cfg_attr(feature = "bytemuck", derive(Copy, Pod, Zeroable))]
#[cfg_attr(
	feature = "zerocopy",
	derive(AsBytes, FromZeroes, FromBytes, Unaligned)
)]
#[repr(C)]
pub struct FlashParams {
	pub guid: Guid,
	pub erase_blk_size: U32Le,
	pub page_size: U32Le,
	pub spare_blk_count: U32Le,
	pub random_access_time_ns: U32Le,
	pub programming_time_ns: U32Le,
	pub read_cycle_ns: U32Le,
	pub write_cycle_ns: U32Le,
	pub _reserved: [u8; 4],
}

impl FlashParams {
	pub const GUID: Guid = [
		0x46, 0x7E, 0x0C, 0x0A, 0x99, 0x33, 0x21, 0x40, 0x90, 0xC8, 0xFA, 0x6D, 0x38, 0x9C, 0x4B,
		0xA2,
	];
}

#[derive(Clone, Debug, Default)]
pub struct Checksum(u32);

impl Checksum {
	pub const NEW: Self = Self(0);

	/// Writes one or more blocks from the boot region into this hasher, the
	/// first of which being the main boot block.
	///
	/// `buf` has too be a multiple of one block in size.
	pub fn write_main_boot_block(&mut self, buf: &[u8]) {
		self.write(&buf[..BootRecord::FLAGS_OFFSET]);
		self.write(&buf[BootRecord::BLK_SIZE_LOG2_OFFSET..BootRecord::PERCENT_OCCUPIED_OFFSET]);
		self.write(&buf[BootRecord::RESERVED_OFFSET..]);
	}

	/// Return the computed checksum.
	#[inline]
	pub fn finalize(self) -> u32 {
		self.0
	}
}

impl Hasher for Checksum {
	#[inline]
	fn finish(&self) -> u64 {
		self.0 as u64
	}

	fn write(&mut self, bytes: &[u8]) {
		self.0 = bytes.iter().fold(self.0, |sum, &byte| {
			u32::wrapping_add(sum.rotate_right(1), byte as u32)
		});
	}
}

/// Contents of a directory.
pub mod dir {
	use super::*;

	#[derive(Clone)]
	#[cfg_attr(feature = "bytemuck", derive(Copy, Pod, Zeroable))]
	#[cfg_attr(
		feature = "zerocopy",
		derive(AsBytes, FromZeroes, FromBytes, Unaligned)
	)]
	#[repr(C)]
	pub struct GenericEntry {
		pub entry_type: u8,
		pub _custom: [u8; 0x13],
		pub first_cluster: U32Le,
		pub data_capacity: U64Le,
	}

	impl GenericEntry {
		pub const TYPE_END: u8 = 0;
		pub const TYPE_BENIGN_FLAG: u8 = 0x20;
		pub const TYPE_SECONDARY_FLAG: u8 = 0x40;
		pub const TYPE_OCCUPIED_FLAG: u8 = 0x80;
		pub const TYPE_FLAGS: u8 = 0xE0;
	}

	#[derive(Clone)]
	#[cfg_attr(feature = "bytemuck", derive(Copy, Pod, Zeroable))]
	#[cfg_attr(
		feature = "zerocopy",
		derive(AsBytes, FromZeroes, FromBytes, Unaligned)
	)]
	#[repr(C)]
	pub struct PrimaryEntry {
		pub entry_type: u8,
		pub secondary_count: u8,
		pub set_checksum: U16Le,
		pub flags: PrimaryEntryFlags,
		pub _custom: [u8; 0xE],
		pub first_cluster: U32Le,
		pub data_capacity: U64Le,
	}

	#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
	#[cfg_attr(feature = "bytemuck", derive(Pod, Zeroable))]
	#[cfg_attr(
		feature = "zerocopy",
		derive(AsBytes, FromZeroes, FromBytes, Unaligned)
	)]
	#[repr(C, packed)]
	pub struct PrimaryEntryFlags(u16);

	bitflags! {
		impl PrimaryEntryFlags: u16 {
			const ALLOC_POSSIBLE = 1_u16.to_le();
			const NO_CLUSTER_CHAIN = 2_u16.to_le();
		}
	}

	#[derive(Clone)]
	#[cfg_attr(feature = "bytemuck", derive(Copy, Pod, Zeroable))]
	#[cfg_attr(
		feature = "zerocopy",
		derive(AsBytes, FromZeroes, FromBytes, Unaligned)
	)]
	#[repr(C)]
	pub struct AllocBitmapEntry {
		pub entry_type: u8,
		pub flags: u8,
		pub _reserved: [u8; 0x12],
		pub first_cluster: U32Le,
		pub data_capacity: U64Le,
	}

	impl AllocBitmapEntry {
		pub const TYPE: u8 = 0x81;
	}

	#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
	#[cfg_attr(feature = "bytemuck", derive(Pod, Zeroable))]
	#[cfg_attr(
		feature = "zerocopy",
		derive(AsBytes, FromZeroes, FromBytes, Unaligned)
	)]
	#[repr(transparent)]
	pub struct AllocBitmapEntryFlags(u8);

	bitflags! {
		impl AllocBitmapEntryFlags: u8 {
			const SECOND_FAT = 1;
		}
	}

	#[derive(Clone)]
	#[cfg_attr(feature = "bytemuck", derive(Copy, Pod, Zeroable))]
	#[cfg_attr(
		feature = "zerocopy",
		derive(AsBytes, FromZeroes, FromBytes, Unaligned)
	)]
	#[repr(C)]
	pub struct UpcaseTableEntry {
		pub entry_type: u8,
		pub _reserved0: [u8; 3],
		pub table_checksum: U32Le,
		pub _reserved1: [u8; 0xC],
		pub first_cluster: U32Le,
		pub data_capacity: U64Le,
	}

	impl UpcaseTableEntry {
		pub const TYPE: u8 = 0x82;
	}

	#[derive(Clone)]
	#[cfg_attr(feature = "bytemuck", derive(Copy, Pod, Zeroable))]
	#[cfg_attr(
		feature = "zerocopy",
		derive(AsBytes, FromZeroes, FromBytes, Unaligned)
	)]
	#[repr(C)]
	pub struct VolumeLabelEntry {
		pub entry_type: u8,
		pub len: u8,
		pub label: [u8; 0x16],
		pub first_cluster: U32Le,
		pub data_capacity: U64Le,
	}

	impl VolumeLabelEntry {
		pub const TYPE: u8 = 0x83;
	}

	#[derive(Clone)]
	#[cfg_attr(feature = "bytemuck", derive(Copy, Pod, Zeroable))]
	#[cfg_attr(
		feature = "zerocopy",
		derive(AsBytes, FromZeroes, FromBytes, Unaligned)
	)]
	#[repr(C)]
	pub struct ChildEntry {
		pub entry_type: u8,
		pub secondary_count: u8,
		pub set_checksum: U16Le,
		pub attributes: Attributes,
		pub _reserved0: [u8; 3],
		pub create_time: U16Le,
		pub create_date: U16Le,
		pub write_time: U16Le,
		pub write_date: U16Le,
		pub access_time: U16Le,
		pub access_date: U16Le,
		pub create_time_10ms: u8,
		pub write_time_10ms: u8,
		pub create_utc_offset: u8,
		pub write_utc_offset: u8,
		pub access_utc_offset: u8,
		pub _reserved1: [u8; 7],
	}

	impl ChildEntry {
		pub const TYPE: u8 = 0x85;
	}

	/// Attributes of a file.
	///
	/// The behavior of [`Attributes::all`]/[`Attributes::from_bits_truncate`] is
	/// stable.
	#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
	#[cfg_attr(feature = "bytemuck", derive(Pod, Zeroable))]
	#[cfg_attr(
		feature = "zerocopy",
		derive(AsBytes, FromZeroes, FromBytes, Unaligned)
	)]
	#[repr(C, packed)]
	pub struct Attributes(u16);

	bitflags! {
		impl Attributes: u16 {
			/// Indicates that writes to the file should fail.
			const READ_ONLY = 1_u16.to_le();
			/// Indicates that normal directory listings should not show this file.
			const HIDDEN = 2_u16.to_le();
			/// Indicates that this is an operating system file.
			const SYSTEM = 4_u16.to_le();
			/// Indicates that this file is actually a directory.
			const DIR = 0x10_u16.to_le();
			/// This attribute supports backup utilities.
			const ARCHIVE = 0x20_u16.to_le();
		}
	}

	#[derive(Clone)]
	#[cfg_attr(feature = "bytemuck", derive(Copy, Pod, Zeroable))]
	#[cfg_attr(
		feature = "zerocopy",
		derive(AsBytes, FromZeroes, FromBytes, Unaligned)
	)]
	#[repr(C)]
	pub struct VolumeGuidEntry {
		pub entry_type: u8,
		pub secondary_count: u8,
		pub set_checksum: U16Le,
		pub flags: PrimaryEntryFlags,
		pub guid: Guid,
		pub _reserved1: [u8; 0xA],
	}

	impl VolumeGuidEntry {
		pub const TYPE: u8 = 0xA0;
	}

	#[derive(Clone)]
	#[cfg_attr(feature = "bytemuck", derive(Copy, Pod, Zeroable))]
	#[cfg_attr(
		feature = "zerocopy",
		derive(AsBytes, FromZeroes, FromBytes, Unaligned)
	)]
	#[repr(C)]
	pub struct SecondaryEntry {
		pub entry_type: u8,
		pub flags: SecondaryEntryFlags,
		pub _custom: [u8; 0x12],
		pub first_cluster: U32Le,
		pub data_capacity: U64Le,
	}

	#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
	#[cfg_attr(feature = "bytemuck", derive(Pod, Zeroable))]
	#[cfg_attr(
		feature = "zerocopy",
		derive(AsBytes, FromZeroes, FromBytes, Unaligned)
	)]
	#[repr(transparent)]
	pub struct SecondaryEntryFlags(u8);

	bitflags! {
		impl SecondaryEntryFlags: u8 {
			const ALLOC_POSSIBLE = 1;
			const NO_CLUSTER_CHAIN = 2;
		}
	}

	#[derive(Clone)]
	#[cfg_attr(feature = "bytemuck", derive(Copy, Pod, Zeroable))]
	#[cfg_attr(
		feature = "zerocopy",
		derive(AsBytes, FromZeroes, FromBytes, Unaligned)
	)]
	#[repr(C)]
	pub struct StreamExtEntry {
		pub entry_type: u8,
		pub flags: SecondaryEntryFlags,
		pub _reserved0: u8,
		pub name_len: u8,
		pub name_hash: U16Le,
		pub _reserved1: [u8; 2],
		pub data_size: U64Le,
		pub _reserved2: [u8; 4],
		pub first_cluster: U32Le,
		pub data_capacity: U64Le,
	}

	impl StreamExtEntry {
		pub const TYPE: u8 = 0xC0;
	}

	#[derive(Clone)]
	#[cfg_attr(feature = "bytemuck", derive(Copy, Pod, Zeroable))]
	#[cfg_attr(
		feature = "zerocopy",
		derive(AsBytes, FromZeroes, FromBytes, Unaligned)
	)]
	#[repr(C)]
	pub struct NameEntry {
		pub entry_type: u8,
		pub flags: SecondaryEntryFlags,
		pub buf: [u8; NameEntry::CAPACITY * 2],
	}

	impl NameEntry {
		pub const TYPE: u8 = 0xC1;
		pub const CAPACITY: usize = 0xF;
	}

	#[derive(Clone)]
	#[cfg_attr(feature = "bytemuck", derive(Copy, Pod, Zeroable))]
	#[cfg_attr(
		feature = "zerocopy",
		derive(AsBytes, FromZeroes, FromBytes, Unaligned)
	)]
	#[repr(C)]
	pub struct VendorExtEntry {
		pub entry_type: u8,
		pub flags: SecondaryEntryFlags,
		pub vendor_guid: Guid,
		pub _custom: [u8; 0xE],
	}

	impl VendorExtEntry {
		pub const TYPE: u8 = 0xE0;
	}

	#[derive(Clone)]
	#[cfg_attr(feature = "bytemuck", derive(Copy, Pod, Zeroable))]
	#[cfg_attr(
		feature = "zerocopy",
		derive(AsBytes, FromZeroes, FromBytes, Unaligned)
	)]
	#[repr(C)]
	pub struct VendorAllocEntry {
		pub entry_type: u8,
		pub flags: SecondaryEntryFlags,
		pub vendor_guid: Guid,
		pub _custom: [u8; 2],
		pub first_cluster: U32Le,
		pub data_capacity: U64Le,
	}

	impl VendorAllocEntry {
		pub const TYPE: u8 = 0xE1;
	}
}

/// Encodes `(hours, minutes, seconds)` into a packed native-endian exFAT access
/// time.
///
/// The result will not represent a valid time if the input was invalid.
///
/// # Examples
/// ```
/// # use fat_fs_types::exfat::pack_access_time;
/// assert_eq!(pack_access_time((17, 29, 3)), 0x8BA1);
/// ```
#[inline]
pub const fn pack_access_time(hours_mins_secs: (u8, u8, u8)) -> u16 {
	crate::fat::pack_write_time(hours_mins_secs)
}

/// Decodes a packed native-endian exFAT access time into
/// `(hours, minutes, seconds)`.
///
/// The result might not be a valid time.
///
/// # Examples
/// ```
/// # use fat_fs_types::exfat::unpack_access_time;
/// assert_eq!(unpack_access_time(0x8BA1), (17, 29, 2));
/// ```
#[inline]
pub const fn unpack_access_time(packed: u16) -> (u8, u8, u8) {
	crate::fat::unpack_write_time(packed)
}