imgtools/romtools/rombuild/r_rom.cpp
changeset 590 360bd6b35136
parent 0 044383f39525
child 617 3a747a240983
equal deleted inserted replaced
588:c7c26511138f 590:360bd6b35136
    20 #include <e32std_private.h>
    20 #include <e32std_private.h>
    21 #include <e32uid.h>
    21 #include <e32uid.h>
    22 #include "h_utl.h"
    22 #include "h_utl.h"
    23 #include <string.h>
    23 #include <string.h>
    24 #include <stdlib.h>
    24 #include <stdlib.h>
    25 
    25 #include <iomanip> 
    26 #if defined(__MSVCDOTNET__) || defined(__TOOLS2__)
       
    27 #include <iomanip>
       
    28 #else //!__MSVCDOTNET__
       
    29 #include <iomanip.h>
       
    30 #endif //__MSVCDOTNET__
       
    31 
    26 
    32 #include "r_global.h"
    27 #include "r_global.h"
    33 #include "r_obey.h"
    28 #include "r_obey.h"
    34 #include "r_rom.h"
    29 #include "r_rom.h"
    35 #include "r_dir.h"
    30 #include "r_dir.h"
    36 #include "patchdataprocessor.h"
    31 #include "patchdataprocessor.h"
    37 #include "memmap.h"
    32 #include "memmap.h"
    38 #include "byte_pair.h"
    33 #include "byte_pair.h"
       
    34 #include "symbolgenerator.h"
    39 
    35 
    40 const TInt KSpareExports=16;
    36 const TInt KSpareExports=16;
    41 extern TInt gThreadNum;
    37 extern TInt gThreadNum;
    42 extern char* gDepInfoFile;
    38 extern string gDepInfoFile;
    43 extern TBool gGenDepGraph;
    39 extern TBool gGenDepGraph;
    44 
    40 
    45 TUint32 DeflateCompressCheck(char *bytes,TInt size,ostream &os);
    41 TUint32 DeflateCompressCheck(char *bytes,TInt size,ostream &os);
    46 void DeflateCompress(char *bytes,TInt size,ostream &os);
    42 void DeflateCompress(char *bytes,TInt size,ostream &os);
    47 void InflateUnCompress(unsigned char* source, int sourcesize,unsigned char* dest, int destsize);
    43 void InflateUnCompress(unsigned char* source, int sourcesize,unsigned char* dest, int destsize);
    64 			{
    60 			{
    65 			TRomBuilderEntry * current = m_queue->front();
    61 			TRomBuilderEntry * current = m_queue->front();
    66 			m_queue->pop();
    62 			m_queue->pop();
    67 			m_mutex.unlock();
    63 			m_mutex.unlock();
    68 			TInt err = current->OpenImageFile();
    64 			TInt err = current->OpenImageFile();
       
    65 			m_mutex.lock();
       
    66 			err = current->GetImageFileInfo(err);
       
    67 			m_mutex.unlock();
    69 			if(err)
    68 			if(err)
    70 				{
    69 				{
    71 				m_mutex.lock();
    70 				m_mutex.lock();
    72 				++m_errors;
    71 				++m_errors;
    73 				m_mutex.unlock();
    72 				m_mutex.unlock();
   131 	m_nextpage = aNextPage;
   130 	m_nextpage = aNextPage;
   132 	}
   131 	}
   133 void CompressPageWorker::operator()()
   132 void CompressPageWorker::operator()()
   134 	{
   133 	{
   135 	SRomPageInfo* pPageBase = (SRomPageInfo*)((TInt)m_rom->iHeader + m_rom->iHeader->iRomPageIndex);
   134 	SRomPageInfo* pPageBase = (SRomPageInfo*)((TInt)m_rom->iHeader + m_rom->iHeader->iRomPageIndex);
   136 	CBytePair bpe(gFastCompress);
   135 	CBytePair bpe;
   137 	while(1)
   136 	while(1)
   138 		{
   137 		{
   139 		m_mutex.lock();
   138 		m_mutex.lock();
   140 		TInt currentPageIndex = m_nextpage++;
   139 		TInt currentPageIndex = m_nextpage++;
   141 		m_mutex.unlock();
   140 		m_mutex.unlock();
   254 	{}
   253 	{}
   255 
   254 
   256 COrderedFileList::~COrderedFileList()
   255 COrderedFileList::~COrderedFileList()
   257 	{
   256 	{
   258 	iCount=0;
   257 	iCount=0;
   259 	delete[] iOrderedFiles;
   258 	if(iOrderedFiles)
       
   259 		delete[] iOrderedFiles;
   260 	}
   260 	}
   261 
   261 
   262 COrderedFileList* COrderedFileList::New(TInt aMaxFiles)
   262 COrderedFileList* COrderedFileList::New(TInt aMaxFiles)
   263 	{
   263 	{
   264 	COrderedFileList *pL=new COrderedFileList(aMaxFiles);
   264 	COrderedFileList *pL=new COrderedFileList(aMaxFiles);
   309 	if ((start!=KErrNotFound) && (end!=KErrNotFound) && (end>start))
   309 	if ((start!=KErrNotFound) && (end!=KErrNotFound) && (end>start))
   310 		{
   310 		{
   311 		// Importing from DLL with Uid
   311 		// Importing from DLL with Uid
   312 		char uidStr[0x100];
   312 		char uidStr[0x100];
   313 		strcpy(uidStr, "0x");
   313 		strcpy(uidStr, "0x");
   314 		strncat(uidStr, aExportName+start+1, end-start-1);
   314 		strncat(uidStr, aExportName+start+1, end-start-1); 
   315 		#ifdef __TOOLS2__
   315 
   316 		istringstream val(uidStr);
   316 				 
   317 		#else
   317 		if (IsValidNumber(uidStr)){
   318 		istrstream val(uidStr,strlen(uidStr));
   318 			TUint32 u = 0;
   319 		#endif
   319 			Val(u,uidStr);
   320 
       
   321 #if defined(__MSVCDOTNET__) || defined(__TOOLS2__)
       
   322 		val >> setbase(0);
       
   323 #endif //__MSVCDOTNET__
       
   324 
       
   325 		TUint32 u;
       
   326 		val >> u;
       
   327 		val.peek();
       
   328 		if (val.eof())
       
   329 			{
       
   330 			aDllUid=TUid::Uid(u);
   320 			aDllUid=TUid::Uid(u);
   331 			char *dot=aExportName+strlen(aExportName)-1;
   321 			char *dot=aExportName+strlen(aExportName)-1;
   332 			while (dot>=aExportName)
   322 			while (dot>=aExportName)
   333 				{
   323 				{
   334 				if (*dot=='.')
   324 				if (*dot=='.')
   344 				}
   334 				}
   345 			}
   335 			}
   346 		}
   336 		}
   347 	}
   337 	}
   348 
   338 
   349 E32Rom::E32Rom(CObeyFile *aObey)
   339 E32Rom::E32Rom(CObeyFile *aObey) {
   350 //
       
   351 // Constructor
       
   352 //
       
   353 	{
       
   354 
   340 
   355 	iSize=sizeof(TRomLoaderHeader)+aObey->iRomSize;
   341 	iSize=sizeof(TRomLoaderHeader)+aObey->iRomSize;
   356 	iObey=aObey;
   342 	iObey=aObey;
   357 	iPeFiles=NULL;
   343 	iPeFiles=NULL;
   358 
   344 	iSymGen = NULL ;
   359 	if(gLowMem)
   345 	if(gLowMem)
   360 	{
   346 	{
   361 		iImageMap = new Memmap();
   347 		iImageMap = new Memmap();
   362 
   348 
   363 		if(iImageMap == NULL)
   349 		if(iImageMap == NULL)
   409 	iBranchesFixedUp=0;
   395 	iBranchesFixedUp=0;
   410 	iVtableEntriesFixedUp=0;
   396 	iVtableEntriesFixedUp=0;
   411 	iOverhead=0;
   397 	iOverhead=0;
   412 	}
   398 	}
   413 
   399 
   414 E32Rom::~E32Rom()
   400 E32Rom::~E32Rom() {
   415 //
   401 	if(iSymGen){		
   416 // Destructor
   402 		delete iSymGen;
   417 //
   403 		iSymGen = NULL ;
   418 	{
   404 	}
   419 
       
   420 	if(gLowMem)
   405 	if(gLowMem)
   421 	{
   406 	{
   422 		iImageMap->CloseMemoryMap(ETrue);
   407 		iImageMap->CloseMemoryMap(ETrue);
   423 		delete iImageMap;
   408 		delete iImageMap;
   424 	}
   409 	}
   472 
   457 
   473 	char* addr = anAddr;
   458 	char* addr = anAddr;
   474 	TRomExceptionSearchTable* exceptionSearchTable = 0;
   459 	TRomExceptionSearchTable* exceptionSearchTable = 0;
   475 
   460 
   476 	if(gPagedRom)
   461 	if(gPagedRom)
   477 		{
   462 		{ 
   478 		gDepInfoFile = (char* )malloc(strlen((char *)iObey->iRomFileName) + 1);
   463 		gDepInfoFile = iObey->iRomFileName; 
   479 		strcpy(gDepInfoFile, (char *)iObey->iRomFileName);
       
   480 		iObey->SetArea().DefaultArea()->SortFilesForPagedRom();
   464 		iObey->SetArea().DefaultArea()->SortFilesForPagedRom();
   481 		// exception search table needs to go at start of ROM to make it not demand paged...
   465 		// exception search table needs to go at start of ROM to make it not demand paged...
   482 		addr = ReserveRomExceptionSearchTable(addr,exceptionSearchTable);
   466 		addr = ReserveRomExceptionSearchTable(addr,exceptionSearchTable);
   483 		}
   467 		}
   484 	else if(gGenDepGraph)
   468 	else if(gGenDepGraph)
   709 	TRomHeader dummy;
   693 	TRomHeader dummy;
   710 	TInt r=LoadContents(addr, &dummy);
   694 	TInt r=LoadContents(addr, &dummy);
   711 	if (r!=KErrNone)
   695 	if (r!=KErrNone)
   712 		{
   696 		{
   713 		Print(EError, "LoadContents failed - return code %d\n", r);
   697 		Print(EError, "LoadContents failed - return code %d\n", r);
       
   698 		if(iSymGen)
       
   699 			iSymGen->WaitThreads();
   714 		return r;
   700 		return r;
   715 		}
   701 		}
   716 	iExtensionRomHeader->iRomRootDirectoryList = dummy.iRomRootDirectoryList;
   702 	iExtensionRomHeader->iRomRootDirectoryList = dummy.iRomRootDirectoryList;
   717 
   703 
   718 	iLoaderHeader->SetUp(iObey);
   704 	iLoaderHeader->SetUp(iObey);
   719 	FinaliseExtensionHeader(aKernelRom);
   705 	FinaliseExtensionHeader(aKernelRom);
   720 	DisplayExtensionHeader();
   706 	DisplayExtensionHeader();
   721 
   707 	if(iSymGen)
       
   708 		iSymGen->WaitThreads();
   722 	return KErrNone;
   709 	return KErrNone;
   723 	}
   710 	}
   724 	
   711 	
   725 TInt E32Rom::Create()
   712 TInt E32Rom::Create()
   726 	{
   713 	{
   747 
   734 
   748 	TInt r=LoadContents(addr, iHeader);
   735 	TInt r=LoadContents(addr, iHeader);
   749 	if (r!=KErrNone)
   736 	if (r!=KErrNone)
   750 		{
   737 		{
   751 		Print(EError, "LoadContents failed - return code %d\n", r);
   738 		Print(EError, "LoadContents failed - return code %d\n", r);
       
   739 		if(iSymGen)
       
   740 			iSymGen->WaitThreads();
   752 		return r;
   741 		return r;
   753 		}
   742 		}
   754 
   743 
   755 	r = SetupPages(); // initialize ROM paging info...
   744 	r = SetupPages(); // initialize ROM paging info...
   756 	if(r!=KErrNone)
   745 	if(r!=KErrNone)
   757 		{
   746 		{
   758 		Print(EError, "Setup pages information failed - return code %d\n", r);
   747 		Print(EError, "Setup pages information failed - return code %d\n", r);
       
   748 		if(iSymGen)
       
   749 			iSymGen->WaitThreads();
   759 		return r;
   750 		return r;
   760 		}
   751 		}
   761 	
   752 	
   762 	r = CheckUnpagedMemSize(); // check for unpaged memory overflow
   753 	r = CheckUnpagedMemSize(); // check for unpaged memory overflow
   763 	if(r!=KErrNone)
   754 	if(r!=KErrNone)
   764 	{
   755 	{
       
   756 		if(iSymGen)
       
   757 			iSymGen->WaitThreads();
   765 		return r;
   758 		return r;
   766 	}
   759 	}
   767 	
   760 	
   768 	r = CompressPages(); // setup ROM paging info...
   761 	r = CompressPages(); // setup ROM paging info...
   769 	if(r!=KErrNone)
   762 	if(r!=KErrNone)
   770 		{
   763 		{
   771 		Print(EError, "CompressPages failed - return code %d\n", r);
   764 		Print(EError, "CompressPages failed - return code %d\n", r);
       
   765 		if(iSymGen)
       
   766 			iSymGen->WaitThreads();
   772 		return r;
   767 		return r;
   773 		}
   768 		}
   774 
   769 
   775 	iLoaderHeader->SetUp(iObey);
   770 	iLoaderHeader->SetUp(iObey);
   776 	ImpTRomHeader* header = (ImpTRomHeader *)iHeader;
   771 	ImpTRomHeader* header = (ImpTRomHeader *)iHeader;
   793 
   788 
   794 	header->Display();
   789 	header->Display();
   795 
   790 
   796 	TUint testCheckSum = HMem::CheckSum((TUint *)iHeader, iHeader->iRomSize);
   791 	TUint testCheckSum = HMem::CheckSum((TUint *)iHeader, iHeader->iRomSize);
   797 	Print(ELog, "Rom 32bit words sum to   %08x\n", testCheckSum);
   792 	Print(ELog, "Rom 32bit words sum to   %08x\n", testCheckSum);
   798 	if (testCheckSum != iObey->iCheckSum)
   793 	if (testCheckSum != iObey->iCheckSum){
       
   794 		if(iSymGen)
       
   795 			iSymGen->WaitThreads();
   799 		return Print(EError, "Rom checksum is incorrect: %08x should be %08x\n",
   796 		return Print(EError, "Rom checksum is incorrect: %08x should be %08x\n",
   800 					testCheckSum, iObey->iCheckSum);
   797 					testCheckSum, iObey->iCheckSum);
       
   798 	}
   801 
   799 
   802 	// 8bit checksum = sum of bytes
   800 	// 8bit checksum = sum of bytes
   803 	// odd/even checksum = checksum of the odd and even halfwords of the image
   801 	// odd/even checksum = checksum of the odd and even halfwords of the image
   804 
   802 
   805 	Print(ELog, "Rom 8bit checksum        %08x\n", HMem::CheckSum8((TUint8 *)iHeader, iHeader->iRomSize));
   803 	Print(ELog, "Rom 8bit checksum        %08x\n", HMem::CheckSum8((TUint8 *)iHeader, iHeader->iRomSize));
   832 					r=(TRomEntry*)(hdr->iNextExtension-iObey->iRomLinearBase+(char*)iHeader);
   830 					r=(TRomEntry*)(hdr->iNextExtension-iObey->iRomLinearBase+(char*)iHeader);
   833 					n++;
   831 					n++;
   834 					}
   832 					}
   835 			}
   833 			}
   836 		}
   834 		}
   837 
   835 	if(iSymGen)
       
   836 			iSymGen->WaitThreads();
   838 	return KErrNone;
   837 	return KErrNone;
   839 	}
   838 	}
   840 
   839 
   841 char *E32Rom::WriteDirectory(char *aAddr, TRomHeader* aHeader)
   840 char *E32Rom::WriteDirectory(char *aAddr, TRomHeader* aHeader)
   842 //
   841 //
   944 	TInt sum=0;
   943 	TInt sum=0;
   945 	TRomBuilderEntry *current=iObey->FirstFile();
   944 	TRomBuilderEntry *current=iObey->FirstFile();
   946 	while (current)
   945 	while (current)
   947 		{
   946 		{
   948 		if (current->iResource || current->HCRDataFile())
   947 		if (current->iResource || current->HCRDataFile())
   949 			sum+=Align(HFile::GetLength((TText*)current->iFileName));
   948 			sum+=Align(HFile::GetLength(current->iFileName));
   950 		else
   949 		else
   951  			sum+=Align(current->SizeInRom());
   950  			sum+=Align(current->SizeInRom());
   952 		current=iObey->NextFile();
   951 		current=iObey->NextFile();
   953 		}
   952 		}
   954 	return sum+iOverhead+Align(iDirectorySize);
   953 	return sum+iOverhead+Align(iDirectorySize);
   992 			}
   991 			}
   993 		}
   992 		}
   994 	return total_errors ? KErrGeneral : KErrNone;
   993 	return total_errors ? KErrGeneral : KErrNone;
   995 	}
   994 	}
   996 
   995 
   997 const TText FileTypeFile[]=		"File     ";
   996 const char FileTypeFile[]=		"File     ";
   998 const TText FileTypePrimary[]=	"Primary  ";
   997 const char FileTypePrimary[]=	"Primary  ";
   999 const TText FileTypeVariant[]=	"Variant  ";
   998 const char FileTypeVariant[]=	"Variant  ";
  1000 const TText FileTypeExtension[]="Extension";
   999 const char FileTypeExtension[]="Extension";
  1001 const TText FileTypeDevice[]=	"Device   ";
  1000 const char FileTypeDevice[]=	"Device   ";
  1002 
  1001 
  1003 void E32Rom::EnumerateVariants()
  1002 void E32Rom::EnumerateVariants()
  1004 	{
  1003 	{
  1005 	TInt vIndex;
  1004 	TInt vIndex;
  1006 	TInt nFiles=iObey->iNumberOfExtensions+iObey->iNumberOfDevices+3;
  1005 	TInt nFiles=iObey->iNumberOfExtensions+iObey->iNumberOfDevices+3;
  1061 			E32ImageHeader *pH=pF->iHdr;
  1060 			E32ImageHeader *pH=pF->iHdr;
  1062 			if (pF->iDataBssOffset!=0xffffffff && pF->iDataBssOffset!=dataOffset)
  1061 			if (pF->iDataBssOffset!=0xffffffff && pF->iDataBssOffset!=dataOffset)
  1063 				Print(EError,"Conflicting DataBss addresses\n");
  1062 				Print(EError,"Conflicting DataBss addresses\n");
  1064 			pF->iDataBssOffset=dataOffset;
  1063 			pF->iDataBssOffset=dataOffset;
  1065 			TInt dataSize=AlignData(pH->iDataSize+pH->iBssSize);
  1064 			TInt dataSize=AlignData(pH->iDataSize+pH->iBssSize);
  1066 			const TText* pT=FileTypeFile;
  1065 			const char* pT=FileTypeFile;
  1067 			if (pF->Primary())
  1066 			if (pF->Primary())
  1068 				pT=FileTypePrimary;
  1067 				pT=FileTypePrimary;
  1069 			if (pF->Variant())
  1068 			if (pF->Variant())
  1070 				pT=FileTypeVariant;
  1069 				pT=FileTypeVariant;
  1071 			if (pF->Extension())
  1070 			if (pF->Extension())
  1093 	const char* tn = "resource";
  1092 	const char* tn = "resource";
  1094 	if (aFile->iNonXIP)
  1093 	if (aFile->iNonXIP)
  1095 		tn = (aFile->iCompression) ? "compressed executable" : "uncompressed executable";
  1094 		tn = (aFile->iCompression) ? "compressed executable" : "uncompressed executable";
  1096 	Print(ELog,"Reading %s %s to rom linear address %08x\n", tn, aFile->iFileName, aAddress.iImageAddr);
  1095 	Print(ELog,"Reading %s %s to rom linear address %08x\n", tn, aFile->iFileName, aAddress.iImageAddr);
  1097 
  1096 
  1098 	TUint32 size=HFile::GetLength((TText*)aFile->iFileName);
  1097 	TUint32 size=HFile::GetLength(aFile->iFileName);
  1099 	if (size==0)
  1098 	if (size==0)
  1100 		{
  1099 		{
  1101 		Print(EWarning, "File %s does not exist or is 0 bytes in length.\n",aFile->iFileName);
  1100 		Print(EWarning, "File %s does not exist or is 0 bytes in length.\n",aFile->iFileName);
  1102 		return size;
  1101 		return size;
  1103 		}
  1102 		}
  1104 
  1103 
  1105 	aFile->iHeaderRange=aAddress;
  1104 	aFile->iHeaderRange=aAddress;
  1106 	char* addr = (char*)aFile->iHeaderRange.iImagePtr;
  1105 	char* addr = (char*)aFile->iHeaderRange.iImagePtr;
  1107 	const char* src = NULL;
  1106 	const char* src = NULL; 
  1108 	#ifdef __TOOLS2__
  1107 	ostringstream os; 
  1109 	ostringstream os;
       
  1110 	#else
       
  1111 	ostrstream os;
       
  1112 	#endif
       
  1113 	if (aFile->iNonXIP)
  1108 	if (aFile->iNonXIP)
  1114 		{
  1109 		{
  1115 		E32ImageFile f(aBPE);
  1110 		E32ImageFile f(aBPE);
  1116 		TInt r = f.Open(aFile->iFileName);
  1111 		TInt r = f.Open(aFile->iFileName);
  1117 		// is it really a valid E32ImageFile?
  1112 		// is it really a valid E32ImageFile?
  1136 					f.iHdr->iModuleVersion |= 0x8000u;
  1131 					f.iHdr->iModuleVersion |= 0x8000u;
  1137 					}
  1132 					}
  1138 				f.UpdateHeaderCrc();
  1133 				f.UpdateHeaderCrc();
  1139 				}
  1134 				}
  1140 			Print(ELog, "Compression Method:0x%08x/0x%08x \n", f.iHdr->CompressionType(), aFile->iCompression);
  1135 			Print(ELog, "Compression Method:0x%08x/0x%08x \n", f.iHdr->CompressionType(), aFile->iCompression);
  1141 			os << f;
  1136 			os << f; 
  1142 			#ifdef __TOOLS2__
       
  1143 			size = (os.str()).length(); 
  1137 			size = (os.str()).length(); 
  1144 			src = (os.str()).c_str();
  1138 			src = (os.str()).c_str(); 
  1145 			#else
       
  1146 			size = os.pcount();
       
  1147 			src = os.str();
       
  1148 			#endif
       
  1149 			}
  1139 			}
  1150 		}
  1140 		}
  1151 	if (addr+size>iData+iSize)
  1141 	if (addr+size>iData+iSize)
  1152 		{
  1142 		{
  1153 		Print(EError, "Can't fit '%s' in Rom.\n", aFile->iFileName);
  1143 		Print(EError, "Can't fit '%s' in Rom.\n", aFile->iFileName);
  1155 		exit(667);
  1145 		exit(667);
  1156 		}
  1146 		}
  1157 	if (src)
  1147 	if (src)
  1158 		memcpy(addr, src, size);
  1148 		memcpy(addr, src, size);
  1159 	else
  1149 	else
  1160 		size = HFile::Read((TText*)aFile->iFileName, (TAny *)addr);
  1150 		size = HFile::Read(aFile->iFileName, (TAny *)addr);
  1161 	Print(ELog,"Size:                    %08x\n", size);
  1151 	Print(ELog,"Size:                    %08x\n", size);
  1162 
  1152 
  1163 	aFile->iHeaderRange.iSize=size;
  1153 	aFile->iHeaderRange.iSize=size;
  1164 	aAddress.Extend(aFile->iHeaderRange.iSize);
  1154 	aAddress.Extend(aFile->iHeaderRange.iSize);
  1165 	return size;
  1155 	return size;
  1327 		SetImageAddr(second,iSectionPtr);
  1317 		SetImageAddr(second,iSectionPtr);
  1328 		secondptr = &second;
  1318 		secondptr = &second;
  1329 		}
  1319 		}
  1330 
  1320 
  1331 	TInt fileCount=0;
  1321 	TInt fileCount=0;
  1332 
  1322 	if(gGenSymbols && !iSymGen) {
       
  1323 		string filename(iObey->GetFileName());
       
  1324 		filename.erase(filename.length() - 3,3);
       
  1325 		filename.append("symbol");
       
  1326 		iSymGen = new SymbolGenerator(filename.c_str(),gThreadNum - 1);		
       
  1327 	}
       
  1328 		
  1333 	//
  1329 	//
  1334 	// Process files in non default areas
  1330 	// Process files in non default areas
  1335 	//
  1331 	//
  1336 
  1332 
  1337         CBytePair bpe(gFastCompress);
  1333         CBytePair bpe;
  1338 	for (NonDefaultAreasIterator areaIt(iObey->SetArea());
  1334 	for (NonDefaultAreasIterator areaIt(iObey->SetArea());
  1339 		 ! areaIt.IsDone();
  1335 		 ! areaIt.IsDone();
  1340 		 areaIt.GoToNext())
  1336 		 areaIt.GoToNext())
  1341 		{
  1337 		{
  1342 		Area& currentArea = areaIt.Current();
  1338 		Area& currentArea = areaIt.Current();
  1396 	// align to likely position of next file
  1392 	// align to likely position of next file
  1397 	TInt offset=(char*)mainptr->iImagePtr-romaddr;
  1393 	TInt offset=(char*)mainptr->iImagePtr-romaddr;
  1398 	offset = Align(offset)-offset;
  1394 	offset = Align(offset)-offset;
  1399 	mainptr->Extend(offset);
  1395 	mainptr->Extend(offset);
  1400 	iOverhead +=offset;
  1396 	iOverhead +=offset;
  1401 
  1397 	if(iSymGen){
       
  1398 		SymGenContext context ;
       
  1399 		memset(&context,0,sizeof(SymGenContext));
       
  1400 		iSymGen->AddEntry(context);
       
  1401 	}
  1402 	return (char*)mainptr->iImagePtr;
  1402 	return (char*)mainptr->iImagePtr;
  1403  	}
  1403  	}
  1404 
  1404 
  1405 void E32Rom::LayoutFile(TRomBuilderEntry* current, TAddressRange& aMain, TAddressRange* aSecond, CBytePair * aBPE)
  1405 void E32Rom::LayoutFile(TRomBuilderEntry* current, TAddressRange& aMain, TAddressRange* aSecond, CBytePair * aBPE)
  1406 //
  1406 //
  1440 
  1440 
  1441 	Print(ELog,"\n********************************************************************\n");
  1441 	Print(ELog,"\n********************************************************************\n");
  1442 
  1442 
  1443 	if (current->iPatched)
  1443 	if (current->iPatched)
  1444 		Print(ELog, "[Patched file]\n");
  1444 		Print(ELog, "[Patched file]\n");
  1445 
  1445 	TLinAddr savedAddr = aMain.iImageAddr;
  1446 	if (current->iResource)
  1446 	if (current->iResource)
  1447 		{
  1447 		{		
  1448 		TInt size=LoadDataToRom(current, aMain, aBPE);
  1448 		TInt size=LoadDataToRom(current, aMain, aBPE);
  1449 		if (aSecond != 0 && aMain.iImageAddr > iObey->iSectionStart)
  1449 		if (aSecond != 0 && aMain.iImageAddr > iObey->iSectionStart)
  1450 			return;		// first section has overflowed
  1450 			return;		// first section has overflowed
  1451 		current->FixupRomEntries(size);
  1451 		current->FixupRomEntries(size);
       
  1452 		if(iSymGen) {
       
  1453 			SymGenContext context ;
       
  1454 			memset(&context,0,sizeof(SymGenContext));
       
  1455 			context.iFileName = current->iFileName ;
       
  1456 			context.iDataAddress = savedAddr ;
       
  1457 			iSymGen->AddEntry(context); 
       
  1458 		}
  1452 		return;
  1459 		return;
  1453 		}
  1460 		}
  1454 	if(current->HCRDataFile()){	
  1461 	if(current->HCRDataFile()){	
  1455 		TInt size=LoadDataToRom(current, aMain, aBPE);		
  1462 		TInt size=LoadDataToRom(current, aMain, aBPE);		
  1456 		if (aSecond != 0 && aMain.iImageAddr > iObey->iSectionStart)
  1463 		if (aSecond != 0 && aMain.iImageAddr > iObey->iSectionStart)
  1457 			return;		// first section has overflowed
  1464 			return;		// first section has overflowed
  1458 		current->FixupRomEntries(size);
  1465 		current->FixupRomEntries(size);
  1459 		iHeader->iHcrFileAddress =  current->iHeaderRange.iImageAddr ;
  1466 		iHeader->iHcrFileAddress =  current->iHeaderRange.iImageAddr ;
  1460 		TRACE(TAREA, Print(ELog, "iHeader->iHcrFileAddress = %08x\n", iHeader->iHcrFileAddress));		 
  1467 		TRACE(TAREA, Print(ELog, "iHeader->iHcrFileAddress = %08x\n", iHeader->iHcrFileAddress));	
       
  1468 		if(iSymGen) {
       
  1469 			SymGenContext context ;
       
  1470 			memset(&context,0,sizeof(SymGenContext));
       
  1471 			context.iFileName = current->iFileName ;
       
  1472 			context.iDataAddress = savedAddr ;
       
  1473 			iSymGen->AddEntry(context); 
       
  1474 		}		
  1461 		return ;
  1475 		return ;
  1462 	}
  1476 	}
  1463 	Print(ELog,"Processing file %s\n",current->iFileName);
  1477 	Print(ELog,"Processing file %s\n",current->iFileName);
  1464 
  1478 
  1465 	if (current->Primary())
  1479 	if (current->Primary())
  1513 
  1527 
  1514 	if (aSecond != 0 && aMain.iImageAddr > iObey->iSectionStart)
  1528 	if (aSecond != 0 && aMain.iImageAddr > iObey->iSectionStart)
  1515 		return;		// first section has overflowed
  1529 		return;		// first section has overflowed
  1516 
  1530 
  1517 	LoadFileToRom(current);
  1531 	LoadFileToRom(current);
  1518 	Display(current->iRomImageHeader);
  1532 	TRomImageHeader *header = current->iRomImageHeader;
       
  1533 	if(iSymGen){
       
  1534 		SymGenContext context  ;
       
  1535 		context.iFileName = current->iFileName ;		
       
  1536 		context.iTotalSize = section1size;
       
  1537 		context.iCodeAddress = header->iCodeAddress; 
       
  1538 		context.iDataAddress = header->iDataAddress; 
       
  1539 		context.iDataBssLinearBase = header->iDataBssLinearBase;	 
       
  1540 		context.iTextSize = header->iTextSize; 
       
  1541 		context.iDataSize = header->iDataSize; 
       
  1542 		context.iBssSize = header->iBssSize;   	
       
  1543 		context.iTotalDataSize = header->iTotalDataSize;
       
  1544 		context.iExecutable = ETrue ;
       
  1545 		iSymGen->AddEntry(context);		
       
  1546 	}
       
  1547 	Display(header);
  1519 	Print(ELog,     "Dll ref table size:      %08x\n", current->iDllRefTableRange.iSize);
  1548 	Print(ELog,     "Dll ref table size:      %08x\n", current->iDllRefTableRange.iSize);
  1520 	Print(ELog,     "Compression:             %08x\n", current->iCompression);
  1549 	Print(ELog,     "Compression:             %08x\n", current->iCompression);
  1521 	Print(ELog,     "\n");
  1550 	Print(ELog,     "\n");
  1522 
  1551 
  1523 	current->FixupRomEntries(section1size);	
  1552 	current->FixupRomEntries(section1size);	
  2058 
  2087 
  2059 TInt E32Rom::WriteImages(TInt aHeaderType)
  2088 TInt E32Rom::WriteImages(TInt aHeaderType)
  2060 	{
  2089 	{
  2061 	if (aHeaderType < 0)
  2090 	if (aHeaderType < 0)
  2062 		aHeaderType = 1;
  2091 		aHeaderType = 1;
  2063 	ofstream romFile((const char *)iObey->iRomFileName,ios::binary);
  2092 	ofstream romFile((const char *)iObey->iRomFileName,ios_base::binary);
  2064 	if (!romFile)
  2093 	if (!romFile)
  2065 		return Print(EError,"Cannot open ROM file %s for output\n",iObey->iRomFileName);
  2094 		return Print(EError,"Cannot open ROM file %s for output\n",iObey->iRomFileName);
  2066 	Write(romFile, aHeaderType);
  2095 	Write(romFile, aHeaderType);
  2067 	romFile.close();
  2096 	romFile.close();
  2068 
  2097 
  2075 		if (strcmp(sname, "*")==0)
  2104 		if (strcmp(sname, "*")==0)
  2076 			{
  2105 			{
  2077 			// use romname with ".odd" appended.
  2106 			// use romname with ".odd" appended.
  2078 			sprintf(sname,"%s.odd",(const char *)iObey->iRomFileName);
  2107 			sprintf(sname,"%s.odd",(const char *)iObey->iRomFileName);
  2079 			}
  2108 			}
  2080 		ofstream oFile(sname,ios::binary);
  2109 		ofstream oFile(sname,ios_base::binary);
  2081 		if (!oFile)
  2110 		if (!oFile)
  2082 			return Print(EError,"Cannot open file %s for output\n",sname);
  2111 			return Print(EError,"Cannot open file %s for output\n",sname);
  2083 		Print(EAlways, "Writing odd half words to file %s\n",sname);
  2112 		Print(EAlways, "Writing odd half words to file %s\n",sname);
  2084 		WriteOdd(oFile);
  2113 		WriteOdd(oFile);
  2085 		oFile.close();
  2114 		oFile.close();
  2090 		if (strcmp(sname, "*")==0)
  2119 		if (strcmp(sname, "*")==0)
  2091 			{
  2120 			{
  2092 			// use romname with ".even" appended.
  2121 			// use romname with ".even" appended.
  2093 			sprintf(sname,"%s.even",(const char *)iObey->iRomFileName);
  2122 			sprintf(sname,"%s.even",(const char *)iObey->iRomFileName);
  2094 			}
  2123 			}
  2095 		ofstream oFile(sname,ios::binary);
  2124 		ofstream oFile(sname,ios_base::binary);
  2096 		if (!oFile)
  2125 		if (!oFile)
  2097 			return Print(EError,"Cannot open file %s for output\n",sname);
  2126 			return Print(EError,"Cannot open file %s for output\n",sname);
  2098 		Print(EAlways, "Writing even half words to file %s\n",sname);
  2127 		Print(EAlways, "Writing even half words to file %s\n",sname);
  2099 		WriteEven(oFile);
  2128 		WriteEven(oFile);
  2100 		oFile.close();
  2129 		oFile.close();
  2108 		if (strcmp(sname, "*")==0)
  2137 		if (strcmp(sname, "*")==0)
  2109 			{
  2138 			{
  2110 			// use romname with ".srec" appended.
  2139 			// use romname with ".srec" appended.
  2111 			sprintf(sname,"%s.srec",(const char *)iObey->iRomFileName);
  2140 			sprintf(sname,"%s.srec",(const char *)iObey->iRomFileName);
  2112 			}
  2141 			}
  2113 		ofstream sFile(sname,ios::binary);
  2142 		ofstream sFile(sname,ios_base::binary);
  2114 		if (!romFile)
  2143 		if (!romFile)
  2115 			return Print(EError,"Cannot open file %s for output\n",sname);
  2144 			return Print(EError,"Cannot open file %s for output\n",sname);
  2116 		Print(EAlways, "Writing S record format to file %s\n",sname);
  2145 		Print(EAlways, "Writing S record format to file %s\n",sname);
  2117 		WriteSRecord(sFile);
  2146 		WriteSRecord(sFile);
  2118 		sFile.close();
  2147 		sFile.close();
  2312 		
  2341 		
  2313 		// Rewrite the header with updated info
  2342 		// Rewrite the header with updated info
  2314 		#ifdef __TOOLS2__
  2343 		#ifdef __TOOLS2__
  2315 		os.seekp(headerpos); 
  2344 		os.seekp(headerpos); 
  2316 		#else
  2345 		#else
  2317 		os.seekp(headerpos,ios::beg);
  2346 		os.seekp(headerpos,ios_base::beg);
  2318 		#endif
  2347 		#endif
  2319 		
  2348 		
  2320 		// Rewrite uncompressed un-paged part (bootstrap + Page Index Table)
  2349 		// Rewrite uncompressed un-paged part (bootstrap + Page Index Table)
  2321 		os.write((char *)(iHeader), iHeader->iCompressedUnpagedStart);
  2350 		os.write((char *)(iHeader), iHeader->iCompressedUnpagedStart);
  2322 		
  2351 		
  2384 	iHeader->iUnpagedUncompressedSize = srcsize;
  2413 	iHeader->iUnpagedUncompressedSize = srcsize;
  2385 		
  2414 		
  2386 	#ifdef __TOOLS2__
  2415 	#ifdef __TOOLS2__
  2387 	os.seekp(headerpos); 
  2416 	os.seekp(headerpos); 
  2388 	#else
  2417 	#else
  2389 	os.seekp(headerpos,ios::beg);
  2418 	os.seekp(headerpos,ios_base::beg);
  2390 	#endif
  2419 	#endif
  2391 	os.write(reinterpret_cast<char*>(iHeader), headersize);	// write header again with (compressed) size info
  2420 	os.write(reinterpret_cast<char*>(iHeader), headersize);	// write header again with (compressed) size info
  2392 	
  2421 	
  2393 	if (gLogLevel & LOG_LEVEL_COMPRESSION_INFO)
  2422 	if (gLogLevel & LOG_LEVEL_COMPRESSION_INFO)
  2394 		Print(ELog, "\tiSizeUsed:%d, iUncompressedSize:%d, headersize:%d, srcsize:%d, rawimagelen:%d \n",iSizeUsed, iUncompressedSize, headersize, srcsize, rawimagelen);
  2423 		Print(ELog, "\tiSizeUsed:%d, iUncompressedSize:%d, headersize:%d, srcsize:%d, rawimagelen:%d \n",iSizeUsed, iUncompressedSize, headersize, srcsize, rawimagelen);
  2395 	}
  2424 	}
  2396 
  2425 
  2397 TInt E32Rom::Compare(char *anImage, TInt aHeaderType)
  2426 TInt E32Rom::Compare(const char *aImage, TInt aHeaderType)
  2398 	{
  2427 	{
  2399 	if (aHeaderType < 0)
  2428 	if (aHeaderType < 0)
  2400 		aHeaderType = 1;
  2429 		aHeaderType = 1;
  2401 	ifstream file(anImage, ios::binary);
  2430 	ifstream file(aImage, ios_base::binary);
  2402 	if (!file)
  2431 	if (!file)
  2403 		return Print(EError, "Cannot open Rom image '%s' for verification\n", anImage);
  2432 		return Print(EError, "Cannot open Rom image '%s' for verification\n", aImage);
  2404 	Print(ELog, "\nVerifying ROM against image in %s\n", anImage);
  2433 	Print(ELog, "\nVerifying ROM against image in %s\n", aImage);
  2405 	switch (aHeaderType)
  2434 	switch (aHeaderType)
  2406 		{
  2435 		{
  2407 	case 0:
  2436 	case 0:
  2408 		break;
  2437 		break;
  2409 	case 1:
  2438 	case 1:
  2658 		{
  2687 		{
  2659 		Print(EWarning, "The size of unpaged memory is not available. The unpaged memory overflow checking is skipped.\n");
  2688 		Print(EWarning, "The size of unpaged memory is not available. The unpaged memory overflow checking is skipped.\n");
  2660 		}
  2689 		}
  2661 		
  2690 		
  2662 	return KErrNone;
  2691 	return KErrNone;
  2663 	}
  2692 }
  2664 
  2693 TRomNode* E32Rom::RootDirectory() const {
  2665 TRomNode* E32Rom::RootDirectory() 
       
  2666 { 
       
  2667 	return iObey->iRootDirectory; 
  2694 	return iObey->iRootDirectory; 
  2668 }
  2695 }
  2669 
  2696 const char* E32Rom::RomFileName() const {
  2670 TText* E32Rom::RomFileName()
       
  2671 { 
       
  2672 	return iObey->iRomFileName; 
  2697 	return iObey->iRomFileName; 
  2673 }
  2698 }
  2674 
  2699 TUint32 E32Rom::RomBase() const {
  2675 TUint32 E32Rom::RomBase()
  2700 	return iHeader->iRomBase; 
  2676 {
       
  2677 	return iHeader->iRomBase;
       
  2678 }
  2701 }
  2679 
  2702 TUint32 E32Rom::RomSize() const {
  2680 TUint32 E32Rom::RomSize()
       
  2681 {
       
  2682 	return iHeader->iRomSize;
  2703 	return iHeader->iRomSize;
  2683 }
  2704 }
  2684 
  2705 TVersion E32Rom::Version() const {
  2685 TVersion E32Rom::Version()
       
  2686 {
       
  2687 	return iHeader->iVersion;
  2706 	return iHeader->iVersion;
  2688 }
  2707 }
  2689 
  2708 TInt64 E32Rom::Time() const {
  2690 TInt64 E32Rom::Time()
       
  2691 {
       
  2692 	return iHeader->iTime;
  2709 	return iHeader->iTime;
  2693 }
  2710 }
  2694 
  2711 TUint32 E32Rom::CheckSum() const {
  2695 TUint32 E32Rom::CheckSum()
       
  2696 {
       
  2697 	return iHeader->iCheckSum;
  2712 	return iHeader->iCheckSum;
  2698 }
  2713 }
  2699 
  2714 TUint32 E32Rom::DataRunAddress() const {
  2700 TUint32 E32Rom::DataRunAddress()
       
  2701 {
       
  2702 	return iObey->iDataRunAddress;
  2715 	return iObey->iDataRunAddress;
  2703 }
  2716 }
  2704 
  2717 TUint32 E32Rom::RomAlign() const {
  2705 TUint32 E32Rom::RomAlign()
       
  2706 {
       
  2707 	return iObey->iRomAlign;
  2718 	return iObey->iRomAlign;
  2708 }
  2719 }