chiark / gitweb /
fix GGC signed pointer warnings and switch volume_id to stdint
[elogind.git] / extras / volume_id / volume_id / fat.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 "fat.h"
40
41 #define FAT12_MAX                       0xff5
42 #define FAT16_MAX                       0xfff5
43 #define FAT_ATTR_VOLUME_ID              0x08
44 #define FAT_ATTR_DIR                    0x10
45 #define FAT_ATTR_LONG_NAME              0x0f
46 #define FAT_ATTR_MASK                   0x3f
47 #define FAT_ENTRY_FREE                  0xe5
48
49 struct vfat_super_block {
50         uint8_t         boot_jump[3];
51         uint8_t         sysid[8];
52         uint16_t        sector_size;
53         uint8_t         sectors_per_cluster;
54         uint16_t        reserved;
55         uint8_t         fats;
56         uint16_t        dir_entries;
57         uint16_t        sectors;
58         uint8_t         media;
59         uint16_t        fat_length;
60         uint16_t        secs_track;
61         uint16_t        heads;
62         uint32_t        hidden;
63         uint32_t        total_sect;
64         union {
65                 struct fat_super_block {
66                         uint8_t         unknown[3];
67                         uint8_t         serno[4];
68                         uint8_t         label[11];
69                         uint8_t         magic[8];
70                         uint8_t         dummy2[192];
71                         uint8_t         pmagic[2];
72                 } __attribute__((__packed__)) fat;
73                 struct fat32_super_block {
74                         uint32_t        fat32_length;
75                         uint16_t        flags;
76                         uint8_t         version[2];
77                         uint32_t        root_cluster;
78                         uint16_t        insfo_sector;
79                         uint16_t        backup_boot;
80                         uint16_t        reserved2[6];
81                         uint8_t         unknown[3];
82                         uint8_t         serno[4];
83                         uint8_t         label[11];
84                         uint8_t         magic[8];
85                         uint8_t         dummy2[164];
86                         uint8_t         pmagic[2];
87                 } __attribute__((__packed__)) fat32;
88         } __attribute__((__packed__)) type;
89 } __attribute__((__packed__));
90
91 struct vfat_dir_entry {
92         uint8_t         name[11];
93         uint8_t         attr;
94         uint16_t        time_creat;
95         uint16_t        date_creat;
96         uint16_t        time_acc;
97         uint16_t        date_acc;
98         uint16_t        cluster_high;
99         uint16_t        time_write;
100         uint16_t        date_write;
101         uint16_t        cluster_low;
102         uint32_t        size;
103 } __attribute__((__packed__));
104
105 static uint8_t *get_attr_volume_id(struct vfat_dir_entry *dir, unsigned int count)
106 {
107         unsigned int i;
108
109         for (i = 0; i < count; i++) {
110                 /* end marker */
111                 if (dir[i].name[0] == 0x00) {
112                         dbg("end of dir");
113                         break;
114                 }
115
116                 /* empty entry */
117                 if (dir[i].name[0] == FAT_ENTRY_FREE)
118                         continue;
119
120                 /* long name */
121                 if ((dir[i].attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME)
122                         continue;
123
124                 if ((dir[i].attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) == FAT_ATTR_VOLUME_ID) {
125                         /* labels do not have file data */
126                         if (dir[i].cluster_high != 0 || dir[i].cluster_low != 0)
127                                 continue;
128
129                         dbg("found ATTR_VOLUME_ID id in root dir");
130                         return dir[i].name;
131                 }
132
133                 dbg("skip dir entry");
134         }
135
136         return NULL;
137 }
138
139 int volume_id_probe_vfat(struct volume_id *id, uint64_t off)
140 {
141         struct vfat_super_block *vs;
142         struct vfat_dir_entry *dir;
143         uint16_t sector_size;
144         uint16_t dir_entries;
145         uint32_t sect_count;
146         uint16_t reserved;
147         uint32_t fat_size;
148         uint32_t root_cluster;
149         uint32_t dir_size;
150         uint32_t cluster_count;
151         uint32_t fat_length;
152         uint64_t root_start;
153         uint32_t start_data_sect;
154         uint16_t root_dir_entries;
155         uint8_t *buf;
156         uint32_t buf_size;
157         uint8_t *label = NULL;
158         uint32_t next;
159         int maxloop;
160
161         dbg("probing at offset 0x%llx", (unsigned long long) off);
162
163         vs = (struct vfat_super_block *) volume_id_get_buffer(id, off, 0x200);
164         if (vs == NULL)
165                 return -1;
166
167         /* believe only that's fat, don't trust the version
168          * the cluster_count will tell us
169          */
170         if (memcmp(vs->sysid, "NTFS", 4) == 0)
171                 return -1;
172
173         if (memcmp(vs->type.fat32.magic, "MSWIN", 5) == 0)
174                 goto valid;
175
176         if (memcmp(vs->type.fat32.magic, "FAT32   ", 8) == 0)
177                 goto valid;
178
179         if (memcmp(vs->type.fat.magic, "FAT16   ", 8) == 0)
180                 goto valid;
181
182         if (memcmp(vs->type.fat.magic, "MSDOS", 5) == 0)
183                 goto valid;
184
185         if (memcmp(vs->type.fat.magic, "FAT12   ", 8) == 0)
186                 goto valid;
187
188         /*
189          * There are old floppies out there without a magic, so we check
190          * for well known values and guess if it's a fat volume
191          */
192
193         /* boot jump address check */
194         if ((vs->boot_jump[0] != 0xeb || vs->boot_jump[2] != 0x90) &&
195              vs->boot_jump[0] != 0xe9)
196                 return -1;
197
198         /* heads check */
199         if (vs->heads == 0)
200                 return -1;
201
202         /* cluster size check*/ 
203         if (vs->sectors_per_cluster == 0 ||
204             (vs->sectors_per_cluster & (vs->sectors_per_cluster-1)))
205                 return -1;
206
207         /* media check */
208         if (vs->media < 0xf8 && vs->media != 0xf0)
209                 return -1;
210
211         /* fat count*/
212         if (vs->fats != 2)
213                 return -1;
214
215 valid:
216         /* sector size check */
217         sector_size = le16_to_cpu(vs->sector_size);
218         if (sector_size != 0x200 && sector_size != 0x400 &&
219             sector_size != 0x800 && sector_size != 0x1000)
220                 return -1;
221
222         dbg("sector_size 0x%x", sector_size);
223         dbg("sectors_per_cluster 0x%x", vs->sectors_per_cluster);
224
225         dir_entries = le16_to_cpu(vs->dir_entries);
226         reserved = le16_to_cpu(vs->reserved);
227         dbg("reserved 0x%x", reserved);
228
229         sect_count = le16_to_cpu(vs->sectors);
230         if (sect_count == 0)
231                 sect_count = le32_to_cpu(vs->total_sect);
232         dbg("sect_count 0x%x", sect_count);
233
234         fat_length = le16_to_cpu(vs->fat_length);
235         if (fat_length == 0)
236                 fat_length = le32_to_cpu(vs->type.fat32.fat32_length);
237         dbg("fat_length 0x%x", fat_length);
238
239         fat_size = fat_length * vs->fats;
240         dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
241                         (sector_size-1)) / sector_size;
242         dbg("dir_size 0x%x", dir_size);
243
244         cluster_count = sect_count - (reserved + fat_size + dir_size);
245         cluster_count /= vs->sectors_per_cluster;
246         dbg("cluster_count 0x%x", cluster_count);
247
248         if (cluster_count < FAT12_MAX) {
249                 strcpy(id->type_version, "FAT12");
250         } else if (cluster_count < FAT16_MAX) {
251                 strcpy(id->type_version, "FAT16");
252         } else {
253                 strcpy(id->type_version, "FAT32");
254                 goto fat32;
255         }
256
257         /* the label may be an attribute in the root directory */
258         root_start = (reserved + fat_size) * sector_size;
259         dbg("root dir start 0x%llx", (unsigned long long) root_start);
260         root_dir_entries = le16_to_cpu(vs->dir_entries);
261         dbg("expected entries 0x%x", root_dir_entries);
262
263         buf_size = root_dir_entries * sizeof(struct vfat_dir_entry);
264         buf = volume_id_get_buffer(id, off + root_start, buf_size);
265         if (buf == NULL)
266                 goto found;
267
268         dir = (struct vfat_dir_entry*) buf;
269
270         label = get_attr_volume_id(dir, root_dir_entries);
271
272         vs = (struct vfat_super_block *) volume_id_get_buffer(id, off, 0x200);
273         if (vs == NULL)
274                 return -1;
275
276         if (label != NULL && memcmp(label, "NO NAME    ", 11) != 0) {
277                 volume_id_set_label_raw(id, label, 11);
278                 volume_id_set_label_string(id, label, 11);
279         } else if (memcmp(vs->type.fat.label, "NO NAME    ", 11) != 0) {
280                 volume_id_set_label_raw(id, vs->type.fat.label, 11);
281                 volume_id_set_label_string(id, vs->type.fat.label, 11);
282         }
283         volume_id_set_uuid(id, vs->type.fat.serno, UUID_DOS);
284         goto found;
285
286 fat32:
287         /* FAT32 root dir is a cluster chain like any other directory */
288         buf_size = vs->sectors_per_cluster * sector_size;
289         root_cluster = le32_to_cpu(vs->type.fat32.root_cluster);
290         dbg("root dir cluster %u", root_cluster);
291         start_data_sect = reserved + fat_size;
292
293         next = root_cluster;
294         maxloop = 100;
295         while (--maxloop) {
296                 uint32_t next_sect_off;
297                 uint64_t next_off;
298                 uint64_t fat_entry_off;
299                 int count;
300
301                 dbg("next cluster %u", next);
302                 next_sect_off = (next - 2) * vs->sectors_per_cluster;
303                 next_off = (start_data_sect + next_sect_off) * sector_size;
304                 dbg("cluster offset 0x%llx", (unsigned long long) next_off);
305
306                 /* get cluster */
307                 buf = volume_id_get_buffer(id, off + next_off, buf_size);
308                 if (buf == NULL)
309                         goto found;
310
311                 dir = (struct vfat_dir_entry*) buf;
312                 count = buf_size / sizeof(struct vfat_dir_entry);
313                 dbg("expected entries 0x%x", count);
314
315                 label = get_attr_volume_id(dir, count);
316                 if (label)
317                         break;
318
319                 /* get FAT entry */
320                 fat_entry_off = (reserved * sector_size) + (next * sizeof(uint32_t));
321                 buf = volume_id_get_buffer(id, off + fat_entry_off, buf_size);
322                 if (buf == NULL)
323                         goto found;
324
325                 /* set next cluster */
326                 next = le32_to_cpu(*((uint32_t *) buf) & 0x0fffffff);
327                 if (next == 0)
328                         break;
329         }
330         if (maxloop == 0)
331                 dbg("reached maximum follow count of root cluster chain, give up");
332
333         vs = (struct vfat_super_block *) volume_id_get_buffer(id, off, 0x200);
334         if (vs == NULL)
335                 return -1;
336
337         if (label != NULL && memcmp(label, "NO NAME    ", 11) != 0) {
338                 volume_id_set_label_raw(id, label, 11);
339                 volume_id_set_label_string(id, label, 11);
340         } else if (memcmp(vs->type.fat32.label, "NO NAME    ", 11) != 0) {
341                 volume_id_set_label_raw(id, vs->type.fat32.label, 11);
342                 volume_id_set_label_string(id, vs->type.fat32.label, 11);
343         }
344         volume_id_set_uuid(id, vs->type.fat32.serno, UUID_DOS);
345
346 found:
347         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
348         id->type = "vfat";
349
350         return 0;
351 }