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