3 Creates exFAT file system.
5 Copyright (C) 2011, 2012 Andrew Nayenko
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.
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.
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/>.
21 #include <sys/types.h>
36 const struct fs_object* objects[] =
53 le16_t volume_label[EXFAT_ENAME_MAX + 1];
54 uint32_t volume_serial;
55 uint64_t first_sector;
59 int get_sector_bits(void)
61 return param.sector_bits;
64 int get_spc_bits(void)
66 return param.spc_bits;
69 off_t get_volume_size(void)
71 return param.volume_size;
74 const le16_t* get_volume_label(void)
76 return param.volume_label;
79 uint32_t get_volume_serial(void)
81 return param.volume_serial;
84 uint64_t get_first_sector(void)
86 return param.first_sector;
89 int get_sector_size(void)
91 return 1 << get_sector_bits();
94 int get_cluster_size(void)
96 return get_sector_size() << get_spc_bits();
99 static off_t setup_volume_size(struct exfat_dev* dev)
101 off_t size = exfat_seek(dev, 0, SEEK_END);
102 if (size == (off_t) -1)
103 exfat_error("failed to get volume size");
107 static int setup_spc_bits(int sector_bits, int user_defined, off_t volume_size)
111 if (user_defined != -1)
113 off_t cluster_size = 1 << sector_bits << user_defined;
114 if (volume_size / cluster_size > EXFAT_LAST_DATA_CLUSTER)
116 struct exfat_human_bytes chb, vhb;
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",
124 1 << setup_spc_bits(sector_bits, -1, volume_size));
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 */
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);
140 static int setup_volume_label(le16_t label[EXFAT_ENAME_MAX + 1], const char* s)
142 memset(label, 0, sizeof(label));
145 return utf8_to_utf16(label, s, EXFAT_ENAME_MAX, strlen(s));
148 static uint32_t setup_volume_serial(uint32_t user_defined)
152 if (user_defined != 0)
155 if (gettimeofday(&now, NULL) != 0)
157 exfat_error("failed to form volume id");
160 return (now.tv_sec << 20) | now.tv_usec;
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)
167 param.sector_bits = sector_bits;
168 param.first_sector = first_sector;
170 param.volume_size = setup_volume_size(dev);
171 if (param.volume_size == (off_t) -1)
174 param.spc_bits = setup_spc_bits(sector_bits, spc_bits, param.volume_size);
175 if (param.spc_bits == -1)
178 if (setup_volume_label(param.volume_label, volume_label) != 0)
181 param.volume_serial = setup_volume_serial(volume_serial);
182 if (param.volume_serial == 0)
185 return mkfs(dev, param.volume_size);
188 static int logarithm2(int n)
192 for (i = 0; i < sizeof(int) * CHAR_BIT - 1; i++)
198 static void usage(const char* prog)
200 fprintf(stderr, "Usage: %s [-i volume-id] [-n label] "
201 "[-p partition-first-sector] "
202 "[-s sectors-per-cluster] [-v] <device>\n", prog);
206 int main(int argc, char* argv[])
208 const char* spec = NULL;
211 const char* volume_label = NULL;
212 uint32_t volume_serial = 0;
213 uint64_t first_sector = 0;
214 struct exfat_dev* dev;
216 printf("mkexfatfs %u.%u.%u\n",
217 EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
219 for (pp = argv + 1; *pp; pp++)
221 if (strcmp(*pp, "-s") == 0)
226 spc_bits = logarithm2(atoi(*pp));
229 exfat_error("invalid option value: `%s'", *pp);
233 else if (strcmp(*pp, "-n") == 0)
240 else if (strcmp(*pp, "-i") == 0)
245 volume_serial = strtol(*pp, NULL, 16);
247 else if (strcmp(*pp, "-p") == 0)
252 first_sector = strtoll(*pp, NULL, 10);
254 else if (strcmp(*pp, "-v") == 0)
256 puts("Copyright (C) 2011, 2012 Andrew Nayenko");
259 else if (spec == NULL)
267 dev = exfat_open(spec, 0);
270 if (setup(dev, 9, spc_bits, volume_label, volume_serial,
276 if (exfat_close(dev) != 0)
278 printf("File system created successfully.\n");