sdmmc-async 0.2.0

An async, no_std, no-alloc SD card stack: an SPI-mode card driver and FAT32 and exFAT (read-only, or writable with the `write` feature), over embedded-hal-async.
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
//! Writing exFAT: the allocation bitmap, FAT chains, entry sets and file data.
//!
//! Writes go straight to the device. A new cluster is taken in the bitmap (and the FAT) first, then the data goes into
//! it, then the directory entry set that makes the change visible is written. An entry set carries a checksum, so a set cut off half way is ignored by every reader rather than half
//! believed.

use super::{Directory, Volume, ATTR_DIRECTORY, ENTRY_FILE, ENTRY_NAME, ENTRY_STREAM};
use crate::name::{check_name, MAX_NAME};
use crate::time::Timestamp;
use crate::{BlockDevice, BlockWrite, Error, File, BLOCK_SIZE};

const EXFAT_END: u32 = 0xffff_ffff;
const ATTR_ARCHIVE: u16 = 0x20;
const ENTRY_BITMAP: u8 = 0x81;
const ENTRY_UPCASE: u8 = 0x82;
/// The stream extension's flags.
const FLAG_ALLOCATION_POSSIBLE: u8 = 0x01;
const FLAG_NO_FAT_CHAIN: u8 = 0x02;
/// In the boot sector's volume flags.
const VOLUME_DIRTY: u16 = 0x0002;
const FLAGS_OFFSET: usize = 106;

const MAX_NAME_ENTRIES: usize = MAX_NAME.div_ceil(15);
/// The file entry, the stream extension and the name entries.
const MAX_SET: usize = 2 + MAX_NAME_ENTRIES;

/// Where an entry sits in a directory.
#[derive(Clone, Copy, Debug)]
pub(crate) struct Pos {
    cluster: u32,
    sector_in_cluster: u32,
    entry_in_sector: u32,
}

impl Pos {
    pub(super) fn of(dir: &Directory) -> Self {
        Self {
            cluster: dir.cluster,
            sector_in_cluster: dir.sector_in_cluster,
            entry_in_sector: dir.entry_in_sector,
        }
    }
}

/// Where an entry set sits: its first entry, how many entries it has, and what is needed to walk on from it.
#[derive(Clone, Copy, Debug)]
pub(crate) struct EntryLoc {
    pub(super) pos: Pos,
    pub(super) count: u8,
    pub(super) dir_start: u32,
    pub(super) dir_no_fat_chain: bool,
}

impl EntryLoc {
    /// For entries nobody will update (read-only use).
    pub(super) const NONE: EntryLoc = EntryLoc {
        pos: Pos {
            cluster: 0,
            sector_in_cluster: 0,
            entry_in_sector: 0,
        },
        count: 0,
        dir_start: 0,
        dir_no_fat_chain: false,
    };

    /// A cursor on the first entry of the set that can step on through the directory's clusters.
    fn cursor(&self) -> Directory {
        let mut dir = Directory::new(self.dir_start, u64::MAX, self.dir_no_fat_chain);
        dir.cluster = self.pos.cluster;
        dir.sector_in_cluster = self.pos.sector_in_cluster;
        dir.entry_in_sector = self.pos.entry_in_sector;
        dir
    }
}

/// A file made of clusters that are not in the FAT chain... or of the clusters of a system file (bitmap, upcase table).
#[derive(Clone, Copy)]
struct Region {
    first: u32,
    length: u64,
}

pub(super) struct WriterState {
    /// The volume was marked in use by this mount.
    dirty: bool,
    /// It was already marked in use when this mount began (a previous writer did not finish): `sync` leaves it so.
    was_dirty: bool,
    /// The system files, found in the root directory at the first write.
    bitmap: Option<Region>,
    upcase: Option<Region>,
    /// Where the next search for a free cluster starts.
    next_free: u32,
}

impl WriterState {
    pub(super) fn new() -> Self {
        Self {
            dirty: false,
            was_dirty: false,
            bitmap: None,
            upcase: None,
            next_free: 2,
        }
    }
}

/// The checksum over an entry set, which skips the checksum field itself.
fn set_checksum(entries: &[[u8; 32]]) -> u16 {
    let mut checksum = 0u16;
    for (entry_index, entry) in entries.iter().enumerate() {
        for (index, &byte) in entry.iter().enumerate() {
            if entry_index == 0 && (index == 2 || index == 3) {
                continue;
            }
            checksum = checksum.rotate_right(1).wrapping_add(u16::from(byte));
        }
    }
    checksum
}

/// exFAT's 32-bit timestamp: date in the top half, time (two-second resolution) in the bottom.
fn exfat_timestamp(now: Timestamp) -> [u8; 4] {
    ((u32::from(now.fat_date()) << 16) | u32::from(now.fat_time())).to_le_bytes()
}

fn upcase_ascii(unit: u16) -> u16 {
    if (u16::from(b'a')..=u16::from(b'z')).contains(&unit) {
        unit - 32
    } else {
        unit
    }
}

/// What a new file or directory starts with.
struct Allocation {
    attributes: u16,
    first_cluster: u32,
    length: u64,
    flags: u8,
}

