os/kernelhwsrv/kernel/eka/drivers/medmmc/emmcptn.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// Partition Management for Embedded MMC devices
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <drivers/emmcptn.h>
sl@0
    19
#include "OstTraceDefinitions.h"
sl@0
    20
#ifdef OST_TRACE_COMPILER_IN_USE
sl@0
    21
#include "locmedia_ost.h"
sl@0
    22
#ifdef __VC32__
sl@0
    23
#pragma warning(disable: 4127) // disabling warning "conditional expression is constant"
sl@0
    24
#endif
sl@0
    25
#include "emmcptnTraces.h"
sl@0
    26
#endif
sl@0
    27
sl@0
    28
sl@0
    29
const TInt KDiskSectorShift=9;
sl@0
    30
sl@0
    31
class DLegacyEMMCPartitionInfo : public DEMMCPartitionInfo
sl@0
    32
	{
sl@0
    33
public:
sl@0
    34
	 DLegacyEMMCPartitionInfo();
sl@0
    35
	~DLegacyEMMCPartitionInfo();
sl@0
    36
public:
sl@0
    37
	virtual TInt Initialise(DMediaDriver* aDriver);
sl@0
    38
	virtual TInt PartitionInfo(TPartitionInfo& anInfo, const TMMCCallBack& aCallBack);
sl@0
    39
	virtual TInt PartitionCaps(TLocDrv& aDrive, TDes8& aInfo);
sl@0
    40
	
sl@0
    41
protected:
sl@0
    42
	void SetPartitionEntry(TPartitionEntry* aEntry, TUint aFirstSector, TUint aNumSectors);
sl@0
    43
	
sl@0
    44
private:
sl@0
    45
	static void SessionEndCallBack(TAny* aSelf);
sl@0
    46
		   void DoSessionEndCallBack();
sl@0
    47
	virtual TInt DecodePartitionInfo();
sl@0
    48
	
sl@0
    49
protected:
sl@0
    50
	DMediaDriver*   iDriver;
sl@0
    51
	TPartitionInfo* iPartitionInfo;
sl@0
    52
	TMMCCallBack	iSessionEndCallBack;
sl@0
    53
	TMMCCallBack 	iCallBack;         // Where to report the PartitionInfo completion
sl@0
    54
	DMMCSession*	iSession;
sl@0
    55
	TMMCard*		iCard;
sl@0
    56
	TUint8*			iIntBuf;
sl@0
    57
	};
sl@0
    58
sl@0
    59
DLegacyEMMCPartitionInfo::DLegacyEMMCPartitionInfo()
sl@0
    60
  : iSessionEndCallBack(DLegacyEMMCPartitionInfo::SessionEndCallBack, this)
sl@0
    61
	{
sl@0
    62
	OstTraceFunctionEntry0( DLEGACYEMMCPARTITIONINFO_DLEGACYEMMCPARTITIONINFO_ENTRY );
sl@0
    63
	}
sl@0
    64
sl@0
    65
DLegacyEMMCPartitionInfo::~DLegacyEMMCPartitionInfo()
sl@0
    66
	{
sl@0
    67
	OstTraceFunctionEntry0( DLEGACYEMMCPARTITIONINFO_DESTRUCTOR_ENTRY );
sl@0
    68
	delete iSession;
sl@0
    69
	OstTraceFunctionExit0( DLEGACYEMMCPARTITIONINFO_DESTRUCTOR_EXIT );
sl@0
    70
	}
sl@0
    71
sl@0
    72
