releasing version 0.9.6-1
[sven/exfat-utils.git] / mkfs / vbr.c
1 /*
2         vbr.c (09.11.10)
3         Volume Boot Record creation code.
4
5         Copyright (C) 2009, 2010  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 <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include "mkexfat.h"
25
26 off_t vbr_alignment(void)
27 {
28         return 1;
29 }
30
31 off_t vbr_size(void)
32 {
33         return 12 * SECTOR_SIZE(sb);
34 }
35
36 int vbr_write(off_t base, int fd)
37 {
38         uint32_t checksum;
39         le32_t* sector = malloc(SECTOR_SIZE(sb));
40         size_t i;
41
42         if (sector == NULL)
43                 return errno;
44
45         if (write(fd, &sb, sizeof(struct exfat_super_block)) == -1)
46         {
47                 free(sector);
48                 return errno;
49         }
50         checksum = exfat_vbr_start_checksum(&sb, sizeof(struct exfat_super_block));
51
52         memset(sector, 0, SECTOR_SIZE(sb));
53         sector[SECTOR_SIZE(sb) / sizeof(sector[0]) - 1] = cpu_to_le32(0xaa550000);
54         for (i = 0; i < 8; i++)
55         {
56                 if (write(fd, sector, SECTOR_SIZE(sb)) == -1)
57                 {
58                         free(sector);
59                         return errno;
60                 }
61                 checksum = exfat_vbr_add_checksum(sector, SECTOR_SIZE(sb), checksum);
62         }
63
64         memset(sector, 0, SECTOR_SIZE(sb));
65         if (write(fd, sector, SECTOR_SIZE(sb)) == -1)
66         {
67                 free(sector);
68                 return errno;
69         }
70         checksum = exfat_vbr_add_checksum(sector, SECTOR_SIZE(sb), checksum);
71         if (write(fd, sector, SECTOR_SIZE(sb)) == -1)
72         {
73                 free(sector);
74                 return errno;
75         }
76         checksum = exfat_vbr_add_checksum(sector, SECTOR_SIZE(sb), checksum);
77
78         for (i = 0; i < SECTOR_SIZE(sb) / sizeof(sector[0]); i++)
79                 sector[i] = cpu_to_le32(checksum);
80         if (write(fd, sector, SECTOR_SIZE(sb)) == -1)
81         {
82                 free(sector);
83                 return errno;
84         }
85
86         free(sector);
87         return 0;
88 }