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
35 #include <asm/types.h>
37 #include "../volume_id.h"
38 #include "../logging.h"
42 #define FAT12_MAX 0xff5
43 #define FAT16_MAX 0xfff5
44 #define FAT_ATTR_VOLUME_ID 0x08
45 #define FAT_ATTR_DIR 0x10
46 #define FAT_ATTR_LONG_NAME 0x0f
47 #define FAT_ATTR_MASK 0x3f
48 #define FAT_ENTRY_FREE 0xe5
50 struct vfat_super_block {
54 __u8 sectors_per_cluster;
66 struct fat_super_block {
73 } __attribute__((__packed__)) fat;
74 struct fat32_super_block {
88 } __attribute__((__packed__)) fat32;
89 } __attribute__((__packed__)) type;
90 } __attribute__((__packed__));
92 struct vfat_dir_entry {
104 } __attribute__((__packed__));
106 static char *get_attr_volume_id(struct vfat_dir_entry *dir, unsigned int count)
110 for (i = 0; i < count; i++) {
112 if (dir[i].name[0] == 0x00) {
118 if (dir[i].name[0] == FAT_ENTRY_FREE)
122 if ((dir[i].attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME)
125 if ((dir[i].attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) == FAT_ATTR_VOLUME_ID) {
126 /* labels do not have file data */
127 if (dir[i].cluster_high != 0 || dir[i].cluster_low != 0)
130 dbg("found ATTR_VOLUME_ID id in root dir");
134 dbg("skip dir entry");
140 int volume_id_probe_vfat(struct volume_id *id, __u64 off)
142 struct vfat_super_block *vs;
143 struct vfat_dir_entry *dir;
154 __u32 start_data_sect;
155 __u16 root_dir_entries;
162 vs = (struct vfat_super_block *) volume_id_get_buffer(id, off, 0x200);
166 /* believe only that's fat, don't trust the version
167 * the cluster_count will tell us
169 if (strncmp(vs->sysid, "NTFS", 4) == 0)
172 if (strncmp(vs->type.fat32.magic, "MSWIN", 5) == 0)
175 if (strncmp(vs->type.fat32.magic, "FAT32 ", 8) == 0)
178 if (strncmp(vs->type.fat.magic, "FAT16 ", 8) == 0)
181 if (strncmp(vs->type.fat.magic, "MSDOS", 5) == 0)
184 if (strncmp(vs->type.fat.magic, "FAT12 ", 8) == 0)
188 * There are old floppies out there without a magic, so we check
189 * for well known values and guess if it's a fat volume
192 /* boot jump address check */
193 if ((vs->boot_jump[0] != 0xeb || vs->boot_jump[2] != 0x90) &&
194 vs->boot_jump[0] != 0xe9)
201 /* cluster size check*/
202 if (vs->sectors_per_cluster == 0 ||
203 (vs->sectors_per_cluster & (vs->sectors_per_cluster-1)))
207 if (vs->media < 0xf8 && vs->media != 0xf0)
215 /* sector size check */
216 sector_size = le16_to_cpu(vs->sector_size);
217 if (sector_size != 0x200 && sector_size != 0x400 &&
218 sector_size != 0x800 && sector_size != 0x1000)
221 dbg("sector_size 0x%x", sector_size);
222 dbg("sectors_per_cluster 0x%x", vs->sectors_per_cluster);
224 dir_entries = le16_to_cpu(vs->dir_entries);
225 reserved = le16_to_cpu(vs->reserved);
226 dbg("reserved 0x%x", reserved);
228 sect_count = le16_to_cpu(vs->sectors);
230 sect_count = le32_to_cpu(vs->total_sect);
231 dbg("sect_count 0x%x", sect_count);
233 fat_length = le16_to_cpu(vs->fat_length);
235 fat_length = le32_to_cpu(vs->type.fat32.fat32_length);
236 dbg("fat_length 0x%x", fat_length);
238 fat_size = fat_length * vs->fats;
239 dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
240 (sector_size-1)) / sector_size;
241 dbg("dir_size 0x%x", dir_size);
243 cluster_count = sect_count - (reserved + fat_size + dir_size);
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");
256 /* the label may be an attribute in the root directory */
257 root_start = (reserved + fat_size) * sector_size;
258 dbg("root dir start 0x%llx", root_start);
259 root_dir_entries = le16_to_cpu(vs->dir_entries);
260 dbg("expected entries 0x%x", root_dir_entries);
262 buf_size = root_dir_entries * sizeof(struct vfat_dir_entry);
263 buf = volume_id_get_buffer(id, off + root_start, buf_size);
267 dir = (struct vfat_dir_entry*) buf;
269 label = get_attr_volume_id(dir, root_dir_entries);
271 vs = (struct vfat_super_block *) volume_id_get_buffer(id, off, 0x200);
275 if (label != NULL && strncmp(label, "NO NAME ", 11) != 0) {
276 volume_id_set_label_raw(id, label, 11);
277 volume_id_set_label_string(id, label, 11);
278 } else if (strncmp(vs->type.fat.label, "NO NAME ", 11) != 0) {
279 volume_id_set_label_raw(id, vs->type.fat.label, 11);
280 volume_id_set_label_string(id, vs->type.fat.label, 11);
282 volume_id_set_uuid(id, vs->type.fat.serno, UUID_DOS);
286 /* FAT32 root dir is a cluster chain like any other directory */
287 buf_size = vs->sectors_per_cluster * sector_size;
288 root_cluster = le32_to_cpu(vs->type.fat32.root_cluster);
289 dbg("root dir cluster %u", root_cluster);
290 start_data_sect = reserved + fat_size;
300 dbg("next cluster %u", next);
301 next_sect_off = (next - 2) * vs->sectors_per_cluster;
302 next_off = (start_data_sect + next_sect_off) * sector_size;
303 dbg("cluster offset 0x%llx", next_off);
306 buf = volume_id_get_buffer(id, off + next_off, buf_size);
310 dir = (struct vfat_dir_entry*) buf;
311 count = buf_size / sizeof(struct vfat_dir_entry);
312 dbg("expected entries 0x%x", count);
314 label = get_attr_volume_id(dir, count);
319 fat_entry_off = (reserved * sector_size) + (next * sizeof(__u32));
320 buf = volume_id_get_buffer(id, off + fat_entry_off, buf_size);
324 /* set next cluster */
325 next = le32_to_cpu(*((__u32 *) buf) & 0x0fffffff);
330 dbg("reached maximum follow count of root cluster chain, give up");
332 vs = (struct vfat_super_block *) volume_id_get_buffer(id, off, 0x200);
336 if (label != NULL && strncmp(label, "NO NAME ", 11) != 0) {
337 volume_id_set_label_raw(id, label, 11);
338 volume_id_set_label_string(id, label, 11);
339 } else if (strncmp(vs->type.fat32.label, "NO NAME ", 11) != 0) {
340 volume_id_set_label_raw(id, vs->type.fat32.label, 11);
341 volume_id_set_label_string(id, vs->type.fat32.label, 11);
343 volume_id_set_uuid(id, vs->type.fat32.serno, UUID_DOS);
346 volume_id_set_usage(id, VOLUME_ID_DISKLABEL);