forked from liftoff-sr/rockchip-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
img_maker.cpp
279 lines (219 loc) · 6.61 KB
/
img_maker.cpp
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
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>
#include "rkrom.h"
#include "rkafp.h"
#include "md5.h"
static const char* progname;
/**
* Function import_data
*
* @param infile which file to import
*/
unsigned import_data( const char* infile, void* head, size_t head_len, FILE* fp )
{
unsigned readlen;
char buffer[1024*16];
FILE* in_fp = fopen( infile, "rb" );
if( !in_fp )
goto import_end;
// read in the UPDATE_HEADER to a special place in caller.
readlen = fread( head, 1, head_len, in_fp );
if( readlen )
{
fwrite( head, 1, readlen, fp );
}
unsigned len;
while( (len = fread( buffer, 1, sizeof(buffer), in_fp ) ) != 0 )
{
fwrite( buffer, 1, len, fp );
readlen += len;
}
import_end:
if( in_fp )
fclose( in_fp );
return readlen;
}
void append_md5sum( FILE* fp )
{
MD5_CTX md5_ctx;
char buffer[1024*16] = {0};
MD5_Init( &md5_ctx );
fseek( fp, 0, SEEK_SET );
unsigned len;
while( (len = fread( buffer, 1, sizeof(buffer), fp ) ) != 0 )
{
MD5_Update( &md5_ctx, buffer, len );
}
MD5_Final( (unsigned char*) buffer, &md5_ctx );
for( int i = 0; i < 16; ++i )
{
fprintf( fp, "%02hhx", buffer[i] );
}
}
int pack_rom( int chiptype,
const char* loader_filename,
int majver,
int minver,
int subver,
const char* image_filename,
const char* outfile )
{
time_t nowtime;
struct tm local_time;
RKFW_HEADER rom_hdr;
UPDATE_HEADER rkaf_hdr;
BOOTLOADER_HEADER loader_hdr;
rom_hdr.chip = chiptype;
rom_hdr.version = ROM_VERSION(majver, minver, subver);
switch(chiptype){
case 0x50:
rom_hdr.code = 0x01030000;
break;
case 0x60:
rom_hdr.code = 0x01050000;
break;
case 0x70:
case 0x80:
case 0x33333041:
rom_hdr.code = 0x01060000;
break;
}
nowtime = time( NULL );
localtime_r( &nowtime, &local_time );
rom_hdr.year = local_time.tm_year + 1900;
rom_hdr.month = local_time.tm_mon + 1;
rom_hdr.day = local_time.tm_mday;
rom_hdr.hour = local_time.tm_hour;
rom_hdr.minute = local_time.tm_min;
rom_hdr.second = local_time.tm_sec;
FILE* fp = fopen( outfile, "wb+" );
if( !fp )
{
fprintf( stderr, "Can't open file %s\n, reason: %s\n", outfile, strerror( errno ) );
goto pack_fail;
}
// temporarily fill the file with an incomplete header at its beginning.
if( 1 != fwrite( &rom_hdr, sizeof(rom_hdr), 1, fp ) )
goto pack_fail;
printf( "rom version: %x.%x.%x\n",
(rom_hdr.version >> 24) & 0xFF,
(rom_hdr.version >> 16) & 0xFF,
(rom_hdr.version) & 0xFFFF );
printf( "build time: %d-%02d-%02d %02d:%02d:%02d\n",
rom_hdr.year, rom_hdr.month, rom_hdr.day,
rom_hdr.hour, rom_hdr.minute, rom_hdr.second );
printf( "chip: %x\n", rom_hdr.chip );
fprintf( stderr, "generate image...\n" );
// open the boot loader, typically <uboot's_full_name>.bin
rom_hdr.loader_length = import_data( loader_filename,
&loader_hdr,
sizeof(loader_hdr),
fp );
if( rom_hdr.loader_length < sizeof(loader_hdr) )
{
fprintf( stderr, "boot loader file '%s' is not long enough\n", loader_filename );
goto pack_fail;
}
rom_hdr.image_offset = rom_hdr.loader_offset + rom_hdr.loader_length;
rom_hdr.image_length = import_data( image_filename, &rkaf_hdr, sizeof(rkaf_hdr), fp );
if( rom_hdr.image_length < sizeof(rkaf_hdr) )
{
fprintf( stderr, "invalid rom :\"\%s\"\n", image_filename );
goto pack_fail;
}
rom_hdr.unknown2 = 1;
rom_hdr.system_fstype = 0;
int i;
for( i = 0; i < rkaf_hdr.num_parts; ++i )
{
if( strcmp( rkaf_hdr.parts[i].name, "backup" ) == 0 )
break;
}
if( i < rkaf_hdr.num_parts )
rom_hdr.backup_endpos =
(rkaf_hdr.parts[i].flash_offset + rkaf_hdr.parts[i].flash_size) / 0x800;
else
rom_hdr.backup_endpos = 0;
fseek( fp, 0, SEEK_SET );
if( 1 != fwrite( &rom_hdr, sizeof(rom_hdr), 1, fp ) )
goto pack_fail;
fprintf( stderr, "append md5sum...\n" );
append_md5sum( fp ); // compute checksum on entire output file and append
fclose( fp );
fprintf( stderr, "success!\n" );
return 0;
pack_fail:
if( fp )
fclose( fp );
return -1;
}
void usage()
{
fprintf( stderr, "USAGE:\n"
"\t%s <chiptype> <loader> <major_version> <minor_version> <sub_version> <input_image> <out_image>\n\n"
"Example for an RK32 board:\n"
"\t%s -rk32 Loader.bin 4 4 0 rawimage.img rkimage.img\n"
"\n"
"Options:\n"
"\t<chiptype>: -rk29 | -rk30 | -rk31 | -rk3128 | -rk32 | -rk3368\n",
progname, progname
);
}
int main( int argc, char** argv )
{
progname = strrchr( argv[0], '/' );
if( !progname )
progname = argv[0];
else
++progname;
fprintf( stderr, "%s version: " __DATE__ "\n", progname );
// loader, majorver, minorver, subver, oldimage, newimage
if( argc == 8 )
{
if( strcmp( argv[1], "-rk29" ) == 0 )
{
pack_rom( 0x50, argv[2], atoi( argv[3] ), atoi( argv[4] ),
atoi( argv[5] ), argv[6], argv[7] );
}
else if( strcmp( argv[1], "-rk30" ) == 0 )
{
pack_rom( 0x60, argv[2], atoi( argv[3] ), atoi( argv[4] ),
atoi( argv[5] ), argv[6], argv[7] );
}
else if( strcmp( argv[1], "-rk31" ) == 0 )
{
pack_rom( 0x70, argv[2], atoi( argv[3] ), atoi( argv[4] ),
atoi( argv[5] ), argv[6], argv[7] );
}
else if( strcmp( argv[1], "-rk32" ) == 0 )
{
pack_rom( 0x80, argv[2], atoi( argv[3] ), atoi( argv[4] ),
atoi( argv[5] ), argv[6], argv[7] );
}
else if (strcmp(argv[1], "-rk3128") == 0)
{
pack_rom(0x33313241, argv[2], atoi( argv[3] ), atoi( argv[4] ),
atoi( argv[5] ), argv[6], argv[7] );
}
else if (strcmp(argv[1], "-rk3368") == 0)
{
pack_rom(0x33333041, argv[2], atoi( argv[3] ), atoi( argv[4] ),
atoi( argv[5] ), argv[6], argv[7] );
}
else
{
usage();
return 1;
}
}
else
{
usage();
return 1;
}
return 0;
}