int AxExtractTables ( char *InputPathname, char *Signature, unsigned int MinimumInstances) { FILE *InputFile; FILE *OutputFile = NULL; size_t BytesWritten; size_t TotalBytesWritten = 0; size_t BytesConverted; unsigned int State = AX_STATE_FIND_HEADER; unsigned int FoundTable = 0; unsigned int Instances = 0; unsigned int ThisInstance; char ThisSignature[4]; int Status = 0; /* Open input in text mode, output is in binary mode */ InputFile = fopen (InputPathname, "rt"); if (!InputFile) { printf ("Could not open %s\n", InputPathname); return (-1); } if (Signature) { /* Are there enough instances of the table to continue? */ AxNormalizeSignature (Signature); Instances = AxCountTableInstances (InputPathname, Signature); if (Instances < MinimumInstances) { printf ("Table %s was not found in %s\n", Signature, InputPathname); Status = -1; goto CleanupAndExit; } if (Instances == 0) { goto CleanupAndExit; } } /* Convert all instances of the table to binary */ while (fgets (LineBuffer, AX_LINE_BUFFER_SIZE, InputFile)) { switch (State) { case AX_STATE_FIND_HEADER: /* Ignore lines that are too short to be header lines */ if (strlen (LineBuffer) < AX_MIN_TABLE_NAME_LENGTH) { continue; } /* Ignore empty lines and lines that start with a space */ if ((LineBuffer[0] == ' ') || (LineBuffer[0] == '\n')) { continue; } /* * Ignore lines that are not of the form <sig> @ <addr>. * Examples of lines that must be supported: * * DSDT @ 0x737e4000 * XSDT @ 0x737f2fff * RSD PTR @ 0xf6cd0 * SSDT @ (nil) */ if (!strstr (LineBuffer, " @ ")) { continue; } AxNormalizeSignature (LineBuffer); strncpy (ThisSignature, LineBuffer, 4); if (Signature) { /* Ignore signatures that don't match */ if (strncmp (ThisSignature, Signature, 4)) { continue; } } /* * Get the instance number for this signature. Only the * SSDT and PSDT tables can have multiple instances. */ ThisInstance = AxGetNextInstance (InputPathname, ThisSignature); /* Build an output filename and create/open the output file */ if (ThisInstance > 0) { sprintf (Filename, "%4.4s%u.dat", ThisSignature, ThisInstance); } else { sprintf (Filename, "%4.4s.dat", ThisSignature); } AxStrlwr (Filename); OutputFile = fopen (Filename, "w+b"); if (!OutputFile) { printf ("Could not open %s\n", Filename); Status = -1; goto CleanupAndExit; } State = AX_STATE_EXTRACT_DATA; TotalBytesWritten = 0; FoundTable = 1; continue; case AX_STATE_EXTRACT_DATA: /* Empty line or non-data line terminates the data */ if ((LineBuffer[0] == '\n') || (LineBuffer[0] != ' ')) { fclose (OutputFile); OutputFile = NULL; State = AX_STATE_FIND_HEADER; printf ("Acpi table [%4.4s] - %u bytes written to %s\n", ThisSignature, (unsigned int) TotalBytesWritten, Filename); continue; } /* Convert the ascii data (one line of text) to binary */ BytesConverted = AxConvertLine (LineBuffer, Data); /* Write the binary data */ BytesWritten = fwrite (Data, 1, BytesConverted, OutputFile); if (BytesWritten != BytesConverted) { printf ("Write error on %s\n", Filename); fclose (OutputFile); OutputFile = NULL; Status = -1; goto CleanupAndExit; } TotalBytesWritten += BytesConverted; continue; default: Status = -1; goto CleanupAndExit; } } if (!FoundTable) { printf ("Table %s was not found in %s\n", Signature, InputPathname); } CleanupAndExit: if (OutputFile) { fclose (OutputFile); if (State == AX_STATE_EXTRACT_DATA) { /* Received an EOF while extracting data */ printf ("Acpi table [%4.4s] - %u bytes written to %s\n", ThisSignature, (unsigned int) TotalBytesWritten, Filename); } } fclose (InputFile); return (Status); }
int AxExtractTables ( char *InputPathname, char *Signature, unsigned int MinimumInstances) { FILE *InputFile; FILE *OutputFile = NULL; unsigned int BytesConverted; unsigned int ThisTableBytesWritten = 0; unsigned int FoundTable = 0; unsigned int Instances = 0; unsigned int ThisInstance; char ThisSignature[5]; char UpperSignature[5]; int Status = 0; unsigned int State = AX_STATE_FIND_HEADER; /* Open input in text mode, output is in binary mode */ InputFile = fopen (InputPathname, "rt"); if (!InputFile) { printf ("Could not open input file %s\n", InputPathname); return (-1); } if (!AxIsFileAscii (InputFile)) { fclose (InputFile); return (-1); } if (Signature) { strncpy (UpperSignature, Signature, 4); UpperSignature[4] = 0; AcpiUtStrupr (UpperSignature); /* Are there enough instances of the table to continue? */ AxNormalizeSignature (UpperSignature); Instances = AxCountTableInstances (InputPathname, UpperSignature); if (Instances < MinimumInstances) { printf ("Table [%s] was not found in %s\n", UpperSignature, InputPathname); fclose (InputFile); return (-1); } if (Instances == 0) { fclose (InputFile); return (-1); } } /* Convert all instances of the table to binary */ while (fgets (Gbl_LineBuffer, AX_LINE_BUFFER_SIZE, InputFile)) { switch (State) { case AX_STATE_FIND_HEADER: if (!AxIsDataBlockHeader ()) { continue; } ACPI_MOVE_NAME (ThisSignature, Gbl_LineBuffer); if (Signature) { /* Ignore signatures that don't match */ if (!ACPI_COMPARE_NAME (ThisSignature, UpperSignature)) { continue; } } /* * Get the instance number for this signature. Only the * SSDT and PSDT tables can have multiple instances. */ ThisInstance = AxGetNextInstance (InputPathname, ThisSignature); /* Build an output filename and create/open the output file */ if (ThisInstance > 0) { /* Add instance number to the output filename */ sprintf (Gbl_OutputFilename, "%4.4s%u.dat", ThisSignature, ThisInstance); } else { sprintf (Gbl_OutputFilename, "%4.4s.dat", ThisSignature); } AcpiUtStrlwr (Gbl_OutputFilename); OutputFile = fopen (Gbl_OutputFilename, "w+b"); if (!OutputFile) { printf ("Could not open output file %s\n", Gbl_OutputFilename); fclose (InputFile); return (-1); } /* * Toss this block header of the form "<sig> @ <addr>" line * and move on to the actual data block */ Gbl_TableCount++; FoundTable = 1; ThisTableBytesWritten = 0; State = AX_STATE_EXTRACT_DATA; continue; case AX_STATE_EXTRACT_DATA: /* Empty line or non-data line terminates the data block */ BytesConverted = AxProcessOneTextLine ( OutputFile, ThisSignature, ThisTableBytesWritten); switch (BytesConverted) { case 0: State = AX_STATE_FIND_HEADER; /* No more data block lines */ continue; case -1: goto CleanupAndExit; /* There was a write error */ default: /* Normal case, get next line */ ThisTableBytesWritten += BytesConverted; continue; } default: Status = -1; goto CleanupAndExit; } } if (!FoundTable) { printf ("No ACPI tables were found in %s\n", InputPathname); } CleanupAndExit: if (State == AX_STATE_EXTRACT_DATA) { /* Received an input file EOF while extracting data */ printf (AX_TABLE_INFO_FORMAT, ThisSignature, ThisTableBytesWritten, Gbl_OutputFilename); } if (Gbl_TableCount > 1) { printf ("\n%u binary ACPI tables extracted\n", Gbl_TableCount); } if (OutputFile) { fclose (OutputFile); } fclose (InputFile); return (Status); }