2 Functions to deal with Mem buffer
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
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.
15 #include "HexEditor.h"
17 extern EFI_HANDLE HImageHandleBackup
;
19 extern HEFI_EDITOR_BUFFER_IMAGE HBufferImage
;
21 extern BOOLEAN HBufferImageNeedRefresh
;
22 extern BOOLEAN HBufferImageOnlyLineNeedRefresh
;
23 extern BOOLEAN HBufferImageMouseNeedRefresh
;
25 extern HEFI_EDITOR_GLOBAL_EDITOR HMainEditor
;
27 HEFI_EDITOR_MEM_IMAGE HMemImage
;
28 HEFI_EDITOR_MEM_IMAGE HMemImageBackupVar
;
30 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL DummyPciRootBridgeIo
;
33 // for basic initialization of HDiskImage
35 HEFI_EDITOR_MEM_IMAGE HMemImageConst
= {
42 Empty function. always returns the same.
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.
50 @retval EFI_UNSUPPORTED.
55 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
* This
,
56 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
,
62 return EFI_UNSUPPORTED
;
66 Empty function. always returns the same.
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.
74 @retval EFI_UNSUPPORTED.
79 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
* This
,
80 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
,
86 return EFI_UNSUPPORTED
;
90 Initialization function for HDiskImage.
92 @retval EFI_SUCCESS The operation was successful.
93 @retval EFI_LOAD_ERROR A load error occured.
103 // basically initialize the HMemImage
105 CopyMem (&HMemImage
, &HMemImageConst
, sizeof (HMemImage
));
107 Status
= gBS
->LocateProtocol (
108 &gEfiPciRootBridgeIoProtocolGuid
,
110 (VOID
**)&HMemImage
.IoFncs
112 if (Status
== EFI_NOT_FOUND
) {
114 // For NT32, no EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL is available
115 // Use Dummy PciRootBridgeIo for memory access
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
;
123 if (!EFI_ERROR (Status
)) {
126 return EFI_LOAD_ERROR
;
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.
134 @retval EFI_SUCCESS The operation was successful.
141 HMemImageBackupVar
.Offset
= HMemImage
.Offset
;
142 HMemImageBackupVar
.Size
= HMemImage
.Size
;
148 Set FileName field in HFileImage.
150 @param[in] Offset The offset.
151 @param[in] Size The size.
153 @retval EFI_SUCCESS The operation was successful.
154 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
157 HMemImageSetMemOffsetSize (
163 HMemImage
.Offset
= Offset
;
164 HMemImage
.Size
= Size
;
170 Read a disk from disk into HBufferImage.
172 @param[in] Offset The offset.
173 @param[in] Size The size.
174 @param[in] Recover if is for recover, no information print.
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.
191 HEFI_EDITOR_LINE
*Line
;
193 EDIT_FILE_TYPE BufferTypeBackup
;
195 BufferTypeBackup
= HBufferImage
.BufferType
;
196 HBufferImage
.BufferType
= FileTypeMemBuffer
;
198 Buffer
= AllocateZeroPool (Size
);
199 if (Buffer
== NULL
) {
200 StatusBarSetStatusString (L
"Read Memory Failed");
201 return EFI_OUT_OF_RESOURCES
;
204 Status
= HMemImage
.IoFncs
->Mem
.Read (
212 if (EFI_ERROR (Status
)) {
214 StatusBarSetStatusString (L
"Memory Specified Not Accessible");
215 return EFI_LOAD_ERROR
;
220 Status
= HBufferImageBufferToList (Buffer
, Size
);
223 if (EFI_ERROR (Status
)) {
224 StatusBarSetStatusString (L
"Read Memory Failed");
228 Status
= HMemImageSetMemOffsetSize (Offset
, Size
);
230 HBufferImage
.DisplayPosition
.Row
= 2;
231 HBufferImage
.DisplayPosition
.Column
= 10;
233 HBufferImage
.MousePosition
.Row
= 2;
234 HBufferImage
.MousePosition
.Column
= 10;
236 HBufferImage
.LowVisibleRow
= 1;
237 HBufferImage
.HighBits
= TRUE
;
239 HBufferImage
.BufferPosition
.Row
= 1;
240 HBufferImage
.BufferPosition
.Column
= 1;
243 Str
= CatSPrint(NULL
, L
"%d Lines Read", HBufferImage
.NumLines
);
245 StatusBarSetStatusString (L
"Read Memory Failed");
246 return EFI_OUT_OF_RESOURCES
;
249 StatusBarSetStatusString (Str
);
250 SHELL_FREE_NON_NULL (Str
);
252 HMainEditor
.SelectStart
= 0;
253 HMainEditor
.SelectEnd
= 0;
260 if (HBufferImage
.Lines
!= NULL
) {
261 HBufferImage
.CurrentLine
= CR (HBufferImage
.ListHead
->ForwardLink
, HEFI_EDITOR_LINE
, Link
, EFI_EDITOR_LINE_LIST
);
264 // create a dummy line
266 Line
= HBufferImageCreateLine ();
268 StatusBarSetStatusString (L
"Read Memory Failed");
269 return EFI_OUT_OF_RESOURCES
;
272 HBufferImage
.CurrentLine
= Line
;
275 HBufferImage
.Modified
= FALSE
;
276 HBufferImageNeedRefresh
= TRUE
;
277 HBufferImageOnlyLineNeedRefresh
= FALSE
;
278 HBufferImageMouseNeedRefresh
= TRUE
;
285 Save lines in HBufferImage to disk.
287 @param[in] Offset The offset.
288 @param[in] Size The size.
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.
304 EDIT_FILE_TYPE BufferTypeBackup
;
307 // not modified, so directly return
309 if (HBufferImage
.Modified
== FALSE
) {
313 BufferTypeBackup
= HBufferImage
.BufferType
;
314 HBufferImage
.BufferType
= FileTypeMemBuffer
;
316 Buffer
= AllocateZeroPool (Size
);
318 if (Buffer
== NULL
) {
319 return EFI_OUT_OF_RESOURCES
;
322 Status
= HBufferImageListToBuffer (Buffer
, Size
);
323 if (EFI_ERROR (Status
)) {
328 // write back to memory
330 Status
= HMemImage
.IoFncs
->Mem
.Write (
340 if (EFI_ERROR (Status
)) {
341 return EFI_LOAD_ERROR
;
346 HBufferImage
.Modified
= FALSE
;