]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/HexEdit.c
511eb9e6366e66d3b4f374dcb494afbc3a75aa39
[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 *NFS;
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 NFS = 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_FEW), gShellDebug1HiiHandle);
95 ShellStatus = SHELL_INVALID_PARAMETER;
96 } else if (ShellCommandLineGetCount(Package) > 4) {
97 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);
98 ShellStatus = SHELL_INVALID_PARAMETER;
99 } else {
100 WhatToDo = FileTypeDiskBuffer;
101 Name = ShellCommandLineGetRawValue(Package, 1);
102 Offset = ShellStrToUintn(ShellCommandLineGetRawValue(Package, 2));
103 Size = ShellStrToUintn(ShellCommandLineGetRawValue(Package, 3));
104 }
105 }
106
107 //
108 // check for -f
109 //
110 if (ShellCommandLineGetFlag(Package, L"-f") && (WhatToDo == FileTypeNone)){
111 if (ShellCommandLineGetCount(Package) > 2) {
112 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);
113 ShellStatus = SHELL_INVALID_PARAMETER;
114 } else {
115 Name = ShellCommandLineGetRawValue(Package, 1);
116 if (!IsValidFileName(Name)) {
117 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, Name);
118 ShellStatus = SHELL_INVALID_PARAMETER;
119 } else {
120 WhatToDo = FileTypeFileBuffer;
121 }
122 }
123 }
124
125 //
126 // check for -m
127 //
128 if (ShellCommandLineGetFlag(Package, L"-m") && (WhatToDo == FileTypeNone)){
129 if (ShellCommandLineGetCount(Package) < 3) {
130 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle);
131 ShellStatus = SHELL_INVALID_PARAMETER;
132 } else if (ShellCommandLineGetCount(Package) > 3) {
133 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);
134 ShellStatus = SHELL_INVALID_PARAMETER;
135 } else {
136 WhatToDo = FileTypeMemBuffer;
137 Offset = ShellStrToUintn(ShellCommandLineGetRawValue(Package, 1));
138 Size = ShellStrToUintn(ShellCommandLineGetRawValue(Package, 2));
139 }
140 }
141 ShellCommandLineFreeVarList (Package);
142
143 if (ShellStatus == SHELL_SUCCESS && WhatToDo == FileTypeNone) {
144 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle);
145 ShellStatus = SHELL_INVALID_PARAMETER;
146 }
147
148 if (ShellStatus == SHELL_SUCCESS) {
149 //
150 // Do the editor
151 //
152 Status = HMainEditorInit ();
153 if (EFI_ERROR (Status)) {
154 gST->ConOut->ClearScreen (gST->ConOut);
155 gST->ConOut->EnableCursor (gST->ConOut, TRUE);
156 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_HEXEDIT_INIT_FAILED), gShellDebug1HiiHandle);
157 } else {
158 HMainEditorBackup ();
159 switch (WhatToDo) {
160 case FileTypeFileBuffer:
161 Status = HBufferImageRead (
162 Name,
163 NULL,
164 0,
165 0,
166 0,
167 0,
168 FileTypeFileBuffer,
169 FALSE
170 );
171 break;
172
173 case FileTypeDiskBuffer:
174 Status = HBufferImageRead (
175 NULL,
176 Name,
177 Offset,
178 Size,
179 0,
180 0,
181 FileTypeDiskBuffer,
182 FALSE
183 );
184 break;
185
186 case FileTypeMemBuffer:
187 Status = HBufferImageRead (
188 NULL,
189 NULL,
190 0,
191 0,
192 (UINT32) Offset,
193 Size,
194 FileTypeMemBuffer,
195 FALSE
196 );
197 break;
198
199 }
200 if (!EFI_ERROR (Status)) {
201 HMainEditorRefresh ();
202 Status = HMainEditorKeyInput ();
203 }
204 if (Status != EFI_OUT_OF_RESOURCES) {
205 //
206 // back up the status string
207 //
208 Buffer = CatSPrint (NULL, L"%s", StatusBarGetString());
209 }
210 }
211
212 //
213 // cleanup
214 //
215 HMainEditorCleanup ();
216
217 if (!EFI_ERROR (Status)) {
218 if (ShellStatus == SHELL_SUCCESS) {
219 ShellStatus = SHELL_UNSUPPORTED;
220 }
221 }
222
223 //
224 // print editor exit code on screen
225 //
226 if (Status == EFI_OUT_OF_RESOURCES) {
227 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle);
228 } else if (EFI_ERROR(Status)){
229 if (Buffer != NULL) {
230 if (StrCmp (Buffer, L"") != 0) {
231 //
232 // print out the status string
233 //
234 ShellPrintEx(-1, -1, L"%s", gShellDebug1HiiHandle, Buffer);
235 } else {
236 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR), gShellDebug1HiiHandle);
237 }
238 } else {
239 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR), gShellDebug1HiiHandle);
240 }
241 }
242 }
243 }
244
245 SHELL_FREE_NON_NULL (Buffer);
246 return ShellStatus;
247 }