kerneltest/e32test/mediaext/d_nfe.cpp
author hgs
Mon, 24 May 2010 18:38:55 +0100
changeset 134 95847726fe57
parent 126 2b2a51c87b12
child 244 a77889bee936
permissions -rw-r--r--
201019_07
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
123
fc55edbf3919 201015_11
hgs
parents:
diff changeset
     1
// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
fc55edbf3919 201015_11
hgs
parents:
diff changeset
     2
// All rights reserved.
fc55edbf3919 201015_11
hgs
parents:
diff changeset
     3
// This component and the accompanying materials are made available
fc55edbf3919 201015_11
hgs
parents:
diff changeset
     4
// under the terms of the License "Eclipse Public License v1.0"
fc55edbf3919 201015_11
hgs
parents:
diff changeset
     5
// which accompanies this distribution, and is available
fc55edbf3919 201015_11
hgs
parents:
diff changeset
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
fc55edbf3919 201015_11
hgs
parents:
diff changeset
     7
//
fc55edbf3919 201015_11
hgs
parents:
diff changeset
     8
// Initial Contributors:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
     9
// Nokia Corporation - initial contribution.
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    10
//
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    11
// Contributors:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    12
//
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    13
// Description:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    14
// e32test\mediext\d_nfe.cpp
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    15
// 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    16
//
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    17
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    18
#include <drivers/locmedia.h>
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    19
#include <platform.h>
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    20
#include <variantmediadef.h>
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    21
#include "nfe.h"
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    22
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    23
#if defined(_DEBUG)
134
95847726fe57 201019_07
hgs
parents: 126
diff changeset
    24
//	#define TRACE_ENABLED
123
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    25
#else
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    26
#endif
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    27
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    28
#if defined(TRACE_ENABLED)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    29
#define __KTRACE_PRINT(p) {p;}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    30
#else
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    31
#define __KTRACE_PRINT(p)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    32
#endif
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    33
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    34
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    35
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    36
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    37
// Variant parameters for test Media Extension Driver
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    38
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    39
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    40
const TInt KNfeThreadPriority = 24;	        // same as file server
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    41
const TInt KNfeDiskOpReady = 100;       //100%
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    42
//const TInt KNfeDiskOpStart = 0;         //0%
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    43
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    44
_LIT(KPddName, "Media.NFE");
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    45
#define NFE_DRIVENAME "NFE"
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    46
#define NFE_NUMMEDIA 1
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    47
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    48
// Define the array of local drives which we're attaching to
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    49
__ASSERT_COMPILE(sizeof(TNfeDeviceInfo) <= 256);	// KMaxQueryDeviceLength
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    50
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    51
// Define the array of local code-paging drives which we're attaching to
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    52
#ifdef __DEMAND_PAGING__
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    53
	__ASSERT_COMPILE(NFE_PAGEDRIVECOUNT <= TNfeDeviceInfo::ENfeMaxPartitionEntries);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    54
	__ASSERT_COMPILE(NFE_DRIVECOUNT >= NFE_PAGEDRIVECOUNT);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    55
	#define	SECTOR_SHIFT 9
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    56
#endif	// #ifdef __DEMAND_PAGING__
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    57
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    58
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    59
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    60
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    61
class DPrimaryMediaExt : public DPrimaryMediaBase
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    62
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    63
public:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    64
	DPrimaryMediaExt(TInt aInstance);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    65
public:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    66
	TInt iInstance;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    67
	TDfcQue iNfeDfcQ;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    68
	};
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    69
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    70
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    71
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    72
// Get the number of drives in the drive array belonging to this instance 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    73
TInt DriveCount(TInt aInstance)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    74
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    75
	static const TInt NfeInstanceDriveCounts[NFE_INSTANCE_COUNT]={NFE_INSTANCE_DRIVE_COUNTS};
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    76
	return NfeInstanceDriveCounts[aInstance];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    77
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    78
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    79
// Get a pointer to the first drive in the drive array belonging to this instance 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    80
const TInt* DriveList(TInt aInstance)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    81
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    82
	static const TInt NfeDriveNumbers[NFE_DRIVECOUNT]={NFE_DRIVELIST};
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    83
	TInt driveListOffset = 0;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    84
	for (TInt n=0; n<aInstance; n++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    85
		driveListOffset+= DriveCount(n);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    86
	return  NfeDriveNumbers + driveListOffset;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    87
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    88
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    89
const TInt* DriveLetterList(TInt aInstance)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    90
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    91
	static const TInt NfeDriveLetters[NFE_DRIVECOUNT]={NFE_DRIVELETTERLIST};
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    92
	TInt driveListOffset = 0;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    93
	for (TInt n=0; n<aInstance; n++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    94
		driveListOffset+= DriveCount(n);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    95
	return  NfeDriveLetters + driveListOffset;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    96
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    97
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    98
TInt DriveLetter(TInt aIndex)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
    99
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   100
	static const TInt NfeDriveLetters[NFE_DRIVECOUNT]={NFE_DRIVELETTERLIST};
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   101
	return NfeDriveLetters[aIndex];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   102
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   103
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   104
TChar DriveLetterToAscii(TInt aDriveLetter)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   105
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   106
	return aDriveLetter >= 0 && aDriveLetter <= 25 ? aDriveLetter +'A' : '?';
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   107
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   108
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   109
#ifdef __DEMAND_PAGING__
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   110
	// Get the number of drives in the paged drive array belonging to this instance 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   111
	TInt PageDriveCount(TInt aInstance)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   112
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   113
	#if NFE_PAGEDRIVECOUNT > 0
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   114
		static const TInt NfeInstancePageDriveCounts[NFE_INSTANCE_COUNT]={NFE_INSTANCE_PAGEDRIVE_COUNTS};
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   115
		return NfeInstancePageDriveCounts[aInstance];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   116
	#else
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   117
		return 0;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   118
	#endif
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   119
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   120
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   121
	// Get a pointer to the first drive in the paged drive array belonging to this instance 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   122
	const TInt* PageDriveList(TInt aInstance)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   123
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   124
	#if NFE_PAGEDRIVECOUNT > 0
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   125
		static const TInt NfePageDriveNumbers[NFE_PAGEDRIVECOUNT]={NFE_PAGEDRIVELIST};
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   126
		TInt driveListOffset = 0;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   127
		for (TInt n=0; n<aInstance; n++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   128
			driveListOffset+= PageDriveCount(n);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   129
		return  NfePageDriveNumbers + driveListOffset;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   130
	#else
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   131
		return NULL;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   132
	#endif
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   133
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   134
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   135
	// Get the number of paging type belonging to this instance 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   136
	TInt PagingType(TInt aInstance)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   137
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   138
	#if NFE_PAGEDRIVECOUNT > 0
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   139
		static const TInt NfeInstancePagingType[NFE_INSTANCE_COUNT]={NFE_INSTANCE_PAGING_TYPE};
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   140
		return NfeInstancePagingType[aInstance];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   141
	#else
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   142
		return 0;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   143
	#endif
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   144
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   145
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   146
	// get the instance of the swap drive
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   147
	TInt SwapInstance()
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   148
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   149
		for (TInt i=0; i<NFE_INSTANCE_COUNT; i++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   150
			if (PagingType(i) & DPagingDevice::EData)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   151
				return i;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   152
		return KErrNotFound;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   153
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   154
#endif	// #ifdef __DEMAND_PAGING__
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   155
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   156
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   157
const char* DriveStatus(TNfeDiskStatus aStatus)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   158
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   159
	const char* KNfeUnmounted = "Unmounted";
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   160
	const char* KNfeDecrypted = "Decrypted";
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   161
	const char* KNfeDecrypting = "Decrypting";
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   162
	const char* KNfeEncrypted = "Encrypted";
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   163
	const char* KNfeEncrypting = "Encrypting";
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   164
	const char* KNfeWiping = "Wiping";
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   165
	const char* KNfeCorrupted = "Corrupted";
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   166
	const char* KNfeUnrecognised = "Unrecognised";
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   167
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   168
	switch(aStatus)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   169
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   170
		case ENfeUnmounted:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   171
			return KNfeUnmounted;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   172
		case ENfeDecrypted:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   173
			return KNfeDecrypted;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   174
		case ENfeDecrypting:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   175
			return KNfeDecrypting;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   176
		case ENfeEncrypted:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   177
			return KNfeEncrypted;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   178
		case ENfeEncrypting:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   179
			return KNfeEncrypting;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   180
		case ENfeWiping:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   181
			return KNfeWiping;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   182
		case ENfeCorrupted:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   183
			return KNfeCorrupted;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   184
		default:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   185
			return KNfeUnrecognised;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   186
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   187
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   188
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   189
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   190
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   191
DPrimaryMediaExt::DPrimaryMediaExt(TInt aInstance) : iInstance(aInstance)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   192
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   193
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   194
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   195
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   196
#define NFE_FAULT()	Kern::Fault("NFEMEDIA",__LINE__)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   197
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   198
// disk encryption/decryption/wiping is only performed after the following period of inactivity
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   199
// NB USB Mass Storage tends to 'poll' the media driver by sending ECaps every second or so, so we need
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   200
// to ensure this timeout period is significantly less to ensure the timer DFC thread gets a chance to run...
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   201
const TInt KNotBusyInterval = 200;		// 200 mS
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   202
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   203
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   204
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   205
class DPhysicalDeviceMediaNFE : public DPhysicalDevice
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   206
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   207
public:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   208
	DPhysicalDeviceMediaNFE();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   209
	virtual TInt Install();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   210
	virtual void GetCaps(TDes8& aDes) const;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   211
	virtual TInt Create(DBase*& aChannel, TInt aMediaId, const TDesC8* anInfo, const TVersion& aVer);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   212
	virtual TInt Validate(TInt aDeviceType, const TDesC8* anInfo, const TVersion& aVer);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   213
	virtual TInt Info(TInt aFunction, TAny* a1);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   214
	};
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   215
								
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   216
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   217
class DMediaDriverNFE : public DMediaDriverExtension
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   218
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   219
public:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   220
	class TPropertyObserver
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   221
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   222
	public:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   223
		void Close();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   224
		static void PropertySubsCompleteFn(TAny* aPtr, TInt aReason);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   225
	public:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   226
		TInt iDriveIndex;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   227
		DMediaDriverNFE* iMediaExt;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   228
		RPropertyRef iProperty;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   229
		TPropertySubsRequest* iPropertySubsRequest;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   230
		TDfc* iPropertyDfc;	// N.B. subscription call backs don't occur in our thread context, hence the need for this DFC
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   231
		TInt iValue;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   232
		};
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   233
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   234
public:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   235
	 DMediaDriverNFE(TInt aMediaId);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   236
	~DMediaDriverNFE();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   237
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   238
	// replacing pure virtual
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   239
	virtual TInt Request(TLocDrvRequest& aRequest);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   240
	virtual TInt PartitionInfo(TPartitionInfo &anInfo);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   241
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   242
	TInt DoCreate(TInt aMediaId);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   243
	void Close();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   244
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   245
	TNfeDriveInfo*  GetSwapDrive();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   246
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   247
private:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   248
	TInt HandleRead(TLocDrvRequest& aRequest);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   249
	TInt HandleWrite(TLocDrvRequest& aRequest);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   250
	TInt HandleFormat(TLocDrvRequest& aRequest);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   251
	TInt HandleCaps(TLocDrvRequest& aReq);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   252
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   253
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   254
	void EncryptBuffer(TDes8& aBuffer);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   255
	void DecryptBuffer(TDes8& aBuffer);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   256
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   257
	inline TUint8 EncryptByte(TUint8 aByte) {return (TUint8) (aByte ^ 0xDD);}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   258
	inline TUint8 DecryptByte(TUint8 aByte) {return (TUint8) (aByte ^ 0xDD);}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   259
	inline TInt DriveIndex(TInt aDriveNum) {return iDriveNumToIndex[aDriveNum];}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   260
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   261
	static void IdleTimerCallBack(TAny* aMediaDriver);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   262
	static void TimerDfcFunction(TAny* aMediaDriver);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   263
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   264
	// Publish & Subscribe stuff - used to listen to requests from UI 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   265
	static void FromUiPropertyDfcFunction(TAny* aObserver);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   266
	void FromUiPropertyDfc(TPropertyObserver& aObserver);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   267
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   268
	// Publish & Subscribe stuff - used to listen to status setting from other NFE drives
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   269
	static void StatusToUiPropertyDfcFunction(TAny* aObserver);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   270
	void StatusToUiPropertyDfc(TPropertyObserver& aObserver);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   271
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   272
	void StartEncrypting();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   273
	void StartDecrypting();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   274
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   275
	TInt HandleDiskContent();	// called from idle timer DFC
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   276
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   277
	TNfeDriveInfo* NextDrive();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   278
	
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   279
	TBool AdjustRequest(TNfeDriveInfo*& aDriveInfo, TInt64& aCurrentPos, TInt64& aCurrentLen);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   280
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   281
	void SetStatus(TNfeDriveInfo& aDi, TNfeDiskStatus aStatus);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   282
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   283
	TBool ValidBootSector(TUint8* aBuffer);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   284
	TUint32 VolumeId(TUint8* aBuffer);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   285
	void CheckBootSector(TNfeDriveInfo &aDriveInfo);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   286
	TInt WriteEncryptionStatusToBootSector(TNfeDriveInfo &aDi, TBool aFinalised = EFalse);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   287
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   288
private:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   289
	TInt iInstance;		// media drive instance
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   290
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   291
	// A local buffer use for encryting / decrypting
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   292
	// For paging requests we need this to be page aligned, so allocate enough to cater for 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   293
	// the worst case of up to 4K being wasted at the start of the buffer and the end
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   294
	enum {KSectorSize = 512, KPageSize = 4096, KBufSize = 65536};
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   295
	TUint8 iNonPageAlignedBuffer[KBufSize + KPageSize*2];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   296
	// a pointer to the start of the first page in iNonPageAlignedBuffer
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   297
	TUint8* iBuffer;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   298
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   299
	
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   300
	// Idle timer & DFC for kicking an encryption pass
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   301
	NTimer iIdleTimer;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   302
	TDfc iTimerDfc;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   303
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   304
	TInt iDriveIndex;								// index of local drive number currently being encrypted
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   305
	TInt iDriveNumToIndex[KMaxPartitionEntries];	// maps drive numbers to index
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   306
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   307
	TBool iBusy;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   308
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   309
	const TInt* iDriveList;	// pointer into the drives in NFE_DRIVELIST belonging to this media driver
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   310
	const TInt* iDriveLetterList;	// pointer into the drive letter in NFE_DRIVELETTERLIST belonging to this media driver
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   311
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   312
	// Publish & subscribe stuff which handles drive command notification events from the UI
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   313
	TPropertyObserver iFromUiPropertyObserver[NFE_DRIVECOUNT];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   314
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   315
	// Publish & subscribe stuff which handles drive status notification events from the other NFE drives
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   316
	TPropertyObserver iStatusToUiPropertyObserver[NFE_DRIVECOUNT];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   317
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   318
	TBool iDriveFinalised;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   319
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   320
public:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   321
	// Partition information etc for drives this driver is attached to
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   322
	TNfeDeviceInfo iInfo;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   323
	};
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   324
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   325
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   326
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   327
class TBootSectorStatus
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   328
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   329
public:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   330
	TUint8 iFatBootSectorData[128];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   331
	
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   332
	enum {ENfeBootSectorSignature = 0x2045464E};	// 'NFE '
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   333
	TUint32 iSignature;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   334
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   335
	TNfeDiskStatus iStatus;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   336
	TBool iFinalised;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   337
	TInt64 iEncryptEndPos;	// position of the last encrypted byte +1. Only written when device is powered down
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   338
	};
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   339
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   340
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   341
DPhysicalDeviceMediaNFE::DPhysicalDeviceMediaNFE()
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   342
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   343
	__KTRACE_PRINT(Kern::Printf(": DPhysicalDeviceMediaNFE::DPhysicalDeviceMediaNFE()"));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   344
	iUnitsMask=0x1;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   345
	iVersion=TVersion(KMediaDriverInterfaceMajorVersion,KMediaDriverInterfaceMinorVersion,KMediaDriverInterfaceBuildVersion);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   346
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   347
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   348
/**
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   349
Install the Internal NFE PDD.
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   350
*/
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   351
TInt DPhysicalDeviceMediaNFE::Install()
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   352
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   353
	__KTRACE_PRINT(Kern::Printf(": TInt DPhysicalDeviceMediaNFE::Install()"));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   354
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   355
	return SetName(&KPddName);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   356
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   357
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   358
void DPhysicalDeviceMediaNFE::GetCaps(TDes8& /*aDes*/) const
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   359
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   360
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   361
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   362
/**
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   363
Create an NFE media driver.
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   364
*/
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   365
TInt DPhysicalDeviceMediaNFE::Create(DBase*& aChannel, TInt aMediaId, const TDesC8* /* anInfo */,const TVersion &aVer)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   366
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   367
	__KTRACE_PRINT(Kern::Printf(": DPhysicalDeviceMediaNFE::Create()"));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   368
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   369
	if (!Kern::QueryVersionSupported(iVersion,aVer))
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   370
		return KErrNotSupported;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   371
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   372
	TInt r=KErrNoMemory;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   373
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   374
	DMediaDriverNFE* pD = new DMediaDriverNFE(aMediaId);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   375
	aChannel=pD;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   376
	if (pD)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   377
		r=pD->DoCreate(aMediaId);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   378
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   379
	if (r == KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   380
		pD->OpenMediaDriverComplete(KErrNone);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   381
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   382
	return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   383
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   384
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   385
TInt DPhysicalDeviceMediaNFE::Validate(TInt aDeviceType, const TDesC8* /*anInfo*/, const TVersion& aVer)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   386
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   387
	TInt r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   388
	if (!Kern::QueryVersionSupported(iVersion,aVer))
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   389
		r = KErrNotSupported;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   390
	else if (aDeviceType == MEDIA_DEVICE_NFE)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   391
		return r = KErrNone;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   392
	else
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   393
		r = KErrNotSupported;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   394
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   395
//	__KTRACE_PRINT(Kern::Printf("DPhysicalDeviceMediaNFE::Validate() aDeviceType %d NfeDeviceType %d r %d", aDeviceType, MEDIA_DEVICE_NFE, r));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   396
	return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   397
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   398
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   399
TInt DPhysicalDeviceMediaNFE::Info(TInt aFunction, TAny*)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   400
//
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   401
// Return the priority of this media driver
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   402
//
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   403
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   404
//	__KTRACE_PRINT(Kern::Printf(": DPhysicalDeviceMediaNFE::Info()"));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   405
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   406
	if (aFunction==EPriority)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   407
		return KMediaDriverPriorityNormal;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   408
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   409
	if (aFunction==EMediaDriverPersistent)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   410
		return KErrNone;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   411
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   412
	return KErrNotSupported;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   413
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   414
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   415
DMediaDriverNFE::DMediaDriverNFE(TInt aMediaId) :
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   416
	DMediaDriverExtension(aMediaId),
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   417
	iInstance(((DPrimaryMediaExt*) iPrimaryMedia)->iInstance),
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   418
	iIdleTimer(IdleTimerCallBack,this),
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   419
	iTimerDfc(TimerDfcFunction,this,2),
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   420
	iDriveList (DriveList(iInstance)),
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   421
	iDriveLetterList (DriveLetterList(iInstance))
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   422
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   423
	__KTRACE_PRINT(Kern::Printf("NFE%d: DMediaDriverNFE::DMediaDriverNFE()", iInstance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   424
	iInfo.iDriveCount = DriveCount(iInstance);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   425
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   426
	__ASSERT_ALWAYS(Kern::RoundToPageSize(1) == KPageSize, NFE_FAULT());
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   427
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   428
	// Align the buffer to a page boundary to improve efficiency for paging requests
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   429
	iBuffer = &iNonPageAlignedBuffer[0];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   430
	iBuffer = (TUint8*) ((((TUint32) &iNonPageAlignedBuffer[0]) + KPageSize-1) & ~(KPageSize-1));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   431
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   432
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   433
DMediaDriverNFE::~DMediaDriverNFE()
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   434
//
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   435
// Destructor.
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   436
//
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   437
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   438
	__KTRACE_PRINT(Kern::Printf("NFE%d: DMediaDriverNFE::~DMediaDriverNFE()", iInstance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   439
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   440
	TInt i;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   441
	for (i=0; i<TNfeDeviceInfo::ENfeMaxPartitionEntries; i++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   442
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   443
		RPropertyRef* property = (RPropertyRef*) iInfo.iDrives[i].iStatusToUiProperty;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   444
		if (property)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   445
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   446
			property->Delete();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   447
			delete property;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   448
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   449
		property = (RPropertyRef*) iInfo.iDrives[i].iToUiProperty;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   450
		if (property)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   451
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   452
			property->Delete();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   453
			delete property;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   454
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   455
		property = (RPropertyRef*) iInfo.iDrives[i].iProgressToUiProperty;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   456
		if (property)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   457
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   458
			property->Delete();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   459
			delete property;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   460
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   461
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   462
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   463
	for (i=0; i<NFE_DRIVECOUNT; i++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   464
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   465
		iFromUiPropertyObserver[i].Close();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   466
		iStatusToUiPropertyObserver[i].Close();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   467
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   468
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   469
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   470
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   471
TInt CreateKey(RPropertyRef*& aProperty, TUint aKey)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   472
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   473
	aProperty = new RPropertyRef;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   474
	if (aProperty == NULL)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   475
		return KErrNoMemory;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   476
	TInt r = aProperty->Attach(KNfeUID, aKey);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   477
	if (r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   478
		return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   479
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   480
    static _LIT_SECURITY_POLICY_PASS(KPassPolicy);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   481
	r = aProperty->Define( RProperty::EInt, KPassPolicy, KPassPolicy );
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   482
	if (r != KErrNone && r != KErrAlreadyExists)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   483
		return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   484
	return KErrNone;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   485
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   486
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   487
TInt DMediaDriverNFE::DoCreate(TInt /*aMediaId*/)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   488
//
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   489
// Create the media driver.
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   490
//
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   491
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   492
	__KTRACE_PRINT(Kern::Printf("NFE%d: TInt DMediaDriverNFE::DoCreate()", iInstance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   493
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   494
	// Associate the idle timer DFC with our thread
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   495
	iTimerDfc.SetDfcQ(iPrimaryMedia->iDfcQ);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   496
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   497
	// Publish & Subscribe stuff - used to initiate an encryption pass from the test app
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   498
	static _LIT_SECURITY_POLICY_PASS(KPassPolicy);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   499
	TInt r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   500
	TInt i;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   501
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   502
	TInt swapInstance = KErrNotFound;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   503
#if defined (__DEMAND_PAGING__)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   504
	swapInstance = SwapInstance();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   505
#endif
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   506
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   507
	// **************************************************************************************
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   508
	// Set up P&S publishers so we can publish the status for our drives
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   509
	// **************************************************************************************
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   510
	__KTRACE_PRINT(Kern::Printf("NFE%d: Setting up StatusToUi, ToUi, ProgressToUi P&S publisher & FromUi P&S observer", iInstance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   511
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   512
	for (i = 0; i<DriveCount(iInstance); i++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   513
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   514
		__KTRACE_PRINT(Kern::Printf("NFE%d:drive index %d", iInstance, i));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   515
		TInt driveLetter = iDriveLetterList[i];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   516
		__KTRACE_PRINT(Kern::Printf("NFE%d:drive letter %c", iInstance, (TInt) DriveLetterToAscii(driveLetter)));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   517
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   518
		// no point setting up P&S for the swap drive
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   519
		if (driveLetter == -1)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   520
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   521
			__KTRACE_PRINT(Kern::Printf("NFE%d: i %d, Skipping P&S for swap partition", iInstance, i));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   522
			continue;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   523
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   524
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   525
		r = CreateKey((RPropertyRef*&) iInfo.iDrives[i].iStatusToUiProperty, NFE_KEY(driveLetter, KNfeStatusToUiKey));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   526
		if (r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   527
			return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   528
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   529
		r = CreateKey((RPropertyRef*&) iInfo.iDrives[i].iToUiProperty, NFE_KEY(driveLetter, KNfeToUiKey));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   530
		if (r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   531
			return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   532
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   533
		r = CreateKey((RPropertyRef*&) iInfo.iDrives[i].iProgressToUiProperty, NFE_KEY(driveLetter, KNfeProgressToUiKey));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   534
		if (r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   535
			return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   536
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   537
		TPropertyObserver& observer = iFromUiPropertyObserver[i];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   538
		observer.iDriveIndex = i;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   539
		observer.iMediaExt = this;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   540
		observer.iPropertySubsRequest = new TPropertySubsRequest(TPropertyObserver::PropertySubsCompleteFn, &observer);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   541
		if (observer.iPropertySubsRequest == NULL)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   542
			return KErrNoMemory;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   543
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   544
		observer.iPropertyDfc = new TDfc(FromUiPropertyDfcFunction,&observer,iPrimaryMedia->iDfcQ,2);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   545
		if (observer.iPropertyDfc == NULL)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   546
			return KErrNoMemory;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   547
		
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   548
		r = observer.iProperty.Attach(KNfeUID, NFE_KEY(driveLetter, KNfeToThreadKey));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   549
		if (r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   550
			return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   551
		r = observer.iProperty.Define(
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   552
			RProperty::EInt,
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   553
			KPassPolicy, 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   554
			KPassPolicy);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   555
		if (r != KErrNone && r != KErrAlreadyExists)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   556
			return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   557
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   558
		r = observer.iProperty.Subscribe(*observer.iPropertySubsRequest);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   559
		if (r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   560
			return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   561
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   562
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   563
	// **************************************************************************************
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   564
	// If this instance owns the swap partition,
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   565
	// set up P&S listeners so we can get status notification events from the other drives
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   566
	// **************************************************************************************
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   567
	__KTRACE_PRINT(Kern::Printf("NFE%d: Setting up StatusToUi P&S observer", iInstance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   568
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   569
	for (i = 0; i < (iInstance == swapInstance ? NFE_DRIVECOUNT : -1); i++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   570
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   571
		__KTRACE_PRINT(Kern::Printf("NFE%d:drive index %d", iInstance, i));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   572
		__KTRACE_PRINT(Kern::Printf("NFE%d:drive letter %c", iInstance, (TInt) DriveLetterToAscii(DriveLetter(i))));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   573
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   574
		// no point setting up P&S for the swap drive
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   575
		if (DriveLetter(i) == -1)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   576
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   577
			__KTRACE_PRINT(Kern::Printf("NFE%d: i %d, Skipping StatusToUi P&S observer for swap partition", iInstance, i));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   578
			continue;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   579
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   580
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   581
		__KTRACE_PRINT(Kern::Printf("NFE%d: i %d, Setting up StatusToUi P&S observer for drive %c", iInstance, i, (TInt) DriveLetterToAscii(DriveLetter(i))));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   582
		TPropertyObserver& observer = iStatusToUiPropertyObserver[i];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   583
		observer.iDriveIndex = i;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   584
		observer.iMediaExt = this;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   585
		observer.iPropertySubsRequest = new TPropertySubsRequest(TPropertyObserver::PropertySubsCompleteFn, &observer);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   586
		if (observer.iPropertySubsRequest == NULL)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   587
			return KErrNoMemory;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   588
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   589
		observer.iPropertyDfc = new TDfc(StatusToUiPropertyDfcFunction,&observer,iPrimaryMedia->iDfcQ,2);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   590
		if (observer.iPropertyDfc == NULL)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   591
			return KErrNoMemory;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   592
		
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   593
		r = observer.iProperty.Attach(KNfeUID, NFE_KEY(DriveLetter(i), KNfeStatusToUiKey));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   594
		if (r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   595
			return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   596
		r = observer.iProperty.Define(
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   597
			RProperty::EInt,
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   598
			KPassPolicy, 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   599
			KPassPolicy);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   600
		if (r != KErrNone && r != KErrAlreadyExists)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   601
			return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   602
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   603
		r = observer.iProperty.Subscribe(*observer.iPropertySubsRequest);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   604
		if (r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   605
			return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   606
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   607
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   608
	return(KErrNone);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   609
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   610
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   611
void DMediaDriverNFE::TPropertyObserver::Close()
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   612
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   613
	iProperty.Close();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   614
	delete iPropertyDfc;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   615
	iPropertyDfc = NULL;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   616
	delete iPropertySubsRequest;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   617
	iPropertySubsRequest = NULL;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   618
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   619
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   620
void DMediaDriverNFE::TPropertyObserver::PropertySubsCompleteFn(TAny* aPtr, TInt /*aReason*/)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   621
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   622
	TPropertyObserver* self = (TPropertyObserver*) aPtr;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   623
	// Queue a DFC to ensure we're running in the correct thread
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   624
	self->iPropertyDfc->Enque();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   625
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   626
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   627
void DMediaDriverNFE::FromUiPropertyDfcFunction(TAny* aObserver)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   628
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   629
	TPropertyObserver& observer = *(TPropertyObserver*) aObserver;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   630
	observer.iMediaExt->FromUiPropertyDfc(observer);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   631
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   632
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   633
void DMediaDriverNFE::FromUiPropertyDfc(TPropertyObserver& aObserver)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   634
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   635
    // Get the value of request from the UI
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   636
    TInt err = aObserver.iProperty.Get(aObserver.iValue);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   637
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   638
	TInt r = aObserver.iProperty.Subscribe(*aObserver.iPropertySubsRequest);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   639
	__ASSERT_ALWAYS(r == KErrNone, NFE_FAULT());
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   640
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   641
	TInt driveLetter = iDriveLetterList[aObserver.iDriveIndex];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   642
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   643
	__KTRACE_PRINT(Kern::Printf("NFE%d: DMediaDriverNFE::FromUiPropertyDfc() cmd %d driveLetter %c", 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   644
		iInstance, aObserver.iValue, (TInt) DriveLetterToAscii(driveLetter)));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   645
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   646
	// is this our drive letter ?
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   647
	TInt driveCount = DriveCount(iInstance);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   648
	TNfeDriveInfo* driveInfo = NULL;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   649
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   650
	for (TInt i=0; i<driveCount; i++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   651
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   652
		TInt myDriveLetter = iDriveLetterList[i];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   653
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   654
		__KTRACE_PRINT(Kern::Printf("NFE%d: Comparing drive %c with myDrive %c", iInstance, (TInt) DriveLetterToAscii(driveLetter), (TInt) DriveLetterToAscii(myDriveLetter)));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   655
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   656
		if (myDriveLetter == driveLetter)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   657
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   658
			TInt driveNumber = iDriveList[i];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   659
			driveInfo = &iInfo.iDrives[iDriveNumToIndex[driveNumber]];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   660
			__KTRACE_PRINT(Kern::Printf("NFE%d: Drive Match found driveNumber %d", iInstance, driveInfo->iLocalDriveNum));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   661
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   662
			__ASSERT_ALWAYS(driveInfo->iProgressToUiProperty, NFE_FAULT());
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   663
			((RPropertyRef*) (driveInfo->iProgressToUiProperty))->Set(0); 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   664
			// Wake up the possibly waiting client, whether or not the request
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   665
			// was successfull.
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   666
			((RPropertyRef*) (driveInfo->iToUiProperty))->Set( err ); // Return value ignored
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   667
			break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   668
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   669
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   670
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   671
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   672
	__KTRACE_PRINT(Kern::Printf("NFE%d: err %d aObserver.iValue %d swap %x swap state %d", iInstance, err, aObserver.iValue, GetSwapDrive(), GetSwapDrive() ? GetSwapDrive()->Status() : -1));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   673
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   674
	if (err == KErrNone && aObserver.iValue == ENfeEncryptDisk && driveInfo != NULL)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   675
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   676
		if (driveInfo->Status() == ENfeDecrypted)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   677
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   678
			SetStatus(*driveInfo, ENfeEncrypting);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   679
			StartEncrypting();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   680
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   681
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   682
	if (err == KErrNone && aObserver.iValue == ENfeDecryptDisk && driveInfo != NULL)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   683
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   684
		if (driveInfo->Status() == ENfeEncrypted)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   685
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   686
			SetStatus(*driveInfo, ENfeDecrypting);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   687
			StartDecrypting();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   688
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   689
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   690
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   691
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   692
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   693
void DMediaDriverNFE::StatusToUiPropertyDfcFunction(TAny* aObserver)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   694
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   695
	TPropertyObserver& observer = *(TPropertyObserver*) aObserver;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   696
	observer.iMediaExt->StatusToUiPropertyDfc(observer);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   697
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   698
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   699
void DMediaDriverNFE::StatusToUiPropertyDfc(TPropertyObserver& aObserver)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   700
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   701
    // Get the value of request from the UI
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   702
    TInt err = aObserver.iProperty.Get(aObserver.iValue);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   703
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   704
	TInt r = aObserver.iProperty.Subscribe(*aObserver.iPropertySubsRequest);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   705
	__ASSERT_ALWAYS(r == KErrNone, NFE_FAULT());
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   706
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   707
	__KTRACE_PRINT(Kern::Printf("NFE%d: DMediaDriverNFE::StatusToUiPropertyDfc() status %d driveLetter %c", 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   708
		iInstance, aObserver.iValue, DriveLetter(aObserver.iDriveIndex) >=0 ? DriveLetter(aObserver.iDriveIndex)+'A' : '?'));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   709
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   710
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   711
	__KTRACE_PRINT(Kern::Printf("NFE%d: err %d aObserver.iValue %d swap %x swap state %d", iInstance, err, aObserver.iValue, GetSwapDrive(), GetSwapDrive() ? GetSwapDrive()->Status() : -1));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   712
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   713
	if (err == KErrNone && (aObserver.iValue == ENfeEncrypted || aObserver.iValue == ENfeEncrypting))
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   714
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   715
		// If any drive is being or is already encrypted then we have to encrypt the swap partition...
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   716
		TNfeDriveInfo* diSwap = GetSwapDrive();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   717
		if (diSwap != NULL && diSwap->Status() == ENfeDecrypted)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   718
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   719
			SetStatus(*diSwap, ENfeEncrypting);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   720
			StartEncrypting();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   721
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   722
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   723
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   724
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   725
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   726
void DMediaDriverNFE::Close()
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   727
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   728
	__KTRACE_PRINT(Kern::Printf("NFE%d: DMediaDriverNFE::Close()", iInstance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   729
	DMediaDriverExtension::Close();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   730
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   731
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   732
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   733
void DMediaDriverNFE::SetStatus(TNfeDriveInfo& aDi, TNfeDiskStatus aStatus)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   734
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   735
	if (aStatus != aDi.Status())
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   736
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   737
		aDi.SetStatus(aStatus);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   738
		__KTRACE_PRINT(Kern::Printf("NFE%d: SetStatus = %s", iInstance, DriveStatus(aDi.Status())));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   739
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   740
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   741
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   742
void TNfeDriveInfo::SetStatus(TNfeDiskStatus aStatus)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   743
    {
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   744
	iStatus = aStatus;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   745
	if (IsUDADrive())
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   746
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   747
		// Update the status pub&sub variable for UI
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   748
		__ASSERT_ALWAYS(iStatusToUiProperty, NFE_FAULT());
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   749
		((RPropertyRef*) iStatusToUiProperty)->Set(aStatus);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   750
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   751
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   752
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   753
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   754
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   755
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   756
TInt DMediaDriverNFE::Request(TLocDrvRequest& aReq)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   757
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   758
//	__KTRACE_PRINT(Kern::Printf("NFE%d: DMediaDriverNFE::DoRequest() : Req %d drv %d flags %x pos %lx len %lx", iInstance, reqId, aReq.Drive()->iDriveNumber, aReq.Flags(), aReq.Pos(), aReq.Length()));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   759
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   760
	TInt r = KErrNotSupported;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   761
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   762
	TInt reqId = aReq.Id();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   763
    TNfeDriveInfo& di = iInfo.iDrives[DriveIndex(aReq.Drive()->iDriveNumber)];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   764
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   765
	switch (reqId)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   766
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   767
#if defined(__DEMAND_PAGING__)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   768
		case DMediaPagingDevice::ERomPageInRequest:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   769
			BTraceContext8(BTrace::EPagingMedia,BTrace::EPagingMediaPagingMedDrvBegin,MEDIA_DEVICE_NFE,&aReq);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   770
			r=HandleRead(aReq);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   771
			break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   772
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   773
		case DMediaPagingDevice::ECodePageInRequest:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   774
			BTraceContext8(BTrace::EPagingMedia,BTrace::EPagingMediaPagingMedDrvBegin,MEDIA_DEVICE_NFE,&aReq);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   775
			r=HandleRead(aReq);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   776
			break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   777
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   778
#endif	// __DEMAND_PAGING__
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   779
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   780
		case DLocalDrive::ERead:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   781
			r=HandleRead(aReq);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   782
			break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   783
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   784
		case DLocalDrive::EWrite:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   785
			r=HandleWrite(aReq);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   786
			break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   787
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   788
		case DLocalDrive::ECaps:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   789
			r = HandleCaps(aReq);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   790
			break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   791
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   792
		case DLocalDrive::EFormat:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   793
			r = HandleFormat(aReq);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   794
			break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   795
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   796
		// API used by T_NFE to query state etc.
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   797
		case DLocalDrive::EQueryDevice:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   798
			switch((TInt) aReq.iArg[0])
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   799
				{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   800
				case EQueryNfeDeviceInfo:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   801
					{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   802
					TNfeDeviceInfo& deviceInfo = *(TNfeDeviceInfo*) aReq.RemoteDes();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   803
					iInfo.iMediaSizeInBytes = iTotalSizeInBytes;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   804
					deviceInfo = iInfo;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   805
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   806
					r = KErrCompletion;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   807
					break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   808
					}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   809
				case RLocalDrive::EQueryFinaliseDrive:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   810
					{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   811
//					TLocalDriveFinaliseInfo& finaliseInfo = *(TLocalDriveFinaliseInfo*) aReq.RemoteDes();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   812
//					__KTRACE_PRINT(Kern::Printf("NFE%d: EQueryFinaliseDrive iMode %d", iInstance, finaliseInfo.iMode));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   813
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   814
					// write to boot sector to indicate that the drive has ben finalised
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   815
					WriteEncryptionStatusToBootSector(di, ETrue);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   816
					}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   817
				default:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   818
					r = KErrNotSupported;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   819
					break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   820
				}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   821
			break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   822
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   823
		default:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   824
			r = ForwardRequest(aReq);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   825
			break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   826
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   827
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   828
//	__KTRACE_PRINT(Kern::Printf("NFE%d: DMediaDriverNFE::DoRequest() : ret: %d", iInstance, r));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   829
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   830
	if (!di.iDriveFinalised && iBusy)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   831
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   832
		// Restart the idle timer after processing a request 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   833
		iIdleTimer.Cancel();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   834
		iTimerDfc.Cancel();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   835
		iIdleTimer.OneShot(NKern::TimerTicks(KNotBusyInterval));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   836
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   837
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   838
	return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   839
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   840
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   841
/**
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   842
PartitionInfo()
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   843
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   844
    Reads the partition information from the attached drive(s). 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   845
    Note: this method is also called when a removable card is removed, so can  
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   846
    be used to detect a memory card insertions/removals. Assumes the swap 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   847
    partition is encrypted if any encrypted FAT drives are found
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   848
*/
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   849
TInt DMediaDriverNFE::PartitionInfo(TPartitionInfo& aInfo)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   850
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   851
	__KTRACE_PRINT(Kern::Printf("NFE%d: DMediaDriverNFE::PartitionInfo()", iInstance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   852
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   853
	TInt r = DoDrivePartitionInfo(aInfo);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   854
	__KTRACE_PRINT(Kern::Printf("NFE%d: DoDrivePartitionInfo() r %d", iInstance, r));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   855
	if (r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   856
		return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   857
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   858
	__KTRACE_PRINT(Kern::Printf("NFE%d: *** Slave drives partition info ***", iInstance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   859
	__KTRACE_PRINT(Kern::Printf("NFE%d: iMediaSizeInBytes %lx", iInstance, aInfo.iMediaSizeInBytes));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   860
	__KTRACE_PRINT(Kern::Printf("NFE%d: iPartitionCount %d", iInstance, aInfo.iPartitionCount));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   861
	__KTRACE_PRINT(Kern::Printf("NFE%d: ", iInstance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   862
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   863
	TInt i;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   864
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   865
	__ASSERT_DEBUG(aInfo.iPartitionCount <= TNfeDeviceInfo::ENfeMaxPartitionEntries, NFE_FAULT());
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   866
	for (i=0; i<aInfo.iPartitionCount; i++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   867
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   868
		TInt driveNum = iDriveList[i];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   869
		iDriveNumToIndex[driveNum] = i;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   870
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   871
		TNfeDriveInfo& di = iInfo.iDrives[i];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   872
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   873
		di.iDriveFinalised = EFalse;	// a remount clears the finalised state
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   874
126
2b2a51c87b12 201015_18
hgs
parents: 123
diff changeset
   875
		// Make sure we haven't lost the swap partition
2b2a51c87b12 201015_18
hgs
parents: 123
diff changeset
   876
		__ASSERT_ALWAYS(!(di.iEntry.iPartitionType == KPartitionTypePagedData && aInfo.iEntry[i].iPartitionType != KPartitionTypePagedData), NFE_FAULT());
2b2a51c87b12 201015_18
hgs
parents: 123
diff changeset
   877
123
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   878
		// Make a copy of the TPartitionEntry
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   879
		di.iEntry = aInfo.iEntry[i];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   880
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   881
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   882
		// save the local drive number
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   883
		di.iLocalDriveNum = driveNum;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   884
		di.iDriveLetter = iDriveLetterList[i];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   885
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   886
		__KTRACE_PRINT(Kern::Printf("NFE%d: DriveNum %d", iInstance, driveNum));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   887
		__KTRACE_PRINT(Kern::Printf("NFE%d: DriveLetter %c", iInstance, (TInt) DriveLetterToAscii(di.iDriveLetter)));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   888
		__KTRACE_PRINT(Kern::Printf("NFE%d: iPartitionBaseAddr %lX", iInstance, di.iEntry.iPartitionBaseAddr));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   889
		__KTRACE_PRINT(Kern::Printf("NFE%d: iPartitionLen %lx", iInstance, di.iEntry.iPartitionLen));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   890
		__KTRACE_PRINT(Kern::Printf("NFE%d: iPartitionType %x", iInstance, di.iEntry.iPartitionType));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   891
		
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   892
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   893
		// If the drive was removed, reset it's state
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   894
		if (di.iEntry.iPartitionType == KPartitionTypeEmpty)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   895
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   896
			__KTRACE_PRINT(Kern::Printf("NFE%d: Empty Partition, setting state to ENfeUnmounted", iInstance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   897
			SetStatus(di, ENfeUnmounted);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   898
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   899
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   900
		// Is this an unencrypted FAT partition ?
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   901
		if (di.IsUDADrive())
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   902
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   903
			r = Read(di.iLocalDriveNum, di.iEntry.iPartitionBaseAddr, (TLinAddr) iBuffer, KSectorSize);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   904
			if (r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   905
				return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   906
			CheckBootSector(di);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   907
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   908
		
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   909
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   910
		__KTRACE_PRINT(Kern::Printf("NFE%d: status = %s", iInstance, DriveStatus(di.Status())));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   911
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   912
		__KTRACE_PRINT(Kern::Printf("NFE%d: iEncryptStartPos %lX", iInstance, di.iEncryptStartPos));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   913
		__KTRACE_PRINT(Kern::Printf("NFE%d: iEncryptEndPos %lX", iInstance, di.iEncryptEndPos));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   914
		__KTRACE_PRINT(Kern::Printf("NFE%d: ", iInstance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   915
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   916
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   917
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   918
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   919
#ifdef COMPOSITE_DRIVES
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   920
	// Accumulate the sizes of consecutive FAT drives & report the accumulated size back in the first FAT partition
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   921
	for (i=0; i<aInfo.iPartitionCount; i++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   922
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   923
		aInfo.iEntry[i] = iInfo.iDrives[i].iEntry;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   924
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   925
		if (iInfo.iDrives[i].IsUDADrive())
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   926
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   927
			aInfo.iEntry[i].iPartitionLen = 0;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   928
			for (TInt j=i; j<aInfo.iPartitionCount; j++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   929
				{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   930
				if (iInfo.iDrives[j].IsUDADrive())
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   931
					{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   932
					aInfo.iEntry[i].iPartitionLen+= iInfo.iDrives[j].iEntry.iPartitionLen;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   933
					}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   934
				}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   935
			iInfo.iDrives[i].iCompositeSize = aInfo.iEntry[i].iPartitionLen;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   936
			i = j;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   937
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   938
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   939
#endif
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   940
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   941
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   942
	SetTotalSizeInBytes(aInfo.iMediaSizeInBytes);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   943
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   944
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   945
	return KErrCompletion;	// synchronous completion
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   946
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   947
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   948
/**
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   949
HandleCaps() - 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   950
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   951
Return the Caps for a particular drive
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   952
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   953
Queries the caps from the attached drive, ORs in appropriate paging flags & returns
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   954
*/
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   955
TInt DMediaDriverNFE::HandleCaps(TLocDrvRequest& aReq)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   956
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   957
	// Get caps from slave drive
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   958
	// NB if we didn't want to alter anything then we could just call ForwardRequest(aReq);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   959
	TBuf8<sizeof(TLocalDriveCapsV6)> slaveCapsBuf;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   960
	TLocalDriveCapsV6& slaveCaps = *(TLocalDriveCapsV6*) slaveCapsBuf.Ptr();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   961
	slaveCapsBuf.SetMax();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   962
	slaveCapsBuf.FillZ();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   963
	TInt r = Caps(aReq.Drive()->iDriveNumber, slaveCapsBuf);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   964
	if (r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   965
		return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   966
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   967
#ifdef COMPOSITE_DRIVES
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   968
	TInt driveNum = aReq.Drive()->iDriveNumber;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   969
	TInt driveIndex = DriveIndex(driveNum);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   970
	if (iInfo.iDrives[driveIndex].iCompositeSize)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   971
		slaveCaps.iSize = iInfo.iDrives[driveIndex].iCompositeSize;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   972
#endif
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   973
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   974
	// copy slave caps to returned caps
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   975
	TLocalDriveCapsV6& caps = *(TLocalDriveCapsV6*)aReq.RemoteDes();		
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   976
	caps = slaveCaps;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   977
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   978
	// set the paging flags
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   979
#ifdef __DEMAND_PAGING__
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   980
	TLocDrv& drive = *aReq.Drive();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   981
	if (drive.iPrimaryMedia->iPagingMedia)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   982
		caps.iMediaAtt|=KMediaAttPageable;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   983
	if (drive.iPagingDrv)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   984
		caps.iDriveAtt|=KDriveAttPageable;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   985
#endif // __DEMAND_PAGING__
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   986
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   987
	return KErrCompletion;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   988
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   989
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   990
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   991
/**
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   992
AdjustRequest() - 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   993
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   994
Adjusts position & length if a request crosses these boundaries:
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   995
- the start of the partition (if RLocalDrive::ELocDrvWholeMedia set)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   996
- the current encrytion point (iEncryptEndPos) N.B. this will point to the end of the partition 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   997
  if the drive is fully encrypted
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   998
fc55edbf3919 201015_11
hgs
parents:
diff changeset
   999
For composite drives, it also adjusts the position, length & drive number as appropriate to cater for 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1000
crossing partition boundaries
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1001
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1002
returns ETrue if buffer needs encrypting/decrypting
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1003
*/
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1004
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1005
TBool DMediaDriverNFE::AdjustRequest(TNfeDriveInfo*& aDriveInfo, TInt64& aCurrentPos, TInt64& aCurrentLen)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1006
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1007
#ifdef COMPOSITE_DRIVES
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1008
	while (aCurrentPos >= aDriveInfo->iEntry.iPartitionLen)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1009
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1010
		aCurrentPos-= aDriveInfo->iEntry.iPartitionLen;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1011
		aDriveInfo++;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1012
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1013
	if (aCurrentPos + aCurrentLen > aDriveInfo->iEntry.iPartitionLen)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1014
		aCurrentLen = aDriveInfo->iEntry.iPartitionLen - aCurrentPos;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1015
#endif
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1016
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1017
	// do we need to encrypt/decrypt this buffer ?
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1018
	TBool encodeBuffer = EFalse;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1019
	
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1020
	if ((aDriveInfo->Status() == ENfeEncrypted) || aDriveInfo->Status() == ENfeEncrypting)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1021
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1022
//		__ASSERT_DEBUG(aDriveInfo->iEncryptEndPos <= aDriveInfo->iEntry.iPartitionBaseAddr + aDriveInfo->iEntry.iPartitionLen, NFE_FAULT());
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1023
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1024
		if (aCurrentPos < aDriveInfo->iEncryptStartPos)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1025
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1026
			aCurrentLen = Min(aCurrentLen, aDriveInfo->iEncryptStartPos - aCurrentPos);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1027
			encodeBuffer = EFalse;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1028
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1029
		else if (aCurrentPos < aDriveInfo->iEncryptEndPos)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1030
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1031
			aCurrentLen = Min(aCurrentLen, aDriveInfo->iEncryptEndPos - aCurrentPos);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1032
			encodeBuffer = ETrue;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1033
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1034
		else
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1035
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1036
			encodeBuffer = EFalse;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1037
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1038
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1039
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1040
	return encodeBuffer;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1041
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1042
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1043
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1044
TInt DMediaDriverNFE::HandleRead(TLocDrvRequest& aReq)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1045
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1046
	TInt r = KErrNone;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1047
	TInt64 currentPos = aReq.Pos();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1048
	TInt64 remainingLength = aReq.Length();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1049
	TInt desPos = 0;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1050
	TNfeDriveInfo* di = &iInfo.iDrives[DriveIndex(aReq.Drive()->iDriveNumber)];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1051
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1052
//	__KTRACE_PRINT(Kern::Printf("NFE%d: HandleRead pos %lx len %lx status %d", iInstance, currentPos, remainingLength, di->Status()));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1053
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1054
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1055
	di->iReadRequestCount++;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1056
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1057
	if (aReq.Flags() & TLocDrvRequest::ECodePaging)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1058
		di->iCodePagingRequesCount++;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1059
	if (aReq.Flags() & TLocDrvRequest::EDataPaging)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1060
		di->iDataPagingReadRequestCount++;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1061
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1062
	
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1063
	// just forward the request if the drive is not encrypted
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1064
	if (di->Status() == ENfeDecrypted)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1065
		return ForwardRequest(aReq);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1066
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1067
	
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1068
	while(remainingLength)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1069
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1070
		TInt64 currentLength = (remainingLength <= KBufSize ? remainingLength : KBufSize);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1071
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1072
		TBool decryptBuffer = AdjustRequest(di, currentPos, currentLength);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1073
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1074
		// Read from attached drive
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1075
#ifdef __DEMAND_PAGING__
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1076
		if (DMediaPagingDevice::PagingRequest(aReq))
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1077
			r = ReadPaged(di->iLocalDriveNum, currentPos, (TLinAddr) iBuffer, I64LOW(currentLength));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1078
		else
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1079
#endif
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1080
		r = Read(di->iLocalDriveNum, currentPos, (TLinAddr) iBuffer, I64LOW(currentLength));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1081
		if(r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1082
			break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1083
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1084
		TPtr8 des(iBuffer, I64LOW(currentLength), I64LOW(currentLength));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1085
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1086
		// decrypt buffer
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1087
		if (decryptBuffer)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1088
			DecryptBuffer(des);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1089
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1090
		//  write back to user
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1091
		r = aReq.WriteRemote(&des, desPos);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1092
		if(r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1093
			break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1094
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1095
		remainingLength-= currentLength;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1096
		currentPos+= currentLength;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1097
		desPos+= I64LOW(currentLength);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1098
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1099
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1100
	return r == KErrNone ? KErrCompletion : r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1101
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1102
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1103
TInt DMediaDriverNFE::HandleWrite(TLocDrvRequest& aReq)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1104
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1105
	TInt r = KErrNone;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1106
	TInt64 currentPos =  aReq.Pos();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1107
	TInt64 remainingLength = aReq.Length();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1108
	TInt desPos = 0;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1109
	TNfeDriveInfo* di = &iInfo.iDrives[DriveIndex(aReq.Drive()->iDriveNumber)];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1110
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1111
//	__KTRACE_PRINT(Kern::Printf("NFE%d: HandleWrite pos %lx len %lx status %d", iInstance, currentPos, remainingLength, di->Status()));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1112
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1113
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1114
	di->iWriteRequestCount++;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1115
	if (aReq.Flags() & TLocDrvRequest::EDataPaging)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1116
		di->iDataPagingWriteRequestCount++;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1117
	
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1118
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1119
	// just forward the request if the drive is not encrypted
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1120
	if (di->Status() == ENfeDecrypted)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1121
		return ForwardRequest(aReq);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1122
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1123
	while(remainingLength)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1124
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1125
		TInt64 currentLength = (remainingLength <= KBufSize ? remainingLength : KBufSize);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1126
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1127
		TBool encryptBuffer = AdjustRequest(di, currentPos, currentLength);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1128
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1129
		// read from user
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1130
		TPtr8 des(iBuffer,0,I64LOW(currentLength));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1131
		r = aReq.ReadRemote(&des, desPos);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1132
		if(r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1133
			break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1134
		
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1135
		// get the length of data read from the user in case user's
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1136
		// descriptor is shorter than advertised
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1137
		currentLength = des.Length();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1138
		if (currentLength == 0)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1139
			break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1140
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1141
		// writing to sector zero ?
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1142
		if (currentPos >= di->iEntry.iPartitionBaseAddr && 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1143
			currentPos < di->iEntry.iPartitionBaseAddr + KSectorSize && 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1144
			di->IsUDADrive())
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1145
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1146
			__KTRACE_PRINT(Kern::Printf("NFE%d: Write to sector #0 detected", iInstance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1147
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1148
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1149
			TUint8* bootSector = iBuffer;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1150
			TUint8 bootSectorBuffer[KSectorSize];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1151
			// writing partial sector ?
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1152
			if (currentPos > di->iEntry.iPartitionBaseAddr || currentLength < KSectorSize)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1153
				{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1154
				bootSector = bootSectorBuffer;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1155
				r = Read(di->iLocalDriveNum, di->iEntry.iPartitionBaseAddr, (TLinAddr) bootSector, KSectorSize);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1156
				if(r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1157
					break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1158
				TInt64 readLen = KSectorSize;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1159
				TBool encryptBuffer = AdjustRequest(di, di->iEntry.iPartitionBaseAddr, readLen);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1160
				if (encryptBuffer)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1161
					{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1162
					TPtr8 des(bootSectorBuffer,KSectorSize,KSectorSize);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1163
					DecryptBuffer(des);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1164
					}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1165
				TInt sectorOffset = (TInt) (currentPos - di->iEntry.iPartitionBaseAddr);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1166
				TInt64 copyLen = currentLength;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1167
				if (copyLen > KSectorSize-sectorOffset)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1168
					copyLen = KSectorSize-sectorOffset;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1169
				memcpy(bootSectorBuffer+sectorOffset, iBuffer, (TInt) copyLen);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1170
				}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1171
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1172
			if ((di->Status() == ENfeUnmounted || di->Status() == ENfeCorrupted) && 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1173
				ValidBootSector(bootSector))
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1174
				{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1175
				__KTRACE_PRINT(Kern::Printf("NFE%d: Setting status to ENfeDecrypted", iInstance ));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1176
				di->SetStatus(ENfeDecrypted);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1177
				}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1178
			di->iUniqueID = VolumeId(bootSector);		// update the Volume ID
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1179
			__KTRACE_PRINT(Kern::Printf("NFE%d: Setting Volume ID to %08X", iInstance, di->iUniqueID ));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1180
			TBootSectorStatus* bootSectorStatus = (TBootSectorStatus*) iBuffer;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1181
			if (di->Status() == ENfeEncrypting || di->Status() == ENfeDecrypting)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1182
				{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1183
				__KTRACE_PRINT(Kern::Printf("NFE%d: Adding NFE status record to boot sector", iInstance ));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1184
				bootSectorStatus->iSignature = TBootSectorStatus::ENfeBootSectorSignature;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1185
				bootSectorStatus->iEncryptEndPos = di->iEncryptEndPos;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1186
				bootSectorStatus->iStatus = di->Status();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1187
				bootSectorStatus->iFinalised = EFalse;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1188
				}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1189
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1190
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1191
		// encrypt the buffer
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1192
		if (encryptBuffer)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1193
			EncryptBuffer(des);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1194
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1195
		// write the data to the attached drive
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1196
#ifdef __DEMAND_PAGING__
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1197
		if (DMediaPagingDevice::PagingRequest(aReq))
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1198
			r = WritePaged(di->iLocalDriveNum, currentPos, (TLinAddr) iBuffer, I64LOW(currentLength));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1199
		else
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1200
#endif
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1201
		r = Write(di->iLocalDriveNum, currentPos, (TLinAddr) iBuffer, I64LOW(currentLength));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1202
		if(r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1203
			break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1204
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1205
		remainingLength-= currentLength;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1206
		currentPos+= currentLength;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1207
		desPos+= I64LOW(currentLength);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1208
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1209
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1210
	return r == KErrNone ? KErrCompletion : r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1211
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1212
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1213
TInt DMediaDriverNFE::HandleFormat(TLocDrvRequest& aReq)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1214
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1215
	TInt r = KErrNone;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1216
	TInt64 currentPos =  aReq.Pos();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1217
	TInt64 remainingLength = aReq.Length();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1218
	TNfeDriveInfo* di = &iInfo.iDrives[DriveIndex(aReq.Drive()->iDriveNumber)];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1219
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1220
//	__KTRACE_PRINT(Kern::Printf("NFE%d: HandleFormat pos %lx len %lx status %d", iInstance, currentPos, remainingLength, di->Status()));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1221
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1222
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1223
	// just forward the request if the drive is not encrypted
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1224
	if (di->Status() == ENfeDecrypted)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1225
		return ForwardRequest(aReq);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1226
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1227
	// otherwise create a buffer containing NULLs, encrypt it and write that to the attached drive
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1228
	while(remainingLength && r == KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1229
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1230
		TInt64 currentLength = (remainingLength <= KBufSize ? remainingLength : KBufSize);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1231
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1232
		TBool encryptBuffer = AdjustRequest(di, currentPos, currentLength);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1233
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1234
		memclr(iBuffer, KBufSize);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1235
		TPtr8 des(iBuffer,KBufSize,KBufSize);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1236
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1237
		if (encryptBuffer)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1238
			EncryptBuffer(des);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1239
		
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1240
		r = Write(di->iLocalDriveNum, currentPos, (TLinAddr) iBuffer, I64LOW(currentLength));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1241
		if(r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1242
			break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1243
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1244
		remainingLength-= currentLength;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1245
		currentPos+= currentLength;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1246
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1247
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1248
	return r == KErrNone ? KErrCompletion : r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1249
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1250
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1251
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1252
void DMediaDriverNFE::EncryptBuffer(TDes8& aBuffer)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1253
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1254
	TInt len = aBuffer.Length();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1255
	for(TInt i=0; i<len; i++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1256
		aBuffer[i] = EncryptByte(aBuffer[i]);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1257
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1258
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1259
void DMediaDriverNFE::DecryptBuffer(TDes8& aBuffer)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1260
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1261
	TInt len = aBuffer.Length();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1262
	for(TInt i=0; i<len; i++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1263
		aBuffer[i] = DecryptByte(aBuffer[i]);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1264
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1265
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1266
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1267
TNfeDriveInfo* DMediaDriverNFE::GetSwapDrive()
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1268
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1269
	for (TInt i=0; i<iInfo.iDriveCount; i++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1270
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1271
		TNfeDriveInfo& di = iInfo.iDrives[i];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1272
		if (di.iEntry.iPartitionType == KPartitionTypePagedData)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1273
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1274
			return &di;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1275
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1276
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1277
	return NULL;	// swap drive not found
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1278
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1279
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1280
/**
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1281
Get the first/next drive to encrypt 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1282
*/
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1283
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1284
TNfeDriveInfo* DMediaDriverNFE::NextDrive()
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1285
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1286
	for (iDriveIndex = 0; iDriveIndex<iInfo.iDriveCount; iDriveIndex++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1287
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1288
		TNfeDriveInfo& di = iInfo.iDrives[iDriveIndex];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1289
		TNfeDiskStatus status = iInfo.iDrives[iDriveIndex].Status();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1290
		if (status == ENfeEncrypting || status == ENfeDecrypting)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1291
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1292
			di.iEncryptStartPos = di.iEncryptEndPos = di.iEntry.iPartitionBaseAddr;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1293
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1294
			// write to boot sector to indicate we are encrypting/decrypting this drive
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1295
			WriteEncryptionStatusToBootSector(di);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1296
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1297
			return &di;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1298
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1299
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1300
	__KTRACE_PRINT(Kern::Printf("NFE%d: Finished encrypting / decrypting", iInstance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1301
	iBusy = EFalse;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1302
	return NULL;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1303
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1304
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1305
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1306
/** 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1307
Finds the first unencrypted drive & kicks off the idle timer - 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1308
when this expires the encryption of the drive will start
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1309
*/
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1310
void DMediaDriverNFE::StartEncrypting()
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1311
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1312
	// start encrypting if not already doing so
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1313
	if (!iBusy)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1314
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1315
		iBusy = ETrue;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1316
		TNfeDriveInfo* di = NextDrive();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1317
		if (di)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1318
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1319
			__KTRACE_PRINT(Kern::Printf("NFE%d: Start encrypting drive %d...", iInstance, iInfo.iDrives[iDriveIndex].iLocalDriveNum));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1320
			iIdleTimer.OneShot(NKern::TimerTicks(KNotBusyInterval));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1321
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1322
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1323
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1324
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1325
/** 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1326
Finds the first unencrypted drive & kicks off the idle timer - 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1327
when this expires the encryption of the drive will start
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1328
*/
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1329
void DMediaDriverNFE::StartDecrypting()
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1330
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1331
	// start encrypting if not already doing so
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1332
	if (!iBusy)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1333
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1334
		iBusy = ETrue;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1335
		TNfeDriveInfo* di = NextDrive();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1336
		if (di)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1337
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1338
			__KTRACE_PRINT(Kern::Printf("NFE%d: Start decrypting drive %d...", iInstance, iInfo.iDrives[iDriveIndex].iLocalDriveNum));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1339
			iIdleTimer.OneShot(NKern::TimerTicks(KNotBusyInterval));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1340
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1341
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1342
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1343
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1344
/**
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1345
Idle timer callback
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1346
Kicks off a DFC to ensure we are running in the correct thread
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1347
*/
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1348
void DMediaDriverNFE::IdleTimerCallBack(TAny* aMediaDriver)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1349
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1350
	((DMediaDriverNFE*)aMediaDriver)->iTimerDfc.Add();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1351
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1352
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1353
/**
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1354
Idle timer DFC
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1355
*/
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1356
void DMediaDriverNFE::TimerDfcFunction(TAny* aMediaDriver)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1357
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1358
	((DMediaDriverNFE*) aMediaDriver)->HandleDiskContent();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1359
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1360
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1361
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1362
TBool DMediaDriverNFE::ValidBootSector(TUint8* aBuffer)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1363
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1364
	if (aBuffer[0] == 0xEB || aBuffer[0] == 0xE9)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1365
		return ETrue;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1366
	else
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1367
		return EFalse;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1368
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1369
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1370
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1371
TUint32 DMediaDriverNFE::VolumeId(TUint8* aBuffer)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1372
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1373
	TUint16 rootDirEntries;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1374
	TUint32 uniqueID;   
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1375
    memcpy(&rootDirEntries,&aBuffer[17], 2);	// 17   TUint16 iRootDirEntries
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1376
	TBool fat32 = rootDirEntries == 0;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1377
	TInt pos = fat32 ? 67 : 39;		// get position of VolumeID
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1378
	memcpy(&uniqueID,&aBuffer[pos],4);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1379
	return uniqueID;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1380
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1381
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1382
void DMediaDriverNFE::CheckBootSector(TNfeDriveInfo &aDi)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1383
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1384
	TNfeDiskStatus  fatBootSectorStatus = ENfeDecrypted;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1385
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1386
	// Try to determine whether the FAT boot sector is encypted
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1387
	if (ValidBootSector(iBuffer))
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1388
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1389
		fatBootSectorStatus = ENfeDecrypted;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1390
		__KTRACE_PRINT(Kern::Printf("NFE%d: FAT Boot sector is decrypted", iInstance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1391
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1392
	else
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1393
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1394
		TPtr8 des(iBuffer, KSectorSize, KSectorSize);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1395
		DecryptBuffer(des);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1396
		if (ValidBootSector(iBuffer))
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1397
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1398
			__KTRACE_PRINT(Kern::Printf("NFE%d: FAT Boot sector is encrypted", iInstance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1399
			fatBootSectorStatus = ENfeEncrypted;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1400
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1401
		else
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1402
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1403
			__KTRACE_PRINT(Kern::Printf("NFE%d: FAT Boot sector is corrupted", iInstance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1404
			fatBootSectorStatus = ENfeCorrupted;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1405
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1406
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1407
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1408
	__KTRACE_PRINT(Kern::Printf("NFE%d: fatBootSectorStatus %d", iInstance, fatBootSectorStatus));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1409
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1410
	// Find out whether the volume has changed
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1411
	TUint32 uniqueID = VolumeId(iBuffer);   
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1412
	TBool volumeChanged = uniqueID != aDi.iUniqueID;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1413
	__KTRACE_PRINT(Kern::Printf("NFE%d: Old Volume ID %08X", iInstance, aDi.iUniqueID));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1414
	__KTRACE_PRINT(Kern::Printf("NFE%d: New Volume ID %08X", iInstance, uniqueID));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1415
	__KTRACE_PRINT(Kern::Printf("NFE%d: volumeChanged %d", iInstance, volumeChanged));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1416
	aDi.iUniqueID = uniqueID;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1417
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1418
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1419
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1420
	TBootSectorStatus* bootSectorStatus = (TBootSectorStatus*) iBuffer;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1421
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1422
	__KTRACE_PRINT(Kern::Printf("NFE%d: CheckBootSector, iSignature %08X", iInstance, bootSectorStatus->iSignature));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1423
	__KTRACE_PRINT(Kern::Printf("NFE%d: CheckBootSector, iStatus %d", iInstance, bootSectorStatus->iStatus));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1424
	__KTRACE_PRINT(Kern::Printf("NFE%d: CheckBootSector, iEncryptEndPos %lx", iInstance, bootSectorStatus->iEncryptEndPos));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1425
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1426
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1427
	/*
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1428
	If there IS NFE info in the boot sector, restore the encryption settings - 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1429
	unless the 'finalised' flag is clear which indicates that the media was removed or power was lost
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1430
	while encrypting the device...
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1431
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1432
	If there is no NFE info in the boot sector and there has been a volume change, then we can decide  
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1433
	whether the drive is encrypted/decrypted/corrupt by examining the boot sector
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1434
	*/
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1435
	if (volumeChanged && 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1436
		fatBootSectorStatus != ENfeCorrupted &&
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1437
		bootSectorStatus->iSignature == TBootSectorStatus::ENfeBootSectorSignature &&
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1438
		!bootSectorStatus->iFinalised)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1439
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1440
		SetStatus(aDi, ENfeCorrupted);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1441
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1442
	else if (volumeChanged && 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1443
		fatBootSectorStatus != ENfeCorrupted &&
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1444
		bootSectorStatus->iFinalised &&
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1445
		bootSectorStatus->iSignature == TBootSectorStatus::ENfeBootSectorSignature &&
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1446
		(bootSectorStatus->iStatus == ENfeDecrypting || bootSectorStatus->iStatus == ENfeEncrypting))
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1447
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1448
		SetStatus(aDi, bootSectorStatus->iStatus);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1449
		aDi.iEncryptEndPos = bootSectorStatus->iEncryptEndPos;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1450
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1451
		// write to boot sector to indicate we are no longer finalised
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1452
		WriteEncryptionStatusToBootSector(aDi, EFalse);	
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1453
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1454
		iBusy = ETrue;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1455
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1456
	else if (volumeChanged || aDi.Status() == ENfeUnmounted)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1457
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1458
		SetStatus(aDi, fatBootSectorStatus);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1459
		if (aDi.Status() == ENfeEncrypted)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1460
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1461
			aDi.iEncryptStartPos = aDi.iEntry.iPartitionBaseAddr;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1462
			aDi.iEncryptEndPos = aDi.iEntry.iPartitionBaseAddr + aDi.iEntry.iPartitionLen;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1463
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1464
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1465
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1466
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1467
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1468
TInt DMediaDriverNFE::WriteEncryptionStatusToBootSector(TNfeDriveInfo &aDi, TBool aFinalised)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1469
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1470
	if (!aDi.IsUDADrive())
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1471
		return KErrNone;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1472
134
95847726fe57 201019_07
hgs
parents: 126
diff changeset
  1473
	aDi.iDriveFinalised = aFinalised;
123
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1474
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1475
	TNfeDiskStatus status = aDi.Status();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1476
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1477
	TInt64 currentPos = aDi.iEntry.iPartitionBaseAddr;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1478
	TInt64 currentLen = KSectorSize;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1479
	TNfeDriveInfo* di = &aDi;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1480
	TBool encodeBuffer = EFalse;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1481
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1482
	if (status == ENfeEncrypting || status == ENfeEncrypted || status == ENfeDecrypting)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1483
		encodeBuffer = AdjustRequest(di, currentPos, currentLen);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1484
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1485
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1486
	TInt r = Read(di->iLocalDriveNum, di->iEntry.iPartitionBaseAddr, (TLinAddr) iBuffer, KSectorSize);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1487
	if (r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1488
		return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1489
	TPtr8 des(iBuffer, I64LOW(currentLen), I64LOW(currentLen));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1490
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1491
	if (encodeBuffer)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1492
		DecryptBuffer(des);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1493
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1494
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1495
	TBootSectorStatus* bootSectorStatus = (TBootSectorStatus*) iBuffer;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1496
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1497
	if (status == ENfeEncrypting || status == ENfeDecrypting)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1498
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1499
		bootSectorStatus->iSignature = TBootSectorStatus::ENfeBootSectorSignature;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1500
		bootSectorStatus->iEncryptEndPos = di->iEncryptEndPos;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1501
		bootSectorStatus->iStatus = status;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1502
		bootSectorStatus->iFinalised = aFinalised;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1503
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1504
	else
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1505
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1506
		bootSectorStatus->iSignature = 0;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1507
		bootSectorStatus->iEncryptEndPos = 0;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1508
		bootSectorStatus->iStatus = ENfeUnmounted;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1509
		bootSectorStatus->iFinalised = EFalse;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1510
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1511
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1512
	if (encodeBuffer)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1513
		EncryptBuffer(des);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1514
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1515
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1516
	r = Write(di->iLocalDriveNum, di->iEntry.iPartitionBaseAddr, (TLinAddr) iBuffer, KSectorSize);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1517
	return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1518
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1519
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1520
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1521
/**
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1522
HandleDiskContent - 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1523
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1524
Called from Idle timer DFC
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1525
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1526
Starts encrypting the current drive (iDrives[iDriveIndex]) from the current encryption position (iEncryptEndPos)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1527
*/
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1528
TInt DMediaDriverNFE::HandleDiskContent()
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1529
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1530
	TNfeDriveInfo* di = &iInfo.iDrives[iDriveIndex];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1531
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1532
	__KTRACE_PRINT(Kern::Printf("NFE%d: Starting to encrypt Drive %d at pos %lx", iInstance, di->iLocalDriveNum, di->iEncryptEndPos));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1533
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1534
	if (di->iDriveFinalised)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1535
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1536
	    __KTRACE_PRINT(Kern::Printf("HandleDiskContent aborting as drive has been finalised", iInstance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1537
		return KErrNone;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1538
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1539
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1540
//	TInt KBackgroundPriority = 7;						//*test*
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1541
//	Kern::SetThreadPriority(KBackgroundPriority);		//*test*
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1542
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1543
	TInt r = KErrNone;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1544
	for (;;)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1545
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1546
		// If we've finished encryting this drive, change the state and move on to the next drive
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1547
		if (r != KErrNone || di->iEncryptEndPos >= di->iEntry.iPartitionBaseAddr + di->iEntry.iPartitionLen)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1548
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1549
			if (di->Status() == ENfeEncrypting)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1550
				{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1551
				__KTRACE_PRINT(Kern::Printf("NFE%d: Finished encrypting Drive %d r %d", iInstance, di->iLocalDriveNum, r));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1552
				SetStatus(*di,  r == KErrNone ? ENfeEncrypted : ENfeCorrupted);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1553
				}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1554
			if (di->Status() == ENfeDecrypting)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1555
				{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1556
				__KTRACE_PRINT(Kern::Printf("NFE%d: Finished decrypting Drive %d r %d", iInstance, di->iLocalDriveNum, r));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1557
				SetStatus(*di,  r == KErrNone ? ENfeDecrypted : ENfeCorrupted);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1558
				}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1559
			
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1560
			// write to boot sector to indicate we have finished encrypting/decrypting this drive
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1561
			r = WriteEncryptionStatusToBootSector(*di);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1562
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1563
			di = NextDrive();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1564
			if (di == NULL)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1565
				{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1566
				r = KErrCompletion;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1567
				break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1568
				}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1569
			__KTRACE_PRINT(Kern::Printf("NFE%d: Starting to encrypt Drive %d", iInstance, iInfo.iDrives[iDriveIndex].iLocalDriveNum));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1570
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1571
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1572
		// If this media or any of the attached media are busy, stop encrypting & wait for the next idle timeout
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1573
		if (MediaBusy(di->iLocalDriveNum))
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1574
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1575
			__KTRACE_PRINT(Kern::Printf("NFE%d: Media is busy !!!", iInstance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1576
			r = KErrNone;	// goto sleep & wait for another timer event
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1577
			break;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1578
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1579
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1580
		TInt64& pos = di->iEncryptEndPos;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1581
		TInt64 partitionEnd = di->iEntry.iPartitionBaseAddr + di->iEntry.iPartitionLen;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1582
		TInt len = (TInt) Min (partitionEnd - pos, KBufSize);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1583
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1584
#if defined(TRACE_ENABLED)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1585
		// print position every 1/16 of the partition size
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1586
		TInt64 printPos = Max((di->iEntry.iPartitionLen >> 4) & ~(KBufSize-1), KBufSize);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1587
		if (((di->iEncryptEndPos - di->iEncryptStartPos)% printPos) == 0) 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1588
			__KTRACE_PRINT(Kern::Printf("NFE%d: Encrypting drive %d from %lx to %lx end %lx", iInstance, di->iLocalDriveNum, pos, pos + len, partitionEnd));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1589
#endif
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1590
//		__KTRACE_PRINT(Kern::Printf("NFE%d: Encrypting drive %d from %lx to %lx end %lx", iInstance, di->iLocalDriveNum, pos, pos + len, partitionEnd));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1591
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1592
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1593
		// Read a buffer, encrypt it, and then write it back
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1594
		// retry in case of media change
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1595
		const TInt KRetries = 5;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1596
		r = KErrNotReady;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1597
		for (TInt i=0; r == KErrNotReady && i < KRetries; i++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1598
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1599
			r = Read(di->iLocalDriveNum, pos, (TLinAddr) iBuffer, len);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1600
			if (r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1601
				continue;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1602
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1603
			TPtr8 des(iBuffer,len,len);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1604
			if (di->Status() == ENfeEncrypting)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1605
				EncryptBuffer(des);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1606
			else
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1607
				DecryptBuffer(des);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1608
			
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1609
			r = Write(di->iLocalDriveNum, pos, (TLinAddr) iBuffer, len);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1610
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1611
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1612
		if (r == KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1613
			pos+= len;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1614
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1615
		if (di->iProgressToUiProperty)	// no iProgressToUiProperty for swap drive
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1616
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1617
			TInt progress = (TInt) (KNfeDiskOpReady * (pos - di->iEntry.iPartitionBaseAddr) / di->iEntry.iPartitionLen);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1618
//			__KTRACE_PRINT(Kern::Printf("NFE%d: Progess %d ", progress));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1619
			((RPropertyRef*) (di->iProgressToUiProperty))->Set( progress ); // Return value ignored
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1620
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1621
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1622
	
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1623
	__KTRACE_PRINT(Kern::Printf("NFE%d: HandleDiskContent returned %d", iInstance, r));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1624
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1625
	// If not completed, start the idle timer & try again later
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1626
	if (r != KErrCompletion)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1627
		iIdleTimer.OneShot(NKern::TimerTicks(KNotBusyInterval));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1628
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1629
//	Kern::SetThreadPriority(KNfeThreadPriority);	//*test*
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1630
	
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1631
	return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1632
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1633
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1634
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1635
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1636
DECLARE_EXTENSION_PDD()
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1637
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1638
	__KTRACE_PRINT(Kern::Printf("DECLARE_EXTENSION_PDD()"));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1639
	return new DPhysicalDeviceMediaNFE;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1640
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1641
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1642
DECLARE_STANDARD_EXTENSION()
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1643
	{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1644
	__KTRACE_PRINT(Kern::Printf("DECLARE_STANDARD_EXTENSION()"));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1645
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1646
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1647
	// Create the media driver factory object and register this with the kernel
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1648
	__KTRACE_PRINT(Kern::Printf("Creating NFE PDD"));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1649
	DPhysicalDeviceMediaNFE* device = new DPhysicalDeviceMediaNFE;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1650
	if (device == NULL)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1651
		return KErrNoMemory;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1652
	TInt r = Kern::InstallPhysicalDevice(device);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1653
	__KTRACE_PRINT(Kern::Printf("Installing NFE PDD in extension init - name %s r:%d", NFE_DRIVENAME, r));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1654
	if (r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1655
		return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1656
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1657
	TInt swapInstance = KErrNotFound;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1658
#if defined (__DEMAND_PAGING__)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1659
	swapInstance = SwapInstance();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1660
#endif
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1661
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1662
	DPrimaryMediaExt* primaryMedia[NFE_INSTANCE_COUNT];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1663
	TInt instance;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1664
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1665
	for (instance=0; instance<NFE_INSTANCE_COUNT; instance++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1666
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1667
		// Register this media device & define which drives we want to attach to.
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1668
		// These drives must already be registered with the local media subsystem
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1669
		// i.e. this media's kernel extension must be defined AFTER any attached
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1670
		// media's kernel extension in the appropriate .IBY file
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1671
		__KTRACE_PRINT(Kern::Printf("NFE%d: Creating NFE primary media", instance));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1672
		DPrimaryMediaExt* pM = new DPrimaryMediaExt(instance);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1673
		if (pM == NULL)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1674
			return KErrNoMemory;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1675
		primaryMedia[instance] = pM;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1676
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1677
		_LIT(KMediaThreadName,"NfeThread?");
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1678
		HBuf* pMediaThreadName = HBuf::New(KMediaThreadName);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1679
		(*pMediaThreadName)[9] = (TUint8) ('0' + (TUint8) instance);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1680
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1681
		TInt r = Kern::DfcQInit(&pM->iNfeDfcQ,KNfeThreadPriority,pMediaThreadName);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1682
		if (r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1683
			return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1684
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1685
#ifdef CPU_AFFINITY_ANY
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1686
		NKern::ThreadSetCpuAffinity((NThread*)(pM->iNfeDfcQ.iThread), KCpuAffinityAny);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1687
#endif
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1688
		
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1689
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1690
		pM->iDfcQ = &pM->iNfeDfcQ;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1691
		pM->iMsgQ.Receive();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1692
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1693
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1694
		const TInt* driveList = DriveList(instance);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1695
		TInt driveCount = DriveCount(instance);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1696
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1697
		TBuf<4> driveName(_L("NFE?"));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1698
		driveName[3] = (TUint8) ('0' + (TUint8) instance);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1699
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1700
		
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1701
		r = LocDrv::RegisterMediaDevice(
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1702
			MEDIA_DEVICE_NFE, 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1703
			driveCount, driveList,
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1704
			pM, NFE_NUMMEDIA, driveName);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1705
		if (r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1706
			return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1707
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1708
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1709
#if defined (__DEMAND_PAGING__)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1710
		if (PagingType(instance))
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1711
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1712
			// Define which of the drives we have already attached to have code or data paging enabled 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1713
			const TInt* pageDriveList = PageDriveList(instance);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1714
			TInt pageDriveCount = PageDriveCount(instance);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1715
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1716
			r = LocDrv::RegisterPagingDevice(pM,pageDriveList,pageDriveCount,PagingType(instance),SECTOR_SHIFT,NFE_NUM_PAGES);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1717
			__KTRACE_PRINT(Kern::Printf("NFE%d: Installing NFE PagingDevice in extension init - r:%d", pM->iInstance, r));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1718
			// Ignore error if demand paging not supported by kernel
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1719
			if (r == KErrNotSupported)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1720
				r = KErrNone;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1721
			if (r != KErrNone)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1722
				return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1723
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1724
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1725
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1726
#endif	// __NAND_DEMAND_PAGING__
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1727
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1728
		/*
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1729
		If there is a swap partition we need to make sure all instances have their PartitionInfo() called
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1730
		so that we can flag the swap partition as 'encrypted' if there are any encrypted drives at all
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1731
		*/
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1732
		if (swapInstance != KErrNotFound)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1733
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1734
			TBuf8<sizeof(TLocalDriveCapsV6)> capsBuf;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1735
			capsBuf.SetMax();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1736
			capsBuf.FillZ();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1737
			DLocalDrive::Caps(driveList[0], capsBuf);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1738
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1739
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1740
		
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1741
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1742
	// If we encounter an encrypted drive belonging to ANY NFE instance, then assume the swap partition is 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1743
	// encrypted too. We need to do this because the swap partition has no equivalent of the boot sector
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1744
	if (swapInstance != KErrNotFound)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1745
		{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1746
		__KTRACE_PRINT(Kern::Printf("NFE: Searching for encrypted drives to determine whether swap partition should be encrypted..."));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1747
		TBool encryptedDriveFound = EFalse;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1748
		TNfeDriveInfo* swapDriveInfo = NULL;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1749
		for (instance=0; instance<NFE_INSTANCE_COUNT; instance++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1750
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1751
			DPrimaryMediaExt* pM = primaryMedia[instance];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1752
			DMediaDriverNFE* mediaDriver = (DMediaDriverNFE*) pM->iDriver;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1753
			__ASSERT_ALWAYS(mediaDriver, NFE_FAULT());
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1754
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1755
			if (swapDriveInfo == NULL)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1756
				swapDriveInfo = mediaDriver->GetSwapDrive();
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1757
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1758
			for (TInt i=0; i<mediaDriver->iInfo.iDriveCount; i++)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1759
				{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1760
				TNfeDriveInfo& di = mediaDriver->iInfo.iDrives[i];
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1761
				__KTRACE_PRINT(Kern::Printf("NFE%d: Testing drive %d DriveLetter %c status %s", 
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1762
					instance, di.iLocalDriveNum, (TInt) DriveLetterToAscii(di.iDriveLetter), DriveStatus(di.Status()) ));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1763
				if (di.Status() == ENfeEncrypted || di.Status() == ENfeEncrypting)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1764
					encryptedDriveFound = ETrue;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1765
				}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1766
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1767
		if (swapDriveInfo)
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1768
			{
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1769
			swapDriveInfo->SetStatus(encryptedDriveFound ? ENfeEncrypted : ENfeDecrypted);
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1770
			swapDriveInfo->iEncryptEndPos = swapDriveInfo->iEntry.iPartitionBaseAddr + swapDriveInfo->iEntry.iPartitionLen;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1771
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1772
			__KTRACE_PRINT(Kern::Printf("NFE: Setting swap partition state to %s...", DriveStatus(swapDriveInfo->Status())));
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1773
			}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1774
		}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1775
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1776
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1777
	return r;
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1778
	}
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1779
fc55edbf3919 201015_11
hgs
parents:
diff changeset
  1780