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