md.c revision 1.2.6.2 1 1.2.6.2 tls /* $NetBSD: md.c,v 1.2.6.2 2014/08/20 00:05:14 tls Exp $ */
2 1.2.6.2 tls
3 1.2.6.2 tls /*
4 1.2.6.2 tls * Copyright 1997 Piermont Information Systems Inc.
5 1.2.6.2 tls * All rights reserved.
6 1.2.6.2 tls *
7 1.2.6.2 tls * Based on code written by Philip A. Nelson for Piermont Information
8 1.2.6.2 tls * Systems Inc.
9 1.2.6.2 tls *
10 1.2.6.2 tls * Redistribution and use in source and binary forms, with or without
11 1.2.6.2 tls * modification, are permitted provided that the following conditions
12 1.2.6.2 tls * are met:
13 1.2.6.2 tls * 1. Redistributions of source code must retain the above copyright
14 1.2.6.2 tls * notice, this list of conditions and the following disclaimer.
15 1.2.6.2 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.6.2 tls * notice, this list of conditions and the following disclaimer in the
17 1.2.6.2 tls * documentation and/or other materials provided with the distribution.
18 1.2.6.2 tls * 3. The name of Piermont Information Systems Inc. may not be used to endorse
19 1.2.6.2 tls * or promote products derived from this software without specific prior
20 1.2.6.2 tls * written permission.
21 1.2.6.2 tls *
22 1.2.6.2 tls * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
23 1.2.6.2 tls * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.2.6.2 tls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.2.6.2 tls * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
26 1.2.6.2 tls * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.2.6.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.2.6.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.2.6.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.2.6.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.2.6.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 1.2.6.2 tls * THE POSSIBILITY OF SUCH DAMAGE.
33 1.2.6.2 tls */
34 1.2.6.2 tls
35 1.2.6.2 tls /* md.c -- arm32 machine specific routines - also used by acorn26 */
36 1.2.6.2 tls
37 1.2.6.2 tls #include <stdio.h>
38 1.2.6.2 tls #include <curses.h>
39 1.2.6.2 tls #include <unistd.h>
40 1.2.6.2 tls #include <fcntl.h>
41 1.2.6.2 tls #include <util.h>
42 1.2.6.2 tls #include <sys/types.h>
43 1.2.6.2 tls #include <sys/disklabel.h>
44 1.2.6.2 tls #include <sys/disklabel_acorn.h>
45 1.2.6.2 tls #include <sys/ioctl.h>
46 1.2.6.2 tls #include <sys/param.h>
47 1.2.6.2 tls
48 1.2.6.2 tls #include "defs.h"
49 1.2.6.2 tls #include "md.h"
50 1.2.6.2 tls #include "msg_defs.h"
51 1.2.6.2 tls #include "menu_defs.h"
52 1.2.6.2 tls
53 1.2.6.2 tls static int filecore_checksum(u_char *);
54 1.2.6.2 tls
55 1.2.6.2 tls void
56 1.2.6.2 tls md_init(void)
57 1.2.6.2 tls {
58 1.2.6.2 tls }
59 1.2.6.2 tls
60 1.2.6.2 tls void
61 1.2.6.2 tls md_init_set_status(int flags)
62 1.2.6.2 tls {
63 1.2.6.2 tls (void)flags;
64 1.2.6.2 tls }
65 1.2.6.2 tls
66 1.2.6.2 tls int
67 1.2.6.2 tls md_get_info(void)
68 1.2.6.2 tls {
69 1.2.6.2 tls struct disklabel disklabel;
70 1.2.6.2 tls int fd;
71 1.2.6.2 tls char dev_name[100];
72 1.2.6.2 tls static unsigned char bb[DEV_BSIZE];
73 1.2.6.2 tls struct filecore_bootblock *fcbb = (struct filecore_bootblock *)bb;
74 1.2.6.2 tls int offset = 0;
75 1.2.6.2 tls
76 1.2.6.2 tls if (strncmp(pm->diskdev, "wd", 2) == 0)
77 1.2.6.2 tls pm->disktype = "ST506";
78 1.2.6.2 tls else
79 1.2.6.2 tls pm->disktype = "SCSI";
80 1.2.6.2 tls
81 1.2.6.2 tls snprintf(dev_name, 100, "/dev/r%s%c", pm->diskdev, 'a' + getrawpartition());
82 1.2.6.2 tls
83 1.2.6.2 tls fd = open(dev_name, O_RDONLY, 0);
84 1.2.6.2 tls if (fd < 0) {
85 1.2.6.2 tls endwin();
86 1.2.6.2 tls fprintf(stderr, "Can't open %s\n", dev_name);
87 1.2.6.2 tls exit(1);
88 1.2.6.2 tls }
89 1.2.6.2 tls if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
90 1.2.6.2 tls endwin();
91 1.2.6.2 tls fprintf(stderr, "Can't read disklabel on %s.\n", dev_name);
92 1.2.6.2 tls close(fd);
93 1.2.6.2 tls exit(1);
94 1.2.6.2 tls }
95 1.2.6.2 tls
96 1.2.6.2 tls if (lseek(fd, (off_t)FILECORE_BOOT_SECTOR * DEV_BSIZE, SEEK_SET) < 0
97 1.2.6.2 tls || read(fd, bb, sizeof(bb)) - sizeof(bb) != 0) {
98 1.2.6.2 tls endwin();
99 1.2.6.2 tls fprintf(stderr, "%s", msg_string(MSG_badreadbb));
100 1.2.6.2 tls close(fd);
101 1.2.6.2 tls exit(1);
102 1.2.6.2 tls }
103 1.2.6.2 tls
104 1.2.6.2 tls /* Check if table is valid. */
105 1.2.6.2 tls if (filecore_checksum(bb) == fcbb->checksum) {
106 1.2.6.2 tls /*
107 1.2.6.2 tls * Check for NetBSD/arm32 (RiscBSD) partition marker.
108 1.2.6.2 tls * If found the NetBSD disklabel location is easy.
109 1.2.6.2 tls */
110 1.2.6.2 tls
111 1.2.6.2 tls offset = (fcbb->partition_cyl_low +
112 1.2.6.2 tls (fcbb->partition_cyl_high << 8)) *
113 1.2.6.2 tls fcbb->heads * fcbb->secspertrack;
114 1.2.6.2 tls
115 1.2.6.2 tls if (fcbb->partition_type == PARTITION_FORMAT_RISCBSD)
116 1.2.6.2 tls ;
117 1.2.6.2 tls else if (fcbb->partition_type == PARTITION_FORMAT_RISCIX) {
118 1.2.6.2 tls /*
119 1.2.6.2 tls * Ok we need to read the RISCiX partition table and
120 1.2.6.2 tls * search for a partition named RiscBSD, NetBSD or
121 1.2.6.2 tls * Empty:
122 1.2.6.2 tls */
123 1.2.6.2 tls
124 1.2.6.2 tls struct riscix_partition_table *riscix_part =
125 1.2.6.2 tls (struct riscix_partition_table *)bb;
126 1.2.6.2 tls struct riscix_partition *part;
127 1.2.6.2 tls int loop;
128 1.2.6.2 tls
129 1.2.6.2 tls if (lseek(fd, (off_t)offset * DEV_BSIZE, SEEK_SET) < 0
130 1.2.6.2 tls || read(fd, bb, sizeof(bb)) - sizeof(bb) != 0) {
131 1.2.6.2 tls endwin();
132 1.2.6.2 tls fprintf(stderr, "%s",
133 1.2.6.2 tls msg_string(MSG_badreadriscix));
134 1.2.6.2 tls close(fd);
135 1.2.6.2 tls exit(1);
136 1.2.6.2 tls }
137 1.2.6.2 tls
138 1.2.6.2 tls /* Break out as soon as we find a suitable partition */
139 1.2.6.2 tls for (loop = 0; loop < NRISCIX_PARTITIONS; ++loop) {
140 1.2.6.2 tls part = &riscix_part->partitions[loop];
141 1.2.6.2 tls if (strcmp((char *)part->rp_name, "RiscBSD") == 0
142 1.2.6.2 tls || strcmp((char *)part->rp_name, "NetBSD") == 0
143 1.2.6.2 tls || strcmp((char *)part->rp_name, "Empty:") == 0) {
144 1.2.6.2 tls offset = part->rp_start;
145 1.2.6.2 tls break;
146 1.2.6.2 tls }
147 1.2.6.2 tls }
148 1.2.6.2 tls if (loop == NRISCIX_PARTITIONS) {
149 1.2.6.2 tls /*
150 1.2.6.2 tls * Valid filecore boot block, RISCiX partition
151 1.2.6.2 tls * table but no NetBSD partition. We should
152 1.2.6.2 tls * leave this disc alone.
153 1.2.6.2 tls */
154 1.2.6.2 tls endwin();
155 1.2.6.2 tls fprintf(stderr, "%s",
156 1.2.6.2 tls msg_string(MSG_notnetbsdriscix));
157 1.2.6.2 tls close(fd);
158 1.2.6.2 tls exit(1);
159 1.2.6.2 tls }
160 1.2.6.2 tls } else {
161 1.2.6.2 tls /*
162 1.2.6.2 tls * Valid filecore boot block and no non-ADFS partition.
163 1.2.6.2 tls * This means that the whole disc is allocated for ADFS
164 1.2.6.2 tls * so do not trash ! If the user really wants to put a
165 1.2.6.2 tls * NetBSD disklabel on the disc then they should remove
166 1.2.6.2 tls * the filecore boot block first with dd.
167 1.2.6.2 tls */
168 1.2.6.2 tls endwin();
169 1.2.6.2 tls fprintf(stderr, "%s", msg_string(MSG_notnetbsd));
170 1.2.6.2 tls close(fd);
171 1.2.6.2 tls exit(1);
172 1.2.6.2 tls }
173 1.2.6.2 tls }
174 1.2.6.2 tls close(fd);
175 1.2.6.2 tls
176 1.2.6.2 tls pm->dlcyl = disklabel.d_ncylinders;
177 1.2.6.2 tls pm->dlhead = disklabel.d_ntracks;
178 1.2.6.2 tls pm->dlsec = disklabel.d_nsectors;
179 1.2.6.2 tls pm->sectorsize = disklabel.d_secsize;
180 1.2.6.2 tls pm->dlcylsize = disklabel.d_secpercyl;
181 1.2.6.2 tls
182 1.2.6.2 tls /*
183 1.2.6.2 tls * Compute whole disk size. Take max of (pm->dlcyl*pm->dlhead*pm->dlsec)
184 1.2.6.2 tls * and secperunit, just in case the disk is already labelled.
185 1.2.6.2 tls * (If our new label's RAW_PART size ends up smaller than the
186 1.2.6.2 tls * in-core RAW_PART size value, updating the label will fail.)
187 1.2.6.2 tls */
188 1.2.6.2 tls pm->dlsize = pm->dlcyl*pm->dlhead*pm->dlsec;
189 1.2.6.2 tls if (disklabel.d_secperunit > pm->dlsize)
190 1.2.6.2 tls pm->dlsize = disklabel.d_secperunit;
191 1.2.6.2 tls
192 1.2.6.2 tls pm->ptstart = offset;
193 1.2.6.2 tls /* endwin();
194 1.2.6.2 tls printf("pm->dlcyl=%d\n", pm->dlcyl);
195 1.2.6.2 tls printf("pm->dlhead=%d\n", pm->dlhead);
196 1.2.6.2 tls printf("pm->dlsec=%d\n", pm->dlsec);
197 1.2.6.2 tls printf("secsz=%d\n", pm->sectorsize);
198 1.2.6.2 tls printf("cylsz=%d\n", pm->dlcylsize);
199 1.2.6.2 tls printf("dlsz=%d\n", pm->dlsize);
200 1.2.6.2 tls printf("pstart=%d\n", pm->ptstart);
201 1.2.6.2 tls printf("pstart=%d\n", partsize);
202 1.2.6.2 tls printf("secpun=%d\n", disklabel.d_secperunit);
203 1.2.6.2 tls backtowin();*/
204 1.2.6.2 tls
205 1.2.6.2 tls return 1;
206 1.2.6.2 tls }
207 1.2.6.2 tls
208 1.2.6.2 tls /*
209 1.2.6.2 tls * md back-end code for menu-driven BSD disklabel editor.
210 1.2.6.2 tls */
211 1.2.6.2 tls int
212 1.2.6.2 tls md_make_bsd_partitions(void)
213 1.2.6.2 tls {
214 1.2.6.2 tls return make_bsd_partitions();
215 1.2.6.2 tls }
216 1.2.6.2 tls
217 1.2.6.2 tls /*
218 1.2.6.2 tls * any additional partition validation
219 1.2.6.2 tls */
220 1.2.6.2 tls int
221 1.2.6.2 tls md_check_partitions(void)
222 1.2.6.2 tls {
223 1.2.6.2 tls return 1;
224 1.2.6.2 tls }
225 1.2.6.2 tls
226 1.2.6.2 tls /*
227 1.2.6.2 tls * hook called before writing new disklabel.
228 1.2.6.2 tls */
229 1.2.6.2 tls int
230 1.2.6.2 tls md_pre_disklabel(void)
231 1.2.6.2 tls {
232 1.2.6.2 tls return 0;
233 1.2.6.2 tls }
234 1.2.6.2 tls
235 1.2.6.2 tls /*
236 1.2.6.2 tls * hook called after writing disklabel to new target disk.
237 1.2.6.2 tls */
238 1.2.6.2 tls int
239 1.2.6.2 tls md_post_disklabel(void)
240 1.2.6.2 tls {
241 1.2.6.2 tls return 0;
242 1.2.6.2 tls }
243 1.2.6.2 tls
244 1.2.6.2 tls /*
245 1.2.6.2 tls * hook called after upgrade() or install() has finished setting
246 1.2.6.2 tls * up the target disk but immediately before the user is given the
247 1.2.6.2 tls * ``disks are now set up'' message.
248 1.2.6.2 tls */
249 1.2.6.2 tls int
250 1.2.6.2 tls md_post_newfs(void)
251 1.2.6.2 tls {
252 1.2.6.2 tls return 0;
253 1.2.6.2 tls }
254 1.2.6.2 tls
255 1.2.6.2 tls int
256 1.2.6.2 tls md_post_extract(void)
257 1.2.6.2 tls {
258 1.2.6.2 tls return 0;
259 1.2.6.2 tls }
260 1.2.6.2 tls
261 1.2.6.2 tls void
262 1.2.6.2 tls md_cleanup_install(void)
263 1.2.6.2 tls {
264 1.2.6.2 tls #ifndef DEBUG
265 1.2.6.2 tls enable_rc_conf();
266 1.2.6.2 tls #endif
267 1.2.6.2 tls }
268 1.2.6.2 tls
269 1.2.6.2 tls int
270 1.2.6.2 tls md_pre_update(void)
271 1.2.6.2 tls {
272 1.2.6.2 tls return 1;
273 1.2.6.2 tls }
274 1.2.6.2 tls
275 1.2.6.2 tls /* Upgrade support */
276 1.2.6.2 tls int
277 1.2.6.2 tls md_update(void)
278 1.2.6.2 tls {
279 1.2.6.2 tls md_post_newfs();
280 1.2.6.2 tls return 1;
281 1.2.6.2 tls }
282 1.2.6.2 tls
283 1.2.6.2 tls /*
284 1.2.6.2 tls * static int filecore_checksum(u_char *bootblock)
285 1.2.6.2 tls *
286 1.2.6.2 tls * Calculates the filecore boot block checksum. This is used to validate
287 1.2.6.2 tls * a filecore boot block on the disk. If a boot block is validated then
288 1.2.6.2 tls * it is used to locate the partition table. If the boot block is not
289 1.2.6.2 tls * validated, it is assumed that the whole disk is NetBSD.
290 1.2.6.2 tls *
291 1.2.6.2 tls * The basic algorithm is:
292 1.2.6.2 tls *
293 1.2.6.2 tls * for (each byte in block, excluding checksum) {
294 1.2.6.2 tls * sum += byte;
295 1.2.6.2 tls * if (sum > 255)
296 1.2.6.2 tls * sum -= 255;
297 1.2.6.2 tls * }
298 1.2.6.2 tls *
299 1.2.6.2 tls * That's equivalent to summing all of the bytes in the block
300 1.2.6.2 tls * (excluding the checksum byte, of course), then calculating the
301 1.2.6.2 tls * checksum as "cksum = sum - ((sum - 1) / 255) * 255)". That
302 1.2.6.2 tls * expression may or may not yield a faster checksum function,
303 1.2.6.2 tls * but it's easier to reason about.
304 1.2.6.2 tls *
305 1.2.6.2 tls * Note that if you have a block filled with bytes of a single
306 1.2.6.2 tls * value "X" (regardless of that value!) and calculate the cksum
307 1.2.6.2 tls * of the block (excluding the checksum byte), you will _always_
308 1.2.6.2 tls * end up with a checksum of X. (Do the math; that can be derived
309 1.2.6.2 tls * from the checksum calculation function!) That means that
310 1.2.6.2 tls * blocks which contain bytes which all have the same value will
311 1.2.6.2 tls * always checksum properly. That's a _very_ unlikely occurence
312 1.2.6.2 tls * (probably impossible, actually) for a valid filecore boot block,
313 1.2.6.2 tls * so we treat such blocks as invalid.
314 1.2.6.2 tls */
315 1.2.6.2 tls
316 1.2.6.2 tls static int
317 1.2.6.2 tls filecore_checksum(u_char *bootblock)
318 1.2.6.2 tls {
319 1.2.6.2 tls u_char byte0, accum_diff;
320 1.2.6.2 tls u_int sum;
321 1.2.6.2 tls int i;
322 1.2.6.2 tls
323 1.2.6.2 tls sum = 0;
324 1.2.6.2 tls accum_diff = 0;
325 1.2.6.2 tls byte0 = bootblock[0];
326 1.2.6.2 tls
327 1.2.6.2 tls /*
328 1.2.6.2 tls * Sum the contents of the block, keeping track of whether
329 1.2.6.2 tls * or not all bytes are the same. If 'accum_diff' ends up
330 1.2.6.2 tls * being zero, all of the bytes are, in fact, the same.
331 1.2.6.2 tls */
332 1.2.6.2 tls for (i = 0; i < 511; ++i) {
333 1.2.6.2 tls sum += bootblock[i];
334 1.2.6.2 tls accum_diff |= bootblock[i] ^ byte0;
335 1.2.6.2 tls }
336 1.2.6.2 tls
337 1.2.6.2 tls /*
338 1.2.6.2 tls * Check to see if the checksum byte is the same as the
339 1.2.6.2 tls * rest of the bytes, too. (Note that if all of the bytes
340 1.2.6.2 tls * are the same except the checksum, a checksum compare
341 1.2.6.2 tls * won't succeed, but that's not our problem.)
342 1.2.6.2 tls */
343 1.2.6.2 tls accum_diff |= bootblock[i] ^ byte0;
344 1.2.6.2 tls
345 1.2.6.2 tls /* All bytes in block are the same; call it invalid. */
346 1.2.6.2 tls if (accum_diff == 0)
347 1.2.6.2 tls return (-1);
348 1.2.6.2 tls
349 1.2.6.2 tls return (sum - ((sum - 1) / 255) * 255);
350 1.2.6.2 tls }
351 1.2.6.2 tls
352 1.2.6.2 tls int
353 1.2.6.2 tls md_pre_mount()
354 1.2.6.2 tls {
355 1.2.6.2 tls return 0;
356 1.2.6.2 tls }
357