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