summaryrefslogtreecommitdiffstats
path: root/libparted/fs/r/fat/table.c
blob: ec0907f90d4f28c2405595cd54537990e629d576 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
    libparted
    Copyright (C) 1998-2000, 2007-2014, 2019-2023 Free Software Foundation,
    Inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>
#include <parted/endian.h>
#include "fat.h"

#ifndef DISCOVER_ONLY

FatTable*
fat_table_new (FatType fat_type, FatCluster size)
{
	FatTable*	ft;
	int		entry_size = fat_table_entry_size (fat_type);

	ft = (FatTable*) ped_malloc (sizeof (FatTable));
	if (!ft) return NULL;

	ft->cluster_count = ft->free_cluster_count = size - 2;

/* ensure there's some free room on the end, to finish off the sector */
	ft->size = ped_div_round_up (size * entry_size, 512) * 512 / entry_size;
	ft->fat_type = fat_type;
	ft->raw_size = ft->size * entry_size;

	ft->table = ped_malloc (ft->raw_size);
	if (!ft->table) {
		free (ft);
		return NULL;
	}

	fat_table_clear (ft);
	return ft;
}

void
fat_table_destroy (FatTable* ft)
{
	free (ft->table);
	free (ft);
}

FatTable*
fat_table_duplicate (const FatTable* ft)
{
	FatTable*	dup_ft;

	dup_ft = fat_table_new (ft->fat_type, ft->size);
	if (!dup_ft) return NULL;

	dup_ft->cluster_count	= ft->cluster_count;
	dup_ft->free_cluster_count	= ft->free_cluster_count;
	dup_ft->bad_cluster_count	= ft->bad_cluster_count;
	dup_ft->last_alloc		= ft->last_alloc;

	memcpy (dup_ft->table, ft->table, ft->raw_size);

	return dup_ft;
}

void
fat_table_clear (FatTable* ft)
{
	memset (ft->table, 0, ft->raw_size);

	fat_table_set (ft, 0, 0x0ffffff8);
	fat_table_set (ft, 1, 0x0fffffff);

	ft->free_cluster_count = ft->cluster_count;
	ft->bad_cluster_count = 0;
	ft->last_alloc = 1;
}

int
fat_table_set_cluster_count (FatTable* ft, FatCluster new_cluster_count)
{
	PED_ASSERT (new_cluster_count + 2 <= ft->size);

	ft->cluster_count = new_cluster_count;
	return fat_table_count_stats (ft);
}

int
fat_table_count_stats (FatTable* ft)
{
	FatCluster	i;

	PED_ASSERT (ft->cluster_count + 2 <= ft->size);

	ft->free_cluster_count = 0;
	ft->bad_cluster_count = 0;

	for (i=2; i < ft->cluster_count + 2; i++) {
		if (fat_table_is_available (ft, i))
			ft->free_cluster_count++;
		if (fat_table_is_bad (ft, i))
			ft->bad_cluster_count++;
	}
	return 1;
}

int
fat_table_read (FatTable* ft, const PedFileSystem* fs, int table_num)
{
	FatSpecific*	fs_info = FAT_SPECIFIC (fs);

	PED_ASSERT (ft->raw_size >= fs_info->fat_sectors * 512);

	memset (ft->table, 0, ft->raw_size);

        if (!ped_geometry_read (fs->geom, (void *) ft->table,
				fs_info->fat_offset
					+ table_num * fs_info->fat_sectors,
				fs_info->fat_sectors))
		return 0;

        if ( *((unsigned char*) ft->table) != fs_info->boot_sector->media) {
		if (ped_exception_throw (
			PED_EXCEPTION_ERROR,
			PED_EXCEPTION_IGNORE_CANCEL,
			_("FAT %d media %x doesn't match the boot sector's "
			  "media %x.  You should probably run scandisk."),
			(int) table_num + 1,
			(int) *((unsigned char*) ft->table),
			(int) fs_info->boot_sector->media)
				!= PED_EXCEPTION_IGNORE)
			return 0;
        }

	ft->cluster_count = fs_info->cluster_count;

	fat_table_count_stats (ft);

	return 1;
}

