imgtools/romtools/readimage/src/rom_image_reader.cpp
changeset 590 360bd6b35136
parent 0 044383f39525
child 712 df89378e9223
equal deleted inserted replaced
588:c7c26511138f 590:360bd6b35136
    18 */
    18 */
    19 
    19 
    20 #include <e32rom.h>
    20 #include <e32rom.h>
    21 #include "rom_image_reader.h"
    21 #include "rom_image_reader.h"
    22 #include "r_rom.h"
    22 #include "r_rom.h"
    23 
    23 #ifdef __LINUX__
       
    24 #define _alloca alloca 
       
    25 #endif
       
    26 #include "utf16string.h"
    24 void InflateUnCompress(unsigned char* source, int sourcesize, unsigned char* dest, int destsize);
    27 void InflateUnCompress(unsigned char* source, int sourcesize, unsigned char* dest, int destsize);
    25 RomImageFSEntry::RomImageFSEntry (const char* aName) : iName(aName), iSibling(0), iChildren(0) {
    28 RomImageFSEntry::RomImageFSEntry (const char* aName) : iName(aName), iSibling(0), iChildren(0) {
    26 }
    29 }
    27 RomImageFSEntry::~RomImageFSEntry() {
    30 RomImageFSEntry::~RomImageFSEntry() {
    28 	if(iChildren){
    31 	if(iChildren){
    34 		iSibling = NULL ;
    37 		iSibling = NULL ;
    35 	}	
    38 	}	
    36 }
    39 }
    37 
    40 
    38 //Rom Image header 
    41 //Rom Image header 
    39 RomImageHeader::RomImageHeader(char* aHdr, EImageType aImgType)
    42 RomImageHeader::RomImageHeader(char* aHdr, EImageType aImgType) {
    40 {
    43 	switch( aImgType) {
    41 	switch( aImgType)
       
    42 	{
       
    43 	case EROM_IMAGE:
    44 	case EROM_IMAGE:
    44 		iLoaderHdr = (TRomLoaderHeader*)aHdr;
    45 		iLoaderHdr = (TRomLoaderHeader*)aHdr;
    45 		iRomHdr = (TRomHeader*)(aHdr + sizeof(TRomLoaderHeader));
    46 		iRomHdr = (TRomHeader*)(aHdr + sizeof(TRomLoaderHeader));
    46 		iExtRomHdr = 0;
    47 		iExtRomHdr = 0;
    47 		break;
    48 		break;
    49 	case EROMX_IMAGE:
    50 	case EROMX_IMAGE:
    50 		iExtRomHdr = (TExtensionRomHeader*)(aHdr);
    51 		iExtRomHdr = (TExtensionRomHeader*)(aHdr);
    51 		iRomHdr = 0;
    52 		iRomHdr = 0;
    52 		iLoaderHdr = 0;
    53 		iLoaderHdr = 0;
    53 		break;
    54 		break;
    54         
    55 
    55     case EBAREROM_IMAGE:
    56 	case EBAREROM_IMAGE:
    56         iLoaderHdr = 0;
    57 		iLoaderHdr = 0;
    57         iRomHdr = (TRomHeader*)aHdr;
    58 		iRomHdr = (TRomHeader*)aHdr;
    58         iExtRomHdr = 0;
    59 		iExtRomHdr = 0;
    59         break;
    60 		break;
    60    default:
    61 	default:
    61    		iExtRomHdr = 0;
    62 		iExtRomHdr = 0;
    62 		iRomHdr = 0;
    63 		iRomHdr = 0;
    63 		iLoaderHdr = 0;
    64 		iLoaderHdr = 0;
    64    	break ;
    65 		break ;
    65 	}
    66 	}
    66 }
    67 }
    67 
    68 
    68 
    69 
    69 void RomImageHeader::DumpRomHdr()
    70 void RomImageHeader::DumpRomHdr() {
    70 {
    71 	if(iLoaderHdr)
    71     if(iLoaderHdr)
    72 		*out << "ROM Image " << endl << endl;
    72         *out << "ROM Image " << endl << endl;
    73 	else
    73     else
    74 		*out << "Bare ROM Image" << endl << endl;
    74         *out << "Bare ROM Image" << endl << endl;
       
    75 	TUint aPos = 0;
    75 	TUint aPos = 0;
    76 	bool aContinue = true;
    76 	bool aContinue = true;
    77 	
    77 
    78 	*out << "Image Signature .................";
    78 	*out << "Image Signature .................";
    79 
    79 
    80     if(iLoaderHdr){
    80 	if(iLoaderHdr){     
    81         TText *aRomName =  reinterpret_cast<TText *>(iLoaderHdr) ;
    81 		char temp[KRomNameSize + 1];
    82         while (aPos < KRomNameSize) {
    82 		memcpy(temp,reinterpret_cast<char*>(iLoaderHdr),KRomNameSize);        
    83             *out << (char)aRomName[aPos++];
    83 		temp[KRomNameSize] = 0;
    84         }
    84 		*out << temp;
    85     }
    85 	}
    86 
    86 
    87 	*out << endl << endl;
    87 	*out << endl << endl;
    88 
    88 
    89 	DumpInHex("Timestamp", (iRomHdr->iTime >> 32)) ;
    89 	DumpInHex("Timestamp", (iRomHdr->iTime >> 32)) ;
    90 	DumpInHex(" ", (iRomHdr->iTime &0xffffffff), aContinue) << endl;
    90 	DumpInHex(" ", (iRomHdr->iTime &0xffffffff), aContinue) << endl;
   112 	DumpInHex("RelocInfo", iRomHdr->iRelocInfo) << endl;
   112 	DumpInHex("RelocInfo", iRomHdr->iRelocInfo) << endl;
   113 	DumpInHex("OldTraceMask", iRomHdr->iOldTraceMask) << endl;
   113 	DumpInHex("OldTraceMask", iRomHdr->iOldTraceMask) << endl;
   114 	DumpInHex("UserDataAddress", iRomHdr->iUserDataAddress) << endl;
   114 	DumpInHex("UserDataAddress", iRomHdr->iUserDataAddress) << endl;
   115 	DumpInHex("TotalUserDataSize", iRomHdr->iTotalUserDataSize) << endl;
   115 	DumpInHex("TotalUserDataSize", iRomHdr->iTotalUserDataSize) << endl;
   116 	DumpInHex("DebugPort", iRomHdr->iDebugPort) << endl;
   116 	DumpInHex("DebugPort", iRomHdr->iDebugPort) << endl;
   117 	
   117 
   118 	DumpInHex("Version", iRomHdr->iVersion.iMajor, false, 2);
   118 	DumpInHex("Version", iRomHdr->iVersion.iMajor, false, 2);
   119 	DumpInHex(".", iRomHdr->iVersion.iMinor, aContinue, 2);
   119 	DumpInHex(".", iRomHdr->iVersion.iMinor, aContinue, 2);
   120 	DumpInHex("(" ,iRomHdr->iVersion.iBuild, aContinue, 2);
   120 	DumpInHex("(" ,iRomHdr->iVersion.iBuild, aContinue, 2);
   121 	*out << ")" << endl;
   121 	*out << ")" << endl;
   122 
   122 
   123 	DumpInHex("CompressionType", iRomHdr->iCompressionType) << endl;
   123 	DumpInHex("CompressionType", iRomHdr->iCompressionType) << endl;
   124 	DumpInHex("CompressedSize", iRomHdr->iCompressedSize) << endl;
   124 	DumpInHex("CompressedSize", iRomHdr->iCompressedSize) << endl;
   125 	DumpInHex("UncompressedSize", iRomHdr->iUncompressedSize) << endl;
   125 	DumpInHex("UncompressedSize", iRomHdr->iUncompressedSize) << endl;
   126 	DumpInHex("HcrFileAddress", iRomHdr->iHcrFileAddress) << endl;
   126 	DumpInHex("HcrFileAddress", iRomHdr->iHcrFileAddress) << endl;
   127 	
   127 
   128 	DumpInHex("DisabledCapabilities", iRomHdr->iDisabledCapabilities[0]);
   128 	DumpInHex("DisabledCapabilities", iRomHdr->iDisabledCapabilities[0]);
   129 	DumpInHex(" ", iRomHdr->iDisabledCapabilities[1], aContinue) << endl;
   129 	DumpInHex(" ", iRomHdr->iDisabledCapabilities[1], aContinue) << endl;
   130 
   130 
   131 	DumpInHex("TraceMask", iRomHdr->iTraceMask[0]);
   131 	DumpInHex("TraceMask", iRomHdr->iTraceMask[0]);
   132 	aPos = 1;
   132 	aPos = 1;
   133 	while( aPos < (TUint)KNumTraceMaskWords)
   133 	while( aPos < (TUint)KNumTraceMaskWords) {
   134 	{
   134 		if(iRomHdr->iTraceMask[aPos]) {
   135 		if(iRomHdr->iTraceMask[aPos])
       
   136 		{
       
   137 			DumpInHex(" ", iRomHdr->iTraceMask[aPos++], aContinue);
   135 			DumpInHex(" ", iRomHdr->iTraceMask[aPos++], aContinue);
   138 		}
   136 		}
   139 		else
   137 		else {
   140 		{
       
   141 			DumpInHex(" ", iRomHdr->iTraceMask[aPos++], aContinue, 1);
   138 			DumpInHex(" ", iRomHdr->iTraceMask[aPos++], aContinue, 1);
   142 		}
   139 		}
   143 
   140 
   144 	}
   141 	}
   145 
   142 
   146 	*out << endl << endl;
   143 	*out << endl << endl;
   147 
   144 
   148 }
   145 }
   149 void RomImageHeader::DumpRomXHdr()
   146 void RomImageHeader::DumpRomXHdr() {
   150 {
       
   151 	*out << "Extension ROM Image" << endl << endl;
   147 	*out << "Extension ROM Image" << endl << endl;
   152 	bool aContinue = true;
   148 	bool aContinue = true;
   153 	
   149 
   154 	DumpInHex("Timestamp", (iExtRomHdr->iTime >> 32)) ;
   150 	DumpInHex("Timestamp", (iExtRomHdr->iTime >> 32)) ;
   155 	DumpInHex(" ", (iExtRomHdr->iTime &0xffffffff), aContinue) << endl;
   151 	DumpInHex(" ", (iExtRomHdr->iTime &0xffffffff), aContinue) << endl;
   156 
   152 
   157 	DumpInHex("RomBase", iExtRomHdr->iRomBase) << endl;
   153 	DumpInHex("RomBase", iExtRomHdr->iRomBase) << endl;
   158 
   154 
   159 	DumpInHex("RomSize", iExtRomHdr->iRomSize) << endl;
   155 	DumpInHex("RomSize", iExtRomHdr->iRomSize) << endl;
   160 	DumpInHex("CheckSum", iExtRomHdr->iCheckSum) << endl;
   156 	DumpInHex("CheckSum", iExtRomHdr->iCheckSum) << endl;
   161 	
   157 
   162 	DumpInHex("Version", iExtRomHdr->iVersion.iMajor, false, 2);
   158 	DumpInHex("Version", iExtRomHdr->iVersion.iMajor, false, 2);
   163 	DumpInHex(".", iExtRomHdr->iVersion.iMinor, aContinue, 2);
   159 	DumpInHex(".", iExtRomHdr->iVersion.iMinor, aContinue, 2);
   164 	DumpInHex("(" ,iExtRomHdr->iVersion.iBuild, aContinue, 2);
   160 	DumpInHex("(" ,iExtRomHdr->iVersion.iBuild, aContinue, 2);
   165 	*out << ")" << endl;
   161 	*out << ")" << endl;
   166 
   162 
   167 	DumpInHex("CompressionType", iExtRomHdr->iCompressionType) << endl;
   163 	DumpInHex("CompressionType", iExtRomHdr->iCompressionType) << endl;
   168 	DumpInHex("CompressedSize", iExtRomHdr->iCompressedSize) << endl;
   164 	DumpInHex("CompressedSize", iExtRomHdr->iCompressedSize) << endl;
   169 	DumpInHex("UncompressedSize", iExtRomHdr->iUncompressedSize) << endl;
   165 	DumpInHex("UncompressedSize", iExtRomHdr->iUncompressedSize) << endl;
   170 	
   166 
   171 	*out << endl << endl;
   167 	*out << endl << endl;
   172 
   168 
   173 }
   169 }
   174 //Rom Image reader
   170 //Rom Image reader
   175 RomImageReader::RomImageReader(const char* aFile, EImageType aImgType) :
   171 RomImageReader::RomImageReader(const char* aFile, EImageType aImgType) :
   176 ImageReader(aFile), iImageHeader(0),
   172 ImageReader(aFile), iImageHeader(0),
   177 iRomSize(0),iHeaderBuffer(0),
   173 iRomSize(0),iHeaderBuffer(0),
   178 iRomLayoutData(0),iImgType(aImgType)
   174 iRomLayoutData(0),iImgType(aImgType) {
   179 {
       
   180 	iRomImageRootDirEntry = new RomImageDirEntry("");
   175 	iRomImageRootDirEntry = new RomImageDirEntry("");
   181 }
   176 }
   182 
   177 
   183 RomImageReader::~RomImageReader()
   178 RomImageReader::~RomImageReader() {
   184 {
       
   185 	if(iFile.is_open())
   179 	if(iFile.is_open())
   186 		iFile.close();
   180 		iFile.close();
   187 	if(iHeaderBuffer)
   181 	if(iHeaderBuffer)
   188 		delete []iHeaderBuffer;
   182 		delete []iHeaderBuffer;
   189 	if(iRomLayoutData)
   183 	if(iRomLayoutData)
   190 		delete []iRomLayoutData;  
   184 		delete []iRomLayoutData;  
   191 	delete iRomImageRootDirEntry;
   185 	delete iRomImageRootDirEntry;
   192 
   186 
   193 	
   187 
   194 }
   188 }
   195 
   189 
   196 void RomImageReader::ReadImage()
   190 void RomImageReader::ReadImage() {
   197 {
       
   198 	if(iFile.is_open()) return ;
   191 	if(iFile.is_open()) return ;
   199 	iFile.open(iImgFileName.c_str(), ios::binary | ios::in);
   192 	iFile.open(iImgFileName.c_str(), ios_base::binary | ios_base::in);
   200 	if( !iFile.is_open() ) {
   193 	if( !iFile.is_open() ) {
   201 		throw ImageReaderException(iImgFileName.c_str(), "Cannot open file ");
   194 		throw ImageReaderException(iImgFileName.c_str(), "Cannot open file ");
   202 	}	
   195 	}	
   203 	
   196 
   204 	TUint headerSize = GetHdrSize() ;	
   197 	TUint headerSize = GetHdrSize() ;	
   205 	if(headerSize > 0){
   198 	if(headerSize > 0){
   206 		iHeaderBuffer = new char[headerSize];
   199 		iHeaderBuffer = new char[headerSize];
   207 		iFile.read(iHeaderBuffer,headerSize);
   200 		iFile.read(iHeaderBuffer,headerSize);
   208 	}	
   201 	}	
   209 }
   202 }
   210  
   203 
   211 void RomImageReader::Validate()
   204 void RomImageReader::Validate() {
   212 {
   205 }
   213 }
   206 
   214 
   207 TUint32 RomImageReader::GetImageCompressionType() {
   215 TUint32 RomImageReader::GetImageCompressionType()
       
   216 {
       
   217 	if(iImageHeader->iRomHdr )// iImageType == EROM_IMAGE
   208 	if(iImageHeader->iRomHdr )// iImageType == EROM_IMAGE
   218 		return iImageHeader->iRomHdr->iCompressionType;
   209 		return iImageHeader->iRomHdr->iCompressionType;
   219 	else
   210 	else
   220 		return iImageHeader->iExtRomHdr->iCompressionType;
   211 		return iImageHeader->iExtRomHdr->iCompressionType;
   221 }
   212 }
   222 
   213 
   223  
   214 
   224 TLinAddr RomImageReader::GetRomBase()
   215 TLinAddr RomImageReader::GetRomBase() {
   225 {
       
   226 	if(iImageHeader->iRomHdr )// iImageType == EROM_IMAGE
   216 	if(iImageHeader->iRomHdr )// iImageType == EROM_IMAGE
   227 		return iImageHeader->iRomHdr->iRomBase ;
   217 		return iImageHeader->iRomHdr->iRomBase ;
   228 	else
   218 	else
   229 		return iImageHeader->iExtRomHdr->iRomBase;
   219 		return iImageHeader->iExtRomHdr->iRomBase;
   230 }
   220 }
   231 
   221 
   232 TLinAddr RomImageReader::GetRootDirList()
   222 TLinAddr RomImageReader::GetRootDirList() {
   233 {
       
   234 	if(iImageHeader->iRomHdr )// iImageType == EROM_IMAGE
   223 	if(iImageHeader->iRomHdr )// iImageType == EROM_IMAGE
   235 		return iImageHeader->iRomHdr->iRomRootDirectoryList;
   224 		return iImageHeader->iRomHdr->iRomRootDirectoryList;
   236 	else
   225 	else
   237 		return iImageHeader->iExtRomHdr->iRomRootDirectoryList;
   226 		return iImageHeader->iExtRomHdr->iRomRootDirectoryList;
   238 }
   227 }
   239 
   228 
   240 TUint RomImageReader::GetHdrSize()
   229 TUint RomImageReader::GetHdrSize() {
   241 {
       
   242 	TUint headerSize = 0;
   230 	TUint headerSize = 0;
   243 	if(EROM_IMAGE == iImgType){
   231 	if(EROM_IMAGE == iImgType){
   244 		headerSize = sizeof(TRomLoaderHeader) + sizeof(TRomHeader);
   232 		headerSize = sizeof(TRomLoaderHeader) + sizeof(TRomHeader);
   245 	}else if(EROMX_IMAGE == iImgType){
   233 	}else if(EROMX_IMAGE == iImgType){
   246 		headerSize = sizeof(TExtensionRomHeader);
   234 		headerSize = sizeof(TExtensionRomHeader);
   247 	}else if(EBAREROM_IMAGE == iImgType){
   235 	}else if(EBAREROM_IMAGE == iImgType){
   248 		headerSize = sizeof(TRomHeader);
   236 		headerSize = sizeof(TRomHeader);
   249 	}
   237 	}
   250 	return headerSize;
   238 	return headerSize;
   251 }
   239 }
   252  
   240 
   253  
   241 
   254 static const TInt KIOBytes = 0x100000;
   242 static const TInt KIOBytes = 0x100000;
   255 // reading a huge buffer at a time is very slow, reading 1MB bytes at a time is much faster
   243 // reading a huge buffer at a time is very slow, reading 1MB bytes at a time is much faster
   256 void RomImageReader::ReadData(char* aBuffer, TUint aLength)
   244 void RomImageReader::ReadData(char* aBuffer, TUint aLength) {
   257 {
       
   258 	TUint readBytes = 0 ;
   245 	TUint readBytes = 0 ;
   259 	while(readBytes < aLength){
   246 	while(readBytes < aLength){
   260 		TUint toRead = KIOBytes;
   247 		TUint toRead = KIOBytes;
   261 		if(readBytes + toRead > aLength)
   248 		if(readBytes + toRead > aLength)
   262 			toRead = aLength - readBytes ;
   249 			toRead = aLength - readBytes ;
   263 		iFile.read(&aBuffer[readBytes],toRead);
   250 		iFile.read(&aBuffer[readBytes],toRead);
   264 		readBytes += toRead ;
   251 		readBytes += toRead ;
   265 	}	
   252 	}	
   266 }
   253 }
   267 void RomImageReader::ProcessImage()
   254 void RomImageReader::ProcessImage() {
   268 {
       
   269 	if(iRomLayoutData) return ;
   255 	if(iRomLayoutData) return ;
   270 	iImageHeader = new RomImageHeader(iHeaderBuffer, iImgType);
   256 	iImageHeader = new RomImageHeader(iHeaderBuffer, iImgType);
   271 
   257 
   272 	iFile.seekg(0, ios::end);
   258 	iFile.seekg(0, ios_base::end);
   273 	// fileSize
   259 	// fileSize
   274 	TUint fileSize = iFile.tellg(); 
   260 	TUint fileSize = iFile.tellg(); 
   275 
   261 
   276 	//let's skip the RomLoaderHeader
   262 	//let's skip the RomLoaderHeader
   277 	TUint romDataBegin = (EROM_IMAGE == iImgType) ? sizeof(TRomLoaderHeader) : 0 ; 
   263 	TUint romDataBegin = (EROM_IMAGE == iImgType) ? sizeof(TRomLoaderHeader) : 0 ; 
   278 	iFile.seekg(romDataBegin,ios::beg);	
   264 	iFile.seekg(romDataBegin,ios_base::beg);	
   279 
   265 
   280 	if(EROMX_IMAGE == iImgType){// EROMX_IMAGE, just set the iUnpagedRomBuffer		
   266 	if(EROMX_IMAGE == iImgType){// EROMX_IMAGE, just set the iUnpagedRomBuffer		
   281 		if(GetImageCompressionType() == KUidCompressionDeflate){
   267 		if(GetImageCompressionType() == KUidCompressionDeflate){
   282 			TUint32 readLen = fileSize - sizeof(TExtensionRomHeader) ; 
   268 			TUint32 readLen = fileSize - sizeof(TExtensionRomHeader) ; 
   283 			iRomSize = iImageHeader->iExtRomHdr->iUncompressedSize ;	 
   269 			iRomSize = iImageHeader->iExtRomHdr->iUncompressedSize ;	 
   287 			iFile.read(iRomLayoutData,sizeof(TExtensionRomHeader)); 
   273 			iFile.read(iRomLayoutData,sizeof(TExtensionRomHeader)); 
   288 			ReadData(temp ,readLen);
   274 			ReadData(temp ,readLen);
   289 			TUint8* uncompressDest = reinterpret_cast<TUint8*>(iRomLayoutData + sizeof(TExtensionRomHeader));
   275 			TUint8* uncompressDest = reinterpret_cast<TUint8*>(iRomLayoutData + sizeof(TExtensionRomHeader));
   290 			InflateUnCompress(reinterpret_cast<TUint8*>(temp),readLen,uncompressDest,iRomSize - sizeof(TExtensionRomHeader));
   276 			InflateUnCompress(reinterpret_cast<TUint8*>(temp),readLen,uncompressDest,iRomSize - sizeof(TExtensionRomHeader));
   291 			delete []temp ;	 
   277 			delete []temp ;	 
   292 			
   278 
   293 		}else{
   279 		}else{
   294 			iRomSize = fileSize  ;
   280 			iRomSize = fileSize  ;
   295 			iRomLayoutData = new char[iRomSize]; 
   281 			iRomLayoutData = new char[iRomSize]; 
   296 			ReadData(iRomLayoutData,iRomSize);
   282 			ReadData(iRomLayoutData,iRomSize);
   297 		}	 
   283 		}	 
   298 	} // end EROMX_IMAGE
   284 	} // end EROMX_IMAGE
   299 	else {
   285 	else {
   300 		 //EROM_IMAGE or EBAREROM_IMAGE
   286 		//EROM_IMAGE or EBAREROM_IMAGE
   301 		const TInt KPageSize = 0x1000; 
   287 		const TInt KPageSize = 0x1000; 
   302 		TRomHeader *hdr = iImageHeader->iRomHdr; 
   288 		TRomHeader *hdr = iImageHeader->iRomHdr; 
   303 		iRomSize = hdr->iUncompressedSize ; 
   289 		iRomSize = hdr->iUncompressedSize ; 
   304 		iRomLayoutData = new char[iRomSize]; 
   290 		iRomLayoutData = new char[iRomSize]; 
   305 		char* curDataPointer = iRomLayoutData ;
   291 		char* curDataPointer = iRomLayoutData ;
   306 		TUint totalReadBytes = 0;
   292 		TUint totalReadBytes = 0;
   307 		bool hasPageableSec = (hdr->iPageableRomStart > 0 && hdr->iPageableRomSize > 0) ;
   293 		bool hasPageableSec = (hdr->iPageableRomStart > 0 && hdr->iPageableRomSize > 0) ;
   308 		
   294 
   309 		// read data before unpaged 
   295 		// read data before unpaged 
   310 		ReadData(curDataPointer,hdr->iCompressedUnpagedStart); 
   296 		ReadData(curDataPointer,hdr->iCompressedUnpagedStart); 
   311 		curDataPointer += hdr->iCompressedUnpagedStart ; 
   297 		curDataPointer += hdr->iCompressedUnpagedStart ; 
   312 		totalReadBytes += hdr->iCompressedUnpagedStart;		
   298 		totalReadBytes += hdr->iCompressedUnpagedStart;		
   313 
   299 
   332 		//if there is a paged section , read and extract it 
   318 		//if there is a paged section , read and extract it 
   333 
   319 
   334 		// read the paged section,
   320 		// read the paged section,
   335 		if(hasPageableSec){		
   321 		if(hasPageableSec){		
   336 			if((TUint)(hdr->iPageableRomStart + hdr->iPageableRomSize) > iRomSize){
   322 			if((TUint)(hdr->iPageableRomStart + hdr->iPageableRomSize) > iRomSize){
   337 					throw ImageReaderException("Incorrect values of ROM header fields.", "");
   323 				throw ImageReaderException("Incorrect values of ROM header fields.", "");
   338 			}
   324 			}
   339 			if(0 == hdr->iRomPageIndex){ //
   325 			if(0 == hdr->iRomPageIndex){ //
   340 				// no compression for paged section ,just read it 			 
   326 				// no compression for paged section ,just read it 			 
   341 				iFile.read(curDataPointer,iRomSize - totalReadBytes);
   327 				iFile.read(curDataPointer,iRomSize - totalReadBytes);
   342 			}
   328 			}
   350 				}
   336 				}
   351 				TUint pagedIndexCount = ( hdr->iUncompressedSize + KPageSize - 1) / KPageSize;
   337 				TUint pagedIndexCount = ( hdr->iUncompressedSize + KPageSize - 1) / KPageSize;
   352 				// how many bytes ?
   338 				// how many bytes ?
   353 				// the page index table include the unpaged part ;
   339 				// the page index table include the unpaged part ;
   354 				TUint firstPagedIndexTblItem = hdr->iPageableRomStart / KPageSize;
   340 				TUint firstPagedIndexTblItem = hdr->iPageableRomStart / KPageSize;
   355 			 
   341 
   356 				TUint tempBufLen = KPageSize << 8;
   342 				TUint tempBufLen = KPageSize << 8;
   357 				char* readBuffer = new char[tempBufLen]; 
   343 				char* readBuffer = new char[tempBufLen]; 
   358 			 
   344 
   359 				TUint8* src,*srcNext = NULL;
   345 				TUint8* src,*srcNext = NULL;
   360 				SRomPageInfo* pageInfo = reinterpret_cast<SRomPageInfo*>(&iRomLayoutData[hdr->iRomPageIndex]);
   346 				SRomPageInfo* pageInfo = reinterpret_cast<SRomPageInfo*>(&iRomLayoutData[hdr->iRomPageIndex]);
   361 				CBytePair bp(EFalse);
   347 				CBytePair bpe;
   362 				for(TUint i = firstPagedIndexTblItem ; i < pagedIndexCount ; i+= 256){
   348 				for(TUint i = firstPagedIndexTblItem ; i < pagedIndexCount ; i+= 256){
   363 					TUint endIndex = i + 255 ;
   349 					TUint endIndex = i + 255 ;
   364 					if(endIndex >= pagedIndexCount)
   350 					if(endIndex >= pagedIndexCount)
   365 						endIndex = pagedIndexCount - 1;
   351 						endIndex = pagedIndexCount - 1;
   366 					TUint readLen = pageInfo[endIndex].iDataStart + pageInfo[endIndex].iDataSize - pageInfo[i].iDataStart ; 
   352 					TUint readLen = pageInfo[endIndex].iDataStart + pageInfo[endIndex].iDataSize - pageInfo[i].iDataStart ; 
   367 					iFile.read(readBuffer,readLen);
   353 					iFile.read(readBuffer,readLen);
   368 					src = reinterpret_cast<TUint8*>(readBuffer);
   354 					src = reinterpret_cast<TUint8*>(readBuffer);
   369 					for(TUint j = i ; j <= endIndex ; j++){
   355 					for(TUint j = i ; j <= endIndex ; j++){
   370 						switch(pageInfo[j].iCompressionType)
   356 						switch(pageInfo[j].iCompressionType) {
   371 						{
   357 						case SRomPageInfo::EBytePair: {
   372 						case SRomPageInfo::EBytePair:
   358 							TInt unpacked = bpe.Decompress(reinterpret_cast<TUint8*>(curDataPointer), KPageSize, src, pageInfo[j].iDataSize, srcNext);
   373 							{
       
   374 							TInt unpacked = bp.Decompress(reinterpret_cast<TUint8*>(curDataPointer), KPageSize, src, pageInfo[j].iDataSize, srcNext);
       
   375 							if (unpacked < 0) {
   359 							if (unpacked < 0) {
   376 								delete []readBuffer;
   360 								delete []readBuffer;
   377 								throw ImageReaderException("Corrupted BytePair compressed ROM image", "");
   361 								throw ImageReaderException("Corrupted BytePair compressed ROM image", "");
   378 							}
   362 							}
   379 							curDataPointer += unpacked;
   363 							curDataPointer += unpacked;
   380 							break ;
   364 							break ;
   381 							}
   365 													  }
   382 						case SRomPageInfo::ENoCompression:
   366 						case SRomPageInfo::ENoCompression:
   383 							memcpy(curDataPointer,src,pageInfo[j].iDataSize);
   367 							memcpy(curDataPointer,src,pageInfo[j].iDataSize);
   384 							curDataPointer +=  pageInfo[j].iDataSize ;
   368 							curDataPointer +=  pageInfo[j].iDataSize ;
   385 							break ;
   369 							break ;
   386 						default:
   370 						default:
   387 							delete []readBuffer;
   371 							delete []readBuffer;
   388 							throw ImageReaderException("Undefined compression type", "");
   372 							throw ImageReaderException("Undefined compression type", "");
   389 							break ;
   373 							break ;
   390 						
   374 
   391 						}
   375 						}
   392 						src += pageInfo[j].iDataSize; 
   376 						src += pageInfo[j].iDataSize; 
   393 					} // end for(TUint j = i ; j <= endIndex ; j++) 
   377 					} // end for(TUint j = i ; j <= endIndex ; j++) 
   394 				} // end for(TUint i = firstPagedIndexTblItem ; i < pagedIndexCount ; i+= 256) 
   378 				} // end for(TUint i = firstPagedIndexTblItem ; i < pagedIndexCount ; i+= 256) 
   395 				delete []readBuffer ;
   379 				delete []readBuffer ;
   396 			}// else
   380 			}// else
   397 			 
   381 
   398 		} // if(hasPageableSec)  
   382 		} // if(hasPageableSec)  
   399 
   383 
   400 	}
   384 	}
   401 	TUint32 offset = GetRootDirList() - GetRomBase(); 
   385 	TUint32 offset = GetRootDirList() - GetRomBase(); 
   402 	TRomRootDirectoryList* rootDirList = reinterpret_cast<TRomRootDirectoryList*>(iRomLayoutData + offset );
   386 	TRomRootDirectoryList* rootDirList = reinterpret_cast<TRomRootDirectoryList*>(iRomLayoutData + offset );
   403 	TInt dirCount = rootDirList->iNumRootDirs ; 
   387 	TInt dirCount = rootDirList->iNumRootDirs ; 
   404 	string tempName ;
   388 	string tempName ;
   405 	for(TInt i = 0 ; i < dirCount ; i++) {
   389 	for(TInt i = 0 ; i < dirCount ; i++) {
   406 		offset = rootDirList->iRootDir[i].iAddressLin - GetRomBase(); 
   390 		offset = rootDirList->iRootDir[i].iAddressLin - GetRomBase(); 
   407 		TRomDir* romDir = reinterpret_cast<TRomDir*>(iRomLayoutData + offset);		
   391 		TRomDir* romDir = reinterpret_cast<TRomDir*>(iRomLayoutData + offset);	
   408 		Name(tempName,reinterpret_cast<const wchar_t *>(romDir->iEntry.iName),romDir->iEntry.iNameLength); 		 
   392 		UTF16String unistr(reinterpret_cast<const TUint16*>(romDir->iEntry.iName),romDir->iEntry.iNameLength); 
       
   393 		if(!unistr.ToUTF8(tempName)) //not utf16?
       
   394 			tempName.assign(reinterpret_cast<const char*>(romDir->iEntry.iName),romDir->iEntry.iNameLength) ;
   409 		BuildDir(romDir, iRomImageRootDirEntry);		 
   395 		BuildDir(romDir, iRomImageRootDirEntry);		 
   410 	}
   396 	}
   411  
   397 
   412 }
   398 }
   413  
   399 
   414  
   400 
   415 void RomImageReader::BuildDir(TRomDir* aDir, RomImageFSEntry* aPaFSEntry)
   401 void RomImageReader::BuildDir(TRomDir* aDir, RomImageFSEntry* aPaFSEntry)
   416 {	 
   402 {	 
   417 	
   403 
   418 	TInt processBytes = 0 ;
   404 	TInt processBytes = 0 ;
   419 	TInt totalDirBytes = aDir->iSize - sizeof(aDir->iSize);
   405 	TInt totalDirBytes = aDir->iSize - sizeof(aDir->iSize);
   420 	TRomEntry* entry = &aDir->iEntry;
   406 	TRomEntry* entry = &aDir->iEntry;
   421 	RomImageFSEntry *fsEntry ;
   407 	RomImageFSEntry *fsEntry ;
   422 	string name ;
   408 	string pritableName ; 
   423 	const char* pritableName = "";
   409 
   424 
   410 	while(processBytes < totalDirBytes){ 
   425 	while(processBytes < totalDirBytes){
   411 		UTF16String unistr(reinterpret_cast<const TUint16*>(entry->iName),entry->iNameLength);
   426 		Name(name,reinterpret_cast<const wchar_t *>(entry->iName),entry->iNameLength);
   412 		if(!unistr.ToUTF8(pritableName)) 
   427 		pritableName = name.c_str(); 
   413 			pritableName.assign(reinterpret_cast<const char*>(entry->iName),entry->iNameLength);
   428 		if(entry->iAtt & 0x10) { // is a directory
   414 		if(entry->iAtt & 0x10) { // is a directory
   429 			fsEntry = new RomImageDirEntry(pritableName); 
   415 			fsEntry = new RomImageDirEntry(pritableName.c_str()); 
   430 			AddChild(aPaFSEntry,fsEntry,NULL);
   416 			AddChild(aPaFSEntry,fsEntry,NULL);
   431 			TUint offset = entry->iAddressLin - GetRomBase();
   417 			TUint offset = entry->iAddressLin - GetRomBase();
   432 			TRomDir* subFolder = reinterpret_cast<TRomDir*>(iRomLayoutData + offset);		 
   418 			TRomDir* subFolder = reinterpret_cast<TRomDir*>(iRomLayoutData + offset);		 
   433 			BuildDir(subFolder,fsEntry); 
   419 			BuildDir(subFolder,fsEntry); 
   434 		}
   420 		}
   435 		else{
   421 		else{
   436 			fsEntry = new RomImageFileEntry(pritableName); 
   422 			fsEntry = new RomImageFileEntry(pritableName.c_str()); 
   437 			AddChild(aPaFSEntry,fsEntry,entry);
   423 			AddChild(aPaFSEntry,fsEntry,entry);
   438 		} 
   424 		} 
   439 		// increase the processedBytes
   425 		// increase the processedBytes
   440 		processBytes += (entry->iNameLength << 1) +  reinterpret_cast<TInt>(entry->iName) - reinterpret_cast<TInt>(entry); 
   426 		processBytes += (entry->iNameLength << 1) +  reinterpret_cast<TInt>(entry->iName) - reinterpret_cast<TInt>(entry); 
   441 
   427 
   442 		//align to next 4 bytes
   428 		//align to next 4 bytes
   443 		processBytes = (processBytes + 3) & ( ~3 ); 
   429 		processBytes = (processBytes + 3) & ( ~3 ); 
   444 		// get next entry
   430 		// get next entry
   445 		entry =  reinterpret_cast<TRomEntry*>( reinterpret_cast<char*>(&aDir->iEntry) + processBytes);
   431 		entry =  reinterpret_cast<TRomEntry*>( reinterpret_cast<char*>(&aDir->iEntry) + processBytes);
   446 	} 
   432 	} 
   447 	
   433 
   448 }
   434 }
   449 
   435 
   450 void RomImageReader::AddChild(RomImageFSEntry *aParent, RomImageFSEntry *aChild, TRomEntry* aRomEntry)
   436 void RomImageReader::AddChild(RomImageFSEntry *aParent, RomImageFSEntry *aChild, TRomEntry* aRomEntry) {
   451 {
       
   452 	if(!aParent->iChildren)
   437 	if(!aParent->iChildren)
   453 		aParent->iChildren = aChild;
   438 		aParent->iChildren = aChild;
   454 	else {
   439 	else {
   455 		RomImageFSEntry *aLast = aParent->iChildren;
   440 		RomImageFSEntry *aLast = aParent->iChildren;
   456 
   441 
   463 		RomImageFileEntry* file = static_cast<RomImageFileEntry*>(aChild);
   448 		RomImageFileEntry* file = static_cast<RomImageFileEntry*>(aChild);
   464 		file->iTRomEntryPtr = aRomEntry;
   449 		file->iTRomEntryPtr = aRomEntry;
   465 
   450 
   466 		if(aRomEntry->iAddressLin > GetRomBase()) {
   451 		if(aRomEntry->iAddressLin > GetRomBase()) {
   467 			TUint32 offset = aRomEntry->iAddressLin - GetRomBase();	 
   452 			TUint32 offset = aRomEntry->iAddressLin - GetRomBase();	 
   468 		 
   453 
   469 			TRomImageHeader* imgHdr = reinterpret_cast<TRomImageHeader*>(iRomLayoutData + offset);
   454 			TRomImageHeader* imgHdr = reinterpret_cast<TRomImageHeader*>(iRomLayoutData + offset);
   470 			 
   455 
   471 			file->ImagePtr.iRomFileEntry = imgHdr;
   456 			file->ImagePtr.iRomFileEntry = imgHdr;
   472 
   457 
   473 			TUint8 aUid1[4];
   458 			TUint8 aUid1[4];
   474 			memcpy(aUid1, &file->ImagePtr.iRomFileEntry->iUid1, 4);
   459 			memcpy(aUid1, &file->ImagePtr.iRomFileEntry->iUid1, 4);
   475 
   460 
   486 		}
   471 		}
   487 	}
   472 	}
   488 
   473 
   489 	if(aParent != iRomImageRootDirEntry) {
   474 	if(aParent != iRomImageRootDirEntry) {
   490 		aChild->iPath = aParent->iPath;
   475 		aChild->iPath = aParent->iPath;
   491 		aChild->iPath += DIR_SEPARATOR;
   476 		aChild->iPath += SLASH_CHAR1;
   492 		aChild->iPath += aParent->iName.c_str();
   477 		aChild->iPath += aParent->iName.c_str();
   493 	}
   478 	}
   494 }
   479 }
   495 
   480 
   496 void RomImageReader::Name(string& aName, const wchar_t* aUnicodeName, TUint aLen)
   481 void RomImageReader::DumpTree() {
   497 {
       
   498 	char* temp = (char*)_alloca((aLen << 1) + 1) ;
       
   499 	size_t n = wcsrtombs(temp,&aUnicodeName,aLen,NULL);
       
   500 	if(n == (size_t)-1){ // the unicode string can not be coverted.
       
   501 		aName = "???";
       
   502 	}
       
   503 	temp[n] = 0;
       
   504 	if(n > 0)
       
   505 		aName.assign(temp,n) ;
       
   506 	else
       
   507 		aName = "";
       
   508  
       
   509 }
       
   510 
       
   511 void RomImageReader::DumpTree()
       
   512 {
       
   513 	RomImageFSEntry* aFsEntry = iRomImageRootDirEntry;
   482 	RomImageFSEntry* aFsEntry = iRomImageRootDirEntry;
   514 	if( aFsEntry->iChildren ) 
   483 	if( aFsEntry->iChildren ) 
   515 		DumpSubTree(aFsEntry->iChildren); 
   484 		DumpSubTree(aFsEntry->iChildren); 
   516 }
   485 }
   517 
   486 
   518 void RomImageReader::DumpDirStructure()
   487 void RomImageReader::DumpDirStructure() {
   519 {
       
   520 	*out << "Directory Listing" << endl;
   488 	*out << "Directory Listing" << endl;
   521 	*out << "=================" << endl;
   489 	*out << "=================" << endl;
   522 	int aPadding = 0;
   490 	int aPadding = 0;
   523 	if( iRomImageRootDirEntry ){
   491 	if( iRomImageRootDirEntry ){
   524 		DumpDirStructure(iRomImageRootDirEntry, aPadding);
   492 		DumpDirStructure(iRomImageRootDirEntry, aPadding);
   525 	}	
   493 	}	
   526 	*out << endl << endl;
   494 	*out << endl << endl;
   527 }
   495 }
   528 
   496 
   529 void RomImageReader::DumpDirStructure(RomImageFSEntry* aEntry, int &aPadding)
   497 void RomImageReader::DumpDirStructure(RomImageFSEntry* aEntry, int &aPadding) {
   530 {
       
   531 	if(!aEntry)
   498 	if(!aEntry)
   532 		return;
   499 		return;
   533 
   500 
   534 	int aPadLen = 2 * aPadding;//scaling for legibility
   501 	int aPadLen = 2 * aPadding;//scaling for legibility
   535 	for (int i = 0; i < aPadLen; i++)
   502 	for (int i = 0; i < aPadLen; i++)
   543 		aPadding--;
   510 		aPadding--;
   544 	}
   511 	}
   545 	DumpDirStructure(aEntry->iSibling, aPadding);
   512 	DumpDirStructure(aEntry->iSibling, aPadding);
   546 }
   513 }
   547 
   514 
   548 void RomImageReader::DumpSubTree(RomImageFSEntry* aFsEntry)
   515 void RomImageReader::DumpSubTree(RomImageFSEntry* aFsEntry) {
   549 {
       
   550 	if(!aFsEntry)
   516 	if(!aFsEntry)
   551 		return;
   517 		return;
   552 
   518 
   553 	if( aFsEntry->iChildren ){
   519 	if( aFsEntry->iChildren ){
   554 		DumpSubTree(aFsEntry->iChildren);
   520 		DumpSubTree(aFsEntry->iChildren);
   556 
   522 
   557 	if( aFsEntry->iSibling )
   523 	if( aFsEntry->iSibling )
   558 		DumpSubTree(aFsEntry->iSibling);
   524 		DumpSubTree(aFsEntry->iSibling);
   559 
   525 
   560 	RomImageFSEntry *aEntry = aFsEntry;
   526 	RomImageFSEntry *aEntry = aFsEntry;
   561 	
   527 
   562 	if(!aEntry->IsDirectory()) {
   528 	if(!aEntry->IsDirectory()) {
   563 		*out << "********************************************************************" << endl;
   529 		*out << "********************************************************************" << endl;
   564 		*out << "File........................" << aEntry->iPath.c_str() << DIR_SEPARATOR << aEntry->Name() << endl;
   530 		*out << "File........................" << aEntry->iPath.c_str() << SLASH_CHAR1 << aEntry->Name() << endl;
   565 
   531 
   566 		if( ((RomImageFileEntry*)aEntry)->iExecutable )	{
   532 		if( ((RomImageFileEntry*)aEntry)->iExecutable )	{
   567 			DumpImage((RomImageFileEntry*)aEntry);
   533 			DumpImage((RomImageFileEntry*)aEntry);
   568 		}
   534 		}
   569 		else{
   535 		else{
   570 			*out << "Linear Addr................." << ((RomImageFileEntry*)aEntry)->ImagePtr.iDataFileAddr << endl;
   536 			*out << "Linear Addr................." << ((RomImageFileEntry*)aEntry)->ImagePtr.iDataFileAddr << endl;
   571 		}
   537 		}
   572 	}
   538 	}
   573 }
   539 }
   574 
   540 
   575 void RomImageReader::DumpImage(RomImageFileEntry * aEntry)
   541 void RomImageReader::DumpImage(RomImageFileEntry * aEntry) {
   576 {
       
   577 	bool aContinue = true;
   542 	bool aContinue = true;
   578 
   543 
   579 	DumpInHex("Load Address", aEntry->iTRomEntryPtr->iAddressLin) << endl;
   544 	DumpInHex("Load Address", aEntry->iTRomEntryPtr->iAddressLin) << endl;
   580 	DumpInHex("Size", aEntry->iTRomEntryPtr->iSize) << endl;
   545 	DumpInHex("Size", aEntry->iTRomEntryPtr->iSize) << endl;
   581 
   546 
   618 	DumpInHex("Secure ID", aRomImgEntry->iS.iSecureId) << endl;
   583 	DumpInHex("Secure ID", aRomImgEntry->iS.iSecureId) << endl;
   619 	DumpInHex("Vendor ID", aRomImgEntry->iS.iVendorId) << endl;
   584 	DumpInHex("Vendor ID", aRomImgEntry->iS.iVendorId) << endl;
   620 
   585 
   621 	DumpInHex("Capability", aRomImgEntry->iS.iCaps[1]);
   586 	DumpInHex("Capability", aRomImgEntry->iS.iCaps[1]);
   622 	DumpInHex(" ", aRomImgEntry->iS.iCaps[0], aContinue) << endl;
   587 	DumpInHex(" ", aRomImgEntry->iS.iCaps[0], aContinue) << endl;
   623 	
   588 
   624 	*out << "Tools Version..............." << dec << (TUint)aRomImgEntry->iToolsVersion.iMajor;
   589 	*out << "Tools Version..............." << dec << (TUint)aRomImgEntry->iToolsVersion.iMajor;
   625 	*out << "." ; 
   590 	*out << "." ; 
   626 	out->width(2) ;
   591 	out->width(2) ;
   627 	out->fill('0');
   592 	out->fill('0');
   628 	*out << dec << (TUint)aRomImgEntry->iToolsVersion.iMinor ;
   593 	*out << dec << (TUint)aRomImgEntry->iToolsVersion.iMinor ;
   655 	}
   620 	}
   656 
   621 
   657 	*out << endl << endl;
   622 	*out << endl << endl;
   658 }
   623 }
   659 
   624 
   660 void RomImageReader::DumpAttribs(RomImageFSEntry* aFsEntry)
   625 void RomImageReader::DumpAttribs(RomImageFSEntry* aFsEntry) {
   661 {
   626 
   662  
   627 	// a larger rom image cause stack overflow under visual studio if we use recursion algorithm here.
   663 // a larger rom image cause stack overflow under visual studio if we use recursion algorithm here.
   628 	// can gcc compiler guarantee this overflow will never be happen ?
   664 // can gcc compiler guarantee this overflow will never be happen ?
   629 	if( aFsEntry->iChildren )
   665  	if( aFsEntry->iChildren )
       
   666 		DumpAttribs(aFsEntry->iChildren);
   630 		DumpAttribs(aFsEntry->iChildren);
   667 
   631 
   668 	if(aFsEntry->iSibling)
   632 	if(aFsEntry->iSibling)
   669 		DumpAttribs(aFsEntry->iSibling);
   633 		DumpAttribs(aFsEntry->iSibling);
   670 	if(aFsEntry->IsDirectory()) return ;
   634 	if(aFsEntry->IsDirectory()) return ;
   671 	RomImageFileEntry* file = static_cast<RomImageFileEntry*>(aFsEntry);
   635 	RomImageFileEntry* file = static_cast<RomImageFileEntry*>(aFsEntry);
   672 	if(!file->iExecutable) return ; 
   636 	if(!file->iExecutable) return ; 
   673 	TRomImageHeader* aRomImgEntry = file->ImagePtr.iRomFileEntry;
   637 	TRomImageHeader* aRomImgEntry = file->ImagePtr.iRomFileEntry;
   674 
   638 
   675 	if( !aRomImgEntry)  return; 
   639 	if( !aRomImgEntry)  return; 
   676 	
   640 
   677 	const char* prefix ;
   641 	const char* prefix ;
   678 	if(aRomImgEntry->iFlags & KRomImageFlagPrimary){
   642 	if(aRomImgEntry->iFlags & KRomImageFlagPrimary){
   679 		prefix = "Primary";
   643 		prefix = "Primary";
   680 	}
   644 	}
   681 	else if(aRomImgEntry->iFlags & KRomImageFlagVariant){
   645 	else if(aRomImgEntry->iFlags & KRomImageFlagVariant){
   695 	*out << left << prefix;
   659 	*out << left << prefix;
   696 	out->width(40);	
   660 	out->width(40);	
   697 	*out << right << file->Name() << "[" ;
   661 	*out << right << file->Name() << "[" ;
   698 	DumpInHex( "", aRomImgEntry->iHardwareVariant, true) << "] ";
   662 	DumpInHex( "", aRomImgEntry->iHardwareVariant, true) << "] ";
   699 	DumpInHex( " DataSize=", (aRomImgEntry->iBssSize + aRomImgEntry->iDataSize), true) << endl;
   663 	DumpInHex( " DataSize=", (aRomImgEntry->iBssSize + aRomImgEntry->iDataSize), true) << endl;
   700 	
   664 
   701 }
   665 }
   702 
   666 
   703 void RomImageReader::Dump()
   667 void RomImageReader::Dump() {
   704 {
       
   705 	if( !((iDisplayOptions & EXTRACT_FILES_FLAG) || 
   668 	if( !((iDisplayOptions & EXTRACT_FILES_FLAG) || 
   706 		(iDisplayOptions & LOG_IMAGE_CONTENTS_FLAG) ||
   669 		(iDisplayOptions & LOG_IMAGE_CONTENTS_FLAG) ||
   707 		(iDisplayOptions & EXTRACT_FILE_SET_FLAG)) ) {
   670 		(iDisplayOptions & EXTRACT_FILE_SET_FLAG)) ) {
   708 		*out << "Image Name................." << iImgFileName.c_str() << endl;
   671 			*out << "Image Name................." << iImgFileName.c_str() << endl;
   709 
   672 
   710 		if( iImageHeader->iRomHdr )
   673 			if( iImageHeader->iRomHdr )
   711 		iImageHeader->DumpRomHdr();
   674 				iImageHeader->DumpRomHdr();
   712 		else
   675 			else
   713 		iImageHeader->DumpRomXHdr();
   676 				iImageHeader->DumpRomXHdr();
   714 
   677 
   715 		DumpAttribs(iRomImageRootDirEntry);
   678 			DumpAttribs(iRomImageRootDirEntry);
   716 		*out << endl ;
   679 			*out << endl ;
   717 
   680 
   718 		if(iDisplayOptions & DUMP_VERBOSE_FLAG) {
   681 			if(iDisplayOptions & DUMP_VERBOSE_FLAG) {
   719 			DumpDirStructure();
   682 				DumpDirStructure();
   720 			DumpTree();
   683 				DumpTree();
   721 		}
   684 			}
   722 
   685 
   723 		if(iDisplayOptions & DUMP_DIR_ENTRIES_FLAG) {
   686 			if(iDisplayOptions & DUMP_DIR_ENTRIES_FLAG) {
   724 			DumpDirStructure();
   687 				DumpDirStructure();
   725 		}
   688 			}
   726 	}
   689 	}
   727 }
   690 }
   728 
   691 
   729 
   692 
   730 /** 
   693 /** 
   732 by making a call to TraverseImage function.
   695 by making a call to TraverseImage function.
   733 
   696 
   734 @internalComponent
   697 @internalComponent
   735 @released
   698 @released
   736 */
   699 */
   737 void RomImageReader::ExtractImageContents()
   700 void RomImageReader::ExtractImageContents() {
   738 {
   701 	if( (iDisplayOptions & EXTRACT_FILE_SET_FLAG) ) { 
   739 	if( (iDisplayOptions & EXTRACT_FILE_SET_FLAG) )
       
   740 	{
       
   741 		//TODO:
       
   742 		ImageReader::ExtractFileSet(iRomLayoutData);
   702 		ImageReader::ExtractFileSet(iRomLayoutData);
   743 	}
   703 	}
   744 
   704 
   745 	if( iDisplayOptions & EXTRACT_FILES_FLAG || iDisplayOptions & LOG_IMAGE_CONTENTS_FLAG )
   705 	if( iDisplayOptions & EXTRACT_FILES_FLAG || iDisplayOptions & LOG_IMAGE_CONTENTS_FLAG ) {
   746 	{
   706 		if(iRomImageRootDirEntry) {
   747 		if(iRomImageRootDirEntry)
       
   748 		{
       
   749 			// name of the log file.
   707 			// name of the log file.
   750 			string logFile;
   708 			string logFile;
   751 			// output stream for the log file.
   709 			// output stream for the log file. 
   752 			ofstream oFile;
   710 			ofstream oFile;
   753 			
   711 
   754 			if( iDisplayOptions & LOG_IMAGE_CONTENTS_FLAG){
   712 			if( iDisplayOptions & LOG_IMAGE_CONTENTS_FLAG){
   755 				if( ImageReader::iZdrivePath.compare("")){
   713 				if( ImageReader::iZdrivePath.compare("")){				 
   756 					// create a string to hold path information.
       
   757 					string filePath(ImageReader::iZdrivePath);
       
   758 					string delimiter("\\"); 
       
   759 					// replace backslash with double backslash. 
       
   760 					FindAndInsertString(filePath,delimiter,delimiter);
       
   761 					logFile = filePath;
       
   762 					// create specified directory.
   714 					// create specified directory.
   763 					CreateSpecifiedDir(&filePath[0],"\\\\");
   715 					CreateSpecifiedDir(ImageReader::iZdrivePath); 
   764 					logFile.append("\\\\");
   716 					int len = ImageReader::iZdrivePath.length() ;
   765 					logFile.append(ImageReader::iLogFileName);
   717 					const char* z = ImageReader::iZdrivePath.c_str();
       
   718 					logFile = "";
       
   719 					for(int i = 0 ; i < len ; i++){
       
   720 						if(z[i] == SLASH_CHAR2)
       
   721 							logFile += SLASH_CHAR1;
       
   722 						else
       
   723 							logFile += z[i];
       
   724 					}
       
   725 					len -- ;
       
   726 					if(z[len] != SLASH_CHAR1)
       
   727 						logFile += SLASH_CHAR1;
       
   728 					logFile += ImageReader::iLogFileName ;
   766 				}
   729 				}
   767 				else
   730 				else {				
   768 				{				
       
   769 					logFile = ImageReader::iLogFileName;
   731 					logFile = ImageReader::iLogFileName;
   770 				}
   732 				}
   771 				// open the specified file in append mode.
   733 				// open the specified file in append mode.
   772 				oFile.open(logFile.c_str() ,ios::out|ios::app);
   734 				oFile.open(logFile.c_str() ,ios_base::out|ios_base::app);
   773 
   735 
   774 				if(!oFile.is_open()){
   736 				if(!oFile.is_open()){
   775 					throw ImageReaderException((char*)ImageReader::iLogFileName.c_str(), "Failed to open the log file");
   737 					throw ImageReaderException(ImageReader::iLogFileName.c_str(), "Failed to open the log file");
   776 				}
   738 				}
   777 			}
   739 			}
   778 			TraverseImage(iRomImageRootDirEntry,oFile);
   740 			TraverseImage(iRomImageRootDirEntry,oFile);
   779 			if(oFile.is_open())  oFile.close();  
   741 			if(oFile.is_open())  oFile.close();  
   780 		}
   742 		}
   790 @released
   752 @released
   791 
   753 
   792 @param aEntity		- pointer to the entry in rom image.
   754 @param aEntity		- pointer to the entry in rom image.
   793 @param aFile		- output stream.
   755 @param aFile		- output stream.
   794 */
   756 */
   795 void RomImageReader::TraverseImage(RomImageFSEntry*  aEntity,ofstream& aFile)
   757 void RomImageReader::TraverseImage(RomImageFSEntry*  aEntity,ofstream& aFile) {
   796 {
       
   797 	if(!aEntity->IsDirectory())	{
   758 	if(!aEntity->IsDirectory())	{
   798 		CheckFileExtension(aEntity,aFile);
   759 		CheckFileExtension(aEntity,aFile);
   799 	}
   760 	}
   800 	
   761 
   801  	if (aEntity->iChildren)	{
   762 	if (aEntity->iChildren)	{
   802 		TraverseImage(aEntity->iChildren,aFile);
   763 		TraverseImage(aEntity->iChildren,aFile);
   803 	}
   764 	}
   804 
   765 
   805 	if (aEntity->iSibling)	{
   766 	if (aEntity->iSibling)	{
   806 		TraverseImage(aEntity->iSibling,aFile);
   767 		TraverseImage(aEntity->iSibling,aFile);
   816 @released
   777 @released
   817 
   778 
   818 @param aEntity		- pointer to the entry in rom image.
   779 @param aEntity		- pointer to the entry in rom image.
   819 @param aFile		- output stream.
   780 @param aFile		- output stream.
   820 */
   781 */
   821 void RomImageReader::CheckFileExtension(RomImageFSEntry*  aEntity,ofstream& aFile)
   782 void RomImageReader::CheckFileExtension(RomImageFSEntry*  aEntity,ofstream& aFile) {
   822 {
       
   823 	RomImageFileEntry*	romEntry = (RomImageFileEntry*)aEntity;
   783 	RomImageFileEntry*	romEntry = (RomImageFileEntry*)aEntity;
   824 	// get the size of the entity.
   784 	// get the size of the entity.
   825 	TUint32 size = romEntry->iTRomEntryPtr->iSize;
   785 	TUint32 size = romEntry->iTRomEntryPtr->iSize;
   826 	// get the offset of the entity.
   786 	// get the offset of the entity.
   827 	TUint32 offset =  romEntry->iTRomEntryPtr->iAddressLin - GetRomBase() ;
   787 	TUint32 offset =  romEntry->iTRomEntryPtr->iAddressLin - GetRomBase() ;
   828 
   788 
   829 	const char* fileName = aEntity->iName.c_str();
   789 	const char* fileName = aEntity->iName.c_str();
   830 
   790 
   831 	// create a string to hold the path information.
   791 	// create a string to hold the path information.
   832 	string romfilePath(romEntry->iPath);
   792 	string romfilePath(romEntry->iPath);
   833 	string forwardSlash("/");
   793 	int len = romfilePath.length();
   834 	string slash("\\");  
   794 	char* str = const_cast<char*>(romfilePath.c_str());
   835 	//replace slash with double backward slash.
   795 	for(int i = 0 ; i < len ; i++){
   836 	FindAndReplaceString( romfilePath, forwardSlash, slash );
   796 		if(str[i] == SLASH_CHAR2)
   837 	
   797 			str[i] = SLASH_CHAR1;
       
   798 	}
       
   799 	if(str[len - 1] != SLASH_CHAR1)
       
   800 		romfilePath += SLASH_CHAR1;
       
   801 
   838 	if( iDisplayOptions & LOG_IMAGE_CONTENTS_FLAG && iDisplayOptions & EXTRACT_FILES_FLAG ) {
   802 	if( iDisplayOptions & LOG_IMAGE_CONTENTS_FLAG && iDisplayOptions & EXTRACT_FILES_FLAG ) {
   839 		// get the position.
   803 		// get the position.
   840 		size_t pos = aEntity->iName.find_last_of(".");
   804 		size_t pos = aEntity->iName.find_last_of(".");
   841 		
   805 
   842 		const char* extName = fileName + ( pos + 1 );
   806 		const char* extName = fileName + ( pos + 1 );
   843 		if ( !stricmp(extName ,"SIS")	||  !stricmp(extName ,"DAT") ) {
   807 		if ( !stricmp(extName ,"SIS")	||  !stricmp(extName ,"DAT") ) {
   844 			// if the two strings are same then extract the corresponding file.
   808 			// if the two strings are same then extract the corresponding file.
   845 			ImageReader::ExtractFile(offset,size,fileName,&romfilePath[0],&ImageReader::iZdrivePath[0],iRomLayoutData);
   809 			ImageReader::ExtractFile(offset,size,fileName,romfilePath.c_str(),ImageReader::iZdrivePath.c_str(),iRomLayoutData);
   846 		}
   810 		}
   847 		else {
   811 		else {
   848 			LogRomEnrtyToFile(romfilePath.c_str(),fileName,aFile);
   812 			LogRomEnrtyToFile(romfilePath.c_str(),fileName,aFile);
   849 		}
   813 		}
   850 	}
   814 	}
   854 	else {
   818 	else {
   855 		if(romEntry->iExecutable) {
   819 		if(romEntry->iExecutable) {
   856 			size += sizeof(TRomImageHeader);
   820 			size += sizeof(TRomImageHeader);
   857 		}
   821 		}
   858 		// extract the corresponding file. 
   822 		// extract the corresponding file. 
   859 		ImageReader::ExtractFile(offset,size,fileName,&romfilePath[0],&ImageReader::iZdrivePath[0],iRomLayoutData);
   823 		ImageReader::ExtractFile(offset,size,fileName,romfilePath.c_str(),ImageReader::iZdrivePath.c_str(),iRomLayoutData);
   860 	}
   824 	}
   861 }
   825 }
   862 
   826 
   863 
   827 
   864 /** 
   828 /** 
   869 
   833 
   870 @param aPath		- Complete path of an entity.
   834 @param aPath		- Complete path of an entity.
   871 @param aEntityName	- Entity name. 
   835 @param aEntityName	- Entity name. 
   872 @param aFile		- output stream.
   836 @param aFile		- output stream.
   873 */
   837 */
   874 void RomImageReader::LogRomEnrtyToFile(const char* aPath,const char* aEntityName,ofstream& aFile)
   838 void RomImageReader::LogRomEnrtyToFile(const char* aPath,const char* aEntityName,ofstream& aFile) {
   875 {
   839 	if(aFile.is_open()) {
   876 	if(aFile.is_open())
   840 		aFile.seekp(0,ios_base::end);
   877 	{
   841 		aFile<<aPath <<aEntityName << endl;
   878 		aFile.seekp(0,ios::end);
       
   879 		aFile<<aPath<<"\\"<<aEntityName<<"\n";
       
   880 	}
   842 	}
   881 }
   843 }
   882 
   844 
   883 
   845 
   884 /** 
   846 /** 
   889 
   851 
   890 @param aEntity		- pointer to the entry in rom image.
   852 @param aEntity		- pointer to the entry in rom image.
   891 @param aFileMap		- map of filename with its size and offset values.
   853 @param aFileMap		- map of filename with its size and offset values.
   892 @param aImgSize		- Image size
   854 @param aImgSize		- Image size
   893 */
   855 */
   894 void RomImageReader::ProcessDirectory(RomImageFSEntry *aEntity, FILEINFOMAP &aFileMap)
   856 void RomImageReader::ProcessDirectory(RomImageFSEntry *aEntity, FILEINFOMAP &aFileMap) {
   895 {
       
   896 	if(!aEntity->IsDirectory()) { 
   857 	if(!aEntity->IsDirectory()) { 
   897 
   858 
   898 		RomImageFileEntry*	romEntry = (RomImageFileEntry*)aEntity;
   859 		RomImageFileEntry*	romEntry = (RomImageFileEntry*)aEntity;
   899 
   860 
   900 		PFILEINFO fileInfo = new FILEINFO;
   861 		PFILEINFO fileInfo = new FILEINFO;
   912 			fileInfo->iOffset = 0;
   873 			fileInfo->iOffset = 0;
   913 			fileInfo->iSize = 0;
   874 			fileInfo->iSize = 0;
   914 		}
   875 		}
   915 
   876 
   916 		string fileName(romEntry->iPath);
   877 		string fileName(romEntry->iPath);
   917 		fileName.append(DIR_SEPARATOR);
   878 		fileName += SLASH_CHAR1;
   918 		fileName.append(aEntity->iName);
   879 		fileName.append(aEntity->iName);
   919 
       
   920 		aFileMap[fileName] = fileInfo;
   880 		aFileMap[fileName] = fileInfo;
   921 	}
   881 	}
   922 	
   882 
   923  	if (aEntity->iChildren) {
   883 	if (aEntity->iChildren) {
   924 		ProcessDirectory(aEntity->iChildren, aFileMap);
   884 		ProcessDirectory(aEntity->iChildren, aFileMap);
   925 	}
   885 	}
   926 
   886 
   927 	if (aEntity->iSibling) {
   887 	if (aEntity->iSibling) {
   928 		ProcessDirectory(aEntity->iSibling, aFileMap);
   888 		ProcessDirectory(aEntity->iSibling, aFileMap);