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