xref: /netbsd-src/sbin/dkscan_bsdlabel/dkscan_util.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /* $NetBSD: dkscan_util.c,v 1.1 2007/03/01 22:01:30 martin Exp $ */
2 
3 /*-
4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Martin Husemann <martin@NetBSD.org>.
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. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <errno.h>
43 #include <stdarg.h>
44 #include <err.h>
45 #include <sys/param.h>
46 #include <sys/proc.h>
47 #include <sys/disk.h>
48 #include <sys/disklabel.h>
49 
50 #include "dkscan_util.h"
51 
52 int verbose = 0;	/* are we verbose? */
53 int no_action = 0;	/* don't do anything, just print info */
54 int disk_fd = -1;	/* file descriptor for disk access */
55 
56 
57 u_int
58 dkcksum(struct disklabel *lp)
59 {
60 	return dkcksum_sized(lp, lp->d_npartitions);
61 }
62 
63 u_int
64 dkcksum_sized(struct disklabel *lp, size_t npartitions)
65 {
66         u_short *start, *end;
67         u_short sum = 0;
68 
69         start = (u_short *)lp;
70         end = (u_short *)&lp->d_partitions[npartitions];
71         while (start < end)
72                 sum ^= *start++;
73         return (sum);
74 }
75 
76 int
77 dkwedge_read(struct disk *pdk, struct vnode *vp, daddr_t blkno,
78 	void *tbuf, size_t len)
79 {
80 	if (pread(disk_fd, tbuf, len, blkno * BLOCK_SIZE) < 0)
81 		return errno;
82 	return 0;
83 }
84 
85 int
86 dkwedge_add(struct dkwedge_info *dkw)
87 {
88 	printf("wedge \"%s\" type \"%s\" start %" PRIu64 " size %" PRIu64,
89 	    dkw->dkw_wname, dkw->dkw_ptype, dkw->dkw_offset, dkw->dkw_size);
90 
91 	if (no_action) {
92 		printf("\n");
93 		return 0;
94 	}
95 
96 	if (ioctl(disk_fd, DIOCAWEDGE, dkw) == -1)
97 		err(1, "addwedge");
98 
99 	printf(": %s\n", dkw->dkw_devname);
100 	return 0;
101 }
102 
103 void
104 aprint_error(const char *format, ...)
105 {
106 	va_list ap;
107 
108 	va_start(ap, format);
109 	vfprintf(stderr, format, ap);
110 	va_end(ap);
111 }
112 
113 void
114 aprint_verbose(const char *format, ...)
115 {
116 	va_list ap;
117 
118 	if (!verbose) return;
119 
120 	va_start(ap, format);
121 	vfprintf(stdout, format, ap);
122 	va_end(ap);
123 }
124 
125