1 /******************************************************************************
3 * Module Name: abcompare - compare AML files
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
47 ACPI_TABLE_HEADER Header1
;
48 ACPI_TABLE_HEADER Header2
;
50 #define BUFFER_SIZE 256
51 char Buffer
[BUFFER_SIZE
];
54 /* Local prototypes */
58 ACPI_TABLE_HEADER
*Header
);
72 ACPI_TABLE_HEADER
*Header
);
76 ACPI_TABLE_HEADER
*Header
,
77 ACPI_TABLE_HEADER
*Header2
);
80 /******************************************************************************
82 * FUNCTION: AbValidateHeader
84 * DESCRIPTION: Check for valid ACPI table header
86 ******************************************************************************/
90 ACPI_TABLE_HEADER
*Header
)
93 if (!AcpiUtValidNameseg (Header
->Signature
))
95 printf ("Header signature is invalid\n");
103 /*******************************************************************************
105 * FUNCTION: AcpiTbSumTable
107 * PARAMETERS: Buffer - Buffer to checksum
108 * Length - Size of the buffer
110 * RETURNS 8 bit checksum of buffer
112 * DESCRIPTION: Computes an 8 bit checksum of the buffer(length) and returns it.
114 ******************************************************************************/
126 if (Buffer
&& Length
)
128 /* Buffer and Length are valid */
130 Limit
= (UINT8
*) Buffer
+ Length
;
132 for (Rover
= Buffer
; Rover
< Limit
; Rover
++)
134 Sum
= (UINT8
) (Sum
+ *Rover
);
142 /*******************************************************************************
144 * FUNCTION: AbPrintHeaderInfo
146 * PARAMETERS: Header - An ACPI table header
150 * DESCRIPTION: Format and display header contents.
152 ******************************************************************************/
156 ACPI_TABLE_HEADER
*Header
)
159 /* Display header information */
161 printf ("Signature : %4.4s\n", Header
->Signature
);
162 printf ("Length : %8.8X\n", Header
->Length
);
163 printf ("Revision : %2.2X\n", Header
->Revision
);
164 printf ("Checksum : %2.2X\n", Header
->Checksum
);
165 printf ("OEM ID : %.6s\n", Header
->OemId
);
166 printf ("OEM Table ID : %.8s\n", Header
->OemTableId
);
167 printf ("OEM Revision : %8.8X\n", Header
->OemRevision
);
168 printf ("ASL Compiler ID : %.4s\n", Header
->AslCompilerId
);
169 printf ("Compiler Revision : %8.8X\n", Header
->AslCompilerRevision
);
175 ACPI_TABLE_HEADER
*Header
,
176 ACPI_TABLE_HEADER
*Header2
)
179 /* Display header information for both headers */
181 printf ("Signature %8.4s : %4.4s\n", Header
->Signature
, Header2
->Signature
);
182 printf ("Length %8.8X : %8.8X\n", Header
->Length
, Header2
->Length
);
183 printf ("Revision %8.2X : %2.2X\n", Header
->Revision
, Header2
->Revision
);
184 printf ("Checksum %8.2X : %2.2X\n", Header
->Checksum
, Header2
->Checksum
);
185 printf ("OEM ID %8.6s : %.6s\n", Header
->OemId
, Header2
->OemId
);
186 printf ("OEM Table ID %8.8s : %.8s\n", Header
->OemTableId
, Header2
->OemTableId
);
187 printf ("OEM Revision %8.8X : %8.8X\n", Header
->OemRevision
, Header2
->OemRevision
);
188 printf ("ASL Compiler ID %8.4s : %.4s\n", Header
->AslCompilerId
, Header2
->AslCompilerId
);
189 printf ("Compiler Revision %8.8X : %8.8X\n", Header
->AslCompilerRevision
, Header2
->AslCompilerRevision
);
194 /******************************************************************************
196 * FUNCTION: AbDisplayHeader
198 * DESCRIPTION: Display an ACPI table header
200 ******************************************************************************/
210 File
= fopen (FilePath
, "rb");
213 printf ("Could not open file %s\n", FilePath
);
217 Actual
= fread (&Header1
, 1, sizeof (ACPI_TABLE_HEADER
), File
);
220 if (Actual
!= sizeof (ACPI_TABLE_HEADER
))
222 printf ("File %s does not contain a valid ACPI table header\n", FilePath
);
226 if (!AbValidateHeader (&Header1
))
231 AbPrintHeaderInfo (&Header1
);
235 /******************************************************************************
237 * FUNCTION: AbComputeChecksum
239 * DESCRIPTION: Compute proper checksum for an ACPI table
241 ******************************************************************************/
248 ACPI_TABLE_HEADER
*Table
;
253 File
= fopen (FilePath
, "rb");
256 printf ("Could not open file %s\n", FilePath
);
260 Actual
= fread (&Header1
, 1, sizeof (ACPI_TABLE_HEADER
), File
);
261 if (Actual
< sizeof (ACPI_TABLE_HEADER
))
263 printf ("File %s does not contain a valid ACPI table header\n", FilePath
);
267 if (!AbValidateHeader (&Header1
))
274 AbPrintHeaderInfo (&Header1
);
277 /* Allocate a buffer to hold the entire table */
279 Table
= AcpiOsAllocate (Header1
.Length
);
282 printf ("Could not allocate buffer for table\n");
286 /* Read the entire table, including header */
288 fseek (File
, 0, SEEK_SET
);
289 Actual
= fread (Table
, 1, Header1
.Length
, File
);
290 if (Actual
!= Header1
.Length
)
292 printf ("Could not read table, length %u\n", Header1
.Length
);
296 /* Compute the checksum for the table */
300 Checksum
= (UINT8
) (0 - AcpiTbSumTable (Table
, Table
->Length
));
301 printf ("Computed checksum: 0x%X\n\n", Checksum
);
303 if (Header1
.Checksum
== Checksum
)
305 printf ("Checksum OK in AML file, not updating\n");
309 /* Open the target file for writing, to update checksum */
312 File
= fopen (FilePath
, "r+b");
315 printf ("Could not open file %s for writing\n", FilePath
);
319 /* Set the checksum, write the new header */
321 Header1
.Checksum
= Checksum
;
323 Actual
= fwrite (&Header1
, 1, sizeof (ACPI_TABLE_HEADER
), File
);
324 if (Actual
!= sizeof (ACPI_TABLE_HEADER
))
326 printf ("Could not write updated table header\n");
330 printf ("Wrote new checksum\n");
344 /******************************************************************************
346 * FUNCTION: AbCompareAmlFiles
348 * DESCRIPTION: Compare two AML files
350 ******************************************************************************/
362 UINT8 Mismatches
= 0;
363 BOOLEAN HeaderMismatch
= FALSE
;
369 File1
= fopen (File1Path
, "rb");
372 printf ("Could not open file %s\n", File1Path
);
376 File2
= fopen (File2Path
, "rb");
379 printf ("Could not open file %s\n", File2Path
);
383 /* Read the ACPI header from each file */
385 Actual1
= fread (&Header1
, 1, sizeof (ACPI_TABLE_HEADER
), File1
);
386 if (Actual1
!= sizeof (ACPI_TABLE_HEADER
))
388 printf ("File %s does not contain an ACPI table header\n", File1Path
);
392 Actual2
= fread (&Header2
, 1, sizeof (ACPI_TABLE_HEADER
), File2
);
393 if (Actual2
!= sizeof (ACPI_TABLE_HEADER
))
395 printf ("File %s does not contain an ACPI table header\n", File2Path
);
399 if ((!AbValidateHeader (&Header1
)) ||
400 (!AbValidateHeader (&Header2
)))
405 /* Table signatures must match */
407 if (*((UINT32
*) Header1
.Signature
) != *((UINT32
*) Header2
.Signature
))
409 printf ("Table signatures do not match\n");
415 /* Display header information */
417 AbPrintHeadersInfo (&Header1
, &Header2
);
420 if (memcmp (&Header1
, &Header2
, sizeof (ACPI_TABLE_HEADER
)))
422 printf ("Headers do not match exactly\n");
423 HeaderMismatch
= TRUE
;
426 /* Do the byte-by-byte compare */
428 Actual1
= fread (&Char1
, 1, 1, File1
);
429 Actual2
= fread (&Char2
, 1, 1, File2
);
430 Offset
= sizeof (ACPI_TABLE_HEADER
);
432 while ((Actual1
== 1) && (Actual2
== 1))
436 printf ("Error - Byte mismatch at offset %8.4X: 0x%2.2X 0x%2.2X\n",
437 Offset
, Char1
, Char2
);
439 if (Mismatches
> 100)
441 printf ("100 Mismatches: Too many mismatches\n");
447 Actual1
= fread (&Char1
, 1, 1, File1
);
448 Actual2
= fread (&Char2
, 1, 1, File2
);
453 printf ("Error - file %s is longer than file %s\n", File1Path
, File2Path
);
458 printf ("Error - file %s is shorter than file %s\n", File1Path
, File2Path
);
461 else if (!Mismatches
)
465 printf ("Files compare exactly after header\n");
469 printf ("Files compare exactly\n");
473 printf ("%u Mismatches found\n", Mismatches
);
488 /******************************************************************************
490 * FUNCTION: AbGetFile
492 * DESCRIPTION: Open a file and read it entirely into a new buffer
494 ******************************************************************************/
507 /* Binary mode does not alter CR/LF pairs */
509 File
= fopen (Filename
, "rb");
512 printf ("Could not open file %s\n", Filename
);
516 /* Need file size to allocate a buffer */
518 Size
= CmGetFileSize (File
);
519 if (Size
== ACPI_UINT32_MAX
)
521 printf ("Could not get file size (seek) for %s\n", Filename
);
525 /* Allocate a buffer for the entire file */
527 Buffer
= calloc (Size
, 1);
530 printf ("Could not allocate buffer of size %u\n", Size
);
534 /* Read the entire file */
536 Actual
= fread (Buffer
, 1, Size
, File
);
539 printf ("Could not read the input file %s\n", Filename
);
553 /******************************************************************************
555 * FUNCTION: AbDumpAmlFile
557 * DESCRIPTION: Dump a binary AML file to a text file
559 ******************************************************************************/
572 /* Get the entire AML file, validate header */
574 FileBuffer
= AbGetFile (File1Path
, &FileSize
);
580 printf ("Input file: %s contains %u (0x%X) bytes\n",
581 File1Path
, FileSize
, FileSize
);
583 FileOutHandle
= fopen (File2Path
, "wb");
586 printf ("Could not open file %s\n", File2Path
);
590 if (!AbValidateHeader ((ACPI_TABLE_HEADER
*) FileBuffer
))
595 /* Convert binary AML to text, using common dump buffer routine */
597 AcpiGbl_DebugFile
= FileOutHandle
;
598 AcpiGbl_DbOutputFlags
= ACPI_DB_REDIRECTABLE_OUTPUT
;
600 AcpiOsPrintf ("%4.4s @ 0x%8.8X\n",
601 ((ACPI_TABLE_HEADER
*) FileBuffer
)->Signature
, 0);
603 AcpiUtDumpBuffer ((UINT8
*) FileBuffer
, FileSize
, DB_BYTE_DISPLAY
, 0);
605 /* Summary for the output file */
607 FileSize
= CmGetFileSize (FileOutHandle
);
608 printf ("Output file: %s contains %u (0x%X) bytes\n\n",
609 File2Path
, FileSize
, FileSize
);
614 fclose (FileOutHandle
);