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>
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
23 #include "superblocks.h"
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));
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;
72 /* 20*/ uint32_t ms_total_sect; /* iff ms_sectors == 0 */
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));
82 struct vfat_dir_entry {
89 uint16_t cluster_high;
94 } __attribute__((packed));
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));
105 /* maximum number of clusters */
106 #define FAT12_MAX 0xFF4
107 #define FAT16_MAX 0xFFF4
108 #define FAT32_MAX 0x0FFFFFF6
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
116 static const char *no_name = "NO NAME ";
118 #define unaligned_le16(x) \
119 (((unsigned char *) x)[0] + (((unsigned char *) x)[1] << 8))
122 * Look for LABEL (name) in the FAT root directory.
124 static unsigned char *search_fat_label(blkid_probe pr,
125 uint64_t offset, uint32_t entries)
127 struct vfat_dir_entry *ent, *dir = NULL;
130 DBG(LOWPROBE, ul_debug("\tlook for label in root-dir "
131 "(entries: %d, offset: %jd)", entries, offset));
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,
138 (blkid_loff_t) entries *
139 sizeof(struct vfat_dir_entry));
144 for (i = 0; i < entries; i++) {
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.
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));
159 if (!ent || ent->name[0] == 0x00)
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))
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)
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)
184 uint16_t sector_size, dir_entries, reserved;
185 uint32_t sect_count, __fat_size, dir_size, __cluster_count, fat_length;
188 /* extra check for FATs without magic strings */
190 /* Old floppies have a valid MBR signature */
191 if (ms->ms_pmagic[0] != 0x55 || ms->ms_pmagic[1] != 0xAA)
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.
203 if ((memcmp(ms->ms_magic, "JFS ", 8) == 0) ||
204 (memcmp(ms->ms_magic, "HPFS ", 8) == 0))
208 /* fat counts(Linux kernel expects at least 1 FAT table) */
211 if (!ms->ms_reserved)
213 if (!(0xf8 <= ms->ms_media || ms->ms_media == 0xf0))
215 if (!is_power_of_2(ms->ms_cluster_size))
218 sector_size = unaligned_le16(&ms->ms_sector_size);
219 if (!is_power_of_2(sector_size) ||
220 sector_size < 512 || sector_size > 4096)
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);
228 sect_count = le32_to_cpu(ms->ms_total_sect);
230 fat_length = le16_to_cpu(ms->ms_fat_length);
232 fat_length = le32_to_cpu(vs->vs_fat32_length);
234 __fat_size = fat_length * ms->ms_fats;
235 dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
236 (sector_size-1)) / sector_size;
238 __cluster_count = (sect_count - (reserved + __fat_size + dir_size)) /
240 if (!ms->ms_fat_length && vs->vs_fat32_length)
241 max_count = FAT32_MAX;
243 max_count = __cluster_count > FAT12_MAX ? FAT16_MAX : FAT12_MAX;
245 if (__cluster_count > max_count)
249 *fat_size = __fat_size;
251 *cluster_count = __cluster_count;
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))
269 return 1; /* valid */
274 * This function is used by MBR partition table parser to avoid
275 * misinterpretation of FAT filesystem.
277 int blkid_probe_is_vfat(blkid_probe pr)
279 struct vfat_super_block *vs;
280 struct msdos_super_block *ms;
281 const struct blkid_idmag *mag = NULL;
284 rc = blkid_probe_get_idmag(pr, &vfat_idinfo, NULL, &mag);
286 return rc; /* error */
287 if (rc != BLKID_PROBE_OK || !mag)
290 ms = blkid_probe_get_sb(pr, mag, struct msdos_super_block);
292 return errno ? -errno : 0;
293 vs = blkid_probe_get_sb(pr, mag, struct vfat_super_block);
295 return errno ? -errno : 0;
297 return fat_valid_superblock(pr, mag, ms, vs, NULL, NULL);
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)
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;
314 ms = blkid_probe_get_sb(pr, mag, struct msdos_super_block);
316 return errno ? -errno : 1;
318 vs = blkid_probe_get_sb(pr, mag, struct vfat_super_block);
320 return errno ? -errno : 1;
322 if (!fat_valid_superblock(pr, mag, ms, vs, &cluster_count, &fat_size))
325 sector_size = unaligned_le16(&ms->ms_sector_size);
326 reserved = le16_to_cpu(ms->ms_reserved);
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);
333 vol_label = search_fat_label(pr, root_start, root_dir_entries);
335 memcpy(vol_label_buf, vol_label, 11);
336 vol_label = vol_label_buf;
339 boot_label = ms->ms_label;
340 vol_serno = ms->ms_serno;
342 blkid_probe_set_value(pr, "SEC_TYPE", (unsigned char *) "msdos",
345 if (cluster_count < FAT12_MAX)
347 else if (cluster_count < FAT16_MAX)
350 } else if (vs->vs_fat32_length) {
352 uint16_t fsinfo_sect;
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);
362 while (next && next < entries && --maxloop) {
363 uint32_t next_sect_off;
364 uint64_t next_off, fat_entry_off;
367 next_sect_off = (next - 2) * vs->vs_cluster_size;
368 next_off = (uint64_t)(start_data_sect + next_sect_off) *
371 count = buf_size / sizeof(struct vfat_dir_entry);
373 vol_label = search_fat_label(pr, next_off, count);
375 memcpy(vol_label_buf, vol_label, 11);
376 vol_label = vol_label_buf;
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);
387 /* set next cluster */
388 next = le32_to_cpu(*((uint32_t *) buf)) & 0x0fffffff;
393 boot_label = vs->vs_label;
394 vol_serno = vs->vs_serno;
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.
401 fsinfo_sect = le16_to_cpu(vs->vs_fsinfo_sector);
403 struct fat32_fsinfo *fsinfo;
405 buf = blkid_probe_get_buffer(pr,
406 (blkid_loff_t) fsinfo_sect * sector_size,
407 sizeof(struct fat32_fsinfo));
409 return errno ? -errno : 1;
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)
416 if (memcmp(fsinfo->signature2, "\x72\x72\x41\x61", 4) != 0 &&
417 memcmp(fsinfo->signature2, "\x00\x00\x00\x00", 4) != 0)
422 if (boot_label && memcmp(boot_label, no_name, 11))
423 blkid_probe_set_id_label(pr, "LABEL_FATBOOT", (unsigned char *) boot_label, 11);
426 blkid_probe_set_label(pr, (unsigned char *) vol_label, 11);
428 /* We can't just print them as %04X, because they are unaligned */
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]);
433 blkid_probe_set_version(pr, version);
439 const struct blkid_idinfo vfat_idinfo =
442 .usage = BLKID_USAGE_FILESYSTEM,
443 .probefunc = probe_vfat,
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 },