installboot.c revision 1.2 1 1.2 tsubai /* $NetBSD: installboot.c,v 1.2 1998/06/26 12:29:29 tsubai Exp $ */
2 1.1 tsubai
3 1.1 tsubai /*
4 1.1 tsubai * Copyright (c) 1994 Paul Kranenburg
5 1.1 tsubai * All rights reserved.
6 1.1 tsubai *
7 1.1 tsubai * Redistribution and use in source and binary forms, with or without
8 1.1 tsubai * modification, are permitted provided that the following conditions
9 1.1 tsubai * are met:
10 1.1 tsubai * 1. Redistributions of source code must retain the above copyright
11 1.1 tsubai * notice, this list of conditions and the following disclaimer.
12 1.1 tsubai * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tsubai * notice, this list of conditions and the following disclaimer in the
14 1.1 tsubai * documentation and/or other materials provided with the distribution.
15 1.1 tsubai * 3. All advertising materials mentioning features or use of this software
16 1.1 tsubai * must display the following acknowledgement:
17 1.1 tsubai * This product includes software developed by Paul Kranenburg.
18 1.1 tsubai * 4. The name of the author may not be used to endorse or promote products
19 1.1 tsubai * derived from this software without specific prior written permission
20 1.1 tsubai *
21 1.1 tsubai * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 tsubai * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 tsubai * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 tsubai * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 tsubai * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 tsubai * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 tsubai * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 tsubai * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 tsubai * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 tsubai * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 tsubai */
32 1.1 tsubai
33 1.1 tsubai #include <sys/param.h>
34 1.1 tsubai #include <sys/mount.h>
35 1.1 tsubai #include <sys/time.h>
36 1.1 tsubai #include <sys/stat.h>
37 1.1 tsubai #include <sys/sysctl.h>
38 1.1 tsubai #include <ufs/ufs/dinode.h>
39 1.1 tsubai #include <ufs/ufs/dir.h>
40 1.1 tsubai #include <ufs/ffs/fs.h>
41 1.1 tsubai #include <err.h>
42 1.1 tsubai #ifdef BOOT_AOUT
43 1.1 tsubai #include <a.out.h>
44 1.1 tsubai #endif
45 1.1 tsubai #include <sys/exec_elf.h>
46 1.1 tsubai #include <fcntl.h>
47 1.1 tsubai #include <nlist.h>
48 1.1 tsubai #include <stdlib.h>
49 1.1 tsubai #include <stdio.h>
50 1.1 tsubai #include <string.h>
51 1.1 tsubai #include <unistd.h>
52 1.1 tsubai
53 1.1 tsubai int verbose, nowrite;
54 1.1 tsubai char *boot, *proto, *dev;
55 1.1 tsubai
56 1.1 tsubai #define BOOTSECTOR_OFFSET 2048
57 1.1 tsubai
58 1.2 tsubai #ifndef DEFAULT_ENTRY
59 1.2 tsubai #define DEFAULT_ENTRY 0x6c0000
60 1.2 tsubai #endif
61 1.2 tsubai
62 1.1 tsubai struct nlist nl[] = {
63 1.1 tsubai #define X_BLOCKTABLE 0
64 1.1 tsubai {"_block_table"},
65 1.1 tsubai #define X_BLOCKCOUNT 1
66 1.1 tsubai {"_block_count"},
67 1.1 tsubai #define X_BLOCKSIZE 2
68 1.1 tsubai {"_block_size"},
69 1.2 tsubai #define X_ENTRY_POINT 3
70 1.2 tsubai {"_entry_point"},
71 1.1 tsubai {NULL}
72 1.1 tsubai };
73 1.1 tsubai
74 1.1 tsubai daddr_t *block_table; /* block number array in prototype image */
75 1.1 tsubai int32_t *block_count_p; /* size of this array */
76 1.1 tsubai int32_t *block_size_p; /* filesystem block size */
77 1.2 tsubai int32_t *entry_point_p; /* entry point */
78 1.1 tsubai int32_t max_block_count;
79 1.1 tsubai
80 1.1 tsubai char *loadprotoblocks __P((char *, long *));
81 1.1 tsubai int loadblocknums __P((char *, int));
82 1.1 tsubai static void devread __P((int, void *, daddr_t, size_t, char *));
83 1.1 tsubai static void usage __P((void));
84 1.1 tsubai int main __P((int, char *[]));
85 1.1 tsubai
86 1.1 tsubai
87 1.1 tsubai static void
88 1.1 tsubai usage()
89 1.1 tsubai {
90 1.1 tsubai fprintf(stderr,
91 1.1 tsubai "usage: installboot [-n] [-v] <boot> <proto> <device>\n");
92 1.1 tsubai exit(1);
93 1.1 tsubai }
94 1.1 tsubai
95 1.1 tsubai int
96 1.1 tsubai main(argc, argv)
97 1.1 tsubai int argc;
98 1.1 tsubai char *argv[];
99 1.1 tsubai {
100 1.1 tsubai int c;
101 1.1 tsubai int devfd;
102 1.1 tsubai char *protostore;
103 1.1 tsubai long protosize;
104 1.1 tsubai int mib[2];
105 1.1 tsubai size_t size;
106 1.1 tsubai
107 1.1 tsubai while ((c = getopt(argc, argv, "vn")) != EOF) {
108 1.1 tsubai switch (c) {
109 1.1 tsubai
110 1.1 tsubai case 'n':
111 1.1 tsubai /* Do not actually write the bootblock to disk */
112 1.1 tsubai nowrite = 1;
113 1.1 tsubai break;
114 1.1 tsubai case 'v':
115 1.1 tsubai /* Chat */
116 1.1 tsubai verbose = 1;
117 1.1 tsubai break;
118 1.1 tsubai default:
119 1.1 tsubai usage();
120 1.1 tsubai }
121 1.1 tsubai }
122 1.1 tsubai
123 1.1 tsubai if (argc - optind < 3) {
124 1.1 tsubai usage();
125 1.1 tsubai }
126 1.1 tsubai
127 1.1 tsubai boot = argv[optind];
128 1.1 tsubai proto = argv[optind + 1];
129 1.1 tsubai dev = argv[optind + 2];
130 1.1 tsubai
131 1.1 tsubai if (verbose) {
132 1.1 tsubai printf("boot: %s\n", boot);
133 1.1 tsubai printf("proto: %s\n", proto);
134 1.1 tsubai printf("device: %s\n", dev);
135 1.1 tsubai }
136 1.1 tsubai
137 1.1 tsubai /* Load proto blocks into core */
138 1.1 tsubai if ((protostore = loadprotoblocks(proto, &protosize)) == NULL)
139 1.1 tsubai exit(1);
140 1.1 tsubai
141 1.1 tsubai /* Open and check raw disk device */
142 1.1 tsubai if ((devfd = open(dev, O_RDONLY, 0)) < 0)
143 1.1 tsubai err(1, "open: %s", dev);
144 1.1 tsubai
145 1.1 tsubai /* Extract and load block numbers */
146 1.1 tsubai if (loadblocknums(boot, devfd) != 0)
147 1.1 tsubai exit(1);
148 1.1 tsubai
149 1.1 tsubai (void)close(devfd);
150 1.1 tsubai
151 1.1 tsubai if (nowrite)
152 1.1 tsubai return 0;
153 1.1 tsubai
154 1.1 tsubai /* Write patched proto bootblocks into the superblock */
155 1.1 tsubai if (protosize > SBSIZE - DEV_BSIZE)
156 1.1 tsubai errx(1, "proto bootblocks too big");
157 1.1 tsubai
158 1.1 tsubai if ((devfd = open(dev, O_RDWR, 0)) < 0)
159 1.1 tsubai err(1, "open: %s", dev);
160 1.1 tsubai
161 1.1 tsubai if (lseek(devfd, BOOTSECTOR_OFFSET, SEEK_SET) != BOOTSECTOR_OFFSET)
162 1.1 tsubai err(1, "lseek bootstrap");
163 1.1 tsubai
164 1.1 tsubai /* Sync filesystems (to clean in-memory superblock?) */
165 1.1 tsubai sync();
166 1.1 tsubai
167 1.1 tsubai if (write(devfd, protostore, protosize) != protosize)
168 1.1 tsubai err(1, "write bootstrap");
169 1.1 tsubai (void)close(devfd);
170 1.1 tsubai return 0;
171 1.1 tsubai }
172 1.1 tsubai
173 1.1 tsubai char *
174 1.1 tsubai loadprotoblocks(fname, size)
175 1.1 tsubai char *fname;
176 1.1 tsubai long *size;
177 1.1 tsubai {
178 1.1 tsubai int fd, sz;
179 1.1 tsubai char *bp;
180 1.1 tsubai struct stat statbuf;
181 1.1 tsubai #ifdef BOOT_AOUT
182 1.1 tsubai struct exec *hp;
183 1.1 tsubai #endif
184 1.1 tsubai long off;
185 1.1 tsubai Elf32_Ehdr *eh;
186 1.1 tsubai Elf32_Phdr *ph;
187 1.1 tsubai
188 1.1 tsubai /* Locate block number array in proto file */
189 1.1 tsubai if (nlist(fname, nl) != 0) {
190 1.1 tsubai warnx("nlist: %s: symbols not found", fname);
191 1.1 tsubai return NULL;
192 1.1 tsubai }
193 1.1 tsubai #ifdef BOOT_AOUT
194 1.1 tsubai if (nl[X_BLOCKTABLE].n_type != N_DATA + N_EXT) {
195 1.1 tsubai warnx("nlist: %s: wrong type", nl[X_BLOCKTABLE].n_un.n_name);
196 1.1 tsubai return NULL;
197 1.1 tsubai }
198 1.1 tsubai if (nl[X_BLOCKCOUNT].n_type != N_DATA + N_EXT) {
199 1.1 tsubai warnx("nlist: %s: wrong type", nl[X_BLOCKCOUNT].n_un.n_name);
200 1.1 tsubai return NULL;
201 1.1 tsubai }
202 1.1 tsubai if (nl[X_BLOCKSIZE].n_type != N_DATA + N_EXT) {
203 1.1 tsubai warnx("nlist: %s: wrong type", nl[X_BLOCKSIZE].n_un.n_name);
204 1.1 tsubai return NULL;
205 1.1 tsubai }
206 1.1 tsubai #endif
207 1.1 tsubai
208 1.1 tsubai if ((fd = open(fname, O_RDONLY)) < 0) {
209 1.1 tsubai warn("open: %s", fname);
210 1.1 tsubai return NULL;
211 1.1 tsubai }
212 1.1 tsubai if (fstat(fd, &statbuf) != 0) {
213 1.1 tsubai warn("fstat: %s", fname);
214 1.1 tsubai close(fd);
215 1.1 tsubai return NULL;
216 1.1 tsubai }
217 1.1 tsubai if ((bp = calloc(roundup(statbuf.st_size, DEV_BSIZE), 1)) == NULL) {
218 1.1 tsubai warnx("malloc: %s: no memory", fname);
219 1.1 tsubai close(fd);
220 1.1 tsubai return NULL;
221 1.1 tsubai }
222 1.1 tsubai if (read(fd, bp, statbuf.st_size) != statbuf.st_size) {
223 1.1 tsubai warn("read: %s", fname);
224 1.1 tsubai free(bp);
225 1.1 tsubai close(fd);
226 1.1 tsubai return NULL;
227 1.1 tsubai }
228 1.1 tsubai close(fd);
229 1.1 tsubai
230 1.1 tsubai #ifdef BOOT_AOUT
231 1.1 tsubai hp = (struct exec *)bp;
232 1.1 tsubai #endif
233 1.1 tsubai eh = (Elf32_Ehdr *)bp;
234 1.1 tsubai ph = (Elf32_Phdr *)(bp + eh->e_phoff);
235 1.1 tsubai sz = 1024;
236 1.1 tsubai
237 1.1 tsubai /* Calculate the symbols' location within the proto file */
238 1.1 tsubai off = ph->p_offset - eh->e_entry;
239 1.1 tsubai block_table = (daddr_t *)(bp + nl[X_BLOCKTABLE].n_value + off);
240 1.1 tsubai block_count_p = (int32_t *)(bp + nl[X_BLOCKCOUNT].n_value + off);
241 1.1 tsubai block_size_p = (int32_t *)(bp + nl[X_BLOCKSIZE].n_value + off);
242 1.2 tsubai entry_point_p = (int32_t *)(bp + nl[X_ENTRY_POINT].n_value + off);
243 1.1 tsubai
244 1.1 tsubai if ((int)block_table & 3) {
245 1.1 tsubai warn("%s: invalid address: block_table = %x",
246 1.1 tsubai fname, block_table);
247 1.1 tsubai free(bp);
248 1.1 tsubai close(fd);
249 1.1 tsubai return NULL;
250 1.1 tsubai }
251 1.1 tsubai if ((int)block_count_p & 3) {
252 1.1 tsubai warn("%s: invalid address: block_count_p = %x",
253 1.1 tsubai fname, block_count_p);
254 1.1 tsubai free(bp);
255 1.1 tsubai close(fd);
256 1.1 tsubai return NULL;
257 1.1 tsubai }
258 1.1 tsubai if ((int)block_size_p & 3) {
259 1.1 tsubai warn("%s: invalid address: block_size_p = %x",
260 1.1 tsubai fname, block_size_p);
261 1.1 tsubai free(bp);
262 1.1 tsubai close(fd);
263 1.1 tsubai return NULL;
264 1.1 tsubai }
265 1.2 tsubai if ((int)entry_point_p & 3) {
266 1.2 tsubai warn("%s: invalid address: entry_point_p = %x",
267 1.2 tsubai fname, entry_point_p);
268 1.2 tsubai free(bp);
269 1.2 tsubai close(fd);
270 1.2 tsubai return NULL;
271 1.2 tsubai }
272 1.1 tsubai max_block_count = *block_count_p;
273 1.1 tsubai
274 1.1 tsubai if (verbose) {
275 1.2 tsubai printf("proto bootblock size: %ld\n", sz);
276 1.1 tsubai }
277 1.1 tsubai
278 1.1 tsubai /*
279 1.1 tsubai * We convert the a.out header in-vitro into something that
280 1.1 tsubai * Sun PROMs understand.
281 1.1 tsubai * Old-style (sun4) ROMs do not expect a header at all, so
282 1.1 tsubai * we turn the first two words into code that gets us past
283 1.1 tsubai * the 32-byte header where the actual code begins. In assembly
284 1.1 tsubai * speak:
285 1.1 tsubai * .word MAGIC ! a NOP
286 1.1 tsubai * ba,a start !
287 1.1 tsubai * .skip 24 ! pad
288 1.1 tsubai * start:
289 1.1 tsubai */
290 1.1 tsubai
291 1.1 tsubai *size = sz;
292 1.1 tsubai return (bp + 0x74);
293 1.1 tsubai }
294 1.1 tsubai
295 1.1 tsubai static void
296 1.1 tsubai devread(fd, buf, blk, size, msg)
297 1.1 tsubai int fd;
298 1.1 tsubai void *buf;
299 1.1 tsubai daddr_t blk;
300 1.1 tsubai size_t size;
301 1.1 tsubai char *msg;
302 1.1 tsubai {
303 1.1 tsubai if (lseek(fd, dbtob(blk), SEEK_SET) != dbtob(blk))
304 1.1 tsubai err(1, "%s: devread: lseek", msg);
305 1.1 tsubai
306 1.1 tsubai if (read(fd, buf, size) != size)
307 1.1 tsubai err(1, "%s: devread: read", msg);
308 1.1 tsubai }
309 1.1 tsubai
310 1.1 tsubai static char sblock[SBSIZE];
311 1.1 tsubai
312 1.1 tsubai int
313 1.1 tsubai loadblocknums(boot, devfd)
314 1.1 tsubai char *boot;
315 1.1 tsubai int devfd;
316 1.1 tsubai {
317 1.1 tsubai int i, fd;
318 1.1 tsubai struct stat statbuf;
319 1.1 tsubai struct statfs statfsbuf;
320 1.1 tsubai struct fs *fs;
321 1.1 tsubai char *buf;
322 1.1 tsubai daddr_t blk, *ap;
323 1.1 tsubai struct dinode *ip;
324 1.1 tsubai int ndb;
325 1.1 tsubai
326 1.1 tsubai /*
327 1.1 tsubai * Open 2nd-level boot program and record the block numbers
328 1.1 tsubai * it occupies on the filesystem represented by `devfd'.
329 1.1 tsubai */
330 1.1 tsubai if ((fd = open(boot, O_RDONLY)) < 0)
331 1.1 tsubai err(1, "open: %s", boot);
332 1.1 tsubai
333 1.1 tsubai if (fstatfs(fd, &statfsbuf) != 0)
334 1.1 tsubai err(1, "statfs: %s", boot);
335 1.1 tsubai
336 1.1 tsubai if (strncmp(statfsbuf.f_fstypename, "ffs", MFSNAMELEN) &&
337 1.1 tsubai strncmp(statfsbuf.f_fstypename, "ufs", MFSNAMELEN)) {
338 1.1 tsubai errx(1, "%s: must be on an FFS filesystem", boot);
339 1.1 tsubai }
340 1.1 tsubai
341 1.1 tsubai if (fsync(fd) != 0)
342 1.1 tsubai err(1, "fsync: %s", boot);
343 1.1 tsubai
344 1.1 tsubai if (fstat(fd, &statbuf) != 0)
345 1.1 tsubai err(1, "fstat: %s", boot);
346 1.1 tsubai
347 1.1 tsubai close(fd);
348 1.1 tsubai
349 1.1 tsubai /* Read superblock */
350 1.1 tsubai devread(devfd, sblock, btodb(SBOFF), SBSIZE, "superblock");
351 1.1 tsubai fs = (struct fs *)sblock;
352 1.1 tsubai
353 1.1 tsubai /* Read inode */
354 1.1 tsubai if ((buf = malloc(fs->fs_bsize)) == NULL)
355 1.1 tsubai errx(1, "No memory for filesystem block");
356 1.1 tsubai
357 1.1 tsubai blk = fsbtodb(fs, ino_to_fsba(fs, statbuf.st_ino));
358 1.1 tsubai devread(devfd, buf, blk, fs->fs_bsize, "inode");
359 1.1 tsubai ip = (struct dinode *)(buf) + ino_to_fsbo(fs, statbuf.st_ino);
360 1.1 tsubai
361 1.1 tsubai /*
362 1.1 tsubai * Register filesystem block size.
363 1.1 tsubai */
364 1.1 tsubai *block_size_p = fs->fs_bsize;
365 1.1 tsubai
366 1.1 tsubai /*
367 1.1 tsubai * Get the block numbers; we don't handle fragments
368 1.1 tsubai */
369 1.1 tsubai ndb = howmany(ip->di_size, fs->fs_bsize);
370 1.1 tsubai if (ndb > max_block_count)
371 1.1 tsubai errx(1, "%s: Too many blocks", boot);
372 1.1 tsubai
373 1.1 tsubai /*
374 1.1 tsubai * Register block count.
375 1.1 tsubai */
376 1.1 tsubai *block_count_p = ndb;
377 1.2 tsubai
378 1.2 tsubai /*
379 1.2 tsubai * Register entry point.
380 1.2 tsubai */
381 1.2 tsubai *entry_point_p = DEFAULT_ENTRY;
382 1.2 tsubai if (verbose)
383 1.2 tsubai printf("entry point: 0x%08x\n", *entry_point_p);
384 1.1 tsubai
385 1.1 tsubai if (verbose)
386 1.1 tsubai printf("%s: block numbers: ", boot);
387 1.1 tsubai ap = ip->di_db;
388 1.1 tsubai for (i = 0; i < NDADDR && *ap && ndb; i++, ap++, ndb--) {
389 1.1 tsubai blk = fsbtodb(fs, *ap);
390 1.1 tsubai block_table[i] = blk;
391 1.1 tsubai if (verbose)
392 1.1 tsubai printf("%d ", blk);
393 1.1 tsubai }
394 1.1 tsubai if (verbose)
395 1.1 tsubai printf("\n");
396 1.1 tsubai
397 1.1 tsubai if (ndb == 0)
398 1.1 tsubai return 0;
399 1.1 tsubai
400 1.1 tsubai /*
401 1.1 tsubai * Just one level of indirections; there isn't much room
402 1.1 tsubai * for more in the 1st-level bootblocks anyway.
403 1.1 tsubai */
404 1.1 tsubai if (verbose)
405 1.1 tsubai printf("%s: block numbers (indirect): ", boot);
406 1.1 tsubai blk = ip->di_ib[0];
407 1.1 tsubai devread(devfd, buf, blk, fs->fs_bsize, "indirect block");
408 1.1 tsubai ap = (daddr_t *)buf;
409 1.1 tsubai for (; i < NINDIR(fs) && *ap && ndb; i++, ap++, ndb--) {
410 1.1 tsubai blk = fsbtodb(fs, *ap);
411 1.1 tsubai block_table[i] = blk;
412 1.1 tsubai if (verbose)
413 1.1 tsubai printf("%d ", blk);
414 1.1 tsubai }
415 1.1 tsubai if (verbose)
416 1.1 tsubai printf("\n");
417 1.1 tsubai
418 1.1 tsubai if (ndb)
419 1.1 tsubai errx(1, "%s: Too many blocks", boot);
420 1.1 tsubai return 0;
421 1.1 tsubai }
422