TInt DLegacyEMMCPartitionInfo::Initialise(DMediaDriver* aDriver)
sl@0
    73
	{
sl@0
    74
	OstTraceFunctionEntry1( DLEGACYEMMCPARTITIONINFO_INITIALISE_ENTRY, this );
sl@0
    75
	iDriver = aDriver;
sl@0
    76
sl@0
    77
	DMMCSocket* socket = ((DMMCSocket*)((DPBusPrimaryMedia*)(iDriver->iPrimaryMedia))->iSocket);
sl@0
    78
	if(socket == NULL)
sl@0
    79
	    {
sl@0
    80
		OstTraceFunctionExitExt( DLEGACYEMMCPARTITIONINFO_INITIALISE_EXIT1, this, KErrNoMemory );
sl@0
    81
		return KErrNoMemory;
sl@0
    82
	    }
sl@0
    83
sl@0
    84
	DMMCStack* stack = socket->Stack(0);
sl@0
    85
	iCard = stack->CardP(((DPBusPrimaryMedia*)(iDriver->iPrimaryMedia))->iSlotNumber);
sl@0
    86
	
sl@0
    87
	iSession = stack->AllocSession(iSessionEndCallBack);
sl@0
    88
	if (iSession == NULL)
sl@0
    89
		return KErrNoMemory;
sl@0
    90
sl@0
    91
	iSession->SetStack(stack);
sl@0
    92
	iSession->SetCard(iCard);
sl@0
    93
sl@0
    94
	// this gets used before any access
sl@0
    95
	TInt bufLen, minorBufLen;
sl@0
    96
	stack->BufferInfo(iIntBuf, bufLen, minorBufLen);
sl@0
    97
sl@0
    98
	OstTraceFunctionExitExt( DLEGACYEMMCPARTITIONINFO_INITIALISE_EXIT2, this, KErrNone );
sl@0
    99
	return KErrNone;
sl@0
   100
	}
sl@0
   101
sl@0
   102
TInt DLegacyEMMCPartitionInfo::PartitionInfo(TPartitionInfo& anInfo, const TMMCCallBack& aCallBack)
sl@0
   103
	{
sl@0
   104
	OstTraceFunctionEntry1( DLEGACYEMMCPARTITIONINFO_PARTITIONINFO_ENTRY, this );
sl@0
   105
	iPartitionInfo = &anInfo;
sl@0
   106
	iCallBack = aCallBack;
sl@0
   107
	// If media driver is persistent (see EMediaDriverPersistent), 
sl@0
   108
	// the card may have changed since last power down, so reset CID
sl@0
   109
	iSession->SetCard(iCard);
sl@0
   110
	
sl@0
   111
	iSession->SetupCIMReadBlock(0, iIntBuf);
sl@0
   112
sl@0
   113
	TInt r = iDriver->InCritical();
sl@0
   114
	if (r == KErrNone)
sl@0
   115
		r = iSession->Engage();
sl@0
   116
sl@0
   117
	if(r != KErrNone)
sl@0
   118
		iDriver->EndInCritical();
sl@0
   119
	
sl@0
   120
	OstTraceFunctionExitExt( DLEGACYEMMCPARTITIONINFO_PARTITIONINFO_EXIT, this, r );
sl@0
   121
	return r;
sl@0
   122
	}
sl@0
   123
sl@0
   124
TInt DLegacyEMMCPartitionInfo::PartitionCaps(TLocDrv& aDrive, TDes8& aInfo)
sl@0
   125
	{
sl@0
   126
	 OstTraceFunctionEntry1( DLEGACYEMMCPARTITIONINFO_PARTITIONCAPS_ENTRY, this );
sl@0
   127
	 TLocalDriveCapsV6Buf& Info = static_cast< TLocalDriveCapsV6Buf&> (aInfo);
sl@0
   128
	
sl@0
   129
	// is this query for the swap partition ?
sl@0
   130
	if (aDrive.iPartitionType == KPartitionTypePagedData)
sl@0
   131
		{
sl@0
   132
		Info().iFileSystemId = KDriveFileNone;
sl@0
   133
		Info().iDriveAtt|= KDriveAttHidden;
sl@0
   134
		}
sl@0
   135
sl@0
   136
	// is this query for the ROFS partition ?
sl@0
   137
	if (aDrive.iPartitionType == KPartitionTypeRofs)
sl@0
   138
		{
sl@0
   139
		Info().iFileSystemId = KDriveFileSysROFS;
sl@0
   140
		Info().iMediaAtt&= ~KMediaAttFormattable;
sl@0
   141
		Info().iMediaAtt|= KMediaAttWriteProtected;
sl@0
   142
		}
sl@0
   143
	
sl@0
   144
	// is this query for the ROM partition ?
sl@0
   145
	if (aDrive.iPartitionType == KPartitionTypeROM)
sl@0
   146
		{
sl@0
   147
		Info().iFileSystemId = KDriveFileNone;
sl@0
   148
		Info().iMediaAtt&= ~KMediaAttFormattable;
sl@0
   149
		Info().iMediaAtt|= KMediaAttWriteProtected;
sl@0
   150
		}
sl@0
   151
	
sl@0
   152
	OstTraceFunctionExitExt( DLEGACYEMMCPARTITIONINFO_PARTITIONCAPS_EXIT, this, KErrNone );
sl@0
   153
	return KErrNone;
sl@0
   154
	}
