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