1 /* $NetBSD: ukfs_disklabel.c,v 1.2 2009/12/03 14:23:49 pooka Exp $ */ 2 3 /* 4 * Local copies of libutil disklabel routines. This uncouples libukfs 5 * from the NetBSD-only libutil. All identifiers are prefixed with 6 * ukfs or UKFS, otherwise the routines are the same. 7 */ 8 9 /* 10 * From: 11 * NetBSD: disklabel_scan.c,v 1.3 2009/01/18 12:13:03 lukem Exp 12 */ 13 14 /*- 15 * Copyright (c) 2002 The NetBSD Foundation, Inc. 16 * All rights reserved. 17 * 18 * This code is derived from software contributed to The NetBSD Foundation 19 * by Roland C. Dowdeswell. 20 * 21 * Redistribution and use in source and binary forms, with or without 22 * modification, are permitted provided that the following conditions 23 * are met: 24 * 1. Redistributions of source code must retain the above copyright 25 * notice, this list of conditions and the following disclaimer. 26 * 2. Redistributions in binary form must reproduce the above copyright 27 * notice, this list of conditions and the following disclaimer in the 28 * documentation and/or other materials provided with the distribution. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 40 * POSSIBILITY OF SUCH DAMAGE. 41 */ 42 43 #include <sys/types.h> 44 45 #include <string.h> 46 #include <unistd.h> 47 48 #include "ukfs_int_disklabel.h" 49 50 #define SCAN_INCR 4 51 52 int 53 ukfs__disklabel_scan(struct ukfs__disklabel *lp, char *buf, size_t buflen) 54 { 55 size_t i; 56 57 /* scan for the correct magic numbers. */ 58 59 for (i=0; i <= buflen - sizeof(*lp); i += SCAN_INCR) { 60 memcpy(lp, buf + i, sizeof(*lp)); 61 if (lp->d_magic == UKFS_DISKMAGIC && 62 lp->d_magic2 == UKFS_DISKMAGIC) 63 goto sanity; 64 } 65 66 return 1; 67 68 sanity: 69 /* we've found something, let's sanity check it */ 70 if (lp->d_npartitions > UKFS_MAXPARTITIONS 71 || ukfs__disklabel_dkcksum(lp)) 72 return 1; 73 74 return 0; 75 } 76 77 78 /* 79 * From: 80 * $NetBSD: disklabel_dkcksum.c,v 1.4 2005/05/15 21:01:34 thorpej Exp 81 */ 82 83 /*- 84 * Copyright (c) 1991, 1993 85 * The Regents of the University of California. All rights reserved. 86 * 87 * Redistribution and use in source and binary forms, with or without 88 * modification, are permitted provided that the following conditions 89 * are met: 90 * 1. Redistributions of source code must retain the above copyright 91 * notice, this list of conditions and the following disclaimer. 92 * 2. Redistributions in binary form must reproduce the above copyright 93 * notice, this list of conditions and the following disclaimer in the 94 * documentation and/or other materials provided with the distribution. 95 * 3. Neither the name of the University nor the names of its contributors 96 * may be used to endorse or promote products derived from this software 97 * without specific prior written permission. 98 * 99 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 100 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 101 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 102 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 103 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 104 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 105 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 106 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 107 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 108 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 109 * SUCH DAMAGE. 110 */ 111 112 uint16_t 113 ukfs__disklabel_dkcksum(struct ukfs__disklabel *lp) 114 { 115 uint16_t *start, *end; 116 uint16_t sum; 117 118 sum = 0; 119 start = (uint16_t *)(void *)lp; 120 end = (uint16_t *)(void *)&lp->d_partitions[lp->d_npartitions]; 121 while (start < end) 122 sum ^= *start++; 123 return (sum); 124 } 125