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