2 Functions to deal with Disk buffer.
4 Copyright (c) 2005 - 2011, 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
= {
48 Initialization function for HDiskImage
62 // basically initialize the HDiskImage
64 CopyMem (&HDiskImage
, &HDiskImageConst
, sizeof (HDiskImage
));
66 CopyMem (&HDiskImageBackupVar
, &HDiskImageConst
, sizeof (HDiskImageBackupVar
));
79 Backup function for HDiskImage
80 Only a few fields need to be backup.
81 This is for making the Disk buffer refresh
91 EFI_OUT_OF_RESOURCES - gST->ConOut of resources
96 // backup the disk name, offset and size
99 SHELL_FREE_NON_NULL (HDiskImageBackupVar
.Name
);
101 HDiskImageBackupVar
.Name
= CatSPrint(NULL
, L
"%s", HDiskImage
.Name
);
102 if (HDiskImageBackupVar
.Name
== NULL
) {
103 return EFI_OUT_OF_RESOURCES
;
106 HDiskImageBackupVar
.Offset
= HDiskImage
.Offset
;
107 HDiskImageBackupVar
.Size
= HDiskImage
.Size
;
120 Cleanup function for HDiskImage
132 SHELL_FREE_NON_NULL (HDiskImage
.Name
);
133 SHELL_FREE_NON_NULL (HDiskImageBackupVar
.Name
);
140 HDiskImageSetDiskNameOffsetSize (
141 IN CONST CHAR16
*Str
,
149 Set FileName field in HFileImage
153 Str - File name to set
168 // free the old file name
170 SHELL_FREE_NON_NULL (HDiskImage
.Name
);
174 HDiskImage
.Name
= AllocateZeroPool (2 * (Len
+ 1));
175 if (HDiskImage
.Name
== NULL
) {
176 return EFI_OUT_OF_RESOURCES
;
179 for (Index
= 0; Index
< Len
; Index
++) {
180 HDiskImage
.Name
[Index
] = Str
[Index
];
183 HDiskImage
.Name
[Len
] = L
'\0';
185 HDiskImage
.Offset
= Offset
;
186 HDiskImage
.Size
= Size
;
193 IN CONST CHAR16
*DeviceName
,
202 Read a disk from disk into HBufferImage
206 DeviceName - filename to read
209 Recover - if is for recover, no information print
216 EFI_INVALID_PARAMETER
220 CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
221 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
222 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePathForFree
;
224 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
231 HEFI_EDITOR_LINE
*Line
;
234 EDIT_FILE_TYPE BufferTypeBackup
;
236 BufferTypeBackup
= HBufferImage
.BufferType
;
237 HBufferImage
.BufferType
= FileTypeDiskBuffer
;
239 DevicePath
= gEfiShellProtocol
->GetDevicePathFromMap(DeviceName
);
240 if (DevicePath
== NULL
) {
241 StatusBarSetStatusString (L
"Cannot Find Device");
242 return EFI_INVALID_PARAMETER
;
244 DupDevicePath
= DuplicateDevicePath(DevicePath
);
245 DupDevicePathForFree
= DupDevicePath
;
247 // get blkio interface
249 Status
= gBS
->LocateDevicePath(&gEfiBlockIoProtocolGuid
,&DupDevicePath
,&Handle
);
250 FreePool(DupDevicePathForFree
);
251 if (EFI_ERROR (Status
)) {
252 StatusBarSetStatusString (L
"Read Disk Failed");
255 Status
= gBS
->OpenProtocol(Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlkIo
, gImageHandle
, NULL
, EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
256 if (EFI_ERROR (Status
)) {
257 StatusBarSetStatusString (L
"Read Disk Failed");
261 // if Offset exceeds LastBlock,
264 if (Offset
> BlkIo
->Media
->LastBlock
|| Offset
+ Size
> BlkIo
->Media
->LastBlock
) {
265 StatusBarSetStatusString (L
"Invalid Offset + Size");
266 return EFI_LOAD_ERROR
;
269 Bytes
= BlkIo
->Media
->BlockSize
* Size
;
270 Buffer
= AllocateZeroPool (Bytes
);
272 if (Buffer
== NULL
) {
273 StatusBarSetStatusString (L
"Read Disk Failed");
274 return EFI_OUT_OF_RESOURCES
;
277 ByteOffset
= MultU64x32 (Offset
, BlkIo
->Media
->BlockSize
);
282 Status
= BlkIo
->ReadBlocks (
284 BlkIo
->Media
->MediaId
,
290 if (EFI_ERROR (Status
)) {
292 StatusBarSetStatusString (L
"Read Disk Failed");
293 return EFI_LOAD_ERROR
;
299 // convert buffer to line list
301 Status
= HBufferImageBufferToList (Buffer
, Bytes
);
304 if (EFI_ERROR (Status
)) {
305 StatusBarSetStatusString (L
"Read Disk Failed");
309 Status
= HDiskImageSetDiskNameOffsetSize (DeviceName
, Offset
, Size
);
310 if (EFI_ERROR (Status
)) {
311 StatusBarSetStatusString (L
"Read Disk Failed");
312 return EFI_OUT_OF_RESOURCES
;
315 // initialize some variables
317 HDiskImage
.BlockSize
= BlkIo
->Media
->BlockSize
;
319 HBufferImage
.DisplayPosition
.Row
= 2;
320 HBufferImage
.DisplayPosition
.Column
= 10;
322 HBufferImage
.MousePosition
.Row
= 2;
323 HBufferImage
.MousePosition
.Column
= 10;
325 HBufferImage
.LowVisibleRow
= 1;
326 HBufferImage
.HighBits
= TRUE
;
328 HBufferImage
.BufferPosition
.Row
= 1;
329 HBufferImage
.BufferPosition
.Column
= 1;
332 Str
= CatSPrint(NULL
, L
"%d Lines Read", HBufferImage
.NumLines
);
334 StatusBarSetStatusString (L
"Read Disk Failed");
335 return EFI_OUT_OF_RESOURCES
;
338 StatusBarSetStatusString (Str
);
339 SHELL_FREE_NON_NULL (Str
);
341 HMainEditor
.SelectStart
= 0;
342 HMainEditor
.SelectEnd
= 0;
349 if (HBufferImage
.Lines
!= NULL
) {
350 HBufferImage
.CurrentLine
= CR (
351 HBufferImage
.ListHead
->ForwardLink
,
358 // create a dummy line
360 Line
= HBufferImageCreateLine ();
362 StatusBarSetStatusString (L
"Read Disk Failed");
363 return EFI_OUT_OF_RESOURCES
;
366 HBufferImage
.CurrentLine
= Line
;
369 HBufferImage
.Modified
= FALSE
;
370 HBufferImageNeedRefresh
= TRUE
;
371 HBufferImageOnlyLineNeedRefresh
= FALSE
;
372 HBufferImageMouseNeedRefresh
= TRUE
;
379 IN CHAR16
*DeviceName
,
387 Save lines in HBufferImage to disk
388 NOT ALLOW TO WRITE TO ANOTHER DISK!!!!!!!!!
392 DeviceName - The device name
401 EFI_INVALID_PARAMETER
406 CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
407 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
408 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
416 EDIT_FILE_TYPE BufferTypeBackup
;
419 // if not modified, directly return
421 if (HBufferImage
.Modified
== FALSE
) {
425 BufferTypeBackup
= HBufferImage
.BufferType
;
426 HBufferImage
.BufferType
= FileTypeDiskBuffer
;
428 DevicePath
= gEfiShellProtocol
->GetDevicePathFromMap(DeviceName
);
429 if (DevicePath
== NULL
) {
430 // StatusBarSetStatusString (L"Cannot Find Device");
431 return EFI_INVALID_PARAMETER
;
433 DupDevicePath
= DuplicateDevicePath(DevicePath
);
436 // get blkio interface
438 Status
= gBS
->LocateDevicePath(&gEfiBlockIoProtocolGuid
,&DupDevicePath
,&Handle
);
439 FreePool(DupDevicePath
);
440 if (EFI_ERROR (Status
)) {
441 // StatusBarSetStatusString (L"Read Disk Failed");
444 Status
= gBS
->OpenProtocol(Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlkIo
, gImageHandle
, NULL
, EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
445 if (EFI_ERROR (Status
)) {
446 // StatusBarSetStatusString (L"Read Disk Failed");
450 Bytes
= BlkIo
->Media
->BlockSize
* Size
;
451 Buffer
= AllocateZeroPool (Bytes
);
453 if (Buffer
== NULL
) {
454 return EFI_OUT_OF_RESOURCES
;
457 // concatenate the line list to a buffer
459 Status
= HBufferImageListToBuffer (Buffer
, Bytes
);
460 if (EFI_ERROR (Status
)) {
465 ByteOffset
= MultU64x32 (Offset
, BlkIo
->Media
->BlockSize
);
468 // write the buffer to disk
470 Status
= BlkIo
->WriteBlocks (
472 BlkIo
->Media
->MediaId
,
480 if (EFI_ERROR (Status
)) {
481 return EFI_LOAD_ERROR
;
486 HBufferImage
.Modified
= FALSE
;