blob: f255fb10a89053d44483a752f251725faaffa0fc [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2 cbm.c (09.11.10)
3 Clusters Bitmap creation code.
4
bigbiff bigbiffca829c42013-01-28 08:14:25 -05005 Copyright (C) 2011-2013 Andrew Nayenko
bigbiff bigbiff9c754052013-01-09 09:09:08 -05006
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 <limits.h>
22#include "cbm.h"
23#include "fat.h"
24#include "uct.h"
25#include "rootdir.h"
26
27static off64_t cbm_alignment(void)
28{
29 return get_cluster_size();
30}
31
32static off64_t cbm_size(void)
33{
34 return DIV_ROUND_UP(
35 (get_volume_size() - get_position(&cbm)) / get_cluster_size(),
36 CHAR_BIT);
37}
38
39static int cbm_write(struct exfat_dev* dev)
40{
41 uint32_t allocated_clusters =
42 DIV_ROUND_UP(cbm.get_size(), get_cluster_size()) +
43 DIV_ROUND_UP(uct.get_size(), get_cluster_size()) +
44 DIV_ROUND_UP(rootdir.get_size(), get_cluster_size());
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 {
51 exfat_error("failed to allocate bitmap of %zu bytes", bitmap_size);
52 return 1;
53 }
54
55 for (i = 0; i < bitmap_size * CHAR_BIT; i++)
56 if (i < allocated_clusters)
57 BMAP_SET(bitmap, i);
58 else
59 BMAP_CLR(bitmap, i);
60 if (exfat_write(dev, bitmap, bitmap_size) < 0)
61 {
62 exfat_error("failed to write bitmap of %zu bytes", bitmap_size);
63 return 1;
64 }
65 free(bitmap);
66 return 0;
67}
68
69const struct fs_object cbm =
70{
71 .get_alignment = cbm_alignment,
72 .get_size = cbm_size,
73 .write = cbm_write,
74};