3 Volume Boot Record creation code.
5 Copyright (C) 2011, 2012 Andrew Nayenko
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.
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.
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/>.
28 static off_t vbr_alignment(void)
30 return get_sector_size();
33 static off_t vbr_size(void)
35 return 12 * get_sector_size();
38 static void init_sb(struct exfat_super_block* sb)
40 uint32_t clusters_max;
43 clusters_max = get_volume_size() / get_cluster_size();
44 fat_sectors = DIV_ROUND_UP((off_t) clusters_max * sizeof(cluster_t),
47 memset(sb, 0, sizeof(struct exfat_super_block));
51 memcpy(sb->oem_name, "EXFAT ", sizeof(sb->oem_name));
52 sb->sector_start = cpu_to_le64(get_first_sector());
53 sb->sector_count = cpu_to_le64(get_volume_size() / get_sector_size());
54 sb->fat_sector_start = cpu_to_le32(
55 fat.get_alignment() / get_sector_size());
56 sb->fat_sector_count = cpu_to_le32(ROUND_UP(
57 le32_to_cpu(sb->fat_sector_start) + fat_sectors,
58 1 << get_spc_bits()) -
59 le32_to_cpu(sb->fat_sector_start));
60 sb->cluster_sector_start = cpu_to_le32(
61 get_position(&cbm) / get_sector_size());
62 sb->cluster_count = cpu_to_le32(clusters_max -
63 ((le32_to_cpu(sb->fat_sector_start) +
64 le32_to_cpu(sb->fat_sector_count)) >> get_spc_bits()));
65 sb->rootdir_cluster = cpu_to_le32(
66 (get_position(&rootdir) - get_position(&cbm)) / get_cluster_size()
67 + EXFAT_FIRST_DATA_CLUSTER);
68 sb->volume_serial = cpu_to_le32(get_volume_serial());
69 sb->version.major = 1;
70 sb->version.minor = 0;
71 sb->volume_state = cpu_to_le16(0);
72 sb->sector_bits = get_sector_bits();
73 sb->spc_bits = get_spc_bits();
76 sb->allocated_percent = 0;
77 sb->boot_signature = cpu_to_le16(0xaa55);
80 static int vbr_write(struct exfat_dev* dev)
82 struct exfat_super_block sb;
84 le32_t* sector = malloc(get_sector_size());
89 exfat_error("failed to allocate sector-sized block of memory");
94 if (exfat_write(dev, &sb, sizeof(struct exfat_super_block)) < 0)
97 exfat_error("failed to write super block sector");
100 checksum = exfat_vbr_start_checksum(&sb, sizeof(struct exfat_super_block));
102 memset(sector, 0, get_sector_size());
103 sector[get_sector_size() / sizeof(sector[0]) - 1] =
104 cpu_to_le32(0xaa550000);
105 for (i = 0; i < 8; i++)
107 if (exfat_write(dev, sector, get_sector_size()) < 0)
110 exfat_error("failed to write a sector with boot signature");
113 checksum = exfat_vbr_add_checksum(sector, get_sector_size(), checksum);
116 memset(sector, 0, get_sector_size());
117 for (i = 0; i < 2; i++)
119 if (exfat_write(dev, sector, get_sector_size()) < 0)
122 exfat_error("failed to write an empty sector");
125 checksum = exfat_vbr_add_checksum(sector, get_sector_size(), checksum);
128 for (i = 0; i < get_sector_size() / sizeof(sector[0]); i++)
129 sector[i] = cpu_to_le32(checksum);
130 if (exfat_write(dev, sector, get_sector_size()) < 0)
133 exfat_error("failed to write checksum sector");
141 const struct fs_object vbr =
143 .get_alignment = vbr_alignment,
144 .get_size = vbr_size,