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