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