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
));
176 static int fat_valid_superblock(blkid_probe pr
,
177 const struct blkid_idmag
*mag
,
178 struct msdos_super_block
*ms
,
179 struct vfat_super_block
*vs
,
180 uint32_t *cluster_count
, uint32_t *fat_size
)
182 uint16_t sector_size
, dir_entries
, reserved
;
183 uint32_t sect_count
, __fat_size
, dir_size
, __cluster_count
, fat_length
;
186 /* extra check for FATs without magic strings */
188 /* Old floppies have a valid MBR signature */
189 if (ms
->ms_pmagic
[0] != 0x55 || ms
->ms_pmagic
[1] != 0xAA)
193 * OS/2 and apparently DFSee will place a FAT12/16-like
194 * pseudo-superblock in the first 512 bytes of non-FAT
195 * filesystems --- at least JFS and HPFS, and possibly others.
196 * So we explicitly check for those filesystems at the
197 * FAT12/16 filesystem magic field identifier, and if they are
198 * present, we rule this out as a FAT filesystem, despite the
199 * FAT-like pseudo-header.
201 if ((memcmp(ms
->ms_magic
, "JFS ", 8) == 0) ||
202 (memcmp(ms
->ms_magic
, "HPFS ", 8) == 0))
206 /* fat counts(Linux kernel expects at least 1 FAT table) */
209 if (!ms
->ms_reserved
)
211 if (!(0xf8 <= ms
->ms_media
|| ms
->ms_media
== 0xf0))
213 if (!is_power_of_2(ms
->ms_cluster_size
))
216 sector_size
= unaligned_le16(&ms
->ms_sector_size
);
217 if (!is_power_of_2(sector_size
) ||
218 sector_size
< 512 || sector_size
> 4096)
221 dir_entries
= unaligned_le16(&ms
->ms_dir_entries
);
222 reserved
= le16_to_cpu(ms
->ms_reserved
);
223 sect_count
= unaligned_le16(&ms
->ms_sectors
);
226 sect_count
= le32_to_cpu(ms
->ms_total_sect
);
228 fat_length
= le16_to_cpu(ms
->ms_fat_length
);
230 fat_length
= le32_to_cpu(vs
->vs_fat32_length
);
232 __fat_size
= fat_length
* ms
->ms_fats
;
233 dir_size
= ((dir_entries
* sizeof(struct vfat_dir_entry
)) +
234 (sector_size
-1)) / sector_size
;
236 __cluster_count
= (sect_count
- (reserved
+ __fat_size
+ dir_size
)) /
238 if (!ms
->ms_fat_length
&& vs
->vs_fat32_length
)
239 max_count
= FAT32_MAX
;
241 max_count
= __cluster_count
> FAT12_MAX
? FAT16_MAX
: FAT12_MAX
;
243 if (__cluster_count
> max_count
)
247 *fat_size
= __fat_size
;
249 *cluster_count
= __cluster_count
;
252 if (blkid_probe_is_wholedisk(pr
)) {
253 /* OK, seems like FAT, but it's possible that we found boot
254 * sector with crazy FAT-like stuff (magic strings, media,
255 * etc..) before MBR. Let's make sure that there is no MBR with
256 * usable partition. */
257 unsigned char *buf
= (unsigned char *) ms
;
258 if (mbr_is_valid_magic(buf
)) {
259 struct dos_partition
*p0
= mbr_get_partition(buf
, 0);
260 if (dos_partition_get_size(p0
) != 0 &&
261 (p0
->boot_ind
== 0 || p0
->boot_ind
== 0x80))
267 return 1; /* valid */
272 * This function is used by MBR partition table parser to avoid
273 * misinterpretation of FAT filesystem.
275 int blkid_probe_is_vfat(blkid_probe pr
)
277 struct vfat_super_block
*vs
;
278 struct msdos_super_block
*ms
;
279 const struct blkid_idmag
*mag
= NULL
;
282 rc
= blkid_probe_get_idmag(pr
, &vfat_idinfo
, NULL
, &mag
);
284 return rc
; /* error */
285 if (rc
!= BLKID_PROBE_OK
|| !mag
)
288 ms
= blkid_probe_get_sb(pr
, mag
, struct msdos_super_block
);
290 return errno
? -errno
: 0;
291 vs
= blkid_probe_get_sb(pr
, mag
, struct vfat_super_block
);
293 return errno
? -errno
: 0;
295 return fat_valid_superblock(pr
, mag
, ms
, vs
, NULL
, NULL
);
299 /* FAT label extraction from the root directory taken from Kay
300 * Sievers's volume_id library */
301 static int probe_vfat(blkid_probe pr
, const struct blkid_idmag
*mag
)
303 struct vfat_super_block
*vs
;
304 struct msdos_super_block
*ms
;
305 const unsigned char *vol_label
= 0;
306 unsigned char *vol_serno
= NULL
, vol_label_buf
[11];
307 uint16_t sector_size
= 0, reserved
;
308 uint32_t cluster_count
, fat_size
;
309 const char *version
= NULL
;
311 ms
= blkid_probe_get_sb(pr
, mag
, struct msdos_super_block
);
313 return errno
? -errno
: 1;
315 vs
= blkid_probe_get_sb(pr
, mag
, struct vfat_super_block
);
317 return errno
? -errno
: 1;
319 if (!fat_valid_superblock(pr
, mag
, ms
, vs
, &cluster_count
, &fat_size
))
322 sector_size
= unaligned_le16(&ms
->ms_sector_size
);
323 reserved
= le16_to_cpu(ms
->ms_reserved
);
325 if (ms
->ms_fat_length
) {
326 /* the label may be an attribute in the root directory */
327 uint32_t root_start
= (reserved
+ fat_size
) * sector_size
;
328 uint32_t root_dir_entries
= unaligned_le16(&vs
->vs_dir_entries
);
330 vol_label
= search_fat_label(pr
, root_start
, root_dir_entries
);
332 memcpy(vol_label_buf
, vol_label
, 11);
333 vol_label
= vol_label_buf
;
336 if (!vol_label
|| !memcmp(vol_label
, no_name
, 11))
337 vol_label
= ms
->ms_label
;
338 vol_serno
= ms
->ms_serno
;
340 blkid_probe_set_value(pr
, "SEC_TYPE", (unsigned char *) "msdos",
343 if (cluster_count
< FAT12_MAX
)
345 else if (cluster_count
< FAT16_MAX
)
348 } else if (vs
->vs_fat32_length
) {
350 uint16_t fsinfo_sect
;
353 /* Search the FAT32 root dir for the label attribute */
354 uint32_t buf_size
= vs
->vs_cluster_size
* sector_size
;
355 uint32_t start_data_sect
= reserved
+ fat_size
;
356 uint32_t entries
= le32_to_cpu(vs
->vs_fat32_length
) *
357 sector_size
/ sizeof(uint32_t);
358 uint32_t next
= le32_to_cpu(vs
->vs_root_cluster
);
360 while (next
&& next
< entries
&& --maxloop
) {
361 uint32_t next_sect_off
;
362 uint64_t next_off
, fat_entry_off
;
365 next_sect_off
= (next
- 2) * vs
->vs_cluster_size
;
366 next_off
= (uint64_t)(start_data_sect
+ next_sect_off
) *
369 count
= buf_size
/ sizeof(struct vfat_dir_entry
);
371 vol_label
= search_fat_label(pr
, next_off
, count
);
373 memcpy(vol_label_buf
, vol_label
, 11);
374 vol_label
= vol_label_buf
;
379 fat_entry_off
= ((uint64_t) reserved
* sector_size
) +
380 (next
* sizeof(uint32_t));
381 buf
= blkid_probe_get_buffer(pr
, fat_entry_off
, buf_size
);
385 /* set next cluster */
386 next
= le32_to_cpu(*((uint32_t *) buf
)) & 0x0fffffff;
391 if (!vol_label
|| !memcmp(vol_label
, no_name
, 11))
392 vol_label
= vs
->vs_label
;
393 vol_serno
= vs
->vs_serno
;
396 * FAT32 should have a valid signature in the fsinfo block,
397 * but also allow all bytes set to '\0', because some volumes
398 * do not set the signature at all.
400 fsinfo_sect
= le16_to_cpu(vs
->vs_fsinfo_sector
);
402 struct fat32_fsinfo
*fsinfo
;
404 buf
= blkid_probe_get_buffer(pr
,
405 (blkid_loff_t
) fsinfo_sect
* sector_size
,
406 sizeof(struct fat32_fsinfo
));
408 return errno
? -errno
: 1;
410 fsinfo
= (struct fat32_fsinfo
*) buf
;
411 if (memcmp(fsinfo
->signature1
, "\x52\x52\x61\x41", 4) != 0 &&
412 memcmp(fsinfo
->signature1
, "\x52\x52\x64\x41", 4) != 0 &&
413 memcmp(fsinfo
->signature1
, "\x00\x00\x00\x00", 4) != 0)
415 if (memcmp(fsinfo
->signature2
, "\x72\x72\x41\x61", 4) != 0 &&
416 memcmp(fsinfo
->signature2
, "\x00\x00\x00\x00", 4) != 0)
421 if (vol_label
&& memcmp(vol_label
, no_name
, 11))
422 blkid_probe_set_label(pr
, (unsigned char *) vol_label
, 11);
424 /* We can't just print them as %04X, because they are unaligned */
426 blkid_probe_sprintf_uuid(pr
, vol_serno
, 4, "%02X%02X-%02X%02X",
427 vol_serno
[3], vol_serno
[2], vol_serno
[1], vol_serno
[0]);
429 blkid_probe_set_version(pr
, version
);
435 const struct blkid_idinfo vfat_idinfo
=
438 .usage
= BLKID_USAGE_FILESYSTEM
,
439 .probefunc
= probe_vfat
,
442 { .magic
= "MSWIN", .len
= 5, .sboff
= 0x52 },
443 { .magic
= "FAT32 ", .len
= 8, .sboff
= 0x52 },
444 { .magic
= "MSDOS", .len
= 5, .sboff
= 0x36 },
445 { .magic
= "FAT16 ", .len
= 8, .sboff
= 0x36 },
446 { .magic
= "FAT12 ", .len
= 8, .sboff
= 0x36 },
447 { .magic
= "FAT ", .len
= 8, .sboff
= 0x36 },
448 { .magic
= "\353", .len
= 1, },
449 { .magic
= "\351", .len
= 1, },
450 { .magic
= "\125\252", .len
= 2, .sboff
= 0x1fe },