blob: 189ae32fdf5b7fbf299b37e2fde2c66196b79000 [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 bigbiff2e33c5e2014-09-04 20:58:41 -04006 Copyright (C) 2011-2014 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>
bigbiff bigbiff2e33c5e2014-09-04 20:58:41 -040024#include <string.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050025#include "cbm.h"
26#include "fat.h"
27#include "uct.h"
28#include "rootdir.h"
29
30static off64_t cbm_alignment(void)
31{
32 return get_cluster_size();
33}
34
35static off64_t cbm_size(void)
36{
37 return DIV_ROUND_UP(
38 (get_volume_size() - get_position(&cbm)) / get_cluster_size(),
39 CHAR_BIT);
40}
41
42static int cbm_write(struct exfat_dev* dev)
43{
44 uint32_t allocated_clusters =
45 DIV_ROUND_UP(cbm.get_size(), get_cluster_size()) +
46 DIV_ROUND_UP(uct.get_size(), get_cluster_size()) +
47 DIV_ROUND_UP(rootdir.get_size(), get_cluster_size());
48 size_t bitmap_size = DIV_ROUND_UP(allocated_clusters, CHAR_BIT);
bigbiff bigbiff2e33c5e2014-09-04 20:58:41 -040049 bitmap_t* bitmap = malloc(BMAP_SIZE(bitmap_size));
bigbiff bigbiff9c754052013-01-09 09:09:08 -050050 size_t i;
51
52 if (bitmap == NULL)
53 {
bigbiff bigbiff2e33c5e2014-09-04 20:58:41 -040054 exfat_error("failed to allocate bitmap of %zu bytes",
55 BMAP_SIZE(bitmap_size));
bigbiff bigbiff9c754052013-01-09 09:09:08 -050056 return 1;
57 }
bigbiff bigbiff2e33c5e2014-09-04 20:58:41 -040058 memset(bitmap, 0, BMAP_SIZE(bitmap_size));
bigbiff bigbiff9c754052013-01-09 09:09:08 -050059
60 for (i = 0; i < bitmap_size * CHAR_BIT; i++)
61 if (i < allocated_clusters)
62 BMAP_SET(bitmap, i);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050063 if (exfat_write(dev, bitmap, bitmap_size) < 0)
64 {
bigbiff bigbiff2e33c5e2014-09-04 20:58:41 -040065 free(bitmap);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050066 exfat_error("failed to write bitmap of %zu bytes", bitmap_size);
67 return 1;
68 }
69 free(bitmap);
70 return 0;
71}
72
73const struct fs_object cbm =
74{
75 .get_alignment = cbm_alignment,
76 .get_size = cbm_size,
77 .write = cbm_write,
78};