xref: /netbsd/sys/arch/atari/stand/ahdilabel/check.c (revision bf9ec67e)
1 /*	$NetBSD: check.c,v 1.2 2000/10/23 06:56:00 jdc 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include "privahdi.h"
40 
41 /* Partitions which have errors */
42 int	ahdi_errp1;
43 int	ahdi_errp2;
44 
45 /*
46  * Check partitions for consistency :
47  *	GEM vs BGM
48  *	Start sector > 2
49  *	End sector < secperunit
50  *	other partition overlap
51  *	auxiliary root overlap
52  *	Number of partitions in root/auxiliary root
53  */
54 int
55 ahdi_checklabel (ptable)
56 	struct ahdi_ptable	*ptable;
57 {
58 	int		i, j, rcount, acount;
59 	u_int32_t	i_end, j_end;
60 
61 	ahdi_errp1 = -1;
62 	ahdi_errp2 = -1;
63 
64 	if (ptable->nparts < 1 || ptable->nparts > MAXPARTITIONS)
65 		return (-2);
66 
67 	rcount = 0;
68 	acount = 0;
69 
70 	for (i = 0; i < ptable->nparts; i++) {
71 
72 		/* GEM vs BGM */
73 		if (ptable->parts[i].size > 32768) {
74 			if (AHDI_MKPID (ptable->parts[i].id[0],
75 			    ptable->parts[i].id[1], ptable->parts[i].id[2])
76 			    == AHDI_PID_GEM) {
77 				ahdi_errp1 = i;
78 				return (-3);
79 			}
80 		} else {
81 			if (AHDI_MKPID (ptable->parts[i].id[0],
82 			    ptable->parts[i].id[1], ptable->parts[i].id[2])
83 			    == AHDI_PID_BGM) {
84 				ahdi_errp1 = i;
85 				return (-3);
86 			}
87 		}
88 
89 		/* Need 2 free sectors at start for root and bad sector list */
90 		if (ptable->parts[i].start < 2) {
91 			ahdi_errp1 = i;
92 			return (-4);
93 		}
94 
95 		i_end = ptable->parts[i].start + ptable->parts[i].size - 1;
96 
97 		/* Check partition does not extend past end of disk */
98 		if (i_end >= ptable->secperunit) {
99 			ahdi_errp1 = i;
100 			return (-5);
101 		}
102 
103 		for (j = i + 1; j < ptable->nparts; j++) {
104 			/* Check for overlap with other partitions */
105 			j_end = ptable->parts[j].start + ptable->parts[j].size
106 			    - 1;
107 			if ((ptable->parts[j].start >= ptable->parts[i].start
108 			    && ptable->parts[j].start <= i_end) ||
109 			    (j_end >= ptable->parts[i].start &&
110 			     j_end <= i_end)) {
111 				ahdi_errp1 = i;
112 				ahdi_errp2 = j;
113 				return (-6);
114 			}
115 			/* Check number of partitions in auxiliary root */
116 			if (ptable->parts[i].root &&
117 			    ptable->parts[i].root == ptable->parts[j].root) {
118 				ahdi_errp1 = i;
119 				ahdi_errp2 = j;
120 				return (-9);
121 			}
122 		}
123 
124 		for (j = i; j < ptable->nparts; j++)
125 			/* Check for overlap with auxiliary root(s) */
126 			if (ptable->parts[j].root >= ptable->parts[i].start &&
127 			    ptable->parts[j].root <= i_end) {
128 				ahdi_errp1 = i;
129 				return (-7);
130 			}
131 
132 		/* Count partitions in root/auxiliary roots */
133 		if (ptable->parts[i].root)
134 			acount ++;
135 		else
136 			rcount ++;
137 
138 	}
139 	/* Check number of partitions in root sector */
140 	if (acount)
141 		rcount++;
142 	if (rcount > 4)
143 		return (-8);
144 	else
145 		return (1);
146 }
147