Home | History | Annotate | Line # | Download | only in installboot
installboot.c revision 1.20.84.1
      1 /*	$NetBSD: installboot.c,v 1.20.84.1 2008/12/13 01:13:04 haad 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 #include <limits.h>
     46 #include <nlist.h>
     47 #include <kvm.h>
     48 
     49 #define	DKTYPENAMES
     50 #define	FSTYPENAMES
     51 #include <sys/disklabel.h>
     52 #include <machine/ahdilabel.h>
     53 
     54 #include "installboot.h"
     55 
     56 static void	usage __P((void));
     57 static void	oscheck __P((void));
     58 static u_int	abcksum __P((void *));
     59 static void	setNVpref __P((void));
     60 static void	setIDEpar __P((u_int8_t *, size_t));
     61 static void	mkahdiboot __P((struct ahdi_root *, char *,
     62 						char *, daddr_t));
     63 static void	mkbootblock __P((struct bootblock *, char *,
     64 				char *, struct disklabel *, u_int));
     65 static void	install_fd __P((char *, struct disklabel *));
     66 static void	install_sd __P((char *, struct disklabel *));
     67 static void	install_wd __P((char *, struct disklabel *));
     68 
     69 static struct bootblock	bootarea;
     70 static struct ahdi_root ahdiboot;
     71 static const char	mdecpath[] = PATH_MDEC;
     72 static const char	stdpath[] = PATH_STD;
     73 static const char	milanpath[] = PATH_MILAN;
     74 static int		nowrite = 0;
     75 static int		verbose = 0;
     76 static int		trackpercyl = 0;
     77 static int		secpertrack = 0;
     78 static int		milan = 0;
     79 
     80 static void
     81 usage ()
     82 {
     83 	fprintf(stderr,
     84 		"usage: installboot [options] device\n"
     85 		"where options are:\n"
     86 		"\t-N  do not actually write anything on the disk\n"
     87 		"\t-m  use Milan boot blocks\n"
     88 		"\t-t  number of tracks per cylinder (IDE disk)\n"
     89 		"\t-u  number of sectors per track (IDE disk)\n"
     90 		"\t-v  verbose mode\n");
     91 	exit(EXIT_FAILURE);
     92 }
     93 
     94 int
     95 main (argc, argv)
     96 	int	argc;
     97 	char	*argv[];
     98 {
     99 	struct disklabel dl;
    100 	char		 *dn;
    101 	char		 *devchr;
    102 	int		 fd, c;
    103 
    104 	/* check OS bootversion */
    105 	oscheck();
    106 
    107 	/* parse options */
    108 	while ((c = getopt(argc, argv, "Nmt:u:v")) != -1) {
    109 		switch (c) {
    110 		  case 'N':
    111 			nowrite = 1;
    112 			break;
    113 		  case 'm':
    114 			milan = 1;
    115 			break;
    116 		  case 't':
    117 			trackpercyl = atoi(optarg);
    118 			break;
    119 		  case 'u':
    120 			secpertrack = atoi(optarg);
    121 			break;
    122 		  case 'v':
    123 			verbose = 1;
    124 			break;
    125 		  default:
    126 			usage();
    127 		}
    128 	}
    129 	argv += optind;
    130 	argc -= optind;
    131 	if (argc != 1)
    132 		usage();
    133 
    134 	/* get disk label */
    135 	dn = alloca(sizeof(_PATH_DEV) + strlen(argv[0]) + 8);
    136 	if (!strchr(argv[0], '/')) {
    137 		sprintf(dn, "%sr%s%c", _PATH_DEV, argv[0], RAW_PART + 'a');
    138 		fd = open(dn, O_RDONLY);
    139 		if (fd < 0 && errno == ENOENT) {
    140 			sprintf(dn, "%sr%s", _PATH_DEV, argv[0]);
    141 			fd = open(dn, O_RDONLY);
    142 		}
    143 	} else {
    144 		sprintf(dn, "%s", argv[0]);
    145 		fd = open(dn, O_RDONLY);
    146 	}
    147 	if (fd < 0)
    148 		err(EXIT_FAILURE, "%s", dn);
    149 	if (ioctl(fd, DIOCGDINFO, &dl))
    150 		err(EXIT_FAILURE, "%s: DIOCGDINFO", dn);
    151 	if (close(fd))
    152 		err(EXIT_FAILURE, "%s", dn);
    153 
    154 	/* Eg: in /dev/fd0c, set devchr to point to the 'f' */
    155 	devchr = strrchr(dn, '/') + 1;
    156 	if (*devchr == 'r')
    157 		++devchr;
    158 
    159 	switch (*devchr) {
    160 		case 'f': /* fd */
    161 			install_fd(dn, &dl);
    162 			break;
    163 		case 'w': /* wd */
    164 			install_wd(dn, &dl);
    165 			setNVpref();
    166 			break;
    167 		case 's': /* sd */
    168 			install_sd(dn, &dl);
    169 			setNVpref();
    170 			break;
    171 		default:
    172 			errx(EXIT_FAILURE,
    173 			     "%s: '%c': Device type not supported.",
    174 			     dn, *devchr);
    175 	}
    176 
    177 	return(EXIT_SUCCESS);
    178 }
    179 
    180 static void
    181 oscheck ()
    182 {
    183 	struct nlist	kbv[] = { { "_bootversion" },
    184 				  { NULL } };
    185 	kvm_t		*kd_kern;
    186 	char		errbuf[_POSIX2_LINE_MAX];
    187 	u_short		kvers;
    188 	struct stat	sb;
    189 
    190 	if (stat(_PATH_UNIX, &sb) < 0) {
    191 		warnx("Cannot stat %s, no bootversion check done", _PATH_UNIX);
    192 		return;
    193 	}
    194 
    195 	kd_kern = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
    196 	if (kd_kern == NULL)
    197 		errx(EXIT_FAILURE, "kvm_openfiles: %s", errbuf);
    198 	if (kvm_nlist(kd_kern, kbv) == -1)
    199 		errx(EXIT_FAILURE, "kvm_nlist: %s", kvm_geterr(kd_kern));
    200 	if (kbv[0].n_value == 0)
    201 		errx(EXIT_FAILURE, "%s not in namelist", kbv[0].n_name);
    202 	if (kvm_read(kd_kern, kbv[0].n_value, (char *)&kvers,
    203 						sizeof(kvers)) == -1)
    204 		errx(EXIT_FAILURE, "kvm_read: %s", kvm_geterr(kd_kern));
    205 	kvm_close(kd_kern);
    206 	if (kvers != BOOTVERSION)
    207 		errx(EXIT_FAILURE, "Kern bootversion: %d, expected: %d",
    208 					kvers, BOOTVERSION);
    209 }
    210 
    211 static void
    212 install_fd (devnm, label)
    213 	char		 *devnm;
    214 	struct disklabel *label;
    215 {
    216 	const char	 *machpath;
    217 	char		 *xxboot, *bootxx;
    218 	struct partition *rootpart;
    219 
    220 	if (label->d_secsize != 512)
    221 		errx(EXIT_FAILURE,
    222 		     "%s: %u: Block size not supported.", devnm,
    223 							  label->d_secsize);
    224 	if (label->d_ntracks != 2)
    225 		errx(EXIT_FAILURE,
    226 		     "%s: Single sided floppy not supported.", devnm);
    227 
    228 	if (milan)
    229 		machpath = milanpath;
    230 	else
    231 		machpath = stdpath;
    232 	xxboot = alloca(strlen(mdecpath) + strlen(machpath) + 8);
    233 	sprintf(xxboot, "%s%sfdboot", mdecpath, machpath);
    234 	bootxx = alloca(strlen(mdecpath) + strlen(machpath) + 8);
    235 	sprintf(bootxx, "%s%sbootxx", mdecpath, machpath);
    236 
    237 	/* first used partition (a, b or c) */		/* XXX */
    238 	for (rootpart = label->d_partitions; ; ++rootpart) {
    239 		if (rootpart->p_size)
    240 			break;
    241 	}
    242 	if (rootpart != label->d_partitions) {		/* XXX */
    243 		*(label->d_partitions) = *rootpart;
    244 		memset(rootpart, 0, sizeof(*rootpart));
    245 	}
    246 	label->d_partitions->p_fstype = FS_BSDFFS;	/* XXX */
    247 	label->d_npartitions = 1;
    248 	label->d_checksum = 0;
    249 	label->d_checksum = dkcksum(label);
    250 
    251 	trackpercyl = secpertrack = 0;
    252 	mkbootblock(&bootarea, xxboot, bootxx, label, 0);
    253 
    254 	if (!nowrite) {
    255 		int	fd;
    256 		if ((fd = open(devnm, O_WRONLY)) < 0)
    257 			err(EXIT_FAILURE, "%s", devnm);
    258 		if (write(fd, &bootarea, sizeof(bootarea)) != sizeof(bootarea))
    259 			err(EXIT_FAILURE, "%s", devnm);
    260 		if (close(fd))
    261 			err(EXIT_FAILURE, "%s", devnm);
    262 		if (verbose)
    263 			printf("Boot block installed on %s\n", devnm);
    264 	}
    265 }
    266 
    267 static void
    268 install_sd (devnm, label)
    269 	char		 *devnm;
    270 	struct disklabel *label;
    271 {
    272 	const char	 *machpath;
    273 	char		 *xxb00t, *xxboot, *bootxx;
    274 	struct disklabel rawlabel;
    275 	daddr_t		 bbsec;
    276 	u_int		 magic;
    277 
    278 	if (label->d_partitions[0].p_size == 0)
    279 		errx(EXIT_FAILURE, "%s: No root-filesystem.", devnm);
    280 	if (label->d_partitions[0].p_fstype != FS_BSDFFS)
    281 		errx(EXIT_FAILURE, "%s: %s: Illegal root-filesystem type.",
    282 		     devnm, fstypenames[label->d_partitions[0].p_fstype]);
    283 
    284 	bbsec = readdisklabel(devnm, &rawlabel);
    285 	if (bbsec == NO_BOOT_BLOCK)
    286 		errx(EXIT_FAILURE, "%s: No NetBSD boot block.", devnm);
    287 	if (memcmp(label, &rawlabel, sizeof(*label)))
    288 		errx(EXIT_FAILURE, "%s: Invalid NetBSD boot block.", devnm);
    289 
    290 	if (milan)
    291 		machpath = milanpath;
    292 	else
    293 		machpath = stdpath;
    294 	if (bbsec) {
    295 		xxb00t = alloca(strlen(mdecpath) + strlen(machpath) + 14);
    296 		sprintf(xxb00t, "%s%ssdb00t.ahdi", mdecpath, machpath);
    297 		xxboot = alloca(strlen(mdecpath) + strlen(machpath) + 14);
    298 		sprintf(xxboot, "%s%sxxboot.ahdi", mdecpath, machpath);
    299 		magic = AHDIMAGIC;
    300 	} else {
    301 		xxb00t = NULL;
    302 		xxboot = alloca(strlen(mdecpath) + strlen(machpath) + 8);
    303 		sprintf(xxboot, "%s%ssdboot", mdecpath, machpath);
    304 		magic = NBDAMAGIC;
    305 	}
    306 	bootxx = alloca(strlen(mdecpath) + strlen(machpath) + 8);
    307 	sprintf(bootxx, "%s%sbootxx", mdecpath, machpath);
    308 
    309 	trackpercyl = secpertrack = 0;
    310 	if (xxb00t)
    311 		mkahdiboot(&ahdiboot, xxb00t, devnm, bbsec);
    312 	mkbootblock(&bootarea, xxboot, bootxx, label, magic);
    313 
    314 	if (!nowrite) {
    315 		off_t	bbo = bbsec * AHDI_BSIZE;
    316 		int	fd;
    317 
    318 		if ((fd = open(devnm, O_WRONLY)) < 0)
    319 			err(EXIT_FAILURE, "%s", devnm);
    320 		if (lseek(fd, bbo, SEEK_SET) != bbo)
    321 			err(EXIT_FAILURE, "%s", devnm);
    322 		if (write(fd, &bootarea, sizeof(bootarea)) != sizeof(bootarea))
    323 			err(EXIT_FAILURE, "%s", devnm);
    324 		if (verbose)
    325 			printf("Boot block installed on %s (sector %d)\n", devnm,
    326 								    bbsec);
    327 		if (xxb00t) {
    328 			if (lseek(fd, (off_t)0, SEEK_SET) != 0)
    329 				err(EXIT_FAILURE, "%s", devnm);
    330 			if (write(fd, &ahdiboot, sizeof(ahdiboot)) != sizeof(ahdiboot))
    331 				err(EXIT_FAILURE, "%s", devnm);
    332 			if (verbose)
    333 				printf("AHDI root  installed on %s (0)\n",
    334 								devnm);
    335 		}
    336 		if (close(fd))
    337 			err(EXIT_FAILURE, "%s", devnm);
    338 	}
    339 }
    340 
    341 static void
    342 install_wd (devnm, label)
    343 	char		 *devnm;
    344 	struct disklabel *label;
    345 {
    346 	const char	 *machpath;
    347 	char		 *xxb00t, *xxboot, *bootxx;
    348 	struct disklabel rawlabel;
    349 	daddr_t		 bbsec;
    350 	u_int		 magic;
    351 
    352 	if (label->d_partitions[0].p_size == 0)
    353 		errx(EXIT_FAILURE, "%s: No root-filesystem.", devnm);
    354 	if (label->d_partitions[0].p_fstype != FS_BSDFFS)
    355 		errx(EXIT_FAILURE, "%s: %s: Illegal root-filesystem type.",
    356 		     devnm, fstypenames[label->d_partitions[0].p_fstype]);
    357 
    358 	bbsec = readdisklabel(devnm, &rawlabel);
    359 	if (bbsec == NO_BOOT_BLOCK)
    360 		errx(EXIT_FAILURE, "%s: No NetBSD boot block.", devnm);
    361 	if (memcmp(label, &rawlabel, sizeof(*label)))
    362 		errx(EXIT_FAILURE, "%s: Invalid NetBSD boot block.", devnm);
    363 
    364 	if (milan)
    365 		machpath = milanpath;
    366 	else
    367 		machpath = stdpath;
    368 	if (bbsec) {
    369 		xxb00t = alloca(strlen(mdecpath) + strlen(machpath) + 14);
    370 		sprintf(xxb00t, "%s%swdb00t.ahdi", mdecpath, machpath);
    371 		xxboot = alloca(strlen(mdecpath) + strlen(machpath) + 14);
    372 		sprintf(xxboot, "%s%sxxboot.ahdi", mdecpath, machpath);
    373 		magic = AHDIMAGIC;
    374 	} else {
    375 		xxb00t = NULL;
    376 		xxboot = alloca(strlen(mdecpath) + strlen(machpath) + 8);
    377 		sprintf(xxboot, "%s%swdboot", mdecpath, machpath);
    378 		magic = NBDAMAGIC;
    379 	}
    380 	bootxx = alloca(strlen(mdecpath) + strlen(machpath) + 8);
    381 	sprintf(bootxx, "%s%sbootxx", mdecpath, machpath);
    382 
    383 	if (xxb00t)
    384 		mkahdiboot(&ahdiboot, xxb00t, devnm, bbsec);
    385 	mkbootblock(&bootarea, xxboot, bootxx, label, magic);
    386 
    387 	if (!nowrite) {
    388 		int	fd;
    389 		off_t	bbo;
    390 
    391 		bbo = bbsec * AHDI_BSIZE;
    392 		if ((fd = open(devnm, O_WRONLY)) < 0)
    393 			err(EXIT_FAILURE, "%s", devnm);
    394 		if (lseek(fd, bbo, SEEK_SET) != bbo)
    395 			err(EXIT_FAILURE, "%s", devnm);
    396 		if (write(fd, &bootarea, sizeof(bootarea)) != sizeof(bootarea))
    397 			err(EXIT_FAILURE, "%s", devnm);
    398 		if (verbose)
    399 			printf("Boot block installed on %s (sector %d)\n", devnm,
    400 								    bbsec);
    401 		if (xxb00t) {
    402 			if (lseek(fd, (off_t)0, SEEK_SET) != 0)
    403 				err(EXIT_FAILURE, "%s", devnm);
    404 			if (write(fd, &ahdiboot, sizeof(ahdiboot))
    405 							!= sizeof(ahdiboot))
    406 				err(EXIT_FAILURE, "%s", devnm);
    407 			if (verbose)
    408 				printf("AHDI root  installed on %s (sector 0)\n",
    409 									devnm);
    410 		}
    411 		if (close(fd))
    412 			err(EXIT_FAILURE, "%s", devnm);
    413 	}
    414 }
    415 
    416 static void
    417 mkahdiboot (newroot, xxb00t, devnm, bbsec)
    418 	struct ahdi_root *newroot;
    419 	char		 *xxb00t,
    420 			 *devnm;
    421 	daddr_t		 bbsec;
    422 {
    423 	struct ahdi_root tmproot;
    424 	struct ahdi_part *pd;
    425 	int		 fd;
    426 
    427 	/* read prototype root-sector */
    428 	if ((fd = open(xxb00t, O_RDONLY)) < 0)
    429 		err(EXIT_FAILURE, "%s", xxb00t);
    430 	if (read(fd, &tmproot, sizeof(tmproot)) != sizeof(tmproot))
    431 		err(EXIT_FAILURE, "%s", xxb00t);
    432 	if (close(fd))
    433 		err(EXIT_FAILURE, "%s", xxb00t);
    434 
    435 	/* set tracks/cylinder and sectors/track */
    436 	setIDEpar(tmproot.ar_fill, sizeof(tmproot.ar_fill));
    437 
    438 	/* read current root-sector */
    439 	if ((fd = open(devnm, O_RDONLY)) < 0)
    440 		err(EXIT_FAILURE, "%s", devnm);
    441 	if (read(fd, newroot, sizeof(*newroot)) != sizeof(*newroot))
    442 		err(EXIT_FAILURE, "%s", devnm);
    443 	if (close(fd))
    444 		err(EXIT_FAILURE, "%s", devnm);
    445 
    446 	/* set bootflags */
    447 	for (pd = newroot->ar_parts; pd-newroot->ar_parts < AHDI_MAXRPD; ++pd) {
    448 		if (pd->ap_st == bbsec) {
    449 			pd->ap_flg = 0x21;	/* bootable, pref = NetBSD */
    450 			goto gotit;
    451 		}
    452 	}
    453 	errx(EXIT_FAILURE,
    454 	     "%s: NetBSD boot block not on primary AHDI partition.", devnm);
    455 
    456 gotit:	/* copy code from prototype and set new checksum */
    457 	memcpy(newroot->ar_fill, tmproot.ar_fill, sizeof(tmproot.ar_fill));
    458 	newroot->ar_checksum = 0;
    459 	newroot->ar_checksum = 0x1234 - abcksum(newroot);
    460 
    461 	if (verbose)
    462 		printf("AHDI      boot loader: %s\n", xxb00t);
    463 }
    464 
    465 static void
    466 mkbootblock (bb, xxb, bxx, label, magic)
    467 	struct bootblock *bb;
    468 	char		 *xxb,
    469 			 *bxx;
    470 	u_int		 magic;
    471 	struct disklabel *label;
    472 {
    473 	int		 fd;
    474 
    475 	memset(bb, 0, sizeof(*bb));
    476 
    477 	/* set boot block magic */
    478 	bb->bb_magic = magic;
    479 
    480 	/* set disk pack label */
    481 	BBSETLABEL(bb, label);
    482 
    483 	/* set second-stage boot loader */
    484 	if ((fd = open(bxx, O_RDONLY)) < 0)
    485 		err(EXIT_FAILURE, "%s", bxx);
    486 	if (read(fd, bb->bb_bootxx, sizeof(bb->bb_bootxx))
    487 					!= sizeof(bb->bb_bootxx))
    488 		err(EXIT_FAILURE, "%s", bxx);
    489 	if (close(fd))
    490 		err(EXIT_FAILURE, "%s", bxx);
    491 
    492 	/* set first-stage bootloader */
    493 	if ((fd = open(xxb, O_RDONLY)) < 0)
    494 		err(EXIT_FAILURE, "%s", xxb);
    495 	if (read(fd, bb->bb_xxboot, sizeof(bb->bb_xxboot))
    496 					!= sizeof(bb->bb_xxboot))
    497 		err(EXIT_FAILURE, "%s", xxb);
    498 	if (close(fd))
    499 		err(EXIT_FAILURE, "%s", xxb);
    500 
    501 	/* set tracks/cylinder and sectors/track */
    502 	setIDEpar(bb->bb_xxboot, sizeof(bb->bb_xxboot));
    503 
    504 	/* set AHDI checksum */
    505 	*((u_int16_t *)bb->bb_xxboot + 255) = 0;
    506 	*((u_int16_t *)bb->bb_xxboot + 255) = 0x1234 - abcksum(bb->bb_xxboot);
    507 
    508 	if (verbose) {
    509 		printf("Primary   boot loader: %s\n", xxb);
    510 		printf("Secondary boot loader: %s\n", bxx);
    511 	}
    512 }
    513 
    514 static void
    515 setIDEpar (start, size)
    516 	u_int8_t	*start;
    517 	size_t		size;
    518 {
    519 	static const u_int8_t	mark[] = { 'N', 'e', 't', 'B', 'S', 'D' };
    520 
    521 	if ((u_int)trackpercyl > 255)
    522 		errx(EXIT_FAILURE,
    523 		     "%d: Illegal tracks/cylinder value (1..255)", trackpercyl);
    524 	if ((u_int)secpertrack > 255)
    525 		errx(EXIT_FAILURE,
    526 		     "%d: Illegal sectors/track value (1..255)", secpertrack);
    527 
    528 	if (trackpercyl || secpertrack) {
    529 		u_int8_t *p;
    530 
    531 		if (!trackpercyl)
    532 			errx(EXIT_FAILURE, "Need tracks/cylinder too.");
    533 		if (!secpertrack)
    534 			errx(EXIT_FAILURE, "Need sectors/track too.");
    535 
    536 		start += 2;
    537 		size  -= sizeof(mark) + 2;
    538 		for (p = start + size; p >= start; --p) {
    539 			if (*p != *mark)
    540 				continue;
    541 			if (!memcmp(p, mark, sizeof(mark)))
    542 				break;
    543 		}
    544 		if (p < start)
    545 			errx(EXIT_FAILURE,
    546 			     "Malformatted xxboot prototype.");
    547 
    548 		*--p = secpertrack;
    549 		*--p = trackpercyl;
    550 
    551 		if (verbose) {
    552 			printf("sectors/track  : %d\n", secpertrack);
    553 			printf("tracks/cylinder: %d\n", trackpercyl);
    554 		}
    555 	}
    556 }
    557 
    558 static void
    559 setNVpref ()
    560 {
    561 	static const u_char	bootpref = BOOTPREF_NETBSD;
    562 	static const char	nvrdev[] = PATH_NVRAM;
    563 
    564 	if (!nowrite) {
    565 		int	fd;
    566 
    567 		if ((fd = open(nvrdev, O_RDWR)) < 0)
    568 			err(EXIT_FAILURE, "%s", nvrdev);
    569 		if (lseek(fd, (off_t)1, SEEK_SET) != 1)
    570 			err(EXIT_FAILURE, "%s", nvrdev);
    571 		if (write(fd, &bootpref, (size_t)1) != 1)
    572 			err(EXIT_FAILURE, "%s", nvrdev);
    573 		if (close(fd))
    574 			err(EXIT_FAILURE, "%s", nvrdev);
    575 		if (verbose)
    576 			printf("Boot preference set to NetBSD.\n");
    577 	}
    578 }
    579 
    580 static u_int
    581 abcksum (bs)
    582 	void	*bs;
    583 {
    584 	u_int16_t sum  = 0,
    585 		  *st  = (u_int16_t *)bs,
    586 		  *end = (u_int16_t *)bs + 256;
    587 
    588 	while (st < end)
    589 		sum += *st++;
    590 	return(sum);
    591 }
    592