blob: a45cc9003a225f3978fb17a6df50e87c7e4d631f [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.
Matt Mower09ef1e42015-12-13 11:29:45 -06006 Copyright (C) 2011-2015 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
bigbiff bigbiff9c754052013-01-09 09:09:08 -050023#include "vbr.h"
24#include "fat.h"
25#include "cbm.h"
26#include "uct.h"
27#include "rootdir.h"
Matt Mower09ef1e42015-12-13 11:29:45 -060028#include <string.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050029
Spegeliusd69ac2b2014-11-23 15:15:06 +020030static loff_t vbr_alignment(void)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050031{
32 return get_sector_size();
33}
34
Spegeliusd69ac2b2014-11-23 15:15:06 +020035static loff_t vbr_size(void)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050036{
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();
Spegeliusd69ac2b2014-11-23 15:15:06 +020046 fat_sectors = DIV_ROUND_UP((loff_t) clusters_max * sizeof(cluster_t),
bigbiff bigbiff9c754052013-01-09 09:09:08 -050047 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};