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.
117 HDiskImageSetDiskNameOffsetSize (
118 IN CONST CHAR16
*Str
,
127 // free the old file name
129 SHELL_FREE_NON_NULL (HDiskImage
.Name
);
133 HDiskImage
.Name
= AllocateZeroPool (2 * (Len
+ 1));
134 if (HDiskImage
.Name
== NULL
) {
135 return EFI_OUT_OF_RESOURCES
;
138 for (Index
= 0; Index
< Len
; Index
++) {
139 HDiskImage
.Name
[Index
] = Str
[Index
];
142 HDiskImage
.Name
[Len
] = L
'\0';
144 HDiskImage
.Offset
= Offset
;
145 HDiskImage
.Size
= Size
;
151 Read a disk from disk into HBufferImage.
153 @param[in] DeviceName filename to read.
154 @param[in] Offset The offset.
155 @param[in] Size The size.
156 @param[in] Recover if is for recover, no information print.
158 @retval EFI_SUCCESS The operation was successful.
159 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
160 @retval EFI_LOAD_ERROR A load error occured.
161 @retval EFI_INVALID_PARAMETER A parameter was invalid.
165 IN CONST CHAR16
*DeviceName
,
171 CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
172 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
173 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePathForFree
;
175 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
182 HEFI_EDITOR_LINE
*Line
;
184 HBufferImage
.BufferType
= FileTypeDiskBuffer
;
186 DevicePath
= gEfiShellProtocol
->GetDevicePathFromMap(DeviceName
);
187 if (DevicePath
== NULL
) {
188 StatusBarSetStatusString (L
"Cannot Find Device");
189 return EFI_INVALID_PARAMETER
;
191 DupDevicePath
= DuplicateDevicePath(DevicePath
);
192 DupDevicePathForFree
= DupDevicePath
;
194 // get blkio interface
196 Status
= gBS
->LocateDevicePath(&gEfiBlockIoProtocolGuid
,&DupDevicePath
,&Handle
);
197 FreePool(DupDevicePathForFree
);
198 if (EFI_ERROR (Status
)) {
199 StatusBarSetStatusString (L
"Read Disk Failed");
202 Status
= gBS
->OpenProtocol(Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlkIo
, gImageHandle
, NULL
, EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
203 if (EFI_ERROR (Status
)) {
204 StatusBarSetStatusString (L
"Read Disk Failed");
208 // if Offset exceeds LastBlock,
211 if (Offset
> BlkIo
->Media
->LastBlock
|| Offset
+ Size
> BlkIo
->Media
->LastBlock
) {
212 StatusBarSetStatusString (L
"Invalid Offset + Size");
213 return EFI_LOAD_ERROR
;
216 Bytes
= BlkIo
->Media
->BlockSize
* Size
;
217 Buffer
= AllocateZeroPool (Bytes
);
219 if (Buffer
== NULL
) {
220 StatusBarSetStatusString (L
"Read Disk Failed");
221 return EFI_OUT_OF_RESOURCES
;
227 Status
= BlkIo
->ReadBlocks (
229 BlkIo
->Media
->MediaId
,
235 if (EFI_ERROR (Status
)) {
237 StatusBarSetStatusString (L
"Read Disk Failed");
238 return EFI_LOAD_ERROR
;
244 // convert buffer to line list
246 Status
= HBufferImageBufferToList (Buffer
, Bytes
);
249 if (EFI_ERROR (Status
)) {
250 StatusBarSetStatusString (L
"Read Disk Failed");
254 Status
= HDiskImageSetDiskNameOffsetSize (DeviceName
, Offset
, Size
);
255 if (EFI_ERROR (Status
)) {
256 StatusBarSetStatusString (L
"Read Disk Failed");
257 return EFI_OUT_OF_RESOURCES
;
260 // initialize some variables
262 HDiskImage
.BlockSize
= BlkIo
->Media
->BlockSize
;
264 HBufferImage
.DisplayPosition
.Row
= 2;
265 HBufferImage
.DisplayPosition
.Column
= 10;
267 HBufferImage
.MousePosition
.Row
= 2;
268 HBufferImage
.MousePosition
.Column
= 10;
270 HBufferImage
.LowVisibleRow
= 1;
271 HBufferImage
.HighBits
= TRUE
;
273 HBufferImage
.BufferPosition
.Row
= 1;
274 HBufferImage
.BufferPosition
.Column
= 1;
277 Str
= CatSPrint(NULL
, L
"%d Lines Read", HBufferImage
.NumLines
);
279 StatusBarSetStatusString (L
"Read Disk Failed");
280 return EFI_OUT_OF_RESOURCES
;
283 StatusBarSetStatusString (Str
);
284 SHELL_FREE_NON_NULL (Str
);
286 HMainEditor
.SelectStart
= 0;
287 HMainEditor
.SelectEnd
= 0;
294 if (HBufferImage
.Lines
!= NULL
) {
295 HBufferImage
.CurrentLine
= CR (
296 HBufferImage
.ListHead
->ForwardLink
,
303 // create a dummy line
305 Line
= HBufferImageCreateLine ();
307 StatusBarSetStatusString (L
"Read Disk Failed");
308 return EFI_OUT_OF_RESOURCES
;
311 HBufferImage
.CurrentLine
= Line
;
314 HBufferImage
.Modified
= FALSE
;
315 HBufferImageNeedRefresh
= TRUE
;
316 HBufferImageOnlyLineNeedRefresh
= FALSE
;
317 HBufferImageMouseNeedRefresh
= TRUE
;
323 Save lines in HBufferImage to disk.
324 NOT ALLOW TO WRITE TO ANOTHER DISK!!!!!!!!!
326 @param[in] DeviceName The device name.
327 @param[in] Offset The offset.
328 @param[in] Size The size.
330 @retval EFI_SUCCESS The operation was successful.
331 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
332 @retval EFI_LOAD_ERROR A load error occured.
333 @retval EFI_INVALID_PARAMETER A parameter was invalid.
337 IN CHAR16
*DeviceName
,
343 CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
344 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
345 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePathForFree
;
346 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
353 // if not modified, directly return
355 if (HBufferImage
.Modified
== FALSE
) {
359 HBufferImage
.BufferType
= FileTypeDiskBuffer
;
361 DevicePath
= gEfiShellProtocol
->GetDevicePathFromMap(DeviceName
);
362 if (DevicePath
== NULL
) {
363 // StatusBarSetStatusString (L"Cannot Find Device");
364 return EFI_INVALID_PARAMETER
;
366 DupDevicePath
= DuplicateDevicePath(DevicePath
);
367 DupDevicePathForFree
= DupDevicePath
;
370 // get blkio interface
372 Status
= gBS
->LocateDevicePath(&gEfiBlockIoProtocolGuid
,&DupDevicePath
,&Handle
);
373 FreePool(DupDevicePathForFree
);
374 if (EFI_ERROR (Status
)) {
375 // StatusBarSetStatusString (L"Read Disk Failed");
378 Status
= gBS
->OpenProtocol(Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlkIo
, gImageHandle
, NULL
, EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
379 if (EFI_ERROR (Status
)) {
380 // StatusBarSetStatusString (L"Read Disk Failed");
384 Bytes
= BlkIo
->Media
->BlockSize
* Size
;
385 Buffer
= AllocateZeroPool (Bytes
);
387 if (Buffer
== NULL
) {
388 return EFI_OUT_OF_RESOURCES
;
391 // concatenate the line list to a buffer
393 Status
= HBufferImageListToBuffer (Buffer
, Bytes
);
394 if (EFI_ERROR (Status
)) {
400 // write the buffer to disk
402 Status
= BlkIo
->WriteBlocks (
404 BlkIo
->Media
->MediaId
,
412 if (EFI_ERROR (Status
)) {
413 return EFI_LOAD_ERROR
;
418 HBufferImage
.Modified
= FALSE
;