userlibandfileserver/fileserver/sfile/sf_image.h
changeset 0 a41df078684a
child 6 0173bcd7697c
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1998-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 // f32\sfile\sf_image.h
       
    15 // 
       
    16 //
       
    17 
       
    18 #ifndef __SF_IMAGE_H__
       
    19 #define __SF_IMAGE_H__
       
    20 #include <f32file.h>
       
    21 #include <f32image.h>
       
    22 #include <e32ldr.h>
       
    23 #include <e32rom.h>
       
    24 #include "sf_deflate.h"
       
    25 
       
    26 _LIT(KLitLoader,"LOADER");
       
    27 //__DATA_CAGING__
       
    28 const TUint32	KCapabilityOffSet	=	0x18;
       
    29 const TUint32	KCapabilityLength	=	0x04;
       
    30 const TUint32	KCapabilityMask		=	0x00000001;
       
    31 
       
    32 enum TPanic
       
    33 	{
       
    34 	ELoadLibraryWithoutDllLock=0,
       
    35 	};
       
    36 
       
    37 class E32Image;
       
    38 class RImageArray : public RPointerArray<E32Image>
       
    39 	{
       
    40 public:
       
    41 	RImageArray();
       
    42 	TInt Add(E32Image* aImage);
       
    43 	void Find(const TDesC8& aRootName, TInt& aFirst, TInt& aLast) const;
       
    44 	E32Image* Find(const TRomImageHeader* aRomImgHdr) const;
       
    45 	};
       
    46 
       
    47 class TFileNameInfo
       
    48 	{
       
    49 public:
       
    50 	enum	{
       
    51 			EIncludeDrive=1,
       
    52 			EIncludePath=2,
       
    53 			EIncludeBase=4,
       
    54 			EIncludeVer=8,
       
    55 			EForceVer=16,
       
    56 			EIncludeUid=32,
       
    57 			EForceUid=64,
       
    58 			EIncludeExt=128,
       
    59 			EIncludeDrivePath=EIncludeDrive|EIncludePath,
       
    60 			EIncludeBaseExt=EIncludeBase|EIncludeExt,
       
    61 			EIncludeDrivePathBaseExt=EIncludeDrive|EIncludePath|EIncludeBase|EIncludeExt,
       
    62 			};
       
    63 	enum	{
       
    64 			EAllowUid=1,
       
    65 			EAllowPlaceholder=2,
       
    66 			EAllowDecimalVersion=4,
       
    67 			};
       
    68 public:
       
    69 	TFileNameInfo();
       
    70 	TInt Set(const TDesC8& aFileName, TUint aFlags);
       
    71 	void Dump() const;
       
    72 public:
       
    73 	inline TInt DriveLen() const {return iPathPos;}
       
    74 	inline TInt PathLen() const {return iBasePos-iPathPos;}
       
    75 	inline TInt BaseLen() const {return iVerPos-iBasePos;}
       
    76 	inline TInt VerLen() const {return iUidPos-iVerPos;}
       
    77 	inline TInt UidLen() const {return iExtPos-iUidPos;}
       
    78 	inline TInt ExtLen() const {return iLen-iExtPos;}
       
    79 	inline TPtrC8 Drive() const {return TPtrC8(iName, iPathPos);}
       
    80 	inline TPtrC8 Path() const {return TPtrC8(iName+iPathPos, iBasePos-iPathPos);}
       
    81 	inline TPtrC8 DriveAndPath() const {return TPtrC8(iName, iBasePos);}
       
    82 	inline TPtrC8 Base() const {return TPtrC8(iName+iBasePos, iVerPos-iBasePos);}
       
    83 	inline TPtrC8 VerStr() const {return TPtrC8(iName+iVerPos, iUidPos-iVerPos);}
       
    84 	inline TPtrC8 UidStr() const {return TPtrC8(iName+iUidPos, iExtPos-iUidPos);}
       
    85 	inline TPtrC8 Ext() const {return TPtrC8(iName+iExtPos, iLen-iExtPos);}
       
    86 	inline TUint32 Version() const {return iVersion;}
       
    87 	inline TUint32 Uid() const {return iUid;}
       
    88 	void GetName(TDes8& aName, TUint aFlags) const;
       
    89 public:
       
    90 	const TText8* iName;
       
    91 	TInt iPathPos;
       
    92 	TInt iBasePos;
       
    93 	TInt iVerPos;
       
    94 	TInt iUidPos;
       
    95 	TInt iExtPos;
       
    96 	TInt iLen;
       
    97 	TUint32 iVersion;
       
    98 	TUint32 iUid;
       
    99 	};
       
   100 
       
   101 class RLoaderMsg;
       
   102 
       
   103 // Information used to search for an image file
       
   104 class RLdrReq : public TLdrInfo
       
   105 	{
       
   106 public:
       
   107 	RLdrReq();
       
   108 	void Close();
       
   109 	void Panic(TInt aPanic);
       
   110 	TInt CheckForSubstDriveInName();
       
   111 	TInt CheckForSubstDrivesInPath();
       
   112 	TInt AddFileExtension(const TDesC8& aExt);
       
   113 	void Dump(const char* aTitle) const;
       
   114 	TInt CheckSecInfo(const SSecurityInfo& aCandidate) const;
       
   115 public:
       
   116 	HBufC8* iFileName;
       
   117 	HBufC8* iCmd;
       
   118 	HBufC8* iPath;
       
   119 	const RLoaderMsg* iMsg;
       
   120 	SCapabilitySet iPlatSecCaps;
       
   121 	RThread iClientThread;
       
   122 	RProcess iClientProcess;
       
   123 	TFileNameInfo iFileNameInfo;
       
   124 	E32Image* iImporter;
       
   125 	};
       
   126 
       
   127 class RLoaderFile : public RFile
       
   128 	{
       
   129 public:
       
   130 	inline TBool IsOpen()
       
   131 		{ return Session().Handle() && SubSessionHandle(); }
       
   132 	};
       
   133 
       
   134 class TImageInfo
       
   135 	{
       
   136 public:
       
   137 	TUint32 iUid[KMaxCheckedUid];
       
   138 	TUint32 iModuleVersion;
       
   139 	SSecurityInfo iS;
       
   140 	TUint32 iAttr;
       
   141 	TUint16 iExportDirCount;
       
   142 	TUint8 iExportDescType;
       
   143 	TUint8 iNameLength;
       
   144 	enum TCacheStatusFlags
       
   145 		{
       
   146 		EHashChecked = 1,
       
   147 		};
       
   148 	TUint8 iCacheStatus;
       
   149 	// 8-bit name follows (store base+ext only, not version)
       
   150 	// export description follows name
       
   151 	};
       
   152 
       
   153 // Information returned by a search for an image file
       
   154 class TFileCacheRecord;
       
   155 class RImageInfo : public TImageInfo
       
   156 	{
       
   157 public:
       
   158 	RImageInfo();
       
   159 	RImageInfo& operator=(const TFileCacheRecord& aRecord);
       
   160 	void Close();
       
   161 	void Accept(RImageInfo& aInfo);
       
   162 	inline TBool FileOpened() { return ((RLoaderFile*)&iFile)->IsOpen(); }
       
   163 public:
       
   164 	RFile iFile;
       
   165 	E32ImageHeader* iHeader;				// header if available
       
   166 	TUint8* iFileData;						// file data if it's been loaded
       
   167 	TUint32 iFileSize;						// size of loaded data
       
   168 	const TRomImageHeader* iRomImageHeader;	// pointer to ROM image header for XIP
       
   169 	TUint16 iExportDescSize;
       
   170 	const TUint8* iExportDesc;				// points into cache record so only valid during a single directory search
       
   171 	TUint8 iNeedHashCheck;					// true if hash check was skipped and must be done at load time
       
   172 	};
       
   173 
       
   174 // Image finder - looks at candidates for a load and remembers the best one
       
   175 class RImageFinder
       
   176 	{
       
   177 public:
       
   178 	RImageFinder();
       
   179 	TInt Set(const RLdrReq& aReq);
       
   180 	void Close();
       
   181 	TInt Search();
       
   182 	TInt Search(const TDesC8* aPath, TInt aDrive);
       
   183 	TInt SearchSingleDir();
       
   184 	TInt SearchExisting(const RImageArray& aArray);
       
   185 	TInt Try(RImageInfo& aInfo, const TDesC8& aRootName, const TDesC8& aDriveAndPath);
       
   186 	void RecordCorruptFile();
       
   187 	void SetName(const TDesC8& aRootName, const TDesC8& aDriveAndPath);
       
   188 	void Dump(const char* aTitle, TInt aR);
       
   189 	void CompareHashL(RImageInfo& aInfo, const TDesC8& aDriveAndPath);
       
   190 public:
       
   191 	TInt iNameMatches;				// number of files for which name matches
       
   192 	TInt iUidFail;					// number of files for which UIDs are incompatible
       
   193 	TInt iCapFail;					// number of files for which capabilities/SID are incompatible
       
   194 	TInt iMajorVersionFail;			// number of files with lower major version than requested
       
   195 	TInt iImportFail;				// number of files which failed import check
       
   196 	TUint32 iCurrentVersion;		// version of current best match
       
   197 	TPtrC8 iCurrentPath;			// current search directory
       
   198 	TUint8 iCurrentDrive;			// current search drive
       
   199 	TUint8 iFindExact;
       
   200 	TUint8 iNewValid;				// a valid new image has been found
       
   201 	const RLdrReq* iReq;
       
   202 	E32Image* iExisting;			// pointer to existing image if that is currently the best
       
   203 	RImageInfo iNew;				// new image info if that is currently the best
       
   204 	TBuf8<KMaxFileName> iNewFileName;	// full path name for new image file
       
   205 	TBuf8<KMaxKernelName> iRootName;
       
   206 	};
       
   207 
       
   208 extern RFs gTheLoaderFs;
       
   209 extern TAny* gExeCodeSeg;
       
   210 extern TUint32 gExeAttr;
       
   211 extern TAny* gKernelCodeSeg;
       
   212 extern TUint32 gKernelAttr;
       
   213 extern TBool gExecutesInSupervisorMode;
       
   214 
       
   215 TInt GetModuleInfo(RLdrReq& aReq);
       
   216 TInt GetInfoFromHeader(const RLoaderMsg& aMsg);
       
   217 TInt CheckSystemBin(const TDesC& aThePath);
       
   218 TInt LoadProcess(RLdrReq& aReq);
       
   219 TInt LoadLibrary(RLdrReq& aReq);
       
   220 TInt LoadDeviceDriver(RLdrReq& aReq, TInt aDeviceType);
       
   221 TInt LoadLocale(RLdrReq& aReq, TLibraryFunction* aExportsList);
       
   222 TInt CheckUids(const TUidType& aUids, const TUidType& aRequestedUids);
       
   223 TInt OpenFile8(RFile& aFile, const TDesC8& aName);
       
   224 TInt CheckSubstDrive(TDes8& aDest, const TDesC8& aSrc);
       
   225 TInt CompareVersions(TUint32 aL, TUint32 aR);
       
   226 TInt CheckRequiredImports(E32Image* aImporter, E32Image* aExporter, TInt aAction);
       
   227 TInt CheckRequiredImports(E32Image* aImporter, const RImageInfo& aExporter, TInt aAction);
       
   228 TInt CheckedCollapse(TDes8& aDest, const TDesC16& aSrc);
       
   229 
       
   230 
       
   231 enum TVersionCompareResult
       
   232 	{
       
   233 	EVersion_MinorBigger=0,
       
   234 	EVersion_Exact,
       
   235 	EVersion_MajorBigger,
       
   236 	EVersion_MinorSmaller,
       
   237 	EVersion_MajorSmaller,
       
   238 	};
       
   239 
       
   240 TInt DetailedCompareVersions(TUint32 aCandidate, TUint32 aRequest);
       
   241 TInt DetailedCompareVersions(TUint32 aCandidate, TUint32 aRequest, TUint32 aCurrent, TBool aStrict);
       
   242 
       
   243 enum TVersionCompareAction
       
   244 	{
       
   245 	EAction_Skip=0,
       
   246 	EAction_CheckLastImport,
       
   247 	EAction_CheckImports,
       
   248 	EAction_Replace,
       
   249 	};
       
   250 
       
   251 
       
   252 
       
   253 class E32Image : public TProcessCreateInfo
       
   254 	{
       
   255 public:
       
   256 	E32Image();
       
   257 	~E32Image();
       
   258 	void Reset();
       
   259 
       
   260 	TInt Construct(RImageFinder& aFinder);
       
   261 	void Construct(const TRomImageHeader& a);
       
   262 	TInt OpenFile();
       
   263 
       
   264 	TBool AlwaysLoaded();
       
   265 
       
   266 	TInt LoadProcess(const RLdrReq& aReq);
       
   267 	TInt LoadCodeSeg(const RLdrReq& aReq);
       
   268 	TInt DoLoadCodeSeg(const RLdrReq& aReq, RImageFinder& aFinder);
       
   269 	TInt DoLoadCodeSeg(const TRomImageHeader& aRomImgHdr);
       
   270 	TInt CheckAlreadyLoaded();
       
   271 	TInt CheckRomXIPAlreadyLoaded();
       
   272 	TInt ProcessFileName();
       
   273 	void GetRomFileName();
       
   274 	static TBool TraverseDirs(const TRomDir& aDir, const TRomImageHeader* aHdr, TDes8& aName);
       
   275 
       
   276 	TInt LoadToRam();
       
   277 	TInt LoadFile();
       
   278 	TInt LoadFileNoCompress();
       
   279 	TInt Read(TUint aPos,TUint8* aDest,TUint aSize,TBool aSvPerms=EFalse);
       
   280 	void LoadFileInflateL();
       
   281 	void LoadFileBytePairUnpakL();
       
   282 	TInt RelocateCode();
       
   283 	TInt RelocateExports();
       
   284 	TInt LoadAndRelocateData();
       
   285 	TInt RelocateSection(E32RelocSection* aSection, TUint32 aLoadAddress);
       
   286 	static TUint8* WordCopy(TAny* aDestination, const TAny* aSource, TInt aNumberOfBytes);
       
   287 	static TUint8* MemCopy(TAny* aDestination, const TAny* aSource, TInt aNumberOfBytes);
       
   288 	TInt ReadImportData();
       
   289 
       
   290 	TInt ProcessImports();
       
   291 
       
   292 	TInt LoadDlls(RImageArray& aDllArray);
       
   293 	TInt GetCurrentImportList(const E32ImportBlock* aBlock);
       
   294 	static TInt FixupDlls(RImageArray& aDllArray);
       
   295 	TUint64* ExpandFixups(TInt aNumFixups);
       
   296 	TInt FinaliseDlls(RImageArray& aDllArray);
       
   297 	void CleanupDlls(RImageArray& aDllArray);
       
   298 
       
   299 	TInt LastCurrentImport();
       
   300 	void SortCurrentImportList();
       
   301 	static TInt Order(const E32Image& aL, const E32Image& aR);
       
   302 	TInt ReadExportDirLoad();
       
   303 
       
   304 	// for demand paging...
       
   305 	TInt ShouldBeCodePaged(TBool& aPage);
       
   306 	TInt LoadCompressionData();
       
   307 	TInt LoadCompressionDataNoCompress();
       
   308 	void LoadCompressionDataBytePairUnpakL();
       
   309 	TInt BuildCodeBlockMap();
       
   310 	TInt AllocateRelocationData(E32RelocSection* aSection, TUint32 aAreaSize, TUint32 aLoadAddress, TUint32*& aProcessedBlock);
       
   311 	TInt BuildImportFixupTable();
       
   312 public:
       
   313 	const TRomImageHeader* iRomImageHeader;
       
   314 	E32ImageHeader* iHeader;
       
   315 	E32RelocSection* iCodeRelocSection;		// address within iRestOfFileData 
       
   316 	E32RelocSection* iDataRelocSection;		// address within iRestOfFileData 
       
   317 	TUint32* iImportData;
       
   318 	TUint8* iRestOfFileData;				// buffer holding all file data after code section
       
   319 	TUint32 iRestOfFileSize;				// size of data at iRestOfFileData
       
   320 	TUint32 iConversionOffset;
       
   321 	RFile iFile;
       
   322 	TUint8* iFileData;						// file data if it's been loaded
       
   323 	TUint32 iFileSize;						// size of loaded data
       
   324 	TUint32* iCopyOfExportDir;
       
   325 	TUint32 iExportDirLoad;
       
   326 	TUint32 iExportDirEntryDelta; // value to add to all values read from iExportDirLoad
       
   327 	E32Image* iMain;
       
   328 	TAny* iCloseCodeSeg;
       
   329 	TInt iCurrentImportCount;
       
   330 	TInt iNextImportPos;
       
   331 	TUint32* iCurrentImportList;
       
   332 	TUint8 iCurrentImportListSorted;
       
   333 	TUint8 iIsDll;
       
   334 	TUint8 iAlreadyLoaded;
       
   335 	TUint8 iPadding2;
       
   336 	TInt iFixupCount;				// number of fixups in iFixups
       
   337 	TUint64* iFixups;				// array of fixups to apply to demand paged code {addr,value} pairs
       
   338 	};
       
   339 
       
   340 
       
   341 inline TBool CheckUid(TUint32 aUid, TUint32 aRequestedUid)
       
   342 	{
       
   343 	return (aRequestedUid==0 || aRequestedUid==aUid);
       
   344 	}
       
   345 
       
   346 
       
   347 #endif