No empty .Rs/.Re
[netbsd-mini2440.git] / distrib / utils / sysinst / arch / acorn32 / md.c
blobea79221d1ea89e47cf32a43fe61b0e20fa65b66b
1 /* $NetBSD: md.c,v 1.24 2009/09/19 14:57:27 abs Exp $ */
3 /*
4 * Copyright 1997 Piermont Information Systems Inc.
5 * All rights reserved.
7 * Based on code written by Philip A. Nelson for Piermont Information
8 * Systems Inc.
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 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
24 * written permission.
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 */
41 #include <stdio.h>
42 #include <curses.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <util.h>
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>
52 #include "defs.h"
53 #include "md.h"
54 #include "msg_defs.h"
55 #include "menu_defs.h"
57 static int filecore_checksum(u_char *);
59 void
60 md_init(void)
64 void
65 md_init_set_status(int minimal)
67 (void)minimal;
70 int
71 md_get_info(void)
73 struct disklabel disklabel;
74 int fd;
75 char dev_name[100];
76 static unsigned char bb[DEV_BSIZE];
77 struct filecore_bootblock *fcbb = (struct filecore_bootblock *)bb;
78 int offset = 0;
80 if (strncmp(diskdev, "wd", 2) == 0)
81 disktype = "ST506";
82 else
83 disktype = "SCSI";
85 snprintf(dev_name, 100, "/dev/r%s%c", diskdev, 'a' + getrawpartition());
87 fd = open(dev_name, O_RDONLY, 0);
88 if (fd < 0) {
89 endwin();
90 fprintf(stderr, "Can't open %s\n", dev_name);
91 exit(1);
93 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
94 endwin();
95 fprintf(stderr, "Can't read disklabel on %s.\n", dev_name);
96 close(fd);
97 exit(1);
100 if (lseek(fd, (off_t)FILECORE_BOOT_SECTOR * DEV_BSIZE, SEEK_SET) < 0
101 || read(fd, bb, sizeof(bb)) - sizeof(bb) != 0) {
102 endwin();
103 fprintf(stderr, msg_string(MSG_badreadbb));
104 close(fd);
105 exit(1);
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
125 * Empty:
128 struct riscix_partition_table *riscix_part =
129 (struct riscix_partition_table *)bb;
130 struct riscix_partition *part;
131 int loop;
133 if (lseek(fd, (off_t)offset * DEV_BSIZE, SEEK_SET) < 0
134 || read(fd, bb, sizeof(bb)) - sizeof(bb) != 0) {
135 endwin();
136 fprintf(stderr, msg_string(MSG_badreadriscix));
137 close(fd);
138 exit(1);
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;
148 break;
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.
157 endwin();
158 fprintf(stderr, msg_string(MSG_notnetbsdriscix));
159 close(fd);
160 exit(1);
162 } else {
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.
170 endwin();
171 fprintf(stderr, msg_string(MSG_notnetbsd));
172 close(fd);
173 exit(1);
176 close(fd);
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;
194 ptstart = offset;
195 /* endwin();
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);
205 backtowin();*/
207 return 1;
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)
225 return 1;
229 * hook called before writing new disklabel.
232 md_pre_disklabel(void)
234 return 0;
238 * hook called after writing disklabel to new target disk.
241 md_post_disklabel(void)
243 return 0;
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.
252 md_post_newfs(void)
254 return 0;
258 md_post_extract(void)
260 return 0;
263 void
264 md_cleanup_install(void)
266 #ifndef DEBUG
267 enable_rc_conf();
268 #endif
272 md_pre_update(void)
274 return 1;
277 /* Upgrade support */
279 md_update(void)
281 md_post_newfs();
282 return 1;
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) {
296 * sum += byte;
297 * if (sum > 255)
298 * sum -= 255;
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.
318 static int
319 filecore_checksum(u_char *bootblock)
321 u_char byte0, accum_diff;
322 u_int sum;
323 int i;
325 sum = 0;
326 accum_diff = 0;
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) {
335 sum += bootblock[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. */
348 if (accum_diff == 0)
349 return (-1);
351 return (sum - ((sum - 1) / 255) * 255);