3
Copyright (C) 1998-2000, 2007-2010 Free Software Foundation, Inc.
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 3 of the License, or
8
(at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#include <parted/endian.h>
26
fat_table_new (FatType fat_type, FatCluster size)
29
int entry_size = fat_table_entry_size (fat_type);
31
ft = (FatTable*) ped_malloc (sizeof (FatTable));
34
ft->cluster_count = ft->free_cluster_count = size - 2;
36
/* ensure there's some free room on the end, to finish off the sector */
37
ft->size = ped_div_round_up (size * entry_size, 512) * 512 / entry_size;
38
ft->fat_type = fat_type;
39
ft->raw_size = ft->size * entry_size;
41
ft->table = ped_malloc (ft->raw_size);
52
fat_table_destroy (FatTable* ft)
59
fat_table_duplicate (const FatTable* ft)
63
dup_ft = fat_table_new (ft->fat_type, ft->size);
64
if (!dup_ft) return NULL;
66
dup_ft->cluster_count = ft->cluster_count;
67
dup_ft->free_cluster_count = ft->free_cluster_count;
68
dup_ft->bad_cluster_count = ft->bad_cluster_count;
69
dup_ft->last_alloc = ft->last_alloc;
71
memcpy (dup_ft->table, ft->table, ft->raw_size);
77
fat_table_clear (FatTable* ft)
79
memset (ft->table, 0, ft->raw_size);
81
fat_table_set (ft, 0, 0x0ffffff8);
82
fat_table_set (ft, 1, 0x0fffffff);
84
ft->free_cluster_count = ft->cluster_count;
85
ft->bad_cluster_count = 0;
90
fat_table_set_cluster_count (FatTable* ft, FatCluster new_cluster_count)
92
PED_ASSERT (new_cluster_count + 2 <= ft->size, return 0);
94
ft->cluster_count = new_cluster_count;
95
return fat_table_count_stats (ft);
99
fat_table_count_stats (FatTable* ft)
103
PED_ASSERT (ft->cluster_count + 2 <= ft->size, return 0);
105
ft->free_cluster_count = 0;
106
ft->bad_cluster_count = 0;
108
for (i=2; i < ft->cluster_count + 2; i++) {
109
if (fat_table_is_available (ft, i))
110
ft->free_cluster_count++;
111
if (fat_table_is_bad (ft, i))
112
ft->bad_cluster_count++;
118
fat_table_read (FatTable* ft, const PedFileSystem* fs, int table_num)
120
FatSpecific* fs_info = FAT_SPECIFIC (fs);
122
PED_ASSERT (ft->raw_size >= fs_info->fat_sectors * 512, return 0);
124
memset (ft->table, 0, ft->raw_size);
126
if (!ped_geometry_read (fs->geom, (void *) ft->table,
128
+ table_num * fs_info->fat_sectors,
129
fs_info->fat_sectors))
132
if ( *((unsigned char*) ft->table) != fs_info->boot_sector->media) {
133
if (ped_exception_throw (
135
PED_EXCEPTION_IGNORE_CANCEL,
136
_("FAT %d media %x doesn't match the boot sector's "
137
"media %x. You should probably run scandisk."),
139
(int) *((unsigned char*) ft->table),
140
(int) fs_info->boot_sector->media)
141
!= PED_EXCEPTION_IGNORE)
145
ft->cluster_count = fs_info->cluster_count;
147
fat_table_count_stats (ft);
153
fat_table_write (const FatTable* ft, PedFileSystem* fs, int table_num)
155
FatSpecific* fs_info = FAT_SPECIFIC (fs);
157
PED_ASSERT (ft->raw_size >= fs_info->fat_sectors * 512, return 0);
159
if (!ped_geometry_write (fs->geom, (void *) ft->table,
161
+ table_num * fs_info->fat_sectors,
162
fs_info->fat_sectors))
164
if (!ped_geometry_sync (fs->geom))
171
fat_table_write_all (const FatTable* ft, PedFileSystem* fs)
173
FatSpecific* fs_info = FAT_SPECIFIC (fs);
176
for (i = 0; i < fs_info->fat_table_count; i++) {
177
if (!fat_table_write (ft, fs, i))
185
fat_table_compare (const FatTable* a, const FatTable* b)
189
if (a->cluster_count != b->cluster_count)
192
for (i = 0; i < a->cluster_count + 2; i++) {
193
if (fat_table_get (a, i) != fat_table_get (b, i))
201
_test_code_available (const FatTable* ft, FatCluster code)
207
_test_code_bad (const FatTable* ft, FatCluster code)
209
switch (ft->fat_type) {
211
if (code == 0xff7) return 1;
215
if (code == 0xfff7) return 1;
219
if (code == 0x0ffffff7) return 1;
226
_test_code_eof (const FatTable* ft, FatCluster code)
228
switch (ft->fat_type) {
230
if (code >= 0xff7) return 1;
234
if (code >= 0xfff7) return 1;
238
if (code >= 0x0ffffff7) return 1;
245
_update_stats (FatTable* ft, FatCluster cluster, FatCluster value)
247
if (_test_code_available (ft, value)
248
&& !fat_table_is_available (ft, cluster)) {
249
ft->free_cluster_count++;
250
if (fat_table_is_bad (ft, cluster))
251
ft->bad_cluster_count--;
254
if (!_test_code_available (ft, value)
255
&& fat_table_is_available (ft, cluster)) {
256
ft->free_cluster_count--;
257
if (_test_code_bad (ft, cluster))
258
ft->bad_cluster_count--;
263
fat_table_set (FatTable* ft, FatCluster cluster, FatCluster value)
265
if (cluster >= ft->cluster_count + 2) {
266
ped_exception_throw (PED_EXCEPTION_BUG,
267
PED_EXCEPTION_CANCEL,
268
_("fat_table_set: cluster %ld outside "
274
_update_stats (ft, cluster, value);
276
switch (ft->fat_type) {
278
PED_ASSERT (0, (void) 0);
282
((unsigned short *) ft->table) [cluster]
283
= PED_CPU_TO_LE16 (value);
287
((unsigned int *) ft->table) [cluster]
288
= PED_CPU_TO_LE32 (value);
295
fat_table_get (const FatTable* ft, FatCluster cluster)
297
if (cluster >= ft->cluster_count + 2) {
298
ped_exception_throw (PED_EXCEPTION_BUG,
299
PED_EXCEPTION_CANCEL,
300
_("fat_table_get: cluster %ld outside "
303
exit (EXIT_FAILURE); /* FIXME */
306
switch (ft->fat_type) {
308
PED_ASSERT (0, (void) 0);
312
return PED_LE16_TO_CPU
313
(((unsigned short *) ft->table) [cluster]);
316
return PED_LE32_TO_CPU
317
(((unsigned int *) ft->table) [cluster]);
324
fat_table_alloc_cluster (FatTable* ft)
329
/* hack: assumes the first two FAT entries are marked as used (which they
332
for (i=1; i < ft->cluster_count + 1; i++) {
333
cluster = (i + ft->last_alloc) % ft->cluster_count;
334
if (fat_table_is_available (ft, cluster)) {
335
ft->last_alloc = cluster;
340
ped_exception_throw (PED_EXCEPTION_ERROR,
341
PED_EXCEPTION_CANCEL,
342
_("fat_table_alloc_cluster: no free clusters"));
347
fat_table_alloc_check_cluster (FatTable* ft, PedFileSystem* fs)
349
FatSpecific* fs_info = FAT_SPECIFIC (fs);
353
result = fat_table_alloc_cluster (ft);
356
if (fat_read_cluster (fs, fs_info->buffer, result))
358
fat_table_set_bad (ft, result);
363
returns true if <cluster> is marked as bad
366
fat_table_is_bad (const FatTable* ft, FatCluster cluster)
368
return _test_code_bad (ft, fat_table_get (ft, cluster));
372
returns true if <cluster> represents an EOF marker
375
fat_table_is_eof (const FatTable* ft, FatCluster cluster)
377
return _test_code_eof (ft, cluster);
381
returns true if <cluster> is available.
384
fat_table_is_available (const FatTable* ft, FatCluster cluster)
386
return _test_code_available (ft, fat_table_get (ft, cluster));
390
returns true if <cluster> is empty. Note that this includes bad clusters.
393
fat_table_is_empty (const FatTable* ft, FatCluster cluster)
395
return fat_table_is_available (ft, cluster)
396
|| fat_table_is_bad (ft, cluster);
400
returns true if <cluster> is being used for something constructive.
403
fat_table_is_active (const FatTable* ft, FatCluster cluster)
405
return !fat_table_is_bad (ft, cluster)
406
&& !fat_table_is_available (ft, cluster);
410
marks <cluster> as the last cluster in the chain
413
fat_table_set_eof (FatTable* ft, FatCluster cluster)
416
switch (ft->fat_type) {
418
PED_ASSERT (0, (void) 0);
422
return fat_table_set (ft, cluster, 0xfff8);
425
return fat_table_set (ft, cluster, 0x0fffffff);
432
Marks a clusters as unusable, due to physical disk damage.
435
fat_table_set_bad (FatTable* ft, FatCluster cluster)
437
if (!fat_table_is_bad (ft, cluster))
438
ft->bad_cluster_count++;
440
switch (ft->fat_type) {
442
return fat_table_set (ft, cluster, 0xff7);
445
return fat_table_set (ft, cluster, 0xfff7);
448
return fat_table_set (ft, cluster, 0x0ffffff7);
455
marks <cluster> as unused/free/available
458
fat_table_set_avail (FatTable* ft, FatCluster cluster)
460
return fat_table_set (ft, cluster, 0);
463
#endif /* !DISCOVER_ONLY */
466
fat_table_entry_size (FatType fat_type)
470
return 2; /* FIXME: how? */