]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/MemImage.c
ShellPkg/for: Fix potential null pointer deference
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / HexEdit / MemImage.c
1 /** @file
2 Functions to deal with Mem buffer
3
4 Copyright (c) 2005 - 2017, 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 //
31 // for basic initialization of HDiskImage
32 //
33 HEFI_EDITOR_MEM_IMAGE HMemImageConst = {
34 NULL,
35 0,
36 0
37 };
38
39 /**
40 Initialization function for HDiskImage.
41
42 @retval EFI_SUCCESS The operation was successful.
43 @retval EFI_LOAD_ERROR A load error occured.
44 **/
45 EFI_STATUS
46 HMemImageInit (
47 VOID
48 )
49 {
50 EFI_STATUS Status;
51
52 //
53 // basically initialize the HMemImage
54 //
55 CopyMem (&HMemImage, &HMemImageConst, sizeof (HMemImage));
56
57 Status = gBS->LocateProtocol (
58 &gEfiCpuIo2ProtocolGuid,
59 NULL,
60 (VOID**)&HMemImage.IoFncs
61 );
62 if (!EFI_ERROR (Status)) {
63 return EFI_SUCCESS;
64 } else {
65 return EFI_LOAD_ERROR;
66 }
67 }
68
69 /**
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.
72
73 @retval EFI_SUCCESS The operation was successful.
74 **/
75 EFI_STATUS
76 HMemImageBackup (
77 VOID
78 )
79 {
80 HMemImageBackupVar.Offset = HMemImage.Offset;
81 HMemImageBackupVar.Size = HMemImage.Size;
82
83 return EFI_SUCCESS;
84 }
85
86 /**
87 Set FileName field in HFileImage.
88
89 @param[in] Offset The offset.
90 @param[in] Size The size.
91
92 @retval EFI_SUCCESS The operation was successful.
93 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
94 **/
95 EFI_STATUS
96 HMemImageSetMemOffsetSize (
97 IN UINTN Offset,
98 IN UINTN Size
99 )
100 {
101
102 HMemImage.Offset = Offset;
103 HMemImage.Size = Size;
104
105 return EFI_SUCCESS;
106 }
107
108 /**
109 Read a disk from disk into HBufferImage.
110
111 @param[in] Offset The offset.
112 @param[in] Size The size.
113 @param[in] Recover if is for recover, no information print.
114
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.
118 **/
119 EFI_STATUS
120 HMemImageRead (
121 IN UINTN Offset,
122 IN UINTN Size,
123 IN BOOLEAN Recover
124 )
125 {
126
127 EFI_STATUS Status;
128 void *Buffer;
129 CHAR16 *Str;
130 HEFI_EDITOR_LINE *Line;
131
132 HBufferImage.BufferType = FileTypeMemBuffer;
133
134 Buffer = AllocateZeroPool (Size);
135 if (Buffer == NULL) {
136 StatusBarSetStatusString (L"Read Memory Failed");
137 return EFI_OUT_OF_RESOURCES;
138 }
139
140 Status = HMemImage.IoFncs->Mem.Read (
141 HMemImage.IoFncs,
142 EfiCpuIoWidthUint8,
143 Offset,
144 Size,
145 Buffer
146 );
147
148 if (EFI_ERROR (Status)) {
149 FreePool (Buffer);
150 StatusBarSetStatusString (L"Memory Specified Not Accessible");
151 return EFI_LOAD_ERROR;
152 }
153
154 HBufferImageFree ();
155
156 Status = HBufferImageBufferToList (Buffer, Size);
157 FreePool (Buffer);
158
159 if (EFI_ERROR (Status)) {
160 StatusBarSetStatusString (L"Read Memory Failed");
161 return Status;
162 }
163
164 Status = HMemImageSetMemOffsetSize (Offset, Size);
165
166 HBufferImage.DisplayPosition.Row = 2;
167 HBufferImage.DisplayPosition.Column = 10;
168
169 HBufferImage.MousePosition.Row = 2;
170 HBufferImage.MousePosition.Column = 10;
171
172 HBufferImage.LowVisibleRow = 1;
173 HBufferImage.HighBits = TRUE;
174
175 HBufferImage.BufferPosition.Row = 1;
176 HBufferImage.BufferPosition.Column = 1;
177
178 if (!Recover) {
179 Str = CatSPrint(NULL, L"%d Lines Read", HBufferImage.NumLines);
180 if (Str == NULL) {
181 StatusBarSetStatusString (L"Read Memory Failed");
182 return EFI_OUT_OF_RESOURCES;
183 }
184
185 StatusBarSetStatusString (Str);
186 SHELL_FREE_NON_NULL (Str);
187
188 HMainEditor.SelectStart = 0;
189 HMainEditor.SelectEnd = 0;
190
191 }
192
193 //
194 // has line
195 //
196 if (HBufferImage.Lines != NULL) {
197 HBufferImage.CurrentLine = CR (HBufferImage.ListHead->ForwardLink, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);
198 } else {
199 //
200 // create a dummy line
201 //
202 Line = HBufferImageCreateLine ();
203 if (Line == NULL) {
204 StatusBarSetStatusString (L"Read Memory Failed");
205 return EFI_OUT_OF_RESOURCES;
206 }
207
208 HBufferImage.CurrentLine = Line;
209 }
210
211 HBufferImage.Modified = FALSE;
212 HBufferImageNeedRefresh = TRUE;
213 HBufferImageOnlyLineNeedRefresh = FALSE;
214 HBufferImageMouseNeedRefresh = TRUE;
215
216 return EFI_SUCCESS;
217
218 }
219
220 /**
221 Save lines in HBufferImage to disk.
222
223 @param[in] Offset The offset.
224 @param[in] Size The size.
225
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.
229 **/
230 EFI_STATUS
231 HMemImageSave (
232 IN UINTN Offset,
233 IN UINTN Size
234 )
235 {
236
237 EFI_STATUS Status;
238 VOID *Buffer;
239
240 //
241 // not modified, so directly return
242 //
243 if (HBufferImage.Modified == FALSE) {
244 return EFI_SUCCESS;
245 }
246
247 HBufferImage.BufferType = FileTypeMemBuffer;
248
249 Buffer = AllocateZeroPool (Size);
250
251 if (Buffer == NULL) {
252 return EFI_OUT_OF_RESOURCES;
253 }
254
255 Status = HBufferImageListToBuffer (Buffer, Size);
256 if (EFI_ERROR (Status)) {
257 FreePool (Buffer);
258 return Status;
259 }
260 //
261 // write back to memory
262 //
263 Status = HMemImage.IoFncs->Mem.Write (
264 HMemImage.IoFncs,
265 EfiCpuIoWidthUint8,
266 Offset,
267 Size,
268 Buffer
269 );
270
271 FreePool (Buffer);
272
273 if (EFI_ERROR (Status)) {
274 return EFI_LOAD_ERROR;
275 }
276 //
277 // now not modified
278 //
279 HBufferImage.Modified = FALSE;
280
281 return EFI_SUCCESS;
282 }
283
284