summaryrefslogtreecommitdiff
path: root/libparted/fs/amiga/affs.c
blob: 750eab268aca106417f32d8e4d5f4c3e8cd9758e (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
/*
    affs.c -- parted support for affs file systems
    Copyright (C) 1998-2000, 2007, 2009-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/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;

	PED_ASSERT (geom != NULL);
	PED_ASSERT (geom->dev != NULL);
	if (geom->dev->sector_size != 512)
		return NULL;
	/* Finds the blocksize 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) {
		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;
	}
	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;

	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) {
		free (block);
		return ped_geometry_duplicate (geom);
	}

error:
	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,
};
static PedFileSystemOps _affs1_ops = {
	probe:		_affs1_probe,
};
static PedFileSystemOps _affs2_ops = {
	probe:		_affs2_probe,
};
static PedFileSystemOps _affs3_ops = {
	probe:		_affs3_probe,
};
static PedFileSystemOps _affs4_ops = {
	probe:		_affs4_probe,
};
static PedFileSystemOps _affs5_ops = {
	probe:		_affs5_probe,
};
static PedFileSystemOps _affs6_ops = {
	probe:		_affs6_probe,
};
static PedFileSystemOps _affs7_ops = {
	probe:		_affs7_probe,
};
static PedFileSystemOps _amufs_ops = {
	probe:		_amufs_probe,
};
static PedFileSystemOps _amufs0_ops = {
	probe:		_amufs0_probe,
};
static PedFileSystemOps _amufs1_ops = {
	probe:		_amufs1_probe,
};
static PedFileSystemOps _amufs2_ops = {
	probe:		_amufs2_probe,
};
static PedFileSystemOps _amufs3_ops = {
	probe:		_amufs3_probe,
};
static PedFileSystemOps _amufs4_ops = {
	probe:		_amufs4_probe,
};
static PedFileSystemOps _amufs5_ops = {
	probe:		_amufs5_probe,
};

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