os/kernelhwsrv/kernel/eka/memmodel/emul/nvram.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) 1997-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\emul\nvram.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "plat_priv.h"
sl@0
    19
#include <assp.h>
sl@0
    20
#include <emulator.h>
sl@0
    21
#include "execs.h"
sl@0
    22
sl@0
    23
_LIT(KLitMachineConfigMutex,"MCConfMutex");
sl@0
    24
_LIT(KLitRamDriveMutex,"RamDriveMutex");
sl@0
    25
sl@0
    26
void K::InitNvRam()
sl@0
    27
	{
sl@0
    28
	__KTRACE_OPT(KBOOT,Kern::Printf("K::InitNvRam"));
sl@0
    29
	if (K::MutexCreate(K::MachineConfigMutex, KLitMachineConfigMutex, NULL, EFalse, KMutexOrdMachineConfig) != KErrNone)
sl@0
    30
		K::Fault(K::EMachineConfigMutexCreateFailed);
sl@0
    31
sl@0
    32
	TInt r=TInternalRamDrive::Create();
sl@0
    33
	if (r!=KErrNone)
sl@0
    34
		K::Fault(K::ERamDriveInitFailed);
sl@0
    35
sl@0
    36
	__KTRACE_OPT(KBOOT,Kern::Printf("K::InitNvRam() completed"));
sl@0
    37
	}
sl@0
    38
sl@0
    39
// Internal RAM Drive
sl@0
    40
sl@0
    41
// We emulated this on WINS by memory mapping a 2MB+4KB file (IRAMLDRV.BIN in the EmulatorMediaPath)
sl@0
    42
// The word of the last page of this file contains the nominal size of the RAM drive 'chunk'
sl@0
    43
sl@0
    44
const CHAR KIRamFileName[] = "IRAMLDRV.BIN";
sl@0
    45
sl@0
    46
TInt TInternalRamDrive::Create()
sl@0
    47
	{
sl@0
    48
	__KTRACE_OPT(KBOOT, Kern::Printf("TInternalRamDrive::Create()"));
sl@0
    49
sl@0
    50
	// create the RAM drive mutex
sl@0
    51
	TInt r = K::MutexCreate((DMutex*&)Mutex, KLitRamDriveMutex, NULL, EFalse, KMutexOrdRamDrive);
sl@0
    52
	if (r != KErrNone)
sl@0
    53
		return r;
sl@0
    54
	__KTRACE_OPT(KBOOT, Kern::Printf("RAM drive mutex created at %08x",Mutex));
sl@0
    55
sl@0
    56
	// locate/open the RAM drive
sl@0
    57
	CHAR filename[MAX_PATH];
sl@0
    58
	strcpy(filename, Arch::TheAsic()->EmulatorMediaPath());
sl@0
    59
	if (!Emulator::CreateAllDirectories(filename))
sl@0
    60
		return Emulator::LastError();
sl@0
    61
	strcat(filename, KIRamFileName);
sl@0
    62
	PP::RamDriveFile = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, NULL);
sl@0
    63
	if (PP::RamDriveFile == INVALID_HANDLE_VALUE)
sl@0
    64
		return Emulator::LastError();
sl@0
    65
	TInt filesize = GetFileSize(PP::RamDriveFile, NULL);
sl@0
    66
	TInt size = 0;
sl@0
    67
sl@0
    68
	// deal with ram drives that were created on a real device and opened in the emulator
sl@0
    69
	// and also with ones created with a different max size setting
sl@0
    70
sl@0
    71
	if ((filesize&0x1ff) == 0)
sl@0
    72
		{
sl@0
    73
		// the file is from a real internal drive on a device
sl@0
    74
		size = filesize;
sl@0
    75
		}
sl@0
    76
	else if ((filesize&0x1ff) == sizeof(TRamDriveInfo))
sl@0
    77
		{
sl@0
    78
		// created by us
sl@0
    79
		DWORD bytes;
sl@0
    80
		if (SetFilePointer(PP::RamDriveFile, -4, NULL, FILE_END) == 0xffffffff
sl@0
    81
			|| !ReadFile(PP::RamDriveFile, &size, sizeof(size), &bytes, NULL))
sl@0
    82
			return Emulator::LastError();
sl@0
    83
		}
