3 File Allocation Table creation code.
5 Copyright (C) 2009, 2010 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/>.
29 off_t fat_alignment(void)
31 return (off_t) le32_to_cpu(sb.fat_sector_start) * SECTOR_SIZE(sb);
36 return (off_t) le32_to_cpu(sb.fat_sector_count) * SECTOR_SIZE(sb);
39 static cluster_t fat_write_entry(cluster_t cluster, cluster_t value, int fd)
41 le32_t fat_entry = cpu_to_le32(value);
42 if (write(fd, &fat_entry, sizeof(fat_entry)) == -1)
47 static cluster_t fat_write_entries(cluster_t cluster, uint64_t length, int fd)
49 cluster_t end = cluster + DIV_ROUND_UP(length, CLUSTER_SIZE(sb));
51 while (cluster < end - 1)
53 cluster = fat_write_entry(cluster, cluster + 1, fd);
57 return fat_write_entry(cluster, EXFAT_CLUSTER_END, fd);
60 int fat_write(off_t base, int fd)
64 if (base != le32_to_cpu(sb.fat_sector_start) * SECTOR_SIZE(sb))
65 exfat_bug("unexpected FAT location: %"PRIu64" (expected %u)",
66 base, le32_to_cpu(sb.fat_sector_start) * SECTOR_SIZE(sb));
68 if (!(c = fat_write_entry(c, 0xfffffff8, fd))) /* media type */
70 if (!(c = fat_write_entry(c, 0xffffffff, fd))) /* some weird constant */
72 if (!(c = fat_write_entries(c, cbm_size(), fd)))
74 if (!(c = fat_write_entries(c, uct_size(), fd)))
76 if (!(c = fat_write_entries(c, rootdir_size(), fd)))