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