dbfileio.c revision 151937
1/*******************************************************************************
2 *
3 * Module Name: dbfileio - Debugger file I/O commands.  These can't usually
4 *              be used when running the debugger in Ring 0 (Kernel mode)
5 *              $Revision: 1.84 $
6 *
7 ******************************************************************************/
8
9/******************************************************************************
10 *
11 * 1. Copyright Notice
12 *
13 * Some or all of this work - Copyright (c) 1999 - 2005, Intel Corp.
14 * All rights reserved.
15 *
16 * 2. License
17 *
18 * 2.1. This is your license from Intel Corp. under its intellectual property
19 * rights.  You may have additional license terms from the party that provided
20 * you this software, covering your right to use that party's intellectual
21 * property rights.
22 *
23 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24 * copy of the source code appearing in this file ("Covered Code") an
25 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26 * base code distributed originally by Intel ("Original Intel Code") to copy,
27 * make derivatives, distribute, use and display any portion of the Covered
28 * Code in any form, with the right to sublicense such rights; and
29 *
30 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31 * license (with the right to sublicense), under only those claims of Intel
32 * patents that are infringed by the Original Intel Code, to make, use, sell,
33 * offer to sell, and import the Covered Code and derivative works thereof
34 * solely to the minimum extent necessary to exercise the above copyright
35 * license, and in no event shall the patent license extend to any additions
36 * to or modifications of the Original Intel Code.  No other license or right
37 * is granted directly or by implication, estoppel or otherwise;
38 *
39 * The above copyright and patent license is granted only if the following
40 * conditions are met:
41 *
42 * 3. Conditions
43 *
44 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45 * Redistribution of source code of any substantial portion of the Covered
46 * Code or modification with rights to further distribute source must include
47 * the above Copyright Notice, the above License, this list of Conditions,
48 * and the following Disclaimer and Export Compliance provision.  In addition,
49 * Licensee must cause all Covered Code to which Licensee contributes to
50 * contain a file documenting the changes Licensee made to create that Covered
51 * Code and the date of any change.  Licensee must include in that file the
52 * documentation of any changes made by any predecessor Licensee.  Licensee
53 * must include a prominent statement that the modification is derived,
54 * directly or indirectly, from Original Intel Code.
55 *
56 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57 * Redistribution of source code of any substantial portion of the Covered
58 * Code or modification without rights to further distribute source must
59 * include the following Disclaimer and Export Compliance provision in the
60 * documentation and/or other materials provided with distribution.  In
61 * addition, Licensee may not authorize further sublicense of source of any
62 * portion of the Covered Code, and must include terms to the effect that the
63 * license from Licensee to its licensee is limited to the intellectual
64 * property embodied in the software Licensee provides to its licensee, and
65 * not to intellectual property embodied in modifications its licensee may
66 * make.
67 *
68 * 3.3. Redistribution of Executable. Redistribution in executable form of any
69 * substantial portion of the Covered Code or modification must reproduce the
70 * above Copyright Notice, and the following Disclaimer and Export Compliance
71 * provision in the documentation and/or other materials provided with the
72 * distribution.
73 *
74 * 3.4. Intel retains all right, title, and interest in and to the Original
75 * Intel Code.
76 *
77 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78 * Intel shall be used in advertising or otherwise to promote the sale, use or
79 * other dealings in products derived from or relating to the Covered Code
80 * without prior written authorization from Intel.
81 *
82 * 4. Disclaimer and Export Compliance
83 *
84 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85 * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
87 * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
88 * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
89 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90 * PARTICULAR PURPOSE.
91 *
92 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
98 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99 * LIMITED REMEDY.
100 *
101 * 4.3. Licensee shall not export, either directly or indirectly, any of this
102 * software or system incorporating such software without first obtaining any
103 * required license or other approval from the U. S. Department of Commerce or
104 * any other agency or department of the United States Government.  In the
105 * event Licensee exports any such software from the United States or
106 * re-exports any such software from a foreign destination, Licensee shall
107 * ensure that the distribution and export/re-export of the software is in
108 * compliance with all laws, regulations, orders, or other restrictions of the
109 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110 * any of its subsidiaries will export/re-export any technical data, process,
111 * software, or service, directly or indirectly, to any country for which the
112 * United States government or any agency thereof requires an export license,
113 * other governmental approval, or letter of assurance, without first obtaining
114 * such license, approval or letter.
115 *
116 *****************************************************************************/
117
118
119#include <contrib/dev/acpica/acpi.h>
120#include <contrib/dev/acpica/acdebug.h>
121#include <contrib/dev/acpica/acnamesp.h>
122#include <contrib/dev/acpica/actables.h>
123
124#if (defined ACPI_DEBUGGER || defined ACPI_DISASSEMBLER)
125
126#define _COMPONENT          ACPI_CA_DEBUGGER
127        ACPI_MODULE_NAME    ("dbfileio")
128
129/*
130 * NOTE: this is here for lack of a better place.  It is used in all
131 * flavors of the debugger, need LCD file
132 */
133#ifdef ACPI_APPLICATION
134#include <stdio.h>
135FILE                        *AcpiGbl_DebugFile = NULL;
136#endif
137
138
139#ifdef ACPI_DEBUGGER
140
141/* Local prototypes */
142
143static ACPI_STATUS
144AcpiDbCheckTextModeCorruption (
145    UINT8                   *Table,
146    UINT32                  TableLength,
147    UINT32                  FileLength);
148
149static ACPI_STATUS
150AeLocalLoadTable (
151    ACPI_TABLE_HEADER       *TablePtr);
152
153
154/*******************************************************************************
155 *
156 * FUNCTION:    AcpiDbCloseDebugFile
157 *
158 * PARAMETERS:  None
159 *
160 * RETURN:      None
161 *
162 * DESCRIPTION: If open, close the current debug output file
163 *
164 ******************************************************************************/
165
166void
167AcpiDbCloseDebugFile (
168    void)
169{
170
171#ifdef ACPI_APPLICATION
172
173    if (AcpiGbl_DebugFile)
174    {
175       fclose (AcpiGbl_DebugFile);
176       AcpiGbl_DebugFile = NULL;
177       AcpiGbl_DbOutputToFile = FALSE;
178       AcpiOsPrintf ("Debug output file %s closed\n", AcpiGbl_DbDebugFilename);
179    }
180#endif
181}
182
183
184/*******************************************************************************
185 *
186 * FUNCTION:    AcpiDbOpenDebugFile
187 *
188 * PARAMETERS:  Name                - Filename to open
189 *
190 * RETURN:      None
191 *
192 * DESCRIPTION: Open a file where debug output will be directed.
193 *
194 ******************************************************************************/
195
196void
197AcpiDbOpenDebugFile (
198    char                    *Name)
199{
200
201#ifdef ACPI_APPLICATION
202
203    AcpiDbCloseDebugFile ();
204    AcpiGbl_DebugFile = fopen (Name, "w+");
205    if (AcpiGbl_DebugFile)
206    {
207        AcpiOsPrintf ("Debug output file %s opened\n", Name);
208        ACPI_STRCPY (AcpiGbl_DbDebugFilename, Name);
209        AcpiGbl_DbOutputToFile = TRUE;
210    }
211    else
212    {
213        AcpiOsPrintf ("Could not open debug file %s\n", Name);
214    }
215
216#endif
217}
218#endif
219
220
221#ifdef ACPI_APPLICATION
222/*******************************************************************************
223 *
224 * FUNCTION:    AcpiDbCheckTextModeCorruption
225 *
226 * PARAMETERS:  Table           - Table buffer
227 *              TableLength     - Length of table from the table header
228 *              FileLength      - Length of the file that contains the table
229 *
230 * RETURN:      Status
231 *
232 * DESCRIPTION: Check table for text mode file corruption where all linefeed
233 *              characters (LF) have been replaced by carriage return linefeed
234 *              pairs (CR/LF).
235 *
236 ******************************************************************************/
237
238static ACPI_STATUS
239AcpiDbCheckTextModeCorruption (
240    UINT8                   *Table,
241    UINT32                  TableLength,
242    UINT32                  FileLength)
243{
244    UINT32                  i;
245    UINT32                  Pairs = 0;
246
247
248    if (TableLength != FileLength)
249    {
250        ACPI_REPORT_WARNING ((
251            "File length (0x%X) is not the same as the table length (0x%X)\n",
252            FileLength, TableLength));
253    }
254
255    /* Scan entire table to determine if each LF has been prefixed with a CR */
256
257    for (i = 1; i < FileLength; i++)
258    {
259        if (Table[i] == 0x0A)
260        {
261            if (Table[i - 1] != 0x0D)
262            {
263                /* The LF does not have a preceeding CR, table not corrupted */
264
265                return (AE_OK);
266            }
267            else
268            {
269                /* Found a CR/LF pair */
270
271                Pairs++;
272            }
273            i++;
274        }
275    }
276
277    /*
278     * Entire table scanned, each CR is part of a CR/LF pair --
279     * meaning that the table was treated as a text file somewhere.
280     *
281     * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
282     * original table are left untouched by the text conversion process --
283     * meaning that we cannot simply replace CR/LF pairs with LFs.
284     */
285    AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
286    AcpiOsPrintf ("All LFs (%d) were changed to CR/LF pairs\n", Pairs);
287    AcpiOsPrintf ("Table cannot be repaired!\n");
288    return (AE_BAD_VALUE);
289}
290
291
292/*******************************************************************************
293 *
294 * FUNCTION:    AcpiDbReadTable
295 *
296 * PARAMETERS:  fp              - File that contains table
297 *              Table           - Return value, buffer with table
298 *              TableLength     - Return value, length of table
299 *
300 * RETURN:      Status
301 *
302 * DESCRIPTION: Load the DSDT from the file pointer
303 *
304 ******************************************************************************/
305
306static ACPI_STATUS
307AcpiDbReadTable (
308    FILE                    *fp,
309    ACPI_TABLE_HEADER       **Table,
310    UINT32                  *TableLength)
311{
312    ACPI_TABLE_HEADER       TableHeader;
313    UINT32                  Actual;
314    ACPI_STATUS             Status;
315    UINT32                  FileSize;
316
317
318    fseek (fp, 0, SEEK_END);
319    FileSize = (UINT32) ftell (fp);
320    fseek (fp, 0, SEEK_SET);
321
322    /* Read the table header */
323
324    if (fread (&TableHeader, 1, sizeof (TableHeader), fp) !=
325            sizeof (ACPI_TABLE_HEADER))
326    {
327        AcpiOsPrintf ("Could not read the table header\n");
328        return (AE_BAD_HEADER);
329    }
330
331    /* Validate the table header/length */
332
333    Status = AcpiTbValidateTableHeader (&TableHeader);
334    if (ACPI_FAILURE (Status))
335    {
336        AcpiOsPrintf ("Table header is invalid!\n");
337        return (Status);
338    }
339
340    /* File size must be at least as long as the Header-specified length */
341
342    if (TableHeader.Length > FileSize)
343    {
344        AcpiOsPrintf (
345            "TableHeader length [0x%X] greater than the input file size [0x%X]\n",
346            TableHeader.Length, FileSize);
347        return (AE_BAD_HEADER);
348    }
349
350    /* We only support a limited number of table types */
351
352    if (ACPI_STRNCMP ((char *) TableHeader.Signature, DSDT_SIG, 4) &&
353        ACPI_STRNCMP ((char *) TableHeader.Signature, PSDT_SIG, 4) &&
354        ACPI_STRNCMP ((char *) TableHeader.Signature, SSDT_SIG, 4))
355    {
356        AcpiOsPrintf ("Table signature [%4.4s] is invalid or not supported\n",
357            (char *) TableHeader.Signature);
358        ACPI_DUMP_BUFFER (&TableHeader, sizeof (ACPI_TABLE_HEADER));
359        return (AE_ERROR);
360    }
361
362    /* Allocate a buffer for the table */
363
364    *TableLength = TableHeader.Length;
365    *Table = AcpiOsAllocate ((size_t) (FileSize));
366    if (!*Table)
367    {
368        AcpiOsPrintf (
369            "Could not allocate memory for ACPI table %4.4s (size=0x%X)\n",
370            TableHeader.Signature, TableHeader.Length);
371        return (AE_NO_MEMORY);
372    }
373
374    /* Get the rest of the table */
375
376    fseek (fp, 0, SEEK_SET);
377    Actual = fread (*Table, 1, (size_t) FileSize, fp);
378    if (Actual == FileSize)
379    {
380        /* Now validate the checksum */
381
382        Status = AcpiTbVerifyTableChecksum (*Table);
383
384        if (Status == AE_BAD_CHECKSUM)
385        {
386            Status = AcpiDbCheckTextModeCorruption ((UINT8 *) *Table,
387                        FileSize, (*Table)->Length);
388            return (Status);
389        }
390        return (AE_OK);
391    }
392
393    if (Actual > 0)
394    {
395        AcpiOsPrintf ("Warning - reading table, asked for %X got %X\n",
396            FileSize, Actual);
397        return (AE_OK);
398    }
399
400    AcpiOsPrintf ("Error - could not read the table file\n");
401    AcpiOsFree (*Table);
402    *Table = NULL;
403    *TableLength = 0;
404
405    return (AE_ERROR);
406}
407
408
409/*******************************************************************************
410 *
411 * FUNCTION:    AeLocalLoadTable
412 *
413 * PARAMETERS:  Table           - pointer to a buffer containing the entire
414 *                                table to be loaded
415 *
416 * RETURN:      Status
417 *
418 * DESCRIPTION: This function is called to load a table from the caller's
419 *              buffer.  The buffer must contain an entire ACPI Table including
420 *              a valid header.  The header fields will be verified, and if it
421 *              is determined that the table is invalid, the call will fail.
422 *
423 ******************************************************************************/
424
425static ACPI_STATUS
426AeLocalLoadTable (
427    ACPI_TABLE_HEADER       *Table)
428{
429    ACPI_STATUS             Status;
430    ACPI_TABLE_DESC         TableInfo;
431
432
433    ACPI_FUNCTION_TRACE ("AeLocalLoadTable");
434
435
436    if (!Table)
437    {
438        return_ACPI_STATUS (AE_BAD_PARAMETER);
439    }
440
441    TableInfo.Pointer = Table;
442    Status = AcpiTbRecognizeTable (&TableInfo, ACPI_TABLE_ALL);
443    if (ACPI_FAILURE (Status))
444    {
445        return_ACPI_STATUS (Status);
446    }
447
448    /* Install the new table into the local data structures */
449
450    Status = AcpiTbInstallTable (&TableInfo);
451    if (ACPI_FAILURE (Status))
452    {
453        if (Status == AE_ALREADY_EXISTS)
454        {
455            /* Table already exists, no error */
456
457            Status = AE_OK;
458        }
459
460        /* Free table allocated by AcpiTbGetTable */
461
462        AcpiTbDeleteSingleTable (&TableInfo);
463        return_ACPI_STATUS (Status);
464    }
465
466#if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
467
468    Status = AcpiNsLoadTable (TableInfo.InstalledDesc, AcpiGbl_RootNode);
469    if (ACPI_FAILURE (Status))
470    {
471        /* Uninstall table and free the buffer */
472
473        AcpiTbDeleteTablesByType (ACPI_TABLE_DSDT);
474        return_ACPI_STATUS (Status);
475    }
476#endif
477
478    return_ACPI_STATUS (Status);
479}
480
481
482/*******************************************************************************
483 *
484 * FUNCTION:    AcpiDbReadTableFromFile
485 *
486 * PARAMETERS:  Filename         - File where table is located
487 *              Table            - Where a pointer to the table is returned
488 *
489 * RETURN:      Status
490 *
491 * DESCRIPTION: Get an ACPI table from a file
492 *
493 ******************************************************************************/
494
495ACPI_STATUS
496AcpiDbReadTableFromFile (
497    char                    *Filename,
498    ACPI_TABLE_HEADER       **Table)
499{
500    FILE                    *fp;
501    UINT32                  TableLength;
502    ACPI_STATUS             Status;
503
504
505    /* Open the file */
506
507    fp = fopen (Filename, "rb");
508    if (!fp)
509    {
510        AcpiOsPrintf ("Could not open input file %s\n", Filename);
511        return (AE_ERROR);
512    }
513
514    /* Get the entire file */
515
516    fprintf (stderr, "Loading Acpi table from file %s\n", Filename);
517    Status = AcpiDbReadTable (fp, Table, &TableLength);
518    fclose(fp);
519
520    if (ACPI_FAILURE (Status))
521    {
522        AcpiOsPrintf ("Could not get table from the file\n");
523        return (Status);
524    }
525
526    return (AE_OK);
527 }
528#endif
529
530
531/*******************************************************************************
532 *
533 * FUNCTION:    AcpiDbGetTableFromFile
534 *
535 * PARAMETERS:  Filename        - File where table is located
536 *              ReturnTable     - Where a pointer to the table is returned
537 *
538 * RETURN:      Status
539 *
540 * DESCRIPTION: Load an ACPI table from a file
541 *
542 ******************************************************************************/
543
544ACPI_STATUS
545AcpiDbGetTableFromFile (
546    char                    *Filename,
547    ACPI_TABLE_HEADER       **ReturnTable)
548{
549#ifdef ACPI_APPLICATION
550    ACPI_STATUS             Status;
551    ACPI_TABLE_HEADER       *Table;
552
553
554    Status = AcpiDbReadTableFromFile (Filename, &Table);
555    if (ACPI_FAILURE (Status))
556    {
557        return (Status);
558    }
559
560   /* Attempt to recognize and install the table */
561
562    Status = AeLocalLoadTable (Table);
563    if (ACPI_FAILURE (Status))
564    {
565        if (Status == AE_ALREADY_EXISTS)
566        {
567            AcpiOsPrintf ("Table %4.4s is already installed\n",
568                            Table->Signature);
569        }
570        else
571        {
572            AcpiOsPrintf ("Could not install table, %s\n",
573                            AcpiFormatException (Status));
574        }
575
576        return (Status);
577    }
578
579    fprintf (stderr, "Acpi table [%4.4s] successfully installed and loaded\n",
580                                Table->Signature);
581
582    AcpiGbl_AcpiHardwarePresent = FALSE;
583    if (ReturnTable)
584    {
585        *ReturnTable = Table;
586    }
587
588
589#endif  /* ACPI_APPLICATION */
590    return (AE_OK);
591}
592
593#endif  /* ACPI_DEBUGGER */
594
595