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