2 * volume_id - reads filesystem label and uuid
4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "libvolume_id.h"
32 #include "libvolume_id-private.h"
34 static struct ntfs_super_block {
37 uint16_t bytes_per_sector;
38 uint8_t sectors_per_cluster;
39 uint16_t reserved_sectors;
41 uint16_t root_entries;
44 uint16_t sectors_per_fat;
45 uint16_t sectors_per_track;
47 uint32_t hidden_sectors;
48 uint32_t large_sectors;
50 uint64_t number_of_sectors;
51 uint64_t mft_cluster_location;
52 uint64_t mft_mirror_cluster_location;
53 int8_t cluster_per_mft_record;
55 int8_t cluster_per_index_record;
57 uint8_t volume_serial[8];
61 static struct master_file_table_record {
66 uint16_t sequence_number;
68 uint16_t attrs_offset;
70 uint32_t bytes_in_use;
71 uint32_t bytes_allocated;
74 static struct file_attribute {
83 uint16_t value_offset;
86 static struct volume_info {
92 #define MFT_RECORD_VOLUME 3
93 #define MFT_RECORD_ATTR_VOLUME_NAME 0x60
94 #define MFT_RECORD_ATTR_VOLUME_INFO 0x70
95 #define MFT_RECORD_ATTR_OBJECT_ID 0x40
96 #define MFT_RECORD_ATTR_END 0xffffffffu
98 int volume_id_probe_ntfs(struct volume_id *id, uint64_t off, uint64_t size)
100 unsigned int sector_size;
101 unsigned int cluster_size;
102 uint64_t mft_cluster;
104 unsigned int mft_record_size;
105 unsigned int attr_type;
106 unsigned int attr_off;
107 unsigned int attr_len;
108 unsigned int val_off;
109 unsigned int val_len;
113 info("probing at offset 0x%llx\n", (unsigned long long) off);
115 ns = (struct ntfs_super_block *) volume_id_get_buffer(id, off, 0x200);
119 if (memcmp(ns->oem_id, "NTFS", 4) != 0)
122 volume_id_set_uuid(id, ns->volume_serial, 0, UUID_64BIT_LE);
124 sector_size = le16_to_cpu(ns->bytes_per_sector);
125 if (sector_size < 0x200)
128 cluster_size = ns->sectors_per_cluster * sector_size;
129 mft_cluster = le64_to_cpu(ns->mft_cluster_location);
130 mft_off = mft_cluster * cluster_size;
132 if (ns->cluster_per_mft_record < 0)
133 /* size = -log2(mft_record_size); normally 1024 Bytes */
134 mft_record_size = 1 << -ns->cluster_per_mft_record;
136 mft_record_size = ns->cluster_per_mft_record * cluster_size;
138 dbg("sectorsize 0x%x\n", sector_size);
139 dbg("clustersize 0x%x\n", cluster_size);
140 dbg("mftcluster %llu\n", (unsigned long long) mft_cluster);
141 dbg("mftoffset 0x%llx\n", (unsigned long long) mft_off);
142 dbg("cluster per mft_record %i\n", ns->cluster_per_mft_record);
143 dbg("mft record size %i\n", mft_record_size);
145 buf = volume_id_get_buffer(id, off + mft_off + (MFT_RECORD_VOLUME * mft_record_size),
150 mftr = (struct master_file_table_record*) buf;
151 dbg("mftr->magic '%c%c%c%c'\n", mftr->magic[0], mftr->magic[1], mftr->magic[2], mftr->magic[3]);
152 if (memcmp(mftr->magic, "FILE", 4) != 0)
155 attr_off = le16_to_cpu(mftr->attrs_offset);
156 dbg("file $Volume's attributes are at offset %i\n", attr_off);
159 attr = (struct file_attribute*) &buf[attr_off];
160 attr_type = le32_to_cpu(attr->type);
161 attr_len = le16_to_cpu(attr->len);
162 val_off = le16_to_cpu(attr->value_offset);
163 val_len = le32_to_cpu(attr->value_len);
164 attr_off += attr_len;
169 if (attr_off >= mft_record_size)
172 if (attr_type == MFT_RECORD_ATTR_END)
175 dbg("found attribute type 0x%x, len %i, at offset %i\n",
176 attr_type, attr_len, attr_off);
178 if (attr_type == MFT_RECORD_ATTR_VOLUME_INFO) {
179 dbg("found info, len %i\n", 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);
185 if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) {
186 dbg("found label, len %i\n", val_len);
187 if (val_len > VOLUME_ID_LABEL_SIZE)
188 val_len = VOLUME_ID_LABEL_SIZE;
190 val = ((uint8_t *) attr) + val_off;
191 volume_id_set_label_raw(id, val, val_len);
192 volume_id_set_label_unicode16(id, val, LE, val_len);
196 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);