summaryrefslogtreecommitdiff
path: root/libparted/fs/amiga/affs.c
blob: 8111f556b8c342ebc4200195dce7deadcf880db2 (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
/*
    affs.c -- parted support for affs file systems
    Copyright (C) 1998-2000, 2007 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/parted.h>
#include <parted/debug.h>
#include <parted/endian.h>

#include "amiga.h"
#include "affs.h"

#if ENABLE_NLS
#  include <libintl.h>
#  define _(String) dgettext (PACKAGE, String)
#else
#  define _(String) (String)
#endif /* ENABLE_NLS */

static int
_affs_probe_root (uint32_t *block, int blocksize) {
	int i;
	uint32_t sum;

	if (PED_BE32_TO_CPU (block[0]) != 2) return 0;
	if (PED_BE32_TO_CPU (block[128*blocksize-1]) != 1) return 0;
	for (i = 0, sum = 0; i < 128*blocksize; i++)
		sum += PED_BE32_TO_CPU (block[i]);
	if (sum) return 0;
	return 1;
}

static PedGeometry*
_generic_affs_probe (PedGeometry* geom, uint32_t kind)
{
	uint32_t *block;
	PedSector root, len, pos;
	struct PartitionBlock * part;
	int blocksize = 1, reserved = 2, prealloc = 0;

	PED_ASSERT (geom != NULL, return NULL);
	PED_ASSERT (geom->dev != NULL, return NULL);

	/* Finds the blocksize, prealloc and reserved values of the partition block */
	if (!(part = ped_malloc (PED_SECTOR_SIZE_DEFAULT*blocksize))) {
		ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
			_("%s : Failed to allocate partition block\n"), __func__);
		goto error_part;
	}
	if (amiga_find_part(geom, part) != NULL) {
		prealloc = PED_BE32_TO_CPU (part->de_PreAlloc);
		reserved = PED_BE32_TO_CPU (part->de_Reserved);
		reserved = reserved == 0 ? 1 : reserved;
		blocksize = PED_BE32_TO_CPU (part->de_SizeBlock)
			* PED_BE32_TO_CPU (part->de_SectorPerBlock) / 128;
	}
	ped_free (part);

	/* Test boot block */
	if (!(block = ped_malloc (PED_SECTOR_SIZE_DEFAULT*blocksize))) {
		ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
			_("%s : Failed to allocate block\n"), __func__);
		goto error_block;
	}
	if (!ped_device_read (geom->dev, block, geom->start, blocksize)) {
		ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
			_("%s : Couldn't read boot block %llu\n"), __func__, geom->start);
		goto error;
	}
	if (PED_BE32_TO_CPU (block[0]) != kind) {
		goto error;
	}

	/* Find and test the root block */
	len = geom->length / blocksize - reserved;
	pos = (len - 1) / 2;
	root = geom->start + (pos + reserved) * blocksize;
	printf ("Pralloc = %d, Reserved = %d, blocksize = %d, root block at %llu\n",
		prealloc, reserved, blocksize, root);

	if (!ped_device_read (geom->dev, block, root, blocksize)) {
		ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
			_("%s : Couldn't read root block %llu\n"), __func__, root);
		goto error;
	}
	if (_affs_probe_root(block, blocksize) == 1) {
		ped_free (block);
		return ped_geometry_duplicate (geom);
	}

error:
	ped_free (block);
error_block:
error_part:
	return NULL;
}
static PedGeometry*
_affs0_probe (PedGeometry* geom) {
	return _generic_affs_probe (geom, 0x444f5300);
}
static PedGeometry*
_affs1_probe (PedGeometry* geom) {
	return _generic_affs_probe (geom, 0x444f5301);
}
static PedGeometry*
_affs2_probe (PedGeometry* geom) {
	return _generic_affs_probe (geom, 0x444f5302);
}
static PedGeometry*
_affs3_probe (PedGeometry* geom) {
	return _generic_affs_probe (geom, 0x444f5303);
}
static PedGeometry*
_affs4_probe (PedGeometry* geom) {
	return _generic_affs_probe (geom, 0x444f5304);
}
static PedGeometry*
_affs5_probe (PedGeometry* geom) {
	return _generic_affs_probe (geom, 0x444f5305);
}
static PedGeometry*
_affs6_probe (PedGeometry* geom) {
	return _generic_affs_probe (geom, 0x444f5306);
}
static PedGeometry*
_affs7_probe (PedGeometry* geom) {
	return _generic_affs_probe (geom, 0x444f5307);
}
static PedGeometry*
_amufs_probe (PedGeometry* geom) {
	return _generic_affs_probe (geom, 0x6d754653);
}
static PedGeometry*
_amufs0_probe (PedGeometry* geom) {
	return _generic_affs_probe (geom, 0x6d754600);
}
static PedGeometry*
_amufs1_probe (PedGeometry* geom) {
	return _generic_affs_probe (geom, 0x6d754601);
}
static PedGeometry*
_amufs2_probe (PedGeometry* geom) {
	return _generic_affs_probe (geom, 0x6d754602);
}
static PedGeometry*
_amufs3_probe (PedGeometry* geom) {
	return _generic_affs_probe (geom, 0x6d754603);
}
static PedGeometry*
_amufs4_probe (PedGeometry* geom) {
	return _generic_affs_probe (geom, 0x6d754604);
}
static PedGeometry*
_amufs5_probe (PedGeometry* geom) {
	return _generic_affs_probe (geom, 0x6d754605);
}

