[BACK]Return to installboot.c CVS log [TXT][DIR] Up to [local] / sys / arch / sparc / stand / installboot

Annotation of sys/arch/sparc/stand/installboot/installboot.c, Revision 1.1.1.1

1.1       nbrk        1: /*     $OpenBSD: installboot.c,v 1.4 2003/08/25 23:36:46 tedu Exp $    */
                      2: /*     $NetBSD: installboot.c,v 1.1 1997/06/01 03:39:45 mrg 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/stat.h>
                     38: #include <sys/sysctl.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:
                     51: int    verbose, nowrite, hflag;
                     52: char   *boot, *proto, *dev;
                     53:
                     54: struct nlist nl[] = {
                     55: #define X_BLOCKTABLE   0
                     56:        {"_block_table"},
                     57: #define X_BLOCKCOUNT   1
                     58:        {"_block_count"},
                     59: #define X_BLOCKSIZE    2
                     60:        {"_block_size"},
                     61:        {NULL}
                     62: };
                     63: daddr_t        *block_table;           /* block number array in prototype image */
                     64: int32_t        *block_count_p;         /* size of this array */
                     65: int32_t        *block_size_p;          /* filesystem block size */
                     66: int32_t        max_block_count;
                     67:
                     68: char   *karch;
                     69: char   cpumodel[130];
                     70:
                     71: int    isofsblk = 0;
                     72: int    isofseblk = 0;
                     73:
                     74: char           *loadprotoblocks(char *, long *);
                     75: int            loadblocknums(char *, int);
                     76: static void    devread(int, void *, daddr_t, size_t, char *);
                     77: static void    usage(void);
                     78: int            main(int, char *[]);
                     79:
                     80:
                     81: static void
                     82: usage()
                     83: {
                     84:        fprintf(stderr,
                     85:                "usage: installboot [-n] [-v] [-h] [-s isofsblk -e isofseblk] [-a <karch>] <boot> <proto> <device>\n");
                     86:        exit(1);
                     87: }
                     88:
                     89: int
                     90: main(argc, argv)
                     91:        int argc;
                     92:        char *argv[];
                     93: {
                     94:        int     c;
                     95:        int     devfd;
                     96:        char    *protostore;
                     97:        long    protosize;
                     98:        int     mib[2];
                     99:        size_t  size;
                    100:
                    101:        while ((c = getopt(argc, argv, "a:vnhs:e:")) != -1) {
                    102:                switch (c) {
                    103:                case 'a':
                    104:                        karch = optarg;
                    105:                        break;
                    106:                case 'h':       /* Note: for backwards compatibility */
                    107:                        /* Don't strip a.out header */
                    108:                        hflag = 1;
                    109:                        break;
                    110:                case 'n':
                    111:                        /* Do not actually write the bootblock to disk */
                    112:                        nowrite = 1;
                    113:                        break;
                    114:                case 'v':
                    115:                        /* Chat */
                    116:                        verbose = 1;
                    117:                        break;
                    118:                case 's':
                    119:                        isofsblk = atoi(optarg);
                    120:                        break;
                    121:                case 'e':
                    122:                        isofseblk = atoi(optarg);
                    123:                        break;
                    124:                default:
                    125:                        usage();
                    126:                }
                    127:        }
                    128:
                    129:        if (argc - optind < 3) {
                    130:                usage();
                    131:        }
                    132:
                    133:        boot = argv[optind];
                    134:        proto = argv[optind + 1];
                    135:        dev = argv[optind + 2];
                    136:
                    137:        if (karch == NULL) {
                    138:                mib[0] = CTL_HW;
                    139:                mib[1] = HW_MODEL;
                    140:                size = sizeof(cpumodel);
                    141:                if (sysctl(mib, 2, cpumodel, &size, NULL, 0) == -1)
                    142:                        err(1, "sysctl");
                    143:
                    144:                if (size < 5 || strncmp(cpumodel, "SUN-4", 5) != 0) /*XXX*/
                    145:                        /* Assume a sun4c/sun4m */
                    146:                        karch = "sun4c";
                    147:                else
                    148:                        karch = "sun4";
                    149:        }
                    150:
                    151:        if (verbose) {
                    152:                printf("boot: %s\n", boot);
                    153:                printf("proto: %s\n", proto);
                    154:                printf("device: %s\n", dev);
                    155:                printf("architecture: %s\n", karch);
                    156:        }
                    157:
                    158:        if (strcmp(karch, "sun4") == 0) {
                    159:                hflag = 0;
                    160:        } else if (strcmp(karch, "sun4c") == 0) {
                    161:                hflag = 1;
                    162:        } else if (strcmp(karch, "sun4m") == 0) {
                    163:                hflag = 1;
                    164:        } else
                    165:                errx(1, "Unsupported architecture");
                    166:
                    167:        /* Load proto blocks into core */
                    168:        if ((protostore = loadprotoblocks(proto, &protosize)) == NULL)
                    169:                exit(1);
                    170:
                    171:        /* Open and check raw disk device */
                    172:        if ((devfd = open(dev, O_RDONLY, 0)) < 0)
                    173:                err(1, "open: %s", dev);
                    174:
                    175:        /* Extract and load block numbers */
                    176:        if (loadblocknums(boot, devfd) != 0)
                    177:                exit(1);
                    178:
                    179:        (void)close(devfd);
                    180:
                    181:        if (nowrite)
                    182:                return 0;
                    183:
                    184:        /* Write patched proto bootblocks into the superblock */
                    185:        if (protosize > SBSIZE - DEV_BSIZE)
                    186:                errx(1, "proto bootblocks too big");
                    187:
                    188:        if ((devfd = open(dev, O_RDWR, 0)) < 0)
                    189:                err(1, "open: %s", dev);
                    190:
                    191:        if (lseek(devfd, DEV_BSIZE, SEEK_SET) != DEV_BSIZE)
                    192:                err(1, "lseek bootstrap");
                    193:
                    194:        /* Sync filesystems (to clean in-memory superblock?) */
                    195:        sync();
                    196:
                    197:        if (write(devfd, protostore, protosize) != protosize)
                    198:                err(1, "write bootstrap");
                    199:        (void)close(devfd);
                    200:        return 0;
                    201: }
                    202:
                    203: char *
                    204: loadprotoblocks(fname, size)
                    205:        char *fname;
                    206:        long *size;
                    207: {
                    208:        int     fd, sz;
                    209:        char    *bp;
                    210:        struct  stat statbuf;
                    211:        struct  exec *hp;
                    212:        long    off;
                    213:
                    214:        /* Locate block number array in proto file */
                    215:        if (nlist(fname, nl) != 0) {
                    216:                warnx("nlist: %s: symbols not found", fname);
                    217:                return NULL;
                    218:        }
                    219:        if (nl[X_BLOCKTABLE].n_type != N_DATA + N_EXT) {
                    220:                warnx("nlist: %s: wrong type", nl[X_BLOCKTABLE].n_un.n_name);
                    221:                return NULL;
                    222:        }
                    223:        if (nl[X_BLOCKCOUNT].n_type != N_DATA + N_EXT) {
                    224:                warnx("nlist: %s: wrong type", nl[X_BLOCKCOUNT].n_un.n_name);
                    225:                return NULL;
                    226:        }
                    227:        if (nl[X_BLOCKSIZE].n_type != N_DATA + N_EXT) {
                    228:                warnx("nlist: %s: wrong type", nl[X_BLOCKSIZE].n_un.n_name);
                    229:                return NULL;
                    230:        }
                    231:
                    232:        if ((fd = open(fname, O_RDONLY)) < 0) {
                    233:                warn("open: %s", fname);
                    234:                return NULL;
                    235:        }
                    236:        if (fstat(fd, &statbuf) != 0) {
                    237:                warn("fstat: %s", fname);
                    238:                close(fd);
                    239:                return NULL;
                    240:        }
                    241:        if ((bp = calloc(roundup(statbuf.st_size, DEV_BSIZE), 1)) == NULL) {
                    242:                warnx("malloc: %s: no memory", fname);
                    243:                close(fd);
                    244:                return NULL;
                    245:        }
                    246:        if (read(fd, bp, statbuf.st_size) != statbuf.st_size) {
                    247:                warn("read: %s", fname);
                    248:                free(bp);
                    249:                close(fd);
                    250:                return NULL;
                    251:        }
                    252:        close(fd);
                    253:
                    254:        hp = (struct exec *)bp;
                    255:        sz = (hflag ? sizeof(*hp) : 0) + hp->a_text + hp->a_data;
                    256:        sz = roundup(sz, DEV_BSIZE);
                    257:
                    258:        /* Calculate the symbols' location within the proto file */
                    259:        off = N_DATOFF(*hp) - N_DATADDR(*hp) - (hp->a_entry - N_TXTADDR(*hp));
                    260:        block_table = (daddr_t *) (bp + nl[X_BLOCKTABLE].n_value + off);
                    261:        block_count_p = (int32_t *)(bp + nl[X_BLOCKCOUNT].n_value + off);
                    262:        block_size_p = (int32_t *) (bp + nl[X_BLOCKSIZE].n_value + off);
                    263:        if ((int)block_table & 3) {
                    264:                warn("%s: invalid address: block_table = %x",
                    265:                     fname, block_table);
                    266:                free(bp);
                    267:                close(fd);
                    268:                return NULL;
                    269:        }
                    270:        if ((int)block_count_p & 3) {
                    271:                warn("%s: invalid address: block_count_p = %x",
                    272:                     fname, block_count_p);
                    273:                free(bp);
                    274:                close(fd);
                    275:                return NULL;
                    276:        }
                    277:        if ((int)block_size_p & 3) {
                    278:                warn("%s: invalid address: block_size_p = %x",
                    279:                     fname, block_size_p);
                    280:                free(bp);
                    281:                close(fd);
                    282:                return NULL;
                    283:        }
                    284:        max_block_count = *block_count_p;
                    285:
                    286:        if (verbose) {
                    287:                printf("%s: entry point %#x\n", fname, hp->a_entry);
                    288:                printf("%s: a.out header %s\n", fname,
                    289:                        hflag?"left on":"stripped off");
                    290:                printf("proto bootblock size %ld\n", sz);
                    291:                printf("room for %d filesystem blocks at %#x\n",
                    292:                        max_block_count, nl[X_BLOCKTABLE].n_value);
                    293:        }
                    294:
                    295:        /*
                    296:         * We convert the a.out header in-vitro into something that
                    297:         * Sun PROMs understand.
                    298:         * Old-style (sun4) ROMs do not expect a header at all, so
                    299:         * we turn the first two words into code that gets us past
                    300:         * the 32-byte header where the actual code begins. In assembly
                    301:         * speak:
                    302:         *      .word   MAGIC           ! a NOP
                    303:         *      ba,a    start           !
                    304:         *      .skip   24              ! pad
                    305:         * start:
                    306:         */
                    307:
                    308: #define SUN_MAGIC      0x01030107
                    309: #define SUN4_BASTART   0x30800007      /* i.e.: ba,a `start' */
                    310:        *((int *)bp) = SUN_MAGIC;
                    311:        *((int *)bp + 1) = SUN4_BASTART;
                    312:
                    313:        *size = sz;
                    314:        return (hflag ? bp : (bp + sizeof(struct exec)));
                    315: }
                    316:
                    317: static void
                    318: devread(fd, buf, blk, size, msg)
                    319:        int     fd;
                    320:        void    *buf;
                    321:        daddr_t blk;
                    322:        size_t  size;
                    323:        char    *msg;
                    324: {
                    325:        if (lseek(fd, dbtob(blk), SEEK_SET) != dbtob(blk))
                    326:                err(1, "%s: devread: lseek", msg);
                    327:
                    328:        if (read(fd, buf, size) != size)
                    329:                err(1, "%s: devread: read", msg);
                    330: }
                    331:
                    332: static char sblock[SBSIZE];
                    333:
                    334: int
                    335: loadblocknums(boot, devfd)
                    336: char   *boot;
                    337: int    devfd;
                    338: {
                    339:        int             i, fd;
                    340:        struct  stat    statbuf;
                    341:        struct  statfs  statfsbuf;
                    342:        struct fs       *fs;
                    343:        char            *buf;
                    344:        daddr_t         blk, *ap;
                    345:        struct ufs1_dinode      *ip;
                    346:        int             ndb;
                    347:
                    348:        /*
                    349:         * Open 2nd-level boot program and record the block numbers
                    350:         * it occupies on the filesystem represented by `devfd'.
                    351:         */
                    352:        if ((fd = open(boot, O_RDONLY)) < 0)
                    353:                err(1, "open: %s", boot);
                    354:
                    355:        if (fstatfs(fd, &statfsbuf) != 0)
                    356:                err(1, "statfs: %s", boot);
                    357:
                    358:        if (isofsblk) {
                    359:                int i;
                    360:
                    361:                *block_size_p = 512;
                    362:                *block_count_p = (isofseblk - isofsblk + 1) * (2048/512);
                    363:                if (*block_count_p > max_block_count)
                    364:                        errx(1, "%s: Too many blocks", boot);
                    365:                if (verbose)
                    366:                        printf("%s: %d block numbers: ", boot, *block_count_p);
                    367:                for (i = 0; i < *block_count_p; i++) {
                    368:                        blk = (isofsblk * (2048/512)) + i;
                    369:                        block_table[i] = blk;
                    370:                        if (verbose)
                    371:                                printf("%d ", blk);
                    372:                }
                    373:                if (verbose)
                    374:                        printf("\n");
                    375:                return 0;
                    376:        }
                    377:
                    378:        if (strncmp(statfsbuf.f_fstypename, "ffs", MFSNAMELEN) &&
                    379:            strncmp(statfsbuf.f_fstypename, "ufs", MFSNAMELEN)) {
                    380:                errx(1, "%s: must be on an FFS filesystem", boot);
                    381:        }
                    382:
                    383:        if (fsync(fd) != 0)
                    384:                err(1, "fsync: %s", boot);
                    385:
                    386:        if (fstat(fd, &statbuf) != 0)
                    387:                err(1, "fstat: %s", boot);
                    388:
                    389:        close(fd);
                    390:
                    391:        /* Read superblock */
                    392:        devread(devfd, sblock, btodb(SBOFF), SBSIZE, "superblock");
                    393:        fs = (struct fs *)sblock;
                    394:
                    395:        /* Read inode */
                    396:        if ((buf = malloc(fs->fs_bsize)) == NULL)
                    397:                errx(1, "No memory for filesystem block");
                    398:
                    399:        blk = fsbtodb(fs, ino_to_fsba(fs, statbuf.st_ino));
                    400:        devread(devfd, buf, blk, fs->fs_bsize, "inode");
                    401:        ip = (struct ufs1_dinode *)(buf) + ino_to_fsbo(fs, statbuf.st_ino);
                    402:
                    403:        /*
                    404:         * Register filesystem block size.
                    405:         */
                    406:        *block_size_p = fs->fs_bsize;
                    407:
                    408:        /*
                    409:         * Get the block numbers; we don't handle fragments
                    410:         */
                    411:        ndb = howmany(ip->di_size, fs->fs_bsize);
                    412:        if (ndb > max_block_count)
                    413:                errx(1, "%s: Too many blocks", boot);
                    414:
                    415:        /*
                    416:         * Register block count.
                    417:         */
                    418:        *block_count_p = ndb;
                    419:
                    420:        if (verbose)
                    421:                printf("%s: block numbers: ", boot);
                    422:        ap = ip->di_db;
                    423:        for (i = 0; i < NDADDR && *ap && ndb; i++, ap++, ndb--) {
                    424:                blk = fsbtodb(fs, *ap);
                    425:                block_table[i] = blk;
                    426:                if (verbose)
                    427:                        printf("%d ", blk);
                    428:        }
                    429:        if (verbose)
                    430:                printf("\n");
                    431:
                    432:        if (ndb == 0)
                    433:                return 0;
                    434:
                    435:        /*
                    436:         * Just one level of indirections; there isn't much room
                    437:         * for more in the 1st-level bootblocks anyway.
                    438:         */
                    439:        if (verbose)
                    440:                printf("%s: block numbers (indirect): ", boot);
                    441:        blk = ip->di_ib[0];
                    442:        devread(devfd, buf, blk, fs->fs_bsize, "indirect block");
                    443:        ap = (daddr_t *)buf;
                    444:        for (; i < NINDIR(fs) && *ap && ndb; i++, ap++, ndb--) {
                    445:                blk = fsbtodb(fs, *ap);
                    446:                block_table[i] = blk;
                    447:                if (verbose)
                    448:                        printf("%d ", blk);
                    449:        }
                    450:        if (verbose)
                    451:                printf("\n");
                    452:
                    453:        if (ndb)
                    454:                errx(1, "%s: Too many blocks", boot);
                    455:        return 0;
                    456: }

CVSweb