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