xref: /netbsd-src/sys/arch/mvme68k/stand/installboot/installboot.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: installboot.c,v 1.10 2004/05/21 08:35:00 he 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/cdefs.h>
41 #include <sys/mount.h>
42 #include <sys/time.h>
43 #include <sys/stat.h>
44 #include <sys/statvfs.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ufs/dir.h>
47 #include <ufs/ffs/fs.h>
48 #include <err.h>
49 #include <fcntl.h>
50 #include <nlist.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <sys/disklabel.h>
56 
57 #include "loadfile.h"
58 
59 int	verbose, nowrite, hflag;
60 char	*boot, *proto, *dev;
61 
62 struct nlist nl[] = {
63 #define X_BLOCK_SIZE	0
64 #define X_BLOCK_COUNT	1
65 #define X_BLOCK_TABLE	2
66 #ifdef __ELF__
67 	{ "block_size" },
68 	{ "block_count" },
69 	{ "block_table" },
70 #else
71 	{ "_block_size" },
72 	{ "_block_count" },
73 	{ "_block_table" },
74 #endif
75 	{ NULL }
76 };
77 
78 int *block_size_p;		/* block size var. in prototype image */
79 int *block_count_p;		/* block count var. in prototype image */
80 /* XXX ondisk32 */
81 int32_t	*block_table;		/* block number array in prototype image */
82 int	maxblocknum;		/* size of this array */
83 
84 
85 char		*loadprotoblocks __P((char *, size_t *));
86 int		loadblocknums __P((char *, int));
87 static void	devread __P((int, void *, daddr_t, size_t, char *));
88 static void	usage __P((void));
89 int 		main __P((int, char *[]));
90 
91 
92 static void
93 usage()
94 {
95 	fprintf(stderr,
96 		"usage: installboot [-n] [-v] [-h] <boot> <proto> <device>\n");
97 	exit(1);
98 }
99 
100 int
101 main(argc, argv)
102 	int argc;
103 	char *argv[];
104 {
105 	int	c;
106 	int	devfd;
107 	char	*protostore;
108 	size_t	protosize;
109 
110 	while ((c = getopt(argc, argv, "vnh")) != -1) {
111 		switch (c) {
112 		case 'h':
113 			/* Don't strip a.out header */
114 			hflag = 1;
115 			break;
116 		case 'n':
117 			/* Do not actually write the bootblock to disk */
118 			nowrite = 1;
119 			break;
120 		case 'v':
121 			/* Chat */
122 			verbose = 1;
123 			break;
124 		default:
125 			usage();
126 		}
127 	}
128 
129 	if (argc - optind < 3) {
130 		usage();
131 	}
132 
133 	boot = argv[optind];
134 	proto = argv[optind + 1];
135 	dev = argv[optind + 2];
136 
137 	if (verbose) {
138 		printf("boot: %s\n", boot);
139 		printf("proto: %s\n", proto);
140 		printf("device: %s\n", dev);
141 	}
142 
143 	/* Load proto blocks into core */
144 	if ((protostore = loadprotoblocks(proto, &protosize)) == NULL)
145 		exit(1);
146 
147 	/* XXX - Paranoia: Make sure size is aligned! */
148 	if (protosize & (DEV_BSIZE - 1))
149 		err(1, "proto bootblock bad size=%d", protosize);
150 
151 	/* Open and check raw disk device */
152 	if ((devfd = open(dev, O_RDONLY, 0)) < 0)
153 		err(1, "open: %s", dev);
154 
155 	/* Extract and load block numbers */
156 	if (loadblocknums(boot, devfd) != 0)
157 		exit(1);
158 
159 	(void)close(devfd);
160 
161 	if (nowrite)
162 		return 0;
163 
164 	/* Write patched proto bootblocks into the superblock */
165 	if (protosize > SBLOCKSIZE - DEV_BSIZE)
166 		errx(1, "proto bootblocks too big");
167 
168 	/* The primary bootblock needs to be written to the raw partition */
169 	dev[strlen(dev) - 1] = 'a' + RAW_PART;
170 
171 	if ((devfd = open(dev, O_RDWR, 0)) < 0)
172 		err(1, "open: %s", dev);
173 
174 	if (lseek(devfd, DEV_BSIZE, SEEK_SET) != DEV_BSIZE)
175 		err(1, "lseek bootstrap");
176 
177 	/* Sync filesystems (to clean in-memory superblock?) */
178 	sync();
179 
180 	if (write(devfd, protostore, protosize) != protosize)
181 		err(1, "write bootstrap");
182 	(void)close(devfd);
183 	return 0;
184 }
185 
186 char *
187 loadprotoblocks(fname, size)
188 	char *fname;
189 	size_t *size;
190 {
191 	int	fd;
192 	u_long	marks[MARK_MAX], bp, offs;
193 
194 	fd = -1;
195 
196 	/* Locate block number array in proto file */
197 	if (nlist(fname, nl) != 0) {
198 		warnx("nlist: %s: symbols not found", fname);
199 		return NULL;
200 	}
201 
202 	marks[MARK_START] = 0;
203 	if ((fd = loadfile(fname, marks, COUNT_TEXT|COUNT_DATA)) == -1)
204 		return NULL;
205 	(void)close(fd);
206 
207 	*size = roundup(marks[MARK_END] - marks[MARK_START], DEV_BSIZE);
208 	bp = (u_long)malloc(*size);
209 
210 	offs = marks[MARK_START];
211 	marks[MARK_START] = bp - offs;
212 
213 	if ((fd = loadfile(fname, marks, LOAD_TEXT|LOAD_DATA)) == -1)
214 		return NULL;
215 	(void)close(fd);
216 
217 	/* Calculate the symbols' locations within the proto file */
218 	block_size_p  =   (int *) (bp + (nl[X_BLOCK_SIZE ].n_value - offs));
219 	block_count_p =   (int *) (bp + (nl[X_BLOCK_COUNT].n_value - offs));
220 	/* XXX ondisk32 */
221 	block_table = (int32_t *) (bp + (nl[X_BLOCK_TABLE].n_value - offs));
222 	maxblocknum = *block_count_p;
223 
224 	if (verbose) {
225 		printf("%s: entry point %#lx\n", fname, marks[MARK_ENTRY]);
226 		printf("proto bootblock size %d\n", *size);
227 		printf("room for %d filesystem blocks at %#lx\n",
228 			maxblocknum, nl[X_BLOCK_TABLE].n_value);
229 	}
230 
231 	return (char *) bp;
232 
233 	if (bp)
234 		free((void *)bp);
235 	return NULL;
236 }
237 
238 static void
239 devread(fd, buf, blk, size, msg)
240 	int	fd;
241 	void	*buf;
242 	daddr_t	blk;
243 	size_t	size;
244 	char	*msg;
245 {
246 	if (lseek(fd, dbtob(blk), SEEK_SET) != dbtob(blk))
247 		err(1, "%s: devread: lseek", msg);
248 
249 	if (read(fd, buf, size) != size)
250 		err(1, "%s: devread: read", msg);
251 }
252 
253 static char sblock[SBLOCKSIZE];
254 
255 int
256 loadblocknums(boot, devfd)
257 char	*boot;
258 int	devfd;
259 {
260 	int		i, fd;
261 	struct	stat	statbuf;
262 	struct	statvfs	statvfsbuf;
263 	struct fs	*fs;
264 	char		*buf;
265 	daddr_t		blk, *ap;
266 	struct ufs1_dinode *ip;
267 	int		ndb;
268 
269 	/*
270 	 * Open 2nd-level boot program and record the block numbers
271 	 * it occupies on the filesystem represented by `devfd'.
272 	 */
273 
274 	/* Make sure the (probably new) boot file is on disk. */
275 	sync(); sleep(1);
276 
277 	if ((fd = open(boot, O_RDONLY)) < 0)
278 		err(1, "open: %s", boot);
279 
280 	if (fstatvfs(fd, &statvfsbuf) != 0)
281 		err(1, "statfs: %s", boot);
282 
283 	if (strncmp(statvfsbuf.f_fstypename, "ffs", MFSNAMELEN) &&
284 	    strncmp(statvfsbuf.f_fstypename, "ufs", MFSNAMELEN) ) {
285 		errx(1, "%s: must be on an FFS filesystem", boot);
286 	}
287 
288 	if (fsync(fd) != 0)
289 		err(1, "fsync: %s", boot);
290 
291 	if (fstat(fd, &statbuf) != 0)
292 		err(1, "fstat: %s", boot);
293 
294 	close(fd);
295 
296 	/* Read superblock */
297 	devread(devfd, sblock, (daddr_t)(BBSIZE / DEV_BSIZE),
298 	    SBLOCKSIZE, "superblock");
299 	fs = (struct fs *)sblock;
300 
301 	/* Sanity-check super-block. */
302 	if (fs->fs_magic != FS_UFS1_MAGIC)
303 		errx(1, "Bad magic number in superblock, must be UFS1");
304 	if (fs->fs_inopb <= 0)
305 		err(1, "Bad inopb=%d in superblock", fs->fs_inopb);
306 
307 	/* Read inode */
308 	if ((buf = malloc(fs->fs_bsize)) == NULL)
309 		errx(1, "No memory for filesystem block");
310 
311 	blk = fsbtodb(fs, ino_to_fsba(fs, statbuf.st_ino));
312 	devread(devfd, buf, blk, fs->fs_bsize, "inode");
313 	ip = (struct ufs1_dinode *)(buf) + ino_to_fsbo(fs, statbuf.st_ino);
314 
315 	/*
316 	 * Have the inode.  Figure out how many blocks we need.
317 	 */
318 	ndb = howmany(ip->di_size, fs->fs_bsize);
319 	if (ndb > maxblocknum)
320 		errx(1, "Too many blocks");
321 	*block_count_p = ndb;
322 	*block_size_p = fs->fs_bsize;
323 	if (verbose)
324 		printf("Will load %d blocks of size %d each.\n",
325 			   ndb, fs->fs_bsize);
326 
327 	/*
328 	 * Get the block numbers; we don't handle fragments
329 	 */
330 	ap = ip->di_db;
331 	for (i = 0; i < NDADDR && *ap && ndb; i++, ap++, ndb--) {
332 		blk = fsbtodb(fs, *ap);
333 		if (verbose)
334 			printf("%d: %d\n", i, blk);
335 		block_table[i] = blk;
336 	}
337 	if (ndb == 0)
338 		return 0;
339 
340 	/*
341 	 * Just one level of indirections; there isn't much room
342 	 * for more in the 1st-level bootblocks anyway.
343 	 */
344 	blk = fsbtodb(fs, ip->di_ib[0]);
345 	devread(devfd, buf, blk, fs->fs_bsize, "indirect block");
346 	/* XXX ondisk32 */
347 	ap = (int32_t *)buf;
348 	for (; i < NINDIR(fs) && *ap && ndb; i++, ap++, ndb--) {
349 		blk = fsbtodb(fs, *ap);
350 		if (verbose)
351 			printf("%d: %d\n", i, blk);
352 		block_table[i] = blk;
353 	}
354 
355 	return 0;
356 }
357 
358