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