sl@0
   155
sl@0
   156
void DLegacyEMMCPartitionInfo::SessionEndCallBack(TAny* aSelf)
sl@0
   157
	{
sl@0
   158
	OstTraceFunctionEntry0( DLEGACYEMMCPARTITIONINFO_SESSIONENDCALLBACK_ENTRY );
sl@0
   159
	DLegacyEMMCPartitionInfo& self = *static_cast<DLegacyEMMCPartitionInfo*>(aSelf);
sl@0
   160
	self.DoSessionEndCallBack();
sl@0
   161
	OstTraceFunctionExit0( DLEGACYEMMCPARTITIONINFO_SESSIONENDCALLBACK_EXIT );
sl@0
   162
	}
sl@0
   163
sl@0
   164
void DLegacyEMMCPartitionInfo::DoSessionEndCallBack()
sl@0
   165
	{
sl@0
   166
	OstTraceFunctionEntry1( DLEGACYEMMCPARTITIONINFO_DOSESSIONENDCALLBACK_ENTRY, this );
sl@0
   167
	iDriver->EndInCritical();
sl@0
   168
sl@0
   169
	TInt r = iSession->EpocErrorCode();
sl@0
   170
sl@0
   171
	if (r == KErrNone)
sl@0
   172
		r = DecodePartitionInfo();
sl@0
   173
sl@0
   174
	iDriver->PartitionInfoComplete(r == KErrNone ? r : KErrNotReady);
sl@0
   175
	OstTraceFunctionExit1( DLEGACYEMMCPARTITIONINFO_DOSESSIONENDCALLBACK_EXIT, this );
sl@0
   176
	}
sl@0
   177
sl@0
   178
TInt DLegacyEMMCPartitionInfo::DecodePartitionInfo()
sl@0
   179
//
sl@0
   180
// decode partition info that was read into internal buffer 
sl@0
   181
