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 it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
26 #include "volume_id.h"
30 struct ntfs_super_block {
33 uint16_t bytes_per_sector;
34 uint8_t sectors_per_cluster;
35 uint16_t reserved_sectors;
37 uint16_t root_entries;
40 uint16_t sectors_per_fat;
41 uint16_t sectors_per_track;
43 uint32_t hidden_sectors;
44 uint32_t large_sectors;
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;
51 int8_t cluster_per_index_record;
53 uint8_t volume_serial[8];
55 } __attribute__((__packed__)) *ns;
57 struct master_file_table_record {
62 uint16_t sequence_number;
64 uint16_t attrs_offset;
66 uint32_t bytes_in_use;
67 uint32_t bytes_allocated;
68 } __attribute__((__packed__)) *mftr;
70 struct file_attribute {
79 uint16_t value_offset;
80 } __attribute__((__packed__)) *attr;
86 } __attribute__((__packed__)) *info;
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
94 int volume_id_probe_ntfs(struct volume_id *id, uint64_t off)
96 unsigned int sector_size;
97 unsigned int cluster_size;
100 unsigned int mft_record_size;
101 unsigned int attr_type;
102 unsigned int attr_off;
103 unsigned int attr_len;
104 unsigned int val_off;
105 unsigned int val_len;
109 dbg("probing at offset 0x%llx", (unsigned long long) off);
111 ns = (struct ntfs_super_block *) volume_id_get_buffer(id, off, 0x200);
115 if (memcmp(ns->oem_id, "NTFS", 4) != 0)
118 volume_id_set_uuid(id, ns->volume_serial, UUID_NTFS);
120 sector_size = le16_to_cpu(ns->bytes_per_sector);
121 cluster_size = ns->sectors_per_cluster * sector_size;
122 mft_cluster = le64_to_cpu(ns->mft_cluster_location);
123 mft_off = mft_cluster * cluster_size;
125 if (ns->cluster_per_mft_record < 0)
126 /* size = -log2(mft_record_size); normally 1024 Bytes */
127 mft_record_size = 1 << -ns->cluster_per_mft_record;
129 mft_record_size = ns->cluster_per_mft_record * cluster_size;
131 dbg("sectorsize 0x%x", sector_size);
132 dbg("clustersize 0x%x", cluster_size);
133 dbg("mftcluster %llu", (unsigned long long) mft_cluster);
134 dbg("mftoffset 0x%llx", (unsigned long long) mft_off);
135 dbg("cluster per mft_record %i", ns->cluster_per_mft_record);
136 dbg("mft record size %i", mft_record_size);
138 buf = volume_id_get_buffer(id, off + mft_off + (MFT_RECORD_VOLUME * mft_record_size),
143 mftr = (struct master_file_table_record*) buf;
145 dbg("mftr->magic '%c%c%c%c'", mftr->magic[0], mftr->magic[1], mftr->magic[2], mftr->magic[3]);
146 if (memcmp(mftr->magic, "FILE", 4) != 0)
149 attr_off = le16_to_cpu(mftr->attrs_offset);
150 dbg("file $Volume's attributes are at offset %i", attr_off);
153 attr = (struct file_attribute*) &buf[attr_off];
154 attr_type = le32_to_cpu(attr->type);
155 attr_len = le16_to_cpu(attr->len);
156 val_off = le16_to_cpu(attr->value_offset);
157 val_len = le32_to_cpu(attr->value_len);
158 attr_off += attr_len;
163 if (attr_off >= mft_record_size)
166 if (attr_type == MFT_RECORD_ATTR_END)
169 dbg("found attribute type 0x%x, len %i, at offset %i",
170 attr_type, attr_len, attr_off);
172 if (attr_type == MFT_RECORD_ATTR_VOLUME_INFO) {
173 dbg("found info, len %i", val_len);
174 info = (struct volume_info*) (((uint8_t *) attr) + val_off);
175 snprintf(id->type_version, sizeof(id->type_version)-1,
176 "%u.%u", info->major_ver, info->minor_ver);
179 if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) {
180 dbg("found label, len %i", val_len);
181 if (val_len > VOLUME_ID_LABEL_SIZE)
182 val_len = VOLUME_ID_LABEL_SIZE;
184 val = ((uint8_t *) attr) + val_off;
185 volume_id_set_label_raw(id, val, val_len);
186 volume_id_set_label_unicode16(id, val, LE, val_len);
191 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);