os/kernelhwsrv/userlibandfileserver/fileserver/shostmassstorage/msproxy/hostusbmsproxy.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
/*
sl@0
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description:
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
//
sl@0
    18
// hostusbmsproxy.cpp
sl@0
    19
//
sl@0
    20
// This file system extension provides a way to access a drive on the MS system in "raw format".
sl@0
    21
// It can be used to test large files / drives
sl@0
    22
//
sl@0
    23
sl@0
    24
/** @file
sl@0
    25
@internalTechnology
sl@0
    26
*/
sl@0
    27
sl@0
    28
#include <f32fsys.h>
sl@0
    29
sl@0
    30
#include "hostusbmsproxy.h"
sl@0
    31
#include "debug.h"
sl@0
    32
sl@0
    33
sl@0
    34
CUsbHostMsProxyDrive::CUsbHostMsProxyDrive(CMountCB* aMount, CExtProxyDriveFactory* aDevice)
sl@0
    35
:   CExtProxyDrive(aMount,aDevice)
sl@0
    36
	{
sl@0
    37
	__MSFNSLOG
sl@0
    38
	}
sl@0
    39
sl@0
    40
CUsbHostMsProxyDrive::~CUsbHostMsProxyDrive()
sl@0
    41
	{
sl@0
    42
	__MSFNSLOG
sl@0
    43
	iUsbHostMsLun.UnInitialise();
sl@0
    44
	}
sl@0
    45
sl@0
    46
TInt CUsbHostMsProxyDrive::InitialiseOffset(TCapsInfo& aCapsInfo)
sl@0
    47
	{
sl@0
    48
	__MSFNSLOG
sl@0
    49
    const TInt KPartitionInfoSize = TMsDataMemMap::KSectorSize;
sl@0
    50
	TBuf8<KPartitionInfoSize> partitionInfo;
sl@0
    51
	TInt r;
sl@0
    52
sl@0
    53
	r = iUsbHostMsLun.Read(0 , KPartitionInfoSize, (TDes8 &) partitionInfo);
sl@0
    54
	if (r != KErrNone)
sl@0
    55
        {
sl@0
    56
		__PXYPRINT1(_L("!! Reading medium failed with %d !!"), r);
sl@0
    57
		return r;
sl@0
    58
        }
sl@0
    59
	TUint8 *iIntBuf = (TUint8 *) partitionInfo.Ptr();
sl@0
    60
sl@0
    61
	// Read of the first sector successful so check for a Master Boot Record
sl@0
    62
	if (*(TUint16*)(&iIntBuf[KMBRSignatureOffset])!= KMBRSignature)
sl@0
    63
		{
sl@0
    64
		__PXYPRINT(_L("MBR not present"));
sl@0
    65
        iMsDataMemMap.Reset();
sl@0
    66
        }
sl@0
    67
	else
sl@0
    68
		{
sl@0
    69
		// Move the partition entries to a 4 byte boundary
sl@0
    70
		memcpy(&iIntBuf[0],&iIntBuf[KMBRFirstPartitionOffset],(sizeof(TMBRPartitionEntry)<<2));
sl@0
    71
		// Search for a x86 default boot partition - let this be the first
sl@0
    72
		TMBRPartitionEntry* pe = (TMBRPartitionEntry*)(&iIntBuf[0]);
sl@0
    73
sl@0
    74
		TInt firstValidPartitionCount = -1;
sl@0
    75
		TInt defaultPartitionNumber = -1;
sl@0
    76
		TInt partitionCount = 0;
sl@0
    77
		for (TInt i = 0; i < KMBRMaxPrimaryPartitions; i++, pe++)
sl@0
    78
			{
sl@0
    79
			if (pe->IsValidDosPartition() || pe->IsValidFAT32Partition())
sl@0
    80
				{
sl@0
    81
				__PXYPRINT(_L("Found a Valid Partition"));
sl@0
    82
				partitionCount++;
sl@0
    83
sl@0
    84
				if (firstValidPartitionCount < 0)
sl@0
    85
					firstValidPartitionCount = i;
sl@0
    86
sl@0
    87
				if (pe->iX86BootIndicator == KBootIndicatorBootable)
sl@0
    88
                    {
sl@0
    89
					defaultPartitionNumber = i;
sl@0
    90
					break;
sl@0
    91
                    }
sl@0
    92
				}
sl@0
    93
			else
sl@0
    94
				{
sl@0
    95
				__PXYPRINT(_L("!! Invalid Partition !!"));
sl@0
    96
				}
sl@0
    97
			}
sl@0
    98
sl@0
    99
		// Check the validity of the partition address boundaries
sl@0
   100
	    if (partitionCount > 0)
sl@0
   101
		    {
sl@0
   102
            __PXYPRINT1(_L("Using Partition %d"), partitionCount);
sl@0
   103
			pe = (TMBRPartitionEntry*)(&iIntBuf[0]);
sl@0
   104
            TInt partitionIndex = firstValidPartitionCount;
sl@0
   105
            if (defaultPartitionNumber > 0)
sl@0
   106
                {
sl@0
   107
                partitionIndex = defaultPartitionNumber;
sl@0
   108
                }
sl@0
   109
sl@0
   110
            TMBRPartitionEntry& partitionEntry = pe[partitionIndex];
sl@0
   111
sl@0
   112
			iMsDataMemMap.InitDataArea(partitionEntry.iFirstSector,
sl@0
   113
                                       partitionEntry.iNumSectors);
sl@0
   114
			__PXYPRINT2(_L("paritioncount = %d defaultpartition = %d"),
sl@0
   115
						partitionCount, partitionIndex);
sl@0
   116
			__PXYPRINT2(_L("iFirstSector = x%x iNumSectors = x%x"),
sl@0
   117
						partitionEntry.iFirstSector,
sl@0
   118
						partitionEntry.iNumSectors);
sl@0
   119
			}
sl@0
   120
		else
sl@0
   121
			{
sl@0
   122
            __PXYPRINT(_L("No partition found"));
sl@0
   123
			iMsDataMemMap.InitDataArea(0, aCapsInfo.iNumberOfBlocks);
sl@0
   124
			__PXYPRINT2(_L("iFirstSector = x%x iNumSectors = x%x"),
sl@0
   125
						0, aCapsInfo.iNumberOfBlocks);
sl@0
   126
			}
sl@0
   127
		}
sl@0
   128
	return KErrNone;
sl@0
   129
	}
