1*171fa94dSjdolecek /* $NetBSD: dkscan_bsdlabel.c,v 1.5 2020/04/11 17:21:16 jdolecek Exp $ */
2c6eb40afSmartin
3c6eb40afSmartin /*-
4c6eb40afSmartin * Copyright (c) 2007 The NetBSD Foundation, Inc.
5c6eb40afSmartin * All rights reserved.
6c6eb40afSmartin *
7c6eb40afSmartin * This code is derived from software contributed to The NetBSD Foundation
8c6eb40afSmartin * by Martin Husemann <martin@NetBSD.org>.
9c6eb40afSmartin *
10c6eb40afSmartin * Redistribution and use in source and binary forms, with or without
11c6eb40afSmartin * modification, are permitted provided that the following conditions
12c6eb40afSmartin * are met:
13c6eb40afSmartin * 1. Redistributions of source code must retain the above copyright
14c6eb40afSmartin * notice, this list of conditions and the following disclaimer.
15c6eb40afSmartin * 2. Redistributions in binary form must reproduce the above copyright
16c6eb40afSmartin * notice, this list of conditions and the following disclaimer in the
17c6eb40afSmartin * documentation and/or other materials provided with the distribution.
18c6eb40afSmartin *
19c6eb40afSmartin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20c6eb40afSmartin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21c6eb40afSmartin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22c6eb40afSmartin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23c6eb40afSmartin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24c6eb40afSmartin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25c6eb40afSmartin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26c6eb40afSmartin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27c6eb40afSmartin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28c6eb40afSmartin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29c6eb40afSmartin * POSSIBILITY OF SUCH DAMAGE.
30c6eb40afSmartin */
31c6eb40afSmartin
32c6eb40afSmartin #include <stdio.h>
33c6eb40afSmartin #include <stdlib.h>
34c6eb40afSmartin #include <stddef.h>
35c6eb40afSmartin #include <string.h>
36c6eb40afSmartin #include <unistd.h>
37c6eb40afSmartin #include <fcntl.h>
38c6eb40afSmartin #include <err.h>
39c6eb40afSmartin #include <util.h>
40c6eb40afSmartin #include <sys/disk.h>
41*171fa94dSjdolecek #include <sys/buf.h>
42c6eb40afSmartin
43c6eb40afSmartin #include "dkscan_util.h"
44c6eb40afSmartin
4571997035Schs struct disk {
4671997035Schs const char *dk_name; /* disk name */
4771997035Schs int dk_blkshift; /* shift to convert DEV_BSIZE to blks */
4871997035Schs };
4971997035Schs
50*171fa94dSjdolecek static struct buf *
geteblk(int size)51*171fa94dSjdolecek geteblk(int size)
52*171fa94dSjdolecek {
53*171fa94dSjdolecek struct buf *bp = malloc(sizeof(*bp) + size);
54*171fa94dSjdolecek
55*171fa94dSjdolecek bp->b_data = (void *)&bp[1];
56*171fa94dSjdolecek
57*171fa94dSjdolecek return bp;
58*171fa94dSjdolecek }
59*171fa94dSjdolecek
60*171fa94dSjdolecek static void
brelse(struct buf * bp,int set)61*171fa94dSjdolecek brelse(struct buf *bp, int set)
62*171fa94dSjdolecek {
63*171fa94dSjdolecek free(bp);
64*171fa94dSjdolecek }
65*171fa94dSjdolecek
66c6eb40afSmartin #include "dkwedge_bsdlabel.c"
67c6eb40afSmartin
6822db016bSjoerg __dead static void usage(void);
6922db016bSjoerg
70c6eb40afSmartin int
main(int argc,char ** argv)71c6eb40afSmartin main(int argc, char **argv)
72c6eb40afSmartin {
73c6eb40afSmartin struct disk d;
74c6eb40afSmartin int ch;
75c6eb40afSmartin char buf[PATH_MAX];
76c6eb40afSmartin const char *devpart;
77c6eb40afSmartin
78c6eb40afSmartin if (argc < 2)
79c6eb40afSmartin usage();
80c6eb40afSmartin
81c6eb40afSmartin while ((ch = getopt(argc, argv, "nv")) != -1) {
82c6eb40afSmartin switch (ch) {
83c6eb40afSmartin case 'n':
84c6eb40afSmartin no_action = 1;
85c6eb40afSmartin break;
86c6eb40afSmartin case 'v':
87c6eb40afSmartin verbose++;
88c6eb40afSmartin break;
89c6eb40afSmartin default:
90c6eb40afSmartin usage();
91c6eb40afSmartin }
92c6eb40afSmartin }
93c6eb40afSmartin if (optind >= argc)
94c6eb40afSmartin usage();
95c6eb40afSmartin
96c6eb40afSmartin disk_fd = opendisk(argv[optind], O_RDWR, buf, PATH_MAX, 0);
97c6eb40afSmartin if (disk_fd == -1)
98c6eb40afSmartin err(1, "%s", argv[optind]);
99c6eb40afSmartin
100c6eb40afSmartin devpart = strrchr(argv[optind], '/');
101c6eb40afSmartin if (devpart == NULL)
102c6eb40afSmartin devpart = argv[optind];
103c6eb40afSmartin
104c6eb40afSmartin memset(&d, 0, sizeof(d));
10571997035Schs d.dk_name = devpart;
106c6eb40afSmartin dkwedge_discover_bsdlabel(&d, NULL);
107c6eb40afSmartin
108c6eb40afSmartin close(disk_fd);
109c6eb40afSmartin return 0;
110c6eb40afSmartin }
111c6eb40afSmartin
112c6eb40afSmartin void
usage(void)113c6eb40afSmartin usage(void)
114c6eb40afSmartin {
115c6eb40afSmartin fprintf(stderr, "usage: %s [-vn] <diskname>\n"
116c6eb40afSmartin " where\n"
117c6eb40afSmartin " -n don't change anything, just print info\n"
118c6eb40afSmartin " -v be more verbose\n"
119c6eb40afSmartin " <diskname> device to scan\n",
120c6eb40afSmartin getprogname());
121c6eb40afSmartin exit(1);
122c6eb40afSmartin }
123