2 * volume_id - reads filesystem label and uuid
4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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
35 #include <asm/types.h>
37 #include "../volume_id.h"
38 #include "../logging.h"
42 #define MFT_RECORD_VOLUME 3
43 #define MFT_RECORD_ATTR_VOLUME_NAME 0x60
44 #define MFT_RECORD_ATTR_VOLUME_INFO 0x70
45 #define MFT_RECORD_ATTR_OBJECT_ID 0x40
46 #define MFT_RECORD_ATTR_END 0xffffffffu
48 int volume_id_probe_ntfs(struct volume_id *id, __u64 off)
50 struct ntfs_super_block {
53 __u16 bytes_per_sector;
54 __u8 sectors_per_cluster;
55 __u16 reserved_sectors;
60 __u16 sectors_per_fat;
61 __u16 sectors_per_track;
66 __u64 number_of_sectors;
67 __u64 mft_cluster_location;
68 __u64 mft_mirror_cluster_location;
69 __s8 cluster_per_mft_record;
71 __s8 cluster_per_index_record;
73 __u8 volume_serial[8];
75 } __attribute__((__packed__)) *ns;
77 struct master_file_table_record {
82 __u16 sequence_number;
87 __u32 bytes_allocated;
88 } __attribute__((__packed__)) *mftr;
90 struct file_attribute {
100 } __attribute__((__packed__)) *attr;
106 } __attribute__((__packed__)) *info;
108 unsigned int sector_size;
109 unsigned int cluster_size;
112 unsigned int mft_record_size;
113 unsigned int attr_type;
114 unsigned int attr_off;
115 unsigned int attr_len;
116 unsigned int val_off;
117 unsigned int val_len;
121 ns = (struct ntfs_super_block *) volume_id_get_buffer(id, off, 0x200);
125 if (memcmp(ns->oem_id, "NTFS", 4) != 0)
128 volume_id_set_uuid(id, ns->volume_serial, UUID_NTFS);
130 sector_size = le16_to_cpu(ns->bytes_per_sector);
131 cluster_size = ns->sectors_per_cluster * sector_size;
132 mft_cluster = le64_to_cpu(ns->mft_cluster_location);
133 mft_off = mft_cluster * cluster_size;
135 if (ns->cluster_per_mft_record < 0)
136 /* size = -log2(mft_record_size); normally 1024 Bytes */
137 mft_record_size = 1 << -ns->cluster_per_mft_record;
139 mft_record_size = ns->cluster_per_mft_record * cluster_size;
141 dbg("sectorsize 0x%x", sector_size);
142 dbg("clustersize 0x%x", cluster_size);
143 dbg("mftcluster %lli", mft_cluster);
144 dbg("mftoffset 0x%llx", mft_off);
145 dbg("cluster per mft_record %i", ns->cluster_per_mft_record);
146 dbg("mft record size %i", mft_record_size);
148 buf = volume_id_get_buffer(id, off + mft_off + (MFT_RECORD_VOLUME * mft_record_size),
153 mftr = (struct master_file_table_record*) buf;
155 dbg("mftr->magic '%c%c%c%c'", mftr->magic[0], mftr->magic[1], mftr->magic[2], mftr->magic[3]);
156 if (memcmp(mftr->magic, "FILE", 4) != 0)
159 attr_off = le16_to_cpu(mftr->attrs_offset);
160 dbg("file $Volume's attributes are at offset %i", attr_off);
163 attr = (struct file_attribute*) &buf[attr_off];
164 attr_type = le32_to_cpu(attr->type);
165 attr_len = le16_to_cpu(attr->len);
166 val_off = le16_to_cpu(attr->value_offset);
167 val_len = le32_to_cpu(attr->value_len);
168 attr_off += attr_len;
173 if (attr_off >= mft_record_size)
176 if (attr_type == MFT_RECORD_ATTR_END)
179 dbg("found attribute type 0x%x, len %i, at offset %i",
180 attr_type, attr_len, attr_off);
182 if (attr_type == MFT_RECORD_ATTR_VOLUME_INFO) {
183 dbg("found info, len %i", val_len);
184 info = (struct volume_info*) (((__u8 *) attr) + val_off);
185 snprintf(id->type_version, VOLUME_ID_FORMAT_SIZE-1,
186 "%u.%u", info->major_ver, info->minor_ver);
189 if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) {
190 dbg("found label, len %i", val_len);
191 if (val_len > VOLUME_ID_LABEL_SIZE)
192 val_len = VOLUME_ID_LABEL_SIZE;
194 val = ((__u8 *) attr) + val_off;
195 volume_id_set_label_raw(id, val, val_len);
196 volume_id_set_label_unicode16(id, val, LE, val_len);
201 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);