kerneltest/f32test/manager/t_romg.cpp
changeset 0 a41df078684a
child 109 b3a1d9898418
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // f32test\manager\t_romg.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <f32file.h>
       
    19 #include <e32test.h>
       
    20 #include <e32rom.h>
       
    21 #include "..\server\t_server.h"
       
    22 
       
    23 const TInt KBufSize=0x10000;
       
    24 const TInt KFillerSize=0x100;
       
    25 const TInt KRomFileHeaderSize=0x100;
       
    26 const TInt KRomFileHeaderNameSize=0x10;
       
    27 
       
    28 class TRomFileHeaderBase
       
    29 	{
       
    30 public:
       
    31 	TText8 iName[KRomFileHeaderNameSize];
       
    32 	TText8 iVersionStr[4];
       
    33 	TText8 iBuildNumStr[4];
       
    34 	TInt iRomSize;
       
    35 	TInt iHeaderSize;
       
    36 	};
       
    37 
       
    38 class TRomFileHeader : public TRomFileHeaderBase
       
    39 	{
       
    40 public:
       
    41 	TUint8 iFiller[KRomFileHeaderSize-sizeof(TRomFileHeaderBase)];
       
    42 	};
       
    43 
       
    44 class CMemDir;
       
    45 class CMemEntry : public CBase
       
    46 	{
       
    47 public:
       
    48 	static CMemEntry* New(const TEntry& anEntry);
       
    49 	~CMemEntry();
       
    50 	TInt EntrySize();
       
    51 	void Write();
       
    52 	void WriteFile();
       
    53 	inline TBool IsDir() {return(iEntry.iAtt&KEntryAttDir);}
       
    54 	inline CMemDir& Dir() {return(*iDir);}
       
    55 	inline void SetDir(CMemDir& aDir) {iDir=(&aDir);}
       
    56 	inline TInt Size() {return(iSize);}
       
    57 	inline TLinAddr Base() {return(iBase);}
       
    58 	inline void SetBase();
       
    59 	inline const TDesC& Name() {return(*iName);}
       
    60 	inline static TInt LinkOffset() {return(_FOFF(CMemEntry,iLink));}
       
    61 protected:
       
    62 	CMemEntry(const TEntry& anEntry);
       
    63 private:
       
    64 	TLinAddr iBase;
       
    65 	TInt iSize;
       
    66 	TRomEntry iEntry;
       
    67 	HBufC* iName;
       
    68 	CMemDir* iDir;
       
    69 	TDblQueLink iLink;
       
    70 	};
       
    71 
       
    72 class CMemDir : public CBase
       
    73 	{
       
    74 public:
       
    75 	static CMemDir* NewL();
       
    76 	~CMemDir();
       
    77 	void LoadDirL(const TDesC& aPath);
       
    78 	void SetBaseDirs();
       
    79 	void SetBaseFiles();
       
    80 	void WriteDirs();
       
    81 	void WriteFiles();
       
    82 	void Write();
       
    83 	TFileName Name(const TDesC& aName);
       
    84 	inline TLinAddr Base() {return(iBase);}
       
    85 protected:
       
    86 	CMemDir();
       
    87 private:
       
    88 	TInt iSize;
       
    89 	TInt iCount;
       
    90 	TLinAddr iBase;
       
    91 	HBufC* iPath;
       
    92 	TDblQue<CMemEntry> iEntryQ;
       
    93 	};
       
    94 
       
    95 class RFileX : public RFile
       
    96 	{
       
    97 public:
       
    98 	RFileX() : RFile() {}
       
    99 	TInt Write(const TDesC8& aDes);
       
   100 	TInt Write(const TDesC8& aDes,TInt aLength);
       
   101 	};
       
   102 
       
   103 GLDEF_D RTest test(_L("T_ROMG"));
       
   104 LOCAL_D RFileX TheFile;
       
   105 LOCAL_D TRomHeader TheRomHeader;
       
   106 LOCAL_D TLinAddr TheCurrentBase;
       
   107 LOCAL_D CMemDir* TheRootDir;
       
   108 LOCAL_D TInt TheLevel;
       
   109 LOCAL_D TBuf8<KBufSize> TheBuf;
       
   110 LOCAL_D TBuf8<KFillerSize> TheFiller;
       
   111 #if defined(_UNICODE)
       
   112 LOCAL_D const TPtrC TheFileName=_L("ROMFILEU.BIN");
       
   113 #else
       
   114 LOCAL_D const TPtrC TheFileName=_L("ROMFILE.BIN");
       
   115 #endif
       
   116 
       
   117 TInt RFileX::Write(const TDesC8& aDes)
       
   118 //
       
   119 // Write and update the file pos
       
   120 //
       
   121 	{
       
   122 
       
   123 	TheCurrentBase+=aDes.Size();
       
   124 	return(RFile::Write(aDes));
       
   125 	}
       
   126 
       
   127 TInt RFileX::Write(const TDesC8& aDes,TInt aLength)
       
   128 //
       
   129 // Write and update the file pos
       
   130 //
       
   131 	{
       
   132 
       
   133 	TheCurrentBase+=aLength;
       
   134 	return(RFile::Write(aDes,aLength));
       
   135 	}
       
   136 
       
   137 CMemEntry* CMemEntry::New(const TEntry& anEntry)
       
   138 //
       
   139 // Create a new entry.
       
   140 //
       
   141 	{
       
   142 
       
   143 	CMemEntry* pE=new(ELeave) CMemEntry(anEntry);
       
   144 	pE->iName=anEntry.iName.Alloc();
       
   145 	test(pE->iName!=NULL);
       
   146 	return(pE);
       
   147 	}
       
   148 
       
   149 #pragma warning( disable : 4705 )	// statement has no effect
       
   150 CMemEntry::CMemEntry(const TEntry& anEntry)
       
   151 //
       
   152 // Constructor.
       
   153 //
       
   154 	{
       
   155 
       
   156 	iEntry.iAtt=(TUint8)(anEntry.iAtt|KEntryAttReadOnly); // All rom files are read only
       
   157 	iEntry.iSize=anEntry.iSize;
       
   158 	iEntry.iNameLength=(TUint8)anEntry.iName.Size();
       
   159 	iSize=Align4(anEntry.iSize);
       
   160 	__DECLARE_NAME(_S("CMemEntry"));
       
   161 	}
       
   162 #pragma warning( default : 4705 )
       
   163 
       
   164 CMemEntry::~CMemEntry()
       
   165 //
       
   166 // Destruct.
       
   167 //
       
   168 	{
       
   169 
       
   170 	if (iLink.iNext!=NULL)
       
   171 		iLink.Deque();
       
   172 	delete iName;
       
   173 	if (IsDir())
       
   174 		delete iDir;
       
   175 	}
       
   176 
       
   177 void CMemEntry::SetBase()
       
   178 //
       
   179 // Set the entries base address.
       
   180 //
       
   181 	{
       
   182 
       
   183 	iBase=TheCurrentBase;
       
   184 	iEntry.iAddressLin=TheCurrentBase;
       
   185 	}
       
   186 
       
   187 TInt CMemEntry::EntrySize()
       
   188 //
       
   189 // Calculate the entries size.
       
   190 //
       
   191 	{
       
   192 
       
   193 	return(Align4(KRomEntrySize+iEntry.iNameLength));
       
   194 	}
       
   195 
       
   196 void CMemEntry::Write()
       
   197 //
       
   198 // Write the current entry.
       
   199 //
       
   200 	{
       
   201 
       
   202 	test(TheFile.Write(TPtrC8((TUint8*)&iEntry,KRomEntrySize))==KErrNone);
       
   203 	test(TheFile.Write(TPtrC8((TUint8*)iName->Ptr(),iName->Size()))==KErrNone);
       
   204 	TInt rem=EntrySize()-iName->Size()-KRomEntrySize;
       
   205 	if (rem)
       
   206 		test(TheFile.Write(TheFiller,rem)==KErrNone);
       
   207 	}
       
   208 
       
   209 void CMemEntry::WriteFile()
       
   210 //
       
   211 // Write the current entries file.
       
   212 //
       
   213 	{
       
   214 
       
   215 	test(iBase==TheCurrentBase);
       
   216 	TAutoClose<RFile> f;
       
   217 	TFileName n=Dir().Name(*iName);
       
   218 	test(f.iObj.Open(TheFs,n,EFileStream|EFileRead)==KErrNone);
       
   219 	TInt size=0;
       
   220 	do
       
   221 		{
       
   222 		test(f.iObj.Read(TheBuf)==KErrNone);
       
   223 		test(TheFile.Write(TheBuf)==KErrNone);
       
   224 		size+=TheBuf.Length();
       
   225 		} while (TheBuf.Length()==TheBuf.MaxLength());
       
   226 	test(iEntry.iSize==size);
       
   227 	TInt rem=iSize-size;
       
   228 	if (rem)
       
   229 		test(TheFile.Write(TheFiller,rem)==KErrNone);
       
   230 	}
       
   231 
       
   232 CMemDir* CMemDir::NewL()
       
   233 //
       
   234 // Create a new directory.
       
   235 //
       
   236 	{
       
   237 
       
   238 	return(new(ELeave) CMemDir);
       
   239 	}
       
   240 
       
   241 CMemDir::CMemDir()
       
   242 //
       
   243 // Constructor.
       
   244 //
       
   245 	: iEntryQ(CMemEntry::LinkOffset())
       
   246 	{
       
   247 
       
   248 	__DECLARE_NAME(_S("CMemDir"));
       
   249 	}
       
   250 
       
   251 CMemDir::~CMemDir()
       
   252 //
       
   253 // Destruct.
       
   254 //
       
   255 	{
       
   256 
       
   257 	while (!iEntryQ.IsEmpty())
       
   258 		{
       
   259 		CMemEntry* pE=iEntryQ.First();
       
   260 		delete pE;
       
   261 		}
       
   262 	delete iPath;
       
   263 	}
       
   264 
       
   265 void CMemDir::SetBaseDirs()
       
   266 //
       
   267 // Set the base address of the directory and any sub-directories.
       
   268 //
       
   269 	{
       
   270 
       
   271 	iBase=TheCurrentBase;
       
   272 	iSize=sizeof(TInt);
       
   273 	TDblQueIter<CMemEntry> q(iEntryQ);
       
   274 	CMemEntry* pE;
       
   275 	while ((pE=q++)!=NULL)
       
   276 		{
       
   277 		iCount++;
       
   278 		iSize+=pE->EntrySize();
       
   279 		}
       
   280 	TheCurrentBase+=iSize;
       
   281 	q.SetToFirst();
       
   282 	while ((pE=q++)!=NULL)
       
   283 		{
       
   284 		if (pE->IsDir())
       
   285 			{
       
   286 			pE->SetBase();
       
   287 			pE->Dir().SetBaseDirs();
       
   288 			}
       
   289 		}
       
   290 	}
       
   291 
       
   292 void CMemDir::SetBaseFiles()
       
   293 //
       
   294 // Set the base address of each file.
       
   295 //
       
   296 	{
       
   297 
       
   298 	TDblQueIter<CMemEntry> q(iEntryQ);
       
   299 	CMemEntry* pE;
       
   300 	while ((pE=q++)!=NULL)
       
   301 		{
       
   302 		if (!pE->IsDir())
       
   303 			{
       
   304 			pE->SetBase();
       
   305 			TheCurrentBase+=pE->Size();
       
   306 			}
       
   307 		}
       
   308 	q.SetToFirst();
       
   309 	while ((pE=q++)!=NULL)
       
   310 		{
       
   311 		if (pE->IsDir())
       
   312 			pE->Dir().SetBaseFiles();
       
   313 		}
       
   314 	}
       
   315 
       
   316 void CMemDir::WriteDirs()
       
   317 //
       
   318 // Write the directory and any sub-directories.
       
   319 //
       
   320 	{
       
   321 
       
   322 	TheLevel++;
       
   323 	TFileName name=Name(_L(""));
       
   324 	test.Printf(_L("%*p%S\n"),TheLevel<<1,&name);
       
   325 	Write();
       
   326 	TDblQueIter<CMemEntry> q(iEntryQ);
       
   327 	CMemEntry* pE;
       
   328 	while ((pE=q++)!=NULL)
       
   329 		{
       
   330 		if (pE->IsDir())
       
   331 			pE->Dir().WriteDirs();
       
   332 		}
       
   333 	TheLevel--;
       
   334 	}
       
   335 
       
   336 void CMemDir::WriteFiles()
       
   337 //
       
   338 // Set the base address of each file.
       
   339 //
       
   340 	{
       
   341 
       
   342 	TheLevel++;
       
   343 	TFileName name=Name(_L(""));
       
   344 	test.Printf(_L("%*p%S\n"),TheLevel<<1,&name);
       
   345 	TDblQueIter<CMemEntry> q(iEntryQ);
       
   346 	CMemEntry* pE;
       
   347 	while ((pE=q++)!=NULL)
       
   348 		{
       
   349 		if (!pE->IsDir())
       
   350 			{
       
   351 			test.Printf(_L("%*p%S\n"),(TheLevel<<1)+2,&pE->Name()),
       
   352 			pE->WriteFile();
       
   353 			}
       
   354 		}
       
   355 	q.SetToFirst();
       
   356 	while ((pE=q++)!=NULL)
       
   357 		{
       
   358 		if (pE->IsDir())
       
   359 			pE->Dir().WriteFiles();
       
   360 		}
       
   361 	TheLevel--;
       
   362 	}
       
   363 
       
   364 void CMemDir::Write()
       
   365 //
       
   366 // Write the current directory.
       
   367 //
       
   368 	{
       
   369 
       
   370 	test(iBase==TheCurrentBase);
       
   371 	TInt size=iSize-sizeof(TInt);
       
   372 	test(TheFile.Write(TPtrC8((TUint8*)&size,sizeof(TInt)))==KErrNone);
       
   373 	TDblQueIter<CMemEntry> q(iEntryQ);
       
   374 	CMemEntry* pE;
       
   375 	while ((pE=q++)!=NULL)
       
   376 		pE->Write();
       
   377 	TInt sz=(TheCurrentBase-iBase);
       
   378 	test(sz==iSize);
       
   379 	}
       
   380 
       
   381 TFileName CMemDir::Name(const TDesC& aName)
       
   382 //
       
   383 // Make a full path name from aName.
       
   384 //
       
   385 	{
       
   386 
       
   387 	TFileName n(*iPath);
       
   388 	n+=_L("\\");
       
   389 	n+=aName;
       
   390 	return(n);
       
   391 	}
       
   392 
       
   393 void CMemDir::LoadDirL(const TDesC& aPath)
       
   394 //
       
   395 // Load a directory.
       
   396 //
       
   397 	{
       
   398 
       
   399 	TheLevel++;
       
   400 	iPath=aPath.AllocL();
       
   401 	TFileName name=Name(_L("*.*"));
       
   402 	test.Printf(_L("%*p%S\n"),TheLevel<<1,&name);
       
   403 	CDir* pD;
       
   404 	test(TheFs.GetDir(Name(_L("*.*")),KEntryAttMatchMask,EDirsFirst|ESortByName,pD)==KErrNone);
       
   405 	TInt count=pD->Count();
       
   406 	TInt i=0;
       
   407 	while (i<count)
       
   408 		{
       
   409 		const TEntry& e=(*pD)[i++];
       
   410 		TParse parse;
       
   411 		parse.Set(e.iName,NULL,NULL);
       
   412 		if (!parse.Ext().CompareF(_L(".NCB")))
       
   413 			continue; // Ignore .ncb files - cannot open/read them.
       
   414 		CMemEntry* pE=CMemEntry::New(e);
       
   415 		iEntryQ.AddLast(*pE);
       
   416 		}
       
   417 	delete pD;
       
   418 	TDblQueIter<CMemEntry> q(iEntryQ);
       
   419 	CMemEntry* pE;
       
   420 	while ((pE=q++)!=NULL)
       
   421 		{
       
   422 		if (pE->IsDir())
       
   423 			{
       
   424 			CMemDir* pM=CMemDir::NewL();
       
   425 			pE->SetDir(*pM);
       
   426 			pM->LoadDirL(Name(pE->Name()));
       
   427 			}
       
   428 		else
       
   429 			pE->SetDir(*this);
       
   430 		}
       
   431 	TheLevel--;
       
   432 	}
       
   433 
       
   434 LOCAL_C void buildRomImageL()
       
   435 //
       
   436 // Build the ROM image.
       
   437 //
       
   438 	{
       
   439 
       
   440 	test.Start(_L("Parse command line"));
       
   441 	HBufC* buf=HBufC::New(RProcess().CommandLineLength());
       
   442    	test(buf!=NULL);
       
   443  	TPtr cmd = buf->Des();
       
   444  	RProcess().CommandLine(cmd);
       
   445 	TLex lex(*buf);
       
   446 	TFileName n=lex.NextToken();
       
   447 	if (n.Length()==0)
       
   448 		n=_L("\\F32");
       
   449 	test(n.Length()!=0);
       
   450 	if (n.Right(1)==_L("\\"))
       
   451 		n.SetLength(n.Length()-1);
       
   452 //
       
   453 	test.Next(_L("Create root mem dir"));
       
   454 	TRAPD(r,TheRootDir=CMemDir::NewL());
       
   455 	test(r==KErrNone);
       
   456 //
       
   457 	test.Next(_L("Load directory structure"));
       
   458 	TheLevel=(-1);
       
   459 	TRAP(r,TheRootDir->LoadDirL(n));
       
   460 	test(r==KErrNone);
       
   461 	test(TheLevel==(-1));
       
   462 //
       
   463 	delete buf;
       
   464 	test.End();
       
   465 	}
       
   466 
       
   467 LOCAL_C void baseRomImage()
       
   468 //
       
   469 // Set base addresses for the ROM image.
       
   470 //
       
   471 	{
       
   472 
       
   473 	test.Start(_L("Setting up the header"));
       
   474 	Mem::FillZ(&TheRomHeader,sizeof(TRomHeader));
       
   475 	test.Printf(_L("1"));
       
   476 	TheRomHeader.iVersion=TVersion(1,0,1);
       
   477 	test.Printf(_L("2"));
       
   478 	TTime t;
       
   479 	t.HomeTime();
       
   480 	test.Printf(_L("3"));
       
   481 	TheRomHeader.iTime=t.Int64();
       
   482 	test.Printf(_L("4"));
       
   483 	TheRomHeader.iRomBase=UserSvr::RomHeaderAddress();
       
   484 	test.Printf(_L("5"));
       
   485 	TheRomHeader.iRomRootDirectoryList=TheCurrentBase=UserSvr::RomHeaderAddress()+sizeof(TRomHeader);
       
   486 	test.Printf(_L("6"));
       
   487 //
       
   488 	test.Next(_L("Set dirs base"));
       
   489 	TheRootDir->SetBaseDirs();
       
   490 //
       
   491 	test.Next(_L("Set files base"));
       
   492 	TheRootDir->SetBaseFiles();
       
   493 	TheRomHeader.iRomSize=TheCurrentBase-UserSvr::RomHeaderAddress();
       
   494 //
       
   495 	test.End();
       
   496 	}
       
   497 
       
   498 LOCAL_C void writeRomImage()
       
   499 //
       
   500 // Write the ROM image.
       
   501 //
       
   502 	{
       
   503 
       
   504 	test.Start(_L("Write rom file header"));
       
   505 //
       
   506 	TPckgBuf<TRomFileHeader> fileHeadB;
       
   507 	fileHeadB.FillZ(fileHeadB.MaxLength());
       
   508 	TRomFileHeader& fileHead=fileHeadB();
       
   509 	Mem::Copy(&fileHead.iName[0],"EPOC468 ROM     ",KRomFileHeaderNameSize);
       
   510 	Mem::Copy(&fileHead.iVersionStr[0],"0.01",4);
       
   511 	Mem::Copy(&fileHead.iBuildNumStr[0],"   1",4);
       
   512 	fileHead.iRomSize=TheRomHeader.iRomSize;
       
   513 	fileHead.iHeaderSize=KRomFileHeaderSize;
       
   514 	test(TheFile.Write(fileHeadB)==KErrNone);
       
   515 //
       
   516 	test.Next(_L("Write rom header"));
       
   517 	TheFiller.FillZ(TheFiller.MaxLength());
       
   518 	TPckgC<TRomHeader> head(TheRomHeader);
       
   519 	test(TheFile.Write(head)==KErrNone);
       
   520 	TheCurrentBase=UserSvr::RomHeaderAddress()+sizeof(TheRomHeader);
       
   521 //
       
   522 	test.Next(_L("Write directories"));
       
   523 	TheLevel=(-1);
       
   524 	TheRootDir->WriteDirs();
       
   525 	test(TheLevel==(-1));
       
   526 //
       
   527 	test.Next(_L("Write files"));
       
   528 	TheLevel=(-1);
       
   529 	TheRootDir->WriteFiles();
       
   530 	test(TheLevel==(-1));
       
   531 //
       
   532 	test.End();
       
   533 	}
       
   534 
       
   535 GLDEF_C void CallTestsL(void)
       
   536 //
       
   537 // Test the file server.
       
   538 //
       
   539     { 	
       
   540 	test.Title();
       
   541 
       
   542 #if defined(__WINS__) 
       
   543 	test.Printf(_L("Running on WINS.  Skipping ROM test"));
       
   544 	return;
       
   545 #endif
       
   546 //
       
   547 	test.Start(_L("Testing rom"));
       
   548 
       
   549 	TInt r=TheFs.Delete(TheFileName);
       
   550 	if (r==KErrAccessDenied)
       
   551 		{
       
   552 		test.Printf(_L("Error: Cannot access %S"),&TheFileName);
       
   553 		test.End();
       
   554 		return;
       
   555 		}
       
   556 	test(r==KErrNone || r==KErrNotFound);
       
   557 //
       
   558 	test.Next(_L("Generating ROM image"));
       
   559 	TRAP(r,buildRomImageL());
       
   560 	test(r==KErrNone);
       
   561 //
       
   562 	test.Next(_L("Basing the rom image"));
       
   563 	baseRomImage();
       
   564 //
       
   565 	TBuf<0x80> b=_L("Writing ROM file ");
       
   566 	b+=TheFileName;
       
   567 	test.Next(b);
       
   568 	r=TheFile.Replace(TheFs,TheFileName,EFileStream|EFileWrite);
       
   569 	test(r==KErrNone);
       
   570 	writeRomImage();
       
   571 	TheFile.Close();
       
   572 	delete TheRootDir;
       
   573 
       
   574 	test.End();
       
   575 	return;
       
   576     }
       
   577