|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * @internalComponent |
|
16 * @released |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 #include "image_handler.h" |
|
22 #include "r_obey.h" |
|
23 #include "r_romnode.h" |
|
24 #include "r_coreimage.h" |
|
25 #include "rofs_image_reader.h" |
|
26 #include "rom_image_reader.h" |
|
27 #include "e32_image_reader.h" |
|
28 #include "e32rom.h" |
|
29 #include "h_ver.h" |
|
30 #include "sis2iby.h" |
|
31 #include <time.h> |
|
32 |
|
33 ECompression gCompress=ECompressionUnknown; |
|
34 ostream *out = &cout; |
|
35 string ImageReader::iE32ImgFileName = ""; |
|
36 string ImageReader::iZdrivePath = ""; |
|
37 string ImageReader::iLogFileName = ""; |
|
38 string ImageReader::iPattern = ""; |
|
39 |
|
40 string SisUtils::iOutputPath = "."; |
|
41 string SisUtils::iExtractPath = "."; |
|
42 |
|
43 ImageHandler::ImageHandler() : iReader(NULL) ,iOptions(0), iSisUtils(NULL) |
|
44 { |
|
45 } |
|
46 |
|
47 ImageHandler::~ImageHandler() |
|
48 { |
|
49 if(iReader) |
|
50 delete iReader; |
|
51 |
|
52 if(iSisUtils) |
|
53 delete iSisUtils; |
|
54 } |
|
55 |
|
56 |
|
57 void ImageHandler::ProcessArgs(int argc, char**argv) |
|
58 { |
|
59 if( argc < 2) |
|
60 { |
|
61 throw ImageReaderUsageException("", ""); |
|
62 } |
|
63 |
|
64 bool aOutFileGiven = false; |
|
65 int aPos = 1; |
|
66 |
|
67 while( argc > aPos ) |
|
68 { |
|
69 if(argv[aPos][0] == '-') |
|
70 { |
|
71 switch(argv[aPos][1]) |
|
72 { |
|
73 case 'd': |
|
74 case 'D': |
|
75 //dump header info |
|
76 iOptions |= DUMP_HDR_FLAG; |
|
77 break; |
|
78 case 'e': |
|
79 case 'E': |
|
80 iOptions |= DUMP_E32_IMG_FLAG; |
|
81 if(argv[aPos+1]) |
|
82 ImageReader::iE32ImgFileName = string(argv[aPos+1]); |
|
83 else |
|
84 throw ImageReaderUsageException("Usage error", argv[aPos]); |
|
85 aPos++; |
|
86 break; |
|
87 case 'h': |
|
88 case 'H': |
|
89 PrintUsage(); |
|
90 exit(EXIT_SUCCESS); |
|
91 case 'o': |
|
92 case 'O': |
|
93 { |
|
94 if(argv[aPos][2]) |
|
95 { |
|
96 if((stricmp(argv[aPos],"-OUTDIR")==0)) |
|
97 { |
|
98 if(argv[aPos+1]) |
|
99 { |
|
100 SisUtils::iOutputPath = string(argv[aPos+1]); |
|
101 } |
|
102 else |
|
103 throw ImageReaderUsageException("Usage Error", argv[aPos]); |
|
104 |
|
105 aPos++; |
|
106 } |
|
107 else |
|
108 throw ImageReaderUsageException("Usage Error", argv[aPos]); |
|
109 } |
|
110 else |
|
111 { |
|
112 aOutFileGiven = true; |
|
113 if( argv[aPos+1] ) |
|
114 { |
|
115 // unless using iOutFile.c_str() immediately after |
|
116 // iOutFile = argv[aPos+1]; |
|
117 // iOutFile will not be assign correctly, |
|
118 // is it a defect of gcc 3.4.5 ? |
|
119 iOutFile = string(argv[aPos+1]); |
|
120 aPos++; |
|
121 } |
|
122 } |
|
123 } |
|
124 break; |
|
125 case 'r': |
|
126 case 'R': |
|
127 { |
|
128 if(argv[aPos][2]) |
|
129 throw ImageReaderUsageException("Usage Error", argv[aPos]); |
|
130 |
|
131 iOptions |= RECURSIVE_FLAG; |
|
132 } |
|
133 break; |
|
134 case 's': |
|
135 case 'S': |
|
136 { |
|
137 if(argv[aPos][2]) |
|
138 { |
|
139 if(stricmp(argv[aPos],"-SIS2IBY")==0) |
|
140 { |
|
141 iOptions |= MODE_SIS2IBY; |
|
142 } |
|
143 else |
|
144 throw ImageReaderUsageException("Usage Error", argv[aPos]); |
|
145 } |
|
146 else |
|
147 iOptions |= DUMP_DIR_ENTRIES_FLAG; |
|
148 } |
|
149 break; |
|
150 case 't': |
|
151 case 'T': |
|
152 { |
|
153 if((stricmp(argv[aPos],"-TMPDIR")==0)) |
|
154 { |
|
155 if(argv[aPos+1]) |
|
156 { |
|
157 SisUtils::iExtractPath = string(argv[aPos+1]); |
|
158 } |
|
159 else |
|
160 throw ImageReaderUsageException("Usage Error", argv[aPos]); |
|
161 |
|
162 aPos++; |
|
163 } |
|
164 else |
|
165 throw ImageReaderUsageException("Usage Error", argv[aPos]); |
|
166 } |
|
167 break; |
|
168 case 'v': |
|
169 case 'V': |
|
170 { |
|
171 if(argv[aPos][2]) |
|
172 throw ImageReaderUsageException("Usage Error", argv[aPos]); |
|
173 |
|
174 iOptions |= DUMP_VERBOSE_FLAG; |
|
175 } |
|
176 break; |
|
177 case 'l': |
|
178 case 'L': |
|
179 { |
|
180 if(argv[aPos+1]) |
|
181 { |
|
182 ImageReader::iLogFileName = string(argv[aPos+1]); |
|
183 } |
|
184 else |
|
185 throw ImageReaderUsageException("Usage error", argv[aPos]); |
|
186 iOptions |= LOG_IMAGE_CONTENTS_FLAG; |
|
187 aPos++; |
|
188 } |
|
189 break; |
|
190 case 'x': |
|
191 case 'X': |
|
192 { |
|
193 if(argv[aPos+1] && (!argv[aPos][2])) |
|
194 { |
|
195 ImageReader::iPattern = string(argv[aPos+1]); |
|
196 } |
|
197 else |
|
198 throw ImageReaderUsageException("Usage error", argv[aPos]); |
|
199 |
|
200 iOptions |= EXTRACT_FILE_SET_FLAG; |
|
201 aPos++; |
|
202 } |
|
203 break; |
|
204 case 'z': |
|
205 case 'Z': |
|
206 { |
|
207 if(argv[aPos+1]) |
|
208 ImageReader::iZdrivePath = string(argv[aPos+1]); |
|
209 else |
|
210 throw ImageReaderUsageException("Usage error", argv[aPos]); |
|
211 |
|
212 iOptions |= EXTRACT_FILES_FLAG; |
|
213 aPos++; |
|
214 } |
|
215 break; |
|
216 default: |
|
217 throw ImageReaderUsageException("Invalid command", argv[aPos]); |
|
218 break; |
|
219 } |
|
220 } |
|
221 else |
|
222 { |
|
223 if(!iInputFileName.empty()) |
|
224 { |
|
225 throw ImageReaderUsageException("Invalid command", "Multiple input file not supported"); |
|
226 } |
|
227 |
|
228 SetInputFile(argv[aPos]); |
|
229 } |
|
230 aPos++; |
|
231 } |
|
232 |
|
233 if( aOutFileGiven && !(iOptions & MODE_SIS2IBY) ) |
|
234 { |
|
235 ofstream* rdout = new ofstream(iOutFile.c_str(), ios::out | ios::trunc); |
|
236 if( !rdout->is_open()){ |
|
237 delete rdout ; |
|
238 rdout = NULL ; |
|
239 throw ImageReaderException((char*)iOutFile.c_str(), "Unable to open File"); |
|
240 } |
|
241 out = rdout ; |
|
242 } |
|
243 |
|
244 // Disable -z option if -x option is passed |
|
245 if( (iOptions & EXTRACT_FILE_SET_FLAG) && (iOptions & EXTRACT_FILES_FLAG) ) |
|
246 { |
|
247 iOptions &= ~(EXTRACT_FILES_FLAG); |
|
248 } |
|
249 |
|
250 // -r option should be used along with -x option |
|
251 if( (iOptions & RECURSIVE_FLAG) && !(iOptions & EXTRACT_FILE_SET_FLAG) ) |
|
252 { |
|
253 throw ImageReaderUsageException("Usage error", "-r should be used with -x"); |
|
254 } |
|
255 } |
|
256 |
|
257 EImageType ImageHandler::ReadMagicWord() |
|
258 { |
|
259 |
|
260 #if defined(__TOOLS2__) || defined(__MSVCDOTNET__) |
|
261 ifstream file(iInputFileName.c_str(), ios_base::in | ios_base::binary ); |
|
262 #else |
|
263 ifstream file(iInputFileName.c_str(), ios::in | ios::binary | ios::nocreate); |
|
264 #endif |
|
265 |
|
266 EImageType retVal = EUNKNOWN_IMAGE; |
|
267 |
|
268 if( !file.is_open() ) |
|
269 { |
|
270 throw ImageReaderException((char*)iInputFileName.c_str(), "Cannot open file "); |
|
271 } |
|
272 |
|
273 TUint8 magicWords[16]; |
|
274 file.read(reinterpret_cast<char*>(magicWords),16); |
|
275 |
|
276 if(0 == memcmp(magicWords,"ROFS",4)) { |
|
277 retVal = EROFS_IMAGE; |
|
278 }else if(0 == memcmp(magicWords,"ROFx",4)) { |
|
279 retVal = EROFX_IMAGE; |
|
280 } |
|
281 else if(0 == memcmp(magicWords,"EPOC",4) && 0 == memcmp(&magicWords[8],"ROM",3) ) { |
|
282 retVal = EROM_IMAGE; |
|
283 } |
|
284 else { |
|
285 E32ImageFile aE32; |
|
286 TUint32 aSz; |
|
287 file.seekg(0,ios::end); |
|
288 aSz = file.tellg(); |
|
289 file.seekg(0,ios::beg); |
|
290 aE32.Adjust(aSz); |
|
291 aE32.iFileSize = aSz; |
|
292 file >> aE32; |
|
293 |
|
294 if(aE32.iError == KErrNone){ |
|
295 retVal = EE32_IMAGE; |
|
296 } |
|
297 else { |
|
298 TExtensionRomHeader exRomHeader; |
|
299 file.seekg(0, ios::beg); |
|
300 file.read(reinterpret_cast<char*>(&exRomHeader), sizeof(TExtensionRomHeader)); |
|
301 TUint zeroTime = time(0); |
|
302 // aExtensionRomHeader.iTime and aExtensionRomHeader.iKernelTime are |
|
303 // in microseconds. So convert them to seconds and see if these are |
|
304 // valid times e.g. a time(in seconds) after midnight Jan 1st, 1970 |
|
305 TUint imgTime = exRomHeader.iTime / 1000000; |
|
306 TUint kernImgTime = exRomHeader.iKernelTime / 1000000; |
|
307 if( imgTime >= zeroTime && kernImgTime >= zeroTime) { |
|
308 //Check if the padding in the header has value 0xff |
|
309 retVal = EROMX_IMAGE; |
|
310 for(int i = sizeof(exRomHeader.iPad) - 1 ; i >= 0 ; i--){ |
|
311 if(0xff != exRomHeader.iPad[i]){ |
|
312 retVal = EUNKNOWN_IMAGE; |
|
313 break ; |
|
314 } |
|
315 } |
|
316 } |
|
317 } |
|
318 } |
|
319 |
|
320 if(retVal == EUNKNOWN_IMAGE){ |
|
321 file.seekg(0,ios::beg); |
|
322 retVal = ReadBareImage(file); |
|
323 } |
|
324 |
|
325 file.close(); |
|
326 |
|
327 return retVal; |
|
328 } |
|
329 |
|
330 |
|
331 /** |
|
332 * @fn ImageHandler::ReadBareImage |
|
333 * @brief this function processes image type under the condition of that if an image is given without header which means the image is not self-described |
|
334 * @return type of the image. |
|
335 * @note this function is introduced for handling issues raised by DEF129908 |
|
336 */ |
|
337 EImageType ImageHandler::ReadBareImage(ifstream& aIfs) |
|
338 { |
|
339 TRomHeader romHdr ; |
|
340 aIfs.read(reinterpret_cast<char*>(&romHdr),sizeof(TRomHeader)); |
|
341 |
|
342 return ((romHdr.iRomBase >= KRomBase ) && |
|
343 (romHdr.iRomRootDirectoryList > KRomBase ) && |
|
344 (romHdr.iRomBase < KRomBaseMaxLimit ) && |
|
345 (romHdr.iRomRootDirectoryList < KRomBaseMaxLimit)) ? EBAREROM_IMAGE : EUNKNOWN_IMAGE; |
|
346 |
|
347 } |
|
348 |
|
349 |
|
350 void ImageHandler::HandleInputFiles() |
|
351 { |
|
352 if(!(iOptions & MODE_SIS2IBY)) |
|
353 { |
|
354 EImageType imgType = ReadMagicWord(); |
|
355 |
|
356 switch(imgType) |
|
357 { |
|
358 case EROFS_IMAGE: |
|
359 case EROFX_IMAGE: |
|
360 iReader = new RofsImageReader((char*)iInputFileName.c_str()); |
|
361 break; |
|
362 case EROM_IMAGE: |
|
363 iReader = new RomImageReader((char*)iInputFileName.c_str()); |
|
364 break; |
|
365 case EBAREROM_IMAGE: |
|
366 iReader = new RomImageReader((char*)iInputFileName.c_str(), EBAREROM_IMAGE); |
|
367 break; |
|
368 case EROMX_IMAGE: |
|
369 iReader = new RomImageReader((char*)iInputFileName.c_str(), EROMX_IMAGE); |
|
370 break; |
|
371 case EE32_IMAGE: |
|
372 iReader = new E32ImageReader((char*)iInputFileName.c_str()); |
|
373 break; |
|
374 default: |
|
375 { |
|
376 throw ImageReaderException((char*)iInputFileName.c_str(), "Unknown Type of Image file"); |
|
377 } |
|
378 break; |
|
379 } |
|
380 |
|
381 if(iReader) |
|
382 { |
|
383 iReader->ReadImage(); |
|
384 iReader->ProcessImage(); |
|
385 iReader->Validate(); |
|
386 iReader->SetDisplayOptions( iOptions ); |
|
387 iReader->ExtractImageContents(); |
|
388 iReader->Dump(); |
|
389 } |
|
390 } |
|
391 else |
|
392 { |
|
393 if(iInputFileName.empty()) |
|
394 { |
|
395 throw SisUtilsException(const_cast<char *>("Usage Error"), |
|
396 const_cast<char *>("No SIS file passed")); |
|
397 } |
|
398 |
|
399 iSisUtils = new Sis2Iby((char*)iInputFileName.c_str()); |
|
400 |
|
401 if(iSisUtils) |
|
402 { |
|
403 if(iOptions & DUMP_VERBOSE_FLAG) |
|
404 { |
|
405 iSisUtils->SetVerboseMode(); |
|
406 } |
|
407 |
|
408 iSisUtils->ProcessSisFile(); |
|
409 iSisUtils->GenerateOutput(); |
|
410 } |
|
411 else |
|
412 { |
|
413 throw SisUtilsException(const_cast<char *>("Error:"), |
|
414 const_cast<char *>("Cannot create Sis2Iby object")); |
|
415 } |
|
416 } |
|
417 } |
|
418 |
|
419 void ImageHandler::PrintVersion() |
|
420 { |
|
421 *out << "\nReadimage - reader for Rom, Rofs and E32 images V"; |
|
422 out->width(1); |
|
423 *out << MajorVersion << "."; |
|
424 out->width(2); |
|
425 out->fill('0'); |
|
426 *out << MinorVersion << " ("; |
|
427 out->width(3); |
|
428 *out << Build << ") " << endl; |
|
429 *out << Copyright; |
|
430 } |
|
431 |
|
432 void ImageHandler::PrintUsage() |
|
433 { |
|
434 PrintVersion(); |
|
435 const char aUsage[] = |
|
436 "Usage: readImage [options] [<-sis2iby> [sis-options]] <filename>\n\n" |
|
437 "Options: With no options, it prints the files and directories in image\n" |
|
438 " -o output file name\n" |
|
439 " -d dump header information(default)\n" |
|
440 " -s dump the directory structure\n" |
|
441 " -v dump image headers and directory structure\n" |
|
442 " -e xxx dump the xxx e32 image within the entire image when used along with -v or -s option\n" |
|
443 " -h this message\n" |
|
444 " -z xxx extract all the file(s) from the given image to xxx location\n" |
|
445 " -l xxx log the image contents on to xxx file\n" |
|
446 " -x xxx extract single or set of files as given in xxx from the given image\n" |
|
447 " -r recursively extract files from the sub-directories when used along with -x option\n\n" |
|
448 "SIS-options: Option -sis2iby changes the mode to generate IBY from SIS file\n" |
|
449 " -sis2iby generates iby file for the given SIS file\n" |
|
450 " -tmpdir xxx extract all sis file contents to xxx location\n" |
|
451 " -outdir xxx generates the iby file(s) to xxx location\n" |
|
452 " -v verbose output\n"; |
|
453 *out << aUsage << endl; |
|
454 } |
|
455 |
|
456 int main(int argc, char** argv) |
|
457 { |
|
458 ImageHandler aIh; |
|
459 int retVal = EXIT_SUCCESS; |
|
460 try |
|
461 { |
|
462 aIh.ProcessArgs(argc, argv); |
|
463 aIh.HandleInputFiles(); |
|
464 } |
|
465 catch(ImageReaderUsageException& aIre) |
|
466 { |
|
467 if(argc >= 2) |
|
468 { |
|
469 //This is a usage error and has to be reported |
|
470 //Otherwise, it is called just to display the usage |
|
471 aIre.Report(); |
|
472 } |
|
473 aIh.PrintUsage(); |
|
474 retVal = EXIT_FAILURE; |
|
475 } |
|
476 catch(ImageReaderException& aIre) |
|
477 { |
|
478 aIre.Report(); |
|
479 retVal = EXIT_FAILURE; |
|
480 } |
|
481 catch(SisUtilsException& aSUe) |
|
482 { |
|
483 aSUe.Report(); |
|
484 retVal = EXIT_FAILURE; |
|
485 } |
|
486 |
|
487 if(out != &cout){ |
|
488 ofstream* rdout = static_cast<ofstream*>(out) ; |
|
489 rdout->close() ; |
|
490 delete rdout ; |
|
491 } |
|
492 |
|
493 return retVal; |
|
494 } |