chiark / gitweb /
[PATCH] udev_volume_id: volume_id version 032
[elogind.git] / extras / volume_id / volume_id / ntfs / 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 #include <asm/types.h>
36
37 #include "../volume_id.h"
38 #include "../logging.h"
39 #include "../util.h"
40 #include "ntfs.h"
41
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
47
48 int volume_id_probe_ntfs(struct volume_id *id, __u64 off)
49 {
50         struct ntfs_super_block {
51                 __u8    jump[3];
52                 __u8    oem_id[8];
53                 __u16   bytes_per_sector;
54                 __u8    sectors_per_cluster;
55                 __u16   reserved_sectors;
56                 __u8    fats;
57                 __u16   root_entries;
58                 __u16   sectors;
59                 __u8    media_type;
60                 __u16   sectors_per_fat;
61                 __u16   sectors_per_track;
62                 __u16   heads;
63                 __u32   hidden_sectors;
64                 __u32   large_sectors;
65                 __u16   unused[2];
66                 __u64   number_of_sectors;
67                 __u64   mft_cluster_location;
68                 __u64   mft_mirror_cluster_location;
69                 __s8    cluster_per_mft_record;
70                 __u8    reserved1[3];
71                 __s8    cluster_per_index_record;
72                 __u8    reserved2[3];
73                 __u8    volume_serial[8];
74                 __u16   checksum;
75         } __attribute__((__packed__)) *ns;
76
77         struct master_file_table_record {
78                 __u8    magic[4];
79                 __u16   usa_ofs;
80                 __u16   usa_count;
81                 __u64   lsn;
82                 __u16   sequence_number;
83                 __u16   link_count;
84                 __u16   attrs_offset;
85                 __u16   flags;
86                 __u32   bytes_in_use;
87                 __u32   bytes_allocated;
88         } __attribute__((__packed__)) *mftr;
89
90         struct file_attribute {
91                 __u32   type;
92                 __u32   len;
93                 __u8    non_resident;
94                 __u8    name_len;
95                 __u16   name_offset;
96                 __u16   flags;
97                 __u16   instance;
98                 __u32   value_len;
99                 __u16   value_offset;
100         } __attribute__((__packed__)) *attr;
101
102         struct volume_info {
103                 __u64 reserved;
104                 __u8 major_ver;
105                 __u8 minor_ver;
106         } __attribute__((__packed__)) *info;
107
108         unsigned int sector_size;
109         unsigned int cluster_size;
110         __u64 mft_cluster;
111         __u64 mft_off;
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;
118         const __u8 *buf;
119         const __u8 *val;
120
121         ns = (struct ntfs_super_block *) volume_id_get_buffer(id, off, 0x200);
122         if (ns == NULL)
123                 return -1;
124
125         if (memcmp(ns->oem_id, "NTFS", 4) != 0)
126                 return -1;
127
128         volume_id_set_uuid(id, ns->volume_serial, UUID_NTFS);
129
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;
134
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;
138         else
139                 mft_record_size = ns->cluster_per_mft_record * cluster_size;
140
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);
147
148         buf = volume_id_get_buffer(id, off + mft_off + (MFT_RECORD_VOLUME * mft_record_size),
149                          mft_record_size);
150         if (buf == NULL)
151                 goto found;
152
153         mftr = (struct master_file_table_record*) buf;
154
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)
157                 goto found;
158
159         attr_off = le16_to_cpu(mftr->attrs_offset);
160         dbg("file $Volume's attributes are at offset %i", attr_off);
161
162         while (1) {
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;
169
170                 if (attr_len == 0)
171                         break;
172
173                 if (attr_off >= mft_record_size)
174                         break;
175
176                 if (attr_type == MFT_RECORD_ATTR_END)
177                         break;
178
179                 dbg("found attribute type 0x%x, len %i, at offset %i",
180                     attr_type, attr_len, attr_off);
181
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);
187                 }
188
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;
193
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);
197                 }
198         }
199
200 found:
201         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
202         id->type = "ntfs";
203
204         return 0;
205 }