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