blob: 476d500a686169924f648424531bc50c92493c77 [file] [log] [blame]
Denis Vlasenkode7684a2008-02-18 21:08:49 +00001/*
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 Bergh3b458012013-07-31 15:45:20 +020021//kbuild:lib-$(CONFIG_FEATURE_VOLUMEID_FAT) += fat.o
22
Sven-Göran Bergh15d0a862013-07-31 15:57:59 +020023//config:
24//config:config FEATURE_VOLUMEID_FAT
25//config: bool "fat filesystem"
26//config: default y
27//config: depends on VOLUMEID
28//config: help
29//config: TODO
30//config:
31
Denis Vlasenkode7684a2008-02-18 21:08:49 +000032#include "volume_id_internal.h"
33
Denis Vlasenkod5e30592008-10-12 11:17:49 +000034/* linux/msdos_fs.h says: */
35#define FAT12_MAX 0xff4
36#define FAT16_MAX 0xfff4
37#define FAT32_MAX 0x0ffffff6
38
Denis Vlasenkode7684a2008-02-18 21:08:49 +000039#define FAT_ATTR_VOLUME_ID 0x08
40#define FAT_ATTR_DIR 0x10
41#define FAT_ATTR_LONG_NAME 0x0f
42#define FAT_ATTR_MASK 0x3f
43#define FAT_ENTRY_FREE 0xe5
44
45struct vfat_super_block {
46 uint8_t boot_jump[3];
47 uint8_t sysid[8];
Denis Vlasenkod5e30592008-10-12 11:17:49 +000048 uint16_t sector_size_bytes;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000049 uint8_t sectors_per_cluster;
Denis Vlasenkod5e30592008-10-12 11:17:49 +000050 uint16_t reserved_sct;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000051 uint8_t fats;
52 uint16_t dir_entries;
53 uint16_t sectors;
54 uint8_t media;
55 uint16_t fat_length;
56 uint16_t secs_track;
57 uint16_t heads;
58 uint32_t hidden;
59 uint32_t total_sect;
60 union {
61 struct fat_super_block {
62 uint8_t unknown[3];
63 uint8_t serno[4];
64 uint8_t label[11];
65 uint8_t magic[8];
66 uint8_t dummy2[192];
67 uint8_t pmagic[2];
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020068 } PACKED fat;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000069 struct fat32_super_block {
70 uint32_t fat32_length;
71 uint16_t flags;
72 uint8_t version[2];
73 uint32_t root_cluster;
74 uint16_t insfo_sector;
75 uint16_t backup_boot;
76 uint16_t reserved2[6];
77 uint8_t unknown[3];
78 uint8_t serno[4];
79 uint8_t label[11];
80 uint8_t magic[8];
81 uint8_t dummy2[164];
82 uint8_t pmagic[2];
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020083 } PACKED fat32;
84 } PACKED type;
85} PACKED;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000086
87struct vfat_dir_entry {
88 uint8_t name[11];
89 uint8_t attr;
90 uint16_t time_creat;
91 uint16_t date_creat;
92 uint16_t time_acc;
93 uint16_t date_acc;
94 uint16_t cluster_high;
95 uint16_t time_write;
96 uint16_t date_write;
97 uint16_t cluster_low;
98 uint32_t size;
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020099} PACKED;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000100
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000101static uint8_t *get_attr_volume_id(struct vfat_dir_entry *dir, int count)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000102{
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000103 for (;--count >= 0; dir++) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000104 /* end marker */
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000105 if (dir->name[0] == 0x00) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000106 dbg("end of dir");
107 break;
108 }
109
110 /* empty entry */
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000111 if (dir->name[0] == FAT_ENTRY_FREE)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000112 continue;
113
114 /* long name */
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000115 if ((dir->attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000116 continue;
117
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000118 if ((dir->attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) == FAT_ATTR_VOLUME_ID) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000119 /* labels do not have file data */
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000120 if (dir->cluster_high != 0 || dir->cluster_low != 0)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000121 continue;
122
123 dbg("found ATTR_VOLUME_ID id in root dir");
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000124 return dir->name;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000125 }
126
127 dbg("skip dir entry");
128 }
129
130 return NULL;
131}
132
Denys Vlasenko89300962009-11-01 23:07:18 +0100133int FAST_FUNC volume_id_probe_vfat(struct volume_id *id /*,uint64_t fat_partition_off*/)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000134{
Denis Vlasenko28ea4292009-02-15 05:51:19 +0000135#define fat_partition_off ((uint64_t)0)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000136 struct vfat_super_block *vs;
137 struct vfat_dir_entry *dir;
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000138 uint16_t sector_size_bytes;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000139 uint16_t dir_entries;
140 uint32_t sect_count;
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000141 uint16_t reserved_sct;
142 uint32_t fat_size_sct;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000143 uint32_t root_cluster;
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000144 uint32_t dir_size_sct;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000145 uint32_t cluster_count;
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000146 uint64_t root_start_off;
147 uint32_t start_data_sct;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000148 uint8_t *buf;
149 uint32_t buf_size;
150 uint8_t *label = NULL;
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000151 uint32_t next_cluster;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000152 int maxloop;
153
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000154 dbg("probing at offset 0x%llx", (unsigned long long) fat_partition_off);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000155
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000156 vs = volume_id_get_buffer(id, fat_partition_off, 0x200);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000157 if (vs == NULL)
158 return -1;
159
160 /* believe only that's fat, don't trust the version
161 * the cluster_count will tell us
162 */
163 if (memcmp(vs->sysid, "NTFS", 4) == 0)
164 return -1;
165
166 if (memcmp(vs->type.fat32.magic, "MSWIN", 5) == 0)
167 goto valid;
168
169 if (memcmp(vs->type.fat32.magic, "FAT32 ", 8) == 0)
170 goto valid;
171
172 if (memcmp(vs->type.fat.magic, "FAT16 ", 8) == 0)
173 goto valid;
174
175 if (memcmp(vs->type.fat.magic, "MSDOS", 5) == 0)
176 goto valid;
177
178 if (memcmp(vs->type.fat.magic, "FAT12 ", 8) == 0)
179 goto valid;
180
181 /*
182 * There are old floppies out there without a magic, so we check
183 * for well known values and guess if it's a fat volume
184 */
185
186 /* boot jump address check */
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100187 if ((vs->boot_jump[0] != 0xeb || vs->boot_jump[2] != 0x90)
188 && vs->boot_jump[0] != 0xe9
189 ) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000190 return -1;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100191 }
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000192
193 /* heads check */
194 if (vs->heads == 0)
195 return -1;
196
Denis Vlasenkoc6938402008-03-24 02:18:03 +0000197 /* cluster size check */
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100198 if (vs->sectors_per_cluster == 0
199 || (vs->sectors_per_cluster & (vs->sectors_per_cluster-1))
200 ) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000201 return -1;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100202 }
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000203
204 /* media check */
205 if (vs->media < 0xf8 && vs->media != 0xf0)
206 return -1;
207
208 /* fat count*/
209 if (vs->fats != 2)
210 return -1;
211
212 valid:
213 /* sector size check */
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000214 sector_size_bytes = le16_to_cpu(vs->sector_size_bytes);
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100215 if (sector_size_bytes != 0x200 && sector_size_bytes != 0x400
216 && sector_size_bytes != 0x800 && sector_size_bytes != 0x1000
217 ) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000218 return -1;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100219 }
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000220
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000221 dbg("sector_size_bytes 0x%x", sector_size_bytes);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000222 dbg("sectors_per_cluster 0x%x", vs->sectors_per_cluster);
223
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000224 reserved_sct = le16_to_cpu(vs->reserved_sct);
225 dbg("reserved_sct 0x%x", reserved_sct);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000226
227 sect_count = le16_to_cpu(vs->sectors);
228 if (sect_count == 0)
229 sect_count = le32_to_cpu(vs->total_sect);
230 dbg("sect_count 0x%x", sect_count);
231
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000232 fat_size_sct = le16_to_cpu(vs->fat_length);
233 if (fat_size_sct == 0)
234 fat_size_sct = le32_to_cpu(vs->type.fat32.fat32_length);
235 fat_size_sct *= vs->fats;
236 dbg("fat_size_sct 0x%x", fat_size_sct);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000237
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000238 dir_entries = le16_to_cpu(vs->dir_entries);
239 dir_size_sct = ((dir_entries * sizeof(struct vfat_dir_entry)) +
240 (sector_size_bytes-1)) / sector_size_bytes;
241 dbg("dir_size_sct 0x%x", dir_size_sct);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000242
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000243 cluster_count = sect_count - (reserved_sct + fat_size_sct + dir_size_sct);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000244 cluster_count /= vs->sectors_per_cluster;
245 dbg("cluster_count 0x%x", cluster_count);
246
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000247// if (cluster_count < FAT12_MAX) {
248// strcpy(id->type_version, "FAT12");
249// } else if (cluster_count < FAT16_MAX) {
250// strcpy(id->type_version, "FAT16");
251// } else {
252// strcpy(id->type_version, "FAT32");
253// goto fat32;
254// }
255 if (cluster_count >= FAT16_MAX)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000256 goto fat32;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000257
258 /* the label may be an attribute in the root directory */
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000259 root_start_off = (reserved_sct + fat_size_sct) * sector_size_bytes;
260 dbg("root dir start 0x%llx", (unsigned long long) root_start_off);
261 dbg("expected entries 0x%x", dir_entries);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000262
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000263 buf_size = dir_entries * sizeof(struct vfat_dir_entry);
264 buf = volume_id_get_buffer(id, fat_partition_off + root_start_off, buf_size);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000265 if (buf == NULL)
Denis Vlasenko1e10afc2008-11-30 17:41:31 +0000266 goto ret;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000267
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000268 label = get_attr_volume_id((struct vfat_dir_entry*) buf, dir_entries);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000269
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000270 vs = volume_id_get_buffer(id, fat_partition_off, 0x200);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000271 if (vs == NULL)
272 return -1;
273
274 if (label != NULL && memcmp(label, "NO NAME ", 11) != 0) {
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000275// volume_id_set_label_raw(id, label, 11);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000276 volume_id_set_label_string(id, label, 11);
277 } else if (memcmp(vs->type.fat.label, "NO NAME ", 11) != 0) {
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000278// volume_id_set_label_raw(id, vs->type.fat.label, 11);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000279 volume_id_set_label_string(id, vs->type.fat.label, 11);
280 }
281 volume_id_set_uuid(id, vs->type.fat.serno, UUID_DOS);
Denis Vlasenko1e10afc2008-11-30 17:41:31 +0000282 goto ret;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000283
284 fat32:
285 /* FAT32 root dir is a cluster chain like any other directory */
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000286 buf_size = vs->sectors_per_cluster * sector_size_bytes;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000287 root_cluster = le32_to_cpu(vs->type.fat32.root_cluster);
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000288 start_data_sct = reserved_sct + fat_size_sct;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000289
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000290 next_cluster = root_cluster;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000291 maxloop = 100;
292 while (--maxloop) {
Denis Vlasenko1e10afc2008-11-30 17:41:31 +0000293 uint64_t next_off_sct;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000294 uint64_t next_off;
295 uint64_t fat_entry_off;
296 int count;
297
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000298 dbg("next_cluster 0x%x", (unsigned)next_cluster);
Denis Vlasenko1e10afc2008-11-30 17:41:31 +0000299 next_off_sct = (uint64_t)(next_cluster - 2) * vs->sectors_per_cluster;
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000300 next_off = (start_data_sct + next_off_sct) * sector_size_bytes;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000301 dbg("cluster offset 0x%llx", (unsigned long long) next_off);
302
303 /* get cluster */
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000304 buf = volume_id_get_buffer(id, fat_partition_off + next_off, buf_size);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000305 if (buf == NULL)
Denis Vlasenko1e10afc2008-11-30 17:41:31 +0000306 goto ret;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000307
308 dir = (struct vfat_dir_entry*) buf;
309 count = buf_size / sizeof(struct vfat_dir_entry);
310 dbg("expected entries 0x%x", count);
311
312 label = get_attr_volume_id(dir, count);
313 if (label)
314 break;
315
316 /* get FAT entry */
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000317 fat_entry_off = (reserved_sct * sector_size_bytes) + (next_cluster * sizeof(uint32_t));
318 dbg("fat_entry_off 0x%llx", (unsigned long long)fat_entry_off);
319 buf = volume_id_get_buffer(id, fat_partition_off + fat_entry_off, buf_size);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000320 if (buf == NULL)
Denis Vlasenko1e10afc2008-11-30 17:41:31 +0000321 goto ret;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000322
323 /* set next cluster */
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000324 next_cluster = le32_to_cpu(*(uint32_t*)buf) & 0x0fffffff;
325 if (next_cluster < 2 || next_cluster > FAT32_MAX)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000326 break;
327 }
328 if (maxloop == 0)
329 dbg("reached maximum follow count of root cluster chain, give up");
330
Denis Vlasenkod5e30592008-10-12 11:17:49 +0000331 vs = volume_id_get_buffer(id, fat_partition_off, 0x200);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000332 if (vs == NULL)
333 return -1;
334
335 if (label != NULL && memcmp(label, "NO NAME ", 11) != 0) {
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000336// volume_id_set_label_raw(id, label, 11);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000337 volume_id_set_label_string(id, label, 11);
338 } else if (memcmp(vs->type.fat32.label, "NO NAME ", 11) != 0) {
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000339// volume_id_set_label_raw(id, vs->type.fat32.label, 11);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000340 volume_id_set_label_string(id, vs->type.fat32.label, 11);
341 }
342 volume_id_set_uuid(id, vs->type.fat32.serno, UUID_DOS);
343
Denis Vlasenko1e10afc2008-11-30 17:41:31 +0000344 ret:
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000345// volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
Denys Vlasenko90615a02010-12-30 00:40:11 +0100346 IF_FEATURE_BLKID_TYPE(id->type = "vfat";)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000347
348 return 0;
349}