ntfs.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * volume_id - reads filesystem label and uuid
  3. *
  4. * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.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. //config:config FEATURE_VOLUMEID_NTFS
  21. //config: bool "ntfs filesystem"
  22. //config: default y
  23. //config: depends on VOLUMEID
  24. //kbuild:lib-$(CONFIG_FEATURE_VOLUMEID_NTFS) += ntfs.o
  25. #include "volume_id_internal.h"
  26. struct ntfs_super_block {
  27. uint8_t jump[3];
  28. uint8_t oem_id[8];
  29. uint16_t bytes_per_sector;
  30. uint8_t sectors_per_cluster;
  31. uint16_t reserved_sectors;
  32. uint8_t fats;
  33. uint16_t root_entries;
  34. uint16_t sectors;
  35. uint8_t media_type;
  36. uint16_t sectors_per_fat;
  37. uint16_t sectors_per_track;
  38. uint16_t heads;
  39. uint32_t hidden_sectors;
  40. uint32_t large_sectors;
  41. uint16_t unused[2];
  42. uint64_t number_of_sectors;
  43. uint64_t mft_cluster_location;
  44. uint64_t mft_mirror_cluster_location;
  45. int8_t cluster_per_mft_record;
  46. uint8_t reserved1[3];
  47. int8_t cluster_per_index_record;
  48. uint8_t reserved2[3];
  49. uint8_t volume_serial[8];
  50. uint16_t checksum;
  51. } PACKED;
  52. struct master_file_table_record {
  53. uint8_t magic[4];
  54. uint16_t usa_ofs;
  55. uint16_t usa_count;
  56. uint64_t lsn;
  57. uint16_t sequence_number;
  58. uint16_t link_count;
  59. uint16_t attrs_offset;
  60. uint16_t flags;
  61. uint32_t bytes_in_use;
  62. uint32_t bytes_allocated;
  63. } PACKED;
  64. struct file_attribute {
  65. uint32_t type;
  66. uint32_t len;
  67. uint8_t non_resident;
  68. uint8_t name_len;
  69. uint16_t name_offset;
  70. uint16_t flags;
  71. uint16_t instance;
  72. uint32_t value_len;
  73. uint16_t value_offset;
  74. } PACKED;
  75. struct volume_info {
  76. uint64_t reserved;
  77. uint8_t major_ver;
  78. uint8_t minor_ver;
  79. } PACKED;
  80. #define MFT_RECORD_VOLUME 3
  81. #define MFT_RECORD_ATTR_VOLUME_NAME 0x60
  82. #define MFT_RECORD_ATTR_VOLUME_INFO 0x70
  83. #define MFT_RECORD_ATTR_OBJECT_ID 0x40
  84. #define MFT_RECORD_ATTR_END 0xffffffffu
  85. int FAST_FUNC volume_id_probe_ntfs(struct volume_id *id /*,uint64_t off*/)
  86. {
  87. #define off ((uint64_t)0)
  88. unsigned sector_size;
  89. unsigned cluster_size;
  90. uint64_t mft_cluster;
  91. uint64_t mft_off;
  92. unsigned mft_record_size;
  93. unsigned attr_type;
  94. unsigned attr_off;
  95. unsigned attr_len;
  96. unsigned val_off;
  97. unsigned val_len;
  98. struct master_file_table_record *mftr;
  99. struct ntfs_super_block *ns;
  100. const uint8_t *buf;
  101. const uint8_t *val;
  102. dbg("probing at offset 0x%llx", (unsigned long long) off);
  103. ns = volume_id_get_buffer(id, off, 0x200);
  104. if (ns == NULL)
  105. return -1;
  106. if (memcmp(ns->oem_id, "NTFS", 4) != 0)
  107. return -1;
  108. volume_id_set_uuid(id, ns->volume_serial, UUID_NTFS);
  109. sector_size = le16_to_cpu(ns->bytes_per_sector);
  110. cluster_size = ns->sectors_per_cluster * sector_size;
  111. mft_cluster = le64_to_cpu(ns->mft_cluster_location);
  112. mft_off = mft_cluster * cluster_size;
  113. if (ns->cluster_per_mft_record < 0)
  114. /* size = -log2(mft_record_size); normally 1024 Bytes */
  115. mft_record_size = 1 << -ns->cluster_per_mft_record;
  116. else
  117. mft_record_size = ns->cluster_per_mft_record * cluster_size;
  118. dbg("sectorsize 0x%x", sector_size);
  119. dbg("clustersize 0x%x", cluster_size);
  120. dbg("mftcluster %llu", (unsigned long long) mft_cluster);
  121. dbg("mftoffset 0x%llx", (unsigned long long) mft_off);
  122. dbg("cluster per mft_record %i", ns->cluster_per_mft_record);
  123. dbg("mft record size %i", mft_record_size);
  124. buf = volume_id_get_buffer(id, off + mft_off + (MFT_RECORD_VOLUME * mft_record_size),
  125. mft_record_size);
  126. if (buf == NULL)
  127. goto found;
  128. mftr = (struct master_file_table_record*) buf;
  129. dbg("mftr->magic '%c%c%c%c'", mftr->magic[0], mftr->magic[1], mftr->magic[2], mftr->magic[3]);
  130. if (memcmp(mftr->magic, "FILE", 4) != 0)
  131. goto found;
  132. attr_off = le16_to_cpu(mftr->attrs_offset);
  133. dbg("file $Volume's attributes are at offset %i", attr_off);
  134. while (1) {
  135. struct file_attribute *attr;
  136. attr = (struct file_attribute*) &buf[attr_off];
  137. attr_type = le32_to_cpu(attr->type);
  138. attr_len = le32_to_cpu(attr->len);
  139. val_off = le16_to_cpu(attr->value_offset);
  140. val_len = le32_to_cpu(attr->value_len);
  141. attr_off += attr_len;
  142. if (attr_len == 0)
  143. break;
  144. if (attr_off >= mft_record_size)
  145. break;
  146. if (attr_type == MFT_RECORD_ATTR_END)
  147. break;
  148. dbg("found attribute type 0x%x, len %i, at offset %i",
  149. attr_type, attr_len, attr_off);
  150. // if (attr_type == MFT_RECORD_ATTR_VOLUME_INFO) {
  151. // struct volume_info *info;
  152. // dbg("found info, len %i", val_len);
  153. // info = (struct volume_info*) (((uint8_t *) attr) + val_off);
  154. // snprintf(id->type_version, sizeof(id->type_version)-1,
  155. // "%u.%u", info->major_ver, info->minor_ver);
  156. // }
  157. if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) {
  158. dbg("found label, len %i", val_len);
  159. if (val_len > VOLUME_ID_LABEL_SIZE)
  160. val_len = VOLUME_ID_LABEL_SIZE;
  161. val = ((uint8_t *) attr) + val_off;
  162. // volume_id_set_label_raw(id, val, val_len);
  163. volume_id_set_label_unicode16(id, val, LE, val_len);
  164. }
  165. }
  166. found:
  167. // volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
  168. IF_FEATURE_BLKID_TYPE(id->type = "ntfs";)
  169. return 0;
  170. }