summaryrefslogtreecommitdiffstats
path: root/ShellPkg/Library/UefiShellDebug1CommandsLib/Comp.c
blob: 5b91327efcb7d219ceb9b514b565057641439eed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/** @file
  Main file for Comp shell Debug1 function.

  (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
  Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>
  This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include "UefiShellDebug1CommandsLib.h"

STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
  {L"-n", TypeValue},
  {L"-s", TypeValue},
  {NULL,  TypeMax}
  };

typedef enum {
  OutOfDiffPoint,
  InDiffPoint,
  InPrevDiffPoint
} READ_STATUS;

/**
  Function to print differnt point data.

  @param[in]  FileName        File name
  @param[in]  Buffer          Data buffer to be printed.
  @param[in]  BufferSize      Size of the data to be printed.
  @param[in]  Address         Address of the differnt point.
  @param[in]  DifferentBytes  Total size of the buffer.

**/
VOID
PrintDifferentPoint(
  CONST CHAR16  *FileName,
  UINT8         *Buffer,
  UINT64        DataSize,
  UINTN         Address,
  UINT64        BufferSize
  )
{
  UINTN Index;

  ShellPrintEx (-1, -1, L"%s: %s\r\n  %08x:", L"File1", FileName, Address);

  //
  // Print data in hex-format.
  //
  for (Index = 0; Index < DataSize; Index++) {
    ShellPrintEx (-1, -1, L" %02x", Buffer[Index]);
  }

  if (DataSize < BufferSize) {
    ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_COMP_END_OF_FILE), gShellDebug1HiiHandle);
  }

  ShellPrintEx (-1, -1, L"    *");

  //
  // Print data in char-format.
  //
  for (Index = 0; Index < DataSize; Index++) {
    if (Buffer[Index] >= 0x20 && Buffer[Index] <= 0x7E) {
      ShellPrintEx (-1, -1, L"%c", Buffer[Index]);
    } else {
      //
      // Print dots for control characters
      //
      ShellPrintEx (-1, -1, L".");
    }
  }

  ShellPrintEx (-1, -1, L"*\r\n");
}

