3 Creates exFAT file system.
5 Free exFAT implementation.
6 Copyright (C) 2011-2016 Andrew Nayenko
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.
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.
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.
31 #include <sys/types.h>
39 const struct fs_object* objects[] =
56 le16_t volume_label[EXFAT_ENAME_MAX + 1];
57 uint32_t volume_serial;
58 uint64_t first_sector;
62 int get_sector_bits(void)
64 return param.sector_bits;
67 int get_spc_bits(void)
69 return param.spc_bits;
72 off_t get_volume_size(void)
74 return param.volume_size;
77 const le16_t* get_volume_label(void)
79 return param.volume_label;
82 uint32_t get_volume_serial(void)
84 return param.volume_serial;
87 uint64_t get_first_sector(void)
89 return param.first_sector;
92 int get_sector_size(void)
94 return 1 << get_sector_bits();
97 int get_cluster_size(void)
99 return get_sector_size() << get_spc_bits();
102 static int setup_spc_bits(int sector_bits, int user_defined, off_t volume_size)
106 if (user_defined != -1)
108 off_t cluster_size = 1 << sector_bits << user_defined;
109 if (volume_size / cluster_size > EXFAT_LAST_DATA_CLUSTER)
111 struct exfat_human_bytes chb, vhb;
113 exfat_humanize_bytes(cluster_size, &chb);
114 exfat_humanize_bytes(volume_size, &vhb);
115 exfat_error("cluster size %"PRIu64" %s is too small for "
116 "%"PRIu64" %s volume, try -s %d",
119 1 << setup_spc_bits(sector_bits, -1, volume_size));
125 if (volume_size < 256ull * 1024 * 1024)
126 return MAX(0, 12 - sector_bits); /* 4 KB */
127 if (volume_size < 32ull * 1024 * 1024 * 1024)
128 return MAX(0, 15 - sector_bits); /* 32 KB */
130 for (i = 17; ; i++) /* 128 KB or more */
131 if (DIV_ROUND_UP(volume_size, 1 << i) <= EXFAT_LAST_DATA_CLUSTER)
132 return MAX(0, i - sector_bits);
135 static int setup_volume_label(le16_t label[EXFAT_ENAME_MAX + 1], const char* s)
137 memset(label, 0, (EXFAT_ENAME_MAX + 1) * sizeof(le16_t));
140 return utf8_to_utf16(label, s, EXFAT_ENAME_MAX, strlen(s));
143 static uint32_t setup_volume_serial(uint32_t user_defined)
147 if (user_defined != 0)
150 if (gettimeofday(&now, NULL) != 0)
152 exfat_error("failed to form volume id");
155 return (now.tv_sec << 20) | now.tv_usec;
158 static int setup(struct exfat_dev* dev, int sector_bits, int spc_bits,
159 const char* volume_label, uint32_t volume_serial,
160 uint64_t first_sector)
162 param.sector_bits = sector_bits;
163 param.first_sector = first_sector;
164 param.volume_size = exfat_get_size(dev);
166 param.spc_bits = setup_spc_bits(sector_bits, spc_bits, param.volume_size);
167 if (param.spc_bits == -1)
170 if (setup_volume_label(param.volume_label, volume_label) != 0)
173 param.volume_serial = setup_volume_serial(volume_serial);
174 if (param.volume_serial == 0)
177 return mkfs(dev, param.volume_size);
180 static int logarithm2(int n)
184 for (i = 0; i < sizeof(int) * CHAR_BIT - 1; i++)
190 static void usage(const char* prog)
192 fprintf(stderr, "Usage: %s [-i volume-id] [-n label] "
193 "[-p partition-first-sector] "
194 "[-s sectors-per-cluster] [-V] <device>\n", prog);
198 int main(int argc, char* argv[])
200 const char* spec = NULL;
203 const char* volume_label = NULL;
204 uint32_t volume_serial = 0;
205 uint64_t first_sector = 0;
206 struct exfat_dev* dev;
208 printf("mkexfatfs %s\n", VERSION);
210 while ((opt = getopt(argc, argv, "i:n:p:s:V")) != -1)
215 volume_serial = strtol(optarg, NULL, 16);
218 volume_label = optarg;
221 first_sector = strtoll(optarg, NULL, 10);
224 spc_bits = logarithm2(atoi(optarg));
227 exfat_error("invalid option value: '%s'", optarg);
232 puts("Copyright (C) 2011-2016 Andrew Nayenko");
239 if (argc - optind != 1)
243 dev = exfat_open(spec, EXFAT_MODE_RW);
246 if (setup(dev, 9, spc_bits, volume_label, volume_serial,
252 if (exfat_close(dev) != 0)
254 printf("File system created successfully.\n");