/// Everything about a new entry set.
struct NewSet<'a> {
    units: &'a [u16],
    name_hash: u16,
    attributes: u16,
    first_cluster: u32,
    length: u64,
    flags: u8,
    now: Timestamp,
}

fn build_set(new: &NewSet<'_>, out: &mut [[u8; 32]; MAX_SET]) -> usize {
    let name_entries = new.units.len().div_ceil(15);
    let count = 2 + name_entries;
    for entry in out.iter_mut().take(count) {
        *entry = [0; 32];
    }
    let stamp = exfat_timestamp(new.now);
    let file = &mut out[0];
    file[0] = ENTRY_FILE;
    file[1] = (1 + name_entries) as u8;
    file[4..6].copy_from_slice(&new.attributes.to_le_bytes());
    file[8..12].copy_from_slice(&stamp);
    file[12..16].copy_from_slice(&stamp);
    file[16..20].copy_from_slice(&stamp);
    let stream = &mut out[1];
    stream[0] = ENTRY_STREAM;
    stream[1] = new.flags;
    stream[3] = new.units.len() as u8;
    stream[4..6].copy_from_slice(&new.name_hash.to_le_bytes());
    stream[8..16].copy_from_slice(&new.length.to_le_bytes());
    stream[20..24].copy_from_slice(&new.first_cluster.to_le_bytes());
    stream[24..32].copy_from_slice(&new.length.to_le_bytes());
    for (index, chunk) in new.units.chunks(15).enumerate() {
        let entry = &mut out[2 + index];
        entry[0] = ENTRY_NAME;
        for (unit_index, unit) in chunk.iter().enumerate() {
            entry[2 + unit_index * 2..4 + unit_index * 2].copy_from_slice(&unit.to_le_bytes());
        }
    }
    let checksum = set_checksum(&out[..count]);
    out[0][2..4].copy_from_slice(&checksum.to_le_bytes());
    count
}

fn encode_name(name: &str, units: &mut [u16; MAX_NAME]) -> usize {
    let mut length = 0;
    for unit in name.encode_utf16() {
        units[length] = unit;
        length += 1;
    }
    length
}

impl<D: BlockDevice + BlockWrite> Volume<D> {
    fn cluster_bytes(&self) -> u64 {
        (BLOCK_SIZE as u64) << self.sectors_per_cluster_shift
    }

    async fn write_sector(
        &mut self,
        sector: u32,
        data: &[u8; BLOCK_SIZE],
    ) -> Result<(), Error<D::Error>> {
        let absolute = self
            .partition_start
            .checked_add(sector)
            .ok_or(Error::BadGeometry)?;
        self.device
            .write_block(absolute, data)
            .await
            .map_err(Error::Device)?;
        self.cache.sector = sector;
        self.cache.valid = true;
        self.cache.data = *data;
        Ok(())
    }

    async fn modify_sector<R>(
        &mut self,
        sector: u32,
        change: impl FnOnce(&mut [u8; BLOCK_SIZE]) -> R,
    ) -> Result<R, Error<D::Error>> {
        let mut data = *self.sector(sector).await?;
        let result = change(&mut data);
        self.write_sector(sector, &data).await?;
        Ok(result)
    }

    async fn set_fat_entry(&mut self, cluster: u32, value: u32) -> Result<(), Error<D::Error>> {
        let offset = cluster.checked_mul(4).ok_or(Error::InvalidCluster)?;
        let sector = self
            .fat_start
            .checked_add(offset / BLOCK_SIZE as u32)
            .ok_or(Error::InvalidCluster)?;
        let at = (offset % BLOCK_SIZE as u32) as usize;
        self.modify_sector(sector, |data| {
            data[at..at + 4].copy_from_slice(&value.to_le_bytes());
        })
        .await
    }

    /// Marks the volume in use before the first change, and finds the allocation bitmap and upcase table.
    async fn mark_dirty(&mut self) -> Result<(), Error<D::Error>> {
        if self.writer.dirty {
            return Ok(());
        }
        self.load_system_files().await?;
        let flags = {
            let data = self.sector(0).await?;
            u16::from_le_bytes([data[FLAGS_OFFSET], data[FLAGS_OFFSET + 1]])
        };
        self.writer.was_dirty = flags & VOLUME_DIRTY != 0;
        if !self.writer.was_dirty {
            self.modify_sector(0, |data| {
                let flags = flags | VOLUME_DIRTY;
                data[FLAGS_OFFSET..FLAGS_OFFSET + 2].copy_from_slice(&flags.to_le_bytes());
            })
            .await?;
        }
        self.writer.dirty = true;
        Ok(())
    }

    async fn load_system_files(&mut self) -> Result<(), Error<D::Error>> {
        let mut root = self.open_root();
        while let Some(raw) = self.next_raw(&mut root).await? {
            match raw[0] {
                0x00 => break,
                ENTRY_BITMAP => {
                    self.writer.bitmap = Some(Region {
                        first: u32::from_le_bytes([raw[20], raw[21], raw[22], raw[23]]),
                        length: u64::from_le_bytes(raw[24..32].try_into().unwrap()),
                    });
                }
                ENTRY_UPCASE => {
                    self.writer.upcase = Some(Region {
                        first: u32::from_le_bytes([raw[20], raw[21], raw[22], raw[23]]),
                        length: u64::from_le_bytes(raw[24..32].try_into().unwrap()),
                    });
                }
                _ => {}
            }
        }
        if self.writer.bitmap.is_none() {
            return Err(Error::BadGeometry);
        }
        Ok(())
    }

