snj doesn't like my accent, so use proper English month names.
[netbsd-mini2440.git] / sbin / dkscan_bsdlabel / dkscan_util.c
blobc9f9da0423388c704711464560905eb140bd005a
1 /* $NetBSD: dkscan_util.c,v 1.1 2007/03/01 22:01:30 martin Exp $ */
3 /*-
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Martin Husemann <martin@NetBSD.org>.
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.
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.
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>
43 #include "dkscan_util.h"
45 int verbose = 0; /* are we verbose? */
46 int no_action = 0; /* don't do anything, just print info */
47 int disk_fd = -1; /* file descriptor for disk access */
50 u_int
51 dkcksum(struct disklabel *lp)
53 return dkcksum_sized(lp, lp->d_npartitions);
56 u_int
57 dkcksum_sized(struct disklabel *lp, size_t npartitions)
59 u_short *start, *end;
60 u_short sum = 0;
62 start = (u_short *)lp;
63 end = (u_short *)&lp->d_partitions[npartitions];
64 while (start < end)
65 sum ^= *start++;
66 return (sum);
69 int
70 dkwedge_read(struct disk *pdk, struct vnode *vp, daddr_t blkno,
71 void *tbuf, size_t len)
73 if (pread(disk_fd, tbuf, len, blkno * BLOCK_SIZE) < 0)
74 return errno;
75 return 0;
78 int
79 dkwedge_add(struct dkwedge_info *dkw)
81 printf("wedge \"%s\" type \"%s\" start %" PRIu64 " size %" PRIu64,
82 dkw->dkw_wname, dkw->dkw_ptype, dkw->dkw_offset, dkw->dkw_size);
84 if (no_action) {
85 printf("\n");
86 return 0;
89 if (ioctl(disk_fd, DIOCAWEDGE, dkw) == -1)
90 err(1, "addwedge");
92 printf(": %s\n", dkw->dkw_devname);
93 return 0;
96 void
97 aprint_error(const char *format, ...)
99 va_list ap;
101 va_start(ap, format);
102 vfprintf(stderr, format, ap);
103 va_end(ap);
106 void
107 aprint_verbose(const char *format, ...)
109 va_list ap;
111 if (!verbose) return;
113 va_start(ap, format);
114 vfprintf(stdout, format, ap);
115 va_end(ap);