int
fat_table_write (const FatTable* ft, PedFileSystem* fs, int table_num)
{
	FatSpecific*	fs_info = FAT_SPECIFIC (fs);

	PED_ASSERT (ft->raw_size >= fs_info->fat_sectors * 512);

        if (!ped_geometry_write (fs->geom, (void *) ft->table,
				 fs_info->fat_offset
					+ table_num * fs_info->fat_sectors,
				 fs_info->fat_sectors))
		return 0;
	if (!ped_geometry_sync (fs->geom))
		return 0;

	return 1;
}

int
fat_table_write_all (const FatTable* ft, PedFileSystem* fs)
{
	FatSpecific*	fs_info = FAT_SPECIFIC (fs);
	int		i;

	for (i = 0; i < fs_info->fat_table_count; i++) {
		if (!fat_table_write (ft, fs, i))
			return 0;
	}

	return 1;
}

int
fat_table_compare (const FatTable* a, const FatTable* b)
{
	FatCluster	i;

	if (a->cluster_count != b->cluster_count)
		return 0;

	for (i = 0; i < a->cluster_count + 2; i++) {
		if (fat_table_get (a, i) != fat_table_get (b, i))
			return 0;
	}

	return 1;
}

static int
_test_code_available (const FatTable* ft, FatCluster code)
{
	return code == 0;
}

static int
_test_code_bad (const FatTable* ft, FatCluster code)
{
	switch (ft->fat_type) {
                case FAT_TYPE_FAT12:
                if (code == 0xff7) return 1;
                break;

		case FAT_TYPE_FAT16:
		if (code == 0xfff7) return 1;
		break;

		case FAT_TYPE_FAT32:
		if (code == 0x0ffffff7) return 1;
		break;
	}
	return 0;
}

static int
_test_code_eof (const FatTable* ft, FatCluster code)
{
	switch (ft->fat_type) {
                case FAT_TYPE_FAT12:
                if (code >= 0xff7) return 1;
                break;

		case FAT_TYPE_FAT16:
		if (code >= 0xfff7) return 1;
		break;

		case FAT_TYPE_FAT32:
		if (code >= 0x0ffffff7) return 1;
		break;
	}
	return 0;
}

void
_update_stats (FatTable* ft, FatCluster cluster, FatCluster value)
{
	if (_test_code_available (ft, value)
	    && !fat_table_is_available (ft, cluster)) {
		ft->free_cluster_count++;
		if (fat_table_is_bad (ft, cluster))
			ft->bad_cluster_count--;
	}

	if (!_test_code_available (ft, value)
	    && fat_table_is_available (ft, cluster)) {
		ft->free_cluster_count--;
		if (_test_code_bad (ft, cluster))
			ft->bad_cluster_count--;
	}
}

int
fat_table_set (FatTable* ft, FatCluster cluster, FatCluster value)
{
	if (cluster >= ft->cluster_count + 2) {
		ped_exception_throw (PED_EXCEPTION_BUG,
				     PED_EXCEPTION_CANCEL,
				     _("fat_table_set: cluster %ld outside "
				       "file system"),
				     (long) cluster);
		return 0;
	}

	_update_stats (ft, cluster, value);

	switch (ft->fat_type) {
                case FAT_TYPE_FAT12:
                PED_ASSERT (0);
                break;

		case FAT_TYPE_FAT16:
		((unsigned short *) ft->table) [cluster]
			= PED_CPU_TO_LE16 (value);
		break;

		case FAT_TYPE_FAT32:
		((unsigned int *) ft->table) [cluster]
			= PED_CPU_TO_LE32 (value);
		break;
	}
	return 1;
}

FatCluster
fat_table_get (const FatTable* ft, FatCluster cluster)
{
	if (cluster >= ft->cluster_count + 2) {
		ped_exception_throw (PED_EXCEPTION_BUG,
				     PED_EXCEPTION_CANCEL,
				     _("fat_table_get: cluster %ld outside "
				       "file system"),
				     (long) cluster);
		exit (EXIT_FAILURE);	/* FIXME */
	}

	switch (ft->fat_type) {
                case FAT_TYPE_FAT12:
                PED_ASSERT (0);
                break;

		case FAT_TYPE_FAT16:
		return PED_LE16_TO_CPU
			(((unsigned short *) ft->table) [cluster]);

		case FAT_TYPE_FAT32:
		return PED_LE32_TO_CPU
			(((unsigned int *) ft->table) [cluster]);
	}

	return 0;
}