sl@0
   130
sl@0
   131
/**
sl@0
   132
Initialise the proxy drive.
sl@0
   133
@return system wide error code.
sl@0
   134
*/
sl@0
   135
TInt CUsbHostMsProxyDrive::Initialise()
sl@0
   136
	{
sl@0
   137
	__MSFNSLOG
sl@0
   138
    __HOSTPRINT(_L(">>> CUsbHostMsProxyDrive::Initialise()"));
sl@0
   139
sl@0
   140
	if(Mount())
sl@0
   141
		{
sl@0
   142
		// as we can't currently handle remounting devices that have
sl@0
   143
		// been removed by unplugging the USB cable, disable critical notifiers
sl@0
   144
		// as there's no point in asking the user to re-insert the disk.
sl@0
   145
		Mount()->SetNotifyOff();
sl@0
   146
		}
sl@0
   147
sl@0
   148
    // Check for media presence
sl@0
   149
	TCapsInfo capsInfo;
sl@0
   150
	TInt err = iUsbHostMsLun.Caps(capsInfo);
sl@0
   151
sl@0
   152
    if (err == KErrNone)
sl@0
   153
        {
sl@0
   154
        err = InitialiseOffset(capsInfo);
sl@0
   155
        }
sl@0
   156
sl@0
   157
    __HOSTPRINT1(_L("<<< CUsbHostMsProxyDrive::Initialise() err = %d"), err);
sl@0
   158
    return err;
sl@0
   159
	}
sl@0
   160
sl@0
   161
TInt CUsbHostMsProxyDrive::SetInfo(const RMessage2 &msg, TAny* aMessageParam2, TAny* aMessageParam3)
sl@0
   162
    {
sl@0
   163
	__MSFNSLOG
sl@0
   164
    __HOSTPRINT(_L(">>> CUsbHostMsProxyDrive::SetInfo()"));
sl@0
   165
	TMassStorageUnitInfo iUnitInfo;
sl@0
   166
    TPckg<TMassStorageUnitInfo> infoPckg(iUnitInfo);
sl@0
   167
	TRAPD(err, msg.ReadL(2, infoPckg));
sl@0
   168
sl@0
   169
	if(err != KErrNone)
sl@0
   170
		{
sl@0
   171
		__PXYPRINT1(_L("Cant read from the RMessage %d"), err);
sl@0
   172
        __HOSTPRINT1(_L("<<< CUsbHostMsProxyDrive::SetInfo() err = %d"), err);
sl@0
   173
		return err;
sl@0
   174
		}
sl@0
   175
sl@0
   176
	err = iUsbHostMsLun.Initialise(msg, 3, iUnitInfo.iLunID);
sl@0
   177
	if(err != KErrNone)
sl@0
   178
		{
sl@0
   179
		__PXYPRINT1(_L("Initialising logical unit failed %d"), err);
sl@0
   180
        __HOSTPRINT1(_L("<<< CUsbHostMsProxyDrive::SetInfo() err = %d"), err);
sl@0
   181
		return err;
sl@0
   182
		}
sl@0
   183
sl@0
   184
    __HOSTPRINT1(_L("<<< CUsbHostMsProxyDrive::SetInfo() err = %d"), err);
sl@0
   185
	return err;
sl@0
   186
    }
sl@0
   187
sl@0
   188
