/src/sleuthkit/tsk/fs/fatxxfs.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** fatxxfs |
3 | | ** The Sleuth Kit |
4 | | ** |
5 | | ** Content and meta data layer support for the FATXX file system |
6 | | ** |
7 | | ** Brian Carrier [carrier <at> sleuthkit [dot] org] |
8 | | ** Copyright (c) 2006-2013 Brian Carrier, Basis Technology. All Rights reserved |
9 | | ** Copyright (c) 2003-2005 Brian Carrier. All rights reserved |
10 | | ** |
11 | | ** TASK |
12 | | ** Copyright (c) 2002 Brian Carrier, @stake Inc. All rights reserved |
13 | | ** |
14 | | ** |
15 | | ** This software is distributed under the Common Public License 1.0 |
16 | | ** |
17 | | ** Unicode added with support from I.D.E.A.L. Technology Corp (Aug '05) |
18 | | ** |
19 | | */ |
20 | | |
21 | | /** |
22 | | * \file fatxxfs.c |
23 | | * Contains the internal TSK FATXX (FAT12, FAT16, FAT32) file system code to |
24 | | * handle basic file system processing for opening file system, processing |
25 | | * sectors, and directory entries. |
26 | | */ |
27 | | |
28 | | #include "tsk_fatxxfs.h" |
29 | | |
30 | | /* |
31 | | * Implementation NOTES |
32 | | * |
33 | | * TSK_FS_META contains the first cluster. file_walk will return sector |
34 | | * values though because the cluster numbers do not start until after |
35 | | * the FAT. That makes it very hard to address the first few blocks! |
36 | | * |
37 | | * Inodes numbers do not exist in FAT. To make up for this we will count |
38 | | * directory entries as the inodes. As the root directory does not have |
39 | | * any records in FAT, we will give it times of 0 and call it inode 2 to |
40 | | * keep consistent with UNIX. After that, each 32-byte slot is numbered |
41 | | * as though it were a directory entry (even if it is not). Therefore, |
42 | | * when an inode walk is performed, not all inode values will be displayed |
43 | | * even when '-e' is given for ils. |
44 | | * |
45 | | * Progs like 'ils -e' are very slow because we have to look at each |
46 | | * block to see if it is a file system structure. |
47 | | */ |
48 | | |
49 | | /** |
50 | | * Print details about the file system to a file handle. |
51 | | * |
52 | | * @param fs File system to print details on |
53 | | * @param hFile File handle to print text to |
54 | | * |
55 | | * @returns 1 on error and 0 on success |
56 | | */ |
57 | | static uint8_t |
58 | | fatxxfs_fsstat(TSK_FS_INFO * fs, FILE * hFile) |
59 | 0 | { |
60 | 0 | unsigned int i; |
61 | 0 | TSK_DADDR_T next, snext, sstart, send; |
62 | 0 | FATFS_INFO *fatfs = (FATFS_INFO *) fs; |
63 | 0 | FATXXFS_SB *sb = (FATXXFS_SB*)fatfs->boot_sector_buffer; |
64 | 0 | char *data_buf; |
65 | 0 | FATXXFS_DENTRY *vol_label_dentry = NULL; |
66 | 0 | ssize_t cnt; |
67 | | |
68 | | // clean up any error messages that are lying around |
69 | 0 | tsk_error_reset(); |
70 | |
|
71 | 0 | if ((data_buf = (char *) tsk_malloc(fs->block_size)) == NULL) { |
72 | 0 | return 1; |
73 | 0 | } |
74 | | |
75 | | |
76 | | /* Read the root directory sector so that we can get the volume |
77 | | * label from it */ |
78 | 0 | cnt = tsk_fs_read_block(fs, fatfs->rootsect, data_buf, fs->block_size); |
79 | 0 | if (cnt != fs->block_size) { |
80 | 0 | if (cnt >= 0) { |
81 | 0 | tsk_error_reset(); |
82 | 0 | tsk_error_set_errno(TSK_ERR_FS_READ); |
83 | 0 | } |
84 | 0 | tsk_error_set_errstr2("fatxxfs_fsstat: root directory: %" PRIuDADDR, |
85 | 0 | fatfs->rootsect); |
86 | 0 | free(data_buf); |
87 | 0 | return 1; |
88 | 0 | } |
89 | | |
90 | | |
91 | | /* Find the dentry that is set as the volume label */ |
92 | 0 | vol_label_dentry = NULL; |
93 | 0 | if (fatfs->ssize <= fs->block_size) { |
94 | 0 | FATXXFS_DENTRY *current_entry = (FATXXFS_DENTRY *) data_buf; |
95 | 0 | for (i = 0; i < fatfs->ssize; i += sizeof(*current_entry)) { |
96 | 0 | if (current_entry->attrib == FATFS_ATTR_VOLUME) { |
97 | 0 | vol_label_dentry = current_entry; |
98 | 0 | break; |
99 | 0 | } |
100 | 0 | current_entry++; |
101 | 0 | } |
102 | 0 | } |
103 | | |
104 | | |
105 | | /* Print the general file system information */ |
106 | |
|
107 | 0 | tsk_fprintf(hFile, "FILE SYSTEM INFORMATION\n"); |
108 | 0 | tsk_fprintf(hFile, "--------------------------------------------\n"); |
109 | |
|
110 | 0 | tsk_fprintf(hFile, "File System Type: FAT"); |
111 | 0 | if (fs->ftype == TSK_FS_TYPE_FAT12) |
112 | 0 | tsk_fprintf(hFile, "12\n"); |
113 | 0 | else if (fs->ftype == TSK_FS_TYPE_FAT16) |
114 | 0 | tsk_fprintf(hFile, "16\n"); |
115 | 0 | else if (fs->ftype == TSK_FS_TYPE_FAT32) |
116 | 0 | tsk_fprintf(hFile, "32\n"); |
117 | 0 | else |
118 | 0 | tsk_fprintf(hFile, "\n"); |
119 | |
|
120 | 0 | tsk_fprintf(hFile, "\nOEM Name: %c%c%c%c%c%c%c%c\n", sb->oemname[0], |
121 | 0 | sb->oemname[1], sb->oemname[2], sb->oemname[3], sb->oemname[4], |
122 | 0 | sb->oemname[5], sb->oemname[6], sb->oemname[7]); |
123 | | |
124 | |
|
125 | 0 | if (fatfs->fs_info.ftype != TSK_FS_TYPE_FAT32) { |
126 | 0 | tsk_fprintf(hFile, "Volume ID: 0x%" PRIx32 "\n", |
127 | 0 | tsk_getu32(fs->endian, sb->a.f16.vol_id)); |
128 | |
|
129 | 0 | tsk_fprintf(hFile, |
130 | 0 | "Volume Label (Boot Sector): %c%c%c%c%c%c%c%c%c%c%c\n", |
131 | 0 | sb->a.f16.vol_lab[0], sb->a.f16.vol_lab[1], |
132 | 0 | sb->a.f16.vol_lab[2], sb->a.f16.vol_lab[3], |
133 | 0 | sb->a.f16.vol_lab[4], sb->a.f16.vol_lab[5], |
134 | 0 | sb->a.f16.vol_lab[6], sb->a.f16.vol_lab[7], |
135 | 0 | sb->a.f16.vol_lab[8], sb->a.f16.vol_lab[9], |
136 | 0 | sb->a.f16.vol_lab[10]); |
137 | |
|
138 | 0 | if ((vol_label_dentry) && (vol_label_dentry->name[0])) { |
139 | 0 | tsk_fprintf(hFile, |
140 | 0 | "Volume Label (Root Directory): %c%c%c%c%c%c%c%c%c%c%c\n", |
141 | 0 | vol_label_dentry->name[0], vol_label_dentry->name[1], vol_label_dentry->name[2], vol_label_dentry->name[3], |
142 | 0 | vol_label_dentry->name[4], vol_label_dentry->name[5], vol_label_dentry->name[6], vol_label_dentry->name[7], |
143 | 0 | vol_label_dentry->ext[0], vol_label_dentry->ext[1], vol_label_dentry->ext[2]); |
144 | 0 | } |
145 | 0 | else { |
146 | 0 | tsk_fprintf(hFile, "Volume Label (Root Directory):\n"); |
147 | 0 | } |
148 | |
|
149 | 0 | tsk_fprintf(hFile, "File System Type Label: %c%c%c%c%c%c%c%c\n", |
150 | 0 | sb->a.f16.fs_type[0], sb->a.f16.fs_type[1], |
151 | 0 | sb->a.f16.fs_type[2], sb->a.f16.fs_type[3], |
152 | 0 | sb->a.f16.fs_type[4], sb->a.f16.fs_type[5], |
153 | 0 | sb->a.f16.fs_type[6], sb->a.f16.fs_type[7]); |
154 | 0 | } |
155 | 0 | else { |
156 | |
|
157 | 0 | char *fat_fsinfo_buf; |
158 | |
|
159 | 0 | if ((fat_fsinfo_buf = (char *) |
160 | 0 | tsk_malloc(sizeof(FATXXFS_FSINFO))) == NULL) { |
161 | 0 | free(data_buf); |
162 | 0 | return 1; |
163 | 0 | } |
164 | | |
165 | 0 | tsk_fprintf(hFile, "Volume ID: 0x%" PRIx32 "\n", |
166 | 0 | tsk_getu32(fs->endian, sb->a.f32.vol_id)); |
167 | |
|
168 | 0 | tsk_fprintf(hFile, |
169 | 0 | "Volume Label (Boot Sector): %c%c%c%c%c%c%c%c%c%c%c\n", |
170 | 0 | sb->a.f32.vol_lab[0], sb->a.f32.vol_lab[1], |
171 | 0 | sb->a.f32.vol_lab[2], sb->a.f32.vol_lab[3], |
172 | 0 | sb->a.f32.vol_lab[4], sb->a.f32.vol_lab[5], |
173 | 0 | sb->a.f32.vol_lab[6], sb->a.f32.vol_lab[7], |
174 | 0 | sb->a.f32.vol_lab[8], sb->a.f32.vol_lab[9], |
175 | 0 | sb->a.f32.vol_lab[10]); |
176 | |
|
177 | 0 | if ((vol_label_dentry) && (vol_label_dentry->name[0])) { |
178 | 0 | tsk_fprintf(hFile, |
179 | 0 | "Volume Label (Root Directory): %c%c%c%c%c%c%c%c%c%c%c\n", |
180 | 0 | vol_label_dentry->name[0], vol_label_dentry->name[1], vol_label_dentry->name[2], vol_label_dentry->name[3], |
181 | 0 | vol_label_dentry->name[4], vol_label_dentry->name[5], vol_label_dentry->name[6], vol_label_dentry->name[7], |
182 | 0 | vol_label_dentry->ext[0], vol_label_dentry->ext[1], vol_label_dentry->ext[2]); |
183 | 0 | } |
184 | 0 | else { |
185 | 0 | tsk_fprintf(hFile, "Volume Label (Root Directory):\n"); |
186 | 0 | } |
187 | |
|
188 | 0 | tsk_fprintf(hFile, "File System Type Label: %c%c%c%c%c%c%c%c\n", |
189 | 0 | sb->a.f32.fs_type[0], sb->a.f32.fs_type[1], |
190 | 0 | sb->a.f32.fs_type[2], sb->a.f32.fs_type[3], |
191 | 0 | sb->a.f32.fs_type[4], sb->a.f32.fs_type[5], |
192 | 0 | sb->a.f32.fs_type[6], sb->a.f32.fs_type[7]); |
193 | | |
194 | | |
195 | | /* Process the FS info */ |
196 | 0 | if (tsk_getu16(fs->endian, sb->a.f32.fsinfo)) { |
197 | 0 | FATXXFS_FSINFO *fat_info; |
198 | 0 | cnt = |
199 | 0 | tsk_fs_read(fs, |
200 | 0 | (TSK_DADDR_T) tsk_getu16(fs->endian, sb->a.f32.fsinfo) * fs->block_size, |
201 | 0 | fat_fsinfo_buf, sizeof(FATXXFS_FSINFO)); |
202 | |
|
203 | 0 | if (cnt != sizeof(FATXXFS_FSINFO)) { |
204 | 0 | if (cnt >= 0) { |
205 | 0 | tsk_error_reset(); |
206 | 0 | tsk_error_set_errno(TSK_ERR_FS_READ); |
207 | 0 | } |
208 | 0 | tsk_error_set_errstr2 |
209 | 0 | ("fatxxfs_fsstat: TSK_FS_TYPE_FAT32 FSINFO block: %" |
210 | 0 | PRIuDADDR, (TSK_DADDR_T) tsk_getu16(fs->endian, |
211 | 0 | sb->a.f32.fsinfo)); |
212 | 0 | free(data_buf); |
213 | 0 | free(fat_fsinfo_buf); |
214 | 0 | return 1; |
215 | 0 | } |
216 | | |
217 | | |
218 | 0 | fat_info = (FATXXFS_FSINFO *) fat_fsinfo_buf; |
219 | 0 | tsk_fprintf(hFile, |
220 | 0 | "Next Free Sector (FS Info): %" PRIuDADDR "\n", |
221 | 0 | FATFS_CLUST_2_SECT(fatfs, tsk_getu32(fs->endian, |
222 | 0 | fat_info->nextfree))); |
223 | |
|
224 | 0 | tsk_fprintf(hFile, |
225 | 0 | "Free Sector Count (FS Info): %" PRIu32 "\n", |
226 | 0 | (tsk_getu32(fs->endian, |
227 | 0 | fat_info->freecnt) * fatfs->csize)); |
228 | |
|
229 | 0 | free(fat_fsinfo_buf); |
230 | 0 | } |
231 | 0 | } |
232 | | |
233 | 0 | free(data_buf); |
234 | |
|
235 | 0 | tsk_fprintf(hFile, "\nSectors before file system: %" PRIu32 "\n", |
236 | 0 | tsk_getu32(fs->endian, sb->prevsect)); |
237 | |
|
238 | 0 | tsk_fprintf(hFile, "\nFile System Layout (in sectors)\n"); |
239 | |
|
240 | 0 | tsk_fprintf(hFile, "Total Range: %" PRIuDADDR " - %" PRIuDADDR "\n", |
241 | 0 | fs->first_block, fs->last_block); |
242 | |
|
243 | 0 | if (fs->last_block != fs->last_block_act) |
244 | 0 | tsk_fprintf(hFile, |
245 | 0 | "Total Range in Image: %" PRIuDADDR " - %" PRIuDADDR "\n", |
246 | 0 | fs->first_block, fs->last_block_act); |
247 | |
|
248 | 0 | tsk_fprintf(hFile, "* Reserved: 0 - %" PRIuDADDR "\n", |
249 | 0 | fatfs->firstfatsect - 1); |
250 | |
|
251 | 0 | tsk_fprintf(hFile, "** Boot Sector: 0\n"); |
252 | |
|
253 | 0 | if (fatfs->fs_info.ftype == TSK_FS_TYPE_FAT32) { |
254 | 0 | tsk_fprintf(hFile, "** FS Info Sector: %" PRIu16 "\n", |
255 | 0 | tsk_getu16(fs->endian, sb->a.f32.fsinfo)); |
256 | |
|
257 | 0 | tsk_fprintf(hFile, "** Backup Boot Sector: %" PRIu16 "\n", |
258 | 0 | tsk_getu16(fs->endian, sb->a.f32.bs_backup)); |
259 | 0 | } |
260 | |
|
261 | 0 | for (i = 0; i < fatfs->numfat; i++) { |
262 | 0 | TSK_DADDR_T base = fatfs->firstfatsect + i * (fatfs->sectperfat); |
263 | |
|
264 | 0 | tsk_fprintf(hFile, "* FAT %d: %" PRIuDADDR " - %" PRIuDADDR "\n", |
265 | 0 | i, base, (base + fatfs->sectperfat - 1)); |
266 | 0 | } |
267 | |
|
268 | 0 | tsk_fprintf(hFile, "* Data Area: %" PRIuDADDR " - %" PRIuDADDR "\n", |
269 | 0 | fatfs->firstdatasect, fs->last_block); |
270 | |
|
271 | 0 | if (fatfs->fs_info.ftype != TSK_FS_TYPE_FAT32) { |
272 | 0 | TSK_DADDR_T x = fatfs->csize * fatfs->clustcnt; |
273 | |
|
274 | 0 | tsk_fprintf(hFile, |
275 | 0 | "** Root Directory: %" PRIuDADDR " - %" PRIuDADDR "\n", |
276 | 0 | fatfs->firstdatasect, fatfs->firstclustsect - 1); |
277 | |
|
278 | 0 | tsk_fprintf(hFile, |
279 | 0 | "** Cluster Area: %" PRIuDADDR " - %" PRIuDADDR "\n", |
280 | 0 | fatfs->firstclustsect, (fatfs->firstclustsect + x - 1)); |
281 | |
|
282 | 0 | if ((fatfs->firstclustsect + x - 1) != fs->last_block) { |
283 | 0 | tsk_fprintf(hFile, |
284 | 0 | "** Non-clustered: %" PRIuDADDR " - %" PRIuDADDR "\n", |
285 | 0 | (fatfs->firstclustsect + x), fs->last_block); |
286 | 0 | } |
287 | 0 | } |
288 | 0 | else { |
289 | 0 | TSK_LIST *list_seen = NULL; |
290 | 0 | TSK_DADDR_T x = fatfs->csize * (fatfs->lastclust - 1); |
291 | 0 | TSK_DADDR_T clust, clust_p; |
292 | |
|
293 | 0 | tsk_fprintf(hFile, |
294 | 0 | "** Cluster Area: %" PRIuDADDR " - %" PRIuDADDR "\n", |
295 | 0 | fatfs->firstclustsect, (fatfs->firstclustsect + x - 1)); |
296 | | |
297 | |
|
298 | 0 | clust_p = fatfs->rootsect; |
299 | 0 | clust = FATFS_SECT_2_CLUST(fatfs, fatfs->rootsect); |
300 | 0 | while ((clust) && (0 == FATFS_ISEOF(clust, FATFS_32_MASK))) { |
301 | 0 | TSK_DADDR_T nxt; |
302 | 0 | clust_p = clust; |
303 | | |
304 | | /* Make sure we do not get into an infinite loop */ |
305 | 0 | if (tsk_list_find(list_seen, clust)) { |
306 | 0 | if (tsk_verbose) |
307 | 0 | tsk_fprintf(stderr, |
308 | 0 | "Loop found while determining root directory size\n"); |
309 | 0 | break; |
310 | 0 | } |
311 | 0 | if (tsk_list_add(&list_seen, clust)) { |
312 | 0 | tsk_list_free(list_seen); |
313 | 0 | list_seen = NULL; |
314 | 0 | return 1; |
315 | 0 | } |
316 | | |
317 | 0 | if (fatfs_getFAT(fatfs, clust, &nxt)) |
318 | 0 | break; |
319 | 0 | clust = nxt; |
320 | 0 | } |
321 | 0 | tsk_list_free(list_seen); |
322 | 0 | list_seen = NULL; |
323 | |
|
324 | 0 | tsk_fprintf(hFile, |
325 | 0 | "*** Root Directory: %" PRIuDADDR " - %" PRIuDADDR "\n", |
326 | 0 | fatfs->rootsect, (FATFS_CLUST_2_SECT(fatfs, clust_p + 1) - 1)); |
327 | |
|
328 | 0 | if ((fatfs->firstclustsect + x - 1) != fs->last_block) { |
329 | 0 | tsk_fprintf(hFile, |
330 | 0 | "** Non-clustered: %" PRIuDADDR " - %" PRIuDADDR "\n", |
331 | 0 | (fatfs->firstclustsect + x), fs->last_block); |
332 | 0 | } |
333 | 0 | } |
334 | | |
335 | | |
336 | 0 | tsk_fprintf(hFile, "\nMETADATA INFORMATION\n"); |
337 | 0 | tsk_fprintf(hFile, "--------------------------------------------\n"); |
338 | |
|
339 | 0 | tsk_fprintf(hFile, "Range: %" PRIuINUM " - %" PRIuINUM "\n", |
340 | 0 | fs->first_inum, fs->last_inum); |
341 | 0 | tsk_fprintf(hFile, "Root Directory: %" PRIuINUM "\n", fs->root_inum); |
342 | | |
343 | |
|
344 | 0 | tsk_fprintf(hFile, "\nCONTENT INFORMATION\n"); |
345 | 0 | tsk_fprintf(hFile, "--------------------------------------------\n"); |
346 | 0 | tsk_fprintf(hFile, "Sector Size: %" PRIu16 "\n", fatfs->ssize); |
347 | 0 | tsk_fprintf(hFile, "Cluster Size: %" PRIu32 "\n", |
348 | 0 | (uint32_t) fatfs->csize << fatfs->ssize_sh); |
349 | |
|
350 | 0 | tsk_fprintf(hFile, "Total Cluster Range: 2 - %" PRIuDADDR "\n", |
351 | 0 | fatfs->lastclust); |
352 | | |
353 | | |
354 | | /* cycle via cluster and look at each cluster in the FAT |
355 | | * for clusters marked as bad */ |
356 | 0 | cnt = 0; |
357 | 0 | for (i = 2; i <= fatfs->lastclust; i++) { |
358 | 0 | TSK_DADDR_T entry; |
359 | 0 | TSK_DADDR_T sect; |
360 | 0 | unsigned int a; |
361 | | |
362 | | /* Get the FAT table entry */ |
363 | 0 | if (fatfs_getFAT(fatfs, i, &entry)) |
364 | 0 | break; |
365 | | |
366 | 0 | if (FATFS_ISBAD(entry, fatfs->mask) == 0) { |
367 | 0 | continue; |
368 | 0 | } |
369 | | |
370 | 0 | if (cnt == 0) |
371 | 0 | tsk_fprintf(hFile, "Bad Sectors: "); |
372 | |
|
373 | 0 | sect = FATFS_CLUST_2_SECT(fatfs, i); |
374 | 0 | for (a = 0; a < fatfs->csize; a++) { |
375 | 0 | tsk_fprintf(hFile, "%" PRIuDADDR " ", sect + a); |
376 | 0 | if ((++cnt % 8) == 0) |
377 | 0 | tsk_fprintf(hFile, "\n"); |
378 | 0 | } |
379 | 0 | } |
380 | 0 | if ((cnt > 0) && ((cnt % 8) != 0)) |
381 | 0 | tsk_fprintf(hFile, "\n"); |
382 | | |
383 | | /* Display the FAT Table */ |
384 | 0 | tsk_fprintf(hFile, "\nFAT CONTENTS (in sectors)\n"); |
385 | 0 | tsk_fprintf(hFile, "--------------------------------------------\n"); |
386 | | |
387 | | /* 'sstart' marks the first sector of the current run to print */ |
388 | 0 | sstart = fatfs->firstclustsect; |
389 | | |
390 | | /* cycle via cluster and look at each cluster in the FAT to make runs */ |
391 | 0 | for (i = 2; i <= fatfs->lastclust; i++) { |
392 | | |
393 | | /* 'send' marks the end sector of the current run, which will extend |
394 | | * when the current cluster continues to the next |
395 | | */ |
396 | 0 | send = FATFS_CLUST_2_SECT(fatfs, i + 1) - 1; |
397 | | |
398 | | /* get the next cluster */ |
399 | 0 | if (fatfs_getFAT(fatfs, i, &next)) |
400 | 0 | break; |
401 | | |
402 | 0 | snext = FATFS_CLUST_2_SECT(fatfs, next); |
403 | | |
404 | | /* we are also using the next sector (clust) */ |
405 | 0 | if ((next & fatfs->mask) == (i + 1)) { |
406 | 0 | continue; |
407 | 0 | } |
408 | | |
409 | | /* The next clust is either further away or the clust is available, |
410 | | * print it if is further away |
411 | | */ |
412 | 0 | else if ((next & fatfs->mask)) { |
413 | 0 | if (FATFS_ISEOF(next, fatfs->mask)) |
414 | 0 | tsk_fprintf(hFile, |
415 | 0 | "%" PRIuDADDR "-%" PRIuDADDR " (%" PRIuDADDR |
416 | 0 | ") -> EOF\n", sstart, send, send - sstart + 1); |
417 | 0 | else if (FATFS_ISBAD(next, fatfs->mask)) |
418 | 0 | tsk_fprintf(hFile, |
419 | 0 | "%" PRIuDADDR "-%" PRIuDADDR " (%" PRIuDADDR |
420 | 0 | ") -> BAD\n", sstart, send, send - sstart + 1); |
421 | 0 | else |
422 | 0 | tsk_fprintf(hFile, |
423 | 0 | "%" PRIuDADDR "-%" PRIuDADDR " (%" PRIuDADDR |
424 | 0 | ") -> %" PRIuDADDR "\n", sstart, send, |
425 | 0 | send - sstart + 1, snext); |
426 | 0 | } |
427 | | |
428 | | /* reset the starting counter */ |
429 | 0 | sstart = send + 1; |
430 | 0 | } |
431 | |
|
432 | 0 | return 0; |
433 | 0 | } |
434 | | |
435 | | uint8_t |
436 | | fatxxfs_open(FATFS_INFO *fatfs) |
437 | 6 | { |
438 | 6 | const char *func_name = "fatxxfs_open"; |
439 | 6 | TSK_FS_INFO *fs = &(fatfs->fs_info); |
440 | 6 | FATXXFS_SB *fatsb = (FATXXFS_SB*)(&fatfs->boot_sector_buffer); |
441 | 6 | int i = 0; |
442 | 6 | TSK_DADDR_T sectors = 0; |
443 | 6 | TSK_FS_DIR * test_dir1; // Directories used to try opening the root directory |
444 | | |
445 | | // clean up any error messages that are lying around |
446 | 6 | tsk_error_reset(); |
447 | | |
448 | | /* Calculate block sizes and layout info */ |
449 | | // sector size |
450 | 6 | fatfs->ssize = tsk_getu16(fs->endian, fatsb->ssize); |
451 | 6 | if (fatfs->ssize == 512) { |
452 | 6 | fatfs->ssize_sh = 9; |
453 | 6 | } |
454 | 0 | else if (fatfs->ssize == 1024) { |
455 | 0 | fatfs->ssize_sh = 10; |
456 | 0 | } |
457 | 0 | else if (fatfs->ssize == 2048) { |
458 | 0 | fatfs->ssize_sh = 11; |
459 | 0 | } |
460 | 0 | else if (fatfs->ssize == 4096) { |
461 | 0 | fatfs->ssize_sh = 12; |
462 | 0 | } |
463 | 0 | else { |
464 | 0 | tsk_error_reset(); |
465 | 0 | tsk_error_set_errno(TSK_ERR_FS_MAGIC); |
466 | 0 | tsk_error_set_errstr |
467 | 0 | ("Error: sector size (%d) is not a multiple of device size (%d)\nDo you have a disk image instead of a partition image?", |
468 | 0 | fatfs->ssize, fs->dev_bsize); |
469 | 0 | if (tsk_verbose) |
470 | 0 | fprintf(stderr, "%s: Invalid sector size (%d)\n", |
471 | 0 | func_name, fatfs->ssize); |
472 | 0 | return 1; |
473 | 0 | } |
474 | | |
475 | | // cluster size |
476 | 6 | fatfs->csize = fatsb->csize; |
477 | 6 | if ((fatfs->csize != 0x01) && |
478 | 6 | (fatfs->csize != 0x02) && |
479 | 6 | (fatfs->csize != 0x04) && |
480 | 6 | (fatfs->csize != 0x08) && |
481 | 6 | (fatfs->csize != 0x10) && |
482 | 6 | (fatfs->csize != 0x20) && |
483 | 6 | (fatfs->csize != 0x40) && (fatfs->csize != 0x80)) { |
484 | 0 | if (tsk_verbose) |
485 | 0 | fprintf(stderr, "%s: Invalid cluster size (%d)\n", |
486 | 0 | func_name, fatfs->csize); |
487 | 0 | tsk_error_reset(); |
488 | 0 | tsk_error_set_errno(TSK_ERR_FS_MAGIC); |
489 | 0 | tsk_error_set_errstr("Not a FATXX file system (cluster size)"); |
490 | 0 | return 1; |
491 | 0 | } |
492 | | |
493 | | // number of FAT tables |
494 | 6 | fatfs->numfat = fatsb->numfat; |
495 | 6 | if ((fatfs->numfat == 0) || (fatfs->numfat > 8)) { |
496 | 0 | if (tsk_verbose) |
497 | 0 | fprintf(stderr, "%s: Invalid number of FATS (%d)\n", |
498 | 0 | func_name, fatfs->numfat); |
499 | 0 | tsk_error_reset(); |
500 | 0 | tsk_error_set_errno(TSK_ERR_FS_MAGIC); |
501 | 0 | tsk_error_set_errstr("Not a FATXX file system (number of FATs)"); |
502 | 0 | return 1; |
503 | 0 | } |
504 | | |
505 | | /* We can't do a sanity check on this b.c. TSK_FS_TYPE_FAT32 has a value of 0 */ |
506 | | /* num of root entries */ |
507 | 6 | fatfs->numroot = tsk_getu16(fs->endian, fatsb->numroot); |
508 | | |
509 | | /* if sectors16 is 0, then the number of sectors is stored in sectors32 */ |
510 | 6 | if (0 == (sectors = tsk_getu16(fs->endian, fatsb->sectors16))) |
511 | 1 | sectors = tsk_getu32(fs->endian, fatsb->sectors32); |
512 | | |
513 | | /* if secperfat16 is 0, then read sectperfat32 */ |
514 | 6 | if (0 == (fatfs->sectperfat = |
515 | 6 | tsk_getu16(fs->endian, fatsb->sectperfat16))) |
516 | 1 | fatfs->sectperfat = |
517 | 1 | tsk_getu32(fs->endian, fatsb->a.f32.sectperfat32); |
518 | | |
519 | 6 | if (fatfs->sectperfat == 0) { |
520 | 0 | if (tsk_verbose) |
521 | 0 | fprintf(stderr, |
522 | 0 | "%s: Invalid number of sectors per FAT (%d)\n", |
523 | 0 | func_name, fatfs->sectperfat); |
524 | 0 | tsk_error_reset(); |
525 | 0 | tsk_error_set_errno(TSK_ERR_FS_MAGIC); |
526 | 0 | tsk_error_set_errstr |
527 | 0 | ("Not a FATXX file system (invalid sectors per FAT)"); |
528 | 0 | return 1; |
529 | 0 | } |
530 | | |
531 | 6 | fatfs->firstfatsect = tsk_getu16(fs->endian, fatsb->reserved); |
532 | 6 | if ((fatfs->firstfatsect == 0) || (fatfs->firstfatsect > sectors)) { |
533 | 0 | tsk_error_reset(); |
534 | 0 | tsk_error_set_errno(TSK_ERR_FS_WALK_RNG); |
535 | 0 | tsk_error_set_errstr |
536 | 0 | ("Not a FATXX file system (invalid first FAT sector %" |
537 | 0 | PRIuDADDR ")", fatfs->firstfatsect); |
538 | 0 | if (tsk_verbose) |
539 | 0 | fprintf(stderr, |
540 | 0 | "%s: Invalid first FAT (%" PRIuDADDR ")\n", |
541 | 0 | func_name, fatfs->firstfatsect); |
542 | 0 | return 1; |
543 | 0 | } |
544 | | |
545 | | /* Calculate the block info |
546 | | * |
547 | | * The sector of the beginning of the data area - which is |
548 | | * after all of the FATs |
549 | | * |
550 | | * For TSK_FS_TYPE_FAT12 and TSK_FS_TYPE_FAT16, the data area starts with the root |
551 | | * directory entries and then the first cluster. For TSK_FS_TYPE_FAT32, |
552 | | * the data area starts with clusters and the root directory |
553 | | * is somewhere in the data area |
554 | | */ |
555 | 6 | fatfs->firstdatasect = fatfs->firstfatsect + |
556 | 6 | fatfs->sectperfat * fatfs->numfat; |
557 | | |
558 | | /* The sector where the first cluster is located. It will be used |
559 | | * to translate cluster addresses to sector addresses |
560 | | * |
561 | | * For TSK_FS_TYPE_FAT32, the first cluster is the start of the data area and |
562 | | * it is after the root directory for TSK_FS_TYPE_FAT12 and TSK_FS_TYPE_FAT16. At this |
563 | | * point in the program, numroot is set to 0 for TSK_FS_TYPE_FAT32 |
564 | | */ |
565 | 6 | fatfs->firstclustsect = fatfs->firstdatasect + |
566 | 6 | ((fatfs->numroot * 32 + fatfs->ssize - 1) / fatfs->ssize); |
567 | | |
568 | | /* total number of clusters */ |
569 | 6 | fatfs->clustcnt = (sectors - fatfs->firstclustsect) / fatfs->csize; |
570 | | |
571 | | /* the first cluster is #2, so the final cluster is: */ |
572 | 6 | fatfs->lastclust = 1 + fatfs->clustcnt; |
573 | | |
574 | | |
575 | | /* identify the FAT type by the total number of data clusters |
576 | | * this calculation is from the MS FAT Overview Doc |
577 | | * |
578 | | * A FAT file system made by another OS could use different values |
579 | | */ |
580 | 6 | if (fatfs->fs_info.ftype == TSK_FS_TYPE_FAT_DETECT) { |
581 | | |
582 | 6 | if (fatfs->clustcnt < 4085) { |
583 | 5 | fatfs->fs_info.ftype = TSK_FS_TYPE_FAT12; |
584 | 5 | } |
585 | 1 | else if (fatfs->clustcnt < 65525) { |
586 | 0 | fatfs->fs_info.ftype = TSK_FS_TYPE_FAT16; |
587 | 0 | } |
588 | 1 | else { |
589 | 1 | fatfs->fs_info.ftype = TSK_FS_TYPE_FAT32; |
590 | 1 | } |
591 | | |
592 | 6 | fatfs->fs_info.ftype = fatfs->fs_info.ftype; |
593 | 6 | } |
594 | | |
595 | | /* Some sanity checks */ |
596 | 0 | else { |
597 | 0 | if ((fatfs->fs_info.ftype == TSK_FS_TYPE_FAT12) |
598 | 0 | && (fatfs->clustcnt >= 4085)) { |
599 | 0 | tsk_error_reset(); |
600 | 0 | tsk_error_set_errno(TSK_ERR_FS_MAGIC); |
601 | 0 | tsk_error_set_errstr |
602 | 0 | ("Too many sectors for TSK_FS_TYPE_FAT12: try auto-detect mode"); |
603 | 0 | if (tsk_verbose) |
604 | 0 | fprintf(stderr, |
605 | 0 | "%s: Too many sectors for FAT12\n", func_name); |
606 | 0 | return 1; |
607 | 0 | } |
608 | 0 | } |
609 | | |
610 | 6 | if ((fatfs->fs_info.ftype == TSK_FS_TYPE_FAT32) && (fatfs->numroot != 0)) { |
611 | 0 | tsk_error_reset(); |
612 | 0 | tsk_error_set_errno(TSK_ERR_FS_MAGIC); |
613 | 0 | tsk_error_set_errstr |
614 | 0 | ("Invalid TSK_FS_TYPE_FAT32 image (numroot != 0)"); |
615 | 0 | if (tsk_verbose) |
616 | 0 | fprintf(stderr, "%s: numroom != 0 for FAT32\n", func_name); |
617 | 0 | return 1; |
618 | 0 | } |
619 | | |
620 | 6 | if ((fatfs->fs_info.ftype != TSK_FS_TYPE_FAT32) && (fatfs->numroot == 0)) { |
621 | 0 | tsk_error_reset(); |
622 | 0 | tsk_error_set_errno(TSK_ERR_FS_MAGIC); |
623 | 0 | tsk_error_set_errstr |
624 | 0 | ("Invalid FAT image (numroot == 0, and not TSK_FS_TYPE_FAT32)"); |
625 | 0 | if (tsk_verbose) |
626 | 0 | fprintf(stderr, "%s: numroom == 0 and not FAT32\n", func_name); |
627 | 0 | return 1; |
628 | 0 | } |
629 | | |
630 | | /* additional sanity checks if we think we are using the backup boot sector. |
631 | | * The scenario to prevent here is if fat_open is called 6 sectors before the real start |
632 | | * of the file system, then we want to detect that it was not a backup that we saw. |
633 | | */ |
634 | 6 | if (fatfs->using_backup_boot_sector) { |
635 | | // only FAT32 has backup boot sectors.. |
636 | 0 | if (fatfs->fs_info.ftype != TSK_FS_TYPE_FAT32) { |
637 | 0 | tsk_error_reset(); |
638 | 0 | tsk_error_set_errno(TSK_ERR_FS_MAGIC); |
639 | 0 | tsk_error_set_errstr |
640 | 0 | ("Invalid FAT image (Used what we thought was a backup boot sector, but it is not TSK_FS_TYPE_FAT32)"); |
641 | 0 | if (tsk_verbose) |
642 | 0 | fprintf(stderr, |
643 | 0 | "%s: Had to use backup boot sector, but this isn't FAT32\n", func_name); |
644 | 0 | return 1; |
645 | 0 | } |
646 | 0 | if (fatfs->numroot > 1) { |
647 | 0 | uint8_t buf1[512]; |
648 | 0 | uint8_t buf2[512]; |
649 | 0 | int i2; |
650 | 0 | int numDiffs; |
651 | 0 | ssize_t cnt = 0; |
652 | |
|
653 | 0 | cnt = |
654 | 0 | tsk_fs_read(fs, fatfs->firstfatsect * fatfs->ssize, |
655 | 0 | (char *) buf1, 512); |
656 | 0 | if (cnt != 512) { |
657 | 0 | if (cnt >= 0) { |
658 | 0 | tsk_error_reset(); |
659 | 0 | tsk_error_set_errno(TSK_ERR_FS_READ); |
660 | 0 | } |
661 | 0 | tsk_error_set_errstr2("%s: FAT1", func_name); |
662 | 0 | fs->tag = 0; |
663 | 0 | return 1; |
664 | 0 | } |
665 | | |
666 | 0 | cnt = |
667 | 0 | tsk_fs_read(fs, |
668 | 0 | (fatfs->firstfatsect + fatfs->sectperfat) * fatfs->ssize, |
669 | 0 | (char *) buf2, 512); |
670 | 0 | if (cnt != 512) { |
671 | 0 | if (cnt >= 0) { |
672 | 0 | tsk_error_reset(); |
673 | 0 | tsk_error_set_errno(TSK_ERR_FS_READ); |
674 | 0 | } |
675 | 0 | tsk_error_set_errstr2("%s: FAT2", func_name); |
676 | 0 | fs->tag = 0; |
677 | 0 | return 1; |
678 | 0 | } |
679 | | |
680 | 0 | numDiffs = 0; |
681 | 0 | for (i2 = 0; i2 < 512; i2++) { |
682 | 0 | if (buf1[i2] != buf2[i2]) { |
683 | 0 | numDiffs++; |
684 | 0 | } |
685 | 0 | } |
686 | 0 | if (numDiffs > 25) { |
687 | 0 | tsk_error_reset(); |
688 | 0 | tsk_error_set_errno(TSK_ERR_FS_MAGIC); |
689 | 0 | tsk_error_set_errstr |
690 | 0 | ("Invalid FAT image (Too many differences between FATS from guessing (%d diffs))", |
691 | 0 | numDiffs); |
692 | 0 | if (tsk_verbose) |
693 | 0 | fprintf(stderr, |
694 | 0 | "%s: Too many differences in FAT from guessing (%d diffs)\n", |
695 | 0 | func_name, numDiffs); |
696 | 0 | return 1; |
697 | 0 | } |
698 | 0 | } |
699 | 0 | } |
700 | | |
701 | | /* Set the mask to use on the cluster values */ |
702 | 6 | if (fatfs->fs_info.ftype == TSK_FS_TYPE_FAT12) { |
703 | 5 | fatfs->mask = FATFS_12_MASK; |
704 | 5 | } |
705 | 1 | else if (fatfs->fs_info.ftype == TSK_FS_TYPE_FAT16) { |
706 | 0 | fatfs->mask = FATFS_16_MASK; |
707 | 0 | } |
708 | 1 | else if (fatfs->fs_info.ftype == TSK_FS_TYPE_FAT32) { |
709 | 1 | fatfs->mask = FATFS_32_MASK; |
710 | 1 | } |
711 | 0 | else { |
712 | 0 | tsk_error_reset(); |
713 | 0 | tsk_error_set_errno(TSK_ERR_FS_ARG); |
714 | 0 | tsk_error_set_errstr("Unknown FAT type in %s: %d\n", |
715 | 0 | func_name, fatfs->fs_info.ftype); |
716 | 0 | return 1; |
717 | 0 | } |
718 | 6 | fs->duname = "Sector"; |
719 | | |
720 | | /* the root directories are always after the FAT for TSK_FS_TYPE_FAT12 and TSK_FS_TYPE_FAT16, |
721 | | * but are dynamically located for TSK_FS_TYPE_FAT32 |
722 | | */ |
723 | 6 | if (fatfs->fs_info.ftype == TSK_FS_TYPE_FAT32) |
724 | 1 | fatfs->rootsect = FATFS_CLUST_2_SECT(fatfs, |
725 | 6 | tsk_getu32(fs->endian, fatsb->a.f32.rootclust)); |
726 | 5 | else |
727 | 5 | fatfs->rootsect = fatfs->firstdatasect; |
728 | | |
729 | 30 | for (i = 0; i < FATFS_FAT_CACHE_N; i++) { |
730 | 24 | fatfs->fatc_addr[i] = 0; |
731 | 24 | fatfs->fatc_ttl[i] = 0; |
732 | 24 | } |
733 | | |
734 | | /* |
735 | | * block calculations : although there are no blocks in fat, we will |
736 | | * use these fields for sector calculations |
737 | | */ |
738 | 6 | fs->first_block = 0; |
739 | 6 | fs->block_count = sectors; |
740 | 6 | fs->last_block = fs->last_block_act = fs->block_count - 1; |
741 | 6 | fs->block_size = fatfs->ssize; |
742 | | |
743 | | // determine the last block we have in this image |
744 | 6 | if ((TSK_DADDR_T) ((fatfs->fs_info.img_info->size - fatfs->fs_info.offset) / fs->block_size) < |
745 | 6 | fs->block_count) |
746 | 6 | fs->last_block_act = |
747 | 6 | (fatfs->fs_info.img_info->size - fatfs->fs_info.offset) / fs->block_size - 1; |
748 | | |
749 | | /* |
750 | | * inode calculations |
751 | | */ |
752 | | |
753 | | /* maximum number of dentries in a sector & cluster */ |
754 | 6 | fatfs->dentry_cnt_se = fatfs->ssize / sizeof(FATXXFS_DENTRY); |
755 | 6 | fatfs->dentry_cnt_cl = fatfs->dentry_cnt_se * fatfs->csize; |
756 | | |
757 | 6 | fs->root_inum = FATFS_ROOTINO; |
758 | 6 | fs->first_inum = FATFS_FIRSTINO; |
759 | | |
760 | | /* Calculate inode addresses for the virtual files (MBR, one or two FATS) |
761 | | * and the virtual orphan files directory. */ |
762 | 6 | fs->last_inum = (FATFS_SECT_2_INODE(fatfs, fs->last_block_act + 1) - 1) + FATFS_NUM_VIRT_FILES(fatfs); |
763 | 6 | fatfs->mbr_virt_inum = fs->last_inum - FATFS_NUM_VIRT_FILES(fatfs) + 1; |
764 | 6 | fatfs->fat1_virt_inum = fatfs->mbr_virt_inum + 1; |
765 | 6 | if (fatfs->numfat == 2) { |
766 | 6 | fatfs->fat2_virt_inum = fatfs->fat1_virt_inum + 1; |
767 | 6 | } |
768 | 0 | else { |
769 | 0 | fatfs->fat2_virt_inum = fatfs->fat1_virt_inum; |
770 | 0 | } |
771 | | |
772 | | /* Calculate the total number of inodes. */ |
773 | 6 | fs->inum_count = fs->last_inum - fs->first_inum + 1; |
774 | | |
775 | | /* Volume ID */ |
776 | 30 | for (fs->fs_id_used = 0; fs->fs_id_used < 4; fs->fs_id_used++) { |
777 | 24 | if (fatfs->fs_info.ftype == TSK_FS_TYPE_FAT32) |
778 | 4 | fs->fs_id[fs->fs_id_used] = |
779 | 4 | fatsb->a.f32.vol_id[fs->fs_id_used]; |
780 | 20 | else |
781 | 20 | fs->fs_id[fs->fs_id_used] = |
782 | 20 | fatsb->a.f16.vol_id[fs->fs_id_used]; |
783 | 24 | } |
784 | | |
785 | | /* |
786 | | * Set the function pointers |
787 | | */ |
788 | | |
789 | 6 | fs->block_walk = fatfs_block_walk; |
790 | 6 | fs->block_getflags = fatfs_block_getflags; |
791 | | |
792 | 6 | fs->inode_walk = fatfs_inode_walk; |
793 | 6 | fs->istat = fatfs_istat; |
794 | 6 | fs->file_add_meta = fatfs_inode_lookup; |
795 | | |
796 | 6 | fs->get_default_attr_type = fatfs_get_default_attr_type; |
797 | 6 | fs->load_attrs = fatfs_make_data_runs; |
798 | | |
799 | 6 | fs->dir_open_meta = fatfs_dir_open_meta; |
800 | 6 | fs->name_cmp = fatfs_name_cmp; |
801 | | |
802 | 6 | fs->fsstat = fatxxfs_fsstat; |
803 | 6 | fs->fscheck = fatfs_fscheck; |
804 | | |
805 | 6 | fs->close = fatfs_close; |
806 | | |
807 | 6 | fs->jblk_walk = fatfs_jblk_walk; |
808 | 6 | fs->jentry_walk = fatfs_jentry_walk; |
809 | 6 | fs->jopen = fatfs_jopen; |
810 | | |
811 | 6 | fatfs->is_cluster_alloc = fatxxfs_is_cluster_alloc; |
812 | 6 | fatfs->is_dentry = fatxxfs_is_dentry; |
813 | 6 | fatfs->dinode_copy = fatxxfs_dinode_copy; |
814 | 6 | fatfs->inode_lookup = fatxxfs_inode_lookup; |
815 | 6 | fatfs->inode_walk_should_skip_dentry = fatxxfs_inode_walk_should_skip_dentry; |
816 | 6 | fatfs->istat_attr_flags = fatxxfs_istat_attr_flags; |
817 | 6 | fatfs->dent_parse_buf = fatxxfs_dent_parse_buf; |
818 | | |
819 | | // initialize the caches |
820 | 6 | tsk_init_lock(&fatfs->cache_lock); |
821 | 6 | tsk_init_lock(&fatfs->dir_lock); |
822 | 6 | fatfs->inum2par = NULL; |
823 | | |
824 | | // Test to see if this is the odd Android case where the FAT entries have no short name |
825 | | // |
826 | | // If there are no entries found with the normal short name |
827 | | // and we find more entries by removing the short name test for allocated directories, then assume |
828 | | // this is the case where we have no short names |
829 | 6 | fatfs->subtype = TSK_FATFS_SUBTYPE_SPEC; |
830 | 6 | test_dir1 = tsk_fs_dir_open_meta(fs, fs->root_inum); |
831 | | |
832 | 6 | if (test_dir1 != NULL && test_dir1->names_used <= 4){ // At most four automatic directories ($MBR, $FAT1, $FAT1, $OrphanFiles) |
833 | 5 | TSK_FS_DIR * test_dir2; // to see if it's the Android FAT version |
834 | | |
835 | 5 | fatfs->subtype = TSK_FATFS_SUBTYPE_ANDROID_1; |
836 | 5 | test_dir2 = tsk_fs_dir_open_meta(fs, fs->root_inum); |
837 | | |
838 | 5 | if (test_dir2 != NULL && test_dir2->names_used > test_dir1->names_used){ |
839 | 1 | fatfs->subtype = TSK_FATFS_SUBTYPE_ANDROID_1; |
840 | 1 | } |
841 | 4 | else{ |
842 | 4 | fatfs->subtype = TSK_FATFS_SUBTYPE_SPEC; |
843 | 4 | } |
844 | 5 | tsk_fs_dir_close(test_dir2); |
845 | 5 | } |
846 | 6 | tsk_fs_dir_close(test_dir1); |
847 | | |
848 | 6 | return 0; |
849 | 6 | } |
850 | | |
851 | | /* Return 1 if allocated, 0 if unallocated, and -1 if error */ |
852 | | int8_t |
853 | | fatxxfs_is_cluster_alloc(FATFS_INFO *fatfs, TSK_DADDR_T clust) |
854 | 3.49k | { |
855 | 3.49k | TSK_DADDR_T content = 0; |
856 | | |
857 | 3.49k | if (fatfs_getFAT(fatfs, clust, &content)) |
858 | 0 | return -1; |
859 | 3.49k | else if (content == FATFS_UNALLOC) |
860 | 3.46k | return 0; |
861 | 33 | else |
862 | 33 | return 1; |
863 | 3.49k | } |