blob: bbf1304d9e9387cfdb001f52a73e473a7ee69ffc [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2 vbr.c (09.11.10)
3 Volume Boot Record creation code.
4
bigbiff bigbiff61cdc022013-08-08 08:35:06 -04005 Free exFAT implementation.
bigbiffc40c1c52014-11-01 09:34:57 -04006 Copyright (C) 2011-2013 Andrew Nayenko
bigbiff bigbiff9c754052013-01-09 09:09:08 -05007
bigbiff bigbiff61cdc022013-08-08 08:35:06 -04008 This program is free software; you can redistribute it and/or modify
bigbiff bigbiff9c754052013-01-09 09:09:08 -05009 it under the terms of the GNU General Public License as published by
bigbiff bigbiff61cdc022013-08-08 08:35:06 -040010 the Free Software Foundation, either version 2 of the License, or
bigbiff bigbiff9c754052013-01-09 09:09:08 -050011 (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
bigbiff bigbiff61cdc022013-08-08 08:35:06 -040018 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.
bigbiff bigbiff9c754052013-01-09 09:09:08 -050021*/
22
23#include <string.h>
24#include "vbr.h"
25#include "fat.h"
26#include "cbm.h"
27#include "uct.h"
28#include "rootdir.h"
29
30static off64_t vbr_alignment(void)
31{
32 return get_sector_size();
33}
34
35static off64_t vbr_size(void)
36{
37 return 12 * get_sector_size();
38}
39
40static 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((off64_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
82static 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
143const struct fs_object vbr =
144{
145 .get_alignment = vbr_alignment,
146 .get_size = vbr_size,
147 .write = vbr_write,
148};