libblkid-tiny: add support for NTFS superblock
[project/fstools.git] / libblkid-tiny / vfat.c
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
19 #if 0
20 #include "pt-mbr.h"
21 #endif
22
23 #include "superblocks.h"
24
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));
55
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;
71 /* V3 BPB */
72 /* 20*/ uint32_t        ms_total_sect; /* iff ms_sectors == 0 */
73 /* V4 BPB */
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));
81
82 struct vfat_dir_entry {
83         uint8_t         name[11];
84         uint8_t         attr;
85         uint16_t        time_creat;
86         uint16_t        date_creat;
87         uint16_t        time_acc;
88         uint16_t        date_acc;
89         uint16_t        cluster_high;
90         uint16_t        time_write;
91         uint16_t        date_write;
92         uint16_t        cluster_low;
93         uint32_t        size;
94 } __attribute__((packed));
95
96 struct fat32_fsinfo {
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));
104
105 /* maximum number of clusters */
106 #define FAT12_MAX 0xFF4
107 #define FAT16_MAX 0xFFF4
108 #define FAT32_MAX 0x0FFFFFF6
109
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
115
116 static const char *no_name = "NO NAME    ";
117
118 #define unaligned_le16(x) \
119                 (((unsigned char *) x)[0] + (((unsigned char *) x)[1] << 8))
120
121 /*
122  * Look for LABEL (name) in the FAT root directory.
123  */
124 static unsigned char *search_fat_label(blkid_probe pr,
125                                 uint64_t offset, uint32_t entries)
126 {
127         struct vfat_dir_entry *ent, *dir = NULL;
128         uint32_t i;
129
130         DBG(LOWPROBE, ul_debug("\tlook for label in root-dir "
131                         "(entries: %d, offset: %jd)", entries, offset));
132
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,
137                                         offset,
138                                         (blkid_loff_t) entries *
139                                                 sizeof(struct vfat_dir_entry));
140                 if (!dir)
141                         return NULL;
142         }
143
144         for (i = 0; i < entries; i++) {
145                 /*
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.
149                  */
150                 if (!dir)
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));
156                 else
157                         ent = &dir[i];
158
159                 if (!ent || ent->name[0] == 0x00)
160                         break;
161
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))
165                         continue;
166
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                         return ent->name;
171                 }
172         }
173         return NULL;
174 }
175
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)
181 {
182         uint16_t sector_size, dir_entries, reserved;
183         uint32_t sect_count, __fat_size, dir_size, __cluster_count, fat_length;
184         uint32_t max_count;
185
186         /* extra check for FATs without magic strings */
187         if (mag->len <= 2) {
188                 /* Old floppies have a valid MBR signature */
189                 if (ms->ms_pmagic[0] != 0x55 || ms->ms_pmagic[1] != 0xAA)
190                         return 0;
191
192                 /*
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.
200                  */
201                 if ((memcmp(ms->ms_magic, "JFS     ", 8) == 0) ||
202                     (memcmp(ms->ms_magic, "HPFS    ", 8) == 0))
203                         return 0;
204         }
205
206         /* fat counts(Linux kernel expects at least 1 FAT table) */
207         if (!ms->ms_fats)
208                 return 0;
209         if (!ms->ms_reserved)
210                 return 0;
211         if (!(0xf8 <= ms->ms_media || ms->ms_media == 0xf0))
212                 return 0;
213         if (!is_power_of_2(ms->ms_cluster_size))
214                 return 0;
215
216         sector_size = unaligned_le16(&ms->ms_sector_size);
217         if (!is_power_of_2(sector_size) ||
218             sector_size < 512 || sector_size > 4096)
219                 return 0;
220
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);
224
225         if (sect_count == 0)
226                 sect_count = le32_to_cpu(ms->ms_total_sect);
227
228         fat_length = le16_to_cpu(ms->ms_fat_length);
229         if (fat_length == 0)
230                 fat_length = le32_to_cpu(vs->vs_fat32_length);
231
232         __fat_size = fat_length * ms->ms_fats;
233         dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
234                                         (sector_size-1)) / sector_size;
235
236         __cluster_count = (sect_count - (reserved + __fat_size + dir_size)) /
237                                                         ms->ms_cluster_size;
238         if (!ms->ms_fat_length && vs->vs_fat32_length)
239                 max_count = FAT32_MAX;
240         else
241                 max_count = __cluster_count > FAT12_MAX ? FAT16_MAX : FAT12_MAX;
242
243         if (__cluster_count > max_count)
244                 return 0;
245
246         if (fat_size)
247                 *fat_size = __fat_size;
248         if (cluster_count)
249                 *cluster_count = __cluster_count;
250
251 #if 0
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))
262                                 return 0;
263                 }
264         }
265 #endif
266
267         return 1;       /* valid */
268 }
269
270 #if 0
271 /*
272  * This function is used by MBR partition table parser to avoid
273  * misinterpretation of FAT filesystem.
274  */
275 int blkid_probe_is_vfat(blkid_probe pr)
276 {
277         struct vfat_super_block *vs;
278         struct msdos_super_block *ms;
279         const struct blkid_idmag *mag = NULL;
280         int rc;
281
282         rc = blkid_probe_get_idmag(pr, &vfat_idinfo, NULL, &mag);
283         if (rc < 0)
284                 return rc;      /* error */
285         if (rc != BLKID_PROBE_OK || !mag)
286                 return 0;
287
288         ms = blkid_probe_get_sb(pr, mag, struct msdos_super_block);
289         if (!ms)
290                 return errno ? -errno : 0;
291         vs = blkid_probe_get_sb(pr, mag, struct vfat_super_block);
292         if (!vs)
293                 return errno ? -errno : 0;
294
295         return fat_valid_superblock(pr, mag, ms, vs, NULL, NULL);
296 }
297 #endif
298
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)
302 {
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;
310
311         ms = blkid_probe_get_sb(pr, mag, struct msdos_super_block);
312         if (!ms)
313                 return errno ? -errno : 1;
314
315         vs = blkid_probe_get_sb(pr, mag, struct vfat_super_block);
316         if (!vs)
317                 return errno ? -errno : 1;
318
319         if (!fat_valid_superblock(pr, mag, ms, vs, &cluster_count, &fat_size))
320                 return 1;
321
322         sector_size = unaligned_le16(&ms->ms_sector_size);
323         reserved =  le16_to_cpu(ms->ms_reserved);
324
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);
329
330                 vol_label = search_fat_label(pr, root_start, root_dir_entries);
331                 if (vol_label) {
332                         memcpy(vol_label_buf, vol_label, 11);
333                         vol_label = vol_label_buf;
334                 }
335
336                 if (!vol_label || !memcmp(vol_label, no_name, 11))
337                         vol_label = ms->ms_label;
338                 vol_serno = ms->ms_serno;
339
340                 blkid_probe_set_value(pr, "SEC_TYPE", (unsigned char *) "msdos",
341                               sizeof("msdos"));
342
343                 if (cluster_count < FAT12_MAX)
344                         version = "FAT12";
345                 else if (cluster_count < FAT16_MAX)
346                         version = "FAT16";
347
348         } else if (vs->vs_fat32_length) {
349                 unsigned char *buf;
350                 uint16_t fsinfo_sect;
351                 int maxloop = 100;
352
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);
359
360                 while (next && next < entries && --maxloop) {
361                         uint32_t next_sect_off;
362                         uint64_t next_off, fat_entry_off;
363                         int count;
364
365                         next_sect_off = (next - 2) * vs->vs_cluster_size;
366                         next_off = (uint64_t)(start_data_sect + next_sect_off) *
367                                 sector_size;
368
369                         count = buf_size / sizeof(struct vfat_dir_entry);
370
371                         vol_label = search_fat_label(pr, next_off, count);
372                         if (vol_label) {
373                                 memcpy(vol_label_buf, vol_label, 11);
374                                 vol_label = vol_label_buf;
375                                 break;
376                         }
377
378                         /* get FAT entry */
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);
382                         if (buf == NULL)
383                                 break;
384
385                         /* set next cluster */
386                         next = le32_to_cpu(*((uint32_t *) buf)) & 0x0fffffff;
387                 }
388
389                 version = "FAT32";
390
391                 if (!vol_label || !memcmp(vol_label, no_name, 11))
392                         vol_label = vs->vs_label;
393                 vol_serno = vs->vs_serno;
394
395                 /*
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.
399                  */
400                 fsinfo_sect = le16_to_cpu(vs->vs_fsinfo_sector);
401                 if (fsinfo_sect) {
402                         struct fat32_fsinfo *fsinfo;
403
404                         buf = blkid_probe_get_buffer(pr,
405                                         (blkid_loff_t) fsinfo_sect * sector_size,
406                                         sizeof(struct fat32_fsinfo));
407                         if (buf == NULL)
408                                 return errno ? -errno : 1;
409
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)
414                                 return 1;
415                         if (memcmp(fsinfo->signature2, "\x72\x72\x41\x61", 4) != 0 &&
416                             memcmp(fsinfo->signature2, "\x00\x00\x00\x00", 4) != 0)
417                                 return 1;
418                 }
419         }
420
421         if (vol_label && memcmp(vol_label, no_name, 11))
422                 blkid_probe_set_label(pr, (unsigned char *) vol_label, 11);
423
424         /* We can't just print them as %04X, because they are unaligned */
425         if (vol_serno)
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]);
428         if (version)
429                 blkid_probe_set_version(pr, version);
430
431         return 0;
432 }
433
434
435 const struct blkid_idinfo vfat_idinfo =
436 {
437         .name           = "vfat",
438         .usage          = BLKID_USAGE_FILESYSTEM,
439         .probefunc      = probe_vfat,
440         .magics         =
441         {
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 },
451                 { NULL }
452         }
453 };
454