2 Main entry point of editor
4 (C) Copyright 2014-2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved. <BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "UefiShellDebug1CommandsLib.h"
11 #include "HexEditor.h"
16 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
24 Function for 'hexedit' command.
26 @param[in] ImageHandle Handle to the Image (NULL if Internal).
27 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
31 ShellCommandRunHexEdit (
32 IN EFI_HANDLE ImageHandle
,
33 IN EFI_SYSTEM_TABLE
*SystemTable
39 SHELL_STATUS ShellStatus
;
45 EDIT_FILE_TYPE WhatToDo
;
48 ShellStatus
= SHELL_SUCCESS
;
54 WhatToDo
= FileTypeNone
;
57 // initialize the shell lib (we must be in non-auto-init...)
59 Status
= ShellInitialize ();
60 ASSERT_EFI_ERROR (Status
);
62 Status
= CommandInit ();
63 ASSERT_EFI_ERROR (Status
);
66 // parse the command line
68 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
69 if (EFI_ERROR (Status
)) {
70 if ((Status
== EFI_VOLUME_CORRUPTED
) && (ProblemParam
!= NULL
)) {
71 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, L
"hexedit", ProblemParam
);
72 FreePool (ProblemParam
);
73 ShellStatus
= SHELL_INVALID_PARAMETER
;
81 if (ShellCommandLineGetFlag (Package
, L
"-d")) {
82 if (ShellCommandLineGetCount (Package
) < 4) {
83 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
, L
"hexedit");
84 ShellStatus
= SHELL_INVALID_PARAMETER
;
85 } else if (ShellCommandLineGetCount (Package
) > 4) {
86 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"hexedit");
87 ShellStatus
= SHELL_INVALID_PARAMETER
;
89 WhatToDo
= FileTypeDiskBuffer
;
90 Name
= ShellCommandLineGetRawValue (Package
, 1);
91 Offset
= ShellStrToUintn (ShellCommandLineGetRawValue (Package
, 2));
92 Size
= ShellStrToUintn (ShellCommandLineGetRawValue (Package
, 3));
95 if ((Offset
== (UINTN
)-1) || (Size
== (UINTN
)-1)) {
96 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_VALUE
), gShellDebug1HiiHandle
, L
"hexedit", L
"-d");
97 ShellStatus
= SHELL_INVALID_PARAMETER
;
104 if (ShellCommandLineGetFlag (Package
, L
"-f") && (WhatToDo
== FileTypeNone
)) {
105 if (ShellCommandLineGetCount (Package
) < 2) {
106 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
, L
"hexedit");
107 ShellStatus
= SHELL_INVALID_PARAMETER
;
108 } else if (ShellCommandLineGetCount (Package
) > 2) {
109 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"hexedit");
110 ShellStatus
= SHELL_INVALID_PARAMETER
;
112 Name
= ShellCommandLineGetRawValue (Package
, 1);
113 if ((Name
== NULL
) || !IsValidFileName (Name
)) {
114 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"hexedit", Name
);
115 ShellStatus
= SHELL_INVALID_PARAMETER
;
117 WhatToDo
= FileTypeFileBuffer
;
125 if (ShellCommandLineGetFlag (Package
, L
"-m") && (WhatToDo
== FileTypeNone
)) {
126 if (ShellCommandLineGetCount (Package
) < 3) {
127 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
, L
"hexedit");
128 ShellStatus
= SHELL_INVALID_PARAMETER
;
129 } else if (ShellCommandLineGetCount (Package
) > 3) {
130 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"hexedit");
131 ShellStatus
= SHELL_INVALID_PARAMETER
;
133 WhatToDo
= FileTypeMemBuffer
;
134 Offset
= ShellStrToUintn (ShellCommandLineGetRawValue (Package
, 1));
135 Size
= ShellStrToUintn (ShellCommandLineGetRawValue (Package
, 2));
139 Name
= ShellCommandLineGetRawValue (Package
, 1);
140 if ((WhatToDo
== FileTypeNone
) && (Name
!= NULL
)) {
141 if (ShellCommandLineGetCount (Package
) > 2) {
142 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"hexedit");
143 ShellStatus
= SHELL_INVALID_PARAMETER
;
144 } else if (!IsValidFileName (Name
)) {
145 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"hexedit", Name
);
146 ShellStatus
= SHELL_INVALID_PARAMETER
;
148 WhatToDo
= FileTypeFileBuffer
;
150 } else if (WhatToDo
== FileTypeNone
) {
151 if (gEfiShellProtocol
->GetCurDir (NULL
) == NULL
) {
152 ShellStatus
= SHELL_NOT_FOUND
;
153 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellDebug1HiiHandle
, L
"hexedit");
155 NewName
= EditGetDefaultFileName (L
"bin");
157 WhatToDo
= FileTypeFileBuffer
;
161 if ((ShellStatus
== SHELL_SUCCESS
) && (WhatToDo
== FileTypeNone
)) {
162 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
, L
"hexedit");
163 ShellStatus
= SHELL_INVALID_PARAMETER
;
164 } else if ((WhatToDo
== FileTypeFileBuffer
) && (ShellGetCurrentDir (NULL
) == NULL
)) {
165 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellDebug1HiiHandle
, L
"hexedit");
166 ShellStatus
= SHELL_INVALID_PARAMETER
;
169 if (ShellStatus
== SHELL_SUCCESS
) {
173 Status
= HMainEditorInit ();
174 if (EFI_ERROR (Status
)) {
175 gST
->ConOut
->ClearScreen (gST
->ConOut
);
176 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
177 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_HEXEDIT_INIT_FAILED
), gShellDebug1HiiHandle
);
179 HMainEditorBackup ();
181 case FileTypeFileBuffer
:
182 Status
= HBufferImageRead (
183 Name
== NULL
? L
"" : Name
,
194 case FileTypeDiskBuffer
:
195 Status
= HBufferImageRead (
197 Name
== NULL
? L
"" : Name
,
207 case FileTypeMemBuffer
:
208 Status
= HBufferImageRead (
221 Status
= EFI_NOT_FOUND
;
225 if (!EFI_ERROR (Status
)) {
226 HMainEditorRefresh ();
227 Status
= HMainEditorKeyInput ();
230 if (Status
!= EFI_OUT_OF_RESOURCES
) {
232 // back up the status string
234 Buffer
= CatSPrint (NULL
, L
"%s\r\n", StatusBarGetString ());
241 HMainEditorCleanup ();
243 if (EFI_ERROR (Status
)) {
244 if (ShellStatus
== SHELL_SUCCESS
) {
245 ShellStatus
= SHELL_UNSUPPORTED
;
250 // print editor exit code on screen
252 if (Status
== EFI_OUT_OF_RESOURCES
) {
253 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellDebug1HiiHandle
, L
"hexedit");
254 } else if (EFI_ERROR (Status
)) {
255 if (Buffer
!= NULL
) {
256 if (StrCmp (Buffer
, L
"") != 0) {
258 // print out the status string
260 ShellPrintEx (-1, -1, L
"%s", Buffer
);
262 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR
), gShellDebug1HiiHandle
);
265 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
);