11.5Sjdolecek/* $NetBSD: dkscan_bsdlabel.c,v 1.5 2020/04/11 17:21:16 jdolecek Exp $ */
21.1Smartin
31.1Smartin/*-
41.1Smartin * Copyright (c) 2007 The NetBSD Foundation, Inc.
51.1Smartin * All rights reserved.
61.1Smartin *
71.1Smartin * This code is derived from software contributed to The NetBSD Foundation
81.1Smartin * by Martin Husemann <martin@NetBSD.org>.
91.1Smartin *
101.1Smartin * Redistribution and use in source and binary forms, with or without
111.1Smartin * modification, are permitted provided that the following conditions
121.1Smartin * are met:
131.1Smartin * 1. Redistributions of source code must retain the above copyright
141.1Smartin *    notice, this list of conditions and the following disclaimer.
151.1Smartin * 2. Redistributions in binary form must reproduce the above copyright
161.1Smartin *    notice, this list of conditions and the following disclaimer in the
171.1Smartin *    documentation and/or other materials provided with the distribution.
181.1Smartin *
191.1Smartin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Smartin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Smartin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Smartin * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Smartin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Smartin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Smartin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Smartin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Smartin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Smartin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Smartin * POSSIBILITY OF SUCH DAMAGE.
301.1Smartin */
311.1Smartin
321.1Smartin#include <stdio.h>
331.1Smartin#include <stdlib.h>
341.1Smartin#include <stddef.h>
351.1Smartin#include <string.h>
361.1Smartin#include <unistd.h>
371.1Smartin#include <fcntl.h>
381.1Smartin#include <err.h>
391.1Smartin#include <util.h>
401.1Smartin#include <sys/disk.h>
411.5Sjdolecek#include <sys/buf.h>
421.1Smartin
431.1Smartin#include "dkscan_util.h"
441.1Smartin
451.4Schsstruct disk {
461.4Schs	const char	*dk_name;	/* disk name */
471.4Schs	int		dk_blkshift;	/* shift to convert DEV_BSIZE to blks */
481.4Schs};
491.4Schs
501.5Sjdolecekstatic struct buf *
511.5Sjdolecekgeteblk(int size)
521.5Sjdolecek{
531.5Sjdolecek	struct buf *bp = malloc(sizeof(*bp) + size);
541.5Sjdolecek
551.5Sjdolecek	bp->b_data = (void *)&bp[1];
561.5Sjdolecek
571.5Sjdolecek	return bp;
581.5Sjdolecek}
591.5Sjdolecek
601.5Sjdolecekstatic void
611.5Sjdolecekbrelse(struct buf *bp, int set)
621.5Sjdolecek{
631.5Sjdolecek	free(bp);
641.5Sjdolecek}
651.5Sjdolecek
661.1Smartin#include "dkwedge_bsdlabel.c"
671.1Smartin
681.3Sjoerg__dead static void usage(void);
691.3Sjoerg
701.1Smartinint
711.1Smartinmain(int argc, char **argv)
721.1Smartin{
731.1Smartin	struct disk d;
741.1Smartin	int ch;
751.1Smartin	char buf[PATH_MAX];
761.1Smartin	const char *devpart;
771.1Smartin
781.1Smartin	if (argc < 2)
791.1Smartin		usage();
801.1Smartin
811.1Smartin	while ((ch = getopt(argc, argv, "nv")) != -1) {
821.1Smartin		switch (ch) {
831.1Smartin		case 'n':
841.1Smartin			no_action = 1;
851.1Smartin			break;
861.1Smartin		case 'v':
871.1Smartin			verbose++;
881.1Smartin			break;
891.1Smartin		default:
901.1Smartin			usage();
911.1Smartin		}
921.1Smartin	}
931.1Smartin	if (optind >= argc)
941.1Smartin		usage();
951.1Smartin
961.1Smartin	disk_fd = opendisk(argv[optind], O_RDWR, buf, PATH_MAX, 0);
971.1Smartin	if (disk_fd == -1)
981.1Smartin		err(1, "%s", argv[optind]);
991.1Smartin
1001.1Smartin	devpart = strrchr(argv[optind], '/');
1011.1Smartin	if (devpart == NULL)
1021.1Smartin		devpart = argv[optind];
1031.1Smartin
1041.1Smartin	memset(&d, 0, sizeof(d));
1051.4Schs	d.dk_name  = devpart;
1061.1Smartin	dkwedge_discover_bsdlabel(&d, NULL);
1071.1Smartin
1081.1Smartin	close(disk_fd);
1091.1Smartin	return 0;
1101.1Smartin}
1111.1Smartin
1121.1Smartinvoid
1131.1Smartinusage(void)
1141.1Smartin{
1151.1Smartin	fprintf(stderr, "usage: %s [-vn] <diskname>\n"
1161.1Smartin	    "  where\n"
1171.1Smartin	    "    -n don't change anything, just print info\n"
1181.1Smartin	    "    -v be more verbose\n"
1191.1Smartin	    "    <diskname> device to scan\n",
1201.1Smartin	    getprogname());
1211.1Smartin	exit(1);
1221.1Smartin}
123