2 * volume_id - reads filesystem label and uuid
4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
26 #include "libvolume_id.h"
29 #define FAT12_MAX 0xff5
30 #define FAT16_MAX 0xfff5
31 #define FAT_ATTR_VOLUME_ID 0x08
32 #define FAT_ATTR_DIR 0x10
33 #define FAT_ATTR_LONG_NAME 0x0f
34 #define FAT_ATTR_MASK 0x3f
35 #define FAT_ENTRY_FREE 0xe5
37 struct vfat_super_block {
41 uint8_t sectors_per_cluster;
53 struct fat_super_block {
61 struct fat32_super_block {
62 uint32_t fat32_length;
65 uint32_t root_cluster;
66 uint16_t fsinfo_sector;
68 uint16_t reserved2[6];
80 uint8_t signature1[4];
81 uint32_t reserved1[120];
82 uint8_t signature2[4];
83 uint32_t free_clusters;
84 uint32_t next_cluster;
85 uint32_t reserved2[4];
88 struct vfat_dir_entry {
95 uint16_t cluster_high;
102 static uint8_t *get_attr_volume_id(struct vfat_dir_entry *dir, unsigned int count)
106 for (i = 0; i < count; i++) {
108 if (dir[i].name[0] == 0x00) {
114 if (dir[i].name[0] == FAT_ENTRY_FREE)
118 if ((dir[i].attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME)
121 if ((dir[i].attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) == FAT_ATTR_VOLUME_ID) {
122 /* labels do not have file data */
123 if (dir[i].cluster_high != 0 || dir[i].cluster_low != 0)
126 dbg("found ATTR_VOLUME_ID id in root dir");
130 dbg("skip dir entry");
136 int volume_id_probe_vfat(struct volume_id *id, uint64_t off, uint64_t size)
138 struct vfat_super_block *vs;
139 struct vfat_dir_entry *dir;
140 struct fat32_fsinfo *fsinfo;
141 uint16_t sector_size;
142 uint16_t dir_entries;
146 uint32_t root_cluster;
148 uint32_t cluster_count;
150 uint32_t fat32_length;
152 uint32_t start_data_sect;
153 uint16_t root_dir_entries;
154 uint16_t fsinfo_sect;
157 uint8_t *label = NULL;
161 info("probing at offset 0x%llx", (unsigned long long) off);
163 buf = volume_id_get_buffer(id, off, 0x400);
167 /* check signature */
168 if (buf[510] != 0x55 || buf[511] != 0xaa)
171 vs = (struct vfat_super_block *) buf;
172 if (memcmp(vs->sysid, "NTFS", 4) == 0)
175 /* believe only that's fat, don't trust the version */
176 if (memcmp(vs->type.fat32.magic, "MSWIN", 5) == 0)
179 if (memcmp(vs->type.fat32.magic, "FAT32 ", 8) == 0)
182 if (memcmp(vs->type.fat.magic, "FAT16 ", 8) == 0)
185 if (memcmp(vs->type.fat.magic, "MSDOS", 5) == 0)
188 if (memcmp(vs->type.fat.magic, "FAT12 ", 8) == 0)
191 /* some old floppies don't have a magic, expect the boot jump address to match */
192 if ((vs->boot_jump[0] != 0xeb || vs->boot_jump[2] != 0x90) &&
193 vs->boot_jump[0] != 0xe9)
197 /* reserverd sector count */
206 if (vs->media < 0xf8 && vs->media != 0xf0)
209 /* cluster size check*/
210 if (vs->sectors_per_cluster == 0 ||
211 (vs->sectors_per_cluster & (vs->sectors_per_cluster-1)))
214 /* sector size check */
215 sector_size = le16_to_cpu(vs->sector_size);
216 if (sector_size != 0x200 && sector_size != 0x400 &&
217 sector_size != 0x800 && sector_size != 0x1000)
220 dbg("sector_size 0x%x", sector_size);
221 dbg("sectors_per_cluster 0x%x", vs->sectors_per_cluster);
223 dir_entries = le16_to_cpu(vs->dir_entries);
224 reserved = le16_to_cpu(vs->reserved);
225 dbg("reserved 0x%x", reserved);
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_length = le16_to_cpu(vs->fat_length);
233 dbg("fat_length 0x%x", fat_length);
234 fat32_length = le32_to_cpu(vs->type.fat32.fat32_length);
235 dbg("fat32_length 0x%x", fat32_length);
238 fat_size = fat_length * vs->fats;
239 else if (fat32_length)
240 fat_size = fat32_length * vs->fats;
243 dbg("fat_size 0x%x", fat_size);
245 dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
246 (sector_size-1)) / sector_size;
247 dbg("dir_size 0x%x", dir_size);
249 cluster_count = sect_count - (reserved + fat_size + dir_size);
250 cluster_count /= vs->sectors_per_cluster;
251 dbg("cluster_count 0x%x", cluster_count);
254 if (!fat_length && fat32_length)
257 /* cluster_count tells us the format */
258 if (cluster_count < FAT12_MAX)
259 strcpy(id->type_version, "FAT12");
260 else if (cluster_count < FAT16_MAX)
261 strcpy(id->type_version, "FAT16");
265 /* the label may be an attribute in the root directory */
266 root_start = (reserved + fat_size) * sector_size;
267 dbg("root dir start 0x%llx", (unsigned long long) root_start);
268 root_dir_entries = le16_to_cpu(vs->dir_entries);
269 dbg("expected entries 0x%x", root_dir_entries);
271 buf_size = root_dir_entries * sizeof(struct vfat_dir_entry);
272 buf = volume_id_get_buffer(id, off + root_start, buf_size);
276 dir = (struct vfat_dir_entry*) buf;
278 label = get_attr_volume_id(dir, root_dir_entries);
280 vs = (struct vfat_super_block *) volume_id_get_buffer(id, off, 0x200);
284 if (label != NULL && memcmp(label, "NO NAME ", 11) != 0) {
285 volume_id_set_label_raw(id, label, 11);
286 volume_id_set_label_string(id, label, 11);
287 } else if (memcmp(vs->type.fat.label, "NO NAME ", 11) != 0) {
288 volume_id_set_label_raw(id, vs->type.fat.label, 11);
289 volume_id_set_label_string(id, vs->type.fat.label, 11);
291 volume_id_set_uuid(id, vs->type.fat.serno, 0, UUID_DOS);
295 /* FAT32 should have a valid signature in the fsinfo block */
296 fsinfo_sect = le16_to_cpu(vs->type.fat32.fsinfo_sector);
297 buf = volume_id_get_buffer(id, off + (fsinfo_sect * sector_size), 0x200);
300 fsinfo = (struct fat32_fsinfo *) buf;
301 if (memcmp(fsinfo->signature1, "\x52\x52\x61\x41", 4) != 0)
303 if (memcmp(fsinfo->signature2, "\x72\x72\x41\x61", 4) != 0)
306 vs = (struct vfat_super_block *) volume_id_get_buffer(id, off, 0x200);
310 strcpy(id->type_version, "FAT32");
312 /* FAT32 root dir is a cluster chain like any other directory */
313 buf_size = vs->sectors_per_cluster * sector_size;
314 root_cluster = le32_to_cpu(vs->type.fat32.root_cluster);
315 dbg("root dir cluster %u", root_cluster);
316 start_data_sect = reserved + fat_size;
321 uint32_t next_sect_off;
323 uint64_t fat_entry_off;
326 dbg("next cluster %u", next);
327 next_sect_off = (next - 2) * vs->sectors_per_cluster;
328 next_off = (start_data_sect + next_sect_off) * sector_size;
329 dbg("cluster offset 0x%llx", (unsigned long long) next_off);
332 buf = volume_id_get_buffer(id, off + next_off, buf_size);
336 dir = (struct vfat_dir_entry*) buf;
337 count = buf_size / sizeof(struct vfat_dir_entry);
338 dbg("expected entries 0x%x", count);
340 label = get_attr_volume_id(dir, count);
345 fat_entry_off = (reserved * sector_size) + (next * sizeof(uint32_t));
346 buf = volume_id_get_buffer(id, off + fat_entry_off, buf_size);
350 /* set next cluster */
351 next = le32_to_cpu(*((uint32_t *) buf)) & 0x0fffffff;
352 if (next < 2 || next >= 0x0ffffff0)
356 dbg("reached maximum follow count of root cluster chain, give up");
358 vs = (struct vfat_super_block *) volume_id_get_buffer(id, off, 0x200);
362 if (label != NULL && memcmp(label, "NO NAME ", 11) != 0) {
363 volume_id_set_label_raw(id, label, 11);
364 volume_id_set_label_string(id, label, 11);
365 } else if (memcmp(vs->type.fat32.label, "NO NAME ", 11) != 0) {
366 volume_id_set_label_raw(id, vs->type.fat32.label, 11);
367 volume_id_set_label_string(id, vs->type.fat32.label, 11);
369 volume_id_set_uuid(id, vs->type.fat32.serno, 0, UUID_DOS);
372 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);