2 Main entry point of editor
4 (C) Copyright 2014-2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2005 - 2011, Intel Corporation. All rights reserved. <BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "UefiShellDebug1CommandsLib.h"
17 #include "HexEditor.h"
22 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
30 Function for 'hexedit' command.
32 @param[in] ImageHandle Handle to the Image (NULL if Internal).
33 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
37 ShellCommandRunHexEdit (
38 IN EFI_HANDLE ImageHandle
,
39 IN EFI_SYSTEM_TABLE
*SystemTable
45 SHELL_STATUS ShellStatus
;
51 EDIT_FILE_TYPE WhatToDo
;
54 ShellStatus
= SHELL_SUCCESS
;
60 WhatToDo
= FileTypeNone
;
63 // initialize the shell lib (we must be in non-auto-init...)
65 Status
= ShellInitialize();
66 ASSERT_EFI_ERROR(Status
);
68 Status
= CommandInit();
69 ASSERT_EFI_ERROR(Status
);
72 // parse the command line
74 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
75 if (EFI_ERROR(Status
)) {
76 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
77 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, L
"hexedit", ProblemParam
);
78 FreePool(ProblemParam
);
79 ShellStatus
= SHELL_INVALID_PARAMETER
;
87 if (ShellCommandLineGetFlag(Package
, L
"-d")){
88 if (ShellCommandLineGetCount(Package
) < 4) {
89 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
, L
"hexedit");
90 ShellStatus
= SHELL_INVALID_PARAMETER
;
91 } else if (ShellCommandLineGetCount(Package
) > 4) {
92 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"hexedit");
93 ShellStatus
= SHELL_INVALID_PARAMETER
;
95 WhatToDo
= FileTypeDiskBuffer
;
96 Name
= ShellCommandLineGetRawValue(Package
, 1);
97 Offset
= ShellStrToUintn(ShellCommandLineGetRawValue(Package
, 2));
98 Size
= ShellStrToUintn(ShellCommandLineGetRawValue(Package
, 3));
100 if (Offset
== (UINTN
)-1 || Size
== (UINTN
)-1) {
101 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_VALUE
), gShellDebug1HiiHandle
, L
"hexedit", L
"-d");
102 ShellStatus
= SHELL_INVALID_PARAMETER
;
109 if (ShellCommandLineGetFlag(Package
, L
"-f") && (WhatToDo
== FileTypeNone
)){
110 if (ShellCommandLineGetCount(Package
) < 2) {
111 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
, L
"hexedit");
112 ShellStatus
= SHELL_INVALID_PARAMETER
;
113 } else if (ShellCommandLineGetCount(Package
) > 2) {
114 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"hexedit");
115 ShellStatus
= SHELL_INVALID_PARAMETER
;
117 Name
= ShellCommandLineGetRawValue(Package
, 1);
118 if (Name
== NULL
|| !IsValidFileName(Name
)) {
119 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"hexedit", Name
);
120 ShellStatus
= SHELL_INVALID_PARAMETER
;
122 WhatToDo
= FileTypeFileBuffer
;
130 if (ShellCommandLineGetFlag(Package
, L
"-m") && (WhatToDo
== FileTypeNone
)){
131 if (ShellCommandLineGetCount(Package
) < 3) {
132 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
, L
"hexedit");
133 ShellStatus
= SHELL_INVALID_PARAMETER
;
134 } else if (ShellCommandLineGetCount(Package
) > 3) {
135 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"hexedit");
136 ShellStatus
= SHELL_INVALID_PARAMETER
;
138 WhatToDo
= FileTypeMemBuffer
;
139 Offset
= ShellStrToUintn(ShellCommandLineGetRawValue(Package
, 1));
140 Size
= ShellStrToUintn(ShellCommandLineGetRawValue(Package
, 2));
143 Name
= ShellCommandLineGetRawValue(Package
, 1);
144 if (WhatToDo
== FileTypeNone
&& Name
!= NULL
) {
145 if (ShellCommandLineGetCount(Package
) > 2) {
146 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"hexedit");
147 ShellStatus
= SHELL_INVALID_PARAMETER
;
148 } else if (!IsValidFileName(Name
)) {
149 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"hexedit", Name
);
150 ShellStatus
= SHELL_INVALID_PARAMETER
;
152 WhatToDo
= FileTypeFileBuffer
;
154 } else if (WhatToDo
== FileTypeNone
) {
155 if (gEfiShellProtocol
->GetCurDir(NULL
) == NULL
) {
156 ShellStatus
= SHELL_NOT_FOUND
;
157 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellDebug1HiiHandle
, L
"hexedit");
159 NewName
= EditGetDefaultFileName(L
"bin");
161 WhatToDo
= FileTypeFileBuffer
;
165 if (ShellStatus
== SHELL_SUCCESS
&& WhatToDo
== FileTypeNone
) {
166 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
, L
"hexedit");
167 ShellStatus
= SHELL_INVALID_PARAMETER
;
168 } else if (WhatToDo
== FileTypeFileBuffer
&& ShellGetCurrentDir(NULL
) == NULL
) {
169 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellDebug1HiiHandle
, L
"hexedit");
170 ShellStatus
= SHELL_INVALID_PARAMETER
;
173 if (ShellStatus
== SHELL_SUCCESS
) {
177 Status
= HMainEditorInit ();
178 if (EFI_ERROR (Status
)) {
179 gST
->ConOut
->ClearScreen (gST
->ConOut
);
180 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
181 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HEXEDIT_INIT_FAILED
), gShellDebug1HiiHandle
);
183 HMainEditorBackup ();
185 case FileTypeFileBuffer
:
186 Status
= HBufferImageRead (
198 case FileTypeDiskBuffer
:
199 Status
= HBufferImageRead (
211 case FileTypeMemBuffer
:
212 Status
= HBufferImageRead (
225 Status
= EFI_NOT_FOUND
;
228 if (!EFI_ERROR (Status
)) {
229 HMainEditorRefresh ();
230 Status
= HMainEditorKeyInput ();
232 if (Status
!= EFI_OUT_OF_RESOURCES
) {
234 // back up the status string
236 Buffer
= CatSPrint (NULL
, L
"%s\r\n", StatusBarGetString());
243 HMainEditorCleanup ();
245 if (EFI_ERROR (Status
)) {
246 if (ShellStatus
== SHELL_SUCCESS
) {
247 ShellStatus
= SHELL_UNSUPPORTED
;
252 // print editor exit code on screen
254 if (Status
== EFI_OUT_OF_RESOURCES
) {
255 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellDebug1HiiHandle
, L
"hexedit");
256 } else if (EFI_ERROR(Status
)){
257 if (Buffer
!= NULL
) {
258 if (StrCmp (Buffer
, L
"") != 0) {
260 // print out the status string
262 ShellPrintEx(-1, -1, L
"%s", Buffer
);
264 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR
), gShellDebug1HiiHandle
);
267 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR
), gShellDebug1HiiHandle
);
271 ShellCommandLineFreeVarList (Package
);
274 SHELL_FREE_NON_NULL (Buffer
);
275 SHELL_FREE_NON_NULL (NewName
);