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