    /// Marks the volume cleanly unmounted (unless an earlier writer left it marked in use). Call it when done writing.
    pub(crate) async fn sync(&mut self) -> Result<(), Error<D::Error>> {
        if !self.writer.dirty {
            return Ok(());
        }
        if !self.writer.was_dirty {
            self.modify_sector(0, |data| {
                let flags = u16::from_le_bytes([data[FLAGS_OFFSET], data[FLAGS_OFFSET + 1]]);
                let flags = flags & !VOLUME_DIRTY;
                data[FLAGS_OFFSET..FLAGS_OFFSET + 2].copy_from_slice(&flags.to_le_bytes());
            })
            .await?;
        }
        self.writer.dirty = false;
        Ok(())
    }

    /// The `index`th cluster of a system file: contiguous unless the FAT chains it.
    async fn region_cluster(&mut self, region: Region, index: u64) -> Result<u32, Error<D::Error>> {
        let chained = self.fat_entry(region.first).await? != 0;
        if !chained {
            return u32::try_from(u64::from(region.first) + index)
                .map_err(|_| Error::InvalidCluster);
        }
        let mut cluster = region.first;
        for _ in 0..index {
            cluster = self
                .next_cluster(cluster)
                .await?
                .ok_or(Error::InvalidCluster)?;
        }
        Ok(cluster)
    }

    /// The sector, byte and mask holding the allocation bit of a cluster.
    async fn bitmap_bit(&mut self, cluster: u32) -> Result<(u32, usize, u8), Error<D::Error>> {
        let region = self.writer.bitmap.ok_or(Error::BadGeometry)?;
        let bit = u64::from(cluster.checked_sub(2).ok_or(Error::InvalidCluster)?);
        let byte = bit / 8;
        if byte >= region.length {
            return Err(Error::InvalidCluster);
        }
        let in_region = self.cluster_bytes();
        let holder = self.region_cluster(region, byte / in_region).await?;
        let within = byte % in_region;
        let sector = self.cluster_sector(holder)? + (within / BLOCK_SIZE as u64) as u32;
        Ok((
            sector,
            (within % BLOCK_SIZE as u64) as usize,
            1 << (bit % 8),
        ))
    }

    async fn is_allocated(&mut self, cluster: u32) -> Result<bool, Error<D::Error>> {
        let (sector, at, mask) = self.bitmap_bit(cluster).await?;
        Ok(self.sector(sector).await?[at] & mask != 0)
    }

    async fn set_allocated(
        &mut self,
        cluster: u32,
        allocated: bool,
    ) -> Result<(), Error<D::Error>> {
        let (sector, at, mask) = self.bitmap_bit(cluster).await?;
        self.modify_sector(sector, |data| {
            if allocated {
                data[at] |= mask;
            } else {
                data[at] &= !mask;
            }
        })
        .await
    }

    /// Takes a free cluster in the bitmap.
    async fn allocate(&mut self) -> Result<u32, Error<D::Error>> {
        let total = self.cluster_count;
        let start = self.writer.next_free.clamp(2, total + 1);
        for step in 0..total {
            let cluster = 2 + (start - 2 + step) % total;
            if !self.is_allocated(cluster).await? {
                self.set_allocated(cluster, true).await?;
                self.writer.next_free = if cluster + 1 < total + 2 {
                    cluster + 1
                } else {
                    2
                };
                return Ok(cluster);
            }
        }
        Err(Error::NoSpace)
    }

    async fn release(&mut self, cluster: u32) -> Result<(), Error<D::Error>> {
        self.set_allocated(cluster, false).await?;
        if cluster < self.writer.next_free {
            self.writer.next_free = cluster;
        }
        Ok(())
    }

    /// Adds a cluster after `last` (the file's last, or none for an empty file). Returns the new cluster and whether the
    /// file still has no FAT chain: one stays contiguous when the next cluster is free, otherwise it gets a FAT chain,
    /// written for the clusters it already has (`count`).
    async fn extend_chain(
        &mut self,
        first: u32,
        last: Option<u32>,
        count: u64,
        no_fat_chain: bool,
    ) -> Result<(u32, bool), Error<D::Error>> {
        let Some(last) = last else {
            let cluster = self.allocate().await?;
            self.set_fat_entry(cluster, EXFAT_END).await?;
            return Ok((cluster, false));
        };
        if no_fat_chain {
            let next = last + 1;
            if u64::from(next) < u64::from(self.cluster_count) + 2
                && !self.is_allocated(next).await?
            {
                self.set_allocated(next, true).await?;
                return Ok((next, true));
            }
            // Out of room next door: from here on the file is a chain.
            let cluster = self.allocate().await?;
            self.set_fat_entry(cluster, EXFAT_END).await?;
            for index in 0..count.saturating_sub(1) {
                let this = first + index as u32;
                self.set_fat_entry(this, this + 1).await?;
            }
            self.set_fat_entry(last, cluster).await?;
            return Ok((cluster, false));
        }
        let cluster = self.allocate().await?;
        self.set_fat_entry(cluster, EXFAT_END).await?;
        self.set_fat_entry(last, cluster).await?;
        Ok((cluster, false))
    }

