bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1 | /* |
| 2 | cbm.c (09.11.10) |
| 3 | Clusters Bitmap creation code. |
| 4 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 5 | Free exFAT implementation. |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 6 | Copyright (C) 2011-2015 Andrew Nayenko |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 7 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 8 | This program is free software; you can redistribute it and/or modify |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 9 | it under the terms of the GNU General Public License as published by |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 10 | the Free Software Foundation, either version 2 of the License, or |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 11 | (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 bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 18 | 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 bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 21 | */ |
| 22 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 23 | #include "cbm.h" |
| 24 | #include "fat.h" |
| 25 | #include "uct.h" |
| 26 | #include "rootdir.h" |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 27 | #include <limits.h> |
| 28 | #include <string.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 29 | |
Spegelius | d69ac2b | 2014-11-23 15:15:06 +0200 | [diff] [blame] | 30 | static loff_t cbm_alignment(void) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 31 | { |
| 32 | return get_cluster_size(); |
| 33 | } |
| 34 | |
Spegelius | d69ac2b | 2014-11-23 15:15:06 +0200 | [diff] [blame] | 35 | static loff_t cbm_size(void) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 36 | { |
| 37 | return DIV_ROUND_UP( |
| 38 | (get_volume_size() - get_position(&cbm)) / get_cluster_size(), |
| 39 | CHAR_BIT); |
| 40 | } |
| 41 | |
| 42 | static 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()); |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 48 | size_t bitmap_size = ROUND_UP(allocated_clusters, CHAR_BIT); |
| 49 | bitmap_t* bitmap = malloc(BMAP_SIZE(bitmap_size)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 50 | size_t i; |
| 51 | |
| 52 | if (bitmap == NULL) |
| 53 | { |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 54 | exfat_error("failed to allocate bitmap of %zu bytes", |
| 55 | BMAP_SIZE(bitmap_size)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 56 | return 1; |
| 57 | } |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 58 | memset(bitmap, 0, BMAP_SIZE(bitmap_size)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 59 | |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 60 | for (i = 0; i < bitmap_size; i++) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 61 | if (i < allocated_clusters) |
| 62 | BMAP_SET(bitmap, i); |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 63 | if (exfat_write(dev, bitmap, bitmap_size / CHAR_BIT) < 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 64 | { |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 65 | free(bitmap); |
| 66 | exfat_error("failed to write bitmap of %zu bytes", |
| 67 | bitmap_size / CHAR_BIT); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 68 | return 1; |
| 69 | } |
| 70 | free(bitmap); |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | const struct fs_object cbm = |
| 75 | { |
| 76 | .get_alignment = cbm_alignment, |
| 77 | .get_size = cbm_size, |
| 78 | .write = cbm_write, |
| 79 | }; |