OSDN Git Service

Handle 64-bit offsets correctly on Android
[android-x86/external-exfat.git] / mkfs / fat.c
1 /*
2         fat.c (09.11.10)
3         File Allocation Table creation code.
4
5         Copyright (C) 2011-2013  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 <exfat.h>
22 #include <unistd.h>
23 #include "fat.h"
24 #include "cbm.h"
25 #include "uct.h"
26 #include "rootdir.h"
27
28 static off_t fat_alignment(void)
29 {
30         return (off_t) 128 * get_sector_size();
31 }
32
33 static off_t fat_size(void)
34 {
35         return get_volume_size() / get_cluster_size() * sizeof(cluster_t);
36 }
37
38 static cluster_t fat_write_entry(struct exfat_dev* dev, cluster_t cluster,
39                 cluster_t value)
40 {
41         le32_t fat_entry = cpu_to_le32(value);
42         if (exfat_write(dev, &fat_entry, sizeof(fat_entry)) < 0)
43         {
44                 exfat_error("failed to write FAT entry 0x%x", value);
45                 return 0;
46         }
47         return cluster + 1;
48 }
49
50 static cluster_t fat_write_entries(struct exfat_dev* dev, cluster_t cluster,
51                 uint64_t length)
52 {
53         cluster_t end = cluster + DIV_ROUND_UP(length, get_cluster_size());
54
55         while (cluster < end - 1)
56         {
57                 cluster = fat_write_entry(dev, cluster, cluster + 1);
58                 if (cluster == 0)
59                         return 0;
60         }
61         return fat_write_entry(dev, cluster, EXFAT_CLUSTER_END);
62 }
63
64 static int fat_write(struct exfat_dev* dev)
65 {
66         cluster_t c = 0;
67
68         if (!(c = fat_write_entry(dev, c, 0xfffffff8))) /* media type */
69                 return 1;
70         if (!(c = fat_write_entry(dev, c, 0xffffffff))) /* some weird constant */
71                 return 1;
72         if (!(c = fat_write_entries(dev, c, cbm.get_size())))
73                 return 1;
74         if (!(c = fat_write_entries(dev, c, uct.get_size())))
75                 return 1;
76         if (!(c = fat_write_entries(dev, c, rootdir.get_size())))
77                 return 1;
78
79         return 0;
80 }
81
82 const struct fs_object fat =
83 {
84         .get_alignment = fat_alignment,
85         .get_size = fat_size,
86         .write = fat_write,
87 };