xref: /netbsd-src/sbin/disklabel/printlabel.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: printlabel.c,v 1.14 2005/08/27 23:34:52 uwe 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.14 2005/08/27 23:34:52 uwe 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 #ifndef PRIu32
51 #define PRIu32 "u"
52 #endif
53 #else
54 #include <sys/disklabel.h>
55 #endif /* HAVE_NBTOOL_CONFIG_H */
56 
57 #include <stdio.h>
58 
59 #include "extern.h"
60 
61 
62 void
63 showinfo(FILE *f, struct disklabel *lp, const char *specialname)
64 {
65 	int	i, j;
66 
67 	(void)fprintf(f, "# %s:\n", specialname);
68 	if ((unsigned) lp->d_type < DKMAXTYPES)
69 		(void)fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
70 	else
71 		(void)fprintf(f, "type: %d\n", lp->d_type);
72 	(void)fprintf(f, "disk: %.*s\n", (int) sizeof(lp->d_typename),
73 	    lp->d_typename);
74 	(void)fprintf(f, "label: %.*s\n", (int) sizeof(lp->d_packname),
75 	    lp->d_packname);
76 	(void)fprintf(f, "flags:");
77 	if (lp->d_flags & D_REMOVABLE)
78 		(void)fprintf(f, " removable");
79 	if (lp->d_flags & D_ECC)
80 		(void)fprintf(f, " ecc");
81 	if (lp->d_flags & D_BADSECT)
82 		(void)fprintf(f, " badsect");
83 	(void)fprintf(f, "\n");
84 	(void)fprintf(f, "bytes/sector: %" PRIu32 "\n", lp->d_secsize);
85 	(void)fprintf(f, "sectors/track: %" PRIu32 "\n", lp->d_nsectors);
86 	(void)fprintf(f, "tracks/cylinder: %" PRIu32 "\n", lp->d_ntracks);
87 	(void)fprintf(f, "sectors/cylinder: %" PRIu32 "\n", lp->d_secpercyl);
88 	(void)fprintf(f, "cylinders: %" PRIu32 "\n", lp->d_ncylinders);
89 	(void)fprintf(f, "total sectors: %" PRIu32 "\n", lp->d_secperunit);
90 	(void)fprintf(f, "rpm: %" PRIu32 "\n", lp->d_rpm);
91 	(void)fprintf(f, "interleave: %" PRIu32 "\n", lp->d_interleave);
92 	(void)fprintf(f, "trackskew: %" PRIu32 "\n", lp->d_trackskew);
93 	(void)fprintf(f, "cylinderskew: %" PRIu32 "\n", lp->d_cylskew);
94 	(void)fprintf(f, "headswitch: %" PRIu32 "\t\t# microseconds\n",
95 	    lp->d_headswitch);
96 	(void)fprintf(f, "track-to-track seek: %" PRIu32 "\t# microseconds\n",
97 	    lp->d_trkseek);
98 	(void)fprintf(f, "drivedata: ");
99 	for (i = NDDATA - 1; i >= 0; i--)
100 		if (lp->d_drivedata[i])
101 			break;
102 	if (i < 0)
103 		i = 0;
104 	for (j = 0; j <= i; j++)
105 		(void)fprintf(f, "%d ", lp->d_drivedata[j]);
106 	(void)fprintf(f, "\n\n");
107 	(void)fflush(f);
108 }
109 
110 void
111 showpartition(FILE *f, struct disklabel *lp, int i, int ctsformat)
112 {
113 	struct partition *pp = lp->d_partitions + i;
114 	if (pp->p_size == 0)
115 		return;
116 
117 	if (ctsformat && lp->d_secpercyl && lp->d_nsectors) {
118 		char sbuf[64], obuf[64];
119 
120 		(void)snprintf(sbuf, sizeof(sbuf), "%u/%u/%u",
121 		    pp->p_size/lp->d_secpercyl,
122 		    (pp->p_size%lp->d_secpercyl) / lp->d_nsectors,
123 		    pp->p_size%lp->d_nsectors);
124 
125 		(void)snprintf(obuf, sizeof(obuf), "%u/%u/%u",
126 		    pp->p_offset/lp->d_secpercyl,
127 		    (pp->p_offset%lp->d_secpercyl) / lp->d_nsectors,
128 		    pp->p_offset%lp->d_nsectors);
129 
130 		(void)fprintf(f, " %c: %9s %9s ",
131 		    'a' + i, sbuf, obuf);
132 	} else {
133 		(void)fprintf(f, " %c: %9u %9u ", 'a' + i,
134 		    pp->p_size, pp->p_offset);
135 	}
136 
137 	if ((unsigned) pp->p_fstype < FSMAXTYPES)
138 		(void)fprintf(f, "%10.10s", fstypenames[pp->p_fstype]);
139 	else
140 		(void)fprintf(f, "%10d", pp->p_fstype);
141 
142 	switch (pp->p_fstype) {
143 	case FS_UNUSED:				/* XXX */
144 		(void)fprintf(f, "  %5u %5u %5.5s ",
145 		    pp->p_fsize, pp->p_fsize * pp->p_frag, "");
146 		break;
147 
148 	case FS_BSDFFS:
149 	case FS_ADOS:
150 	case FS_APPLEUFS:
151 		(void)fprintf(f, "  %5u %5u %5u ",
152 		    pp->p_fsize, pp->p_fsize * pp->p_frag, pp->p_cpg);
153 		break;
154 
155 	case FS_BSDLFS:
156 		(void)fprintf(f, "  %5u %5u %5u ",
157 		    pp->p_fsize, pp->p_fsize * pp->p_frag, pp->p_sgs);
158 		break;
159 
160 	case FS_EX2FS:
161 		(void)fprintf(f, "  %5u %5u       ",
162 		    pp->p_fsize, pp->p_fsize * pp->p_frag);
163 		break;
164 
165 	case FS_ISO9660:
166 		(void)fprintf(f, "  %6u            ", pp->p_cdsession);
167 		break;
168 
169 	default:
170 		(void)fprintf(f, "%20.20s", "");
171 		break;
172 	}
173 	if (lp->d_secpercyl != 0) {
174 		(void)fprintf(f, " # (Cyl. %6u",
175 		    pp->p_offset / lp->d_secpercyl);
176 
177 		if (pp->p_offset > lp->d_secperunit)
178 		    putc('+', f);
179 		else if (pp->p_offset % lp->d_secpercyl)
180 		    putc('*', f);
181 		else
182 		    putc(' ', f);
183 
184 		(void)fprintf(f, "- %6u",
185 		    (pp->p_offset +
186 		    pp->p_size + lp->d_secpercyl - 1) /
187 		    lp->d_secpercyl - 1);
188 
189 		if ((pp->p_offset + pp->p_size) > lp->d_secperunit)
190 		    putc('+', f);
191 		else if ((pp->p_offset + pp->p_size) % lp->d_secpercyl)
192 		    putc('*', f);
193 
194 		(void)fprintf(f, ")\n");
195 	} else
196 		(void)fprintf(f, "\n");
197 }
198 
199 void
200 showpartitions(FILE *f, struct disklabel *lp, int ctsformat)
201 {
202 	int	i;
203 
204 	(void)fprintf(f, "%d partitions:\n", lp->d_npartitions);
205 	(void)fprintf(f,
206 	    "#        size    offset     fstype [fsize bsize cpg/sgs]\n");
207 
208 	for (i = 0; i < lp->d_npartitions; i++)
209 		showpartition(f, lp, i, ctsformat);
210 	(void)fflush(f);
211 }
212