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