xref: /netbsd-src/sbin/dkscan_bsdlabel/dkscan_util.c (revision 4467f3ec3d0f85366e4af088b419c13e4f73ee8f)
1 /* $NetBSD: dkscan_util.c,v 1.3 2013/01/01 18:44:27 dsl 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <stdarg.h>
37 #include <err.h>
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/proc.h>
41 #include <sys/disk.h>
42 #include <sys/disklabel.h>
43 
44 #include "dkscan_util.h"
45 
46 int verbose = 0;	/* are we verbose? */
47 int no_action = 0;	/* don't do anything, just print info */
48 int disk_fd = -1;	/* file descriptor for disk access */
49 
50 
51 u_int
dkcksum(struct disklabel * lp)52 dkcksum(struct disklabel *lp)
53 {
54 	return dkcksum_sized(lp, lp->d_npartitions);
55 }
56 
57 u_int
dkcksum_sized(struct disklabel * lp,size_t npartitions)58 dkcksum_sized(struct disklabel *lp, size_t npartitions)
59 {
60         u_short *start, *end;
61         u_short sum = 0;
62 
63         start = (u_short *)lp;
64         end = (u_short *)&lp->d_partitions[npartitions];
65         while (start < end)
66                 sum ^= *start++;
67         return (sum);
68 }
69 
70 int
dkwedge_read(struct disk * pdk,struct vnode * vp,daddr_t blkno,void * tbuf,size_t len)71 dkwedge_read(struct disk *pdk, struct vnode *vp, daddr_t blkno,
72 	void *tbuf, size_t len)
73 {
74 	if (pread(disk_fd, tbuf, len, blkno * BLOCK_SIZE) < 0)
75 		return errno;
76 	return 0;
77 }
78 
79 int
dkwedge_add(struct dkwedge_info * dkw)80 dkwedge_add(struct dkwedge_info *dkw)
81 {
82 	printf("wedge \"%s\" type \"%s\" start %" PRIu64 " size %" PRIu64,
83 	    dkw->dkw_wname, dkw->dkw_ptype, dkw->dkw_offset, dkw->dkw_size);
84 
85 	if (no_action) {
86 		printf("\n");
87 		return 0;
88 	}
89 
90 	if (ioctl(disk_fd, DIOCAWEDGE, dkw) == -1)
91 		err(1, "addwedge");
92 
93 	printf(": %s\n", dkw->dkw_devname);
94 	return 0;
95 }
96 
97 void
aprint_error(const char * format,...)98 aprint_error(const char *format, ...)
99 {
100 	va_list ap;
101 
102 	va_start(ap, format);
103 	vfprintf(stderr, format, ap);
104 	va_end(ap);
105 }
106 
107 void
aprint_verbose(const char * format,...)108 aprint_verbose(const char *format, ...)
109 {
110 	va_list ap;
111 
112 	if (!verbose) return;
113 
114 	va_start(ap, format);
115 	vfprintf(stdout, format, ap);
116 	va_end(ap);
117 }
118 
119