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