blob: f38deac8b24c4578eef3a15784156b4311258f18 [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
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#include "superblocks.h"
20
21/* Yucky misaligned values */
22struct 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));
51
52/* Yucky misaligned values */
53struct 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;
67/* V3 BPB */
68/* 20*/ uint32_t ms_total_sect; /* iff ms_sectors == 0 */
69/* V4 BPB */
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));
77
78struct vfat_dir_entry {
79 uint8_t name[11];
80 uint8_t attr;
81 uint16_t time_creat;
82 uint16_t date_creat;
83 uint16_t time_acc;
84 uint16_t date_acc;
85 uint16_t cluster_high;
86 uint16_t time_write;
87 uint16_t date_write;
88 uint16_t cluster_low;
89 uint32_t size;
90} __attribute__((packed));
91
92struct fat32_fsinfo {
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));
100
101/* maximum number of clusters */
102#define FAT12_MAX 0xFF4
103#define FAT16_MAX 0xFFF4
104#define FAT32_MAX 0x0FFFFFF6
105
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
111
112static const char *no_name = "NO NAME ";
113
114#define unaligned_le16(x) \
115 (((unsigned char *) x)[0] + (((unsigned char *) x)[1] << 8))
116
117/*
118 * Look for LABEL (name) in the FAT root directory.
119 */
120static unsigned char *search_fat_label(blkid_probe pr,
121 uint64_t offset, uint32_t entries)
122{
123 struct vfat_dir_entry *ent, *dir = NULL;
124 uint32_t i;
125
bigbiff7b4c7a62015-01-01 19:44:14 -0500126 DBG(LOWPROBE, ul_debug("\tlook for label in root-dir "
127 "(entries: %d, offset: %jd)", entries, offset));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500128
129 if (!blkid_probe_is_tiny(pr)) {
130 /* large disk, read whole root directory */
131 dir = (struct vfat_dir_entry *)
132 blkid_probe_get_buffer(pr,
133 offset,
134 (blkid_loff_t) entries *
135 sizeof(struct vfat_dir_entry));
136 if (!dir)
137 return NULL;
138 }
139
140 for (i = 0; i < entries; i++) {
141 /*
142 * The root directory could be relatively large (4-16kB).
143 * Fortunately, the LABEL is usually the first entry in the
144 * directory. On tiny disks we call read() per entry.
145 */
146 if (!dir)
147 ent = (struct vfat_dir_entry *)
148 blkid_probe_get_buffer(pr,
149 (blkid_loff_t) offset + (i *
150 sizeof(struct vfat_dir_entry)),
151 sizeof(struct vfat_dir_entry));
152 else
153 ent = &dir[i];
154
155 if (!ent || ent->name[0] == 0x00)
156 break;
157
158 if ((ent->name[0] == FAT_ENTRY_FREE) ||
159 (ent->cluster_high != 0 || ent->cluster_low != 0) ||
160 ((ent->attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME))
161 continue;
162
163 if ((ent->attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) ==
164 FAT_ATTR_VOLUME_ID) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500165 DBG(LOWPROBE, ul_debug("\tfound fs LABEL at entry %d", i));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500166 return ent->name;
167 }
168 }
169 return NULL;
170}
171
172static int fat_valid_superblock(const struct blkid_idmag *mag,
173 struct msdos_super_block *ms,
174 struct vfat_super_block *vs,
175 uint32_t *cluster_count, uint32_t *fat_size)
176{
177 uint16_t sector_size, dir_entries, reserved;
178 uint32_t sect_count, __fat_size, dir_size, __cluster_count, fat_length;
179 uint32_t max_count;
180
181 /* extra check for FATs without magic strings */
182 if (mag->len <= 2) {
183 /* Old floppies have a valid MBR signature */
184 if (ms->ms_pmagic[0] != 0x55 || ms->ms_pmagic[1] != 0xAA)
185 return 0;
186
187 /*
188 * OS/2 and apparently DFSee will place a FAT12/16-like
189 * pseudo-superblock in the first 512 bytes of non-FAT
190 * filesystems --- at least JFS and HPFS, and possibly others.
191 * So we explicitly check for those filesystems at the
192 * FAT12/16 filesystem magic field identifier, and if they are
193 * present, we rule this out as a FAT filesystem, despite the
194 * FAT-like pseudo-header.
195 */
196 if ((memcmp(ms->ms_magic, "JFS ", 8) == 0) ||
197 (memcmp(ms->ms_magic, "HPFS ", 8) == 0))
198 return 0;
199 }
200
201 /* fat counts(Linux kernel expects at least 1 FAT table) */
202 if (!ms->ms_fats)
203 return 0;
204 if (!ms->ms_reserved)
205 return 0;
206 if (!(0xf8 <= ms->ms_media || ms->ms_media == 0xf0))
207 return 0;
208 if (!is_power_of_2(ms->ms_cluster_size))
209 return 0;
210
211 sector_size = unaligned_le16(&ms->ms_sector_size);
212 if (!is_power_of_2(sector_size) ||
213 sector_size < 512 || sector_size > 4096)
214 return 0;
215
216 dir_entries = unaligned_le16(&ms->ms_dir_entries);
217 reserved = le16_to_cpu(ms->ms_reserved);
218 sect_count = unaligned_le16(&ms->ms_sectors);
219
220 if (sect_count == 0)
221 sect_count = le32_to_cpu(ms->ms_total_sect);
222
223 fat_length = le16_to_cpu(ms->ms_fat_length);
224 if (fat_length == 0)
225 fat_length = le32_to_cpu(vs->vs_fat32_length);
226
227 __fat_size = fat_length * ms->ms_fats;
228 dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
229 (sector_size-1)) / sector_size;
230
231 __cluster_count = (sect_count - (reserved + __fat_size + dir_size)) /
232 ms->ms_cluster_size;
233 if (!ms->ms_fat_length && vs->vs_fat32_length)
234 max_count = FAT32_MAX;
235 else
236 max_count = __cluster_count > FAT12_MAX ? FAT16_MAX : FAT12_MAX;
237
238 if (__cluster_count > max_count)
239 return 0;
240
241 if (fat_size)
242 *fat_size = __fat_size;
243 if (cluster_count)
244 *cluster_count = __cluster_count;
245
246 return 1; /* valid */
247}
248
249/*
250 * This function is used by MBR partition table parser to avoid
251 * misinterpretation of FAT filesystem.
252 */
253int blkid_probe_is_vfat(blkid_probe pr)
254{
255 struct vfat_super_block *vs;
256 struct msdos_super_block *ms;
257 const struct blkid_idmag *mag = NULL;
bigbiff7b4c7a62015-01-01 19:44:14 -0500258 int rc;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500259
bigbiff7b4c7a62015-01-01 19:44:14 -0500260 rc = blkid_probe_get_idmag(pr, &vfat_idinfo, NULL, &mag);
261 if (rc < 0)
262 return rc; /* error */
263 if (rc != BLKID_PROBE_OK || !mag)
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500264 return 0;
265
266 ms = blkid_probe_get_sb(pr, mag, struct msdos_super_block);
267 if (!ms)
bigbiff7b4c7a62015-01-01 19:44:14 -0500268 return errno ? -errno : 0;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500269 vs = blkid_probe_get_sb(pr, mag, struct vfat_super_block);
270 if (!vs)
bigbiff7b4c7a62015-01-01 19:44:14 -0500271 return errno ? -errno : 0;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500272
273 return fat_valid_superblock(mag, ms, vs, NULL, NULL);
274}
275
276/* FAT label extraction from the root directory taken from Kay
277 * Sievers's volume_id library */
278static int probe_vfat(blkid_probe pr, const struct blkid_idmag *mag)
279{
280 struct vfat_super_block *vs;
281 struct msdos_super_block *ms;
282 const unsigned char *vol_label = 0;
283 unsigned char *vol_serno = NULL, vol_label_buf[11];
284 uint16_t sector_size = 0, reserved;
285 uint32_t cluster_count, fat_size;
286 const char *version = NULL;
287
288 ms = blkid_probe_get_sb(pr, mag, struct msdos_super_block);
289 if (!ms)
bigbiff7b4c7a62015-01-01 19:44:14 -0500290 return errno ? -errno : 1;
291
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500292 vs = blkid_probe_get_sb(pr, mag, struct vfat_super_block);
293 if (!vs)
bigbiff7b4c7a62015-01-01 19:44:14 -0500294 return errno ? -errno : 1;
295
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500296 if (!fat_valid_superblock(mag, ms, vs, &cluster_count, &fat_size))
297 return 1;
298
299 sector_size = unaligned_le16(&ms->ms_sector_size);
300 reserved = le16_to_cpu(ms->ms_reserved);
301
302 if (ms->ms_fat_length) {
303 /* the label may be an attribute in the root directory */
304 uint32_t root_start = (reserved + fat_size) * sector_size;
305 uint32_t root_dir_entries = unaligned_le16(&vs->vs_dir_entries);
306
307 vol_label = search_fat_label(pr, root_start, root_dir_entries);
308 if (vol_label) {
309 memcpy(vol_label_buf, vol_label, 11);
310 vol_label = vol_label_buf;
311 }
312
313 if (!vol_label || !memcmp(vol_label, no_name, 11))
314 vol_label = ms->ms_label;
315 vol_serno = ms->ms_serno;
316
317 blkid_probe_set_value(pr, "SEC_TYPE", (unsigned char *) "msdos",
318 sizeof("msdos"));
319
320 if (cluster_count < FAT12_MAX)
321 version = "FAT12";
322 else if (cluster_count < FAT16_MAX)
323 version = "FAT16";
324
325 } else if (vs->vs_fat32_length) {
326 unsigned char *buf;
327 uint16_t fsinfo_sect;
328 int maxloop = 100;
329
330 /* Search the FAT32 root dir for the label attribute */
331 uint32_t buf_size = vs->vs_cluster_size * sector_size;
332 uint32_t start_data_sect = reserved + fat_size;
333 uint32_t entries = le32_to_cpu(vs->vs_fat32_length) *
334 sector_size / sizeof(uint32_t);
335 uint32_t next = le32_to_cpu(vs->vs_root_cluster);
336
337 while (next && next < entries && --maxloop) {
338 uint32_t next_sect_off;
339 uint64_t next_off, fat_entry_off;
340 int count;
341
342 next_sect_off = (next - 2) * vs->vs_cluster_size;
343 next_off = (uint64_t)(start_data_sect + next_sect_off) *
344 sector_size;
345
346 count = buf_size / sizeof(struct vfat_dir_entry);
347
348 vol_label = search_fat_label(pr, next_off, count);
349 if (vol_label) {
350 memcpy(vol_label_buf, vol_label, 11);
351 vol_label = vol_label_buf;
352 break;
353 }
354
355 /* get FAT entry */
356 fat_entry_off = ((uint64_t) reserved * sector_size) +
357 (next * sizeof(uint32_t));
358 buf = blkid_probe_get_buffer(pr, fat_entry_off, buf_size);
359 if (buf == NULL)
360 break;
361
362 /* set next cluster */
363 next = le32_to_cpu(*((uint32_t *) buf)) & 0x0fffffff;
364 }
365
366 version = "FAT32";
367
368 if (!vol_label || !memcmp(vol_label, no_name, 11))
369 vol_label = vs->vs_label;
370 vol_serno = vs->vs_serno;
371
372 /*
373 * FAT32 should have a valid signature in the fsinfo block,
374 * but also allow all bytes set to '\0', because some volumes
375 * do not set the signature at all.
376 */
377 fsinfo_sect = le16_to_cpu(vs->vs_fsinfo_sector);
378 if (fsinfo_sect) {
379 struct fat32_fsinfo *fsinfo;
380
381 buf = blkid_probe_get_buffer(pr,
382 (blkid_loff_t) fsinfo_sect * sector_size,
383 sizeof(struct fat32_fsinfo));
384 if (buf == NULL)
bigbiff7b4c7a62015-01-01 19:44:14 -0500385 return errno ? -errno : 1;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500386
387 fsinfo = (struct fat32_fsinfo *) buf;
388 if (memcmp(fsinfo->signature1, "\x52\x52\x61\x41", 4) != 0 &&
389 memcmp(fsinfo->signature1, "\x52\x52\x64\x41", 4) != 0 &&
390 memcmp(fsinfo->signature1, "\x00\x00\x00\x00", 4) != 0)
bigbiff7b4c7a62015-01-01 19:44:14 -0500391 return 1;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500392 if (memcmp(fsinfo->signature2, "\x72\x72\x41\x61", 4) != 0 &&
393 memcmp(fsinfo->signature2, "\x00\x00\x00\x00", 4) != 0)
bigbiff7b4c7a62015-01-01 19:44:14 -0500394 return 1;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500395 }
396 }
397
398 if (vol_label && memcmp(vol_label, no_name, 11))
399 blkid_probe_set_label(pr, (unsigned char *) vol_label, 11);
400
401 /* We can't just print them as %04X, because they are unaligned */
402 if (vol_serno)
403 blkid_probe_sprintf_uuid(pr, vol_serno, 4, "%02X%02X-%02X%02X",
404 vol_serno[3], vol_serno[2], vol_serno[1], vol_serno[0]);
405 if (version)
406 blkid_probe_set_version(pr, version);
407
408 return 0;
409}
410
411
412const struct blkid_idinfo vfat_idinfo =
413{
414 .name = "vfat",
415 .usage = BLKID_USAGE_FILESYSTEM,
416 .probefunc = probe_vfat,
417 .magics =
418 {
419 { .magic = "MSWIN", .len = 5, .sboff = 0x52 },
420 { .magic = "FAT32 ", .len = 8, .sboff = 0x52 },
421 { .magic = "MSDOS", .len = 5, .sboff = 0x36 },
422 { .magic = "FAT16 ", .len = 8, .sboff = 0x36 },
423 { .magic = "FAT12 ", .len = 8, .sboff = 0x36 },
424 { .magic = "FAT ", .len = 8, .sboff = 0x36 },
425 { .magic = "\353", .len = 1, },
426 { .magic = "\351", .len = 1, },
427 { .magic = "\125\252", .len = 2, .sboff = 0x1fe },
428 { NULL }
429 }
430};
431