First public contribution.
1 // Copyright (c) 1997-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\memmodel\emul\nvram.cpp
18 #include "plat_priv.h"
23 _LIT(KLitMachineConfigMutex,"MCConfMutex");
24 _LIT(KLitRamDriveMutex,"RamDriveMutex");
28 __KTRACE_OPT(KBOOT,Kern::Printf("K::InitNvRam"));
29 if (K::MutexCreate(K::MachineConfigMutex, KLitMachineConfigMutex, NULL, EFalse, KMutexOrdMachineConfig) != KErrNone)
30 K::Fault(K::EMachineConfigMutexCreateFailed);
32 TInt r=TInternalRamDrive::Create();
34 K::Fault(K::ERamDriveInitFailed);
36 __KTRACE_OPT(KBOOT,Kern::Printf("K::InitNvRam() completed"));
41 // We emulated this on WINS by memory mapping a 2MB+4KB file (IRAMLDRV.BIN in the EmulatorMediaPath)
42 // The word of the last page of this file contains the nominal size of the RAM drive 'chunk'
44 const CHAR KIRamFileName[] = "IRAMLDRV.BIN";
46 TInt TInternalRamDrive::Create()
48 __KTRACE_OPT(KBOOT, Kern::Printf("TInternalRamDrive::Create()"));
50 // create the RAM drive mutex
51 TInt r = K::MutexCreate((DMutex*&)Mutex, KLitRamDriveMutex, NULL, EFalse, KMutexOrdRamDrive);
54 __KTRACE_OPT(KBOOT, Kern::Printf("RAM drive mutex created at %08x",Mutex));
56 // locate/open the RAM drive
57 CHAR filename[MAX_PATH];
58 strcpy(filename, Arch::TheAsic()->EmulatorMediaPath());
59 if (!Emulator::CreateAllDirectories(filename))
60 return Emulator::LastError();
61 strcat(filename, KIRamFileName);
62 PP::RamDriveFile = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, NULL);
63 if (PP::RamDriveFile == INVALID_HANDLE_VALUE)
64 return Emulator::LastError();
65 TInt filesize = GetFileSize(PP::RamDriveFile, NULL);
68 // deal with ram drives that were created on a real device and opened in the emulator
69 // and also with ones created with a different max size setting
71 if ((filesize&0x1ff) == 0)
73 // the file is from a real internal drive on a device
76 else if ((filesize&0x1ff) == sizeof(TRamDriveInfo))
80 if (SetFilePointer(PP::RamDriveFile, -4, NULL, FILE_END) == 0xffffffff
81 || !ReadFile(PP::RamDriveFile, &size, sizeof(size), &bytes, NULL))
82 return Emulator::LastError();
84 if (size > PP::RamDriveMaxSize)
85 PP::RamDriveMaxSize = size;
87 // deal with a resized ram drive file
88 if (filesize != PP::RamDriveMaxSize + TInt(sizeof(TRamDriveInfo)))
91 if (SetFilePointer(PP::RamDriveFile, PP::RamDriveMaxSize, NULL, FILE_BEGIN) == 0xffffffff
92 || !WriteFile(PP::RamDriveFile, &size, sizeof(size), &bytes, NULL)
93 || !SetEndOfFile(PP::RamDriveFile))
94 return Emulator::LastError();
97 PP::RamDriveFileMapping = CreateFileMappingA(PP::RamDriveFile, NULL, PAGE_READWRITE, 0, PP::RamDriveMaxSize + sizeof(TRamDriveInfo), NULL);
98 if (PP::RamDriveFileMapping == NULL)
99 return Emulator::LastError();
101 PP::RamDriveStartAddress = (TLinAddr)MapViewOfFile(PP::RamDriveFileMapping, FILE_MAP_WRITE, 0, 0, PP::RamDriveMaxSize + sizeof(TRamDriveInfo));
102 if (PP::RamDriveStartAddress == NULL)
103 return Emulator::LastError();
105 PP::RamDriveInfo = (TRamDriveInfo*)(PP::RamDriveStartAddress + PP::RamDriveMaxSize);
107 TheSuperPage().iRamDriveSize = size;
111 EXPORT_C TLinAddr TInternalRamDrive::Base()
113 // Return the Internal Ram Drive base address
116 return PP::RamDriveStartAddress;
119 EXPORT_C TInt TInternalRamDrive::Size()
121 // Return the Internal Ram Drive size
124 return PP::RamDriveInfo->iSize;
127 EXPORT_C TInt TInternalRamDrive::Adjust(TInt aNewSize)
129 // Adjust the size of the internal ram drive
134 if (aNewSize>PP::RamDriveMaxSize)
136 if (aNewSize != PP::RamDriveInfo->iSize)
137 PP::RamDriveInfo->iSize = aNewSize;
141 EXPORT_C void TInternalRamDrive::Wait()
143 Kern::MutexWait(*Mutex);
146 EXPORT_C void TInternalRamDrive::Signal()
148 Kern::MutexSignal(*Mutex);
151 void ExecHandler::UnlockRamDrive()
155 EXPORT_C void TInternalRamDrive::Unlock()
158 EXPORT_C void TInternalRamDrive::Lock()