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