static PedFileSystemOps _affs0_ops = {
	probe:		_affs0_probe,
	clobber:	NULL,
	open:		NULL,
	create:         NULL,
	close:		NULL,
	check:          NULL,
	resize:		NULL,
	copy:           NULL,
	get_create_constraint:	NULL,
	get_copy_constraint:	NULL,
	get_resize_constraint:	NULL
};
static PedFileSystemOps _affs1_ops = {
	probe:		_affs1_probe,
	clobber:	NULL,
	open:		NULL,
	create:         NULL,
	close:		NULL,
	check:          NULL,
	resize:		NULL,
	copy:           NULL,
	get_create_constraint:	NULL,
	get_copy_constraint:	NULL,
	get_resize_constraint:	NULL
};
static PedFileSystemOps _affs2_ops = {
	probe:		_affs2_probe,
	clobber:	NULL,
	open:		NULL,
	create:         NULL,
	close:		NULL,
	check:          NULL,
	resize:		NULL,
	copy:           NULL,
	get_create_constraint:	NULL,
	get_copy_constraint:	NULL,
	get_resize_constraint:	NULL
};
static PedFileSystemOps _affs3_ops = {
	probe:		_affs3_probe,
	clobber:	NULL,
	open:		NULL,
	create:         NULL,
	close:		NULL,
	check:          NULL,
	resize:		NULL,
	copy:           NULL,
	get_create_constraint:	NULL,
	get_copy_constraint:	NULL,
	get_resize_constraint:	NULL
};
static PedFileSystemOps _affs4_ops = {
	probe:		_affs4_probe,
	clobber:	NULL,
	open:		NULL,
	create:         NULL,
	close:		NULL,
	check:          NULL,
	resize:		NULL,
	copy:           NULL,
	get_create_constraint:	NULL,
	get_copy_constraint:	NULL,
	get_resize_constraint:	NULL
};
static PedFileSystemOps _affs5_ops = {
	probe:		_affs5_probe,
	clobber:	NULL,
	open:		NULL,
	create:         NULL,
	close:		NULL,
	check:          NULL,
	resize:		NULL,
	copy:           NULL,
	get_create_constraint:	NULL,
	get_copy_constraint:	NULL,
	get_resize_constraint:	NULL
};
static PedFileSystemOps _affs6_ops = {
	probe:		_affs6_probe,
	clobber:	NULL,
	open:		NULL,
	create:         NULL,
	close:		NULL,
	check:          NULL,
	resize:		NULL,
	copy:           NULL,
	get_create_constraint:	NULL,
	get_copy_constraint:	NULL,
	get_resize_constraint:	NULL
};
static PedFileSystemOps _affs7_ops = {
	probe:		_affs7_probe,
	clobber:	NULL,
	open:		NULL,
	create:         NULL,
	close:		NULL,
	check:          NULL,
	resize:		NULL,
	copy:           NULL,
	get_create_constraint:	NULL,
	get_copy_constraint:	NULL,
	get_resize_constraint:	NULL
};
static PedFileSystemOps _amufs_ops = {
	probe:		_amufs_probe,
	clobber:	NULL,
	open:		NULL,
	create:         NULL,
	close:		NULL,
	check:          NULL,
	resize:		NULL,
	copy:           NULL,
	get_create_constraint:	NULL,
	get_copy_constraint:	NULL,
	get_resize_constraint:	NULL
};
static PedFileSystemOps _amufs0_ops = {
	probe:		_amufs0_probe,
	clobber:	NULL,
	open:		NULL,
	create:         NULL,
	close:		NULL,
	check:          NULL,
	resize:		NULL,
	copy:           NULL,
	get_create_constraint:	NULL,
	get_copy_constraint:	NULL,
	get_resize_constraint:	NULL
};
static PedFileSystemOps _amufs1_ops = {
	probe:		_amufs1_probe,
	clobber:	NULL,
	open:		NULL,
	create:         NULL,
	close:		NULL,
	check:          NULL,
	resize:		NULL,
	copy:           NULL,
	get_create_constraint:	NULL,
	get_copy_constraint:	NULL,
	get_resize_constraint:	NULL
};
static PedFileSystemOps _amufs2_ops = {
	probe:		_amufs2_probe,
	clobber:	NULL,
	open:		NULL,
	create:         NULL,
	close:		NULL,
	check:          NULL,
	resize:		NULL,
	copy:           NULL,
	get_create_constraint:	NULL,
	get_copy_constraint:	NULL,
	get_resize_constraint:	NULL
};
static PedFileSystemOps _amufs3_ops = {
	probe:		_amufs3_probe,
	clobber:	NULL,
	open:		NULL,
	create:         NULL,
	close:		NULL,
	check:          NULL,
	resize:		NULL,
	copy:           NULL,
	get_create_constraint:	NULL,
	get_copy_constraint:	NULL,
	get_resize_constraint:	NULL
};
static PedFileSystemOps _amufs4_ops = {
	probe:		_amufs4_probe,
	clobber:	NULL,
	open:		NULL,
	create:         NULL,
	close:		NULL,
	check:          NULL,
	resize:		NULL,
	copy:           NULL,
	get_create_constraint:	NULL,
	get_copy_constraint:	NULL,
	get_resize_constraint:	NULL
};
static PedFileSystemOps _amufs5_ops = {
	probe:		_amufs5_probe,
	clobber:	NULL,
	open:		NULL,
	create:         NULL,
	close:		NULL,
	check:          NULL,
	resize:		NULL,
	copy:           NULL,
	get_create_constraint:	NULL,
	get_copy_constraint:	NULL,
	get_resize_constraint:	NULL
};