    /// Frees all the clusters of a file or directory of `length` bytes.
    async fn free_chain(
        &mut self,
        first: u32,
        no_fat_chain: bool,
        length: u64,
    ) -> Result<(), Error<D::Error>> {
        if no_fat_chain {
            let count = length.div_ceil(self.cluster_bytes());
            for index in 0..count {
                self.release(first + index as u32).await?;
            }
            return Ok(());
        }
        let mut cluster = first;
        for _ in 0..=self.cluster_count {
            let next = self.next_cluster(cluster).await?;
            self.set_fat_entry(cluster, 0).await?;
            self.release(cluster).await?;
            match next {
                Some(next) => cluster = next,
                None => return Ok(()),
            }
        }
        Err(Error::InvalidCluster)
    }

    async fn walk_to(&mut self, file: &mut File, index: u64) -> Result<(), Error<D::Error>> {
        if file.first_cluster < 2 {
            return Err(Error::TruncatedFile);
        }
        if file.no_fat_chain {
            file.cluster = u32::try_from(u64::from(file.first_cluster) + index)
                .map_err(|_| Error::InvalidCluster)?;
            file.clusters_walked = index as u32;
            return Ok(());
        }
        if u64::from(file.clusters_walked) > index || file.cluster < 2 {
            file.cluster = file.first_cluster;
            file.clusters_walked = 0;
        }
        while u64::from(file.clusters_walked) < index {
            match self.next_cluster(file.cluster).await? {
                Some(next) => {
                    file.cluster = next;
                    file.clusters_walked += 1;
                }
                None => return Err(Error::TruncatedFile),
            }
        }
        Ok(())
    }

    /// Writes `data` at the file's position, growing the file as needed; returns the bytes written. The entry is updated
    /// by `flush_file`.
    pub(crate) async fn write_file(
        &mut self,
        file: &mut File,
        data: &[u8],
    ) -> Result<usize, Error<D::Error>> {
        if data.is_empty() {
            return Ok(0);
        }
        self.mark_dirty().await?;
        let cluster_bytes = self.cluster_bytes();
        let mut done = 0;
        while done < data.len() {
            let index = file.cursor / cluster_bytes;
            if file.cursor == file.size && file.cursor.is_multiple_of(cluster_bytes) {
                let last = if index == 0 {
                    None
                } else {
                    self.walk_to(file, index - 1).await?;
                    Some(file.cluster)
                };
                let (cluster, no_fat_chain) = match self
                    .extend_chain(file.first_cluster, last, index, file.no_fat_chain)
                    .await
                {
                    Ok(extended) => extended,
                    Err(Error::NoSpace) if done > 0 => break,
                    Err(error) => return Err(error),
                };
                if index == 0 {
                    file.first_cluster = cluster;
                }
                file.no_fat_chain = no_fat_chain;
                file.cluster = cluster;
                file.clusters_walked = index as u32;
                file.dirty = true;
            } else {
                self.walk_to(file, index).await?;
            }

            let offset = file.cursor % cluster_bytes;
            let sector = self.cluster_sector(file.cluster)? + (offset / BLOCK_SIZE as u64) as u32;
            let within = (offset % BLOCK_SIZE as u64) as usize;
            let take = (BLOCK_SIZE - within).min(data.len() - done);
            let chunk = &data[done..done + take];
            if take == BLOCK_SIZE {
                let mut block = [0u8; BLOCK_SIZE];
                block.copy_from_slice(chunk);
                self.write_sector(sector, &block).await?;
            } else {
                self.modify_sector(sector, |block| {
                    block[within..within + take].copy_from_slice(chunk)
                })
                .await?;
            }
            file.cursor += take as u64;
            done += take;
            if file.cursor > file.size {
                file.size = file.cursor;
            }
            file.dirty = true;
        }
        Ok(done)
    }

    /// Reads an entry set into `out`.
    async fn read_set(
        &mut self,
        loc: &EntryLoc,
        out: &mut [[u8; 32]; MAX_SET],
    ) -> Result<usize, Error<D::Error>> {
        let count = loc.count as usize;
        if !(3..=MAX_SET).contains(&count) {
            return Err(Error::InvalidOperation);
        }
        let mut cursor = loc.cursor();
        for entry in out.iter_mut().take(count) {
            *entry = self
                .next_raw(&mut cursor)
                .await?
                .ok_or(Error::InvalidCluster)?;
        }
        Ok(count)
    }

