]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellDebug1CommandsLib/Comp.c
MdeModulePkg/TerminalDxe: Avoid always append device path to *Dev
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / Comp.c
CommitLineData
5d73d92f 1/** @file\r
2 Main file for Comp shell Debug1 function.\r
3\r
c011b6c9 4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>\r
d571c666 5 Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>\r
5d73d92f 6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "UefiShellDebug1CommandsLib.h"\r
17\r
6e84fd6e
CC
18STATIC CONST SHELL_PARAM_ITEM ParamList[] = {\r
19 {L"-n", TypeValue},\r
20 {L"-s", TypeValue},\r
21 {NULL, TypeMax}\r
22 };\r
23\r
24typedef enum {\r
25 OutOfDiffPoint,\r
26 InDiffPoint,\r
27 InPrevDiffPoint\r
28} READ_STATUS;\r
29\r
30/**\r
31 Function to print differnt point data.\r
32\r
33 @param[in] FileName File name\r
34 @param[in] Buffer Data buffer to be printed.\r
35 @param[in] BufferSize Size of the data to be printed.\r
36 @param[in] Address Address of the differnt point.\r
37 @param[in] DifferentBytes Total size of the buffer.\r
38\r
39**/\r
40VOID\r
41PrintDifferentPoint(\r
42 CONST CHAR16 *FileName,\r
43 UINT8 *Buffer,\r
891417a7 44 UINT64 BufferSize,\r
6e84fd6e 45 UINTN Address,\r
891417a7 46 UINT64 DifferentBytes\r
6e84fd6e
CC
47 )\r
48{\r
49 UINTN Index;\r
50\r
51 ShellPrintEx (-1, -1, L"%s: %s\r\n %08x:", L"File1", FileName, Address);\r
52\r
53 //\r
54 // Print data in hex-format.\r
55 //\r
891417a7 56 for (Index = 0; Index < BufferSize; Index++) {\r
6e84fd6e
CC
57 ShellPrintEx (-1, -1, L" %02x", Buffer[Index]);\r
58 }\r
59\r
891417a7 60 if (BufferSize < DifferentBytes) {\r
6e84fd6e
CC
61 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_COMP_END_OF_FILE), gShellDebug1HiiHandle);\r
62 }\r
63\r
64 ShellPrintEx (-1, -1, L" *");\r
65\r
66 //\r
67 // Print data in char-format.\r
68 //\r
891417a7 69 for (Index = 0; Index < BufferSize; Index++) {\r
6e84fd6e
CC
70 if (Buffer[Index] >= 0x20 && Buffer[Index] <= 0x7E) {\r
71 ShellPrintEx (-1, -1, L"%c", Buffer[Index]);\r
72 } else {\r
73 //\r
74 // Print dots for control characters\r
75 //\r
76 ShellPrintEx (-1, -1, L".");\r
77 }\r
78 }\r
79\r
80 ShellPrintEx (-1, -1, L"*\r\n");\r
81}\r
82\r
3737ac2b 83/**\r
84 Function for 'comp' command.\r
85\r
86 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
87 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
88**/\r
5d73d92f 89SHELL_STATUS\r
90EFIAPI\r
91ShellCommandRunComp (\r
92 IN EFI_HANDLE ImageHandle,\r
93 IN EFI_SYSTEM_TABLE *SystemTable\r
94 )\r
95{\r
96 EFI_STATUS Status;\r
97 LIST_ENTRY *Package;\r
98 CHAR16 *ProblemParam;\r
d571c666
CC
99 CHAR16 *FileName1;\r
100 CHAR16 *FileName2;\r
101 CONST CHAR16 *TempParam;\r
5d73d92f 102 SHELL_STATUS ShellStatus;\r
5d73d92f 103 SHELL_FILE_HANDLE FileHandle1;\r
104 SHELL_FILE_HANDLE FileHandle2;\r
5d73d92f 105 UINT64 Size1;\r
106 UINT64 Size2;\r
6e84fd6e
CC
107 UINT64 DifferentBytes;\r
108 UINT64 DifferentCount;\r
109 UINT8 DiffPointNumber;\r
110 UINT8 OneByteFromFile1;\r
111 UINT8 OneByteFromFile2;\r
112 UINT8 *DataFromFile1;\r
113 UINT8 *DataFromFile2;\r
114 UINTN InsertPosition1;\r
115 UINTN InsertPosition2;\r
5d73d92f 116 UINTN DataSizeFromFile1;\r
117 UINTN DataSizeFromFile2;\r
6e84fd6e
CC
118 UINTN TempAddress;\r
119 UINTN Index;\r
d571c666 120 UINTN DiffPointAddress;\r
6e84fd6e 121 READ_STATUS ReadStatus;\r
5d73d92f 122\r
5d73d92f 123 ShellStatus = SHELL_SUCCESS;\r
124 Status = EFI_SUCCESS;\r
125 FileName1 = NULL;\r
126 FileName2 = NULL;\r
127 FileHandle1 = NULL;\r
128 FileHandle2 = NULL;\r
6e84fd6e
CC
129 DataFromFile1 = NULL;\r
130 DataFromFile2 = NULL;\r
131 ReadStatus = OutOfDiffPoint;\r
132 DifferentCount = 10;\r
133 DifferentBytes = 4;\r
134 DiffPointNumber = 0;\r
135 InsertPosition1 = 0;\r
136 InsertPosition2 = 0;\r
137 TempAddress = 0;\r
31abcf1d 138 DiffPointAddress = 0;\r
5d73d92f 139\r
140 //\r
141 // initialize the shell lib (we must be in non-auto-init...)\r
142 //\r
143 Status = ShellInitialize();\r
144 ASSERT_EFI_ERROR(Status);\r
145\r
146 Status = CommandInit();\r
147 ASSERT_EFI_ERROR(Status);\r
148\r
149 //\r
150 // parse the command line\r
151 //\r
6e84fd6e 152 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);\r
5d73d92f 153 if (EFI_ERROR(Status)) {\r
154 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
4092a8f6 155 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"comp", ProblemParam); \r
5d73d92f 156 FreePool(ProblemParam);\r
157 ShellStatus = SHELL_INVALID_PARAMETER;\r
158 } else {\r
159 ASSERT(FALSE);\r
160 }\r
161 } else {\r
162 if (ShellCommandLineGetCount(Package) > 3) {\r
4092a8f6 163 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"comp"); \r
5d73d92f 164 ShellStatus = SHELL_INVALID_PARAMETER;\r
165 } else if (ShellCommandLineGetCount(Package) < 3) {\r
4092a8f6 166 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"comp"); \r
5d73d92f 167 ShellStatus = SHELL_INVALID_PARAMETER;\r
168 } else {\r
ecae5117 169 TempParam = ShellCommandLineGetRawValue(Package, 1);\r
170 ASSERT(TempParam != NULL);\r
171 FileName1 = ShellFindFilePath(TempParam);\r
3737ac2b 172 if (FileName1 == NULL) {\r
4092a8f6 173 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, L"comp", TempParam); \r
5d73d92f 174 ShellStatus = SHELL_NOT_FOUND;\r
3737ac2b 175 } else {\r
176 Status = ShellOpenFileByName(FileName1, &FileHandle1, EFI_FILE_MODE_READ, 0);\r
177 if (EFI_ERROR(Status)) {\r
4092a8f6 178 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"comp", TempParam); \r
3737ac2b 179 ShellStatus = SHELL_NOT_FOUND;\r
180 }\r
5d73d92f 181 }\r
ecae5117 182 TempParam = ShellCommandLineGetRawValue(Package, 2);\r
183 ASSERT(TempParam != NULL);\r
184 FileName2 = ShellFindFilePath(TempParam);\r
3737ac2b 185 if (FileName2 == NULL) {\r
4092a8f6 186 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, L"comp", TempParam); \r
5d73d92f 187 ShellStatus = SHELL_NOT_FOUND;\r
3737ac2b 188 } else {\r
189 Status = ShellOpenFileByName(FileName2, &FileHandle2, EFI_FILE_MODE_READ, 0);\r
190 if (EFI_ERROR(Status)) {\r
4092a8f6 191 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"comp", TempParam); \r
3737ac2b 192 ShellStatus = SHELL_NOT_FOUND;\r
193 }\r
5d73d92f 194 }\r
195 if (ShellStatus == SHELL_SUCCESS) {\r
5d73d92f 196 Status = gEfiShellProtocol->GetFileSize(FileHandle1, &Size1);\r
197 ASSERT_EFI_ERROR(Status);\r
198 Status = gEfiShellProtocol->GetFileSize(FileHandle2, &Size2);\r
199 ASSERT_EFI_ERROR(Status);\r
6e84fd6e
CC
200\r
201 if (ShellCommandLineGetFlag (Package, L"-n")) {\r
202 TempParam = ShellCommandLineGetValue (Package, L"-n");\r
203 if (TempParam == NULL) {\r
204 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDebug1HiiHandle, L"comp", L"-n");\r
205 ShellStatus = SHELL_INVALID_PARAMETER;\r
206 } else {\r
207 if (gUnicodeCollation->StriColl (gUnicodeCollation, (CHAR16 *)TempParam, L"all") == 0) {\r
208 DifferentCount = MAX_UINTN;\r
209 } else {\r
210 Status = ShellConvertStringToUint64 (TempParam, &DifferentCount, FALSE, TRUE);\r
211 if (EFI_ERROR(Status) || DifferentCount == 0) {\r
212 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellDebug1HiiHandle, L"comp", TempParam, L"-n");\r
213 ShellStatus = SHELL_INVALID_PARAMETER;\r
214 }\r
215 }\r
216 }\r
217 }\r
218\r
219 if (ShellCommandLineGetFlag (Package, L"-s")) {\r
220 TempParam = ShellCommandLineGetValue (Package, L"-s");\r
221 if (TempParam == NULL) {\r
222 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDebug1HiiHandle, L"comp", L"-s");\r
223 ShellStatus = SHELL_INVALID_PARAMETER;\r
224 } else {\r
225 Status = ShellConvertStringToUint64 (TempParam, &DifferentBytes, FALSE, TRUE);\r
226 if (EFI_ERROR(Status) || DifferentBytes == 0) {\r
227 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellDebug1HiiHandle, L"comp", TempParam, L"-s");\r
228 ShellStatus = SHELL_INVALID_PARAMETER;\r
229 } else {\r
230 if (DifferentBytes > MAX (Size1, Size2)) {\r
231 DifferentBytes = MAX (Size1, Size2);\r
232 }\r
233 }\r
234 }\r
5d73d92f 235 }\r
236 }\r
6e84fd6e 237\r
5d73d92f 238 if (ShellStatus == SHELL_SUCCESS) {\r
6e84fd6e
CC
239 DataFromFile1 = AllocateZeroPool ((UINTN)DifferentBytes);\r
240 DataFromFile2 = AllocateZeroPool ((UINTN)DifferentBytes);\r
241 if (DataFromFile1 == NULL || DataFromFile2 == NULL) {\r
242 ShellStatus = SHELL_OUT_OF_RESOURCES;\r
243 SHELL_FREE_NON_NULL (DataFromFile1);\r
244 SHELL_FREE_NON_NULL (DataFromFile2);\r
245 }\r
246 }\r
247\r
248 if (ShellStatus == SHELL_SUCCESS) {\r
249 while (DiffPointNumber < DifferentCount) {\r
5d73d92f 250 DataSizeFromFile1 = 1;\r
251 DataSizeFromFile2 = 1;\r
6e84fd6e
CC
252 OneByteFromFile1 = 0;\r
253 OneByteFromFile2 = 0;\r
254 Status = gEfiShellProtocol->ReadFile (FileHandle1, &DataSizeFromFile1, &OneByteFromFile1);\r
255 ASSERT_EFI_ERROR (Status);\r
256 Status = gEfiShellProtocol->ReadFile (FileHandle2, &DataSizeFromFile2, &OneByteFromFile2);\r
257 ASSERT_EFI_ERROR (Status);\r
33eb033f 258\r
6e84fd6e
CC
259 TempAddress++;\r
260\r
261 //\r
262 // 1.When end of file and no chars in DataFromFile buffer, then break while.\r
263 // 2.If no more char in File1 or File2, The ReadStatus is InPrevDiffPoint forever.\r
264 // So the previous different point is the last one, then break the while block.\r
265 //\r
266 if ( (DataSizeFromFile1 == 0 && InsertPosition1 == 0 && DataSizeFromFile2 == 0 && InsertPosition2 == 0) ||\r
267 (ReadStatus == InPrevDiffPoint && (DataSizeFromFile1 == 0 || DataSizeFromFile2 == 0))\r
268 ) {\r
269 break;\r
270 }\r
271\r
272 if (ReadStatus == OutOfDiffPoint) {\r
273 if (OneByteFromFile1 != OneByteFromFile2) {\r
274 ReadStatus = InDiffPoint;\r
275 DiffPointAddress = TempAddress;\r
276 if (DataSizeFromFile1 == 1) {\r
277 DataFromFile1[InsertPosition1++] = OneByteFromFile1;\r
278 }\r
279 if (DataSizeFromFile2 == 1) {\r
280 DataFromFile2[InsertPosition2++] = OneByteFromFile2;\r
5d73d92f 281 }\r
282 }\r
6e84fd6e
CC
283 } else if (ReadStatus == InDiffPoint) {\r
284 if (DataSizeFromFile1 == 1) {\r
285 DataFromFile1[InsertPosition1++] = OneByteFromFile1;\r
286 }\r
287 if (DataSizeFromFile2 == 1) {\r
288 DataFromFile2[InsertPosition2++] = OneByteFromFile2;\r
289 }\r
290 } else if (ReadStatus == InPrevDiffPoint) {\r
291 if (OneByteFromFile1 == OneByteFromFile2) {\r
292 ReadStatus = OutOfDiffPoint;\r
293 }\r
294 }\r
295\r
296 //\r
297 // ReadStatus should be always equal InDiffPoint.\r
298 //\r
299 if ( InsertPosition1 == DifferentBytes ||\r
300 InsertPosition2 == DifferentBytes ||\r
301 (DataSizeFromFile1 == 0 && DataSizeFromFile2 == 0)\r
302 ) {\r
303\r
304 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_COMP_DIFFERENCE_POINT), gShellDebug1HiiHandle, ++DiffPointNumber);\r
305 PrintDifferentPoint (FileName1, DataFromFile1, InsertPosition1, DiffPointAddress, DifferentBytes);\r
306 PrintDifferentPoint (FileName2, DataFromFile2, InsertPosition2, DiffPointAddress, DifferentBytes);\r
33eb033f
JC
307\r
308 //\r
6e84fd6e 309 // One of two buffuers is empty, it means this is the last different point.\r
33eb033f 310 //\r
6e84fd6e
CC
311 if (InsertPosition1 == 0 || InsertPosition2 == 0) {\r
312 break;\r
313 }\r
314\r
315 for (Index = 1; Index < InsertPosition1 && Index < InsertPosition2; Index++) {\r
316 if (DataFromFile1[Index] == DataFromFile2[Index]) {\r
317 ReadStatus = OutOfDiffPoint;\r
318 break;\r
319 }\r
320 }\r
321\r
322 if (ReadStatus == OutOfDiffPoint) {\r
323 //\r
324 // Try to find a new different point in the rest of DataFromFile.\r
325 //\r
326 for (; Index < MAX (InsertPosition1,InsertPosition2); Index++) {\r
327 if (DataFromFile1[Index] != DataFromFile2[Index]) {\r
328 ReadStatus = InDiffPoint;\r
329 DiffPointAddress += Index;\r
330 break;\r
331 }\r
332 }\r
5d73d92f 333 } else {\r
6e84fd6e
CC
334 //\r
335 // Doesn't find a new different point, still in the same different point.\r
336 //\r
337 ReadStatus = InPrevDiffPoint;\r
5d73d92f 338 }\r
6e84fd6e
CC
339\r
340 CopyMem (DataFromFile1, DataFromFile1 + Index, InsertPosition1 - Index);\r
341 CopyMem (DataFromFile2, DataFromFile2 + Index, InsertPosition2 - Index);\r
342\r
343 SetMem (DataFromFile1 + InsertPosition1 - Index, (UINTN)DifferentBytes - InsertPosition1 + Index, 0);\r
344 SetMem (DataFromFile2 + InsertPosition2 - Index, (UINTN)DifferentBytes - InsertPosition2 + Index, 0);\r
345\r
346 InsertPosition1 -= Index;\r
347 InsertPosition2 -= Index;\r
5d73d92f 348 }\r
349 }\r
6e84fd6e
CC
350\r
351 SHELL_FREE_NON_NULL (DataFromFile1);\r
352 SHELL_FREE_NON_NULL (DataFromFile2);\r
353\r
354 if (DiffPointNumber == 0) {\r
3737ac2b 355 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_COMP_FOOTER_PASS), gShellDebug1HiiHandle);\r
356 } else {\r
357 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_COMP_FOOTER_FAIL), gShellDebug1HiiHandle);\r
358 }\r
5d73d92f 359 }\r
360 }\r
361\r
362 ShellCommandLineFreeVarList (Package);\r
363 }\r
3737ac2b 364 SHELL_FREE_NON_NULL(FileName1);\r
365 SHELL_FREE_NON_NULL(FileName2);\r
366\r
5d73d92f 367 if (FileHandle1 != NULL) {\r
368 gEfiShellProtocol->CloseFile(FileHandle1);\r
369 }\r
370 if (FileHandle2 != NULL) {\r
371 gEfiShellProtocol->CloseFile(FileHandle2);\r
372 }\r
373\r
374 return (ShellStatus);\r
375}\r