busybox/util-linux/volume_id/fat.c
<<
>>
Prefs
   1/*
   2 * volume_id - reads filesystem label and uuid
   3 *
   4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
   5 *
   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.
  10 *
  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.
  15 *
  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
  19 */
  20//config:config FEATURE_VOLUMEID_FAT
  21//config:       bool "fat filesystem"
  22//config:       default y
  23//config:       depends on VOLUMEID
  24
  25//kbuild:lib-$(CONFIG_FEATURE_VOLUMEID_FAT) += fat.o
  26
  27#include "volume_id_internal.h"
  28
  29/* linux/msdos_fs.h says: */
  30#define FAT12_MAX                       0xff4
  31#define FAT16_MAX                       0xfff4
  32#define FAT32_MAX                       0x0ffffff6
  33
  34#define FAT_ATTR_VOLUME_ID              0x08
  35#define FAT_ATTR_DIR                    0x10
  36#define FAT_ATTR_LONG_NAME              0x0f
  37#define FAT_ATTR_MASK                   0x3f
  38#define FAT_ENTRY_FREE                  0xe5
  39
  40struct vfat_super_block {
  41        uint8_t         boot_jump[3];
  42        uint8_t         sysid[8];
  43        uint16_t        sector_size_bytes;
  44        uint8_t         sectors_per_cluster;
  45        uint16_t        reserved_sct;
  46        uint8_t         fats;
  47        uint16_t        dir_entries;
  48        uint16_t        sectors;
  49        uint8_t         media;
  50        uint16_t        fat_length;
  51        uint16_t        secs_track;
  52        uint16_t        heads;
  53        uint32_t        hidden;
  54        uint32_t        total_sect;
  55        union {
  56                struct fat_super_block {
  57                        uint8_t         unknown[3];
  58                        uint8_t         serno[4];
  59                        uint8_t         label[11];
  60                        uint8_t         magic[8];
  61                        uint8_t         dummy2[192];
  62                        uint8_t         pmagic[2];
  63                } PACKED fat;
  64                struct fat32_super_block {
  65                        uint32_t        fat32_length;
  66                        uint16_t        flags;
  67                        uint8_t         version[2];
  68                        uint32_t        root_cluster;
  69                        uint16_t        insfo_sector;
  70                        uint16_t        backup_boot;
  71                        uint16_t        reserved2[6];
  72                        uint8_t         unknown[3];
  73                        uint8_t         serno[4];
  74                        uint8_t         label[11];
  75                        uint8_t         magic[8];
  76                        uint8_t         dummy2[164];
  77                        uint8_t         pmagic[2];
  78                } PACKED fat32;
  79        } PACKED type;
  80} PACKED;
  81
  82struct vfat_dir_entry {
  83        uint8_t         name[11];
  84        uint8_t         attr;
  85        uint16_t        time_creat;
  86        uint16_t        date_creat;
  87        uint16_t        time_acc;
  88        uint16_t        date_acc;
  89        uint16_t        cluster_high;
  90        uint16_t        time_write;
  91        uint16_t        date_write;
  92        uint16_t        cluster_low;
  93        uint32_t        size;
  94} PACKED;
  95
  96static uint8_t *get_attr_volume_id(struct vfat_dir_entry *dir, int count)
  97{
  98        for (;--count >= 0; dir++) {
  99                /* end marker */
 100                if (dir->name[0] == 0x00) {
 101                        dbg("end of dir");
 102                        break;
 103                }
 104
 105                /* empty entry */
 106                if (dir->name[0] == FAT_ENTRY_FREE)
 107                        continue;
 108
 109                /* long name */
 110                if ((dir->attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME)
 111                        continue;
 112
 113                if ((dir->attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) == FAT_ATTR_VOLUME_ID) {
 114                        /* labels do not have file data */
 115                        if (dir->cluster_high != 0 || dir->cluster_low != 0)
 116                                continue;
 117
 118                        dbg("found ATTR_VOLUME_ID id in root dir");
 119                        return dir->name;
 120                }
 121
 122                dbg("skip dir entry");
 123        }
 124
 125        return NULL;
 126}
 127
 128int FAST_FUNC volume_id_probe_vfat(struct volume_id *id /*,uint64_t fat_partition_off*/)
 129{
 130#define fat_partition_off ((uint64_t)0)
 131        struct vfat_super_block *vs;
 132        struct vfat_dir_entry *dir;
 133        uint16_t sector_size_bytes;
 134        uint16_t dir_entries;
 135        uint32_t sect_count;
 136        uint16_t reserved_sct;
 137        uint32_t fat_size_sct;
 138        uint32_t root_cluster;
 139        uint32_t dir_size_sct;
 140        uint32_t cluster_count;
 141        uint64_t root_start_off;
 142        uint32_t start_data_sct;
 143        uint8_t *buf;
 144        uint32_t buf_size;
 145        uint8_t *label = NULL;
 146        uint32_t next_cluster;
 147        int maxloop;
 148
 149        dbg("probing at offset 0x%llx", (unsigned long long) fat_partition_off);
 150
 151        vs = volume_id_get_buffer(id, fat_partition_off, 0x200);
 152        if (vs == NULL)
 153                return -1;
 154
 155        /* believe only that's fat, don't trust the version
 156         * the cluster_count will tell us
 157         */
 158        if (memcmp(vs->sysid, "NTFS", 4) == 0)
 159                return -1;
 160
 161        if (memcmp(vs->type.fat32.magic, "MSWIN", 5) == 0)
 162                goto valid;
 163
 164        if (memcmp(vs->type.fat32.magic, "FAT32   ", 8) == 0)
 165                goto valid;
 166
 167        if (memcmp(vs->type.fat.magic, "FAT16   ", 8) == 0)
 168                goto valid;
 169
 170        if (memcmp(vs->type.fat.magic, "MSDOS", 5) == 0)
 171                goto valid;
 172
 173        if (memcmp(vs->type.fat.magic, "FAT12   ", 8) == 0)
 174                goto valid;
 175
 176        /*
 177         * There are old floppies out there without a magic, so we check
 178         * for well known values and guess if it's a fat volume
 179         */
 180
 181        /* boot jump address check */
 182        if ((vs->boot_jump[0] != 0xeb || vs->boot_jump[2] != 0x90)
 183         && vs->boot_jump[0] != 0xe9
 184        ) {
 185                return -1;
 186        }
 187
 188        /* heads check */
 189        if (vs->heads == 0)
 190                return -1;
 191
 192        /* cluster size check */
 193        if (vs->sectors_per_cluster == 0
 194         || (vs->sectors_per_cluster & (vs->sectors_per_cluster-1))
 195        ) {
 196                return -1;
 197        }
 198
 199        /* media check */
 200        if (vs->media < 0xf8 && vs->media != 0xf0)
 201                return -1;
 202
 203        /* fat count*/
 204        if (vs->fats != 2)
 205                return -1;
 206
 207 valid:
 208        /* sector size check */
 209        sector_size_bytes = le16_to_cpu(vs->sector_size_bytes);
 210        if (sector_size_bytes != 0x200 && sector_size_bytes != 0x400
 211         && sector_size_bytes != 0x800 && sector_size_bytes != 0x1000
 212        ) {
 213                return -1;
 214        }
 215
 216        dbg("sector_size_bytes 0x%x", sector_size_bytes);
 217        dbg("sectors_per_cluster 0x%x", vs->sectors_per_cluster);
 218
 219        reserved_sct = le16_to_cpu(vs->reserved_sct);
 220        dbg("reserved_sct 0x%x", reserved_sct);
 221
 222        sect_count = le16_to_cpu(vs->sectors);
 223        if (sect_count == 0)
 224                sect_count = le32_to_cpu(vs->total_sect);
 225        dbg("sect_count 0x%x", sect_count);
 226
 227        fat_size_sct = le16_to_cpu(vs->fat_length);
 228        if (fat_size_sct == 0)
 229                fat_size_sct = le32_to_cpu(vs->type.fat32.fat32_length);
 230        fat_size_sct *= vs->fats;
 231        dbg("fat_size_sct 0x%x", fat_size_sct);
 232
 233        dir_entries = le16_to_cpu(vs->dir_entries);
 234        dir_size_sct = ((dir_entries * sizeof(struct vfat_dir_entry)) +
 235                        (sector_size_bytes-1)) / sector_size_bytes;
 236        dbg("dir_size_sct 0x%x", dir_size_sct);
 237
 238        cluster_count = sect_count - (reserved_sct + fat_size_sct + dir_size_sct);
 239        cluster_count /= vs->sectors_per_cluster;
 240        dbg("cluster_count 0x%x", cluster_count);
 241
 242//      if (cluster_count < FAT12_MAX) {
 243//              strcpy(id->type_version, "FAT12");
 244//      } else if (cluster_count < FAT16_MAX) {
 245//              strcpy(id->type_version, "FAT16");
 246//      } else {
 247//              strcpy(id->type_version, "FAT32");
 248//              goto fat32;
 249//      }
 250        if (cluster_count > FAT16_MAX)
 251                goto fat32;
 252
 253        /* the label may be an attribute in the root directory */
 254        root_start_off = (reserved_sct + fat_size_sct) * sector_size_bytes;
 255        dbg("root dir start 0x%llx", (unsigned long long) root_start_off);
 256        dbg("expected entries 0x%x", dir_entries);
 257
 258        buf_size = dir_entries * sizeof(struct vfat_dir_entry);
 259        buf = volume_id_get_buffer(id, fat_partition_off + root_start_off, buf_size);
 260        if (buf == NULL)
 261                goto ret;
 262
 263        label = get_attr_volume_id((struct vfat_dir_entry*) buf, dir_entries);
 264
 265        vs = volume_id_get_buffer(id, fat_partition_off, 0x200);
 266        if (vs == NULL)
 267                return -1;
 268
 269        if (label != NULL && memcmp(label, "NO NAME    ", 11) != 0) {
 270//              volume_id_set_label_raw(id, label, 11);
 271                volume_id_set_label_string(id, label, 11);
 272        } else if (memcmp(vs->type.fat.label, "NO NAME    ", 11) != 0) {
 273//              volume_id_set_label_raw(id, vs->type.fat.label, 11);
 274                volume_id_set_label_string(id, vs->type.fat.label, 11);
 275        }
 276        volume_id_set_uuid(id, vs->type.fat.serno, UUID_DOS);
 277        goto ret;
 278
 279 fat32:
 280        /* FAT32 root dir is a cluster chain like any other directory */
 281        buf_size = vs->sectors_per_cluster * sector_size_bytes;
 282        root_cluster = le32_to_cpu(vs->type.fat32.root_cluster);
 283        start_data_sct = reserved_sct + fat_size_sct;
 284
 285        next_cluster = root_cluster;
 286        maxloop = 100;
 287        while (--maxloop) {
 288                uint64_t next_off_sct;
 289                uint64_t next_off;
 290                uint64_t fat_entry_off;
 291                int count;
 292
 293                dbg("next_cluster 0x%x", (unsigned)next_cluster);
 294                next_off_sct = (uint64_t)(next_cluster - 2) * vs->sectors_per_cluster;
 295                next_off = (start_data_sct + next_off_sct) * sector_size_bytes;
 296                dbg("cluster offset 0x%llx", (unsigned long long) next_off);
 297
 298                /* get cluster */
 299                buf = volume_id_get_buffer(id, fat_partition_off + next_off, buf_size);
 300                if (buf == NULL)
 301                        goto ret;
 302
 303                dir = (struct vfat_dir_entry*) buf;
 304                count = buf_size / sizeof(struct vfat_dir_entry);
 305                dbg("expected entries 0x%x", count);
 306
 307                label = get_attr_volume_id(dir, count);
 308                if (label)
 309                        break;
 310
 311                /* get FAT entry */
 312                fat_entry_off = (reserved_sct * sector_size_bytes) + (next_cluster * sizeof(uint32_t));
 313                dbg("fat_entry_off 0x%llx", (unsigned long long)fat_entry_off);
 314                buf = volume_id_get_buffer(id, fat_partition_off + fat_entry_off, buf_size);
 315                if (buf == NULL)
 316                        goto ret;
 317
 318                /* set next cluster */
 319                next_cluster = le32_to_cpu(*(uint32_t*)buf) & 0x0fffffff;
 320                if (next_cluster < 2 || next_cluster > FAT32_MAX)
 321                        break;
 322        }
 323        if (maxloop == 0)
 324                dbg("reached maximum follow count of root cluster chain, give up");
 325
 326        vs = volume_id_get_buffer(id, fat_partition_off, 0x200);
 327        if (vs == NULL)
 328                return -1;
 329
 330        if (label != NULL && memcmp(label, "NO NAME    ", 11) != 0) {
 331//              volume_id_set_label_raw(id, label, 11);
 332                volume_id_set_label_string(id, label, 11);
 333        } else if (memcmp(vs->type.fat32.label, "NO NAME    ", 11) != 0) {
 334//              volume_id_set_label_raw(id, vs->type.fat32.label, 11);
 335                volume_id_set_label_string(id, vs->type.fat32.label, 11);
 336        }
 337        volume_id_set_uuid(id, vs->type.fat32.serno, UUID_DOS);
 338
 339 ret:
 340//      volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
 341        IF_FEATURE_BLKID_TYPE(id->type = "vfat";)
 342
 343        return 0;
 344}
 345