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