X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=extras%2Fvolume_id%2Flib%2Ffat.c;h=c0cfed82c008ccfd9d18c3a37f466f45ea7abbcf;hb=f07996885dab45102492d7f16e7e2997e264c725;hp=54e6bab7d27201891a64c9a3fa0f0ccfb23a0163;hpb=335b8aace33a2018c947381d32c33bcebe6cbe37;p=elogind.git diff --git a/extras/volume_id/lib/fat.c b/extras/volume_id/lib/fat.c index 54e6bab7d..c0cfed82c 100644 --- a/extras/volume_id/lib/fat.c +++ b/extras/volume_id/lib/fat.c @@ -4,19 +4,24 @@ * Copyright (C) 2004-2007 Kay Sievers * Copyright (C) 2007 Ryan Lortie * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation version 2 of the License. + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ #ifndef _GNU_SOURCE #define _GNU_SOURCE 1 #endif -#ifdef HAVE_CONFIG_H -# include -#endif - #include #include #include @@ -25,7 +30,7 @@ #include #include "libvolume_id.h" -#include "util.h" +#include "libvolume_id-private.h" #define FAT12_MAX 0xff5 #define FAT16_MAX 0xfff5 @@ -217,7 +222,7 @@ static size_t get_fat_attr_volume_id(uint8_t *filename, size_t fnsize, for (i = 0; i < count; i++) { /* end marker */ if (dir[i].name[0] == 0x00) { - dbg("end of dir"); + dbg("end of dir\n"); break; } @@ -234,11 +239,11 @@ static size_t get_fat_attr_volume_id(uint8_t *filename, size_t fnsize, if (dir[i].cluster_high != 0 || dir[i].cluster_low != 0) continue; - dbg("found ATTR_VOLUME_ID id in root dir"); + dbg("found ATTR_VOLUME_ID id in root dir\n"); return fat_read_filename(filename, fnsize, dir, &dir[i]); } - dbg("skip dir entry"); + dbg("skip dir entry\n"); } return 0; @@ -270,16 +275,12 @@ int volume_id_probe_vfat(struct volume_id *id, uint64_t off, uint64_t size) int maxloop; size_t fnlen; - info("probing at offset 0x%llx", (unsigned long long) off); + info("probing at offset 0x%" PRIx64 "\n", off); buf = volume_id_get_buffer(id, off, 0x400); if (buf == NULL) return -1; - /* check signature */ - if (buf[510] != 0x55 || buf[511] != 0xaa) - return -1; - vs = (struct vfat_super_block *) buf; if (memcmp(vs->sysid, "NTFS", 4) == 0) return -1; @@ -300,17 +301,22 @@ int volume_id_probe_vfat(struct volume_id *id, uint64_t off, uint64_t size) if (memcmp(vs->type.fat.magic, "FAT12 ", 8) == 0) goto magic; + /* check signature */ + if (buf[510] != 0x55 || buf[511] != 0xaa) + return -1; + /* some old floppies don't have a magic, expect the boot jump address to match */ if ((vs->boot_jump[0] != 0xeb || vs->boot_jump[2] != 0x90) && vs->boot_jump[0] != 0xe9) return -1; magic: + info("magic found\n"); /* reserverd sector count */ if (!vs->reserved) return -1; - /* fat count*/ + /* fat count */ if (!vs->fats) return -1; @@ -318,33 +324,33 @@ magic: if (vs->media < 0xf8 && vs->media != 0xf0) return -1; - /* cluster size check*/ + /* cluster size check */ if (vs->sectors_per_cluster == 0 || (vs->sectors_per_cluster & (vs->sectors_per_cluster-1))) return -1; /* sector size check */ sector_size = le16_to_cpu(vs->sector_size); - if (sector_size != 0x200 && sector_size != 0x400 && - sector_size != 0x800 && sector_size != 0x1000) + if (sector_size == 0 || ((sector_size & (sector_size-1)) != 0)) return -1; - dbg("sector_size 0x%x", sector_size); - dbg("sectors_per_cluster 0x%x", vs->sectors_per_cluster); + info("checks passed\n"); + dbg("sector_size 0x%x\n", sector_size); + dbg("sectors_per_cluster 0x%x\n", vs->sectors_per_cluster); dir_entries = le16_to_cpu(vs->dir_entries); reserved = le16_to_cpu(vs->reserved); - dbg("reserved 0x%x", reserved); + dbg("reserved 0x%x\n", reserved); sect_count = le16_to_cpu(vs->sectors); if (sect_count == 0) sect_count = le32_to_cpu(vs->total_sect); - dbg("sect_count 0x%x", sect_count); + dbg("sect_count 0x%x\n", sect_count); fat_length = le16_to_cpu(vs->fat_length); - dbg("fat_length 0x%x", fat_length); + info("fat_length 0x%x\n", fat_length); fat32_length = le32_to_cpu(vs->type.fat32.fat32_length); - dbg("fat32_length 0x%x", fat32_length); + info("fat32_length 0x%x\n", fat32_length); if (fat_length) fat_size = fat_length * vs->fats; @@ -352,15 +358,15 @@ magic: fat_size = fat32_length * vs->fats; else return -1; - dbg("fat_size 0x%x", fat_size); + info("fat_size 0x%x\n", fat_size); dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) + (sector_size-1)) / sector_size; - dbg("dir_size 0x%x", dir_size); + info("dir_size 0x%x\n", dir_size); cluster_count = sect_count - (reserved + fat_size + dir_size); cluster_count /= vs->sectors_per_cluster; - dbg("cluster_count 0x%x", cluster_count); + info("cluster_count 0x%x\n", cluster_count); /* must be FAT32 */ if (!fat_length && fat32_length) @@ -376,9 +382,9 @@ magic: /* the label may be an attribute in the root directory */ root_start = (reserved + fat_size) * sector_size; - dbg("root dir start 0x%llx", (unsigned long long) root_start); + dbg("root dir start 0x%" PRIx64 "\n", root_start); root_dir_entries = le16_to_cpu(vs->dir_entries); - dbg("expected entries 0x%x", root_dir_entries); + dbg("expected entries 0x%x\n", root_dir_entries); buf_size = root_dir_entries * sizeof(struct vfat_dir_entry); buf = volume_id_get_buffer(id, off + root_start, buf_size); @@ -404,16 +410,30 @@ magic: goto found; fat32: - /* FAT32 should have a valid signature in the fsinfo block */ + info("looking for FAT32\n"); + /* + * FAT32 should have a valid signature in the fsinfo block, + * but also allow all bytes set to '\0', because some volumes + * do not set the signature at all. + */ fsinfo_sect = le16_to_cpu(vs->type.fat32.fsinfo_sector); buf = volume_id_get_buffer(id, off + (fsinfo_sect * sector_size), 0x200); if (buf == NULL) return -1; fsinfo = (struct fat32_fsinfo *) buf; - if (memcmp(fsinfo->signature1, "\x52\x52\x61\x41", 4) != 0) + info("signature1: 0x%02x%02x%02x%02x\n", + fsinfo->signature1[0], fsinfo->signature1[1], + fsinfo->signature1[2], fsinfo->signature1[3]); + info("signature2: 0x%02x%02x%02x%02x\n", + fsinfo->signature2[0], fsinfo->signature2[1], + fsinfo->signature2[2], fsinfo->signature2[3]); + if (memcmp(fsinfo->signature1, "\x52\x52\x61\x41", 4) != 0 && + memcmp(fsinfo->signature1, "\x00\x00\x00\x00", 4) != 0) return -1; - if (memcmp(fsinfo->signature2, "\x72\x72\x41\x61", 4) != 0) + if (memcmp(fsinfo->signature2, "\x72\x72\x41\x61", 4) != 0 && + memcmp(fsinfo->signature2, "\x00\x00\x00\x00", 4) != 0) return -1 ; + info("FAT32 signatures ok\n"); vs = (struct vfat_super_block *) volume_id_get_buffer(id, off, 0x200); if (vs == NULL) @@ -424,7 +444,7 @@ fat32: /* FAT32 root dir is a cluster chain like any other directory */ buf_size = vs->sectors_per_cluster * sector_size; root_cluster = le32_to_cpu(vs->type.fat32.root_cluster); - dbg("root dir cluster %u", root_cluster); + dbg("root dir cluster %u\n", root_cluster); start_data_sect = reserved + fat_size; next = root_cluster; @@ -435,10 +455,10 @@ fat32: uint64_t fat_entry_off; int count; - dbg("next cluster %u", next); + dbg("next cluster %u\n", next); next_sect_off = (next - 2) * vs->sectors_per_cluster; next_off = (start_data_sect + next_sect_off) * sector_size; - dbg("cluster offset 0x%llx", (unsigned long long) next_off); + dbg("cluster offset 0x%" PRIx64 "\n", next_off); /* get cluster */ buf = volume_id_get_buffer(id, off + next_off, buf_size); @@ -447,7 +467,7 @@ fat32: dir = (struct vfat_dir_entry*) buf; count = buf_size / sizeof(struct vfat_dir_entry); - dbg("expected entries 0x%x", count); + dbg("expected entries 0x%x\n", count); fnlen = get_fat_attr_volume_id(filename, sizeof(filename), dir, count); if (fnlen > 0) @@ -465,7 +485,7 @@ fat32: break; } if (maxloop == 0) - dbg("reached maximum follow count of root cluster chain, give up"); + dbg("reached maximum follow count of root cluster chain, give up\n"); vs = (struct vfat_super_block *) volume_id_get_buffer(id, off, 0x200); if (vs == NULL) @@ -483,6 +503,7 @@ fat32: found: volume_id_set_usage(id, VOLUME_ID_FILESYSTEM); id->type = "vfat"; - + /* we think this is fat, but we make sure no other signatures are found */ + id->force_unique_result = 1; return 0; }