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