kernel/eka/memmodel/epoc/mmubase/kblockmap.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2006-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 // e32\memmodel\epoc\mmubase\kblockmap.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <memmodel/epoc/mmubase/kblockmap.h>
       
    19 #include <e32cmn.h>
       
    20 #include <e32cmn_private.h>
       
    21 #include <e32atomics.h>
       
    22 
       
    23 #undef ASSERT
       
    24 
       
    25 #ifdef __KERNEL_MODE__
       
    26 
       
    27 #include <kernel/kernel.h>
       
    28 #define TRACECLASS Kern
       
    29 #undef TRACE
       
    30 #define ASSERT(x) __NK_ASSERT_DEBUG(x)
       
    31 #define FREE(x) Kern::Free(x)
       
    32 
       
    33 #define RETURN_ERROR(err, traceMsg)													 \
       
    34 	{																				 \
       
    35 	__KTRACE_OPT(KPAGING,Kern::Printf("DP: %s, returning error %d", traceMsg, err)); \
       
    36 	return err;																		 \
       
    37 	}
       
    38 
       
    39 #define TRACE_FATAL(x) __KTRACE_OPT(KPANIC, Kern::x)
       
    40 
       
    41 #else
       
    42 
       
    43 #include <e32debug.h>
       
    44 #include <e32test.h>
       
    45 #define TRACECLASS RDebug
       
    46 extern RTest test;
       
    47 #define ASSERT(x) test(x);
       
    48 #define FREE(x) User::Free(x)
       
    49 
       
    50 #define RETURN_ERROR(err, traceMsg)								  \
       
    51 	{															  \
       
    52 	RDebug::Printf("DP: %s, returning error %d", traceMsg, err); \
       
    53 	return err;													  \
       
    54 	}
       
    55 
       
    56 #define TRACE_FATAL(x) RDebug::x
       
    57 
       
    58 #endif
       
    59 
       
    60 TInt TBlockMap::Initialise(const SBlockMapInfoBase& aBlockMapInfo,
       
    61 						   TBlockMapEntryBase* aBlockMapEntries,
       
    62 						   TInt aBlockMapEntriesSize,
       
    63 						   TInt aReadUnitShift,
       
    64 						   TInt aDataLengthInFile)
       
    65 	{
       
    66 	ASSERT(iExtents == NULL);
       
    67 
       
    68 	if (aBlockMapEntriesSize % sizeof(TBlockMapEntryBase) != 0)
       
    69 		RETURN_ERROR(KErrArgument, "Size of block map is not multiple of entry size");
       
    70 	iExtentCount = aBlockMapEntriesSize / sizeof(TBlockMapEntryBase);
       
    71 
       
    72 	TInt blockShift = __e32_find_ms1_32(aBlockMapInfo.iBlockGranularity);
       
    73 	if ((1u << blockShift) != aBlockMapInfo.iBlockGranularity)
       
    74 		RETURN_ERROR(KErrArgument, "Block granularity not a power of two");
       
    75 	if (blockShift < aReadUnitShift)
       
    76 		RETURN_ERROR(KErrArgument, "Block size must be greater than or equal to read unit size");
       
    77 	TInt blockScaling = blockShift - aReadUnitShift;
       
    78 	TInt readUnitMask = (1 << aReadUnitShift) - 1;
       
    79 
       
    80 	if ((aBlockMapInfo.iStartBlockAddress & ((1 << aReadUnitShift) - 1)) != 0)
       
    81 		RETURN_ERROR(KErrArgument, "Block zero address not a multiple of read unit size");
       
    82 	TUint blockZeroNumber = (TUint)(aBlockMapInfo.iStartBlockAddress >> aReadUnitShift);  // offset of block zero from start of partition
       
    83 
       
    84 	if (aBlockMapInfo.iBlockStartOffset >= aBlockMapInfo.iBlockGranularity)
       
    85 		RETURN_ERROR(KErrArgument, "Block start offset must be less than block size");
       
    86 
       
    87 	if (aDataLengthInFile <= 0)
       
    88 		RETURN_ERROR(KErrArgument, "Length of code data in file must be greater than zero"); 
       
    89 	iDataLength = aDataLengthInFile;
       
    90 	
       
    91 	// Process block map data into kernel-side reprsentation
       
    92 	TInt dataOffset = -(TInt)(aBlockMapInfo.iBlockStartOffset & readUnitMask);
       
    93 	SExtent entry;
       
    94 	for (TInt i = 0 ; i < iExtentCount ; ++i)
       
    95 		{
       
    96 		const TBlockMapEntryBase& data = aBlockMapEntries[i];
       
    97 		entry.iDataOffset = dataOffset;
       
    98 		entry.iBlockNumber = (data.iStartBlock << blockScaling) + blockZeroNumber;
       
    99 		dataOffset += data.iNumberOfBlocks << blockShift;
       
   100 		if (i == 0)
       
   101 			{
       
   102 			TInt adjustStartBlock = aBlockMapInfo.iBlockStartOffset >> aReadUnitShift;
       
   103 			entry.iBlockNumber += adjustStartBlock;
       
   104 			dataOffset -= adjustStartBlock << aReadUnitShift;
       
   105 			}
       
   106 		(SExtent&)data = entry;
       
   107 		}
       
   108 	
       
   109 	if (dataOffset < iDataLength)
       
   110 		RETURN_ERROR(KErrArgument, "Block map too short");
       
   111 
       
   112 	// Take ownership of buffer
       
   113 	iExtents = (SExtent*) aBlockMapEntries;
       
   114 
       
   115 	return KErrNone;
       
   116 	}
       
   117 
       
   118 TBlockMap::TBlockMap()
       
   119 	: iExtents(NULL)
       
   120 	{
       
   121 	}
       
   122 
       
   123 TBlockMap::~TBlockMap()
       
   124 	{
       
   125 	FREE(iExtents);
       
   126 	}
       
   127 
       
   128 TInt TBlockMap::FindFirstExtent(TInt aPos) const
       
   129 	{
       
   130 	if (aPos < 0 || aPos >= iDataLength)
       
   131 		return KErrArgument;
       
   132 	RArray<SExtent> extents(sizeof(SExtent), iExtents, iExtentCount);
       
   133 	SExtent findEntry = { aPos, 0 };
       
   134 	TInt i = -1;
       
   135 	extents.SpecificFindInSignedKeyOrder(findEntry, i, EArrayFindMode_Last);
       
   136 	--i;
       
   137 	if (i < 0 || i >= iExtentCount)
       
   138 		return KErrArgument;
       
   139 	return i;
       
   140 	}
       
   141 
       
   142 TInt TBlockMap::Read(TLinAddr aBuffer, TInt aPos, TInt aLength, TInt aReadUnitShift, TReadFunc aReadFunc, TAny* aArg1, TAny* aArg2) const
       
   143 	{
       
   144 	TInt dataOffset = aPos;
       
   145 	TInt remain = aLength;
       
   146 	TInt readUnitMask = (1 << aReadUnitShift) - 1;
       
   147 	
       
   148 	TInt i = FindFirstExtent(dataOffset);
       
   149 	if (i < 0)
       
   150 		return i;
       
   151 	TInt bufferStart = (dataOffset - Extent(i).iDataOffset) & readUnitMask;  // start of page in buffer
       
   152 	TInt bufferOffset = 0;
       
   153 	while (remain > 0)
       
   154 		{
       
   155  		if (i >= Count())
       
   156 			return KErrArgument;
       
   157 		
       
   158 		const SExtent& extent = Extent(i);
       
   159 		TInt blockStartOffset = dataOffset - extent.iDataOffset;
       
   160 		TInt blockNumber = extent.iBlockNumber + (blockStartOffset >> aReadUnitShift);
       
   161 		TInt nextExtentStart = i == Count() - 1 ? iDataLength : Extent(i + 1).iDataOffset;
       
   162 		TInt dataSize = Min(remain, nextExtentStart - dataOffset);
       
   163 		TInt blockCount = (dataSize + (blockStartOffset & readUnitMask) + readUnitMask) >> aReadUnitShift;
       
   164 		
       
   165 		TInt r = aReadFunc(aArg1, aArg2, aBuffer + bufferOffset, blockNumber, blockCount);
       
   166 		if (r != KErrNone)
       
   167 			{
       
   168 			TRACE_FATAL(Printf("TBlockMap::Read: error reading media at %08x + %x: %d", blockNumber << aReadUnitShift, blockCount << aReadUnitShift, r));
       
   169 			return r;
       
   170 			}
       
   171 
       
   172 		bufferOffset += blockCount << aReadUnitShift;
       
   173 		dataOffset += dataSize;
       
   174 		remain -= dataSize;
       
   175 		++i;
       
   176 		}
       
   177 	
       
   178 	return bufferStart;
       
   179 	}
       
   180 
       
   181 #ifdef _DEBUG
       
   182 
       
   183 void TBlockMap::Dump() const
       
   184 	{
       
   185 	TRACECLASS::Printf("TBlockMap:");
       
   186 	for (TInt i = 0 ; i < Count() ; ++i)
       
   187 		{
       
   188 		TInt nextExtentStart = i == Count() - 1 ? iDataLength : Extent(i + 1).iDataOffset;
       
   189 		TRACECLASS::Printf("  %d: %08x -> %08x: %08x", i, Extent(i).iDataOffset, nextExtentStart, Extent(i).iBlockNumber);
       
   190 		}
       
   191 	}
       
   192 
       
   193 #endif