fat.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. fat.c (09.11.10)
  3. File Allocation Table 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 <unistd.h>
  17. #include "fat.h"
  18. #include "cbm.h"
  19. #include "uct.h"
  20. #include "rootdir.h"
  21. static off_t fat_alignment(void)
  22. {
  23. return (off_t) 128 * get_sector_size();
  24. }
  25. static off_t fat_size(void)
  26. {
  27. return get_volume_size() / get_cluster_size() * sizeof(cluster_t);
  28. }
  29. static cluster_t fat_write_entry(struct exfat_dev* dev, cluster_t cluster,
  30. cluster_t value)
  31. {
  32. le32_t fat_entry = cpu_to_le32(value);
  33. if (exfat_write(dev, &fat_entry, sizeof(fat_entry)) < 0)
  34. {
  35. exfat_error("failed to write FAT entry 0x%x", value);
  36. return 0;
  37. }
  38. return cluster + 1;
  39. }
  40. static cluster_t fat_write_entries(struct exfat_dev* dev, cluster_t cluster,
  41. uint64_t length)
  42. {
  43. cluster_t end = cluster + DIV_ROUND_UP(length, get_cluster_size());
  44. while (cluster < end - 1)
  45. {
  46. cluster = fat_write_entry(dev, cluster, cluster + 1);
  47. if (cluster == 0)
  48. return 0;
  49. }
  50. return fat_write_entry(dev, cluster, EXFAT_CLUSTER_END);
  51. }
  52. static int fat_write(struct exfat_dev* dev)
  53. {
  54. cluster_t c = 0;
  55. if (!(c = fat_write_entry(dev, c, 0xfffffff8))) /* media type */
  56. return 1;
  57. if (!(c = fat_write_entry(dev, c, 0xffffffff))) /* some weird constant */
  58. return 1;
  59. if (!(c = fat_write_entries(dev, c, cbm.get_size())))
  60. return 1;
  61. if (!(c = fat_write_entries(dev, c, uct.get_size())))
  62. return 1;
  63. if (!(c = fat_write_entries(dev, c, rootdir.get_size())))
  64. return 1;
  65. return 0;
  66. }
  67. const struct fs_object fat =
  68. {
  69. .get_alignment = fat_alignment,
  70. .get_size = fat_size,
  71. .write = fat_write,
  72. };