FatCluster
fat_table_alloc_cluster (FatTable* ft)
{
	FatCluster	i;
	FatCluster	cluster;

/* hack: assumes the first two FAT entries are marked as used (which they
 * always should be)
 */
	for (i=1; i < ft->cluster_count + 1; i++) {
		cluster = (i + ft->last_alloc) % ft->cluster_count;
		if (fat_table_is_available (ft, cluster)) {
			ft->last_alloc = cluster;
			return cluster;
		}
	}

	ped_exception_throw (PED_EXCEPTION_ERROR,
			     PED_EXCEPTION_CANCEL,
			     _("fat_table_alloc_cluster: no free clusters"));
	return 0;
}

FatCluster
fat_table_alloc_check_cluster (FatTable* ft, PedFileSystem* fs)
{
	FatSpecific*	fs_info = FAT_SPECIFIC (fs);
	FatCluster	result;

	while (1) {
		result = fat_table_alloc_cluster (ft);
		if (!result)
			return 0;
		if (fat_read_cluster (fs, fs_info->buffer, result))
			return result;
		fat_table_set_bad (ft, result);
	}
}

/*
    returns true if <cluster> is marked as bad
*/
int
fat_table_is_bad (const FatTable* ft, FatCluster cluster)
{
	return _test_code_bad (ft, fat_table_get (ft, cluster));
}

/*
    returns true if <cluster> represents an EOF marker
*/
int _GL_ATTRIBUTE_PURE
fat_table_is_eof (const FatTable* ft, FatCluster cluster)
{
	return _test_code_eof (ft, cluster);
}

/*
    returns true if <cluster> is available.
*/
int
fat_table_is_available (const FatTable* ft, FatCluster cluster)
{
	return _test_code_available (ft, fat_table_get (ft, cluster));
}

/*
    returns true if <cluster> is empty.  Note that this includes bad clusters.
*/
int
fat_table_is_empty (const FatTable* ft, FatCluster cluster)
{
	return fat_table_is_available (ft, cluster)
		|| fat_table_is_bad (ft, cluster);
}

/*
    returns true if <cluster> is being used for something constructive.
*/
int
fat_table_is_active (const FatTable* ft, FatCluster cluster)
{
	return !fat_table_is_bad (ft, cluster)
		&& !fat_table_is_available (ft, cluster);
}

/*
    marks <cluster> as the last cluster in the chain
*/
int
fat_table_set_eof (FatTable* ft, FatCluster cluster)
{

	switch (ft->fat_type) {
                case FAT_TYPE_FAT12:
                PED_ASSERT (0);
                break;

		case FAT_TYPE_FAT16:
		return fat_table_set (ft, cluster, 0xfff8);

		case FAT_TYPE_FAT32:
		return fat_table_set (ft, cluster, 0x0fffffff);
	}

	return 0;
}

/*
	Marks a clusters as unusable, due to physical disk damage.
*/
int
fat_table_set_bad (FatTable* ft, FatCluster cluster)
{
	if (!fat_table_is_bad (ft, cluster))
		ft->bad_cluster_count++;

	switch (ft->fat_type) {
                case FAT_TYPE_FAT12:
		return fat_table_set (ft, cluster, 0xff7);

		case FAT_TYPE_FAT16:
		return fat_table_set (ft, cluster, 0xfff7);

		case FAT_TYPE_FAT32:
		return fat_table_set (ft, cluster, 0x0ffffff7);
	}

	return 0;
}

/*
    marks <cluster> as unused/free/available
*/
int
fat_table_set_avail (FatTable* ft, FatCluster cluster)
{
	return fat_table_set (ft, cluster, 0);
}

#endif /* !DISCOVER_ONLY */

int _GL_ATTRIBUTE_CONST
fat_table_entry_size (FatType fat_type)
{
	switch (fat_type) {
		case FAT_TYPE_FAT12:
		return 2;		/* FIXME: how? */

		case FAT_TYPE_FAT16:
		return 2;

		case FAT_TYPE_FAT32:
		return 4;
	}

	return 0;
}