imgtools/romtools/rofsbuild/r_build.cpp
changeset 708 0757c2976f96
parent 705 67449e76cb84
equal deleted inserted replaced
707:ccd52fece6ff 708:0757c2976f96
    66 extern TInt  gCodePagingOverride;
    66 extern TInt  gCodePagingOverride;
    67 extern TInt  gDataPagingOverride;
    67 extern TInt  gDataPagingOverride;
    68 extern TInt  gLogLevel;
    68 extern TInt  gLogLevel;
    69 extern bool gCache;
    69 extern bool gCache;
    70 extern TBool gIsOBYUTF8;
    70 extern TBool gIsOBYUTF8;
       
    71 
       
    72 #define MAX_LINE  65535
       
    73 
    71 TBool gDriveImage=EFalse;	// for drive image support.
    74 TBool gDriveImage=EFalse;	// for drive image support.
    72 
    75 
    73 
    76 
    74 TInt TRomNode::Count=0;
    77 TInt TRomNode::Count=0;
    75 TRomNode* TRomNode::TheFirstNode = NULL;
    78 TRomNode* TRomNode::TheFirstNode = NULL;
   623 			//Offset in 32bit words from start of file block
   626 			//Offset in 32bit words from start of file block
   624 			TUint32 offset = ((((TUint8*) entry) - fileBlockBase) >> 2);
   627 			TUint32 offset = ((((TUint8*) entry) - fileBlockBase) >> 2);
   625 			
   628 			
   626 			if(offset > 0xFFFF)
   629 			if(offset > 0xFFFF)
   627 			{
   630 			{
   628 				printf("ERROR: Offset overflow: name=%s, OFFSET = %d\n", node->iName, offset);
   631 				printf("ERROR: Offset overflow: name=%s, OFFSET = %d\n", node->iName, (int)offset);
   629 				throw "fail";
   632 				throw "fail";
   630 			}
   633 			}
   631 			
   634 			
   632 			
   635 			
   633 			array[index].iOffset = (TUint16) offset;
   636 			array[index].iOffset = (TUint16) offset;
   638 			entry = pDirEntry;
   641 			entry = pDirEntry;
   639 
   642 
   640 			TUint32 offset = ((((TUint8*) entry) - dirBlockBase) >> 2);
   643 			TUint32 offset = ((((TUint8*) entry) - dirBlockBase) >> 2);
   641 			if(offset > 0xFFFF)
   644 			if(offset > 0xFFFF)
   642 			{
   645 			{
   643 				printf("ERROR: Offset overflow: name=%s, OFFSET = %d\n", node->iName, offset);
   646 				printf("ERROR: Offset overflow: name=%s, OFFSET = %d\n", node->iName, (int)offset);
   644 				throw "fail";
   647 				throw "fail";
   645 			}
   648 			}
   646 			
   649 			
   647 			//Offset in 32bit words from start of directory block
   650 			//Offset in 32bit words from start of directory block
   648 			array[index].iOffset = (TUint16) offset;
   651 			array[index].iOffset = (TUint16) offset;
   787 		Print(EError, "Adding Node for same file: TRomNode structure corrupted\n");
   790 		Print(EError, "Adding Node for same file: TRomNode structure corrupted\n");
   788 		exit(666);
   791 		exit(666);
   789 		}
   792 		}
   790 	iNextNodeForSameFile = aPreviousNode;
   793 	iNextNodeForSameFile = aPreviousNode;
   791 	}
   794 	}
       
   795 void TRomNode::FlushLogMessages()
       
   796 	{
       
   797 	if(iEntry)
       
   798 		{
       
   799 		for(int i = 0; i < (int)iEntry->iLogMessages.size(); i++)
       
   800 			{
       
   801 			TLogItem& aLogItem = iEntry->iLogMessages[i];
       
   802 			Print(aLogItem.iPrintType, aLogItem.iLogMessage.c_str());
       
   803 			}
       
   804 		iEntry->iLogMessages.clear();
       
   805 		}
       
   806 	}
   792 
   807 
   793 
   808 
   794 
   809 
   795 
   810 
   796 
   811 
   860 // compress it, if it isn't already compressed.
   875 // compress it, if it isn't already compressed.
   861 //
   876 //
   862 // Returns the number of bytes used, or -ve error code
   877 // Returns the number of bytes used, or -ve error code
   863 TInt TRomBuilderEntry::PlaceFile( TUint8* &aDest,TUint aMaxSize, CBytePair *aBPE ){
   878 TInt TRomBuilderEntry::PlaceFile( TUint8* &aDest,TUint aMaxSize, CBytePair *aBPE ){
   864 
   879 
   865 
   880 	char tmpbuf[MAX_LINE];
       
   881 	TLogItem tmpLog;
   866 	TUint compression = 0;
   882 	TUint compression = 0;
   867 	TBool executable = iExecutable;
   883 	TBool executable = iExecutable;
   868 	Print(ELog,"Reading file %s to image\n", iFileName );
   884 	sprintf(tmpbuf,"Reading file %s to image\n", iFileName );
       
   885 	tmpLog.iPrintType = ELog;
       
   886 	tmpLog.iLogMessage = tmpbuf;
       
   887 	iLogMessages.push_back(tmpLog);
       
   888 
   869 	TUint32 size = HFile::GetLength(iFileName);
   889 	TUint32 size = HFile::GetLength(iFileName);
   870 	if (size==0)
   890 	if (size==0)
   871 		Print(EWarning, "File %s does not exist or is 0 bytes in length.\n",iFileName);
   891 	{
       
   892 		sprintf(tmpbuf, "File %s does not exist or is 0 bytes in length.\n",iFileName);
       
   893 		tmpLog.iPrintType = EWarning;
       
   894 		tmpLog.iLogMessage = tmpbuf;
       
   895 		iLogMessages.push_back(tmpLog);
       
   896 	}
   872 	if (aDest == NULL) {
   897 	if (aDest == NULL) {
   873 		aMaxSize = size << 1;
   898 		aMaxSize = size << 1;
   874 		aMaxSize = (aMaxSize>0) ? aMaxSize : 2;
   899 		aMaxSize = (aMaxSize>0) ? aMaxSize : 2;
   875 		aDest = new TUint8[aMaxSize];
   900 		aDest = new TUint8[aMaxSize];
   876    }
   901    }
   888 		E32ImageFile f(aBPE);
   913 		E32ImageFile f(aBPE);
   889 		TInt r = f.Open(iFileName);
   914 		TInt r = f.Open(iFileName);
   890 		// is it really a valid E32ImageFile?
   915 		// is it really a valid E32ImageFile?
   891 		if (r != KErrNone)
   916 		if (r != KErrNone)
   892 		{
   917 		{
   893 			Print(EWarning, "File '%s' is not a valid executable.  Placing file as data.\n", iFileName);
   918 			sprintf(tmpbuf, "File '%s' is not a valid executable.  Placing file as data.\n", iFileName);
       
   919 			tmpLog.iPrintType = EWarning;
       
   920 			tmpLog.iLogMessage = tmpbuf;
       
   921 			iLogMessages.push_back(tmpLog);
   894 			executable = EFalse;
   922 			executable = EFalse;
   895 		}
   923 		}
   896 		else
   924 		else
   897 		{
   925 		{
   898 
   926 
   910 				// get the corresponding location and update it.While considering the Data section limits
   938 				// get the corresponding location and update it.While considering the Data section limits
   911 				// don't include the Bss section, as it doesn't exist as yet in the image.
   939 				// don't include the Bss section, as it doesn't exist as yet in the image.
   912 				while( aDllEntry ){
   940 				while( aDllEntry ){
   913 					if(aDllEntry->iOrdinal != (TUint32)-1){
   941 					if(aDllEntry->iOrdinal != (TUint32)-1){
   914 						if(aDllEntry->iOrdinal < 1 || aDllEntry->iOrdinal > (TUint)f.iOrigHdr->iExportDirCount){
   942 						if(aDllEntry->iOrdinal < 1 || aDllEntry->iOrdinal > (TUint)f.iOrigHdr->iExportDirCount){
   915 							Print(EWarning, "Invalid ordinal %d specified for DLL %s\n", aDllEntry->iOrdinal, iRomNode->iName);
   943 							sprintf(tmpbuf, "Invalid ordinal %d specified for DLL %s\n", (unsigned int) aDllEntry->iOrdinal, iRomNode->iName);
       
   944 							tmpLog.iPrintType = EWarning;
       
   945 							tmpLog.iLogMessage = tmpbuf;
       
   946 							iLogMessages.push_back(tmpLog);
   916 							aDllEntry = aDllEntry->NextDllDataEntry();
   947 							aDllEntry = aDllEntry->NextDllDataEntry();
   917 							continue;
   948 							continue;
   918 						}
   949 						}
   919 
   950 
   920 						//	Get the address of the data field via the export table.
   951 						//	Get the address of the data field via the export table.
   929 							aLocation = (void*)(aDataSeg + aDataAddr - f.iOrigHdr->iDataBase );
   960 							aLocation = (void*)(aDataSeg + aDataAddr - f.iOrigHdr->iDataBase );
   930 							memcpy(aLocation, &aDllEntry->iNewValue, aDllEntry->iSize);
   961 							memcpy(aLocation, &aDllEntry->iNewValue, aDllEntry->iSize);
   931 						}
   962 						}
   932 						else
   963 						else
   933 						{
   964 						{
   934 							Print(EWarning, "Patchdata failed as address pointed by ordinal %d of DLL %s doesn't lie within Code or Data section limits\n", aDllEntry->iOrdinal, iRomNode->iName);
   965 							sprintf(tmpbuf, "Patchdata failed as address pointed by ordinal %d of DLL %s doesn't lie within Code or Data section limits\n", (unsigned int) aDllEntry->iOrdinal, iRomNode->iName);
       
   966 							tmpLog.iPrintType = EWarning;
       
   967 							tmpLog.iLogMessage = tmpbuf;
       
   968 							iLogMessages.push_back(tmpLog);
   935 						}
   969 						}
   936 					}
   970 					}
   937 					else if(aDllEntry->iDataAddress != (TLinAddr)-1){
   971 					else if(aDllEntry->iDataAddress != (TLinAddr)-1){
   938 						aDataAddr = aDllEntry->iDataAddress + aDllEntry->iOffset;
   972 						aDataAddr = aDllEntry->iDataAddress + aDllEntry->iOffset;
   939 						if( aDataAddr >= f.iOrigHdr->iCodeBase && aDataAddr <= (f.iOrigHdr->iCodeBase + f.iOrigHdr->iCodeSize)){
   973 						if( aDataAddr >= f.iOrigHdr->iCodeBase && aDataAddr <= (f.iOrigHdr->iCodeBase + f.iOrigHdr->iCodeSize)){
   946 							aLocation = (void*)(aDataSeg + aDataAddr - f.iOrigHdr->iDataBase );
   980 							aLocation = (void*)(aDataSeg + aDataAddr - f.iOrigHdr->iDataBase );
   947 							memcpy(aLocation, &aDllEntry->iNewValue, aDllEntry->iSize);
   981 							memcpy(aLocation, &aDllEntry->iNewValue, aDllEntry->iSize);
   948 						}
   982 						}
   949 						else
   983 						else
   950 						{
   984 						{
   951 							Print(EWarning, "Patchdata failed as address 0x%x of DLL %s doesn't lie within Code or Data section limits\n", aDllEntry->iOrdinal, iRomNode->iName);
   985 							sprintf(tmpbuf, "Patchdata failed as address 0x%x of DLL %s doesn't lie within Code or Data section limits\n", (unsigned int) aDllEntry->iOrdinal, iRomNode->iName);
       
   986 							tmpLog.iPrintType = EWarning;
       
   987 							tmpLog.iLogMessage = tmpbuf;
       
   988 							iLogMessages.push_back(tmpLog);
   952 						}
   989 						}
   953 					}
   990 					}
   954 					aDllEntry = aDllEntry->NextDllDataEntry();
   991 					aDllEntry = aDllEntry->NextDllDataEntry();
   955 				}
   992 				}
   956 			}
   993 			}
   957 
   994 
   958 			compression = f.iHdr->CompressionType();
   995 			compression = f.iHdr->CompressionType();
   959 			Print(ELog,"Original file:'%s' is compressed by method:%08x\n", iFileName, compression);
   996 			sprintf(tmpbuf,"Original file:'%s' is compressed by method:%08x\n", iFileName, compression);
       
   997 			tmpLog.iPrintType = ELog;
       
   998 			tmpLog.iLogMessage = tmpbuf;
       
   999 			iLogMessages.push_back(tmpLog);
   960 
  1000 
   961 
  1001 
   962 			TUint32 oldFileComp;
  1002 			TUint32 oldFileComp;
   963 			TUint32 newFileComp;
  1003 			TUint32 newFileComp;
   964 
  1004 
  1068 			if ( oldFileComp != newFileComp )
  1108 			if ( oldFileComp != newFileComp )
  1069 			{
  1109 			{
  1070 
  1110 
  1071 				if( newFileComp == 0)
  1111 				if( newFileComp == 0)
  1072 				{
  1112 				{
  1073 					Print(ELog,"Decompressing executable '%s'\n", iFileName);
  1113 					sprintf(tmpbuf,"Decompressing executable '%s'\n", iFileName);
  1074 					f.iHdr->iCompressionType = 0;
  1114 					f.iHdr->iCompressionType = 0;
       
  1115 					tmpLog.iPrintType = ELog;
       
  1116 					tmpLog.iLogMessage = tmpbuf;
       
  1117 					iLogMessages.push_back(tmpLog);
  1075 				}
  1118 				}
  1076 				else
  1119 				else
  1077 				{
  1120 				{
  1078 					Print(ELog,"Compressing executable '%s' with method:%08x\n", iFileName, newFileComp);
  1121 					sprintf(tmpbuf,"Compressing executable '%s' with method:%08x\n", iFileName, (unsigned int) newFileComp);
       
  1122 					tmpLog.iPrintType = ELog;
       
  1123 					tmpLog.iLogMessage = tmpbuf;
       
  1124 					iLogMessages.push_back(tmpLog);
  1079 					f.iHdr->iCompressionType = newFileComp;
  1125 					f.iHdr->iCompressionType = newFileComp;
  1080 				}
  1126 				}
  1081 				f.UpdateHeaderCrc();
  1127 				f.UpdateHeaderCrc();
  1082 				if (overflow)
  1128 				if (overflow)
  1083 				{
  1129 				{
  1084 					// need to check if the compressed file will fit in the image
  1130 					// need to check if the compressed file will fit in the image
  1085 					//TODO the checking will slow down the build process, should do it later along with the writing on aDest.
  1131 					
  1086 					TUint32 compressedSize;
  1132 					TUint32 compressedSize;
  1087 					char * buffer = new char [size];
  1133 					char * buffer = new char [size];
  1088 #if defined(__LINUX__)
  1134 #if defined(__LINUX__)
  1089 					ostrstream os((char*)aDest, aMaxSize, (ios_base::openmode)(ios_base::out+ios_base::binary));
  1135 					ostrstream os((char*)aDest, aMaxSize, (ios_base::openmode)(ios_base::out+ios_base::binary));
  1090 #elif ( defined(__TOOLS2__) || defined(__MSVCDOTNET__)) && defined (_STLP_THREADS)
  1136 #elif ( defined(__TOOLS2__) || defined(__MSVCDOTNET__)) && defined (_STLP_THREADS)
  1101 						overflow = EFalse;
  1147 						overflow = EFalse;
  1102 				}
  1148 				}
  1103 			}
  1149 			}
  1104 			if (overflow)
  1150 			if (overflow)
  1105 			{
  1151 			{
  1106 				Print(EError, "Can't fit '%s' in image\n", iFileName);
  1152 				sprintf(tmpbuf, "Can't fit '%s' in image\n", iFileName);
  1107 				Print(EError, "Overflowed by approximately 0x%x bytes.\n", size - aMaxSize);
  1153 				tmpLog.iPrintType = EError;
       
  1154 				tmpLog.iLogMessage = tmpbuf;
       
  1155 				iLogMessages.push_back(tmpLog);
       
  1156 				sprintf(tmpbuf, "Overflowed by approximately 0x%x bytes.\n", (unsigned int) (size - aMaxSize));
       
  1157 				tmpLog.iPrintType = EError;
       
  1158 				tmpLog.iLogMessage = tmpbuf;
       
  1159 				iLogMessages.push_back(tmpLog);
  1108 				exit(667);
  1160 				exit(667);
  1109 			}
  1161 			}
  1110 
  1162 
  1111 			//try to use cached version where possible.
  1163 			//try to use cached version where possible.
  1112 			if(gCache && !gDriveImage && !(iRomNode->iAlias) && (iRomNode->iEntry->iExecutable) && !(iRomNode->iOverride & KOverrideDllData))
  1164 			if(gCache && !gDriveImage && !(iRomNode->iAlias) && (iRomNode->iEntry->iExecutable) && !(iRomNode->iOverride & KOverrideDllData))
  1122 					memcpy(aDest, entryref->GetCachedFileBuffer(), size);
  1174 					memcpy(aDest, entryref->GetCachedFileBuffer(), size);
  1123 					memcpy(aDest,f.iHdr,sizeof(E32ImageHeaderV));
  1175 					memcpy(aDest,f.iHdr,sizeof(E32ImageHeaderV));
  1124 					compression = atoi(entryref->GetCachedFileCompressionID());
  1176 					compression = atoi(entryref->GetCachedFileCompressionID());
  1125 					memcpy(&iUids[0], aDest, sizeof(iUids));
  1177 					memcpy(&iUids[0], aDest, sizeof(iUids));
  1126 					if (compression)
  1178 					if (compression)
  1127 						Print(ELog,"Compressed executable File '%s' size: %08x, mode:%08x\n", iFileName, size, compression);
  1179 					{
       
  1180 						sprintf(tmpbuf,"Compressed executable File '%s' size: %08x, mode:%08x\n", iFileName, (unsigned int) size, (unsigned int) compression);
       
  1181 
       
  1182 						tmpLog.iPrintType = ELog;
       
  1183 						tmpLog.iLogMessage = tmpbuf;
       
  1184 						iLogMessages.push_back(tmpLog);
       
  1185 					}
  1128 					else if (iExecutable)
  1186 					else if (iExecutable)
  1129 						Print(ELog,"Executable File '%s' size: %08x\n", iFileName, size);
  1187 					{
       
  1188 						sprintf(tmpbuf,"Executable File '%s' size: %08x\n", iFileName, (unsigned int) size);
       
  1189 						tmpLog.iPrintType = ELog;
       
  1190 						tmpLog.iLogMessage = tmpbuf;
       
  1191 						iLogMessages.push_back(tmpLog);
       
  1192 					}
  1130 					else
  1193 					else
  1131 						Print(ELog,"File '%s' size: %08x\n", iFileName, size);
  1194 					{
       
  1195 						sprintf(tmpbuf,"File '%s' size: %08x\n", iFileName, (unsigned int) size);
       
  1196 						tmpLog.iPrintType = ELog;
       
  1197 						tmpLog.iLogMessage = tmpbuf;
       
  1198 						iLogMessages.push_back(tmpLog);
       
  1199 					}
  1132 					iRealFileSize = size;	// required later when directory is written
  1200 					iRealFileSize = size;	// required later when directory is written
  1133 
  1201 
  1134 					return size;
  1202 					return size;
  1135 				}
  1203 				}
  1136 			}
  1204 			}
  1181 						memcpy(temp,iFileName,len);
  1249 						memcpy(temp,iFileName,len);
  1182 						CacheManager::GetInstance()->Invalidate(temp, newentryref); 
  1250 						CacheManager::GetInstance()->Invalidate(temp, newentryref); 
  1183 					}
  1251 					}
  1184 					catch (CacheException ce)
  1252 					catch (CacheException ce)
  1185 					{
  1253 					{
  1186 						Print(EWarning, "Cache brings up an exception (%s) when processes %s\r\n", ce.GetErrorMessage(), iFileName);
  1254 						sprintf(tmpbuf, "Cache brings up an exception (%s) when processes %s\r\n", ce.GetErrorMessage(), iFileName);
       
  1255 						tmpLog.iPrintType = EWarning;
       
  1256 						tmpLog.iLogMessage = tmpbuf;
       
  1257 						iLogMessages.push_back(tmpLog);
  1187 					}
  1258 					}
  1188 				}
  1259 				}
  1189 			}
  1260 			}
  1190 
  1261 
  1191 			compression = f.iHdr->CompressionType();
  1262 			compression = f.iHdr->CompressionType();
  1194 	}
  1265 	}
  1195 	if (!executable)
  1266 	if (!executable)
  1196 	{
  1267 	{
  1197 		if ( size > aMaxSize )
  1268 		if ( size > aMaxSize )
  1198 		{
  1269 		{
  1199 			Print(EError, "Can't fit '%s' in image\n", iFileName);
  1270 			sprintf(tmpbuf, "Can't fit '%s' in image\n", iFileName);
  1200 			Print(EError, "Overflowed by approximately 0x%x bytes.\n", size - aMaxSize);
  1271 			tmpLog.iPrintType = EError;
       
  1272 			tmpLog.iLogMessage = tmpbuf;
       
  1273 			iLogMessages.push_back(tmpLog);
       
  1274 			sprintf(tmpbuf, "Overflowed by approximately 0x%x bytes.\n", (unsigned int) (size - aMaxSize));
       
  1275 			tmpLog.iPrintType = EError;
       
  1276 			tmpLog.iLogMessage = tmpbuf;
       
  1277 			iLogMessages.push_back(tmpLog);
  1201 			exit(667);
  1278 			exit(667);
  1202 		}
  1279 		}
  1203 		size = HFile::Read(iFileName, (TAny *)aDest);
  1280 		size = HFile::Read(iFileName, (TAny *)aDest);
  1204 		TUint32 Uidslen = (size > sizeof(iUids)) ? sizeof(iUids) : size;
  1281 		TUint32 Uidslen = (size > sizeof(iUids)) ? sizeof(iUids) : size;
  1205 		memcpy(&iUids[0], aDest, Uidslen);
  1282 		memcpy(&iUids[0], aDest, Uidslen);
  1206 	}
  1283 	}
  1207 
  1284 
  1208 	if (compression)
  1285 	if (compression)
  1209 		Print(ELog,"Compressed executable File '%s' size: %08x, mode:%08x\n", iFileName, size, compression);
  1286 	{
       
  1287 		sprintf(tmpbuf,"Compressed executable File '%s' size: %08x, mode:%08x\n", iFileName, (unsigned int) size, (unsigned int) compression);
       
  1288 		tmpLog.iPrintType = ELog;
       
  1289 		tmpLog.iLogMessage = tmpbuf;
       
  1290 		iLogMessages.push_back(tmpLog);
       
  1291 	}
  1210 	else if (iExecutable)
  1292 	else if (iExecutable)
  1211 		Print(ELog,"Executable File '%s' size: %08x\n", iFileName, size);
  1293 	{
       
  1294 		sprintf(tmpbuf,"Executable File '%s' size: %08x\n", iFileName, (unsigned int) size);
       
  1295 		tmpLog.iPrintType = ELog;
       
  1296 		tmpLog.iLogMessage = tmpbuf;
       
  1297 		iLogMessages.push_back(tmpLog);
       
  1298 	}
  1212 	else
  1299 	else
  1213 		Print(ELog,"File '%s' size: %08x\n", iFileName, size);
  1300 	{
       
  1301 		sprintf(tmpbuf,"File '%s' size: %08x\n", iFileName, (unsigned int) size);
       
  1302 		tmpLog.iPrintType = ELog;
       
  1303 		tmpLog.iLogMessage = tmpbuf;
       
  1304 		iLogMessages.push_back(tmpLog);
       
  1305 	}
  1214 	iCompressEnabled = compression;
  1306 	iCompressEnabled = compression;
  1215 	iRealFileSize = size;	// required later when directory is written
  1307 	iRealFileSize = size;	// required later when directory is written
  1216 
  1308 
  1217 	return size;
  1309 	return size;
  1218 }
  1310 }
  1349 		if(iFileName)
  1441 		if(iFileName)
  1350 			Print(aWhere, "%s\t%d\n", iFileName, RealFileSize());
  1442 			Print(aWhere, "%s\t%d\n", iFileName, RealFileSize());
  1351 		}
  1443 		}
  1352 
  1444 
  1353 }
  1445 }
       
  1446 char* TRomBuilderEntry::GetSystemFullName()
       
  1447 {
       
  1448 	TBool aIgnoreHiddenAttrib = ETrue;
       
  1449 	TInt aLen = iRomNode->FullNameLength(aIgnoreHiddenAttrib);
       
  1450 	char *aBuf = new char[aLen+1];
       
  1451 	iRomNode->GetFullName(aBuf, aIgnoreHiddenAttrib);
       
  1452 	return aBuf;
       
  1453 }