xref: /titanic_50/usr/src/cmd/acpi/acpidump/utbuffer.c (revision 24bb1048b1197ebc9afe761e4098573aeedfe8c9)
1 /******************************************************************************
2  *
3  * Module Name: utbuffer - Buffer dump routines
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2016, 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 "acpi.h"
45 #include "accommon.h"
46 
47 #define _COMPONENT          ACPI_UTILITIES
48         ACPI_MODULE_NAME    ("utbuffer")
49 
50 
51 /*******************************************************************************
52  *
53  * FUNCTION:    AcpiUtDumpBuffer
54  *
55  * PARAMETERS:  Buffer              - Buffer to dump
56  *              Count               - Amount to dump, in bytes
57  *              Display             - BYTE, WORD, DWORD, or QWORD display:
58  *                                      DB_BYTE_DISPLAY
59  *                                      DB_WORD_DISPLAY
60  *                                      DB_DWORD_DISPLAY
61  *                                      DB_QWORD_DISPLAY
62  *              BaseOffset          - Beginning buffer offset (display only)
63  *
64  * RETURN:      None
65  *
66  * DESCRIPTION: Generic dump buffer in both hex and ascii.
67  *
68  ******************************************************************************/
69 
70 void
71 AcpiUtDumpBuffer (
72     UINT8                   *Buffer,
73     UINT32                  Count,
74     UINT32                  Display,
75     UINT32                  BaseOffset)
76 {
77     UINT32                  i = 0;
78     UINT32                  j;
79     UINT32                  Temp32;
80     UINT8                   BufChar;
81 
82 
83     if (!Buffer)
84     {
85         AcpiOsPrintf ("Null Buffer Pointer in DumpBuffer!\n");
86         return;
87     }
88 
89     if ((Count < 4) || (Count & 0x01))
90     {
91         Display = DB_BYTE_DISPLAY;
92     }
93 
94     /* Nasty little dump buffer routine! */
95 
96     while (i < Count)
97     {
98         /* Print current offset */
99 
100         AcpiOsPrintf ("%7.4X: ", (BaseOffset + i));
101 
102         /* Print 16 hex chars */
103 
104         for (j = 0; j < 16;)
105         {
106             if (i + j >= Count)
107             {
108                 /* Dump fill spaces */
109 
110                 AcpiOsPrintf ("%*s", ((Display * 2) + 1), " ");
111                 j += Display;
112                 continue;
113             }
114 
115             switch (Display)
116             {
117             case DB_BYTE_DISPLAY:
118             default:    /* Default is BYTE display */
119 
120                 AcpiOsPrintf ("%02X ", Buffer[(ACPI_SIZE) i + j]);
121                 break;
122 
123             case DB_WORD_DISPLAY:
124 
125                 ACPI_MOVE_16_TO_32 (&Temp32, &Buffer[(ACPI_SIZE) i + j]);
126                 AcpiOsPrintf ("%04X ", Temp32);
127                 break;
128 
129             case DB_DWORD_DISPLAY:
130 
131                 ACPI_MOVE_32_TO_32 (&Temp32, &Buffer[(ACPI_SIZE) i + j]);
132                 AcpiOsPrintf ("%08X ", Temp32);
133                 break;
134 
135             case DB_QWORD_DISPLAY:
136 
137                 ACPI_MOVE_32_TO_32 (&Temp32, &Buffer[(ACPI_SIZE) i + j]);
138                 AcpiOsPrintf ("%08X", Temp32);
139 
140                 ACPI_MOVE_32_TO_32 (&Temp32, &Buffer[(ACPI_SIZE) i + j + 4]);
141                 AcpiOsPrintf ("%08X ", Temp32);
142                 break;
143             }
144 
145             j += Display;
146         }
147 
148         /*
149          * Print the ASCII equivalent characters but watch out for the bad
150          * unprintable ones (printable chars are 0x20 through 0x7E)
151          */
152         AcpiOsPrintf (" ");
153         for (j = 0; j < 16; j++)
154         {
155             if (i + j >= Count)
156             {
157                 AcpiOsPrintf ("\n");
158                 return;
159             }
160 
161             /*
162              * Add comment characters so rest of line is ignored when
163              * compiled
164              */
165             if (j == 0)
166             {
167                 AcpiOsPrintf ("// ");
168             }
169 
170             BufChar = Buffer[(ACPI_SIZE) i + j];
171             if (isprint (BufChar))
172             {
173                 AcpiOsPrintf ("%c", BufChar);
174             }
175             else
176             {
177                 AcpiOsPrintf (".");
178             }
179         }
180 
181         /* Done with that line. */
182 
183         AcpiOsPrintf ("\n");
184         i += 16;
185     }
186 
187     return;
188 }
189 
190 
191 /*******************************************************************************
192  *
193  * FUNCTION:    AcpiUtDebugDumpBuffer
194  *
195  * PARAMETERS:  Buffer              - Buffer to dump
196  *              Count               - Amount to dump, in bytes
197  *              Display             - BYTE, WORD, DWORD, or QWORD display:
198  *                                      DB_BYTE_DISPLAY
199  *                                      DB_WORD_DISPLAY
200  *                                      DB_DWORD_DISPLAY
201  *                                      DB_QWORD_DISPLAY
202  *              ComponentID         - Caller's component ID
203  *
204  * RETURN:      None
205  *
206  * DESCRIPTION: Generic dump buffer in both hex and ascii.
207  *
208  ******************************************************************************/
209 
210 void
211 AcpiUtDebugDumpBuffer (
212     UINT8                   *Buffer,
213     UINT32                  Count,
214     UINT32                  Display,
215     UINT32                  ComponentId)
216 {
217 
218     /* Only dump the buffer if tracing is enabled */
219 
220     if (!((ACPI_LV_TABLES & AcpiDbgLevel) &&
221         (ComponentId & AcpiDbgLayer)))
222     {
223         return;
224     }
225 
226     AcpiUtDumpBuffer (Buffer, Count, Display, 0);
227 }
228 
229 
230 #ifdef ACPI_APPLICATION
231 /*******************************************************************************
232  *
233  * FUNCTION:    AcpiUtDumpBufferToFile
234  *
235  * PARAMETERS:  File                - File descriptor
236  *              Buffer              - Buffer to dump
237  *              Count               - Amount to dump, in bytes
238  *              Display             - BYTE, WORD, DWORD, or QWORD display:
239  *                                      DB_BYTE_DISPLAY
240  *                                      DB_WORD_DISPLAY
241  *                                      DB_DWORD_DISPLAY
242  *                                      DB_QWORD_DISPLAY
243  *              BaseOffset          - Beginning buffer offset (display only)
244  *
245  * RETURN:      None
246  *
247  * DESCRIPTION: Generic dump buffer in both hex and ascii to a file.
248  *
249  ******************************************************************************/
250 
251 void
252 AcpiUtDumpBufferToFile (
253     ACPI_FILE               File,
254     UINT8                   *Buffer,
255     UINT32                  Count,
256     UINT32                  Display,
257     UINT32                  BaseOffset)
258 {
259     UINT32                  i = 0;
260     UINT32                  j;
261     UINT32                  Temp32;
262     UINT8                   BufChar;
263 
264 
265     if (!Buffer)
266     {
267         AcpiUtFilePrintf (File, "Null Buffer Pointer in DumpBuffer!\n");
268         return;
269     }
270 
271     if ((Count < 4) || (Count & 0x01))
272     {
273         Display = DB_BYTE_DISPLAY;
274     }
275 
276     /* Nasty little dump buffer routine! */
277 
278     while (i < Count)
279     {
280         /* Print current offset */
281 
282         AcpiUtFilePrintf (File, "%7.4X: ", (BaseOffset + i));
283 
284         /* Print 16 hex chars */
285 
286         for (j = 0; j < 16;)
287         {
288             if (i + j >= Count)
289             {
290                 /* Dump fill spaces */
291 
292                 AcpiUtFilePrintf (File, "%*s", ((Display * 2) + 1), " ");
293                 j += Display;
294                 continue;
295             }
296 
297             switch (Display)
298             {
299             case DB_BYTE_DISPLAY:
300             default:    /* Default is BYTE display */
301 
302                 AcpiUtFilePrintf (File, "%02X ", Buffer[(ACPI_SIZE) i + j]);
303                 break;
304 
305             case DB_WORD_DISPLAY:
306 
307                 ACPI_MOVE_16_TO_32 (&Temp32, &Buffer[(ACPI_SIZE) i + j]);
308                 AcpiUtFilePrintf (File, "%04X ", Temp32);
309                 break;
310 
311             case DB_DWORD_DISPLAY:
312 
313                 ACPI_MOVE_32_TO_32 (&Temp32, &Buffer[(ACPI_SIZE) i + j]);
314                 AcpiUtFilePrintf (File, "%08X ", Temp32);
315                 break;
316 
317             case DB_QWORD_DISPLAY:
318 
319                 ACPI_MOVE_32_TO_32 (&Temp32, &Buffer[(ACPI_SIZE) i + j]);
320                 AcpiUtFilePrintf (File, "%08X", Temp32);
321 
322                 ACPI_MOVE_32_TO_32 (&Temp32, &Buffer[(ACPI_SIZE) i + j + 4]);
323                 AcpiUtFilePrintf (File, "%08X ", Temp32);
324                 break;
325             }
326 
327             j += Display;
328         }
329 
330         /*
331          * Print the ASCII equivalent characters but watch out for the bad
332          * unprintable ones (printable chars are 0x20 through 0x7E)
333          */
334         AcpiUtFilePrintf (File, " ");
335         for (j = 0; j < 16; j++)
336         {
337             if (i + j >= Count)
338             {
339                 AcpiUtFilePrintf (File, "\n");
340                 return;
341             }
342 
343             BufChar = Buffer[(ACPI_SIZE) i + j];
344             if (isprint (BufChar))
345             {
346                 AcpiUtFilePrintf (File, "%c", BufChar);
347             }
348             else
349             {
350                 AcpiUtFilePrintf (File, ".");
351             }
352         }
353 
354         /* Done with that line. */
355 
356         AcpiUtFilePrintf (File, "\n");
357         i += 16;
358     }
359 
360     return;
361 }
362 #endif
363