/
bootcsum.c
executable file
·213 lines (163 loc) · 6.41 KB
/
bootcsum.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
/*
* pseultra/tools/bootcsum/src/bootcsum.c
* PIFrom ipl2 checksum function decomp
*
* (C) pseudophpt 2018
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#define MAGIC 0x95DACFDC
uint64_t calculate_checksum (uint32_t *bcode);
static inline uint64_t checksum_helper (uint64_t op1, uint64_t op2, uint64_t op3);
int main (int argc, char *argv[]) {
// If arguments not adequate
if (argc < 2) {
printf("Usage: bootcsum <rom file> [<expected checksum>]\n");
return -1;
}
FILE* rom_file;
uint32_t rom_buffer[0x1000 / sizeof(uint32_t)];
rom_file = fopen(argv[1], "r");
fread(rom_buffer, sizeof(uint32_t), 0x1000 / sizeof(uint32_t), rom_file);
fclose(rom_file);
// LE to BE
for (int i = 0; i < 0x1000 / sizeof(uint32_t); i++) {
uint32_t le = rom_buffer[i];
uint32_t be = ((le & 0xff) << 24) | ((le & 0xff00) << 8) | ((le & 0xff0000) >> 8) | ((le & 0xff000000) >> 24);
rom_buffer[i] = be;
}
// Verification or calculation
if (argc == 2) {
// Calculation
uint64_t checksum = calculate_checksum(&rom_buffer[0x10]);
printf("0x%llx\n", checksum);
return 0;
}
if (argc == 3) {
// Verification
uint64_t expected_checksum = strtol(argv[2], NULL, 0);
uint64_t checksum = calculate_checksum(&rom_buffer[0x10]);
if (checksum == expected_checksum) {
printf("Correct\n");
return 0;
}
else {
printf("Incorrect:\n");
printf("Expected 0x%llx, got 0x%llx\n", expected_checksum, checksum);
return -1;
}
}
}
/*
* Helper function commonly called during checksum
*/
static inline uint64_t checksum_helper (uint64_t op1, uint64_t op2, uint64_t op3) {
int high_mult;
int low_mult;
if (op2 == 0) {
op2 = op3;
}
low_mult = (op1 * op2) & 0x00000000FFFFFFFF;
high_mult = ((op1 * op2) & 0xFFFFFFFF00000000) >> 32;
if (high_mult - low_mult == 0) {
return low_mult;
}
else return high_mult - low_mult;
}
/*
* Decompiled checksum function
*/
uint64_t calculate_checksum (uint32_t *bcode) {
uint32_t *bcode_inst_ptr = bcode;
uint32_t loop_count = 0;
uint32_t bcode_inst = *bcode_inst_ptr;
uint32_t next_inst;
uint32_t prev_inst;
uint32_t frame [16];
uint32_t sframe [4];
// Calculate magic number
uint32_t magic = MAGIC ^ bcode_inst;
// Generate frame. This is done earlier in IPC2
for (int i = 0; i < 16; i ++) {
frame[i] = magic;
};
// First part of checksum, calculates frame
for (;;) {
/* Loop start, 11E8 - 11FC */
prev_inst = bcode_inst;
bcode_inst = *bcode_inst_ptr;
loop_count ++;
bcode_inst_ptr ++;
next_inst = *(bcode_inst_ptr);
/* Main processing */
frame[0] += checksum_helper(0x3EF - loop_count, bcode_inst, loop_count);
frame[1] = checksum_helper(frame[1], bcode_inst, loop_count);
frame[2] ^= bcode_inst;
frame[3] += checksum_helper(bcode_inst + 5, 0x6c078965, loop_count);
if (prev_inst < bcode_inst) {
frame[9] = checksum_helper(frame[9], bcode_inst, loop_count);
}
else frame[9] += bcode_inst;
frame[4] += ((bcode_inst << (0x20 - (prev_inst & 0x1f))) | (bcode_inst >> (prev_inst & 0x1f)));
frame[7] = checksum_helper(frame[7], ((bcode_inst >> (0x20 - (prev_inst & 0x1f))) | (bcode_inst << (prev_inst & 0x1f))), loop_count);
if (bcode_inst < frame[6]) {
frame[6] = (bcode_inst + loop_count) ^ (frame[3] + frame[6]);
}
else frame[6] = (frame[4] + bcode_inst) ^ frame[6];
frame[5] += (bcode_inst >> (0x20 - (prev_inst >> 27))) | (bcode_inst << (prev_inst >> 27));
frame[8] = checksum_helper(frame[8], (bcode_inst << (0x20 - (prev_inst >> 27))) | (bcode_inst >> (prev_inst >> 27)), loop_count);
if (loop_count == 0x3F0) break;
uint32_t tmp1 = checksum_helper(frame[15], (bcode_inst >> (0x20 - (prev_inst >> 27))) | (bcode_inst << (prev_inst >> 27)), loop_count);
frame[15] = checksum_helper(
tmp1,
(next_inst << (bcode_inst >> 27)) | (next_inst >> (0x20 - (bcode_inst >> 27))),
loop_count
);
uint32_t tmp2 = ((bcode_inst << (0x20 - (prev_inst & 0x1f))) | (bcode_inst >> (prev_inst & 0x1f)));
uint32_t tmp3 = checksum_helper(frame[14], tmp2, loop_count); // v0 at 1384
uint32_t tmp4 = checksum_helper(tmp3, (next_inst >> (bcode_inst & 0x1f)) | (next_inst << (0x20 - (bcode_inst & 0x1f))), loop_count); // v0 at 13a4
frame[14] = tmp4;
frame[13] += ((bcode_inst >> (bcode_inst & 0x1f)) | (bcode_inst << (0x20 - (bcode_inst & 0x1f)))) + ((next_inst >> (next_inst & 0x1f)) | (next_inst << (0x20 - (next_inst & 0x1f))));
frame[10] = checksum_helper(frame[10] + bcode_inst, next_inst, loop_count);
frame[11] = checksum_helper(frame[11] ^ bcode_inst, next_inst, loop_count);
frame[12] += (frame[8] ^ bcode_inst);
}
// Second part, calculates sframe
// Every value in sframe is initialized to frame[0]
for (int i = 0; i < 4; i ++) {
sframe[i] = frame[0];
}
uint32_t *frame_word_ptr = &frame[0];
uint32_t frame_word;
for (uint32_t frame_number = 0; frame_number != 0x10; frame_number ++) {
// Updates
frame_word = *frame_word_ptr;
// Calculations
sframe[0] += ((frame_word << (0x20 - frame_word & 0x1f)) | frame_word >> (frame_word & 0x1f));
if (frame_word < sframe[0]) {
sframe[1] += frame_word;
}
else {
sframe[1] = checksum_helper(sframe[1], frame_word, 0);
}
if (((frame_word & 0x02) >> 1) == (frame_word & 0x01)) {
sframe[2] += frame_word;
}
else {
sframe[2] = checksum_helper(sframe[2], frame_word, frame_number);
}
if (frame_word & 0x01 == 1) {
sframe[3] ^= frame_word;
}
else {
sframe[3] = checksum_helper(sframe[3], frame_word, frame_number);
}
frame_word_ptr ++;
}
// combine sframe into checksum
uint64_t checksum = sframe[2] ^ sframe[3];
checksum |= checksum_helper(sframe[0], sframe[1], 0x10) << 32;
checksum &= 0x0000ffffffffffff;
return checksum;
}