sl@0
    84
	if (size > PP::RamDriveMaxSize)
sl@0
    85
		PP::RamDriveMaxSize = size;
sl@0
    86
sl@0
    87
	// deal with a resized ram drive file
sl@0
    88
	if (filesize != PP::RamDriveMaxSize + TInt(sizeof(TRamDriveInfo)))
sl@0
    89
		{
sl@0
    90
		DWORD bytes;
sl@0
    91
		if (SetFilePointer(PP::RamDriveFile, PP::RamDriveMaxSize, NULL, FILE_BEGIN) == 0xffffffff
sl@0
    92
			|| !WriteFile(PP::RamDriveFile, &size, sizeof(size), &bytes, NULL)
sl@0
    93
			|| !SetEndOfFile(PP::RamDriveFile))
sl@0
    94
			return Emulator::LastError();
sl@0
    95
		}
sl@0
    96
sl@0
    97
	PP::RamDriveFileMapping = CreateFileMappingA(PP::RamDriveFile, NULL, PAGE_READWRITE, 0, PP::RamDriveMaxSize + sizeof(TRamDriveInfo), NULL);
sl@0
    98
	if (PP::RamDriveFileMapping == NULL)
sl@0
    99
		return Emulator::LastError();
sl@0
   100
sl@0
   101
	PP::RamDriveStartAddress = (TLinAddr)MapViewOfFile(PP::RamDriveFileMapping, FILE_MAP_WRITE, 0, 0, PP::RamDriveMaxSize + sizeof(TRamDriveInfo));
sl@0
   102
	if (PP::RamDriveStartAddress == NULL)
sl@0
   103
		return Emulator::LastError();
sl@0
   104
sl@0
   105
	PP::RamDriveInfo = (TRamDriveInfo*)(PP::RamDriveStartAddress + PP::RamDriveMaxSize);
sl@0
   106
sl@0
   107
	TheSuperPage().iRamDriveSize = size;
sl@0
   108
	return KErrNone;
sl@0
   109
	}
sl@0
   110
sl@0
   111
EXPORT_C TLinAddr TInternalRamDrive::Base()
sl@0
   112
//
sl@0
   113
// Return the Internal Ram Drive base address
sl@0
   114
//
sl@0
   115
	{
sl@0
   116
	return PP::RamDriveStartAddress;
sl@0
   117
	}
sl@0
   118
sl@0
   119
EXPORT_C TInt TInternalRamDrive::Size()
sl@0
   120
//
sl@0
   121
// Return the Internal Ram Drive size
sl@0
   122
//
sl@0
   123
	{
sl@0
   124
	return PP::RamDriveInfo->iSize;
sl@0
   125
	}
sl@0
   126
sl@0
   127
EXPORT_C TInt TInternalRamDrive::Adjust(TInt aNewSize)
sl@0
   128
//
sl@0
   129
// Adjust the size of the internal ram drive
sl@0
   130
//
sl@0
   131
	{
sl@0
   132
	if (aNewSize<0)
sl@0
   133
		return KErrArgument;
sl@0
   134
	if (aNewSize>PP::RamDriveMaxSize)
sl@0
   135
		return KErrDiskFull;
sl@0
   136
	if (aNewSize != PP::RamDriveInfo->iSize)
sl@0
   137
		PP::RamDriveInfo->iSize = aNewSize;
sl@0
   138
	return KErrNone;
sl@0
   139
	}
sl@0
   140
sl@0
   141
EXPORT_C void TInternalRamDrive::Wait()
sl@0
   142
	{
sl@0
   143
	Kern::MutexWait(*Mutex);
sl@0
   144
	}
sl@0
   145
sl@0
   146
EXPORT_C void TInternalRamDrive::Signal()
sl@0
   147
	{
sl@0
   148
	Kern::MutexSignal(*Mutex);
sl@0
   149
	}
sl@0
   150
sl@0
   151
void ExecHandler::UnlockRamDrive()
sl@0
   152
	{
sl@0
   153
	}
sl@0
   154
sl@0
   155
EXPORT_C void TInternalRamDrive::Unlock()
sl@0
   156
	{}
sl@0
   157
sl@0
   158
EXPORT_C void TInternalRamDrive::Lock()
sl@0
   159
	{}
sl@0
   160