]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - LinuxGUI/Ventoy2Disk/Lib/exfat/src/mkfs/fat.c
Fix a bug when install Ventoy if the USB already mounted at a path that contains...
[Ventoy.git] / LinuxGUI / Ventoy2Disk / Lib / exfat / src / mkfs / fat.c
1 /*
2 fat.c (09.11.10)
3 File Allocation Table creation code.
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 "fat.h"
24 #include "cbm.h"
25 #include "uct.h"
26 #include "rootdir.h"
27 #include <unistd.h>
28
29 static off_t fat_alignment(void)
30 {
31 return (off_t) 128 * get_sector_size();
32 }
33
34 static off_t fat_size(void)
35 {
36 return get_volume_size() / get_cluster_size() * sizeof(cluster_t);
37 }
38
39 static cluster_t fat_write_entry(struct exfat_dev* dev, cluster_t cluster,
40 cluster_t value)
41 {
42 le32_t fat_entry = cpu_to_le32(value);
43 if (exfat_write(dev, &fat_entry, sizeof(fat_entry)) < 0)
44 {
45 exfat_error("failed to write FAT entry 0x%x", value);
46 return 0;
47 }
48 return cluster + 1;
49 }
50
51 static cluster_t fat_write_entries(struct exfat_dev* dev, cluster_t cluster,
52 uint64_t length)
53 {
54 cluster_t end = cluster + DIV_ROUND_UP(length, get_cluster_size());
55
56 while (cluster < end - 1)
57 {
58 cluster = fat_write_entry(dev, cluster, cluster + 1);
59 if (cluster == 0)
60 return 0;
61 }
62 return fat_write_entry(dev, cluster, EXFAT_CLUSTER_END);
63 }
64
65 static int fat_write(struct exfat_dev* dev)
66 {
67 cluster_t c = 0;
68
69 if (!(c = fat_write_entry(dev, c, 0xfffffff8))) /* media type */
70 return 1;
71 if (!(c = fat_write_entry(dev, c, 0xffffffff))) /* some weird constant */
72 return 1;
73 if (!(c = fat_write_entries(dev, c, cbm.get_size())))
74 return 1;
75 if (!(c = fat_write_entries(dev, c, uct.get_size())))
76 return 1;
77 if (!(c = fat_write_entries(dev, c, rootdir.get_size())))
78 return 1;
79
80 return 0;
81 }
82
83 const struct fs_object fat =
84 {
85 .get_alignment = fat_alignment,
86 .get_size = fat_size,
87 .write = fat_write,
88 };