forked from apxii/rkboottools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rk-makebootable.c
183 lines (154 loc) · 4.08 KB
/
rk-makebootable.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 <string.h>
#include <sys/types.h>
#include <unistd.h>
struct bootinfo {
uint32_t flag;
uint32_t zero1;
uint32_t zero2;
uint16_t unk1;
uint16_t unk2;
uint8_t zeros [0x1EA];
uint16_t stage1length;
uint16_t stage2length;
uint16_t zero;
} __attribute__((packed));
/* 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];
}
}
static int rc4_file(int fdin,int fdout,int len)
{
int rlen,wlen,restlen;
char bu[0x200],buo[0x200];
rlen=lseek(fdin,0,SEEK_END);
lseek(fdin,0,SEEK_SET);
restlen=len;
while(rlen){
if(rlen>0x200)
wlen=0x200;
else
wlen=rlen;
memset(bu,0,0x200);
read(fdin,bu,wlen);
rk_rc4(bu,buo,0x200);
write(fdout,buo,0x200);
rlen-=wlen;
restlen--;
}
while(restlen--){
memset(buo,0,0x200);
write(fdout,buo,0x200);
}
return 0;
}
int main(int argc,char *argv[])
{
int fd1,fd2,fdout;
size_t flen;
struct bootinfo bi;
char buf[0x200];
if(argc<3){
printf("Rockchip SD Boot image maker\nUsage:\n %s <Stage1> <Stage2> <Output>\n",argv[0]);
return 1;
}
memset(&bi,0,sizeof(bi));
bi.flag=0x0FF0AA55; /* Boot flag */
bi.unk1=4; /* Unknown value == 4 */
bi.unk2=4; /* Unknown value == 4 */
fdout=open(argv[3],O_WRONLY|O_CREAT,0664);
if(fdout==-1){
perror("Can't open Output file");
return 1;
}
fd1=open(argv[1],O_RDONLY);
if(fd1==-1){
perror("Can't open Stage1 file");
return 1;
}
fd2=open(argv[2],O_RDONLY);
if(fd2==-1){
perror("Can't open Stage2 file");
return 1;
}
flen=lseek(fd1,0,SEEK_END);
bi.stage1length=flen/0x200;
if(flen%0x200)
bi.stage1length++;
/* Align Stage1 length to be a multiple of 4 sectors, otherwise it won't boot. */
bi.stage1length=(bi.stage1length+3)&~3;
flen=lseek(fd2,0,SEEK_END);
bi.stage2length=flen/0x200;
if(flen%0x200)
bi.stage2length++;
bi.stage2length+=bi.stage1length+1;
rk_rc4((char *)&bi,buf,0x200);
write(fdout,buf,sizeof(bi));
/*
Pad 3*0x200 bytes so stage1 code will be placed at 0x8800 on SD card
*/
memset(buf,0,0x200);
write(fdout,buf,0x200);
write(fdout,buf,0x200);
write(fdout,buf,0x200);
rc4_file(fd1,fdout,bi.stage1length);
rc4_file(fd2,fdout,bi.stage2length);
close(fdout);
close(fd1);
close(fd2);
printf("RK boot image written,\n Stage1 at 0x8800, length=0x%04X\n Stage2 at 0x%04X, length=0x%04X\n",
bi.stage1length,bi.stage1length*0x200+0x8800,bi.stage2length);
return 0;
}