    /// Writes entries over the ones starting at `cursor`, one write per sector. Returns whether it overwrote the
    /// end-of-directory marker.
    async fn write_run(
        &mut self,
        mut cursor: Directory,
        entries: &[[u8; 32]],
    ) -> Result<bool, Error<D::Error>> {
        let mut ended = false;
        let mut index = 0;
        let mut last_cluster = cursor.cluster;
        while index < entries.len() {
            let sector = self.cluster_sector(cursor.cluster)? + cursor.sector_in_cluster;
            let mut block = *self.sector(sector).await?;
            loop {
                let offset = cursor.entry_in_sector as usize * 32;
                if block[offset] == 0x00 {
                    ended = true;
                }
                block[offset..offset + 32].copy_from_slice(&entries[index]);
                index += 1;
                last_cluster = cursor.cluster;
                // Step on, to the next entry or the next sector.
                let (cluster, sector_in_cluster) = (cursor.cluster, cursor.sector_in_cluster);
                self.advance(&mut cursor).await?;
                if index == entries.len() {
                    break;
                }
                if cursor.finished {
                    return Err(Error::InvalidCluster);
                }
                if cursor.cluster != cluster || cursor.sector_in_cluster != sector_in_cluster {
                    break;
                }
            }
            self.write_sector(sector, &block).await?;
        }
        // A directory without a FAT chain does not say where it ends, so the marker is not written in a cluster after the one the
        // set ended in (a new directory cluster is zeroed, and so already ends the directory).
        if ended && !cursor.finished && !(cursor.no_fat_chain && cursor.cluster != last_cluster) {
            // The entry after the set must still end the directory.
            let sector = self.cluster_sector(cursor.cluster)? + cursor.sector_in_cluster;
            let offset = cursor.entry_in_sector as usize * 32;
            if self.sector(sector).await?[offset] != 0x00 {
                self.modify_sector(sector, |block| block[offset] = 0x00)
                    .await?;
            }
        }
        Ok(ended)
    }

    /// Changes an entry set in place and rewrites its checksum.
    async fn modify_set(
        &mut self,
        loc: &EntryLoc,
        change: impl FnOnce(&mut [[u8; 32]]),
    ) -> Result<(), Error<D::Error>> {
        let mut set = [[0u8; 32]; MAX_SET];
        let count = self.read_set(loc, &mut set).await?;
        change(&mut set[..count]);
        let checksum = set_checksum(&set[..count]);
        set[0][2..4].copy_from_slice(&checksum.to_le_bytes());
        self.write_run(loc.cursor(), &set[..count]).await?;
        Ok(())
    }

    /// Writes the file's size, first cluster, flags and times into its entry set.
    pub(crate) async fn flush_file(
        &mut self,
        file: &mut File,
        now: Timestamp,
    ) -> Result<(), Error<D::Error>> {
        if !file.dirty {
            return Ok(());
        }
        let loc = file.loc.exfat().ok_or(Error::InvalidOperation)?;
        let stamp = exfat_timestamp(now);
        let (size, first, no_fat_chain) = (file.size, file.first_cluster, file.no_fat_chain);
        self.modify_set(&loc, |set| {
            let attributes = u16::from_le_bytes([set[0][4], set[0][5]]) | ATTR_ARCHIVE;
            set[0][4..6].copy_from_slice(&attributes.to_le_bytes());
            set[0][12..16].copy_from_slice(&stamp);
            set[0][16..20].copy_from_slice(&stamp);
            let stream = &mut set[1];
            stream[1] = FLAG_ALLOCATION_POSSIBLE | if no_fat_chain { FLAG_NO_FAT_CHAIN } else { 0 };
            stream[8..16].copy_from_slice(&size.to_le_bytes());
            stream[20..24].copy_from_slice(&first.to_le_bytes());
            stream[24..32].copy_from_slice(&size.to_le_bytes());
        })
        .await?;
        file.dirty = false;
        Ok(())
    }

    /// Sets the file's length: shorter frees clusters, longer fills with zeros. Shortening writes the new length into the
    /// entry before it frees anything, so a cut in power leaves clusters nothing refers to, never a file longer than its
    /// clusters.
    pub(crate) async fn set_len(
        &mut self,
        file: &mut File,
        length: u64,
        now: Timestamp,
    ) -> Result<(), Error<D::Error>> {
        if length == file.size {
            return Ok(());
        }
        self.mark_dirty().await?;
        let position = file.cursor;
        if length > file.size {
            file.cursor = file.size;
            let zeros = [0u8; BLOCK_SIZE];
            while file.size < length {
                let take = (length - file.size).min(BLOCK_SIZE as u64) as usize;
                if self.write_file(file, &zeros[..take]).await? == 0 {
                    return Err(Error::NoSpace);
                }
            }
        } else {
            let cluster_bytes = self.cluster_bytes();
            let keep = length.div_ceil(cluster_bytes);
            let have = file.size.div_ceil(cluster_bytes);
            let (old_first, old_size, old_no_fat_chain) =
                (file.first_cluster, file.size, file.no_fat_chain);
            let mut cut = None;
            if keep > 0 && !file.no_fat_chain {
                file.cluster = file.first_cluster;
                file.clusters_walked = 0;
                self.walk_to(file, keep - 1).await?;
                cut = Some((file.cluster, self.next_cluster(file.cluster).await?));
            }
            if keep == 0 {
                file.first_cluster = 0;
                file.no_fat_chain = false;
            }
            file.size = length;
            file.dirty = true;
            self.flush_file(file, now).await?;
            if keep == 0 {
                if old_first >= 2 {
                    self.free_chain(old_first, old_no_fat_chain, old_size)
                        .await?;
                }
            } else if old_no_fat_chain {
                for index in keep..have {
                    self.release(old_first + index as u32).await?;
                }
            } else if let Some((last, tail)) = cut {
                self.set_fat_entry(last, EXFAT_END).await?;
                if let Some(tail) = tail {
                    self.free_chain(tail, false, 0).await?;
                }
            }
        }
        file.dirty = true;
        file.cluster = file.first_cluster;
        file.clusters_walked = 0;
        file.cursor = position.min(file.size);
        if file.cursor < file.size {
            let target = file.cursor;
            file.cursor = 0;
            self.seek(file, target).await?;
        }
        Ok(())
    }

