dbfileio.c revision 167802
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.94 $
6 *
7 ******************************************************************************/
8
9/******************************************************************************
10 *
11 * 1. Copyright Notice
12 *
13 * Some or all of this work - Copyright (c) 1999 - 2007, 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#include <contrib/dev/acpica/acdisasm.h>
124
125#if (defined ACPI_DEBUGGER || defined ACPI_DISASSEMBLER)
126
127#define _COMPONENT          ACPI_CA_DEBUGGER
128        ACPI_MODULE_NAME    ("dbfileio")
129
130/*
131 * NOTE: this is here for lack of a better place.  It is used in all
132 * flavors of the debugger, need LCD file
133 */
134#ifdef ACPI_APPLICATION
135#include <stdio.h>
136FILE                        *AcpiGbl_DebugFile = NULL;
137#endif
138
139
140#ifdef ACPI_DEBUGGER
141
142/* Local prototypes */
143
144#ifdef ACPI_APPLICATION
145
146static ACPI_STATUS
147AcpiDbCheckTextModeCorruption (
148    UINT8                   *Table,
149    UINT32                  TableLength,
150    UINT32                  FileLength);
151
152static ACPI_STATUS
153AeLocalLoadTable (
154    ACPI_TABLE_HEADER       *TablePtr);
155#endif
156
157/*******************************************************************************
158 *
159 * FUNCTION:    AcpiDbCloseDebugFile
160 *
161 * PARAMETERS:  None
162 *
163 * RETURN:      None
164 *
165 * DESCRIPTION: If open, close the current debug output file
166 *
167 ******************************************************************************/
168
169void
170AcpiDbCloseDebugFile (
171    void)
172{
173
174#ifdef ACPI_APPLICATION
175
176    if (AcpiGbl_DebugFile)
177    {
178       fclose (AcpiGbl_DebugFile);
179       AcpiGbl_DebugFile = NULL;
180       AcpiGbl_DbOutputToFile = FALSE;
181       AcpiOsPrintf ("Debug output file %s closed\n", AcpiGbl_DbDebugFilename);
182    }
183#endif
184}
185
186
187/*******************************************************************************
188 *
189 * FUNCTION:    AcpiDbOpenDebugFile
190 *
191 * PARAMETERS:  Name                - Filename to open
192 *
193 * RETURN:      None
194 *
195 * DESCRIPTION: Open a file where debug output will be directed.
196 *
197 ******************************************************************************/
198
199void
200AcpiDbOpenDebugFile (
201    char                    *Name)
202{
203
204#ifdef ACPI_APPLICATION
205
206    AcpiDbCloseDebugFile ();
207    AcpiGbl_DebugFile = fopen (Name, "w+");
208    if (AcpiGbl_DebugFile)
209    {
210        AcpiOsPrintf ("Debug output file %s opened\n", Name);
211        ACPI_STRCPY (AcpiGbl_DbDebugFilename, Name);
212        AcpiGbl_DbOutputToFile = TRUE;
213    }
214    else
215    {
216        AcpiOsPrintf ("Could not open debug file %s\n", Name);
217    }
218
219#endif
220}
221#endif
222
223
224#ifdef ACPI_APPLICATION
225/*******************************************************************************
226 *
227 * FUNCTION:    AcpiDbCheckTextModeCorruption
228 *
229 * PARAMETERS:  Table           - Table buffer
230 *              TableLength     - Length of table from the table header
231 *              FileLength      - Length of the file that contains the table
232 *
233 * RETURN:      Status
234 *
235 * DESCRIPTION: Check table for text mode file corruption where all linefeed
236 *              characters (LF) have been replaced by carriage return linefeed
237 *              pairs (CR/LF).
238 *
239 ******************************************************************************/
240
241static ACPI_STATUS
242AcpiDbCheckTextModeCorruption (
243    UINT8                   *Table,
244    UINT32                  TableLength,
245    UINT32                  FileLength)
246{
247    UINT32                  i;
248    UINT32                  Pairs = 0;
249
250
251    if (TableLength != FileLength)
252    {
253        ACPI_WARNING ((AE_INFO,
254            "File length (0x%X) is not the same as the table length (0x%X)",
255            FileLength, TableLength));
256    }
257
258    /* Scan entire table to determine if each LF has been prefixed with a CR */
259
260    for (i = 1; i < FileLength; i++)
261    {
262        if (Table[i] == 0x0A)
263        {
264            if (Table[i - 1] != 0x0D)
265            {
266                /* The LF does not have a preceeding CR, table not corrupted */
267
268                return (AE_OK);
269            }
270            else
271            {
272                /* Found a CR/LF pair */
273
274                Pairs++;
275            }
276            i++;
277        }
278    }
279
280    if (!Pairs)
281    {
282        return (AE_OK);
283    }
284
285    /*
286     * Entire table scanned, each CR is part of a CR/LF pair --
287     * meaning that the table was treated as a text file somewhere.
288     *
289     * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
290     * original table are left untouched by the text conversion process --
291     * meaning that we cannot simply replace CR/LF pairs with LFs.
292     */
293    AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
294    AcpiOsPrintf ("All LFs (%d) were changed to CR/LF pairs\n", Pairs);
295    AcpiOsPrintf ("Table cannot be repaired!\n");
296    return (AE_BAD_VALUE);
297}
298
299
300/*******************************************************************************
301 *
302 * FUNCTION:    AcpiDbReadTable
303 *
304 * PARAMETERS:  fp              - File that contains table
305 *              Table           - Return value, buffer with table
306 *              TableLength     - Return value, length of table
307 *
308 * RETURN:      Status
309 *
310 * DESCRIPTION: Load the DSDT from the file pointer
311 *
312 ******************************************************************************/
313
314static ACPI_STATUS
315AcpiDbReadTable (
316    FILE                    *fp,
317    ACPI_TABLE_HEADER       **Table,
318    UINT32                  *TableLength)
319{
320    ACPI_TABLE_HEADER       TableHeader;
321    UINT32                  Actual;
322    ACPI_STATUS             Status;
323    UINT32                  FileSize;
324    BOOLEAN                 StandardHeader = TRUE;
325
326
327    /* Get the file size */
328
329    fseek (fp, 0, SEEK_END);
330    FileSize = (UINT32) ftell (fp);
331    fseek (fp, 0, SEEK_SET);
332
333    if (FileSize < 4)
334    {
335        return (AE_BAD_HEADER);
336    }
337
338    /* Read the signature */
339
340    if (fread (&TableHeader, 1, 4, fp) != 4)
341    {
342        AcpiOsPrintf ("Could not read the table signature\n");
343        return (AE_BAD_HEADER);
344    }
345
346    fseek (fp, 0, SEEK_SET);
347
348    /* The RSDT and FACS tables do not have standard ACPI headers */
349
350    if (ACPI_COMPARE_NAME (TableHeader.Signature, "RSD ") ||
351        ACPI_COMPARE_NAME (TableHeader.Signature, "FACS"))
352    {
353        *TableLength = FileSize;
354        StandardHeader = FALSE;
355    }
356    else
357    {
358    /* Read the table header */
359
360        if (fread (&TableHeader, 1, sizeof (TableHeader), fp) !=
361                sizeof (ACPI_TABLE_HEADER))
362        {
363            AcpiOsPrintf ("Could not read the table header\n");
364            return (AE_BAD_HEADER);
365        }
366
367#if 0
368        /* Validate the table header/length */
369
370        Status = AcpiTbValidateTableHeader (&TableHeader);
371        if (ACPI_FAILURE (Status))
372        {
373            AcpiOsPrintf ("Table header is invalid!\n");
374            return (Status);
375        }
376#endif
377
378        /* File size must be at least as long as the Header-specified length */
379
380        if (TableHeader.Length > FileSize)
381        {
382            AcpiOsPrintf (
383                "TableHeader length [0x%X] greater than the input file size [0x%X]\n",
384                TableHeader.Length, FileSize);
385            return (AE_BAD_HEADER);
386        }
387
388#ifdef ACPI_OBSOLETE_CODE
389        /* We only support a limited number of table types */
390
391        if (ACPI_STRNCMP ((char *) TableHeader.Signature, DSDT_SIG, 4) &&
392            ACPI_STRNCMP ((char *) TableHeader.Signature, PSDT_SIG, 4) &&
393            ACPI_STRNCMP ((char *) TableHeader.Signature, SSDT_SIG, 4))
394        {
395            AcpiOsPrintf ("Table signature [%4.4s] is invalid or not supported\n",
396                (char *) TableHeader.Signature);
397            ACPI_DUMP_BUFFER (&TableHeader, sizeof (ACPI_TABLE_HEADER));
398            return (AE_ERROR);
399        }
400#endif
401
402        *TableLength = TableHeader.Length;
403    }
404
405    /* Allocate a buffer for the table */
406
407    *Table = AcpiOsAllocate ((size_t) FileSize);
408    if (!*Table)
409    {
410        AcpiOsPrintf (
411            "Could not allocate memory for ACPI table %4.4s (size=0x%X)\n",
412            TableHeader.Signature, *TableLength);
413        return (AE_NO_MEMORY);
414    }
415
416    /* Get the rest of the table */
417
418    fseek (fp, 0, SEEK_SET);
419    Actual = fread (*Table, 1, (size_t) FileSize, fp);
420    if (Actual == FileSize)
421    {
422        if (StandardHeader)
423        {
424            /* Now validate the checksum */
425
426            Status = AcpiTbChecksum ((void *) *Table,
427                        ACPI_CAST_PTR (ACPI_TABLE_HEADER, *Table)->Length);
428
429            if (Status == AE_BAD_CHECKSUM)
430            {
431                Status = AcpiDbCheckTextModeCorruption ((UINT8 *) *Table,
432                            FileSize, (*Table)->Length);
433                return (Status);
434            }
435        }
436        return (AE_OK);
437    }
438
439    if (Actual > 0)
440    {
441        AcpiOsPrintf ("Warning - reading table, asked for %X got %X\n",
442            FileSize, Actual);
443        return (AE_OK);
444    }
445
446    AcpiOsPrintf ("Error - could not read the table file\n");
447    AcpiOsFree (*Table);
448    *Table = NULL;
449    *TableLength = 0;
450
451    return (AE_ERROR);
452}
453
454
455/*******************************************************************************
456 *
457 * FUNCTION:    AeLocalLoadTable
458 *
459 * PARAMETERS:  Table           - pointer to a buffer containing the entire
460 *                                table to be loaded
461 *
462 * RETURN:      Status
463 *
464 * DESCRIPTION: This function is called to load a table from the caller's
465 *              buffer.  The buffer must contain an entire ACPI Table including
466 *              a valid header.  The header fields will be verified, and if it
467 *              is determined that the table is invalid, the call will fail.
468 *
469 ******************************************************************************/
470
471static ACPI_STATUS
472AeLocalLoadTable (
473    ACPI_TABLE_HEADER       *Table)
474{
475    ACPI_STATUS             Status = AE_OK;
476/*    ACPI_TABLE_DESC         TableInfo; */
477
478
479    ACPI_FUNCTION_TRACE (AeLocalLoadTable);
480#if 0
481
482
483    if (!Table)
484    {
485        return_ACPI_STATUS (AE_BAD_PARAMETER);
486    }
487
488    TableInfo.Pointer = Table;
489    Status = AcpiTbRecognizeTable (&TableInfo, ACPI_TABLE_ALL);
490    if (ACPI_FAILURE (Status))
491    {
492        return_ACPI_STATUS (Status);
493    }
494
495    /* Install the new table into the local data structures */
496
497    Status = AcpiTbInstallTable (&TableInfo);
498    if (ACPI_FAILURE (Status))
499    {
500        if (Status == AE_ALREADY_EXISTS)
501        {
502            /* Table already exists, no error */
503
504            Status = AE_OK;
505        }
506
507        /* Free table allocated by AcpiTbGetTable */
508
509        AcpiTbDeleteSingleTable (&TableInfo);
510        return_ACPI_STATUS (Status);
511    }
512
513#if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
514
515    Status = AcpiNsLoadTable (TableInfo.InstalledDesc, AcpiGbl_RootNode);
516    if (ACPI_FAILURE (Status))
517    {
518        /* Uninstall table and free the buffer */
519
520        AcpiTbDeleteTablesByType (ACPI_TABLE_ID_DSDT);
521        return_ACPI_STATUS (Status);
522    }
523#endif
524#endif
525
526    return_ACPI_STATUS (Status);
527}
528
529
530/*******************************************************************************
531 *
532 * FUNCTION:    AcpiDbReadTableFromFile
533 *
534 * PARAMETERS:  Filename         - File where table is located
535 *              Table            - Where a pointer to the table is returned
536 *
537 * RETURN:      Status
538 *
539 * DESCRIPTION: Get an ACPI table from a file
540 *
541 ******************************************************************************/
542
543ACPI_STATUS
544AcpiDbReadTableFromFile (
545    char                    *Filename,
546    ACPI_TABLE_HEADER       **Table)
547{
548    FILE                    *fp;
549    UINT32                  TableLength;
550    ACPI_STATUS             Status;
551
552
553    /* Open the file */
554
555    fp = fopen (Filename, "rb");
556    if (!fp)
557    {
558        AcpiOsPrintf ("Could not open input file %s\n", Filename);
559        return (AE_ERROR);
560    }
561
562    /* Get the entire file */
563
564    fprintf (stderr, "Loading Acpi table from file %s\n", Filename);
565    Status = AcpiDbReadTable (fp, Table, &TableLength);
566    fclose(fp);
567
568    if (ACPI_FAILURE (Status))
569    {
570        AcpiOsPrintf ("Could not get table from the file\n");
571        return (Status);
572    }
573
574    return (AE_OK);
575 }
576#endif
577
578
579/*******************************************************************************
580 *
581 * FUNCTION:    AcpiDbGetTableFromFile
582 *
583 * PARAMETERS:  Filename        - File where table is located
584 *              ReturnTable     - Where a pointer to the table is returned
585 *
586 * RETURN:      Status
587 *
588 * DESCRIPTION: Load an ACPI table from a file
589 *
590 ******************************************************************************/
591
592ACPI_STATUS
593AcpiDbGetTableFromFile (
594    char                    *Filename,
595    ACPI_TABLE_HEADER       **ReturnTable)
596{
597#ifdef ACPI_APPLICATION
598    ACPI_STATUS             Status;
599    ACPI_TABLE_HEADER       *Table;
600    BOOLEAN                 IsAmlTable = TRUE;
601
602
603    Status = AcpiDbReadTableFromFile (Filename, &Table);
604    if (ACPI_FAILURE (Status))
605    {
606        return (Status);
607    }
608
609#ifdef ACPI_DATA_TABLE_DISASSEMBLY
610    IsAmlTable = AcpiUtIsAmlTable (Table);
611#endif
612
613    if (IsAmlTable)
614    {
615        /* Attempt to recognize and install the table */
616
617        Status = AeLocalLoadTable (Table);
618        if (ACPI_FAILURE (Status))
619        {
620            if (Status == AE_ALREADY_EXISTS)
621            {
622                AcpiOsPrintf ("Table %4.4s is already installed\n",
623                    Table->Signature);
624            }
625            else
626            {
627                AcpiOsPrintf ("Could not install table, %s\n",
628                    AcpiFormatException (Status));
629            }
630
631            return (Status);
632        }
633
634        fprintf (stderr,
635            "Acpi table [%4.4s] successfully installed and loaded\n",
636            Table->Signature);
637    }
638
639    AcpiGbl_AcpiHardwarePresent = FALSE;
640    if (ReturnTable)
641    {
642        *ReturnTable = Table;
643    }
644
645
646#endif  /* ACPI_APPLICATION */
647    return (AE_OK);
648}
649
650#endif  /* ACPI_DEBUGGER */
651
652