OSDN Git Service

Merge branch 'master' of git://github.com/relan/exfat into marshmallow-x86
[android-x86/external-exfat.git] / mkfs / main.c
1 /*
2         main.c (15.08.10)
3         Creates exFAT file system.
4
5         Free exFAT implementation.
6         Copyright (C) 2011-2016  Andrew Nayenko
7
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.
12
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.
17
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.
21 */
22
23 #include "mkexfat.h"
24 #include "vbr.h"
25 #include "fat.h"
26 #include "cbm.h"
27 #include "uct.h"
28 #include "rootdir.h"
29 #include <utf.h>
30 #include <exfat.h>
31 #include <sys/types.h>
32 #include <sys/time.h>
33 #include <unistd.h>
34 #include <inttypes.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <limits.h>
38
39 const struct fs_object* objects[] =
40 {
41         &vbr,
42         &vbr,
43         &fat,
44         /* clusters heap */
45         &cbm,
46         &uct,
47         &rootdir,
48         NULL,
49 };
50
51 static struct
52 {
53         int sector_bits;
54         int spc_bits;
55         off_t volume_size;
56         le16_t volume_label[EXFAT_ENAME_MAX + 1];
57         uint32_t volume_serial;
58         uint64_t first_sector;
59 }
60 param;
61
62 int get_sector_bits(void)
63 {
64         return param.sector_bits;
65 }
66
67 int get_spc_bits(void)
68 {
69         return param.spc_bits;
70 }
71
72 off_t get_volume_size(void)
73 {
74         return param.volume_size;
75 }
76
77 const le16_t* get_volume_label(void)
78 {
79         return param.volume_label;
80 }
81
82 uint32_t get_volume_serial(void)
83 {
84         return param.volume_serial;
85 }
86
87 uint64_t get_first_sector(void)
88 {
89         return param.first_sector;
90 }
91
92 int get_sector_size(void)
93 {
94         return 1 << get_sector_bits();
95 }
96
97 int get_cluster_size(void)
98 {
99         return get_sector_size() << get_spc_bits();
100 }
101
102 static int setup_spc_bits(int sector_bits, int user_defined, off_t volume_size)
103 {
104         int i;
105
106         if (user_defined != -1)
107         {
108                 off_t cluster_size = 1 << sector_bits << user_defined;
109                 if (volume_size / cluster_size > EXFAT_LAST_DATA_CLUSTER)
110                 {
111                         struct exfat_human_bytes chb, vhb;
112
113                         exfat_humanize_bytes(cluster_size, &chb);
114                         exfat_humanize_bytes(volume_size, &vhb);
115                         exfat_error("cluster size %"PRIu64" %s is too small for "
116                                         "%"PRIu64" %s volume, try -s %d",
117                                         chb.value, chb.unit,
118                                         vhb.value, vhb.unit,
119                                         1 << setup_spc_bits(sector_bits, -1, volume_size));
120                         return -1;
121                 }
122                 return user_defined;
123         }
124
125         if (volume_size < 256ull * 1024 * 1024)
126                 return MAX(0, 12 - sector_bits);        /* 4 KB */
127         if (volume_size < 32ull * 1024 * 1024 * 1024)
128                 return MAX(0, 15 - sector_bits);        /* 32 KB */
129
130         for (i = 17; ; i++)                                             /* 128 KB or more */
131                 if (DIV_ROUND_UP(volume_size, 1 << i) <= EXFAT_LAST_DATA_CLUSTER)
132                         return MAX(0, i - sector_bits);
133 }
134
135 static int setup_volume_label(le16_t label[EXFAT_ENAME_MAX + 1], const char* s)
136 {
137         memset(label, 0, (EXFAT_ENAME_MAX + 1) * sizeof(le16_t));
138         if (s == NULL)
139                 return 0;
140         return utf8_to_utf16(label, s, EXFAT_ENAME_MAX, strlen(s));
141 }
142
143 static uint32_t setup_volume_serial(uint32_t user_defined)
144 {
145         struct timeval now;
146
147         if (user_defined != 0)
148                 return user_defined;
149
150         if (gettimeofday(&now, NULL) != 0)
151         {
152                 exfat_error("failed to form volume id");
153                 return 0;
154         }
155         return (now.tv_sec << 20) | now.tv_usec;
156 }
157
158 static int setup(struct exfat_dev* dev, int sector_bits, int spc_bits,
159                 const char* volume_label, uint32_t volume_serial,
160                 uint64_t first_sector)
161 {
162         param.sector_bits = sector_bits;
163         param.first_sector = first_sector;
164         param.volume_size = exfat_get_size(dev);
165
166         param.spc_bits = setup_spc_bits(sector_bits, spc_bits, param.volume_size);
167         if (param.spc_bits == -1)
168                 return 1;
169
170         if (setup_volume_label(param.volume_label, volume_label) != 0)
171                 return 1;
172
173         param.volume_serial = setup_volume_serial(volume_serial);
174         if (param.volume_serial == 0)
175                 return 1;
176
177         return mkfs(dev, param.volume_size);
178 }
179
180 static int logarithm2(int n)
181 {
182         int i;
183
184         for (i = 0; i < sizeof(int) * CHAR_BIT - 1; i++)
185                 if ((1 << i) == n)
186                         return i;
187         return -1;
188 }
189
190 static void usage(const char* prog)
191 {
192         fprintf(stderr, "Usage: %s [-i volume-id] [-n label] "
193                         "[-p partition-first-sector] "
194                         "[-s sectors-per-cluster] [-V] <device>\n", prog);
195         exit(1);
196 }
197
198 int main(int argc, char* argv[])
199 {
200         const char* spec = NULL;
201         int opt;
202         int spc_bits = -1;
203         const char* volume_label = NULL;
204         uint32_t volume_serial = 0;
205         uint64_t first_sector = 0;
206         struct exfat_dev* dev;
207
208         printf("mkexfatfs %s\n", VERSION);
209
210         while ((opt = getopt(argc, argv, "i:n:p:s:V")) != -1)
211         {
212                 switch (opt)
213                 {
214                 case 'i':
215                         volume_serial = strtol(optarg, NULL, 16);
216                         break;
217                 case 'n':
218                         volume_label = optarg;
219                         break;
220                 case 'p':
221                         first_sector = strtoll(optarg, NULL, 10);
222                         break;
223                 case 's':
224                         spc_bits = logarithm2(atoi(optarg));
225                         if (spc_bits < 0)
226                         {
227                                 exfat_error("invalid option value: '%s'", optarg);
228                                 return 1;
229                         }
230                         break;
231                 case 'V':
232                         puts("Copyright (C) 2011-2016  Andrew Nayenko");
233                         return 0;
234                 default:
235                         usage(argv[0]);
236                         break;
237                 }
238         }
239         if (argc - optind != 1)
240                 usage(argv[0]);
241         spec = argv[optind];
242
243         dev = exfat_open(spec, EXFAT_MODE_RW);
244         if (dev == NULL)
245                 return 1;
246         if (setup(dev, 9, spc_bits, volume_label, volume_serial,
247                                 first_sector) != 0)
248         {
249                 exfat_close(dev);
250                 return 1;
251         }
252         if (exfat_close(dev) != 0)
253                 return 1;
254         printf("File system created successfully.\n");
255         return 0;
256 }