/**
  Function for 'comp' command.

  @param[in] ImageHandle  Handle to the Image (NULL if Internal).
  @param[in] SystemTable  Pointer to the System Table (NULL if Internal).
**/
SHELL_STATUS
EFIAPI
ShellCommandRunComp (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_STATUS          Status;
  LIST_ENTRY          *Package;
  CHAR16              *ProblemParam;
  CHAR16              *FileName1;
  CHAR16              *FileName2;
  CONST CHAR16        *TempParam;
  SHELL_STATUS        ShellStatus;
  SHELL_FILE_HANDLE   FileHandle1;
  SHELL_FILE_HANDLE   FileHandle2;
  UINT64              Size1;
  UINT64              Size2;
  UINT64              DifferentBytes;
  UINT64              DifferentCount;
  UINT8               DiffPointNumber;
  UINT8               OneByteFromFile1;
  UINT8               OneByteFromFile2;
  UINT8               *DataFromFile1;
  UINT8               *DataFromFile2;
  UINTN               InsertPosition1;
  UINTN               InsertPosition2;
  UINTN               DataSizeFromFile1;
  UINTN               DataSizeFromFile2;
  UINTN               TempAddress;
  UINTN               Index;
  UINTN               DiffPointAddress;
  READ_STATUS         ReadStatus;

  ShellStatus         = SHELL_SUCCESS;
  Status              = EFI_SUCCESS;
  FileName1           = NULL;
  FileName2           = NULL;
  FileHandle1         = NULL;
  FileHandle2         = NULL;
  DataFromFile1       = NULL;
  DataFromFile2       = NULL;
  ReadStatus          = OutOfDiffPoint;
  DifferentCount      = 10;
  DifferentBytes      = 4;
  DiffPointNumber     = 0;
  InsertPosition1     = 0;
  InsertPosition2     = 0;
  TempAddress         = 0;
  DiffPointAddress    = 0;

  //
  // initialize the shell lib (we must be in non-auto-init...)
  //
  Status = ShellInitialize();
  ASSERT_EFI_ERROR(Status);

  Status = CommandInit();
  ASSERT_EFI_ERROR(Status);

  //
  // parse the command line
  //
  Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
  if (EFI_ERROR(Status)) {
    if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"comp", ProblemParam);  
      FreePool(ProblemParam);
      ShellStatus = SHELL_INVALID_PARAMETER;
    } else {
      ASSERT(FALSE);
    }
  } else {
    if (ShellCommandLineGetCount(Package) > 3) {
      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"comp");  
      ShellStatus = SHELL_INVALID_PARAMETER;
    } else if (ShellCommandLineGetCount(Package) < 3) {
      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"comp");  
      ShellStatus = SHELL_INVALID_PARAMETER;
    } else {
      TempParam = ShellCommandLineGetRawValue(Package, 1);
      ASSERT(TempParam != NULL);
      FileName1 = ShellFindFilePath(TempParam);
      if (FileName1 == NULL) {
        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, L"comp", TempParam);  
        ShellStatus = SHELL_NOT_FOUND;
      } else {
        Status = ShellOpenFileByName(FileName1, &FileHandle1, EFI_FILE_MODE_READ, 0);
        if (EFI_ERROR(Status)) {
          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"comp", TempParam);  
          ShellStatus = SHELL_NOT_FOUND;
        }
      }
      TempParam = ShellCommandLineGetRawValue(Package, 2);
      ASSERT(TempParam != NULL);
      FileName2 = ShellFindFilePath(TempParam);
      if (FileName2 == NULL) {
        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, L"comp", TempParam);  
        ShellStatus = SHELL_NOT_FOUND;
      } else {
        Status = ShellOpenFileByName(FileName2, &FileHandle2, EFI_FILE_MODE_READ, 0);
        if (EFI_ERROR(Status)) {
          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"comp", TempParam);  
          ShellStatus = SHELL_NOT_FOUND;
        }
      }
      if (ShellStatus == SHELL_SUCCESS) {
        Status = gEfiShellProtocol->GetFileSize(FileHandle1, &Size1);
        ASSERT_EFI_ERROR(Status);
        Status = gEfiShellProtocol->GetFileSize(FileHandle2, &Size2);
        ASSERT_EFI_ERROR(Status);

        if (ShellCommandLineGetFlag (Package, L"-n")) {
          TempParam = ShellCommandLineGetValue (Package, L"-n");
          if (TempParam == NULL) {
            ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDebug1HiiHandle, L"comp", L"-n");
            ShellStatus = SHELL_INVALID_PARAMETER;
          } else {
            if (gUnicodeCollation->StriColl (gUnicodeCollation, (CHAR16 *)TempParam, L"all") == 0) {
              DifferentCount = MAX_UINTN;
            } else {
              Status = ShellConvertStringToUint64 (TempParam, &DifferentCount, FALSE, TRUE);
              if (EFI_ERROR(Status) || DifferentCount == 0) {
                ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellDebug1HiiHandle, L"comp", TempParam, L"-n");
                ShellStatus = SHELL_INVALID_PARAMETER;
              }
            }
          }
        }

        if (ShellCommandLineGetFlag (Package, L"-s")) {
          TempParam = ShellCommandLineGetValue (Package, L"-s");
          if (TempParam == NULL) {
            ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDebug1HiiHandle, L"comp", L"-s");
            ShellStatus = SHELL_INVALID_PARAMETER;
          } else {
            Status = ShellConvertStringToUint64 (TempParam, &DifferentBytes, FALSE, TRUE);
            if (EFI_ERROR(Status) || DifferentBytes == 0) {
              ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellDebug1HiiHandle, L"comp", TempParam, L"-s");
              ShellStatus = SHELL_INVALID_PARAMETER;
            } else {
              if (DifferentBytes > MAX (Size1, Size2)) {
                DifferentBytes = MAX (Size1, Size2);
              }
            }
          }
        }
      }

      if (ShellStatus == SHELL_SUCCESS) {
        DataFromFile1 = AllocateZeroPool ((UINTN)DifferentBytes);
        DataFromFile2 = AllocateZeroPool ((UINTN)DifferentBytes);
        if (DataFromFile1 == NULL || DataFromFile2 == NULL) {
          ShellStatus = SHELL_OUT_OF_RESOURCES;
          SHELL_FREE_NON_NULL (DataFromFile1);
          SHELL_FREE_NON_NULL (DataFromFile2);
        }
      }

      if (ShellStatus == SHELL_SUCCESS) {
        while (DiffPointNumber < DifferentCount) {
          DataSizeFromFile1 = 1;
          DataSizeFromFile2 = 1;
          OneByteFromFile1 = 0;
          OneByteFromFile2 = 0;
          Status = gEfiShellProtocol->ReadFile (FileHandle1, &DataSizeFromFile1, &OneByteFromFile1);
          ASSERT_EFI_ERROR (Status);
          Status = gEfiShellProtocol->ReadFile (FileHandle2, &DataSizeFromFile2, &OneByteFromFile2);
          ASSERT_EFI_ERROR (Status);

          TempAddress++;

          //
          // 1.When end of file and no chars in DataFromFile buffer, then break while.
          // 2.If no more char in File1 or File2, The ReadStatus is InPrevDiffPoint forever.
          //   So the previous different point is the last one, then break the while block.
          //
          if ( (DataSizeFromFile1 == 0 && InsertPosition1 == 0 && DataSizeFromFile2 == 0 && InsertPosition2 == 0) ||
               (ReadStatus == InPrevDiffPoint && (DataSizeFromFile1 == 0 || DataSizeFromFile2 == 0))
             ) {
            break;
          }

          if (ReadStatus == OutOfDiffPoint) {
            if (OneByteFromFile1 != OneByteFromFile2) {
              ReadStatus = InDiffPoint;
              DiffPointAddress = TempAddress;
              if (DataSizeFromFile1 == 1) {
                DataFromFile1[InsertPosition1++] = OneByteFromFile1;
              }
              if (DataSizeFromFile2 == 1) {
                DataFromFile2[InsertPosition2++] = OneByteFromFile2;
              }
            }
          } else if (ReadStatus == InDiffPoint) {
            if (DataSizeFromFile1 == 1) {
              DataFromFile1[InsertPosition1++] = OneByteFromFile1;
            }
            if (DataSizeFromFile2 == 1) {
              DataFromFile2[InsertPosition2++] = OneByteFromFile2;
            }
          } else if (ReadStatus == InPrevDiffPoint) {
            if (OneByteFromFile1 == OneByteFromFile2) {
              ReadStatus = OutOfDiffPoint;
            }
          }

          //
          // ReadStatus should be always equal InDiffPoint.
          //
          if ( InsertPosition1 == DifferentBytes ||
               InsertPosition2 == DifferentBytes ||
               (DataSizeFromFile1 == 0 && DataSizeFromFile2 == 0)
             ) {

            ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_COMP_DIFFERENCE_POINT), gShellDebug1HiiHandle, ++DiffPointNumber);
            PrintDifferentPoint (FileName1, DataFromFile1, InsertPosition1, DiffPointAddress, DifferentBytes);
            PrintDifferentPoint (FileName2, DataFromFile2, InsertPosition2, DiffPointAddress, DifferentBytes);

            //
            // One of two buffuers is empty, it means this is the last different point.
            //
            if (InsertPosition1 == 0 || InsertPosition2 == 0) {
              break;
            }

            for (Index = 1; Index < InsertPosition1 && Index < InsertPosition2; Index++) {
              if (DataFromFile1[Index] == DataFromFile2[Index]) {
                ReadStatus = OutOfDiffPoint;
                break;
              }
            }

            if (ReadStatus == OutOfDiffPoint) {
              //
              // Try to find a new different point in the rest of DataFromFile.
              //
              for (; Index < MAX (InsertPosition1,InsertPosition2); Index++) {
                if (DataFromFile1[Index] != DataFromFile2[Index]) {
                  ReadStatus = InDiffPoint;
                  DiffPointAddress += Index;
                  break;
                }
              }
            } else {
              //
              // Doesn't find a new different point, still in the same different point.
              //
              ReadStatus = InPrevDiffPoint;
            }

            CopyMem (DataFromFile1, DataFromFile1 + Index, InsertPosition1 - Index);
            CopyMem (DataFromFile2, DataFromFile2 + Index, InsertPosition2 - Index);

            SetMem (DataFromFile1 + InsertPosition1 - Index, (UINTN)DifferentBytes - InsertPosition1 + Index, 0);
            SetMem (DataFromFile2 + InsertPosition2 - Index, (UINTN)DifferentBytes - InsertPosition2 + Index, 0);

            InsertPosition1 -= Index;
            InsertPosition2 -= Index;
          }
        }

        SHELL_FREE_NON_NULL (DataFromFile1);
        SHELL_FREE_NON_NULL (DataFromFile2);

        if (DiffPointNumber == 0) {
          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_COMP_FOOTER_PASS), gShellDebug1HiiHandle);
        } else {
          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_COMP_FOOTER_FAIL), gShellDebug1HiiHandle);
        }
      }
    }

    ShellCommandLineFreeVarList (Package);
  }
  SHELL_FREE_NON_NULL(FileName1);
  SHELL_FREE_NON_NULL(FileName2);

  if (FileHandle1 != NULL) {
    gEfiShellProtocol->CloseFile(FileHandle1);
  }
  if (FileHandle2 != NULL) {
    gEfiShellProtocol->CloseFile(FileHandle2);
  }

  return (ShellStatus);
}