Home | History | Annotate | Line # | Download | only in installboot
installboot.c revision 1.3
      1  1.3  tsubai /*	$NetBSD: installboot.c,v 1.3 1998/07/17 18:48:37 tsubai Exp $ */
      2  1.1  tsubai 
      3  1.1  tsubai /*
      4  1.1  tsubai  * Copyright (c) 1994 Paul Kranenburg
      5  1.1  tsubai  * All rights reserved.
      6  1.1  tsubai  *
      7  1.1  tsubai  * Redistribution and use in source and binary forms, with or without
      8  1.1  tsubai  * modification, are permitted provided that the following conditions
      9  1.1  tsubai  * are met:
     10  1.1  tsubai  * 1. Redistributions of source code must retain the above copyright
     11  1.1  tsubai  *    notice, this list of conditions and the following disclaimer.
     12  1.1  tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  tsubai  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  tsubai  *    documentation and/or other materials provided with the distribution.
     15  1.1  tsubai  * 3. All advertising materials mentioning features or use of this software
     16  1.1  tsubai  *    must display the following acknowledgement:
     17  1.1  tsubai  *      This product includes software developed by Paul Kranenburg.
     18  1.1  tsubai  * 4. The name of the author may not be used to endorse or promote products
     19  1.1  tsubai  *    derived from this software without specific prior written permission
     20  1.1  tsubai  *
     21  1.1  tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1  tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1  tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1  tsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1  tsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1  tsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1  tsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1  tsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1  tsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.1  tsubai  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1  tsubai  */
     32  1.1  tsubai 
     33  1.1  tsubai #include <sys/param.h>
     34  1.1  tsubai #include <sys/mount.h>
     35  1.1  tsubai #include <sys/time.h>
     36  1.1  tsubai #include <sys/stat.h>
     37  1.1  tsubai #include <sys/sysctl.h>
     38  1.1  tsubai #include <ufs/ufs/dinode.h>
     39  1.1  tsubai #include <ufs/ufs/dir.h>
     40  1.1  tsubai #include <ufs/ffs/fs.h>
     41  1.1  tsubai #include <err.h>
     42  1.1  tsubai #ifdef BOOT_AOUT
     43  1.1  tsubai #include <a.out.h>
     44  1.1  tsubai #endif
     45  1.1  tsubai #include <sys/exec_elf.h>
     46  1.1  tsubai #include <fcntl.h>
     47  1.1  tsubai #include <nlist.h>
     48  1.1  tsubai #include <stdlib.h>
     49  1.1  tsubai #include <stdio.h>
     50  1.1  tsubai #include <string.h>
     51  1.1  tsubai #include <unistd.h>
     52  1.1  tsubai 
     53  1.3  tsubai #include "dpme.h"
     54  1.3  tsubai 
     55  1.1  tsubai int	verbose, nowrite;
     56  1.1  tsubai char	*boot, *proto, *dev;
     57  1.1  tsubai 
     58  1.1  tsubai #define BOOTSECTOR_OFFSET 2048
     59  1.1  tsubai 
     60  1.2  tsubai #ifndef DEFAULT_ENTRY
     61  1.2  tsubai #define DEFAULT_ENTRY 0x6c0000
     62  1.2  tsubai #endif
     63  1.2  tsubai 
     64  1.1  tsubai struct nlist nl[] = {
     65  1.1  tsubai #define X_BLOCKTABLE	0
     66  1.1  tsubai 	{"_block_table"},
     67  1.1  tsubai #define X_BLOCKCOUNT	1
     68  1.1  tsubai 	{"_block_count"},
     69  1.1  tsubai #define X_BLOCKSIZE	2
     70  1.1  tsubai 	{"_block_size"},
     71  1.2  tsubai #define X_ENTRY_POINT	3
     72  1.2  tsubai 	{"_entry_point"},
     73  1.1  tsubai 	{NULL}
     74  1.1  tsubai };
     75  1.1  tsubai 
     76  1.1  tsubai daddr_t	*block_table;		/* block number array in prototype image */
     77  1.1  tsubai int32_t	*block_count_p;		/* size of this array */
     78  1.1  tsubai int32_t	*block_size_p;		/* filesystem block size */
     79  1.2  tsubai int32_t	*entry_point_p;		/* entry point */
     80  1.1  tsubai int32_t	max_block_count;
     81  1.1  tsubai 
     82  1.1  tsubai char		*loadprotoblocks __P((char *, long *));
     83  1.1  tsubai int		loadblocknums __P((char *, int));
     84  1.1  tsubai static void	devread __P((int, void *, daddr_t, size_t, char *));
     85  1.1  tsubai static void	usage __P((void));
     86  1.1  tsubai int 		main __P((int, char *[]));
     87  1.1  tsubai 
     88  1.1  tsubai 
     89  1.1  tsubai static void
     90  1.1  tsubai usage()
     91  1.1  tsubai {
     92  1.1  tsubai 	fprintf(stderr,
     93  1.1  tsubai 		"usage: installboot [-n] [-v] <boot> <proto> <device>\n");
     94  1.1  tsubai 	exit(1);
     95  1.1  tsubai }
     96  1.1  tsubai 
     97  1.1  tsubai int
     98  1.1  tsubai main(argc, argv)
     99  1.1  tsubai 	int argc;
    100  1.1  tsubai 	char *argv[];
    101  1.1  tsubai {
    102  1.1  tsubai 	int	c;
    103  1.1  tsubai 	int	devfd;
    104  1.1  tsubai 	char	*protostore;
    105  1.1  tsubai 	long	protosize;
    106  1.1  tsubai 	int	mib[2];
    107  1.1  tsubai 	size_t	size;
    108  1.1  tsubai 
    109  1.1  tsubai 	while ((c = getopt(argc, argv, "vn")) != EOF) {
    110  1.1  tsubai 		switch (c) {
    111  1.1  tsubai 
    112  1.1  tsubai 		case 'n':
    113  1.1  tsubai 			/* Do not actually write the bootblock to disk */
    114  1.1  tsubai 			nowrite = 1;
    115  1.1  tsubai 			break;
    116  1.1  tsubai 		case 'v':
    117  1.1  tsubai 			/* Chat */
    118  1.1  tsubai 			verbose = 1;
    119  1.1  tsubai 			break;
    120  1.1  tsubai 		default:
    121  1.1  tsubai 			usage();
    122  1.1  tsubai 		}
    123  1.1  tsubai 	}
    124  1.1  tsubai 
    125  1.1  tsubai 	if (argc - optind < 3) {
    126  1.1  tsubai 		usage();
    127  1.1  tsubai 	}
    128  1.1  tsubai 
    129  1.1  tsubai 	boot = argv[optind];
    130  1.1  tsubai 	proto = argv[optind + 1];
    131  1.1  tsubai 	dev = argv[optind + 2];
    132  1.1  tsubai 
    133  1.1  tsubai 	if (verbose) {
    134  1.1  tsubai 		printf("boot: %s\n", boot);
    135  1.1  tsubai 		printf("proto: %s\n", proto);
    136  1.1  tsubai 		printf("device: %s\n", dev);
    137  1.1  tsubai 	}
    138  1.1  tsubai 
    139  1.1  tsubai 	/* Load proto blocks into core */
    140  1.1  tsubai 	if ((protostore = loadprotoblocks(proto, &protosize)) == NULL)
    141  1.1  tsubai 		exit(1);
    142  1.1  tsubai 
    143  1.1  tsubai 	/* Open and check raw disk device */
    144  1.1  tsubai 	if ((devfd = open(dev, O_RDONLY, 0)) < 0)
    145  1.1  tsubai 		err(1, "open: %s", dev);
    146  1.1  tsubai 
    147  1.1  tsubai 	/* Extract and load block numbers */
    148  1.1  tsubai 	if (loadblocknums(boot, devfd) != 0)
    149  1.1  tsubai 		exit(1);
    150  1.1  tsubai 
    151  1.1  tsubai 	(void)close(devfd);
    152  1.1  tsubai 
    153  1.1  tsubai 	if (nowrite)
    154  1.1  tsubai 		return 0;
    155  1.1  tsubai 
    156  1.1  tsubai 	/* Write patched proto bootblocks into the superblock */
    157  1.1  tsubai 	if (protosize > SBSIZE - DEV_BSIZE)
    158  1.1  tsubai 		errx(1, "proto bootblocks too big");
    159  1.1  tsubai 
    160  1.1  tsubai 	if ((devfd = open(dev, O_RDWR, 0)) < 0)
    161  1.1  tsubai 		err(1, "open: %s", dev);
    162  1.1  tsubai 
    163  1.3  tsubai 	if (writeapplepartmap(devfd) < 0)
    164  1.3  tsubai 		err(1, "write apm: %s", dev);
    165  1.3  tsubai 
    166  1.1  tsubai 	if (lseek(devfd, BOOTSECTOR_OFFSET, SEEK_SET) != BOOTSECTOR_OFFSET)
    167  1.1  tsubai 		err(1, "lseek bootstrap");
    168  1.1  tsubai 
    169  1.1  tsubai 	/* Sync filesystems (to clean in-memory superblock?) */
    170  1.1  tsubai 	sync();
    171  1.1  tsubai 
    172  1.1  tsubai 	if (write(devfd, protostore, protosize) != protosize)
    173  1.1  tsubai 		err(1, "write bootstrap");
    174  1.1  tsubai 	(void)close(devfd);
    175  1.1  tsubai 	return 0;
    176  1.1  tsubai }
    177  1.1  tsubai 
    178  1.1  tsubai char *
    179  1.1  tsubai loadprotoblocks(fname, size)
    180  1.1  tsubai 	char *fname;
    181  1.1  tsubai 	long *size;
    182  1.1  tsubai {
    183  1.1  tsubai 	int	fd, sz;
    184  1.1  tsubai 	char	*bp;
    185  1.1  tsubai 	struct	stat statbuf;
    186  1.1  tsubai #ifdef BOOT_AOUT
    187  1.1  tsubai 	struct	exec *hp;
    188  1.1  tsubai #endif
    189  1.1  tsubai 	long	off;
    190  1.1  tsubai 	Elf32_Ehdr *eh;
    191  1.1  tsubai 	Elf32_Phdr *ph;
    192  1.1  tsubai 
    193  1.1  tsubai 	/* Locate block number array in proto file */
    194  1.1  tsubai 	if (nlist(fname, nl) != 0) {
    195  1.1  tsubai 		warnx("nlist: %s: symbols not found", fname);
    196  1.1  tsubai 		return NULL;
    197  1.1  tsubai 	}
    198  1.1  tsubai #ifdef BOOT_AOUT
    199  1.1  tsubai 	if (nl[X_BLOCKTABLE].n_type != N_DATA + N_EXT) {
    200  1.1  tsubai 		warnx("nlist: %s: wrong type", nl[X_BLOCKTABLE].n_un.n_name);
    201  1.1  tsubai 		return NULL;
    202  1.1  tsubai 	}
    203  1.1  tsubai 	if (nl[X_BLOCKCOUNT].n_type != N_DATA + N_EXT) {
    204  1.1  tsubai 		warnx("nlist: %s: wrong type", nl[X_BLOCKCOUNT].n_un.n_name);
    205  1.1  tsubai 		return NULL;
    206  1.1  tsubai 	}
    207  1.1  tsubai 	if (nl[X_BLOCKSIZE].n_type != N_DATA + N_EXT) {
    208  1.1  tsubai 		warnx("nlist: %s: wrong type", nl[X_BLOCKSIZE].n_un.n_name);
    209  1.1  tsubai 		return NULL;
    210  1.1  tsubai 	}
    211  1.1  tsubai #endif
    212  1.1  tsubai 
    213  1.1  tsubai 	if ((fd = open(fname, O_RDONLY)) < 0) {
    214  1.1  tsubai 		warn("open: %s", fname);
    215  1.1  tsubai 		return NULL;
    216  1.1  tsubai 	}
    217  1.1  tsubai 	if (fstat(fd, &statbuf) != 0) {
    218  1.1  tsubai 		warn("fstat: %s", fname);
    219  1.1  tsubai 		close(fd);
    220  1.1  tsubai 		return NULL;
    221  1.1  tsubai 	}
    222  1.1  tsubai 	if ((bp = calloc(roundup(statbuf.st_size, DEV_BSIZE), 1)) == NULL) {
    223  1.1  tsubai 		warnx("malloc: %s: no memory", fname);
    224  1.1  tsubai 		close(fd);
    225  1.1  tsubai 		return NULL;
    226  1.1  tsubai 	}
    227  1.1  tsubai 	if (read(fd, bp, statbuf.st_size) != statbuf.st_size) {
    228  1.1  tsubai 		warn("read: %s", fname);
    229  1.1  tsubai 		free(bp);
    230  1.1  tsubai 		close(fd);
    231  1.1  tsubai 		return NULL;
    232  1.1  tsubai 	}
    233  1.1  tsubai 	close(fd);
    234  1.1  tsubai 
    235  1.1  tsubai #ifdef BOOT_AOUT
    236  1.1  tsubai 	hp = (struct exec *)bp;
    237  1.1  tsubai #endif
    238  1.1  tsubai 	eh = (Elf32_Ehdr *)bp;
    239  1.1  tsubai 	ph = (Elf32_Phdr *)(bp + eh->e_phoff);
    240  1.1  tsubai 	sz = 1024;
    241  1.1  tsubai 
    242  1.1  tsubai 	/* Calculate the symbols' location within the proto file */
    243  1.1  tsubai 	off = ph->p_offset - eh->e_entry;
    244  1.1  tsubai 	block_table = (daddr_t *)(bp + nl[X_BLOCKTABLE].n_value + off);
    245  1.1  tsubai 	block_count_p = (int32_t *)(bp + nl[X_BLOCKCOUNT].n_value + off);
    246  1.1  tsubai 	block_size_p = (int32_t *)(bp + nl[X_BLOCKSIZE].n_value + off);
    247  1.2  tsubai 	entry_point_p = (int32_t *)(bp + nl[X_ENTRY_POINT].n_value + off);
    248  1.1  tsubai 
    249  1.1  tsubai 	if ((int)block_table & 3) {
    250  1.1  tsubai 		warn("%s: invalid address: block_table = %x",
    251  1.1  tsubai 		     fname, block_table);
    252  1.1  tsubai 		free(bp);
    253  1.1  tsubai 		close(fd);
    254  1.1  tsubai 		return NULL;
    255  1.1  tsubai 	}
    256  1.1  tsubai 	if ((int)block_count_p & 3) {
    257  1.1  tsubai 		warn("%s: invalid address: block_count_p = %x",
    258  1.1  tsubai 		     fname, block_count_p);
    259  1.1  tsubai 		free(bp);
    260  1.1  tsubai 		close(fd);
    261  1.1  tsubai 		return NULL;
    262  1.1  tsubai 	}
    263  1.1  tsubai 	if ((int)block_size_p & 3) {
    264  1.1  tsubai 		warn("%s: invalid address: block_size_p = %x",
    265  1.1  tsubai 		     fname, block_size_p);
    266  1.1  tsubai 		free(bp);
    267  1.1  tsubai 		close(fd);
    268  1.1  tsubai 		return NULL;
    269  1.1  tsubai 	}
    270  1.2  tsubai 	if ((int)entry_point_p & 3) {
    271  1.2  tsubai 		warn("%s: invalid address: entry_point_p = %x",
    272  1.2  tsubai 		     fname, entry_point_p);
    273  1.2  tsubai 		free(bp);
    274  1.2  tsubai 		close(fd);
    275  1.2  tsubai 		return NULL;
    276  1.2  tsubai 	}
    277  1.1  tsubai 	max_block_count = *block_count_p;
    278  1.1  tsubai 
    279  1.1  tsubai 	if (verbose) {
    280  1.2  tsubai 		printf("proto bootblock size: %ld\n", sz);
    281  1.1  tsubai 	}
    282  1.1  tsubai 
    283  1.1  tsubai 	/*
    284  1.1  tsubai 	 * We convert the a.out header in-vitro into something that
    285  1.1  tsubai 	 * Sun PROMs understand.
    286  1.1  tsubai 	 * Old-style (sun4) ROMs do not expect a header at all, so
    287  1.1  tsubai 	 * we turn the first two words into code that gets us past
    288  1.1  tsubai 	 * the 32-byte header where the actual code begins. In assembly
    289  1.1  tsubai 	 * speak:
    290  1.1  tsubai 	 *	.word	MAGIC		! a NOP
    291  1.1  tsubai 	 *	ba,a	start		!
    292  1.1  tsubai 	 *	.skip	24		! pad
    293  1.1  tsubai 	 * start:
    294  1.1  tsubai 	 */
    295  1.1  tsubai 
    296  1.1  tsubai 	*size = sz;
    297  1.1  tsubai 	return (bp + 0x74);
    298  1.1  tsubai }
    299  1.1  tsubai 
    300  1.1  tsubai static void
    301  1.1  tsubai devread(fd, buf, blk, size, msg)
    302  1.1  tsubai 	int	fd;
    303  1.1  tsubai 	void	*buf;
    304  1.1  tsubai 	daddr_t	blk;
    305  1.1  tsubai 	size_t	size;
    306  1.1  tsubai 	char	*msg;
    307  1.1  tsubai {
    308  1.1  tsubai 	if (lseek(fd, dbtob(blk), SEEK_SET) != dbtob(blk))
    309  1.1  tsubai 		err(1, "%s: devread: lseek", msg);
    310  1.1  tsubai 
    311  1.1  tsubai 	if (read(fd, buf, size) != size)
    312  1.1  tsubai 		err(1, "%s: devread: read", msg);
    313  1.1  tsubai }
    314  1.1  tsubai 
    315  1.1  tsubai static char sblock[SBSIZE];
    316  1.1  tsubai 
    317  1.1  tsubai int
    318  1.1  tsubai loadblocknums(boot, devfd)
    319  1.1  tsubai char	*boot;
    320  1.1  tsubai int	devfd;
    321  1.1  tsubai {
    322  1.1  tsubai 	int		i, fd;
    323  1.1  tsubai 	struct	stat	statbuf;
    324  1.1  tsubai 	struct	statfs	statfsbuf;
    325  1.1  tsubai 	struct fs	*fs;
    326  1.1  tsubai 	char		*buf;
    327  1.1  tsubai 	daddr_t		blk, *ap;
    328  1.1  tsubai 	struct dinode	*ip;
    329  1.1  tsubai 	int		ndb;
    330  1.1  tsubai 
    331  1.1  tsubai 	/*
    332  1.1  tsubai 	 * Open 2nd-level boot program and record the block numbers
    333  1.1  tsubai 	 * it occupies on the filesystem represented by `devfd'.
    334  1.1  tsubai 	 */
    335  1.1  tsubai 	if ((fd = open(boot, O_RDONLY)) < 0)
    336  1.1  tsubai 		err(1, "open: %s", boot);
    337  1.1  tsubai 
    338  1.1  tsubai 	if (fstatfs(fd, &statfsbuf) != 0)
    339  1.1  tsubai 		err(1, "statfs: %s", boot);
    340  1.1  tsubai 
    341  1.1  tsubai 	if (strncmp(statfsbuf.f_fstypename, "ffs", MFSNAMELEN) &&
    342  1.1  tsubai 	    strncmp(statfsbuf.f_fstypename, "ufs", MFSNAMELEN)) {
    343  1.1  tsubai 		errx(1, "%s: must be on an FFS filesystem", boot);
    344  1.1  tsubai 	}
    345  1.1  tsubai 
    346  1.1  tsubai 	if (fsync(fd) != 0)
    347  1.1  tsubai 		err(1, "fsync: %s", boot);
    348  1.1  tsubai 
    349  1.1  tsubai 	if (fstat(fd, &statbuf) != 0)
    350  1.1  tsubai 		err(1, "fstat: %s", boot);
    351  1.1  tsubai 
    352  1.1  tsubai 	close(fd);
    353  1.1  tsubai 
    354  1.1  tsubai 	/* Read superblock */
    355  1.1  tsubai 	devread(devfd, sblock, btodb(SBOFF), SBSIZE, "superblock");
    356  1.1  tsubai 	fs = (struct fs *)sblock;
    357  1.1  tsubai 
    358  1.1  tsubai 	/* Read inode */
    359  1.1  tsubai 	if ((buf = malloc(fs->fs_bsize)) == NULL)
    360  1.1  tsubai 		errx(1, "No memory for filesystem block");
    361  1.1  tsubai 
    362  1.1  tsubai 	blk = fsbtodb(fs, ino_to_fsba(fs, statbuf.st_ino));
    363  1.1  tsubai 	devread(devfd, buf, blk, fs->fs_bsize, "inode");
    364  1.1  tsubai 	ip = (struct dinode *)(buf) + ino_to_fsbo(fs, statbuf.st_ino);
    365  1.1  tsubai 
    366  1.1  tsubai 	/*
    367  1.1  tsubai 	 * Register filesystem block size.
    368  1.1  tsubai 	 */
    369  1.1  tsubai 	*block_size_p = fs->fs_bsize;
    370  1.1  tsubai 
    371  1.1  tsubai 	/*
    372  1.1  tsubai 	 * Get the block numbers; we don't handle fragments
    373  1.1  tsubai 	 */
    374  1.1  tsubai 	ndb = howmany(ip->di_size, fs->fs_bsize);
    375  1.1  tsubai 	if (ndb > max_block_count)
    376  1.1  tsubai 		errx(1, "%s: Too many blocks", boot);
    377  1.1  tsubai 
    378  1.1  tsubai 	/*
    379  1.1  tsubai 	 * Register block count.
    380  1.1  tsubai 	 */
    381  1.1  tsubai 	*block_count_p = ndb;
    382  1.2  tsubai 
    383  1.2  tsubai 	/*
    384  1.2  tsubai 	 * Register entry point.
    385  1.2  tsubai 	 */
    386  1.2  tsubai 	*entry_point_p = DEFAULT_ENTRY;
    387  1.2  tsubai 	if (verbose)
    388  1.2  tsubai 		printf("entry point: 0x%08x\n", *entry_point_p);
    389  1.1  tsubai 
    390  1.1  tsubai 	if (verbose)
    391  1.1  tsubai 		printf("%s: block numbers: ", boot);
    392  1.1  tsubai 	ap = ip->di_db;
    393  1.1  tsubai 	for (i = 0; i < NDADDR && *ap && ndb; i++, ap++, ndb--) {
    394  1.1  tsubai 		blk = fsbtodb(fs, *ap);
    395  1.1  tsubai 		block_table[i] = blk;
    396  1.1  tsubai 		if (verbose)
    397  1.1  tsubai 			printf("%d ", blk);
    398  1.1  tsubai 	}
    399  1.1  tsubai 	if (verbose)
    400  1.1  tsubai 		printf("\n");
    401  1.1  tsubai 
    402  1.1  tsubai 	if (ndb == 0)
    403  1.1  tsubai 		return 0;
    404  1.1  tsubai 
    405  1.1  tsubai 	/*
    406  1.1  tsubai 	 * Just one level of indirections; there isn't much room
    407  1.1  tsubai 	 * for more in the 1st-level bootblocks anyway.
    408  1.1  tsubai 	 */
    409  1.1  tsubai 	if (verbose)
    410  1.1  tsubai 		printf("%s: block numbers (indirect): ", boot);
    411  1.1  tsubai 	blk = ip->di_ib[0];
    412  1.1  tsubai 	devread(devfd, buf, blk, fs->fs_bsize, "indirect block");
    413  1.1  tsubai 	ap = (daddr_t *)buf;
    414  1.1  tsubai 	for (; i < NINDIR(fs) && *ap && ndb; i++, ap++, ndb--) {
    415  1.1  tsubai 		blk = fsbtodb(fs, *ap);
    416  1.1  tsubai 		block_table[i] = blk;
    417  1.1  tsubai 		if (verbose)
    418  1.1  tsubai 			printf("%d ", blk);
    419  1.1  tsubai 	}
    420  1.1  tsubai 	if (verbose)
    421  1.1  tsubai 		printf("\n");
    422  1.1  tsubai 
    423  1.1  tsubai 	if (ndb)
    424  1.1  tsubai 		errx(1, "%s: Too many blocks", boot);
    425  1.3  tsubai 	return 0;
    426  1.3  tsubai }
    427  1.3  tsubai 
    428  1.3  tsubai int
    429  1.3  tsubai writeapplepartmap(fd)
    430  1.3  tsubai 	int fd;
    431  1.3  tsubai {
    432  1.3  tsubai 	struct drvr_map dm;
    433  1.3  tsubai 	struct partmapentry pme;
    434  1.3  tsubai 
    435  1.3  tsubai 	/* block 0 */
    436  1.3  tsubai 	if (lseek(fd, 0, SEEK_SET) != 0)
    437  1.3  tsubai 		return -1;
    438  1.3  tsubai 	if (read(fd, &dm, 512) != 512)		/* read existing disklabel */
    439  1.3  tsubai 		return -1;
    440  1.3  tsubai 	if (lseek(fd, 0, SEEK_SET) != 0)
    441  1.3  tsubai 		return -1;
    442  1.3  tsubai 
    443  1.3  tsubai 	dm.sbSig = DRIVER_MAP_MAGIC;
    444  1.3  tsubai 	dm.sbBlockSize = 512;
    445  1.3  tsubai 	dm.sbBlkCount = 0;
    446  1.3  tsubai 
    447  1.3  tsubai 	if (write(fd, &dm, 512) != 512)
    448  1.3  tsubai 		return -1;
    449  1.3  tsubai 
    450  1.3  tsubai 	/* block 1: Apple Partition Map */
    451  1.3  tsubai 	bzero(&pme, sizeof(pme));
    452  1.3  tsubai 	pme.pmSig = DPME_MAGIC;
    453  1.3  tsubai 	pme.pmMapBlkCnt = 2;
    454  1.3  tsubai 	pme.pmPyPartStart = 1;
    455  1.3  tsubai 	pme.pmPartBlkCnt = pme.pmDataCnt = 2;
    456  1.3  tsubai 	strcpy(pme.pmPartName, "Apple");
    457  1.3  tsubai 	strcpy(pme.pmPartType, "Apple_partition_map");
    458  1.3  tsubai 
    459  1.3  tsubai 	pme.pmPartStatus = 0x37;
    460  1.3  tsubai 
    461  1.3  tsubai 	if (lseek(fd, 512, SEEK_SET) != 512)
    462  1.3  tsubai 		return -1;
    463  1.3  tsubai 	if (write(fd, &pme, 512) != 512)
    464  1.3  tsubai 		return -1;
    465  1.3  tsubai 
    466  1.3  tsubai 	/* block 2: NetBSD partition */
    467  1.3  tsubai 	bzero(&pme, sizeof(pme));
    468  1.3  tsubai 	pme.pmSig = DPME_MAGIC;
    469  1.3  tsubai 	pme.pmMapBlkCnt = 2;
    470  1.3  tsubai 	pme.pmPyPartStart = 4;
    471  1.3  tsubai 	pme.pmPartBlkCnt = pme.pmDataCnt = 0x7fffffff;
    472  1.3  tsubai 	strcpy(pme.pmPartName, "NetBSD");
    473  1.3  tsubai 	strcpy(pme.pmPartType, "NetBSD/macppc");
    474  1.3  tsubai 	pme.pmPartStatus = 0x3b;
    475  1.3  tsubai 	pme.pmBootSize = 0x400;
    476  1.3  tsubai 	pme.pmBootLoad = 0x4000;
    477  1.3  tsubai 	pme.pmBootEntry = 0x4000;
    478  1.3  tsubai 	strcpy(pme.pmProcessor, "PowerPC");
    479  1.3  tsubai 
    480  1.3  tsubai 	if (lseek(fd, 1024, SEEK_SET) != 1024)
    481  1.3  tsubai 		return -1;
    482  1.3  tsubai 	if (write(fd, &pme, 512) != 512)
    483  1.3  tsubai 		return -1;
    484  1.3  tsubai 
    485  1.1  tsubai 	return 0;
    486  1.1  tsubai }
    487