-
Notifications
You must be signed in to change notification settings - Fork 2
/
mergeBin.c
326 lines (268 loc) · 7.72 KB
/
mergeBin.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <sys/stat.h>
#define BUFF_SIZE ( 1024 * 1024 )
uint8_t buff[BUFF_SIZE];
void help( void )
{
printf("mergeBin.exe help:\n \
Author: LiDeLong\n \
cmd format: mergeBin.exe [offset_addr_1(Hex: 0x...)] [File_1] ... [offset_addr_n(Hex: 0x...)] [File_n] [Target File] [Blank-filled data(Hex: 0x...)]\n \
example1: merge.exe 0x00001000 RSL.bin 0x00000000 Boot.bin flash.bin 0x00\n \
example2: merge.exe 0x00000000 Boot.bin 0x00001000 RSL.bin flash.bin 0x00\n\n");
}
long int get_file_size( FILE *file, bool first_times )
{
struct stat statbuf;
long int size = -1;
if ( first_times )
{
size = 0;
}
else
{
if ( fseek( file, 0L, SEEK_END ) == 0 )
{
size = ftell( file );
}
}
return size;
}
int data_filling( FILE *file, uint32_t start_addr, uint32_t size, uint8_t data, bool addr_over)
{
int ret = -1;
uint32_t need_write_size = 0;
int set_type;
if ( addr_over )
{
set_type = SEEK_END;
start_addr = 0;
}
else
{
set_type = SEEK_SET;
}
memset( buff, data, sizeof( buff ) );
if ( fseek( file, start_addr, set_type ) == 0 )
{
while( size )
{
if ( size >= sizeof( buff ) )
{
need_write_size = sizeof( buff );
}
else
{
need_write_size = size;
}
if ( fwrite( ( void * )buff, need_write_size, 1, file ) != 1 )
{
ret = -1;
break;
}
size -= need_write_size;
ret = 0;
}
}
return ret;
}
#define INVALID_NUM ( 3 )
#define DEST_FILE_CMD_INDEX ( 0 )
#define DEST_FILLINT_DATA_CMD_INDEX ( 1 )
#define CMD_INDEX( file_num_, index_ ) ( ( ( file_num_ ) * 2 ) + 1 + ( index_ ) )
int main( int cnt, char *cmd[] )
{
int ret = -100;
FILE** file = NULL;
FILE* dest_file = NULL;
uint32_t *addr = NULL;
int i,j;
int file_num;
uint32_t src_size, dest_size;
uint32_t src_end_addr, dest_end_addr, filling_start_addr;
long int file_size;
uint32_t temp_size;
uint8_t filing_data = 0;
bool first_times = true;
do
{
if ( cnt <= INVALID_NUM )
{
ret = -7;
break;
}
if ( ( ( cnt - INVALID_NUM ) % 2 ) != 0 )
{
ret = -8;
break;
}
file_num = ( ( cnt - INVALID_NUM ) / 2 );
file = ( FILE ** )malloc( sizeof( FILE* ) * file_num );
addr = ( uint32_t * ) malloc( sizeof( uint32_t ) * file_num );
memset( file, 0, sizeof( FILE* ) * file_num ) ;
memset( addr, 0, sizeof( uint32_t* ) * file_num ) ;
if ( ( addr == NULL ) || ( file == NULL ) )
{
ret = -9;
break;
}
printf( "file num: %d\n", file_num );
dest_file = fopen(cmd[ CMD_INDEX( file_num, DEST_FILE_CMD_INDEX ) ], "w+b");
if ( dest_file == NULL )
{
printf(" OPEN file fail: %s\n ", cmd[ CMD_INDEX( file_num, DEST_FILE_CMD_INDEX ) ] );
ret = -10;
break;
}
for ( i = 0, j = 2; i < file_num; ++i, j += 2 )
{
file[ i ] = fopen(cmd[j], "rb");
if( file[ i ] == NULL )
{
printf(" OPEN file fail: %s\n ", cmd[ j ]);
ret = -11;
break;
}
}
if ( i != file_num )
{
break;
}
for ( i = 0, j = 1; i < file_num; ++i, j += 2 )
{
addr[i] = ( uint32_t )strtoul( cmd[ j ], NULL, 16);
printf ( "addr: 0x%08X\n", addr[i] );
}
filing_data = ( uint8_t ) strtoul( cmd[ CMD_INDEX( file_num, DEST_FILLINT_DATA_CMD_INDEX ) ], NULL, 16);
for ( i = 0, j = 2; i < file_num; ++i, j += 2 )
{
file_size = get_file_size( file[i], false );
if ( file_size <= 0 )
{
printf( " get file size error: %s\n", cmd[ j ] );
ret = -12;
break;
}
src_size = ( uint32_t )file_size;
src_end_addr = src_size - 1;
printf( " %s file size: %u\n", cmd[ j ], src_size );
file_size = get_file_size( dest_file, first_times );
if ( file_size < 0 )
{
printf( " get file size error: %s\n", cmd[ CMD_INDEX( file_num, DEST_FILE_CMD_INDEX ) ] );
ret = -13;
break;
}
dest_size = ( uint32_t )file_size;
printf( " %s file size: %u\n", cmd[ CMD_INDEX( file_num, DEST_FILE_CMD_INDEX ) ], dest_size );
if ( dest_size == 0 )
{
filling_start_addr = 0;
dest_end_addr = 0;
}
else
{
dest_end_addr = dest_size - 1;
filling_start_addr = dest_size;
}
if ( dest_end_addr <= addr[i] )
{
uint32_t fill_size;
if ( dest_size == 0 )
{
fill_size = addr[i] - dest_end_addr;
}
else
{
fill_size = addr[i] - dest_size;
}
if ( dest_end_addr < addr[i] )
{
data_filling( dest_file, addr[i], fill_size, filing_data, true);
}
else if ( dest_size != 0 )
{
if ( fseek( dest_file, addr[i], SEEK_SET ) != 0 )
{
ret = -5;
break;
}
}
}
else
{
if ( fseek( dest_file, addr[i], SEEK_SET ) != 0 )
{
ret = -6;
break;
}
}
ret = 0;
while( src_size )
{
if ( fseek( file[ i ], 0, SEEK_SET ) != 0 )
{
i = file_num;
ret = -1;
break;
}
if ( src_size >= sizeof( buff ) )
{
temp_size = sizeof( buff );
}
else
{
temp_size = src_size;
}
if ( fread( ( void * )buff, 1, temp_size, file[ i ] ) != temp_size )
{
i = file_num;
ret = -2;
break;
}
if ( fwrite( ( void * )buff, temp_size, 1, dest_file ) != 1 )
{
i = file_num;
ret = -3;
break;
}
if ( fflush( dest_file) != 0 )
{
i = file_num;
ret = -4;
break;
}
src_size -= temp_size;
}
first_times = false;
}
}while(0);
if ( file )
{
for ( i = 0; i < file_num; ++i )
{
if ( file[ i ] != NULL )
{
fclose( file[ i ] );
}
}
}
if ( dest_file ) {
fclose( dest_file );
}
if ( file ) {
free( file );
}
if ( addr ) {
free( addr );
}
if ( ret != 0 )
{
printf( "\n\nFile merge error, error code: %d\n", ret );
help();
}
return ret;
}