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