exfat.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * volume_id - reads filesystem label and uuid
  3. *
  4. * Copyright (C) 2012 S-G Bergh <sgb@systemasis.org>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. //kbuild:lib-$(CONFIG_FEATURE_VOLUMEID_EXFAT) += exfat.o
  21. //config:
  22. //config:config FEATURE_VOLUMEID_EXFAT
  23. //config: bool "exFAT filesystem"
  24. //config: default y
  25. //config: depends on VOLUMEID
  26. //config: help
  27. //config: exFAT (extended FAT) is a proprietary file system designed especially
  28. //config: for flash drives. It has many features from NTFS, but with less
  29. //config: overhead. exFAT is used on most SDXC cards for consumer electronics.
  30. //config:
  31. #include "volume_id_internal.h"
  32. #define EXFAT_SB_OFFSET 0
  33. #define EXFAT_DIR_ENTRY_SZ 32
  34. #define EXFAT_MAX_DIR_ENTRIES 100
  35. struct exfat_super_block {
  36. /* 0x00 */ uint8_t boot_jump[3];
  37. /* 0x03 */ uint8_t fs_name[8];
  38. /* 0x0B */ uint8_t must_be_zero[53];
  39. /* 0x40 */ uint64_t partition_offset;
  40. /* 0x48 */ uint64_t volume_length;
  41. /* 0x50 */ uint32_t fat_offset; // Sector address of 1st FAT
  42. /* 0x54 */ uint32_t fat_size; // In sectors
  43. /* 0x58 */ uint32_t cluster_heap_offset; // Sector address of Data Region
  44. /* 0x5C */ uint32_t cluster_count;
  45. /* 0x60 */ uint32_t root_dir; // Cluster address of Root Directory
  46. /* 0x64 */ uint8_t vol_serial_nr[4]; // Volume ID
  47. /* 0x68 */ uint16_t fs_revision; // VV.MM
  48. /* 0x6A */ uint16_t vol_flags;
  49. /* 0x6C */ uint8_t bytes_per_sector; // Power of 2: 9 => 512, 12 => 4096
  50. /* 0x6D */ uint8_t sectors_per_cluster; // Power of 2
  51. /* 0x6E */ uint8_t nr_of_fats; // 2 for TexFAT
  52. /* 0x6F */ // ...
  53. } PACKED;
  54. struct exfat_dir_entry {
  55. /* 0x00 */ uint8_t entry_type;
  56. union {
  57. struct volume_label {
  58. /* 0x01 */ uint8_t char_count; // Length of label
  59. /* 0x02 */ uint16_t vol_label[11]; // UTF16 string without null termination
  60. /* 0x18 */ uint8_t reserved[8];
  61. /* 0x20 */ } PACKED label;
  62. struct volume_guid {
  63. /* 0x01 */ uint8_t sec_count;
  64. /* 0x02 */ uint16_t set_checksum;
  65. /* 0x04 */ uint16_t flags;
  66. /* 0x06 */ uint8_t vol_guid[16];
  67. /* 0x16 */ uint8_t reserved[10];
  68. /* 0x20 */ } PACKED guid;
  69. } PACKED type;
  70. } PACKED;
  71. int FAST_FUNC volume_id_probe_exfat(struct volume_id *id /*,uint64_t off*/)
  72. {
  73. struct exfat_super_block *sb;
  74. struct exfat_dir_entry *de;
  75. unsigned sector_sz;
  76. unsigned cluster_sz;
  77. uint64_t root_dir_off;
  78. unsigned count;
  79. unsigned need_lbl_guid;
  80. // Primary super block
  81. dbg("exFAT: probing at offset 0x%x", EXFAT_SB_OFFSET);
  82. sb = volume_id_get_buffer(id, EXFAT_SB_OFFSET, sizeof(*sb));
  83. if (!sb)
  84. return -1;
  85. if (memcmp(sb->fs_name, "EXFAT ", 8) != 0)
  86. return -1;
  87. sector_sz = 1 << sb->bytes_per_sector;
  88. cluster_sz = sector_sz << sb->sectors_per_cluster;
  89. // There are no clusters 0 and 1, so the first cluster is 2.
  90. root_dir_off = (uint64_t)EXFAT_SB_OFFSET +
  91. // Hmm... should we cast sector_sz/cluster_sz to uint64_t?
  92. (le32_to_cpu(sb->cluster_heap_offset)) * sector_sz +
  93. (le32_to_cpu(sb->root_dir) - 2) * cluster_sz;
  94. dbg("exFAT: sector size 0x%x bytes", sector_sz);
  95. dbg("exFAT: cluster size 0x%x bytes", cluster_sz);
  96. dbg("exFAT: root dir is at 0x%llx", (long long)root_dir_off);
  97. // Use DOS uuid as fallback, if no GUID set
  98. volume_id_set_uuid(id, sb->vol_serial_nr, UUID_DOS);
  99. // EXFAT_MAX_DIR_ENTRIES is used as a safety belt.
  100. // The Root Directory may hold an unlimited number of entries,
  101. // so we do not want to check all. Usually label and GUID
  102. // are in the beginning, but there are no guarantees.
  103. need_lbl_guid = (1 << 0) | (1 << 1);
  104. for (count = 0; count < EXFAT_MAX_DIR_ENTRIES; count++) {
  105. de = volume_id_get_buffer(id, root_dir_off + (count * EXFAT_DIR_ENTRY_SZ), EXFAT_DIR_ENTRY_SZ);
  106. if (de == NULL)
  107. break;
  108. if (de->entry_type == 0x00) {
  109. // End of Directory Marker
  110. dbg("exFAT: End of root directory reached after %u entries", count);
  111. break;
  112. }
  113. if (de->entry_type == 0x83) {
  114. // Volume Label Directory Entry
  115. volume_id_set_label_unicode16(id, (uint8_t *)de->type.label.vol_label,
  116. LE, 2 * de->type.label.char_count);
  117. need_lbl_guid &= ~(1 << 0);
  118. }
  119. if (de->entry_type == 0xA0) {
  120. // Volume GUID Directory Entry
  121. volume_id_set_uuid(id, de->type.guid.vol_guid, UUID_DCE);
  122. need_lbl_guid &= ~(1 << 1);
  123. }
  124. if (!need_lbl_guid)
  125. break;
  126. }
  127. IF_FEATURE_BLKID_TYPE(id->type = "exfat";)
  128. return 0;
  129. }