3 Volume Boot Record creation code.
5 Free exFAT implementation.
6 Copyright (C) 2011-2017 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 static off_t vbr_alignment(void)
32 return get_sector_size();
35 static off_t vbr_size(void)
37 return 12 * get_sector_size();
40 static void init_sb(struct exfat_super_block* sb)
42 uint32_t clusters_max;
45 clusters_max = get_volume_size() / get_cluster_size();
46 fat_sectors = DIV_ROUND_UP((off_t) clusters_max * sizeof(cluster_t),
49 memset(sb, 0, sizeof(struct exfat_super_block));
53 memcpy(sb->oem_name, "EXFAT ", sizeof(sb->oem_name));
54 sb->sector_start = cpu_to_le64(get_first_sector());
55 sb->sector_count = cpu_to_le64(get_volume_size() / get_sector_size());
56 sb->fat_sector_start = cpu_to_le32(
57 fat.get_alignment() / get_sector_size());
58 sb->fat_sector_count = cpu_to_le32(ROUND_UP(
59 le32_to_cpu(sb->fat_sector_start) + fat_sectors,
60 1 << get_spc_bits()) -
61 le32_to_cpu(sb->fat_sector_start));
62 sb->cluster_sector_start = cpu_to_le32(
63 get_position(&cbm) / get_sector_size());
64 sb->cluster_count = cpu_to_le32(clusters_max -
65 ((le32_to_cpu(sb->fat_sector_start) +
66 le32_to_cpu(sb->fat_sector_count)) >> get_spc_bits()));
67 sb->rootdir_cluster = cpu_to_le32(
68 (get_position(&rootdir) - get_position(&cbm)) / get_cluster_size()
69 + EXFAT_FIRST_DATA_CLUSTER);
70 sb->volume_serial = cpu_to_le32(get_volume_serial());
71 sb->version.major = 1;
72 sb->version.minor = 0;
73 sb->volume_state = cpu_to_le16(0);
74 sb->sector_bits = get_sector_bits();
75 sb->spc_bits = get_spc_bits();
78 sb->allocated_percent = 0;
79 sb->boot_signature = cpu_to_le16(0xaa55);
82 static int vbr_write(struct exfat_dev* dev)
84 struct exfat_super_block sb;
86 le32_t* sector = malloc(get_sector_size());
91 exfat_error("failed to allocate sector-sized block of memory");
96 if (exfat_write(dev, &sb, sizeof(struct exfat_super_block)) < 0)
99 exfat_error("failed to write super block sector");
102 checksum = exfat_vbr_start_checksum(&sb, sizeof(struct exfat_super_block));
104 memset(sector, 0, get_sector_size());
105 sector[get_sector_size() / sizeof(sector[0]) - 1] =
106 cpu_to_le32(0xaa550000);
107 for (i = 0; i < 8; i++)
109 if (exfat_write(dev, sector, get_sector_size()) < 0)
112 exfat_error("failed to write a sector with boot signature");
115 checksum = exfat_vbr_add_checksum(sector, get_sector_size(), checksum);
118 memset(sector, 0, get_sector_size());
119 for (i = 0; i < 2; i++)
121 if (exfat_write(dev, sector, get_sector_size()) < 0)
124 exfat_error("failed to write an empty sector");
127 checksum = exfat_vbr_add_checksum(sector, get_sector_size(), checksum);
130 for (i = 0; i < get_sector_size() / sizeof(sector[0]); i++)
131 sector[i] = cpu_to_le32(checksum);
132 if (exfat_write(dev, sector, get_sector_size()) < 0)
135 exfat_error("failed to write checksum sector");
143 const struct fs_object vbr =
145 .get_alignment = vbr_alignment,
146 .get_size = vbr_size,