TInt CUsbHostMsProxyDrive::Dismounted()
sl@0
   189
	{
sl@0
   190
	__MSFNSLOG
sl@0
   191
	return KErrNone;
sl@0
   192
	}
sl@0
   193
sl@0
   194
TInt CUsbHostMsProxyDrive::Enlarge(TInt /*aLength*/)
sl@0
   195
	{
sl@0
   196
	__MSFNSLOG
sl@0
   197
	return KErrNotSupported;
sl@0
   198
	}
sl@0
   199
sl@0
   200
sl@0
   201
TInt CUsbHostMsProxyDrive::ReduceSize(TInt /*aPos*/, TInt /*aLength*/)
sl@0
   202
	{
sl@0
   203
	__MSFNSLOG
sl@0
   204
	return KErrNotSupported;
sl@0
   205
	}
sl@0
   206
sl@0
   207
#define GetIndex(msg, aAddress, aIndex)			\
sl@0
   208
	aIndex = msg.Ptr0() == aAddress ? 0 :				\
sl@0
   209
				msg.Ptr1() == aAddress ? 1 :			\
sl@0
   210
					msg.Ptr1() == aAddress ? 2 :		\
sl@0
   211
						msg.Ptr1() == aAddress ? 3 : -1;
sl@0
   212
sl@0
   213
/**
sl@0
   214
Read from the proxy drive.
sl@0
   215
sl@0
   216
@param aPos    The address from where the read begins.
sl@0
   217
@param aLength The length of the read.
sl@0
   218
@param aTrg    A descriptor of the memory buffer from which to read.
sl@0
   219
@param aThreadHandle The handle-number representing the drive thread.
sl@0
   220
@param aOffset Offset into aTrg to read the data from.
sl@0
   221
sl@0
   222
@return system wide error code.
sl@0
   223
*/
sl@0
   224
TInt CUsbHostMsProxyDrive::Read(TInt64 aPos, TInt aLength,
sl@0
   225
                                const TAny* aTrg, TInt aThreadHandle, TInt aOffset)
sl@0
   226
	{
sl@0
   227
	__MSFNSLOG
sl@0
   228
    __HOSTPRINT4(_L("\n>>> HOST Read Pos=0x%lx LBA=0x%lx 0x%x 0x%x"),
sl@0
   229
                 aPos, aPos/KBlockSize, aLength, aOffset);
sl@0
   230
sl@0
   231
	TBool localMessage = (aThreadHandle == KLocalMessageHandle);
sl@0
   232
sl@0
   233
	//
sl@0
   234
	// Set file position to where we want to read...
sl@0
   235
	//
sl@0
   236
	if(!localMessage)
sl@0
   237
		{
sl@0
   238
		RMessage2 msg(*(RMessagePtr2 *) &aThreadHandle);
sl@0
   239
		localMessage = (msg.Handle() == KLocalMessageHandle);
sl@0
   240
		}
sl@0
   241
sl@0
   242
	TInt index = 0;
sl@0
   243
	if (!localMessage)
sl@0
   244
		{
sl@0
   245
		RMessage2 msg(*(RMessagePtr2 *) &aThreadHandle);
sl@0
   246
		GetIndex(msg, aTrg, index);
sl@0
   247
sl@0
   248
		if (index < 0)
sl@0
   249
            {
sl@0
   250
            __HOSTPRINT1(_L("<<< HOST Read ret=%d"), KErrArgument);
sl@0
   251
            return KErrArgument;
sl@0
   252
            }
sl@0
   253
		}
sl@0
   254
sl@0
   255
	/* Calculate the end position */
sl@0
   256
	TInt64 end = aPos + static_cast<TInt64>(aLength);
sl@0
   257
sl@0
   258
	/* check whether there is enough source data to write to the destination descriptor */
sl@0
   259
	TInt64 truncate;
sl@0
   260
	if(localMessage)
sl@0
   261
		{
sl@0
   262
		truncate = aLength - (((TPtr8* )aTrg)->MaxLength() - aOffset);
sl@0
   263
	    __PXYPRINT1(_L("Descriptor length: %08x"), ((TPtr8* )aTrg)->MaxLength());
sl@0
   264
		}
sl@0
   265
	else
sl@0
   266
		{
sl@0
   267
		RMessage2 msg(*(RMessagePtr2 *) &aThreadHandle);
sl@0
   268
		truncate = aLength - (msg.GetDesMaxLength(index) - aOffset);
sl@0
   269
	    __PXYPRINT1(_L("Descriptor length: %08x"), msg.GetDesMaxLength(index));
sl@0
   270
		}
sl@0
   271
sl@0
   272
	__PXYPRINT1(_L("Offset: %08x"), aOffset);
sl@0
   273
	__PXYPRINT1(_L("Truncate: 0x%lx"), truncate);
sl@0
   274
sl@0
   275
	if (truncate > 0)
sl@0
   276
		{
sl@0
   277
		end -= truncate;
sl@0
   278
		}
sl@0
   279
sl@0
   280
	iBuf.SetMax();
sl@0
   281
    TInt r;
sl@0
   282
    TInt64 mediaPos;
sl@0
   283
	while (aPos < end)
sl@0
   284
        {
sl@0
   285
		TInt len = end - aPos;
sl@0
   286
        mediaPos = aPos;
sl@0
   287
        r = iMsDataMemMap.CheckBlockInRange(mediaPos, len);
sl@0
   288
        if (r != KErrNone)
sl@0
   289
            {
sl@0
   290
            __HOSTPRINT1(_L("<<< HOST Read ret=%d"), r);
sl@0
   291
            return r;
sl@0
   292
            }
sl@0
   293
sl@0
   294
		if (localMessage)
sl@0
   295
			{
sl@0
   296
			TPtr8* pTrgPtr = (TPtr8*)aTrg;
sl@0
   297
			TPtr8 trgDes((TUint8*)(pTrgPtr->MidTPtr(aOffset).Ptr()), pTrgPtr->MaxLength() - aOffset);
sl@0
   298
			r = iUsbHostMsLun.Read(mediaPos, len, trgDes);
sl@0
   299
			if (r != KErrNone)
sl@0
   300
				return r;
sl@0
   301
			pTrgPtr->SetLength(aOffset + trgDes.Length());
sl@0
   302
			}
sl@0
   303
		else
sl@0
   304
			{
sl@0
   305
			if (len > iBuf.MaxLength())
sl@0
   306
				len = iBuf.MaxLength();
sl@0
   307
sl@0
   308
            r = iUsbHostMsLun.Read(mediaPos, len, iBuf);
sl@0
   309
			if (r != KErrNone)
sl@0
   310
                {
sl@0
   311
                __HOSTPRINT1(_L("<<< HOST Read ret=%d"), r);
sl@0
   312
                return r;
sl@0
   313
                }
sl@0
   314
sl@0
   315
			iBuf.SetLength(len);
sl@0
   316
sl@0
   317
			RMessage2 msg(*(RMessagePtr2 *) &aThreadHandle);
sl@0
   318
			r = msg.Write(index, iBuf, aOffset);
sl@0
   319
			if (r != KErrNone)
sl@0
   320
                {
sl@0
   321
                __HOSTPRINT1(_L("<<< HOST Read ret=%d"), r);
sl@0
   322
                return r;
sl@0
   323
                }
sl@0
   324
			}
sl@0
   325
sl@0
   326
        aPos += len;
sl@0
   327
        aOffset += len;
sl@0
   328
        }
sl@0
   329
sl@0
   330
    __HOSTPRINT1(_L("<<< HOST Read ret=%d"), KErrNone);
sl@0
   331
	return KErrNone;
sl@0
   332
	}
