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