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