a24e6c354389e24506ec928f1f11ede4e7e4b05a
[project/ubox.git] / libblkid-tiny / vfat.c
1 /*
2  * Copyright (C) 1999 by Andries Brouwer
3  * Copyright (C) 1999, 2000, 2003 by Theodore Ts'o
4  * Copyright (C) 2001 by Andreas Dilger
5  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6  * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
7  *
8  * This file may be redistributed under the terms of the
9  * GNU Lesser General Public License.
10  */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <ctype.h>
17 #include <stdint.h>
18
19 #include "superblocks.h"
20
21 /* Yucky misaligned values */
22 struct vfat_super_block {
23 /* 00*/ unsigned char   vs_ignored[3];
24 /* 03*/ unsigned char   vs_sysid[8];
25 /* 0b*/ unsigned char   vs_sector_size[2];
26 /* 0d*/ uint8_t         vs_cluster_size;
27 /* 0e*/ uint16_t        vs_reserved;
28 /* 10*/ uint8_t         vs_fats;
29 /* 11*/ unsigned char   vs_dir_entries[2];
30 /* 13*/ unsigned char   vs_sectors[2];
31 /* 15*/ unsigned char   vs_media;
32 /* 16*/ uint16_t        vs_fat_length;
33 /* 18*/ uint16_t        vs_secs_track;
34 /* 1a*/ uint16_t        vs_heads;
35 /* 1c*/ uint32_t        vs_hidden;
36 /* 20*/ uint32_t        vs_total_sect;
37 /* 24*/ uint32_t        vs_fat32_length;
38 /* 28*/ uint16_t        vs_flags;
39 /* 2a*/ uint8_t         vs_version[2];
40 /* 2c*/ uint32_t        vs_root_cluster;
41 /* 30*/ uint16_t        vs_fsinfo_sector;
42 /* 32*/ uint16_t        vs_backup_boot;
43 /* 34*/ uint16_t        vs_reserved2[6];
44 /* 40*/ unsigned char   vs_unknown[3];
45 /* 43*/ unsigned char   vs_serno[4];
46 /* 47*/ unsigned char   vs_label[11];
47 /* 52*/ unsigned char   vs_magic[8];
48 /* 5a*/ unsigned char   vs_dummy2[0x1fe - 0x5a];
49 /*1fe*/ unsigned char   vs_pmagic[2];
50 } __attribute__((packed));
51
52 /* Yucky misaligned values */
53 struct msdos_super_block {
54 /* 00*/ unsigned char   ms_ignored[3];
55 /* 03*/ unsigned char   ms_sysid[8];
56 /* 0b*/ unsigned char   ms_sector_size[2];
57 /* 0d*/ uint8_t         ms_cluster_size;
58 /* 0e*/ uint16_t        ms_reserved;
59 /* 10*/ uint8_t         ms_fats;
60 /* 11*/ unsigned char   ms_dir_entries[2];
61 /* 13*/ unsigned char   ms_sectors[2]; /* =0 iff V3 or later */
62 /* 15*/ unsigned char   ms_media;
63 /* 16*/ uint16_t        ms_fat_length; /* Sectors per FAT */
64 /* 18*/ uint16_t        ms_secs_track;
65 /* 1a*/ uint16_t        ms_heads;
66 /* 1c*/ uint32_t        ms_hidden;
67 /* V3 BPB */
68 /* 20*/ uint32_t        ms_total_sect; /* iff ms_sectors == 0 */
69 /* V4 BPB */
70 /* 24*/ unsigned char   ms_unknown[3]; /* Phys drive no., resvd, V4 sig (0x29) */
71 /* 27*/ unsigned char   ms_serno[4];
72 /* 2b*/ unsigned char   ms_label[11];
73 /* 36*/ unsigned char   ms_magic[8];
74 /* 3e*/ unsigned char   ms_dummy2[0x1fe - 0x3e];
75 /*1fe*/ unsigned char   ms_pmagic[2];
76 } __attribute__((packed));
77
78 struct vfat_dir_entry {
79         uint8_t         name[11];
80         uint8_t         attr;
81         uint16_t        time_creat;
82         uint16_t        date_creat;
83         uint16_t        time_acc;
84         uint16_t        date_acc;
85         uint16_t        cluster_high;
86         uint16_t        time_write;
87         uint16_t        date_write;
88         uint16_t        cluster_low;
89         uint32_t        size;
90 } __attribute__((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 } __attribute__((packed));
100
101 /* maximum number of clusters */
102 #define FAT12_MAX 0xFF4
103 #define FAT16_MAX 0xFFF4
104 #define FAT32_MAX 0x0FFFFFF6
105
106 #define FAT_ATTR_VOLUME_ID              0x08
107 #define FAT_ATTR_DIR                    0x10
108 #define FAT_ATTR_LONG_NAME              0x0f
109 #define FAT_ATTR_MASK                   0x3f
110 #define FAT_ENTRY_FREE                  0xe5
111
112 static const char *no_name = "NO NAME    ";
113
114 #define unaligned_le16(x) \
115                 (((unsigned char *) x)[0] + (((unsigned char *) x)[1] << 8))
116
117 /*
118  * Look for LABEL (name) in the FAT root directory.
119  */
120 static unsigned char *search_fat_label(blkid_probe pr,
121                                 uint64_t offset, uint32_t entries)
122 {
123         struct vfat_dir_entry *ent, *dir = NULL;
124         uint32_t i;
125
126         DBG(DEBUG_LOWPROBE,
127                 printf("\tlook for label in root-dir "
128                         "(entries: %d, offset: %jd)\n", entries, offset));
129
130         if (!blkid_probe_is_tiny(pr)) {
131                 /* large disk, read whole root directory */
132                 dir = (struct vfat_dir_entry *)
133                         blkid_probe_get_buffer(pr,
134                                         offset,
135                                         (blkid_loff_t) entries *
136                                                 sizeof(struct vfat_dir_entry));
137                 if (!dir)
138                         return NULL;
139         }
140
141         for (i = 0; i < entries; i++) {
142                 /*
143                  * The root directory could be relatively large (4-16kB).
144                  * Fortunately, the LABEL is usually the first entry in the
145                  * directory. On tiny disks we call read() per entry.
146                  */
147                 if (!dir)
148                         ent = (struct vfat_dir_entry *)
149                                 blkid_probe_get_buffer(pr,
150                                         (blkid_loff_t) offset + (i *
151                                                 sizeof(struct vfat_dir_entry)),
152                                         sizeof(struct vfat_dir_entry));
153                 else
154                         ent = &dir[i];
155
156                 if (!ent || ent->name[0] == 0x00)
157                         break;
158
159                 if ((ent->name[0] == FAT_ENTRY_FREE) ||
160                     (ent->cluster_high != 0 || ent->cluster_low != 0) ||
161                     ((ent->attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME))
162                         continue;
163
164                 if ((ent->attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) ==
165                     FAT_ATTR_VOLUME_ID) {
166                         DBG(DEBUG_LOWPROBE,
167                                 printf("\tfound fs LABEL at entry %d\n", i));
168                         return ent->name;
169                 }
170         }
171         return NULL;
172 }
173
174 static int fat_valid_superblock(const struct blkid_idmag *mag,
175                         struct msdos_super_block *ms,
176                         struct vfat_super_block *vs,
177                         uint32_t *cluster_count, uint32_t *fat_size)
178 {
179         uint16_t sector_size, dir_entries, reserved;
180         uint32_t sect_count, __fat_size, dir_size, __cluster_count, fat_length;
181         uint32_t max_count;
182
183         /* extra check for FATs without magic strings */
184         if (mag->len <= 2) {
185                 /* Old floppies have a valid MBR signature */
186                 if (ms->ms_pmagic[0] != 0x55 || ms->ms_pmagic[1] != 0xAA)
187                         return 0;
188
189                 /*
190                  * OS/2 and apparently DFSee will place a FAT12/16-like
191                  * pseudo-superblock in the first 512 bytes of non-FAT
192                  * filesystems --- at least JFS and HPFS, and possibly others.
193                  * So we explicitly check for those filesystems at the
194                  * FAT12/16 filesystem magic field identifier, and if they are
195                  * present, we rule this out as a FAT filesystem, despite the
196                  * FAT-like pseudo-header.
197                  */
198                 if ((memcmp(ms->ms_magic, "JFS     ", 8) == 0) ||
199                     (memcmp(ms->ms_magic, "HPFS    ", 8) == 0))
200                         return 0;
201         }
202
203         /* fat counts(Linux kernel expects at least 1 FAT table) */
204         if (!ms->ms_fats)
205                 return 0;
206         if (!ms->ms_reserved)
207                 return 0;
208         if (!(0xf8 <= ms->ms_media || ms->ms_media == 0xf0))
209                 return 0;
210         if (!is_power_of_2(ms->ms_cluster_size))
211                 return 0;
212
213         sector_size = unaligned_le16(&ms->ms_sector_size);
214         if (!is_power_of_2(sector_size) ||
215             sector_size < 512 || sector_size > 4096)
216                 return 0;
217
218         dir_entries = unaligned_le16(&ms->ms_dir_entries);
219         reserved =  le16_to_cpu(ms->ms_reserved);
220         sect_count = unaligned_le16(&ms->ms_sectors);
221
222         if (sect_count == 0)
223                 sect_count = le32_to_cpu(ms->ms_total_sect);
224
225         fat_length = le16_to_cpu(ms->ms_fat_length);
226         if (fat_length == 0)
227                 fat_length = le32_to_cpu(vs->vs_fat32_length);
228
229         __fat_size = fat_length * ms->ms_fats;
230         dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
231                                         (sector_size-1)) / sector_size;
232
233         __cluster_count = (sect_count - (reserved + __fat_size + dir_size)) /
234                                                         ms->ms_cluster_size;
235         if (!ms->ms_fat_length && vs->vs_fat32_length)
236                 max_count = FAT32_MAX;
237         else
238                 max_count = __cluster_count > FAT12_MAX ? FAT16_MAX : FAT12_MAX;
239
240         if (__cluster_count > max_count)
241                 return 0;
242
243         if (fat_size)
244                 *fat_size = __fat_size;
245         if (cluster_count)
246                 *cluster_count = __cluster_count;
247
248         return 1;       /* valid */
249 }
250
251 /*
252  * This function is used by MBR partition table parser to avoid
253  * misinterpretation of FAT filesystem.
254  */
255 /*static int blkid_probe_is_vfat(blkid_probe pr)
256 {
257         struct vfat_super_block *vs;
258         struct msdos_super_block *ms;
259         const struct blkid_idmag *mag = NULL;
260
261         if (blkid_probe_get_idmag(pr, &vfat_idinfo, NULL, &mag) || !mag)
262                 return 0;
263
264         ms = blkid_probe_get_sb(pr, mag, struct msdos_super_block);
265         if (!ms)
266                 return 0;
267         vs = blkid_probe_get_sb(pr, mag, struct vfat_super_block);
268         if (!vs)
269                 return 0;
270
271         return fat_valid_superblock(mag, ms, vs, NULL, NULL);
272 }*/
273 static struct vfat_super_block vs;
274 static struct msdos_super_block ms;
275
276 /* FAT label extraction from the root directory taken from Kay
277  * Sievers's volume_id library */
278 static int probe_vfat(blkid_probe pr, const struct blkid_idmag *mag)
279 {
280         struct vfat_super_block *_vs;
281         struct msdos_super_block *_ms;
282         const unsigned char *vol_label = 0;
283         unsigned char *vol_serno = NULL, vol_label_buf[11];
284         uint16_t sector_size = 0, reserved;
285         uint32_t cluster_count, fat_size;
286         const char *version = NULL;
287
288         _ms = blkid_probe_get_sb(pr, mag, struct msdos_super_block);
289         if (!_ms)
290                 return 0;
291         _vs = blkid_probe_get_sb(pr, mag, struct vfat_super_block);
292         if (!_vs)
293                 return 0;
294         memcpy(&ms, _ms, sizeof(struct vfat_super_block));
295         memcpy(&vs, _vs, sizeof(struct msdos_super_block));
296         if (!fat_valid_superblock(mag, &ms, &vs, &cluster_count, &fat_size))
297                 return 1;
298
299         sector_size = unaligned_le16(&ms.ms_sector_size);
300         reserved =  le16_to_cpu(ms.ms_reserved);
301
302         if (ms.ms_fat_length) {
303                 /* the label may be an attribute in the root directory */
304                 uint32_t root_start = (reserved + fat_size) * sector_size;
305                 uint32_t root_dir_entries = unaligned_le16(&vs.vs_dir_entries);
306
307                 vol_label = search_fat_label(pr, root_start, root_dir_entries);
308                 if (vol_label) {
309                         memcpy(vol_label_buf, vol_label, 11);
310                         vol_label = vol_label_buf;
311                 }
312
313                 if (!vol_label || !memcmp(vol_label, no_name, 11))
314                         vol_label = ms.ms_label;
315                 vol_serno = ms.ms_serno;
316
317                 blkid_probe_set_value(pr, "SEC_TYPE", (unsigned char *) "msdos",
318                               sizeof("msdos"));
319
320                 if (cluster_count < FAT12_MAX)
321                         version = "FAT12";
322                 else if (cluster_count < FAT16_MAX)
323                         version = "FAT16";
324
325         } else if (vs.vs_fat32_length) {
326                 unsigned char *buf;
327                 uint16_t fsinfo_sect;
328                 int maxloop = 100;
329
330                 /* Search the FAT32 root dir for the label attribute */
331                 uint32_t buf_size = vs.vs_cluster_size * sector_size;
332                 uint32_t start_data_sect = reserved + fat_size;
333                 uint32_t entries = le32_to_cpu(vs.vs_fat32_length) *
334                                         sector_size / sizeof(uint32_t);
335                 uint32_t next = le32_to_cpu(vs.vs_root_cluster);
336
337                 while (next && next < entries && --maxloop) {
338                         uint32_t next_sect_off;
339                         uint64_t next_off, fat_entry_off;
340                         int count;
341
342                         next_sect_off = (next - 2) * le32_to_cpu(vs.vs_cluster_size);
343                         next_off = (uint64_t)(start_data_sect + next_sect_off) *
344                                 sector_size;
345
346                         count = buf_size / sizeof(struct vfat_dir_entry);
347
348                         vol_label = search_fat_label(pr, next_off, count);
349                         if (vol_label) {
350                                 memcpy(vol_label_buf, vol_label, 11);
351                                 vol_label = vol_label_buf;
352                                 break;
353                         }
354
355                         /* get FAT entry */
356                         fat_entry_off = ((uint64_t) reserved * sector_size) +
357                                 (next * sizeof(uint32_t));
358                         buf = blkid_probe_get_buffer(pr, fat_entry_off, buf_size);
359                         if (buf == NULL)
360                                 break;
361
362                         /* set next cluster */
363                         next = le32_to_cpu(*((uint32_t *) buf)) & 0x0fffffff;
364                 }
365
366                 version = "FAT32";
367
368                 if (!vol_label || !memcmp(vol_label, no_name, 11))
369                         vol_label = NULL;
370                 vol_serno = vs.vs_serno;
371
372                 /*
373                  * FAT32 should have a valid signature in the fsinfo block,
374                  * but also allow all bytes set to '\0', because some volumes
375                  * do not set the signature at all.
376                  */
377                 fsinfo_sect = le16_to_cpu(vs.vs_fsinfo_sector);
378                 if (fsinfo_sect) {
379                         struct fat32_fsinfo *fsinfo;
380
381                         buf = blkid_probe_get_buffer(pr,
382                                         (blkid_loff_t) fsinfo_sect * sector_size,
383                                         sizeof(struct fat32_fsinfo));
384                         if (buf == NULL)
385                                 return -1;
386
387                         fsinfo = (struct fat32_fsinfo *) buf;
388                         if (memcmp(fsinfo->signature1, "\x52\x52\x61\x41", 4) != 0 &&
389                             memcmp(fsinfo->signature1, "\x52\x52\x64\x41", 4) != 0 &&
390                             memcmp(fsinfo->signature1, "\x00\x00\x00\x00", 4) != 0)
391                                 return -1;
392                         if (memcmp(fsinfo->signature2, "\x72\x72\x41\x61", 4) != 0 &&
393                             memcmp(fsinfo->signature2, "\x00\x00\x00\x00", 4) != 0)
394                                 return -1;
395                 }
396         }
397
398         if (vol_label && memcmp(vol_label, no_name, 11))
399                 blkid_probe_set_label(pr, (unsigned char *) vol_label, 11);
400
401         /* We can't just print them as %04X, because they are unaligned */
402         if (vol_serno)
403                 blkid_probe_sprintf_uuid(pr, vol_serno, 4, "%02x%02x-%02x%02x",
404                         vol_serno[3], vol_serno[2], vol_serno[1], vol_serno[0]);
405         if (version)
406                 blkid_probe_set_version(pr, version);
407
408         return 0;
409 }
410
411
412 const struct blkid_idinfo vfat_idinfo =
413 {
414         .name           = "vfat",
415         .usage          = BLKID_USAGE_FILESYSTEM,
416         .probefunc      = probe_vfat,
417         .magics         =
418         {
419                 { .magic = "MSWIN",    .len = 5, .sboff = 0x52 },
420                 { .magic = "FAT32   ", .len = 8, .sboff = 0x52 },
421                 { .magic = "MSDOS",    .len = 5, .sboff = 0x36 },
422                 { .magic = "FAT16   ", .len = 8, .sboff = 0x36 },
423                 { .magic = "FAT12   ", .len = 8, .sboff = 0x36 },
424                 { .magic = "FAT     ", .len = 8, .sboff = 0x36 },
425                 { .magic = "\353",     .len = 1, },
426                 { .magic = "\351",     .len = 1, },
427                 { .magic = "\125\252", .len = 2, .sboff = 0x1fe },
428                 { NULL }
429         }
430 };
431