xref: /titanic_51/usr/src/lib/fm/libdiskstatus/common/ds_util.c (revision 24fe0b3bf671e123467ce1df0b67cadd3614c8e4)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <ctype.h>
29 #include <libdiskstatus.h>
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include "ds_impl.h"
35 
36 boolean_t ds_debug;
37 
38 /*PRINTFLIKE1*/
39 void
40 dprintf(const char *fmt, ...)
41 {
42 	va_list ap;
43 
44 	if (!ds_debug)
45 		return;
46 
47 	va_start(ap, fmt);
48 	(void) vprintf(fmt, ap);
49 	va_end(ap);
50 }
51 
52 void
53 ddump(const char *label, const void *data, size_t length)
54 {
55 	int byte_count;
56 	int i;
57 #define	LINEBUFLEN 128
58 	char linebuf[LINEBUFLEN];
59 	char *linep;
60 	int bufleft, len;
61 	const char *start = data;
62 
63 	if (!ds_debug)
64 		return;
65 
66 	if (label != NULL)
67 		dprintf("%s\n", label);
68 
69 	linep = linebuf;
70 	bufleft = LINEBUFLEN;
71 
72 	for (byte_count = 0; byte_count < length; byte_count += i) {
73 
74 		(void) snprintf(linep, bufleft, "0x%08x ", byte_count);
75 		len = strlen(linep);
76 		bufleft -= len;
77 		linep += len;
78 
79 		/*
80 		 * Inner loop processes 16 bytes at a time, or less
81 		 * if we have less than 16 bytes to go
82 		 */
83 		for (i = 0; (i < 16) && ((byte_count + i) < length); i++) {
84 			(void) snprintf(linep, bufleft, "%02X", (unsigned int)
85 			    (unsigned char) start[byte_count + i]);
86 
87 			len = strlen(linep);
88 			bufleft -= len;
89 			linep += len;
90 
91 			if (bufleft >= 2) {
92 				if (i == 7)
93 					*linep = '-';
94 				else
95 					*linep = ' ';
96 
97 				--bufleft;
98 				++linep;
99 			}
100 		}
101 
102 		/*
103 		 * If i is less than 16, then we had less than 16 bytes
104 		 * written to the output.  We need to fixup the alignment
105 		 * to allow the "text" output to be aligned
106 		 */
107 		if (i < 16) {
108 			int numspaces = (16 - i) * 3;
109 			while (numspaces-- > 0) {
110 				if (bufleft >= 2) {
111 					*linep = ' ';
112 					--bufleft;
113 					linep++;
114 				}
115 			}
116 		}
117 
118 		if (bufleft >= 2) {
119 			*linep = ' ';
120 			--bufleft;
121 			++linep;
122 		}
123 
124 		for (i = 0; (i < 16) && ((byte_count + i) < length); i++) {
125 			int subscript = byte_count + i;
126 			char ch =  (isprint(start[subscript]) ?
127 			    start[subscript] : '.');
128 
129 			if (bufleft >= 2) {
130 				*linep = ch;
131 				--bufleft;
132 				++linep;
133 			}
134 		}
135 
136 		linebuf[LINEBUFLEN - bufleft] = 0;
137 
138 		dprintf("%s\n", linebuf);
139 
140 		linep = linebuf;
141 		bufleft = LINEBUFLEN;
142 	}
143 
144 }
145 
146 const char *
147 disk_status_errmsg(int error)
148 {
149 	switch (error) {
150 	case EDS_NOMEM:
151 		return ("memory allocation failure");
152 	case EDS_CANT_OPEN:
153 		return ("failed to open device");
154 	case EDS_NO_TRANSPORT:
155 		return ("no supported communication protocol");
156 	case EDS_NOT_SUPPORTED:
157 		return ("disk status information not supported");
158 	case EDS_NOT_SIMULATOR:
159 		return ("not a valid simulator file");
160 	case EDS_IO:
161 		return ("I/O error from device");
162 	default:
163 		return ("unknown error");
164 	}
165 }
166 
167 int
168 ds_set_errno(disk_status_t *dsp, int error)
169 {
170 	dsp->ds_error = error;
171 	return (-1);
172 }
173