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