Home | History | Annotate | Line # | Download | only in libukfs
      1 /*	$NetBSD: ukfs_disklabel.c,v 1.4 2025/12/18 23:15:54 nia 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 #include <sys/endian.h>
     45 
     46 #include <string.h>
     47 #include <unistd.h>
     48 
     49 #include "ukfs_int_disklabel.h"
     50 
     51 #define SCAN_INCR	4
     52 
     53 int
     54 ukfs__disklabel_scan(struct ukfs__disklabel *lp, int *isswapped,
     55 	char *buf, size_t buflen)
     56 {
     57 	size_t i;
     58 	int imswapped;
     59 	uint16_t npart;
     60 
     61 	/* scan for the correct magic numbers. */
     62 
     63 	for (i=0; i <= buflen - sizeof(*lp); i += SCAN_INCR) {
     64 		memcpy(lp, buf + i, sizeof(*lp));
     65 		if (lp->d_magic == UKFS_DISKMAGIC &&
     66 		    lp->d_magic2 == UKFS_DISKMAGIC) {
     67 			imswapped = 0;
     68 			goto sanity;
     69 		}
     70 		if (lp->d_magic == bswap32(UKFS_DISKMAGIC) &&
     71 		    lp->d_magic2 == bswap32(UKFS_DISKMAGIC)) {
     72 			imswapped = 1;
     73 			goto sanity;
     74 		}
     75 	}
     76 
     77 	return 1;
     78 
     79 sanity:
     80 	if (imswapped)
     81 		npart = bswap16(lp->d_npartitions);
     82 	else
     83 		npart = lp->d_npartitions;
     84 	/* we've found something, let's sanity check it */
     85 	if (npart > UKFS_MAXPARTITIONS
     86 	    || ukfs__disklabel_dkcksum(lp, imswapped))
     87 		return 1;
     88 
     89 	*isswapped = imswapped;
     90 	return 0;
     91 }
     92 
     93 
     94 /*
     95  * From:
     96  *	$NetBSD: disklabel_dkcksum.c,v 1.4 2005/05/15 21:01:34 thorpej Exp
     97  */
     98 
     99 /*-
    100  * Copyright (c) 1991, 1993
    101  *	The Regents of the University of California.  All rights reserved.
    102  *
    103  * Redistribution and use in source and binary forms, with or without
    104  * modification, are permitted provided that the following conditions
    105  * are met:
    106  * 1. Redistributions of source code must retain the above copyright
    107  *    notice, this list of conditions and the following disclaimer.
    108  * 2. Redistributions in binary form must reproduce the above copyright
    109  *    notice, this list of conditions and the following disclaimer in the
    110  *    documentation and/or other materials provided with the distribution.
    111  * 3. Neither the name of the University nor the names of its contributors
    112  *    may be used to endorse or promote products derived from this software
    113  *    without specific prior written permission.
    114  *
    115  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    116  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    117  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    118  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    119  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    120  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    121  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    122  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    123  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    124  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    125  * SUCH DAMAGE.
    126  */
    127 
    128 uint16_t
    129 ukfs__disklabel_dkcksum(struct ukfs__disklabel *lp, int imswapped)
    130 {
    131 	uint16_t *start, *end;
    132 	uint16_t sum;
    133 	uint16_t npart;
    134 
    135 	if (imswapped)
    136 		npart = bswap16(lp->d_npartitions);
    137 	else
    138 		npart = lp->d_npartitions;
    139 
    140 	sum = 0;
    141 	start = (uint16_t *)(void *)lp;
    142 	end = (uint16_t *)(void *)&lp->d_partitions[npart];
    143 	while (start < end) {
    144 		if (imswapped)
    145 			sum ^= bswap16(*start);
    146 		else
    147 			sum ^= *start;
    148 		start++;
    149 	}
    150 	return (sum);
    151 }
    152