kerneltest/f32test/ext/t_rawext.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 //
       
    18 // RAWEXT.CPP
       
    19 //
       
    20 // This file system extension provides a way to access a drive on the local windows system in "raw format".
       
    21 // It can be used to test large files / drives
       
    22 // 
       
    23 // NB This should be used WITH CARE to avoid unintentionally overwriting or formatting a windows disk
       
    24 // 
       
    25 
       
    26 #include <f32fsys.h>
       
    27 
       
    28 #include <emulator.h>
       
    29 
       
    30 #include <windows.h>
       
    31 
       
    32 #define WIN32_LEAN_AND_MEAN
       
    33 #pragma warning (disable:4201) // warning C4201: nonstandard extension used : nameless struct/union
       
    34 
       
    35 #include <winioctl.h>
       
    36 
       
    37 // Enable this macro to find the last REMOVABLE drive (e.g. a card reader drive) 
       
    38 // (to minimise the chance of doing any damage !
       
    39 // Doesn't even try any drive letters below E (again for safety):
       
    40 #define __LOOK_FOR_DRIVE__
       
    41 
       
    42 // Otherwise the windows drive letter can be hard-coded here:
       
    43 #ifndef __LOOK_FOR_DRIVE__
       
    44 	char diskName[] = "\\\\.\\Y:";
       
    45 #endif
       
    46 
       
    47 
       
    48 const TInt KSectorSize = 512;
       
    49 
       
    50 class CRawWinDiskExtProxyDrive : public CBaseExtProxyDrive
       
    51 	{
       
    52 public:
       
    53 	static CRawWinDiskExtProxyDrive* NewL(CProxyDrive* aProxyDrive, CMountCB* aMount);
       
    54 	~CRawWinDiskExtProxyDrive();
       
    55 public:
       
    56 	virtual TInt Initialise();
       
    57 	virtual TInt Dismounted();
       
    58 	virtual TInt Enlarge(TInt aLength);
       
    59 	virtual TInt ReduceSize(TInt aPos, TInt aLength);
       
    60 	virtual TInt Read(TInt64 aPos,TInt aLength,const TAny* aTrg,TInt aThreadHandle,TInt anOffset);
       
    61 	virtual TInt Read(TInt64 aPos,TInt aLength,TDes8& aTrg);
       
    62 	virtual TInt Write(TInt64 aPos,TInt aLength,const TAny* aSrc,TInt aThreadHandle,TInt anOffset);
       
    63 	virtual TInt Write(TInt64 aPos,const TDesC8& aSrc);
       
    64 	virtual TInt Caps(TDes8& anInfo);
       
    65 	virtual TInt Format(TFormatInfo& anInfo);
       
    66 private:
       
    67 	CRawWinDiskExtProxyDrive(CProxyDrive* aProxyDrive, CMountCB* aMount);
       
    68 private:
       
    69 	HANDLE iDeviceHandle; 
       
    70 	};
       
    71 
       
    72 class CRawWinDiskProxyDriveFactory : public CProxyDriveFactory
       
    73 	{
       
    74 public:
       
    75 	CRawWinDiskProxyDriveFactory();
       
    76 	virtual TInt Install();			
       
    77 	virtual CProxyDrive* NewProxyDriveL(CProxyDrive* aProxy,CMountCB* aMount);
       
    78 	};
       
    79 
       
    80 
       
    81 
       
    82 CRawWinDiskExtProxyDrive* CRawWinDiskExtProxyDrive::NewL(CProxyDrive* aProxyDrive, CMountCB* aMount)
       
    83 //
       
    84 //
       
    85 //
       
    86 	{
       
    87 	CRawWinDiskExtProxyDrive* temp=new(ELeave) CRawWinDiskExtProxyDrive(aProxyDrive,aMount);
       
    88 	return(temp);
       
    89 	}
       
    90 
       
    91 
       
    92 CRawWinDiskExtProxyDrive::CRawWinDiskExtProxyDrive(CProxyDrive* aProxyDrive, CMountCB* aMount):CBaseExtProxyDrive(aProxyDrive,aMount)
       
    93 	{
       
    94 	RDebug::Print(_L("CRawWinDiskExtProxyDrive::CRawWinDiskExtProxyDrive"));
       
    95 	}
       
    96 
       
    97 CRawWinDiskExtProxyDrive::~CRawWinDiskExtProxyDrive()
       
    98 //
       
    99 //
       
   100 //
       
   101 	{
       
   102 	CloseHandle(iDeviceHandle);
       
   103 	}
       
   104 
       
   105 TInt CRawWinDiskExtProxyDrive::Initialise()
       
   106 //
       
   107 //
       
   108 //
       
   109 	{
       
   110 	RDebug::Print(_L("CRawWinDiskExtProxyDrive::Initialise()"));
       
   111 
       
   112 
       
   113 #ifdef __LOOK_FOR_DRIVE__
       
   114 	// Find the last REMOVABLE drive (to minimise the chance of doing any damage !
       
   115 	// Don't even try any drive letters below E (agai for safety):
       
   116 	char diskName[] = "\\\\.\\?:";
       
   117 	char driveLetter;
       
   118 	for (driveLetter = 'Z'; driveLetter > 'D'; driveLetter--)
       
   119 		{
       
   120 		diskName[4] = driveLetter;
       
   121 
       
   122 		DISK_GEOMETRY geometry;
       
   123 		DWORD dummy;
       
   124 
       
   125 		iDeviceHandle = CreateFileA(diskName, 
       
   126 			GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 
       
   127 			NULL, OPEN_EXISTING, 0, NULL); 
       
   128  
       
   129 		if (iDeviceHandle == INVALID_HANDLE_VALUE) 
       
   130 			{
       
   131 			continue;
       
   132 			}
       
   133 
       
   134 		if(DeviceIoControl(iDeviceHandle, 
       
   135 							IOCTL_DISK_GET_DRIVE_GEOMETRY, 
       
   136 							NULL, 
       
   137 							0, 
       
   138 							&geometry, 
       
   139 							sizeof(geometry), 
       
   140 							&dummy, 
       
   141 							(LPOVERLAPPED)NULL))
       
   142 			{
       
   143 			RDebug::Print(_L("Drive %c MediaType %d removable %s"), 
       
   144 				driveLetter, 
       
   145 				geometry.MediaType,
       
   146 				geometry.MediaType==RemovableMedia ? _S16("True") : _S16("False"));
       
   147 
       
   148 			}
       
   149 
       
   150 		CloseHandle(iDeviceHandle);
       
   151 		if (geometry.MediaType==RemovableMedia)
       
   152 			break;
       
   153 		}
       
   154 	if (driveLetter == 'D')
       
   155 		return KErrNotFound;
       
   156 #endif
       
   157 
       
   158 	// Creating a handle to drive a: using CreateFile () function ..
       
   159 	TPtrC8 diskName8((const TUint8*) diskName);
       
   160 	TBuf16<16> diskName16;
       
   161 	diskName16.Copy(diskName8);
       
   162 	RDebug::Print(_L("RAWEXT: Opening drive %S"), &diskName16);
       
   163 
       
   164 	iDeviceHandle = CreateFileA(
       
   165 //		"\\\\.\\H:",
       
   166 		diskName,
       
   167         GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 
       
   168         NULL, OPEN_EXISTING, 0, NULL); 
       
   169  
       
   170     if (iDeviceHandle == INVALID_HANDLE_VALUE) 
       
   171 		{
       
   172 		return Emulator::LastError();
       
   173 		}
       
   174 
       
   175 	return KErrNone;
       
   176 	}
       
   177 
       
   178 TInt CRawWinDiskExtProxyDrive::Dismounted()
       
   179 //
       
   180 //
       
   181 //
       
   182 	{
       
   183 	RDebug::Print(_L("CRawWinDiskExtProxyDrive::Dismounted()"));
       
   184 	return(KErrNone);
       
   185 	}
       
   186 
       
   187 TInt CRawWinDiskExtProxyDrive::Enlarge(TInt /*aLength*/)
       
   188 //
       
   189 //
       
   190 //
       
   191 	{
       
   192 	return(KErrNotSupported);
       
   193 	}
       
   194 
       
   195 
       
   196 TInt CRawWinDiskExtProxyDrive::ReduceSize(TInt /*aPos*/, TInt /*aLength*/)
       
   197 //
       
   198 //
       
   199 //
       
   200 	{
       
   201 	return(KErrNotSupported);
       
   202 	}
       
   203 
       
   204 LOCAL_C TInt ParameterNum(const TInt aMessageHandle, const TAny* aAddress)
       
   205 	{
       
   206 	RMessage2 tmpMessage(*(RMessagePtr2 *) &aMessageHandle);
       
   207 		
       
   208 	if (tmpMessage.Ptr0() == aAddress)
       
   209 		{
       
   210 		return 0;
       
   211 		}
       
   212 	if (tmpMessage.Ptr1() == aAddress)
       
   213 		{
       
   214 		return 1;
       
   215 		}
       
   216 	if (tmpMessage.Ptr2() == aAddress)
       
   217 		{
       
   218 		return 2;
       
   219 		}
       
   220 	if (tmpMessage.Ptr3() == aAddress)
       
   221 		{
       
   222 		return 3;
       
   223 		}
       
   224 	User::Panic(_L("RAWEXT"),KErrBadHandle);
       
   225 	return -1;
       
   226 	}
       
   227 
       
   228 
       
   229 
       
   230 TInt CRawWinDiskExtProxyDrive::Read(TInt64 aPos,TInt aLength,const TAny* aTrg,TInt aThreadHandle,TInt aOffset)
       
   231 //
       
   232 //
       
   233 //
       
   234 	{
       
   235 
       
   236 	//
       
   237 	// Set file position to where we want to read...
       
   238 	//
       
   239 	LARGE_INTEGER li;
       
   240 	li.QuadPart = aPos;
       
   241 
       
   242 	long posH = I64HIGH(aPos);
       
   243 	long posL = I64LOW(aPos);
       
   244 
       
   245 	TInt off = posL & 0x1FF;
       
   246 	posL &= ~0x1FF;
       
   247 
       
   248 	if(SetFilePointer (iDeviceHandle, posL, &posH, FILE_BEGIN) == 0xFFFFFFFF)
       
   249 		{
       
   250 		return Emulator::LastError();
       
   251 		}
       
   252 
       
   253 	const TInt KLocalBufferSize = 256*1024;
       
   254 	char buffer[KLocalBufferSize];
       
   255 
       
   256 	const TInt param = ParameterNum(aThreadHandle, aTrg);
       
   257 
       
   258 	TInt totalBytesRead = 0;
       
   259 
       
   260 	//
       
   261 	// Read first sector if start of data is offset from 512 bytes...
       
   262 	//
       
   263 	if(off)
       
   264 		{
       
   265 		// read 1st sector if offset
       
   266 		DWORD bytesRead;    
       
   267 		if (!ReadFile (iDeviceHandle, buffer, KSectorSize, &bytesRead, NULL) )
       
   268 			{
       
   269 			return Emulator::LastError();
       
   270 			}
       
   271 
       
   272 		if(bytesRead != KSectorSize)
       
   273 			{
       
   274 			return KErrNotReady;
       
   275 			}
       
   276 
       
   277 		bytesRead-= off;
       
   278 		TInt copyLen = Min(aLength, bytesRead);;
       
   279 
       
   280 		totalBytesRead += copyLen;
       
   281 		aLength -= copyLen;
       
   282 
       
   283 		TPtrC8 des((TUint8*) buffer+off, copyLen);
       
   284 		((RMessagePtr2 *) &aThreadHandle)->Write(param, des, aOffset);
       
   285 		}
       
   286 
       
   287 	//
       
   288 	// Read the remainder of the data, accounting for partial last sector...
       
   289 	//
       
   290 	while(aLength > 0)
       
   291 		{
       
   292 		TUint32 bytesThisTime = min(KLocalBufferSize, (TUint32)aLength);
       
   293 		
       
   294 		bytesThisTime = (bytesThisTime + KSectorSize-1) & ~(KSectorSize-1);
       
   295 
       
   296 		DWORD bytesread;    
       
   297 		if (!ReadFile (iDeviceHandle, buffer, bytesThisTime, &bytesread, NULL) )
       
   298 			{
       
   299 			return Emulator::LastError();
       
   300 			}
       
   301 
       
   302 		if(bytesread != (TUint32)bytesThisTime)
       
   303 			{
       
   304 			return KErrNotReady;
       
   305 			}
       
   306     
       
   307 		TPtrC8 des((TUint8*)buffer, min((TUint32)aLength, bytesThisTime));
       
   308 		((RMessagePtr2 *) &aThreadHandle)->Write(param, des, aOffset + totalBytesRead);
       
   309 		totalBytesRead += bytesThisTime;
       
   310 		aLength -= bytesThisTime;
       
   311 		}
       
   312 
       
   313 	return KErrNone;
       
   314 	}
       
   315 
       
   316 //////
       
   317 
       
   318 GLDEF_C void DumpBuffer( const TDesC8& aBuffer )
       
   319 	/**
       
   320 	 * Dump the content of aBuffer in hex
       
   321 	 */
       
   322 	{
       
   323 	static const TText hextab[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
       
   324 										'A', 'B', 'C', 'D', 'E', 'F' };
       
   325 	const TInt KBytesPerLine = 32;
       
   326 	const TInt KCharsPerLine = KBytesPerLine * 2;
       
   327 
       
   328 	TInt remaining = aBuffer.Length();
       
   329 	TUint8* pSrc = const_cast<TUint8*>(aBuffer.Ptr());
       
   330 
       
   331 	TBuf<KCharsPerLine> line;
       
   332 	line.SetLength( KCharsPerLine );	// don't need to print trailing space
       
   333 	TInt bytesPerLine = KBytesPerLine;
       
   334 	TInt lineOffs = 0;
       
   335 	while( remaining )
       
   336 		{
       
   337 		if( remaining < KBytesPerLine )
       
   338 			{
       
   339 			bytesPerLine = remaining;
       
   340 			line.SetLength( (bytesPerLine*2) );
       
   341 			}
       
   342 		TUint16* pDest = const_cast<TUint16*>(line.Ptr());
       
   343 		remaining -= bytesPerLine;
       
   344 		for( TInt i = bytesPerLine; i > 0; --i )
       
   345 			{
       
   346 			TUint8 c = *pSrc++;
       
   347 			*pDest++ = hextab[c >> 4];
       
   348 			*pDest++ = hextab[c & 0xF];
       
   349 			}
       
   350 		_LIT( KFmt, "%06x: %S\n\r" );
       
   351 		RDebug::Print( KFmt, lineOffs, &line );
       
   352 		lineOffs += bytesPerLine;
       
   353 		}
       
   354 	}
       
   355 
       
   356 
       
   357 
       
   358 TInt CRawWinDiskExtProxyDrive::Read(TInt64 aPos,TInt aLength,TDes8& aTrg)
       
   359 //
       
   360 //
       
   361 //
       
   362 	{
       
   363 
       
   364 	//
       
   365 	// Set file position to where we want to read...
       
   366 	//
       
   367 	LARGE_INTEGER li;
       
   368 	li.QuadPart = aPos;
       
   369 
       
   370 	long posH = I64HIGH(aPos);
       
   371 	long posL = I64LOW(aPos);
       
   372 
       
   373 	TInt off = posL & 0x1FF;
       
   374 	posL &= ~0x1FF;
       
   375 
       
   376 	if(SetFilePointer (iDeviceHandle, posL, &posH, FILE_BEGIN) == 0xFFFFFFFF)
       
   377 		{
       
   378 		return Emulator::LastError();
       
   379 		}
       
   380 
       
   381 	const TInt KLocalBufferSize = 256*1024;
       
   382 	char buffer[KLocalBufferSize];
       
   383 
       
   384 	TInt totalBytesRead = 0;
       
   385 
       
   386 	aTrg.SetLength(aLength);
       
   387 	
       
   388 	//
       
   389 	// Read first sector if start of data is offset from 512 bytes...
       
   390 	//
       
   391 	if(off)
       
   392 		{
       
   393 		// read 1st sector if offset
       
   394 		DWORD bytesRead;    
       
   395 		if (!ReadFile (iDeviceHandle, buffer, KSectorSize, &bytesRead, NULL) )
       
   396 			{
       
   397 			return Emulator::LastError();
       
   398 			}
       
   399 
       
   400 		if(bytesRead != KSectorSize)
       
   401 			{
       
   402 			return KErrNotReady;
       
   403 			}
       
   404 
       
   405 		bytesRead-= off;
       
   406 		TInt copyLen = Min(aLength, bytesRead);;
       
   407 
       
   408 		totalBytesRead += copyLen;
       
   409 		aLength -= copyLen;
       
   410 
       
   411 		memcpy(&aTrg[0], &buffer[off], copyLen);
       
   412 		}
       
   413 
       
   414 	//
       
   415 	// Read the remainder of the data, accounting for partial last sector...
       
   416 	//
       
   417 	while(aLength > 0)
       
   418 		{
       
   419 		TInt bytesThisTime = (aLength > KLocalBufferSize) ? KLocalBufferSize : aLength;
       
   420 
       
   421 		bytesThisTime = (bytesThisTime + KSectorSize-1) & ~(KSectorSize-1);
       
   422 
       
   423 		DWORD bytesread;    
       
   424 		if (!ReadFile (iDeviceHandle, buffer, bytesThisTime, &bytesread, NULL) )
       
   425 			{
       
   426 			return Emulator::LastError();
       
   427 			}
       
   428 
       
   429 		if(bytesread != (TUint32)bytesThisTime)
       
   430 			 return KErrNotReady;
       
   431 		
       
   432 		TInt copyLen = aLength < bytesThisTime ? aLength : bytesThisTime;
       
   433 
       
   434 		memcpy(&aTrg[totalBytesRead], buffer, copyLen);
       
   435 
       
   436 		totalBytesRead += bytesThisTime;
       
   437 		aLength -= bytesThisTime;
       
   438 		}
       
   439 
       
   440 //	DumpBuffer(aTrg);
       
   441 	
       
   442 	return KErrNone;
       
   443 	}
       
   444 
       
   445 TInt CRawWinDiskExtProxyDrive::Write(TInt64 aPos,TInt aLength,const TAny* aSrc,TInt aThreadHandle,TInt aOffset)
       
   446 //
       
   447 //
       
   448 //
       
   449 	{
       
   450 	//
       
   451 	// Set file position to where we want to write...
       
   452 	//
       
   453 	long posStartH = I64HIGH(aPos);
       
   454 	long posStartL = I64LOW(aPos);
       
   455 
       
   456 	TInt offStart = posStartL & 0x1FF;
       
   457 	posStartL &= ~0x1FF;
       
   458 
       
   459 	if(SetFilePointer (iDeviceHandle, posStartL, &posStartH, FILE_BEGIN) == 0xFFFFFFFF)
       
   460 		{
       
   461 		return Emulator::LastError();
       
   462 		}
       
   463 
       
   464 	const TInt KLocalBufferSize = 256*1024;
       
   465 	char buffer[KLocalBufferSize];
       
   466 
       
   467 	const TInt param = ParameterNum(aThreadHandle, aSrc);
       
   468 
       
   469 	TInt totalBytesRead = 0;
       
   470 
       
   471 	//
       
   472 	// Read-Modify-Write on first sector if start of data is offset from 512 bytes
       
   473 	// or if this is a partial write...
       
   474 	//
       
   475 	if(offStart || aLength < KSectorSize)
       
   476 		{
       
   477 		DWORD bytesread;    
       
   478 		if (!ReadFile (iDeviceHandle, buffer, KSectorSize, &bytesread, NULL) )
       
   479 			{
       
   480 			return Emulator::LastError();
       
   481 			}
       
   482 
       
   483 		if(bytesread != KSectorSize)
       
   484 			{
       
   485 			return KErrNotReady;
       
   486 			}
       
   487 
       
   488 		totalBytesRead = min(KSectorSize-offStart, aLength);
       
   489 		aLength -= totalBytesRead;
       
   490 
       
   491 		TPtr8 des((TUint8*)&buffer[offStart], totalBytesRead);
       
   492 		((RMessagePtr2 *) &aThreadHandle)->Read(param, des, aOffset);
       
   493 
       
   494 		if(SetFilePointer (iDeviceHandle, posStartL, &posStartH, FILE_BEGIN) == 0xFFFFFFFF)
       
   495 			{
       
   496 			return Emulator::LastError();
       
   497 			}
       
   498 
       
   499 		if (!WriteFile (iDeviceHandle, buffer, KSectorSize, &bytesread, NULL) )
       
   500 			{
       
   501 			return Emulator::LastError();
       
   502 			}
       
   503 		}
       
   504 
       
   505 	//
       
   506 	// Write the remainder of the aligned data
       
   507 	//
       
   508 	while(aLength >= KSectorSize)
       
   509 		{
       
   510 		TInt alignedLength = aLength & ~0x1FF;
       
   511 
       
   512 		TInt bytesThisTime = (alignedLength > KLocalBufferSize) ? KLocalBufferSize : alignedLength;
       
   513 
       
   514 		TPtr8 des((TUint8*)buffer, bytesThisTime);
       
   515 		((RMessagePtr2 *) &aThreadHandle)->Read(param, des, aOffset + totalBytesRead);
       
   516 	
       
   517 		DWORD bytesWritten;    
       
   518 		if (!WriteFile (iDeviceHandle, buffer, bytesThisTime, &bytesWritten, NULL) )
       
   519 			{
       
   520 			return Emulator::LastError();
       
   521 			}
       
   522 
       
   523 		if(bytesWritten != (TUint32)bytesThisTime)
       
   524 			{
       
   525 			return KErrNotReady;
       
   526 			}
       
   527 		
       
   528 		totalBytesRead += bytesThisTime;
       
   529 		aLength -= bytesThisTime;
       
   530 		}
       
   531 
       
   532 	//
       
   533 	// Read-Modify-Write on the last block if a partial write...
       
   534 	//
       
   535 	if(aLength > 0)
       
   536 		{
       
   537 		DWORD curPos = SetFilePointer(iDeviceHandle, 0, NULL, FILE_CURRENT);
       
   538 
       
   539 		// RMW last sector if offset
       
   540 		DWORD bytesread;    
       
   541 		if (!ReadFile (iDeviceHandle, buffer, KSectorSize, &bytesread, NULL) )
       
   542 			{
       
   543 			return Emulator::LastError();
       
   544 			}
       
   545 
       
   546 		if(bytesread != KSectorSize)
       
   547 			 return KErrNotReady;
       
   548 
       
   549 		TPtr8 des((TUint8*)buffer, aLength);
       
   550 		((RMessagePtr2 *) &aThreadHandle)->Read(param, des, aOffset + totalBytesRead);
       
   551 
       
   552 		if(SetFilePointer (iDeviceHandle, curPos, &posStartH, FILE_BEGIN) == 0xFFFFFFFF)
       
   553 			{
       
   554 			return Emulator::LastError();
       
   555 			}
       
   556 
       
   557 		if (!WriteFile (iDeviceHandle, buffer, KSectorSize, &bytesread, NULL) )
       
   558 			{
       
   559 			return Emulator::LastError();
       
   560 			}
       
   561 		}
       
   562 
       
   563 	return KErrNone;
       
   564 	}
       
   565 
       
   566 TInt CRawWinDiskExtProxyDrive::Write(TInt64 aPos,const TDesC8& aSrc)
       
   567 //
       
   568 //
       
   569 //
       
   570 	{
       
   571 	//
       
   572 	// Set file position to where we want to write...
       
   573 	//
       
   574 	TInt length = aSrc.Length();
       
   575 
       
   576 	long posStartH = I64HIGH(aPos);
       
   577 	long posStartL = I64LOW(aPos);
       
   578 
       
   579 	TInt offStart = posStartL & 0x1FF;
       
   580 	posStartL &= ~0x1FF;
       
   581 
       
   582 	if(SetFilePointer (iDeviceHandle, posStartL, &posStartH, FILE_BEGIN) == 0xFFFFFFFF)
       
   583 		{
       
   584 		return Emulator::LastError();
       
   585 		}
       
   586 
       
   587 	const TInt KLocalBufferSize = 256*1024;
       
   588 	char buffer[KLocalBufferSize];
       
   589 
       
   590 	TInt totalBytesRead = 0;
       
   591 
       
   592 	//
       
   593 	// Read-Modify-Write on first sector if start of data is offset from 512 bytes
       
   594 	// or if this is a partial write...
       
   595 	//
       
   596 	if(offStart || length < KSectorSize)
       
   597 		{
       
   598 		DWORD bytesread;    
       
   599 		if (!ReadFile (iDeviceHandle, buffer, KSectorSize, &bytesread, NULL) )
       
   600 			{
       
   601 			return Emulator::LastError();
       
   602 			}
       
   603 
       
   604 		if(bytesread != KSectorSize)
       
   605 			{
       
   606 			return KErrNotReady;
       
   607 			}
       
   608 
       
   609 		totalBytesRead = min(KSectorSize-offStart, length);
       
   610 		length -= totalBytesRead;
       
   611 
       
   612 		memcpy(&buffer[offStart], &aSrc[0], totalBytesRead);
       
   613 
       
   614 		if(SetFilePointer (iDeviceHandle, posStartL, &posStartH, FILE_BEGIN) == 0xFFFFFFFF)
       
   615 			{
       
   616 			return Emulator::LastError();
       
   617 			}
       
   618 
       
   619 		if (!WriteFile (iDeviceHandle, buffer, KSectorSize, &bytesread, NULL) )
       
   620 			{
       
   621 			return Emulator::LastError();
       
   622 			}
       
   623 		}
       
   624 
       
   625 	//
       
   626 	// Write the remainder of the aligned data
       
   627 	//
       
   628 	while(length >= KSectorSize)
       
   629 		{
       
   630 		TInt alignedLength = length & ~0x1FF;
       
   631 
       
   632 		TInt bytesThisTime = (alignedLength > KLocalBufferSize) ? KLocalBufferSize : alignedLength;
       
   633 
       
   634 		memcpy(buffer, &aSrc[totalBytesRead], bytesThisTime);
       
   635 	
       
   636 		DWORD bytesWritten;    
       
   637 		if (!WriteFile (iDeviceHandle, buffer, bytesThisTime, &bytesWritten, NULL) )
       
   638 			{
       
   639 			return Emulator::LastError();
       
   640 			}
       
   641 
       
   642 		if(bytesWritten != (TUint32)bytesThisTime)
       
   643 			{
       
   644 			return KErrNotReady;
       
   645 			}
       
   646 		
       
   647 		totalBytesRead += bytesThisTime;
       
   648 		length -= bytesThisTime;
       
   649 		}
       
   650 
       
   651 	//
       
   652 	// Read-Modify-Write on the last block if a partial write...
       
   653 	//
       
   654 	if(length > 0)
       
   655 		{
       
   656 		DWORD curPos = SetFilePointer(iDeviceHandle, 0, NULL, FILE_CURRENT);
       
   657 
       
   658 		// RMW last sector if offset
       
   659 		DWORD bytesread;    
       
   660 		if (!ReadFile (iDeviceHandle, buffer, KSectorSize, &bytesread, NULL) )
       
   661 			{
       
   662 			return Emulator::LastError();
       
   663 			}
       
   664 
       
   665 		if(bytesread != KSectorSize)
       
   666 			 return KErrNotReady;
       
   667 
       
   668 		memcpy(&buffer[0], &aSrc[totalBytesRead], length);
       
   669 
       
   670 		if(SetFilePointer (iDeviceHandle, curPos, &posStartH, FILE_BEGIN) == 0xFFFFFFFF)
       
   671 			{
       
   672 			return Emulator::LastError();
       
   673 			}
       
   674 
       
   675 		if (!WriteFile (iDeviceHandle, buffer, KSectorSize, &bytesread, NULL) )
       
   676 			{
       
   677 			return Emulator::LastError();
       
   678 			}
       
   679 		}
       
   680 
       
   681 
       
   682 	return KErrNone;
       
   683 	}
       
   684 
       
   685 TInt CRawWinDiskExtProxyDrive::Caps(TDes8& anInfo)
       
   686 //
       
   687 //
       
   688 //
       
   689 	{
       
   690 	TLocalDriveCapsV3Buf caps;
       
   691 	TInt err = CBaseExtProxyDrive::Caps(caps);
       
   692 	if(err == KErrNone)
       
   693 		{
       
   694 		DISK_GEOMETRY geometry;
       
   695 		DWORD dummy;
       
   696 
       
   697 		if(!DeviceIoControl(iDeviceHandle, 
       
   698 							IOCTL_DISK_GET_DRIVE_GEOMETRY, 
       
   699 							NULL, 
       
   700 							0, 
       
   701 							&geometry, 
       
   702 							sizeof(geometry), 
       
   703 							&dummy, 
       
   704 							(LPOVERLAPPED)NULL))
       
   705 			{
       
   706 			return KErrNotReady;
       
   707 			}
       
   708 
       
   709 		caps().iExtraInfo = EFalse;
       
   710 
       
   711 		// NB This seems to be incorrect:If the disk is formatted by Windows, then the 
       
   712 		// number of sectors reported in the Boot Sector is slightly greater than
       
   713 		// the number of sectors calculated here ... resulting in CheckDisk failures.
       
   714 		// One solution is to ensure the disk is formatted by the emulator.
       
   715 		caps().iSize =	MAKE_TINT64(geometry.Cylinders.HighPart, geometry.Cylinders.LowPart) * 
       
   716 									geometry.TracksPerCylinder * 
       
   717 									geometry.SectorsPerTrack * 
       
   718 									geometry.BytesPerSector;
       
   719 
       
   720 		anInfo = caps.Left(Min(caps.Length(),anInfo.MaxLength()));
       
   721 		}
       
   722 
       
   723 	return(err);
       
   724 	}
       
   725 
       
   726 TInt CRawWinDiskExtProxyDrive::Format(TFormatInfo& /*anInfo*/)
       
   727 //
       
   728 //
       
   729 //
       
   730 	{
       
   731 	return(KErrEof);
       
   732 	}
       
   733 
       
   734 
       
   735 CRawWinDiskProxyDriveFactory::CRawWinDiskProxyDriveFactory()
       
   736 //
       
   737 //
       
   738 //
       
   739 	{
       
   740 	RDebug::Print(_L("CRawWinDiskProxyDriveFactory::CRawWinDiskProxyDriveFactory"));
       
   741 	}
       
   742 
       
   743 TInt CRawWinDiskProxyDriveFactory::Install()
       
   744 //
       
   745 //
       
   746 //
       
   747 	{
       
   748 	_LIT(KLoggerName,"RAWEXT");
       
   749 	return(SetName(&KLoggerName));
       
   750 	}
       
   751 
       
   752 
       
   753 CProxyDrive* CRawWinDiskProxyDriveFactory::NewProxyDriveL(CProxyDrive* aProxy,CMountCB* aMount)
       
   754 //
       
   755 //
       
   756 //
       
   757 	{
       
   758 	return(CRawWinDiskExtProxyDrive::NewL(aProxy,aMount));
       
   759 	}
       
   760 
       
   761 extern "C" {
       
   762 
       
   763 EXPORT_C CProxyDriveFactory* CreateFileSystem()
       
   764 //
       
   765 // Create a new file system
       
   766 //
       
   767 	{
       
   768 	return(new CRawWinDiskProxyDriveFactory());
       
   769 	}
       
   770 }
       
   771