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