#define AFFS_BLOCK_SIZES        ((int[5]){512, 1024, 2048, 4096, 0})
#define AMUFS_BLOCK_SIZES       ((int[2]){512, 0})


PedFileSystemType _affs0_type = {
       next:		 NULL,
       ops:		 &_affs0_ops,
       name:		 "affs0",
       block_sizes:      AFFS_BLOCK_SIZES
};
PedFileSystemType _affs1_type = {
       next:		 NULL,
       ops:		 &_affs1_ops,
       name:		 "affs1",
       block_sizes:      AFFS_BLOCK_SIZES
};
PedFileSystemType _affs2_type = {
       next:		 NULL,
       ops:		 &_affs2_ops,
       name:		 "affs2",
       block_sizes:      AFFS_BLOCK_SIZES
};
PedFileSystemType _affs3_type = {
       next:		 NULL,
       ops:		 &_affs3_ops,
       name:		 "affs3",
       block_sizes:      AFFS_BLOCK_SIZES
};
PedFileSystemType _affs4_type = {
       next:		 NULL,
       ops:		 &_affs4_ops,
       name:		 "affs4",
       block_sizes:      AFFS_BLOCK_SIZES
};
PedFileSystemType _affs5_type = {
       next:		 NULL,
       ops:		 &_affs5_ops,
       name:		 "affs5",
       block_sizes:      AFFS_BLOCK_SIZES
};
PedFileSystemType _affs6_type = {
       next:		 NULL,
       ops:		 &_affs6_ops,
       name:		 "affs6",
       block_sizes:      AFFS_BLOCK_SIZES
};
PedFileSystemType _affs7_type = {
       next:		 NULL,
       ops:		 &_affs7_ops,
       name:		 "affs7",
       block_sizes:      AFFS_BLOCK_SIZES
};
PedFileSystemType _amufs_type = {
       next:		 NULL,
       ops:		 &_amufs_ops,
       name:		 "amufs",
       block_sizes:      AMUFS_BLOCK_SIZES
};
PedFileSystemType _amufs0_type = {
       next:		 NULL,
       ops:		 &_amufs0_ops,
       name:		 "amufs0",
       block_sizes:      AMUFS_BLOCK_SIZES
};
PedFileSystemType _amufs1_type = {
       next:		 NULL,
       ops:		 &_amufs1_ops,
       name:		 "amufs1",
       block_sizes:      AMUFS_BLOCK_SIZES
};
PedFileSystemType _amufs2_type = {
       next:		 NULL,
       ops:		 &_amufs2_ops,
       name:		 "amufs2",
       block_sizes:      AMUFS_BLOCK_SIZES
};
PedFileSystemType _amufs3_type = {
       next:		 NULL,
       ops:		 &_amufs3_ops,
       name:		 "amufs3",
       block_sizes:      AMUFS_BLOCK_SIZES
};
PedFileSystemType _amufs4_type = {
       next:		 NULL,
       ops:		 &_amufs4_ops,
       name:		 "amufs4",
       block_sizes:      AMUFS_BLOCK_SIZES
};
PedFileSystemType _amufs5_type = {
       next:		 NULL,
       ops:		 &_amufs5_ops,
       name:		 "amufs5",
       block_sizes:      AMUFS_BLOCK_SIZES
};