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