blob: bf85f7ed380604e5be273e9222e0cded2010d945 [file] [log] [blame]
Denis Vlasenkode7684a2008-02-18 21:08:49 +00001/*
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
Sven-Göran Bergh3b458012013-07-31 15:45:20 +020021//kbuild:lib-$(CONFIG_FEATURE_VOLUMEID_NTFS) += ntfs.o
22
Sven-Göran Bergh15d0a862013-07-31 15:57:59 +020023//config:config FEATURE_VOLUMEID_NTFS
24//config: bool "ntfs filesystem"
25//config: default y
26//config: depends on VOLUMEID
Sven-Göran Bergh15d0a862013-07-31 15:57:59 +020027
Denis Vlasenkode7684a2008-02-18 21:08:49 +000028#include "volume_id_internal.h"
29
30struct ntfs_super_block {
31 uint8_t jump[3];
32 uint8_t oem_id[8];
33 uint16_t bytes_per_sector;
34 uint8_t sectors_per_cluster;
35 uint16_t reserved_sectors;
36 uint8_t fats;
37 uint16_t root_entries;
38 uint16_t sectors;
39 uint8_t media_type;
40 uint16_t sectors_per_fat;
41 uint16_t sectors_per_track;
42 uint16_t heads;
43 uint32_t hidden_sectors;
44 uint32_t large_sectors;
45 uint16_t unused[2];
46 uint64_t number_of_sectors;
47 uint64_t mft_cluster_location;
48 uint64_t mft_mirror_cluster_location;
49 int8_t cluster_per_mft_record;
50 uint8_t reserved1[3];
51 int8_t cluster_per_index_record;
52 uint8_t reserved2[3];
53 uint8_t volume_serial[8];
54 uint16_t checksum;
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020055} PACKED;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000056
57struct master_file_table_record {
58 uint8_t magic[4];
59 uint16_t usa_ofs;
60 uint16_t usa_count;
61 uint64_t lsn;
62 uint16_t sequence_number;
63 uint16_t link_count;
64 uint16_t attrs_offset;
65 uint16_t flags;
66 uint32_t bytes_in_use;
67 uint32_t bytes_allocated;
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020068} PACKED;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000069
70struct file_attribute {
71 uint32_t type;
72 uint32_t len;
73 uint8_t non_resident;
74 uint8_t name_len;
75 uint16_t name_offset;
76 uint16_t flags;
77 uint16_t instance;
78 uint32_t value_len;
79 uint16_t value_offset;
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020080} PACKED;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000081
82struct volume_info {
83 uint64_t reserved;
84 uint8_t major_ver;
85 uint8_t minor_ver;
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020086} PACKED;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000087
88#define MFT_RECORD_VOLUME 3
89#define MFT_RECORD_ATTR_VOLUME_NAME 0x60
90#define MFT_RECORD_ATTR_VOLUME_INFO 0x70
91#define MFT_RECORD_ATTR_OBJECT_ID 0x40
92#define MFT_RECORD_ATTR_END 0xffffffffu
93
Denys Vlasenko89300962009-11-01 23:07:18 +010094int FAST_FUNC volume_id_probe_ntfs(struct volume_id *id /*,uint64_t off*/)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000095{
Denis Vlasenko28ea4292009-02-15 05:51:19 +000096#define off ((uint64_t)0)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000097 unsigned sector_size;
98 unsigned cluster_size;
99 uint64_t mft_cluster;
100 uint64_t mft_off;
101 unsigned mft_record_size;
102 unsigned attr_type;
103 unsigned attr_off;
104 unsigned attr_len;
105 unsigned val_off;
106 unsigned val_len;
107 struct master_file_table_record *mftr;
108 struct ntfs_super_block *ns;
109 const uint8_t *buf;
110 const uint8_t *val;
111
112 dbg("probing at offset 0x%llx", (unsigned long long) off);
113
114 ns = volume_id_get_buffer(id, off, 0x200);
115 if (ns == NULL)
116 return -1;
117
118 if (memcmp(ns->oem_id, "NTFS", 4) != 0)
119 return -1;
120
121 volume_id_set_uuid(id, ns->volume_serial, UUID_NTFS);
122
123 sector_size = le16_to_cpu(ns->bytes_per_sector);
124 cluster_size = ns->sectors_per_cluster * sector_size;
125 mft_cluster = le64_to_cpu(ns->mft_cluster_location);
126 mft_off = mft_cluster * cluster_size;
127
128 if (ns->cluster_per_mft_record < 0)
129 /* size = -log2(mft_record_size); normally 1024 Bytes */
130 mft_record_size = 1 << -ns->cluster_per_mft_record;
131 else
132 mft_record_size = ns->cluster_per_mft_record * cluster_size;
133
134 dbg("sectorsize 0x%x", sector_size);
135 dbg("clustersize 0x%x", cluster_size);
136 dbg("mftcluster %llu", (unsigned long long) mft_cluster);
137 dbg("mftoffset 0x%llx", (unsigned long long) mft_off);
138 dbg("cluster per mft_record %i", ns->cluster_per_mft_record);
139 dbg("mft record size %i", mft_record_size);
140
141 buf = volume_id_get_buffer(id, off + mft_off + (MFT_RECORD_VOLUME * mft_record_size),
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100142 mft_record_size);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000143 if (buf == NULL)
144 goto found;
145
146 mftr = (struct master_file_table_record*) buf;
147
148 dbg("mftr->magic '%c%c%c%c'", mftr->magic[0], mftr->magic[1], mftr->magic[2], mftr->magic[3]);
149 if (memcmp(mftr->magic, "FILE", 4) != 0)
150 goto found;
151
152 attr_off = le16_to_cpu(mftr->attrs_offset);
153 dbg("file $Volume's attributes are at offset %i", attr_off);
154
155 while (1) {
156 struct file_attribute *attr;
157
158 attr = (struct file_attribute*) &buf[attr_off];
159 attr_type = le32_to_cpu(attr->type);
Oliver Metz041baf22013-05-08 20:21:29 +0200160 attr_len = le32_to_cpu(attr->len);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000161 val_off = le16_to_cpu(attr->value_offset);
162 val_len = le32_to_cpu(attr->value_len);
163 attr_off += attr_len;
164
165 if (attr_len == 0)
166 break;
167
168 if (attr_off >= mft_record_size)
169 break;
170
171 if (attr_type == MFT_RECORD_ATTR_END)
172 break;
173
174 dbg("found attribute type 0x%x, len %i, at offset %i",
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100175 attr_type, attr_len, attr_off);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000176
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000177// if (attr_type == MFT_RECORD_ATTR_VOLUME_INFO) {
178// struct volume_info *info;
179// dbg("found info, len %i", val_len);
180// info = (struct volume_info*) (((uint8_t *) attr) + val_off);
181// snprintf(id->type_version, sizeof(id->type_version)-1,
182// "%u.%u", info->major_ver, info->minor_ver);
183// }
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000184
185 if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) {
186 dbg("found label, len %i", val_len);
187 if (val_len > VOLUME_ID_LABEL_SIZE)
188 val_len = VOLUME_ID_LABEL_SIZE;
189
190 val = ((uint8_t *) attr) + val_off;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000191// volume_id_set_label_raw(id, val, val_len);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000192 volume_id_set_label_unicode16(id, val, LE, val_len);
193 }
194 }
195
196 found:
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000197// volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
Denys Vlasenko90615a02010-12-30 00:40:11 +0100198 IF_FEATURE_BLKID_TYPE(id->type = "ntfs";)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000199
200 return 0;
201}