1 /******************************************************************************
3 * Module Name: abcompare - compare AML files
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, 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.
49 ACPI_TABLE_HEADER Header1;
50 ACPI_TABLE_HEADER Header2;
52 #define BUFFER_SIZE 256
53 char Buffer[BUFFER_SIZE];
56 /* Local prototypes */
60 ACPI_TABLE_HEADER *Header);
78 ACPI_TABLE_HEADER *Header);
82 ACPI_TABLE_HEADER *Header,
83 ACPI_TABLE_HEADER *Header2);
86 AeLocalGetRootPointer (
90 /*******************************************************************************
92 * FUNCTION: UtHexCharToValue
94 * PARAMETERS: HexChar - Hex character in Ascii
96 * RETURN: The binary value of the hex character
98 * DESCRIPTION: Perform ascii-to-hex translation
100 ******************************************************************************/
108 if (HexChar >= 0x30 && HexChar <= 0x39)
110 *OutBinary = (UINT8) (HexChar - 0x30);
114 else if (HexChar >= 0x41 && HexChar <= 0x46)
116 *OutBinary = (UINT8) (HexChar - 0x37);
120 else if (HexChar >= 0x61 && HexChar <= 0x66)
122 *OutBinary = (UINT8) (HexChar - 0x57);
137 if (!UtHexCharToValue (HexString[0], &Local1))
141 if (!UtHexCharToValue (HexString[1], &Local2))
146 *OutBinary = (UINT8) ((Local1 << 4) | Local2);
152 /******************************************************************************
154 * FUNCTION: AbValidateHeader
156 * DESCRIPTION: Check for valid ACPI table header
158 ******************************************************************************/
162 ACPI_TABLE_HEADER *Header)
165 if (!AcpiUtValidAcpiName (Header->Signature))
167 printf ("Header signature is invalid\n");
175 /*******************************************************************************
177 * FUNCTION: AcpiTbSumTable
179 * PARAMETERS: Buffer - Buffer to checksum
180 * Length - Size of the buffer
182 * RETURNS 8 bit checksum of buffer
184 * DESCRIPTION: Computes an 8 bit checksum of the buffer(length) and returns it.
186 ******************************************************************************/
198 if (Buffer && Length)
200 /* Buffer and Length are valid */
202 limit = (UINT8 *) Buffer + Length;
204 for (rover = Buffer; rover < limit; rover++)
206 sum = (UINT8) (sum + *rover);
213 /*******************************************************************************
215 * FUNCTION: AbPrintHeaderInfo
217 * PARAMETERS: Header - An ACPI table header
221 * DESCRIPTION: Format and display header contents.
223 ******************************************************************************/
227 ACPI_TABLE_HEADER *Header)
230 /* Display header information */
232 printf ("Signature : %4.4s\n", Header->Signature);
233 printf ("Length : %8.8X\n", Header->Length);
234 printf ("Revision : %2.2X\n", Header->Revision);
235 printf ("Checksum : %2.2X\n", Header->Checksum);
236 printf ("OEM ID : %6.6s\n", Header->OemId);
237 printf ("OEM Table ID : %8.8s\n", Header->OemTableId);
238 printf ("OEM Revision : %8.8X\n", Header->OemRevision);
239 printf ("ASL Compiler ID : %4.4s\n", Header->AslCompilerId);
240 printf ("Compiler Revision : %8.8X\n", Header->AslCompilerRevision);
246 ACPI_TABLE_HEADER *Header,
247 ACPI_TABLE_HEADER *Header2)
250 /* Display header information for both headers */
252 printf ("Signature %8.4s : %4.4s\n", Header->Signature, Header2->Signature);
253 printf ("Length %8.8X : %8.8X\n", Header->Length, Header2->Length);
254 printf ("Revision %8.2X : %2.2X\n", Header->Revision, Header2->Revision);
255 printf ("Checksum %8.2X : %2.2X\n", Header->Checksum, Header2->Checksum);
256 printf ("OEM ID %8.6s : %6.6s\n", Header->OemId, Header2->OemId);
257 printf ("OEM Table ID %8.8s : %8.8s\n", Header->OemTableId, Header2->OemTableId);
258 printf ("OEM Revision %8.8X : %8.8X\n", Header->OemRevision, Header2->OemRevision);
259 printf ("ASL Compiler ID %8.4s : %4.4s\n", Header->AslCompilerId, Header2->AslCompilerId);
260 printf ("Compiler Revision %8.8X : %8.8X\n", Header->AslCompilerRevision, Header2->AslCompilerRevision);
265 /******************************************************************************
267 * FUNCTION: AbDisplayHeader
269 * DESCRIPTION: Display an ACPI table header
271 ******************************************************************************/
280 File1 = fopen (File1Path, "rb");
283 printf ("Could not open file %s\n", File1Path);
287 Actual = fread (&Header1, 1, sizeof (ACPI_TABLE_HEADER), File1);
288 if (Actual != sizeof (ACPI_TABLE_HEADER))
290 printf ("File %s does not contain an ACPI table header\n", File1Path);
294 if (!AbValidateHeader (&Header1))
299 AbPrintHeaderInfo (&Header1);
303 /******************************************************************************
305 * FUNCTION: AbComputeChecksum
307 * DESCRIPTION: Compute proper checksum for an ACPI table
309 ******************************************************************************/
316 ACPI_TABLE_HEADER *Table;
320 File1 = fopen (File1Path, "rb");
323 printf ("Could not open file %s\n", File1Path);
327 Actual = fread (&Header1, 1, sizeof (ACPI_TABLE_HEADER), File1);
328 if (Actual < sizeof (ACPI_TABLE_HEADER))
330 printf ("File %s does not contain an ACPI table header\n", File1Path);
334 if (!AbValidateHeader (&Header1))
341 AbPrintHeaderInfo (&Header1);
344 /* Allocate a buffer to hold the entire table */
346 Table = AcpiOsAllocate (Header1.Length);
349 printf ("could not allocate\n");
353 /* Read the entire table, including header */
355 fseek (File1, 0, SEEK_SET);
356 Actual = fread (Table, 1, Header1.Length, File1);
357 if (Actual != Header1.Length)
359 printf ("could not read table, length %u\n", Header1.Length);
363 /* Compute the checksum for the table */
367 Checksum = (UINT8) (0 - AcpiTbSumTable (Table, Table->Length));
368 printf ("Computed checksum: 0x%X\n\n", Checksum);
370 if (Header1.Checksum == Checksum)
372 printf ("Checksum ok in AML file, not updating\n");
376 /* Open the target file for writing, to update checksum */
379 File1 = fopen (File1Path, "r+b");
382 printf ("Could not open file %s for writing\n", File1Path);
386 /* Set the checksum, write the new header */
388 Header1.Checksum = Checksum;
390 Actual = fwrite (&Header1, 1, sizeof (ACPI_TABLE_HEADER), File1);
391 if (Actual != sizeof (ACPI_TABLE_HEADER))
393 printf ("Could not write updated table header\n");
397 printf ("Wrote new checksum\n");
402 /******************************************************************************
404 * FUNCTION: AbCompareAmlFiles
406 * DESCRIPTION: Compare two AML files
408 ******************************************************************************/
420 UINT8 Mismatches = 0;
421 BOOLEAN HeaderMismatch = FALSE;
424 File1 = fopen (File1Path, "rb");
427 printf ("Could not open file %s\n", File1Path);
431 File2 = fopen (File2Path, "rb");
434 printf ("Could not open file %s\n", File2Path);
438 /* Read the ACPI header from each file */
440 Actual1 = fread (&Header1, 1, sizeof (ACPI_TABLE_HEADER), File1);
441 if (Actual1 != sizeof (ACPI_TABLE_HEADER))
443 printf ("File %s does not contain an ACPI table header\n", File1Path);
447 Actual2 = fread (&Header2, 1, sizeof (ACPI_TABLE_HEADER), File2);
448 if (Actual2 != sizeof (ACPI_TABLE_HEADER))
450 printf ("File %s does not contain an ACPI table header\n", File2Path);
454 if ((!AbValidateHeader (&Header1)) ||
455 (!AbValidateHeader (&Header2)))
460 /* Table signatures must match */
462 if (*((UINT32 *) Header1.Signature) != *((UINT32 *) Header2.Signature))
464 printf ("Table signatures do not match\n");
470 /* Display header information */
472 AbPrintHeadersInfo (&Header1, &Header2);
475 if (memcmp (&Header1, &Header2, sizeof (ACPI_TABLE_HEADER)))
477 printf ("Headers do not match exactly\n");
478 HeaderMismatch = TRUE;
481 /* Do the byte-by-byte compare */
483 Actual1 = fread (&Char1, 1, 1, File1);
484 Actual2 = fread (&Char2, 1, 1, File2);
485 Offset = sizeof (ACPI_TABLE_HEADER);
487 while ((Actual1 == 1) && (Actual2 == 1))
491 printf ("Error - Byte mismatch at offset %8.8X: 0x%2.2X 0x%2.2X\n",
492 Offset, Char1, Char2);
494 if (Mismatches > 100)
496 printf ("100 Mismatches: Too many mismatches\n");
502 Actual1 = fread (&Char1, 1, 1, File1);
503 Actual2 = fread (&Char2, 1, 1, File2);
508 printf ("Error - file %s is longer than file %s\n", File1Path, File2Path);
513 printf ("Error - file %s is shorter than file %s\n", File1Path, File2Path);
516 else if (!Mismatches)
520 printf ("Files compare exactly after header\n");
524 printf ("Files compare exactly\n");
528 printf ("%u Mismatches found\n", Mismatches);
533 /******************************************************************************
535 * FUNCTION: AbGetFileSize
537 * DESCRIPTION: Get the size of an open file
539 ******************************************************************************/
549 Offset = ftell (File);
551 if (fseek (File, 0, SEEK_END))
556 FileSize = (UINT32) ftell (File);
558 /* Restore file pointer */
560 if (fseek (File, Offset, SEEK_SET))
569 /******************************************************************************
571 * FUNCTION: AbGetFile
573 * DESCRIPTION: Open a file and read it entirely into a new buffer
575 ******************************************************************************/
588 /* Binary mode does not alter CR/LF pairs */
590 File = fopen (Filename, "rb");
593 printf ("Could not open file %s\n", Filename);
597 /* Need file size to allocate a buffer */
599 Size = AbGetFileSize (File);
602 printf ("Could not get file size (seek) for %s\n", Filename);
606 /* Allocate a buffer for the entire file */
608 Buffer = calloc (Size, 1);
611 printf ("Could not allocate buffer of size %u\n", Size);
615 /* Read the entire file */
617 Actual = fread (Buffer, 1, Size, File);
620 printf ("Could not read the input file %s\n", Filename);
634 /******************************************************************************
636 * FUNCTION: AbDumpAmlFile
638 * DESCRIPTION: Dump a binary AML file to a text file
640 ******************************************************************************/
652 /* Get the entire AML file, validate header */
654 FileBuffer = AbGetFile (File1Path, &FileSize);
660 printf ("Input file: %s contains %u (0x%X) bytes\n",
661 File1Path, FileSize, FileSize);
663 FileOutHandle = fopen (File2Path, "wb");
666 printf ("Could not open file %s\n", File2Path);
670 if (!AbValidateHeader ((ACPI_TABLE_HEADER *) FileBuffer))
675 /* Convert binary AML to text, using common dump buffer routine */
677 AcpiGbl_DebugFile = FileOutHandle;
678 AcpiGbl_DbOutputFlags = ACPI_DB_REDIRECTABLE_OUTPUT;
680 AcpiOsPrintf ("%4.4s @ 0x%8.8X\n",
681 ((ACPI_TABLE_HEADER *) FileBuffer)->Signature, 0);
683 AcpiUtDumpBuffer ((UINT8 *) FileBuffer, FileSize, DB_BYTE_DISPLAY, 0);
685 /* Summary for the output file */
687 FileSize = AbGetFileSize (FileOutHandle);
688 printf ("Output file: %s contains %u (0x%X) bytes\n\n",
689 File2Path, FileSize, FileSize);
695 /******************************************************************************
697 * FUNCTION: AbExtractAmlFile
699 * DESCRIPTION: Extract a binary AML file from a text file (as produced by the
700 * DumpAmlFile procedure or the "acpidump" table utility.
702 ******************************************************************************/
719 /* Open in/out files. input is in text mode, output is in binary mode */
721 FileHandle = fopen (File1Path, "rt");
724 printf ("Could not open file %s\n", File1Path);
728 FileOutHandle = fopen (File2Path, "w+b");
731 printf ("Could not open file %s\n", File2Path);
735 /* Force input table sig to uppercase */
737 AcpiUtStrupr (TableSig);
740 /* TBD: examine input for ASCII */
743 /* We have an ascii file, grab one line at a time */
745 while (fgets (Buffer, BUFFER_SIZE, FileHandle))
747 /* The 4-char ACPI signature appears at the beginning of a line */
749 if (ACPI_COMPARE_NAME (Buffer, TableSig))
751 printf ("Found table [%4.4s]\n", TableSig);
754 * Eat all lines in the table, of the form:
755 * <offset>: <16 bytes of hex data, separated by spaces> <ASCII representation> <newline>
759 * 02C0: 5F 53 42 5F 4C 4E 4B 44 00 12 13 04 0C FF FF 08 _SB_LNKD........
762 while (fgets (Buffer, BUFFER_SIZE, FileHandle))
764 /* Get past the offset, terminated by a colon */
766 Table = strchr (Buffer, ':');
769 /* No colon, all done */
773 Table += 2; /* Eat the colon + space */
775 for (i = 0; i < 16; i++)
777 Scanned = AbHexByteToBinary (Table, &Value);
783 Table += 3; /* Go past this hex byte and space */
785 /* Write the converted (binary) byte */
787 if (fwrite (&Value, 1, 1, FileOutHandle) != 1)
789 printf ("Error writing byte %u to output file: %s\n",
797 /* No more lines, EOF, all done */
803 /* Searched entire file, no match to table signature */
805 printf ("Could not match table signature\n");
810 printf ("%u (0x%X) bytes written to %s\n", Count, Count, File2Path);
812 fclose (FileOutHandle);
817 /******************************************************************************
821 * DESCRIPTION: For linkage
823 ******************************************************************************/
825 ACPI_PHYSICAL_ADDRESS
826 AeLocalGetRootPointer (
841 ACPI_EXECUTE_TYPE Type,
842 ACPI_OSD_EXEC_CALLBACK Function,