First public contribution.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // f32test\manager\t_romg.cpp
21 #include "..\server\t_server.h"
23 const TInt KBufSize=0x10000;
24 const TInt KFillerSize=0x100;
25 const TInt KRomFileHeaderSize=0x100;
26 const TInt KRomFileHeaderNameSize=0x10;
28 class TRomFileHeaderBase
31 TText8 iName[KRomFileHeaderNameSize];
32 TText8 iVersionStr[4];
33 TText8 iBuildNumStr[4];
38 class TRomFileHeader : public TRomFileHeaderBase
41 TUint8 iFiller[KRomFileHeaderSize-sizeof(TRomFileHeaderBase)];
45 class CMemEntry : public CBase
48 static CMemEntry* New(const TEntry& anEntry);
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));}
62 CMemEntry(const TEntry& anEntry);
72 class CMemDir : public CBase
75 static CMemDir* NewL();
77 void LoadDirL(const TDesC& aPath);
83 TFileName Name(const TDesC& aName);
84 inline TLinAddr Base() {return(iBase);}
92 TDblQue<CMemEntry> iEntryQ;
95 class RFileX : public RFile
99 TInt Write(const TDesC8& aDes);
100 TInt Write(const TDesC8& aDes,TInt aLength);
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");
114 LOCAL_D const TPtrC TheFileName=_L("ROMFILE.BIN");
117 TInt RFileX::Write(const TDesC8& aDes)
119 // Write and update the file pos
123 TheCurrentBase+=aDes.Size();
124 return(RFile::Write(aDes));
127 TInt RFileX::Write(const TDesC8& aDes,TInt aLength)
129 // Write and update the file pos
133 TheCurrentBase+=aLength;
134 return(RFile::Write(aDes,aLength));
137 CMemEntry* CMemEntry::New(const TEntry& anEntry)
139 // Create a new entry.
143 CMemEntry* pE=new(ELeave) CMemEntry(anEntry);
144 pE->iName=anEntry.iName.Alloc();
145 test(pE->iName!=NULL);
149 #pragma warning( disable : 4705 ) // statement has no effect
150 CMemEntry::CMemEntry(const TEntry& anEntry)
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"));
162 #pragma warning( default : 4705 )
164 CMemEntry::~CMemEntry()
170 if (iLink.iNext!=NULL)
177 void CMemEntry::SetBase()
179 // Set the entries base address.
183 iBase=TheCurrentBase;
184 iEntry.iAddressLin=TheCurrentBase;
187 TInt CMemEntry::EntrySize()
189 // Calculate the entries size.
193 return(Align4(KRomEntrySize+iEntry.iNameLength));
196 void CMemEntry::Write()
198 // Write the current entry.
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;
206 test(TheFile.Write(TheFiller,rem)==KErrNone);
209 void CMemEntry::WriteFile()
211 // Write the current entries file.
215 test(iBase==TheCurrentBase);
217 TFileName n=Dir().Name(*iName);
218 test(f.iObj.Open(TheFs,n,EFileStream|EFileRead)==KErrNone);
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);
229 test(TheFile.Write(TheFiller,rem)==KErrNone);
232 CMemDir* CMemDir::NewL()
234 // Create a new directory.
238 return(new(ELeave) CMemDir);
245 : iEntryQ(CMemEntry::LinkOffset())
248 __DECLARE_NAME(_S("CMemDir"));
257 while (!iEntryQ.IsEmpty())
259 CMemEntry* pE=iEntryQ.First();
265 void CMemDir::SetBaseDirs()
267 // Set the base address of the directory and any sub-directories.
271 iBase=TheCurrentBase;
273 TDblQueIter<CMemEntry> q(iEntryQ);
275 while ((pE=q++)!=NULL)
278 iSize+=pE->EntrySize();
280 TheCurrentBase+=iSize;
282 while ((pE=q++)!=NULL)
287 pE->Dir().SetBaseDirs();
292 void CMemDir::SetBaseFiles()
294 // Set the base address of each file.
298 TDblQueIter<CMemEntry> q(iEntryQ);
300 while ((pE=q++)!=NULL)
305 TheCurrentBase+=pE->Size();
309 while ((pE=q++)!=NULL)
312 pE->Dir().SetBaseFiles();
316 void CMemDir::WriteDirs()
318 // Write the directory and any sub-directories.
323 TFileName name=Name(_L(""));
324 test.Printf(_L("%*p%S\n"),TheLevel<<1,&name);
326 TDblQueIter<CMemEntry> q(iEntryQ);
328 while ((pE=q++)!=NULL)
331 pE->Dir().WriteDirs();
336 void CMemDir::WriteFiles()
338 // Set the base address of each file.
343 TFileName name=Name(_L(""));
344 test.Printf(_L("%*p%S\n"),TheLevel<<1,&name);
345 TDblQueIter<CMemEntry> q(iEntryQ);
347 while ((pE=q++)!=NULL)
351 test.Printf(_L("%*p%S\n"),(TheLevel<<1)+2,&pE->Name()),
356 while ((pE=q++)!=NULL)
359 pE->Dir().WriteFiles();
364 void CMemDir::Write()
366 // Write the current directory.
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);
375 while ((pE=q++)!=NULL)
377 TInt sz=(TheCurrentBase-iBase);
381 TFileName CMemDir::Name(const TDesC& aName)
383 // Make a full path name from aName.
393 void CMemDir::LoadDirL(const TDesC& aPath)
400 iPath=aPath.AllocL();
401 TFileName name=Name(_L("*.*"));
402 test.Printf(_L("%*p%S\n"),TheLevel<<1,&name);
404 test(TheFs.GetDir(Name(_L("*.*")),KEntryAttMatchMask,EDirsFirst|ESortByName,pD)==KErrNone);
405 TInt count=pD->Count();
409 const TEntry& e=(*pD)[i++];
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);
418 TDblQueIter<CMemEntry> q(iEntryQ);
420 while ((pE=q++)!=NULL)
424 CMemDir* pM=CMemDir::NewL();
426 pM->LoadDirL(Name(pE->Name()));
434 LOCAL_C void buildRomImageL()
436 // Build the ROM image.
440 test.Start(_L("Parse command line"));
441 HBufC* buf=HBufC::New(RProcess().CommandLineLength());
443 TPtr cmd = buf->Des();
444 RProcess().CommandLine(cmd);
446 TFileName n=lex.NextToken();
450 if (n.Right(1)==_L("\\"))
451 n.SetLength(n.Length()-1);
453 test.Next(_L("Create root mem dir"));
454 TRAPD(r,TheRootDir=CMemDir::NewL());
457 test.Next(_L("Load directory structure"));
459 TRAP(r,TheRootDir->LoadDirL(n));
461 test(TheLevel==(-1));
467 LOCAL_C void baseRomImage()
469 // Set base addresses for the ROM image.
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"));
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"));
488 test.Next(_L("Set dirs base"));
489 TheRootDir->SetBaseDirs();
491 test.Next(_L("Set files base"));
492 TheRootDir->SetBaseFiles();
493 TheRomHeader.iRomSize=TheCurrentBase-UserSvr::RomHeaderAddress();
498 LOCAL_C void writeRomImage()
500 // Write the ROM image.
504 test.Start(_L("Write rom file header"));
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);
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);
522 test.Next(_L("Write directories"));
524 TheRootDir->WriteDirs();
525 test(TheLevel==(-1));
527 test.Next(_L("Write files"));
529 TheRootDir->WriteFiles();
530 test(TheLevel==(-1));
535 GLDEF_C void CallTestsL(void)
537 // Test the file server.
542 #if defined(__WINS__)
543 test.Printf(_L("Running on WINS. Skipping ROM test"));
547 test.Start(_L("Testing rom"));
549 TInt r=TheFs.Delete(TheFileName);
550 if (r==KErrAccessDenied)
552 test.Printf(_L("Error: Cannot access %S"),&TheFileName);
556 test(r==KErrNone || r==KErrNotFound);
558 test.Next(_L("Generating ROM image"));
559 TRAP(r,buildRomImageL());
562 test.Next(_L("Basing the rom image"));
565 TBuf<0x80> b=_L("Writing ROM file ");
568 r=TheFile.Replace(TheFs,TheFileName,EFileStream|EFileWrite);