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