-
Notifications
You must be signed in to change notification settings - Fork 4
/
rk-splitboot.c
183 lines (161 loc) · 3.95 KB
/
rk-splitboot.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
/*
* Based on code that is
* Copyright (c) Rockbox authors.
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/types.h>
#include <unistd.h>
#include <inttypes.h>
struct bootloader_header {
uint8_t magic[4];
uint16_t head_len;
uint16_t version;
uint16_t unknown1;
uint16_t build_year;
uint8_t build_month;
uint8_t build_day;
uint8_t build_hour;
uint8_t build_minute;
uint8_t build_second;
/* 104 (0x68) bytes */
uint32_t chip;
} __attribute__ ((packed));
struct file_header {
uint8_t unk1;
uint32_t num;
uint16_t uname[20];
uint32_t offset;
uint32_t len;
uint32_t unk2;
} __attribute__ ((packed));
char *uni_to_onebyte_dumb(unsigned short str[])
{
static char re[20];
int i;
for(i=0;i<20;i++)
re[i]=str[i];
return re;
}
/* scrambling/descrambling reverse engineered by AleMaxx */
static void rk_rc4(char *inpg, char *outpg, const int size)
{
char key[] = {
0x7C, 0x4E, 0x03, 0x04,
0x55, 0x05, 0x09, 0x07,
0x2D, 0x2C, 0x7B, 0x38,
0x17, 0x0D, 0x17, 0x11
};
int i, i3, x, val, idx;
char key1[0x100];
char key2[0x100];
for (i=0; i<0x100; i++) {
key1[i] = i;
key2[i] = key[i&0xf];
}
i3 = 0;
for (i=0; i<0x100; i++) {
x = key1[i];
i3 = key1[i] + i3;
i3 += key2[i];
i3 &= 0xff;
key1[i] = key1[i3];
key1[i3] = x;
}
idx = 0;
for (i=0; i<size; i++) {
x = key1[(i+1) & 0xff];
val = x;
idx = (x + idx) & 0xff;
key1[(i+1) & 0xff] = key1[idx];
key1[idx] = (x & 0xff);
val = (key1[(i+1)&0xff] + x) & 0xff;
val = key1[val];
outpg[i] = val ^ inpg[i];
}
}
int decode_file(int fdin,struct file_header *fh)
{
int fdout;
int rlen,wlen;
char bu[0x200],buo[0x200];
fdout=open(uni_to_onebyte_dumb(fh->uname),O_WRONLY|O_CREAT,0664);
lseek(fdin,fh->offset,SEEK_SET);
rlen=fh->len;
while(rlen){
if(rlen>0x200)
wlen=0x200;
else
wlen=rlen;
read(fdin,bu,wlen);
rk_rc4(bu,buo,wlen);
write(fdout,buo,wlen);
rlen-=wlen;
}
close(fdout);
}
int main(int argc,char *argv[])
{
struct bootloader_header bh;
struct file_header fh[4];
int fdin;
int i;
if(argc<2)
return 1;
fdin=open(argv[1],O_RDONLY);
read(fdin,&bh,sizeof(bh));
printf("Header info:\n"
"Magic:\t\t%c%c%c%c\n"
"Header length:\t%X\n"
"Version:\t%u\n"
"Unknown:\t%08"PRIX32"\n"
"Date:\t\t%u-%u-%u %u:%u:%u\n"
"Chip:\t\t%08"PRIX32"\n"
,bh.magic[0],bh.magic[1],bh.magic[2],bh.magic[3]
,bh.head_len
,bh.version
,bh.unknown1
,bh.build_year
,bh.build_month
,bh.build_day
,bh.build_hour
,bh.build_minute
,bh.build_second
,bh.chip
);
lseek(fdin,bh.head_len,SEEK_SET);
read(fdin,&fh,sizeof(fh));
for(i=0;i<4;i++){
printf("\nFile info:\n"
"Unknown1:\t\t%u\n"
"Num:\t\t%u\n"
"Name:\t\t%s\n"
"Offset:\t\t%08"PRIX32"\n"
"Length:\t\t%08"PRIX32"\n"
"Unknown2:\t%08"PRIX32"\n"
,fh[i].unk1
,fh[i].num
,uni_to_onebyte_dumb(fh[i].uname)
,fh[i].offset
,fh[i].len
,fh[i].unk2
);
decode_file(fdin,&fh[i]);
}
close(fdin);
return 0;
}