1 /******************************************************************************
2  *
3  * Module Name: ahdecode - Miscellaneous decoding for acpihelp utility
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2017, 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  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #define ACPI_CREATE_PREDEFINED_TABLE
153 #define ACPI_CREATE_RESOURCE_TABLE
154 
155 #include "acpihelp.h"
156 #include "acpredef.h"
157 
158 
159 /* Local prototypes */
160 
161 static BOOLEAN
162 AhDisplayPredefinedName (
163     char                    *Name,
164     UINT32                  Length);
165 
166 static void
167 AhDisplayPredefinedInfo (
168     char                    *Name);
169 
170 static void
171 AhDisplayResourceName (
172     const ACPI_PREDEFINED_INFO  *ThisName);
173 
174 
175 /*******************************************************************************
176  *
177  * FUNCTION:    AhPrintOneField
178  *
179  * PARAMETERS:  Indent              - Indent length for new line(s)
180  *              CurrentPosition     - Position on current line
181  *              MaxPosition         - Max allowed line length
182  *              Field               - Data to output
183  *
184  * RETURN:      Line position after field is written
185  *
186  * DESCRIPTION: Split long lines appropriately for ease of reading.
187  *
188  ******************************************************************************/
189 
190 void
191 AhPrintOneField (
192     UINT32                  Indent,
193     UINT32                  CurrentPosition,
194     UINT32                  MaxPosition,
195     const char              *Field)
196 {
197     UINT32                  Position;
198     UINT32                  TokenLength;
199     const char              *This;
200     const char              *Next;
201     const char              *Last;
202 
203 
204     This = Field;
205     Position = CurrentPosition;
206 
207     if (Position == 0)
208     {
209         printf ("%*s", (int) Indent, " ");
210         Position = Indent;
211     }
212 
213     Last = This + strlen (This);
214     while ((Next = strpbrk (This, " ")))
215     {
216         TokenLength = Next - This;
217         Position += TokenLength;
218 
219         /* Split long lines */
220 
221         if (Position > MaxPosition)
222         {
223             printf ("\n%*s", (int) Indent, " ");
224             Position = TokenLength;
225         }
226 
227         printf ("%.*s ", (int) TokenLength, This);
228         This = Next + 1;
229     }
230 
231     /* Handle last token on the input line */
232 
233     TokenLength = Last - This;
234     if (TokenLength > 0)
235     {
236         Position += TokenLength;
237         if (Position > MaxPosition)
238         {
239             printf ("\n%*s", (int) Indent, " ");
240         }
241 
242         printf ("%s", This);
243     }
244 }
245 
246 
247 /*******************************************************************************
248  *
249  * FUNCTION:    AhDisplayDirectives
250  *
251  * PARAMETERS:  None
252  *
253  * RETURN:      None
254  *
255  * DESCRIPTION: Display all iASL preprocessor directives.
256  *
257  ******************************************************************************/
258 
259 void
260 AhDisplayDirectives (
261     void)
262 {
263     const AH_DIRECTIVE_INFO *Info;
264 
265 
266     printf ("iASL Preprocessor Directives\n\n");
267 
268     for (Info = Gbl_PreprocessorDirectives; Info->Name; Info++)
269     {
270         printf ("  %-36s : %s\n", Info->Name, Info->Description);
271     }
272 }
273 
274 
275 /*******************************************************************************
276  *
277  * FUNCTION:    AhFindPredefinedNames (entry point for predefined name search)
278  *
279  * PARAMETERS:  NamePrefix          - Name or prefix to find. Must start with
280  *                                    an underscore. NULL means "find all"
281  *
282  * RETURN:      None
283  *
284  * DESCRIPTION: Find and display all ACPI predefined names that match the
285  *              input name or prefix. Includes the required number of arguments
286  *              and the expected return type, if any.
287  *
288  ******************************************************************************/
289 
290 void
291 AhFindPredefinedNames (
292     char                    *NamePrefix)
293 {
294     UINT32                  Length;
295     BOOLEAN                 Found;
296     char                    Name[9];
297 
298 
299     if (!NamePrefix || (NamePrefix[0] == '*'))
300     {
301         Found = AhDisplayPredefinedName (NULL, 0);
302         return;
303     }
304 
305     /* Contruct a local name or name prefix */
306 
307     AcpiUtStrupr (NamePrefix);
308     if (*NamePrefix == '_')
309     {
310         NamePrefix++;
311     }
312 
313     Name[0] = '_';
314     strncpy (&Name[1], NamePrefix, 7);
315 
316     Length = strlen (Name);
317     if (Length > ACPI_NAME_SIZE)
318     {
319         printf ("%.8s: Predefined name must be 4 characters maximum\n", Name);
320         return;
321     }
322 
323     Found = AhDisplayPredefinedName (Name, Length);
324     if (!Found)
325     {
326         printf ("%s, no matching predefined names\n", Name);
327     }
328 }
329 
330 
331 /*******************************************************************************
332  *
333  * FUNCTION:    AhDisplayPredefinedName
334  *
335  * PARAMETERS:  Name                - Name or name prefix
336  *
337  * RETURN:      TRUE if any names matched, FALSE otherwise
338  *
339  * DESCRIPTION: Display information about ACPI predefined names that match
340  *              the input name or name prefix.
341  *
342  ******************************************************************************/
343 
344 static BOOLEAN
345 AhDisplayPredefinedName (
346     char                    *Name,
347     UINT32                  Length)
348 {
349     const AH_PREDEFINED_NAME    *Info;
350     BOOLEAN                     Found = FALSE;
351     BOOLEAN                     Matched;
352     UINT32                      i = 0;
353 
354 
355     /* Find/display all names that match the input name prefix */
356 
357     for (Info = AslPredefinedInfo; Info->Name; Info++)
358     {
359         if (!Name)
360         {
361             Found = TRUE;
362             printf ("%s: <%s>\n", Info->Name, Info->Description);
363             printf ("%*s%s\n", 6, " ", Info->Action);
364 
365             AhDisplayPredefinedInfo (Info->Name);
366             i++;
367             continue;
368         }
369 
370         Matched = TRUE;
371         for (i = 0; i < Length; i++)
372         {
373             if (Info->Name[i] != Name[i])
374             {
375                 Matched = FALSE;
376                 break;
377             }
378         }
379 
380         if (Matched)
381         {
382             Found = TRUE;
383             printf ("%s: <%s>\n", Info->Name, Info->Description);
384             printf ("%*s%s\n", 6, " ", Info->Action);
385 
386             AhDisplayPredefinedInfo (Info->Name);
387         }
388     }
389 
390     if (!Name)
391     {
392         printf ("\nFound %d Predefined ACPI Names\n", i);
393     }
394     return (Found);
395 }
396 
397 
398 /*******************************************************************************
399  *
400  * FUNCTION:    AhDisplayPredefinedInfo
401  *
402  * PARAMETERS:  Name                - Exact 4-character ACPI name.
403  *
404  * RETURN:      None
405  *
406  * DESCRIPTION: Find the name in the main ACPICA predefined info table and
407  *              display the # of arguments and the return value type.
408  *
409  *              Note: Resource Descriptor field names do not appear in this
410  *              table -- thus, nothing will be displayed for them.
411  *
412  ******************************************************************************/
413 
414 static void
415 AhDisplayPredefinedInfo (
416     char                        *Name)
417 {
418     const ACPI_PREDEFINED_INFO  *ThisName;
419 
420 
421     /* NOTE: we check both tables always because there are some dupes */
422 
423     /* Check against the predefine methods first */
424 
425     ThisName = AcpiUtMatchPredefinedMethod (Name);
426     if (ThisName)
427     {
428         AcpiUtDisplayPredefinedMethod (Gbl_Buffer, ThisName, TRUE);
429     }
430 
431     /* Check against the predefined resource descriptor names */
432 
433     ThisName = AcpiUtMatchResourceName (Name);
434     if (ThisName)
435     {
436         AhDisplayResourceName (ThisName);
437     }
438 }
439 
440 
441 /*******************************************************************************
442  *
443  * FUNCTION:    AhDisplayResourceName
444  *
445  * PARAMETERS:  ThisName            - Entry in the predefined method/name table
446  *
447  * RETURN:      None
448  *
449  * DESCRIPTION: Display information about a resource descriptor name.
450  *
451  ******************************************************************************/
452 
453 static void
454 AhDisplayResourceName (
455     const ACPI_PREDEFINED_INFO  *ThisName)
456 {
457     UINT32                      NumTypes;
458 
459 
460     NumTypes = AcpiUtGetResourceBitWidth (Gbl_Buffer,
461         ThisName->Info.ArgumentList);
462 
463     printf ("      %4.4s resource descriptor field is %s bits wide%s\n",
464         ThisName->Info.Name,
465         Gbl_Buffer,
466         (NumTypes > 1) ? " (depending on descriptor type)" : "");
467 }
468 
469 
470 /*******************************************************************************
471  *
472  * FUNCTION:    AhDisplayDeviceIds
473  *
474  * PARAMETERS:  Name                - Device Hardware ID string.
475  *                                    NULL means "find all"
476  *
477  * RETURN:      None
478  *
479  * DESCRIPTION: Display PNP* and ACPI* device IDs.
480  *
481  ******************************************************************************/
482 
483 void
484 AhDisplayDeviceIds (
485     char                    *Name)
486 {
487     const AH_DEVICE_ID      *Info;
488     UINT32                  Length;
489     BOOLEAN                 Matched;
490     UINT32                  i;
491     BOOLEAN                 Found = FALSE;
492 
493 
494     /* Null input name indicates "display all" */
495 
496     if (!Name || (Name[0] == '*'))
497     {
498         printf ("ACPI and PNP Device/Hardware IDs:\n\n");
499         for (Info = AslDeviceIds; Info->Name; Info++)
500         {
501             printf ("%8s   %s\n", Info->Name, Info->Description);
502         }
503 
504         return;
505     }
506 
507     Length = strlen (Name);
508     if (Length > 8)
509     {
510         printf ("%.8s: Hardware ID must be 8 characters maximum\n", Name);
511         return;
512     }
513 
514     /* Find/display all names that match the input name prefix */
515 
516     AcpiUtStrupr (Name);
517     for (Info = AslDeviceIds; Info->Name; Info++)
518     {
519         Matched = TRUE;
520         for (i = 0; i < Length; i++)
521         {
522             if (Info->Name[i] != Name[i])
523             {
524                 Matched = FALSE;
525                 break;
526             }
527         }
528 
529         if (Matched)
530         {
531             Found = TRUE;
532             printf ("%8s   %s\n", Info->Name, Info->Description);
533         }
534     }
535 
536     if (!Found)
537     {
538         printf ("%s, Hardware ID not found\n", Name);
539     }
540 }
541 
542 
543 /*******************************************************************************
544  *
545  * FUNCTION:    AhDisplayUuids
546  *
547  * PARAMETERS:  None
548  *
549  * RETURN:      None
550  *
551  * DESCRIPTION: Display all known UUIDs.
552  *
553  ******************************************************************************/
554 
555 void
556 AhDisplayUuids (
557     void)
558 {
559     const AH_UUID           *Info;
560 
561 
562     printf ("ACPI-related UUIDs/GUIDs:\n");
563 
564     /* Display entire table of known ACPI-related UUIDs/GUIDs */
565 
566     for (Info = Gbl_AcpiUuids; Info->Description; Info++)
567     {
568         if (!Info->String) /* Null UUID string means group description */
569         {
570             printf ("\n%36s\n", Info->Description);
571         }
572         else
573         {
574             printf ("%32s : %s\n", Info->Description, Info->String);
575         }
576     }
577 
578     /* Help info on how UUIDs/GUIDs strings are encoded */
579 
580     printf ("\n\nByte encoding of UUID/GUID strings"
581         " into ACPI Buffer objects (use ToUUID from ASL):\n\n");
582 
583     printf ("%32s : %s\n", "Input UUID/GUID String format",
584         "aabbccdd-eeff-gghh-iijj-kkllmmnnoopp");
585 
586     printf ("%32s : %s\n", "Expected output ACPI buffer",
587         "dd,cc,bb,aa, ff,ee, hh,gg, ii,jj, kk,ll,mm,nn,oo,pp");
588 }
589 
590 
591 /*******************************************************************************
592  *
593  * FUNCTION:    AhDisplayTables
594  *
595  * PARAMETERS:  None
596  *
597  * RETURN:      None
598  *
599  * DESCRIPTION: Display all known ACPI tables
600  *
601  ******************************************************************************/
602 
603 void
604 AhDisplayTables (
605     void)
606 {
607     const AH_TABLE          *Info;
608     UINT32                  i = 0;
609 
610 
611     printf ("Known ACPI tables:\n");
612 
613     for (Info = Gbl_AcpiSupportedTables; Info->Signature; Info++)
614     {
615         printf ("%8s : %s\n", Info->Signature, Info->Description);
616         i++;
617     }
618 
619     printf ("\nTotal %u ACPI tables\n\n", i);
620 }
621 
622 
623 /*******************************************************************************
624  *
625  * FUNCTION:    AhDecodeException
626  *
627  * PARAMETERS:  HexString           - ACPI status string from command line, in
628  *                                    hex. If null, display all exceptions.
629  *
630  * RETURN:      None
631  *
632  * DESCRIPTION: Decode and display an ACPI_STATUS exception code.
633  *
634  ******************************************************************************/
635 
636 void
637 AhDecodeException (
638     char                    *HexString)
639 {
640     const ACPI_EXCEPTION_INFO   *ExceptionInfo;
641     UINT32                      Status;
642     UINT32                      i;
643 
644 
645     /*
646      * A null input string means to decode and display all known
647      * exception codes.
648      */
649     if (!HexString)
650     {
651         printf ("All defined ACPICA exception codes:\n\n");
652         AH_DISPLAY_EXCEPTION (0,
653             "AE_OK                        (No error occurred)");
654 
655         /* Display codes in each block of exception types */
656 
657         for (i = 1; (i & AE_CODE_MASK) <= AE_CODE_MAX; i += 0x1000)
658         {
659             Status = i;
660             do
661             {
662                 ExceptionInfo = AcpiUtValidateException ((ACPI_STATUS) Status);
663                 if (ExceptionInfo)
664                 {
665                     AH_DISPLAY_EXCEPTION_TEXT (Status, ExceptionInfo);
666                 }
667 
668                 Status++;
669 
670             } while (ExceptionInfo);
671         }
672         return;
673     }
674 
675     /* Decode a single user-supplied exception code */
676 
677     Status = strtoul (HexString, NULL, 16);
678     if (!Status)
679     {
680         printf ("%s: Invalid hexadecimal exception code value\n", HexString);
681         return;
682     }
683 
684     if (Status > ACPI_UINT16_MAX)
685     {
686         AH_DISPLAY_EXCEPTION (Status, "Invalid exception code (more than 16 bits)");
687         return;
688     }
689 
690     ExceptionInfo = AcpiUtValidateException ((ACPI_STATUS) Status);
691     if (!ExceptionInfo)
692     {
693         AH_DISPLAY_EXCEPTION (Status, "Unknown exception code");
694         return;
695     }
696 
697     AH_DISPLAY_EXCEPTION_TEXT (Status, ExceptionInfo);
698 }
699