//
sl@0
   182
	{
sl@0
   183
	OstTraceFunctionEntry1( DLEGACYEMMCPARTITIONINFO_DECODEPARTITIONINFO_ENTRY, this );
sl@0
   184
	TUint partitionCount=iPartitionInfo->iPartitionCount=0;
sl@0
   185
	TInt defaultPartitionNumber=-1;
sl@0
   186
	TMBRPartitionEntry* pe;
sl@0
   187
	const TUint KMBRFirstPartitionOffsetAligned = KMBRFirstPartitionOffset & ~3;
sl@0
   188
	TInt i;
sl@0
   189
sl@0
   190
	// Read of the first sector successful so check for a Master Boot Record
sl@0
   191
	if (*(TUint16*)(&iIntBuf[KMBRSignatureOffset])!=0xAA55)
sl@0
   192
	    {
sl@0
   193
		// If no valid signature give up now, No way to re-format an internal drive correctly
sl@0
   194
		OstTraceFunctionExitExt( DLEGACYEMMCPARTITIONINFO_DECODEPARTITIONINFO_EXIT1, this, KErrCorrupt );
sl@0
   195
		return KErrCorrupt;
sl@0
   196
	    }
sl@0
   197
sl@0
   198
	__ASSERT_COMPILE(KMBRFirstPartitionOffsetAligned + KMBRMaxPrimaryPartitions * sizeof(TMBRPartitionEntry) <= KMBRSignatureOffset);
sl@0
   199
sl@0
   200
	memmove(&iIntBuf[0], &iIntBuf[2],
sl@0
   201
		KMBRFirstPartitionOffsetAligned + KMBRMaxPrimaryPartitions * sizeof(TMBRPartitionEntry)); 
sl@0
   202
sl@0
   203
sl@0
   204
	for (i=0, pe = (TMBRPartitionEntry*)(&iIntBuf[KMBRFirstPartitionOffsetAligned]);
sl@0
   205
		pe->iPartitionType != 0 && i < KMaxPartitionEntries; i++,pe--)
sl@0
   206
		{
sl@0
   207
		if (pe->IsDefaultBootPartition())
sl@0
   208
			{
sl@0
   209
			SetPartitionEntry(&iPartitionInfo->iEntry[0],pe->iFirstSector,pe->iNumSectors);
sl@0
   210
			defaultPartitionNumber=i;
sl@0
   211
			partitionCount++;
sl@0
   212
			break;
sl@0
   213
			}
sl@0
   214
		}
sl@0
   215
sl@0
   216
	// Now add any other partitions
sl@0
   217
	for (i=0, pe = (TMBRPartitionEntry*)(&iIntBuf[KMBRFirstPartitionOffsetAligned]);
sl@0
   218
		pe->iPartitionType != 0 && i < KMaxPartitionEntries; i++,pe--)
sl@0
   219
		{
sl@0
   220
		if (defaultPartitionNumber==i)
sl@0
   221
			{
sl@0
   222
			// Already sorted
sl@0
   223
			}
sl@0
   224
sl@0
   225
		// FAT partition ?
sl@0
   226
		else if (pe->IsValidDosPartition() || pe->IsValidFAT32Partition() || pe->IsValidExFATPartition())
sl@0
   227
			{
sl@0
   228
			SetPartitionEntry(&iPartitionInfo->iEntry[partitionCount],pe->iFirstSector,pe->iNumSectors);
sl@0
   229
			__KTRACE_OPT(KLOCDPAGING, Kern::Printf("Mmc: FAT partition found at sector #%u", pe->iFirstSector));
sl@0
   230
			OstTrace1(TRACE_INTERNALS, DLEGACYEMMCPARTITIONINFO_DECODEPARTITIONINFO_FAT, "FAT partition found at sector #%x", pe->iFirstSector);
sl@0
   231
			partitionCount++;
sl@0
   232
			}
sl@0
   233
sl@0
   234
		else if (pe->iPartitionType == KPartitionTypeROM)
sl@0
   235
			{
sl@0
   236
			TPartitionEntry& partitionEntry = iPartitionInfo->iEntry[partitionCount];
sl@0
   237
			SetPartitionEntry(&iPartitionInfo->iEntry[partitionCount],pe->iFirstSector,pe->iNumSectors);
sl@0
   238
			partitionEntry.iPartitionType = pe->iPartitionType;
sl@0
   239
			partitionCount++;				 
sl@0
   240
sl@0
   241
			__KTRACE_OPT(KLOCDPAGING, Kern::Printf("Mmc: KPartitionTypeROM found at sector #%u", pe->iFirstSector));
sl@0
   242
			OstTrace1(TRACE_INTERNALS, DLEGACYEMMCPARTITIONINFO_DECODEPARTITIONINFO_ROM, "KPartitionTypeROM found at sector #%x", pe->iFirstSector);
sl@0
   243
			}
sl@0
   244
sl@0
   245
		// ROFS partition ?
sl@0
   246
		else if (pe->iPartitionType == KPartitionTypeRofs)
sl@0
   247
			{
sl@0
   248
			
sl@0
   249
// Don't expose this for normal operation only boot?			
sl@0
   250
			TPartitionEntry& partitionEntry = iPartitionInfo->iEntry[partitionCount];
sl@0
   251
			SetPartitionEntry(&iPartitionInfo->iEntry[partitionCount],pe->iFirstSector,pe->iNumSectors);
sl@0
   252
			partitionEntry.iPartitionType = pe->iPartitionType;
sl@0
   253
			__KTRACE_OPT(KLOCDPAGING, Kern::Printf("Mmc: KPartitionTypeRofs found at sector #%u", pe->iFirstSector));
sl@0
   254
			OstTrace1(TRACE_INTERNALS, DLEGACYEMMCPARTITIONINFO_DECODEPARTITIONINFO_ROFS, "KPartitionTypeRofs found at sector #%x", pe->iFirstSector);
sl@0
   255
			partitionCount++;
sl@0
   256
			}
sl@0
   257
 
sl@0
   258
		// Swap partition ?
sl@0
   259
		else if (pe->iPartitionType == KPartitionTypePagedData)
sl@0
   260
			{
sl@0
   261
			__KTRACE_OPT(KLOCDPAGING, Kern::Printf("Mmc: KPartitionTypePagedData found at sector #%u", pe->iFirstSector));
sl@0
   262
			OstTrace1(TRACE_INTERNALS, DLEGACYEMMCPARTITIONINFO_DECODEPARTITIONINFO_PAGED, "KPartitionTypeRofs found at sector #%x", pe->iFirstSector);
sl@0
   263
			TPartitionEntry& partitionEntry = iPartitionInfo->iEntry[partitionCount];
sl@0
   264
			SetPartitionEntry(&iPartitionInfo->iEntry[partitionCount],pe->iFirstSector,pe->iNumSectors);
sl@0
   265
			partitionEntry.iPartitionType = pe->iPartitionType;
sl@0
   266
			partitionCount++;
sl@0
   267
			}
sl@0
   268
		}
sl@0
   269
sl@0
   270
	// Check the validity of the partition address boundaries
sl@0
   271
	// If there is any MBR errors
sl@0
   272
	if(partitionCount > 0)
sl@0
   273
		{
sl@0
   274
		const TInt64 deviceSize = iCard->DeviceSize64();
sl@0
   275
		TPartitionEntry& part = iPartitionInfo->iEntry[partitionCount - 1];
sl@0
   276
		// Check that the card address space boundary is not exceeded by the last partition
sl@0
   277
		if(part.iPartitionBaseAddr + part.iPartitionLen > deviceSize)
sl@0
   278
			{
sl@0
   279
			__KTRACE_OPT(KPBUSDRV, Kern::Printf("Mmc: MBR partition exceeds card memory space"));
sl@0
   280
			OstTraceFunctionExitExt( DLEGACYEMMCPARTITIONINFO_DECODEPARTITIONINFO_EXIT2, this, KErrCorrupt );
sl@0
   281
			return KErrCorrupt;
sl@0
   282
			}
sl@0
   283
		
sl@0
   284
		// More than one partition. Go through all of them
sl@0
   285
		if (partitionCount > 0)
sl@0
   286
			{
sl@0
   287
			for(i=partitionCount-1; i>0; i--)
sl@0
   288
				{
sl@0
   289
				const TPartitionEntry& curr = iPartitionInfo->iEntry[i];
sl@0
   290
				TPartitionEntry& prev = iPartitionInfo->iEntry[i-1];
sl@0
   291
				// Check if partitions overlap
sl@0
   292
				if(curr.iPartitionBaseAddr < (prev.iPartitionBaseAddr + prev.iPartitionLen))
sl@0
   293
					{
sl@0
   294
					__KTRACE_OPT(KPBUSDRV, Kern::Printf("Mmc: Overlapping partitions"));
sl@0
   295
					OstTraceFunctionExitExt( DLEGACYEMMCPARTITIONINFO_DECODEPARTITIONINFO_EXIT3, this, KErrCorrupt );
sl@0
   296
					return KErrCorrupt;
sl@0
   297
					}
sl@0
   298
				}
sl@0
   299
			}
sl@0
   300
		}
sl@0
   301
sl@0
   302
	if (defaultPartitionNumber==(-1) && partitionCount==0)
sl@0
   303
		{
sl@0
   304
		__KTRACE_OPT(KPBUSDRV, Kern::Printf("No Valid Partitions Found!"));
sl@0
   305
		OstTraceFunctionExitExt( DLEGACYEMMCPARTITIONINFO_DECODEPARTITIONINFO_EXIT4, this, KErrCorrupt );
sl@0
   306
		return KErrCorrupt;
sl@0
   307
		}
sl@0
   308
sl@0
   309
	iPartitionInfo->iPartitionCount=partitionCount;
sl@0
   310
	iPartitionInfo->iMediaSizeInBytes=iCard->DeviceSize64();
sl@0
   311
sl@0
   312
#ifdef _DEBUG
sl@0
   313
	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<Mmc:PartitionInfo (C:%d)",partitionCount));
sl@0
   314
	for (TUint x=0; x<partitionCount; x++)
sl@0
   315
		__KTRACE_OPT(KPBUSDRV, Kern::Printf("     Partition%d (B:%xH L:%xH)",x,I64LOW(iPartitionInfo->iEntry[x].iPartitionBaseAddr),I64LOW(iPartitionInfo->iEntry[x].iPartitionLen)));
sl@0
   316
#endif
sl@0
   317
sl@0
   318
	//Notify medmmc that partitioninfo is complete.
sl@0
   319
	iCallBack.CallBack();
sl@0
   320
	
sl@0
   321
	OstTraceFunctionExitExt( DLEGACYEMMCPARTITIONINFO_DECODEPARTITIONINFO_EXIT5, this, KErrNone );
sl@0
   322
	return KErrNone;
sl@0
   323
	}
