3 Creates exFAT file system.
5 Copyright (C) 2011-2013 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 int setup_spc_bits(int sector_bits, int user_defined, off_t volume_size)
103 if (user_defined != -1)
105 off_t cluster_size = 1 << sector_bits << user_defined;
106 if (volume_size / cluster_size > EXFAT_LAST_DATA_CLUSTER)
108 struct exfat_human_bytes chb, vhb;
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",
116 1 << setup_spc_bits(sector_bits, -1, volume_size));
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 */
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);
132 static int setup_volume_label(le16_t label[EXFAT_ENAME_MAX + 1], const char* s)
134 memset(label, 0, (EXFAT_ENAME_MAX + 1) * sizeof(le16_t));
137 return utf8_to_utf16(label, s, EXFAT_ENAME_MAX, strlen(s));
140 static uint32_t setup_volume_serial(uint32_t user_defined)
144 if (user_defined != 0)
147 if (gettimeofday(&now, NULL) != 0)
149 exfat_error("failed to form volume id");
152 return (now.tv_sec << 20) | now.tv_usec;
155 static 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)
159 param.sector_bits = sector_bits;
160 param.first_sector = first_sector;
161 param.volume_size = exfat_get_size(dev);
163 param.spc_bits = setup_spc_bits(sector_bits, spc_bits, param.volume_size);
164 if (param.spc_bits == -1)
167 if (setup_volume_label(param.volume_label, volume_label) != 0)
170 param.volume_serial = setup_volume_serial(volume_serial);
171 if (param.volume_serial == 0)
174 return mkfs(dev, param.volume_size);
177 static int logarithm2(int n)
181 for (i = 0; i < sizeof(int) * CHAR_BIT - 1; i++)
187 static void usage(const char* prog)
189 fprintf(stderr, "Usage: %s [-i volume-id] [-n label] "
190 "[-p partition-first-sector] "
191 "[-s sectors-per-cluster] [-v] <device>\n", prog);
195 int main(int argc, char* argv[])
197 const char* spec = NULL;
200 const char* volume_label = NULL;
201 uint32_t volume_serial = 0;
202 uint64_t first_sector = 0;
203 struct exfat_dev* dev;
205 printf("mkexfatfs %u.%u.%u\n",
206 EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
208 for (pp = argv + 1; *pp; pp++)
210 if (strcmp(*pp, "-s") == 0)
215 spc_bits = logarithm2(atoi(*pp));
218 exfat_error("invalid option value: `%s'", *pp);
222 else if (strcmp(*pp, "-n") == 0)
229 else if (strcmp(*pp, "-i") == 0)
234 volume_serial = strtol(*pp, NULL, 16);
236 else if (strcmp(*pp, "-p") == 0)
241 first_sector = strtoll(*pp, NULL, 10);
243 else if (strcmp(*pp, "-v") == 0)
245 puts("Copyright (C) 2011-2013 Andrew Nayenko");
248 else if (spec == NULL)
256 dev = exfat_open(spec, EXFAT_MODE_RW);
259 if (setup(dev, 9, spc_bits, volume_label, volume_serial,
265 if (exfat_close(dev) != 0)
267 printf("File system created successfully.\n");