xref: /netbsd/sys/arch/acorn32/stand/lib/riscospart.c (revision 6550d01e)
1 /*	$NetBSD: riscospart.c,v 1.3 2010/11/25 13:45:17 skrll Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006 Ben Harris
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * Copyright (c) 1995 Mark Brinicombe
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  * 3. All advertising materials mentioning features or use of this software
43  *    must display the following acknowledgement:
44  *	This product includes software developed by the University of
45  *	California, Berkeley and its contributors.
46  * 4. Neither the name of the University nor the names of its contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  */
62 
63 #include <sys/types.h>
64 #include <sys/param.h>
65 #include <sys/disklabel.h>
66 #include <sys/disklabel_acorn.h>
67 
68 #include <lib/libsa/stand.h>
69 
70 #include "riscospart.h"
71 
72 /*
73  * This function should be shared between here,
74  * sys/arch/arm/arm/disksubr_acorn.c, and
75  * sys/fs/filecorefs/filecore_utils.c, rather than being copied.
76  */
77 /*
78  * static int filecore_checksum(u_char *bootblock)
79  *
80  * Calculates the filecore boot block checksum. This is used to validate
81  * a filecore boot block on the disk.  If a boot block is validated then
82  * it is used to locate the partition table. If the boot block is not
83  * validated, it is assumed that the whole disk is NetBSD.
84  *
85  * The basic algorithm is:
86  *
87  *	for (each byte in block, excluding checksum) {
88  *		sum += byte;
89  *		if (sum > 255)
90  *			sum -= 255;
91  *	}
92  *
93  * That's equivalent to summing all of the bytes in the block
94  * (excluding the checksum byte, of course), then calculating the
95  * checksum as "cksum = sum - ((sum - 1) / 255) * 255)".  That
96  * expression may or may not yield a faster checksum function,
97  * but it's easier to reason about.
98  *
99  * Note that if you have a block filled with bytes of a single
100  * value "X" (regardless of that value!) and calculate the cksum
101  * of the block (excluding the checksum byte), you will _always_
102  * end up with a checksum of X.  (Do the math; that can be derived
103  * from the checksum calculation function!)  That means that
104  * blocks which contain bytes which all have the same value will
105  * always checksum properly.  That's a _very_ unlikely occurence
106  * (probably impossible, actually) for a valid filecore boot block,
107  * so we treat such blocks as invalid.
108  */
109 static int
110 filecore_checksum(u_char *bootblock)
111 {
112 	u_char byte0, accum_diff;
113 	u_int sum;
114 	int i;
115 
116 	sum = 0;
117 	accum_diff = 0;
118 	byte0 = bootblock[0];
119 
120 	/*
121 	 * Sum the contents of the block, keeping track of whether
122 	 * or not all bytes are the same.  If 'accum_diff' ends up
123 	 * being zero, all of the bytes are, in fact, the same.
124 	 */
125 	for (i = 0; i < 511; ++i) {
126 		sum += bootblock[i];
127 		accum_diff |= bootblock[i] ^ byte0;
128 	}
129 
130 	/*
131 	 * Check to see if the checksum byte is the same as the
132 	 * rest of the bytes, too.  (Note that if all of the bytes
133 	 * are the same except the checksum, a checksum compare
134 	 * won't succeed, but that's not our problem.)
135 	 */
136 	accum_diff |= bootblock[i] ^ byte0;
137 
138 	/* All bytes in block are the same; call it invalid. */
139 	if (accum_diff == 0)
140 		return (-1);
141 
142 	return (sum - ((sum - 1) / 255) * 255);
143 }
144 
145 
146 int
147 getdisklabel_acorn(struct open_file *f, struct disklabel *lp)
148 {
149 	size_t rsize;
150 	int err;
151 	char *buf;
152 	struct filecore_bootblock *bb;
153 	daddr_t labelsect;
154 	char *msg;
155 
156 	buf = alloc(DEV_BSIZE);
157 	err = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
158 	    FILECORE_BOOT_SECTOR, DEV_BSIZE, buf, &rsize);
159 	if (err != 0) goto out;
160 	bb = (struct filecore_bootblock *) buf;
161 	if (bb->checksum == filecore_checksum((u_char *)bb)) {
162 		if (bb->partition_type == PARTITION_FORMAT_RISCBSD)
163 			labelsect = ((bb->partition_cyl_high << 8) + bb->partition_cyl_low) *
164 			    bb->heads * bb->secspertrack + LABELSECTOR;
165 		else {
166 			err = EUNLAB;
167 			goto out;
168 		}
169 	} else
170 		labelsect = LABELSECTOR;
171 	err = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
172 	    labelsect, DEV_BSIZE, buf, &rsize);
173 	if (err != 0) goto out;
174 	msg = getdisklabel(buf, lp);
175 	if (msg) {
176 		printf("%s\n", msg);
177 		err = ERDLAB;
178 	}
179 out:
180 	dealloc(buf, DEV_BSIZE);
181 	return err;
182 }
183