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