]> git.sven.stormbind.net Git - sven/exfat-utils.git/blob - mkfs/cbm.c
Add README.source to reference quilt.
[sven/exfat-utils.git] / mkfs / cbm.c
1 /*
2         cbm.c (09.11.10)
3         Clusters Bitmap 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 <limits.h>
23 #include <inttypes.h>
24 #include <errno.h>
25 #include "mkexfat.h"
26 #include "uct.h"
27 #include "rootdir.h"
28
29 off_t cbm_alignment(void)
30 {
31         return CLUSTER_SIZE(sb);
32 }
33
34 off_t cbm_size(void)
35 {
36         return DIV_ROUND_UP(le32_to_cpu(sb.cluster_count), CHAR_BIT);
37 }
38
39 int cbm_write(struct exfat_dev* dev, off_t base)
40 {
41         uint32_t allocated_clusters =
42                         DIV_ROUND_UP(cbm_size(), CLUSTER_SIZE(sb)) +
43                         DIV_ROUND_UP(uct_size(), CLUSTER_SIZE(sb)) +
44                         DIV_ROUND_UP(rootdir_size(), CLUSTER_SIZE(sb));
45         size_t bitmap_size = DIV_ROUND_UP(allocated_clusters, CHAR_BIT);
46         uint8_t* bitmap = malloc(bitmap_size);
47         size_t i;
48
49         if (bitmap == NULL)
50                 return errno;
51
52         for (i = 0; i < bitmap_size * CHAR_BIT; i++)
53                 if (i < allocated_clusters)
54                         BMAP_SET(bitmap, i);
55                 else
56                         BMAP_CLR(bitmap, i);
57         if (exfat_write(dev, bitmap, bitmap_size) < 0)
58                 return errno;
59         free(bitmap);
60
61         sb.cluster_sector_start = cpu_to_le32(base / SECTOR_SIZE(sb));
62         bitmap_entry.start_cluster = cpu_to_le32(OFFSET_TO_CLUSTER(base));
63         bitmap_entry.size = cpu_to_le64(cbm_size());
64         return 0;
65 }