]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/HexEdit.c
smbiosview - add user input verification.
[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 CONST CHAR16 *Cwd;
47 CHAR16 *NewName;
48 CHAR16 *Spot;
49 CONST CHAR16 *Name;
50 UINTN Offset;
51 UINTN Size;
52 UINT64 LastOffset;
53 EDIT_FILE_TYPE WhatToDo;
54
55 Buffer = NULL;
56 ShellStatus = SHELL_SUCCESS;
57 NewName = NULL;
58 Cwd = NULL;
59 Buffer = NULL;
60 Name = NULL;
61 Spot = NULL;
62 Offset = 0;
63 Size = 0;
64 LastOffset = 0;
65 WhatToDo = FileTypeNone;
66
67 //
68 // initialize the shell lib (we must be in non-auto-init...)
69 //
70 Status = ShellInitialize();
71 ASSERT_EFI_ERROR(Status);
72
73 Status = CommandInit();
74 ASSERT_EFI_ERROR(Status);
75
76 //
77 // parse the command line
78 //
79 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
80 if (EFI_ERROR(Status)) {
81 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
82 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, ProblemParam);
83 FreePool(ProblemParam);
84 ShellStatus = SHELL_INVALID_PARAMETER;
85 } else {
86 ASSERT(FALSE);
87 }
88 } else {
89 //
90 // Check for -d
91 //
92 if (ShellCommandLineGetFlag(Package, L"-d")){
93 if (ShellCommandLineGetCount(Package) > 4) {
94 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);
95 ShellStatus = SHELL_INVALID_PARAMETER;
96 } else {
97 WhatToDo = FileTypeDiskBuffer;
98 Name = ShellCommandLineGetRawValue(Package, 1);
99 Offset = ShellStrToUintn(ShellCommandLineGetRawValue(Package, 2));
100 Size = ShellStrToUintn(ShellCommandLineGetRawValue(Package, 3));
101 }
102 }
103
104 //
105 // check for -f
106 //
107 if (ShellCommandLineGetFlag(Package, L"-f") && (WhatToDo == FileTypeNone)){
108 if (ShellCommandLineGetCount(Package) > 2) {
109 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);
110 ShellStatus = SHELL_INVALID_PARAMETER;
111 } else {
112 Name = ShellCommandLineGetRawValue(Package, 1);
113 if (!IsValidFileName(Name)) {
114 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, 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);
128 ShellStatus = SHELL_INVALID_PARAMETER;
129 } else if (ShellCommandLineGetCount(Package) > 3) {
130 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);
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 if (WhatToDo == FileTypeNone && ShellCommandLineGetRawValue(Package, 1) != NULL) {
139 Name = ShellCommandLineGetRawValue(Package, 1);
140 if (!IsValidFileName(Name)) {
141 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, Name);
142 ShellStatus = SHELL_INVALID_PARAMETER;
143 } else {
144 WhatToDo = FileTypeFileBuffer;
145 }
146 } else if (WhatToDo == FileTypeNone) {
147 if (gEfiShellProtocol->GetCurDir(NULL) == NULL) {
148 ShellStatus = SHELL_NOT_FOUND;
149 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellDebug1HiiHandle);
150 } else {
151 NewName = EditGetDefaultFileName(L"bin");
152 Name = NewName;
153 WhatToDo = FileTypeFileBuffer;
154 }
155 }
156
157 if (ShellStatus == SHELL_SUCCESS && WhatToDo == FileTypeNone) {
158 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle);
159 ShellStatus = SHELL_INVALID_PARAMETER;
160 }
161
162 if (ShellStatus == SHELL_SUCCESS) {
163 //
164 // Do the editor
165 //
166 Status = HMainEditorInit ();
167 if (EFI_ERROR (Status)) {
168 gST->ConOut->ClearScreen (gST->ConOut);
169 gST->ConOut->EnableCursor (gST->ConOut, TRUE);
170 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_HEXEDIT_INIT_FAILED), gShellDebug1HiiHandle);
171 } else {
172 HMainEditorBackup ();
173 switch (WhatToDo) {
174 case FileTypeFileBuffer:
175 Status = HBufferImageRead (
176 Name,
177 NULL,
178 0,
179 0,
180 0,
181 0,
182 FileTypeFileBuffer,
183 FALSE
184 );
185 break;
186
187 case FileTypeDiskBuffer:
188 Status = HBufferImageRead (
189 NULL,
190 Name,
191 Offset,
192 Size,
193 0,
194 0,
195 FileTypeDiskBuffer,
196 FALSE
197 );
198 break;
199
200 case FileTypeMemBuffer:
201 Status = HBufferImageRead (
202 NULL,
203 NULL,
204 0,
205 0,
206 (UINT32) Offset,
207 Size,
208 FileTypeMemBuffer,
209 FALSE
210 );
211 break;
212
213 }
214 if (!EFI_ERROR (Status)) {
215 HMainEditorRefresh ();
216 Status = HMainEditorKeyInput ();
217 }
218 if (Status != EFI_OUT_OF_RESOURCES) {
219 //
220 // back up the status string
221 //
222 Buffer = CatSPrint (NULL, L"%s", StatusBarGetString());
223 }
224 }
225
226 //
227 // cleanup
228 //
229 HMainEditorCleanup ();
230
231 if (!EFI_ERROR (Status)) {
232 if (ShellStatus == SHELL_SUCCESS) {
233 ShellStatus = SHELL_UNSUPPORTED;
234 }
235 }
236
237 //
238 // print editor exit code on screen
239 //
240 if (Status == EFI_OUT_OF_RESOURCES) {
241 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle);
242 } else if (EFI_ERROR(Status)){
243 if (Buffer != NULL) {
244 if (StrCmp (Buffer, L"") != 0) {
245 //
246 // print out the status string
247 //
248 ShellPrintEx(-1, -1, L"%s", gShellDebug1HiiHandle, Buffer);
249 } else {
250 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR), gShellDebug1HiiHandle);
251 }
252 } else {
253 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR), gShellDebug1HiiHandle);
254 }
255 }
256 }
257 ShellCommandLineFreeVarList (Package);
258 }
259
260 SHELL_FREE_NON_NULL (Buffer);
261 SHELL_FREE_NON_NULL (NewName);
262 return ShellStatus;
263 }