    // ---- names --------------------------------------------------------------------------------------------------

    /// The upper-case form of a UTF-16 unit, through the volume's table (ASCII needs no lookup).
    async fn upcase(&mut self, unit: u16) -> Result<u16, Error<D::Error>> {
        if unit < 0x80 {
            return Ok(upcase_ascii(unit));
        }
        let Some(table) = self.writer.upcase else {
            return Ok(unit);
        };
        // The table is a list of upper-case units by code, where 0xffff followed by n means n units map to themselves.
        let mut code = 0u32;
        let mut position = 0u64;
        let words = table.length / 2;
        let cluster_bytes = self.cluster_bytes();
        let mut pending_skip = false;
        while position < words {
            let byte = position * 2;
            let holder = self.region_cluster(table, byte / cluster_bytes).await?;
            let within = byte % cluster_bytes;
            let sector = self.cluster_sector(holder)? + (within / BLOCK_SIZE as u64) as u32;
            let at = (within % BLOCK_SIZE as u64) as usize;
            let data = self.sector(sector).await?;
            let value = u16::from_le_bytes([data[at], data[at + 1]]);
            position += 1;
            if pending_skip {
                code += u32::from(value);
                pending_skip = false;
                if code > u32::from(unit) {
                    return Ok(unit);
                }
            } else if value == 0xffff {
                pending_skip = true;
            } else {
                if code == u32::from(unit) {
                    return Ok(value);
                }
                code += 1;
            }
        }
        Ok(unit)
    }

    async fn name_hash(&mut self, units: &[u16]) -> Result<u16, Error<D::Error>> {
        let mut hash = 0u16;
        for &unit in units {
            let upper = self.upcase(unit).await?;
            for byte in upper.to_le_bytes() {
                hash = hash.rotate_right(1).wrapping_add(u16::from(byte));
            }
        }
        Ok(hash)
    }

    // ---- directories --------------------------------------------------------------------------------------------

    async fn zero_cluster(&mut self, cluster: u32) -> Result<(), Error<D::Error>> {
        let first = self.cluster_sector(cluster)?;
        let zeros = [0u8; BLOCK_SIZE];
        for sector in 0..1u32 << self.sectors_per_cluster_shift {
            self.write_sector(first + sector, &zeros).await?;
        }
        Ok(())
    }

    /// Adds a zeroed cluster to the end of a directory (whose cursor is on its last cluster), and records the new length in
    /// the directory's own entry. Returns the new cluster and whether the directory still has no FAT chain.
    async fn grow_directory(&mut self, dir: &Directory) -> Result<(u32, bool), Error<D::Error>> {
        let have = u64::from(dir.clusters_walked) + 1;
        let (cluster, no_fat_chain) = self
            .extend_chain(dir.start_cluster, Some(dir.cluster), have, dir.no_fat_chain)
            .await?;
        self.zero_cluster(cluster).await?;
        if let Some(loc) = dir.entry {
            let length = (have + 1) * self.cluster_bytes();
            self.modify_set(&loc, |set| {
                let stream = &mut set[1];
                stream[1] =
                    FLAG_ALLOCATION_POSSIBLE | if no_fat_chain { FLAG_NO_FAT_CHAIN } else { 0 };
                stream[8..16].copy_from_slice(&length.to_le_bytes());
                stream[24..32].copy_from_slice(&length.to_le_bytes());
            })
            .await?;
        }
        Ok((cluster, no_fat_chain))
    }

    /// Finds `count` consecutive free entries in a directory (given as a cursor at its start), adding clusters when it
    /// is full. Returns a cursor at the first of them, free to step on through the directory.
    async fn find_free_run(
        &mut self,
        directory: Directory,
        count: usize,
    ) -> Result<Directory, Error<D::Error>> {
        let mut at = directory;
        let mut run_start = at;
        let mut run = 0usize;
        let mut after_end = false;
        let per_cluster = (1usize << self.sectors_per_cluster_shift) * BLOCK_SIZE / 32;
        loop {
            let sector = self.cluster_sector(at.cluster)? + at.sector_in_cluster;
            let first_byte = self.sector(sector).await?[at.entry_in_sector as usize * 32];
            // An entry whose in-use bit is clear is free, and everything after the first 0x00 is.
            let free = after_end || first_byte & 0x80 == 0;
            if first_byte == 0x00 {
                after_end = true;
            }
            if free {
                if run == 0 {
                    run_start = at;
                }
                run += 1;
                if run == count {
                    return Ok(Self::unbounded(run_start));
                }
            } else {
                run = 0;
            }
            self.advance(&mut at).await?;
            if !at.finished {
                continue;
            }
            // Out of directory: the rest of the run goes in new clusters.
            loop {
                let (cluster, no_fat_chain) = self.grow_directory(&at).await?;
                if !no_fat_chain && at.no_fat_chain {
                    // The directory got a FAT chain: from here on every cursor on it follows that.
                    run_start.no_fat_chain = false;
                }
                let mut grown = at;
                grown.cluster = cluster;
                grown.sector_in_cluster = 0;
                grown.entry_in_sector = 0;
                grown.clusters_walked += 1;
                grown.no_fat_chain = no_fat_chain;
                grown.finished = false;
                grown.remaining = if at.remaining == u64::MAX {
                    u64::MAX
                } else {
                    per_cluster as u64 * 32
                };
                if run == 0 {
                    run_start = grown;
                }
                run += per_cluster;
                if run >= count {
                    run_start.no_fat_chain = no_fat_chain && run_start.no_fat_chain;
                    return Ok(Self::unbounded(run_start));
                }
                at = grown;
                // `at` now stands on the new cluster; step to its last entry and let the grow loop add another.
                at.sector_in_cluster = (1u32 << self.sectors_per_cluster_shift) - 1;
                at.entry_in_sector = BLOCK_SIZE as u32 / 32 - 1;
            }
        }
    }

