2 Main entry point of editor
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 "UefiShellDebug1CommandsLib.h"
16 #include "HexEditor.h"
21 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
29 Function for 'hexedit' command.
31 @param[in] ImageHandle Handle to the Image (NULL if Internal).
32 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
36 ShellCommandRunHexEdit (
37 IN EFI_HANDLE ImageHandle
,
38 IN EFI_SYSTEM_TABLE
*SystemTable
44 SHELL_STATUS ShellStatus
;
53 EDIT_FILE_TYPE WhatToDo
;
56 ShellStatus
= SHELL_SUCCESS
;
65 WhatToDo
= FileTypeNone
;
68 // initialize the shell lib (we must be in non-auto-init...)
70 Status
= ShellInitialize();
71 ASSERT_EFI_ERROR(Status
);
73 Status
= CommandInit();
74 ASSERT_EFI_ERROR(Status
);
77 // parse the command line
79 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
80 if (EFI_ERROR(Status
)) {
81 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
82 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, ProblemParam
);
83 FreePool(ProblemParam
);
84 ShellStatus
= SHELL_INVALID_PARAMETER
;
92 if (ShellCommandLineGetFlag(Package
, L
"-d")){
93 if (ShellCommandLineGetCount(Package
) < 4) {
94 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
);
95 ShellStatus
= SHELL_INVALID_PARAMETER
;
96 } else if (ShellCommandLineGetCount(Package
) > 4) {
97 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
);
98 ShellStatus
= SHELL_INVALID_PARAMETER
;
100 WhatToDo
= FileTypeDiskBuffer
;
101 Name
= ShellCommandLineGetRawValue(Package
, 1);
102 Offset
= ShellStrToUintn(ShellCommandLineGetRawValue(Package
, 2));
103 Size
= ShellStrToUintn(ShellCommandLineGetRawValue(Package
, 3));
105 if (Offset
== (UINTN
)-1 || Size
== (UINTN
)-1) {
106 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM_VAL
), gShellDebug1HiiHandle
, L
"-d");
107 ShellStatus
= SHELL_INVALID_PARAMETER
;
114 if (ShellCommandLineGetFlag(Package
, L
"-f") && (WhatToDo
== FileTypeNone
)){
115 if (ShellCommandLineGetCount(Package
) < 2) {
116 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
);
117 ShellStatus
= SHELL_INVALID_PARAMETER
;
118 } else if (ShellCommandLineGetCount(Package
) > 2) {
119 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
);
120 ShellStatus
= SHELL_INVALID_PARAMETER
;
122 Name
= ShellCommandLineGetRawValue(Package
, 1);
123 if (Name
== NULL
|| !IsValidFileName(Name
)) {
124 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, Name
);
125 ShellStatus
= SHELL_INVALID_PARAMETER
;
127 WhatToDo
= FileTypeFileBuffer
;
135 if (ShellCommandLineGetFlag(Package
, L
"-m") && (WhatToDo
== FileTypeNone
)){
136 if (ShellCommandLineGetCount(Package
) < 3) {
137 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
);
138 ShellStatus
= SHELL_INVALID_PARAMETER
;
139 } else if (ShellCommandLineGetCount(Package
) > 3) {
140 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
);
141 ShellStatus
= SHELL_INVALID_PARAMETER
;
143 WhatToDo
= FileTypeMemBuffer
;
144 Offset
= ShellStrToUintn(ShellCommandLineGetRawValue(Package
, 1));
145 Size
= ShellStrToUintn(ShellCommandLineGetRawValue(Package
, 2));
148 Name
= ShellCommandLineGetRawValue(Package
, 1);
149 if (WhatToDo
== FileTypeNone
&& Name
!= NULL
) {
150 if (ShellCommandLineGetCount(Package
) > 2) {
151 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
);
152 ShellStatus
= SHELL_INVALID_PARAMETER
;
153 } else if (!IsValidFileName(Name
)) {
154 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, Name
);
155 ShellStatus
= SHELL_INVALID_PARAMETER
;
157 WhatToDo
= FileTypeFileBuffer
;
159 } else if (WhatToDo
== FileTypeNone
) {
160 if (gEfiShellProtocol
->GetCurDir(NULL
) == NULL
) {
161 ShellStatus
= SHELL_NOT_FOUND
;
162 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellDebug1HiiHandle
);
164 NewName
= EditGetDefaultFileName(L
"bin");
166 WhatToDo
= FileTypeFileBuffer
;
170 if (ShellStatus
== SHELL_SUCCESS
&& WhatToDo
== FileTypeNone
) {
171 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
);
172 ShellStatus
= SHELL_INVALID_PARAMETER
;
173 } else if (WhatToDo
== FileTypeFileBuffer
&& ShellGetCurrentDir(NULL
) == NULL
) {
174 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellDebug1HiiHandle
);
175 ShellStatus
= SHELL_INVALID_PARAMETER
;
178 if (ShellStatus
== SHELL_SUCCESS
) {
182 Status
= HMainEditorInit ();
183 if (EFI_ERROR (Status
)) {
184 gST
->ConOut
->ClearScreen (gST
->ConOut
);
185 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
186 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HEXEDIT_INIT_FAILED
), gShellDebug1HiiHandle
);
188 HMainEditorBackup ();
190 case FileTypeFileBuffer
:
191 Status
= HBufferImageRead (
203 case FileTypeDiskBuffer
:
204 Status
= HBufferImageRead (
216 case FileTypeMemBuffer
:
217 Status
= HBufferImageRead (
230 Status
= EFI_NOT_FOUND
;
233 if (!EFI_ERROR (Status
)) {
234 HMainEditorRefresh ();
235 Status
= HMainEditorKeyInput ();
237 if (Status
!= EFI_OUT_OF_RESOURCES
) {
239 // back up the status string
241 Buffer
= CatSPrint (NULL
, L
"%s\r\n", StatusBarGetString());
248 HMainEditorCleanup ();
250 if (!EFI_ERROR (Status
)) {
251 if (ShellStatus
== SHELL_SUCCESS
) {
252 ShellStatus
= SHELL_UNSUPPORTED
;
257 // print editor exit code on screen
259 if (Status
== EFI_OUT_OF_RESOURCES
) {
260 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellDebug1HiiHandle
);
261 } else if (EFI_ERROR(Status
)){
262 if (Buffer
!= NULL
) {
263 if (StrCmp (Buffer
, L
"") != 0) {
265 // print out the status string
267 ShellPrintEx(-1, -1, L
"%s", Buffer
);
269 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR
), gShellDebug1HiiHandle
);
272 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR
), gShellDebug1HiiHandle
);
276 ShellCommandLineFreeVarList (Package
);
279 SHELL_FREE_NON_NULL (Buffer
);
280 SHELL_FREE_NON_NULL (NewName
);