os/kernelhwsrv/userlibandfileserver/fileserver/shostmassstorage/msproxy/hostusbmsproxy.cpp
Update contrib.
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
20 // This file system extension provides a way to access a drive on the MS system in "raw format".
21 // It can be used to test large files / drives
30 #include "hostusbmsproxy.h"
34 CUsbHostMsProxyDrive::CUsbHostMsProxyDrive(CMountCB* aMount, CExtProxyDriveFactory* aDevice)
35 : CExtProxyDrive(aMount,aDevice)
40 CUsbHostMsProxyDrive::~CUsbHostMsProxyDrive()
43 iUsbHostMsLun.UnInitialise();
46 TInt CUsbHostMsProxyDrive::InitialiseOffset(TCapsInfo& aCapsInfo)
49 const TInt KPartitionInfoSize = TMsDataMemMap::KSectorSize;
50 TBuf8<KPartitionInfoSize> partitionInfo;
53 r = iUsbHostMsLun.Read(0 , KPartitionInfoSize, (TDes8 &) partitionInfo);
56 __PXYPRINT1(_L("!! Reading medium failed with %d !!"), r);
59 TUint8 *iIntBuf = (TUint8 *) partitionInfo.Ptr();
61 // Read of the first sector successful so check for a Master Boot Record
62 if (*(TUint16*)(&iIntBuf[KMBRSignatureOffset])!= KMBRSignature)
64 __PXYPRINT(_L("MBR not present"));
65 iMsDataMemMap.Reset();
69 // Move the partition entries to a 4 byte boundary
70 memcpy(&iIntBuf[0],&iIntBuf[KMBRFirstPartitionOffset],(sizeof(TMBRPartitionEntry)<<2));
71 // Search for a x86 default boot partition - let this be the first
72 TMBRPartitionEntry* pe = (TMBRPartitionEntry*)(&iIntBuf[0]);
74 TInt firstValidPartitionCount = -1;
75 TInt defaultPartitionNumber = -1;
76 TInt partitionCount = 0;
77 for (TInt i = 0; i < KMBRMaxPrimaryPartitions; i++, pe++)
79 if (pe->IsValidDosPartition() || pe->IsValidFAT32Partition())
81 __PXYPRINT(_L("Found a Valid Partition"));
84 if (firstValidPartitionCount < 0)
85 firstValidPartitionCount = i;
87 if (pe->iX86BootIndicator == KBootIndicatorBootable)
89 defaultPartitionNumber = i;
95 __PXYPRINT(_L("!! Invalid Partition !!"));
99 // Check the validity of the partition address boundaries
100 if (partitionCount > 0)
102 __PXYPRINT1(_L("Using Partition %d"), partitionCount);
103 pe = (TMBRPartitionEntry*)(&iIntBuf[0]);
104 TInt partitionIndex = firstValidPartitionCount;
105 if (defaultPartitionNumber > 0)
107 partitionIndex = defaultPartitionNumber;
110 TMBRPartitionEntry& partitionEntry = pe[partitionIndex];
112 iMsDataMemMap.InitDataArea(partitionEntry.iFirstSector,
113 partitionEntry.iNumSectors);
114 __PXYPRINT2(_L("paritioncount = %d defaultpartition = %d"),
115 partitionCount, partitionIndex);
116 __PXYPRINT2(_L("iFirstSector = x%x iNumSectors = x%x"),
117 partitionEntry.iFirstSector,
118 partitionEntry.iNumSectors);
122 __PXYPRINT(_L("No partition found"));
123 iMsDataMemMap.InitDataArea(0, aCapsInfo.iNumberOfBlocks);
124 __PXYPRINT2(_L("iFirstSector = x%x iNumSectors = x%x"),
125 0, aCapsInfo.iNumberOfBlocks);
132 Initialise the proxy drive.
133 @return system wide error code.
135 TInt CUsbHostMsProxyDrive::Initialise()
138 __HOSTPRINT(_L(">>> CUsbHostMsProxyDrive::Initialise()"));
142 // as we can't currently handle remounting devices that have
143 // been removed by unplugging the USB cable, disable critical notifiers
144 // as there's no point in asking the user to re-insert the disk.
145 Mount()->SetNotifyOff();
148 // Check for media presence
150 TInt err = iUsbHostMsLun.Caps(capsInfo);
154 err = InitialiseOffset(capsInfo);
157 __HOSTPRINT1(_L("<<< CUsbHostMsProxyDrive::Initialise() err = %d"), err);
161 TInt CUsbHostMsProxyDrive::SetInfo(const RMessage2 &msg, TAny* aMessageParam2, TAny* aMessageParam3)
164 __HOSTPRINT(_L(">>> CUsbHostMsProxyDrive::SetInfo()"));
165 TMassStorageUnitInfo iUnitInfo;
166 TPckg<TMassStorageUnitInfo> infoPckg(iUnitInfo);
167 TRAPD(err, msg.ReadL(2, infoPckg));
171 __PXYPRINT1(_L("Cant read from the RMessage %d"), err);
172 __HOSTPRINT1(_L("<<< CUsbHostMsProxyDrive::SetInfo() err = %d"), err);
176 err = iUsbHostMsLun.Initialise(msg, 3, iUnitInfo.iLunID);
179 __PXYPRINT1(_L("Initialising logical unit failed %d"), err);
180 __HOSTPRINT1(_L("<<< CUsbHostMsProxyDrive::SetInfo() err = %d"), err);
184 __HOSTPRINT1(_L("<<< CUsbHostMsProxyDrive::SetInfo() err = %d"), err);
188 TInt CUsbHostMsProxyDrive::Dismounted()
194 TInt CUsbHostMsProxyDrive::Enlarge(TInt /*aLength*/)
197 return KErrNotSupported;
201 TInt CUsbHostMsProxyDrive::ReduceSize(TInt /*aPos*/, TInt /*aLength*/)
204 return KErrNotSupported;
207 #define GetIndex(msg, aAddress, aIndex) \
208 aIndex = msg.Ptr0() == aAddress ? 0 : \
209 msg.Ptr1() == aAddress ? 1 : \
210 msg.Ptr1() == aAddress ? 2 : \
211 msg.Ptr1() == aAddress ? 3 : -1;
214 Read from the proxy drive.
216 @param aPos The address from where the read begins.
217 @param aLength The length of the read.
218 @param aTrg A descriptor of the memory buffer from which to read.
219 @param aThreadHandle The handle-number representing the drive thread.
220 @param aOffset Offset into aTrg to read the data from.
222 @return system wide error code.
224 TInt CUsbHostMsProxyDrive::Read(TInt64 aPos, TInt aLength,
225 const TAny* aTrg, TInt aThreadHandle, TInt aOffset)
228 __HOSTPRINT4(_L("\n>>> HOST Read Pos=0x%lx LBA=0x%lx 0x%x 0x%x"),
229 aPos, aPos/KBlockSize, aLength, aOffset);
231 TBool localMessage = (aThreadHandle == KLocalMessageHandle);
234 // Set file position to where we want to read...
238 RMessage2 msg(*(RMessagePtr2 *) &aThreadHandle);
239 localMessage = (msg.Handle() == KLocalMessageHandle);
245 RMessage2 msg(*(RMessagePtr2 *) &aThreadHandle);
246 GetIndex(msg, aTrg, index);
250 __HOSTPRINT1(_L("<<< HOST Read ret=%d"), KErrArgument);
255 /* Calculate the end position */
256 TInt64 end = aPos + static_cast<TInt64>(aLength);
258 /* check whether there is enough source data to write to the destination descriptor */
262 truncate = aLength - (((TPtr8* )aTrg)->MaxLength() - aOffset);
263 __PXYPRINT1(_L("Descriptor length: %08x"), ((TPtr8* )aTrg)->MaxLength());
267 RMessage2 msg(*(RMessagePtr2 *) &aThreadHandle);
268 truncate = aLength - (msg.GetDesMaxLength(index) - aOffset);
269 __PXYPRINT1(_L("Descriptor length: %08x"), msg.GetDesMaxLength(index));
272 __PXYPRINT1(_L("Offset: %08x"), aOffset);
273 __PXYPRINT1(_L("Truncate: 0x%lx"), truncate);
285 TInt len = end - aPos;
287 r = iMsDataMemMap.CheckBlockInRange(mediaPos, len);
290 __HOSTPRINT1(_L("<<< HOST Read ret=%d"), r);
296 TPtr8* pTrgPtr = (TPtr8*)aTrg;
297 TPtr8 trgDes((TUint8*)(pTrgPtr->MidTPtr(aOffset).Ptr()), pTrgPtr->MaxLength() - aOffset);
298 r = iUsbHostMsLun.Read(mediaPos, len, trgDes);
301 pTrgPtr->SetLength(aOffset + trgDes.Length());
305 if (len > iBuf.MaxLength())
306 len = iBuf.MaxLength();
308 r = iUsbHostMsLun.Read(mediaPos, len, iBuf);
311 __HOSTPRINT1(_L("<<< HOST Read ret=%d"), r);
317 RMessage2 msg(*(RMessagePtr2 *) &aThreadHandle);
318 r = msg.Write(index, iBuf, aOffset);
321 __HOSTPRINT1(_L("<<< HOST Read ret=%d"), r);
330 __HOSTPRINT1(_L("<<< HOST Read ret=%d"), KErrNone);
336 Read from the proxy drive, and pass flags to driver.
338 @param aPos The address from where the read begins.
339 @param aLength The length of the read.
340 @param aTrg A descriptor of the memory buffer from which to read.
341 @param aThreadHandle The handle-number representing the drive thread.
342 @param aOffset Offset into aTrg to read the data from.
343 @param aFlags Flags to be passed into the driver.
345 @return system wide error code.
347 TInt CUsbHostMsProxyDrive::Read(TInt64 aPos, TInt aLength,
348 const TAny* aTrg, TInt aThreadHandle, TInt aOffset, TInt /* aFlags */)
351 return Read(aPos, aLength, aTrg, aThreadHandle, aOffset);
355 Read from the proxy drive.
357 @param aPos The address from where the read begins.
358 @param aLength The length of the read.
359 @param aTrg A descriptor of the memory buffer from which to read.
361 @return system wide error code.
363 TInt CUsbHostMsProxyDrive::Read(TInt64 aPos, TInt aLength, TDes8& aTrg)
366 __HOSTPRINT3(_L("\n>>> HOST Read Pos=0x%lx LBA=0x%lx 0x%x"),
367 aPos, aPos/KBlockSize, aLength);
368 return iUsbHostMsLun.Read(iMsDataMemMap.GetDataPos(aPos), aLength, aTrg);
372 Write to the proxy drive.
374 @param aPos The address from where the write begins.
375 @param aLength The length of the write.
376 @param aSrc A descriptor of the memory buffer from which to write.
377 @param aThreadHandle The handle-number representing the drive thread.
378 @param aOffset Offset into aSrc to write the data to.
380 @return system wide error code.
382 TInt CUsbHostMsProxyDrive::Write(TInt64 aPos, TInt aLength,
383 const TAny* aSrc, TInt aThreadHandle, TInt aOffset)
386 // Set file position to where we want to write...
389 __HOSTPRINT4(_L("\n>>> HOST Write Pos=0x%lx LBA=0%lx 0x%x 0x%x"),
390 aPos, aPos/KBlockSize, aLength, aOffset);
392 TBool localMessage = (aThreadHandle == KLocalMessageHandle);
396 RMessage2 msg(*(RMessagePtr2 *) &aThreadHandle);
397 localMessage = (msg.Handle() == KLocalMessageHandle);
403 RMessage2 msg(*(RMessagePtr2 *) &aThreadHandle);
404 GetIndex(msg, aSrc, index);
410 /* Calculate the end position */
411 TInt64 end = aPos + static_cast<TInt64>(aLength);
412 /* check whether there is enough source data to read */
416 truncate = aLength - (((TPtr8* )aSrc)->Length() - aOffset);
417 __PXYPRINT1(_L("Descriptor length: %08x"), ((TPtr8* )aSrc)->Length());
421 RMessage2 msg(*(RMessagePtr2 *) &aThreadHandle);
422 truncate = aLength - (msg.GetDesLength(index) - aOffset);
423 __PXYPRINT1(_L("Descriptor length: %08x"), msg.GetDesLength(index));
426 __PXYPRINT1(_L("Offset: %08x"), aOffset);
427 __PXYPRINT1(_L("Truncate: 0x%lx"), truncate);
429 /* if truncate is > 0 we are short of source data as claimed by the aLength. Hence adjust the 'end' */
441 TInt len = end - aPos;
443 r = iMsDataMemMap.CheckBlockInRange(mediaPos, len);
446 __HOSTPRINT1(_L("<<< HOST Write ret=%d"), r);
452 r = iUsbHostMsLun.Write(mediaPos, len, ((TPtr8*)aSrc)->MidTPtr(aOffset));
456 __HOSTPRINT1(_L("<<< HOST Write ret=%d"), r);
462 if (len > iBuf.Length())
465 RMessage2 msg(*(RMessagePtr2 *) &aThreadHandle);
466 r = msg.Read(index, iBuf, aOffset);
469 __HOSTPRINT1(_L("<<< HOST Write ret=%d"), r);
473 r = iUsbHostMsLun.Write(mediaPos, len, iBuf);
476 __HOSTPRINT1(_L("<<< HOST Write ret=%d"), r);
485 __HOSTPRINT1(_L("<<< HOST Write ret=%d"), KErrNone);
490 Write to the proxy drive and pass flags to driver
492 @param aPos The address from where the write begins.
493 @param aLength The length of the write.
494 @param aSrc A descriptor of the memory buffer from which to write.
495 @param aThreadHandle The handle-number representing the drive thread.
496 @param aOffset Offset into aSrc to write the data to.
497 @param aFlags Flags to be passed into the driver.
499 @return system wide error code.
501 TInt CUsbHostMsProxyDrive::Write(TInt64 aPos, TInt aLength,
502 const TAny* aSrc, TInt aThreadHandle, TInt aOffset, TInt /* aFlags */)
505 return Write(aPos, aLength, aSrc, aThreadHandle, aOffset);
509 Write to the proxy drive.
511 @param aPos The address from where the write begins.
512 @param aSrc A descriptor of the memory buffer from which to write.
514 @return system wide error code.
516 TInt CUsbHostMsProxyDrive::Write(TInt64 aPos,const TDesC8& aSrc)
519 __HOSTPRINT3(_L("\n>>> HOST Write Pos=0x%lx LBA=0x%lx 0x%x"),
520 aPos, aPos/KBlockSize, aSrc.Length());
521 return iUsbHostMsLun.Write(iMsDataMemMap.GetDataPos(aPos), aSrc.Length(), aSrc);
525 Get the proxy drive's capabilities information.
527 @param anInfo A descriptor of the connected drives capabilities.
529 @return system wide error code
531 TInt CUsbHostMsProxyDrive::Caps(TDes8& anInfo)
534 __HOSTPRINT(_L("\n>>> HOST Caps"));
535 TLocalDriveCapsV6Buf caps;
538 TLocalDriveCapsV6& c = caps();
540 c.iType = EMediaHardDisk;
541 c.iConnectionBusType = EConnectionBusUsb;
542 c.iDriveAtt = KDriveAttLocal | KDriveAttRemovable | KDriveAttExternal;
543 c.iMediaAtt = KMediaAttFormattable;
544 c.iFileSystemId = KDriveFileSysFAT;
547 TInt r = iUsbHostMsLun.Caps(capsInfo);
550 c.iBlockSize = capsInfo.iBlockLength;
551 TUint64 size = iMsDataMemMap.DataSize();
554 // No valid partitions so specify the size of the disk
555 size = static_cast<TUint64>(capsInfo.iNumberOfBlocks) * capsInfo.iBlockLength;
559 c.iEraseBlockSize = 0;
561 if (capsInfo.iWriteProtect)
563 c.iMediaAtt |= KMediaAttWriteProtected;
565 __HOSTPRINT4(_L("<<< HOST Caps Block[num=0x%x size=0x%x] Media[size=0x%lx WP=0x%x]"),
566 capsInfo.iNumberOfBlocks, capsInfo.iBlockLength,
567 caps().iSize, caps().iMediaAtt);
571 __HOSTPRINT(_L("<<< HOST Caps Media Not Present"));
572 c.iType = EMediaNotPresent;
573 if(r != KErrNotReady)
576 anInfo = caps.Left(Min(caps.Length(),anInfo.MaxLength()));
583 Format the proxy drive. The drive is assumed to be a single partition. The
584 partition size is equivalent to the size of the media.
586 @param aPos The position of the data which is being formatted.
587 @param aLength [IN] The length of the data which is being formatted. [OUT] The
588 length of data formatted, truncated when end of drive is reached.
590 @return system wide error code.
592 TInt CUsbHostMsProxyDrive::Erase(TInt64 aPos, TInt& aLength)
595 __HOSTPRINT3(_L("\n HOST Erase Pos=0x%lx LBA=0x%lx 0x%x"),
596 aPos, aPos/KBlockSize, aLength);
597 TInt err = iMsDataMemMap.TranslateDataPos(aPos, aLength);
602 err = iUsbHostMsLun.Erase(aPos, aLength);
608 Format the proxy drive.
610 @param aPos The position of the data which is being formatted.
611 @param aLength The length of the data which is being formatted.
613 @return system wide error code.
615 TInt CUsbHostMsProxyDrive::Format(TInt64 aPos, TInt aLength)
618 return Erase(aPos, aLength);
623 Format the connected drive.
625 @param anInfo Device specific format information.
627 @return system wide error code.
629 TInt CUsbHostMsProxyDrive::Format(TFormatInfo& aInfo)
633 const TInt KDefaultMaxBytesPerFormat = 0x100 * TMsDataMemMap::KSectorSize; // 128K
635 if (aInfo.i512ByteSectorsFormatted < 0)
638 if (!aInfo.iFormatIsCurrent)
640 aInfo.iFormatIsCurrent = ETrue;
641 aInfo.i512ByteSectorsFormatted = 0;
642 aInfo.iMaxBytesPerFormat = KDefaultMaxBytesPerFormat;
644 TLocalDriveCapsV6Buf caps;
649 iMsDataMemMap.InitDataArea(caps().iSize);
652 TInt64 pos = static_cast<TInt64>(aInfo.i512ByteSectorsFormatted) << TMsDataMemMap::KFormatSectorShift;
653 TInt length = aInfo.iMaxBytesPerFormat;
654 TInt r = Erase(pos, length);
658 length += TMsDataMemMap::KSectorSize - 1;
659 length >>= TMsDataMemMap::KFormatSectorShift;
660 aInfo.i512ByteSectorsFormatted += length;
667 TInt CUsbHostMsProxyDrive::NotifyChange(TDes8 &aChanged,TRequestStatus* aStatus)
670 iUsbHostMsLun.NotifyChange(aChanged, *aStatus);
672 if(*aStatus != KRequestPending)
678 void CUsbHostMsProxyDrive::NotifyChangeCancel()
681 iUsbHostMsLun.NotifyChangeCancel();
684 TInt CUsbHostMsProxyDrive::SetMountInfo(const TDesC8* /*aMountInfo*/,TInt /*aMountInfoThreadHandle=KCurrentThreadHandle*/)
690 TInt CUsbHostMsProxyDrive::ForceRemount(TUint aFlags)
693 iUsbHostMsLun.ForceRemount(aFlags);
697 TInt CUsbHostMsProxyDrive::Unlock(TMediaPassword& /*aPassword*/, TBool /*aStorePassword*/)
700 return KErrNotSupported;
703 TInt CUsbHostMsProxyDrive::Lock(TMediaPassword& /*aOldPassword*/, TMediaPassword& /*aNewPassword*/, TBool /*aStorePassword*/)
706 return KErrNotSupported;
709 TInt CUsbHostMsProxyDrive::Clear(TMediaPassword& /*aPassword*/)
712 return KErrNotSupported;
715 TInt CUsbHostMsProxyDrive::ErasePassword()
718 return KErrNotSupported;
721 TInt CUsbHostMsProxyDrive::GetInterface(TInt aInterfaceId,TAny*& aInterface,TAny* aInput)
725 case ELocalBufferSupport:
729 TBool isFinalised = (TBool)aInput;
732 iUsbHostMsLun.SuspendLun();
737 return KErrNotSupported;