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 *
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/proc.h>
40#include <sys/disk.h>
41#include <sys/disklabel.h>
42
43#include "dkscan_util.h"
44
45int verbose = 0;	/* are we verbose? */
46int no_action = 0;	/* don't do anything, just print info */
47int disk_fd = -1;	/* file descriptor for disk access */
48
49
50u_int
51dkcksum(struct disklabel *lp)
52{
53	return dkcksum_sized(lp, lp->d_npartitions);
54}
55
56u_int
57dkcksum_sized(struct disklabel *lp, size_t npartitions)
58{
59        u_short *start, *end;
60        u_short sum = 0;
61
62        start = (u_short *)lp;
63        end = (u_short *)&lp->d_partitions[npartitions];
64        while (start < end)
65                sum ^= *start++;
66        return (sum);
67}
68
69int
70dkwedge_read(struct disk *pdk, struct vnode *vp, daddr_t blkno,
71	void *tbuf, size_t len)
72{
73	if (pread(disk_fd, tbuf, len, blkno * BLOCK_SIZE) < 0)
74		return errno;
75	return 0;
76}
77
78int
79dkwedge_add(struct dkwedge_info *dkw)
80{
81	printf("wedge \"%s\" type \"%s\" start %" PRIu64 " size %" PRIu64,
82	    dkw->dkw_wname, dkw->dkw_ptype, dkw->dkw_offset, dkw->dkw_size);
83
84	if (no_action) {
85		printf("\n");
86		return 0;
87	}
88
89	if (ioctl(disk_fd, DIOCAWEDGE, dkw) == -1)
90		err(1, "addwedge");
91
92	printf(": %s\n", dkw->dkw_devname);
93	return 0;
94}
95
96void
97aprint_error(const char *format, ...)
98{
99	va_list ap;
100
101	va_start(ap, format);
102	vfprintf(stderr, format, ap);
103	va_end(ap);
104}
105
106void
107aprint_verbose(const char *format, ...)
108{
109	va_list ap;
110
111	if (!verbose) return;
112
113	va_start(ap, format);
114	vfprintf(stdout, format, ap);
115	va_end(ap);
116}
117
118