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