xref: /netbsd-src/sys/arch/mvme68k/stand/installboot/installboot.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: installboot.c,v 1.3 1997/12/17 21:30:23 scw Exp $ */
2 
3 /*
4  * Copyright (c) 1994 Paul Kranenburg
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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Paul Kranenburg.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/mount.h>
35 #include <sys/time.h>
36 #include <sys/stat.h>
37 #include <ufs/ufs/dinode.h>
38 #include <ufs/ufs/dir.h>
39 #include <ufs/ffs/fs.h>
40 #include <err.h>
41 #include <a.out.h>
42 #include <fcntl.h>
43 #include <nlist.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <sys/disklabel.h>
49 
50 int	verbose, nowrite, hflag;
51 char	*boot, *proto, *dev;
52 struct nlist nl[] = {
53 #define X_BLOCK_SIZE	0
54 	{"_block_size"},
55 #define X_BLOCK_COUNT	1
56 	{"_block_count"},
57 #define X_BLOCK_TABLE	2
58 	{"_block_table"},
59 	{NULL}
60 };
61 
62 int *block_size_p;		/* block size var. in prototype image */
63 int *block_count_p;		/* block count var. in prototype image */
64 daddr_t	*block_table;	/* block number array in prototype image */
65 int	maxblocknum;		/* size of this array */
66 
67 
68 char		*loadprotoblocks __P((char *, long *));
69 int		loadblocknums __P((char *, int));
70 static void	devread __P((int, void *, daddr_t, size_t, char *));
71 static void	usage __P((void));
72 int 		main __P((int, char *[]));
73 
74 
75 static void
76 usage()
77 {
78 	fprintf(stderr,
79 		"usage: installboot [-n] [-v] [-h] <boot> <proto> <device>\n");
80 	exit(1);
81 }
82 
83 int
84 main(argc, argv)
85 	int argc;
86 	char *argv[];
87 {
88 	int	c;
89 	int	devfd;
90 	char	*protostore;
91 	long	protosize;
92 
93 	while ((c = getopt(argc, argv, "vnh")) != -1) {
94 		switch (c) {
95 		case 'h':
96 			/* Don't strip a.out header */
97 			hflag = 1;
98 			break;
99 		case 'n':
100 			/* Do not actually write the bootblock to disk */
101 			nowrite = 1;
102 			break;
103 		case 'v':
104 			/* Chat */
105 			verbose = 1;
106 			break;
107 		default:
108 			usage();
109 		}
110 	}
111 
112 	if (argc - optind < 3) {
113 		usage();
114 	}
115 
116 	boot = argv[optind];
117 	proto = argv[optind + 1];
118 	dev = argv[optind + 2];
119 
120 	if (verbose) {
121 		printf("boot: %s\n", boot);
122 		printf("proto: %s\n", proto);
123 		printf("device: %s\n", dev);
124 	}
125 
126 	/* Load proto blocks into core */
127 	if ((protostore = loadprotoblocks(proto, &protosize)) == NULL)
128 		exit(1);
129 
130 	/* XXX - Paranoia: Make sure size is aligned! */
131 	if (protosize & (DEV_BSIZE - 1))
132 		err(1, "proto bootblock bad size=%d", protosize);
133 
134 	/* Open and check raw disk device */
135 	if ((devfd = open(dev, O_RDONLY, 0)) < 0)
136 		err(1, "open: %s", dev);
137 
138 	/* Extract and load block numbers */
139 	if (loadblocknums(boot, devfd) != 0)
140 		exit(1);
141 
142 	(void)close(devfd);
143 
144 	if (nowrite)
145 		return 0;
146 
147 	/* Write patched proto bootblocks into the superblock */
148 	if (protosize > SBSIZE - DEV_BSIZE)
149 		errx(1, "proto bootblocks too big");
150 
151 	/* The primary bootblock needs to be written to the raw partition */
152 	dev[strlen(dev) - 1] = 'a' + RAW_PART;
153 
154 	if ((devfd = open(dev, O_RDWR, 0)) < 0)
155 		err(1, "open: %s", dev);
156 
157 	if (lseek(devfd, DEV_BSIZE, SEEK_SET) != DEV_BSIZE)
158 		err(1, "lseek bootstrap");
159 
160 	/* Sync filesystems (to clean in-memory superblock?) */
161 	sync();
162 
163 	if (write(devfd, protostore, protosize) != protosize)
164 		err(1, "write bootstrap");
165 	(void)close(devfd);
166 	return 0;
167 }
168 
169 char *
170 loadprotoblocks(fname, size)
171 	char *fname;
172 	long *size;
173 {
174 	int	fd;
175 	size_t	tdsize;		/* text+data size */
176 	size_t	bbsize;		/* boot block size (block aligned) */
177 	char	*bp;
178 	struct	nlist *nlp;
179 	struct	exec eh;
180 	long	off;
181 
182 	fd = -1;
183 	bp = NULL;
184 
185 	/* Locate block number array in proto file */
186 	if (nlist(fname, nl) != 0) {
187 		warnx("nlist: %s: symbols not found", fname);
188 		return NULL;
189 	}
190 	/* Validate symbol types (global data). */
191 	for (nlp = nl; nlp->n_un.n_name; nlp++) {
192 		if (nlp->n_type != (N_DATA | N_EXT)) {
193 			warnx("nlist: %s: wrong type", nlp->n_un.n_name);
194 			return NULL;
195 		}
196 	}
197 
198 	if ((fd = open(fname, O_RDONLY)) < 0) {
199 		warn("open: %s", fname);
200 		return NULL;
201 	}
202 	if (read(fd, &eh, sizeof(eh)) != sizeof(eh)) {
203 		warn("read: %s", fname);
204 		goto bad;
205 	}
206 	if (N_GETMAGIC(eh) != OMAGIC) {
207 		warn("bad magic: 0x%x", eh.a_midmag);
208 		goto bad;
209 	}
210 	/*
211 	 * We have to include the exec header in the beginning of
212 	 * the buffer, and leave extra space at the end in case
213 	 * the actual write to disk wants to skip the header.
214 	 */
215 	tdsize = eh.a_text + eh.a_data;
216 	bbsize = tdsize + sizeof(eh);
217 	bbsize = roundup(bbsize, DEV_BSIZE);
218 
219 	/*
220 	 * Allocate extra space here because the caller may copy
221 	 * the boot block starting at the end of the exec header.
222 	 * This prevents reading beyond the end of the buffer.
223 	 */
224 	if ((bp = calloc(bbsize + sizeof(eh), 1)) == NULL) {
225 		warnx("malloc: %s: no memory", fname);
226 		goto bad;
227 	}
228 	/* Copy the exec header and read the rest of the file. */
229 	memcpy(bp, &eh, sizeof(eh));
230 	if (read(fd, bp+sizeof(eh), tdsize) != tdsize) {
231 		warn("read: %s", fname);
232 		goto bad;
233 	}
234 
235 	*size = bbsize;	/* aligned to DEV_BSIZE */
236 
237 	/* Calculate the symbols' locations within the proto file */
238 	off = N_DATOFF(eh) - N_DATADDR(eh) - (eh.a_entry - N_TXTADDR(eh));
239 	block_size_p  =   (int *) (bp + nl[X_BLOCK_SIZE ].n_value + off);
240 	block_count_p =   (int *) (bp + nl[X_BLOCK_COUNT].n_value + off);
241 	block_table = (daddr_t *) (bp + nl[X_BLOCK_TABLE].n_value + off);
242 	maxblocknum = *block_count_p;
243 
244 	if (verbose) {
245 		printf("%s: entry point %#x\n", fname, eh.a_entry);
246 		printf("proto bootblock size %ld\n", *size);
247 		printf("room for %d filesystem blocks at %#x\n",
248 			maxblocknum, nl[X_BLOCK_TABLE].n_value);
249 	}
250 
251 	close(fd);
252 	if (!hflag)
253 		bp += sizeof(struct exec);
254 	return bp;
255 
256  bad:
257 	if (bp)
258 		free(bp);
259 	if (fd >= 0)
260 		close(fd);
261 	return NULL;
262 }
263 
264 static void
265 devread(fd, buf, blk, size, msg)
266 	int	fd;
267 	void	*buf;
268 	daddr_t	blk;
269 	size_t	size;
270 	char	*msg;
271 {
272 	if (lseek(fd, dbtob(blk), SEEK_SET) != dbtob(blk))
273 		err(1, "%s: devread: lseek", msg);
274 
275 	if (read(fd, buf, size) != size)
276 		err(1, "%s: devread: read", msg);
277 }
278 
279 static char sblock[SBSIZE];
280 
281 int
282 loadblocknums(boot, devfd)
283 char	*boot;
284 int	devfd;
285 {
286 	int		i, fd;
287 	struct	stat	statbuf;
288 	struct	statfs	statfsbuf;
289 	struct fs	*fs;
290 	char		*buf;
291 	daddr_t		blk, *ap;
292 	struct dinode	*ip;
293 	int		ndb;
294 
295 	/*
296 	 * Open 2nd-level boot program and record the block numbers
297 	 * it occupies on the filesystem represented by `devfd'.
298 	 */
299 
300 	/* Make sure the (probably new) boot file is on disk. */
301 	sync(); sleep(1);
302 
303 	if ((fd = open(boot, O_RDONLY)) < 0)
304 		err(1, "open: %s", boot);
305 
306 	if (fstatfs(fd, &statfsbuf) != 0)
307 		err(1, "statfs: %s", boot);
308 
309 	if (strncmp(statfsbuf.f_fstypename, "ffs", MFSNAMELEN) &&
310 	    strncmp(statfsbuf.f_fstypename, "ufs", MFSNAMELEN) ) {
311 		errx(1, "%s: must be on an FFS filesystem", boot);
312 	}
313 
314 	if (fsync(fd) != 0)
315 		err(1, "fsync: %s", boot);
316 
317 	if (fstat(fd, &statbuf) != 0)
318 		err(1, "fstat: %s", boot);
319 
320 	close(fd);
321 
322 	/* Read superblock */
323 	devread(devfd, sblock, SBLOCK, SBSIZE, "superblock");
324 	fs = (struct fs *)sblock;
325 
326 	/* Sanity-check super-block. */
327 	if (fs->fs_magic != FS_MAGIC)
328 		errx(1, "Bad magic number in superblock");
329 	if (fs->fs_inopb <= 0)
330 		err(1, "Bad inopb=%d in superblock", fs->fs_inopb);
331 
332 	/* Read inode */
333 	if ((buf = malloc(fs->fs_bsize)) == NULL)
334 		errx(1, "No memory for filesystem block");
335 
336 	blk = fsbtodb(fs, ino_to_fsba(fs, statbuf.st_ino));
337 	devread(devfd, buf, blk, fs->fs_bsize, "inode");
338 	ip = (struct dinode *)(buf) + ino_to_fsbo(fs, statbuf.st_ino);
339 
340 	/*
341 	 * Have the inode.  Figure out how many blocks we need.
342 	 */
343 	ndb = howmany(ip->di_size, fs->fs_bsize);
344 	if (ndb > maxblocknum)
345 		errx(1, "Too many blocks");
346 	*block_count_p = ndb;
347 	*block_size_p = fs->fs_bsize;
348 	if (verbose)
349 		printf("Will load %d blocks of size %d each.\n",
350 			   ndb, fs->fs_bsize);
351 
352 	/*
353 	 * Get the block numbers; we don't handle fragments
354 	 */
355 	ap = ip->di_db;
356 	for (i = 0; i < NDADDR && *ap && ndb; i++, ap++, ndb--) {
357 		blk = fsbtodb(fs, *ap);
358 		if (verbose)
359 			printf("%d: %d\n", i, blk);
360 		block_table[i] = blk;
361 	}
362 	if (ndb == 0)
363 		return 0;
364 
365 	/*
366 	 * Just one level of indirections; there isn't much room
367 	 * for more in the 1st-level bootblocks anyway.
368 	 */
369 	blk = fsbtodb(fs, ip->di_ib[0]);
370 	devread(devfd, buf, blk, fs->fs_bsize, "indirect block");
371 	ap = (daddr_t *)buf;
372 	for (; i < NINDIR(fs) && *ap && ndb; i++, ap++, ndb--) {
373 		blk = fsbtodb(fs, *ap);
374 		if (verbose)
375 			printf("%d: %d\n", i, blk);
376 		block_table[i] = blk;
377 	}
378 
379 	return 0;
380 }
381 
382