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