]> git.sven.stormbind.net Git - sven/exfat-utils.git/blob - mkfs/vbr.c
702aa6d1466fee540bbbbc5f5cbd75c9c369cbde
[sven/exfat-utils.git] / mkfs / vbr.c
1 /*
2         vbr.c (09.11.10)
3         Volume Boot Record creation code.
4
5         Free exFAT implementation.
6         Copyright (C) 2011-2015  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 "vbr.h"
24 #include "fat.h"
25 #include "cbm.h"
26 #include "uct.h"
27 #include "rootdir.h"
28 #include <string.h>
29
30 static off_t vbr_alignment(void)
31 {
32         return get_sector_size();
33 }
34
35 static off_t vbr_size(void)
36 {
37         return 12 * get_sector_size();
38 }
39
40 static void init_sb(struct exfat_super_block* sb)
41 {
42         uint32_t clusters_max;
43         uint32_t fat_sectors;
44
45         clusters_max = get_volume_size() / get_cluster_size();
46         fat_sectors = DIV_ROUND_UP((off_t) clusters_max * sizeof(cluster_t),
47                         get_sector_size());
48
49         memset(sb, 0, sizeof(struct exfat_super_block));
50         sb->jump[0] = 0xeb;
51         sb->jump[1] = 0x76;
52         sb->jump[2] = 0x90;
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();
76         sb->fat_count = 1;
77         sb->drive_no = 0x80;
78         sb->allocated_percent = 0;
79         sb->boot_signature = cpu_to_le16(0xaa55);
80 }
81
82 static int vbr_write(struct exfat_dev* dev)
83 {
84         struct exfat_super_block sb;
85         uint32_t checksum;
86         le32_t* sector = malloc(get_sector_size());
87         size_t i;
88
89         if (sector == NULL)
90         {
91                 exfat_error("failed to allocate sector-sized block of memory");
92                 return 1;
93         }
94
95         init_sb(&sb);
96         if (exfat_write(dev, &sb, sizeof(struct exfat_super_block)) < 0)
97         {
98                 free(sector);
99                 exfat_error("failed to write super block sector");
100                 return 1;
101         }
102         checksum = exfat_vbr_start_checksum(&sb, sizeof(struct exfat_super_block));
103
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++)
108         {
109                 if (exfat_write(dev, sector, get_sector_size()) < 0)
110                 {
111                         free(sector);
112                         exfat_error("failed to write a sector with boot signature");
113                         return 1;
114                 }
115                 checksum = exfat_vbr_add_checksum(sector, get_sector_size(), checksum);
116         }
117
118         memset(sector, 0, get_sector_size());
119         for (i = 0; i < 2; i++)
120         {
121                 if (exfat_write(dev, sector, get_sector_size()) < 0)
122                 {
123                         free(sector);
124                         exfat_error("failed to write an empty sector");
125                         return 1;
126                 }
127                 checksum = exfat_vbr_add_checksum(sector, get_sector_size(), checksum);
128         }
129
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)
133         {
134                 free(sector);
135                 exfat_error("failed to write checksum sector");
136                 return 1;
137         }
138
139         free(sector);
140         return 0;
141 }
142
143 const struct fs_object vbr =
144 {
145         .get_alignment = vbr_alignment,
146         .get_size = vbr_size,
147         .write = vbr_write,
148 };