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 insfo_sector;
68 uint16_t reserved2[6];
79 struct vfat_dir_entry {
86 uint16_t cluster_high;
93 static uint8_t *get_attr_volume_id(struct vfat_dir_entry *dir, unsigned int count)
97 for (i = 0; i < count; i++) {
99 if (dir[i].name[0] == 0x00) {
105 if (dir[i].name[0] == FAT_ENTRY_FREE)
109 if ((dir[i].attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME)
112 if ((dir[i].attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) == FAT_ATTR_VOLUME_ID) {
113 /* labels do not have file data */
114 if (dir[i].cluster_high != 0 || dir[i].cluster_low != 0)
117 dbg("found ATTR_VOLUME_ID id in root dir");
121 dbg("skip dir entry");
127 int volume_id_probe_vfat(struct volume_id *id, uint64_t off)
129 struct vfat_super_block *vs;
130 struct vfat_dir_entry *dir;
131 uint16_t sector_size;
132 uint16_t dir_entries;
136 uint32_t root_cluster;
138 uint32_t cluster_count;
140 uint32_t fat32_length;
142 uint32_t start_data_sect;
143 uint16_t root_dir_entries;
146 uint8_t *label = NULL;
150 dbg("probing at offset 0x%llx", (unsigned long long) off);
152 vs = (struct vfat_super_block *) volume_id_get_buffer(id, off, 0x200);
156 /* believe only that's fat, don't trust the version
157 * the cluster_count will tell us
159 if (memcmp(vs->sysid, "NTFS", 4) == 0)
162 if (memcmp(vs->type.fat32.magic, "MSWIN", 5) == 0)
165 if (memcmp(vs->type.fat32.magic, "FAT32 ", 8) == 0)
168 if (memcmp(vs->type.fat.magic, "FAT16 ", 8) == 0)
171 if (memcmp(vs->type.fat.magic, "MSDOS", 5) == 0)
174 if (memcmp(vs->type.fat.magic, "FAT12 ", 8) == 0)
177 /* some old floppies don't have a magic, so we expect the boot code to match */
179 /* boot jump address check */
180 if ((vs->boot_jump[0] != 0xeb || vs->boot_jump[2] != 0x90) &&
181 vs->boot_jump[0] != 0xe9)
185 /* reserverd sector count */
194 if (vs->media < 0xf8 && vs->media != 0xf0)
197 /* cluster size check*/
198 if (vs->sectors_per_cluster == 0 ||
199 (vs->sectors_per_cluster & (vs->sectors_per_cluster-1)))
202 /* sector size check */
203 sector_size = le16_to_cpu(vs->sector_size);
204 if (sector_size != 0x200 && sector_size != 0x400 &&
205 sector_size != 0x800 && sector_size != 0x1000)
208 dbg("sector_size 0x%x", sector_size);
209 dbg("sectors_per_cluster 0x%x", vs->sectors_per_cluster);
211 dir_entries = le16_to_cpu(vs->dir_entries);
212 reserved = le16_to_cpu(vs->reserved);
213 dbg("reserved 0x%x", reserved);
215 sect_count = le16_to_cpu(vs->sectors);
217 sect_count = le32_to_cpu(vs->total_sect);
218 dbg("sect_count 0x%x", sect_count);
220 fat_length = le16_to_cpu(vs->fat_length);
221 dbg("fat_length 0x%x", fat_length);
222 fat32_length = le32_to_cpu(vs->type.fat32.fat32_length);
223 dbg("fat32_length 0x%x", fat32_length);
226 fat_size = fat_length * vs->fats;
227 else if (fat32_length)
228 fat_size = fat32_length * vs->fats;
231 dbg("fat_size 0x%x", fat_size);
233 dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
234 (sector_size-1)) / sector_size;
235 dbg("dir_size 0x%x", dir_size);
237 cluster_count = sect_count - (reserved + fat_size + dir_size);
238 cluster_count /= vs->sectors_per_cluster;
239 dbg("cluster_count 0x%x", cluster_count);
242 if (!fat_length && fat32_length)
245 /* cluster_count tells us the format */
246 if (cluster_count < FAT12_MAX)
247 strcpy(id->type_version, "FAT12");
248 else if (cluster_count < FAT16_MAX)
249 strcpy(id->type_version, "FAT16");
253 /* the label may be an attribute in the root directory */
254 root_start = (reserved + fat_size) * sector_size;
255 dbg("root dir start 0x%llx", (unsigned long long) root_start);
256 root_dir_entries = le16_to_cpu(vs->dir_entries);
257 dbg("expected entries 0x%x", root_dir_entries);
259 buf_size = root_dir_entries * sizeof(struct vfat_dir_entry);
260 buf = volume_id_get_buffer(id, off + root_start, buf_size);
264 dir = (struct vfat_dir_entry*) buf;
266 label = get_attr_volume_id(dir, root_dir_entries);
268 vs = (struct vfat_super_block *) volume_id_get_buffer(id, off, 0x200);
272 if (label != NULL && memcmp(label, "NO NAME ", 11) != 0) {
273 volume_id_set_label_raw(id, label, 11);
274 volume_id_set_label_string(id, label, 11);
275 } else if (memcmp(vs->type.fat.label, "NO NAME ", 11) != 0) {
276 volume_id_set_label_raw(id, vs->type.fat.label, 11);
277 volume_id_set_label_string(id, vs->type.fat.label, 11);
279 volume_id_set_uuid(id, vs->type.fat.serno, UUID_DOS);
283 strcpy(id->type_version, "FAT32");
285 /* FAT32 root dir is a cluster chain like any other directory */
286 buf_size = vs->sectors_per_cluster * sector_size;
287 root_cluster = le32_to_cpu(vs->type.fat32.root_cluster);
288 dbg("root dir cluster %u", root_cluster);
289 start_data_sect = reserved + fat_size;
294 uint32_t next_sect_off;
296 uint64_t fat_entry_off;
299 dbg("next cluster %u", next);
300 next_sect_off = (next - 2) * vs->sectors_per_cluster;
301 next_off = (start_data_sect + next_sect_off) * sector_size;
302 dbg("cluster offset 0x%llx", (unsigned long long) next_off);
305 buf = volume_id_get_buffer(id, off + next_off, buf_size);
309 dir = (struct vfat_dir_entry*) buf;
310 count = buf_size / sizeof(struct vfat_dir_entry);
311 dbg("expected entries 0x%x", count);
313 label = get_attr_volume_id(dir, count);
318 fat_entry_off = (reserved * sector_size) + (next * sizeof(uint32_t));
319 buf = volume_id_get_buffer(id, off + fat_entry_off, buf_size);
323 /* set next cluster */
324 next = le32_to_cpu(*((uint32_t *) buf) & 0x0fffffff);
329 dbg("reached maximum follow count of root cluster chain, give up");
331 vs = (struct vfat_super_block *) volume_id_get_buffer(id, 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);