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