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