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
;
50 EDIT_FILE_TYPE WhatToDo
;
53 ShellStatus
= SHELL_SUCCESS
;
59 WhatToDo
= FileTypeNone
;
62 // initialize the shell lib (we must be in non-auto-init...)
64 Status
= ShellInitialize();
65 ASSERT_EFI_ERROR(Status
);
67 Status
= CommandInit();
68 ASSERT_EFI_ERROR(Status
);
71 // parse the command line
73 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
74 if (EFI_ERROR(Status
)) {
75 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
76 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, ProblemParam
);
77 FreePool(ProblemParam
);
78 ShellStatus
= SHELL_INVALID_PARAMETER
;
86 if (ShellCommandLineGetFlag(Package
, L
"-d")){
87 if (ShellCommandLineGetCount(Package
) < 4) {
88 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
);
89 ShellStatus
= SHELL_INVALID_PARAMETER
;
90 } else if (ShellCommandLineGetCount(Package
) > 4) {
91 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
);
92 ShellStatus
= SHELL_INVALID_PARAMETER
;
94 WhatToDo
= FileTypeDiskBuffer
;
95 Name
= ShellCommandLineGetRawValue(Package
, 1);
96 Offset
= ShellStrToUintn(ShellCommandLineGetRawValue(Package
, 2));
97 Size
= ShellStrToUintn(ShellCommandLineGetRawValue(Package
, 3));
99 if (Offset
== (UINTN
)-1 || Size
== (UINTN
)-1) {
100 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM_VAL
), gShellDebug1HiiHandle
, L
"-d");
101 ShellStatus
= SHELL_INVALID_PARAMETER
;
108 if (ShellCommandLineGetFlag(Package
, L
"-f") && (WhatToDo
== FileTypeNone
)){
109 if (ShellCommandLineGetCount(Package
) < 2) {
110 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
);
111 ShellStatus
= SHELL_INVALID_PARAMETER
;
112 } else if (ShellCommandLineGetCount(Package
) > 2) {
113 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
);
114 ShellStatus
= SHELL_INVALID_PARAMETER
;
116 Name
= ShellCommandLineGetRawValue(Package
, 1);
117 if (Name
== NULL
|| !IsValidFileName(Name
)) {
118 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, Name
);
119 ShellStatus
= SHELL_INVALID_PARAMETER
;
121 WhatToDo
= FileTypeFileBuffer
;
129 if (ShellCommandLineGetFlag(Package
, L
"-m") && (WhatToDo
== FileTypeNone
)){
130 if (ShellCommandLineGetCount(Package
) < 3) {
131 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
);
132 ShellStatus
= SHELL_INVALID_PARAMETER
;
133 } else if (ShellCommandLineGetCount(Package
) > 3) {
134 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
);
135 ShellStatus
= SHELL_INVALID_PARAMETER
;
137 WhatToDo
= FileTypeMemBuffer
;
138 Offset
= ShellStrToUintn(ShellCommandLineGetRawValue(Package
, 1));
139 Size
= ShellStrToUintn(ShellCommandLineGetRawValue(Package
, 2));
142 Name
= ShellCommandLineGetRawValue(Package
, 1);
143 if (WhatToDo
== FileTypeNone
&& Name
!= NULL
) {
144 if (ShellCommandLineGetCount(Package
) > 2) {
145 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
);
146 ShellStatus
= SHELL_INVALID_PARAMETER
;
147 } else if (!IsValidFileName(Name
)) {
148 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, Name
);
149 ShellStatus
= SHELL_INVALID_PARAMETER
;
151 WhatToDo
= FileTypeFileBuffer
;
153 } else if (WhatToDo
== FileTypeNone
) {
154 if (gEfiShellProtocol
->GetCurDir(NULL
) == NULL
) {
155 ShellStatus
= SHELL_NOT_FOUND
;
156 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellDebug1HiiHandle
);
158 NewName
= EditGetDefaultFileName(L
"bin");
160 WhatToDo
= FileTypeFileBuffer
;
164 if (ShellStatus
== SHELL_SUCCESS
&& WhatToDo
== FileTypeNone
) {
165 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
);
166 ShellStatus
= SHELL_INVALID_PARAMETER
;
167 } else if (WhatToDo
== FileTypeFileBuffer
&& ShellGetCurrentDir(NULL
) == NULL
) {
168 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellDebug1HiiHandle
);
169 ShellStatus
= SHELL_INVALID_PARAMETER
;
172 if (ShellStatus
== SHELL_SUCCESS
) {
176 Status
= HMainEditorInit ();
177 if (EFI_ERROR (Status
)) {
178 gST
->ConOut
->ClearScreen (gST
->ConOut
);
179 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
180 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HEXEDIT_INIT_FAILED
), gShellDebug1HiiHandle
);
182 HMainEditorBackup ();
184 case FileTypeFileBuffer
:
185 Status
= HBufferImageRead (
197 case FileTypeDiskBuffer
:
198 Status
= HBufferImageRead (
210 case FileTypeMemBuffer
:
211 Status
= HBufferImageRead (
224 Status
= EFI_NOT_FOUND
;
227 if (!EFI_ERROR (Status
)) {
228 HMainEditorRefresh ();
229 Status
= HMainEditorKeyInput ();
231 if (Status
!= EFI_OUT_OF_RESOURCES
) {
233 // back up the status string
235 Buffer
= CatSPrint (NULL
, L
"%s\r\n", StatusBarGetString());
242 HMainEditorCleanup ();
244 if (!EFI_ERROR (Status
)) {
245 if (ShellStatus
== SHELL_SUCCESS
) {
246 ShellStatus
= SHELL_UNSUPPORTED
;
251 // print editor exit code on screen
253 if (Status
== EFI_OUT_OF_RESOURCES
) {
254 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellDebug1HiiHandle
);
255 } else if (EFI_ERROR(Status
)){
256 if (Buffer
!= NULL
) {
257 if (StrCmp (Buffer
, L
"") != 0) {
259 // print out the status string
261 ShellPrintEx(-1, -1, L
"%s", Buffer
);
263 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR
), gShellDebug1HiiHandle
);
266 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR
), gShellDebug1HiiHandle
);
270 ShellCommandLineFreeVarList (Package
);
273 SHELL_FREE_NON_NULL (Buffer
);
274 SHELL_FREE_NON_NULL (NewName
);