    fn unbounded(mut dir: Directory) -> Directory {
        dir.remaining = u64::MAX;
        dir.finished = false;
        dir
    }

    /// Adds an entry set to a directory and returns where it landed.
    async fn add_entry(
        &mut self,
        directory: &Directory,
        name: &str,
        new: Allocation,
        now: Timestamp,
    ) -> Result<EntryLoc, Error<D::Error>> {
        let mut units = [0u16; MAX_NAME];
        let unit_count = encode_name(name, &mut units);
        let name_hash = self.name_hash(&units[..unit_count]).await?;
        let new = NewSet {
            units: &units[..unit_count],
            name_hash,
            attributes: new.attributes,
            first_cluster: new.first_cluster,
            length: new.length,
            flags: new.flags,
            now,
        };
        let mut set = [[0u8; 32]; MAX_SET];
        let count = build_set(&new, &mut set);
        self.place_set(directory, &set[..count]).await
    }

    /// Writes a finished entry set into the first free run of a directory.
    async fn place_set(
        &mut self,
        directory: &Directory,
        set: &[[u8; 32]],
    ) -> Result<EntryLoc, Error<D::Error>> {
        let first = self.find_free_run(*directory, set.len()).await?;
        self.write_run(first, set).await?;
        Ok(EntryLoc {
            pos: Pos::of(&first),
            count: set.len() as u8,
            dir_start: first.start_cluster,
            dir_no_fat_chain: first.no_fat_chain,
        })
    }

