-
Notifications
You must be signed in to change notification settings - Fork 24
/
unself.c
370 lines (299 loc) · 7.58 KB
/
unself.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
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
// Copyright 2010 Sven Peter <[email protected]>
// Licensed under the terms of the GNU GPL, version 2
// http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
#include "tools.h"
#include "types.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#define MAX_SECTIONS 255
static u8 *self = NULL;
static u8 *elf = NULL;
static FILE *out = NULL;
static u64 info_offset;
static u32 key_ver;
static u64 phdr_offset;
static u64 shdr_offset;
static u64 sec_offset;
static u64 ver_offset;
static u64 version;
static u64 elf_offset;
static u64 meta_offset;
static u64 header_len;
static u64 filesize;
static u32 arch64;
static u32 n_sections;
static struct elf_hdr ehdr;
static u32 app_type;
static struct {
u32 offset;
u32 size;
u32 compressed;
u32 size_uncompressed;
u32 elf_offset;
} self_sections[MAX_SECTIONS];
static void read_header(void)
{
key_ver = be16(self + 0x08);
meta_offset = be32(self + 0x0c);
header_len = be64(self + 0x10);
filesize = be64(self + 0x18);
info_offset = be64(self + 0x28);
elf_offset = be64(self + 0x30);
phdr_offset = be64(self + 0x38) - elf_offset;
shdr_offset = be64(self + 0x40) - elf_offset;
sec_offset = be64(self + 0x48);
ver_offset = be64(self + 0x50);
version = be64(self + info_offset + 0x10);
app_type = be32(self + info_offset + 0x0c);
elf = self + elf_offset;
arch64 = elf_read_hdr(elf, &ehdr);
}
struct self_sec {
u32 idx;
u64 offset;
u64 size;
u32 compressed;
u32 encrypted;
u64 next;
};
static void read_section(u32 i, struct self_sec *sec)
{
u8 *ptr;
ptr = self + sec_offset + i*0x20;
sec->idx = i;
sec->offset = be64(ptr + 0x00);
sec->size = be64(ptr + 0x08);
sec->compressed = be32(ptr + 0x10) == 2 ? 1 : 0;
sec->encrypted = be32(ptr + 0x20);
sec->next = be64(ptr + 0x20);
}
static int qsort_compare(const void *a, const void *b)
{
const struct self_sec *sa, *sb;
sa = a;
sb = b;
if (sa->offset > sb->offset)
return 1;
else if(sa->offset < sb->offset)
return -1;
else
return 0;
}
static void read_sections(void)
{
struct self_sec s[MAX_SECTIONS];
struct elf_phdr p;
u32 i;
u32 j;
u32 n_secs;
u32 self_offset, elf_offset;
memset(s, 0, sizeof s);
for (i = 0, j = 0; i < ehdr.e_phnum; i++) {
read_section(i, &s[j]);
if (s[j].compressed)
j++;
}
n_secs = j;
qsort(s, n_secs, sizeof(*s), qsort_compare);
elf_offset = 0;
self_offset = header_len;
j = 0;
i = 0;
while (elf_offset < filesize) {
if (i == n_secs) {
self_sections[j].offset = self_offset;
self_sections[j].size = filesize - elf_offset;
self_sections[j].compressed = 0;
self_sections[j].size_uncompressed = filesize - elf_offset;
self_sections[j].elf_offset = elf_offset;
elf_offset = filesize;
} else if (self_offset == s[i].offset) {
self_sections[j].offset = self_offset;
self_sections[j].size = s[i].size;
self_sections[j].compressed = 1;
elf_read_phdr(arch64, elf + phdr_offset +
(ehdr.e_phentsize * s[i].idx), &p);
self_sections[j].size_uncompressed = p.p_filesz;
self_sections[j].elf_offset = p.p_off;
elf_offset = p.p_off + p.p_filesz;
self_offset = s[i].next;
i++;
} else {
elf_read_phdr(arch64, elf + phdr_offset +
(ehdr.e_phentsize * s[i].idx), &p);
self_sections[j].offset = self_offset;
self_sections[j].size = p.p_off - elf_offset;
self_sections[j].compressed = 0;
self_sections[j].size_uncompressed = self_sections[j].size;
self_sections[j].elf_offset = elf_offset;
elf_offset += self_sections[j].size;
self_offset += s[i].offset - self_offset;
}
j++;
}
n_sections = j;
}
static void write_elf(void)
{
u32 i;
u8 *bfr;
u32 size;
u32 offset = 0;
for (i = 0; i < n_sections; i++) {
fseek(out, self_sections[i].elf_offset, SEEK_SET);
offset = self_sections[i].elf_offset;
if (self_sections[i].compressed) {
size = self_sections[i].size_uncompressed;
bfr = malloc(size);
if (bfr == NULL)
fail("failed to allocate %d bytes", size);
offset += size;
decompress(self + self_sections[i].offset,
self_sections[i].size,
bfr, size);
fwrite(bfr, size, 1, out);
free(bfr);
} else {
bfr = self + self_sections[i].offset;
size = self_sections[i].size;
offset += size;
fwrite(bfr, size, 1, out);
}
}
}
static void remove_shnames(u64 shdr_offset, u16 n_shdr, u64 shstrtab_offset, u32 strtab_size)
{
u16 i;
u32 size;
struct elf_shdr s;
if (arch64)
size = 0x40;
else
size = 0x28;
for (i = 0; i < n_shdr; i++) {
elf_read_shdr(arch64, elf + shdr_offset + i * size, &s);
s.sh_name = 0;
if (s.sh_type == 3) {
s.sh_offset = shstrtab_offset;
s.sh_size = strtab_size;
}
elf_write_shdr(arch64, elf + shdr_offset + i * size, &s);
}
}
static void check_elf(void)
{
u8 bfr[0x48];
u64 elf_offset;
u64 phdr_offset;
u64 shdr_offset;
u64 phdr_offset_new;
u64 shdr_offset_new;
u64 shstrtab_offset;
u16 n_phdr;
u16 n_shdr;
const char shstrtab[] = ".unknown\0\0";
const char elf_magic[4] = {0x7f, 'E', 'L', 'F'};
fseek(out, 0, SEEK_SET);
fread(bfr, 4, 1, out);
if (memcmp(bfr, elf_magic, sizeof elf_magic) == 0)
return;
elf_offset = be64(self + 0x30);
phdr_offset = be64(self + 0x38) - elf_offset;
shdr_offset = be64(self + 0x40) - elf_offset;
if (arch64) {
fseek(out, 0x48, SEEK_SET);
phdr_offset_new = 0x48;
fseek(out, 0, SEEK_END);
shdr_offset_new = ftell(out);
n_phdr = be16(elf + 0x38);
n_shdr = be16(elf + 0x3c);
shstrtab_offset = shdr_offset_new + n_shdr * 0x40;
remove_shnames(shdr_offset, n_shdr, shstrtab_offset, sizeof shstrtab);
fseek(out, phdr_offset_new, SEEK_SET);
fwrite(elf + phdr_offset, 0x38, n_phdr, out);
fseek(out, shdr_offset_new, SEEK_SET);
fwrite(elf + shdr_offset, 0x40, n_shdr, out);
wbe64(elf + 0x20, phdr_offset_new);
wbe64(elf + 0x28, shdr_offset_new);
fseek(out, SEEK_SET, 0);
fwrite(elf, 0x48, 1, out);
fseek(out, shstrtab_offset, SEEK_SET);
fwrite(shstrtab, sizeof shstrtab, 1, out);
} else {
fseek(out, 0x34, SEEK_SET);
phdr_offset_new = 0x34;
fseek(out, 0, SEEK_END);
shdr_offset_new = ftell(out);
n_phdr = be16(elf + 0x2c);
n_shdr = be16(elf + 0x30);
shstrtab_offset = shdr_offset_new + n_shdr * 0x40;
remove_shnames(shdr_offset, n_shdr, shstrtab_offset, sizeof shstrtab);
fseek(out, phdr_offset_new, SEEK_SET);
fwrite(elf + phdr_offset, 0x20, n_phdr, out);
fseek(out, shdr_offset_new, SEEK_SET);
fwrite(elf + shdr_offset, 0x28, n_shdr, out);
wbe32(elf + 0x1c, phdr_offset_new);
wbe32(elf + 0x20, shdr_offset_new);
fseek(out, SEEK_SET, 0);
fwrite(elf, 0x34, 1, out);
fseek(out, shstrtab_offset, SEEK_SET);
fwrite(shstrtab, sizeof shstrtab, 1, out);
}
}
static struct keylist *self_load_keys(void)
{
enum sce_key id;
switch (app_type) {
case 1:
id = KEY_LV0;
break;
case 2:
id = KEY_LV1;
break;
case 3:
id = KEY_LV2;
break;
case 4:
id = KEY_APP;
break;
case 5:
id = KEY_ISO;
break;
case 6:
id = KEY_LDR;
break;
default:
fail("invalid type: %08x", app_type);
}
return keys_get(id);
}
static void self_decrypt(void)
{
struct keylist *klist;
klist = self_load_keys();
if (klist == NULL)
fail("no key found");
if (sce_decrypt_header(self, klist) < 0)
fail("self_decrypt_header failed");
if (sce_decrypt_data(self) < 0)
fail("self_decrypt_data failed");
}
int main(int argc, char *argv[])
{
if (argc != 3)
fail("usage: unself in.self out.elf");
self = mmap_file(argv[1]);
if (be32(self) != 0x53434500)
fail("not a SELF");
read_header();
read_sections();
if (key_ver != 0x8000)
self_decrypt();
out = fopen(argv[2], "w+");
write_elf();
check_elf();
fclose(out);
return 0;
}