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
59void
60showinfo(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: %" PRIu16 "\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, "%" PRIu32 " ", lp->d_drivedata[j]);
103	(void)fprintf(f, "\n\n");
104	(void)fflush(f);
105}
106
107void
108showpartition(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),
118                    "%" PRIu32 "/%" PRIu32 "/%" PRIu32,
119		    pp->p_size/lp->d_secpercyl,
120		    (pp->p_size%lp->d_secpercyl) / lp->d_nsectors,
121		    pp->p_size%lp->d_nsectors);
122
123		(void)snprintf(obuf, sizeof(obuf),
124                    "%" PRIu32 "/%" PRIu32 "/%" PRIu32,
125		    pp->p_offset/lp->d_secpercyl,
126		    (pp->p_offset%lp->d_secpercyl) / lp->d_nsectors,
127		    pp->p_offset%lp->d_nsectors);
128
129		(void)fprintf(f, " %c: %9s %9s ",
130		    'a' + i, sbuf, obuf);
131	} else {
132		(void)fprintf(f, " %c: %9" PRIu32 " %9" PRIu32 " ",
133		    'a' + i, pp->p_size, pp->p_offset);
134	}
135
136	if ((unsigned) pp->p_fstype < FSMAXTYPES)
137		(void)fprintf(f, "%10.10s", fstypenames[pp->p_fstype]);
138	else
139		(void)fprintf(f, "%10" PRIu8, pp->p_fstype);
140
141	switch (pp->p_fstype) {
142	case FS_UNUSED:				/* XXX */
143		(void)fprintf(f, "  %5" PRIu32 " %5" PRIu64 " %5.5s ",
144		    pp->p_fsize, (uint64_t)pp->p_fsize * pp->p_frag, "");
145		break;
146
147	case FS_BSDFFS:
148	case FS_ADOS:
149	case FS_APPLEUFS:
150		(void)fprintf(f, "  %5" PRIu32 " %5" PRIu64 " %5" PRIu16 " ",
151		    pp->p_fsize, (uint64_t)pp->p_fsize * pp->p_frag, pp->p_cpg);
152		break;
153
154	case FS_BSDLFS:
155		(void)fprintf(f, "  %5" PRIu32 " %5" PRIu64 " %5" PRIu16 " ",
156		    pp->p_fsize, (uint64_t)pp->p_fsize * pp->p_frag, pp->p_sgs);
157		break;
158
159	case FS_EX2FS:
160		(void)fprintf(f, "  %5" PRIu32 " %5" PRIu64 "       ",
161		    pp->p_fsize, (uint64_t)pp->p_fsize * pp->p_frag);
162		break;
163
164	case FS_ISO9660:
165		(void)fprintf(f, "  %6" PRIu32 "            ",
166		    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. %6" PRIu32,
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, "- %6" PRIu32,
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
199void
200showpartitions(FILE *f, struct disklabel *lp, int ctsformat)
201{
202	int	i;
203
204	(void)fprintf(f, "%" PRIu16 " 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