emulator/emulatorbsp/win_drive/win_media_device.h
changeset 0 cec860690d41
equal deleted inserted replaced
-1:000000000000 0:cec860690d41
       
     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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Definitions for the classes that represent Windows media objects, e.g. a file, physical drive or partition.
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20 */
       
    21 
       
    22 #ifndef WIN_MEDIA_DEVICE_H
       
    23 #define WIN_MEDIA_DEVICE_H
       
    24 
       
    25 #include "common.h"
       
    26 
       
    27 #define WIN32_LEAN_AND_MEAN
       
    28 #pragma warning( disable : 4201 ) // nonstandard extension used : nameless struct/union
       
    29 #include <windows.h>
       
    30 #pragma warning( disable : 4201 ) // nonstandard extension used : nameless struct/union
       
    31 #include <winioctl.h> 
       
    32 #pragma warning( default : 4201 ) // nonstandard extension used : nameless struct/union
       
    33 
       
    34 
       
    35 //-----------------------------------------------------------------------------
       
    36 
       
    37 const TUint32 KDefaultSectorSz = 512;       //-- default sector size
       
    38 const TUint32 KMinMediaSizeInSectors = 128; //-- minimal number of sectors on the media allowed
       
    39 
       
    40 
       
    41 //-----------------------------------------------------------------------------
       
    42 
       
    43 /** 
       
    44     this structure describes "drive" geometry
       
    45     the "drive" consists of a number of sectors.
       
    46 */
       
    47 class TDriveGeometry
       
    48 {
       
    49  public:
       
    50     TDriveGeometry() {iBytesPerSector=0; iSizeInSectors=0;}
       
    51     
       
    52     TInt64 TotalSizeInBytes() const {return iBytesPerSector*(TInt64)iSizeInSectors;}
       
    53 
       
    54  public:
       
    55 
       
    56     TUint32 iBytesPerSector;  ///< bytes per sectors (usually 512)
       
    57     TUint32 iSizeInSectors;   ///< size of the bedia in sectors
       
    58 };
       
    59 
       
    60 
       
    61 //-----------------------------------------------------------------------------
       
    62 
       
    63 /**
       
    64     Windows media device creation parameters
       
    65 */
       
    66 class TMediaDeviceParams
       
    67 {
       
    68  public:       
       
    69  
       
    70     TMediaDeviceParams() {ipDevName = NULL; iReadOnly = EFalse;}
       
    71 
       
    72  public:
       
    73     TDriveGeometry iDrvGeometry;    ///< drive geometry
       
    74     const char*    ipDevName;       ///< windows device name
       
    75     TBool          iReadOnly;       ///< if ETrue, the device shall be opened as RO
       
    76 
       
    77 };
       
    78 
       
    79 //-----------------------------------------------------------------------------
       
    80 
       
    81 /**
       
    82     Abstract class representing interface to the Windows device (file or volume).
       
    83 */
       
    84 class CWinMediaDeviceBase
       
    85 {
       
    86  public:  
       
    87              CWinMediaDeviceBase();
       
    88     virtual ~CWinMediaDeviceBase();
       
    89 
       
    90     virtual TInt Connect(const TMediaDeviceParams& aParams)=0;
       
    91     virtual void Disconnect(); 
       
    92 
       
    93 
       
    94     virtual TInt Read(TInt64 aPos,TInt aLength, TDes8& aDataDes)=0;
       
    95     virtual TInt Write(TInt64 aPos,TInt aLength, const TDesC8& aDataDes)=0;
       
    96     virtual TInt Erase(TInt64 aPos, TUint32 aLength, TUint8 aFill);
       
    97 
       
    98     void  GetDriveGeometry(TDriveGeometry& aDG) const {aDG = iDrvGeometry;}
       
    99 
       
   100  protected:
       
   101     
       
   102     CWinMediaDeviceBase(const CWinMediaDeviceBase&);
       
   103     CWinMediaDeviceBase& operator=(const CWinMediaDeviceBase&);
       
   104     
       
   105     inline HANDLE Handle() const {return iDevHandle;}
       
   106     inline TBool HandleValid() const;
       
   107     inline TBool HandleValid(HANDLE aHandle) const;
       
   108     
       
   109     TUint32 BytesPerSector() const {return iDrvGeometry.iBytesPerSector;}
       
   110 
       
   111     TInt MapWinError(DWORD aWinError) const; 
       
   112 
       
   113     enum {KScratchBufSz = 128*1024}; ///< scratch buffer size
       
   114 
       
   115  protected:
       
   116     
       
   117     HANDLE              iDevHandle;     ///< Windows device handle
       
   118     TDriveGeometry      iDrvGeometry;   ///< drive geometry.
       
   119     TUint8*             ipScratchBuf;   ///< scratch buffer for IO operations
       
   120 };
       
   121 
       
   122 //-----------------------------------------------------------------------------
       
   123 
       
   124 TBool CWinMediaDeviceBase::HandleValid() const 
       
   125 {
       
   126     return HandleValid(iDevHandle);
       
   127 }
       
   128 
       
   129 TBool CWinMediaDeviceBase::HandleValid(HANDLE aHandle) const 
       
   130 {
       
   131     return aHandle && (aHandle != INVALID_HANDLE_VALUE);
       
   132 }
       
   133 
       
   134 
       
   135 //-----------------------------------------------------------------------------
       
   136 
       
   137 /**
       
   138     This class represents a windows "volume" device, like a physical or logical drive or partition.
       
   139     it can also handle files, but not very effectiively.
       
   140 */
       
   141 class CWinVolumeDevice: public CWinMediaDeviceBase
       
   142 {
       
   143  public:  
       
   144     CWinVolumeDevice();
       
   145    ~CWinVolumeDevice();
       
   146 
       
   147     virtual TInt Connect(const TMediaDeviceParams& aParams);
       
   148     virtual TInt Read(TInt64 aPos,TInt aLength, TDes8& aDataDes);
       
   149     virtual TInt Write(TInt64 aPos,TInt aLength, const TDesC8& aDataDes);
       
   150  private:   
       
   151     
       
   152     MEDIA_TYPE iMediaType;  ///< windows media type.
       
   153 };
       
   154 
       
   155 
       
   156 
       
   157 //-----------------------------------------------------------------------------
       
   158 
       
   159 /**
       
   160     This class represents a windows "file" device, i.e. image file on some drive
       
   161 */
       
   162 class CWinImgFileDevice: public CWinMediaDeviceBase
       
   163 {
       
   164  public:  
       
   165     CWinImgFileDevice();
       
   166    ~CWinImgFileDevice();
       
   167 
       
   168     virtual TInt Connect(const TMediaDeviceParams& aParams);
       
   169     virtual void Disconnect(); 
       
   170 
       
   171     virtual TInt Read(TInt64 aPos,TInt aLength, TDes8& aDataDes);
       
   172     virtual TInt Write(TInt64 aPos,TInt aLength, const TDesC8& aDataDes);
       
   173 
       
   174  private:
       
   175    
       
   176  private:   
       
   177     HANDLE  ihFileMapping; ///< handle for the image file mapping
       
   178     TUint8* ipImageFile;   ///< pointer to the beginning of the image file mapped into memory
       
   179 };
       
   180 
       
   181 
       
   182 
       
   183 
       
   184 
       
   185 
       
   186 
       
   187 
       
   188 
       
   189 
       
   190 #endif //WIN_MEDIA_DEVICE_H
       
   191 
       
   192 
       
   193 
       
   194 
       
   195 
       
   196 
       
   197 
       
   198 
       
   199 
       
   200 
       
   201 
       
   202 
       
   203