cbm.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. cbm.c (09.11.10)
  3. Clusters Bitmap creation code.
  4. Copyright (C) 2011-2013 Andrew Nayenko
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <limits.h>
  17. #include "cbm.h"
  18. #include "fat.h"
  19. #include "uct.h"
  20. #include "rootdir.h"
  21. static off_t cbm_alignment(void)
  22. {
  23. return get_cluster_size();
  24. }
  25. static off_t cbm_size(void)
  26. {
  27. return DIV_ROUND_UP(
  28. (get_volume_size() - get_position(&cbm)) / get_cluster_size(),
  29. CHAR_BIT);
  30. }
  31. static int cbm_write(struct exfat_dev* dev)
  32. {
  33. uint32_t allocated_clusters =
  34. DIV_ROUND_UP(cbm.get_size(), get_cluster_size()) +
  35. DIV_ROUND_UP(uct.get_size(), get_cluster_size()) +
  36. DIV_ROUND_UP(rootdir.get_size(), get_cluster_size());
  37. size_t bitmap_size = DIV_ROUND_UP(allocated_clusters, CHAR_BIT);
  38. uint8_t* bitmap = malloc(bitmap_size);
  39. size_t i;
  40. if (bitmap == NULL)
  41. {
  42. exfat_error("failed to allocate bitmap of %zu bytes", bitmap_size);
  43. return 1;
  44. }
  45. for (i = 0; i < bitmap_size * CHAR_BIT; i++)
  46. if (i < allocated_clusters)
  47. BMAP_SET(bitmap, i);
  48. else
  49. BMAP_CLR(bitmap, i);
  50. if (exfat_write(dev, bitmap, bitmap_size) < 0)
  51. {
  52. exfat_error("failed to write bitmap of %zu bytes", bitmap_size);
  53. return 1;
  54. }
  55. free(bitmap);
  56. return 0;
  57. }
  58. const struct fs_object cbm =
  59. {
  60. .get_alignment = cbm_alignment,
  61. .get_size = cbm_size,
  62. .write = cbm_write,
  63. };