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.
19 #include "superblocks.h"
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
));
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
;
68 /* 20*/ uint32_t ms_total_sect
; /* iff ms_sectors == 0 */
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
));
78 struct vfat_dir_entry
{
85 uint16_t cluster_high
;
90 } __attribute__((packed
));
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
));
101 /* maximum number of clusters */
102 #define FAT12_MAX 0xFF4
103 #define FAT16_MAX 0xFFF4
104 #define FAT32_MAX 0x0FFFFFF6
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
112 static const char *no_name
= "NO NAME ";
114 #define unaligned_le16(x) \
115 (((unsigned char *) x)[0] + (((unsigned char *) x)[1] << 8))
118 * Look for LABEL (name) in the FAT root directory.
120 static unsigned char *search_fat_label(blkid_probe pr
,
121 uint64_t offset
, uint32_t entries
)
123 struct vfat_dir_entry
*ent
, *dir
= NULL
;
127 printf("\tlook for label in root-dir "
128 "(entries: %d, offset: %jd)\n", entries
, offset
));
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
,
135 (blkid_loff_t
) entries
*
136 sizeof(struct vfat_dir_entry
));
141 for (i
= 0; i
< entries
; i
++) {
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.
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
));
156 if (!ent
|| ent
->name
[0] == 0x00)
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
))
164 if ((ent
->attr
& (FAT_ATTR_VOLUME_ID
| FAT_ATTR_DIR
)) ==
165 FAT_ATTR_VOLUME_ID
) {
167 printf("\tfound fs LABEL at entry %d\n", i
));
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
)
179 uint16_t sector_size
, dir_entries
, reserved
;
180 uint32_t sect_count
, __fat_size
, dir_size
, __cluster_count
, fat_length
;
183 /* extra check for FATs without magic strings */
185 /* Old floppies have a valid MBR signature */
186 if (ms
->ms_pmagic
[0] != 0x55 || ms
->ms_pmagic
[1] != 0xAA)
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.
198 if ((memcmp(ms
->ms_magic
, "JFS ", 8) == 0) ||
199 (memcmp(ms
->ms_magic
, "HPFS ", 8) == 0))
203 /* fat counts(Linux kernel expects at least 1 FAT table) */
206 if (!ms
->ms_reserved
)
208 if (!(0xf8 <= ms
->ms_media
|| ms
->ms_media
== 0xf0))
210 if (!is_power_of_2(ms
->ms_cluster_size
))
213 sector_size
= unaligned_le16(&ms
->ms_sector_size
);
214 if (!is_power_of_2(sector_size
) ||
215 sector_size
< 512 || sector_size
> 4096)
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
);
223 sect_count
= le32_to_cpu(ms
->ms_total_sect
);
225 fat_length
= le16_to_cpu(ms
->ms_fat_length
);
227 fat_length
= le32_to_cpu(vs
->vs_fat32_length
);
229 __fat_size
= fat_length
* ms
->ms_fats
;
230 dir_size
= ((dir_entries
* sizeof(struct vfat_dir_entry
)) +
231 (sector_size
-1)) / sector_size
;
233 __cluster_count
= (sect_count
- (reserved
+ __fat_size
+ dir_size
)) /
235 if (!ms
->ms_fat_length
&& vs
->vs_fat32_length
)
236 max_count
= FAT32_MAX
;
238 max_count
= __cluster_count
> FAT12_MAX
? FAT16_MAX
: FAT12_MAX
;
240 if (__cluster_count
> max_count
)
244 *fat_size
= __fat_size
;
246 *cluster_count
= __cluster_count
;
248 return 1; /* valid */
252 * This function is used by MBR partition table parser to avoid
253 * misinterpretation of FAT filesystem.
255 /*static int blkid_probe_is_vfat(blkid_probe pr)
257 struct vfat_super_block *vs;
258 struct msdos_super_block *ms;
259 const struct blkid_idmag *mag = NULL;
261 if (blkid_probe_get_idmag(pr, &vfat_idinfo, NULL, &mag) || !mag)
264 ms = blkid_probe_get_sb(pr, mag, struct msdos_super_block);
267 vs = blkid_probe_get_sb(pr, mag, struct vfat_super_block);
271 return fat_valid_superblock(mag, ms, vs, NULL, NULL);
273 static struct vfat_super_block vs
;
274 static struct msdos_super_block ms
;
276 static int set_label(blkid_probe pr
, unsigned char *vol_label
)
280 for (c
= vol_label
+ 10; c
>= vol_label
&& *c
== ' '; c
--)
286 return blkid_probe_set_label(pr
, vol_label
, 11);
289 /* FAT label extraction from the root directory taken from Kay
290 * Sievers's volume_id library */
291 static int probe_vfat(blkid_probe pr
, const struct blkid_idmag
*mag
)
293 struct vfat_super_block
*_vs
;
294 struct msdos_super_block
*_ms
;
295 const unsigned char *vol_label
= 0;
296 unsigned char *vol_serno
= NULL
, vol_label_buf
[11];
297 uint16_t sector_size
= 0, reserved
;
298 uint32_t cluster_count
, fat_size
;
299 const char *version
= NULL
;
301 _ms
= blkid_probe_get_sb(pr
, mag
, struct msdos_super_block
);
304 _vs
= blkid_probe_get_sb(pr
, mag
, struct vfat_super_block
);
307 memcpy(&ms
, _ms
, sizeof(struct vfat_super_block
));
308 memcpy(&vs
, _vs
, sizeof(struct msdos_super_block
));
309 if (!fat_valid_superblock(mag
, &ms
, &vs
, &cluster_count
, &fat_size
))
312 sector_size
= unaligned_le16(&ms
.ms_sector_size
);
313 reserved
= le16_to_cpu(ms
.ms_reserved
);
315 if (ms
.ms_fat_length
) {
316 /* the label may be an attribute in the root directory */
317 uint32_t root_start
= (reserved
+ fat_size
) * sector_size
;
318 uint32_t root_dir_entries
= unaligned_le16(&vs
.vs_dir_entries
);
320 vol_label
= search_fat_label(pr
, root_start
, root_dir_entries
);
322 memcpy(vol_label_buf
, vol_label
, 11);
323 vol_label
= vol_label_buf
;
326 if (!vol_label
|| !memcmp(vol_label
, no_name
, 11))
327 vol_label
= ms
.ms_label
;
328 vol_serno
= ms
.ms_serno
;
330 blkid_probe_set_value(pr
, "SEC_TYPE", (unsigned char *) "msdos",
333 if (cluster_count
< FAT12_MAX
)
335 else if (cluster_count
< FAT16_MAX
)
338 } else if (vs
.vs_fat32_length
) {
340 uint16_t fsinfo_sect
;
343 /* Search the FAT32 root dir for the label attribute */
344 uint32_t buf_size
= vs
.vs_cluster_size
* sector_size
;
345 uint32_t start_data_sect
= reserved
+ fat_size
;
346 uint32_t entries
= le32_to_cpu(vs
.vs_fat32_length
) *
347 sector_size
/ sizeof(uint32_t);
348 uint32_t next
= le32_to_cpu(vs
.vs_root_cluster
);
350 while (next
&& next
< entries
&& --maxloop
) {
351 uint32_t next_sect_off
;
352 uint64_t next_off
, fat_entry_off
;
355 next_sect_off
= (next
- 2) * le32_to_cpu(vs
.vs_cluster_size
);
356 next_off
= (uint64_t)(start_data_sect
+ next_sect_off
) *
359 count
= buf_size
/ sizeof(struct vfat_dir_entry
);
361 vol_label
= search_fat_label(pr
, next_off
, count
);
363 memcpy(vol_label_buf
, vol_label
, 11);
364 vol_label
= vol_label_buf
;
369 fat_entry_off
= ((uint64_t) reserved
* sector_size
) +
370 (next
* sizeof(uint32_t));
371 buf
= blkid_probe_get_buffer(pr
, fat_entry_off
, buf_size
);
375 /* set next cluster */
376 next
= le32_to_cpu(*((uint32_t *) buf
)) & 0x0fffffff;
381 if (!vol_label
|| !memcmp(vol_label
, no_name
, 11))
383 vol_serno
= vs
.vs_serno
;
386 * FAT32 should have a valid signature in the fsinfo block,
387 * but also allow all bytes set to '\0', because some volumes
388 * do not set the signature at all.
390 fsinfo_sect
= le16_to_cpu(vs
.vs_fsinfo_sector
);
392 struct fat32_fsinfo
*fsinfo
;
394 buf
= blkid_probe_get_buffer(pr
,
395 (blkid_loff_t
) fsinfo_sect
* sector_size
,
396 sizeof(struct fat32_fsinfo
));
400 fsinfo
= (struct fat32_fsinfo
*) buf
;
401 if (memcmp(fsinfo
->signature1
, "\x52\x52\x61\x41", 4) != 0 &&
402 memcmp(fsinfo
->signature1
, "\x52\x52\x64\x41", 4) != 0 &&
403 memcmp(fsinfo
->signature1
, "\x00\x00\x00\x00", 4) != 0)
405 if (memcmp(fsinfo
->signature2
, "\x72\x72\x41\x61", 4) != 0 &&
406 memcmp(fsinfo
->signature2
, "\x00\x00\x00\x00", 4) != 0)
411 if (vol_label
&& memcmp(vol_label
, no_name
, 11))
412 set_label(pr
, (unsigned char *) vol_label
);
414 /* We can't just print them as %04X, because they are unaligned */
416 blkid_probe_sprintf_uuid(pr
, vol_serno
, 4, "%02x%02x-%02x%02x",
417 vol_serno
[3], vol_serno
[2], vol_serno
[1], vol_serno
[0]);
419 blkid_probe_set_version(pr
, version
);
425 const struct blkid_idinfo vfat_idinfo
=
428 .usage
= BLKID_USAGE_FILESYSTEM
,
429 .probefunc
= probe_vfat
,
432 { .magic
= "MSWIN", .len
= 5, .sboff
= 0x52 },
433 { .magic
= "FAT32 ", .len
= 8, .sboff
= 0x52 },
434 { .magic
= "MSDOS", .len
= 5, .sboff
= 0x36 },
435 { .magic
= "FAT16 ", .len
= 8, .sboff
= 0x36 },
436 { .magic
= "FAT12 ", .len
= 8, .sboff
= 0x36 },
437 { .magic
= "FAT ", .len
= 8, .sboff
= 0x36 },
438 { .magic
= "\353", .len
= 1, },
439 { .magic
= "\351", .len
= 1, },
440 { .magic
= "\125\252", .len
= 2, .sboff
= 0x1fe },