sl@0
   324
sl@0
   325
sl@0
   326
void DLegacyEMMCPartitionInfo::SetPartitionEntry(TPartitionEntry* aEntry, TUint aFirstSector, TUint aNumSectors)
sl@0
   327
//
sl@0
   328
// auxiliary static function to record partition information in TPartitionEntry object
sl@0
   329
//
sl@0
   330
	{
sl@0
   331
	OstTraceFunctionEntryExt( DLEGACYEMMCPARTITIONINFO_SETPARTITIONENTRY_ENTRY, this );
sl@0
   332
	aEntry->iPartitionBaseAddr=aFirstSector;
sl@0
   333
	aEntry->iPartitionBaseAddr<<=KDiskSectorShift;
sl@0
   334
	aEntry->iPartitionLen=aNumSectors;
sl@0
   335
	aEntry->iPartitionLen<<=KDiskSectorShift;
sl@0
   336
	aEntry->iPartitionType=KPartitionTypeFAT12;
sl@0
   337
	OstTraceFunctionExit1( DLEGACYEMMCPARTITIONINFO_SETPARTITIONENTRY_EXIT, this );
sl@0
   338
	}
sl@0
   339
sl@0
   340
// End - DLegacyEMMCPartitionInfo
sl@0
   341
sl@0
   342
sl@0
   343
EXPORT_C DEMMCPartitionInfo* CreateEmmcPartitionInfo()
sl@0
   344
	{
sl@0
   345
	OstTraceFunctionEntry0( _CREATEEMMCPARTITIONINFO_ENTRY );
sl@0
   346
	return new DLegacyEMMCPartitionInfo;
sl@0
   347
	}
sl@0
   348
sl@0
   349
DECLARE_STANDARD_EXTENSION()
sl@0
   350
	{
sl@0
   351
	return KErrNone;
sl@0
   352
	}