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 HBufferImage
.BufferType
= FileTypeMemBuffer
;
195 Buffer
= AllocateZeroPool (Size
);
196 if (Buffer
== NULL
) {
197 StatusBarSetStatusString (L
"Read Memory Failed");
198 return EFI_OUT_OF_RESOURCES
;
201 Status
= HMemImage
.IoFncs
->Mem
.Read (
209 if (EFI_ERROR (Status
)) {
211 StatusBarSetStatusString (L
"Memory Specified Not Accessible");
212 return EFI_LOAD_ERROR
;
217 Status
= HBufferImageBufferToList (Buffer
, Size
);
220 if (EFI_ERROR (Status
)) {
221 StatusBarSetStatusString (L
"Read Memory Failed");
225 Status
= HMemImageSetMemOffsetSize (Offset
, Size
);
227 HBufferImage
.DisplayPosition
.Row
= 2;
228 HBufferImage
.DisplayPosition
.Column
= 10;
230 HBufferImage
.MousePosition
.Row
= 2;
231 HBufferImage
.MousePosition
.Column
= 10;
233 HBufferImage
.LowVisibleRow
= 1;
234 HBufferImage
.HighBits
= TRUE
;
236 HBufferImage
.BufferPosition
.Row
= 1;
237 HBufferImage
.BufferPosition
.Column
= 1;
240 Str
= CatSPrint(NULL
, L
"%d Lines Read", HBufferImage
.NumLines
);
242 StatusBarSetStatusString (L
"Read Memory Failed");
243 return EFI_OUT_OF_RESOURCES
;
246 StatusBarSetStatusString (Str
);
247 SHELL_FREE_NON_NULL (Str
);
249 HMainEditor
.SelectStart
= 0;
250 HMainEditor
.SelectEnd
= 0;
257 if (HBufferImage
.Lines
!= NULL
) {
258 HBufferImage
.CurrentLine
= CR (HBufferImage
.ListHead
->ForwardLink
, HEFI_EDITOR_LINE
, Link
, EFI_EDITOR_LINE_LIST
);
261 // create a dummy line
263 Line
= HBufferImageCreateLine ();
265 StatusBarSetStatusString (L
"Read Memory Failed");
266 return EFI_OUT_OF_RESOURCES
;
269 HBufferImage
.CurrentLine
= Line
;
272 HBufferImage
.Modified
= FALSE
;
273 HBufferImageNeedRefresh
= TRUE
;
274 HBufferImageOnlyLineNeedRefresh
= FALSE
;
275 HBufferImageMouseNeedRefresh
= TRUE
;
282 Save lines in HBufferImage to disk.
284 @param[in] Offset The offset.
285 @param[in] Size The size.
287 @retval EFI_LOAD_ERROR A load error occured.
288 @retval EFI_SUCCESS The operation was successful.
289 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
302 // not modified, so directly return
304 if (HBufferImage
.Modified
== FALSE
) {
308 HBufferImage
.BufferType
= FileTypeMemBuffer
;
310 Buffer
= AllocateZeroPool (Size
);
312 if (Buffer
== NULL
) {
313 return EFI_OUT_OF_RESOURCES
;
316 Status
= HBufferImageListToBuffer (Buffer
, Size
);
317 if (EFI_ERROR (Status
)) {
322 // write back to memory
324 Status
= HMemImage
.IoFncs
->Mem
.Write (
334 if (EFI_ERROR (Status
)) {
335 return EFI_LOAD_ERROR
;
340 HBufferImage
.Modified
= FALSE
;