os/kernelhwsrv/kerneltest/f32test/manager/t_romg.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/manager/t_romg.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,577 @@
     1.4 +// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// f32test\manager\t_romg.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include <f32file.h>
    1.22 +#include <e32test.h>
    1.23 +#include <e32rom.h>
    1.24 +#include "..\server\t_server.h"
    1.25 +
    1.26 +const TInt KBufSize=0x10000;
    1.27 +const TInt KFillerSize=0x100;
    1.28 +const TInt KRomFileHeaderSize=0x100;
    1.29 +const TInt KRomFileHeaderNameSize=0x10;
    1.30 +
    1.31 +class TRomFileHeaderBase
    1.32 +	{
    1.33 +public:
    1.34 +	TText8 iName[KRomFileHeaderNameSize];
    1.35 +	TText8 iVersionStr[4];
    1.36 +	TText8 iBuildNumStr[4];
    1.37 +	TInt iRomSize;
    1.38 +	TInt iHeaderSize;
    1.39 +	};
    1.40 +
    1.41 +class TRomFileHeader : public TRomFileHeaderBase
    1.42 +	{
    1.43 +public:
    1.44 +	TUint8 iFiller[KRomFileHeaderSize-sizeof(TRomFileHeaderBase)];
    1.45 +	};
    1.46 +
    1.47 +class CMemDir;
    1.48 +class CMemEntry : public CBase
    1.49 +	{
    1.50 +public:
    1.51 +	static CMemEntry* New(const TEntry& anEntry);
    1.52 +	~CMemEntry();
    1.53 +	TInt EntrySize();
    1.54 +	void Write();
    1.55 +	void WriteFile();
    1.56 +	inline TBool IsDir() {return(iEntry.iAtt&KEntryAttDir);}
    1.57 +	inline CMemDir& Dir() {return(*iDir);}
    1.58 +	inline void SetDir(CMemDir& aDir) {iDir=(&aDir);}
    1.59 +	inline TInt Size() {return(iSize);}
    1.60 +	inline TLinAddr Base() {return(iBase);}
    1.61 +	inline void SetBase();
    1.62 +	inline const TDesC& Name() {return(*iName);}
    1.63 +	inline static TInt LinkOffset() {return(_FOFF(CMemEntry,iLink));}
    1.64 +protected:
    1.65 +	CMemEntry(const TEntry& anEntry);
    1.66 +private:
    1.67 +	TLinAddr iBase;
    1.68 +	TInt iSize;
    1.69 +	TRomEntry iEntry;
    1.70 +	HBufC* iName;
    1.71 +	CMemDir* iDir;
    1.72 +	TDblQueLink iLink;
    1.73 +	};
    1.74 +
    1.75 +class CMemDir : public CBase
    1.76 +	{
    1.77 +public:
    1.78 +	static CMemDir* NewL();
    1.79 +	~CMemDir();
    1.80 +	void LoadDirL(const TDesC& aPath);
    1.81 +	void SetBaseDirs();
    1.82 +	void SetBaseFiles();
    1.83 +	void WriteDirs();
    1.84 +	void WriteFiles();
    1.85 +	void Write();
    1.86 +	TFileName Name(const TDesC& aName);
    1.87 +	inline TLinAddr Base() {return(iBase);}
    1.88 +protected:
    1.89 +	CMemDir();
    1.90 +private:
    1.91 +	TInt iSize;
    1.92 +	TInt iCount;
    1.93 +	TLinAddr iBase;
    1.94 +	HBufC* iPath;
    1.95 +	TDblQue<CMemEntry> iEntryQ;
    1.96 +	};
    1.97 +
    1.98 +class RFileX : public RFile
    1.99 +	{
   1.100 +public:
   1.101 +	RFileX() : RFile() {}
   1.102 +	TInt Write(const TDesC8& aDes);
   1.103 +	TInt Write(const TDesC8& aDes,TInt aLength);
   1.104 +	};
   1.105 +
   1.106 +GLDEF_D RTest test(_L("T_ROMG"));
   1.107 +LOCAL_D RFileX TheFile;
   1.108 +LOCAL_D TRomHeader TheRomHeader;
   1.109 +LOCAL_D TLinAddr TheCurrentBase;
   1.110 +LOCAL_D CMemDir* TheRootDir;
   1.111 +LOCAL_D TInt TheLevel;
   1.112 +LOCAL_D TBuf8<KBufSize> TheBuf;
   1.113 +LOCAL_D TBuf8<KFillerSize> TheFiller;
   1.114 +#if defined(_UNICODE)
   1.115 +LOCAL_D const TPtrC TheFileName=_L("ROMFILEU.BIN");
   1.116 +#else
   1.117 +LOCAL_D const TPtrC TheFileName=_L("ROMFILE.BIN");
   1.118 +#endif
   1.119 +
   1.120 +TInt RFileX::Write(const TDesC8& aDes)
   1.121 +//
   1.122 +// Write and update the file pos
   1.123 +//
   1.124 +	{
   1.125 +
   1.126 +	TheCurrentBase+=aDes.Size();
   1.127 +	return(RFile::Write(aDes));
   1.128 +	}
   1.129 +
   1.130 +TInt RFileX::Write(const TDesC8& aDes,TInt aLength)
   1.131 +//
   1.132 +// Write and update the file pos
   1.133 +//
   1.134 +	{
   1.135 +
   1.136 +	TheCurrentBase+=aLength;
   1.137 +	return(RFile::Write(aDes,aLength));
   1.138 +	}
   1.139 +
   1.140 +CMemEntry* CMemEntry::New(const TEntry& anEntry)
   1.141 +//
   1.142 +// Create a new entry.
   1.143 +//
   1.144 +	{
   1.145 +
   1.146 +	CMemEntry* pE=new(ELeave) CMemEntry(anEntry);
   1.147 +	pE->iName=anEntry.iName.Alloc();
   1.148 +	test(pE->iName!=NULL);
   1.149 +	return(pE);
   1.150 +	}
   1.151 +
   1.152 +#pragma warning( disable : 4705 )	// statement has no effect
   1.153 +CMemEntry::CMemEntry(const TEntry& anEntry)
   1.154 +//
   1.155 +// Constructor.
   1.156 +//
   1.157 +	{
   1.158 +
   1.159 +	iEntry.iAtt=(TUint8)(anEntry.iAtt|KEntryAttReadOnly); // All rom files are read only
   1.160 +	iEntry.iSize=anEntry.iSize;
   1.161 +	iEntry.iNameLength=(TUint8)anEntry.iName.Size();
   1.162 +	iSize=Align4(anEntry.iSize);
   1.163 +	__DECLARE_NAME(_S("CMemEntry"));
   1.164 +	}
   1.165 +#pragma warning( default : 4705 )
   1.166 +
   1.167 +CMemEntry::~CMemEntry()
   1.168 +//
   1.169 +// Destruct.
   1.170 +//
   1.171 +	{
   1.172 +
   1.173 +	if (iLink.iNext!=NULL)
   1.174 +		iLink.Deque();
   1.175 +	delete iName;
   1.176 +	if (IsDir())
   1.177 +		delete iDir;
   1.178 +	}
   1.179 +
   1.180 +void CMemEntry::SetBase()
   1.181 +//
   1.182 +// Set the entries base address.
   1.183 +//
   1.184 +	{
   1.185 +
   1.186 +	iBase=TheCurrentBase;
   1.187 +	iEntry.iAddressLin=TheCurrentBase;
   1.188 +	}
   1.189 +
   1.190 +TInt CMemEntry::EntrySize()
   1.191 +//
   1.192 +// Calculate the entries size.
   1.193 +//
   1.194 +	{
   1.195 +
   1.196 +	return(Align4(KRomEntrySize+iEntry.iNameLength));
   1.197 +	}
   1.198 +
   1.199 +void CMemEntry::Write()
   1.200 +//
   1.201 +// Write the current entry.
   1.202 +//
   1.203 +	{
   1.204 +
   1.205 +	test(TheFile.Write(TPtrC8((TUint8*)&iEntry,KRomEntrySize))==KErrNone);
   1.206 +	test(TheFile.Write(TPtrC8((TUint8*)iName->Ptr(),iName->Size()))==KErrNone);
   1.207 +	TInt rem=EntrySize()-iName->Size()-KRomEntrySize;
   1.208 +	if (rem)
   1.209 +		test(TheFile.Write(TheFiller,rem)==KErrNone);
   1.210 +	}
   1.211 +
   1.212 +void CMemEntry::WriteFile()
   1.213 +//
   1.214 +// Write the current entries file.
   1.215 +//
   1.216 +	{
   1.217 +
   1.218 +	test(iBase==TheCurrentBase);
   1.219 +	TAutoClose<RFile> f;
   1.220 +	TFileName n=Dir().Name(*iName);
   1.221 +	test(f.iObj.Open(TheFs,n,EFileStream|EFileRead)==KErrNone);
   1.222 +	TInt size=0;
   1.223 +	do
   1.224 +		{
   1.225 +		test(f.iObj.Read(TheBuf)==KErrNone);
   1.226 +		test(TheFile.Write(TheBuf)==KErrNone);
   1.227 +		size+=TheBuf.Length();
   1.228 +		} while (TheBuf.Length()==TheBuf.MaxLength());
   1.229 +	test(iEntry.iSize==size);
   1.230 +	TInt rem=iSize-size;
   1.231 +	if (rem)
   1.232 +		test(TheFile.Write(TheFiller,rem)==KErrNone);
   1.233 +	}
   1.234 +
   1.235 +CMemDir* CMemDir::NewL()
   1.236 +//
   1.237 +// Create a new directory.
   1.238 +//
   1.239 +	{
   1.240 +
   1.241 +	return(new(ELeave) CMemDir);
   1.242 +	}
   1.243 +
   1.244 +CMemDir::CMemDir()
   1.245 +//
   1.246 +// Constructor.
   1.247 +//
   1.248 +	: iEntryQ(CMemEntry::LinkOffset())
   1.249 +	{
   1.250 +
   1.251 +	__DECLARE_NAME(_S("CMemDir"));
   1.252 +	}
   1.253 +
   1.254 +CMemDir::~CMemDir()
   1.255 +//
   1.256 +// Destruct.
   1.257 +//
   1.258 +	{
   1.259 +
   1.260 +	while (!iEntryQ.IsEmpty())
   1.261 +		{
   1.262 +		CMemEntry* pE=iEntryQ.First();
   1.263 +		delete pE;
   1.264 +		}
   1.265 +	delete iPath;
   1.266 +	}
   1.267 +
   1.268 +void CMemDir::SetBaseDirs()
   1.269 +//
   1.270 +// Set the base address of the directory and any sub-directories.
   1.271 +//
   1.272 +	{
   1.273 +
   1.274 +	iBase=TheCurrentBase;
   1.275 +	iSize=sizeof(TInt);
   1.276 +	TDblQueIter<CMemEntry> q(iEntryQ);
   1.277 +	CMemEntry* pE;
   1.278 +	while ((pE=q++)!=NULL)
   1.279 +		{
   1.280 +		iCount++;
   1.281 +		iSize+=pE->EntrySize();
   1.282 +		}
   1.283 +	TheCurrentBase+=iSize;
   1.284 +	q.SetToFirst();
   1.285 +	while ((pE=q++)!=NULL)
   1.286 +		{
   1.287 +		if (pE->IsDir())
   1.288 +			{
   1.289 +			pE->SetBase();
   1.290 +			pE->Dir().SetBaseDirs();
   1.291 +			}
   1.292 +		}
   1.293 +	}
   1.294 +
   1.295 +void CMemDir::SetBaseFiles()
   1.296 +//
   1.297 +// Set the base address of each file.
   1.298 +//
   1.299 +	{
   1.300 +
   1.301 +	TDblQueIter<CMemEntry> q(iEntryQ);
   1.302 +	CMemEntry* pE;
   1.303 +	while ((pE=q++)!=NULL)
   1.304 +		{
   1.305 +		if (!pE->IsDir())
   1.306 +			{
   1.307 +			pE->SetBase();
   1.308 +			TheCurrentBase+=pE->Size();
   1.309 +			}
   1.310 +		}
   1.311 +	q.SetToFirst();
   1.312 +	while ((pE=q++)!=NULL)
   1.313 +		{
   1.314 +		if (pE->IsDir())
   1.315 +			pE->Dir().SetBaseFiles();
   1.316 +		}
   1.317 +	}
   1.318 +
   1.319 +void CMemDir::WriteDirs()
   1.320 +//
   1.321 +// Write the directory and any sub-directories.
   1.322 +//
   1.323 +	{
   1.324 +
   1.325 +	TheLevel++;
   1.326 +	TFileName name=Name(_L(""));
   1.327 +	test.Printf(_L("%*p%S\n"),TheLevel<<1,&name);
   1.328 +	Write();
   1.329 +	TDblQueIter<CMemEntry> q(iEntryQ);
   1.330 +	CMemEntry* pE;
   1.331 +	while ((pE=q++)!=NULL)
   1.332 +		{
   1.333 +		if (pE->IsDir())
   1.334 +			pE->Dir().WriteDirs();
   1.335 +		}
   1.336 +	TheLevel--;
   1.337 +	}
   1.338 +
   1.339 +void CMemDir::WriteFiles()
   1.340 +//
   1.341 +// Set the base address of each file.
   1.342 +//
   1.343 +	{
   1.344 +
   1.345 +	TheLevel++;
   1.346 +	TFileName name=Name(_L(""));
   1.347 +	test.Printf(_L("%*p%S\n"),TheLevel<<1,&name);
   1.348 +	TDblQueIter<CMemEntry> q(iEntryQ);
   1.349 +	CMemEntry* pE;
   1.350 +	while ((pE=q++)!=NULL)
   1.351 +		{
   1.352 +		if (!pE->IsDir())
   1.353 +			{
   1.354 +			test.Printf(_L("%*p%S\n"),(TheLevel<<1)+2,&pE->Name()),
   1.355 +			pE->WriteFile();
   1.356 +			}
   1.357 +		}
   1.358 +	q.SetToFirst();
   1.359 +	while ((pE=q++)!=NULL)
   1.360 +		{
   1.361 +		if (pE->IsDir())
   1.362 +			pE->Dir().WriteFiles();
   1.363 +		}
   1.364 +	TheLevel--;
   1.365 +	}
   1.366 +
   1.367 +void CMemDir::Write()
   1.368 +//
   1.369 +// Write the current directory.
   1.370 +//
   1.371 +	{
   1.372 +
   1.373 +	test(iBase==TheCurrentBase);
   1.374 +	TInt size=iSize-sizeof(TInt);
   1.375 +	test(TheFile.Write(TPtrC8((TUint8*)&size,sizeof(TInt)))==KErrNone);
   1.376 +	TDblQueIter<CMemEntry> q(iEntryQ);
   1.377 +	CMemEntry* pE;
   1.378 +	while ((pE=q++)!=NULL)
   1.379 +		pE->Write();
   1.380 +	TInt sz=(TheCurrentBase-iBase);
   1.381 +	test(sz==iSize);
   1.382 +	}
   1.383 +
   1.384 +TFileName CMemDir::Name(const TDesC& aName)
   1.385 +//
   1.386 +// Make a full path name from aName.
   1.387 +//
   1.388 +	{
   1.389 +
   1.390 +	TFileName n(*iPath);
   1.391 +	n+=_L("\\");
   1.392 +	n+=aName;
   1.393 +	return(n);
   1.394 +	}
   1.395 +
   1.396 +void CMemDir::LoadDirL(const TDesC& aPath)
   1.397 +//
   1.398 +// Load a directory.
   1.399 +//
   1.400 +	{
   1.401 +
   1.402 +	TheLevel++;
   1.403 +	iPath=aPath.AllocL();
   1.404 +	TFileName name=Name(_L("*.*"));
   1.405 +	test.Printf(_L("%*p%S\n"),TheLevel<<1,&name);
   1.406 +	CDir* pD;
   1.407 +	test(TheFs.GetDir(Name(_L("*.*")),KEntryAttMatchMask,EDirsFirst|ESortByName,pD)==KErrNone);
   1.408 +	TInt count=pD->Count();
   1.409 +	TInt i=0;
   1.410 +	while (i<count)
   1.411 +		{
   1.412 +		const TEntry& e=(*pD)[i++];
   1.413 +		TParse parse;
   1.414 +		parse.Set(e.iName,NULL,NULL);
   1.415 +		if (!parse.Ext().CompareF(_L(".NCB")))
   1.416 +			continue; // Ignore .ncb files - cannot open/read them.
   1.417 +		CMemEntry* pE=CMemEntry::New(e);
   1.418 +		iEntryQ.AddLast(*pE);
   1.419 +		}
   1.420 +	delete pD;
   1.421 +	TDblQueIter<CMemEntry> q(iEntryQ);
   1.422 +	CMemEntry* pE;
   1.423 +	while ((pE=q++)!=NULL)
   1.424 +		{
   1.425 +		if (pE->IsDir())
   1.426 +			{
   1.427 +			CMemDir* pM=CMemDir::NewL();
   1.428 +			pE->SetDir(*pM);
   1.429 +			pM->LoadDirL(Name(pE->Name()));
   1.430 +			}
   1.431 +		else
   1.432 +			pE->SetDir(*this);
   1.433 +		}
   1.434 +	TheLevel--;
   1.435 +	}
   1.436 +
   1.437 +LOCAL_C void buildRomImageL()
   1.438 +//
   1.439 +// Build the ROM image.
   1.440 +//
   1.441 +	{
   1.442 +
   1.443 +	test.Start(_L("Parse command line"));
   1.444 +	HBufC* buf=HBufC::New(RProcess().CommandLineLength());
   1.445 +   	test(buf!=NULL);
   1.446 + 	TPtr cmd = buf->Des();
   1.447 + 	RProcess().CommandLine(cmd);
   1.448 +	TLex lex(*buf);
   1.449 +	TFileName n=lex.NextToken();
   1.450 +	if (n.Length()==0)
   1.451 +		n=_L("\\F32");
   1.452 +	test(n.Length()!=0);
   1.453 +	if (n.Right(1)==_L("\\"))
   1.454 +		n.SetLength(n.Length()-1);
   1.455 +//
   1.456 +	test.Next(_L("Create root mem dir"));
   1.457 +	TRAPD(r,TheRootDir=CMemDir::NewL());
   1.458 +	test(r==KErrNone);
   1.459 +//
   1.460 +	test.Next(_L("Load directory structure"));
   1.461 +	TheLevel=(-1);
   1.462 +	TRAP(r,TheRootDir->LoadDirL(n));
   1.463 +	test(r==KErrNone);
   1.464 +	test(TheLevel==(-1));
   1.465 +//
   1.466 +	delete buf;
   1.467 +	test.End();
   1.468 +	}
   1.469 +
   1.470 +LOCAL_C void baseRomImage()
   1.471 +//
   1.472 +// Set base addresses for the ROM image.
   1.473 +//
   1.474 +	{
   1.475 +
   1.476 +	test.Start(_L("Setting up the header"));
   1.477 +	Mem::FillZ(&TheRomHeader,sizeof(TRomHeader));
   1.478 +	test.Printf(_L("1"));
   1.479 +	TheRomHeader.iVersion=TVersion(1,0,1);
   1.480 +	test.Printf(_L("2"));
   1.481 +	TTime t;
   1.482 +	t.HomeTime();
   1.483 +	test.Printf(_L("3"));
   1.484 +	TheRomHeader.iTime=t.Int64();
   1.485 +	test.Printf(_L("4"));
   1.486 +	TheRomHeader.iRomBase=UserSvr::RomHeaderAddress();
   1.487 +	test.Printf(_L("5"));
   1.488 +	TheRomHeader.iRomRootDirectoryList=TheCurrentBase=UserSvr::RomHeaderAddress()+sizeof(TRomHeader);
   1.489 +	test.Printf(_L("6"));
   1.490 +//
   1.491 +	test.Next(_L("Set dirs base"));
   1.492 +	TheRootDir->SetBaseDirs();
   1.493 +//
   1.494 +	test.Next(_L("Set files base"));
   1.495 +	TheRootDir->SetBaseFiles();
   1.496 +	TheRomHeader.iRomSize=TheCurrentBase-UserSvr::RomHeaderAddress();
   1.497 +//
   1.498 +	test.End();
   1.499 +	}
   1.500 +
   1.501 +LOCAL_C void writeRomImage()
   1.502 +//
   1.503 +// Write the ROM image.
   1.504 +//
   1.505 +	{
   1.506 +
   1.507 +	test.Start(_L("Write rom file header"));
   1.508 +//
   1.509 +	TPckgBuf<TRomFileHeader> fileHeadB;
   1.510 +	fileHeadB.FillZ(fileHeadB.MaxLength());
   1.511 +	TRomFileHeader& fileHead=fileHeadB();
   1.512 +	Mem::Copy(&fileHead.iName[0],"EPOC468 ROM     ",KRomFileHeaderNameSize);
   1.513 +	Mem::Copy(&fileHead.iVersionStr[0],"0.01",4);
   1.514 +	Mem::Copy(&fileHead.iBuildNumStr[0],"   1",4);
   1.515 +	fileHead.iRomSize=TheRomHeader.iRomSize;
   1.516 +	fileHead.iHeaderSize=KRomFileHeaderSize;
   1.517 +	test(TheFile.Write(fileHeadB)==KErrNone);
   1.518 +//
   1.519 +	test.Next(_L("Write rom header"));
   1.520 +	TheFiller.FillZ(TheFiller.MaxLength());
   1.521 +	TPckgC<TRomHeader> head(TheRomHeader);
   1.522 +	test(TheFile.Write(head)==KErrNone);
   1.523 +	TheCurrentBase=UserSvr::RomHeaderAddress()+sizeof(TheRomHeader);
   1.524 +//
   1.525 +	test.Next(_L("Write directories"));
   1.526 +	TheLevel=(-1);
   1.527 +	TheRootDir->WriteDirs();
   1.528 +	test(TheLevel==(-1));
   1.529 +//
   1.530 +	test.Next(_L("Write files"));
   1.531 +	TheLevel=(-1);
   1.532 +	TheRootDir->WriteFiles();
   1.533 +	test(TheLevel==(-1));
   1.534 +//
   1.535 +	test.End();
   1.536 +	}
   1.537 +
   1.538 +GLDEF_C void CallTestsL(void)
   1.539 +//
   1.540 +// Test the file server.
   1.541 +//
   1.542 +    { 	
   1.543 +	test.Title();
   1.544 +
   1.545 +#if defined(__WINS__) 
   1.546 +	test.Printf(_L("Running on WINS.  Skipping ROM test"));
   1.547 +	return;
   1.548 +#endif
   1.549 +//
   1.550 +	test.Start(_L("Testing rom"));
   1.551 +
   1.552 +	TInt r=TheFs.Delete(TheFileName);
   1.553 +	if (r==KErrAccessDenied)
   1.554 +		{
   1.555 +		test.Printf(_L("Error: Cannot access %S"),&TheFileName);
   1.556 +		test.End();
   1.557 +		return;
   1.558 +		}
   1.559 +	test(r==KErrNone || r==KErrNotFound);
   1.560 +//
   1.561 +	test.Next(_L("Generating ROM image"));
   1.562 +	TRAP(r,buildRomImageL());
   1.563 +	test(r==KErrNone);
   1.564 +//
   1.565 +	test.Next(_L("Basing the rom image"));
   1.566 +	baseRomImage();
   1.567 +//
   1.568 +	TBuf<0x80> b=_L("Writing ROM file ");
   1.569 +	b+=TheFileName;
   1.570 +	test.Next(b);
   1.571 +	r=TheFile.Replace(TheFs,TheFileName,EFileStream|EFileWrite);
   1.572 +	test(r==KErrNone);
   1.573 +	writeRomImage();
   1.574 +	TheFile.Close();
   1.575 +	delete TheRootDir;
   1.576 +
   1.577 +	test.End();
   1.578 +	return;
   1.579 +    }
   1.580 +