emulator/emulatorbsp/estart/estartmain.cpp
changeset 0 cec860690d41
equal deleted inserted replaced
-1:000000000000 0:cec860690d41
       
     1 // Copyright (c) 2004-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 "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 // wins\estart\estartmain.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32std.h>
       
    19 #include <f32fsys.h>
       
    20 #include <e32uid.h>
       
    21 #include "estart.h"
       
    22 
       
    23 #include <e32wins.h>
       
    24 #include <emulator.h>
       
    25 #define WIN32_LEAN_AND_MEAN
       
    26 #pragma warning( disable : 4201 ) // nonstandard extension used : nameless struct/union
       
    27 #include <windows.h>
       
    28 #pragma warning( default : 4201 ) // nonstandard extension used : nameless struct/union
       
    29 
       
    30 
       
    31 
       
    32 _LIT(KLitAddFsFail,"Add fs fail");
       
    33 _LIT(KLitConnectFsFail2,"Connect fs 2 fail");
       
    34 _LIT(KLitNoWS,"No WSERV");
       
    35 _LIT(KStartupModeFail,"Get startup mode failed");
       
    36 _LIT(KLocalFsName,"elocal");
       
    37 
       
    38 class TWinsFSStartup : public TFSStartup
       
    39 	{
       
    40 public:
       
    41 	virtual TInt StartSystem();
       
    42 	virtual TInt DefaultLocalDrive(TInt aLocalDrive);
       
    43 	virtual TPtrC LocalDriveMappingFileName();
       
    44 #if !defined(AUTODETECT_DISABLE)		
       
    45 	virtual TInt GetNextStandardFSInfoEntry(const SFileSystemInfo** anEntry,TInt aPos);
       
    46 #endif
       
    47 public:	
       
    48 	void LocalFSInitialisation();
       
    49 protected:
       
    50 	TInt GetStartupMode();
       
    51 	};
       
    52 
       
    53 #if !defined(AUTODETECT_DISABLE)
       
    54 TInt DetectEmulRAM(RLocalDrive ld, TInt cr, TLocalDriveCapsV2& caps)
       
    55 	{
       
    56 	(void)ld;
       
    57 	(void)cr;
       
    58 	if (caps.iType==EMediaRam)
       
    59 		return KErrNone;
       
    60 	return KErrGeneral;
       
    61 	}
       
    62 
       
    63 TInt DetectEmul_CF_FAT(RLocalDrive ld, TInt cr, TLocalDriveCapsV2& caps)
       
    64 	{
       
    65 	(void)ld;
       
    66 	(void)cr;
       
    67 	if (caps.iType!=EMediaHardDisk || !(caps.iDriveAtt & KDriveAttRemovable))
       
    68 		return KErrGeneral;
       
    69 	if (cr==KErrNotReady)
       
    70 		return KErrNone;	// removable and not ready - assume fat
       
    71 	if (cr==KErrNone && PartitionIsFAT(caps.iPartitionType))
       
    72 		return KErrNone;
       
    73 	return KErrGeneral;
       
    74 	}
       
    75 
       
    76 TInt DetectEmul_CF_FAT32(RLocalDrive ld, TInt cr, TLocalDriveCapsV2& caps)
       
    77 	{
       
    78 	(void)ld;
       
    79 	(void)cr;
       
    80 	if (caps.iType!=EMediaHardDisk || !(caps.iDriveAtt & KDriveAttRemovable))
       
    81 		return KErrGeneral;
       
    82 	if (cr==KErrNotReady)
       
    83 		return KErrNone;	// removable and not ready - assume fat
       
    84 	if (cr==KErrNone && (PartitionIsFAT32(caps.iPartitionType) || PartitionIsFAT(caps.iPartitionType)) )
       
    85 		return KErrNone;
       
    86 	return KErrGeneral;
       
    87 	}
       
    88 #endif
       
    89 	
       
    90 	
       
    91 _LIT(KLocalDriveMappingFileSysBin,"Z:\\SYS\\DATA\\ESTART.TXT");
       
    92 TPtrC TWinsFSStartup::LocalDriveMappingFileName()
       
    93 	{	
       
    94 	__ASSERT_ALWAYS(InitCompositeFileSystem()==KErrNone,User::Panic(_L("InitCompositeFileSystem failed."),0));
       
    95 	return(KLocalDriveMappingFileSysBin());
       
    96 	}
       
    97 	
       
    98 LOCAL_C void AutoRun()
       
    99 	{
       
   100 	// Ensure that any background initialisation completes before auto-running
       
   101 	RProcess me;
       
   102 	me.SetPriority(EPriorityLow);
       
   103 	const char* autorun = EmulatorAutoRun();
       
   104 	if (autorun)
       
   105 		{
       
   106 		TInt r = KErrNone;
       
   107 		const char* cmdline = EmulatorCommandLine();
       
   108 		TFileName exe;
       
   109 		exe.Copy(TPtrC8((const TText8*)autorun));
       
   110 		TPtrC8 c((const TText8*)cmdline);
       
   111 		HBufC* command = HBufC::New(c.Length());
       
   112 		if (!command)
       
   113 			r = KErrNoMemory;
       
   114 		else
       
   115 			{
       
   116 			TPtr ptr(command->Des());
       
   117 			ptr.Copy(c);
       
   118 			RProcess p;
       
   119 			r = p.Create(exe, *command);
       
   120 			if (r == KErrNone)
       
   121 				{
       
   122 				TRequestStatus s;
       
   123 				p.Logon(s);
       
   124 				me.SetPriority(EPriorityHigh);	// ensure we detect termination early
       
   125 				p.Resume();
       
   126 				p.Close();
       
   127 				User::WaitForRequest(s);
       
   128 				r = s.Int();
       
   129 				}
       
   130 			}
       
   131 		Emulator::Escape();	// make sure we don't get suspended while exiting
       
   132 		ExitProcess(r);
       
   133 		}
       
   134 	}
       
   135 	
       
   136 const TInt WinsDefaultLocalDrives[KMaxLocalDrives]=
       
   137 			{
       
   138 			EDriveY,               //0
       
   139 			EDriveX,               //1 MMC - Slot 0, partition 0
       
   140 			KDriveInvalid,         //2 MMC - Slot 0, partition 1
       
   141 			EDriveT,               //3 MMC - Slot 1, partition 0
       
   142 			KDriveInvalid,         //4 MMC - Slot 1, partition 1
       
   143 			EDriveU,               //5 NAND - USER DATA
       
   144 			EDriveV,               //6 NAND - CODE
       
   145 			KDriveInvalid,         //7
       
   146 			EDriveW,	           //8
       
   147 			KDriveInvalid,         //9
       
   148 			KDriveInvalid,         //10
       
   149 			KDriveInvalid,         //11
       
   150 			KDriveInvalid,         //12
       
   151 			KDriveInvalid,         //13
       
   152 			KDriveInvalid,         //14
       
   153 			KDriveInvalid          //15
       
   154 			};
       
   155 			
       
   156 TInt TWinsFSStartup::DefaultLocalDrive(TInt aLocalDrive)
       
   157 	{
       
   158 	
       
   159 	return(WinsDefaultLocalDrives[aLocalDrive]);
       
   160 	}
       
   161 
       
   162 #if !defined(AUTODETECT_DISABLE)	
       
   163 const TInt KMaxWinsFSInfoTableEntries=7;	
       
   164 LOCAL_D const SFileSystemInfo FileSystems[KMaxWinsFSInfoTableEntries] =
       
   165 	{
       
   166 		{DetectEmulRAM,      _S("efat32"),      _S("fat"),      0,             FS_FORMAT_CORRUPT},
       
   167 		{DetectEmul_CF_FAT32,_S("efat32"),      _S("fat"),      0,             FS_FORMAT_CORRUPT},
       
   168 		{DetectFtl,          _S("efat32"),      _S("fat"),      0,             FS_FORMAT_CORRUPT},
       
   169 		{DetectRofs,         _S("erofs"),       _S("rofs"),     0,             FS_DISMNT_CORRUPT},
       
   170 		{DetectEneaLFFS,     _S("elffs"),       _S("lffs"),     0,             FS_FORMAT_CORRUPT},
       
   171 		{DetectIso9660,      _S("iso9660"),     0,              0,             0},
       
   172 		{DetectNtfs,         _S("ntfs"),        0,              0,             0},
       
   173 	};
       
   174 
       
   175 // additional structure for DetectFtl entry with extension set to "0"
       
   176 LOCAL_D const SFileSystemInfo FileSystemForDetectFtl =
       
   177 		{DetectFtl,         _S("efat32"),         _S("fat"),      0,	 			  FS_FORMAT_CORRUPT};
       
   178 
       
   179 
       
   180 TInt TWinsFSStartup::GetNextStandardFSInfoEntry(const SFileSystemInfo** anEntry,TInt aPos)
       
   181 	{
       
   182     if (aPos<KMaxWinsFSInfoTableEntries)
       
   183 		{
       
   184 		*anEntry=&FileSystems[aPos];
       
   185 		return(KErrNone);
       
   186 		}
       
   187 	else
       
   188 		return(KErrNotFound);	
       
   189 	}
       
   190 #endif		
       
   191 	
       
   192 void TWinsFSStartup::LocalFSInitialisation()
       
   193 //
       
   194 // Add and mount local file system
       
   195 //
       
   196 	{
       
   197 	
       
   198 	DEBUGPRINT("LocalFSInitialisation");
       
   199 
       
   200 	// Add local file system
       
   201 	TInt r=iFs.AddFileSystem(KLocalFsName);
       
   202 	if (r!=KErrNone)
       
   203 		User::Panic(KLitAddFsFail,r);
       
   204 
       
   205 	// Close and reopen to make sure the session path is updated
       
   206 	iFs.Close();
       
   207 	r = iFs.Connect();
       
   208 	if (r!=KErrNone)
       
   209 		User::Panic(KLitConnectFsFail2,r);
       
   210 
       
   211 	TInt i;
       
   212 	_LIT(KLitLocalFsName,"Win32");
       
   213 	TChar drive;
       
   214 	TBuf<4> root=_L("?:\\");
       
   215 	for(i=0;i<KMaxDrives-1;++i)
       
   216 		{
       
   217 		r=RFs::DriveToChar(i,drive);
       
   218 		if(r!=KErrNone)
       
   219 			continue;
       
   220 		root[0]=(TText)drive;
       
   221 		TFileName dummy;
       
   222 		if (MapEmulatedFileName(dummy,root)==KErrNone)
       
   223 			{
       
   224 			r=iFs.MountFileSystem(KLitLocalFsName,i);
       
   225 			DEBUGPRINT2("MountFileSystem() on drive %d r=%d",i,r);
       
   226 			}
       
   227 		}
       
   228 	}
       
   229 
       
   230 _LIT(KWindowServerRootName1, "EWSRV.EXE");
       
   231 _LIT(KWindowServerRootName2, "WSERV.EXE");	
       
   232 _LIT(KSystemStarterName, "z:\\sys\\bin\\SYSSTART.EXE");
       
   233 _LIT(KSystemStateManager, "z:\\sys\\bin\\SYSSTATEMGR.EXE");
       
   234 _LIT(KPropertyError, "RProperty return error");
       
   235 
       
   236 TInt TWinsFSStartup::StartSystem()
       
   237 	{
       
   238 	// Get boot mode with need to access file server
       
   239 	TInt r = GetStartupModeFromFile();
       
   240 	if (r != KErrNotSupported)
       
   241         {
       
   242         if (r != KErrNone)
       
   243 		    User::Panic(KStartupModeFail,r);
       
   244         // Update startup mode property value
       
   245         r = RProperty::Set(KUidSystemCategory, KSystemStartupModeKey, iStartupMode);
       
   246         if (r != KErrNone)
       
   247             User::Panic(KPropertyError, r);
       
   248         }
       
   249 
       
   250 	// Launch system starter and return unless in noGui or textshell mode
       
   251 	TBool noSysStart = EFalse;
       
   252 	UserSvr::HalFunction(EHalGroupEmulator,EEmulatorHalBoolProperty,  (TAny*)"_NOSYSSTART",  &noSysStart);
       
   253 	
       
   254 	if (!EmulatorNoGui() && !EmulatorTextShell() && !EmulatorMiniGui() && !noSysStart)
       
   255 		{
       
   256 		RProcess ws;
       
   257 		r=ws.Create(KSystemStateManager, KNullDesC);
       
   258 		if (r!=KErrNone)
       
   259 			{
       
   260 			r=ws.Create(KSystemStarterName, KNullDesC);
       
   261 			if (r==KErrNone)
       
   262 				{
       
   263 				TRequestStatus status;
       
   264 				ws.Rendezvous(status);
       
   265 				ws.Resume();		
       
   266 				User::WaitForRequest(status);
       
   267 				// Some system starters may not exit on completion.
       
   268 				if (ws.ExitType() != EExitKill && ws.ExitType() != EExitPending)
       
   269 					User::Panic(_L("SysStart died"),status.Int());
       
   270 				ws.Close();
       
   271 				return KErrNone;
       
   272 				}
       
   273 			}
       
   274 		else
       
   275 			{
       
   276 			TRequestStatus status;
       
   277 			ws.Rendezvous(status);
       
   278 			ws.Resume();		
       
   279 			User::WaitForRequest(status);
       
   280 			if (ws.ExitType() != EExitKill && ws.ExitType() != EExitPending)
       
   281 				User::Panic(_L("SysStart died"),status.Int());
       
   282 			ws.Close();
       
   283 			return KErrNone;
       
   284 			}
       
   285 		}
       
   286 
       
   287 	// Start system agent 2, continue if executable not found, otherwise panic
       
   288 	r = StartSysAgt2();
       
   289 	if (r!=KErrNone && r!=KErrNotFound)
       
   290 		User::Panic(_L("Error launching System Agent"),r);
       
   291 
       
   292 	// If noGui specifed, then we're done
       
   293 	if (EmulatorNoGui())
       
   294 		return KErrNone;
       
   295 
       
   296 	// Attempt to start the appropriate window server
       
   297 	TDriveList list;
       
   298 	iFs.DriveList(list);
       
   299 	if (!EmulatorTextShell() && CreateServer(list,KWindowServerRootName2))
       
   300 		return KErrNone;
       
   301 	if (!CreateServer(list,KWindowServerRootName1))
       
   302 		User::Panic(KLitNoWS,KErrNotFound);
       
   303 
       
   304 	return KErrNone;
       
   305 	}
       
   306 
       
   307 TInt TWinsFSStartup::GetStartupMode()
       
   308     {
       
   309 	UserSvr::HalFunction(EHalGroupEmulator, EEmulatorHalIntProperty, 
       
   310 						 (TAny*)"StartupMode", &iStartupMode);
       
   311 	return(KErrNone);
       
   312     }
       
   313 	
       
   314 GLDEF_C TInt E32Main()
       
   315 	{
       
   316 	
       
   317 	TWinsFSStartup fsStart;
       
   318 	fsStart.Init();
       
   319 	
       
   320 	fsStart.LocalFSInitialisation();
       
   321 	fsStart.Run();
       
   322 	
       
   323 	fsStart.StartSystem();
       
   324 	
       
   325 	fsStart.Close();
       
   326 	
       
   327 	AutoRun();
       
   328 	
       
   329 	return(0);
       
   330 	}
       
   331