Update contrib.
1 // Copyright (c) 2007-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // Implementaion of the media abstraction classs.
22 #include "win_media.h"
23 #include "win_media_device.h"
26 //#########################################################################################################################
27 //## CMediaBase abstract base class implementation
28 //#########################################################################################################################
31 CMediaBase::CMediaBase(TInt aEpocDrvNumber)
32 :iEpocDrvNumber((TDriveNumber)aEpocDrvNumber), iReadOnly(0), ipDevice(NULL)
34 ASSERT(iEpocDrvNumber >= EDriveA && iEpocDrvNumber <= EDriveZ);
35 ipSettingsManager = NULL;
38 CMediaBase::~CMediaBase()
42 delete ipSettingsManager;
46 This method is called in order to disconnect from the media (on dismount)
48 void CMediaBase::Disconnect()
51 ipDevice->Disconnect();
54 //-----------------------------------------------------------------------------
56 Get EPOC-style drive capabilities
57 @param aCaps - some fields of this structure will be filled with the information from the WinMediaDevice
58 @return standard EPOC code
60 TInt CMediaBase::GetEpocCaps(TLocalDriveCaps& aCaps)
62 //-- get real drive geometry from the windows device we are using and
63 //-- override the native media driver's data
65 ipDevice->GetDriveGeometry(dg);
66 aCaps.iSize = dg.TotalSizeInBytes();
68 //-- here we can also override other information, like drive and media attributes etc..
71 TUint32 val = ipSettingsManager->TMediaType_Override();
73 aCaps.iType = (TMediaType)val;
76 TUint32 andMask, orMask;
77 ipSettingsManager->MediaAtt_OverrideMasks(andMask, orMask);
78 aCaps.iMediaAtt &= andMask;
79 aCaps.iMediaAtt |= orMask;
82 ipSettingsManager->DriveAtt_OverrideMasks(andMask, orMask);
83 aCaps.iDriveAtt &= andMask;
84 aCaps.iDriveAtt |= orMask;
89 //#########################################################################################################################
90 //## CGenericBlockMedia class implementation
91 //#########################################################################################################################
95 CGenericBlockMedia::CGenericBlockMedia(TInt aEpocDrvNumber)
96 :CMediaBase(aEpocDrvNumber)
100 CGenericBlockMedia::~CGenericBlockMedia()
104 //-----------------------------------------------------------------------------
107 Connect to the media, i.e. do mounting job.
108 Read settings from the ini file, instantiate "windows device" class..
110 TInt CGenericBlockMedia::Connect()
112 __LOG1(_L("#-- CGenericBlockMedia::Connect(), epocDrv:%d"), iEpocDrvNumber);
120 //-- initialise an object to work with settings from ini file
121 if(!ipSettingsManager)
123 ipSettingsManager = TSettingsManager::Create(iEpocDrvNumber);
124 if(!ipSettingsManager)
126 __LOG(_L("#-- Can not create TSettingsManager!"));
132 //-- 1. obtain and process windows device name
133 char szDevName[KMaxFileName];
134 Settings().WinDeviceName(szDevName, sizeof(szDevName));
136 if(strlen(szDevName) < 4)
138 __LOG(_L("#-- CGenericBlockMedia::Connect() Bad device name!"));
143 //-- 1.1 find out if it is the physical volume or image file
144 TBool bImageFile = ETrue;
145 const char szDevPrefix[] = "\\\\.\\";
146 if( memcmp(szDevName, szDevPrefix, strlen(szDevPrefix)) == 0)
147 {//-- this is a physical device
152 //-- 2. instantinate an interface to the Windows media device
156 {//-- device name corresponds to the image file
157 ipDevice = new CWinImgFileDevice;
160 {//-- device name corresponds to the physical volume file
161 ipDevice = new CWinVolumeDevice;
167 //-- 3. other parameters
168 iReadOnly = Settings().IsReadOnly();
170 //-- set and validate parameters
171 TMediaDeviceParams connectParams;
173 const TUint32 bps = Settings().MediaSectorSize();
175 {//-- some value is provided in the ini file.
176 if(!IsPowerOf2(bps) || bps < KDefaultSectorSz || bps > 4096)
178 __LOG1(_L("#-- 'BytesPerSector' value is incorrect! :%d"), bps);
183 connectParams.iDrvGeometry.iBytesPerSector = bps;
185 //-- !!! chech size here!!
186 connectParams.iDrvGeometry.iSizeInSectors = Settings().MediaSizeInSectors();
187 connectParams.ipDevName = szDevName;
188 connectParams.iReadOnly = iReadOnly;
190 //-- connect to the device driver
191 nRes = ipDevice->Connect(connectParams);
198 //-----------------------------------------------------------------------------
200 Disconnect from the media, dismounting.
202 void CGenericBlockMedia::Disconnect()
204 __PRINT1(_L("#-- CGenericBlockMedia::Disconnect(), epocDrv:%d"), iEpocDrvNumber);
206 CMediaBase::Disconnect();
209 //-----------------------------------------------------------------------------
211 Read from the media. See corresponding CWinDrvProxyDrive:: method
213 TInt CGenericBlockMedia::Read(TInt64 aPos, TInt aLength, TDes8& aDataDes)
215 //__PRINT3(_L("#-- CGenericBlockMedia::Read(), epocDrv:%d, pos:%LU, len:%u"), iEpocDrvNumber, aPos, aLength);
217 return ipDevice->Read(aPos, aLength, aDataDes);
220 //-----------------------------------------------------------------------------
223 Write to the media. See corresponding CWinDrvProxyDrive:: method
225 TInt CGenericBlockMedia::Write(TInt64 aPos,TInt aLength, const TDesC8& aDataDes)
227 //__PRINT3(_L("#-- CGenericBlockMedia::Write(), epocDrv:%d, pos:%LU, len:%u"), iEpocDrvNumber, aPos, aLength);
232 return KErrAccessDenied;
234 return ipDevice->Write(aPos, aLength, aDataDes);
238 //-----------------------------------------------------------------------------
242 For "formatting" called CWinMediaDeviceBase::Erase(...) method that fills required media region with zeroes (see its implementation).
243 As soon as the number of erased sectors becomes >= max. number of sectors on this media, KErrEof is reurned indicationg the last
244 formatting step done.
246 TInt CGenericBlockMedia::Format(TFormatInfo& anInfo)
249 return KErrAccessDenied;
253 const TUint32 KMaxFmtSteps = 100; //-- max. number of formatting steps.
256 ipDevice->GetDriveGeometry(dg);
257 const TUint32 KFmtSectors = (dg.iSizeInSectors + (KMaxFmtSteps-1))/ KMaxFmtSteps; //-- how many sectors format during one step
259 if(anInfo.iFormatIsCurrent == 0)
260 {//-- this is a first entry, prepare format data
261 iSectorsFormatted = 0;
262 anInfo.iFormatIsCurrent = 1;
265 //-- erase block of the media
266 TUint32 sectorsToErase = KFmtSectors;
267 if(iSectorsFormatted+KFmtSectors > dg.iSizeInSectors)
269 sectorsToErase -= (iSectorsFormatted+KFmtSectors-dg.iSizeInSectors);
272 nRes = ipDevice->Erase(iSectorsFormatted*dg.iBytesPerSector, sectorsToErase*dg.iBytesPerSector, 0);
280 iSectorsFormatted+=KFmtSectors;
281 anInfo.i512ByteSectorsFormatted = iSectorsFormatted;
283 if(iSectorsFormatted >= dg.iSizeInSectors)
284 return KErrEof; //-- formatted has finished
291 //-----------------------------------------------------------------------------
294 "format" the specified region on the media. See corresponding CWinDrvProxyDrive:: method.
296 TInt CGenericBlockMedia::Format(TInt64 aPos, TInt aLength)
298 TInt nRes = ipDevice->Erase(aPos, aLength, 0);
300 return nRes; //-- shall it be KErrEof ?