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