sl@0
   333
sl@0
   334
sl@0
   335
/**
sl@0
   336
Read from the proxy drive, and pass flags to driver.
sl@0
   337
sl@0
   338
@param aPos    The address from where the read begins.
sl@0
   339
@param aLength The length of the read.
sl@0
   340
@param aTrg    A descriptor of the memory buffer from which to read.
sl@0
   341
@param aThreadHandle The handle-number representing the drive thread.
sl@0
   342
@param aOffset Offset into aTrg to read the data from.
sl@0
   343
@param aFlags  Flags to be passed into the driver.
sl@0
   344
sl@0
   345
@return system wide error code.
sl@0
   346
*/
sl@0
   347
TInt CUsbHostMsProxyDrive::Read(TInt64 aPos, TInt aLength,
sl@0
   348
                                const TAny* aTrg, TInt aThreadHandle, TInt aOffset, TInt /* aFlags */)
sl@0
   349
	{
sl@0
   350
	__MSFNSLOG
sl@0
   351
	return Read(aPos, aLength, aTrg, aThreadHandle, aOffset);
sl@0
   352
	}
sl@0
   353
sl@0
   354
/**
sl@0
   355
Read from the proxy drive.
sl@0
   356
sl@0
   357
@param aPos    The address from where the read begins.
sl@0
   358
@param aLength The length of the read.
sl@0
   359
@param aTrg    A descriptor of the memory buffer from which to read.
sl@0
   360
sl@0
   361
@return system wide error code.
sl@0
   362
*/
sl@0
   363
TInt CUsbHostMsProxyDrive::Read(TInt64 aPos, TInt aLength, TDes8& aTrg)
sl@0
   364
	{
sl@0
   365
	__MSFNSLOG
sl@0
   366
    __HOSTPRINT3(_L("\n>>> HOST Read Pos=0x%lx LBA=0x%lx 0x%x"),
sl@0
   367
                 aPos, aPos/KBlockSize, aLength);
sl@0
   368
	return iUsbHostMsLun.Read(iMsDataMemMap.GetDataPos(aPos), aLength, aTrg);
sl@0
   369
	}
