Home | History | Annotate | Line # | Download | only in ahdilabel
check.c revision 1.4.74.1
      1 /*	$NetBSD: check.c,v 1.4.74.1 2008/06/02 13:21:57 mjf Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Julian Coleman, Waldi Ravens and Leo Weppelman.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include "privahdi.h"
     33 
     34 /* Partitions which have errors */
     35 int	ahdi_errp1;
     36 int	ahdi_errp2;
     37 
     38 /*
     39  * Check partitions for consistency :
     40  *	GEM vs BGM
     41  *	Start sector > 2
     42  *	End sector < secperunit
     43  *	other partition overlap
     44  *	auxiliary root overlap
     45  *	Number of partitions in root/auxiliary root
     46  */
     47 int
     48 ahdi_checklabel (ptable)
     49 	struct ahdi_ptable	*ptable;
     50 {
     51 	int		i, j, rcount, acount;
     52 	u_int32_t	i_end, j_end;
     53 
     54 	ahdi_errp1 = -1;
     55 	ahdi_errp2 = -1;
     56 
     57 	if (ptable->nparts < 1 || ptable->nparts > MAXPARTITIONS)
     58 		return (-2);
     59 
     60 	rcount = 0;
     61 	acount = 0;
     62 
     63 	for (i = 0; i < ptable->nparts; i++) {
     64 
     65 		/* GEM vs BGM */
     66 		if (ptable->parts[i].size > 32768) {
     67 			if (AHDI_MKPID (ptable->parts[i].id[0],
     68 			    ptable->parts[i].id[1], ptable->parts[i].id[2])
     69 			    == AHDI_PID_GEM) {
     70 				ahdi_errp1 = i;
     71 				return (-3);
     72 			}
     73 		} else {
     74 			if (AHDI_MKPID (ptable->parts[i].id[0],
     75 			    ptable->parts[i].id[1], ptable->parts[i].id[2])
     76 			    == AHDI_PID_BGM) {
     77 				ahdi_errp1 = i;
     78 				return (-3);
     79 			}
     80 		}
     81 
     82 		/* Need 2 free sectors at start for root and bad sector list */
     83 		if (ptable->parts[i].start < 2) {
     84 			ahdi_errp1 = i;
     85 			return (-4);
     86 		}
     87 
     88 		i_end = ptable->parts[i].start + ptable->parts[i].size - 1;
     89 
     90 		/* Check partition does not extend past end of disk */
     91 		if (i_end >= ptable->secperunit) {
     92 			ahdi_errp1 = i;
     93 			return (-5);
     94 		}
     95 
     96 		for (j = i + 1; j < ptable->nparts; j++) {
     97 			/* Check for overlap with other partitions */
     98 			j_end = ptable->parts[j].start + ptable->parts[j].size
     99 			    - 1;
    100 			if ((ptable->parts[j].start >= ptable->parts[i].start
    101 			    && ptable->parts[j].start <= i_end) ||
    102 			    (j_end >= ptable->parts[i].start &&
    103 			     j_end <= i_end)) {
    104 				ahdi_errp1 = i;
    105 				ahdi_errp2 = j;
    106 				return (-6);
    107 			}
    108 			/* Check number of partitions in auxiliary root */
    109 			if (ptable->parts[i].root &&
    110 			    ptable->parts[i].root == ptable->parts[j].root) {
    111 				ahdi_errp1 = i;
    112 				ahdi_errp2 = j;
    113 				return (-9);
    114 			}
    115 		}
    116 
    117 		for (j = i; j < ptable->nparts; j++)
    118 			/* Check for overlap with auxiliary root(s) */
    119 			if (ptable->parts[j].root >= ptable->parts[i].start &&
    120 			    ptable->parts[j].root <= i_end) {
    121 				ahdi_errp1 = i;
    122 				return (-7);
    123 			}
    124 
    125 		/* Count partitions in root/auxiliary roots */
    126 		if (ptable->parts[i].root)
    127 			acount++;
    128 		else
    129 			rcount++;
    130 
    131 	}
    132 	/* Check number of partitions in root sector */
    133 	if (acount)
    134 		rcount++;
    135 	if (rcount > 4)
    136 		return (-8);
    137 	else
    138 		return (1);
    139 }
    140