xref: /freebsd/sys/contrib/dev/acpica/components/debugger/dbconvert.c (revision 49b49cda41feabe3439f7318e8bf40e3896c7bf4)
1 /*******************************************************************************
2  *
3  * Module Name: dbconvert - debugger miscellaneous conversion routines
4  *
5  ******************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2015, Intel Corp.
9  * All rights reserved.
10  *
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.
25  *
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.
29  *
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.
42  */
43 
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/acdebug.h>
47 
48 
49 #define _COMPONENT          ACPI_CA_DEBUGGER
50         ACPI_MODULE_NAME    ("dbconvert")
51 
52 
53 #define DB_DEFAULT_PKG_ELEMENTS     33
54 
55 
56 /*******************************************************************************
57  *
58  * FUNCTION:    AcpiDbHexCharToValue
59  *
60  * PARAMETERS:  HexChar             - Ascii Hex digit, 0-9|a-f|A-F
61  *              ReturnValue         - Where the converted value is returned
62  *
63  * RETURN:      Status
64  *
65  * DESCRIPTION: Convert a single hex character to a 4-bit number (0-16).
66  *
67  ******************************************************************************/
68 
69 ACPI_STATUS
70 AcpiDbHexCharToValue (
71     int                     HexChar,
72     UINT8                   *ReturnValue)
73 {
74     UINT8                   Value;
75 
76 
77     /* Digit must be ascii [0-9a-fA-F] */
78 
79     if (!isxdigit (HexChar))
80     {
81         return (AE_BAD_HEX_CONSTANT);
82     }
83 
84     if (HexChar <= 0x39)
85     {
86         Value = (UINT8) (HexChar - 0x30);
87     }
88     else
89     {
90         Value = (UINT8) (toupper (HexChar) - 0x37);
91     }
92 
93     *ReturnValue = Value;
94     return (AE_OK);
95 }
96 
97 
98 /*******************************************************************************
99  *
100  * FUNCTION:    AcpiDbHexByteToBinary
101  *
102  * PARAMETERS:  HexByte             - Double hex digit (0x00 - 0xFF) in format:
103  *                                    HiByte then LoByte.
104  *              ReturnValue         - Where the converted value is returned
105  *
106  * RETURN:      Status
107  *
108  * DESCRIPTION: Convert two hex characters to an 8 bit number (0 - 255).
109  *
110  ******************************************************************************/
111 
112 static ACPI_STATUS
113 AcpiDbHexByteToBinary (
114     char                    *HexByte,
115     UINT8                   *ReturnValue)
116 {
117     UINT8                   Local0;
118     UINT8                   Local1;
119     ACPI_STATUS             Status;
120 
121 
122     /* High byte */
123 
124     Status = AcpiDbHexCharToValue (HexByte[0], &Local0);
125     if (ACPI_FAILURE (Status))
126     {
127         return (Status);
128     }
129 
130     /* Low byte */
131 
132     Status = AcpiDbHexCharToValue (HexByte[1], &Local1);
133     if (ACPI_FAILURE (Status))
134     {
135         return (Status);
136     }
137 
138     *ReturnValue = (UINT8) ((Local0 << 4) | Local1);
139     return (AE_OK);
140 }
141 
142 
143 /*******************************************************************************
144  *
145  * FUNCTION:    AcpiDbConvertToBuffer
146  *
147  * PARAMETERS:  String              - Input string to be converted
148  *              Object              - Where the buffer object is returned
149  *
150  * RETURN:      Status
151  *
152  * DESCRIPTION: Convert a string to a buffer object. String is treated a list
153  *              of buffer elements, each separated by a space or comma.
154  *
155  ******************************************************************************/
156 
157 static ACPI_STATUS
158 AcpiDbConvertToBuffer (
159     char                    *String,
160     ACPI_OBJECT             *Object)
161 {
162     UINT32                  i;
163     UINT32                  j;
164     UINT32                  Length;
165     UINT8                   *Buffer;
166     ACPI_STATUS             Status;
167 
168 
169     /* Generate the final buffer length */
170 
171     for (i = 0, Length = 0; String[i];)
172     {
173         i+=2;
174         Length++;
175 
176         while (String[i] &&
177               ((String[i] == ',') || (String[i] == ' ')))
178         {
179             i++;
180         }
181     }
182 
183     Buffer = ACPI_ALLOCATE (Length);
184     if (!Buffer)
185     {
186         return (AE_NO_MEMORY);
187     }
188 
189     /* Convert the command line bytes to the buffer */
190 
191     for (i = 0, j = 0; String[i];)
192     {
193         Status = AcpiDbHexByteToBinary (&String[i], &Buffer[j]);
194         if (ACPI_FAILURE (Status))
195         {
196             ACPI_FREE (Buffer);
197             return (Status);
198         }
199 
200         j++;
201         i+=2;
202         while (String[i] &&
203               ((String[i] == ',') || (String[i] == ' ')))
204         {
205             i++;
206         }
207     }
208 
209     Object->Type = ACPI_TYPE_BUFFER;
210     Object->Buffer.Pointer = Buffer;
211     Object->Buffer.Length = Length;
212     return (AE_OK);
213 }
214 
215 
216 /*******************************************************************************
217  *
218  * FUNCTION:    AcpiDbConvertToPackage
219  *
220  * PARAMETERS:  String              - Input string to be converted
221  *              Object              - Where the package object is returned
222  *
223  * RETURN:      Status
224  *
225  * DESCRIPTION: Convert a string to a package object. Handles nested packages
226  *              via recursion with AcpiDbConvertToObject.
227  *
228  ******************************************************************************/
229 
230 ACPI_STATUS
231 AcpiDbConvertToPackage (
232     char                    *String,
233     ACPI_OBJECT             *Object)
234 {
235     char                    *This;
236     char                    *Next;
237     UINT32                  i;
238     ACPI_OBJECT_TYPE        Type;
239     ACPI_OBJECT             *Elements;
240     ACPI_STATUS             Status;
241 
242 
243     Elements = ACPI_ALLOCATE_ZEROED (
244         DB_DEFAULT_PKG_ELEMENTS * sizeof (ACPI_OBJECT));
245 
246     This = String;
247     for (i = 0; i < (DB_DEFAULT_PKG_ELEMENTS - 1); i++)
248     {
249         This = AcpiDbGetNextToken (This, &Next, &Type);
250         if (!This)
251         {
252             break;
253         }
254 
255         /* Recursive call to convert each package element */
256 
257         Status = AcpiDbConvertToObject (Type, This, &Elements[i]);
258         if (ACPI_FAILURE (Status))
259         {
260             AcpiDbDeleteObjects (i + 1, Elements);
261             ACPI_FREE (Elements);
262             return (Status);
263         }
264 
265         This = Next;
266     }
267 
268     Object->Type = ACPI_TYPE_PACKAGE;
269     Object->Package.Count = i;
270     Object->Package.Elements = Elements;
271     return (AE_OK);
272 }
273 
274 
275 /*******************************************************************************
276  *
277  * FUNCTION:    AcpiDbConvertToObject
278  *
279  * PARAMETERS:  Type                - Object type as determined by parser
280  *              String              - Input string to be converted
281  *              Object              - Where the new object is returned
282  *
283  * RETURN:      Status
284  *
285  * DESCRIPTION: Convert a typed and tokenized string to an ACPI_OBJECT. Typing:
286  *              1) String objects were surrounded by quotes.
287  *              2) Buffer objects were surrounded by parentheses.
288  *              3) Package objects were surrounded by brackets "[]".
289  *              4) All standalone tokens are treated as integers.
290  *
291  ******************************************************************************/
292 
293 ACPI_STATUS
294 AcpiDbConvertToObject (
295     ACPI_OBJECT_TYPE        Type,
296     char                    *String,
297     ACPI_OBJECT             *Object)
298 {
299     ACPI_STATUS             Status = AE_OK;
300 
301 
302     switch (Type)
303     {
304     case ACPI_TYPE_STRING:
305 
306         Object->Type = ACPI_TYPE_STRING;
307         Object->String.Pointer = String;
308         Object->String.Length = (UINT32) strlen (String);
309         break;
310 
311     case ACPI_TYPE_BUFFER:
312 
313         Status = AcpiDbConvertToBuffer (String, Object);
314         break;
315 
316     case ACPI_TYPE_PACKAGE:
317 
318         Status = AcpiDbConvertToPackage (String, Object);
319         break;
320 
321     default:
322 
323         Object->Type = ACPI_TYPE_INTEGER;
324         Status = AcpiUtStrtoul64 (String, 16, &Object->Integer.Value);
325         break;
326     }
327 
328     return (Status);
329 }
330 
331 
332 /*******************************************************************************
333  *
334  * FUNCTION:    AcpiDbEncodePldBuffer
335  *
336  * PARAMETERS:  PldInfo             - _PLD buffer struct (Using local struct)
337  *
338  * RETURN:      Encode _PLD buffer suitable for return value from _PLD
339  *
340  * DESCRIPTION: Bit-packs a _PLD buffer struct. Used to test the _PLD macros
341  *
342  ******************************************************************************/
343 
344 UINT8 *
345 AcpiDbEncodePldBuffer (
346     ACPI_PLD_INFO           *PldInfo)
347 {
348     UINT32                  *Buffer;
349     UINT32                  Dword;
350 
351 
352     Buffer = ACPI_ALLOCATE_ZEROED (ACPI_PLD_BUFFER_SIZE);
353     if (!Buffer)
354     {
355         return (NULL);
356     }
357 
358     /* First 32 bits */
359 
360     Dword = 0;
361     ACPI_PLD_SET_REVISION       (&Dword, PldInfo->Revision);
362     ACPI_PLD_SET_IGNORE_COLOR   (&Dword, PldInfo->IgnoreColor);
363     ACPI_PLD_SET_RED            (&Dword, PldInfo->Red);
364     ACPI_PLD_SET_GREEN          (&Dword, PldInfo->Green);
365     ACPI_PLD_SET_BLUE           (&Dword, PldInfo->Blue);
366     ACPI_MOVE_32_TO_32 (&Buffer[0], &Dword);
367 
368     /* Second 32 bits */
369 
370     Dword = 0;
371     ACPI_PLD_SET_WIDTH          (&Dword, PldInfo->Width);
372     ACPI_PLD_SET_HEIGHT         (&Dword, PldInfo->Height);
373     ACPI_MOVE_32_TO_32 (&Buffer[1], &Dword);
374 
375     /* Third 32 bits */
376 
377     Dword = 0;
378     ACPI_PLD_SET_USER_VISIBLE   (&Dword, PldInfo->UserVisible);
379     ACPI_PLD_SET_DOCK           (&Dword, PldInfo->Dock);
380     ACPI_PLD_SET_LID            (&Dword, PldInfo->Lid);
381     ACPI_PLD_SET_PANEL          (&Dword, PldInfo->Panel);
382     ACPI_PLD_SET_VERTICAL       (&Dword, PldInfo->VerticalPosition);
383     ACPI_PLD_SET_HORIZONTAL     (&Dword, PldInfo->HorizontalPosition);
384     ACPI_PLD_SET_SHAPE          (&Dword, PldInfo->Shape);
385     ACPI_PLD_SET_ORIENTATION    (&Dword, PldInfo->GroupOrientation);
386     ACPI_PLD_SET_TOKEN          (&Dword, PldInfo->GroupToken);
387     ACPI_PLD_SET_POSITION       (&Dword, PldInfo->GroupPosition);
388     ACPI_PLD_SET_BAY            (&Dword, PldInfo->Bay);
389     ACPI_MOVE_32_TO_32 (&Buffer[2], &Dword);
390 
391     /* Fourth 32 bits */
392 
393     Dword = 0;
394     ACPI_PLD_SET_EJECTABLE      (&Dword, PldInfo->Ejectable);
395     ACPI_PLD_SET_OSPM_EJECT     (&Dword, PldInfo->OspmEjectRequired);
396     ACPI_PLD_SET_CABINET        (&Dword, PldInfo->CabinetNumber);
397     ACPI_PLD_SET_CARD_CAGE      (&Dword, PldInfo->CardCageNumber);
398     ACPI_PLD_SET_REFERENCE      (&Dword, PldInfo->Reference);
399     ACPI_PLD_SET_ROTATION       (&Dword, PldInfo->Rotation);
400     ACPI_PLD_SET_ORDER          (&Dword, PldInfo->Order);
401     ACPI_MOVE_32_TO_32 (&Buffer[3], &Dword);
402 
403     if (PldInfo->Revision >= 2)
404     {
405         /* Fifth 32 bits */
406 
407         Dword = 0;
408         ACPI_PLD_SET_VERT_OFFSET    (&Dword, PldInfo->VerticalOffset);
409         ACPI_PLD_SET_HORIZ_OFFSET   (&Dword, PldInfo->HorizontalOffset);
410         ACPI_MOVE_32_TO_32 (&Buffer[4], &Dword);
411     }
412 
413     return (ACPI_CAST_PTR (UINT8, Buffer));
414 }
415 
416 
417 /*******************************************************************************
418  *
419  * FUNCTION:    AcpiDbDumpPldBuffer
420  *
421  * PARAMETERS:  ObjDesc             - Object returned from _PLD method
422  *
423  * RETURN:      None.
424  *
425  * DESCRIPTION: Dumps formatted contents of a _PLD return buffer.
426  *
427  ******************************************************************************/
428 
429 #define ACPI_PLD_OUTPUT     "%20s : %-6X\n"
430 
431 void
432 AcpiDbDumpPldBuffer (
433     ACPI_OBJECT             *ObjDesc)
434 {
435     ACPI_OBJECT             *BufferDesc;
436     ACPI_PLD_INFO           *PldInfo;
437     UINT8                   *NewBuffer;
438     ACPI_STATUS             Status;
439 
440 
441     /* Object must be of type Package with at least one Buffer element */
442 
443     if (ObjDesc->Type != ACPI_TYPE_PACKAGE)
444     {
445         return;
446     }
447 
448     BufferDesc = &ObjDesc->Package.Elements[0];
449     if (BufferDesc->Type != ACPI_TYPE_BUFFER)
450     {
451         return;
452     }
453 
454     /* Convert _PLD buffer to local _PLD struct */
455 
456     Status = AcpiDecodePldBuffer (BufferDesc->Buffer.Pointer,
457         BufferDesc->Buffer.Length, &PldInfo);
458     if (ACPI_FAILURE (Status))
459     {
460         return;
461     }
462 
463     /* Encode local _PLD struct back to a _PLD buffer */
464 
465     NewBuffer = AcpiDbEncodePldBuffer (PldInfo);
466     if (!NewBuffer)
467     {
468         return;
469     }
470 
471     /* The two bit-packed buffers should match */
472 
473     if (memcmp (NewBuffer, BufferDesc->Buffer.Pointer,
474         BufferDesc->Buffer.Length))
475     {
476         AcpiOsPrintf ("Converted _PLD buffer does not compare. New:\n");
477 
478         AcpiUtDumpBuffer (NewBuffer,
479             BufferDesc->Buffer.Length, DB_BYTE_DISPLAY, 0);
480     }
481 
482     /* First 32-bit dword */
483 
484     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Revision", PldInfo->Revision);
485     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_IgnoreColor", PldInfo->IgnoreColor);
486     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Red", PldInfo->Red);
487     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Green", PldInfo->Green);
488     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Blue", PldInfo->Blue);
489 
490     /* Second 32-bit dword */
491 
492     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Width", PldInfo->Width);
493     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Height", PldInfo->Height);
494 
495     /* Third 32-bit dword */
496 
497     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_UserVisible", PldInfo->UserVisible);
498     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Dock", PldInfo->Dock);
499     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Lid", PldInfo->Lid);
500     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Panel", PldInfo->Panel);
501     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_VerticalPosition", PldInfo->VerticalPosition);
502     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_HorizontalPosition", PldInfo->HorizontalPosition);
503     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Shape", PldInfo->Shape);
504     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_GroupOrientation", PldInfo->GroupOrientation);
505     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_GroupToken", PldInfo->GroupToken);
506     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_GroupPosition", PldInfo->GroupPosition);
507     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Bay", PldInfo->Bay);
508 
509     /* Fourth 32-bit dword */
510 
511     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Ejectable", PldInfo->Ejectable);
512     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_EjectRequired", PldInfo->OspmEjectRequired);
513     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_CabinetNumber", PldInfo->CabinetNumber);
514     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_CardCageNumber", PldInfo->CardCageNumber);
515     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Reference", PldInfo->Reference);
516     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Rotation", PldInfo->Rotation);
517     AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Order", PldInfo->Order);
518 
519     /* Fifth 32-bit dword */
520 
521     if (BufferDesc->Buffer.Length > 16)
522     {
523         AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_VerticalOffset", PldInfo->VerticalOffset);
524         AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_HorizontalOffset", PldInfo->HorizontalOffset);
525     }
526 
527     ACPI_FREE (PldInfo);
528     ACPI_FREE (NewBuffer);
529 }
530