OSDN Git Service

klibc基本機能実装. ACPICAの準備
[vaneos/DivergeMirror.git] / drivers / acpi / components / debugger / dbtest.c
1 /*******************************************************************************
2  *
3  * Module Name: dbtest - Various debug-related tests
4  *
5  ******************************************************************************/
6
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2015, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************/
115
116 #include "acpi.h"
117 #include "accommon.h"
118 #include "acdebug.h"
119 #include "acnamesp.h"
120 #include "acpredef.h"
121
122 #ifdef ACPI_DEBUGGER
123
124 #define _COMPONENT          ACPI_CA_DEBUGGER
125         ACPI_MODULE_NAME    ("dbtest")
126
127
128 /* Local prototypes */
129
130 static void
131 AcpiDbTestAllObjects (
132     void);
133
134 static ACPI_STATUS
135 AcpiDbTestOneObject (
136     ACPI_HANDLE             ObjHandle,
137     UINT32                  NestingLevel,
138     void                    *Context,
139     void                    **ReturnValue);
140
141 static ACPI_STATUS
142 AcpiDbTestIntegerType (
143     ACPI_NAMESPACE_NODE     *Node,
144     UINT32                  BitLength);
145
146 static ACPI_STATUS
147 AcpiDbTestBufferType (
148     ACPI_NAMESPACE_NODE     *Node,
149     UINT32                  BitLength);
150
151 static ACPI_STATUS
152 AcpiDbTestStringType (
153     ACPI_NAMESPACE_NODE     *Node,
154     UINT32                  ByteLength);
155
156 static ACPI_STATUS
157 AcpiDbReadFromObject (
158     ACPI_NAMESPACE_NODE     *Node,
159     ACPI_OBJECT_TYPE        ExpectedType,
160     ACPI_OBJECT             **Value);
161
162 static ACPI_STATUS
163 AcpiDbWriteToObject (
164     ACPI_NAMESPACE_NODE     *Node,
165     ACPI_OBJECT             *Value);
166
167 static void
168 AcpiDbEvaluateAllPredefinedNames (
169     char                    *CountArg);
170
171 static ACPI_STATUS
172 AcpiDbEvaluateOnePredefinedName (
173     ACPI_HANDLE             ObjHandle,
174     UINT32                  NestingLevel,
175     void                    *Context,
176     void                    **ReturnValue);
177
178 /*
179  * Test subcommands
180  */
181 static ACPI_DB_ARGUMENT_INFO    AcpiDbTestTypes [] =
182 {
183     {"OBJECTS"},
184     {"PREDEFINED"},
185     {NULL}           /* Must be null terminated */
186 };
187
188 #define CMD_TEST_OBJECTS        0
189 #define CMD_TEST_PREDEFINED     1
190
191 #define BUFFER_FILL_VALUE       0xFF
192
193 /*
194  * Support for the special debugger read/write control methods.
195  * These methods are installed into the current namespace and are
196  * used to read and write the various namespace objects. The point
197  * is to force the AML interpreter do all of the work.
198  */
199 #define                     ACPI_DB_READ_METHOD     "\\_T98"
200 #define                     ACPI_DB_WRITE_METHOD    "\\_T99"
201
202 static ACPI_HANDLE          ReadHandle = NULL;
203 static ACPI_HANDLE          WriteHandle = NULL;
204
205 /* ASL Definitions of the debugger read/write control methods */
206
207 #if 0
208 DefinitionBlock ("ssdt.aml", "SSDT", 2, "Intel", "DEBUG", 0x00000001)
209 {
210     Method (_T98, 1, NotSerialized)     /* Read */
211     {
212         Return (DeRefOf (Arg0))
213     }
214 }
215 DefinitionBlock ("ssdt2.aml", "SSDT", 2, "Intel", "DEBUG", 0x00000001)
216 {
217     Method (_T99, 2, NotSerialized)     /* Write */
218     {
219         Store (Arg1, Arg0)
220     }
221 }
222 #endif
223
224 static unsigned char ReadMethodCode[] =
225 {
226     0x53,0x53,0x44,0x54,0x2E,0x00,0x00,0x00,  /* 00000000    "SSDT...." */
227     0x02,0xC9,0x49,0x6E,0x74,0x65,0x6C,0x00,  /* 00000008    "..Intel." */
228     0x44,0x45,0x42,0x55,0x47,0x00,0x00,0x00,  /* 00000010    "DEBUG..." */
229     0x01,0x00,0x00,0x00,0x49,0x4E,0x54,0x4C,  /* 00000018    "....INTL" */
230     0x18,0x12,0x13,0x20,0x14,0x09,0x5F,0x54,  /* 00000020    "... .._T" */
231     0x39,0x38,0x01,0xA4,0x83,0x68             /* 00000028    "98...h"   */
232 };
233
234 static unsigned char WriteMethodCode[] =
235 {
236     0x53,0x53,0x44,0x54,0x2E,0x00,0x00,0x00,  /* 00000000    "SSDT...." */
237     0x02,0x15,0x49,0x6E,0x74,0x65,0x6C,0x00,  /* 00000008    "..Intel." */
238     0x44,0x45,0x42,0x55,0x47,0x00,0x00,0x00,  /* 00000010    "DEBUG..." */
239     0x01,0x00,0x00,0x00,0x49,0x4E,0x54,0x4C,  /* 00000018    "....INTL" */
240     0x18,0x12,0x13,0x20,0x14,0x09,0x5F,0x54,  /* 00000020    "... .._T" */
241     0x39,0x39,0x02,0x70,0x69,0x68             /* 00000028    "99.pih"   */
242 };
243
244
245 /*******************************************************************************
246  *
247  * FUNCTION:    AcpiDbExecuteTest
248  *
249  * PARAMETERS:  TypeArg         - Subcommand
250  *
251  * RETURN:      None
252  *
253  * DESCRIPTION: Execute various debug tests.
254  *
255  * Note: Code is prepared for future expansion of the TEST command.
256  *
257  ******************************************************************************/
258
259 void
260 AcpiDbExecuteTest (
261     char                    *TypeArg)
262 {
263     UINT32                  Temp;
264
265
266     AcpiUtStrupr (TypeArg);
267     Temp = AcpiDbMatchArgument (TypeArg, AcpiDbTestTypes);
268     if (Temp == ACPI_TYPE_NOT_FOUND)
269     {
270         AcpiOsPrintf ("Invalid or unsupported argument\n");
271         return;
272     }
273
274     switch (Temp)
275     {
276     case CMD_TEST_OBJECTS:
277
278         AcpiDbTestAllObjects ();
279         break;
280
281     case CMD_TEST_PREDEFINED:
282
283         AcpiDbEvaluateAllPredefinedNames (NULL);
284         break;
285
286     default:
287         break;
288     }
289 }
290
291
292 /*******************************************************************************
293  *
294  * FUNCTION:    AcpiDbTestAllObjects
295  *
296  * PARAMETERS:  None
297  *
298  * RETURN:      None
299  *
300  * DESCRIPTION: This test implements the OBJECTS subcommand. It exercises the
301  *              namespace by reading/writing/comparing all data objects such
302  *              as integers, strings, buffers, fields, buffer fields, etc.
303  *
304  ******************************************************************************/
305
306 static void
307 AcpiDbTestAllObjects (
308     void)
309 {
310     ACPI_STATUS             Status;
311
312
313     /* Install the debugger read-object control method if necessary */
314
315     if (!ReadHandle)
316     {
317         Status = AcpiInstallMethod (ReadMethodCode);
318         if (ACPI_FAILURE (Status))
319         {
320             AcpiOsPrintf ("%s, Could not install debugger read method\n",
321                 AcpiFormatException (Status));
322             return;
323         }
324
325         Status = AcpiGetHandle (NULL, ACPI_DB_READ_METHOD, &ReadHandle);
326         if (ACPI_FAILURE (Status))
327         {
328             AcpiOsPrintf ("Could not obtain handle for debug method %s\n",
329                 ACPI_DB_READ_METHOD);
330             return;
331         }
332     }
333
334     /* Install the debugger write-object control method if necessary */
335
336     if (!WriteHandle)
337     {
338         Status = AcpiInstallMethod (WriteMethodCode);
339         if (ACPI_FAILURE (Status))
340         {
341             AcpiOsPrintf ("%s, Could not install debugger write method\n",
342                 AcpiFormatException (Status));
343             return;
344         }
345
346         Status = AcpiGetHandle (NULL, ACPI_DB_WRITE_METHOD, &WriteHandle);
347         if (ACPI_FAILURE (Status))
348         {
349             AcpiOsPrintf ("Could not obtain handle for debug method %s\n",
350                 ACPI_DB_WRITE_METHOD);
351             return;
352         }
353     }
354
355     /* Walk the entire namespace, testing each supported named data object */
356
357     (void) AcpiWalkNamespace (ACPI_TYPE_ANY, ACPI_ROOT_OBJECT,
358                 ACPI_UINT32_MAX, AcpiDbTestOneObject, NULL, NULL, NULL);
359 }
360
361
362 /*******************************************************************************
363  *
364  * FUNCTION:    AcpiDbTestOneObject
365  *
366  * PARAMETERS:  ACPI_WALK_CALLBACK
367  *
368  * RETURN:      Status
369  *
370  * DESCRIPTION: Test one namespace object. Supported types are Integer,
371  *              String, Buffer, BufferField, and FieldUnit. All other object
372  *              types are simply ignored.
373  *
374  *              Note: Support for Packages is not implemented.
375  *
376  ******************************************************************************/
377
378 static ACPI_STATUS
379 AcpiDbTestOneObject (
380     ACPI_HANDLE             ObjHandle,
381     UINT32                  NestingLevel,
382     void                    *Context,
383     void                    **ReturnValue)
384 {
385     ACPI_NAMESPACE_NODE     *Node;
386     ACPI_OPERAND_OBJECT     *ObjDesc;
387     ACPI_OPERAND_OBJECT     *RegionObj;
388     ACPI_OBJECT_TYPE        LocalType;
389     UINT32                  BitLength = 0;
390     UINT32                  ByteLength = 0;
391     ACPI_STATUS             Status = AE_OK;
392
393
394     Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, ObjHandle);
395     ObjDesc = Node->Object;
396
397     /*
398      * For the supported types, get the actual bit length or
399      * byte length. Map the type to one of Integer/String/Buffer.
400      */
401     switch (Node->Type)
402     {
403     case ACPI_TYPE_INTEGER:
404
405         /* Integer width is either 32 or 64 */
406
407         LocalType = ACPI_TYPE_INTEGER;
408         BitLength = AcpiGbl_IntegerBitWidth;
409         break;
410
411     case ACPI_TYPE_STRING:
412
413         LocalType = ACPI_TYPE_STRING;
414         ByteLength = ObjDesc->String.Length;
415         break;
416
417     case ACPI_TYPE_BUFFER:
418
419         LocalType = ACPI_TYPE_BUFFER;
420         ByteLength = ObjDesc->Buffer.Length;
421         BitLength = ByteLength * 8;
422         break;
423
424     case ACPI_TYPE_FIELD_UNIT:
425     case ACPI_TYPE_BUFFER_FIELD:
426     case ACPI_TYPE_LOCAL_REGION_FIELD:
427     case ACPI_TYPE_LOCAL_INDEX_FIELD:
428     case ACPI_TYPE_LOCAL_BANK_FIELD:
429
430         LocalType = ACPI_TYPE_INTEGER;
431         if (ObjDesc)
432         {
433             /*
434              * Returned object will be a Buffer if the field length
435              * is larger than the size of an Integer (32 or 64 bits
436              * depending on the DSDT version).
437              */
438             BitLength = ObjDesc->CommonField.BitLength;
439             ByteLength = ACPI_ROUND_BITS_UP_TO_BYTES (BitLength);
440             if (BitLength > AcpiGbl_IntegerBitWidth)
441             {
442                 LocalType = ACPI_TYPE_BUFFER;
443             }
444         }
445         break;
446
447     default:
448
449         /* Ignore all other types */
450
451         return (AE_OK);
452     }
453
454     /* Emit the common prefix: Type:Name */
455
456     AcpiOsPrintf ("%14s: %4.4s",
457         AcpiUtGetTypeName (Node->Type), Node->Name.Ascii);
458     if (!ObjDesc)
459     {
460         AcpiOsPrintf (" Ignoring, no attached object\n");
461         return (AE_OK);
462     }
463
464     /*
465      * Check for unsupported region types. Note: AcpiExec simulates
466      * access to SystemMemory, SystemIO, PCI_Config, and EC.
467      */
468     switch (Node->Type)
469     {
470     case ACPI_TYPE_LOCAL_REGION_FIELD:
471
472         RegionObj = ObjDesc->Field.RegionObj;
473         switch (RegionObj->Region.SpaceId)
474         {
475         case ACPI_ADR_SPACE_SYSTEM_MEMORY:
476         case ACPI_ADR_SPACE_SYSTEM_IO:
477         case ACPI_ADR_SPACE_PCI_CONFIG:
478         case ACPI_ADR_SPACE_EC:
479
480             break;
481
482         default:
483
484             AcpiOsPrintf ("      %s space is not supported [%4.4s]\n",
485                 AcpiUtGetRegionName (RegionObj->Region.SpaceId),
486                 RegionObj->Region.Node->Name.Ascii);
487             return (AE_OK);
488         }
489         break;
490
491     default:
492         break;
493     }
494
495     /* At this point, we have resolved the object to one of the major types */
496
497     switch (LocalType)
498     {
499     case ACPI_TYPE_INTEGER:
500
501         Status = AcpiDbTestIntegerType (Node, BitLength);
502         break;
503
504     case ACPI_TYPE_STRING:
505
506         Status = AcpiDbTestStringType (Node, ByteLength);
507         break;
508
509     case ACPI_TYPE_BUFFER:
510
511         Status = AcpiDbTestBufferType (Node, BitLength);
512         break;
513
514     default:
515
516         AcpiOsPrintf (" Ignoring, type not implemented (%2.2X)",
517             LocalType);
518         break;
519     }
520
521     switch (Node->Type)
522     {
523     case ACPI_TYPE_LOCAL_REGION_FIELD:
524
525         RegionObj = ObjDesc->Field.RegionObj;
526         AcpiOsPrintf (" (%s)",
527             AcpiUtGetRegionName (RegionObj->Region.SpaceId));
528         break;
529
530     default:
531         break;
532     }
533
534     AcpiOsPrintf ("\n");
535     return (Status);
536 }
537
538
539 /*******************************************************************************
540  *
541  * FUNCTION:    AcpiDbTestIntegerType
542  *
543  * PARAMETERS:  Node                - Parent NS node for the object
544  *              BitLength           - Actual length of the object. Used for
545  *                                    support of arbitrary length FieldUnit
546  *                                    and BufferField objects.
547  *
548  * RETURN:      Status
549  *
550  * DESCRIPTION: Test read/write for an Integer-valued object. Performs a
551  *              write/read/compare of an arbitrary new value, then performs
552  *              a write/read/compare of the original value.
553  *
554  ******************************************************************************/
555
556 static ACPI_STATUS
557 AcpiDbTestIntegerType (
558     ACPI_NAMESPACE_NODE     *Node,
559     UINT32                  BitLength)
560 {
561     ACPI_OBJECT             *Temp1 = NULL;
562     ACPI_OBJECT             *Temp2 = NULL;
563     ACPI_OBJECT             *Temp3 = NULL;
564     ACPI_OBJECT             WriteValue;
565     UINT64                  ValueToWrite;
566     ACPI_STATUS             Status;
567
568
569     if (BitLength > 64)
570     {
571         AcpiOsPrintf (" Invalid length for an Integer: %u", BitLength);
572         return (AE_OK);
573     }
574
575     /* Read the original value */
576
577     Status = AcpiDbReadFromObject (Node, ACPI_TYPE_INTEGER, &Temp1);
578     if (ACPI_FAILURE (Status))
579     {
580         return (Status);
581     }
582
583     AcpiOsPrintf (" (%4.4X/%3.3X) %8.8X%8.8X",
584         BitLength, ACPI_ROUND_BITS_UP_TO_BYTES (BitLength),
585         ACPI_FORMAT_UINT64 (Temp1->Integer.Value));
586
587     ValueToWrite = ACPI_UINT64_MAX >> (64 - BitLength);
588     if (Temp1->Integer.Value == ValueToWrite)
589     {
590         ValueToWrite = 0;
591     }
592
593     /* Write a new value */
594
595     WriteValue.Type = ACPI_TYPE_INTEGER;
596     WriteValue.Integer.Value = ValueToWrite;
597     Status = AcpiDbWriteToObject (Node, &WriteValue);
598     if (ACPI_FAILURE (Status))
599     {
600         goto Exit;
601     }
602
603     /* Ensure that we can read back the new value */
604
605     Status = AcpiDbReadFromObject (Node, ACPI_TYPE_INTEGER, &Temp2);
606     if (ACPI_FAILURE (Status))
607     {
608         goto Exit;
609     }
610
611     if (Temp2->Integer.Value != ValueToWrite)
612     {
613         AcpiOsPrintf (" MISMATCH 2: %8.8X%8.8X, expecting %8.8X%8.8X",
614             ACPI_FORMAT_UINT64 (Temp2->Integer.Value),
615             ACPI_FORMAT_UINT64 (ValueToWrite));
616     }
617
618     /* Write back the original value */
619
620     WriteValue.Integer.Value = Temp1->Integer.Value;
621     Status = AcpiDbWriteToObject (Node, &WriteValue);
622     if (ACPI_FAILURE (Status))
623     {
624         goto Exit;
625     }
626
627     /* Ensure that we can read back the original value */
628
629     Status = AcpiDbReadFromObject (Node, ACPI_TYPE_INTEGER, &Temp3);
630     if (ACPI_FAILURE (Status))
631     {
632         goto Exit;
633     }
634
635     if (Temp3->Integer.Value != Temp1->Integer.Value)
636     {
637         AcpiOsPrintf (" MISMATCH 3: %8.8X%8.8X, expecting %8.8X%8.8X",
638             ACPI_FORMAT_UINT64 (Temp3->Integer.Value),
639             ACPI_FORMAT_UINT64 (Temp1->Integer.Value));
640     }
641
642 Exit:
643     if (Temp1) {AcpiOsFree (Temp1);}
644     if (Temp2) {AcpiOsFree (Temp2);}
645     if (Temp3) {AcpiOsFree (Temp3);}
646     return (AE_OK);
647 }
648
649
650 /*******************************************************************************
651  *
652  * FUNCTION:    AcpiDbTestBufferType
653  *
654  * PARAMETERS:  Node                - Parent NS node for the object
655  *              BitLength           - Actual length of the object.
656  *
657  * RETURN:      Status
658  *
659  * DESCRIPTION: Test read/write for an Buffer-valued object. Performs a
660  *              write/read/compare of an arbitrary new value, then performs
661  *              a write/read/compare of the original value.
662  *
663  ******************************************************************************/
664
665 static ACPI_STATUS
666 AcpiDbTestBufferType (
667     ACPI_NAMESPACE_NODE     *Node,
668     UINT32                  BitLength)
669 {
670     ACPI_OBJECT             *Temp1 = NULL;
671     ACPI_OBJECT             *Temp2 = NULL;
672     ACPI_OBJECT             *Temp3 = NULL;
673     UINT8                   *Buffer;
674     ACPI_OBJECT             WriteValue;
675     ACPI_STATUS             Status;
676     UINT32                  ByteLength;
677     UINT32                  i;
678     UINT8                   ExtraBits;
679
680
681     ByteLength = ACPI_ROUND_BITS_UP_TO_BYTES (BitLength);
682     if (ByteLength == 0)
683     {
684         AcpiOsPrintf (" Ignoring zero length buffer");
685         return (AE_OK);
686     }
687
688     /* Allocate a local buffer */
689
690     Buffer = ACPI_ALLOCATE_ZEROED (ByteLength);
691     if (!Buffer)
692     {
693         return (AE_NO_MEMORY);
694     }
695
696     /* Read the original value */
697
698     Status = AcpiDbReadFromObject (Node, ACPI_TYPE_BUFFER, &Temp1);
699     if (ACPI_FAILURE (Status))
700     {
701         goto Exit;
702     }
703
704     /* Emit a few bytes of the buffer */
705
706     AcpiOsPrintf (" (%4.4X/%3.3X)", BitLength, Temp1->Buffer.Length);
707     for (i = 0; ((i < 4) && (i < ByteLength)); i++)
708     {
709         AcpiOsPrintf (" %2.2X", Temp1->Buffer.Pointer[i]);
710     }
711     AcpiOsPrintf ("...  ");
712
713     /*
714      * Write a new value.
715      *
716      * Handle possible extra bits at the end of the buffer. Can
717      * happen for FieldUnits larger than an integer, but the bit
718      * count is not an integral number of bytes. Zero out the
719      * unused bits.
720      */
721     ACPI_MEMSET (Buffer, BUFFER_FILL_VALUE, ByteLength);
722     ExtraBits = BitLength % 8;
723     if (ExtraBits)
724     {
725         Buffer [ByteLength - 1] = ACPI_MASK_BITS_ABOVE (ExtraBits);
726     }
727
728     WriteValue.Type = ACPI_TYPE_BUFFER;
729     WriteValue.Buffer.Length = ByteLength;
730     WriteValue.Buffer.Pointer = Buffer;
731
732     Status = AcpiDbWriteToObject (Node, &WriteValue);
733     if (ACPI_FAILURE (Status))
734     {
735         goto Exit;
736     }
737
738     /* Ensure that we can read back the new value */
739
740     Status = AcpiDbReadFromObject (Node, ACPI_TYPE_BUFFER, &Temp2);
741     if (ACPI_FAILURE (Status))
742     {
743         goto Exit;
744     }
745
746     if (ACPI_MEMCMP (Temp2->Buffer.Pointer, Buffer, ByteLength))
747     {
748         AcpiOsPrintf (" MISMATCH 2: New buffer value");
749     }
750
751     /* Write back the original value */
752
753     WriteValue.Buffer.Length = ByteLength;
754     WriteValue.Buffer.Pointer = Temp1->Buffer.Pointer;
755
756     Status = AcpiDbWriteToObject (Node, &WriteValue);
757     if (ACPI_FAILURE (Status))
758     {
759         goto Exit;
760     }
761
762     /* Ensure that we can read back the original value */
763
764     Status = AcpiDbReadFromObject (Node, ACPI_TYPE_BUFFER, &Temp3);
765     if (ACPI_FAILURE (Status))
766     {
767         goto Exit;
768     }
769
770     if (ACPI_MEMCMP (Temp1->Buffer.Pointer, Temp3->Buffer.Pointer, ByteLength))
771     {
772         AcpiOsPrintf (" MISMATCH 3: While restoring original buffer");
773     }
774
775 Exit:
776     ACPI_FREE (Buffer);
777     if (Temp1) {AcpiOsFree (Temp1);}
778     if (Temp2) {AcpiOsFree (Temp2);}
779     if (Temp3) {AcpiOsFree (Temp3);}
780     return (Status);
781 }
782
783
784 /*******************************************************************************
785  *
786  * FUNCTION:    AcpiDbTestStringType
787  *
788  * PARAMETERS:  Node                - Parent NS node for the object
789  *              ByteLength          - Actual length of the object.
790  *
791  * RETURN:      Status
792  *
793  * DESCRIPTION: Test read/write for an String-valued object. Performs a
794  *              write/read/compare of an arbitrary new value, then performs
795  *              a write/read/compare of the original value.
796  *
797  ******************************************************************************/
798
799 static ACPI_STATUS
800 AcpiDbTestStringType (
801     ACPI_NAMESPACE_NODE     *Node,
802     UINT32                  ByteLength)
803 {
804     ACPI_OBJECT             *Temp1 = NULL;
805     ACPI_OBJECT             *Temp2 = NULL;
806     ACPI_OBJECT             *Temp3 = NULL;
807     char                    *ValueToWrite = "Test String from AML Debugger";
808     ACPI_OBJECT             WriteValue;
809     ACPI_STATUS             Status;
810
811
812     /* Read the original value */
813
814     Status = AcpiDbReadFromObject (Node, ACPI_TYPE_STRING, &Temp1);
815     if (ACPI_FAILURE (Status))
816     {
817         return (Status);
818     }
819
820     AcpiOsPrintf (" (%4.4X/%3.3X) \"%s\"", (Temp1->String.Length * 8),
821         Temp1->String.Length, Temp1->String.Pointer);
822
823     /* Write a new value */
824
825     WriteValue.Type = ACPI_TYPE_STRING;
826     WriteValue.String.Length = ACPI_STRLEN (ValueToWrite);
827     WriteValue.String.Pointer = ValueToWrite;
828
829     Status = AcpiDbWriteToObject (Node, &WriteValue);
830     if (ACPI_FAILURE (Status))
831     {
832         goto Exit;
833     }
834
835     /* Ensure that we can read back the new value */
836
837     Status = AcpiDbReadFromObject (Node, ACPI_TYPE_STRING, &Temp2);
838     if (ACPI_FAILURE (Status))
839     {
840         goto Exit;
841     }
842
843     if (ACPI_STRCMP (Temp2->String.Pointer, ValueToWrite))
844     {
845         AcpiOsPrintf (" MISMATCH 2: %s, expecting %s",
846             Temp2->String.Pointer, ValueToWrite);
847     }
848
849     /* Write back the original value */
850
851     WriteValue.String.Length = ACPI_STRLEN (Temp1->String.Pointer);
852     WriteValue.String.Pointer = Temp1->String.Pointer;
853
854     Status = AcpiDbWriteToObject (Node, &WriteValue);
855     if (ACPI_FAILURE (Status))
856     {
857         goto Exit;
858     }
859
860     /* Ensure that we can read back the original value */
861
862     Status = AcpiDbReadFromObject (Node, ACPI_TYPE_STRING, &Temp3);
863     if (ACPI_FAILURE (Status))
864     {
865         goto Exit;
866     }
867
868     if (ACPI_STRCMP (Temp1->String.Pointer, Temp3->String.Pointer))
869     {
870         AcpiOsPrintf (" MISMATCH 3: %s, expecting %s",
871             Temp3->String.Pointer, Temp1->String.Pointer);
872     }
873
874 Exit:
875     if (Temp1) {AcpiOsFree (Temp1);}
876     if (Temp2) {AcpiOsFree (Temp2);}
877     if (Temp3) {AcpiOsFree (Temp3);}
878     return (Status);
879 }
880
881
882 /*******************************************************************************
883  *
884  * FUNCTION:    AcpiDbReadFromObject
885  *
886  * PARAMETERS:  Node                - Parent NS node for the object
887  *              ExpectedType        - Object type expected from the read
888  *              Value               - Where the value read is returned
889  *
890  * RETURN:      Status
891  *
892  * DESCRIPTION: Performs a read from the specified object by invoking the
893  *              special debugger control method that reads the object. Thus,
894  *              the AML interpreter is doing all of the work, increasing the
895  *              validity of the test.
896  *
897  ******************************************************************************/
898
899 static ACPI_STATUS
900 AcpiDbReadFromObject (
901     ACPI_NAMESPACE_NODE     *Node,
902     ACPI_OBJECT_TYPE        ExpectedType,
903     ACPI_OBJECT             **Value)
904 {
905     ACPI_OBJECT             *RetValue;
906     ACPI_OBJECT_LIST        ParamObjects;
907     ACPI_OBJECT             Params[2];
908     ACPI_BUFFER             ReturnObj;
909     ACPI_STATUS             Status;
910
911
912     Params[0].Type = ACPI_TYPE_LOCAL_REFERENCE;
913     Params[0].Reference.ActualType = Node->Type;
914     Params[0].Reference.Handle = ACPI_CAST_PTR (ACPI_HANDLE, Node);
915
916     ParamObjects.Count = 1;
917     ParamObjects.Pointer = Params;
918
919     ReturnObj.Length  = ACPI_ALLOCATE_BUFFER;
920
921     AcpiGbl_MethodExecuting = TRUE;
922     Status = AcpiEvaluateObject (ReadHandle, NULL, &ParamObjects, &ReturnObj);
923     AcpiGbl_MethodExecuting = FALSE;
924
925     if (ACPI_FAILURE (Status))
926     {
927         AcpiOsPrintf ("Could not read from object, %s",
928             AcpiFormatException (Status));
929         return (Status);
930     }
931
932     RetValue = (ACPI_OBJECT *) ReturnObj.Pointer;
933
934     switch (RetValue->Type)
935     {
936     case ACPI_TYPE_INTEGER:
937     case ACPI_TYPE_BUFFER:
938     case ACPI_TYPE_STRING:
939         /*
940          * Did we receive the type we wanted? Most important for the
941          * Integer/Buffer case (when a field is larger than an Integer,
942          * it should return a Buffer).
943          */
944         if (RetValue->Type != ExpectedType)
945         {
946             AcpiOsPrintf (" Type mismatch:  Expected %s, Received %s",
947                 AcpiUtGetTypeName (ExpectedType),
948                 AcpiUtGetTypeName (RetValue->Type));
949
950             return (AE_TYPE);
951         }
952
953         *Value = RetValue;
954         break;
955
956     default:
957
958         AcpiOsPrintf (" Unsupported return object type, %s",
959             AcpiUtGetTypeName (RetValue->Type));
960         AcpiOsFree (ReturnObj.Pointer);
961
962         return (AE_TYPE);
963     }
964
965     return (Status);
966 }
967
968
969 /*******************************************************************************
970  *
971  * FUNCTION:    AcpiDbWriteToObject
972  *
973  * PARAMETERS:  Node                - Parent NS node for the object
974  *              Value               - Value to be written
975  *
976  * RETURN:      Status
977  *
978  * DESCRIPTION: Performs a write to the specified object by invoking the
979  *              special debugger control method that writes the object. Thus,
980  *              the AML interpreter is doing all of the work, increasing the
981  *              validity of the test.
982  *
983  ******************************************************************************/
984
985 static ACPI_STATUS
986 AcpiDbWriteToObject (
987     ACPI_NAMESPACE_NODE     *Node,
988     ACPI_OBJECT             *Value)
989 {
990     ACPI_OBJECT_LIST        ParamObjects;
991     ACPI_OBJECT             Params[2];
992     ACPI_STATUS             Status;
993
994
995     Params[0].Type = ACPI_TYPE_LOCAL_REFERENCE;
996     Params[0].Reference.ActualType = Node->Type;
997     Params[0].Reference.Handle = ACPI_CAST_PTR (ACPI_HANDLE, Node);
998
999     /* Copy the incoming user parameter */
1000
1001     ACPI_MEMCPY (&Params[1], Value, sizeof (ACPI_OBJECT));
1002
1003     ParamObjects.Count = 2;
1004     ParamObjects.Pointer = Params;
1005
1006     AcpiGbl_MethodExecuting = TRUE;
1007     Status = AcpiEvaluateObject (WriteHandle, NULL, &ParamObjects, NULL);
1008     AcpiGbl_MethodExecuting = FALSE;
1009
1010     if (ACPI_FAILURE (Status))
1011     {
1012         AcpiOsPrintf ("Could not write to object, %s",
1013             AcpiFormatException (Status));
1014     }
1015
1016     return (Status);
1017 }
1018
1019
1020 /*******************************************************************************
1021  *
1022  * FUNCTION:    AcpiDbEvaluateAllPredefinedNames
1023  *
1024  * PARAMETERS:  CountArg            - Max number of methods to execute
1025  *
1026  * RETURN:      None
1027  *
1028  * DESCRIPTION: Namespace batch execution. Execute predefined names in the
1029  *              namespace, up to the max count, if specified.
1030  *
1031  ******************************************************************************/
1032
1033 static void
1034 AcpiDbEvaluateAllPredefinedNames (
1035     char                    *CountArg)
1036 {
1037     ACPI_DB_EXECUTE_WALK    Info;
1038
1039
1040     Info.Count = 0;
1041     Info.MaxCount = ACPI_UINT32_MAX;
1042
1043     if (CountArg)
1044     {
1045         Info.MaxCount = ACPI_STRTOUL (CountArg, NULL, 0);
1046     }
1047
1048     /* Search all nodes in namespace */
1049
1050     (void) AcpiWalkNamespace (ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
1051                 AcpiDbEvaluateOnePredefinedName, NULL, (void *) &Info, NULL);
1052
1053     AcpiOsPrintf ("Evaluated %u predefined names in the namespace\n", Info.Count);
1054 }
1055
1056
1057 /*******************************************************************************
1058  *
1059  * FUNCTION:    AcpiDbEvaluateOnePredefinedName
1060  *
1061  * PARAMETERS:  Callback from WalkNamespace
1062  *
1063  * RETURN:      Status
1064  *
1065  * DESCRIPTION: Batch execution module. Currently only executes predefined
1066  *              ACPI names.
1067  *
1068  ******************************************************************************/
1069
1070 static ACPI_STATUS
1071 AcpiDbEvaluateOnePredefinedName (
1072     ACPI_HANDLE             ObjHandle,
1073     UINT32                  NestingLevel,
1074     void                    *Context,
1075     void                    **ReturnValue)
1076 {
1077     ACPI_NAMESPACE_NODE         *Node = (ACPI_NAMESPACE_NODE *) ObjHandle;
1078     ACPI_DB_EXECUTE_WALK        *Info = (ACPI_DB_EXECUTE_WALK *) Context;
1079     char                        *Pathname;
1080     const ACPI_PREDEFINED_INFO  *Predefined;
1081     ACPI_DEVICE_INFO            *ObjInfo;
1082     ACPI_OBJECT_LIST            ParamObjects;
1083     ACPI_OBJECT                 Params[ACPI_METHOD_NUM_ARGS];
1084     ACPI_OBJECT                 *ThisParam;
1085     ACPI_BUFFER                 ReturnObj;
1086     ACPI_STATUS                 Status;
1087     UINT16                      ArgTypeList;
1088     UINT8                       ArgCount;
1089     UINT8                       ArgType;
1090     UINT32                      i;
1091
1092
1093     /* The name must be a predefined ACPI name */
1094
1095     Predefined = AcpiUtMatchPredefinedMethod (Node->Name.Ascii);
1096     if (!Predefined)
1097     {
1098         return (AE_OK);
1099     }
1100
1101     if (Node->Type == ACPI_TYPE_LOCAL_SCOPE)
1102     {
1103         return (AE_OK);
1104     }
1105
1106     Pathname = AcpiNsGetExternalPathname (Node);
1107     if (!Pathname)
1108     {
1109         return (AE_OK);
1110     }
1111
1112     /* Get the object info for number of method parameters */
1113
1114     Status = AcpiGetObjectInfo (ObjHandle, &ObjInfo);
1115     if (ACPI_FAILURE (Status))
1116     {
1117         ACPI_FREE (Pathname);
1118         return (Status);
1119     }
1120
1121     ParamObjects.Count = 0;
1122     ParamObjects.Pointer = NULL;
1123
1124     if (ObjInfo->Type == ACPI_TYPE_METHOD)
1125     {
1126         /* Setup default parameters (with proper types) */
1127
1128         ArgTypeList = Predefined->Info.ArgumentList;
1129         ArgCount = METHOD_GET_ARG_COUNT (ArgTypeList);
1130
1131         /*
1132          * Setup the ACPI-required number of arguments, regardless of what
1133          * the actual method defines. If there is a difference, then the
1134          * method is wrong and a warning will be issued during execution.
1135          */
1136         ThisParam = Params;
1137         for (i = 0; i < ArgCount; i++)
1138         {
1139             ArgType = METHOD_GET_NEXT_TYPE (ArgTypeList);
1140             ThisParam->Type = ArgType;
1141
1142             switch (ArgType)
1143             {
1144             case ACPI_TYPE_INTEGER:
1145
1146                 ThisParam->Integer.Value = 1;
1147                 break;
1148
1149             case ACPI_TYPE_STRING:
1150
1151                 ThisParam->String.Pointer = "This is the default argument string";
1152                 ThisParam->String.Length = ACPI_STRLEN (ThisParam->String.Pointer);
1153                 break;
1154
1155             case ACPI_TYPE_BUFFER:
1156
1157                 ThisParam->Buffer.Pointer = (UINT8 *) Params; /* just a garbage buffer */
1158                 ThisParam->Buffer.Length = 48;
1159                 break;
1160
1161              case ACPI_TYPE_PACKAGE:
1162
1163                 ThisParam->Package.Elements = NULL;
1164                 ThisParam->Package.Count = 0;
1165                 break;
1166
1167            default:
1168
1169                 AcpiOsPrintf ("%s: Unsupported argument type: %u\n",
1170                     Pathname, ArgType);
1171                 break;
1172             }
1173
1174             ThisParam++;
1175         }
1176
1177         ParamObjects.Count = ArgCount;
1178         ParamObjects.Pointer = Params;
1179     }
1180
1181     ACPI_FREE (ObjInfo);
1182     ReturnObj.Pointer = NULL;
1183     ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
1184
1185     /* Do the actual method execution */
1186
1187     AcpiGbl_MethodExecuting = TRUE;
1188
1189     Status = AcpiEvaluateObject (Node, NULL, &ParamObjects, &ReturnObj);
1190
1191     AcpiOsPrintf ("%-32s returned %s\n", Pathname, AcpiFormatException (Status));
1192     AcpiGbl_MethodExecuting = FALSE;
1193     ACPI_FREE (Pathname);
1194
1195     /* Ignore status from method execution */
1196
1197     Status = AE_OK;
1198
1199     /* Update count, check if we have executed enough methods */
1200
1201     Info->Count++;
1202     if (Info->Count >= Info->MaxCount)
1203     {
1204         Status = AE_CTRL_TERMINATE;
1205     }
1206
1207     return (Status);
1208 }
1209
1210 #endif /* ACPI_DEBUGGER */