1 /*	$NetBSD: installboot.c,v 1.3 2002/03/17 23:45:57 nonaka Exp $	*/
2 
3 /*
4  * Copyright (c) 2000 NONAKA Kimihiro (nonaka@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 int nowrite, verbose;
41 char *boot, *dev;
42 
43 void usage(void);
44 int devread(int, void *, daddr_t, size_t, char *);
45 char *load_boot(char *, size_t *);
46 int load_prep_partition(int, struct mbr_partition *);
47 int main(int, char **);
48 
49 void
50 usage()
51 {
52 
53 	fprintf(stderr, "usage: %s [-n] [-v] <boot> <device>\n",
54 	    getprogname());
55 	exit(1);
56 }
57 
58 int
59 devread(int fd, void *buf, daddr_t blk, size_t size, char *msg)
60 {
61 
62 	if (lseek(fd, (off_t)dbtob(blk), SEEK_SET) != dbtob(blk)) {
63 		warn("%s: devread: lseek", msg);
64 		return 1;
65 	}
66 	if (read(fd, buf, size) != size) {
67 		warn("%s: devread: read", msg);
68 		return 1;
69 	}
70 	return 0;
71 }
72 
73 char *
74 load_boot(char *boot, size_t *bootsize)
75 {
76 	Elf32_Ehdr eh;
77 	Elf32_Phdr ph;
78 	struct stat st;
79 	int fd;
80 	int i;
81 	size_t imgsz = 0;
82 	char *bp = NULL;
83 
84 	if ((fd = open(boot, O_RDONLY)) < 0) {
85 		warn("open: %s", boot);
86 		return NULL;
87 	}
88 
89 	if (fstat(fd, &st) != 0) {
90 		warn("fstat: %s", boot);
91 		goto out;
92 	}
93 
94 	/*
95 	 * First, check ELF.
96 	 */
97 	if (read(fd, &eh, sizeof(eh)) != sizeof(eh)) {
98 		warn("read: eh: %s", boot);
99 		goto out;
100 	}
101 	if (memcmp(eh.e_ident, ELFMAG, SELFMAG) != 0 ||
102 	    eh.e_ident[EI_CLASS] != ELFCLASS32) {
103 		lseek(fd, 0L, SEEK_SET);
104 		goto notelf;
105 	}
106 	if (be16toh(eh.e_machine) != EM_PPC) {
107 		warn("not PowerPC binary.");
108 		goto out;
109 	}
110 
111 	for (i = 0; i < be16toh(eh.e_phnum); i++) {
112 		(void)lseek(fd, be32toh(eh.e_phoff) + sizeof(ph) * i, SEEK_SET);
113 		if (read(fd, &ph, sizeof(ph)) != sizeof(ph)) {
114 			warn("read: ph: %s", boot);
115 			goto out;
116 		}
117 
118 		if ((be32toh(ph.p_type) != PT_LOAD) ||
119 		    !(be32toh(ph.p_flags) & PF_X))
120 			continue;
121 
122 		imgsz = st.st_size - be32toh(ph.p_offset);
123 		lseek(fd, be32toh(ph.p_offset), SEEK_SET);
124 		break;
125 	}
126 
127 notelf:
128 	/*
129 	 * Second, check PReP bootable image.
130 	 */
131 	if (imgsz == 0) {
132 		char buf[DEV_BSIZE];
133 
134 		printf("Bootable image: ");
135 		if (load_prep_partition(fd, 0)) {
136 			warn("no PReP bootable image.");
137 			goto out;
138 		}
139 
140 		if (lseek(fd, (off_t)dbtob(1), SEEK_SET) != dbtob(1)) {
141 			warn("bootable image lseek sector 1");
142 			goto out;
143 		}
144 		if (read(fd, buf, DEV_BSIZE) != DEV_BSIZE) {
145 			warn("read: start/size");
146 			goto out;
147 		}
148 
149 		imgsz = le32toh(*(u_int32_t *)(buf + sizeof(u_int32_t)))
150 		    - dbtob(2);
151 		lseek(fd, le32toh(*(u_int32_t *)buf), SEEK_SET);
152 	}
153 
154 	if ((bp = (char *)calloc(roundup(imgsz, DEV_BSIZE), 1)) == NULL) {
155 		warn("calloc: no memory for boot image.");
156 		goto out;
157 	}
158 
159 	if (read(fd, bp, imgsz) != imgsz) {
160 		warn("read: boot image: %s", boot);
161 		goto out;
162 	}
163 
164 	if (verbose) {
165 		printf("image size = %d\n", imgsz);
166 	}
167 
168 	*bootsize = roundup(imgsz, DEV_BSIZE);
169 
170 	close(fd);
171 	return bp;
172 
173 out:
174 	if (bp != NULL)
175 		free(bp);
176 	if (fd >= 0)
177 		close(fd);
178 	return NULL;
179 }
180 
181 int
182 load_prep_partition(int devfd, struct mbr_partition *ppp)
183 {
184 	char mbr[512];
185 	struct mbr_partition *mbrp;
186 	int i;
187 
188 	if (devread(devfd, mbr, MBR_BBSECTOR, DEV_BSIZE, "MBR") != 0)
189 		return 1;
190 	if (*(u_int16_t *)&mbr[MBR_MAGICOFF] != htole16(MBR_MAGIC)) {
191 		warn("no MBR_MAGIC");
192 		return 1;
193 	}
194 
195 	mbrp = (struct mbr_partition *)&mbr[MBR_PARTOFF];
196 	for (i = 0; i < NMBRPART; i++) {
197 		if (mbrp[i].mbrp_typ == MBR_PTYPE_PREP)
198 			break;
199 	}
200 	if (i == NMBRPART) {
201 		warn("no PReP partition.");
202 		return 1;
203 	}
204 
205 	if (verbose) {
206 		printf("PReP partition: start = %d, size = %d\n",
207 		    le32toh(mbrp[i].mbrp_start), le32toh(mbrp[i].mbrp_size));
208 	}
209 
210 	if (ppp) {
211 		*ppp = mbrp[i];
212 		ppp->mbrp_start = le32toh(ppp->mbrp_start);
213 		ppp->mbrp_size = le32toh(ppp->mbrp_size);
214 	}
215 
216 	return 0;
217 }
218 
219 int
220 main(int argc, char **argv)
221 {
222 	struct mbr_partition ppp;
223 	size_t bootsize;
224 	int c;
225 	int boot00[512/sizeof(int)];
226 	int devfd = -1;
227 	char *bp;
228 
229 	while ((c = getopt(argc, argv, "vn")) != EOF) {
230 		switch (c) {
231 		case 'n':
232 			nowrite = 1;
233 			break;
234 		case 'v':
235 			verbose = 1;
236 			break;
237 		default:
238 			usage();
239 			break;
240 		}
241 	}
242 
243 	if (argc - optind < 2)
244 		usage();
245 
246 	boot = argv[optind];
247 	dev = argv[optind + 1];
248 	if (verbose) {
249 		printf("boot: %s\n", boot);
250 		printf("dev: %s\n", dev);
251 	}
252 
253 	if ((bp = load_boot(boot, &bootsize)) == NULL)
254 		return 1;
255 
256 	if ((devfd = open(dev, O_RDONLY, 0)) < 0) {
257 		warn("open: %s", dev);
258 		goto out;
259 	}
260 
261 	if (load_prep_partition(devfd, &ppp)) {
262 		warn("load_prep_partition");
263 		goto out;
264 	}
265 
266 	if (bootsize + dbtob(2) > dbtob(ppp.mbrp_size)) {
267 		warn("boot image is too big.");
268 		goto out;
269 	}
270 
271 	close(devfd);
272 
273 	if (nowrite) {
274 		free(bp);
275 		return 0;
276 	}
277 
278 	if ((devfd = open(dev, O_RDWR, 0)) < 0) {
279 		warn("open: %s", dev);
280 		goto out;
281 	}
282 
283 	/*
284 	 * Write boot image.
285 	 */
286 	memset(boot00, 0, sizeof(boot00));
287 	(void)lseek(devfd, (off_t)dbtob(ppp.mbrp_start), SEEK_SET);
288 	if (write(devfd, boot00, sizeof(boot00)) != sizeof(boot00)) {
289 		warn("write boot00(prep mbr)");
290 		goto out;
291 	}
292 
293 	(void)lseek(devfd, (off_t)dbtob(ppp.mbrp_start+1), SEEK_SET);
294 	boot00[0] = htole32(dbtob(2));
295 	boot00[1] = htole32(bootsize);
296 	if (write(devfd, boot00, sizeof(boot00)) != sizeof(boot00)) {
297 		warn("write boot00(prep start/size)");
298 		goto out;
299 	}
300 
301 	if (devread(devfd, boot00, 1, DEV_BSIZE, "start/size") != 0)
302 		goto out;
303 	boot00[0] = htole32(dbtob(ppp.mbrp_start));
304 	boot00[1] = htole32(bootsize + dbtob(2));
305 	(void)lseek(devfd, (off_t)dbtob(1), SEEK_SET);
306 	if (write(devfd, boot00, sizeof(boot00)) != sizeof(boot00)) {
307 		warn("write boot00(master start/size)");
308 		goto out;
309 	}
310 
311 	(void)lseek(devfd, (off_t)dbtob(ppp.mbrp_start+2), SEEK_SET);
312 	if (write(devfd, bp, bootsize) != bootsize) {
313 		warn("write boot loader");
314 		goto out;
315 	}
316 
317 	close(devfd);
318 	free(bp);
319 	return 0;
320 
321 out:
322 	if (devfd >= 0)
323 		close(devfd);
324 	if (bp != NULL)
325 		free(bp);
326 	return 1;
327 }
328