sl@0
   370
sl@0
   371
/**
sl@0
   372
Write to the proxy drive.
sl@0
   373
sl@0
   374
@param aPos    The address from where the write begins.
sl@0
   375
@param aLength The length of the write.
sl@0
   376
@param aSrc    A descriptor of the memory buffer from which to write.
sl@0
   377
@param aThreadHandle The handle-number representing the drive thread.
sl@0
   378
@param aOffset Offset into aSrc to write the data to.
sl@0
   379
sl@0
   380
@return system wide error code.
sl@0
   381
*/
sl@0
   382
TInt CUsbHostMsProxyDrive::Write(TInt64 aPos, TInt aLength,
sl@0
   383
                                 const TAny* aSrc, TInt aThreadHandle, TInt aOffset)
sl@0
   384
	{
sl@0
   385
	//
sl@0
   386
	// Set file position to where we want to write...
sl@0
   387
	//
sl@0
   388
	__MSFNSLOG
sl@0
   389
    __HOSTPRINT4(_L("\n>>> HOST Write Pos=0x%lx LBA=0%lx 0x%x 0x%x"),
sl@0
   390
                 aPos, aPos/KBlockSize, aLength, aOffset);
sl@0
   391
sl@0
   392
	TBool localMessage = (aThreadHandle == KLocalMessageHandle);
sl@0
   393
sl@0
   394
	if(!localMessage)
sl@0
   395
		{
sl@0
   396
		RMessage2 msg(*(RMessagePtr2 *) &aThreadHandle);
sl@0
   397
		localMessage = (msg.Handle() == KLocalMessageHandle);
sl@0
   398
		}
sl@0
   399
sl@0
   400
	TInt index = 0;
sl@0
   401
	if(!localMessage)
sl@0
   402
		{
sl@0
   403
		RMessage2 msg(*(RMessagePtr2 *) &aThreadHandle);
sl@0
   404
		GetIndex(msg, aSrc, index);
sl@0
   405
sl@0
   406
		if (index < 0)
sl@0
   407
			return KErrArgument;
sl@0
   408
		}
sl@0
   409
sl@0
   410
	/* Calculate the end position */
sl@0
   411
	TInt64 end =  aPos + static_cast<TInt64>(aLength);
sl@0
   412
	/* check whether there is enough source data to read */
sl@0
   413
	TInt64 truncate;
sl@0
   414
	if (localMessage)
sl@0
   415
		{
sl@0
   416
		truncate = aLength - (((TPtr8* )aSrc)->Length() - aOffset);
sl@0
   417
	    __PXYPRINT1(_L("Descriptor length: %08x"), ((TPtr8* )aSrc)->Length());
sl@0
   418
		}
sl@0
   419
	else
sl@0
   420
		{
sl@0
   421
		RMessage2 msg(*(RMessagePtr2 *) &aThreadHandle);
sl@0
   422
		truncate = aLength - (msg.GetDesLength(index) - aOffset);
sl@0
   423
	    __PXYPRINT1(_L("Descriptor length: %08x"), msg.GetDesLength(index));
sl@0
   424
		}
sl@0
   425
sl@0
   426
	__PXYPRINT1(_L("Offset: %08x"), aOffset);
sl@0
   427
	__PXYPRINT1(_L("Truncate: 0x%lx"), truncate);
sl@0
   428
sl@0
   429
	/* if truncate is > 0  we are short of source data as claimed by the aLength. Hence adjust the 'end' */
sl@0
   430
	if (truncate > 0)
sl@0
   431
		{
sl@0
   432
		end -= truncate;
sl@0
   433
		}
sl@0
   434
sl@0
   435
	iBuf.SetMax();
sl@0
   436
sl@0
   437
    TInt r;
sl@0
   438
    TInt64 mediaPos;
sl@0
   439
	while (aPos < end)
sl@0
   440
        {
sl@0
   441
		TInt len = end - aPos;
sl@0
   442
        mediaPos = aPos;
sl@0
   443
        r = iMsDataMemMap.CheckBlockInRange(mediaPos, len);
sl@0
   444
        if (r != KErrNone)
sl@0
   445
            {
sl@0
   446
            __HOSTPRINT1(_L("<<< HOST Write ret=%d"), r);
sl@0
   447
            return r;
sl@0
   448
            }
sl@0
   449
sl@0
   450
		if (localMessage)
sl@0
   451
			{
sl@0
   452
			r = iUsbHostMsLun.Write(mediaPos, len, ((TPtr8*)aSrc)->MidTPtr(aOffset));
sl@0
   453
sl@0
   454
			if (r != KErrNone)
sl@0
   455
                {
sl@0
   456
                __HOSTPRINT1(_L("<<< HOST Write ret=%d"), r);
sl@0
   457
                return r;
sl@0
   458
                }
sl@0
   459
			}
sl@0
   460
		else
sl@0
   461
			{
sl@0
   462
			if (len > iBuf.Length())
sl@0
   463
				len = iBuf.Length();
sl@0
   464
sl@0
   465
			RMessage2 msg(*(RMessagePtr2 *) &aThreadHandle);
sl@0
   466
			r = msg.Read(index, iBuf, aOffset);
sl@0
   467
			if (r != KErrNone)
sl@0
   468
                {
sl@0
   469
                __HOSTPRINT1(_L("<<< HOST Write ret=%d"), r);
sl@0
   470
                return r;
sl@0
   471
                }
sl@0
   472
sl@0
   473
			r = iUsbHostMsLun.Write(mediaPos, len, iBuf);
sl@0
   474
			if (r != KErrNone)
sl@0
   475
                {
sl@0
   476
                __HOSTPRINT1(_L("<<< HOST Write ret=%d"), r);
sl@0
   477
                return r;
sl@0
   478
                }
sl@0
   479
			}
sl@0
   480
sl@0
   481
        aPos += len;
sl@0
   482
        aOffset += len;
sl@0
   483
        }
sl@0
   484
sl@0
   485
    __HOSTPRINT1(_L("<<< HOST Write ret=%d"), KErrNone);
sl@0
   486
	return KErrNone;
sl@0
   487
	}