    /// Splits a path into the directory that holds it and the last name.
    async fn resolve_parent<'p>(
        &mut self,
        path: &'p str,
        forbidden: Option<u32>,
    ) -> Result<(Directory, &'p str), Error<D::Error>> {
        let mut parts = path
            .split('/')
            .filter(|part| !part.is_empty() && *part != ".")
            .peekable();
        let mut directory = self.open_root();
        let mut last = None;
        while let Some(part) = parts.next() {
            if part == ".." {
                return Err(Error::InvalidName);
            }
            if parts.peek().is_some() {
                let entry = self.find_in(&directory, part).await?;
                if !entry.is_directory {
                    return Err(Error::NotADirectory);
                }
                if forbidden == Some(entry.first_cluster) {
                    return Err(Error::InvalidOperation);
                }
                directory = Directory::new(entry.first_cluster, entry.size, entry.no_fat_chain);
                directory.entry = entry.loc.exfat();
            } else {
                last = Some(part);
            }
        }
        Ok((directory, last.ok_or(Error::InvalidName)?))
    }

    async fn require_absent(
        &mut self,
        directory: &Directory,
        name: &str,
    ) -> Result<(), Error<D::Error>> {
        match self.find_in(directory, name).await {
            Ok(_) => Err(Error::AlreadyExists),
            Err(Error::NotFound) => Ok(()),
            Err(other) => Err(other),
        }
    }

    pub(crate) async fn create_file(
        &mut self,
        path: &str,
        now: Timestamp,
    ) -> Result<File, Error<D::Error>> {
        let (directory, name) = self.resolve_parent(path, None).await?;
        check_name(name)?;
        self.require_absent(&directory, name).await?;
        self.mark_dirty().await?;
        let loc = self
            .add_entry(
                &directory,
                name,
                Allocation {
                    attributes: ATTR_ARCHIVE,
                    first_cluster: 0,
                    length: 0,
                    flags: FLAG_ALLOCATION_POSSIBLE,
                },
                now,
            )
            .await?;
        Ok(File {
            first_cluster: 0,
            cluster: 0,
            size: 0,
            cursor: 0,
            clusters_walked: 0,
            no_fat_chain: false,
            loc: crate::fs::EntryLoc::ExFat(loc),
            dirty: false,
        })
    }

    pub(crate) async fn create_dir(
        &mut self,
        path: &str,
        now: Timestamp,
    ) -> Result<(), Error<D::Error>> {
        let (parent, name) = self.resolve_parent(path, None).await?;
        check_name(name)?;
        self.require_absent(&parent, name).await?;
        self.mark_dirty().await?;
        let cluster = self.allocate().await?;
        self.set_fat_entry(cluster, EXFAT_END).await?;
        self.zero_cluster(cluster).await?;
        let length = self.cluster_bytes();
        if let Err(error) = self
            .add_entry(
                &parent,
                name,
                Allocation {
                    attributes: ATTR_DIRECTORY,
                    first_cluster: cluster,
                    length,
                    flags: FLAG_ALLOCATION_POSSIBLE,
                },
                now,
            )
            .await
        {
            let _ = self.free_chain(cluster, false, 0).await;
            return Err(error);
        }
        Ok(())
    }

    /// Marks every entry of a set not in use.
    async fn delete_set(&mut self, loc: &EntryLoc) -> Result<(), Error<D::Error>> {
        let mut set = [[0u8; 32]; MAX_SET];
        let count = self.read_set(loc, &mut set).await?;
        for entry in set.iter_mut().take(count) {
            entry[0] &= 0x7f;
        }
        self.write_run(loc.cursor(), &set[..count]).await?;
        Ok(())
    }

    pub(crate) async fn remove_file(&mut self, path: &str) -> Result<(), Error<D::Error>> {
        let entry = self.find_path(path).await?;
        if entry.is_directory {
            return Err(Error::NotAFile);
        }
        let loc = entry.loc.exfat().ok_or(Error::InvalidOperation)?;
        self.mark_dirty().await?;
        self.delete_set(&loc).await?;
        if entry.first_cluster >= 2 {
            self.free_chain(entry.first_cluster, entry.no_fat_chain, entry.size)
                .await?;
        }
        Ok(())
    }

    pub(crate) async fn remove_dir(&mut self, path: &str) -> Result<(), Error<D::Error>> {
        let entry = self.find_path(path).await?;
        if !entry.is_directory {
            return Err(Error::NotADirectory);
        }
        let loc = entry.loc.exfat().ok_or(Error::InvalidOperation)?;
        let mut contents = Directory::new(entry.first_cluster, entry.size, entry.no_fat_chain);
        if self.next_entry(&mut contents).await?.is_some() {
            return Err(Error::NotEmpty);
        }
        self.mark_dirty().await?;
        self.delete_set(&loc).await?;
        if entry.first_cluster >= 2 {
            self.free_chain(entry.first_cluster, entry.no_fat_chain, entry.size)
                .await?;
        }
        Ok(())
    }

    pub(crate) async fn rename(
        &mut self,
        from: &str,
        to: &str,
        now: Timestamp,
    ) -> Result<(), Error<D::Error>> {
        let entry = self.find_path(from).await?;
        let old = entry.loc.exfat().ok_or(Error::InvalidOperation)?;
        let forbidden = entry.is_directory.then_some(entry.first_cluster);
        let (directory, name) = self.resolve_parent(to, forbidden).await?;
        check_name(name)?;
        match self.find_in(&directory, name).await {
            Ok(existing) => {
                let same = existing.loc.exfat().is_some_and(|other| {
                    other.pos.cluster == old.pos.cluster
                        && other.pos.sector_in_cluster == old.pos.sector_in_cluster
                        && other.pos.entry_in_sector == old.pos.entry_in_sector
                });
                if !same {
                    return Err(Error::AlreadyExists);
                }
            }
            Err(Error::NotFound) => {}
            Err(other) => return Err(other),
        }
        self.mark_dirty().await?;
        let mut set = [[0u8; 32]; MAX_SET];
        self.read_set(&old, &mut set).await?;
        let mut units = [0u16; MAX_NAME];
        let unit_count = encode_name(name, &mut units);
        let name_hash = self.name_hash(&units[..unit_count]).await?;
        // The same file under a new name: keep the attributes, times and the stream's allocation.
        let mut new = [[0u8; 32]; MAX_SET];
        let new_count = build_set(
            &NewSet {
                units: &units[..unit_count],
                name_hash,
                attributes: u16::from_le_bytes([set[0][4], set[0][5]]),
                first_cluster: 0,
                length: 0,
                flags: 0,
                now,
            },
            &mut new,
        );
        new[0][4..28].copy_from_slice(&set[0][4..28]);
        new[1][1] = set[1][1];
        new[1][8..16].copy_from_slice(&set[1][8..16]);
        new[1][20..32].copy_from_slice(&set[1][20..32]);
        let checksum = set_checksum(&new[..new_count]);
        new[0][2..4].copy_from_slice(&checksum.to_le_bytes());
        // Where it can, the new set goes over the old one in a single sector write. Otherwise it is placed first and the old
        // one removed after: a cut in power then leaves the file under both names (which a checker calls two files sharing
        // clusters and may truncate), never under neither.
        let per_sector = BLOCK_SIZE as u32 / 32;
        let in_place = old.dir_start == directory.start_cluster
            && usize::from(old.count) == new_count
            && old.pos.entry_in_sector + new_count as u32 <= per_sector;
        if in_place {
            self.write_run(old.cursor(), &new[..new_count]).await?;
        } else {
            self.place_set(&directory, &new[..new_count]).await?;
            self.delete_set(&old).await?;
        }
        Ok(())
    }
}