]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - LinuxGUI/Ventoy2Disk/Lib/exfat/src/mkfs/mkexfat_main.c
Support veket_24
[Ventoy.git] / LinuxGUI / Ventoy2Disk / Lib / exfat / src / mkfs / mkexfat_main.c
1 /*
2 main.c (15.08.10)
3 Creates exFAT file system.
4
5 Free exFAT implementation.
6 Copyright (C) 2011-2018 Andrew Nayenko
7
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.
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
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.
21 */
22
23 #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"
30 #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>
37
38 const struct fs_object* objects[] =
39 {
40 &vbr,
41 &vbr,
42 &fat,
43 /* clusters heap */
44 &cbm,
45 &uct,
46 &rootdir,
47 NULL,
48 };
49
50 static struct
51 {
52 int sector_bits;
53 int spc_bits;
54 off_t volume_size;
55 le16_t volume_label[EXFAT_ENAME_MAX + 1];
56 uint32_t volume_serial;
57 uint64_t first_sector;
58 }
59 param;
60
61 extern int g_vtoy_exfat_disk_fd;
62 extern uint64_t g_vtoy_exfat_part_size;
63
64 int get_sector_bits(void)
65 {
66 return param.sector_bits;
67 }
68
69 int get_spc_bits(void)
70 {
71 return param.spc_bits;
72 }
73
74 off_t get_volume_size(void)
75 {
76 return param.volume_size;
77 }
78
79 const le16_t* get_volume_label(void)
80 {
81 return param.volume_label;
82 }
83
84 uint32_t get_volume_serial(void)
85 {
86 return param.volume_serial;
87 }
88
89 uint64_t get_first_sector(void)
90 {
91 return param.first_sector;
92 }
93
94 int get_sector_size(void)
95 {
96 return 1 << get_sector_bits();
97 }
98
99 int get_cluster_size(void)
100 {
101 return get_sector_size() << get_spc_bits();
102 }
103
104 static int setup_spc_bits(int sector_bits, int user_defined, off_t volume_size)
105 {
106 int i;
107
108 if (user_defined != -1)
109 {
110 off_t cluster_size = 1 << sector_bits << user_defined;
111 if (volume_size / cluster_size > EXFAT_LAST_DATA_CLUSTER)
112 {
113 struct exfat_human_bytes chb, vhb;
114
115 exfat_humanize_bytes(cluster_size, &chb);
116 exfat_humanize_bytes(volume_size, &vhb);
117 exfat_error("cluster size %"PRIu64" %s is too small for "
118 "%"PRIu64" %s volume, try -s %d",
119 chb.value, chb.unit,
120 vhb.value, vhb.unit,
121 1 << setup_spc_bits(sector_bits, -1, volume_size));
122 return -1;
123 }
124 return user_defined;
125 }
126
127 if (volume_size < 256ull * 1024 * 1024)
128 return MAX(0, 12 - sector_bits); /* 4 KB */
129 if (volume_size < 32ull * 1024 * 1024 * 1024)
130 return MAX(0, 15 - sector_bits); /* 32 KB */
131
132 for (i = 17; ; i++) /* 128 KB or more */
133 if (DIV_ROUND_UP(volume_size, 1 << i) <= EXFAT_LAST_DATA_CLUSTER)
134 return MAX(0, i - sector_bits);
135 }
136
137 static int setup_volume_label(le16_t label[EXFAT_ENAME_MAX + 1], const char* s)
138 {
139 memset(label, 0, (EXFAT_ENAME_MAX + 1) * sizeof(le16_t));
140 if (s == NULL)
141 return 0;
142 return utf8_to_utf16(label, s, EXFAT_ENAME_MAX + 1, strlen(s));
143 }
144
145 static uint32_t setup_volume_serial(uint32_t user_defined)
146 {
147 struct timeval now;
148
149 if (user_defined != 0)
150 return user_defined;
151
152 if (gettimeofday(&now, NULL) != 0)
153 {
154 exfat_error("failed to form volume id");
155 return 0;
156 }
157 return (now.tv_sec << 20) | now.tv_usec;
158 }
159
160 static int setup(struct exfat_dev* dev, int sector_bits, int spc_bits,
161 const char* volume_label, uint32_t volume_serial,
162 uint64_t first_sector)
163 {
164 param.sector_bits = sector_bits;
165 param.first_sector = first_sector;
166 param.volume_size = exfat_get_size(dev);
167
168 param.spc_bits = setup_spc_bits(sector_bits, spc_bits, param.volume_size);
169 if (param.spc_bits == -1)
170 return 1;
171
172 if (setup_volume_label(param.volume_label, volume_label) != 0)
173 return 1;
174
175 param.volume_serial = setup_volume_serial(volume_serial);
176 if (param.volume_serial == 0)
177 return 1;
178
179 return mkfs(dev, param.volume_size);
180 }
181
182 static int logarithm2(int n)
183 {
184 int i;
185
186 for (i = 0; i < sizeof(int) * CHAR_BIT - 1; i++)
187 if ((1 << i) == n)
188 return i;
189 return -1;
190 }
191
192 static void usage(const char* prog)
193 {
194 fprintf(stderr, "Usage: %s [-i volume-id] [-n label] "
195 "[-p partition-first-sector] "
196 "[-s sectors-per-cluster] [-V] <device>\n", prog);
197 exit(1);
198 }
199
200 int mkexfat_main(const char *devpath, int fd, uint64_t part_sector_count)
201 {
202 int spc_bits = -1;
203 uint32_t volume_serial = 0;
204 uint64_t first_sector = 0;
205 struct exfat_dev* dev;
206
207 #if 0
208 while ((opt = getopt(argc, argv, "i:n:p:s:V")) != -1)
209 {
210 switch (opt)
211 {
212 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));
223 if (spc_bits < 0)
224 {
225 exfat_error("invalid option value: '%s'", optarg);
226 return 1;
227 }
228 break;
229 case 'V':
230 puts("Copyright (C) 2011-2018 Andrew Nayenko");
231 return 0;
232 default:
233 usage(argv[0]);
234 break;
235 }
236 }
237 #endif /* #if 0 */
238
239 /*
240 * DiskSize > 32GB Cluster Size use 128KB
241 * DiskSize < 32GB Cluster Size use 32KB
242 */
243 if ((part_sector_count / 2097152) > 32)
244 {
245 spc_bits = logarithm2(256);
246 }
247 else
248 {
249 spc_bits = logarithm2(64);
250 }
251
252 g_vtoy_exfat_disk_fd = fd;
253 g_vtoy_exfat_part_size = part_sector_count * 512;
254
255 dev = exfat_open(devpath, EXFAT_MODE_RW);
256 if (dev == NULL)
257 return 1;
258 if (setup(dev, 9, spc_bits, "Ventoy", volume_serial, first_sector) != 0)
259 {
260 exfat_close(dev);
261 return 1;
262 }
263 if (exfat_close(dev) != 0)
264 return 1;
265
266 return 0;
267 }
268