1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kernel/eka/memmodel/emul/nvram.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,160 @@
1.4 +// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32\memmodel\emul\nvram.cpp
1.18 +//
1.19 +//
1.20 +
1.21 +#include "plat_priv.h"
1.22 +#include <assp.h>
1.23 +#include <emulator.h>
1.24 +#include "execs.h"
1.25 +
1.26 +_LIT(KLitMachineConfigMutex,"MCConfMutex");
1.27 +_LIT(KLitRamDriveMutex,"RamDriveMutex");
1.28 +
1.29 +void K::InitNvRam()
1.30 + {
1.31 + __KTRACE_OPT(KBOOT,Kern::Printf("K::InitNvRam"));
1.32 + if (K::MutexCreate(K::MachineConfigMutex, KLitMachineConfigMutex, NULL, EFalse, KMutexOrdMachineConfig) != KErrNone)
1.33 + K::Fault(K::EMachineConfigMutexCreateFailed);
1.34 +
1.35 + TInt r=TInternalRamDrive::Create();
1.36 + if (r!=KErrNone)
1.37 + K::Fault(K::ERamDriveInitFailed);
1.38 +
1.39 + __KTRACE_OPT(KBOOT,Kern::Printf("K::InitNvRam() completed"));
1.40 + }
1.41 +
1.42 +// Internal RAM Drive
1.43 +
1.44 +// We emulated this on WINS by memory mapping a 2MB+4KB file (IRAMLDRV.BIN in the EmulatorMediaPath)
1.45 +// The word of the last page of this file contains the nominal size of the RAM drive 'chunk'
1.46 +
1.47 +const CHAR KIRamFileName[] = "IRAMLDRV.BIN";
1.48 +
1.49 +TInt TInternalRamDrive::Create()
1.50 + {
1.51 + __KTRACE_OPT(KBOOT, Kern::Printf("TInternalRamDrive::Create()"));
1.52 +
1.53 + // create the RAM drive mutex
1.54 + TInt r = K::MutexCreate((DMutex*&)Mutex, KLitRamDriveMutex, NULL, EFalse, KMutexOrdRamDrive);
1.55 + if (r != KErrNone)
1.56 + return r;
1.57 + __KTRACE_OPT(KBOOT, Kern::Printf("RAM drive mutex created at %08x",Mutex));
1.58 +
1.59 + // locate/open the RAM drive
1.60 + CHAR filename[MAX_PATH];
1.61 + strcpy(filename, Arch::TheAsic()->EmulatorMediaPath());
1.62 + if (!Emulator::CreateAllDirectories(filename))
1.63 + return Emulator::LastError();
1.64 + strcat(filename, KIRamFileName);
1.65 + PP::RamDriveFile = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, NULL);
1.66 + if (PP::RamDriveFile == INVALID_HANDLE_VALUE)
1.67 + return Emulator::LastError();
1.68 + TInt filesize = GetFileSize(PP::RamDriveFile, NULL);
1.69 + TInt size = 0;
1.70 +
1.71 + // deal with ram drives that were created on a real device and opened in the emulator
1.72 + // and also with ones created with a different max size setting
1.73 +
1.74 + if ((filesize&0x1ff) == 0)
1.75 + {
1.76 + // the file is from a real internal drive on a device
1.77 + size = filesize;
1.78 + }
1.79 + else if ((filesize&0x1ff) == sizeof(TRamDriveInfo))
1.80 + {
1.81 + // created by us
1.82 + DWORD bytes;
1.83 + if (SetFilePointer(PP::RamDriveFile, -4, NULL, FILE_END) == 0xffffffff
1.84 + || !ReadFile(PP::RamDriveFile, &size, sizeof(size), &bytes, NULL))
1.85 + return Emulator::LastError();
1.86 + }
1.87 + if (size > PP::RamDriveMaxSize)
1.88 + PP::RamDriveMaxSize = size;
1.89 +
1.90 + // deal with a resized ram drive file
1.91 + if (filesize != PP::RamDriveMaxSize + TInt(sizeof(TRamDriveInfo)))
1.92 + {
1.93 + DWORD bytes;
1.94 + if (SetFilePointer(PP::RamDriveFile, PP::RamDriveMaxSize, NULL, FILE_BEGIN) == 0xffffffff
1.95 + || !WriteFile(PP::RamDriveFile, &size, sizeof(size), &bytes, NULL)
1.96 + || !SetEndOfFile(PP::RamDriveFile))
1.97 + return Emulator::LastError();
1.98 + }
1.99 +
1.100 + PP::RamDriveFileMapping = CreateFileMappingA(PP::RamDriveFile, NULL, PAGE_READWRITE, 0, PP::RamDriveMaxSize + sizeof(TRamDriveInfo), NULL);
1.101 + if (PP::RamDriveFileMapping == NULL)
1.102 + return Emulator::LastError();
1.103 +
1.104 + PP::RamDriveStartAddress = (TLinAddr)MapViewOfFile(PP::RamDriveFileMapping, FILE_MAP_WRITE, 0, 0, PP::RamDriveMaxSize + sizeof(TRamDriveInfo));
1.105 + if (PP::RamDriveStartAddress == NULL)
1.106 + return Emulator::LastError();
1.107 +
1.108 + PP::RamDriveInfo = (TRamDriveInfo*)(PP::RamDriveStartAddress + PP::RamDriveMaxSize);
1.109 +
1.110 + TheSuperPage().iRamDriveSize = size;
1.111 + return KErrNone;
1.112 + }
1.113 +
1.114 +EXPORT_C TLinAddr TInternalRamDrive::Base()
1.115 +//
1.116 +// Return the Internal Ram Drive base address
1.117 +//
1.118 + {
1.119 + return PP::RamDriveStartAddress;
1.120 + }
1.121 +
1.122 +EXPORT_C TInt TInternalRamDrive::Size()
1.123 +//
1.124 +// Return the Internal Ram Drive size
1.125 +//
1.126 + {
1.127 + return PP::RamDriveInfo->iSize;
1.128 + }
1.129 +
1.130 +EXPORT_C TInt TInternalRamDrive::Adjust(TInt aNewSize)
1.131 +//
1.132 +// Adjust the size of the internal ram drive
1.133 +//
1.134 + {
1.135 + if (aNewSize<0)
1.136 + return KErrArgument;
1.137 + if (aNewSize>PP::RamDriveMaxSize)
1.138 + return KErrDiskFull;
1.139 + if (aNewSize != PP::RamDriveInfo->iSize)
1.140 + PP::RamDriveInfo->iSize = aNewSize;
1.141 + return KErrNone;
1.142 + }
1.143 +
1.144 +EXPORT_C void TInternalRamDrive::Wait()
1.145 + {
1.146 + Kern::MutexWait(*Mutex);
1.147 + }
1.148 +
1.149 +EXPORT_C void TInternalRamDrive::Signal()
1.150 + {
1.151 + Kern::MutexSignal(*Mutex);
1.152 + }
1.153 +
1.154 +void ExecHandler::UnlockRamDrive()
1.155 + {
1.156 + }
1.157 +
1.158 +EXPORT_C void TInternalRamDrive::Unlock()
1.159 + {}
1.160 +
1.161 +EXPORT_C void TInternalRamDrive::Lock()
1.162 + {}
1.163 +