forked from oct0xor/unpacking_devolution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum.c
341 lines (328 loc) · 8.92 KB
/
checksum.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
#include <assert.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Program to bruteforce Devolution's anti-tamper checksum by fiddling the 4-5 bytes at the end of it
// As far as I can tell I've never needed more than 34 bits to bruteforce the checksum
// Set N_THREADS to the number of threads you want this to use
typedef union {
struct __attribute__((packed)) {
uint16_t cpu_to_dsp_h; // 0x400
uint16_t cpu_to_dsp_l; // 0x401
uint16_t unk_0; // 0x402
uint16_t unlock_len; // 0x403
uint16_t aram_addr_h; // 0x404
uint16_t aram_addr_l; // 0x405
uint16_t dsp_to_cpu_h; // 0x406
uint16_t dsp_to_cpu_l; // 0x407
};
uint16_t work[0x12];
} data_u;
typedef struct {
uint8_t *buffer;
uint32_t addr;
uint32_t size;
uint16_t gain;
uint16_t a1;
uint16_t a2;
uint16_t yn1;
uint16_t yn2;
} accel_s;
void card4_subfunc(data_u *data, uint32_t acc0)
{
uint32_t acc1;
acc0 = acc0 + (data->work[0x0e] << 16 | data->work[0x0f]);
acc1 = (acc0 & 0xFFFF) << 16;
acc1 ^= data->work[0x0d] << 16;
acc1 >>= 16;
acc1 = (acc0 & 0xFFFF0000) | (acc1 & 0xFFFF);
acc1 ^= data->work[0x0c] << 16; acc0 = (acc0 & 0xFFFF0000) | (data->work[0x11]);
acc0++;
data->work[0x11] = acc0 & 0xFFFF;
acc0 += data->work[0x10];
acc0 &= 0x1f; acc0 <<= 16;
uint16_t shift0 = acc0 >> 16;
acc0 = ((acc0 & 0xFFFF0000) + (int32_t)((uint32_t)-0x20 << 16)) | (acc0 & 0xFFFF);
uint16_t shift1 = acc0 >> 16; acc0 = acc1;
if (shift0 & 64)
{
if (shift0 & 63)
{
uint16_t true_shift = 64 - (shift0 & 0x3f);
if (true_shift >= 32)
{
acc0 = 0;
}
else
{
acc0 >>= true_shift;
}
}
}
else
{
uint16_t true_shift = shift0 & 0x3f;
if (true_shift >= 32)
{
acc0 = 0;
}
else
{
acc0 <<= shift0 & 0x3f;
}
}
if (shift1 & 64)
{
if (shift1 & 63)
{
uint16_t true_shift = 64 - (shift1 & 0x3f);
if (true_shift >= 32)
{
acc1 = 0;
}
else
{
acc1 >>= true_shift;
}
}
}
else
{
uint16_t true_shift = shift1 & 0x3f;
if (true_shift >= 32)
{
acc1 = 0;
}
else
{
acc1 <<= shift1 & 0x3f;
}
}
acc0 += acc1;
acc0 = acc0 + (data->work[0x0a] << 16 | data->work[0x0b]);
data->work[0x0b] = acc0 & 0xFFFF;
data->work[0x0a] = acc0 >> 16;
acc0 ^= data->work[0x08] << 16;
acc0 ^= data->work[0x0e] << 16;
data->work[0x0c] = acc0 >> 16; acc0 <<= 16;
acc0 ^= data->work[0x09] << 16;
acc0 ^= data->work[0x0f] << 16;
data->work[0x0d] = acc0 >> 16;
acc1 = ((~data->work[0x09] & 0xFFFF) << 16) | (acc1 & 0xFFFF);
acc1 &= (acc0 & 0xFFFF0000) | 0xFFFF;
acc0 = (data->work[0x09] << 16) | (acc0 & 0xFFFF);
acc0 &= (data->work[0x0b] << 16) | 0xFFFF;
acc0 |= acc1 & 0xFFFF0000; acc1 = ((~data->work[0x08] & 0xFFFF) << 16) | (acc1 & 0xFFFF);
acc0 >>= 16;
acc1 &= (data->work[0x0c] << 16) | 0xFFFF; acc0 = (data->work[0x08] << 16) | (acc0 & 0xFFFF);
acc0 &= (data->work[0x0a] << 16) | 0xFFFF;
acc0 |= (acc1 & 0xFFFF0000); acc1 = (data->work[0x0e] << 16) | (acc1 & 0xFFFF);
acc1 = (acc1 & 0xFFFF0000) | (data->work[0x0f]);
acc0 += acc1;
data->work[0x0e] = (acc0 >> 16) & 0xFFFF;
data->work[0x0f] = acc0 & 0xFFFF;
}
uint16_t read_accel(accel_s *accel)
{
uint16_t data;
if (accel->addr & 1)
{
data = accel->buffer[accel->addr/2] & 0xF;
}
else
{
data = accel->buffer[accel->addr/2] >> 4;
}
accel->addr++;
data = (data * accel->gain + accel->yn1 * accel->a1 + accel->yn2 * accel->a2);
accel->yn2 = accel->yn1;
accel->yn1 = data;
return data;
}
void card4_part3(data_u *data, accel_s *accel, uint32_t bytes_to_read)
{
uint32_t new = ((data->work[0x08] << 16) | data->work[0x09]) + ((read_accel(accel) << 16) | read_accel(accel));
data->work[0x08] = new >> 16;
data->work[0x09] = new & 0xFFFF;
for (int i = 0; i < bytes_to_read - 1; i++)
{
card4_subfunc(data, new);
new = ((data->work[0x08] << 16) | data->work[0x09]) + ((read_accel(accel) << 16) | read_accel(accel));
data->work[0x08] = new >> 16;
data->work[0x09] = new & 0xFFFF;
}
card4_subfunc(data, new);
}
void card4_part2(data_u *data, accel_s *accel, uint32_t acc0, uint16_t byte_len)
{
uint32_t acc1 = acc0;
acc0 = (int32_t)acc0 + (int32_t)0x4ea21e71;
data->work[0x08] = acc0 >> 16;
data->work[0x09] = acc0 & 0xFFFF;
data->work[0x0a] = 0xcc0a;
data->work[0x0b] = 0x144b;
data->work[0x0c] = 0xf541;
data->work[0x0d] = 0x878d;
data->work[0x0e] = 0xa3bc;
acc1 += 3; data->work[0x0f] = 0x64e4;
data->work[0x10] = acc1 & 0xFFFF;
data->work[0x11] = 0;
accel->addr = 0;
accel->gain = 0xfc82;
accel->a1 = 0x978;
accel->a2 = 0xe541;
accel->yn1 = 0x0000;
accel->yn2 = 0x0000;
card4_part3(data, accel, byte_len);
}
typedef struct {
uint64_t id;
uint32_t n;
uint32_t bytes;
uint32_t payload_len;
data_u *data_bak;
accel_s *accel_bak;
} parallel_info;
atomic_bool found = ATOMIC_VAR_INIT(false);
uint8_t patch_buf[8];
void* bruteforce_func(void* usrdata)
{
data_u ldata;
accel_s laccel;
parallel_info *info = (parallel_info*)usrdata;
uint8_t dumb_buf[8];
uint32_t magic_other_half = 0x735f0000 + info->payload_len;
for (uint64_t val = info->id; val < (1ULL << info->bytes * 8); val += info->n)
{
memcpy(&ldata, info->data_bak, sizeof(data_u));
memcpy(&laccel, info->accel_bak, sizeof(accel_s));
laccel.buffer = &dumb_buf[0];
laccel.addr = 0;
for (int o = 0; o < info->bytes; o++)
{
laccel.buffer[o] = val >> ((info->bytes*8) - ((o+1)*8));
}
card4_part3(&ldata, &laccel, info->bytes);
uint32_t checksum = (ldata.work[0x0a] << 16) | ldata.work[0x0b];
// DSP check on the left (lower 16-bits must be 0xFFFF), PPC check on the right
if (((checksum + info->payload_len) & 0xFFFF) == 0xFFFF && (checksum ^ magic_other_half) == 0x7FFFFFFF)
{
printf("Magic value is %d bytes: 0x%08lx (0x%08x 0x%08x)\n", info->bytes, val, info->payload_len, checksum + info->payload_len);
found = true;
memcpy(&patch_buf[8-info->bytes], &dumb_buf[0], 8);
break;
}
if (found)
{
break;
}
}
return NULL;
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf ("checksum requires one file argument\n");
return 1;
}
FILE *ppc_payload = fopen(argv[1], "rb");
if (ppc_payload == NULL)
{
printf("Unable to open payload %s\n", argv[1]);
return 2;
}
fseek(ppc_payload, 0L, SEEK_END);
uint32_t payload_len = ftell(ppc_payload);
rewind(ppc_payload);
data_u data;
accel_s accel;
accel.buffer = malloc(payload_len);
uint64_t jeff;
if (fread(accel.buffer, payload_len, 1, ppc_payload) != 1)
{
printf("Didn't read enough bytes!\n");
return 3;
}
fclose(ppc_payload);
payload_len = 0x30974;
card4_part2(&data, &accel, 0, payload_len % 0x10000);
for (int i = 0; i < payload_len / 0x10000 - 1; i++)
{
card4_part3(&data, &accel, 0x10000);
}
card4_part3(&data, &accel, 0x10000-5);
accel.buffer[0x3096F] = 0xFF; // Try to bruteforce with this byte as 0xFF first
data_u data_bak_5;
accel_s accel_bak_5;
memcpy(&data_bak_5, &data, sizeof(data_u));
memcpy(&accel_bak_5, &accel, sizeof(accel_s));
card4_part3(&data, &accel, 1);
data_u data_bak_4;
accel_s accel_bak_4;
memcpy(&data_bak_4, &data, sizeof(data_u));
memcpy(&accel_bak_4, &accel, sizeof(accel_s));
// Bruteforce the checksum
#ifndef N_THREADS
#define N_THREADS 8
#endif
memset(&patch_buf[0], 0xFF, 8);
uint8_t n_bytes = 4;
pthread_t threads[N_THREADS];
parallel_info info[N_THREADS];
for (int i = 0; i < N_THREADS; i++)
{
info[i].id = i;
info[i].n = N_THREADS;
info[i].bytes = n_bytes;
info[i].payload_len = payload_len;
info[i].data_bak = &data_bak_4;
info[i].accel_bak = &accel_bak_4;
pthread_create(&threads[i], NULL, bruteforce_func, &info[i]);
}
for (int i = 0; i < N_THREADS; i++)
{
pthread_join(threads[i], NULL);
}
if (!found) {
n_bytes = 5;
printf("Doing 5-byte bruteforce. This will take a while.\n");
pthread_t threads[N_THREADS];
parallel_info info[N_THREADS];
for (int i = 0; i < N_THREADS; i++)
{
info[i].id = i;
info[i].n = N_THREADS;
info[i].bytes = n_bytes;
info[i].payload_len = payload_len;
info[i].data_bak = &data_bak_5;
info[i].accel_bak = &accel_bak_5;
pthread_create(&threads[i], NULL, bruteforce_func, &info[i]);
}
for (int i = 0; i < N_THREADS; i++)
{
pthread_join(threads[i], NULL);
}
}
if (!found) {
printf("Warning: could not find checksum!\n");
}
else
{
FILE *ppc_payload = fopen(argv[1], "r+b");
if (ppc_payload == NULL)
{
printf("Unable to open payload %s for writing\n", argv[1]);
return 4;
}
fseek(ppc_payload, 0x30974 - 8, SEEK_SET);
fwrite(&patch_buf[0], 8, 1, ppc_payload);
fclose(ppc_payload);
}
free(accel.buffer);
return 0;
}