xref: /netbsd-src/lib/libutil/disklabel_scan.c (revision c5eb4ab6019d3068d6831a742ba274377a333a48)
1*c5eb4ab6Slukem /* $NetBSD: disklabel_scan.c,v 1.3 2009/01/18 12:13:03 lukem Exp $ */
2448f6217Selric 
3448f6217Selric /*-
4448f6217Selric  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5448f6217Selric  * All rights reserved.
6448f6217Selric  *
7448f6217Selric  * This code is derived from software contributed to The NetBSD Foundation
8448f6217Selric  * by Roland C. Dowdeswell.
9448f6217Selric  *
10448f6217Selric  * Redistribution and use in source and binary forms, with or without
11448f6217Selric  * modification, are permitted provided that the following conditions
12448f6217Selric  * are met:
13448f6217Selric  * 1. Redistributions of source code must retain the above copyright
14448f6217Selric  *    notice, this list of conditions and the following disclaimer.
15448f6217Selric  * 2. Redistributions in binary form must reproduce the above copyright
16448f6217Selric  *    notice, this list of conditions and the following disclaimer in the
17448f6217Selric  *    documentation and/or other materials provided with the distribution.
18448f6217Selric  *
19448f6217Selric  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20448f6217Selric  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21448f6217Selric  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22448f6217Selric  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23448f6217Selric  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24448f6217Selric  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25448f6217Selric  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26448f6217Selric  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27448f6217Selric  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28448f6217Selric  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29448f6217Selric  * POSSIBILITY OF SUCH DAMAGE.
30448f6217Selric  */
31448f6217Selric 
32448f6217Selric #include <sys/cdefs.h>
33448f6217Selric #ifndef lint
34448f6217Selric __COPYRIGHT(
35448f6217Selric "@(#) Copyright (c) 2002\
36448f6217Selric 	The NetBSD Foundation, Inc.  All rights reserved.");
37*c5eb4ab6Slukem __RCSID("$NetBSD: disklabel_scan.c,v 1.3 2009/01/18 12:13:03 lukem Exp $");
38448f6217Selric #endif
39448f6217Selric 
40448f6217Selric #include <string.h>
41448f6217Selric #include <unistd.h>
42448f6217Selric #include <util.h>
43448f6217Selric 
44448f6217Selric #include <sys/disklabel.h>
45448f6217Selric 
46448f6217Selric #define SCAN_INCR	4
47448f6217Selric 
48448f6217Selric int
disklabel_scan(struct disklabel * lp,char * buf,size_t buflen)49448f6217Selric disklabel_scan(struct disklabel *lp, char *buf, size_t buflen)
50448f6217Selric {
51*c5eb4ab6Slukem 	size_t	i;
52448f6217Selric 
53448f6217Selric 	/* scan for the correct magic numbers. */
54448f6217Selric 
55448f6217Selric 	for (i=0; i <= buflen - sizeof(*lp); i += SCAN_INCR) {
56448f6217Selric 		memcpy(lp, buf + i, sizeof(*lp));
57448f6217Selric 		if (lp->d_magic == DISKMAGIC && lp->d_magic2 == DISKMAGIC)
58448f6217Selric 			goto sanity;
59448f6217Selric 	}
60448f6217Selric 
61448f6217Selric 	return 1;
62448f6217Selric 
63448f6217Selric sanity:
64448f6217Selric 	/* we've found something, let's sanity check it */
65448f6217Selric 	if (lp->d_npartitions > MAXPARTITIONS || disklabel_dkcksum(lp))
66448f6217Selric 		return 1;
67448f6217Selric 
68448f6217Selric 	return 0;
69448f6217Selric }
70