sl@0
   488
sl@0
   489
/**
sl@0
   490
Write to the proxy drive and pass flags to driver
sl@0
   491
sl@0
   492
@param aPos    The address from where the write begins.
sl@0
   493
@param aLength The length of the write.
sl@0
   494
@param aSrc    A descriptor of the memory buffer from which to write.
sl@0
   495
@param aThreadHandle The handle-number representing the drive thread.
sl@0
   496
@param aOffset Offset into aSrc to write the data to.
sl@0
   497
@param aFlags  Flags to be passed into the driver.
sl@0
   498
sl@0
   499
@return system wide error code.
sl@0
   500
*/
sl@0
   501
TInt CUsbHostMsProxyDrive::Write(TInt64 aPos, TInt aLength,
sl@0
   502
                                 const TAny* aSrc, TInt aThreadHandle, TInt aOffset, TInt /* aFlags */)
sl@0
   503
	{
sl@0
   504
	__MSFNSLOG
sl@0
   505
	return Write(aPos, aLength, aSrc, aThreadHandle, aOffset);
sl@0
   506
	}
sl@0
   507
sl@0
   508
/**
sl@0
   509
Write to the proxy drive.
sl@0
   510
sl@0
   511
@param aPos    The address from where the write begins.
sl@0
   512
@param aSrc    A descriptor of the memory buffer from which to write.
sl@0
   513
sl@0
   514
@return system wide error code.
sl@0
   515
*/
sl@0
   516
TInt CUsbHostMsProxyDrive::Write(TInt64 aPos,const TDesC8& aSrc)
sl@0
   517
	{
sl@0
   518
	__MSFNSLOG
sl@0
   519
    __HOSTPRINT3(_L("\n>>> HOST Write Pos=0x%lx LBA=0x%lx 0x%x"),
sl@0
   520
                 aPos, aPos/KBlockSize, aSrc.Length());
sl@0
   521
	return iUsbHostMsLun.Write(iMsDataMemMap.GetDataPos(aPos), aSrc.Length(), aSrc);
sl@0
   522
	}
sl@0
   523
sl@0
   524
/**
sl@0
   525
Get the proxy drive's capabilities information.
sl@0
   526
sl@0
   527
@param anInfo A descriptor of the connected drives capabilities.
sl@0
   528
sl@0
   529
@return system wide error code
sl@0
   530
*/
sl@0
   531
