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