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
= {
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
;
186 EDIT_FILE_TYPE BufferTypeBackup
;
188 BufferTypeBackup
= HBufferImage
.BufferType
;
189 HBufferImage
.BufferType
= FileTypeDiskBuffer
;
191 DevicePath
= gEfiShellProtocol
->GetDevicePathFromMap(DeviceName
);
192 if (DevicePath
== NULL
) {
193 StatusBarSetStatusString (L
"Cannot Find Device");
194 return EFI_INVALID_PARAMETER
;
196 DupDevicePath
= DuplicateDevicePath(DevicePath
);
197 DupDevicePathForFree
= DupDevicePath
;
199 // get blkio interface
201 Status
= gBS
->LocateDevicePath(&gEfiBlockIoProtocolGuid
,&DupDevicePath
,&Handle
);
202 FreePool(DupDevicePathForFree
);
203 if (EFI_ERROR (Status
)) {
204 StatusBarSetStatusString (L
"Read Disk Failed");
207 Status
= gBS
->OpenProtocol(Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlkIo
, gImageHandle
, NULL
, EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
208 if (EFI_ERROR (Status
)) {
209 StatusBarSetStatusString (L
"Read Disk Failed");
213 // if Offset exceeds LastBlock,
216 if (Offset
> BlkIo
->Media
->LastBlock
|| Offset
+ Size
> BlkIo
->Media
->LastBlock
) {
217 StatusBarSetStatusString (L
"Invalid Offset + Size");
218 return EFI_LOAD_ERROR
;
221 Bytes
= BlkIo
->Media
->BlockSize
* Size
;
222 Buffer
= AllocateZeroPool (Bytes
);
224 if (Buffer
== NULL
) {
225 StatusBarSetStatusString (L
"Read Disk Failed");
226 return EFI_OUT_OF_RESOURCES
;
229 ByteOffset
= MultU64x32 (Offset
, BlkIo
->Media
->BlockSize
);
234 Status
= BlkIo
->ReadBlocks (
236 BlkIo
->Media
->MediaId
,
242 if (EFI_ERROR (Status
)) {
244 StatusBarSetStatusString (L
"Read Disk Failed");
245 return EFI_LOAD_ERROR
;
251 // convert buffer to line list
253 Status
= HBufferImageBufferToList (Buffer
, Bytes
);
256 if (EFI_ERROR (Status
)) {
257 StatusBarSetStatusString (L
"Read Disk Failed");
261 Status
= HDiskImageSetDiskNameOffsetSize (DeviceName
, Offset
, Size
);
262 if (EFI_ERROR (Status
)) {
263 StatusBarSetStatusString (L
"Read Disk Failed");
264 return EFI_OUT_OF_RESOURCES
;
267 // initialize some variables
269 HDiskImage
.BlockSize
= BlkIo
->Media
->BlockSize
;
271 HBufferImage
.DisplayPosition
.Row
= 2;
272 HBufferImage
.DisplayPosition
.Column
= 10;
274 HBufferImage
.MousePosition
.Row
= 2;
275 HBufferImage
.MousePosition
.Column
= 10;
277 HBufferImage
.LowVisibleRow
= 1;
278 HBufferImage
.HighBits
= TRUE
;
280 HBufferImage
.BufferPosition
.Row
= 1;
281 HBufferImage
.BufferPosition
.Column
= 1;
284 Str
= CatSPrint(NULL
, L
"%d Lines Read", HBufferImage
.NumLines
);
286 StatusBarSetStatusString (L
"Read Disk Failed");
287 return EFI_OUT_OF_RESOURCES
;
290 StatusBarSetStatusString (Str
);
291 SHELL_FREE_NON_NULL (Str
);
293 HMainEditor
.SelectStart
= 0;
294 HMainEditor
.SelectEnd
= 0;
301 if (HBufferImage
.Lines
!= NULL
) {
302 HBufferImage
.CurrentLine
= CR (
303 HBufferImage
.ListHead
->ForwardLink
,
310 // create a dummy line
312 Line
= HBufferImageCreateLine ();
314 StatusBarSetStatusString (L
"Read Disk Failed");
315 return EFI_OUT_OF_RESOURCES
;
318 HBufferImage
.CurrentLine
= Line
;
321 HBufferImage
.Modified
= FALSE
;
322 HBufferImageNeedRefresh
= TRUE
;
323 HBufferImageOnlyLineNeedRefresh
= FALSE
;
324 HBufferImageMouseNeedRefresh
= TRUE
;
330 Save lines in HBufferImage to disk.
331 NOT ALLOW TO WRITE TO ANOTHER DISK!!!!!!!!!
333 @param[in] DeviceName The device name.
334 @param[in] Offset The offset.
335 @param[in] Size The size.
337 @retval EFI_SUCCESS The operation was successful.
338 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
339 @retval EFI_LOAD_ERROR A load error occured.
340 @retval EFI_INVALID_PARAMETER A parameter was invalid.
344 IN CHAR16
*DeviceName
,
350 CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
351 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
352 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
360 EDIT_FILE_TYPE BufferTypeBackup
;
363 // if not modified, directly return
365 if (HBufferImage
.Modified
== FALSE
) {
369 BufferTypeBackup
= HBufferImage
.BufferType
;
370 HBufferImage
.BufferType
= FileTypeDiskBuffer
;
372 DevicePath
= gEfiShellProtocol
->GetDevicePathFromMap(DeviceName
);
373 if (DevicePath
== NULL
) {
374 // StatusBarSetStatusString (L"Cannot Find Device");
375 return EFI_INVALID_PARAMETER
;
377 DupDevicePath
= DuplicateDevicePath(DevicePath
);
380 // get blkio interface
382 Status
= gBS
->LocateDevicePath(&gEfiBlockIoProtocolGuid
,&DupDevicePath
,&Handle
);
383 FreePool(DupDevicePath
);
384 if (EFI_ERROR (Status
)) {
385 // StatusBarSetStatusString (L"Read Disk Failed");
388 Status
= gBS
->OpenProtocol(Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlkIo
, gImageHandle
, NULL
, EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
389 if (EFI_ERROR (Status
)) {
390 // StatusBarSetStatusString (L"Read Disk Failed");
394 Bytes
= BlkIo
->Media
->BlockSize
* Size
;
395 Buffer
= AllocateZeroPool (Bytes
);
397 if (Buffer
== NULL
) {
398 return EFI_OUT_OF_RESOURCES
;
401 // concatenate the line list to a buffer
403 Status
= HBufferImageListToBuffer (Buffer
, Bytes
);
404 if (EFI_ERROR (Status
)) {
409 ByteOffset
= MultU64x32 (Offset
, BlkIo
->Media
->BlockSize
);
412 // write the buffer to disk
414 Status
= BlkIo
->WriteBlocks (
416 BlkIo
->Media
->MediaId
,
424 if (EFI_ERROR (Status
)) {
425 return EFI_LOAD_ERROR
;
430 HBufferImage
.Modified
= FALSE
;