TInt CUsbHostMsProxyDrive::Caps(TDes8& anInfo)
sl@0
   532
	{
sl@0
   533
	__MSFNSLOG
sl@0
   534
    __HOSTPRINT(_L("\n>>> HOST Caps"));
sl@0
   535
	TLocalDriveCapsV6Buf caps;
sl@0
   536
    caps.FillZ();
sl@0
   537
sl@0
   538
    TLocalDriveCapsV6& c = caps();
sl@0
   539
sl@0
   540
	c.iType = EMediaHardDisk;
sl@0
   541
    c.iConnectionBusType = EConnectionBusUsb;
sl@0
   542
	c.iDriveAtt = KDriveAttLocal | KDriveAttRemovable | KDriveAttExternal;
sl@0
   543
	c.iMediaAtt = KMediaAttFormattable;
sl@0
   544
	c.iFileSystemId = KDriveFileSysFAT;
sl@0
   545
sl@0
   546
	TCapsInfo capsInfo;
sl@0
   547
	TInt r = iUsbHostMsLun.Caps(capsInfo);
sl@0
   548
	if (KErrNone == r)
sl@0
   549
		{
sl@0
   550
        c.iBlockSize = capsInfo.iBlockLength;
sl@0
   551
        TUint64 size = iMsDataMemMap.DataSize();
sl@0
   552
        if (size == 0)
sl@0
   553
            {
sl@0
   554
            // No valid partitions so specify the size of the disk
sl@0
   555
            size = static_cast<TUint64>(capsInfo.iNumberOfBlocks) * capsInfo.iBlockLength;
sl@0
   556
            }
sl@0
   557
        c.iSize = size;
sl@0
   558
sl@0
   559
        c.iEraseBlockSize = 0;
sl@0
   560
sl@0
   561
        if (capsInfo.iWriteProtect)
sl@0
   562
            {
sl@0
   563
            c.iMediaAtt |= KMediaAttWriteProtected;
sl@0
   564
            }
sl@0
   565
        __HOSTPRINT4(_L("<<< HOST Caps Block[num=0x%x size=0x%x] Media[size=0x%lx WP=0x%x]"),
sl@0
   566
                    capsInfo.iNumberOfBlocks, capsInfo.iBlockLength,
sl@0
   567
		            caps().iSize, caps().iMediaAtt);
sl@0
   568
		}
sl@0
   569
	else
sl@0
   570
        {
sl@0
   571
        __HOSTPRINT(_L("<<< HOST Caps Media Not Present"));
sl@0
   572
		c.iType = EMediaNotPresent;
sl@0
   573
		if(r != KErrNotReady)
sl@0
   574
			r = KErrUnknown;
sl@0
   575
        }
sl@0
   576
	anInfo = caps.Left(Min(caps.Length(),anInfo.MaxLength()));
sl@0
   577
	return r;
sl@0
   578
	}
sl@0
   579
sl@0
   580
sl@0
   581
sl@0
   582
/**
sl@0
   583
Format the proxy drive. The drive is assumed to be a single partition. The
sl@0
   584
partition size is equivalent to the size of the media.
sl@0
   585
sl@0
   586
@param aPos    The position of the data which is being formatted.
sl@0
   587
@param aLength [IN] The length of the data which is being formatted. [OUT] The
sl@0
   588
length of data formatted, truncated when end of drive is reached.
sl@0
   589
sl@0
   590
@return system wide error code.
sl@0
   591
*/
sl@0
   592
TInt CUsbHostMsProxyDrive::Erase(TInt64 aPos, TInt& aLength)
sl@0
   593
	{
sl@0
   594
	__MSFNSLOG
sl@0
   595
    __HOSTPRINT3(_L("\n HOST Erase Pos=0x%lx LBA=0x%lx 0x%x"),
sl@0
   596
                 aPos, aPos/KBlockSize, aLength);
sl@0
   597
    TInt err = iMsDataMemMap.TranslateDataPos(aPos, aLength);
sl@0
   598
sl@0
   599
    if (err)
sl@0
   600
        return err;
sl@0
   601
sl@0
   602
    err = iUsbHostMsLun.Erase(aPos, aLength);
sl@0
   603
    return err;
sl@0
   604
	}
sl@0
   605
sl@0
   606
sl@0
   607
/**
sl@0
   608
Format the proxy drive.
sl@0
   609
sl@0
   610
@param aPos    The position of the data which is being formatted.
sl@0
   611
@param aLength The length of the data which is being formatted.
sl@0
   612
sl@0
   613
@return system wide error code.
sl@0
   614
*/
sl@0
   615
TInt CUsbHostMsProxyDrive::Format(TInt64 aPos, TInt aLength)
sl@0
   616
	{
sl@0
   617
	__MSFNSLOG
sl@0
   618
    return Erase(aPos, aLength);
sl@0
   619
	}
sl@0
   620
sl@0
   621
sl@0
   622
/**
sl@0
   623
Format the connected drive.
sl@0
   624
sl@0
   625
@param anInfo Device specific format information.
sl@0
   626
sl@0
   627
@return system wide error code.
sl@0
   628
*/
sl@0
   629
