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