2 Functions to deal with Disk buffer.
4 Copyright (c) 2005 - 2016, 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"
16 #include <Protocol/BlockIo.h>
18 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_DISK_IMAGE HDiskImage
;
28 HEFI_EDITOR_DISK_IMAGE HDiskImageBackupVar
;
31 // for basic initialization of HDiskImage
33 HEFI_EDITOR_DISK_IMAGE HDiskImageConst
= {
41 Initialization function for HDiskImage.
43 @retval EFI_SUCCESS The operation was successful.
44 @retval EFI_LOAD_ERROR A load error occured.
52 // basically initialize the HDiskImage
54 CopyMem (&HDiskImage
, &HDiskImageConst
, sizeof (HDiskImage
));
56 CopyMem (&HDiskImageBackupVar
, &HDiskImageConst
, sizeof (HDiskImageBackupVar
));
62 Backup function for HDiskImage. Only a few fields need to be backup.
63 This is for making the Disk buffer refresh as few as possible.
65 @retval EFI_SUCCESS The operation was successful.
66 @retval EFI_OUT_OF_RESOURCES gST->ConOut of resources.
74 // backup the disk name, offset and size
77 SHELL_FREE_NON_NULL (HDiskImageBackupVar
.Name
);
79 HDiskImageBackupVar
.Name
= CatSPrint(NULL
, L
"%s", HDiskImage
.Name
);
80 if (HDiskImageBackupVar
.Name
== NULL
) {
81 return EFI_OUT_OF_RESOURCES
;
84 HDiskImageBackupVar
.Offset
= HDiskImage
.Offset
;
85 HDiskImageBackupVar
.Size
= HDiskImage
.Size
;
91 Cleanup function for HDiskImage.
93 @retval EFI_SUCCESS The operation was successful.
100 SHELL_FREE_NON_NULL (HDiskImage
.Name
);
101 SHELL_FREE_NON_NULL (HDiskImageBackupVar
.Name
);
107 Set FileName field in HFileImage.
109 @param[in] Str File name to set.
110 @param[in] Offset The offset.
111 @param[in] Size The size.
113 @retval EFI_SUCCESS The operation was successful.
114 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
118 HDiskImageSetDiskNameOffsetSize (
119 IN CONST CHAR16
*Str
,
128 // free the old file name
130 SHELL_FREE_NON_NULL (HDiskImage
.Name
);
134 HDiskImage
.Name
= AllocateZeroPool (2 * (Len
+ 1));
135 if (HDiskImage
.Name
== NULL
) {
136 return EFI_OUT_OF_RESOURCES
;
139 for (Index
= 0; Index
< Len
; Index
++) {
140 HDiskImage
.Name
[Index
] = Str
[Index
];
143 HDiskImage
.Name
[Len
] = L
'\0';
145 HDiskImage
.Offset
= Offset
;
146 HDiskImage
.Size
= Size
;
152 Read a disk from disk into HBufferImage.
154 @param[in] DeviceName filename to read.
155 @param[in] Offset The offset.
156 @param[in] Size The size.
157 @param[in] Recover if is for recover, no information print.
159 @retval EFI_SUCCESS The operation was successful.
160 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
161 @retval EFI_LOAD_ERROR A load error occured.
162 @retval EFI_INVALID_PARAMETER A parameter was invalid.
166 IN CONST CHAR16
*DeviceName
,
172 CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
173 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
174 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePathForFree
;
176 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
183 HEFI_EDITOR_LINE
*Line
;
185 HBufferImage
.BufferType
= FileTypeDiskBuffer
;
187 DevicePath
= gEfiShellProtocol
->GetDevicePathFromMap(DeviceName
);
188 if (DevicePath
== NULL
) {
189 StatusBarSetStatusString (L
"Cannot Find Device");
190 return EFI_INVALID_PARAMETER
;
192 DupDevicePath
= DuplicateDevicePath(DevicePath
);
193 DupDevicePathForFree
= DupDevicePath
;
195 // get blkio interface
197 Status
= gBS
->LocateDevicePath(&gEfiBlockIoProtocolGuid
,&DupDevicePath
,&Handle
);
198 FreePool(DupDevicePathForFree
);
199 if (EFI_ERROR (Status
)) {
200 StatusBarSetStatusString (L
"Read Disk Failed");
203 Status
= gBS
->OpenProtocol(Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlkIo
, gImageHandle
, NULL
, EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
204 if (EFI_ERROR (Status
)) {
205 StatusBarSetStatusString (L
"Read Disk Failed");
209 // if Offset exceeds LastBlock,
212 if (Offset
> BlkIo
->Media
->LastBlock
|| Offset
+ Size
> BlkIo
->Media
->LastBlock
) {
213 StatusBarSetStatusString (L
"Invalid Offset + Size");
214 return EFI_LOAD_ERROR
;
217 Bytes
= BlkIo
->Media
->BlockSize
* Size
;
218 Buffer
= AllocateZeroPool (Bytes
);
220 if (Buffer
== NULL
) {
221 StatusBarSetStatusString (L
"Read Disk Failed");
222 return EFI_OUT_OF_RESOURCES
;
228 Status
= BlkIo
->ReadBlocks (
230 BlkIo
->Media
->MediaId
,
236 if (EFI_ERROR (Status
)) {
238 StatusBarSetStatusString (L
"Read Disk Failed");
239 return EFI_LOAD_ERROR
;
245 // convert buffer to line list
247 Status
= HBufferImageBufferToList (Buffer
, Bytes
);
250 if (EFI_ERROR (Status
)) {
251 StatusBarSetStatusString (L
"Read Disk Failed");
255 Status
= HDiskImageSetDiskNameOffsetSize (DeviceName
, Offset
, Size
);
256 if (EFI_ERROR (Status
)) {
257 StatusBarSetStatusString (L
"Read Disk Failed");
258 return EFI_OUT_OF_RESOURCES
;
261 // initialize some variables
263 HDiskImage
.BlockSize
= BlkIo
->Media
->BlockSize
;
265 HBufferImage
.DisplayPosition
.Row
= 2;
266 HBufferImage
.DisplayPosition
.Column
= 10;
268 HBufferImage
.MousePosition
.Row
= 2;
269 HBufferImage
.MousePosition
.Column
= 10;
271 HBufferImage
.LowVisibleRow
= 1;
272 HBufferImage
.HighBits
= TRUE
;
274 HBufferImage
.BufferPosition
.Row
= 1;
275 HBufferImage
.BufferPosition
.Column
= 1;
278 Str
= CatSPrint(NULL
, L
"%d Lines Read", HBufferImage
.NumLines
);
280 StatusBarSetStatusString (L
"Read Disk Failed");
281 return EFI_OUT_OF_RESOURCES
;
284 StatusBarSetStatusString (Str
);
285 SHELL_FREE_NON_NULL (Str
);
287 HMainEditor
.SelectStart
= 0;
288 HMainEditor
.SelectEnd
= 0;
295 if (HBufferImage
.Lines
!= NULL
) {
296 HBufferImage
.CurrentLine
= CR (
297 HBufferImage
.ListHead
->ForwardLink
,
304 // create a dummy line
306 Line
= HBufferImageCreateLine ();
308 StatusBarSetStatusString (L
"Read Disk Failed");
309 return EFI_OUT_OF_RESOURCES
;
312 HBufferImage
.CurrentLine
= Line
;
315 HBufferImage
.Modified
= FALSE
;
316 HBufferImageNeedRefresh
= TRUE
;
317 HBufferImageOnlyLineNeedRefresh
= FALSE
;
318 HBufferImageMouseNeedRefresh
= TRUE
;
324 Save lines in HBufferImage to disk.
325 NOT ALLOW TO WRITE TO ANOTHER DISK!!!!!!!!!
327 @param[in] DeviceName The device name.
328 @param[in] Offset The offset.
329 @param[in] Size The size.
331 @retval EFI_SUCCESS The operation was successful.
332 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
333 @retval EFI_LOAD_ERROR A load error occured.
334 @retval EFI_INVALID_PARAMETER A parameter was invalid.
338 IN CHAR16
*DeviceName
,
344 CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
345 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
346 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePathForFree
;
347 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
354 // if not modified, directly return
356 if (HBufferImage
.Modified
== FALSE
) {
360 HBufferImage
.BufferType
= FileTypeDiskBuffer
;
362 DevicePath
= gEfiShellProtocol
->GetDevicePathFromMap(DeviceName
);
363 if (DevicePath
== NULL
) {
364 // StatusBarSetStatusString (L"Cannot Find Device");
365 return EFI_INVALID_PARAMETER
;
367 DupDevicePath
= DuplicateDevicePath(DevicePath
);
368 DupDevicePathForFree
= DupDevicePath
;
371 // get blkio interface
373 Status
= gBS
->LocateDevicePath(&gEfiBlockIoProtocolGuid
,&DupDevicePath
,&Handle
);
374 FreePool(DupDevicePathForFree
);
375 if (EFI_ERROR (Status
)) {
376 // StatusBarSetStatusString (L"Read Disk Failed");
379 Status
= gBS
->OpenProtocol(Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlkIo
, gImageHandle
, NULL
, EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
380 if (EFI_ERROR (Status
)) {
381 // StatusBarSetStatusString (L"Read Disk Failed");
385 Bytes
= BlkIo
->Media
->BlockSize
* Size
;
386 Buffer
= AllocateZeroPool (Bytes
);
388 if (Buffer
== NULL
) {
389 return EFI_OUT_OF_RESOURCES
;
392 // concatenate the line list to a buffer
394 Status
= HBufferImageListToBuffer (Buffer
, Bytes
);
395 if (EFI_ERROR (Status
)) {
401 // write the buffer to disk
403 Status
= BlkIo
->WriteBlocks (
405 BlkIo
->Media
->MediaId
,
413 if (EFI_ERROR (Status
)) {
414 return EFI_LOAD_ERROR
;
419 HBufferImage
.Modified
= FALSE
;