xref: /netbsd-src/sbin/disklabel/printlabel.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: printlabel.c,v 1.15 2009/01/18 21:15:14 apb Exp $	*/
2 
3 /*
4  * Copyright (c) 1987, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Symmetric Computer Systems.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
37 #endif
38 
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __RCSID("$NetBSD: printlabel.c,v 1.15 2009/01/18 21:15:14 apb Exp $");
42 #endif	/* not lint */
43 
44 #include <sys/param.h>
45 
46 #define DKTYPENAMES
47 #define FSTYPENAMES
48 #if HAVE_NBTOOL_CONFIG_H
49 #include <nbinclude/sys/disklabel.h>
50 #else
51 #include <sys/disklabel.h>
52 #endif /* HAVE_NBTOOL_CONFIG_H */
53 
54 #include <stdio.h>
55 
56 #include "extern.h"
57 
58 
59 void
60 showinfo(FILE *f, struct disklabel *lp, const char *specialname)
61 {
62 	int	i, j;
63 
64 	(void)fprintf(f, "# %s:\n", specialname);
65 	if ((unsigned) lp->d_type < DKMAXTYPES)
66 		(void)fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
67 	else
68 		(void)fprintf(f, "type: %d\n", lp->d_type);
69 	(void)fprintf(f, "disk: %.*s\n", (int) sizeof(lp->d_typename),
70 	    lp->d_typename);
71 	(void)fprintf(f, "label: %.*s\n", (int) sizeof(lp->d_packname),
72 	    lp->d_packname);
73 	(void)fprintf(f, "flags:");
74 	if (lp->d_flags & D_REMOVABLE)
75 		(void)fprintf(f, " removable");
76 	if (lp->d_flags & D_ECC)
77 		(void)fprintf(f, " ecc");
78 	if (lp->d_flags & D_BADSECT)
79 		(void)fprintf(f, " badsect");
80 	(void)fprintf(f, "\n");
81 	(void)fprintf(f, "bytes/sector: %" PRIu32 "\n", lp->d_secsize);
82 	(void)fprintf(f, "sectors/track: %" PRIu32 "\n", lp->d_nsectors);
83 	(void)fprintf(f, "tracks/cylinder: %" PRIu32 "\n", lp->d_ntracks);
84 	(void)fprintf(f, "sectors/cylinder: %" PRIu32 "\n", lp->d_secpercyl);
85 	(void)fprintf(f, "cylinders: %" PRIu32 "\n", lp->d_ncylinders);
86 	(void)fprintf(f, "total sectors: %" PRIu32 "\n", lp->d_secperunit);
87 	(void)fprintf(f, "rpm: %" PRIu32 "\n", lp->d_rpm);
88 	(void)fprintf(f, "interleave: %" PRIu32 "\n", lp->d_interleave);
89 	(void)fprintf(f, "trackskew: %" PRIu32 "\n", lp->d_trackskew);
90 	(void)fprintf(f, "cylinderskew: %" PRIu32 "\n", lp->d_cylskew);
91 	(void)fprintf(f, "headswitch: %" PRIu32 "\t\t# microseconds\n",
92 	    lp->d_headswitch);
93 	(void)fprintf(f, "track-to-track seek: %" PRIu32 "\t# microseconds\n",
94 	    lp->d_trkseek);
95 	(void)fprintf(f, "drivedata: ");
96 	for (i = NDDATA - 1; i >= 0; i--)
97 		if (lp->d_drivedata[i])
98 			break;
99 	if (i < 0)
100 		i = 0;
101 	for (j = 0; j <= i; j++)
102 		(void)fprintf(f, "%d ", lp->d_drivedata[j]);
103 	(void)fprintf(f, "\n\n");
104 	(void)fflush(f);
105 }
106 
107 void
108 showpartition(FILE *f, struct disklabel *lp, int i, int ctsformat)
109 {
110 	struct partition *pp = lp->d_partitions + i;
111 	if (pp->p_size == 0)
112 		return;
113 
114 	if (ctsformat && lp->d_secpercyl && lp->d_nsectors) {
115 		char sbuf[64], obuf[64];
116 
117 		(void)snprintf(sbuf, sizeof(sbuf), "%u/%u/%u",
118 		    pp->p_size/lp->d_secpercyl,
119 		    (pp->p_size%lp->d_secpercyl) / lp->d_nsectors,
120 		    pp->p_size%lp->d_nsectors);
121 
122 		(void)snprintf(obuf, sizeof(obuf), "%u/%u/%u",
123 		    pp->p_offset/lp->d_secpercyl,
124 		    (pp->p_offset%lp->d_secpercyl) / lp->d_nsectors,
125 		    pp->p_offset%lp->d_nsectors);
126 
127 		(void)fprintf(f, " %c: %9s %9s ",
128 		    'a' + i, sbuf, obuf);
129 	} else {
130 		(void)fprintf(f, " %c: %9u %9u ", 'a' + i,
131 		    pp->p_size, pp->p_offset);
132 	}
133 
134 	if ((unsigned) pp->p_fstype < FSMAXTYPES)
135 		(void)fprintf(f, "%10.10s", fstypenames[pp->p_fstype]);
136 	else
137 		(void)fprintf(f, "%10d", pp->p_fstype);
138 
139 	switch (pp->p_fstype) {
140 	case FS_UNUSED:				/* XXX */
141 		(void)fprintf(f, "  %5u %5u %5.5s ",
142 		    pp->p_fsize, pp->p_fsize * pp->p_frag, "");
143 		break;
144 
145 	case FS_BSDFFS:
146 	case FS_ADOS:
147 	case FS_APPLEUFS:
148 		(void)fprintf(f, "  %5u %5u %5u ",
149 		    pp->p_fsize, pp->p_fsize * pp->p_frag, pp->p_cpg);
150 		break;
151 
152 	case FS_BSDLFS:
153 		(void)fprintf(f, "  %5u %5u %5u ",
154 		    pp->p_fsize, pp->p_fsize * pp->p_frag, pp->p_sgs);
155 		break;
156 
157 	case FS_EX2FS:
158 		(void)fprintf(f, "  %5u %5u       ",
159 		    pp->p_fsize, pp->p_fsize * pp->p_frag);
160 		break;
161 
162 	case FS_ISO9660:
163 		(void)fprintf(f, "  %6u            ", pp->p_cdsession);
164 		break;
165 
166 	default:
167 		(void)fprintf(f, "%20.20s", "");
168 		break;
169 	}
170 	if (lp->d_secpercyl != 0) {
171 		(void)fprintf(f, " # (Cyl. %6u",
172 		    pp->p_offset / lp->d_secpercyl);
173 
174 		if (pp->p_offset > lp->d_secperunit)
175 		    putc('+', f);
176 		else if (pp->p_offset % lp->d_secpercyl)
177 		    putc('*', f);
178 		else
179 		    putc(' ', f);
180 
181 		(void)fprintf(f, "- %6u",
182 		    (pp->p_offset +
183 		    pp->p_size + lp->d_secpercyl - 1) /
184 		    lp->d_secpercyl - 1);
185 
186 		if ((pp->p_offset + pp->p_size) > lp->d_secperunit)
187 		    putc('+', f);
188 		else if ((pp->p_offset + pp->p_size) % lp->d_secpercyl)
189 		    putc('*', f);
190 
191 		(void)fprintf(f, ")\n");
192 	} else
193 		(void)fprintf(f, "\n");
194 }
195 
196 void
197 showpartitions(FILE *f, struct disklabel *lp, int ctsformat)
198 {
199 	int	i;
200 
201 	(void)fprintf(f, "%d partitions:\n", lp->d_npartitions);
202 	(void)fprintf(f,
203 	    "#        size    offset     fstype [fsize bsize cpg/sgs]\n");
204 
205 	for (i = 0; i < lp->d_npartitions; i++)
206 		showpartition(f, lp, i, ctsformat);
207 	(void)fflush(f);
208 }
209