|
1 /* |
|
2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "memmap.h" |
|
20 |
|
21 /** |
|
22 Constructor: Memmap class |
|
23 Initilize the parameters to data members. |
|
24 |
|
25 @internalComponent |
|
26 @released |
|
27 |
|
28 @param aFillFlg - Flag to enable the initialisation of memory map |
|
29 @param aOutputFile - Name of the output file |
|
30 */ |
|
31 Memmap::Memmap( int aFillFlg, String aOutputFile ) |
|
32 : iOutFileName(aOutputFile), iData(0), iMaxMapSize(0), iStartOffset(0), iFillFlg(aFillFlg) |
|
33 { |
|
34 iUtils = new MemmapUtils(); |
|
35 } |
|
36 |
|
37 /** |
|
38 Constructor: Memmap class |
|
39 Initilize the parameters to data members. |
|
40 |
|
41 @internalComponent |
|
42 @released |
|
43 |
|
44 @param aFillFlg - Flag to enable the initialisation of memory map |
|
45 */ |
|
46 Memmap::Memmap( int aFillFlg ) |
|
47 : iData(0), iMaxMapSize(0), iStartOffset(0), iFillFlg(aFillFlg) |
|
48 { |
|
49 iUtils = new MemmapUtils(); |
|
50 } |
|
51 |
|
52 |
|
53 /** |
|
54 Destructor: Memmap class |
|
55 Deallocates the memory for data members |
|
56 |
|
57 @internalComponent |
|
58 @released |
|
59 */ |
|
60 Memmap::~Memmap( ) |
|
61 { |
|
62 if(iData) |
|
63 { |
|
64 CloseMemoryMap(); |
|
65 } |
|
66 |
|
67 if(iUtils) |
|
68 { |
|
69 delete iUtils; |
|
70 } |
|
71 } |
|
72 |
|
73 /** |
|
74 SetOutputFile: To set the output image file |
|
75 |
|
76 @internalComponent |
|
77 @released |
|
78 |
|
79 @param aOutputFile - Name of the output image file |
|
80 */ |
|
81 void Memmap::SetOutputFile( String aOutputFile ) |
|
82 { |
|
83 iOutFileName = aOutputFile; |
|
84 } |
|
85 |
|
86 /** |
|
87 SetMaxMapSize: To set the maximum size of the memory map |
|
88 |
|
89 @internalComponent |
|
90 @released |
|
91 |
|
92 @param aMaxSize - Size of the memory map |
|
93 */ |
|
94 void Memmap::SetMaxMapSize( unsigned long aMaxSize ) |
|
95 { |
|
96 iMaxMapSize = aMaxSize; |
|
97 }; |
|
98 |
|
99 /** |
|
100 GetMapSize: To get the size of the memory map |
|
101 |
|
102 @internalComponent |
|
103 @released |
|
104 */ |
|
105 unsigned long Memmap::GetMapSize( ) |
|
106 { |
|
107 return iMaxMapSize; |
|
108 } |
|
109 |
|
110 /** |
|
111 operator[]: To access the memory map contents |
|
112 |
|
113 @internalComponent |
|
114 @released |
|
115 |
|
116 @param aIndex - Offset of the memory map location |
|
117 */ |
|
118 char& Memmap::operator[]( unsigned long aIndex ) |
|
119 { |
|
120 return iData[aIndex]; |
|
121 } |
|
122 |
|
123 /** |
|
124 CreateMemoryMap: |
|
125 Opens the memory map file |
|
126 Initialises the map size member |
|
127 Create the memory map pointer |
|
128 Fill the memory map with the specified value |
|
129 |
|
130 @internalComponent |
|
131 @released |
|
132 |
|
133 @param aStartOffset - Start offset of the memory map location |
|
134 @param aFillVal - Value to be filled in the memory map |
|
135 */ |
|
136 int Memmap::CreateMemoryMap( unsigned long aStartOffset, unsigned char aFillVal ) |
|
137 { |
|
138 if((!iMaxMapSize) || (aStartOffset > iMaxMapSize)) |
|
139 { |
|
140 return KStatFalse; |
|
141 } |
|
142 else if(iUtils->IsMapFileOpen() && iData) |
|
143 { |
|
144 iStartOffset = aStartOffset; |
|
145 return KStatTrue; |
|
146 } |
|
147 |
|
148 if(iUtils->IsMapFileOpen() == KStatFalse) |
|
149 { |
|
150 if(iUtils->OpenMapFile() == KStatFalse) |
|
151 { |
|
152 return KStatFalse; |
|
153 } |
|
154 } |
|
155 |
|
156 if(iUtils->CreateFileMapObject(iMaxMapSize) == KStatFalse) |
|
157 { |
|
158 return KStatFalse; |
|
159 } |
|
160 |
|
161 iData = (char*)(iUtils->OpenMemMapPointer(0,iMaxMapSize)); |
|
162 if( !iData ) |
|
163 { |
|
164 return KStatFalse; |
|
165 } |
|
166 |
|
167 iStartOffset = aStartOffset; |
|
168 |
|
169 if(iFillFlg) |
|
170 { |
|
171 return FillMemMap( aFillVal ); |
|
172 } |
|
173 |
|
174 return KStatTrue; |
|
175 } |
|
176 |
|
177 /** |
|
178 CloseMemoryMap: Close the memory map and the associated objects |
|
179 |
|
180 @internalComponent |
|
181 @released |
|
182 |
|
183 @param aCloseFile - Flag to close the memory map file |
|
184 */ |
|
185 void Memmap::CloseMemoryMap( int aCloseFile ) |
|
186 { |
|
187 // Close map view pointer |
|
188 if(!iUtils->CloseMemMapPointer((void*)iData, iMaxMapSize)) |
|
189 { |
|
190 Print(ELog, "Failed to unmap the memory map object"); |
|
191 } |
|
192 iData = 0; |
|
193 |
|
194 iUtils->CloseFileMapObject(); |
|
195 |
|
196 // Close map file |
|
197 if(aCloseFile) |
|
198 { |
|
199 iUtils->CloseMapFile(); |
|
200 } |
|
201 } |
|
202 |
|
203 /** |
|
204 GetMemoryMapPointer: Get the stating address of the memory map |
|
205 |
|
206 @internalComponent |
|
207 @released |
|
208 */ |
|
209 char *Memmap::GetMemoryMapPointer( ) |
|
210 { |
|
211 if(iData) |
|
212 return (iData + iStartOffset); |
|
213 |
|
214 return KStatFalse; |
|
215 } |
|
216 |
|
217 /** |
|
218 WriteToOutputFile: Writes the memory map contents to the output file |
|
219 |
|
220 @internalComponent |
|
221 @released |
|
222 */ |
|
223 void Memmap::WriteToOutputFile( ) |
|
224 { |
|
225 Ofstream ofs; |
|
226 |
|
227 if(!iData) |
|
228 { |
|
229 Print(EAlways, "Memory map has not been created"); |
|
230 } |
|
231 |
|
232 if(iOutFileName.empty()) |
|
233 { |
|
234 Print(EAlways, "Output file has not been set"); |
|
235 return; |
|
236 } |
|
237 |
|
238 ofs.open(((const char*)iOutFileName.data()), std::ios::binary); |
|
239 if(!ofs.is_open()) |
|
240 { |
|
241 Print(EAlways, "Cannot open output file %s", (char*)iOutFileName.data()); |
|
242 return; |
|
243 } |
|
244 |
|
245 ofs.write((const char*)(iData + iStartOffset), (iMaxMapSize - iStartOffset)); |
|
246 |
|
247 ofs.close(); |
|
248 |
|
249 return; |
|
250 } |
|
251 |
|
252 /** |
|
253 FillMemMap: Fills the memory map with the specified value |
|
254 |
|
255 @internalComponent |
|
256 @released |
|
257 |
|
258 @param aFillVal - Value to be filled |
|
259 */ |
|
260 int Memmap::FillMemMap( unsigned char aFillVal ) |
|
261 { |
|
262 if(iData) |
|
263 { |
|
264 // Fill the value |
|
265 memset(iData, aFillVal, iMaxMapSize); |
|
266 |
|
267 // Unmap the file |
|
268 if(iUtils->CloseMemMapPointer((void*)iData, iMaxMapSize) == KStatFalse) |
|
269 { |
|
270 return KStatFalse; |
|
271 } |
|
272 |
|
273 // Map it again |
|
274 iData = (char*)(iUtils->OpenMemMapPointer(0,iMaxMapSize)); |
|
275 if(!iData) |
|
276 { |
|
277 return KStatFalse; |
|
278 } |
|
279 } |
|
280 |
|
281 return KStatTrue; |
|
282 } |
|
283 |