3 File Allocation Table creation code.
5 Copyright (C) 2011, 2012 Andrew Nayenko
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 static off_t fat_alignment(void)
29 return (off_t) 128 * get_sector_size();
32 static off_t fat_size(void)
34 return get_volume_size() / get_cluster_size() * sizeof(cluster_t);
37 static cluster_t fat_write_entry(struct exfat_dev* dev, cluster_t cluster,
40 le32_t fat_entry = cpu_to_le32(value);
41 if (exfat_write(dev, &fat_entry, sizeof(fat_entry)) < 0)
43 exfat_error("failed to write FAT entry 0x%x", value);
49 static cluster_t fat_write_entries(struct exfat_dev* dev, cluster_t cluster,
52 cluster_t end = cluster + DIV_ROUND_UP(length, get_cluster_size());
54 while (cluster < end - 1)
56 cluster = fat_write_entry(dev, cluster, cluster + 1);
60 return fat_write_entry(dev, cluster, EXFAT_CLUSTER_END);
63 static int fat_write(struct exfat_dev* dev)
67 if (!(c = fat_write_entry(dev, c, 0xfffffff8))) /* media type */
69 if (!(c = fat_write_entry(dev, c, 0xffffffff))) /* some weird constant */
71 if (!(c = fat_write_entries(dev, c, cbm.get_size())))
73 if (!(c = fat_write_entries(dev, c, uct.get_size())))
75 if (!(c = fat_write_entries(dev, c, rootdir.get_size())))
81 const struct fs_object fat =
83 .get_alignment = fat_alignment,