1 /* $NetBSD: md.c,v 1.24 2009/09/19 14:57:27 abs Exp $ */
4 * Copyright 1997 Piermont Information Systems Inc.
7 * Based on code written by Philip A. Nelson for Piermont Information
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 for the NetBSD Project by
21 * Piermont Information Systems Inc.
22 * 4. The name of Piermont Information Systems Inc. may not be used to endorse
23 * or promote products derived from this software without specific prior
26 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
30 * 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
36 * THE POSSIBILITY OF SUCH DAMAGE.
39 /* md.c -- arm32 machine specific routines - also used by acorn26 */
46 #include <sys/types.h>
47 #include <sys/disklabel.h>
48 #include <sys/disklabel_acorn.h>
49 #include <sys/ioctl.h>
50 #include <sys/param.h>
55 #include "menu_defs.h"
57 static int filecore_checksum(u_char
*);
65 md_init_set_status(int minimal
)
73 struct disklabel disklabel
;
76 static unsigned char bb
[DEV_BSIZE
];
77 struct filecore_bootblock
*fcbb
= (struct filecore_bootblock
*)bb
;
80 if (strncmp(diskdev
, "wd", 2) == 0)
85 snprintf(dev_name
, 100, "/dev/r%s%c", diskdev
, 'a' + getrawpartition());
87 fd
= open(dev_name
, O_RDONLY
, 0);
90 fprintf(stderr
, "Can't open %s\n", dev_name
);
93 if (ioctl(fd
, DIOCGDINFO
, &disklabel
) == -1) {
95 fprintf(stderr
, "Can't read disklabel on %s.\n", dev_name
);
100 if (lseek(fd
, (off_t
)FILECORE_BOOT_SECTOR
* DEV_BSIZE
, SEEK_SET
) < 0
101 || read(fd
, bb
, sizeof(bb
)) - sizeof(bb
) != 0) {
103 fprintf(stderr
, msg_string(MSG_badreadbb
));
108 /* Check if table is valid. */
109 if (filecore_checksum(bb
) == fcbb
->checksum
) {
111 * Check for NetBSD/arm32 (RiscBSD) partition marker.
112 * If found the NetBSD disklabel location is easy.
115 offset
= (fcbb
->partition_cyl_low
+
116 (fcbb
->partition_cyl_high
<< 8)) *
117 fcbb
->heads
* fcbb
->secspertrack
;
119 if (fcbb
->partition_type
== PARTITION_FORMAT_RISCBSD
)
121 else if (fcbb
->partition_type
== PARTITION_FORMAT_RISCIX
) {
123 * Ok we need to read the RISCiX partition table and
124 * search for a partition named RiscBSD, NetBSD or
128 struct riscix_partition_table
*riscix_part
=
129 (struct riscix_partition_table
*)bb
;
130 struct riscix_partition
*part
;
133 if (lseek(fd
, (off_t
)offset
* DEV_BSIZE
, SEEK_SET
) < 0
134 || read(fd
, bb
, sizeof(bb
)) - sizeof(bb
) != 0) {
136 fprintf(stderr
, msg_string(MSG_badreadriscix
));
141 /* Break out as soon as we find a suitable partition */
142 for (loop
= 0; loop
< NRISCIX_PARTITIONS
; ++loop
) {
143 part
= &riscix_part
->partitions
[loop
];
144 if (strcmp((char *)part
->rp_name
, "RiscBSD") == 0
145 || strcmp((char *)part
->rp_name
, "NetBSD") == 0
146 || strcmp((char *)part
->rp_name
, "Empty:") == 0) {
147 offset
= part
->rp_start
;
151 if (loop
== NRISCIX_PARTITIONS
) {
153 * Valid filecore boot block, RISCiX partition
154 * table but no NetBSD partition. We should
155 * leave this disc alone.
158 fprintf(stderr
, msg_string(MSG_notnetbsdriscix
));
164 * Valid filecore boot block and no non-ADFS partition.
165 * This means that the whole disc is allocated for ADFS
166 * so do not trash ! If the user really wants to put a
167 * NetBSD disklabel on the disc then they should remove
168 * the filecore boot block first with dd.
171 fprintf(stderr
, msg_string(MSG_notnetbsd
));
178 dlcyl
= disklabel
.d_ncylinders
;
179 dlhead
= disklabel
.d_ntracks
;
180 dlsec
= disklabel
.d_nsectors
;
181 sectorsize
= disklabel
.d_secsize
;
182 dlcylsize
= disklabel
.d_secpercyl
;
185 * Compute whole disk size. Take max of (dlcyl*dlhead*dlsec)
186 * and secperunit, just in case the disk is already labelled.
187 * (If our new label's RAW_PART size ends up smaller than the
188 * in-core RAW_PART size value, updating the label will fail.)
190 dlsize
= dlcyl
*dlhead
*dlsec
;
191 if (disklabel
.d_secperunit
> dlsize
)
192 dlsize
= disklabel
.d_secperunit
;
196 printf("dlcyl=%d\n", dlcyl);
197 printf("dlhead=%d\n", dlhead);
198 printf("dlsec=%d\n", dlsec);
199 printf("secsz=%d\n", sectorsize);
200 printf("cylsz=%d\n", dlcylsize);
201 printf("dlsz=%d\n", dlsize);
202 printf("pstart=%d\n", ptstart);
203 printf("pstart=%d\n", partsize);
204 printf("secpun=%d\n", disklabel.d_secperunit);
211 * md back-end code for menu-driven BSD disklabel editor.
214 md_make_bsd_partitions(void)
216 return make_bsd_partitions();
220 * any additional partition validation
223 md_check_partitions(void)
229 * hook called before writing new disklabel.
232 md_pre_disklabel(void)
238 * hook called after writing disklabel to new target disk.
241 md_post_disklabel(void)
247 * hook called after upgrade() or install() has finished setting
248 * up the target disk but immediately before the user is given the
249 * ``disks are now set up'' message.
258 md_post_extract(void)
264 md_cleanup_install(void)
277 /* Upgrade support */
286 * static int filecore_checksum(u_char *bootblock)
288 * Calculates the filecore boot block checksum. This is used to validate
289 * a filecore boot block on the disk. If a boot block is validated then
290 * it is used to locate the partition table. If the boot block is not
291 * validated, it is assumed that the whole disk is NetBSD.
293 * The basic algorithm is:
295 * for (each byte in block, excluding checksum) {
301 * That's equivalent to summing all of the bytes in the block
302 * (excluding the checksum byte, of course), then calculating the
303 * checksum as "cksum = sum - ((sum - 1) / 255) * 255)". That
304 * expression may or may not yield a faster checksum function,
305 * but it's easier to reason about.
307 * Note that if you have a block filled with bytes of a single
308 * value "X" (regardless of that value!) and calculate the cksum
309 * of the block (excluding the checksum byte), you will _always_
310 * end up with a checksum of X. (Do the math; that can be derived
311 * from the checksum calculation function!) That means that
312 * blocks which contain bytes which all have the same value will
313 * always checksum properly. That's a _very_ unlikely occurence
314 * (probably impossible, actually) for a valid filecore boot block,
315 * so we treat such blocks as invalid.
319 filecore_checksum(u_char
*bootblock
)
321 u_char byte0
, accum_diff
;
327 byte0
= bootblock
[0];
330 * Sum the contents of the block, keeping track of whether
331 * or not all bytes are the same. If 'accum_diff' ends up
332 * being zero, all of the bytes are, in fact, the same.
334 for (i
= 0; i
< 511; ++i
) {
336 accum_diff
|= bootblock
[i
] ^ byte0
;
340 * Check to see if the checksum byte is the same as the
341 * rest of the bytes, too. (Note that if all of the bytes
342 * are the same except the checksum, a checksum compare
343 * won't succeed, but that's not our problem.)
345 accum_diff
|= bootblock
[i
] ^ byte0
;
347 /* All bytes in block are the same; call it invalid. */
351 return (sum
- ((sum
- 1) / 255) * 255);