2 Functions to deal with Mem buffer
4 Copyright (c) 2005 - 2018, 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
;
31 // for basic initialization of HDiskImage
33 HEFI_EDITOR_MEM_IMAGE HMemImageConst
= {
40 Initialization function for HDiskImage.
42 @retval EFI_SUCCESS The operation was successful.
43 @retval EFI_LOAD_ERROR A load error occured.
53 // basically initialize the HMemImage
55 CopyMem (&HMemImage
, &HMemImageConst
, sizeof (HMemImage
));
57 Status
= gBS
->LocateProtocol (
58 &gEfiCpuIo2ProtocolGuid
,
60 (VOID
**)&HMemImage
.IoFncs
62 if (!EFI_ERROR (Status
)) {
65 return EFI_LOAD_ERROR
;
70 Backup function for HDiskImage. Only a few fields need to be backup.
71 This is for making the Disk buffer refresh as few as possible.
73 @retval EFI_SUCCESS The operation was successful.
80 HMemImageBackupVar
.Offset
= HMemImage
.Offset
;
81 HMemImageBackupVar
.Size
= HMemImage
.Size
;
87 Set FileName field in HFileImage.
89 @param[in] Offset The offset.
90 @param[in] Size The size.
92 @retval EFI_SUCCESS The operation was successful.
93 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
96 HMemImageSetMemOffsetSize (
102 HMemImage
.Offset
= Offset
;
103 HMemImage
.Size
= Size
;
109 Read a disk from disk into HBufferImage.
111 @param[in] Offset The offset.
112 @param[in] Size The size.
113 @param[in] Recover if is for recover, no information print.
115 @retval EFI_LOAD_ERROR A load error occured.
116 @retval EFI_SUCCESS The operation was successful.
117 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
130 HEFI_EDITOR_LINE
*Line
;
132 HBufferImage
.BufferType
= FileTypeMemBuffer
;
134 Buffer
= AllocateZeroPool (Size
);
135 if (Buffer
== NULL
) {
136 StatusBarSetStatusString (L
"Read Memory Failed");
137 return EFI_OUT_OF_RESOURCES
;
140 Status
= HMemImage
.IoFncs
->Mem
.Read (
148 if (EFI_ERROR (Status
)) {
150 StatusBarSetStatusString (L
"Memory Specified Not Accessible");
151 return EFI_LOAD_ERROR
;
156 Status
= HBufferImageBufferToList (Buffer
, Size
);
159 if (EFI_ERROR (Status
)) {
160 StatusBarSetStatusString (L
"Read Memory Failed");
164 Status
= HMemImageSetMemOffsetSize (Offset
, Size
);
166 HBufferImage
.DisplayPosition
.Row
= 2;
167 HBufferImage
.DisplayPosition
.Column
= 10;
169 HBufferImage
.MousePosition
.Row
= 2;
170 HBufferImage
.MousePosition
.Column
= 10;
172 HBufferImage
.LowVisibleRow
= 1;
173 HBufferImage
.HighBits
= TRUE
;
175 HBufferImage
.BufferPosition
.Row
= 1;
176 HBufferImage
.BufferPosition
.Column
= 1;
179 Str
= CatSPrint(NULL
, L
"%d Lines Read", HBufferImage
.NumLines
);
181 StatusBarSetStatusString (L
"Read Memory Failed");
182 return EFI_OUT_OF_RESOURCES
;
185 StatusBarSetStatusString (Str
);
186 SHELL_FREE_NON_NULL (Str
);
188 HMainEditor
.SelectStart
= 0;
189 HMainEditor
.SelectEnd
= 0;
196 if (HBufferImage
.Lines
!= NULL
) {
197 HBufferImage
.CurrentLine
= CR (HBufferImage
.ListHead
->ForwardLink
, HEFI_EDITOR_LINE
, Link
, EFI_EDITOR_LINE_LIST
);
200 // create a dummy line
202 Line
= HBufferImageCreateLine ();
204 StatusBarSetStatusString (L
"Read Memory Failed");
205 return EFI_OUT_OF_RESOURCES
;
208 HBufferImage
.CurrentLine
= Line
;
211 HBufferImage
.Modified
= FALSE
;
212 HBufferImageNeedRefresh
= TRUE
;
213 HBufferImageOnlyLineNeedRefresh
= FALSE
;
214 HBufferImageMouseNeedRefresh
= TRUE
;
221 Save lines in HBufferImage to disk.
223 @param[in] Offset The offset.
224 @param[in] Size The size.
226 @retval EFI_LOAD_ERROR A load error occured.
227 @retval EFI_SUCCESS The operation was successful.
228 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
241 // not modified, so directly return
243 if (HBufferImage
.Modified
== FALSE
) {
247 HBufferImage
.BufferType
= FileTypeMemBuffer
;
249 Buffer
= AllocateZeroPool (Size
);
251 if (Buffer
== NULL
) {
252 return EFI_OUT_OF_RESOURCES
;
255 Status
= HBufferImageListToBuffer (Buffer
, Size
);
256 if (EFI_ERROR (Status
)) {
261 // write back to memory
263 Status
= HMemImage
.IoFncs
->Mem
.Write (
273 if (EFI_ERROR (Status
)) {
274 return EFI_LOAD_ERROR
;
279 HBufferImage
.Modified
= FALSE
;