forked from psgroove/psgroove
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpayload_apploader.S
383 lines (356 loc) · 7.75 KB
/
payload_apploader.S
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
// Thank you ***** :)
#include "payload_symbols.S"
/* Addressing Macros */
// Payload bases
#define BASE1 (0x700080 - 0x80)
#define BASE2 (0x50BD4 - 0x244)
// Absolute branching
#define ABS(base, target) ((target) - (base) - .)
#define ABS1(target) ABS(BASE1, (target))
#define ABS2(target) ABS(BASE2, (target))
// Dynamic macros to load a label into a register
#define MEM_BASE(dest) \
li dest, 1; \
rldicr dest, dest, 63, 0;
#define LOAD_LABEL(base, dest, source, address) \
oris dest, source, ((base) + (address))@h; \
ori dest, dest, ((base) + (address))@l;
#define LOAD_LABEL1(dest, source, address) LOAD_LABEL(BASE1, dest, source, address)
#define LOAD_LABEL2(dest, source, address) LOAD_LABEL(BASE2, dest, source, address)
#define LOADI_LABEL1(dest, address) LOAD_LABEL(BASE1, dest, dest, address)
#define LOADI_LABEL2(dest, address) LOAD_LABEL(BASE2, dest, dest, address)
// For loading an absolute value
#define LOAD_ABS(dest, source, address) LOAD_LABEL(0, dest, source, address)
#define LOADI_ABS(dest, address) LOAD_ABS(dest, dest, address)
// Absolute .quads
#define QUAD_ABS(base, address) .long 0x80000000; .long base + address
#define QUAD_ABS1(address) QUAD_ABS(BASE1, address)
#define QUAD_ABS2(address) QUAD_ABS(BASE2, address)
/* Patch Table Macros */
#define PATCH_INST(offset, instruction...) .long offset; instruction
#define PATCH_DATA(offset, data...) .long offset; .long data
#define PATCH_BRANCH(offset, op, target) .long offset; op (. + (target - offset))
.org 0
// USB config descriptor
.byte 0x09, 0x02, 0x12, 0x00, 0x01, 0x00, 0x00, 0x80
.byte 0xfa, 0x09, 0x04, 0x00, 0x00, 0x00, 0xfe, 0x01
.byte 0x02, 0x00, 0x00, 0x00
.long 0
.long 0xfaceb003
.long 0xaabbccdd
subi %r3, %r3, 0x1000
li %r5, 0x1000
li %r4, 1
rldicr %r4, %r4, 63, 0
oris %r4, %r4, 0x70
shell_code_copy:
subi %r5, %r5, 8
ldx %r6, %r3, %r5
stdx %r6, %r4, %r5
cmpldi %r5, 0
bne shell_code_copy
addi %r4,%r4, 0x80
mtctr %r4
bctr
.org 0x80
shell_code_entry:
mflr %r0
stdu %r1, -0xa0(%r1)
std %r27, 0x78(%r1)
std %r28, 0x80(%r1)
std %r29, 0x88(%r1)
std %r30, 0x90(%r1)
std %r31, 0x98(%r1)
std %r0, 0xb0(%r1)
MEM_BASE(%r31)
LOAD_LABEL2(%r3, %r31, return1)
LOAD_LABEL1(%r4, %r31, return1)
li %r5, 0x4fa
bl ABS1(memcpy)
LOAD_ABS(%r4, %r31, 0x2eb128)
LOAD_LABEL2(%r3, %r31, syscall6_peek_desc)
std %r3, (0x08*6)(%r4)
LOAD_LABEL2(%r3, %r31, syscall7_poke_desc)
std %r3, (0x08*7)(%r4)
LOAD_LABEL2(%r3, %r31, syscall8_alloc_desc)
std %r3, (0x08*8)(%r4)
LOAD_LABEL2(%r3, %r31, syscall9_exec_desc)
std %r3, (0x08*9)(%r4)
LOAD_LABEL1(%r5, %r31, patch_table)
shell_code_entry_loop:
lwz %r3, 0(%r5)
cmplwi %r3, 0
beq shell_code_entry_return
lwz %r4, 4(%r5)
add %r3, %r3, %r31
stw %r4, 0(%r3)
addi %r5, %r5, 8
b shell_code_entry_loop
shell_code_entry_return:
b zero_payload
stdu %r1, -0xb0(%r1)
mflr %r0
std %r30, 0xa0(%r1)
std %r31, 0xa8(%r1)
std %r29, 0x98(%r1)
std %r0, 0xc0(%r1)
li %r30, 0x7d0
li %r31, 0xc8
b ABS1(0xab04)
patch_table:
PATCH_INST(0x0490e0, ld %r4, 0xf08(%r2))
PATCH_INST(0x0490e4, ld %r3, 0x20(%r28))
PATCH_INST(0x0490e8, std %r3, 0(%r4))
PATCH_BRANCH(0x04f0a8, bl, 0x050b44) // memory_patching_desc
PATCH_BRANCH(0x2aafc8, b, 0x050b48) // vsh_open_hook_desc
PATCH_INST(0x04ed18, li %r4, 0)
PATCH_INST(0x04ed1c, stw %r4, 0(%r3))
PATCH_INST(0x04ed20, blr)
PATCH_DATA(0x3ba890, 0x01000000)
PATCH_INST(0x0505d0, li %r3, 1)
PATCH_INST(0x0505d4, blr)
.long 0
.org 0x1ac
return1:
li %r3, 1
blr
memory_patching_desc:
b memory_patching
vsh_open_hook_desc:
b vsh_open_hook
syscall6_peek_desc:
QUAD_ABS2(syscall6_peek)
syscall7_poke_desc:
QUAD_ABS2(syscall7_poke)
syscall8_alloc_desc:
QUAD_ABS2(syscall8_alloc)
syscall9_exec_desc:
QUAD_ABS2(syscall9_exec)
game_name:
.quad 0
flags:
.long 0
.long 0
.org 0x244
syscall6_peek:
ld %r3, 0x00(%r3)
blr
syscall7_poke:
std %r4, 0x00(%r3)
blr
syscall8_alloc:
li %r4, 0x27
b ABS2(alloc)
syscall9_exec:
mtctr %r3
bctr
vsh_open_hook:
stdu %r1, -0xa0(%r1)
mflr %r0
std %r28, 0x80(%r1)
std %r29, 0x88(%r1)
std %r31, 0x98(%r1)
std %r26, 0x70(%r1)
std %r27, 0x78(%r1)
std %r0, 0xb0(%r1)
mr %r28, %r4
mr %r29, %r3
MEM_BASE(%r31)
# r31 = 0x8000000000000000
# r29 = path
# r28 = r4 (open mode?)
# r27 = gametitle
# r26 = &gametitle
LOAD_LABEL2(%r4, %r31, strAppHome)
li %r5, 9
bl ABS2(strncmp)
cmpldi %r3, 0
bne vsh_open_return
LOAD_LABEL2(%r26, %r31, game_name)
ld %r27, 0x00(%r26)
cmplwi %r27, 0
bne vsh_open_noalloc
li %r4, 0x27
li %r3, 0x800
bl ABS2(alloc)
std %r3, 0x00(%r26)
mr %r27, %r3
LOAD_LABEL2(%r4, %r31, strDevUSB)
bl ABS2(strcpy)
vsh_open_noalloc:
addi %r3, %r27, 11
addi %r4, %r29, 9
bl ABS2(strcpy)
mr %r29, %r27
vsh_open_return:
mr %r3, %r29
mr %r4, %r28
b ABS2(0x2aaff0)
strAppHome:
.string "/app_home"
strDevUSB:
.string "/dev_usb000"
.align 4
memory_patching:
mflr %r0
stdu %r1, -0x1a0(%r1)
std %r27, 0x78(%r1)
std %r28, 0x80(%r1)
std %r29, 0x88(%r1)
std %r30, 0x90(%r1)
std %r31, 0x98(%r1)
std %r0, 0x1b0(%r1)
mr %r29, %r3
mr %r30, %r4
MEM_BASE(%r31)
ld %r28, -0x6a00(%r2)
ld %r28, 0x68(%r28)
ld %r28, 0x18(%r28)
ld %r27, 0xf08(%r2)
ld %r9, 0x18(%r29)
lwz %r9, 0x30(%r9)
rldicl %r9, %r9, 48, 16
cmpwi %r9, 0x29
bne loc_4d4
ld %r4, 0x10(%r28)
rldicr %r5, %r4, 24, 39
rldicl %r5, %r5, 8, 56
cmpwi %r5, 0xff
beq loc_4a8
ori %r4, %r4, 3
std %r4, 0x10(%r28)
li %r3, 6
stw %r3, 0(%r30)
b loc_4b8
loc_4a8:
ori %r4, %r4, 2
std %r4, 0x10(%r28)
li %r3, 0x2c
stw %r3, 0(%r30)
loc_4b8:
lwz %r5, 4(%r28)
ld %r4, 8(%r28)
ld %r3, 0(%r27)
add %r9, %r3, %r5
std %r9, 0(%r27)
bl ABS2(memcpy)
b loc_594
loc_4d4:
mr %r3, %r29
mr %r4, %r30
bl ABS2(0x4e81c)
mr %r29, %r31
LOADI_LABEL2(%r29, flags + 4)
lwz %r3, 0(%r29)
lwz %r5, 4(%r28)
add %r3, %r3, %r5
stw %r3, 0(%r29)
ld %r4, 0x10(%r28)
rldicr %r5, %r4, 24, 39
rldicl %r5, %r5, 8, 56
cmpwi %r5, 0xff
bne loc_594
ld %r3, 0(%r27)
li %r4, 0
li %r6, 0
loc_51c:
add %r7, %r3, %r4
lwz %r5, 0(%r7)
xor %r6, %r6, %r5
addi %r4, %r4, 4
cmpldi %r4, 0x400
bne loc_51c
lwz %r3, 0(%r29)
rldicr %r6, %r6, 32, 31
or %r6, %r6, %r3
li %r3, 0
stw %r3, 0(%r29)
mr %r7, %r31
LOADI_LABEL2(%r7, memory_patch_table)
loc_554:
ld %r3, 0(%r7)
cmpldi %r3, 0
beq loc_594
addi %r7, %r7, 0x10
cmpld %r3, %r6
bne loc_554
ld %r5, -8(%r7)
ld %r7, 0(%r27)
loc_574:
lwz %r3, 0(%r5)
cmplwi %r3, 0
beq loc_594
lwz %r4, 4(%r5)
add %r3, %r3, %r7
stw %r4, 0(%r3)
addi %r5, %r5, 8
b loc_574
loc_594:
li %r3, 0
ld %r27, 0x78(%r1)
ld %r28, 0x80(%r1)
ld %r29, 0x88(%r1)
ld %r30, 0x90(%r1)
ld %r31, 0x98(%r1)
ld %r0, 0x1b0(%r1)
addi %r1, %r1, 0x1a0
mtlr %r0
blr
stdu %r1, -0xb0(%r1)
mflr %r0
std %r30, 0xa0(%r1)
std %r31, 0xa8(%r1)
std %r29, 0x98(%r1)
std %r0, 0xc0(%r1)
li %r30, 0xfa0
li %r31, 0xc8
b ABS2(0xab04)
memory_patch_table:
.quad 0xa0556f3d002cb8fd
QUAD_ABS2(memory_patch_table_1)
.quad 0x8c0a948c000d99b1
QUAD_ABS2(memory_patch_table_3)
.quad 0xa2bc1a5600052adc
QUAD_ABS2(memory_patch_table_4)
.quad 0x6b70280200020017
QUAD_ABS2(memory_patch_table_2)
.quad 0
memory_patch_table_1:
PATCH_INST(0x305354, li %r3, 130)
PATCH_INST(0x5f3fc0, li %r3, 1)
PATCH_INST(0x5f3fc4, blr)
.long 0
memory_patch_table_2:
PATCH_INST(0x2ed0c, li %r29, 1)
.long 0
memory_patch_table_3:
PATCH_DATA(0x0022b888, 0x5f746f6f)
PATCH_DATA(0x0022b88c, 0x6c322e78)
PATCH_DATA(0x0022b890, 0x6d6c2372)
PATCH_DATA(0x0022b894, 0x6f6f7400)
.long 0
memory_patch_table_4:
PATCH_DATA(0x000d68b8, 0x5f746f6f)
PATCH_DATA(0x000d68bc, 0x6c322e78)
PATCH_DATA(0x000d68c0, 0x6d6c2372)
PATCH_DATA(0x000d68c4, 0x6f6f7400)
.long 0
.org 0x6b0
zero_payload:
ld %r27, 0x78(%r1)
ld %r28, 0x80(%r1)
ld %r29, 0x88(%r1)
ld %r30, 0x90(%r1)
ld %r31, 0x98(%r1)
ld %r0, 0xb0(%r1)
addi %r1, %r1, 0xa0
mtlr %r0
MEM_BASE(%r3)
oris %r3, %r3, 0x70
li %r4, 0
li %r5, 0x6e8
b ABS1(memset)
nop
nop
// fill out to 3840 bytes
.org 3840