1 | /********************************************************************************/
|
---|
2 | /* */
|
---|
3 | /* TPM Debug Utilities */
|
---|
4 | /* Written by Ken Goldman */
|
---|
5 | /* IBM Thomas J. Watson Research Center */
|
---|
6 | /* $Id: tpm_debug.c 4179 2010-11-10 20:10:24Z kgoldman $ */
|
---|
7 | /* */
|
---|
8 | /* (c) Copyright IBM Corporation 2006, 2010. */
|
---|
9 | /* */
|
---|
10 | /* All rights reserved. */
|
---|
11 | /* */
|
---|
12 | /* Redistribution and use in source and binary forms, with or without */
|
---|
13 | /* modification, are permitted provided that the following conditions are */
|
---|
14 | /* met: */
|
---|
15 | /* */
|
---|
16 | /* Redistributions of source code must retain the above copyright notice, */
|
---|
17 | /* this list of conditions and the following disclaimer. */
|
---|
18 | /* */
|
---|
19 | /* Redistributions in binary form must reproduce the above copyright */
|
---|
20 | /* notice, this list of conditions and the following disclaimer in the */
|
---|
21 | /* documentation and/or other materials provided with the distribution. */
|
---|
22 | /* */
|
---|
23 | /* Neither the names of the IBM Corporation nor the names of its */
|
---|
24 | /* contributors may be used to endorse or promote products derived from */
|
---|
25 | /* this software without specific prior written permission. */
|
---|
26 | /* */
|
---|
27 | /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
|
---|
28 | /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
|
---|
29 | /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR */
|
---|
30 | /* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
|
---|
31 | /* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
|
---|
32 | /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
|
---|
33 | /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
|
---|
34 | /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY */
|
---|
35 | /* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
|
---|
36 | /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE */
|
---|
37 | /* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
|
---|
38 | /********************************************************************************/
|
---|
39 |
|
---|
40 | #include <stdio.h>
|
---|
41 |
|
---|
42 | #include "tpm_debug.h"
|
---|
43 | #undef printf
|
---|
44 |
|
---|
45 | #if 0
|
---|
46 |
|
---|
47 | int swallow_rc = 0;
|
---|
48 |
|
---|
49 | int tpm_swallow_printf_args(const char *format, ...)
|
---|
50 | {
|
---|
51 | format = format; /* to silence compiler */
|
---|
52 | return 0;
|
---|
53 | }
|
---|
54 |
|
---|
55 | #else
|
---|
56 |
|
---|
57 | void TPM_PrintFourLimit(const char *string,
|
---|
58 | const unsigned char *buff, size_t buflen)
|
---|
59 | {
|
---|
60 | if (buff != NULL) {
|
---|
61 | switch (buflen) {
|
---|
62 | case 0:
|
---|
63 | TPMLIB_LogPrintf("%s (no data)\n", string);
|
---|
64 | break;
|
---|
65 | case 1:
|
---|
66 | TPMLIB_LogPrintf("%s %02x\n",
|
---|
67 | string,
|
---|
68 | buff[0]);
|
---|
69 | break;
|
---|
70 | case 2:
|
---|
71 | TPMLIB_LogPrintf("%s %02x %02x\n",
|
---|
72 | string,
|
---|
73 | buff[0],
|
---|
74 | buff[1]);
|
---|
75 | break;
|
---|
76 | case 3:
|
---|
77 | TPMLIB_LogPrintf("%s %02x %02x %02x\n",
|
---|
78 | string,
|
---|
79 | buff[0],
|
---|
80 | buff[1],
|
---|
81 | buff[2]);
|
---|
82 | break;
|
---|
83 | default:
|
---|
84 | TPMLIB_LogPrintf("%s %02x %02x %02x %02x\n",
|
---|
85 | string,
|
---|
86 | buff[0],
|
---|
87 | buff[1],
|
---|
88 | buff[2],
|
---|
89 | buff[3]);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | else {
|
---|
93 | TPMLIB_LogPrintf("%s null\n", string);
|
---|
94 | }
|
---|
95 | return;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /* TPM_PrintFour() prints a prefix plus 4 bytes of a buffer */
|
---|
99 |
|
---|
100 | void TPM_PrintFour(const char *string, const unsigned char* buff)
|
---|
101 | {
|
---|
102 | TPM_PrintFourLimit(string, buff, 4);
|
---|
103 | }
|
---|
104 |
|
---|
105 | #endif
|
---|
106 |
|
---|
107 | /* TPM_PrintAll() prints 'string', the length, and then the entire byte array
|
---|
108 | */
|
---|
109 |
|
---|
110 | void TPM_PrintAll(const char *string, const unsigned char* buff, uint32_t length)
|
---|
111 | {
|
---|
112 | uint32_t i;
|
---|
113 | int indent;
|
---|
114 |
|
---|
115 | if (buff != NULL) {
|
---|
116 | indent = TPMLIB_LogPrintf("%s length %u\n", string, length);
|
---|
117 | if (indent < 0)
|
---|
118 | return;
|
---|
119 |
|
---|
120 | for (i = 0 ; i < length ; i++) {
|
---|
121 | if (i && !( i % 16 ))
|
---|
122 | TPMLIB_LogPrintfA(0, "\n");
|
---|
123 |
|
---|
124 | if (!(i % 16))
|
---|
125 | TPMLIB_LogPrintf(" %.2X ", buff[i]);
|
---|
126 | else
|
---|
127 | TPMLIB_LogPrintfA(0, "%.2X ", buff[i]);
|
---|
128 | }
|
---|
129 | TPMLIB_LogPrintfA(0, "\n");
|
---|
130 | } else {
|
---|
131 | TPMLIB_LogPrintf("%s null\n", string);
|
---|
132 | }
|
---|
133 | return;
|
---|
134 | }
|
---|