]>
Commit | Line | Data |
---|---|---|
632820d1 | 1 | /** @file\r |
2 | Functions to deal with Mem buffer\r | |
3 | \r | |
e90153cb | 4 | Copyright (c) 2005 - 2017, Intel Corporation. All rights reserved. <BR>\r |
632820d1 | 5 | This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include "HexEditor.h"\r | |
16 | \r | |
17 | extern EFI_HANDLE HImageHandleBackup;\r | |
18 | \r | |
19 | extern HEFI_EDITOR_BUFFER_IMAGE HBufferImage;\r | |
20 | \r | |
21 | extern BOOLEAN HBufferImageNeedRefresh;\r | |
22 | extern BOOLEAN HBufferImageOnlyLineNeedRefresh;\r | |
23 | extern BOOLEAN HBufferImageMouseNeedRefresh;\r | |
24 | \r | |
25 | extern HEFI_EDITOR_GLOBAL_EDITOR HMainEditor;\r | |
26 | \r | |
27 | HEFI_EDITOR_MEM_IMAGE HMemImage;\r | |
28 | HEFI_EDITOR_MEM_IMAGE HMemImageBackupVar;\r | |
29 | \r | |
632820d1 | 30 | //\r |
31 | // for basic initialization of HDiskImage\r | |
32 | //\r | |
33 | HEFI_EDITOR_MEM_IMAGE HMemImageConst = {\r | |
34 | NULL,\r | |
35 | 0,\r | |
36 | 0\r | |
37 | };\r | |
38 | \r | |
a1d4bfcc | 39 | /**\r |
40 | Initialization function for HDiskImage.\r | |
632820d1 | 41 | \r |
a1d4bfcc | 42 | @retval EFI_SUCCESS The operation was successful.\r |
43 | @retval EFI_LOAD_ERROR A load error occured.\r | |
44 | **/\r | |
632820d1 | 45 | EFI_STATUS\r |
46 | HMemImageInit (\r | |
47 | VOID\r | |
48 | )\r | |
632820d1 | 49 | {\r |
50 | EFI_STATUS Status;\r | |
51 | \r | |
52 | //\r | |
53 | // basically initialize the HMemImage\r | |
54 | //\r | |
55 | CopyMem (&HMemImage, &HMemImageConst, sizeof (HMemImage));\r | |
56 | \r | |
57 | Status = gBS->LocateProtocol (\r | |
e90153cb | 58 | &gEfiCpuIo2ProtocolGuid,\r |
632820d1 | 59 | NULL,\r |
60 | (VOID**)&HMemImage.IoFncs\r | |
61 | );\r | |
632820d1 | 62 | if (!EFI_ERROR (Status)) {\r |
63 | return EFI_SUCCESS;\r | |
64 | } else {\r | |
65 | return EFI_LOAD_ERROR;\r | |
66 | }\r | |
67 | }\r | |
68 | \r | |
a1d4bfcc | 69 | /**\r |
70 | Backup function for HDiskImage. Only a few fields need to be backup. \r | |
71 | This is for making the Disk buffer refresh as few as possible.\r | |
72 | \r | |
73 | @retval EFI_SUCCESS The operation was successful.\r | |
74 | **/\r | |
632820d1 | 75 | EFI_STATUS\r |
76 | HMemImageBackup (\r | |
77 | VOID\r | |
78 | )\r | |
632820d1 | 79 | {\r |
80 | HMemImageBackupVar.Offset = HMemImage.Offset;\r | |
81 | HMemImageBackupVar.Size = HMemImage.Size;\r | |
82 | \r | |
83 | return EFI_SUCCESS;\r | |
84 | }\r | |
85 | \r | |
a1d4bfcc | 86 | /**\r |
87 | Set FileName field in HFileImage.\r | |
632820d1 | 88 | \r |
a1d4bfcc | 89 | @param[in] Offset The offset.\r |
90 | @param[in] Size The size.\r | |
632820d1 | 91 | \r |
a1d4bfcc | 92 | @retval EFI_SUCCESS The operation was successful.\r |
93 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r | |
94 | **/\r | |
632820d1 | 95 | EFI_STATUS\r |
96 | HMemImageSetMemOffsetSize (\r | |
97 | IN UINTN Offset,\r | |
98 | IN UINTN Size\r | |
99 | )\r | |
632820d1 | 100 | {\r |
101 | \r | |
102 | HMemImage.Offset = Offset;\r | |
103 | HMemImage.Size = Size;\r | |
104 | \r | |
105 | return EFI_SUCCESS;\r | |
106 | }\r | |
107 | \r | |
a1d4bfcc | 108 | /**\r |
109 | Read a disk from disk into HBufferImage.\r | |
110 | \r | |
111 | @param[in] Offset The offset.\r | |
112 | @param[in] Size The size.\r | |
113 | @param[in] Recover if is for recover, no information print.\r | |
114 | \r | |
115 | @retval EFI_LOAD_ERROR A load error occured.\r | |
116 | @retval EFI_SUCCESS The operation was successful.\r | |
117 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r | |
118 | **/\r | |
632820d1 | 119 | EFI_STATUS\r |
120 | HMemImageRead (\r | |
a1d4bfcc | 121 | IN UINTN Offset,\r |
122 | IN UINTN Size,\r | |
123 | IN BOOLEAN Recover\r | |
632820d1 | 124 | )\r |
632820d1 | 125 | {\r |
126 | \r | |
127 | EFI_STATUS Status;\r | |
128 | void *Buffer;\r | |
129 | CHAR16 *Str;\r | |
130 | HEFI_EDITOR_LINE *Line;\r | |
131 | \r | |
632820d1 | 132 | HBufferImage.BufferType = FileTypeMemBuffer;\r |
133 | \r | |
134 | Buffer = AllocateZeroPool (Size);\r | |
135 | if (Buffer == NULL) {\r | |
136 | StatusBarSetStatusString (L"Read Memory Failed");\r | |
137 | return EFI_OUT_OF_RESOURCES;\r | |
138 | }\r | |
139 | \r | |
140 | Status = HMemImage.IoFncs->Mem.Read (\r | |
141 | HMemImage.IoFncs,\r | |
142 | EfiPciWidthUint8,\r | |
143 | Offset,\r | |
144 | Size,\r | |
145 | Buffer\r | |
146 | );\r | |
147 | \r | |
148 | if (EFI_ERROR (Status)) {\r | |
149 | FreePool (Buffer);\r | |
150 | StatusBarSetStatusString (L"Memory Specified Not Accessible");\r | |
151 | return EFI_LOAD_ERROR;\r | |
152 | }\r | |
153 | \r | |
154 | HBufferImageFree ();\r | |
155 | \r | |
156 | Status = HBufferImageBufferToList (Buffer, Size);\r | |
157 | FreePool (Buffer);\r | |
158 | \r | |
159 | if (EFI_ERROR (Status)) {\r | |
160 | StatusBarSetStatusString (L"Read Memory Failed");\r | |
161 | return Status;\r | |
162 | }\r | |
163 | \r | |
164 | Status = HMemImageSetMemOffsetSize (Offset, Size);\r | |
165 | \r | |
166 | HBufferImage.DisplayPosition.Row = 2;\r | |
167 | HBufferImage.DisplayPosition.Column = 10;\r | |
168 | \r | |
169 | HBufferImage.MousePosition.Row = 2;\r | |
170 | HBufferImage.MousePosition.Column = 10;\r | |
171 | \r | |
172 | HBufferImage.LowVisibleRow = 1;\r | |
173 | HBufferImage.HighBits = TRUE;\r | |
174 | \r | |
175 | HBufferImage.BufferPosition.Row = 1;\r | |
176 | HBufferImage.BufferPosition.Column = 1;\r | |
177 | \r | |
178 | if (!Recover) {\r | |
179 | Str = CatSPrint(NULL, L"%d Lines Read", HBufferImage.NumLines);\r | |
180 | if (Str == NULL) {\r | |
181 | StatusBarSetStatusString (L"Read Memory Failed");\r | |
182 | return EFI_OUT_OF_RESOURCES;\r | |
183 | }\r | |
184 | \r | |
185 | StatusBarSetStatusString (Str);\r | |
186 | SHELL_FREE_NON_NULL (Str);\r | |
187 | \r | |
188 | HMainEditor.SelectStart = 0;\r | |
189 | HMainEditor.SelectEnd = 0;\r | |
190 | \r | |
191 | }\r | |
192 | \r | |
193 | //\r | |
194 | // has line\r | |
195 | //\r | |
196 | if (HBufferImage.Lines != NULL) {\r | |
197 | HBufferImage.CurrentLine = CR (HBufferImage.ListHead->ForwardLink, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);\r | |
198 | } else {\r | |
199 | //\r | |
200 | // create a dummy line\r | |
201 | //\r | |
202 | Line = HBufferImageCreateLine ();\r | |
203 | if (Line == NULL) {\r | |
204 | StatusBarSetStatusString (L"Read Memory Failed");\r | |
205 | return EFI_OUT_OF_RESOURCES;\r | |
206 | }\r | |
207 | \r | |
208 | HBufferImage.CurrentLine = Line;\r | |
209 | }\r | |
210 | \r | |
211 | HBufferImage.Modified = FALSE;\r | |
212 | HBufferImageNeedRefresh = TRUE;\r | |
213 | HBufferImageOnlyLineNeedRefresh = FALSE;\r | |
214 | HBufferImageMouseNeedRefresh = TRUE;\r | |
215 | \r | |
216 | return EFI_SUCCESS;\r | |
217 | \r | |
218 | }\r | |
219 | \r | |
a1d4bfcc | 220 | /**\r |
221 | Save lines in HBufferImage to disk.\r | |
222 | \r | |
223 | @param[in] Offset The offset.\r | |
224 | @param[in] Size The size.\r | |
225 | \r | |
226 | @retval EFI_LOAD_ERROR A load error occured.\r | |
227 | @retval EFI_SUCCESS The operation was successful.\r | |
228 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r | |
229 | **/\r | |
632820d1 | 230 | EFI_STATUS\r |
231 | HMemImageSave (\r | |
232 | IN UINTN Offset,\r | |
233 | IN UINTN Size\r | |
234 | )\r | |
632820d1 | 235 | {\r |
236 | \r | |
237 | EFI_STATUS Status;\r | |
238 | VOID *Buffer;\r | |
239 | \r | |
632820d1 | 240 | //\r |
241 | // not modified, so directly return\r | |
242 | //\r | |
243 | if (HBufferImage.Modified == FALSE) {\r | |
244 | return EFI_SUCCESS;\r | |
245 | }\r | |
246 | \r | |
632820d1 | 247 | HBufferImage.BufferType = FileTypeMemBuffer;\r |
248 | \r | |
249 | Buffer = AllocateZeroPool (Size);\r | |
250 | \r | |
251 | if (Buffer == NULL) {\r | |
252 | return EFI_OUT_OF_RESOURCES;\r | |
253 | }\r | |
254 | \r | |
255 | Status = HBufferImageListToBuffer (Buffer, Size);\r | |
256 | if (EFI_ERROR (Status)) {\r | |
257 | FreePool (Buffer);\r | |
258 | return Status;\r | |
259 | }\r | |
260 | //\r | |
261 | // write back to memory\r | |
262 | //\r | |
263 | Status = HMemImage.IoFncs->Mem.Write (\r | |
264 | HMemImage.IoFncs,\r | |
265 | EfiPciWidthUint8,\r | |
266 | Offset,\r | |
267 | Size,\r | |
268 | Buffer\r | |
269 | );\r | |
270 | \r | |
271 | FreePool (Buffer);\r | |
272 | \r | |
273 | if (EFI_ERROR (Status)) {\r | |
274 | return EFI_LOAD_ERROR;\r | |
275 | }\r | |
276 | //\r | |
277 | // now not modified\r | |
278 | //\r | |
279 | HBufferImage.Modified = FALSE;\r | |
280 | \r | |
281 | return EFI_SUCCESS;\r | |
282 | }\r | |
283 | \r | |
632820d1 | 284 | \r |