2 * volume_id - reads filesystem label and uuid
4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 //kbuild:lib-$(CONFIG_FEATURE_VOLUMEID_FAT) += fat.o
24 //config:config FEATURE_VOLUMEID_FAT
25 //config: bool "fat filesystem"
27 //config: depends on VOLUMEID
32 #include "volume_id_internal.h"
34 /* linux/msdos_fs.h says: */
35 #define FAT12_MAX 0xff4
36 #define FAT16_MAX 0xfff4
37 #define FAT32_MAX 0x0ffffff6
39 #define FAT_ATTR_VOLUME_ID 0x08
40 #define FAT_ATTR_DIR 0x10
41 #define FAT_ATTR_LONG_NAME 0x0f
42 #define FAT_ATTR_MASK 0x3f
43 #define FAT_ENTRY_FREE 0xe5
45 struct vfat_super_block {
48 uint16_t sector_size_bytes;
49 uint8_t sectors_per_cluster;
50 uint16_t reserved_sct;
61 struct fat_super_block {
69 struct fat32_super_block {
70 uint32_t fat32_length;
73 uint32_t root_cluster;
74 uint16_t insfo_sector;
76 uint16_t reserved2[6];
87 struct vfat_dir_entry {
94 uint16_t cluster_high;
101 static uint8_t *get_attr_volume_id(struct vfat_dir_entry *dir, int count)
103 for (;--count >= 0; dir++) {
105 if (dir->name[0] == 0x00) {
111 if (dir->name[0] == FAT_ENTRY_FREE)
115 if ((dir->attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME)
118 if ((dir->attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) == FAT_ATTR_VOLUME_ID) {
119 /* labels do not have file data */
120 if (dir->cluster_high != 0 || dir->cluster_low != 0)
123 dbg("found ATTR_VOLUME_ID id in root dir");
127 dbg("skip dir entry");
133 int FAST_FUNC volume_id_probe_vfat(struct volume_id *id /*,uint64_t fat_partition_off*/)
135 #define fat_partition_off ((uint64_t)0)
136 struct vfat_super_block *vs;
137 struct vfat_dir_entry *dir;
138 uint16_t sector_size_bytes;
139 uint16_t dir_entries;
141 uint16_t reserved_sct;
142 uint32_t fat_size_sct;
143 uint32_t root_cluster;
144 uint32_t dir_size_sct;
145 uint32_t cluster_count;
146 uint64_t root_start_off;
147 uint32_t start_data_sct;
150 uint8_t *label = NULL;
151 uint32_t next_cluster;
154 dbg("probing at offset 0x%llx", (unsigned long long) fat_partition_off);
156 vs = volume_id_get_buffer(id, fat_partition_off, 0x200);
160 /* believe only that's fat, don't trust the version
161 * the cluster_count will tell us
163 if (memcmp(vs->sysid, "NTFS", 4) == 0)
166 if (memcmp(vs->type.fat32.magic, "MSWIN", 5) == 0)
169 if (memcmp(vs->type.fat32.magic, "FAT32 ", 8) == 0)
172 if (memcmp(vs->type.fat.magic, "FAT16 ", 8) == 0)
175 if (memcmp(vs->type.fat.magic, "MSDOS", 5) == 0)
178 if (memcmp(vs->type.fat.magic, "FAT12 ", 8) == 0)
182 * There are old floppies out there without a magic, so we check
183 * for well known values and guess if it's a fat volume
186 /* boot jump address check */
187 if ((vs->boot_jump[0] != 0xeb || vs->boot_jump[2] != 0x90)
188 && vs->boot_jump[0] != 0xe9
197 /* cluster size check */
198 if (vs->sectors_per_cluster == 0
199 || (vs->sectors_per_cluster & (vs->sectors_per_cluster-1))
205 if (vs->media < 0xf8 && vs->media != 0xf0)
213 /* sector size check */
214 sector_size_bytes = le16_to_cpu(vs->sector_size_bytes);
215 if (sector_size_bytes != 0x200 && sector_size_bytes != 0x400
216 && sector_size_bytes != 0x800 && sector_size_bytes != 0x1000
221 dbg("sector_size_bytes 0x%x", sector_size_bytes);
222 dbg("sectors_per_cluster 0x%x", vs->sectors_per_cluster);
224 reserved_sct = le16_to_cpu(vs->reserved_sct);
225 dbg("reserved_sct 0x%x", reserved_sct);
227 sect_count = le16_to_cpu(vs->sectors);
229 sect_count = le32_to_cpu(vs->total_sect);
230 dbg("sect_count 0x%x", sect_count);
232 fat_size_sct = le16_to_cpu(vs->fat_length);
233 if (fat_size_sct == 0)
234 fat_size_sct = le32_to_cpu(vs->type.fat32.fat32_length);
235 fat_size_sct *= vs->fats;
236 dbg("fat_size_sct 0x%x", fat_size_sct);
238 dir_entries = le16_to_cpu(vs->dir_entries);
239 dir_size_sct = ((dir_entries * sizeof(struct vfat_dir_entry)) +
240 (sector_size_bytes-1)) / sector_size_bytes;
241 dbg("dir_size_sct 0x%x", dir_size_sct);
243 cluster_count = sect_count - (reserved_sct + fat_size_sct + dir_size_sct);
244 cluster_count /= vs->sectors_per_cluster;
245 dbg("cluster_count 0x%x", cluster_count);
247 // if (cluster_count < FAT12_MAX) {
248 // strcpy(id->type_version, "FAT12");
249 // } else if (cluster_count < FAT16_MAX) {
250 // strcpy(id->type_version, "FAT16");
252 // strcpy(id->type_version, "FAT32");
255 if (cluster_count >= FAT16_MAX)
258 /* the label may be an attribute in the root directory */
259 root_start_off = (reserved_sct + fat_size_sct) * sector_size_bytes;
260 dbg("root dir start 0x%llx", (unsigned long long) root_start_off);
261 dbg("expected entries 0x%x", dir_entries);
263 buf_size = dir_entries * sizeof(struct vfat_dir_entry);
264 buf = volume_id_get_buffer(id, fat_partition_off + root_start_off, buf_size);
268 label = get_attr_volume_id((struct vfat_dir_entry*) buf, dir_entries);
270 vs = volume_id_get_buffer(id, fat_partition_off, 0x200);
274 if (label != NULL && memcmp(label, "NO NAME ", 11) != 0) {
275 // volume_id_set_label_raw(id, label, 11);
276 volume_id_set_label_string(id, label, 11);
277 } else if (memcmp(vs->type.fat.label, "NO NAME ", 11) != 0) {
278 // volume_id_set_label_raw(id, vs->type.fat.label, 11);
279 volume_id_set_label_string(id, vs->type.fat.label, 11);
281 volume_id_set_uuid(id, vs->type.fat.serno, UUID_DOS);
285 /* FAT32 root dir is a cluster chain like any other directory */
286 buf_size = vs->sectors_per_cluster * sector_size_bytes;
287 root_cluster = le32_to_cpu(vs->type.fat32.root_cluster);
288 start_data_sct = reserved_sct + fat_size_sct;
290 next_cluster = root_cluster;
293 uint64_t next_off_sct;
295 uint64_t fat_entry_off;
298 dbg("next_cluster 0x%x", (unsigned)next_cluster);
299 next_off_sct = (uint64_t)(next_cluster - 2) * vs->sectors_per_cluster;
300 next_off = (start_data_sct + next_off_sct) * sector_size_bytes;
301 dbg("cluster offset 0x%llx", (unsigned long long) next_off);
304 buf = volume_id_get_buffer(id, fat_partition_off + next_off, buf_size);
308 dir = (struct vfat_dir_entry*) buf;
309 count = buf_size / sizeof(struct vfat_dir_entry);
310 dbg("expected entries 0x%x", count);
312 label = get_attr_volume_id(dir, count);
317 fat_entry_off = (reserved_sct * sector_size_bytes) + (next_cluster * sizeof(uint32_t));
318 dbg("fat_entry_off 0x%llx", (unsigned long long)fat_entry_off);
319 buf = volume_id_get_buffer(id, fat_partition_off + fat_entry_off, buf_size);
323 /* set next cluster */
324 next_cluster = le32_to_cpu(*(uint32_t*)buf) & 0x0fffffff;
325 if (next_cluster < 2 || next_cluster > FAT32_MAX)
329 dbg("reached maximum follow count of root cluster chain, give up");
331 vs = volume_id_get_buffer(id, fat_partition_off, 0x200);
335 if (label != NULL && memcmp(label, "NO NAME ", 11) != 0) {
336 // volume_id_set_label_raw(id, label, 11);
337 volume_id_set_label_string(id, label, 11);
338 } else if (memcmp(vs->type.fat32.label, "NO NAME ", 11) != 0) {
339 // volume_id_set_label_raw(id, vs->type.fat32.label, 11);
340 volume_id_set_label_string(id, vs->type.fat32.label, 11);
342 volume_id_set_uuid(id, vs->type.fat32.serno, UUID_DOS);
345 // volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
346 IF_FEATURE_BLKID_TYPE(id->type = "vfat";)