From dfe554f6730d77e1afec8b84c82efa354a9acd18 Mon Sep 17 00:00:00 2001 From: aystarik Date: Wed, 29 Jun 2005 20:36:19 +0000 Subject: [PATCH] Added extract command. Cleanup date 2005.04.07.22.12.00; author rmoore1; state Exp; --- source/tools/acpibin/abcompare.c | 276 ++++++++++++++++++++++++++----- source/tools/acpibin/abmain.c | 111 +++++++++---- source/tools/acpibin/acpibin.h | 29 ++-- 3 files changed, 326 insertions(+), 90 deletions(-) diff --git a/source/tools/acpibin/abcompare.c b/source/tools/acpibin/abcompare.c index 90328e6d7..3225ef2c6 100644 --- a/source/tools/acpibin/abcompare.c +++ b/source/tools/acpibin/abcompare.c @@ -2,7 +2,7 @@ /****************************************************************************** * * Module Name: abcompare - compare AML files - * $Revision: 1.13 $ + * $Revision: 1.15 $ * *****************************************************************************/ @@ -123,6 +123,17 @@ FILE *File1; FILE *File2; ACPI_TABLE_HEADER Header1; ACPI_TABLE_HEADER Header2; +struct stat Gbl_StatBuf; + +#define BUFFER_SIZE 256 +char Buffer[BUFFER_SIZE]; + + +#define DB_CONSOLE_OUTPUT 0x02 +#define ACPI_DB_REDIRECTABLE_OUTPUT 0x01 + +extern FILE *AcpiGbl_DebugFile = NULL; +extern UINT8 AcpiGbl_DbOutputFlags = DB_CONSOLE_OUTPUT ; /****************************************************************************** @@ -173,7 +184,7 @@ AcpiTbChecksum ( if (Buffer && Length) { - /* Buffer and Length are valid */ + /* Buffer and Length are valid */ limit = (UINT8 *) Buffer + Length; @@ -186,6 +197,55 @@ AcpiTbChecksum ( } +/****************************************************************************** + * + * FUNCTION: AbDisplayHeader + * + * DESCRIPTION: Display an ACPI table header + * + ******************************************************************************/ + +void +AbDisplayHeader ( + char *File1Path) +{ + UINT32 Actual1; + + + File1 = fopen (File1Path, "rb"); + if (!File1) + { + printf ("Could not open file %s\n", File1Path); + return; + } + + Actual1 = fread (&Header1, 1, sizeof (ACPI_TABLE_HEADER), File1); + if (Actual1 < sizeof (ACPI_TABLE_HEADER)) + { + printf ("File %s does not contain an ACPI table header\n", File1Path); + return; + } + + if (!AbValidateHeader (&Header1)) + { + return; + } + + /* Display header information */ + + printf ("Signature : %8.4s\n", Header1.Signature); + printf ("Length : %8.8X\n", Header1.Length); + printf ("Revision : % 8.2X\n", Header1.Revision); + printf ("Checksum : % 8.2X\n", Header1.Checksum); + printf ("OEM ID : %8.6s\n", Header1.OemId); + printf ("OEM Table ID : %8.8s\n", Header1.OemTableId); + printf ("OEM Revision : %8.8X\n", Header1.OemRevision); + printf ("ASL Compiler ID : %8.4s\n", Header1.AslCompilerId); + printf ("Compiler Revision : %8.8X\n", Header1.AslCompilerRevision); + printf ("\n"); +} + + /****************************************************************************** * * FUNCTION: AbComputeChecksum @@ -228,8 +288,8 @@ AbComputeChecksum ( printf ("Signature : %8.4s\n", Header1.Signature); printf ("Length : %8.8X\n", Header1.Length); - printf ("Revision : % 8.2X\n", Header1.Revision); - printf ("Checksum : % 8.2X\n", Header1.Checksum); + printf ("Revision : % 8.2X\n", Header1.Revision); + printf ("Checksum : % 8.2X\n", Header1.Checksum); printf ("OEM ID : %8.6s\n", Header1.OemId); printf ("OEM Table ID : %8.8s\n", Header1.OemTableId); printf ("OEM Revision : %8.8X\n", Header1.OemRevision); @@ -238,7 +298,6 @@ AbComputeChecksum ( printf ("\n"); } - Table = AcpiOsAllocate (Header1.Length); if (!Table) { @@ -256,10 +315,34 @@ AbComputeChecksum ( Table->Checksum = 0; - Checksum = (UINT8) (0 - AcpiTbChecksum (Table, Table->Length)); - - + Checksum = (UINT8) (0 - AcpiTbChecksum (Table, Table->Length)); printf ("Computed checksum: 0x%X\n\n", Checksum); + + if (Header1.Checksum == Checksum) + { + printf ("Checksum ok in AML file, not updating\n"); + return; + } + + fclose (File1); + File1 = fopen (File1Path, "r+b"); + if (!File1) + { + printf ("Could not open file %s for writing\n", File1Path); + return; + } + + Header1.Checksum = Checksum; + + Actual1 = fwrite (&Header1, 1, sizeof (ACPI_TABLE_HEADER), File1); + if (Actual1 < sizeof (ACPI_TABLE_HEADER)) + { + printf ("Could not write updated table header\n"); + return; + } + + printf ("Wrote new checksum\n"); + return; } @@ -282,6 +365,7 @@ AbCompareAmlFiles ( UINT8 Char1; UINT8 Char2; UINT8 Mismatches = 0; + BOOLEAN HeaderMismatch = FALSE; File1 = fopen (File1Path, "rb"); @@ -291,7 +375,6 @@ AbCompareAmlFiles ( return -1; } - File2 = fopen (File2Path, "rb"); if (!File2) { @@ -299,7 +382,6 @@ AbCompareAmlFiles ( return -1; } - /* Read the ACPI header from each file */ Actual1 = fread (&Header1, 1, sizeof (ACPI_TABLE_HEADER), File1); @@ -330,7 +412,6 @@ AbCompareAmlFiles ( return -1; } - if (!Gbl_TerseMode) { /* Display header information */ @@ -347,6 +428,12 @@ AbCompareAmlFiles ( printf ("\n"); } + if (memcmp (Header1.Signature, Header2.Signature, sizeof (ACPI_TABLE_HEADER))) + { + printf ("Headers do not match exactly\n"); + HeaderMismatch = TRUE; + } + /* Do the byte-by-byte compare */ Actual1 = fread (&Char1, 1, 1, File1); @@ -357,22 +444,21 @@ AbCompareAmlFiles ( { if (Char1 != Char2) { - printf ("Error - Byte mismatch at offset %8.8X: 0x%2.2X 0x%2.2X\n", Offset, Char1, Char2); + printf ("Error - Byte mismatch at offset %8.8X: 0x%2.2X 0x%2.2X\n", + Offset, Char1, Char2); Mismatches++; if (Mismatches > 100) { printf ("100 Mismatches: Too many mismatches\n"); - return 0; + return -1; } } Offset++; Actual1 = fread (&Char1, 1, 1, File1); Actual2 = fread (&Char2, 1, 1, File2); - } - if (Actual1) { printf ("Error - file %s is longer than file %s\n", File1Path, File2Path); @@ -385,7 +471,14 @@ AbCompareAmlFiles ( } else if (!Mismatches) { - printf ("Files compare exactly after header\n"); + if (HeaderMismatch) + { + printf ("Files compare exactly after header\n"); + } + else + { + printf ("Files compare exactly\n"); + } } printf ("%d Mismatches found\n", Mismatches); @@ -393,9 +486,6 @@ AbCompareAmlFiles ( } -struct stat Gbl_StatBuf; - - /****************************************************************************** * * FUNCTION: AsGetFile @@ -432,14 +522,12 @@ AbGetFile ( /* * Create a buffer for the entire file - * Add 10% extra to accomodate string replacements */ - Size = Gbl_StatBuf.st_size; - Buffer = calloc (Size + (Size / 10), 1); + Buffer = calloc (Size, 1); if (!Buffer) { - printf ("Could not allocate buffer of size %d\n", Size + (Size / 10)); + printf ("Could not allocate buffer of size %d\n", Size); goto ErrorExit; } @@ -463,54 +551,158 @@ ErrorExit: } +/****************************************************************************** + * + * FUNCTION: AbDumpAmlFile + * + * DESCRIPTION: Dump a binary AML file to a text file + * + ******************************************************************************/ + int AbDumpAmlFile ( - char *File1Path) + char *File1Path, + char *File2Path) { char *FileBuffer; UINT32 FileSize; + FILE *FileOutHandle; FileBuffer = AbGetFile (File1Path, &FileSize); printf ("File %s contains 0x%X bytes\n\n", File1Path, FileSize); + FileOutHandle = fopen (File2Path, "wb"); + if (!FileOutHandle) + { + printf ("Could not open %s\n", File2Path); + return -1; + } if (!AbValidateHeader ((ACPI_TABLE_HEADER *) FileBuffer)) { return -1; } + AcpiGbl_DebugFile = FileOutHandle; + AcpiGbl_DbOutputFlags = ACPI_DB_REDIRECTABLE_OUTPUT; + AcpiOsPrintf ("%4.4s\n", ((ACPI_TABLE_HEADER *) FileBuffer)->Signature); AcpiDbgLevel = ACPI_UINT32_MAX; AcpiUtDumpBuffer (FileBuffer, FileSize, DB_BYTE_DISPLAY, ACPI_UINT32_MAX); return 0; } -#define DB_CONSOLE_OUTPUT 0x02 -FILE *AcpiGbl_DebugFile = NULL; -UINT8 AcpiGbl_DbOutputFlags = DB_CONSOLE_OUTPUT ; +/****************************************************************************** + * + * FUNCTION: AbExtractAmlFile + * + * DESCRIPTION: Extract a binary AML file from a text file (as produced by the + * DumpAmlFile procedure or the "acpidmp" table utility. + * + ******************************************************************************/ -void -AcpiUtFree ( - void *Address, - UINT32 Component, - char *Module, - UINT32 Line) +int +AbExtractAmlFile ( + char *TableSig, + char *File1Path, + char *File2Path) { - return; + char *Table; + long Value; + UINT32 i; + char *End; + FILE *FileHandle; + FILE *FileOutHandle; + + + /* Open in/out files. Binary mode leaves CR/LF pairs */ + + FileHandle = fopen (File1Path, "rb"); + if (!FileHandle) + { + printf ("Could not open %s\n", File1Path); + return -1; + } + + FileOutHandle = fopen (File2Path, "wb"); + if (!FileOutHandle) + { + printf ("Could not open %s\n", File2Path); + return -1; + } + + /* Force input table sig to uppercase */ + + strupr (TableSig); + + /* We have an ascii file, grab one line at a time */ + + while (fgets (Buffer, BUFFER_SIZE, FileHandle)) + { + /* 4-char signature appears at the beginning of a line */ + + if (!strncmp (Buffer, TableSig, 4)) + { + printf ("Found table [%4.4s]\n", TableSig); + + /* + * Eat all lines in the table, of the form: + * + * offset: <16 bytes of data, separated by spaces>, extra junk, newline + */ + while (fgets (Buffer, BUFFER_SIZE, FileHandle)) + { + /* Get past the offset: */ + + Table = strchr (Buffer, ':'); + if (!Table) + { + return 0; + } + + /* Convert up to 16 bytes per input line */ + + Table += 2; + for (i = 0; i < 16; i++) + { + Value = strtoul (Table, &End, 16); + if (End == Table) + { + /* No conversion, all done */ + + return 0; + } + + /* Write the converted byte */ + + Table += 3; + fwrite (&(char *) Value, 1, 1, FileOutHandle); + } + } + + /* No more lines, all done */ + + return (0); + } + } + + /* Searched entire file, no match to table signature */ + + printf ("Could not match table signature\n"); + return -1; } -void * -AcpiUtMemset ( - void *Dest, - UINT32 Value, - NATIVE_UINT Count) -{ - return (Dest); -} +/****************************************************************************** + * + * FUNCTION: Stubs + * + * DESCRIPTION: For linkage + * + ******************************************************************************/ ACPI_STATUS AeLocalGetRootPointer ( diff --git a/source/tools/acpibin/abmain.c b/source/tools/acpibin/abmain.c index fc74595f5..fb0aa909f 100644 --- a/source/tools/acpibin/abmain.c +++ b/source/tools/acpibin/abmain.c @@ -2,7 +2,7 @@ /****************************************************************************** * * Module Name: abmain - Main module for the acpi binary utility - * $Revision: 1.7 $ + * $Revision: 1.10 $ * *****************************************************************************/ @@ -118,6 +118,8 @@ #define _DECLARE_GLOBALS #include "acpibin.h" +#include "acapps.h" +#include "acconfig.h" /****************************************************************************** @@ -129,14 +131,25 @@ ******************************************************************************/ void -AbDisplayUsage (void) +AbDisplayUsage ( + UINT8 OptionCount) { - printf ("Usage: acpibin [-cd] []\n\n"); - printf ("Options: -c Compare AML files\n"); - printf (" -d Dump AML file\n"); - printf (" -s Compute checksum for ACPI table\n"); - printf (" -t Terse mode\n"); + if (OptionCount) + { + printf ("Option requires %d arguments\n\n", OptionCount); + } + + printf ("Usage: acpibin [options]\n\n"); + printf ("Options:\n\n"); + printf (" -c Compare two AML files\n"); + printf (" -d Dump AML binary to text file\n"); + printf (" -e Extract binary AML table from AcpiDmp file\n\n"); + + printf (" -h Display table header for binary AML file\n"); + printf (" -s Update checksum for binary AML file\n"); + printf (" -t Terse mode\n"); + printf ("\n"); return; } @@ -152,61 +165,93 @@ AbDisplayUsage (void) int ACPI_SYSTEM_XFACE main ( - NATIVE_UINT argc, + ACPI_NATIVE_UINT argc, char *argv[]) { int j; + int Status; - printf ("\nACPI Binary (AML) Utility"); - printf (" version [%s]\n\n", __DATE__); + AcpiOsInitialize (); + + printf ("\nACPI Binary Utility for AML files version %X [%s]\n\n", + ACPI_CA_VERSION, __DATE__); if (argc < 2) { - AbDisplayUsage (); + AbDisplayUsage (0); return 0; } /* Command line options */ - while ((j = getopt (argc, argv, "cdst")) != EOF) switch(j) + while ((j = AcpiGetopt (argc, argv, "c:d:e:h:s:t")) != EOF) switch(j) { - case 'c': - /* Compare Files */ + case 'c': /* Compare Files */ - Gbl_CompareMode = TRUE; + if (argc < 4) + { + AbDisplayUsage (2); + return -1; + } + + Status = AbCompareAmlFiles (AcpiGbl_Optarg, argv[AcpiGbl_Optind]); break; - case 'd': + case 'd': /* Dump AML file */ - Gbl_DumpMode = TRUE; + if (argc < 4) + { + AbDisplayUsage (2); + return -1; + } + + Status = AbDumpAmlFile (AcpiGbl_Optarg, argv[AcpiGbl_Optind]); break; - case 's': + case 'e': /* Extract AML text file */ - AbComputeChecksum (argv[optind]); + if (argc < 5) + { + AbDisplayUsage (3); + return -1; + } + + Status = AbExtractAmlFile (AcpiGbl_Optarg, argv[AcpiGbl_Optind], + argv[AcpiGbl_Optind+1]); + break; + + case 'h': /* Display ACPI table header */ + + if (argc < 3) + { + AbDisplayUsage (1); + return -1; + } + + AbDisplayHeader (AcpiGbl_Optarg); return (0); - case 't': + case 's': /* Compute/update checksum */ + + if (argc < 3) + { + AbDisplayUsage (1); + return -1; + } + + AbComputeChecksum (AcpiGbl_Optarg); + return (0); + + case 't': /* Enable terse mode */ Gbl_TerseMode = TRUE; break; default: - AbDisplayUsage (); + AbDisplayUsage (0); return -1; } - - if (Gbl_CompareMode) - { - AbCompareAmlFiles (argv[optind], argv[optind+1]); - } - - else if (Gbl_DumpMode) - { - AbDumpAmlFile (argv[optind]); - } - - return 0; + return Status; } diff --git a/source/tools/acpibin/acpibin.h b/source/tools/acpibin/acpibin.h index 6402f232f..32c80a85f 100644 --- a/source/tools/acpibin/acpibin.h +++ b/source/tools/acpibin/acpibin.h @@ -2,7 +2,7 @@ /****************************************************************************** * * Module Name: acpibinh - Include file for AcpiBin utility - * $Revision: 1.5 $ + * $Revision: 1.8 $ * *****************************************************************************/ @@ -131,9 +131,6 @@ /* Constants */ -/* Globals */ - - /* * Global variables. Defined in main.c only, externed in all other files */ @@ -147,32 +144,34 @@ #endif -extern int optind; -extern char *optarg; - /* Globals */ EXTERN BOOLEAN INIT_GLOBAL (Gbl_TerseMode, FALSE); -EXTERN BOOLEAN INIT_GLOBAL (Gbl_CompareMode, FALSE); -EXTERN BOOLEAN INIT_GLOBAL (Gbl_DumpMode, FALSE); /* Prototypes */ -int -getopt ( - int argc, - char **argv, - char *opts); - int AbCompareAmlFiles ( char *File1Path, char *File2Path); +int +AbExtractAmlFile ( + char *TableSig, + char *File1Path, + char *File2Path); int AbDumpAmlFile ( + char *File1Path, + char *File2Path); + +void +AbComputeChecksum ( char *File1Path); +void +AbDisplayHeader ( + char *File1Path);