TianoCore EDK2 master
Loading...
Searching...
No Matches
DiskImage.c
Go to the documentation of this file.
1
9#include "HexEditor.h"
10#include <Protocol/BlockIo.h>
11
12extern EFI_HANDLE HImageHandleBackup;
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_DISK_IMAGE HDiskImage;
22HEFI_EDITOR_DISK_IMAGE HDiskImageBackupVar;
23
24//
25// for basic initialization of HDiskImage
26//
27HEFI_EDITOR_DISK_IMAGE HDiskImageConst = {
28 NULL,
29 0,
30 0,
31 0
32};
33
42 VOID
43 )
44{
45 //
46 // basically initialize the HDiskImage
47 //
48 CopyMem (&HDiskImage, &HDiskImageConst, sizeof (HDiskImage));
49
50 CopyMem (&HDiskImageBackupVar, &HDiskImageConst, sizeof (HDiskImageBackupVar));
51
52 return EFI_SUCCESS;
53}
54
64 VOID
65 )
66{
67 //
68 // backup the disk name, offset and size
69 //
70 //
71 SHELL_FREE_NON_NULL (HDiskImageBackupVar.Name);
72
73 HDiskImageBackupVar.Name = CatSPrint (NULL, L"%s", HDiskImage.Name);
74 if (HDiskImageBackupVar.Name == NULL) {
75 return EFI_OUT_OF_RESOURCES;
76 }
77
78 HDiskImageBackupVar.Offset = HDiskImage.Offset;
79 HDiskImageBackupVar.Size = HDiskImage.Size;
80
81 return EFI_SUCCESS;
82}
83
91 VOID
92 )
93{
94 SHELL_FREE_NON_NULL (HDiskImage.Name);
95 SHELL_FREE_NON_NULL (HDiskImageBackupVar.Name);
96
97 return EFI_SUCCESS;
98}
99
112 IN CONST CHAR16 *Str,
113 IN UINTN Offset,
114 IN UINTN Size
115 )
116{
117 if (Str == HDiskImage.Name) {
118 //
119 // This function might be called using HDiskImage.FileName as Str.
120 // Directly return without updating HDiskImage.FileName.
121 //
122 return EFI_SUCCESS;
123 }
124
125 //
126 // free the old file name
127 //
128 SHELL_FREE_NON_NULL (HDiskImage.Name);
129 HDiskImage.Name = AllocateCopyPool (StrSize (Str), Str);
130 if (HDiskImage.Name == NULL) {
131 return EFI_OUT_OF_RESOURCES;
132 }
133
134 HDiskImage.Offset = Offset;
135 HDiskImage.Size = Size;
136
137 return EFI_SUCCESS;
138}
139
155 IN CONST CHAR16 *DeviceName,
156 IN UINTN Offset,
157 IN UINTN Size,
158 IN BOOLEAN Recover
159 )
160{
162 EFI_DEVICE_PATH_PROTOCOL *DupDevicePath;
163 EFI_DEVICE_PATH_PROTOCOL *DupDevicePathForFree;
164 EFI_HANDLE Handle;
166 EFI_STATUS Status;
167
168 VOID *Buffer;
169 CHAR16 *Str;
170 UINTN Bytes;
171
172 HEFI_EDITOR_LINE *Line;
173
174 HBufferImage.BufferType = FileTypeDiskBuffer;
175
176 DevicePath = gEfiShellProtocol->GetDevicePathFromMap (DeviceName);
177 if (DevicePath == NULL) {
178 StatusBarSetStatusString (L"Cannot Find Device");
179 return EFI_INVALID_PARAMETER;
180 }
181
182 DupDevicePath = DuplicateDevicePath (DevicePath);
183 DupDevicePathForFree = DupDevicePath;
184 //
185 // get blkio interface
186 //
187 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &DupDevicePath, &Handle);
188 FreePool (DupDevicePathForFree);
189 if (EFI_ERROR (Status)) {
190 StatusBarSetStatusString (L"Read Disk Failed");
191 return Status;
192 }
193
194 Status = gBS->OpenProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlkIo, gImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
195 if (EFI_ERROR (Status)) {
196 StatusBarSetStatusString (L"Read Disk Failed");
197 return Status;
198 }
199
200 //
201 // if Offset exceeds LastBlock,
202 // return error
203 //
204 if ((Offset > BlkIo->Media->LastBlock) || (Offset + Size > BlkIo->Media->LastBlock)) {
205 StatusBarSetStatusString (L"Invalid Offset + Size");
206 return EFI_LOAD_ERROR;
207 }
208
209 Bytes = BlkIo->Media->BlockSize * Size;
210 Buffer = AllocateZeroPool (Bytes);
211
212 if (Buffer == NULL) {
213 StatusBarSetStatusString (L"Read Disk Failed");
214 return EFI_OUT_OF_RESOURCES;
215 }
216
217 //
218 // read from disk
219 //
220 Status = BlkIo->ReadBlocks (
221 BlkIo,
222 BlkIo->Media->MediaId,
223 Offset,
224 Bytes,
225 Buffer
226 );
227
228 if (EFI_ERROR (Status)) {
229 FreePool (Buffer);
230 StatusBarSetStatusString (L"Read Disk Failed");
231 return EFI_LOAD_ERROR;
232 }
233
235
236 //
237 // convert buffer to line list
238 //
239 Status = HBufferImageBufferToList (Buffer, Bytes);
240 FreePool (Buffer);
241
242 if (EFI_ERROR (Status)) {
243 StatusBarSetStatusString (L"Read Disk Failed");
244 return Status;
245 }
246
247 Status = HDiskImageSetDiskNameOffsetSize (DeviceName, Offset, Size);
248 if (EFI_ERROR (Status)) {
249 StatusBarSetStatusString (L"Read Disk Failed");
250 return EFI_OUT_OF_RESOURCES;
251 }
252
253 //
254 // initialize some variables
255 //
256 HDiskImage.BlockSize = BlkIo->Media->BlockSize;
257
258 HBufferImage.DisplayPosition.Row = 2;
259 HBufferImage.DisplayPosition.Column = 10;
260
261 HBufferImage.MousePosition.Row = 2;
262 HBufferImage.MousePosition.Column = 10;
263
264 HBufferImage.LowVisibleRow = 1;
265 HBufferImage.HighBits = TRUE;
266
267 HBufferImage.BufferPosition.Row = 1;
268 HBufferImage.BufferPosition.Column = 1;
269
270 if (!Recover) {
271 Str = CatSPrint (NULL, L"%d Lines Read", HBufferImage.NumLines);
272 if (Str == NULL) {
273 StatusBarSetStatusString (L"Read Disk Failed");
274 return EFI_OUT_OF_RESOURCES;
275 }
276
278 SHELL_FREE_NON_NULL (Str);
279
280 HMainEditor.SelectStart = 0;
281 HMainEditor.SelectEnd = 0;
282 }
283
284 //
285 // has line
286 //
287 if (HBufferImage.Lines != NULL) {
288 HBufferImage.CurrentLine = CR (
289 HBufferImage.ListHead->ForwardLink,
291 Link,
292 EFI_EDITOR_LINE_LIST
293 );
294 } else {
295 //
296 // create a dummy line
297 //
298 Line = HBufferImageCreateLine ();
299 if (Line == NULL) {
300 StatusBarSetStatusString (L"Read Disk Failed");
301 return EFI_OUT_OF_RESOURCES;
302 }
303
304 HBufferImage.CurrentLine = Line;
305 }
306
307 HBufferImage.Modified = FALSE;
308 HBufferImageNeedRefresh = TRUE;
309 HBufferImageOnlyLineNeedRefresh = FALSE;
310 HBufferImageMouseNeedRefresh = TRUE;
311
312 return EFI_SUCCESS;
313}
314
330 IN CHAR16 *DeviceName,
331 IN UINTN Offset,
332 IN UINTN Size
333 )
334{
336 EFI_DEVICE_PATH_PROTOCOL *DupDevicePath;
337 EFI_DEVICE_PATH_PROTOCOL *DupDevicePathForFree;
339 EFI_STATUS Status;
340 EFI_HANDLE Handle;
341 VOID *Buffer;
342 UINTN Bytes;
343
344 //
345 // if not modified, directly return
346 //
347 if (HBufferImage.Modified == FALSE) {
348 return EFI_SUCCESS;
349 }
350
351 HBufferImage.BufferType = FileTypeDiskBuffer;
352
353 DevicePath = gEfiShellProtocol->GetDevicePathFromMap (DeviceName);
354 if (DevicePath == NULL) {
355 // StatusBarSetStatusString (L"Cannot Find Device");
356 return EFI_INVALID_PARAMETER;
357 }
358
359 DupDevicePath = DuplicateDevicePath (DevicePath);
360 DupDevicePathForFree = DupDevicePath;
361
362 //
363 // get blkio interface
364 //
365 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &DupDevicePath, &Handle);
366 FreePool (DupDevicePathForFree);
367 if (EFI_ERROR (Status)) {
368 // StatusBarSetStatusString (L"Read Disk Failed");
369 return Status;
370 }
371
372 Status = gBS->OpenProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlkIo, gImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
373 if (EFI_ERROR (Status)) {
374 // StatusBarSetStatusString (L"Read Disk Failed");
375 return Status;
376 }
377
378 Bytes = BlkIo->Media->BlockSize * Size;
379 Buffer = AllocateZeroPool (Bytes);
380
381 if (Buffer == NULL) {
382 return EFI_OUT_OF_RESOURCES;
383 }
384
385 //
386 // concatenate the line list to a buffer
387 //
388 Status = HBufferImageListToBuffer (Buffer, Bytes);
389 if (EFI_ERROR (Status)) {
390 FreePool (Buffer);
391 return Status;
392 }
393
394 //
395 // write the buffer to disk
396 //
397 Status = BlkIo->WriteBlocks (
398 BlkIo,
399 BlkIo->Media->MediaId,
400 Offset,
401 Bytes,
402 Buffer
403 );
404
405 FreePool (Buffer);
406
407 if (EFI_ERROR (Status)) {
408 return EFI_LOAD_ERROR;
409 }
410
411 //
412 // now not modified
413 //
414 HBufferImage.Modified = FALSE;
415
416 return EFI_SUCCESS;
417}
UINT64 UINTN
UINTN EFIAPI StrSize(IN CONST CHAR16 *String)
Definition: String.c:72
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_DEVICE_PATH_PROTOCOL *EFIAPI DuplicateDevicePath(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath)
EFI_STATUS HDiskImageCleanup(VOID)
Definition: DiskImage.c:90
EFI_STATUS HDiskImageSetDiskNameOffsetSize(IN CONST CHAR16 *Str, IN UINTN Offset, IN UINTN Size)
Definition: DiskImage.c:111
EFI_STATUS HDiskImageSave(IN CHAR16 *DeviceName, IN UINTN Offset, IN UINTN Size)
Definition: DiskImage.c:329
EFI_STATUS HDiskImageInit(VOID)
Definition: DiskImage.c:41
EFI_STATUS HDiskImageBackup(VOID)
Definition: DiskImage.c:63
EFI_STATUS HDiskImageRead(IN CONST CHAR16 *DeviceName, IN UINTN Offset, IN UINTN Size, IN BOOLEAN Recover)
Definition: DiskImage.c:154
EFI_STATUS StatusBarSetStatusString(IN CHAR16 *Str)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
VOID *EFIAPI AllocateCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#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
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_HANDLE gImageHandle
EFI_BOOT_SERVICES * gBS
CHAR16 *EFIAPI CatSPrint(IN CHAR16 *String OPTIONAL, IN CONST CHAR16 *FormatString,...)
Definition: UefiLibPrint.c:827
EFI_BLOCK_IO_MEDIA * Media
Definition: BlockIo.h:224
UINT32 BlockSize
Definition: BlockIo.h:167
EFI_LBA LastBlock
Definition: BlockIo.h:178