]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/HexEdit.c
ShellPkg: Fixed build error 'variable set but not used'
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / HexEdit / HexEdit.c
1 /** @file
2 Main entry point of editor
3
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
9
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.
12
13 **/
14
15 #include "UefiShellDebug1CommandsLib.h"
16 #include "HexEditor.h"
17
18 //
19 // Global Variables
20 //
21 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
22 {L"-f", TypeFlag},
23 {L"-d", TypeFlag},
24 {L"-m", TypeFlag},
25 {NULL, TypeMax}
26 };
27
28 /**
29 Function for 'hexedit' command.
30
31 @param[in] ImageHandle Handle to the Image (NULL if Internal).
32 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
33 **/
34 SHELL_STATUS
35 EFIAPI
36 ShellCommandRunHexEdit (
37 IN EFI_HANDLE ImageHandle,
38 IN EFI_SYSTEM_TABLE *SystemTable
39 )
40 {
41 EFI_STATUS Status;
42 CHAR16 *Buffer;
43 CHAR16 *ProblemParam;
44 SHELL_STATUS ShellStatus;
45 LIST_ENTRY *Package;
46 CHAR16 *NewName;
47 CONST CHAR16 *Name;
48 UINTN Offset;
49 UINTN Size;
50 EDIT_FILE_TYPE WhatToDo;
51
52 Buffer = NULL;
53 ShellStatus = SHELL_SUCCESS;
54 NewName = NULL;
55 Buffer = NULL;
56 Name = NULL;
57 Offset = 0;
58 Size = 0;
59 WhatToDo = FileTypeNone;
60
61 //
62 // initialize the shell lib (we must be in non-auto-init...)
63 //
64 Status = ShellInitialize();
65 ASSERT_EFI_ERROR(Status);
66
67 Status = CommandInit();
68 ASSERT_EFI_ERROR(Status);
69
70 //
71 // parse the command line
72 //
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;
79 } else {
80 ASSERT(FALSE);
81 }
82 } else {
83 //
84 // Check for -d
85 //
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;
93 } else {
94 WhatToDo = FileTypeDiskBuffer;
95 Name = ShellCommandLineGetRawValue(Package, 1);
96 Offset = ShellStrToUintn(ShellCommandLineGetRawValue(Package, 2));
97 Size = ShellStrToUintn(ShellCommandLineGetRawValue(Package, 3));
98 }
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;
102 }
103 }
104
105 //
106 // check for -f
107 //
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;
115 } else {
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;
120 } else {
121 WhatToDo = FileTypeFileBuffer;
122 }
123 }
124 }
125
126 //
127 // check for -m
128 //
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;
136 } else {
137 WhatToDo = FileTypeMemBuffer;
138 Offset = ShellStrToUintn(ShellCommandLineGetRawValue(Package, 1));
139 Size = ShellStrToUintn(ShellCommandLineGetRawValue(Package, 2));
140 }
141 }
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;
150 } else {
151 WhatToDo = FileTypeFileBuffer;
152 }
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);
157 } else {
158 NewName = EditGetDefaultFileName(L"bin");
159 Name = NewName;
160 WhatToDo = FileTypeFileBuffer;
161 }
162 }
163
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;
170 }
171
172 if (ShellStatus == SHELL_SUCCESS) {
173 //
174 // Do the editor
175 //
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);
181 } else {
182 HMainEditorBackup ();
183 switch (WhatToDo) {
184 case FileTypeFileBuffer:
185 Status = HBufferImageRead (
186 Name==NULL?L"":Name,
187 NULL,
188 0,
189 0,
190 0,
191 0,
192 FileTypeFileBuffer,
193 FALSE
194 );
195 break;
196
197 case FileTypeDiskBuffer:
198 Status = HBufferImageRead (
199 NULL,
200 Name==NULL?L"":Name,
201 Offset,
202 Size,
203 0,
204 0,
205 FileTypeDiskBuffer,
206 FALSE
207 );
208 break;
209
210 case FileTypeMemBuffer:
211 Status = HBufferImageRead (
212 NULL,
213 NULL,
214 0,
215 0,
216 (UINT32) Offset,
217 Size,
218 FileTypeMemBuffer,
219 FALSE
220 );
221 break;
222
223 default:
224 Status = EFI_NOT_FOUND;
225 break;
226 }
227 if (!EFI_ERROR (Status)) {
228 HMainEditorRefresh ();
229 Status = HMainEditorKeyInput ();
230 }
231 if (Status != EFI_OUT_OF_RESOURCES) {
232 //
233 // back up the status string
234 //
235 Buffer = CatSPrint (NULL, L"%s\r\n", StatusBarGetString());
236 }
237 }
238
239 //
240 // cleanup
241 //
242 HMainEditorCleanup ();
243
244 if (!EFI_ERROR (Status)) {
245 if (ShellStatus == SHELL_SUCCESS) {
246 ShellStatus = SHELL_UNSUPPORTED;
247 }
248 }
249
250 //
251 // print editor exit code on screen
252 //
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) {
258 //
259 // print out the status string
260 //
261 ShellPrintEx(-1, -1, L"%s", Buffer);
262 } else {
263 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR), gShellDebug1HiiHandle);
264 }
265 } else {
266 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR), gShellDebug1HiiHandle);
267 }
268 }
269 }
270 ShellCommandLineFreeVarList (Package);
271 }
272
273 SHELL_FREE_NON_NULL (Buffer);
274 SHELL_FREE_NON_NULL (NewName);
275 return ShellStatus;
276 }