Home | History | Annotate | Line # | Download | only in installboot
installboot.c revision 1.2
      1 /*	$NetBSD: installboot.c,v 1.2 2001/02/19 22:48:58 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2000 NONAKA Kimihiro (nonaka (at) netbsd.org).
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/param.h>
     31 #include <sys/exec_elf.h>
     32 #include <sys/disklabel_mbr.h>
     33 
     34 #include <stdio.h>
     35 #include <stdlib.h>
     36 #include <unistd.h>
     37 #include <fcntl.h>
     38 #include <err.h>
     39 
     40 #define	MBR_PTYPE_PREP	0x41
     41 
     42 int nowrite, verbose;
     43 char *boot, *dev;
     44 
     45 void usage(void);
     46 int devread(int, void *, daddr_t, size_t, char *);
     47 char *load_boot(char *, size_t *);
     48 int load_prep_partition(int, struct mbr_partition *);
     49 int main(int, char **);
     50 
     51 void
     52 usage()
     53 {
     54 
     55 	fprintf(stderr, "usage: %s [-n] [-v] <boot> <device>\n",
     56 	    getprogname());
     57 	exit(1);
     58 }
     59 
     60 int
     61 devread(int fd, void *buf, daddr_t blk, size_t size, char *msg)
     62 {
     63 
     64 	if (lseek(fd, (off_t)dbtob(blk), SEEK_SET) != dbtob(blk)) {
     65 		warn("%s: devread: lseek", msg);
     66 		return 1;
     67 	}
     68 	if (read(fd, buf, size) != size) {
     69 		warn("%s: devread: read", msg);
     70 		return 1;
     71 	}
     72 	return 0;
     73 }
     74 
     75 char *
     76 load_boot(char *boot, size_t *bootsize)
     77 {
     78 	Elf32_Ehdr eh;
     79 	Elf32_Phdr ph;
     80 	struct stat st;
     81 	int fd;
     82 	int i;
     83 	size_t imgsz = 0;
     84 	char *bp = NULL;
     85 
     86 	if ((fd = open(boot, O_RDONLY)) < 0) {
     87 		warn("open: %s", boot);
     88 		return NULL;
     89 	}
     90 
     91 	if (fstat(fd, &st) != 0) {
     92 		warn("fstat: %s", boot);
     93 		goto out;
     94 	}
     95 
     96 	/*
     97 	 * First, check ELF.
     98 	 */
     99 	if (read(fd, &eh, sizeof(eh)) != sizeof(eh)) {
    100 		warn("read: eh: %s", boot);
    101 		goto out;
    102 	}
    103 	if (memcmp(eh.e_ident, ELFMAG, SELFMAG) != 0 ||
    104 	    eh.e_ident[EI_CLASS] != ELFCLASS32) {
    105 		lseek(fd, 0L, SEEK_SET);
    106 		goto notelf;
    107 	}
    108 	if (be16toh(eh.e_machine) != EM_PPC) {
    109 		warn("not PowerPC binary.");
    110 		goto out;
    111 	}
    112 
    113 	for (i = 0; i < be16toh(eh.e_phnum); i++) {
    114 		(void)lseek(fd, be32toh(eh.e_phoff) + sizeof(ph) * i, SEEK_SET);
    115 		if (read(fd, &ph, sizeof(ph)) != sizeof(ph)) {
    116 			warn("read: ph: %s", boot);
    117 			goto out;
    118 		}
    119 
    120 		if ((be32toh(ph.p_type) != PT_LOAD) ||
    121 		    !(be32toh(ph.p_flags) & PF_X))
    122 			continue;
    123 
    124 		imgsz = st.st_size - be32toh(ph.p_offset);
    125 		lseek(fd, be32toh(ph.p_offset), SEEK_SET);
    126 		break;
    127 	}
    128 
    129 notelf:
    130 	/*
    131 	 * Second, check PReP bootable image.
    132 	 */
    133 	if (imgsz == 0) {
    134 		char buf[DEV_BSIZE];
    135 
    136 		printf("Bootable image: ");
    137 		if (load_prep_partition(fd, 0)) {
    138 			warn("no PReP bootable image.");
    139 			goto out;
    140 		}
    141 
    142 		if (lseek(fd, (off_t)dbtob(1), SEEK_SET) != dbtob(1)) {
    143 			warn("bootable image lseek sector 1");
    144 			goto out;
    145 		}
    146 		if (read(fd, buf, DEV_BSIZE) != DEV_BSIZE) {
    147 			warn("read: start/size");
    148 			goto out;
    149 		}
    150 
    151 		imgsz = le32toh(*(u_int32_t *)(buf + sizeof(u_int32_t)))
    152 		    - dbtob(2);
    153 		lseek(fd, le32toh(*(u_int32_t *)buf), SEEK_SET);
    154 	}
    155 
    156 	if ((bp = (char *)calloc(roundup(imgsz, DEV_BSIZE), 1)) == NULL) {
    157 		warn("calloc: no memory for boot image.");
    158 		goto out;
    159 	}
    160 
    161 	if (read(fd, bp, imgsz) != imgsz) {
    162 		warn("read: boot image: %s", boot);
    163 		goto out;
    164 	}
    165 
    166 	if (verbose) {
    167 		printf("image size = %d\n", imgsz);
    168 	}
    169 
    170 	*bootsize = roundup(imgsz, DEV_BSIZE);
    171 
    172 	close(fd);
    173 	return bp;
    174 
    175 out:
    176 	if (bp != NULL)
    177 		free(bp);
    178 	if (fd >= 0)
    179 		close(fd);
    180 	return NULL;
    181 }
    182 
    183 int
    184 load_prep_partition(int devfd, struct mbr_partition *ppp)
    185 {
    186 	char mbr[512];
    187 	struct mbr_partition *mbrp;
    188 	int i;
    189 
    190 	if (devread(devfd, mbr, MBR_BBSECTOR, DEV_BSIZE, "MBR") != 0)
    191 		return 1;
    192 	if (*(u_int16_t *)&mbr[MBR_MAGICOFF] != htole16(MBR_MAGIC)) {
    193 		warn("no MBR_MAGIC");
    194 		return 1;
    195 	}
    196 
    197 	mbrp = (struct mbr_partition *)&mbr[MBR_PARTOFF];
    198 	for (i = 0; i < NMBRPART; i++) {
    199 		if (mbrp[i].mbrp_typ == MBR_PTYPE_PREP)
    200 			break;
    201 	}
    202 	if (i == NMBRPART) {
    203 		warn("no PReP partition.");
    204 		return 1;
    205 	}
    206 
    207 	if (verbose) {
    208 		printf("PReP partition: start = %d, size = %d\n",
    209 		    le32toh(mbrp[i].mbrp_start), le32toh(mbrp[i].mbrp_size));
    210 	}
    211 
    212 	if (ppp) {
    213 		*ppp = mbrp[i];
    214 		ppp->mbrp_start = le32toh(ppp->mbrp_start);
    215 		ppp->mbrp_size = le32toh(ppp->mbrp_size);
    216 	}
    217 
    218 	return 0;
    219 }
    220 
    221 int
    222 main(int argc, char **argv)
    223 {
    224 	struct mbr_partition ppp;
    225 	size_t bootsize;
    226 	int c;
    227 	int boot00[512/sizeof(int)];
    228 	int devfd = -1;
    229 	char *bp;
    230 
    231 	while ((c = getopt(argc, argv, "vn")) != EOF) {
    232 		switch (c) {
    233 		case 'n':
    234 			nowrite = 1;
    235 			break;
    236 		case 'v':
    237 			verbose = 1;
    238 			break;
    239 		default:
    240 			usage();
    241 			break;
    242 		}
    243 	}
    244 
    245 	if (argc - optind < 2)
    246 		usage();
    247 
    248 	boot = argv[optind];
    249 	dev = argv[optind + 1];
    250 	if (verbose) {
    251 		printf("boot: %s\n", boot);
    252 		printf("dev: %s\n", dev);
    253 	}
    254 
    255 	if ((bp = load_boot(boot, &bootsize)) == NULL)
    256 		return 1;
    257 
    258 	if ((devfd = open(dev, O_RDONLY, 0)) < 0) {
    259 		warn("open: %s", dev);
    260 		goto out;
    261 	}
    262 
    263 	if (load_prep_partition(devfd, &ppp)) {
    264 		warn("load_prep_partition");
    265 		goto out;
    266 	}
    267 
    268 	if (bootsize + dbtob(2) > dbtob(ppp.mbrp_size)) {
    269 		warn("boot image is too big.");
    270 		goto out;
    271 	}
    272 
    273 	close(devfd);
    274 
    275 	if (nowrite) {
    276 		free(bp);
    277 		return 0;
    278 	}
    279 
    280 	if ((devfd = open(dev, O_RDWR, 0)) < 0) {
    281 		warn("open: %s", dev);
    282 		goto out;
    283 	}
    284 
    285 	/*
    286 	 * Write boot image.
    287 	 */
    288 	memset(boot00, 0, sizeof(boot00));
    289 	(void)lseek(devfd, (off_t)dbtob(ppp.mbrp_start), SEEK_SET);
    290 	if (write(devfd, boot00, sizeof(boot00)) != sizeof(boot00)) {
    291 		warn("write boot00(prep mbr)");
    292 		goto out;
    293 	}
    294 
    295 	(void)lseek(devfd, (off_t)dbtob(ppp.mbrp_start+1), SEEK_SET);
    296 	boot00[0] = htole32(dbtob(2));
    297 	boot00[1] = htole32(bootsize);
    298 	if (write(devfd, boot00, sizeof(boot00)) != sizeof(boot00)) {
    299 		warn("write boot00(prep start/size)");
    300 		goto out;
    301 	}
    302 
    303 	if (devread(devfd, boot00, 1, DEV_BSIZE, "start/size") != 0)
    304 		goto out;
    305 	boot00[0] = htole32(dbtob(ppp.mbrp_start));
    306 	boot00[1] = htole32(bootsize + dbtob(2));
    307 	(void)lseek(devfd, (off_t)dbtob(1), SEEK_SET);
    308 	if (write(devfd, boot00, sizeof(boot00)) != sizeof(boot00)) {
    309 		warn("write boot00(master start/size)");
    310 		goto out;
    311 	}
    312 
    313 	(void)lseek(devfd, (off_t)dbtob(ppp.mbrp_start+2), SEEK_SET);
    314 	if (write(devfd, bp, bootsize) != bootsize) {
    315 		warn("write boot loader");
    316 		goto out;
    317 	}
    318 
    319 	close(devfd);
    320 	free(bp);
    321 	return 0;
    322 
    323 out:
    324 	if (devfd >= 0)
    325 		close(devfd);
    326 	if (bp != NULL)
    327 		free(bp);
    328 	return 1;
    329 }
    330