kerneltest/e32test/mediaext/d_nfe.cpp
branchRCL_3
changeset 44 3e88ff8f41d5
parent 43 c1f20ce4abcf
equal deleted inserted replaced
43:c1f20ce4abcf 44:3e88ff8f41d5
    20 #include <variantmediadef.h>
    20 #include <variantmediadef.h>
    21 #include "nfe.h"
    21 #include "nfe.h"
    22 
    22 
    23 #if defined(_DEBUG)
    23 #if defined(_DEBUG)
    24 //	#define TRACE_ENABLED
    24 //	#define TRACE_ENABLED
       
    25 #define TRACE_ENABLED	//*test*
    25 #else
    26 #else
    26 #endif
    27 #endif
    27 
    28 
    28 #if defined(TRACE_ENABLED)
    29 #if defined(TRACE_ENABLED)
    29 #define __KTRACE_PRINT(p) {p;}
    30 #define __KTRACE_PRINT(p) {p;}
  1048 	TInt64 remainingLength = aReq.Length();
  1049 	TInt64 remainingLength = aReq.Length();
  1049 	TInt desPos = 0;
  1050 	TInt desPos = 0;
  1050 	TNfeDriveInfo* di = &iInfo.iDrives[DriveIndex(aReq.Drive()->iDriveNumber)];
  1051 	TNfeDriveInfo* di = &iInfo.iDrives[DriveIndex(aReq.Drive()->iDriveNumber)];
  1051 
  1052 
  1052 //	__KTRACE_PRINT(Kern::Printf("NFE%d: HandleRead pos %lx len %lx status %d", iInstance, currentPos, remainingLength, di->Status()));
  1053 //	__KTRACE_PRINT(Kern::Printf("NFE%d: HandleRead pos %lx len %lx status %d", iInstance, currentPos, remainingLength, di->Status()));
  1053 
       
  1054 	if (di->iEntry.iPartitionLen == 0)
       
  1055 		return KErrNotReady;
       
  1056 
  1054 
  1057 
  1055 
  1058 	di->iReadRequestCount++;
  1056 	di->iReadRequestCount++;
  1059 
  1057 
  1060 	if (aReq.Flags() & TLocDrvRequest::ECodePaging)
  1058 	if (aReq.Flags() & TLocDrvRequest::ECodePaging)
  1213 	return r == KErrNone ? KErrCompletion : r;
  1211 	return r == KErrNone ? KErrCompletion : r;
  1214 	}
  1212 	}
  1215 
  1213 
  1216 TInt DMediaDriverNFE::HandleFormat(TLocDrvRequest& aReq)
  1214 TInt DMediaDriverNFE::HandleFormat(TLocDrvRequest& aReq)
  1217 	{
  1215 	{
       
  1216 	TInt r = KErrNone;
  1218 	TInt64 currentPos =  aReq.Pos();
  1217 	TInt64 currentPos =  aReq.Pos();
       
  1218 	TInt64 remainingLength = aReq.Length();
  1219 	TNfeDriveInfo* di = &iInfo.iDrives[DriveIndex(aReq.Drive()->iDriveNumber)];
  1219 	TNfeDriveInfo* di = &iInfo.iDrives[DriveIndex(aReq.Drive()->iDriveNumber)];
  1220 
  1220 
  1221 	__KTRACE_PRINT(Kern::Printf("NFE%d: HandleFormat pos %lx len %lx status %d", iInstance, currentPos, aReq.Length(), di->Status()));
  1221 //	__KTRACE_PRINT(Kern::Printf("NFE%d: HandleFormat pos %lx len %lx status %d", iInstance, currentPos, remainingLength, di->Status()));
  1222 
  1222 
  1223 	if (di->Status() == ENfeEncrypting)
  1223 
  1224 		{
  1224 	// just forward the request if the drive is not encrypted
  1225 		di->iEncryptEndPos = di->iEntry.iPartitionBaseAddr + di->iEntry.iPartitionLen;
  1225 	if (di->Status() == ENfeDecrypted)
  1226 		SetStatus(*di,  ENfeEncrypted);
  1226 		return ForwardRequest(aReq);
  1227 		__KTRACE_PRINT(Kern::Printf("NFE%d: HandleFormat() , Setting status to %s", iInstance, DriveStatus(di->Status())));
  1227 
  1228 		}
  1228 	// otherwise create a buffer containing NULLs, encrypt it and write that to the attached drive
  1229 
  1229 	while(remainingLength && r == KErrNone)
  1230 	if (currentPos >= di->iEntry.iPartitionBaseAddr && 
  1230 		{
  1231 		currentPos < di->iEntry.iPartitionBaseAddr + KSectorSize && 
  1231 		TInt64 currentLength = (remainingLength <= KBufSize ? remainingLength : KBufSize);
  1232 		di->IsUDADrive() &&
  1232 
  1233 		di->Status() == ENfeEncrypted)
  1233 		TBool encryptBuffer = AdjustRequest(di, currentPos, currentLength);
  1234 		{
  1234 
  1235 		__KTRACE_PRINT(Kern::Printf("NFE%d: Write to sector #0 detected", iInstance));
  1235 		memclr(iBuffer, KBufSize);
  1236 		di->iUniqueID = 0;	// undefined
  1236 		TPtr8 des(iBuffer,KBufSize,KBufSize);
  1237 		__KTRACE_PRINT(Kern::Printf("NFE%d: Setting Volume ID to %08X", iInstance, di->iUniqueID ));
  1237 
  1238 		}
  1238 		if (encryptBuffer)
  1239 
  1239 			EncryptBuffer(des);
  1240 
  1240 		
  1241 	return ForwardRequest(aReq);
  1241 		r = Write(di->iLocalDriveNum, currentPos, (TLinAddr) iBuffer, I64LOW(currentLength));
       
  1242 		if(r != KErrNone)
       
  1243 			break;
       
  1244 
       
  1245 		remainingLength-= currentLength;
       
  1246 		currentPos+= currentLength;
       
  1247 		}
       
  1248 
       
  1249 	return r == KErrNone ? KErrCompletion : r;
  1242 	}
  1250 	}
  1243 
  1251 
  1244 
  1252 
  1245 void DMediaDriverNFE::EncryptBuffer(TDes8& aBuffer)
  1253 void DMediaDriverNFE::EncryptBuffer(TDes8& aBuffer)
  1246 	{
  1254 	{
  1353 
  1361 
  1354 
  1362 
  1355 TBool DMediaDriverNFE::ValidBootSector(TUint8* aBuffer)
  1363 TBool DMediaDriverNFE::ValidBootSector(TUint8* aBuffer)
  1356 	{
  1364 	{
  1357 	if (aBuffer[0] == 0xEB || aBuffer[0] == 0xE9)
  1365 	if (aBuffer[0] == 0xEB || aBuffer[0] == 0xE9)
  1358 		{
       
  1359 		return ETrue;
  1366 		return ETrue;
  1360 		}
       
  1361 	else
  1367 	else
  1362 		{
       
  1363 		return EFalse;
  1368 		return EFalse;
  1364 		}
       
  1365 	}
  1369 	}
  1366 
  1370 
  1367 
  1371 
  1368 TUint32 DMediaDriverNFE::VolumeId(TUint8* aBuffer)
  1372 TUint32 DMediaDriverNFE::VolumeId(TUint8* aBuffer)
  1369 	{
  1373 	{
  1404 
  1408 
  1405 	__KTRACE_PRINT(Kern::Printf("NFE%d: fatBootSectorStatus %d", iInstance, fatBootSectorStatus));
  1409 	__KTRACE_PRINT(Kern::Printf("NFE%d: fatBootSectorStatus %d", iInstance, fatBootSectorStatus));
  1406 
  1410 
  1407 	// Find out whether the volume has changed
  1411 	// Find out whether the volume has changed
  1408 	TUint32 uniqueID = VolumeId(iBuffer);   
  1412 	TUint32 uniqueID = VolumeId(iBuffer);   
  1409 	TBool volumeChanged = (aDi.iUniqueID != 0) && (uniqueID != aDi.iUniqueID);
  1413 	TBool volumeChanged = uniqueID != aDi.iUniqueID;
  1410 	__KTRACE_PRINT(Kern::Printf("NFE%d: Old Volume ID %08X", iInstance, aDi.iUniqueID));
  1414 	__KTRACE_PRINT(Kern::Printf("NFE%d: Old Volume ID %08X", iInstance, aDi.iUniqueID));
  1411 	__KTRACE_PRINT(Kern::Printf("NFE%d: New Volume ID %08X", iInstance, uniqueID));
  1415 	__KTRACE_PRINT(Kern::Printf("NFE%d: New Volume ID %08X", iInstance, uniqueID));
  1412 	__KTRACE_PRINT(Kern::Printf("NFE%d: volumeChanged %d", iInstance, volumeChanged));
  1416 	__KTRACE_PRINT(Kern::Printf("NFE%d: volumeChanged %d", iInstance, volumeChanged));
  1413 	aDi.iUniqueID = uniqueID;
  1417 	aDi.iUniqueID = uniqueID;
  1414 
  1418 
  1531 	if (di->iDriveFinalised)
  1535 	if (di->iDriveFinalised)
  1532 		{
  1536 		{
  1533 	    __KTRACE_PRINT(Kern::Printf("HandleDiskContent aborting as drive has been finalised", iInstance));
  1537 	    __KTRACE_PRINT(Kern::Printf("HandleDiskContent aborting as drive has been finalised", iInstance));
  1534 		return KErrNone;
  1538 		return KErrNone;
  1535 		}
  1539 		}
       
  1540 
       
  1541 //	TInt KBackgroundPriority = 7;						//*test*
       
  1542 //	Kern::SetThreadPriority(KBackgroundPriority);		//*test*
  1536 
  1543 
  1537 	TInt r = KErrNone;
  1544 	TInt r = KErrNone;
  1538 	for (;;)
  1545 	for (;;)
  1539 		{
  1546 		{
  1540 		// If we've finished encryting this drive, change the state and move on to the next drive
  1547 		// If we've finished encryting this drive, change the state and move on to the next drive
  1618 
  1625 
  1619 	// If not completed, start the idle timer & try again later
  1626 	// If not completed, start the idle timer & try again later
  1620 	if (r != KErrCompletion)
  1627 	if (r != KErrCompletion)
  1621 		iIdleTimer.OneShot(NKern::TimerTicks(KNotBusyInterval));
  1628 		iIdleTimer.OneShot(NKern::TimerTicks(KNotBusyInterval));
  1622 
  1629 
       
  1630 //	Kern::SetThreadPriority(KNfeThreadPriority);	//*test*
       
  1631 	
  1623 	return r;
  1632 	return r;
  1624 	}
  1633 	}
  1625 
  1634 
  1626 
  1635 
  1627 
  1636