TianoCore EDK2 master
Loading...
Searching...
No Matches
HexEdit.c
Go to the documentation of this file.
1
11#include "HexEditor.h"
12
13//
14// Global Variables
15//
16STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
17 { L"-f", TypeFlag },
18 { L"-d", TypeFlag },
19 { L"-m", TypeFlag },
20 { NULL, TypeMax }
21};
22
30EFIAPI
32 IN EFI_HANDLE ImageHandle,
33 IN EFI_SYSTEM_TABLE *SystemTable
34 )
35{
36 EFI_STATUS Status;
37 CHAR16 *Buffer;
38 CHAR16 *ProblemParam;
39 SHELL_STATUS ShellStatus;
40 LIST_ENTRY *Package;
41 CHAR16 *NewName;
42 CONST CHAR16 *Name;
43 UINTN Offset;
44 UINTN Size;
45 EDIT_FILE_TYPE WhatToDo;
46
47 Buffer = NULL;
48 ShellStatus = SHELL_SUCCESS;
49 NewName = NULL;
50 Buffer = NULL;
51 Name = NULL;
52 Offset = 0;
53 Size = 0;
54 WhatToDo = FileTypeNone;
55
56 //
57 // initialize the shell lib (we must be in non-auto-init...)
58 //
59 Status = ShellInitialize ();
60 ASSERT_EFI_ERROR (Status);
61
62 Status = CommandInit ();
63 ASSERT_EFI_ERROR (Status);
64
65 //
66 // parse the command line
67 //
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;
74 } else {
75 ASSERT (FALSE);
76 }
77 } else {
78 //
79 // Check for -d
80 //
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;
88 } else {
89 WhatToDo = FileTypeDiskBuffer;
90 Name = ShellCommandLineGetRawValue (Package, 1);
91 Offset = ShellStrToUintn (ShellCommandLineGetRawValue (Package, 2));
92 Size = ShellStrToUintn (ShellCommandLineGetRawValue (Package, 3));
93 }
94
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;
98 }
99 }
100
101 //
102 // check for -f
103 //
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;
111 } else {
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;
116 } else {
117 WhatToDo = FileTypeFileBuffer;
118 }
119 }
120 }
121
122 //
123 // check for -m
124 //
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;
132 } else {
133 WhatToDo = FileTypeMemBuffer;
134 Offset = ShellStrToUintn (ShellCommandLineGetRawValue (Package, 1));
135 Size = ShellStrToUintn (ShellCommandLineGetRawValue (Package, 2));
136 }
137 }
138
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;
147 } else {
148 WhatToDo = FileTypeFileBuffer;
149 }
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");
154 } else {
155 NewName = EditGetDefaultFileName (L"bin");
156 Name = NewName;
157 WhatToDo = FileTypeFileBuffer;
158 }
159 }
160
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;
167 }
168
169 if (ShellStatus == SHELL_SUCCESS) {
170 //
171 // Do the editor
172 //
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);
178 } else {
180 switch (WhatToDo) {
181 case FileTypeFileBuffer:
182 Status = HBufferImageRead (
183 Name == NULL ? L"" : Name,
184 NULL,
185 0,
186 0,
187 0,
188 0,
189 FileTypeFileBuffer,
190 FALSE
191 );
192 break;
193
194 case FileTypeDiskBuffer:
195 Status = HBufferImageRead (
196 NULL,
197 Name == NULL ? L"" : Name,
198 Offset,
199 Size,
200 0,
201 0,
202 FileTypeDiskBuffer,
203 FALSE
204 );
205 break;
206
207 case FileTypeMemBuffer:
208 Status = HBufferImageRead (
209 NULL,
210 NULL,
211 0,
212 0,
213 (UINT32)Offset,
214 Size,
215 FileTypeMemBuffer,
216 FALSE
217 );
218 break;
219
220 default:
221 Status = EFI_NOT_FOUND;
222 break;
223 }
224
225 if (!EFI_ERROR (Status)) {
227 Status = HMainEditorKeyInput ();
228 }
229
230 if (Status != EFI_OUT_OF_RESOURCES) {
231 //
232 // back up the status string
233 //
234 Buffer = CatSPrint (NULL, L"%s\r\n", StatusBarGetString ());
235 }
236 }
237
238 //
239 // cleanup
240 //
242
243 if (EFI_ERROR (Status)) {
244 if (ShellStatus == SHELL_SUCCESS) {
245 ShellStatus = SHELL_UNSUPPORTED;
246 }
247 }
248
249 //
250 // print editor exit code on screen
251 //
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) {
257 //
258 // print out the status string
259 //
260 ShellPrintEx (-1, -1, L"%s", Buffer);
261 } else {
262 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR), gShellDebug1HiiHandle);
263 }
264 } else {
265 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR), gShellDebug1HiiHandle);
266 }
267 }
268 }
269
271 }
272
273 SHELL_FREE_NON_NULL (Buffer);
274 SHELL_FREE_NON_NULL (NewName);
275 return ShellStatus;
276}
UINT64 UINTN
INTN EFIAPI StrCmp(IN CONST CHAR16 *FirstString, IN CONST CHAR16 *SecondString)
Definition: String.c:109
EFI_STATUS HBufferImageRead(IN CONST CHAR16 *FileName, IN CONST CHAR16 *DiskName, IN UINTN DiskOffset, IN UINTN DiskSize, IN UINTN MemOffset, IN UINTN MemSize, IN EDIT_FILE_TYPE BufferType, IN BOOLEAN Recover)
Definition: BufferImage.c:909
CONST CHAR16 * StatusBarGetString(VOID)
VOID EFIAPI FreePool(IN VOID *Buffer)
SHELL_STATUS EFIAPI ShellCommandRunHexEdit(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: HexEdit.c:31
EFI_STATUS HMainEditorKeyInput(VOID)
VOID HMainEditorBackup(VOID)
EFI_STATUS HMainEditorRefresh(VOID)
EFI_STATUS HMainEditorCleanup(VOID)
EFI_STATUS HMainEditorInit(VOID)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
SHELL_STATUS
Definition: Shell.h:21
@ SHELL_SUCCESS
Definition: Shell.h:25
@ SHELL_NOT_FOUND
Definition: Shell.h:101
@ SHELL_UNSUPPORTED
Definition: Shell.h:40
@ SHELL_INVALID_PARAMETER
Definition: Shell.h:35
EFI_STATUS EFIAPI CommandInit(VOID)
CONST CHAR16 *EFIAPI ShellGetCurrentDir(IN CHAR16 *CONST DeviceName OPTIONAL)
UINTN EFIAPI ShellStrToUintn(IN CONST CHAR16 *String)
#define ShellCommandLineParse(CheckList, CheckPackage, ProblemParam, AutoPageBreak)
Make it easy to upgrade from older versions of the shell library.
Definition: ShellLib.h:755
EFI_STATUS EFIAPI ShellPrintHiiEx(IN INT32 Col OPTIONAL, IN INT32 Row OPTIONAL, IN CONST CHAR8 *Language OPTIONAL, IN CONST EFI_STRING_ID HiiFormatStringId, IN CONST EFI_HII_HANDLE HiiFormatHandle,...)
BOOLEAN EFIAPI ShellCommandLineGetFlag(IN CONST LIST_ENTRY *CONST CheckPackage, IN CONST CHAR16 *CONST KeyString)
@ TypeFlag
A flag that is present or not present only (IE "-a").
Definition: ShellLib.h:699
VOID EFIAPI ShellCommandLineFreeVarList(IN LIST_ENTRY *CheckPackage)
EFI_STATUS EFIAPI ShellInitialize(VOID)
Definition: UefiShellLib.c:532
EFI_STATUS EFIAPI ShellPrintEx(IN INT32 Col OPTIONAL, IN INT32 Row OPTIONAL, IN CONST CHAR16 *Format,...)
CONST CHAR16 *EFIAPI ShellCommandLineGetRawValue(IN CONST LIST_ENTRY *CONST CheckPackage, IN UINTN Position)
UINTN EFIAPI ShellCommandLineGetCount(IN CONST LIST_ENTRY *CheckPackage)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
EFI_SYSTEM_TABLE * gST
#define STRING_TOKEN(t)
CHAR16 *EFIAPI CatSPrint(IN CHAR16 *String OPTIONAL, IN CONST CHAR16 *FormatString,...)
Definition: UefiLibPrint.c:827
BOOLEAN IsValidFileName(IN CONST CHAR16 *Name)
CHAR16 * EditGetDefaultFileName(IN CONST CHAR16 *Extension)
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * ConOut
Definition: UefiSpec.h:2064