forked from ps2homebrew/hdl-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isofs.c
331 lines (299 loc) · 10.8 KB
/
isofs.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
/*
* isofs.c
* $Id: isofs.c,v 1.11 2006/09/01 17:23:22 bobi Exp $
*
* Copyright 2004 Bobi B., [email protected]
*
* This file is part of hdl_dump.
*
* hdl_dump 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.
*
* hdl_dump 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 hdl_dump; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <ctype.h>
#include <string.h>
#include "isofs.h"
#include "retcodes.h"
#include "common.h"
#define CDVD_SECT_SIZE 2048
/**************************************************************/
/*
* pvd == "Primary Volume Descriptor"
* ptr == "Path Table Record"
*/
static int
isofs_find_pvd_addr(iin_t *iin,
/*@out@*/ u_int64_t *pvd_start_addr,
/*@out@*/ u_int64_t *ptr_start_addr,
/*@out@*/ char system_id[32 + 1],
/*@out@*/ char volume_id[32 + 1],
int layer)
{
u_int32_t start_sector = 16;
const char *data;
u_int32_t len;
const unsigned char *buffer;
int result = iin->read(iin, start_sector, 1, &data, &len);
buffer = (const unsigned char *)data;
*pvd_start_addr = *ptr_start_addr = 0;
*system_id = *volume_id = '\0';
if (result == OSAL_OK)
result = memcmp(buffer + 1, "CD001", 5) == 0 ? OSAL_OK : RET_BAD_ISOFS;
if (layer == 1) {
start_sector = ((u_int32_t)buffer[83] << 24 |
(u_int32_t)buffer[82] << 16 |
(u_int32_t)buffer[81] << 8 |
(u_int32_t)buffer[80] << 0);
result = iin->read(iin, start_sector, 1, &data, &len);
if (len != 2048)
result = RET_BAD_ISOFS;
if (result == OSAL_OK) {
buffer = (const unsigned char *)data;
result = (memcmp(buffer + 1, "CD001", 5) == 0 ?
OSAL_OK :
RET_BAD_ISOFS);
}
}
if (result == OSAL_OK) {
if (buffer[0] == 0x01) /* primary volume descriptor set */
{
*pvd_start_addr = start_sector * IIN_SECTOR_SIZE;
*ptr_start_addr = (u_int64_t)(buffer[143] << 24 |
buffer[142] << 16 |
buffer[141] << 8 |
buffer[140] << 0) *
(u_int64_t)CDVD_SECT_SIZE;
if (system_id != NULL) {
memcpy(system_id, buffer + 8, 32);
system_id[32] = '\0';
(void)rtrim(system_id);
}
if (volume_id != NULL) {
memcpy(volume_id, buffer + 40, 32);
volume_id[32] = '\0';
(void)rtrim(volume_id);
}
} else
result = RET_BAD_ISOFS; /* primary volume descriptor set not found */
}
return (result);
}
/**************************************************************/
static int
isofs_get_root_addr(iin_t *iin,
u_int64_t ptr_start_addr,
/*@out@*/ u_int64_t *root_start_addr)
{
u_int32_t len;
const char *data;
int result = iin->read(iin, (u_int32_t)(ptr_start_addr / IIN_SECTOR_SIZE),
1, &data, &len);
const unsigned char *buffer = (const unsigned char *)data;
*root_start_addr = 0;
if (result == OSAL_OK) {
int found = 0;
u_int32_t dir_start_addr;
const unsigned char *p = buffer;
do {
int id_len;
u_int16_t parent_dir;
id_len = *p++;
if (id_len == 0 ||
p > buffer + 2047)
break; /* last entry */
p++;
dir_start_addr = ((u_int32_t)p[3] << 24 |
(u_int32_t)p[2] << 16 |
(u_int32_t)p[1] << 8 |
(u_int32_t)p[0] << 0);
p += 4;
parent_dir = (u_int16_t)p[1] << 8 | (u_int16_t)p[0];
p += 2;
if (*p == '\0') { /* root dir? */
found = 1;
break;
} else
p += id_len + (id_len % 2); /* skip to next entry */
} while (1);
if (found)
*root_start_addr = (u_int64_t)dir_start_addr * CDVD_SECT_SIZE;
else
result = RET_BAD_ISOFS; /* root dir not found */
}
return (result);
}
/**************************************************************/
static int
isofs_get_file_addr(iin_t *iin,
u_int64_t dir_start_addr,
const char *file_name,
/*@out@*/ u_int64_t *file_start_addr,
/*@out@*/ u_int64_t *file_length)
{
u_int32_t file_name_len = strlen(file_name);
u_int32_t len;
const char *data;
int result = iin->read(iin, (u_int32_t)(dir_start_addr / IIN_SECTOR_SIZE),
1, &data, &len);
const unsigned char *buffer = (const unsigned char *)data;
*file_start_addr = *file_length = 0;
if (result == OSAL_OK) {
int found = 0;
u_int32_t start_addr, length;
const unsigned char *p = buffer;
do {
int len_dr;
u_int32_t name_len;
const unsigned char *start = p;
len_dr = *p++;
if (len_dr == 0 ||
p > buffer + 2047)
break; /* last entry */
p++;
start_addr = ((u_int32_t)p[3] << 24 |
(u_int32_t)p[2] << 16 |
(u_int32_t)p[1] << 8 |
(u_int32_t)p[0] << 0);
p += 8;
length = ((u_int32_t)p[3] << 24 |
(u_int32_t)p[2] << 16 |
(u_int32_t)p[1] << 8 |
(u_int32_t)p[0] << 0);
p += 8;
p += 7; /* recording date/time */
++p;
++p;
++p;
p += 4; /* flags, unit size, intereave gap size, volume seq no */
name_len = *p++;
if ((name_len == file_name_len) && (memcmp(p, file_name, file_name_len) == 0)) { /* found */
found = 1;
break;
} else
p = start + len_dr; /* continue to the next file */
} while (1);
if (found) {
*file_start_addr = (u_int64_t)start_addr * CDVD_SECT_SIZE;
*file_length = length;
} else
result = RET_NOT_FOUND;
}
return (result);
}
/**************************************************************/
static int
parse_config_cnf(const char *contents,
u_int32_t length,
/*@out@*/ char signature[12 + 1])
{
/* when using memory-mapped I/O buffer is read-only */
char buf[131072];
int found = 0;
char *line = buf;
const char *end = buf + length;
*signature = '\0';
if (length <= sizeof(buf))
memcpy(buf, contents, length);
else
return (RET_NOT_PS_CDVD);
do {
char *p = line, *start = line;
while (*p != '\r' && *p != '\n' && p < end && *p != '\0')
++p;
*p = '\0';
line = p + 1;
/* expected format: "BOOT2 = cdrom0:\SCES_xxx.xx;1\r\n" */
p = start;
if (memcmp(p, "BOOT2", 5) == 0) {
p += 5;
while (*p == ' ' || *p == '\t' || *p == '=')
++p;
if ((memcmp(p, "cdrom0:\\", 8) == 0) || (memcmp(p, "cdrom1:\\", 8) == 0)) { /*cdrom1 is used by some game mods*/
p += 8;
while (*p != ';')
*signature++ = *p++;
*signature = '\0';
found = 1;
break;
}
}
} while (line < end);
return (found ? OSAL_OK : RET_NOT_PS_CDVD /* perhaps PSOne CD-ROM */);
}
/**************************************************************/
static int
isofs_parse_config_cnf(iin_t *iin,
u_int64_t file_start_addr,
u_int64_t file_length,
/*@out@*/ char signature[12 + 1])
{
u_int32_t len;
const char *data;
int result = iin->read(iin, (u_int32_t)(file_start_addr / IIN_SECTOR_SIZE),
1, &data, &len);
*signature = '\0';
if (result == OSAL_OK)
result = parse_config_cnf(data, (u_int32_t)file_length, signature);
return (result);
}
/**************************************************************/
static int
isofs_detect_media_type(iin_t *iin,
/*@out@*/ ps2_cdvd_info_t *info)
{
const char *buf = NULL;
u_int32_t len = 0;
int result = iin->read(iin, 16, 1, &buf, &len);
if (result == OSAL_OK && len == CDVD_SECT_SIZE) {
if (memcmp(buf + 1024, "CD-XA001", 8) == 0)
info->media_type = mt_cd;
else if (memcmp(buf + 1024, "\0\0\0\0\0\0\0\0", 8) == 0)
info->media_type = mt_dvd;
else
info->media_type = mt_unknown;
}
return (result);
}
/**************************************************************/
int isofs_get_ps2_cdvd_info(iin_t *iin,
ps2_cdvd_info_t *info)
{
u_int64_t pvd_start_addr, ptr_start_addr, root_start_addr;
u_int64_t system_cnf_start_addr, system_cnf_length;
char system_id[32 + 1];
int result;
result = isofs_detect_media_type(iin, info);
if (result == OSAL_OK)
result = isofs_find_pvd_addr(iin, &pvd_start_addr, &ptr_start_addr,
system_id, info->volume_id, 0);
if (result == OSAL_OK)
if (strcmp(system_id, "PLAYSTATION") != 0)
result = RET_NOT_PS_CDVD;
if (result == OSAL_OK)
result = isofs_get_root_addr(iin, ptr_start_addr, &root_start_addr);
if (result == OSAL_OK)
result = isofs_get_file_addr(iin, root_start_addr, "SYSTEM.CNF;1",
&system_cnf_start_addr, &system_cnf_length);
if (result == OSAL_OK)
result = isofs_parse_config_cnf(iin, system_cnf_start_addr,
system_cnf_length, info->startup_elf);
if (result == OSAL_OK) {
char tmp_volume_id[32 + 1];
if (isofs_find_pvd_addr(iin, &pvd_start_addr, &ptr_start_addr,
system_id, tmp_volume_id, 1) == OSAL_OK)
info->layer_pvd = pvd_start_addr / CDVD_SECT_SIZE;
else
info->layer_pvd = 0;
}
return (result);
}