TInt CUsbHostMsProxyDrive::Format(TFormatInfo& aInfo)
sl@0
   630
	{
sl@0
   631
	__MSFNSLOG
sl@0
   632
sl@0
   633
    const TInt KDefaultMaxBytesPerFormat = 0x100 * TMsDataMemMap::KSectorSize;  // 128K
sl@0
   634
sl@0
   635
    if (aInfo.i512ByteSectorsFormatted < 0)
sl@0
   636
        return KErrArgument;
sl@0
   637
sl@0
   638
    if (!aInfo.iFormatIsCurrent)
sl@0
   639
        {
sl@0
   640
        aInfo.iFormatIsCurrent = ETrue;
sl@0
   641
        aInfo.i512ByteSectorsFormatted = 0;
sl@0
   642
        aInfo.iMaxBytesPerFormat = KDefaultMaxBytesPerFormat;
sl@0
   643
sl@0
   644
		TLocalDriveCapsV6Buf caps;
sl@0
   645
		TInt r = Caps(caps);
sl@0
   646
		if (r != KErrNone)
sl@0
   647
			return r;
sl@0
   648
sl@0
   649
        iMsDataMemMap.InitDataArea(caps().iSize);
sl@0
   650
        }
sl@0
   651
sl@0
   652
    TInt64 pos = static_cast<TInt64>(aInfo.i512ByteSectorsFormatted) << TMsDataMemMap::KFormatSectorShift;
sl@0
   653
    TInt length = aInfo.iMaxBytesPerFormat;
sl@0
   654
    TInt r = Erase(pos, length);
sl@0
   655
sl@0
   656
    if (r == KErrNone)
sl@0
   657
        {
sl@0
   658
        length += TMsDataMemMap::KSectorSize - 1;
sl@0
   659
        length >>= TMsDataMemMap::KFormatSectorShift;
sl@0
   660
        aInfo.i512ByteSectorsFormatted += length;
sl@0
   661
        }
sl@0
   662
sl@0
   663
    return r;
sl@0
   664
    }
sl@0
   665
sl@0
   666
sl@0
   667
TInt CUsbHostMsProxyDrive::NotifyChange(TDes8 &aChanged,TRequestStatus* aStatus)
sl@0
   668
	{
sl@0
   669
	__MSFNSLOG
sl@0
   670
	iUsbHostMsLun.NotifyChange(aChanged, *aStatus);
sl@0
   671
sl@0
   672
	if(*aStatus != KRequestPending)
sl@0
   673
		return KErrUnknown;
sl@0
   674
sl@0
   675
	return KErrNone;
sl@0
   676
	}
sl@0
   677
sl@0
   678
void CUsbHostMsProxyDrive::NotifyChangeCancel()
sl@0
   679
	{
sl@0
   680
	__MSFNSLOG
sl@0
   681
	iUsbHostMsLun.NotifyChangeCancel();
sl@0
   682
	}
sl@0
   683
sl@0
   684
TInt CUsbHostMsProxyDrive::SetMountInfo(const TDesC8* /*aMountInfo*/,TInt /*aMountInfoThreadHandle=KCurrentThreadHandle*/)
sl@0
   685
    {
sl@0
   686
	__MSFNSLOG
sl@0
   687
    return KErrNone;
sl@0
   688
    }
sl@0
   689
sl@0
   690
TInt CUsbHostMsProxyDrive::ForceRemount(TUint aFlags)
sl@0
   691
    {
sl@0
   692
	__MSFNSLOG
sl@0
   693
    iUsbHostMsLun.ForceRemount(aFlags);
sl@0
   694
    return KErrNone;
sl@0
   695
    }
sl@0
   696
sl@0
   697
TInt CUsbHostMsProxyDrive::Unlock(TMediaPassword& /*aPassword*/, TBool /*aStorePassword*/)
sl@0
   698
    {
sl@0
   699
	__MSFNSLOG
sl@0
   700
    return KErrNotSupported;
sl@0
   701
    }
sl@0
   702
sl@0
   703
TInt CUsbHostMsProxyDrive::Lock(TMediaPassword& /*aOldPassword*/, TMediaPassword& /*aNewPassword*/, TBool /*aStorePassword*/)
sl@0
   704
    {
sl@0
   705
	__MSFNSLOG
sl@0
   706
    return KErrNotSupported;
sl@0
   707
    }
sl@0
   708
sl@0
   709
TInt CUsbHostMsProxyDrive::Clear(TMediaPassword& /*aPassword*/)
sl@0
   710
    {
sl@0
   711
	__MSFNSLOG
sl@0
   712
    return KErrNotSupported;
sl@0
   713
    }
sl@0
   714
sl@0
   715
TInt CUsbHostMsProxyDrive::ErasePassword()
sl@0
   716
    {
sl@0
   717
	__MSFNSLOG
sl@0
   718
    return KErrNotSupported;
sl@0
   719
    }
sl@0
   720
sl@0
   721
TInt CUsbHostMsProxyDrive::GetInterface(TInt aInterfaceId,TAny*& aInterface,TAny* aInput)
sl@0
   722
	{
sl@0
   723
	switch(aInterfaceId)
sl@0
   724
		{
sl@0
   725
		case ELocalBufferSupport:
sl@0
   726
			return KErrNone;
sl@0
   727
		case EFinalised:
sl@0
   728
			{
sl@0
   729
			TBool isFinalised = (TBool)aInput;
sl@0
   730
			if(isFinalised)
sl@0
   731
				{
sl@0
   732
				iUsbHostMsLun.SuspendLun();
sl@0
   733
				}
sl@0
   734
			}
sl@0
   735
			return KErrNone;
sl@0
   736
		default:
sl@0
   737
			return KErrNotSupported;
sl@0
   738
		}
sl@0
   739
	}