]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/HexEdit.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / HexEdit / HexEdit.c
1 /** @file
2 Main entry point of editor
3
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
7
8 **/
9
10 #include "UefiShellDebug1CommandsLib.h"
11 #include "HexEditor.h"
12
13 //
14 // Global Variables
15 //
16 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
17 { L"-f", TypeFlag },
18 { L"-d", TypeFlag },
19 { L"-m", TypeFlag },
20 { NULL, TypeMax }
21 };
22
23 /**
24 Function for 'hexedit' command.
25
26 @param[in] ImageHandle Handle to the Image (NULL if Internal).
27 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
28 **/
29 SHELL_STATUS
30 EFIAPI
31 ShellCommandRunHexEdit (
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 {
179 HMainEditorBackup ();
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)) {
226 HMainEditorRefresh ();
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 //
241 HMainEditorCleanup ();
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
270 ShellCommandLineFreeVarList (Package);
271 }
272
273 SHELL_FREE_NON_NULL (Buffer);
274 SHELL_FREE_NON_NULL (NewName);
275 return ShellStatus;
276 }