blob: 43bc0fbea45128a5addf58a4d0837a7a7712e854 [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2 main.c (15.08.10)
3 Creates exFAT file system.
4
bigbiff bigbiff61cdc022013-08-08 08:35:06 -04005 Free exFAT implementation.
Matt Mower09ef1e42015-12-13 11:29:45 -06006 Copyright (C) 2011-2015 Andrew Nayenko
bigbiff bigbiff9c754052013-01-09 09:09:08 -05007
bigbiff bigbiff61cdc022013-08-08 08:35:06 -04008 This program is free software; you can redistribute it and/or modify
bigbiff bigbiff9c754052013-01-09 09:09:08 -05009 it under the terms of the GNU General Public License as published by
bigbiff bigbiff61cdc022013-08-08 08:35:06 -040010 the Free Software Foundation, either version 2 of the License, or
bigbiff bigbiff9c754052013-01-09 09:09:08 -050011 (at your option) any later version.
12
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.
17
bigbiff bigbiff61cdc022013-08-08 08:35:06 -040018 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.
bigbiff bigbiff9c754052013-01-09 09:09:08 -050021*/
22
Matt Mower09ef1e42015-12-13 11:29:45 -060023#include "mkexfat.h"
24#include "vbr.h"
25#include "fat.h"
26#include "cbm.h"
27#include "uct.h"
28#include "rootdir.h"
29#include <exfat.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050030#include <sys/types.h>
31#include <sys/time.h>
32#include <unistd.h>
33#include <inttypes.h>
34#include <stdio.h>
35#include <string.h>
36#include <limits.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050037
38const struct fs_object* objects[] =
39{
40 &vbr,
41 &vbr,
42 &fat,
43 /* clusters heap */
44 &cbm,
45 &uct,
46 &rootdir,
47 NULL,
48};
49
50static struct
51{
52 int sector_bits;
53 int spc_bits;
Spegeliusd69ac2b2014-11-23 15:15:06 +020054 loff_t volume_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050055 le16_t volume_label[EXFAT_ENAME_MAX + 1];
56 uint32_t volume_serial;
57 uint64_t first_sector;
58}
59param;
60
61int get_sector_bits(void)
62{
63 return param.sector_bits;
64}
65
66int get_spc_bits(void)
67{
68 return param.spc_bits;
69}
70
Spegeliusd69ac2b2014-11-23 15:15:06 +020071loff_t get_volume_size(void)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050072{
73 return param.volume_size;
74}
75
76const le16_t* get_volume_label(void)
77{
78 return param.volume_label;
79}
80
81uint32_t get_volume_serial(void)
82{
83 return param.volume_serial;
84}
85
86uint64_t get_first_sector(void)
87{
88 return param.first_sector;
89}
90
91int get_sector_size(void)
92{
93 return 1 << get_sector_bits();
94}
95
96int get_cluster_size(void)
97{
98 return get_sector_size() << get_spc_bits();
99}
100
Spegeliusd69ac2b2014-11-23 15:15:06 +0200101static int setup_spc_bits(int sector_bits, int user_defined, loff_t volume_size)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500102{
103 int i;
104
105 if (user_defined != -1)
106 {
Spegeliusd69ac2b2014-11-23 15:15:06 +0200107 loff_t cluster_size = 1 << sector_bits << user_defined;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500108 if (volume_size / cluster_size > EXFAT_LAST_DATA_CLUSTER)
109 {
110 struct exfat_human_bytes chb, vhb;
111
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",
116 chb.value, chb.unit,
117 vhb.value, vhb.unit,
118 1 << setup_spc_bits(sector_bits, -1, volume_size));
119 return -1;
120 }
121 return user_defined;
122 }
123
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 */
128
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);
132}
133
134static int setup_volume_label(le16_t label[EXFAT_ENAME_MAX + 1], const char* s)
135{
136 memset(label, 0, (EXFAT_ENAME_MAX + 1) * sizeof(le16_t));
137 if (s == NULL)
138 return 0;
139 return utf8_to_utf16(label, s, EXFAT_ENAME_MAX, strlen(s));
140}
141
142static uint32_t setup_volume_serial(uint32_t user_defined)
143{
144 struct timeval now;
145
146 if (user_defined != 0)
147 return user_defined;
148
149 if (gettimeofday(&now, NULL) != 0)
150 {
151 exfat_error("failed to form volume id");
152 return 0;
153 }
154 return (now.tv_sec << 20) | now.tv_usec;
155}
156
157static 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)
160{
161 param.sector_bits = sector_bits;
162 param.first_sector = first_sector;
163 param.volume_size = exfat_get_size(dev);
164
165 param.spc_bits = setup_spc_bits(sector_bits, spc_bits, param.volume_size);
166 if (param.spc_bits == -1)
167 return 1;
168
169 if (setup_volume_label(param.volume_label, volume_label) != 0)
170 return 1;
171
172 param.volume_serial = setup_volume_serial(volume_serial);
173 if (param.volume_serial == 0)
174 return 1;
175
176 return mkfs(dev, param.volume_size);
177}
178
179static int logarithm2(int n)
180{
181 int i;
182
183 for (i = 0; i < sizeof(int) * CHAR_BIT - 1; i++)
184 if ((1 << i) == n)
185 return i;
186 return -1;
187}
188
189static void usage(const char* prog)
190{
191 fprintf(stderr, "Usage: %s [-i volume-id] [-n label] "
192 "[-p partition-first-sector] "
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400193 "[-s sectors-per-cluster] [-V] <device>\n", prog);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500194 exit(1);
195}
196
197int main(int argc, char* argv[])
198{
199 const char* spec = NULL;
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400200 int opt;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500201 int spc_bits = -1;
202 const char* volume_label = NULL;
203 uint32_t volume_serial = 0;
204 uint64_t first_sector = 0;
205 struct exfat_dev* dev;
206
Matt Mower09ef1e42015-12-13 11:29:45 -0600207 printf("mkexfatfs %s\n", VERSION);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500208
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400209 while ((opt = getopt(argc, argv, "i:n:p:s:V")) != -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500210 {
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400211 switch (opt)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500212 {
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400213 case 'i':
214 volume_serial = strtol(optarg, NULL, 16);
215 break;
216 case 'n':
217 volume_label = optarg;
218 break;
219 case 'p':
220 first_sector = strtoll(optarg, NULL, 10);
221 break;
222 case 's':
223 spc_bits = logarithm2(atoi(optarg));
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500224 if (spc_bits < 0)
225 {
Matt Mower09ef1e42015-12-13 11:29:45 -0600226 exfat_error("invalid option value: '%s'", optarg);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500227 return 1;
228 }
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400229 break;
230 case 'V':
Matt Mower09ef1e42015-12-13 11:29:45 -0600231 puts("Copyright (C) 2011-2015 Andrew Nayenko");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500232 return 0;
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400233 default:
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500234 usage(argv[0]);
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400235 break;
236 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500237 }
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400238 if (argc - optind != 1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500239 usage(argv[0]);
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400240 spec = argv[optind];
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500241
242 dev = exfat_open(spec, EXFAT_MODE_RW);
243 if (dev == NULL)
244 return 1;
245 if (setup(dev, 9, spc_bits, volume_label, volume_serial,
246 first_sector) != 0)
247 {
248 exfat_close(dev);
249 return 1;
250 }
251 if (exfat_close(dev) != 0)
252 return 1;
253 printf("File system created successfully.\n");
254 return 0;
255}