blob: 12bb838b56b7cffc312381c5508f3730bb05805c [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2 main.c (15.08.10)
3 Creates exFAT file system.
4
bigbiff bigbiffca829c42013-01-28 08:14:25 -05005 Copyright (C) 2011-2013 Andrew Nayenko
bigbiff bigbiff9c754052013-01-09 09:09:08 -05006
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.
11
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.
16
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/>.
19*/
20
21#include <sys/types.h>
22#include <sys/time.h>
23#include <unistd.h>
24#include <inttypes.h>
25#include <stdio.h>
26#include <string.h>
27#include <limits.h>
28#include <exfat.h>
29#include "mkexfat.h"
30#include "vbr.h"
31#include "fat.h"
32#include "cbm.h"
33#include "uct.h"
34#include "rootdir.h"
35
36const struct fs_object* objects[] =
37{
38 &vbr,
39 &vbr,
40 &fat,
41 /* clusters heap */
42 &cbm,
43 &uct,
44 &rootdir,
45 NULL,
46};
47
48static struct
49{
50 int sector_bits;
51 int spc_bits;
52 off64_t volume_size;
53 le16_t volume_label[EXFAT_ENAME_MAX + 1];
54 uint32_t volume_serial;
55 uint64_t first_sector;
56}
57param;
58
59int get_sector_bits(void)
60{
61 return param.sector_bits;
62}
63
64int get_spc_bits(void)
65{
66 return param.spc_bits;
67}
68
69off64_t get_volume_size(void)
70{
71 return param.volume_size;
72}
73
74const le16_t* get_volume_label(void)
75{
76 return param.volume_label;
77}
78
79uint32_t get_volume_serial(void)
80{
81 return param.volume_serial;
82}
83
84uint64_t get_first_sector(void)
85{
86 return param.first_sector;
87}
88
89int get_sector_size(void)
90{
91 return 1 << get_sector_bits();
92}
93
94int get_cluster_size(void)
95{
96 return get_sector_size() << get_spc_bits();
97}
98
99static int setup_spc_bits(int sector_bits, int user_defined, off64_t volume_size)
100{
101 int i;
102
103 if (user_defined != -1)
104 {
105 off64_t cluster_size = 1 << sector_bits << user_defined;
106 if (volume_size / cluster_size > EXFAT_LAST_DATA_CLUSTER)
107 {
108 struct exfat_human_bytes chb, vhb;
109
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",
114 chb.value, chb.unit,
115 vhb.value, vhb.unit,
116 1 << setup_spc_bits(sector_bits, -1, volume_size));
117 return -1;
118 }
119 return user_defined;
120 }
121
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 */
126
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);
130}
131
132static int setup_volume_label(le16_t label[EXFAT_ENAME_MAX + 1], const char* s)
133{
134 memset(label, 0, (EXFAT_ENAME_MAX + 1) * sizeof(le16_t));
135 if (s == NULL)
136 return 0;
137 return utf8_to_utf16(label, s, EXFAT_ENAME_MAX, strlen(s));
138}
139
140static uint32_t setup_volume_serial(uint32_t user_defined)
141{
142 struct timeval now;
143
144 if (user_defined != 0)
145 return user_defined;
146
147 if (gettimeofday(&now, NULL) != 0)
148 {
149 exfat_error("failed to form volume id");
150 return 0;
151 }
152 return (now.tv_sec << 20) | now.tv_usec;
153}
154
155static 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)
158{
159 param.sector_bits = sector_bits;
160 param.first_sector = first_sector;
161 param.volume_size = exfat_get_size(dev);
162
163 param.spc_bits = setup_spc_bits(sector_bits, spc_bits, param.volume_size);
164 if (param.spc_bits == -1)
165 return 1;
166
167 if (setup_volume_label(param.volume_label, volume_label) != 0)
168 return 1;
169
170 param.volume_serial = setup_volume_serial(volume_serial);
171 if (param.volume_serial == 0)
172 return 1;
173
174 return mkfs(dev, param.volume_size);
175}
176
177static int logarithm2(int n)
178{
179 int i;
180
181 for (i = 0; i < sizeof(int) * CHAR_BIT - 1; i++)
182 if ((1 << i) == n)
183 return i;
184 return -1;
185}
186
187static void usage(const char* prog)
188{
189 fprintf(stderr, "Usage: %s [-i volume-id] [-n label] "
190 "[-p partition-first-sector] "
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400191 "[-s sectors-per-cluster] [-V] <device>\n", prog);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500192 exit(1);
193}
194
195int main(int argc, char* argv[])
196{
197 const char* spec = NULL;
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400198 int opt;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500199 int spc_bits = -1;
200 const char* volume_label = NULL;
201 uint32_t volume_serial = 0;
202 uint64_t first_sector = 0;
203 struct exfat_dev* dev;
204
205 printf("mkexfatfs %u.%u.%u\n",
206 EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
207
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400208 while ((opt = getopt(argc, argv, "i:n:p:s:V")) != -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500209 {
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400210 switch (opt)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500211 {
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400212 case 'i':
213 volume_serial = strtol(optarg, NULL, 16);
214 break;
215 case 'n':
216 volume_label = optarg;
217 break;
218 case 'p':
219 first_sector = strtoll(optarg, NULL, 10);
220 break;
221 case 's':
222 spc_bits = logarithm2(atoi(optarg));
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500223 if (spc_bits < 0)
224 {
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400225 exfat_error("invalid option value: `%s'", optarg);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500226 return 1;
227 }
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400228 break;
229 case 'V':
bigbiff bigbiffca829c42013-01-28 08:14:25 -0500230 puts("Copyright (C) 2011-2013 Andrew Nayenko");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500231 return 0;
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400232 default:
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500233 usage(argv[0]);
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400234 break;
235 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500236 }
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400237 if (argc - optind != 1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500238 usage(argv[0]);
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400239 spec = argv[optind];
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500240
241 dev = exfat_open(spec, EXFAT_MODE_RW);
242 if (dev == NULL)
243 return 1;
244 if (setup(dev, 9, spc_bits, volume_label, volume_serial,
245 first_sector) != 0)
246 {
247 exfat_close(dev);
248 return 1;
249 }
250 if (exfat_close(dev) != 0)
251 return 1;
252 printf("File system created successfully.\n");
253 return 0;
254}