chiark / gitweb /
Merge rsync://rsync.kernel.org/pub/scm/linux/hotplug/udev
[elogind.git] / extras / volume_id / volume_id / ntfs.c
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
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE 1
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <ctype.h>
35
36 #include "volume_id.h"
37 #include "logging.h"
38 #include "util.h"
39 #include "ntfs.h"
40
41 struct ntfs_super_block {
42         uint8_t         jump[3];
43         uint8_t         oem_id[8];
44         uint16_t        bytes_per_sector;
45         uint8_t         sectors_per_cluster;
46         uint16_t        reserved_sectors;
47         uint8_t         fats;
48         uint16_t        root_entries;
49         uint16_t        sectors;
50         uint8_t         media_type;
51         uint16_t        sectors_per_fat;
52         uint16_t        sectors_per_track;
53         uint16_t        heads;
54         uint32_t        hidden_sectors;
55         uint32_t        large_sectors;
56         uint16_t        unused[2];
57         uint64_t        number_of_sectors;
58         uint64_t        mft_cluster_location;
59         uint64_t        mft_mirror_cluster_location;
60         int8_t          cluster_per_mft_record;
61         uint8_t         reserved1[3];
62         int8_t          cluster_per_index_record;
63         uint8_t         reserved2[3];
64         uint8_t         volume_serial[8];
65         uint16_t        checksum;
66 } __attribute__((__packed__)) *ns;
67
68 struct master_file_table_record {
69         uint8_t         magic[4];
70         uint16_t        usa_ofs;
71         uint16_t        usa_count;
72         uint64_t        lsn;
73         uint16_t        sequence_number;
74         uint16_t        link_count;
75         uint16_t        attrs_offset;
76         uint16_t        flags;
77         uint32_t        bytes_in_use;
78         uint32_t        bytes_allocated;
79 } __attribute__((__packed__)) *mftr;
80
81 struct file_attribute {
82         uint32_t        type;
83         uint32_t        len;
84         uint8_t         non_resident;
85         uint8_t         name_len;
86         uint16_t        name_offset;
87         uint16_t        flags;
88         uint16_t        instance;
89         uint32_t        value_len;
90         uint16_t        value_offset;
91 } __attribute__((__packed__)) *attr;
92
93 struct volume_info {
94         uint64_t        reserved;
95         uint8_t         major_ver;
96         uint8_t         minor_ver;
97 } __attribute__((__packed__)) *info;
98
99 #define MFT_RECORD_VOLUME                       3
100 #define MFT_RECORD_ATTR_VOLUME_NAME             0x60
101 #define MFT_RECORD_ATTR_VOLUME_INFO             0x70
102 #define MFT_RECORD_ATTR_OBJECT_ID               0x40
103 #define MFT_RECORD_ATTR_END                     0xffffffffu
104
105 int volume_id_probe_ntfs(struct volume_id *id, uint64_t off)
106 {
107         unsigned int sector_size;
108         unsigned int cluster_size;
109         uint64_t mft_cluster;
110         uint64_t mft_off;
111         unsigned int mft_record_size;
112         unsigned int attr_type;
113         unsigned int attr_off;
114         unsigned int attr_len;
115         unsigned int val_off;
116         unsigned int val_len;
117         const uint8_t *buf;
118         const uint8_t *val;
119
120         dbg("probing at offset 0x%llx", (unsigned long long) off);
121
122         ns = (struct ntfs_super_block *) volume_id_get_buffer(id, off, 0x200);
123         if (ns == NULL)
124                 return -1;
125
126         if (memcmp(ns->oem_id, "NTFS", 4) != 0)
127                 return -1;
128
129         volume_id_set_uuid(id, ns->volume_serial, UUID_NTFS);
130
131         sector_size = le16_to_cpu(ns->bytes_per_sector);
132         cluster_size = ns->sectors_per_cluster * sector_size;
133         mft_cluster = le64_to_cpu(ns->mft_cluster_location);
134         mft_off = mft_cluster * cluster_size;
135
136         if (ns->cluster_per_mft_record < 0)
137                 /* size = -log2(mft_record_size); normally 1024 Bytes */
138                 mft_record_size = 1 << -ns->cluster_per_mft_record;
139         else
140                 mft_record_size = ns->cluster_per_mft_record * cluster_size;
141
142         dbg("sectorsize  0x%x", sector_size);
143         dbg("clustersize 0x%x", cluster_size);
144         dbg("mftcluster  %llu", (unsigned long long) mft_cluster);
145         dbg("mftoffset  0x%llx", (unsigned long long) mft_off);
146         dbg("cluster per mft_record  %i", ns->cluster_per_mft_record);
147         dbg("mft record size  %i", mft_record_size);
148
149         buf = volume_id_get_buffer(id, off + mft_off + (MFT_RECORD_VOLUME * mft_record_size),
150                          mft_record_size);
151         if (buf == NULL)
152                 goto found;
153
154         mftr = (struct master_file_table_record*) buf;
155
156         dbg("mftr->magic '%c%c%c%c'", mftr->magic[0], mftr->magic[1], mftr->magic[2], mftr->magic[3]);
157         if (memcmp(mftr->magic, "FILE", 4) != 0)
158                 goto found;
159
160         attr_off = le16_to_cpu(mftr->attrs_offset);
161         dbg("file $Volume's attributes are at offset %i", attr_off);
162
163         while (1) {
164                 attr = (struct file_attribute*) &buf[attr_off];
165                 attr_type = le32_to_cpu(attr->type);
166                 attr_len = le16_to_cpu(attr->len);
167                 val_off = le16_to_cpu(attr->value_offset);
168                 val_len = le32_to_cpu(attr->value_len);
169                 attr_off += attr_len;
170
171                 if (attr_len == 0)
172                         break;
173
174                 if (attr_off >= mft_record_size)
175                         break;
176
177                 if (attr_type == MFT_RECORD_ATTR_END)
178                         break;
179
180                 dbg("found attribute type 0x%x, len %i, at offset %i",
181                     attr_type, attr_len, attr_off);
182
183                 if (attr_type == MFT_RECORD_ATTR_VOLUME_INFO) {
184                         dbg("found info, len %i", val_len);
185                         info = (struct volume_info*) (((uint8_t *) attr) + val_off);
186                         snprintf(id->type_version, sizeof(id->type_version)-1,
187                                  "%u.%u", info->major_ver, info->minor_ver);
188                 }
189
190                 if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) {
191                         dbg("found label, len %i", val_len);
192                         if (val_len > VOLUME_ID_LABEL_SIZE)
193                                 val_len = VOLUME_ID_LABEL_SIZE;
194
195                         val = ((uint8_t *) attr) + val_off;
196                         volume_id_set_label_raw(id, val, val_len);
197                         volume_id_set_label_unicode16(id, val, LE, val_len);
198                 }
199         }
200
201 found:
202         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
203         id->type = "ntfs";
204
205         return 0;
206 }