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