stvariants/montblanc/src/soc/peripheral/mmc/8500ed/mmc/emmcptn.cpp
changeset 14 cf4c5641c6dd
parent 12 a80bfc23ff0e
child 15 ac35e54b1f85
child 17 ce191d16bb9a
child 19 703c56cf2669
equal deleted inserted replaced
12:a80bfc23ff0e 14:cf4c5641c6dd
     1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Partition Management for Embedded MMC devices
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "emmcptn.h"
       
    19 
       
    20 #define ST_FLASHER
       
    21 #define BGAHSMMC_PI_STR_SIZE sizeof( BGAHSMMC_PI_STR )
       
    22 
       
    23 //- Constants ---------------------------------------------------------------
       
    24 
       
    25 const TUint8  KMaxNbrOfTocItems     = 16; //condition false in decode partition info function 32; //should be 16 (32*16=512)
       
    26 const TUint8  KXMaxNbrOfTocItems    = 16;
       
    27 const TUint32 KEndOfToc             = 0xFFFFFFFFUL;
       
    28 const TInt  KMaxItemNameLen   		= 12;
       
    29 
       
    30 const TUint32 KMegaByte 			= 1024 * 1024;
       
    31 const TUint32 KEraseStepSize 		= 4 * KMegaByte;
       
    32 
       
    33 // for Emmc(S) min erase is 256KB for Emmc(T) min erase is 512KB
       
    34 const TUint32 KEraseMinSize			= 512 * 1024;
       
    35 const TInt KDiskSectorSize			=512;
       
    36 const TInt KDiskSectorShift			=9;
       
    37 
       
    38 // UnistoreII definitions
       
    39 #define VOLNUM							0			
       
    40 #define SECTOR_SIZE						512
       
    41 #define TWO_SECTOR_SIZE					1024
       
    42 
       
    43 struct STocItem
       
    44 	{
       
    45 	TUint32 iByteStartAddress;
       
    46 	TUint32 iByteSize;
       
    47 	TUint32 iFlags;
       
    48 	TUint32 iAlign;
       
    49 	TUint32 iLoadAddress;
       
    50 	TText8  iFileName[KMaxItemNameLen];
       
    51 	};
       
    52 
       
    53 
       
    54 class Toc
       
    55     {
       
    56     public:
       
    57         TInt Init();
       
    58         TInt GetItem(const TText8* aItemName, STocItem& aItem);
       
    59         TInt GetItemEx(const TText8* aName, STocItem& aItem);
       
    60 	public:
       
    61 		STocItem iTOC[KMaxNbrOfTocItems];
       
    62 		STocItem iEmmcPartitionTable[4];		
       
    63 		TUint32  iTocStartSector;
       
    64 		TUint64  iUserAreaInBytes;
       
    65 		TInt 	 iPartitionCount;
       
    66 		TBool    iEMMCPtnUpdate;
       
    67 		
       
    68 #if defined(_VERSION_INFO)
       
    69 		TUint32 iVersionInfoItems;
       
    70 		TVersionInfoItem iVersionInfo[KMaxSectionItems];
       
    71 #endif
       
    72 
       
    73 	private:
       
    74         TInt GetItemX(const TText8* aItemName, STocItem& aItem);
       
    75 
       
    76     };
       
    77 
       
    78 
       
    79 class DLegacyEMMCPartitionInfo : public DEMMCPartitionInfo
       
    80 	{
       
    81 public:
       
    82 	 DLegacyEMMCPartitionInfo();
       
    83 	~DLegacyEMMCPartitionInfo();
       
    84 	Toc *iTocPtr;
       
    85 
       
    86 
       
    87 public:
       
    88 	virtual TInt Initialise(DMediaDriver* aDriver);
       
    89 	virtual TInt PartitionInfo(TPartitionInfo& anInfo, const TMMCCallBack& aCallBack);
       
    90 	virtual TInt PartitionCaps(TLocDrv& aDrive, TDes8& aInfo);
       
    91 	
       
    92 protected:
       
    93 	void SetPartitionEntry(TPartitionEntry* aEntry, TUint aFirstSector, TUint aNumSectors);
       
    94 	
       
    95 private:
       
    96 	static void SessionEndCallBack(TAny* aSelf);
       
    97 		   void DoSessionEndCallBack();
       
    98 	virtual TInt DecodePartitionInfo();
       
    99 	
       
   100 protected:
       
   101 	DMediaDriver*   iDriver;
       
   102 	TPartitionInfo* iPartitionInfo;
       
   103 	TMMCCallBack	iSessionEndCallBack;
       
   104 	TMMCCallBack 	iCallBack;         // Where to report the PartitionInfo completion
       
   105 	DMMCSession*	iSession;
       
   106 	TMMCard*		iCard;
       
   107 	TUint8*			iIntBuf;
       
   108 	};
       
   109 
       
   110 DLegacyEMMCPartitionInfo::DLegacyEMMCPartitionInfo()
       
   111   : iSessionEndCallBack(DLegacyEMMCPartitionInfo::SessionEndCallBack, this)
       
   112 	{
       
   113 	}
       
   114 
       
   115 DLegacyEMMCPartitionInfo::~DLegacyEMMCPartitionInfo()
       
   116 	{
       
   117 	delete iSession;
       
   118 	}
       
   119 
       
   120 TInt DLegacyEMMCPartitionInfo::Initialise(DMediaDriver* aDriver)
       
   121 	{
       
   122 	iDriver = aDriver;
       
   123 
       
   124 	DMMCSocket* socket = ((DMMCSocket*)((DPBusPrimaryMedia*)(iDriver->iPrimaryMedia))->iSocket);
       
   125 	if(socket == NULL)
       
   126 		return(KErrNoMemory);
       
   127 
       
   128 	DMMCStack* stack = socket->Stack(0);
       
   129 	iCard = stack->CardP(((DPBusPrimaryMedia*)(iDriver->iPrimaryMedia))->iSlotNumber);
       
   130 	
       
   131 	iSession = stack->AllocSession(iSessionEndCallBack);
       
   132 	if (iSession == NULL)
       
   133 		return(KErrNoMemory);
       
   134 
       
   135 	iSession->SetStack(stack);
       
   136 	iSession->SetCard(iCard);
       
   137 
       
   138 	// this gets used before any access
       
   139 	TInt bufLen, minorBufLen;
       
   140 	stack->BufferInfo(iIntBuf, bufLen, minorBufLen);
       
   141 
       
   142 	return(KErrNone);
       
   143 	}
       
   144 
       
   145 TInt DLegacyEMMCPartitionInfo::PartitionInfo(TPartitionInfo& anInfo, const TMMCCallBack& aCallBack)
       
   146 	{
       
   147 	iPartitionInfo = &anInfo;
       
   148 	iCallBack = aCallBack;
       
   149 	// If media driver is persistent (see EMediaDriverPersistent), 
       
   150 	// the card may have changed since last power down, so reset CID
       
   151 	iSession->SetCard(iCard);
       
   152 	
       
   153 	TUint32 TocOffset =0; //TOC is on first Block
       
   154 
       
   155 #ifndef ST_FLASHER
       
   156 	TocOffset = 1280; //TOC mapped 0x000A0000 bytes offset i.e. 1280 blocks
       
   157 #endif //!ST_FLASHER
       
   158 
       
   159 	iSession->SetupCIMReadBlock(TocOffset, iIntBuf);	// aBlocks = 1
       
   160 
       
   161 	TInt r = iDriver->InCritical();
       
   162 	if (r == KErrNone)
       
   163 		r = iSession->Engage();
       
   164 
       
   165 	if(r != KErrNone)
       
   166 		iDriver->EndInCritical();
       
   167 	
       
   168 	return(r);
       
   169 	}
       
   170 
       
   171 TInt DLegacyEMMCPartitionInfo::PartitionCaps(TLocDrv& aDrive, TDes8& aInfo)
       
   172 	{
       
   173 	 TLocalDriveCapsV6Buf& Info = static_cast< TLocalDriveCapsV6Buf&> (aInfo);
       
   174 	__KTRACE_OPT(KPBUSDRV,Kern::Printf(">>iPartitionType=%d",aDrive.iPartitionType));	
       
   175 	
       
   176 
       
   177 	if (aDrive.iPartitionType == KPartitionTypeRofs)
       
   178 		{
       
   179 		__KTRACE_OPT(KPBUSDRV,Kern::Printf("eMMC proto: Caps for ROFS,drive NO =%d",aDrive.iDriveNumber));
       
   180 
       
   181 		Info().iType 			= EMediaHardDisk;		
       
   182 		Info().iDriveAtt 		= KDriveAttLocal | KDriveAttInternal;
       
   183 		Info().iFileSystemId  	= KDriveFileSysROFS;
       
   184 		Info().iPartitionType 	= KPartitionTypeRofs;
       
   185 		Info().iMediaAtt      	|= KMediaAttWriteProtected;
       
   186 		Info().iSize 		 	= TUint32(iTocPtr->iEmmcPartitionTable[1].iByteSize);
       
   187 
       
   188 		}
       
   189 	else if	(aDrive.iPartitionType == KPartitionTypeEmpty) //for CoreOS
       
   190 		{
       
   191 
       
   192 		__KTRACE_OPT(KPBUSDRV,Kern::Printf("eMMC proto: Caps for EmptyP,drive NO =%d",aDrive.iDriveNumber));
       
   193 		
       
   194 		Info().iType 			= EMediaHardDisk;
       
   195 		Info().iDriveAtt 		= KDriveAttLocal | KDriveAttInternal;	
       
   196 		Info().iFileSystemId  	= KDriveFileNone;
       
   197 		Info().iPartitionType 	= KPartitionTypeEmpty;
       
   198 		Info().iMediaAtt      	|= KMediaAttWriteProtected;
       
   199 		Info().iSize 		    = iTocPtr->iEmmcPartitionTable[0].iByteSize;
       
   200 		
       
   201 		}
       
   202 		
       
   203 	else if((aDrive.iDriveNumber == 2) && (!iTocPtr->iEMMCPtnUpdate))
       
   204 		{
       
   205 		__KTRACE_OPT(KPBUSDRV,Kern::Printf("eMMC proto: Caps iDriveNumber =%d",aDrive.iDriveNumber));
       
   206 		
       
   207 
       
   208 		Info().iType 			= EMediaHardDisk;
       
   209 		Info().iDriveAtt 		|= KDriveAttLocal | KDriveAttInternal ;
       
   210 
       
   211 		Info().iFileSystemId 	= KDriveFileSysFAT;
       
   212 		Info().iPartitionType 	= KPartitionTypeWin95FAT32;
       
   213 		Info().iMediaAtt      	|= KMediaAttFormattable;
       
   214 		Info().iSize 		 	= TUint64(iTocPtr->iUserAreaInBytes);
       
   215 		}
       
   216 		
       
   217 	// is this query for the swap partition ?
       
   218 	if (aDrive.iPartitionType == KPartitionTypePagedData)
       
   219 		{
       
   220 		Info().iFileSystemId = KDriveFileNone;
       
   221 		Info().iDriveAtt|= KDriveAttHidden;
       
   222 		}
       
   223 
       
   224 	// is this query for the ROFS partition ?
       
   225 	if (aDrive.iPartitionType == KPartitionTypeRofs)
       
   226 		{
       
   227 		Info().iFileSystemId = KDriveFileSysROFS;
       
   228 		Info().iMediaAtt&= ~KMediaAttFormattable;
       
   229 		Info().iMediaAtt|= KMediaAttWriteProtected;
       
   230 		}
       
   231 	
       
   232 	// is this query for the ROM partition ?
       
   233 	if (aDrive.iPartitionType == KPartitionTypeROM)
       
   234 		{
       
   235 		Info().iFileSystemId = KDriveFileNone;
       
   236 		Info().iMediaAtt&= ~KMediaAttFormattable;
       
   237 		Info().iMediaAtt|= KMediaAttWriteProtected;
       
   238 		}
       
   239 	
       
   240 	return KErrNone;
       
   241 	}
       
   242 
       
   243 void DLegacyEMMCPartitionInfo::SessionEndCallBack(TAny* aSelf)
       
   244 	{
       
   245 	DLegacyEMMCPartitionInfo& self = *static_cast<DLegacyEMMCPartitionInfo*>(aSelf);
       
   246 	self.DoSessionEndCallBack();
       
   247 	}
       
   248 
       
   249 void DLegacyEMMCPartitionInfo::DoSessionEndCallBack()
       
   250 	{
       
   251 	iDriver->EndInCritical();
       
   252 
       
   253 	TInt r = iSession->EpocErrorCode();
       
   254 
       
   255 	if (r == KErrNone)
       
   256 		r = DecodePartitionInfo();
       
   257 
       
   258 	iDriver->PartitionInfoComplete(r == KErrNone ? r : KErrNotReady);
       
   259 	}
       
   260 
       
   261 TInt DLegacyEMMCPartitionInfo::DecodePartitionInfo()
       
   262 //
       
   263 // decode partition info that was read into internal buffer 
       
   264 //
       
   265 	{
       
   266 	__KTRACE_OPT(KPBUSDRV,Kern::Printf(">>emcptn:DecodePartitionInfo"));
       
   267 
       
   268 	TUint partitionCount=iPartitionInfo->iPartitionCount=0;
       
   269 	TInt defaultPartitionNumber=-1;
       
   270 	TMBRPartitionEntry* pe;
       
   271 	const TUint KMBRFirstPartitionOffsetAligned = KMBRFirstPartitionOffset & ~3;
       
   272 	TInt i;
       
   273 	iTocPtr = reinterpret_cast<Toc*>(iIntBuf);
       
   274 	iTocPtr->iEMMCPtnUpdate = EFalse;
       
   275 	iTocPtr->iPartitionCount = 0;
       
   276 
       
   277 	STocItem item; 
       
   278 		
       
   279 #ifdef ST_FLASHER
       
   280 	for (TUint8 nCnt = 0; nCnt < 5; nCnt++) 
       
   281 #else
       
   282 	for (TUint8 nCnt = 0; nCnt < KMaxNbrOfTocItems; nCnt++) 
       
   283 #endif //ST_FLASHER	
       
   284 		{ 
       
   285 
       
   286 #ifdef ST_FLASHER
       
   287 		if((iTocPtr->GetItemEx((TText8*)"NORMAL", item) == KErrNone) ||(iTocPtr->GetItemEx((TText8*)"X-LOADER", item)== KErrNone))
       
   288 #else
       
   289 		if(iTocPtr->GetItemEx("SOS+CORE", item)== KErrNone) 
       
   290 #endif //ST_FLASHER
       
   291 			{
       
   292 			__KTRACE_OPT(KPBUSDRV,Kern::Printf(">>Valid TOC/PIB found"));
       
   293 			}
       
   294 		else 
       
   295 			{
       
   296 			__KTRACE_OPT(KPBUSDRV,Kern::Printf("ERROR eMMC proto: No Valid TOC/PIB structure !"));
       
   297 			iTocPtr->iEMMCPtnUpdate = ETrue; 
       
   298 			}
       
   299 		}
       
   300 
       
   301 	if(iTocPtr)
       
   302 	{
       
   303 	if(!iTocPtr->iEMMCPtnUpdate)
       
   304 		{
       
   305 
       
   306 		TInt ret = KErrNone;
       
   307 
       
   308 		//SOS+CORE/NORMAL=0,SOS+ROFS1=1,SOS+ROFS2=2,SOS-USER/PRODUCTION=3
       
   309 
       
   310 #ifdef ST_FLASHER
       
   311 
       
   312 
       
   313 
       
   314 		ret = iTocPtr->GetItemEx((TText8*)"NORMAL", item);
       
   315 #else
       
   316 		ret = iTocPtr->GetItemEx("SOS+CORE", item); 
       
   317 #endif //ST_FLASHER
       
   318 					
       
   319 		if(ret == KErrNone)	
       
   320 			{
       
   321 			__KTRACE_OPT(KPBUSDRV,Kern::Printf(">>SOS+CORE/NORMAL partition found"));
       
   322 			//SOS+CORE Partition
       
   323 			iTocPtr->iEmmcPartitionTable[0] = item;	
       
   324 			}
       
   325 		else 
       
   326 			{
       
   327 			__KTRACE_OPT(KPBUSDRV,Kern::Printf(">>ERROR SOS+CORE/NORMAL partition NOT found"));
       
   328 			}
       
   329 
       
   330 		//In ST flasher ROFS partitions are not define
       
   331 #ifndef ST_FLASHER
       
   332 		ret = iTocPtr->GetItemEx("SOS+ROFS1", item); 
       
   333 		if(ret == KErrNone)	
       
   334 			{
       
   335 			__KTRACE_OPT(KPBUSDRV,Kern::Printf(">>SOS+ROFS1 partition found"));
       
   336 			//Rofs Partition
       
   337 			iTocPtr->iEmmcPartitionTable[1] = item;	
       
   338 			}
       
   339 		else 
       
   340 			{
       
   341 			__KTRACE_OPT(KPBUSDRV,Kern::Printf(">>ERROR SOS+ROFS1 partition NOT found"));
       
   342 			}
       
   343 
       
   344 		// In ST flasher consider : Normal -> coreos, ADL -> Rofs, No Rofs_Ext,Production -> User
       
   345 
       
   346 		ret = iTocPtr->GetItemEx("SOS+ROFS2", item); 
       
   347 		if(ret == KErrNone)	
       
   348 			{
       
   349 			__KTRACE_OPT(KPBUSDRV,Kern::Printf(">>SOS+ROFS2 partition found"));
       
   350 			//ROFX Partition
       
   351 			iTocPtr->iEmmcPartitionTable[2] = item;	
       
   352 			}
       
   353 		else 
       
   354 			{
       
   355 			__KTRACE_OPT(KPBUSDRV,Kern::Printf(">>ERROR SOS+ROFS2 partition NOT found"));
       
   356 			}
       
   357 #endif //ST_FLASHER
       
   358 
       
   359 #ifdef ST_FLASHER
       
   360 		//USER partition is => Start address of PRODUCTION + Size of PRODUCTION
       
   361 		ret = iTocPtr->GetItemEx((TText8*)"PRODUCTION", item);
       
   362 #else
       
   363 		ret = iTocPtr->GetItemEx("SOS-USER", item); 
       
   364 #endif //ST_FLASHER		
       
   365 		if(ret == KErrNone)	
       
   366 			{
       
   367 			__KTRACE_OPT(KPBUSDRV,Kern::Printf(">>SOS-USER partition found"));
       
   368 			//SOS-USER Partition
       
   369 			iTocPtr->iEmmcPartitionTable[3] = item;	
       
   370 #ifdef ST_FLASHER			
       
   371 			TUint64 NoOfBlock512KAlign 							= (iTocPtr->iEmmcPartitionTable[3].iByteStartAddress + iTocPtr->iEmmcPartitionTable[3].iByteSize)/(KEraseMinSize);	
       
   372 			iTocPtr->iEmmcPartitionTable[3].iByteStartAddress	= (NoOfBlock512KAlign * KEraseMinSize) + KEraseStepSize;//align with 512KB + extra 4MB
       
   373 			iTocPtr->iUserAreaInBytes 							= (iCard->DeviceSize64()  - ((TUint64)(iTocPtr->iEmmcPartitionTable[3].iByteStartAddress)));		
       
   374 #else //ST_FLASHER
       
   375 			iTocPtr->iEmmcPartitionTable[3].iByteStartAddress 	= iTocPtr->iEmmcPartitionTable[3].iByteStartAddress;
       
   376 			iTocPtr->iUserAreaInBytes 							= iTocPtr->iEmmcPartitionTable[3].iByteSize;
       
   377 #endif //ST_FLASHER			
       
   378 			}
       
   379 		else //search MEM_INIT partition
       
   380 			{
       
   381 			__KTRACE_OPT(KPBUSDRV,Kern::Printf(">>ERROR SOS-USER partition NOT found search for MEM_INIT"));
       
   382 			//USER partition is => Start address of MEM_INIT + Size of MEM_INIT
       
   383 			ret = iTocPtr->GetItemEx((TText8*)"MEM_INIT", item); 
       
   384 			if(ret == KErrNone)	
       
   385 				{
       
   386 				__KTRACE_OPT(KPBUSDRV,Kern::Printf(">>MEM_INIT partition found"));
       
   387 				//SOS-USER Partition
       
   388 				iTocPtr->iEmmcPartitionTable[3] 					= item;	
       
   389 				TUint64 NoOfBlock512KAlign 							= (iTocPtr->iEmmcPartitionTable[3].iByteStartAddress + iTocPtr->iEmmcPartitionTable[3].iByteSize)/(KEraseMinSize);	
       
   390 				iTocPtr->iEmmcPartitionTable[3].iByteStartAddress	= (NoOfBlock512KAlign * KEraseMinSize) + KEraseStepSize;//align with 512KB + extra 4MB
       
   391 				iTocPtr->iUserAreaInBytes 							= (iCard->DeviceSize64()  - ((TUint64)(iTocPtr->iEmmcPartitionTable[3].iByteStartAddress)));		
       
   392 				}
       
   393 			else 
       
   394 				{
       
   395 				__KTRACE_OPT(KPBUSDRV,Kern::Printf(">>ERROR MEM_INIT partition NOT found"));
       
   396 				}
       
   397 			}
       
   398 			
       
   399 
       
   400 		//	User Partition : iPartitionCount =0
       
   401 		iPartitionInfo->iEntry[iTocPtr->iPartitionCount].iPartitionBaseAddr 	= iTocPtr->iEmmcPartitionTable[3].iByteStartAddress;
       
   402 		//iTocPtr->iUserAreaInBytes 											= iTocPtr->iUserAreaInBytes - (TUint64)(KEraseStepSize - KDiskSectorSize);// (0x3ffe00)		
       
   403 		iPartitionInfo->iEntry[iTocPtr->iPartitionCount].iPartitionLen 			= iTocPtr->iUserAreaInBytes;		
       
   404 
       
   405 		__KTRACE_OPT(KPBUSDRV,Kern::Printf(">>iTocPtr->iEmmcPartitionTable[3].iByteSize=0x%x",iTocPtr->iUserAreaInBytes));		
       
   406 
       
   407 		SetPartitionEntry( &iPartitionInfo->iEntry[iTocPtr->iPartitionCount], iTocPtr->iEmmcPartitionTable[3].iByteStartAddress/KDiskSectorSize, iTocPtr->iUserAreaInBytes/(TUint64)(KDiskSectorSize ));		
       
   408 		iPartitionInfo->iEntry[iTocPtr->iPartitionCount].iPartitionType 		= KPartitionTypeFAT16;
       
   409 		iTocPtr->iPartitionCount++;
       
   410 
       
   411 		//	CoreOs Partition : iPartitionCount =1
       
   412 		iPartitionInfo->iEntry[iTocPtr->iPartitionCount].iPartitionBaseAddr 	= iTocPtr->iEmmcPartitionTable[0].iByteStartAddress;
       
   413 		iPartitionInfo->iEntry[iTocPtr->iPartitionCount].iPartitionLen 			= iTocPtr->iEmmcPartitionTable[0].iByteSize;
       
   414 
       
   415 		SetPartitionEntry( &iPartitionInfo->iEntry[iTocPtr->iPartitionCount], iTocPtr->iEmmcPartitionTable[0].iByteStartAddress/KDiskSectorSize, iTocPtr->iEmmcPartitionTable[0].iByteSize/KDiskSectorSize );		
       
   416 		iPartitionInfo->iEntry[iTocPtr->iPartitionCount].iPartitionType 		=  KPartitionTypeROM;//KPartitionTypeEmpty indar
       
   417 		iTocPtr->iPartitionCount ++;				
       
   418 		}
       
   419 
       
   420 	}
       
   421 
       
   422 	__KTRACE_OPT(KPBUSDRV,Kern::Printf(">>partitionCount=%d",iTocPtr->iPartitionCount));
       
   423 
       
   424 	partitionCount=iTocPtr->iPartitionCount;
       
   425 
       
   426 	// Read of the first sector successful so check for a Master Boot Record
       
   427 	if (*(TUint16*)(&iIntBuf[KMBRSignatureOffset])!=0xAA55)
       
   428 	// If no valid signature give up now, No way to re-format an internal drive correctly
       
   429 			{
       
   430 			__KTRACE_OPT(KPBUSDRV, Kern::Printf("mmc:No MBR Found"));
       
   431 			// return KErrCorrupt;
       
   432 			}
       
   433 				
       
   434 
       
   435 	__ASSERT_COMPILE(KMBRFirstPartitionOffsetAligned + KMBRMaxPrimaryPartitions * sizeof(TMBRPartitionEntry) <= KMBRSignatureOffset);
       
   436 
       
   437 	memmove(&iIntBuf[0], &iIntBuf[2],
       
   438 		KMBRFirstPartitionOffsetAligned + KMBRMaxPrimaryPartitions * sizeof(TMBRPartitionEntry)); 
       
   439 
       
   440 
       
   441 	for (i=0, pe = (TMBRPartitionEntry*)(&iIntBuf[KMBRFirstPartitionOffsetAligned]);
       
   442 		pe->iPartitionType != 0 && i < KMaxPartitionEntries; i++,pe--)
       
   443 		{
       
   444 		if (pe->IsDefaultBootPartition())
       
   445 			{
       
   446 			SetPartitionEntry(&iPartitionInfo->iEntry[0],pe->iFirstSector,pe->iNumSectors);
       
   447 			defaultPartitionNumber=i;
       
   448 			partitionCount++;
       
   449 			break;
       
   450 			}
       
   451 		}
       
   452 
       
   453 	// Now add any other partitions
       
   454 	for (i=0, pe = (TMBRPartitionEntry*)(&iIntBuf[KMBRFirstPartitionOffsetAligned]);
       
   455 		pe->iPartitionType != 0 && i < KMaxPartitionEntries; i++,pe--)
       
   456 		{
       
   457 		if (defaultPartitionNumber==i)
       
   458 			{
       
   459 			// Already sorted
       
   460 			}
       
   461 
       
   462 		// FAT partition ?
       
   463 		else if (pe->IsValidDosPartition() || pe->IsValidFAT32Partition())
       
   464 			{
       
   465 			SetPartitionEntry(&iPartitionInfo->iEntry[partitionCount],pe->iFirstSector,pe->iNumSectors);
       
   466 			__KTRACE_OPT(KLOCDPAGING, Kern::Printf("Mmc: FAT partition found at sector #%u", pe->iFirstSector));
       
   467 			partitionCount++;
       
   468 			}
       
   469 
       
   470 		else if (pe->iPartitionType == KPartitionTypeROM)
       
   471 			{
       
   472 			TPartitionEntry& partitionEntry = iPartitionInfo->iEntry[partitionCount];
       
   473 			SetPartitionEntry(&iPartitionInfo->iEntry[partitionCount],pe->iFirstSector,pe->iNumSectors);
       
   474 			partitionEntry.iPartitionType = pe->iPartitionType;
       
   475 			partitionCount++;				 
       
   476 
       
   477 			__KTRACE_OPT(KLOCDPAGING, Kern::Printf("Mmc: KPartitionTypeROM found at sector #%u", pe->iFirstSector));
       
   478 			}
       
   479 
       
   480 		// ROFS partition ?
       
   481 		else if (pe->iPartitionType == KPartitionTypeRofs)
       
   482 			{
       
   483 			
       
   484 // Don't expose this for normal operation only boot?			
       
   485 			TPartitionEntry& partitionEntry = iPartitionInfo->iEntry[partitionCount];
       
   486 			SetPartitionEntry(&iPartitionInfo->iEntry[partitionCount],pe->iFirstSector,pe->iNumSectors);
       
   487 			partitionEntry.iPartitionType = pe->iPartitionType;
       
   488 			__KTRACE_OPT(KLOCDPAGING, Kern::Printf("Mmc: KPartitionTypeRofs found at sector #%u", pe->iFirstSector));
       
   489 			partitionCount++;
       
   490 			}
       
   491  
       
   492 		// Swap partition ?
       
   493 		else if (pe->iPartitionType == KPartitionTypePagedData)
       
   494 			{
       
   495 			__KTRACE_OPT(KLOCDPAGING, Kern::Printf("Mmc: KPartitionTypePagedData found at sector #%u", pe->iFirstSector));
       
   496 
       
   497 			TPartitionEntry& partitionEntry = iPartitionInfo->iEntry[partitionCount];
       
   498 			SetPartitionEntry(&iPartitionInfo->iEntry[partitionCount],pe->iFirstSector,pe->iNumSectors);
       
   499 			partitionEntry.iPartitionType = pe->iPartitionType;
       
   500 			partitionCount++;
       
   501 			}
       
   502 		}
       
   503 
       
   504 	// Check the validity of the partition address boundaries
       
   505 	// If there is any MBR errors
       
   506 	if(partitionCount > 0)
       
   507 		{
       
   508 		const TInt64 deviceSize = iCard->DeviceSize64();
       
   509 		TPartitionEntry& part = iPartitionInfo->iEntry[partitionCount - 1];
       
   510 		// Check that the card address space boundary is not exceeded by the last partition
       
   511 		if(part.iPartitionBaseAddr + part.iPartitionLen > deviceSize)
       
   512 			{
       
   513 			__KTRACE_OPT(KPBUSDRV, Kern::Printf("Mmc: MBR partition exceeds card memory space"));
       
   514 			return KErrCorrupt;
       
   515 			}
       
   516 		
       
   517 		// More than one partition. Go through all of them
       
   518 		if (partitionCount > 0)
       
   519 			{
       
   520 			for(i=partitionCount-1; i>0; i--)
       
   521 				{
       
   522 				const TPartitionEntry& curr = iPartitionInfo->iEntry[i];
       
   523 				TPartitionEntry& prev = iPartitionInfo->iEntry[i-1];
       
   524 				// Check if partitions overlap
       
   525 				if(curr.iPartitionBaseAddr < (prev.iPartitionBaseAddr + prev.iPartitionLen))
       
   526 					{
       
   527 					__KTRACE_OPT(KPBUSDRV, Kern::Printf("Mmc: Overlapping partitions"));
       
   528 						//return KErrCorrupt;
       
   529 					}
       
   530 				}
       
   531 			}
       
   532 		}
       
   533 
       
   534 	if (defaultPartitionNumber==(-1) && partitionCount==0)
       
   535 		{
       
   536 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("No Valid Partitions Found!"));
       
   537 		return KErrCorrupt;
       
   538 		}
       
   539 
       
   540 	
       
   541 	iPartitionInfo->iPartitionCount=partitionCount;
       
   542 	iPartitionInfo->iMediaSizeInBytes=iCard->DeviceSize64();
       
   543 
       
   544 #ifdef _DEBUG
       
   545 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<Mmc:PartitionInfo (C:%d)",partitionCount));
       
   546 	for (TUint x=0; x<partitionCount; x++)
       
   547 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("     Partition%d (B:%xH L:%xH)",x,I64LOW(iPartitionInfo->iEntry[x].iPartitionBaseAddr),I64LOW(iPartitionInfo->iEntry[x].iPartitionLen)));
       
   548 #endif
       
   549 
       
   550 
       
   551 	//Notify medmmc that partitioninfo is complete.
       
   552 	iCallBack.CallBack();
       
   553 	
       
   554 	return(KErrNone);
       
   555 	}
       
   556 
       
   557 
       
   558 void DLegacyEMMCPartitionInfo::SetPartitionEntry(TPartitionEntry* aEntry, TUint aFirstSector, TUint aNumSectors)
       
   559 //
       
   560 // auxiliary static function to record partition information in TPartitionEntry object
       
   561 //
       
   562 	{
       
   563 	aEntry->iPartitionBaseAddr=aFirstSector;
       
   564 	aEntry->iPartitionBaseAddr<<=KDiskSectorShift;
       
   565 	aEntry->iPartitionLen=aNumSectors;
       
   566 	aEntry->iPartitionLen<<=KDiskSectorShift;
       
   567 	aEntry->iPartitionType=KPartitionTypeFAT12;
       
   568 	}
       
   569 
       
   570 // End - DLegacyEMMCPartitionInfo
       
   571 
       
   572 
       
   573 EXPORT_C DEMMCPartitionInfo* CreateEmmcPartitionInfo()
       
   574 	{
       
   575 	return new DLegacyEMMCPartitionInfo;
       
   576 	}
       
   577 
       
   578 DECLARE_STANDARD_EXTENSION()
       
   579 	{
       
   580 	return KErrNone;
       
   581 	}
       
   582 
       
   583 
       
   584 
       
   585 
       
   586 /**************************************************************************
       
   587 * TInt Toc::GetItemX(const TText8* aItemName, STocItem& aItem)
       
   588 *-----------------------------------------------------------------------
       
   589 * Search entry in XLOADER TOC with ItemName.
       
   590 * End of TOC limited by amount of entries only
       
   591 *-----------------------------------------------------------------------
       
   592 * Parameters:
       
   593 * TText8		: Item Name
       
   594 * STocItem		: Reference of Item
       
   595 *-----------------------------------------------------------------------
       
   596 * Return Value
       
   597 * KErrNone		: If Item Found other wise return KErrNotFound
       
   598 *************************************************************************/
       
   599 
       
   600 TInt Toc::GetItemX(const TText8* aItemName, STocItem& aItem)
       
   601     {
       
   602 	TUint8 i = 0;
       
   603 
       
   604     if ( aItemName == NULL )
       
   605         {
       
   606         return KErrNotFound;
       
   607         }
       
   608 
       
   609     // check all items
       
   610 	while ( i < KXMaxNbrOfTocItems )
       
   611 		{
       
   612 		TUint8 j;	
       
   613 		for ( j = 0; j < KMaxItemNameLen; j++ )				
       
   614 			{
       
   615 				if ( aItemName[j] == iTOC[i].iFileName[j] )
       
   616 					{
       
   617 					if ( aItemName[j] == 0 )
       
   618 						{
       
   619 						// item found
       
   620 						aItem = iTOC[i];
       
   621 						return KErrNone;
       
   622 						}
       
   623 					}
       
   624 				else
       
   625 					{
       
   626 					break;
       
   627 					}
       
   628 			}
       
   629 
       
   630 		i++;
       
   631 		}
       
   632 	
       
   633 	return KErrNotFound;
       
   634     }
       
   635 
       
   636 
       
   637 /**************************************************************************
       
   638 * TInt Toc::GetItem(const TText8* aItemName, STocItem& aItem)
       
   639 *-----------------------------------------------------------------------
       
   640 * Search entry in TOC with ItemName.
       
   641 *-----------------------------------------------------------------------
       
   642 * Parameters:
       
   643 * TText8		: Item Name
       
   644 * STocItem		: Reference of Item
       
   645 *-----------------------------------------------------------------------
       
   646 * Return Value
       
   647 * KErrNone		: If Item Found other wise return KErrNotFound
       
   648 *************************************************************************/
       
   649 
       
   650 TInt Toc::GetItem(const TText8* aItemName, STocItem& aItem)
       
   651     {
       
   652 	TUint8 i = 0;
       
   653 
       
   654     if ( aItemName == NULL )
       
   655         {
       
   656         return KErrNotFound;
       
   657         }
       
   658 
       
   659 	// check all items
       
   660 	while ( i < KMaxNbrOfTocItems && iTOC[i].iByteStartAddress != KEndOfToc )
       
   661 		{
       
   662 		TUint8 j;	
       
   663 		for ( j = 0; j < KMaxItemNameLen; j++ )				
       
   664 			{
       
   665 				if ( aItemName[j] == iTOC[i].iFileName[j] )
       
   666 					{
       
   667 					if ( aItemName[j] == 0 )
       
   668 						{
       
   669 						// item found
       
   670 						aItem = iTOC[i];
       
   671 						aItem.iByteStartAddress += ((TUint32)SECTOR_SIZE) * iTocStartSector;
       
   672 						return KErrNone;
       
   673 						}
       
   674 					}
       
   675 				else
       
   676 					{
       
   677 					break;
       
   678 					}
       
   679 			}
       
   680 
       
   681 		i++;
       
   682 		}
       
   683 	
       
   684 	return KErrNotFound;
       
   685     }
       
   686 
       
   687 /**************************************************************************
       
   688 * TInt Toc::GetItem(const TText8* aItemName, STocItem& aItem)
       
   689 *-----------------------------------------------------------------------
       
   690 * Search entry in TOC with aName as part of ItemName.
       
   691 *-----------------------------------------------------------------------
       
   692 * Parameters:
       
   693 * TText8		: Item Name
       
   694 * STocItem		: Reference of Item
       
   695 *-----------------------------------------------------------------------
       
   696 * Return Value
       
   697 * KErrNone		: If Item Found other wise return KErrNotFound
       
   698 *************************************************************************/
       
   699 
       
   700 TInt Toc::GetItemEx(const TText8* aName, STocItem& aItem)
       
   701     {
       
   702 	TInt i = 0;
       
   703 	TInt l1 =0;
       
   704 	TInt j , k,l2;
       
   705 
       
   706     if ( aName == NULL )
       
   707         {
       
   708         return KErrNotFound;
       
   709         }
       
   710         
       
   711 	// calculate length for name to be searched
       
   712 	while ( i < KMaxItemNameLen && aName[i] != 0 ) { i++; l1++; }	
       
   713 	if ( !l1 ) return KErrGeneral; // zero length
       
   714 
       
   715 	// check all items
       
   716 	i = 0;
       
   717 	while ( i < KMaxNbrOfTocItems && iTOC[i].iByteStartAddress != KEndOfToc )
       
   718 		{	
       
   719 		// calculate length of current item
       
   720 		j = 0; l2 = 0;
       
   721 		while ( j < KMaxItemNameLen && iTOC[i].iFileName[j] != 0 ) { j++; l2++; }
       
   722 		if ( l2 < l1 ) { i++; continue; } // too short name, skip it
       
   723 	
       
   724 
       
   725 		// compare Item with aName
       
   726 		for ( j = 0; j <= (l2 - l1); j++ )				
       
   727 			{
       
   728 				for ( k = 0; k < l1; k++ )
       
   729 					{
       
   730 						if ( aName[k] != iTOC[i].iFileName[j+k] ) break;
       
   731 					}
       
   732 
       
   733 				if ( k == l1 )
       
   734 					{
       
   735 					// item found
       
   736 					aItem = iTOC[i];
       
   737 			//		aItem.iByteStartAddress += ((TUint32)SECTOR_SIZE) * iTocStartSector; //indar check*
       
   738 					return KErrNone;
       
   739 					}
       
   740 			}		
       
   741 
       
   742 		i++;
       
   743 		}
       
   744 	
       
   745 	return KErrNotFound;
       
   746     }
       
   747 //  End of File
       
   748