3 Creates exFAT file system.
5 Free exFAT implementation.
6 Copyright (C) 2011-2018 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.
30 #include <sys/types.h>
38 const struct fs_object* objects[] =
55 le16_t volume_label[EXFAT_ENAME_MAX + 1];
56 uint32_t volume_serial;
57 uint64_t first_sector;
61 int get_sector_bits(void)
63 return param.sector_bits;
66 int get_spc_bits(void)
68 return param.spc_bits;
71 off_t get_volume_size(void)
73 return param.volume_size;
76 const le16_t* get_volume_label(void)
78 return param.volume_label;
81 uint32_t get_volume_serial(void)
83 return param.volume_serial;
86 uint64_t get_first_sector(void)
88 return param.first_sector;
91 int get_sector_size(void)
93 return 1 << get_sector_bits();
96 int get_cluster_size(void)
98 return get_sector_size() << get_spc_bits();
101 static int setup_spc_bits(int sector_bits, int user_defined, off_t volume_size)
105 if (user_defined != -1)
107 off_t cluster_size = 1 << sector_bits << user_defined;
108 if (volume_size / cluster_size > EXFAT_LAST_DATA_CLUSTER)
110 struct exfat_human_bytes chb, vhb;
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",
118 1 << setup_spc_bits(sector_bits, -1, volume_size));
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 */
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);
134 static int setup_volume_label(le16_t label[EXFAT_ENAME_MAX + 1], const char* s)
136 memset(label, 0, (EXFAT_ENAME_MAX + 1) * sizeof(le16_t));
139 return utf8_to_utf16(label, s, EXFAT_ENAME_MAX + 1, strlen(s));
142 static uint32_t setup_volume_serial(uint32_t user_defined)
146 if (user_defined != 0)
149 if (gettimeofday(&now, NULL) != 0)
151 exfat_error("failed to form volume id");
154 return (now.tv_sec << 20) | now.tv_usec;
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)
161 param.sector_bits = sector_bits;
162 param.first_sector = first_sector;
163 param.volume_size = exfat_get_size(dev);
165 param.spc_bits = setup_spc_bits(sector_bits, spc_bits, param.volume_size);
166 if (param.spc_bits == -1)
169 if (setup_volume_label(param.volume_label, volume_label) != 0)
172 param.volume_serial = setup_volume_serial(volume_serial);
173 if (param.volume_serial == 0)
176 return mkfs(dev, param.volume_size);
179 static int logarithm2(int n)
183 for (i = 0; i < sizeof(int) * CHAR_BIT - 1; i++)
189 static void usage(const char* prog)
191 fprintf(stderr, "Usage: %s [-i volume-id] [-n label] "
192 "[-p partition-first-sector] "
193 "[-s sectors-per-cluster] [-V] <device>\n", prog);
197 int main(int argc, char* argv[])
199 const char* spec = NULL;
202 const char* volume_label = NULL;
203 uint32_t volume_serial = 0;
204 uint64_t first_sector = 0;
205 struct exfat_dev* dev;
207 printf("mkexfatfs %s\n", VERSION);
209 while ((opt = getopt(argc, argv, "i:n:p:s:V")) != -1)
214 volume_serial = strtol(optarg, NULL, 16);
217 volume_label = optarg;
220 first_sector = strtoll(optarg, NULL, 10);
223 spc_bits = logarithm2(atoi(optarg));
226 exfat_error("invalid option value: '%s'", optarg);
231 puts("Copyright (C) 2011-2018 Andrew Nayenko");
238 if (argc - optind != 1)
242 dev = exfat_open(spec, EXFAT_MODE_RW);
245 if (setup(dev, 9, spc_bits, volume_label, volume_serial,
251 if (exfat_close(dev) != 0)
253 printf("File system created successfully.\n");