Home | History | Annotate | Line # | Download | only in installboot
installboot.c revision 1.7
      1  1.7  tsutsui /*	$NetBSD: installboot.c,v 1.7 2002/03/30 07:19:30 tsutsui 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.7  tsutsui int	verbose, nowrite, conblockmode, conblockstart;
     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.5   tsubai #define DEFAULT_ENTRY 0x600000
     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.7  tsutsui int		loadblocknums_contig __P((char *, int));
     90  1.7  tsutsui int		loadblocknums_ffs __P((char *, int));
     91  1.1   tsubai static void	devread __P((int, void *, daddr_t, size_t, char *));
     92  1.1   tsubai static void	usage __P((void));
     93  1.1   tsubai int 		main __P((int, char *[]));
     94  1.1   tsubai 
     95  1.1   tsubai 
     96  1.1   tsubai static void
     97  1.1   tsubai usage()
     98  1.1   tsubai {
     99  1.1   tsubai 	fprintf(stderr,
    100  1.7  tsutsui 	    "usage: installboot [-n] [-v] [-b bno] <boot> <proto> <device>\n");
    101  1.1   tsubai 	exit(1);
    102  1.1   tsubai }
    103  1.1   tsubai 
    104  1.1   tsubai int
    105  1.1   tsubai main(argc, argv)
    106  1.1   tsubai 	int argc;
    107  1.1   tsubai 	char *argv[];
    108  1.1   tsubai {
    109  1.1   tsubai 	int	c;
    110  1.1   tsubai 	int	devfd;
    111  1.1   tsubai 	char	*protostore;
    112  1.1   tsubai 	long	protosize;
    113  1.1   tsubai 	int	mib[2];
    114  1.1   tsubai 	size_t	size;
    115  1.7  tsutsui 	int (*loadblocknums_func) __P((char *, int));
    116  1.1   tsubai 
    117  1.7  tsutsui 	while ((c = getopt(argc, argv, "vnb:")) != -1) {
    118  1.1   tsubai 		switch (c) {
    119  1.1   tsubai 
    120  1.7  tsutsui 		case 'b':
    121  1.7  tsutsui 			/* generic override, supply starting block # */
    122  1.7  tsutsui 			conblockmode = 1;
    123  1.7  tsutsui 			conblockstart = atoi(optarg);
    124  1.7  tsutsui 			break;
    125  1.1   tsubai 		case 'n':
    126  1.1   tsubai 			/* Do not actually write the bootblock to disk */
    127  1.1   tsubai 			nowrite = 1;
    128  1.1   tsubai 			break;
    129  1.1   tsubai 		case 'v':
    130  1.1   tsubai 			/* Chat */
    131  1.1   tsubai 			verbose = 1;
    132  1.1   tsubai 			break;
    133  1.1   tsubai 		default:
    134  1.1   tsubai 			usage();
    135  1.1   tsubai 		}
    136  1.1   tsubai 	}
    137  1.1   tsubai 
    138  1.1   tsubai 	if (argc - optind < 3) {
    139  1.1   tsubai 		usage();
    140  1.1   tsubai 	}
    141  1.1   tsubai 
    142  1.1   tsubai 	boot = argv[optind];
    143  1.1   tsubai 	proto = argv[optind + 1];
    144  1.1   tsubai 	dev = argv[optind + 2];
    145  1.1   tsubai 
    146  1.1   tsubai 	if (verbose) {
    147  1.1   tsubai 		printf("boot: %s\n", boot);
    148  1.1   tsubai 		printf("proto: %s\n", proto);
    149  1.1   tsubai 		printf("device: %s\n", dev);
    150  1.1   tsubai 	}
    151  1.1   tsubai 
    152  1.1   tsubai 	/* Load proto blocks into core */
    153  1.1   tsubai 	if ((protostore = loadprotoblocks(proto, &protosize)) == NULL)
    154  1.1   tsubai 		exit(1);
    155  1.1   tsubai 
    156  1.1   tsubai 	/* Open and check raw disk device */
    157  1.1   tsubai 	if ((devfd = open(dev, O_RDONLY, 0)) < 0)
    158  1.1   tsubai 		err(1, "open: %s", dev);
    159  1.1   tsubai 
    160  1.1   tsubai 	/* Extract and load block numbers */
    161  1.7  tsutsui 	if (conblockmode)
    162  1.7  tsutsui 		loadblocknums_func = loadblocknums_contig;
    163  1.7  tsutsui 	else
    164  1.7  tsutsui 		loadblocknums_func = loadblocknums_ffs;
    165  1.7  tsutsui 
    166  1.7  tsutsui 	if ((loadblocknums_func)(boot, devfd) != 0)
    167  1.1   tsubai 		exit(1);
    168  1.1   tsubai 
    169  1.1   tsubai 	(void)close(devfd);
    170  1.1   tsubai 
    171  1.1   tsubai 	if (nowrite)
    172  1.1   tsubai 		return 0;
    173  1.1   tsubai 
    174  1.1   tsubai 	/* Write patched proto bootblocks into the superblock */
    175  1.1   tsubai 	if (protosize > SBSIZE - DEV_BSIZE)
    176  1.1   tsubai 		errx(1, "proto bootblocks too big");
    177  1.1   tsubai 
    178  1.1   tsubai 	if ((devfd = open(dev, O_RDWR, 0)) < 0)
    179  1.1   tsubai 		err(1, "open: %s", dev);
    180  1.1   tsubai 
    181  1.3   tsubai 	if (writeapplepartmap(devfd) < 0)
    182  1.3   tsubai 		err(1, "write apm: %s", dev);
    183  1.3   tsubai 
    184  1.1   tsubai 	if (lseek(devfd, BOOTSECTOR_OFFSET, SEEK_SET) != BOOTSECTOR_OFFSET)
    185  1.1   tsubai 		err(1, "lseek bootstrap");
    186  1.1   tsubai 
    187  1.1   tsubai 	/* Sync filesystems (to clean in-memory superblock?) */
    188  1.1   tsubai 	sync();
    189  1.1   tsubai 
    190  1.1   tsubai 	if (write(devfd, protostore, protosize) != protosize)
    191  1.1   tsubai 		err(1, "write bootstrap");
    192  1.1   tsubai 	(void)close(devfd);
    193  1.1   tsubai 	return 0;
    194  1.1   tsubai }
    195  1.1   tsubai 
    196  1.1   tsubai char *
    197  1.1   tsubai loadprotoblocks(fname, size)
    198  1.1   tsubai 	char *fname;
    199  1.1   tsubai 	long *size;
    200  1.1   tsubai {
    201  1.1   tsubai 	int	fd, sz;
    202  1.1   tsubai 	char	*bp;
    203  1.1   tsubai 	struct	stat statbuf;
    204  1.1   tsubai #ifdef BOOT_AOUT
    205  1.1   tsubai 	struct	exec *hp;
    206  1.1   tsubai #endif
    207  1.1   tsubai 	long	off;
    208  1.1   tsubai 	Elf32_Ehdr *eh;
    209  1.1   tsubai 	Elf32_Phdr *ph;
    210  1.1   tsubai 
    211  1.1   tsubai 	/* Locate block number array in proto file */
    212  1.1   tsubai 	if (nlist(fname, nl) != 0) {
    213  1.1   tsubai 		warnx("nlist: %s: symbols not found", fname);
    214  1.1   tsubai 		return NULL;
    215  1.1   tsubai 	}
    216  1.1   tsubai #ifdef BOOT_AOUT
    217  1.1   tsubai 	if (nl[X_BLOCKTABLE].n_type != N_DATA + N_EXT) {
    218  1.1   tsubai 		warnx("nlist: %s: wrong type", nl[X_BLOCKTABLE].n_un.n_name);
    219  1.1   tsubai 		return NULL;
    220  1.1   tsubai 	}
    221  1.1   tsubai 	if (nl[X_BLOCKCOUNT].n_type != N_DATA + N_EXT) {
    222  1.1   tsubai 		warnx("nlist: %s: wrong type", nl[X_BLOCKCOUNT].n_un.n_name);
    223  1.1   tsubai 		return NULL;
    224  1.1   tsubai 	}
    225  1.1   tsubai 	if (nl[X_BLOCKSIZE].n_type != N_DATA + N_EXT) {
    226  1.1   tsubai 		warnx("nlist: %s: wrong type", nl[X_BLOCKSIZE].n_un.n_name);
    227  1.1   tsubai 		return NULL;
    228  1.1   tsubai 	}
    229  1.1   tsubai #endif
    230  1.1   tsubai 
    231  1.1   tsubai 	if ((fd = open(fname, O_RDONLY)) < 0) {
    232  1.1   tsubai 		warn("open: %s", fname);
    233  1.1   tsubai 		return NULL;
    234  1.1   tsubai 	}
    235  1.1   tsubai 	if (fstat(fd, &statbuf) != 0) {
    236  1.1   tsubai 		warn("fstat: %s", fname);
    237  1.1   tsubai 		close(fd);
    238  1.1   tsubai 		return NULL;
    239  1.1   tsubai 	}
    240  1.1   tsubai 	if ((bp = calloc(roundup(statbuf.st_size, DEV_BSIZE), 1)) == NULL) {
    241  1.1   tsubai 		warnx("malloc: %s: no memory", fname);
    242  1.1   tsubai 		close(fd);
    243  1.1   tsubai 		return NULL;
    244  1.1   tsubai 	}
    245  1.1   tsubai 	if (read(fd, bp, statbuf.st_size) != statbuf.st_size) {
    246  1.1   tsubai 		warn("read: %s", fname);
    247  1.1   tsubai 		free(bp);
    248  1.1   tsubai 		close(fd);
    249  1.1   tsubai 		return NULL;
    250  1.1   tsubai 	}
    251  1.1   tsubai 	close(fd);
    252  1.1   tsubai 
    253  1.1   tsubai #ifdef BOOT_AOUT
    254  1.1   tsubai 	hp = (struct exec *)bp;
    255  1.1   tsubai #endif
    256  1.1   tsubai 	eh = (Elf32_Ehdr *)bp;
    257  1.1   tsubai 	ph = (Elf32_Phdr *)(bp + eh->e_phoff);
    258  1.1   tsubai 	sz = 1024;
    259  1.1   tsubai 
    260  1.1   tsubai 	/* Calculate the symbols' location within the proto file */
    261  1.1   tsubai 	off = ph->p_offset - eh->e_entry;
    262  1.1   tsubai 	block_table = (daddr_t *)(bp + nl[X_BLOCKTABLE].n_value + off);
    263  1.1   tsubai 	block_count_p = (int32_t *)(bp + nl[X_BLOCKCOUNT].n_value + off);
    264  1.1   tsubai 	block_size_p = (int32_t *)(bp + nl[X_BLOCKSIZE].n_value + off);
    265  1.2   tsubai 	entry_point_p = (int32_t *)(bp + nl[X_ENTRY_POINT].n_value + off);
    266  1.1   tsubai 
    267  1.1   tsubai 	if ((int)block_table & 3) {
    268  1.1   tsubai 		warn("%s: invalid address: block_table = %x",
    269  1.1   tsubai 		     fname, block_table);
    270  1.1   tsubai 		free(bp);
    271  1.1   tsubai 		close(fd);
    272  1.1   tsubai 		return NULL;
    273  1.1   tsubai 	}
    274  1.1   tsubai 	if ((int)block_count_p & 3) {
    275  1.1   tsubai 		warn("%s: invalid address: block_count_p = %x",
    276  1.1   tsubai 		     fname, block_count_p);
    277  1.1   tsubai 		free(bp);
    278  1.1   tsubai 		close(fd);
    279  1.1   tsubai 		return NULL;
    280  1.1   tsubai 	}
    281  1.1   tsubai 	if ((int)block_size_p & 3) {
    282  1.1   tsubai 		warn("%s: invalid address: block_size_p = %x",
    283  1.1   tsubai 		     fname, block_size_p);
    284  1.1   tsubai 		free(bp);
    285  1.1   tsubai 		close(fd);
    286  1.1   tsubai 		return NULL;
    287  1.1   tsubai 	}
    288  1.2   tsubai 	if ((int)entry_point_p & 3) {
    289  1.2   tsubai 		warn("%s: invalid address: entry_point_p = %x",
    290  1.2   tsubai 		     fname, entry_point_p);
    291  1.2   tsubai 		free(bp);
    292  1.2   tsubai 		close(fd);
    293  1.2   tsubai 		return NULL;
    294  1.2   tsubai 	}
    295  1.1   tsubai 	max_block_count = *block_count_p;
    296  1.1   tsubai 
    297  1.1   tsubai 	if (verbose) {
    298  1.2   tsubai 		printf("proto bootblock size: %ld\n", sz);
    299  1.1   tsubai 	}
    300  1.1   tsubai 
    301  1.1   tsubai 	/*
    302  1.1   tsubai 	 * We convert the a.out header in-vitro into something that
    303  1.1   tsubai 	 * Sun PROMs understand.
    304  1.1   tsubai 	 * Old-style (sun4) ROMs do not expect a header at all, so
    305  1.1   tsubai 	 * we turn the first two words into code that gets us past
    306  1.1   tsubai 	 * the 32-byte header where the actual code begins. In assembly
    307  1.1   tsubai 	 * speak:
    308  1.1   tsubai 	 *	.word	MAGIC		! a NOP
    309  1.1   tsubai 	 *	ba,a	start		!
    310  1.1   tsubai 	 *	.skip	24		! pad
    311  1.1   tsubai 	 * start:
    312  1.1   tsubai 	 */
    313  1.1   tsubai 
    314  1.1   tsubai 	*size = sz;
    315  1.1   tsubai 	return (bp + 0x74);
    316  1.1   tsubai }
    317  1.1   tsubai 
    318  1.1   tsubai static void
    319  1.1   tsubai devread(fd, buf, blk, size, msg)
    320  1.1   tsubai 	int	fd;
    321  1.1   tsubai 	void	*buf;
    322  1.1   tsubai 	daddr_t	blk;
    323  1.1   tsubai 	size_t	size;
    324  1.1   tsubai 	char	*msg;
    325  1.1   tsubai {
    326  1.1   tsubai 	if (lseek(fd, dbtob(blk), SEEK_SET) != dbtob(blk))
    327  1.1   tsubai 		err(1, "%s: devread: lseek", msg);
    328  1.1   tsubai 
    329  1.1   tsubai 	if (read(fd, buf, size) != size)
    330  1.1   tsubai 		err(1, "%s: devread: read", msg);
    331  1.1   tsubai }
    332  1.1   tsubai 
    333  1.7  tsutsui int loadblocknums_contig(boot, devfd)
    334  1.7  tsutsui 	char *boot;
    335  1.7  tsutsui 	int devfd;
    336  1.7  tsutsui {
    337  1.7  tsutsui 	int size;
    338  1.7  tsutsui 	struct stat sb;
    339  1.7  tsutsui 
    340  1.7  tsutsui 	if (stat(boot, &sb) == -1)
    341  1.7  tsutsui 		err(1, "stat: %s", boot);
    342  1.7  tsutsui 	size = sb.st_size;
    343  1.7  tsutsui 
    344  1.7  tsutsui 	if (verbose) {
    345  1.7  tsutsui 		printf("%s: blockstart %d\n", dev, conblockstart);
    346  1.7  tsutsui 		printf("%s: size %d\n", boot, size);
    347  1.7  tsutsui 	}
    348  1.7  tsutsui 
    349  1.7  tsutsui 	*block_size_p = roundup(size, 512);
    350  1.7  tsutsui 	*block_count_p = 1;
    351  1.7  tsutsui 	*entry_point_p = DEFAULT_ENTRY;
    352  1.7  tsutsui 
    353  1.7  tsutsui 	block_table[0] = conblockstart;
    354  1.7  tsutsui 
    355  1.7  tsutsui 	return 0;
    356  1.7  tsutsui }
    357  1.7  tsutsui 
    358  1.1   tsubai static char sblock[SBSIZE];
    359  1.1   tsubai 
    360  1.1   tsubai int
    361  1.7  tsutsui loadblocknums_ffs(boot, devfd)
    362  1.1   tsubai char	*boot;
    363  1.1   tsubai int	devfd;
    364  1.1   tsubai {
    365  1.1   tsubai 	int		i, fd;
    366  1.1   tsubai 	struct	stat	statbuf;
    367  1.1   tsubai 	struct	statfs	statfsbuf;
    368  1.1   tsubai 	struct fs	*fs;
    369  1.1   tsubai 	char		*buf;
    370  1.1   tsubai 	daddr_t		blk, *ap;
    371  1.1   tsubai 	struct dinode	*ip;
    372  1.1   tsubai 	int		ndb;
    373  1.1   tsubai 
    374  1.1   tsubai 	/*
    375  1.1   tsubai 	 * Open 2nd-level boot program and record the block numbers
    376  1.1   tsubai 	 * it occupies on the filesystem represented by `devfd'.
    377  1.1   tsubai 	 */
    378  1.1   tsubai 	if ((fd = open(boot, O_RDONLY)) < 0)
    379  1.1   tsubai 		err(1, "open: %s", boot);
    380  1.1   tsubai 
    381  1.1   tsubai 	if (fstatfs(fd, &statfsbuf) != 0)
    382  1.1   tsubai 		err(1, "statfs: %s", boot);
    383  1.1   tsubai 
    384  1.1   tsubai 	if (strncmp(statfsbuf.f_fstypename, "ffs", MFSNAMELEN) &&
    385  1.1   tsubai 	    strncmp(statfsbuf.f_fstypename, "ufs", MFSNAMELEN)) {
    386  1.1   tsubai 		errx(1, "%s: must be on an FFS filesystem", boot);
    387  1.1   tsubai 	}
    388  1.1   tsubai 
    389  1.1   tsubai 	if (fsync(fd) != 0)
    390  1.1   tsubai 		err(1, "fsync: %s", boot);
    391  1.1   tsubai 
    392  1.1   tsubai 	if (fstat(fd, &statbuf) != 0)
    393  1.1   tsubai 		err(1, "fstat: %s", boot);
    394  1.1   tsubai 
    395  1.1   tsubai 	close(fd);
    396  1.1   tsubai 
    397  1.1   tsubai 	/* Read superblock */
    398  1.1   tsubai 	devread(devfd, sblock, btodb(SBOFF), SBSIZE, "superblock");
    399  1.1   tsubai 	fs = (struct fs *)sblock;
    400  1.1   tsubai 
    401  1.1   tsubai 	/* Read inode */
    402  1.1   tsubai 	if ((buf = malloc(fs->fs_bsize)) == NULL)
    403  1.1   tsubai 		errx(1, "No memory for filesystem block");
    404  1.1   tsubai 
    405  1.1   tsubai 	blk = fsbtodb(fs, ino_to_fsba(fs, statbuf.st_ino));
    406  1.1   tsubai 	devread(devfd, buf, blk, fs->fs_bsize, "inode");
    407  1.1   tsubai 	ip = (struct dinode *)(buf) + ino_to_fsbo(fs, statbuf.st_ino);
    408  1.1   tsubai 
    409  1.1   tsubai 	/*
    410  1.1   tsubai 	 * Register filesystem block size.
    411  1.1   tsubai 	 */
    412  1.1   tsubai 	*block_size_p = fs->fs_bsize;
    413  1.1   tsubai 
    414  1.1   tsubai 	/*
    415  1.1   tsubai 	 * Get the block numbers; we don't handle fragments
    416  1.1   tsubai 	 */
    417  1.1   tsubai 	ndb = howmany(ip->di_size, fs->fs_bsize);
    418  1.1   tsubai 	if (ndb > max_block_count)
    419  1.1   tsubai 		errx(1, "%s: Too many blocks", boot);
    420  1.1   tsubai 
    421  1.1   tsubai 	/*
    422  1.1   tsubai 	 * Register block count.
    423  1.1   tsubai 	 */
    424  1.1   tsubai 	*block_count_p = ndb;
    425  1.2   tsubai 
    426  1.2   tsubai 	/*
    427  1.2   tsubai 	 * Register entry point.
    428  1.2   tsubai 	 */
    429  1.2   tsubai 	*entry_point_p = DEFAULT_ENTRY;
    430  1.2   tsubai 	if (verbose)
    431  1.2   tsubai 		printf("entry point: 0x%08x\n", *entry_point_p);
    432  1.1   tsubai 
    433  1.1   tsubai 	if (verbose)
    434  1.1   tsubai 		printf("%s: block numbers: ", boot);
    435  1.1   tsubai 	ap = ip->di_db;
    436  1.1   tsubai 	for (i = 0; i < NDADDR && *ap && ndb; i++, ap++, ndb--) {
    437  1.1   tsubai 		blk = fsbtodb(fs, *ap);
    438  1.1   tsubai 		block_table[i] = blk;
    439  1.1   tsubai 		if (verbose)
    440  1.1   tsubai 			printf("%d ", blk);
    441  1.1   tsubai 	}
    442  1.1   tsubai 	if (verbose)
    443  1.1   tsubai 		printf("\n");
    444  1.1   tsubai 
    445  1.1   tsubai 	if (ndb == 0)
    446  1.1   tsubai 		return 0;
    447  1.1   tsubai 
    448  1.1   tsubai 	/*
    449  1.1   tsubai 	 * Just one level of indirections; there isn't much room
    450  1.1   tsubai 	 * for more in the 1st-level bootblocks anyway.
    451  1.1   tsubai 	 */
    452  1.1   tsubai 	if (verbose)
    453  1.1   tsubai 		printf("%s: block numbers (indirect): ", boot);
    454  1.1   tsubai 	blk = ip->di_ib[0];
    455  1.1   tsubai 	devread(devfd, buf, blk, fs->fs_bsize, "indirect block");
    456  1.1   tsubai 	ap = (daddr_t *)buf;
    457  1.1   tsubai 	for (; i < NINDIR(fs) && *ap && ndb; i++, ap++, ndb--) {
    458  1.1   tsubai 		blk = fsbtodb(fs, *ap);
    459  1.1   tsubai 		block_table[i] = blk;
    460  1.1   tsubai 		if (verbose)
    461  1.1   tsubai 			printf("%d ", blk);
    462  1.1   tsubai 	}
    463  1.1   tsubai 	if (verbose)
    464  1.1   tsubai 		printf("\n");
    465  1.1   tsubai 
    466  1.1   tsubai 	if (ndb)
    467  1.1   tsubai 		errx(1, "%s: Too many blocks", boot);
    468  1.3   tsubai 	return 0;
    469  1.3   tsubai }
    470  1.3   tsubai 
    471  1.3   tsubai int
    472  1.3   tsubai writeapplepartmap(fd)
    473  1.3   tsubai 	int fd;
    474  1.3   tsubai {
    475  1.3   tsubai 	struct drvr_map dm;
    476  1.3   tsubai 	struct partmapentry pme;
    477  1.3   tsubai 
    478  1.3   tsubai 	/* block 0 */
    479  1.3   tsubai 	if (lseek(fd, 0, SEEK_SET) != 0)
    480  1.3   tsubai 		return -1;
    481  1.3   tsubai 	if (read(fd, &dm, 512) != 512)		/* read existing disklabel */
    482  1.3   tsubai 		return -1;
    483  1.3   tsubai 	if (lseek(fd, 0, SEEK_SET) != 0)
    484  1.3   tsubai 		return -1;
    485  1.3   tsubai 
    486  1.3   tsubai 	dm.sbSig = DRIVER_MAP_MAGIC;
    487  1.3   tsubai 	dm.sbBlockSize = 512;
    488  1.3   tsubai 	dm.sbBlkCount = 0;
    489  1.3   tsubai 
    490  1.3   tsubai 	if (write(fd, &dm, 512) != 512)
    491  1.3   tsubai 		return -1;
    492  1.3   tsubai 
    493  1.3   tsubai 	/* block 1: Apple Partition Map */
    494  1.6      wiz 	memset(&pme, 0, sizeof(pme));
    495  1.3   tsubai 	pme.pmSig = DPME_MAGIC;
    496  1.3   tsubai 	pme.pmMapBlkCnt = 2;
    497  1.3   tsubai 	pme.pmPyPartStart = 1;
    498  1.3   tsubai 	pme.pmPartBlkCnt = pme.pmDataCnt = 2;
    499  1.3   tsubai 	strcpy(pme.pmPartName, "Apple");
    500  1.3   tsubai 	strcpy(pme.pmPartType, "Apple_partition_map");
    501  1.3   tsubai 
    502  1.3   tsubai 	pme.pmPartStatus = 0x37;
    503  1.3   tsubai 
    504  1.3   tsubai 	if (lseek(fd, 512, SEEK_SET) != 512)
    505  1.3   tsubai 		return -1;
    506  1.3   tsubai 	if (write(fd, &pme, 512) != 512)
    507  1.3   tsubai 		return -1;
    508  1.3   tsubai 
    509  1.3   tsubai 	/* block 2: NetBSD partition */
    510  1.6      wiz 	memset(&pme, 0, sizeof(pme));
    511  1.3   tsubai 	pme.pmSig = DPME_MAGIC;
    512  1.3   tsubai 	pme.pmMapBlkCnt = 2;
    513  1.3   tsubai 	pme.pmPyPartStart = 4;
    514  1.3   tsubai 	pme.pmPartBlkCnt = pme.pmDataCnt = 0x7fffffff;
    515  1.3   tsubai 	strcpy(pme.pmPartName, "NetBSD");
    516  1.3   tsubai 	strcpy(pme.pmPartType, "NetBSD/macppc");
    517  1.3   tsubai 	pme.pmPartStatus = 0x3b;
    518  1.3   tsubai 	pme.pmBootSize = 0x400;
    519  1.3   tsubai 	pme.pmBootLoad = 0x4000;
    520  1.3   tsubai 	pme.pmBootEntry = 0x4000;
    521  1.3   tsubai 	strcpy(pme.pmProcessor, "PowerPC");
    522  1.3   tsubai 
    523  1.3   tsubai 	if (lseek(fd, 1024, SEEK_SET) != 1024)
    524  1.3   tsubai 		return -1;
    525  1.3   tsubai 	if (write(fd, &pme, 512) != 512)
    526  1.3   tsubai 		return -1;
    527  1.3   tsubai 
    528  1.1   tsubai 	return 0;
    529  1.1   tsubai }
    530