3 File Allocation Table creation code.
5 Free exFAT implementation.
6 Copyright (C) 2011-2016 Andrew Nayenko
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 static off_t fat_alignment(void)
31 return (off_t) 128 * get_sector_size();
34 static off_t fat_size(void)
36 return get_volume_size() / get_cluster_size() * sizeof(cluster_t);
39 static cluster_t fat_write_entry(struct exfat_dev* dev, cluster_t cluster,
42 le32_t fat_entry = cpu_to_le32(value);
43 if (exfat_write(dev, &fat_entry, sizeof(fat_entry)) < 0)
45 exfat_error("failed to write FAT entry 0x%x", value);
51 static cluster_t fat_write_entries(struct exfat_dev* dev, cluster_t cluster,
54 cluster_t end = cluster + DIV_ROUND_UP(length, get_cluster_size());
56 while (cluster < end - 1)
58 cluster = fat_write_entry(dev, cluster, cluster + 1);
62 return fat_write_entry(dev, cluster, EXFAT_CLUSTER_END);
65 static int fat_write(struct exfat_dev* dev)
69 if (!(c = fat_write_entry(dev, c, 0xfffffff8))) /* media type */
71 if (!(c = fat_write_entry(dev, c, 0xffffffff))) /* some weird constant */
73 if (!(c = fat_write_entries(dev, c, cbm.get_size())))
75 if (!(c = fat_write_entries(dev, c, uct.get_size())))
77 if (!(c = fat_write_entries(dev, c, rootdir.get_size())))
83 const struct fs_object fat =
85 .get_alignment = fat_alignment,