]> git.sven.stormbind.net Git - sven/exfat-utils.git/blob - mkfs/fat.c
Imported Upstream version 0.9.7
[sven/exfat-utils.git] / mkfs / fat.c
1 /*
2         fat.c (09.11.10)
3         File Allocation Table creation code.
4
5         Copyright (C) 2009, 2010  Andrew Nayenko
6
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.
11
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.
16
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/>.
19 */
20
21 #include <unistd.h>
22 #include <inttypes.h>
23 #include <errno.h>
24 #include "mkexfat.h"
25 #include "cbm.h"
26 #include "uct.h"
27 #include "rootdir.h"
28
29 off_t fat_alignment(void)
30 {
31         return (off_t) le32_to_cpu(sb.fat_sector_start) * SECTOR_SIZE(sb);
32 }
33
34 off_t fat_size(void)
35 {
36         return (off_t) le32_to_cpu(sb.fat_sector_count) * SECTOR_SIZE(sb);
37 }
38
39 static cluster_t fat_write_entry(struct exfat_dev* dev, cluster_t cluster,
40                 cluster_t value)
41 {
42         le32_t fat_entry = cpu_to_le32(value);
43         if (exfat_write(dev, &fat_entry, sizeof(fat_entry)) < 0)
44                 return 0;
45         return cluster + 1;
46 }
47
48 static cluster_t fat_write_entries(struct exfat_dev* dev, cluster_t cluster,
49                 uint64_t length)
50 {
51         cluster_t end = cluster + DIV_ROUND_UP(length, CLUSTER_SIZE(sb));
52
53         while (cluster < end - 1)
54         {
55                 cluster = fat_write_entry(dev, cluster, cluster + 1);
56                 if (cluster == 0)
57                         return 0;
58         }
59         return fat_write_entry(dev, cluster, EXFAT_CLUSTER_END);
60 }
61
62 int fat_write(struct exfat_dev* dev, off_t base)
63 {
64         cluster_t c = 0;
65
66         if (base != le32_to_cpu(sb.fat_sector_start) * SECTOR_SIZE(sb))
67                 exfat_bug("unexpected FAT location: %"PRIu64" (expected %u)",
68                                 base, le32_to_cpu(sb.fat_sector_start) * SECTOR_SIZE(sb));
69
70         if (!(c = fat_write_entry(dev, c, 0xfffffff8))) /* media type */
71                 return errno;
72         if (!(c = fat_write_entry(dev, c, 0xffffffff))) /* some weird constant */
73                 return errno;
74         if (!(c = fat_write_entries(dev, c, cbm_size())))
75                 return errno;
76         if (!(c = fat_write_entries(dev, c, uct_size())))
77                 return errno;
78         if (!(c = fat_write_entries(dev, c, rootdir_size())))
79                 return errno;
80
81         return 0;
82 }