installboot.c revision 1.17 1 1.17 dholland /* $NetBSD: installboot.c,v 1.17 2013/06/19 17:51:26 dholland 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 *
19 1.4 pk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.4 pk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.4 pk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.4 pk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.4 pk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.4 pk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.4 pk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.4 pk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.4 pk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.4 pk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.4 pk * POSSIBILITY OF SUCH DAMAGE.
30 1.1 chuck */
31 1.1 chuck
32 1.1 chuck #include <sys/param.h>
33 1.5 jdolecek #include <sys/cdefs.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.10 he #include <sys/statvfs.h>
38 1.1 chuck #include <ufs/ufs/dinode.h>
39 1.1 chuck #include <ufs/ufs/dir.h>
40 1.1 chuck #include <ufs/ffs/fs.h>
41 1.1 chuck #include <err.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.6 scw #include "loadfile.h"
51 1.6 scw
52 1.1 chuck int verbose, nowrite, hflag;
53 1.1 chuck char *boot, *proto, *dev;
54 1.6 scw
55 1.1 chuck struct nlist nl[] = {
56 1.1 chuck #define X_BLOCK_SIZE 0
57 1.1 chuck #define X_BLOCK_COUNT 1
58 1.1 chuck #define X_BLOCK_TABLE 2
59 1.7 scw #ifdef __ELF__
60 1.6 scw { "block_size" },
61 1.6 scw { "block_count" },
62 1.6 scw { "block_table" },
63 1.7 scw #else
64 1.7 scw { "_block_size" },
65 1.7 scw { "_block_count" },
66 1.7 scw { "_block_table" },
67 1.7 scw #endif
68 1.6 scw { NULL }
69 1.1 chuck };
70 1.1 chuck
71 1.1 chuck int *block_size_p; /* block size var. in prototype image */
72 1.1 chuck int *block_count_p; /* block count var. in prototype image */
73 1.8 fvdl /* XXX ondisk32 */
74 1.8 fvdl int32_t *block_table; /* block number array in prototype image */
75 1.1 chuck int maxblocknum; /* size of this array */
76 1.1 chuck
77 1.1 chuck
78 1.14 tsutsui char *loadprotoblocks(char *, size_t *);
79 1.14 tsutsui int loadblocknums(char *, int);
80 1.14 tsutsui static void devread(int, void *, daddr_t, size_t, char *);
81 1.14 tsutsui static void usage(void);
82 1.14 tsutsui int main(int, char *[]);
83 1.1 chuck
84 1.1 chuck
85 1.1 chuck static void
86 1.14 tsutsui usage(void)
87 1.1 chuck {
88 1.14 tsutsui
89 1.1 chuck fprintf(stderr,
90 1.14 tsutsui "usage: installboot [-n] [-v] [-h] <boot> <proto> <device>\n");
91 1.1 chuck exit(1);
92 1.1 chuck }
93 1.1 chuck
94 1.1 chuck int
95 1.14 tsutsui main(int argc, char *argv[])
96 1.1 chuck {
97 1.1 chuck int c;
98 1.1 chuck int devfd;
99 1.1 chuck char *protostore;
100 1.6 scw size_t protosize;
101 1.1 chuck
102 1.2 lukem while ((c = getopt(argc, argv, "vnh")) != -1) {
103 1.1 chuck switch (c) {
104 1.1 chuck case 'h':
105 1.1 chuck /* Don't strip a.out header */
106 1.1 chuck hflag = 1;
107 1.1 chuck break;
108 1.1 chuck case 'n':
109 1.1 chuck /* Do not actually write the bootblock to disk */
110 1.1 chuck nowrite = 1;
111 1.1 chuck break;
112 1.1 chuck case 'v':
113 1.1 chuck /* Chat */
114 1.1 chuck verbose = 1;
115 1.1 chuck break;
116 1.1 chuck default:
117 1.1 chuck usage();
118 1.1 chuck }
119 1.1 chuck }
120 1.1 chuck
121 1.1 chuck if (argc - optind < 3) {
122 1.1 chuck usage();
123 1.1 chuck }
124 1.1 chuck
125 1.1 chuck boot = argv[optind];
126 1.1 chuck proto = argv[optind + 1];
127 1.1 chuck dev = argv[optind + 2];
128 1.1 chuck
129 1.1 chuck if (verbose) {
130 1.1 chuck printf("boot: %s\n", boot);
131 1.1 chuck printf("proto: %s\n", proto);
132 1.1 chuck printf("device: %s\n", dev);
133 1.1 chuck }
134 1.1 chuck
135 1.1 chuck /* Load proto blocks into core */
136 1.1 chuck if ((protostore = loadprotoblocks(proto, &protosize)) == NULL)
137 1.1 chuck exit(1);
138 1.1 chuck
139 1.1 chuck /* XXX - Paranoia: Make sure size is aligned! */
140 1.1 chuck if (protosize & (DEV_BSIZE - 1))
141 1.6 scw err(1, "proto bootblock bad size=%d", protosize);
142 1.1 chuck
143 1.1 chuck /* Open and check raw disk device */
144 1.1 chuck if ((devfd = open(dev, O_RDONLY, 0)) < 0)
145 1.1 chuck err(1, "open: %s", dev);
146 1.1 chuck
147 1.1 chuck /* Extract and load block numbers */
148 1.1 chuck if (loadblocknums(boot, devfd) != 0)
149 1.1 chuck exit(1);
150 1.1 chuck
151 1.1 chuck (void)close(devfd);
152 1.1 chuck
153 1.1 chuck if (nowrite)
154 1.1 chuck return 0;
155 1.1 chuck
156 1.1 chuck /* Write patched proto bootblocks into the superblock */
157 1.9 he if (protosize > SBLOCKSIZE - DEV_BSIZE)
158 1.1 chuck errx(1, "proto bootblocks too big");
159 1.3 scw
160 1.3 scw /* The primary bootblock needs to be written to the raw partition */
161 1.3 scw dev[strlen(dev) - 1] = 'a' + RAW_PART;
162 1.1 chuck
163 1.1 chuck if ((devfd = open(dev, O_RDWR, 0)) < 0)
164 1.1 chuck err(1, "open: %s", dev);
165 1.1 chuck
166 1.1 chuck if (lseek(devfd, DEV_BSIZE, SEEK_SET) != DEV_BSIZE)
167 1.1 chuck err(1, "lseek bootstrap");
168 1.1 chuck
169 1.1 chuck /* Sync filesystems (to clean in-memory superblock?) */
170 1.1 chuck sync();
171 1.1 chuck
172 1.1 chuck if (write(devfd, protostore, protosize) != protosize)
173 1.1 chuck err(1, "write bootstrap");
174 1.1 chuck (void)close(devfd);
175 1.1 chuck return 0;
176 1.1 chuck }
177 1.1 chuck
178 1.1 chuck char *
179 1.14 tsutsui loadprotoblocks(char *fname, size_t *size)
180 1.1 chuck {
181 1.1 chuck int fd;
182 1.6 scw u_long marks[MARK_MAX], bp, offs;
183 1.1 chuck
184 1.1 chuck fd = -1;
185 1.1 chuck
186 1.1 chuck /* Locate block number array in proto file */
187 1.1 chuck if (nlist(fname, nl) != 0) {
188 1.1 chuck warnx("nlist: %s: symbols not found", fname);
189 1.1 chuck return NULL;
190 1.1 chuck }
191 1.1 chuck
192 1.6 scw marks[MARK_START] = 0;
193 1.6 scw if ((fd = loadfile(fname, marks, COUNT_TEXT|COUNT_DATA)) == -1)
194 1.1 chuck return NULL;
195 1.6 scw (void)close(fd);
196 1.6 scw
197 1.6 scw *size = roundup(marks[MARK_END] - marks[MARK_START], DEV_BSIZE);
198 1.6 scw bp = (u_long)malloc(*size);
199 1.6 scw
200 1.6 scw offs = marks[MARK_START];
201 1.6 scw marks[MARK_START] = bp - offs;
202 1.1 chuck
203 1.6 scw if ((fd = loadfile(fname, marks, LOAD_TEXT|LOAD_DATA)) == -1)
204 1.6 scw return NULL;
205 1.6 scw (void)close(fd);
206 1.1 chuck
207 1.1 chuck /* Calculate the symbols' locations within the proto file */
208 1.14 tsutsui block_size_p = (int *)(bp + (nl[X_BLOCK_SIZE ].n_value - offs));
209 1.14 tsutsui block_count_p = (int *)(bp + (nl[X_BLOCK_COUNT].n_value - offs));
210 1.8 fvdl /* XXX ondisk32 */
211 1.14 tsutsui block_table = (int32_t *)(bp + (nl[X_BLOCK_TABLE].n_value - offs));
212 1.1 chuck maxblocknum = *block_count_p;
213 1.1 chuck
214 1.1 chuck if (verbose) {
215 1.6 scw printf("%s: entry point %#lx\n", fname, marks[MARK_ENTRY]);
216 1.6 scw printf("proto bootblock size %d\n", *size);
217 1.5 jdolecek printf("room for %d filesystem blocks at %#lx\n",
218 1.1 chuck maxblocknum, nl[X_BLOCK_TABLE].n_value);
219 1.1 chuck }
220 1.1 chuck
221 1.14 tsutsui return (char *)bp;
222 1.1 chuck
223 1.1 chuck if (bp)
224 1.6 scw free((void *)bp);
225 1.1 chuck return NULL;
226 1.1 chuck }
227 1.1 chuck
228 1.1 chuck static void
229 1.14 tsutsui devread(int fd, void *buf, daddr_t blk, size_t size, char *msg)
230 1.1 chuck {
231 1.14 tsutsui
232 1.1 chuck if (lseek(fd, dbtob(blk), SEEK_SET) != dbtob(blk))
233 1.1 chuck err(1, "%s: devread: lseek", msg);
234 1.1 chuck
235 1.1 chuck if (read(fd, buf, size) != size)
236 1.1 chuck err(1, "%s: devread: read", msg);
237 1.1 chuck }
238 1.1 chuck
239 1.9 he static char sblock[SBLOCKSIZE];
240 1.1 chuck
241 1.1 chuck int
242 1.14 tsutsui loadblocknums(char *boot, int devfd)
243 1.1 chuck {
244 1.1 chuck int i, fd;
245 1.1 chuck struct stat statbuf;
246 1.10 he struct statvfs statvfsbuf;
247 1.1 chuck struct fs *fs;
248 1.1 chuck char *buf;
249 1.1 chuck daddr_t blk, *ap;
250 1.9 he struct ufs1_dinode *ip;
251 1.1 chuck int ndb;
252 1.1 chuck
253 1.1 chuck /*
254 1.1 chuck * Open 2nd-level boot program and record the block numbers
255 1.1 chuck * it occupies on the filesystem represented by `devfd'.
256 1.1 chuck */
257 1.1 chuck
258 1.1 chuck /* Make sure the (probably new) boot file is on disk. */
259 1.1 chuck sync(); sleep(1);
260 1.1 chuck
261 1.1 chuck if ((fd = open(boot, O_RDONLY)) < 0)
262 1.1 chuck err(1, "open: %s", boot);
263 1.1 chuck
264 1.10 he if (fstatvfs(fd, &statvfsbuf) != 0)
265 1.1 chuck err(1, "statfs: %s", boot);
266 1.1 chuck
267 1.14 tsutsui if (strncmp(statvfsbuf.f_fstypename, "ffs",
268 1.14 tsutsui sizeof(statvfsbuf.f_fstypename)) &&
269 1.14 tsutsui strncmp(statvfsbuf.f_fstypename, "ufs",
270 1.14 tsutsui sizeof(statvfsbuf.f_fstypename))) {
271 1.1 chuck errx(1, "%s: must be on an FFS filesystem", boot);
272 1.1 chuck }
273 1.1 chuck
274 1.1 chuck if (fsync(fd) != 0)
275 1.1 chuck err(1, "fsync: %s", boot);
276 1.1 chuck
277 1.1 chuck if (fstat(fd, &statbuf) != 0)
278 1.1 chuck err(1, "fstat: %s", boot);
279 1.1 chuck
280 1.1 chuck close(fd);
281 1.1 chuck
282 1.1 chuck /* Read superblock */
283 1.9 he devread(devfd, sblock, (daddr_t)(BBSIZE / DEV_BSIZE),
284 1.9 he SBLOCKSIZE, "superblock");
285 1.1 chuck fs = (struct fs *)sblock;
286 1.1 chuck
287 1.1 chuck /* Sanity-check super-block. */
288 1.9 he if (fs->fs_magic != FS_UFS1_MAGIC)
289 1.9 he errx(1, "Bad magic number in superblock, must be UFS1");
290 1.1 chuck if (fs->fs_inopb <= 0)
291 1.1 chuck err(1, "Bad inopb=%d in superblock", fs->fs_inopb);
292 1.1 chuck
293 1.1 chuck /* Read inode */
294 1.1 chuck if ((buf = malloc(fs->fs_bsize)) == NULL)
295 1.1 chuck errx(1, "No memory for filesystem block");
296 1.1 chuck
297 1.1 chuck blk = fsbtodb(fs, ino_to_fsba(fs, statbuf.st_ino));
298 1.1 chuck devread(devfd, buf, blk, fs->fs_bsize, "inode");
299 1.9 he ip = (struct ufs1_dinode *)(buf) + ino_to_fsbo(fs, statbuf.st_ino);
300 1.1 chuck
301 1.1 chuck /*
302 1.1 chuck * Have the inode. Figure out how many blocks we need.
303 1.1 chuck */
304 1.1 chuck ndb = howmany(ip->di_size, fs->fs_bsize);
305 1.1 chuck if (ndb > maxblocknum)
306 1.1 chuck errx(1, "Too many blocks");
307 1.1 chuck *block_count_p = ndb;
308 1.1 chuck *block_size_p = fs->fs_bsize;
309 1.1 chuck if (verbose)
310 1.1 chuck printf("Will load %d blocks of size %d each.\n",
311 1.14 tsutsui ndb, fs->fs_bsize);
312 1.1 chuck
313 1.1 chuck /*
314 1.1 chuck * Get the block numbers; we don't handle fragments
315 1.1 chuck */
316 1.1 chuck ap = ip->di_db;
317 1.16 dholland for (i = 0; i < UFS_NDADDR && *ap && ndb; i++, ap++, ndb--) {
318 1.1 chuck blk = fsbtodb(fs, *ap);
319 1.1 chuck if (verbose)
320 1.1 chuck printf("%d: %d\n", i, blk);
321 1.1 chuck block_table[i] = blk;
322 1.1 chuck }
323 1.1 chuck if (ndb == 0)
324 1.1 chuck return 0;
325 1.1 chuck
326 1.1 chuck /*
327 1.1 chuck * Just one level of indirections; there isn't much room
328 1.1 chuck * for more in the 1st-level bootblocks anyway.
329 1.1 chuck */
330 1.1 chuck blk = fsbtodb(fs, ip->di_ib[0]);
331 1.1 chuck devread(devfd, buf, blk, fs->fs_bsize, "indirect block");
332 1.8 fvdl /* XXX ondisk32 */
333 1.8 fvdl ap = (int32_t *)buf;
334 1.17 dholland for (; i < FFS_NINDIR(fs) && *ap && ndb; i++, ap++, ndb--) {
335 1.1 chuck blk = fsbtodb(fs, *ap);
336 1.1 chuck if (verbose)
337 1.1 chuck printf("%d: %d\n", i, blk);
338 1.1 chuck block_table[i] = blk;
339 1.1 chuck }
340 1.1 chuck
341 1.1 chuck return 0;
342 1.1 chuck }
343