TianoCore EDK2 master
Loading...
Searching...
No Matches
MemImage.c
Go to the documentation of this file.
1
9#include "HexEditor.h"
10
11extern EFI_HANDLE HImageHandleBackup;
12
13extern HEFI_EDITOR_BUFFER_IMAGE HBufferImage;
14
15extern BOOLEAN HBufferImageNeedRefresh;
16extern BOOLEAN HBufferImageOnlyLineNeedRefresh;
17extern BOOLEAN HBufferImageMouseNeedRefresh;
18
19extern HEFI_EDITOR_GLOBAL_EDITOR HMainEditor;
20
21HEFI_EDITOR_MEM_IMAGE HMemImage;
22HEFI_EDITOR_MEM_IMAGE HMemImageBackupVar;
23
24//
25// for basic initialization of HDiskImage
26//
27HEFI_EDITOR_MEM_IMAGE HMemImageConst = {
28 NULL,
29 0,
30 0
31};
32
41 VOID
42 )
43{
44 EFI_STATUS Status;
45
46 //
47 // basically initialize the HMemImage
48 //
49 CopyMem (&HMemImage, &HMemImageConst, sizeof (HMemImage));
50
51 Status = gBS->LocateProtocol (
52 &gEfiCpuIo2ProtocolGuid,
53 NULL,
54 (VOID **)&HMemImage.IoFncs
55 );
56 if (!EFI_ERROR (Status)) {
57 return EFI_SUCCESS;
58 } else {
59 return EFI_LOAD_ERROR;
60 }
61}
62
71 VOID
72 )
73{
74 HMemImageBackupVar.Offset = HMemImage.Offset;
75 HMemImageBackupVar.Size = HMemImage.Size;
76
77 return EFI_SUCCESS;
78}
79
91 IN UINTN Offset,
92 IN UINTN Size
93 )
94{
95 HMemImage.Offset = Offset;
96 HMemImage.Size = Size;
97
98 return EFI_SUCCESS;
99}
100
114 IN UINTN Offset,
115 IN UINTN Size,
116 IN BOOLEAN Recover
117 )
118{
119 EFI_STATUS Status;
120 void *Buffer;
121 CHAR16 *Str;
122 HEFI_EDITOR_LINE *Line;
123
124 HBufferImage.BufferType = FileTypeMemBuffer;
125
126 Buffer = AllocateZeroPool (Size);
127 if (Buffer == NULL) {
128 StatusBarSetStatusString (L"Read Memory Failed");
129 return EFI_OUT_OF_RESOURCES;
130 }
131
132 Status = HMemImage.IoFncs->Mem.Read (
133 HMemImage.IoFncs,
134 EfiCpuIoWidthUint8,
135 Offset,
136 Size,
137 Buffer
138 );
139
140 if (EFI_ERROR (Status)) {
141 FreePool (Buffer);
142 StatusBarSetStatusString (L"Memory Specified Not Accessible");
143 return EFI_LOAD_ERROR;
144 }
145
147
148 Status = HBufferImageBufferToList (Buffer, Size);
149 FreePool (Buffer);
150
151 if (EFI_ERROR (Status)) {
152 StatusBarSetStatusString (L"Read Memory Failed");
153 return Status;
154 }
155
156 Status = HMemImageSetMemOffsetSize (Offset, Size);
157
158 HBufferImage.DisplayPosition.Row = 2;
159 HBufferImage.DisplayPosition.Column = 10;
160
161 HBufferImage.MousePosition.Row = 2;
162 HBufferImage.MousePosition.Column = 10;
163
164 HBufferImage.LowVisibleRow = 1;
165 HBufferImage.HighBits = TRUE;
166
167 HBufferImage.BufferPosition.Row = 1;
168 HBufferImage.BufferPosition.Column = 1;
169
170 if (!Recover) {
171 Str = CatSPrint (NULL, L"%d Lines Read", HBufferImage.NumLines);
172 if (Str == NULL) {
173 StatusBarSetStatusString (L"Read Memory Failed");
174 return EFI_OUT_OF_RESOURCES;
175 }
176
178 SHELL_FREE_NON_NULL (Str);
179
180 HMainEditor.SelectStart = 0;
181 HMainEditor.SelectEnd = 0;
182 }
183
184 //
185 // has line
186 //
187 if (HBufferImage.Lines != NULL) {
188 HBufferImage.CurrentLine = CR (HBufferImage.ListHead->ForwardLink, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);
189 } else {
190 //
191 // create a dummy line
192 //
193 Line = HBufferImageCreateLine ();
194 if (Line == NULL) {
195 StatusBarSetStatusString (L"Read Memory Failed");
196 return EFI_OUT_OF_RESOURCES;
197 }
198
199 HBufferImage.CurrentLine = Line;
200 }
201
202 HBufferImage.Modified = FALSE;
203 HBufferImageNeedRefresh = TRUE;
204 HBufferImageOnlyLineNeedRefresh = FALSE;
205 HBufferImageMouseNeedRefresh = TRUE;
206
207 return EFI_SUCCESS;
208}
209
222 IN UINTN Offset,
223 IN UINTN Size
224 )
225{
226 EFI_STATUS Status;
227 VOID *Buffer;
228
229 //
230 // not modified, so directly return
231 //
232 if (HBufferImage.Modified == FALSE) {
233 return EFI_SUCCESS;
234 }
235
236 HBufferImage.BufferType = FileTypeMemBuffer;
237
238 Buffer = AllocateZeroPool (Size);
239
240 if (Buffer == NULL) {
241 return EFI_OUT_OF_RESOURCES;
242 }
243
244 Status = HBufferImageListToBuffer (Buffer, Size);
245 if (EFI_ERROR (Status)) {
246 FreePool (Buffer);
247 return Status;
248 }
249
250 //
251 // write back to memory
252 //
253 Status = HMemImage.IoFncs->Mem.Write (
254 HMemImage.IoFncs,
255 EfiCpuIoWidthUint8,
256 Offset,
257 Size,
258 Buffer
259 );
260
261 FreePool (Buffer);
262
263 if (EFI_ERROR (Status)) {
264 return EFI_LOAD_ERROR;
265 }
266
267 //
268 // now not modified
269 //
270 HBufferImage.Modified = FALSE;
271
272 return EFI_SUCCESS;
273}
UINT64 UINTN
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
EFI_STATUS HBufferImageFree(VOID)
Definition: BufferImage.c:1082
EFI_STATUS HBufferImageListToBuffer(IN VOID *Buffer, IN UINTN Bytes)
Definition: BufferImage.c:2238
EFI_STATUS HBufferImageBufferToList(IN VOID *Buffer, IN UINTN Bytes)
Definition: BufferImage.c:2175
HEFI_EDITOR_LINE * HBufferImageCreateLine(VOID)
Definition: BufferImage.c:1040
EFI_STATUS StatusBarSetStatusString(IN CHAR16 *Str)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define CR(Record, TYPE, Field, TestSignature)
Definition: DebugLib.h:659
EFI_STATUS HMemImageSetMemOffsetSize(IN UINTN Offset, IN UINTN Size)
Definition: MemImage.c:90
EFI_STATUS HMemImageSave(IN UINTN Offset, IN UINTN Size)
Definition: MemImage.c:221
EFI_STATUS HMemImageInit(VOID)
Definition: MemImage.c:40
EFI_STATUS HMemImageRead(IN UINTN Offset, IN UINTN Size, IN BOOLEAN Recover)
Definition: MemImage.c:113
EFI_STATUS HMemImageBackup(VOID)
Definition: MemImage.c:70
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_BOOT_SERVICES * gBS
CHAR16 *EFIAPI CatSPrint(IN CHAR16 *String OPTIONAL, IN CONST CHAR16 *FormatString,...)
Definition: UefiLibPrint.c:827
EFI_CPU_IO_PROTOCOL_ACCESS Mem
Definition: CpuIo2.h:127
EFI_CPU_IO_PROTOCOL_IO_MEM Read
Definition: CpuIo2.h:112
EFI_CPU_IO_PROTOCOL_IO_MEM Write
Definition: CpuIo2.h:116