Indentation fix, cleanup.
[AROS.git] / arch / all-pc / acpica / source / tools / acpihelp / ahdecode.c
blob38e2b7e2378e5033c859b7d6ea12cfc06392afd6
1 /******************************************************************************
3 * Module Name: ahdecode - Operator/Opcode decoding for acpihelp utility
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #define ACPI_CREATE_PREDEFINED_TABLE
45 #define ACPI_CREATE_RESOURCE_TABLE
47 #include "acpihelp.h"
48 #include "acpredef.h"
51 /* Device IDs defined in the ACPI specification */
53 static const AH_DEVICE_ID AhDeviceIds[] =
55 {"PNP0A05", "Generic Container Device"},
56 {"PNP0A06", "Generic Container Device"},
57 {"PNP0C08", "ACPI core hardware"},
58 {"PNP0C09", "Embedded Controller Device"},
59 {"PNP0C0A", "Control Method Battery"},
60 {"PNP0C0B", "Fan"},
61 {"PNP0C0C", "Power Button Device"},
62 {"PNP0C0D", "Lid Device"},
63 {"PNP0C0E", "Sleep Button Device"},
64 {"PNP0C0F", "PCI Interrupt Link Device"},
65 {"PNP0C80", "Memory Device"},
67 {"ACPI0001", "SMBus 1.0 Host Controller"},
68 {"ACPI0002", "Smart Battery Subsystem"},
69 {"ACPI0003", "Power Source Device"},
70 {"ACPI0004", "Module Device"},
71 {"ACPI0005", "SMBus 2.0 Host Controller"},
72 {"ACPI0006", "GPE Block Device"},
73 {"ACPI0007", "Processor Device"},
74 {"ACPI0008", "Ambient Light Sensor Device"},
75 {"ACPI0009", "I/O xAPIC Device"},
76 {"ACPI000A", "I/O APIC Device"},
77 {"ACPI000B", "I/O SAPIC Device"},
78 {"ACPI000C", "Processor Aggregator Device"},
79 {"ACPI000D", "Power Meter Device"},
80 {"ACPI000E", "Time/Alarm Device"},
81 {"ACPI000F", "User Presence Detection Device"},
83 {NULL, NULL}
86 #define AH_DISPLAY_EXCEPTION(Status, Name) \
87 printf ("%.4X: %s\n", Status, Name)
89 #define AH_DISPLAY_EXCEPTION_TEXT(Status, Exception) \
90 printf ("%.4X: %-28s (%s)\n", Status, Exception->Name, Exception->Description)
92 #define BUFFER_LENGTH 128
93 #define LINE_BUFFER_LENGTH 512
95 static char Gbl_Buffer[BUFFER_LENGTH];
96 static char Gbl_LineBuffer[LINE_BUFFER_LENGTH];
98 /* Local prototypes */
100 static BOOLEAN
101 AhDisplayPredefinedName (
102 char *Name,
103 UINT32 Length);
105 static void
106 AhDisplayPredefinedInfo (
107 char *Name);
109 static void
110 AhDisplayResourceName (
111 const ACPI_PREDEFINED_INFO *ThisName);
113 static void
114 AhDisplayAmlOpcode (
115 const AH_AML_OPCODE *Op);
117 static void
118 AhDisplayAslOperator (
119 const AH_ASL_OPERATOR *Op);
121 static void
122 AhDisplayOperatorKeywords (
123 const AH_ASL_OPERATOR *Op);
125 static void
126 AhDisplayAslKeyword (
127 const AH_ASL_KEYWORD *Op);
129 static void
130 AhPrintOneField (
131 UINT32 Indent,
132 UINT32 CurrentPosition,
133 UINT32 MaxPosition,
134 const char *Field);
137 /*******************************************************************************
139 * FUNCTION: AhFindPredefinedNames (entry point for predefined name search)
141 * PARAMETERS: NamePrefix - Name or prefix to find. Must start with
142 * an underscore. NULL means "find all"
144 * RETURN: None
146 * DESCRIPTION: Find and display all ACPI predefined names that match the
147 * input name or prefix. Includes the required number of arguments
148 * and the expected return type, if any.
150 ******************************************************************************/
152 void
153 AhFindPredefinedNames (
154 char *NamePrefix)
156 UINT32 Length;
157 BOOLEAN Found;
158 char Name[9];
161 if (!NamePrefix)
163 Found = AhDisplayPredefinedName (Name, 0);
164 return;
167 /* Contruct a local name or name prefix */
169 AhStrupr (NamePrefix);
170 if (*NamePrefix == '_')
172 NamePrefix++;
175 Name[0] = '_';
176 strncpy (&Name[1], NamePrefix, 7);
178 Length = strlen (Name);
179 if (Length > 4)
181 printf ("%.8s: Predefined name must be 4 characters maximum\n", Name);
182 return;
185 Found = AhDisplayPredefinedName (Name, Length);
186 if (!Found)
188 printf ("%s, no matching predefined names\n", Name);
193 /*******************************************************************************
195 * FUNCTION: AhDisplayPredefinedName
197 * PARAMETERS: Name - Name or name prefix
199 * RETURN: TRUE if any names matched, FALSE otherwise
201 * DESCRIPTION: Display information about ACPI predefined names that match
202 * the input name or name prefix.
204 ******************************************************************************/
206 static BOOLEAN
207 AhDisplayPredefinedName (
208 char *Name,
209 UINT32 Length)
211 const AH_PREDEFINED_NAME *Info;
212 BOOLEAN Found = FALSE;
213 BOOLEAN Matched;
214 UINT32 i;
217 /* Find/display all names that match the input name prefix */
219 for (Info = AslPredefinedInfo; Info->Name; Info++)
221 if (!Name)
223 Found = TRUE;
224 printf ("%s: <%s>\n", Info->Name, Info->Description);
225 printf ("%*s%s\n", 6, " ", Info->Action);
227 AhDisplayPredefinedInfo (Info->Name);
228 continue;
231 Matched = TRUE;
232 for (i = 0; i < Length; i++)
234 if (Info->Name[i] != Name[i])
236 Matched = FALSE;
237 break;
241 if (Matched)
243 Found = TRUE;
244 printf ("%s: <%s>\n", Info->Name, Info->Description);
245 printf ("%*s%s\n", 6, " ", Info->Action);
247 AhDisplayPredefinedInfo (Info->Name);
251 return (Found);
255 /*******************************************************************************
257 * FUNCTION: AhDisplayPredefinedInfo
259 * PARAMETERS: Name - Exact 4-character ACPI name.
261 * RETURN: None
263 * DESCRIPTION: Find the name in the main ACPICA predefined info table and
264 * display the # of arguments and the return value type.
266 * Note: Resource Descriptor field names do not appear in this
267 * table -- thus, nothing will be displayed for them.
269 ******************************************************************************/
271 static void
272 AhDisplayPredefinedInfo (
273 char *Name)
275 const ACPI_PREDEFINED_INFO *ThisName;
278 /* NOTE: we check both tables always because there are some dupes */
280 /* Check against the predefine methods first */
282 ThisName = AcpiUtMatchPredefinedMethod (Name);
283 if (ThisName)
285 AcpiUtDisplayPredefinedMethod (Gbl_Buffer, ThisName, TRUE);
288 /* Check against the predefined resource descriptor names */
290 ThisName = AcpiUtMatchResourceName (Name);
291 if (ThisName)
293 AhDisplayResourceName (ThisName);
298 /*******************************************************************************
300 * FUNCTION: AhDisplayResourceName
302 * PARAMETERS: ThisName - Entry in the predefined method/name table
304 * RETURN: None
306 * DESCRIPTION: Display information about a resource descriptor name.
308 ******************************************************************************/
310 static void
311 AhDisplayResourceName (
312 const ACPI_PREDEFINED_INFO *ThisName)
314 UINT32 NumTypes;
317 NumTypes = AcpiUtGetResourceBitWidth (Gbl_Buffer,
318 ThisName->Info.ArgumentList);
320 printf (" %4.4s resource descriptor field is %s bits wide%s\n",
321 ThisName->Info.Name,
322 Gbl_Buffer,
323 (NumTypes > 1) ? " (depending on descriptor type)" : "");
327 /*******************************************************************************
329 * FUNCTION: AhFindAmlOpcode (entry point for AML opcode name search)
331 * PARAMETERS: Name - Name or prefix for an AML opcode.
332 * NULL means "find all"
334 * RETURN: None
336 * DESCRIPTION: Find all AML opcodes that match the input Name or name
337 * prefix.
339 ******************************************************************************/
341 void
342 AhFindAmlOpcode (
343 char *Name)
345 const AH_AML_OPCODE *Op;
346 BOOLEAN Found = FALSE;
349 AhStrupr (Name);
351 /* Find/display all opcode names that match the input name prefix */
353 for (Op = AmlOpcodeInfo; Op->OpcodeString; Op++)
355 if (!Op->OpcodeName) /* Unused opcodes */
357 continue;
360 if (!Name)
362 AhDisplayAmlOpcode (Op);
363 Found = TRUE;
364 continue;
367 /* Upper case the opcode name before substring compare */
369 strcpy (Gbl_Buffer, Op->OpcodeName);
370 AhStrupr (Gbl_Buffer);
372 if (strstr (Gbl_Buffer, Name) == Gbl_Buffer)
374 AhDisplayAmlOpcode (Op);
375 Found = TRUE;
379 if (!Found)
381 printf ("%s, no matching AML operators\n", Name);
386 /*******************************************************************************
388 * FUNCTION: AhDecodeAmlOpcode (entry point for AML opcode search)
390 * PARAMETERS: OpcodeString - String version of AML opcode
392 * RETURN: None
394 * DESCRIPTION: Display information about the input AML opcode
396 ******************************************************************************/
398 void
399 AhDecodeAmlOpcode (
400 char *OpcodeString)
402 const AH_AML_OPCODE *Op;
403 UINT32 Opcode;
404 UINT8 Prefix;
407 if (!OpcodeString)
409 AhFindAmlOpcode (NULL);
410 return;
413 Opcode = ACPI_STRTOUL (OpcodeString, NULL, 16);
414 if (Opcode > ACPI_UINT16_MAX)
416 printf ("Invalid opcode (more than 16 bits)\n");
417 return;
420 /* Only valid opcode extension is 0x5B */
422 Prefix = (Opcode & 0x0000FF00) >> 8;
423 if (Prefix && (Prefix != 0x5B))
425 printf ("Invalid opcode (invalid extension prefix 0x%X)\n",
426 Prefix);
427 return;
430 /* Find/Display the opcode. May fall within an opcode range */
432 for (Op = AmlOpcodeInfo; Op->OpcodeString; Op++)
434 if ((Opcode >= Op->OpcodeRangeStart) &&
435 (Opcode <= Op->OpcodeRangeEnd))
437 AhDisplayAmlOpcode (Op);
443 /*******************************************************************************
445 * FUNCTION: AhDisplayAmlOpcode
447 * PARAMETERS: Op - An opcode info struct
449 * RETURN: None
451 * DESCRIPTION: Display the contents of an AML opcode information struct
453 ******************************************************************************/
455 static void
456 AhDisplayAmlOpcode (
457 const AH_AML_OPCODE *Op)
460 if (!Op->OpcodeName)
462 printf ("%18s: Opcode=%-9s\n", "Reserved opcode", Op->OpcodeString);
463 return;
466 /* Opcode name and value(s) */
468 printf ("%18s: Opcode=%-9s Type (%s)",
469 Op->OpcodeName, Op->OpcodeString, Op->Type);
471 /* Optional fixed/static arguments */
473 if (Op->FixedArguments)
475 printf (" FixedArgs (");
476 AhPrintOneField (37, 36 + 7 + strlen (Op->Type) + 12,
477 AH_MAX_AML_LINE_LENGTH, Op->FixedArguments);
478 printf (")");
481 /* Optional variable-length argument list */
483 if (Op->VariableArguments)
485 if (Op->FixedArguments)
487 printf ("\n%*s", 36, " ");
489 printf (" VariableArgs (");
490 AhPrintOneField (37, 15, AH_MAX_AML_LINE_LENGTH, Op->VariableArguments);
491 printf (")");
493 printf ("\n");
495 /* Grammar specification */
497 if (Op->Grammar)
499 AhPrintOneField (37, 0, AH_MAX_AML_LINE_LENGTH, Op->Grammar);
500 printf ("\n");
505 /*******************************************************************************
507 * FUNCTION: AhFindAslKeywords (entry point for ASL keyword search)
509 * PARAMETERS: Name - Name or prefix for an ASL keyword.
510 * NULL means "find all"
512 * RETURN: None
514 * DESCRIPTION: Find all ASL keywords that match the input Name or name
515 * prefix.
517 ******************************************************************************/
519 void
520 AhFindAslKeywords (
521 char *Name)
523 const AH_ASL_KEYWORD *Keyword;
524 BOOLEAN Found = FALSE;
527 AhStrupr (Name);
529 for (Keyword = AslKeywordInfo; Keyword->Name; Keyword++)
531 if (!Name)
533 AhDisplayAslKeyword (Keyword);
534 Found = TRUE;
535 continue;
538 /* Upper case the operator name before substring compare */
540 strcpy (Gbl_Buffer, Keyword->Name);
541 AhStrupr (Gbl_Buffer);
543 if (strstr (Gbl_Buffer, Name) == Gbl_Buffer)
545 AhDisplayAslKeyword (Keyword);
546 Found = TRUE;
550 if (!Found)
552 printf ("%s, no matching ASL keywords\n", Name);
557 /*******************************************************************************
559 * FUNCTION: AhDisplayAslKeyword
561 * PARAMETERS: Op - Pointer to ASL keyword with syntax info
563 * RETURN: None
565 * DESCRIPTION: Format and display syntax info for an ASL keyword. Splits
566 * long lines appropriately for reading.
568 ******************************************************************************/
570 static void
571 AhDisplayAslKeyword (
572 const AH_ASL_KEYWORD *Op)
575 /* ASL keyword name and description */
577 printf ("%22s: %s\n", Op->Name, Op->Description);
578 if (!Op->KeywordList)
580 return;
583 /* List of actual keywords */
585 AhPrintOneField (24, 0, AH_MAX_ASL_LINE_LENGTH, Op->KeywordList);
586 printf ("\n");
590 /*******************************************************************************
592 * FUNCTION: AhFindAslOperators (entry point for ASL operator search)
594 * PARAMETERS: Name - Name or prefix for an ASL operator.
595 * NULL means "find all"
597 * RETURN: None
599 * DESCRIPTION: Find all ASL operators that match the input Name or name
600 * prefix.
602 ******************************************************************************/
604 void
605 AhFindAslOperators (
606 char *Name)
608 const AH_ASL_OPERATOR *Operator;
609 BOOLEAN Found = FALSE;
612 AhStrupr (Name);
614 /* Find/display all names that match the input name prefix */
616 for (Operator = AslOperatorInfo; Operator->Name; Operator++)
618 if (!Name)
620 AhDisplayAslOperator (Operator);
621 Found = TRUE;
622 continue;
625 /* Upper case the operator name before substring compare */
627 strcpy (Gbl_Buffer, Operator->Name);
628 AhStrupr (Gbl_Buffer);
630 if (strstr (Gbl_Buffer, Name) == Gbl_Buffer)
632 AhDisplayAslOperator (Operator);
633 Found = TRUE;
637 if (!Found)
639 printf ("%s, no matching ASL operators\n", Name);
644 /*******************************************************************************
646 * FUNCTION: AhDisplayAslOperator
648 * PARAMETERS: Op - Pointer to ASL operator with syntax info
650 * RETURN: None
652 * DESCRIPTION: Format and display syntax info for an ASL operator. Splits
653 * long lines appropriately for reading.
655 ******************************************************************************/
657 static void
658 AhDisplayAslOperator (
659 const AH_ASL_OPERATOR *Op)
662 /* ASL operator name and description */
664 printf ("%16s: %s\n", Op->Name, Op->Description);
665 if (!Op->Syntax)
667 return;
670 /* Syntax for the operator */
672 AhPrintOneField (18, 0, AH_MAX_ASL_LINE_LENGTH, Op->Syntax);
673 printf ("\n");
675 AhDisplayOperatorKeywords (Op);
676 printf ("\n");
680 /*******************************************************************************
682 * FUNCTION: AhDisplayOperatorKeywords
684 * PARAMETERS: Op - Pointer to ASL keyword with syntax info
686 * RETURN: None
688 * DESCRIPTION: Display any/all keywords that are associated with the ASL
689 * operator.
691 ******************************************************************************/
693 static void
694 AhDisplayOperatorKeywords (
695 const AH_ASL_OPERATOR *Op)
697 char *Token;
698 char *Separators = "(){}, ";
699 BOOLEAN FirstKeyword = TRUE;
702 if (!Op || !Op->Syntax)
704 return;
708 * Find all parameters that have the word "keyword" within, and then
709 * display the info about that keyword
711 strcpy (Gbl_LineBuffer, Op->Syntax);
712 Token = strtok (Gbl_LineBuffer, Separators);
713 while (Token)
715 if (strstr (Token, "Keyword"))
717 if (FirstKeyword)
719 printf ("\n");
720 FirstKeyword = FALSE;
723 /* Found a keyword, display keyword information */
725 AhFindAslKeywords (Token);
728 Token = strtok (NULL, Separators);
733 /*******************************************************************************
735 * FUNCTION: AhPrintOneField
737 * PARAMETERS: Indent - Indent length for new line(s)
738 * CurrentPosition - Position on current line
739 * MaxPosition - Max allowed line length
740 * Field - Data to output
742 * RETURN: Line position after field is written
744 * DESCRIPTION: Split long lines appropriately for ease of reading.
746 ******************************************************************************/
748 static void
749 AhPrintOneField (
750 UINT32 Indent,
751 UINT32 CurrentPosition,
752 UINT32 MaxPosition,
753 const char *Field)
755 UINT32 Position;
756 UINT32 TokenLength;
757 const char *This;
758 const char *Next;
759 const char *Last;
762 This = Field;
763 Position = CurrentPosition;
765 if (Position == 0)
767 printf ("%*s", (int) Indent, " ");
768 Position = Indent;
771 Last = This + strlen (This);
772 while ((Next = strpbrk (This, " ")))
774 TokenLength = Next - This;
775 Position += TokenLength;
777 /* Split long lines */
779 if (Position > MaxPosition)
781 printf ("\n%*s", (int) Indent, " ");
782 Position = TokenLength;
785 printf ("%.*s ", (int) TokenLength, This);
786 This = Next + 1;
789 /* Handle last token on the input line */
791 TokenLength = Last - This;
792 if (TokenLength > 0)
794 Position += TokenLength;
795 if (Position > MaxPosition)
797 printf ("\n%*s", (int) Indent, " ");
799 printf ("%s", This);
804 /*******************************************************************************
806 * FUNCTION: AhDisplayDeviceIds
808 * PARAMETERS: None
810 * RETURN: None
812 * DESCRIPTION: Display all PNP* and ACPI* device IDs defined in the ACPI spec.
814 ******************************************************************************/
816 void
817 AhDisplayDeviceIds (
818 void)
820 const AH_DEVICE_ID *DeviceId = AhDeviceIds;
823 printf ("ACPI and PNP Device IDs defined in the ACPI specification:\n\n");
824 while (DeviceId->Name)
826 printf ("%8s %s\n", DeviceId->Name, DeviceId->Description);
827 DeviceId++;
832 /*******************************************************************************
834 * FUNCTION: AhDecodeException
836 * PARAMETERS: HexString - ACPI status string from command line, in
837 * hex. If null, display all exceptions.
839 * RETURN: None
841 * DESCRIPTION: Decode and display an ACPI_STATUS exception code.
843 ******************************************************************************/
845 void
846 AhDecodeException (
847 char *HexString)
849 const ACPI_EXCEPTION_INFO *ExceptionInfo;
850 UINT32 Status;
851 UINT32 i;
855 * A null input string means to decode and display all known
856 * exception codes.
858 if (!HexString)
860 printf ("All defined ACPICA exception codes:\n\n");
861 AH_DISPLAY_EXCEPTION (0, "AE_OK (No error occurred)");
863 /* Display codes in each block of exception types */
865 for (i = 1; (i & AE_CODE_MASK) <= AE_CODE_MAX; i += 0x1000)
867 Status = i;
870 ExceptionInfo = AcpiUtValidateException ((ACPI_STATUS) Status);
871 if (ExceptionInfo)
873 AH_DISPLAY_EXCEPTION_TEXT (Status, ExceptionInfo);
875 Status++;
877 } while (ExceptionInfo);
879 return;
882 /* Decode a single user-supplied exception code */
884 Status = ACPI_STRTOUL (HexString, NULL, 16);
885 if (!Status)
887 printf ("%s: Invalid hexadecimal exception code value\n", HexString);
888 return;
891 if (Status > ACPI_UINT16_MAX)
893 AH_DISPLAY_EXCEPTION (Status, "Invalid exception code (more than 16 bits)");
894 return;
897 ExceptionInfo = AcpiUtValidateException ((ACPI_STATUS) Status);
898 if (!ExceptionInfo)
900 AH_DISPLAY_EXCEPTION (Status, "Unknown exception code");
901 return;
904 AH_DISPLAY_EXCEPTION_TEXT (Status, ExceptionInfo);