chiark / gitweb /
volume_id: add more vfat debugging information
[elogind.git] / extras / volume_id / lib / fat.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2004-2007 Kay Sievers <kay.sievers@vrfy.org>
5  * Copyright (C) 2007 Ryan Lortie <desrt@desrt.ca>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE 1
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <ctype.h>
31
32 #include "libvolume_id.h"
33 #include "libvolume_id-private.h"
34
35 #define FAT12_MAX                       0xff5
36 #define FAT16_MAX                       0xfff5
37 #define FAT_ATTR_VOLUME_ID              0x08
38 #define FAT_ATTR_DIR                    0x10
39 #define FAT_ATTR_LONG_NAME              0x0f
40 #define FAT_ATTR_MASK                   0x3f
41 #define FAT_ENTRY_FREE                  0xe5
42
43 #define VFAT_LFN_SEQ_MASK               0x3f
44 #define VFAT_LFN_SEQ_LAST               0x40
45 #define VFAT_LFN_SEQ_MAX                20
46 #define VFAT_LFN_CHARS_PER_ENTRY        (5 + 6 + 2)
47 #define VFAT_LOWERCASE_NAME             0x10
48 #define VFAT_LOWERCASE_EXT              0x08
49
50 struct vfat_super_block {
51         uint8_t         boot_jump[3];
52         uint8_t         sysid[8];
53         uint16_t        sector_size;
54         uint8_t         sectors_per_cluster;
55         uint16_t        reserved;
56         uint8_t         fats;
57         uint16_t        dir_entries;
58         uint16_t        sectors;
59         uint8_t         media;
60         uint16_t        fat_length;
61         uint16_t        secs_track;
62         uint16_t        heads;
63         uint32_t        hidden;
64         uint32_t        total_sect;
65         union {
66                 struct fat_super_block {
67                         uint8_t         unknown[3];
68                         uint8_t         serno[4];
69                         uint8_t         label[11];
70                         uint8_t         magic[8];
71                         uint8_t         dummy2[192];
72                         uint8_t         pmagic[2];
73                 } PACKED fat;
74                 struct fat32_super_block {
75                         uint32_t        fat32_length;
76                         uint16_t        flags;
77                         uint8_t         version[2];
78                         uint32_t        root_cluster;
79                         uint16_t        fsinfo_sector;
80                         uint16_t        backup_boot;
81                         uint16_t        reserved2[6];
82                         uint8_t         unknown[3];
83                         uint8_t         serno[4];
84                         uint8_t         label[11];
85                         uint8_t         magic[8];
86                         uint8_t         dummy2[164];
87                         uint8_t         pmagic[2];
88                 } PACKED fat32;
89         } PACKED type;
90 } PACKED;
91
92 struct fat32_fsinfo {
93         uint8_t signature1[4];
94         uint32_t reserved1[120];
95         uint8_t signature2[4];
96         uint32_t free_clusters;
97         uint32_t next_cluster;
98         uint32_t reserved2[4];
99 } PACKED;
100
101 struct vfat_dir_entry {
102         uint8_t         name[11];
103         uint8_t         attr;
104         uint8_t         lowercase;
105         uint8_t         fine_time_creat;
106         uint16_t        time_creat;
107         uint16_t        date_creat;
108         uint16_t        date_acc;
109         uint16_t        cluster_high;
110         uint16_t        time_write;
111         uint16_t        date_write;
112         uint16_t        cluster_low;
113         uint32_t        size;
114 } PACKED;
115
116
117 struct vfat_lfn_entry {
118         uint8_t         seq;
119         uint16_t        name0[5];
120         uint8_t         attr;
121         uint8_t         reserved;
122         uint8_t         cksum;
123         uint16_t        name1[6];
124         uint16_t        cluster;
125         uint16_t        name2[2];
126 } PACKED;
127
128 static uint8_t fat_lfn_checksum(const uint8_t name[11])
129 {
130         uint8_t cksum = 0;
131         int i;
132
133         /* http://en.wikipedia.org/wiki/File_Allocation_Table */
134         for (i = 0; i < 11; i++)
135                 cksum = ((cksum & 1) ? 0x80 : 0) + (cksum >> 1) + name[i];
136
137         return cksum;
138 }
139
140 static size_t fat_read_lfn(uint8_t *filename, size_t fnsize,
141                            struct vfat_dir_entry *dir,
142                            struct vfat_dir_entry *entry)
143 {
144         uint8_t buffer[VFAT_LFN_SEQ_MAX * VFAT_LFN_CHARS_PER_ENTRY * 2];
145         uint8_t expected_seq = 1;
146         uint8_t cksum;
147         size_t len = 0;
148         size_t fnlen = 0;
149
150         cksum = fat_lfn_checksum(entry->name);
151
152         while (--entry >= dir) {
153                 struct vfat_lfn_entry *lfn = (struct vfat_lfn_entry *) entry;
154
155                 if (expected_seq > VFAT_LFN_SEQ_MAX)
156                         break;
157
158                 if ((lfn->attr & FAT_ATTR_MASK) != FAT_ATTR_LONG_NAME)
159                         break;
160
161                 if (lfn->cksum != cksum)
162                         break;
163
164                 if ((lfn->seq & VFAT_LFN_SEQ_MASK) != expected_seq++)
165                         break;
166
167                 if (lfn->cluster != 0)
168                         break;
169
170                 /* extra paranoia -- should never happen */
171                 if (len + sizeof(lfn->name0) + sizeof(lfn->name1) +
172                     sizeof(lfn->name2) > sizeof(buffer))
173                         break;
174
175                 memcpy (&buffer[len], lfn->name0, sizeof(lfn->name0));
176                 len += sizeof(lfn->name0);
177                 memcpy (&buffer[len], lfn->name1, sizeof(lfn->name1));
178                 len += sizeof(lfn->name1);
179                 memcpy (&buffer[len], lfn->name2, sizeof(lfn->name2));
180                 len += sizeof(lfn->name2);
181
182                 if (lfn->seq & VFAT_LFN_SEQ_LAST) {
183                         fnlen = volume_id_set_unicode16(filename, fnsize, buffer, LE, len);
184                         break;
185                 }
186         }
187
188         return fnlen;
189 }
190
191 static size_t fat_read_filename(uint8_t *filename, size_t fnsize,
192                                 struct vfat_dir_entry *dir, struct vfat_dir_entry *entry)
193 {
194         size_t len;
195         int i;
196
197         /* check if maybe we have LFN entries */
198         len = fat_read_lfn(filename, fnsize, dir, entry);
199         if (len > 0)
200                 goto out;
201
202         /* else, read the normal 8.3 name */
203         for (i = 0; i < 11; i++) {
204                 if (entry->lowercase & ((i < 8) ? VFAT_LOWERCASE_NAME : VFAT_LOWERCASE_EXT))
205                         filename[i] = tolower(entry->name[i]);
206                 else
207                         filename[i] = entry->name[i];
208         }
209         len = 11;
210
211 out:
212         filename[len] = '\0';
213         return len;
214 }
215
216 /* fills filename, returns string length */
217 static size_t get_fat_attr_volume_id(uint8_t *filename, size_t fnsize,
218                                      struct vfat_dir_entry *dir, unsigned int count)
219 {
220         unsigned int i;
221
222         for (i = 0; i < count; i++) {
223                 /* end marker */
224                 if (dir[i].name[0] == 0x00) {
225                         dbg("end of dir\n");
226                         break;
227                 }
228
229                 /* empty entry */
230                 if (dir[i].name[0] == FAT_ENTRY_FREE)
231                         continue;
232
233                 /* long name */
234                 if ((dir[i].attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME)
235                         continue;
236
237                 if ((dir[i].attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) == FAT_ATTR_VOLUME_ID) {
238                         /* labels do not have file data */
239                         if (dir[i].cluster_high != 0 || dir[i].cluster_low != 0)
240                                 continue;
241
242                         dbg("found ATTR_VOLUME_ID id in root dir\n");
243                         return fat_read_filename(filename, fnsize, dir, &dir[i]);
244                 }
245
246                 dbg("skip dir entry\n");
247         }
248
249         return 0;
250 }
251
252 int volume_id_probe_vfat(struct volume_id *id, uint64_t off, uint64_t size)
253 {
254         uint8_t filename[255 * 3];
255         struct vfat_super_block *vs;
256         struct vfat_dir_entry *dir;
257         struct fat32_fsinfo *fsinfo;
258         uint16_t sector_size;
259         uint16_t dir_entries;
260         uint32_t sect_count;
261         uint16_t reserved;
262         uint32_t fat_size;
263         uint32_t root_cluster;
264         uint32_t dir_size;
265         uint32_t cluster_count;
266         uint16_t fat_length;
267         uint32_t fat32_length;
268         uint64_t root_start;
269         uint32_t start_data_sect;
270         uint16_t root_dir_entries;
271         uint16_t fsinfo_sect;
272         uint8_t *buf;
273         uint32_t buf_size;
274         uint32_t next;
275         int maxloop;
276         size_t fnlen;
277
278         info("probing at offset 0x%" PRIx64 "\n", off);
279
280         buf = volume_id_get_buffer(id, off, 0x400);
281         if (buf == NULL)
282                 return -1;
283
284         /* check signature */
285         if (buf[510] != 0x55 || buf[511] != 0xaa)
286                 return -1;
287
288         vs = (struct vfat_super_block *) buf;
289         if (memcmp(vs->sysid, "NTFS", 4) == 0)
290                 return -1;
291
292         /* believe only that's fat, don't trust the version */
293         if (memcmp(vs->type.fat32.magic, "MSWIN", 5) == 0)
294                 goto magic;
295
296         if (memcmp(vs->type.fat32.magic, "FAT32   ", 8) == 0)
297                 goto magic;
298
299         if (memcmp(vs->type.fat.magic, "FAT16   ", 8) == 0)
300                 goto magic;
301
302         if (memcmp(vs->type.fat.magic, "MSDOS", 5) == 0)
303                 goto magic;
304
305         if (memcmp(vs->type.fat.magic, "FAT12   ", 8) == 0)
306                 goto magic;
307
308         /* some old floppies don't have a magic, expect the boot jump address to match */
309         if ((vs->boot_jump[0] != 0xeb || vs->boot_jump[2] != 0x90) &&
310              vs->boot_jump[0] != 0xe9)
311                 return -1;
312
313 magic:
314         info("magic found\n");
315         /* reserverd sector count */
316         if (!vs->reserved)
317                 return -1;
318
319         /* fat count */
320         if (!vs->fats)
321                 return -1;
322
323         /* media check */
324         if (vs->media < 0xf8 && vs->media != 0xf0)
325                 return -1;
326
327         /* cluster size check */
328         if (vs->sectors_per_cluster == 0 ||
329             (vs->sectors_per_cluster & (vs->sectors_per_cluster-1)))
330                 return -1;
331
332         /* sector size check */
333         sector_size = le16_to_cpu(vs->sector_size);
334         if (sector_size == 0 || ((sector_size & (sector_size-1)) != 0))
335                 return -1;
336
337         info("checks passed\n");
338         dbg("sector_size 0x%x\n", sector_size);
339         dbg("sectors_per_cluster 0x%x\n", vs->sectors_per_cluster);
340
341         dir_entries = le16_to_cpu(vs->dir_entries);
342         reserved = le16_to_cpu(vs->reserved);
343         dbg("reserved 0x%x\n", reserved);
344
345         sect_count = le16_to_cpu(vs->sectors);
346         if (sect_count == 0)
347                 sect_count = le32_to_cpu(vs->total_sect);
348         dbg("sect_count 0x%x\n", sect_count);
349
350         fat_length = le16_to_cpu(vs->fat_length);
351         dbg("fat_length 0x%x\n", fat_length);
352         fat32_length = le32_to_cpu(vs->type.fat32.fat32_length);
353         dbg("fat32_length 0x%x\n", fat32_length);
354
355         if (fat_length)
356                 fat_size = fat_length * vs->fats;
357         else if (fat32_length)
358                 fat_size = fat32_length * vs->fats;
359         else
360                 return -1;
361         info("fat_size 0x%x\n", fat_size);
362
363         dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
364                         (sector_size-1)) / sector_size;
365         dbg("dir_size 0x%x\n", dir_size);
366
367         cluster_count = sect_count - (reserved + fat_size + dir_size);
368         cluster_count /= vs->sectors_per_cluster;
369         info("cluster_count 0x%x\n", cluster_count);
370
371         /* must be FAT32 */
372         if (!fat_length && fat32_length)
373                 goto fat32;
374
375         /* cluster_count tells us the format */
376         if (cluster_count < FAT12_MAX)
377                 strcpy(id->type_version, "FAT12");
378         else if (cluster_count < FAT16_MAX)
379                 strcpy(id->type_version, "FAT16");
380         else
381                 goto fat32;
382
383         /* the label may be an attribute in the root directory */
384         root_start = (reserved + fat_size) * sector_size;
385         dbg("root dir start 0x%" PRIx64 "\n", root_start);
386         root_dir_entries = le16_to_cpu(vs->dir_entries);
387         dbg("expected entries 0x%x\n", root_dir_entries);
388
389         buf_size = root_dir_entries * sizeof(struct vfat_dir_entry);
390         buf = volume_id_get_buffer(id, off + root_start, buf_size);
391         if (buf == NULL)
392                 goto found;
393
394         dir = (struct vfat_dir_entry*) buf;
395
396         fnlen = get_fat_attr_volume_id(filename, sizeof(filename), dir, root_dir_entries);
397
398         vs = (struct vfat_super_block *) volume_id_get_buffer(id, off, 0x200);
399         if (vs == NULL)
400                 return -1;
401
402         if (fnlen > 0 && memcmp(filename, "NO NAME    ", 11) != 0) {
403                 volume_id_set_label_raw(id, filename, fnlen);
404                 volume_id_set_label_string(id, filename, fnlen);
405         } else if (memcmp(vs->type.fat.label, "NO NAME    ", 11) != 0) {
406                 volume_id_set_label_raw(id, vs->type.fat.label, 11);
407                 volume_id_set_label_string(id, vs->type.fat.label, 11);
408         }
409         volume_id_set_uuid(id, vs->type.fat.serno, 0, UUID_DOS);
410         goto found;
411
412 fat32:
413         info("looking for FAT32\n");
414         /* FAT32 should have a valid signature in the fsinfo block */
415         fsinfo_sect = le16_to_cpu(vs->type.fat32.fsinfo_sector);
416         buf = volume_id_get_buffer(id, off + (fsinfo_sect * sector_size), 0x200);
417         if (buf == NULL)
418                 return -1;
419         fsinfo = (struct fat32_fsinfo *) buf;
420         info("signature1: 0x%02x%02x%02x%02x\n",
421              fsinfo->signature1[0], fsinfo->signature1[1],
422              fsinfo->signature1[2], fsinfo->signature1[3]);
423         info("signature2: 0x%02x%02x%02x%02x\n",
424              fsinfo->signature2[0], fsinfo->signature2[1],
425              fsinfo->signature2[2], fsinfo->signature2[3]);
426         if (memcmp(fsinfo->signature1, "\x52\x52\x61\x41", 4) != 0)
427                 return -1;
428         if (memcmp(fsinfo->signature2, "\x72\x72\x41\x61", 4) != 0)
429                 return -1 ;
430         info("FAT32 signatures match\n");
431
432         vs = (struct vfat_super_block *) volume_id_get_buffer(id, off, 0x200);
433         if (vs == NULL)
434                 return -1;
435
436         strcpy(id->type_version, "FAT32");
437
438         /* FAT32 root dir is a cluster chain like any other directory */
439         buf_size = vs->sectors_per_cluster * sector_size;
440         root_cluster = le32_to_cpu(vs->type.fat32.root_cluster);
441         dbg("root dir cluster %u\n", root_cluster);
442         start_data_sect = reserved + fat_size;
443
444         next = root_cluster;
445         maxloop = 100;
446         while (--maxloop) {
447                 uint32_t next_sect_off;
448                 uint64_t next_off;
449                 uint64_t fat_entry_off;
450                 int count;
451
452                 dbg("next cluster %u\n", next);
453                 next_sect_off = (next - 2) * vs->sectors_per_cluster;
454                 next_off = (start_data_sect + next_sect_off) * sector_size;
455                 dbg("cluster offset 0x%" PRIx64 "\n", next_off);
456
457                 /* get cluster */
458                 buf = volume_id_get_buffer(id, off + next_off, buf_size);
459                 if (buf == NULL)
460                         goto found;
461
462                 dir = (struct vfat_dir_entry*) buf;
463                 count = buf_size / sizeof(struct vfat_dir_entry);
464                 dbg("expected entries 0x%x\n", count);
465
466                 fnlen = get_fat_attr_volume_id(filename, sizeof(filename), dir, count);
467                 if (fnlen > 0)
468                         break;
469
470                 /* get FAT entry */
471                 fat_entry_off = (reserved * sector_size) + (next * sizeof(uint32_t));
472                 buf = volume_id_get_buffer(id, off + fat_entry_off, buf_size);
473                 if (buf == NULL)
474                         goto found;
475
476                 /* set next cluster */
477                 next = le32_to_cpu(*((uint32_t *) buf)) & 0x0fffffff;
478                 if (next < 2 || next >= 0x0ffffff0)
479                         break;
480         }
481         if (maxloop == 0)
482                 dbg("reached maximum follow count of root cluster chain, give up\n");
483
484         vs = (struct vfat_super_block *) volume_id_get_buffer(id, off, 0x200);
485         if (vs == NULL)
486                 return -1;
487
488         if (fnlen > 0 && memcmp(filename, "NO NAME    ", 11) != 0) {
489                 volume_id_set_label_raw(id, filename, fnlen);
490                 volume_id_set_label_string(id, filename, fnlen);
491         } else if (memcmp(vs->type.fat32.label, "NO NAME    ", 11) != 0) {
492                 volume_id_set_label_raw(id, vs->type.fat32.label, 11);
493                 volume_id_set_label_string(id, vs->type.fat32.label, 11);
494         }
495         volume_id_set_uuid(id, vs->type.fat32.serno, 0, UUID_DOS);
496
497 found:
498         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
499         id->type = "vfat";
500
501         return 0;
502 }