-
Notifications
You must be signed in to change notification settings - Fork 1
/
apascan.c
331 lines (257 loc) · 7.76 KB
/
apascan.c
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
/*
* apascan - Scans for APA partitions used on the PlayStation 2
* Copyright (C) 2008 James Le Cuirot
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#define _GNU_SOURCE
#include <sys/types.h>
#include <asm/types.h>
#include <asm/byteorder.h>
#include <linux/fs.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "libdevmapper.h"
#include "config.h"
#include "ps2.h"
#define ERROR(...) { printf(__VA_ARGS__); goto error; }
void version()
{
printf("%s v%s - Copyright (c) 2008 James Le Cuirot <%s>\n", PACKAGE_NAME, PACKAGE_VERSION, PACKAGE_BUGREPORT);
printf("Distributed under the GNU General Public License v2 with no warranty.\n");
}
void usage(char *arg0)
{
printf("Usage: %s [-rhV] [BLOCKDEV]\n\n", arg0);
printf("BLOCKDEV is the path to a block device such as /dev/hda. For manipulating disk\n");
printf("images, use losetup and a loopback device such as /dev/loop0. These are the\n");
printf("options.\n\n");
printf(" -d Remove all mapped partitions for the given device. Do this before\n");
printf(" unplugging your hard drive. It is not necessary to use this option\n");
printf(" before rescanning. The existing mappings are removed automatically.\n");
printf(" -h Display this help and exit.\n");
printf(" -V Output version information and exit.\n\n");
}
unsigned int remove_mapping(char *dev_name, struct dm_names *names)
{
unsigned int temp;
struct dm_task *dmt = NULL;
if (names->next)
{
if (!remove_mapping(dev_name, ((void *) names) + names->next))
return 0;
}
temp = strlen(dev_name);
if (!strncmp(names->name, dev_name, temp) && !strncmp(names->name + temp, "-apa-", 5))
{
if (!(dmt = dm_task_create(DM_DEVICE_REMOVE)))
ERROR("Unable to initialize mapping removal!\n")
if (!dm_task_set_name(dmt, names->name))
ERROR("Unable to set mapping to remove!\n")
if (!dm_task_run(dmt))
ERROR("Unable to remove mapping!\n")
dm_task_destroy(dmt);
dmt = NULL;
}
return 1;
error:
if (dmt) dm_task_destroy(dmt);
return 0;
}
unsigned int remove_mappings(char *dev_name)
{
unsigned int rc;
struct dm_task *dmt = NULL;
struct dm_names *names;
if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
ERROR("Unable to initialize mapping list!\n")
if (!dm_task_run(dmt))
ERROR("Unable to create mapping list!\n")
if (!(names = dm_task_get_names(dmt)))
ERROR("Unable to get mapping names!\n")
rc = names->dev ? remove_mapping(dev_name, names) : 1;
dm_task_destroy(dmt);
return rc;
error:
if (dmt) dm_task_destroy(dmt);
return 0;
}
unsigned int create_mappings(int fd, char *path, char *dev_name, unsigned int ss, off_t sector)
{
unsigned int temp, temp2, reserved, offset = 0;
char *args, *part_name = NULL;
struct ps2_partition *pp = NULL;
struct dm_task *dmt = NULL;
ssize_t bytes_read, bytes_to_read;
bytes_to_read = sizeof(struct ps2_partition);
if (!(pp = malloc(bytes_to_read)))
ERROR("Out of memory!\n")
reread:
lseek(fd, sector * ss, SEEK_SET);
bytes_read = read(fd, (void *) pp, bytes_to_read);
if (bytes_read < 1)
ERROR("Error reading device!\n")
else if (bytes_read < bytes_to_read)
{
/* I'm lazy. */
goto reread;
}
if (sector == 0)
{
if (memcmp(pp->mbr.magic, PS2_MBR_MAGIC, 32) || pp->mbr.version > PS2_MBR_VERSION)
ERROR("APA partition table not found!\n")
}
if (pp->magic != PS2_PARTITION_MAGIC)
goto error;
/* Don't add the MBR partition. It's a dummy. Don't add empty or sub partitions either. */
if (sector == 0 || pp->fstype == 0 || pp->flag & PS2_PART_FLAG_SUB)
{
sector = __le32_to_cpu(pp->next);
free(pp);
return sector ? create_mappings(fd, path, dev_name, ss, sector) : 0;
}
/* Just in case... */
pp->id[PS2_PART_NID - 1] = '\0';
printf("Found partition \"%s\" with %d parts.\n", pp->id, __le32_to_cpu(pp->nsub) + 1);
if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
ERROR("Unable to initialize mapping!\n")
temp = strlen(dev_name);
/* I haven't forgotten the null terminator. It's included in PS2_PART_NID. */
if (!(part_name = malloc(sizeof(char) * (temp + PS2_PART_NID + 5))))
ERROR("Out of memory!\n")
strncpy(part_name, dev_name, temp);
strncpy(part_name + temp, "-apa-", 5);
strcpy(part_name + temp + 5, pp->id);
/* Replace all spaces and slashes with underscores. */
for (temp = 0; part_name[temp] != '\0'; temp++)
{
switch (part_name[temp])
{
case ' ':
case '/':
part_name[temp] = '_';
}
}
printf("Mapping to \"%s\".\n", part_name);
if (!dm_task_set_name(dmt, part_name))
ERROR("Unable to set mapping name!\n")
reserved = PS2_PART_RESV_MAIN / ss;
if (asprintf(&args, "%s %d", path, __le32_to_cpu(pp->start) + reserved) < 0)
ERROR("Out of memory!\n");
temp2 = __le32_to_cpu(pp->nsector) - reserved;
if (!dm_task_add_target(dmt, offset, temp2, "linear", args))
ERROR("Unable to add part 1!\n");
free(args);
offset += temp2;
reserved = PS2_PART_RESV_SUB / ss;
for (temp = 0; temp < __le32_to_cpu(pp->nsub); temp++)
{
if (asprintf(&args, "%s %d", path, __le32_to_cpu(pp->subs[temp].start) + reserved) < 0)
ERROR("Out of memory!\n");
temp2 = __le32_to_cpu(pp->subs[temp].nsector) - reserved;
if (!dm_task_add_target(dmt, offset, temp2, "linear", args))
ERROR("Unable to add part %d!\n", temp + 2);
free(args);
offset += temp2;
}
if (!dm_task_run(dmt))
ERROR("Unable to create mapping!\n")
sector = __le32_to_cpu(pp->next);
dm_task_destroy(dmt);
free(part_name);
free(pp);
return sector ? 1 + create_mappings(fd, path, dev_name, ss, sector) : 1;
error:
if (dmt) dm_task_destroy(dmt);
free(part_name);
free(pp);
return 0;
}
int main(int argc, char **argv)
{
struct stat sbuf;
unsigned int ss, count, opt, remove = 0;
char *path = NULL, *dev_name = NULL;
int fd = -1;
opterr = 0;
while ((opt = getopt(argc, argv, "rhV")) != -1)
{
switch (opt)
{
case 'r':
remove = 1;
break;
case 'h':
version(argv[0]);
printf("\n");
usage(argv[0]);
return 0;
case 'V':
version(argv[0]);
return 0;
case '?':
version(argv[0]);
printf("\n");
usage(argv[0]);
return 1;
default:
abort();
}
}
version(argv[0]);
printf("\n");
if (argc != optind + 1)
{
usage(argv[0]);
return 1;
}
if (!(path = realpath(argv[optind], NULL)))
ERROR("Cannot find device %s!\n", argv[optind])
printf("Scanning %s...\n", path);
if ((fd = open(path, O_RDONLY)) == -1)
ERROR("Unable to open device!\n")
if (fstat(fd, &sbuf) < 0)
ERROR("Unable to stat device!\n")
if (!S_ISBLK(sbuf.st_mode))
ERROR("This is not a block device!\n")
if (ioctl(fd, BLKSSZGET, (void *) &ss))
ERROR("Unable to get sector size!\n")
if (!(dev_name = basename(path)))
ERROR("Out of memory!\n")
if (!remove_mappings(dev_name))
goto error;
if (!remove)
{
count = create_mappings(fd, path, dev_name, ss, 0);
printf("%d partitions mapped.\n", count);
if (!count)
goto error;
}
else
printf("All mapped partitions successfully removed.\n");
free(path);
close(fd);
return 0;
error:
free(path);
if (fd != -1) close(fd);
return 1;
}