-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
executable file
·344 lines (269 loc) · 7.53 KB
/
main.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
Extractor for LARA Firmware Files
Copyright (C) 2012
Andreas Schuler <andreas at schulerdev.de>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License v2 as published by the Free
Software Foundation.
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, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#define BIGENDIAN 1
#if BIGENDIAN
#define SWAP32(x) (((x) & 0xff) << 24 | ((x) & 0xff00) << 8 | ((x) & 0xff0000) >> 8 | ((x) >> 24) & 0xff)
#define SWAP16(x) (((x) & 0xff) << 8 | (x) >> 8 )
#else
#define SWAP32(x) (x)
#define SWAP16(x) (x)
#endif
struct parthead
{
char magic[14]; //LARA partition
unsigned short mn;
unsigned int num;
unsigned int nextpart; //byteswapped
char unknown[72];
char filename[32];
}__attribute__((packed));
struct filehead
{
char magic[13]; //LARA firmware
unsigned short mn;
char infostr1[14];
unsigned short build;
char edition[64];
char vendor[16];
char product[16];
char unknown[113];
}__attribute__((packed));
#define MODE_NONE 0
#define MODE_INFO 1
#define MODE_EXTRACT 2
void mode_info(char *fw);
void mode_extract(char *fw);
bool read_parthead(FILE *f,struct parthead *ph);
bool read_filehead(FILE *f,struct filehead *fh);
void print_parthead(struct parthead *ph);
void print_filehead(struct filehead *fh);
void write_file(FILE *f,struct filehead *fh,size_t count);
int main(int argc, char *argv[])
{
char *filename;
char c;
char mode=MODE_NONE;
printf("LARA partition file manager by Andreas Schuler (andreas at schulerdev dot de)\n");
while((c=getopt(argc,argv,"ie"))!=-1)
{
switch(c)
{
case 'i': //print header infos
if(mode==MODE_NONE)
mode=MODE_INFO;
else
{
printf("Can't use both options...\n");
abort();
}
break;
case 'e': //extract all
if(mode==MODE_NONE)
mode=MODE_EXTRACT;
else
{
printf("Can't use both options...\n");
abort();
}
break;
case '?':
/* if (optopt=='l' || optopt=='o' || optopt=='e')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
*/
return 1;
default:
printf("Usage:\nlara -i firmware : print firmware information\nlara -e firmware : extract firmware file\n");
abort ();
}
}
if(optind<argc)
filename=argv[optind];
switch(mode)
{
case MODE_INFO:
mode_info(filename);
break;
case MODE_EXTRACT:
mode_extract(filename);
break;
default:
mode_info(filename);
break;
}
}
void mode_info(char *fw)
{
FILE *f=fopen(fw,"r");
if(!f)
{
printf("cant open !\n");
exit(1);
}
char md5[16];
if(fread(md5,1,16,f)!=16)
{
printf("Can't read md5 sum\n");
return;
}
printf("md5sum:");
for(int i=0;i<16;i++)
printf("%0.2x",((unsigned char)(md5[i])));
printf("\n");
struct filehead fh;
if(!read_filehead(f,&fh))
{
printf("cant read fileheader");
return;
}
print_filehead(&fh);
struct parthead ph;
while(read_parthead(f,&ph))
{
int dofs=ftell(f);
printf("Data offset: %.8x (%u)\n",dofs,dofs);
print_parthead(&ph);
if(ph.nextpart)
fseek(f,ph.nextpart,SEEK_SET);
else
break;
}
fclose(f);
}
void mode_extract(char *fw)
{
FILE *f=fopen(fw,"r");
if(!f)
{
printf("cant open !\n");
exit(1);
}
fseek(f,16,SEEK_SET);
struct filehead fh;
if(!read_filehead(f,&fh))
{
printf("cant read fileheader");
return;
}
print_filehead(&fh);
struct parthead ph;
size_t lastpos=0x180;
while(read_parthead(f,&ph))
{
if(ph.nextpart)
{
write_file(f,&fh,ph.nextpart-lastpos-sizeof(parthead));
fseek(f,ph.nextpart,SEEK_SET);
}
else
break;
lastpos=ph.nextpart;
}
fclose(f);
}
bool read_parthead(FILE *f,struct parthead *ph)
{
if(fread(ph,1,sizeof(struct parthead),f)!=sizeof(struct parthead))
return false;
if(memcmp(ph->magic,"LARA partition",14))
printf("LARA partition:magic value doesn't matches\n");
ph->num=SWAP32(ph->num);
ph->nextpart=SWAP32(ph->nextpart);
ph->mn=SWAP16(ph->mn);
return true;
}
void print_parthead(struct parthead *ph)
{
printf("Magic Value: %s\n",ph->magic);
printf("Magic Number: %u\n",ph->mn);
printf("Partition Number: %u\n",ph->num);
printf("Next Partition Offset: 0x%x\n",ph->nextpart);
printf("Filename: %s\n",ph->filename);
printf("Unknown Bytes:\n");
int i=0;
while(i<72)
{
printf("%.2x",((unsigned char)(ph->unknown[i])));
if(((i+1)%16)==0)
printf("\n");
i++;
}
printf("\n\n");
}
bool read_filehead(FILE *f,struct filehead *fh)
{
if(fread(fh,1,sizeof(struct filehead),f)!=sizeof(struct filehead))
return false;
if(memcmp(fh->magic,"LARA firmware",13))
{
printf("LARA firmware:magic value doesn't matches\n");
return false;
}
fh->mn=SWAP16(fh->mn);
fh->build=SWAP16(fh->build);
return true;
}
void print_filehead(struct filehead *fh)
{
printf("Magic Value: %s\n",fh->magic);
printf("Magic Number: %u\n",fh->mn);
printf("infostr1: %s\n",fh->infostr1);
printf("build: %u\n",fh->build);
printf("edition: %s\n",fh->edition);
printf("vendor: %s\n",fh->vendor);
printf("product: %s\n",fh->product);
printf("Unknown Bytes:\n");
int i=0;
while(i<112)
{
printf("%.2x",((unsigned char)(fh->unknown[i])));
if(((i+1)%16)==0)
printf("\n");
i++;
}
printf("\n\n");
}
char partname[]="partname_0";
void write_file( FILE *f, struct filehead *fh,size_t count)
{
FILE *outf=fopen(partname,"w+");
printf("writing file :%s\n",partname);
partname[9]++;
char buf[512];
size_t rlen=0,wlen=0;
while(!feof(f))
{
if(count<=0)
break;
rlen=(count<512)?count:512;
wlen=fread(buf,1,rlen,f);
fwrite(buf,1,wlen,outf);
count-=wlen;
}
fclose(outf);
}