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