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