installboot.c revision 1.2 1 1.2 leo /* $NetBSD: installboot.c,v 1.2 1996/03/28 21:53:35 leo Exp $ */
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1995 Waldi Ravens
5 1.1 leo * All rights reserved.
6 1.1 leo *
7 1.1 leo * Redistribution and use in source and binary forms, with or without
8 1.1 leo * modification, are permitted provided that the following conditions
9 1.1 leo * are met:
10 1.1 leo * 1. Redistributions of source code must retain the above copyright
11 1.1 leo * notice, this list of conditions and the following disclaimer.
12 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 leo * notice, this list of conditions and the following disclaimer in the
14 1.1 leo * documentation and/or other materials provided with the distribution.
15 1.1 leo * 3. All advertising materials mentioning features or use of this software
16 1.1 leo * must display the following acknowledgement:
17 1.1 leo * This product includes software developed by Waldi Ravens.
18 1.1 leo * 4. The name of the author may not be used to endorse or promote products
19 1.1 leo * derived from this software without specific prior written permission
20 1.1 leo *
21 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 leo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 leo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 leo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 leo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 leo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 leo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 leo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 leo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 leo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 leo */
32 1.1 leo
33 1.1 leo #include <sys/types.h>
34 1.1 leo #include <sys/param.h>
35 1.1 leo #include <sys/sysctl.h>
36 1.1 leo #include <sys/ioctl.h>
37 1.1 leo #include <unistd.h>
38 1.1 leo #include <string.h>
39 1.1 leo #include <stdlib.h>
40 1.1 leo #include <stdio.h>
41 1.1 leo #include <paths.h>
42 1.1 leo #include <fcntl.h>
43 1.1 leo #include <errno.h>
44 1.1 leo #include <err.h>
45 1.1 leo
46 1.2 leo /*
47 1.2 leo * Small reminder for myself ;-)
48 1.2 leo */
49 1.2 leo #if NetBSD != 199511
50 1.2 leo #error New NetBSD version! Update OS_LIST in installboot.h
51 1.2 leo #endif
52 1.2 leo
53 1.1 leo #define DKTYPENAMES
54 1.1 leo #include <sys/disklabel.h>
55 1.1 leo #include <machine/ahdilabel.h>
56 1.1 leo
57 1.1 leo #include "installboot.h"
58 1.1 leo
59 1.1 leo static void usage __P((void));
60 1.1 leo static void oscheck __P((void));
61 1.1 leo static u_int abcksum __P((void *));
62 1.1 leo static void setNVpref __P((void));
63 1.1 leo static void setIDEpar __P((u_int8_t *, size_t));
64 1.1 leo static void mkahdiboot __P((struct ahdi_root *, char *,
65 1.1 leo char *, daddr_t));
66 1.1 leo static void mkbootblock __P((struct bootblock *, char *,
67 1.1 leo char *, struct disklabel *, u_int));
68 1.1 leo static void install_fd __P((char *, struct disklabel *));
69 1.1 leo static void install_sd __P((char *, struct disklabel *));
70 1.1 leo static void install_wd __P((char *, struct disklabel *));
71 1.1 leo
72 1.1 leo static struct bootblock bootarea;
73 1.1 leo static struct ahdi_root ahdiboot;
74 1.1 leo static const char mdecpath[] = PATH_MDEC;
75 1.1 leo static int nowrite = 0;
76 1.1 leo static int verbose = 0;
77 1.1 leo static int trackpercyl = 0;
78 1.1 leo static int secpertrack = 0;
79 1.1 leo
80 1.1 leo static void
81 1.1 leo usage ()
82 1.1 leo {
83 1.1 leo fprintf(stderr,
84 1.1 leo "usage: installboot [options] device\n"
85 1.1 leo "where options are:\n"
86 1.1 leo "\t-N do not actually write anything on the disk\n"
87 1.1 leo "\t-t number of tracks per cylinder (IDE disk)\n"
88 1.1 leo "\t-u number of sectors per track (IDE disk)\n"
89 1.1 leo "\t-v verbose mode\n");
90 1.1 leo exit(EXIT_FAILURE);
91 1.1 leo }
92 1.1 leo
93 1.1 leo int
94 1.1 leo main (argc, argv)
95 1.1 leo int argc;
96 1.1 leo char *argv[];
97 1.1 leo {
98 1.1 leo struct disklabel dl;
99 1.1 leo char *dn;
100 1.1 leo int fd, c;
101 1.1 leo
102 1.1 leo /* check OS type, release and revision */
103 1.1 leo oscheck();
104 1.1 leo
105 1.1 leo /* parse options */
106 1.1 leo while ((c = getopt(argc, argv, "Nt:u:v")) != -1) {
107 1.1 leo switch (c) {
108 1.1 leo case 'N':
109 1.1 leo nowrite = 1;
110 1.1 leo break;
111 1.1 leo case 't':
112 1.1 leo trackpercyl = atoi(optarg);
113 1.1 leo break;
114 1.1 leo case 'u':
115 1.1 leo secpertrack = atoi(optarg);
116 1.1 leo break;
117 1.1 leo case 'v':
118 1.1 leo verbose = 1;
119 1.1 leo break;
120 1.1 leo default:
121 1.1 leo usage();
122 1.1 leo }
123 1.1 leo }
124 1.1 leo argv += optind;
125 1.1 leo argc -= optind;
126 1.1 leo if (argc != 1)
127 1.1 leo usage();
128 1.1 leo
129 1.1 leo /* get disk label */
130 1.1 leo dn = alloca(sizeof(_PATH_DEV) + strlen(argv[0]) + 8);
131 1.1 leo if (!strchr(argv[0], '/')) {
132 1.1 leo sprintf(dn, "%sr%s%c", _PATH_DEV, argv[0], RAW_PART + 'a');
133 1.1 leo fd = open(dn, O_RDONLY);
134 1.1 leo if (fd < 0 && errno == ENOENT) {
135 1.1 leo sprintf(dn, "%sr%s", _PATH_DEV, argv[0]);
136 1.1 leo fd = open(dn, O_RDONLY);
137 1.1 leo }
138 1.1 leo } else {
139 1.1 leo sprintf(dn, "%s", argv[0]);
140 1.1 leo fd = open(dn, O_RDONLY);
141 1.1 leo }
142 1.1 leo if (fd < 0)
143 1.1 leo err(EXIT_FAILURE, "%s", dn);
144 1.1 leo if (ioctl(fd, DIOCGDINFO, &dl))
145 1.1 leo err(EXIT_FAILURE, "%s: DIOCGDINFO", dn);
146 1.1 leo if (close(fd))
147 1.1 leo err(EXIT_FAILURE, "%s", dn);
148 1.1 leo
149 1.1 leo switch (dl.d_type) {
150 1.1 leo case DTYPE_FLOPPY:
151 1.1 leo install_fd(dn, &dl);
152 1.1 leo break;
153 1.1 leo case DTYPE_ST506:
154 1.1 leo install_wd(dn, &dl);
155 1.1 leo setNVpref();
156 1.1 leo break;
157 1.1 leo case DTYPE_SCSI:
158 1.1 leo install_sd(dn, &dl);
159 1.1 leo setNVpref();
160 1.1 leo break;
161 1.1 leo default:
162 1.1 leo errx(EXIT_FAILURE,
163 1.1 leo "%s: %s: Device type not supported.",
164 1.1 leo dn, dktypenames[dl.d_type]);
165 1.1 leo }
166 1.1 leo
167 1.1 leo return(EXIT_SUCCESS);
168 1.1 leo }
169 1.1 leo
170 1.2 leo static char *
171 1.2 leo lststr (lst, str, bra)
172 1.2 leo char *lst, *str, *bra;
173 1.2 leo {
174 1.2 leo char *p;
175 1.2 leo
176 1.2 leo while ((p = strchr(lst, bra[0])) != NULL) {
177 1.2 leo lst = strchr(++p, bra[1]);
178 1.2 leo if (strncmp(str, p, lst - p))
179 1.2 leo continue;
180 1.2 leo if ((p = strchr(lst, bra[0])))
181 1.2 leo *p = 0;
182 1.2 leo return(++lst);
183 1.2 leo }
184 1.2 leo return(NULL);
185 1.2 leo }
186 1.2 leo
187 1.1 leo static void
188 1.1 leo oscheck ()
189 1.1 leo {
190 1.2 leo /* ideally, this would be a nested function... */
191 1.2 leo static char *lststr __P((char *, char *, char *));
192 1.2 leo static const char os_list[] = OS_LIST;
193 1.2 leo
194 1.2 leo char *list, *type, *rel, *rev;
195 1.2 leo int mib[2], rvi;
196 1.1 leo size_t len;
197 1.1 leo
198 1.2 leo list = alloca(sizeof(os_list));
199 1.2 leo strcpy(list, os_list);
200 1.2 leo
201 1.1 leo mib[0] = CTL_KERN;
202 1.1 leo mib[1] = KERN_OSTYPE;
203 1.1 leo sysctl(mib, 2, NULL, &len, NULL, 0);
204 1.1 leo type = alloca(len);
205 1.1 leo sysctl(mib, 2, type, &len, NULL, 0);
206 1.2 leo if ((list = lststr(list, type, BRA_TYPE)) == NULL)
207 1.1 leo errx(EXIT_FAILURE,
208 1.1 leo "%s: OS type not supported", type);
209 1.1 leo
210 1.1 leo mib[0] = CTL_KERN;
211 1.1 leo mib[1] = KERN_OSRELEASE;
212 1.1 leo sysctl(mib, 2, NULL, &len, NULL, 0);
213 1.1 leo rel = alloca(len);
214 1.1 leo sysctl(mib, 2, rel, &len, NULL, 0);
215 1.2 leo if ((list = lststr(list, rel, BRA_RELEASE)) == NULL)
216 1.1 leo errx(EXIT_FAILURE,
217 1.1 leo "%s %s: OS release not supported", type, rel);
218 1.1 leo
219 1.1 leo mib[0] = CTL_KERN;
220 1.1 leo mib[1] = KERN_OSREV;
221 1.2 leo len = sizeof(rvi);
222 1.2 leo sysctl(mib, 2, &rvi, &len, NULL, 0);
223 1.2 leo rev = alloca(3 * sizeof(rvi));
224 1.2 leo sprintf(rev, "%u", rvi);
225 1.2 leo if ((list = lststr(list, rev, BRA_REVISION)) == NULL)
226 1.1 leo errx(EXIT_FAILURE,
227 1.2 leo "%s %s %s: OS revision not supported", type, rel, rev);
228 1.1 leo }
229 1.1 leo
230 1.1 leo static void
231 1.1 leo install_fd (devnm, label)
232 1.1 leo char *devnm;
233 1.1 leo struct disklabel *label;
234 1.1 leo {
235 1.1 leo char *xxboot, *bootxx;
236 1.1 leo struct partition *rootpart;
237 1.1 leo
238 1.1 leo if (label->d_secsize != 512)
239 1.1 leo errx(EXIT_FAILURE,
240 1.1 leo "%s: %u: Block size not supported.", devnm,
241 1.1 leo label->d_secsize);
242 1.1 leo if (label->d_ntracks != 2)
243 1.1 leo errx(EXIT_FAILURE,
244 1.1 leo "%s: Single sided floppy not supported.", devnm);
245 1.1 leo
246 1.1 leo xxboot = alloca(strlen(mdecpath) + 8);
247 1.1 leo sprintf(xxboot, "%sfdboot", mdecpath);
248 1.1 leo bootxx = alloca(strlen(mdecpath) + 8);
249 1.1 leo sprintf(bootxx, "%sbootxx", mdecpath);
250 1.1 leo
251 1.1 leo /* first used partition (a, b or c) */ /* XXX */
252 1.1 leo for (rootpart = label->d_partitions; ; ++rootpart) {
253 1.1 leo if (rootpart->p_size)
254 1.1 leo break;
255 1.1 leo }
256 1.1 leo if (rootpart != label->d_partitions) { /* XXX */
257 1.1 leo *(label->d_partitions) = *rootpart;
258 1.1 leo memset(rootpart, 0, sizeof(*rootpart));
259 1.1 leo }
260 1.1 leo label->d_partitions->p_fstype = FS_BSDFFS; /* XXX */
261 1.1 leo label->d_npartitions = 1;
262 1.1 leo label->d_checksum = 0;
263 1.1 leo label->d_checksum = dkcksum(label);
264 1.1 leo
265 1.1 leo trackpercyl = secpertrack = 0;
266 1.1 leo mkbootblock(&bootarea, xxboot, bootxx, label, 0);
267 1.1 leo
268 1.1 leo if (!nowrite) {
269 1.1 leo int fd;
270 1.1 leo if ((fd = open(devnm, O_WRONLY)) < 0)
271 1.1 leo err(EXIT_FAILURE, "%s", devnm);
272 1.1 leo if (write(fd, &bootarea, sizeof(bootarea)) != sizeof(bootarea))
273 1.1 leo err(EXIT_FAILURE, "%s", devnm);
274 1.1 leo if (close(fd))
275 1.1 leo err(EXIT_FAILURE, "%s", devnm);
276 1.1 leo if (verbose)
277 1.1 leo printf("Boot block installed on %s\n", devnm);
278 1.1 leo }
279 1.1 leo }
280 1.1 leo
281 1.1 leo static void
282 1.1 leo install_sd (devnm, label)
283 1.1 leo char *devnm;
284 1.1 leo struct disklabel *label;
285 1.1 leo {
286 1.1 leo char *xxb00t, *xxboot, *bootxx;
287 1.1 leo struct disklabel rawlabel;
288 1.1 leo daddr_t bbsec;
289 1.1 leo u_int magic;
290 1.1 leo
291 1.1 leo if (label->d_partitions[0].p_size == 0)
292 1.1 leo errx(EXIT_FAILURE, "%s: No root-filesystem.", devnm);
293 1.1 leo if (label->d_partitions[0].p_fstype != FS_BSDFFS)
294 1.1 leo errx(EXIT_FAILURE, "%s: %s: Illegal root-filesystem type.",
295 1.1 leo devnm, fstypenames[label->d_partitions[0].p_fstype]);
296 1.1 leo
297 1.1 leo bbsec = readdisklabel(devnm, &rawlabel);
298 1.1 leo if (bbsec == NO_BOOT_BLOCK)
299 1.1 leo errx(EXIT_FAILURE, "%s: No NetBSD boot block.", devnm);
300 1.1 leo if (memcmp(label, &rawlabel, sizeof(*label)))
301 1.1 leo errx(EXIT_FAILURE, "%s: Invalid NetBSD boot block.", devnm);
302 1.1 leo
303 1.1 leo if (bbsec) {
304 1.1 leo xxb00t = alloca(strlen(mdecpath) + 14);
305 1.1 leo sprintf(xxb00t, "%ssdb00t.ahdi", mdecpath);
306 1.1 leo xxboot = alloca(strlen(mdecpath) + 14);
307 1.1 leo sprintf(xxboot, "%sxxboot.ahdi", mdecpath);
308 1.1 leo magic = AHDIMAGIC;
309 1.1 leo } else {
310 1.1 leo xxb00t = NULL;
311 1.1 leo xxboot = alloca(strlen(mdecpath) + 8);
312 1.1 leo sprintf(xxboot, "%ssdboot", mdecpath);
313 1.1 leo magic = NBDAMAGIC;
314 1.1 leo }
315 1.1 leo bootxx = alloca(strlen(mdecpath) + 8);
316 1.1 leo sprintf(bootxx, "%sbootxx", mdecpath);
317 1.1 leo
318 1.1 leo trackpercyl = secpertrack = 0;
319 1.1 leo if (xxb00t)
320 1.1 leo mkahdiboot(&ahdiboot, xxb00t, devnm, bbsec);
321 1.1 leo mkbootblock(&bootarea, xxboot, bootxx, label, magic);
322 1.1 leo
323 1.1 leo if (!nowrite) {
324 1.1 leo off_t bbo = bbsec * AHDI_BSIZE;
325 1.1 leo int fd;
326 1.1 leo
327 1.1 leo if ((fd = open(devnm, O_WRONLY)) < 0)
328 1.1 leo err(EXIT_FAILURE, "%s", devnm);
329 1.1 leo if (lseek(fd, bbo, SEEK_SET) != bbo)
330 1.1 leo err(EXIT_FAILURE, "%s", devnm);
331 1.1 leo if (write(fd, &bootarea, sizeof(bootarea)) != sizeof(bootarea))
332 1.1 leo err(EXIT_FAILURE, "%s", devnm);
333 1.1 leo if (verbose)
334 1.1 leo printf("Boot block installed on %s (%u)\n", devnm,
335 1.1 leo bbsec);
336 1.1 leo if (xxb00t) {
337 1.1 leo if (lseek(fd, (off_t)0, SEEK_SET) != 0)
338 1.1 leo err(EXIT_FAILURE, "%s", devnm);
339 1.1 leo if (write(fd, &ahdiboot, sizeof(ahdiboot)) != sizeof(ahdiboot))
340 1.1 leo err(EXIT_FAILURE, "%s", devnm);
341 1.1 leo if (verbose)
342 1.1 leo printf("AHDI root installed on %s (0)\n",
343 1.1 leo devnm);
344 1.1 leo }
345 1.1 leo if (close(fd))
346 1.1 leo err(EXIT_FAILURE, "%s", devnm);
347 1.1 leo }
348 1.1 leo }
349 1.1 leo
350 1.1 leo static void
351 1.1 leo install_wd (devnm, label)
352 1.1 leo char *devnm;
353 1.1 leo struct disklabel *label;
354 1.1 leo {
355 1.1 leo char *xxb00t, *xxboot, *bootxx;
356 1.1 leo struct disklabel rawlabel;
357 1.1 leo daddr_t bbsec;
358 1.1 leo u_int magic;
359 1.1 leo
360 1.1 leo if (label->d_partitions[0].p_size == 0)
361 1.1 leo errx(EXIT_FAILURE, "%s: No root-filesystem.", devnm);
362 1.1 leo if (label->d_partitions[0].p_fstype != FS_BSDFFS)
363 1.1 leo errx(EXIT_FAILURE, "%s: %s: Illegal root-filesystem type.",
364 1.1 leo devnm, fstypenames[label->d_partitions[0].p_fstype]);
365 1.1 leo
366 1.1 leo bbsec = readdisklabel(devnm, &rawlabel);
367 1.1 leo if (bbsec == NO_BOOT_BLOCK)
368 1.1 leo errx(EXIT_FAILURE, "%s: No NetBSD boot block.", devnm);
369 1.1 leo if (memcmp(label, &rawlabel, sizeof(*label)))
370 1.1 leo errx(EXIT_FAILURE, "%s: Invalid NetBSD boot block.", devnm);
371 1.1 leo
372 1.1 leo if (bbsec) {
373 1.1 leo xxb00t = alloca(strlen(mdecpath) + 14);
374 1.1 leo sprintf(xxb00t, "%swdb00t.ahdi", mdecpath);
375 1.1 leo xxboot = alloca(strlen(mdecpath) + 14);
376 1.1 leo sprintf(xxboot, "%sxxboot.ahdi", mdecpath);
377 1.1 leo magic = AHDIMAGIC;
378 1.1 leo } else {
379 1.1 leo xxb00t = NULL;
380 1.1 leo xxboot = alloca(strlen(mdecpath) + 8);
381 1.1 leo sprintf(xxboot, "%swdboot", mdecpath);
382 1.1 leo magic = NBDAMAGIC;
383 1.1 leo }
384 1.1 leo bootxx = alloca(strlen(mdecpath) + 8);
385 1.1 leo sprintf(bootxx, "%sbootxx", mdecpath);
386 1.1 leo
387 1.1 leo if (xxb00t)
388 1.1 leo mkahdiboot(&ahdiboot, xxb00t, devnm, bbsec);
389 1.1 leo mkbootblock(&bootarea, xxboot, bootxx, label, magic);
390 1.1 leo
391 1.1 leo if (!nowrite) {
392 1.1 leo int fd;
393 1.1 leo off_t bbo;
394 1.1 leo
395 1.1 leo bbo = bbsec * AHDI_BSIZE;
396 1.1 leo if ((fd = open(devnm, O_WRONLY)) < 0)
397 1.1 leo err(EXIT_FAILURE, "%s", devnm);
398 1.1 leo if (lseek(fd, bbo, SEEK_SET) != bbo)
399 1.1 leo err(EXIT_FAILURE, "%s", devnm);
400 1.1 leo if (write(fd, &bootarea, sizeof(bootarea)) != sizeof(bootarea))
401 1.1 leo err(EXIT_FAILURE, "%s", devnm);
402 1.1 leo if (verbose)
403 1.1 leo printf("Boot block installed on %s (%u)\n", devnm,
404 1.1 leo bbsec);
405 1.1 leo if (xxb00t) {
406 1.1 leo if (lseek(fd, (off_t)0, SEEK_SET) != 0)
407 1.1 leo err(EXIT_FAILURE, "%s", devnm);
408 1.1 leo if (write(fd, &ahdiboot, sizeof(ahdiboot))
409 1.1 leo != sizeof(ahdiboot))
410 1.1 leo err(EXIT_FAILURE, "%s", devnm);
411 1.1 leo if (verbose)
412 1.1 leo printf("AHDI root installed on %s (0)\n",
413 1.1 leo devnm);
414 1.1 leo }
415 1.1 leo if (close(fd))
416 1.1 leo err(EXIT_FAILURE, "%s", devnm);
417 1.1 leo }
418 1.1 leo }
419 1.1 leo
420 1.1 leo static void
421 1.1 leo mkahdiboot (newroot, xxb00t, devnm, bbsec)
422 1.1 leo struct ahdi_root *newroot;
423 1.1 leo char *xxb00t,
424 1.1 leo *devnm;
425 1.1 leo daddr_t bbsec;
426 1.1 leo {
427 1.1 leo struct ahdi_root tmproot;
428 1.1 leo struct ahdi_part *pd;
429 1.1 leo int fd;
430 1.1 leo
431 1.1 leo /* read prototype root-sector */
432 1.1 leo if ((fd = open(xxb00t, O_RDONLY)) < 0)
433 1.1 leo err(EXIT_FAILURE, "%s", xxb00t);
434 1.1 leo if (read(fd, &tmproot, sizeof(tmproot)) != sizeof(tmproot))
435 1.1 leo err(EXIT_FAILURE, "%s", xxb00t);
436 1.1 leo if (close(fd))
437 1.1 leo err(EXIT_FAILURE, "%s", xxb00t);
438 1.1 leo
439 1.1 leo /* set tracks/cylinder and sectors/track */
440 1.1 leo setIDEpar(tmproot.ar_fill, sizeof(tmproot.ar_fill));
441 1.1 leo
442 1.1 leo /* read current root-sector */
443 1.1 leo if ((fd = open(devnm, O_RDONLY)) < 0)
444 1.1 leo err(EXIT_FAILURE, "%s", devnm);
445 1.1 leo if (read(fd, newroot, sizeof(*newroot)) != sizeof(*newroot))
446 1.1 leo err(EXIT_FAILURE, "%s", devnm);
447 1.1 leo if (close(fd))
448 1.1 leo err(EXIT_FAILURE, "%s", devnm);
449 1.1 leo
450 1.1 leo /* set bootflags */
451 1.1 leo for (pd = newroot->ar_parts; pd-newroot->ar_parts < AHDI_MAXRPD; ++pd) {
452 1.1 leo if (pd->ap_st == bbsec) {
453 1.1 leo pd->ap_flg = 0x21; /* bootable, pref = NetBSD */
454 1.1 leo goto gotit;
455 1.1 leo }
456 1.1 leo }
457 1.1 leo errx(EXIT_FAILURE,
458 1.1 leo "%s: NetBSD boot block not on primary AHDI partition.", devnm);
459 1.1 leo
460 1.1 leo gotit: /* copy code from prototype and set new checksum */
461 1.1 leo memcpy(newroot->ar_fill, tmproot.ar_fill, sizeof(tmproot.ar_fill));
462 1.1 leo newroot->ar_checksum = 0;
463 1.1 leo newroot->ar_checksum = 0x1234 - abcksum(newroot);
464 1.1 leo
465 1.1 leo if (verbose)
466 1.1 leo printf("AHDI boot loader: %s\n", xxb00t);
467 1.1 leo }
468 1.1 leo
469 1.1 leo static void
470 1.1 leo mkbootblock (bb, xxb, bxx, label, magic)
471 1.1 leo struct bootblock *bb;
472 1.1 leo char *xxb,
473 1.1 leo *bxx;
474 1.1 leo u_int magic;
475 1.1 leo struct disklabel *label;
476 1.1 leo {
477 1.1 leo int fd;
478 1.1 leo
479 1.1 leo memset(bb, 0, sizeof(*bb));
480 1.1 leo
481 1.1 leo /* set boot block magic */
482 1.1 leo bb->bb_magic = magic;
483 1.1 leo
484 1.1 leo /* set disk pack label */
485 1.1 leo BBSETLABEL(bb, label);
486 1.1 leo
487 1.1 leo /* set second-stage boot loader */
488 1.1 leo if ((fd = open(bxx, O_RDONLY)) < 0)
489 1.1 leo err(EXIT_FAILURE, "%s", bxx);
490 1.1 leo if (read(fd, bb->bb_bootxx, sizeof(bb->bb_bootxx))
491 1.1 leo != sizeof(bb->bb_bootxx))
492 1.1 leo err(EXIT_FAILURE, "%s", bxx);
493 1.1 leo if (close(fd))
494 1.1 leo err(EXIT_FAILURE, "%s", bxx);
495 1.1 leo
496 1.1 leo /* set first-stage bootloader */
497 1.1 leo if ((fd = open(xxb, O_RDONLY)) < 0)
498 1.1 leo err(EXIT_FAILURE, "%s", xxb);
499 1.1 leo if (read(fd, bb->bb_xxboot, sizeof(bb->bb_xxboot))
500 1.1 leo != sizeof(bb->bb_xxboot))
501 1.1 leo err(EXIT_FAILURE, "%s", xxb);
502 1.1 leo if (close(fd))
503 1.1 leo err(EXIT_FAILURE, "%s", xxb);
504 1.1 leo
505 1.1 leo /* set tracks/cylinder and sectors/track */
506 1.1 leo setIDEpar(bb->bb_xxboot, sizeof(bb->bb_xxboot));
507 1.1 leo
508 1.1 leo /* set AHDI checksum */
509 1.1 leo *((u_int16_t *)bb->bb_xxboot + 255) = 0;
510 1.1 leo *((u_int16_t *)bb->bb_xxboot + 255) = 0x1234 - abcksum(bb->bb_xxboot);
511 1.1 leo
512 1.1 leo if (verbose) {
513 1.1 leo printf("Primary boot loader: %s\n", xxb);
514 1.1 leo printf("Secondary boot loader: %s\n", bxx);
515 1.1 leo }
516 1.1 leo }
517 1.1 leo
518 1.1 leo static void
519 1.1 leo setIDEpar (start, size)
520 1.1 leo u_int8_t *start;
521 1.1 leo size_t size;
522 1.1 leo {
523 1.1 leo static const u_int8_t mark[] = { 'N', 'e', 't', 'B', 'S', 'D' };
524 1.1 leo
525 1.1 leo if ((u_int)trackpercyl > 255)
526 1.1 leo errx(EXIT_FAILURE,
527 1.1 leo "%d: Illegal tracks/cylinder value (1..255)", trackpercyl);
528 1.1 leo if ((u_int)secpertrack > 255)
529 1.1 leo errx(EXIT_FAILURE,
530 1.1 leo "%d: Illegal sectors/track value (1..255)", secpertrack);
531 1.1 leo
532 1.1 leo if (trackpercyl || secpertrack) {
533 1.1 leo u_int8_t *p;
534 1.1 leo
535 1.1 leo if (!trackpercyl)
536 1.1 leo errx(EXIT_FAILURE, "Need tracks/cylinder too.");
537 1.1 leo if (!secpertrack)
538 1.1 leo errx(EXIT_FAILURE, "Need sectors/track too.");
539 1.1 leo
540 1.1 leo start += 2;
541 1.1 leo size -= sizeof(mark) + 2;
542 1.1 leo for (p = start + size; p >= start; --p) {
543 1.1 leo if (*p != *mark)
544 1.1 leo continue;
545 1.1 leo if (!memcmp(p, mark, sizeof(mark)))
546 1.1 leo break;
547 1.1 leo }
548 1.1 leo if (p < start)
549 1.1 leo errx(EXIT_FAILURE,
550 1.1 leo "Malformatted xxboot prototype.");
551 1.1 leo
552 1.1 leo *--p = secpertrack;
553 1.1 leo *--p = trackpercyl;
554 1.1 leo
555 1.1 leo if (verbose) {
556 1.1 leo printf("sectors/track : %d\n", secpertrack);
557 1.1 leo printf("tracks/cylinder: %d\n", trackpercyl);
558 1.1 leo }
559 1.1 leo }
560 1.1 leo }
561 1.1 leo
562 1.1 leo static void
563 1.1 leo setNVpref ()
564 1.1 leo {
565 1.1 leo static const u_char bootpref = BOOTPREF_NETBSD;
566 1.1 leo static const char nvrdev[] = PATH_NVRAM;
567 1.1 leo
568 1.1 leo if (!nowrite) {
569 1.1 leo int fd;
570 1.1 leo
571 1.1 leo if ((fd = open(nvrdev, O_RDWR)) < 0)
572 1.1 leo err(EXIT_FAILURE, "%s", nvrdev);
573 1.1 leo if (lseek(fd, (off_t)1, SEEK_SET) != 1)
574 1.1 leo err(EXIT_FAILURE, "%s", nvrdev);
575 1.1 leo if (write(fd, &bootpref, (size_t)1) != 1)
576 1.1 leo err(EXIT_FAILURE, "%s", nvrdev);
577 1.1 leo if (close(fd))
578 1.1 leo err(EXIT_FAILURE, "%s", nvrdev);
579 1.1 leo if (verbose)
580 1.1 leo printf("Boot preference set to NetBSD.\n");
581 1.1 leo }
582 1.1 leo }
583 1.1 leo
584 1.1 leo static u_int
585 1.1 leo abcksum (bs)
586 1.1 leo void *bs;
587 1.1 leo {
588 1.1 leo u_int16_t sum = 0,
589 1.1 leo *st = (u_int16_t *)bs,
590 1.1 leo *end = (u_int16_t *)bs + 256;
591 1.1 leo
592 1.1 leo while (st < end)
593 1.1 leo sum += *st++;
594 1.1 leo return(sum);
595 1.1 leo }
596