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