Update ACPI build wrappers to use new ACPICA(20050309) code.
[dragonfly.git] / sys / contrib / dev / acpica-unix-20050211 / debugger / dbfileio.c
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: 77 $
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 "acpi.h"
120 #include "acdebug.h"
121 #include "acnamesp.h"
122 #include "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 /*
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>
136 FILE                        *AcpiGbl_DebugFile = NULL;
137 #endif
138
139
140 #ifdef ACPI_DEBUGGER
141 /*******************************************************************************
142  *
143  * FUNCTION:    AcpiDbCloseDebugFile
144  *
145  * PARAMETERS:  None
146  *
147  * RETURN:      Status
148  *
149  * DESCRIPTION: If open, close the current debug output file
150  *
151  ******************************************************************************/
152
153 void
154 AcpiDbCloseDebugFile (
155     void)
156 {
157
158 #ifdef ACPI_APPLICATION
159
160     if (AcpiGbl_DebugFile)
161     {
162        fclose (AcpiGbl_DebugFile);
163        AcpiGbl_DebugFile = NULL;
164        AcpiGbl_DbOutputToFile = FALSE;
165        AcpiOsPrintf ("Debug output file %s closed\n", AcpiGbl_DbDebugFilename);
166     }
167 #endif
168 }
169
170
171 /*******************************************************************************
172  *
173  * FUNCTION:    AcpiDbOpenDebugFile
174  *
175  * PARAMETERS:  Name                - Filename to open
176  *
177  * RETURN:      Status
178  *
179  * DESCRIPTION: Open a file where debug output will be directed.
180  *
181  ******************************************************************************/
182
183 void
184 AcpiDbOpenDebugFile (
185     char                    *Name)
186 {
187
188 #ifdef ACPI_APPLICATION
189
190     AcpiDbCloseDebugFile ();
191     AcpiGbl_DebugFile = fopen (Name, "w+");
192     if (AcpiGbl_DebugFile)
193     {
194         AcpiOsPrintf ("Debug output file %s opened\n", Name);
195         ACPI_STRCPY (AcpiGbl_DbDebugFilename, Name);
196         AcpiGbl_DbOutputToFile = TRUE;
197     }
198     else
199     {
200         AcpiOsPrintf ("Could not open debug file %s\n", Name);
201     }
202
203 #endif
204 }
205 #endif
206
207
208 #ifdef ACPI_APPLICATION
209
210 /*******************************************************************************
211  *
212  * FUNCTION:    AcpiDbCheckTextModeCorruption
213  *
214  * PARAMETERS:  Table           - Table buffer
215  *              TableLength     - Length of table from the table header
216  *              FileLength      - Length of the file that contains the table
217  *
218  * RETURN:      Status
219  *
220  * DESCRIPTION: Check table for text mode file corruption where all linefeed
221  *              characters (LF) have been replaced by carriage return linefeed
222  *              pairs (CR/LF).
223  *
224  ******************************************************************************/
225
226 static ACPI_STATUS
227 AcpiDbCheckTextModeCorruption (
228     UINT8                   *Table,
229     UINT32                  TableLength,
230     UINT32                  FileLength)
231 {
232     UINT32                  i;
233     UINT32                  Pairs = 0;
234
235
236     if (TableLength != FileLength)
237     {
238         ACPI_REPORT_WARNING (("File length (0x%X) is not the same as the table length (0x%X)\n",
239                 FileLength, TableLength));
240     }
241
242     /* Scan entire table to determine if each LF has been prefixed with a CR */
243
244     for (i = 1; i < FileLength; i++)
245     {
246         if (Table[i] == 0x0A)
247         {
248             if (Table[i - 1] != 0x0D)
249             {
250                 /* the LF does not have a preceeding CR, table is not corrupted */
251
252                 return (AE_OK);
253             }
254             else
255             {
256                 /* Found a CR/LF pair */
257
258                 Pairs++;
259             }
260             i++;
261         }
262     }
263
264     /*
265      * Entire table scanned, each CR is part of a CR/LF pair --
266      * meaning that the table was treated as a text file somewhere.
267      *
268      * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
269      * original table are left untouched by the text conversion process --
270      * meaning that we cannot simply replace CR/LF pairs with LFs.
271      */
272     AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
273     AcpiOsPrintf ("All LFs (%d) were changed to CR/LF pairs\n", Pairs);
274     AcpiOsPrintf ("Table cannot be repaired!\n");
275     return (AE_BAD_VALUE);
276 }
277
278
279 /*******************************************************************************
280  *
281  * FUNCTION:    AcpiDbReadTable
282  *
283  * PARAMETERS:  fp              - File that contains table
284  *              Table           - Return value, buffer with table
285  *              TableLength     - Return value, length of table
286  *
287  * RETURN:      Status
288  *
289  * DESCRIPTION: Load the DSDT from the file pointer
290  *
291  ******************************************************************************/
292
293 static ACPI_STATUS
294 AcpiDbReadTable (
295     FILE                    *fp,
296     ACPI_TABLE_HEADER       **Table,
297     UINT32                  *TableLength)
298 {
299     ACPI_TABLE_HEADER       TableHeader;
300     UINT32                  Actual;
301     ACPI_STATUS             Status;
302     UINT32                  FileSize;
303
304
305     fseek (fp, 0, SEEK_END);
306     FileSize = ftell (fp);
307     fseek (fp, 0, SEEK_SET);
308
309     /* Read the table header */
310
311     if (fread (&TableHeader, 1, sizeof (TableHeader), fp) != sizeof (ACPI_TABLE_HEADER))
312     {
313         AcpiOsPrintf ("Couldn't read the table header\n");
314         return (AE_BAD_SIGNATURE);
315     }
316
317     /* Validate the table header/length */
318
319     Status = AcpiTbValidateTableHeader (&TableHeader);
320     if ((ACPI_FAILURE (Status)) ||
321         (TableHeader.Length > 0x800000))  /* 8 Mbyte should be enough */
322     {
323         AcpiOsPrintf ("Table header is invalid!\n");
324         return (AE_ERROR);
325     }
326
327     /* We only support a limited number of table types */
328
329     if (ACPI_STRNCMP ((char *) TableHeader.Signature, DSDT_SIG, 4) &&
330         ACPI_STRNCMP ((char *) TableHeader.Signature, PSDT_SIG, 4) &&
331         ACPI_STRNCMP ((char *) TableHeader.Signature, SSDT_SIG, 4))
332     {
333         AcpiOsPrintf ("Table signature is invalid\n");
334         ACPI_DUMP_BUFFER (&TableHeader, sizeof (ACPI_TABLE_HEADER));
335         return (AE_ERROR);
336     }
337
338     /* Allocate a buffer for the table */
339
340     *TableLength = TableHeader.Length;
341     *Table = AcpiOsAllocate ((size_t) (FileSize));
342     if (!*Table)
343     {
344         AcpiOsPrintf ("Could not allocate memory for ACPI table %4.4s (size=%X)\n",
345                     TableHeader.Signature, TableHeader.Length);
346         return (AE_NO_MEMORY);
347     }
348
349     /* Get the rest of the table */
350
351     fseek (fp, 0, SEEK_SET);
352     Actual = fread (*Table, 1, (size_t) FileSize, fp);
353     if (Actual == FileSize)
354     {
355         /* Now validate the checksum */
356
357         Status = AcpiTbVerifyTableChecksum (*Table);
358
359         if (Status == AE_BAD_CHECKSUM)
360         {
361             Status = AcpiDbCheckTextModeCorruption ((UINT8 *) *Table,
362                         FileSize, (*Table)->Length);
363             return (Status);
364         }
365         return (AE_OK);
366     }
367
368     if (Actual > 0)
369     {
370         AcpiOsPrintf ("Warning - reading table, asked for %X got %X\n",
371             FileSize, Actual);
372         return (AE_OK);
373     }
374
375     AcpiOsPrintf ("Error - could not read the table file\n");
376     AcpiOsFree (*Table);
377     *Table = NULL;
378     *TableLength = 0;
379
380     return (AE_ERROR);
381 }
382 #endif
383
384
385 /*******************************************************************************
386  *
387  * FUNCTION:    AeLocalLoadTable
388  *
389  * PARAMETERS:  Table           - pointer to a buffer containing the entire
390  *                                table to be loaded
391  *
392  * RETURN:      Status
393  *
394  * DESCRIPTION: This function is called to load a table from the caller's
395  *              buffer.  The buffer must contain an entire ACPI Table including
396  *              a valid header.  The header fields will be verified, and if it
397  *              is determined that the table is invalid, the call will fail.
398  *
399  ******************************************************************************/
400
401 ACPI_STATUS
402 AeLocalLoadTable (
403     ACPI_TABLE_HEADER       *Table)
404 {
405     ACPI_STATUS             Status;
406     ACPI_TABLE_DESC         TableInfo;
407
408
409     ACPI_FUNCTION_TRACE ("AeLocalLoadTable");
410
411
412     if (!Table)
413     {
414         return_ACPI_STATUS (AE_BAD_PARAMETER);
415     }
416
417     TableInfo.Pointer = Table;
418     Status = AcpiTbRecognizeTable (&TableInfo, ACPI_TABLE_ALL);
419     if (ACPI_FAILURE (Status))
420     {
421         return_ACPI_STATUS (Status);
422     }
423
424     /* Install the new table into the local data structures */
425
426     Status = AcpiTbInstallTable (&TableInfo);
427     if (ACPI_FAILURE (Status))
428     {
429         /* Free table allocated by AcpiTbGetTable */
430
431         AcpiTbDeleteSingleTable (&TableInfo);
432         return_ACPI_STATUS (Status);
433     }
434
435 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
436
437     Status = AcpiNsLoadTable (TableInfo.InstalledDesc, AcpiGbl_RootNode);
438     if (ACPI_FAILURE (Status))
439     {
440         /* Uninstall table and free the buffer */
441
442         AcpiTbDeleteTablesByType (ACPI_TABLE_DSDT);
443         return_ACPI_STATUS (Status);
444     }
445 #endif
446
447     return_ACPI_STATUS (Status);
448 }
449
450
451 #ifdef ACPI_APPLICATION
452 /*******************************************************************************
453  *
454  * FUNCTION:    AcpiDbReadTableFromFile
455  *
456  * PARAMETERS:  Filename         - File where table is located
457  *              Table            - Where a pointer to the table is returned
458  *
459  * RETURN:      Status
460  *
461  * DESCRIPTION: Get an ACPI table from a file
462  *
463  ******************************************************************************/
464
465 ACPI_STATUS
466 AcpiDbReadTableFromFile (
467     char                    *Filename,
468     ACPI_TABLE_HEADER       **Table)
469 {
470     FILE                    *fp;
471     UINT32                  TableLength;
472     ACPI_STATUS             Status;
473
474
475     /* Open the file */
476
477     fp = fopen (Filename, "rb");
478     if (!fp)
479     {
480         AcpiOsPrintf ("Could not open input file %s\n", Filename);
481         return (AE_ERROR);
482     }
483
484     /* Get the entire file */
485
486     fprintf (stderr, "Loading Acpi table from file %s\n", Filename);
487     Status = AcpiDbReadTable (fp, Table, &TableLength);
488     fclose(fp);
489
490     if (ACPI_FAILURE (Status))
491     {
492         AcpiOsPrintf ("Couldn't get table from the file\n");
493         return (Status);
494     }
495
496     return (AE_OK);
497  }
498 #endif
499
500
501 /*******************************************************************************
502  *
503  * FUNCTION:    AcpiDbGetTableFromFile
504  *
505  * PARAMETERS:  Filename         - File where table is located
506  *              Table            - Where a pointer to the table is returned
507  *
508  * RETURN:      Status
509  *
510  * DESCRIPTION: Load an ACPI table from a file
511  *
512  ******************************************************************************/
513
514 ACPI_STATUS
515 AcpiDbGetTableFromFile (
516     char                    *Filename,
517     ACPI_TABLE_HEADER       **ReturnTable)
518 {
519 #ifdef ACPI_APPLICATION
520     ACPI_STATUS             Status;
521     ACPI_TABLE_HEADER       *Table;
522
523
524     Status = AcpiDbReadTableFromFile (Filename, &Table);
525     if (ACPI_FAILURE (Status))
526     {
527         return (Status);
528     }
529
530    /* Attempt to recognize and install the table */
531
532     Status = AeLocalLoadTable (Table);
533     if (ACPI_FAILURE (Status))
534     {
535         if (Status == AE_ALREADY_EXISTS)
536         {
537             AcpiOsPrintf ("Table %4.4s is already installed\n",
538                             Table->Signature);
539         }
540         else
541         {
542             AcpiOsPrintf ("Could not install table, %s\n",
543                             AcpiFormatException (Status));
544         }
545
546         return (Status);
547     }
548
549     fprintf (stderr, "Acpi table [%4.4s] successfully installed and loaded\n",
550                                 Table->Signature);
551
552     AcpiGbl_AcpiHardwarePresent = FALSE;
553     if (ReturnTable)
554     {
555         *ReturnTable = Table;
556     }
557
558
559 #endif  /* ACPI_APPLICATION */
560     return (AE_OK);
561 }
562
563 #endif  /* ACPI_DEBUGGER */
564