Annotation of coherent/d/conf/patch/patch.c, revision 1.1

1.1     ! root        1: static char Copyright[] =      "$Copyright: (c) 1985, INETCO Systems, Ltd.$";
        !             2: static char Release[] =                "$Release: INETCO COHERENT V8.0$";
        !             3: static char Date[] =           "$Date: 91/04/24 14:20:20 $";
        !             4: 
        !             5: /* (lgl-
        !             6:  *     The information contained herein is a trade secret of Mark Williams
        !             7:  *     Company, and  is confidential information.  It is provided  under a
        !             8:  *     license agreement,  and may be  copied or disclosed  only under the
        !             9:  *     terms of  that agreement.  Any  reproduction or disclosure  of this
        !            10:  *     material without the express written authorization of Mark Williams
        !            11:  *     Company or persuant to the license agreement is unlawful.
        !            12:  *
        !            13:  *     COHERENT Version 2.3.35
        !            14:  *     Copyright (c) 1982, 1983, 1984.
        !            15:  *     An unpublished work by Mark Williams Company, Chicago.
        !            16:  *     All rights reserved.
        !            17:  -lgl) */
        !            18: /*
        !            19:  * Patch binary system images
        !            20:  * and possibly the running system.
        !            21:  * This program is not expected to work other than on PC Coherent.
        !            22:  * Certain hot patches may not be effective, since some values are only
        !            23:  * referenced once at system initialization.
        !            24:  *
        !            25:  * $Log:       /newbits/conf/patch/patch.c,v $
        !            26:  * Revision 1.1        91/04/24  14:20:20      bin
        !            27:  * Initial revision
        !            28:  * 
        !            29:  * 87/02/01    Allan Cornish           /usr/src/cmd/conf/patch.c
        !            30:  * myatol() routine added which recognizes numeric base specifications.
        !            31:  * All references to atol() modified to use myatol().
        !            32:  * main() now enables buffering on standard output.
        !            33:  *
        !            34:  */
        !            35: char helpmessage[] = "\
        !            36: patch -- alter coherent binary image\n\
        !            37: Usage: patch [ -k ] imagename symbol=value [ ... ]\n\
        !            38: Options:\n\
        !            39:        -k      patch running system via /dev/kmem\n\
        !            40: Patch alters the value of 'symbol' to 'value' in the binary 'imagename'.\n\
        !            41: Both 'symbol' and 'value' may be composed of a decimal numeric constant\n\
        !            42: or of a symbol in the image's symbol table, trailing '_' is significant,\n\
        !            43: optionally offset by + or - a decimal numeric constant.\n\
        !            44: The 'value' field may be optionally composed of 'makedev(d1, d2)' where 'd1'\n\
        !            45: and 'd2' are decimal numbers and the result is a dev_t value.\n\
        !            46: The size of the altered field is by default sizeof(int), but the 'value'\n\
        !            47: specification may be followed by a ':' and a 'c', 's', 'i', or 'l' to\n\
        !            48: explicitly specify a char, short, int, or long sized patch.\n\
        !            49: \
        !            50: ";
        !            51: 
        !            52: #include <stdio.h>
        !            53: #include <l.out.h>
        !            54: #include <canon.h>
        !            55: #include <ctype.h>
        !            56: 
        !            57: #include <sys/machine.h>
        !            58: #include <sys/types.h>
        !            59: #include <sys/stat.h>
        !            60: 
        !            61: /*
        !            62:  * Nlist tables and patch records.
        !            63:  */
        !            64: #define NNLS   512
        !            65: int    nnls;   /* Number of nlist elements used */
        !            66: struct nlist nl[NNLS*2];
        !            67: struct plist {
        !            68:        struct  nlist *p_lvnp, *p_rvnp;
        !            69:        long    p_lval, p_rval;
        !            70:        int     p_type;
        !            71:        char    p_char;
        !            72:        short   p_short;
        !            73:        int     p_int;
        !            74:        long    p_long;
        !            75: } pl[NNLS];
        !            76: char *namep;
        !            77: struct ldheader ldh;
        !            78: int hotpatch = 0;
        !            79: 
        !            80: main(argc, argv)
        !            81: char *argv[];
        !            82: {
        !            83:        static char obuf[BUFSIZ];
        !            84: 
        !            85:        /*
        !            86:         * Enable output buffering.
        !            87:         */
        !            88:        setbuf( stdout, obuf );
        !            89: 
        !            90:        if (argc > 1 && strcmp(argv[1], "-k") == 0) {
        !            91:                hotpatch += 1;
        !            92:                argv += 1;
        !            93:                argc -= 1;
        !            94:        }
        !            95:        if (argc < 3)
        !            96:                usage();
        !            97:        namep = argv[1];
        !            98:        if (getnames(argc-2, &argv[2]) == 0) {
        !            99:                setfile(argc-2);
        !           100:                if (hotpatch)
        !           101:                        setkmem(argc-2);
        !           102:                exit(0);
        !           103:        }
        !           104:        exit(1);
        !           105: }
        !           106: 
        !           107: getnames(nn, npp)
        !           108: int nn;
        !           109: char **npp;
        !           110: {
        !           111:        register int i;
        !           112:        register struct plist *p;
        !           113:        register struct nlist *np;
        !           114:        int nbad;
        !           115: 
        !           116:        nbad = 0;
        !           117:        for (i = 0; i < nn; i += 1)
        !           118:                if (i < NNLS-1)
        !           119:                        getone(i, npp[i]);
        !           120:        nlist(namep, nl);
        !           121:        for (i = 0; i < nn; i += 1)
        !           122:                if (i >= NNLS)
        !           123:                        printf("Too many patches: %s ignored\n", npp[i]);
        !           124:                else {
        !           125:                        p = &pl[i];
        !           126:                        if ((np = p->p_lvnp) != NULL) {
        !           127:                                if (np->n_type || np->n_value)
        !           128:                                        p->p_lval += np->n_value;
        !           129:                                else {
        !           130:                                        nbad += 1;
        !           131:                                        badsym(np->n_name);
        !           132:                                }
        !           133:                        }
        !           134:                        if ((np = p->p_rvnp) != NULL) {
        !           135:                                if (np->n_type || np->n_value)
        !           136:                                        p->p_rval += np->n_value;
        !           137:                                else {
        !           138:                                        nbad += 1;
        !           139:                                        badsym(np->n_name);
        !           140:                                }
        !           141:                        }
        !           142:                        switch (p->p_type) {
        !           143:                        case 'c': p->p_char     = p->p_rval; break;
        !           144:                        case 's': p->p_short    = p->p_rval; break;
        !           145:                        case 'i': p->p_int      = p->p_rval; break;
        !           146:                        case 'l': p->p_long     = p->p_rval; break;
        !           147:                        default:
        !           148:                                nbad += 1;
        !           149:                                printf("Bad type in %s\n", npp[i]);
        !           150:                                break;
        !           151:                        }
        !           152:                }
        !           153:        return (nbad);
        !           154: }
        !           155: 
        !           156: badsym(np)
        !           157: char *np;
        !           158: {
        !           159:        printf("%*.*s not found in %s\n", NCPLN, NCPLN, np, namep);
        !           160: }
        !           161: 
        !           162: getone(i, np)
        !           163: int i;
        !           164: register char *np;
        !           165: {
        !           166:        register int n;
        !           167:        register char *cp;
        !           168:        long myatol();
        !           169: 
        !           170:        pl[i].p_lvnp = NULL;
        !           171:        pl[i].p_lval = 0;
        !           172:        pl[i].p_rvnp = NULL;
        !           173:        pl[i].p_rval = 0;
        !           174:        pl[i].p_type = 'i';
        !           175:        if (isalpha(*np) || *np == '_') {
        !           176:                pl[i].p_lvnp = nl + nnls;
        !           177:                cp = nl[nnls].n_name;
        !           178:                nnls += 1;
        !           179:                for (n = 0; ; n += 1) {
        !           180:                        if ( ! isalnum(*np) && *np != '_')
        !           181:                                break;
        !           182:                        if (n < NCPLN)
        !           183:                                *cp++ = *np;
        !           184:                        np += 1;
        !           185:                }
        !           186: /* printf(" %s", nl[nnls-1].n_name); */
        !           187:        }
        !           188:        if (*np == '+')
        !           189:                np += 1;
        !           190:        pl[i].p_lval = myatol(np);
        !           191: /* printf(" %D =", pl[i].p_lval); */
        !           192:        np = index(np, '=');
        !           193:        if (np != NULL) {
        !           194:                np += 1;
        !           195:                if (strncmp(np, "makedev(", 8) == 0) {
        !           196:                        int d1, d2;
        !           197: 
        !           198:                        np = index(np, '(') + 1;
        !           199:                        d1 = myatol(np);
        !           200:                        np = index(np, ',');
        !           201:                        if (np != NULL) {
        !           202:                                d2 = myatol(np + 1);
        !           203:                                np = index(np, ')');
        !           204:                        } else
        !           205:                                d2 = 0;
        !           206:                        pl[i].p_rval = makedev(d1, d2);
        !           207:                        if (np == NULL)
        !           208:                                np = "";
        !           209:                        else
        !           210:                                np += 1;
        !           211:                        goto tail;
        !           212:                } else if (isalpha(*np) || *np == '_') {
        !           213:                        pl[i].p_rvnp = nl + nnls;
        !           214:                        cp = nl[nnls].n_name;
        !           215:                        nnls += 1;
        !           216:                        for (n = 0; ; n += 1) {
        !           217:                                if ( ! isalnum(*np) && *np != '_')
        !           218:                                        break;
        !           219:                                if (n < NCPLN)
        !           220:                                        *cp++ = *np;
        !           221:                                np += 1;
        !           222:                        }
        !           223: /* printf(" %s", nl[nnls-1].n_name); */
        !           224:                }
        !           225:                if (*np == '+')
        !           226:                        np += 1;
        !           227:                pl[i].p_rval = myatol(np);
        !           228: /* printf(" %D", pl[i].p_rval); */
        !           229: tail:
        !           230:                np = index(np, ':');
        !           231:                if (np != NULL)
        !           232:                        pl[i].p_type = np[1];
        !           233:        }
        !           234: /* printf(" : %c\n", pl[i].p_type); */
        !           235: }
        !           236: 
        !           237: setfile(n)
        !           238: int n;
        !           239: {
        !           240:        int u;
        !           241:        register int i;
        !           242:        long seekoff, minval, maxval;
        !           243:        long seek;
        !           244: 
        !           245:        if ((u=open(namep, 2)) < 0) {
        !           246:                printf("Cannot open %s\n", namep);
        !           247:                exit(1);
        !           248:        }
        !           249:        if (read(u, &ldh, sizeof(ldh)) != sizeof(ldh)) {
        !           250:                printf("Cannot read %s\n", namep);
        !           251:                exit(1);
        !           252:        }
        !           253:        canint(ldh.l_magic);
        !           254:        if (ldh.l_magic != L_MAGIC) {
        !           255:                printf("%s is not an image\n", namep);
        !           256:                exit(1);
        !           257:        }
        !           258:        canint(ldh.l_flag);
        !           259:        cansize(ldh.l_ssize[L_SHRI]);
        !           260:        cansize(ldh.l_ssize[L_PRVI]);
        !           261:        cansize(ldh.l_ssize[L_SHRD]);
        !           262:        cansize(ldh.l_ssize[L_PRVD]);
        !           263:        seekoff = sizeof(ldh);
        !           264:        minval = 0;
        !           265:        if (ldh.l_flag&LF_SEP)  /* Separate i and d */
        !           266:                seekoff += ldh.l_ssize[L_SHRI] + ldh.l_ssize[L_PRVI];
        !           267:        else
        !           268:                minval = ldh.l_ssize[L_SHRI] + ldh.l_ssize[L_PRVI];
        !           269:        maxval = minval + ldh.l_ssize[L_SHRD] + ldh.l_ssize[L_PRVD];
        !           270:        for (i = 0; i < n; i += 1) {
        !           271:                seek = pl[i].p_lval;
        !           272:                if (seek < minval || seek >= maxval) {
        !           273:                        printf("%s: cannot patch\n", nl[i].n_name);
        !           274:                        continue;
        !           275:                }
        !           276:                lseek(u, seekoff+seek, 0);
        !           277:                if (patch(u, &pl[i]) < 0)
        !           278:                        printf("Write error in %s\n", namep);
        !           279:        }
        !           280:        close(u);
        !           281: }
        !           282: 
        !           283: setkmem(n)
        !           284: int n;
        !           285: {
        !           286:        int u;
        !           287:        register int i;
        !           288: 
        !           289:        if ((u=open("/dev/kmem", 2)) < 0) {
        !           290:                printf("Cannot open /dev/kmem\n");
        !           291:                return;
        !           292:        }
        !           293:        for (i = 0; i < n; i += 1) {
        !           294:                lseek(u, pl[i].p_lval, 0);
        !           295:                if (patch(u, &pl[i]) < 0)
        !           296:                        printf("Write error in /dev/kmem\n");
        !           297:        }
        !           298:        close(u);
        !           299: }
        !           300: 
        !           301: patch(fd, p)
        !           302: int fd;
        !           303: struct plist *p;
        !           304: {
        !           305:        register char *bp;
        !           306:        register int nc;
        !           307: 
        !           308:        switch (p->p_type) {
        !           309:        case 'c':
        !           310:                bp = &p->p_char; nc = sizeof(char); break;
        !           311:        case 's':
        !           312:                bp = &p->p_short; nc = sizeof(short); break;
        !           313:        case 'i':
        !           314:                bp = &p->p_int; nc = sizeof(int); break;
        !           315:        case 'l':
        !           316:                bp = &p->p_long; nc = sizeof(long); break;
        !           317:        }
        !           318:        if (write(fd, bp, nc) != nc)
        !           319:                return (-1);
        !           320:        return (0);
        !           321: }
        !           322: 
        !           323: /**
        !           324:  *
        !           325:  * long
        !           326:  * myatol( s )         -- Ascii to Long integer conversion.
        !           327:  * char * s;
        !           328:  *
        !           329:  *     Input:  s = pointer to string containing a numeric prefix.
        !           330:  *
        !           331:  *     Action: Parse input string.
        !           332:  *             Parse optional leading sign character '-'.
        !           333:  *             Parse optional numeric base specification '0', '0o', and '0x'.
        !           334:  *             Parse following numeric digits.
        !           335:  *
        !           336:  *     Return: Long integer value.
        !           337:  *
        !           338:  *     Notes:  Numeric parsing terminates on first non-digit.
        !           339:  */
        !           340: long
        !           341: myatol( s )
        !           342: register char * s;
        !           343: {
        !           344:        register int base;
        !           345:        register int sign;
        !           346:        auto    long valu;
        !           347: 
        !           348:        /*
        !           349:         * Check for leading negative sign.
        !           350:         */
        !           351:        sign = 1;
        !           352:        if ( *s == '-' ) {
        !           353:                sign = -1;
        !           354:                s++;
        !           355:        }
        !           356: 
        !           357:        /*
        !           358:         * Check for base specification.
        !           359:         */
        !           360:        base = 10;
        !           361:        if ( *s == '0' ) {
        !           362:                switch ( *++s ) {
        !           363:                case 'x':       base = 16;      ++s;    break;
        !           364:                case 'o':       base =  8;      ++s;    break;
        !           365:                default:        base =  8;
        !           366:                }
        !           367:        }
        !           368: 
        !           369:        for ( valu = 0L; *s != '\0'; s++ ) {
        !           370: 
        !           371:                /*
        !           372:                 * Decimal digit.
        !           373:                 */
        !           374:                if ( ('0' <= *s) && (*s <= '9') ) {
        !           375:                        valu *= base;
        !           376:                        valu += *s - '0';
        !           377:                }
        !           378: 
        !           379:                /*
        !           380:                 * Upper case hex digit.
        !           381:                 */
        !           382:                else if ( (base == 16) && ('A' <= *s) && (*s <= 'F') ) {
        !           383:                        valu *= base;
        !           384:                        valu += *s - ('A' - 10);
        !           385:                }
        !           386: 
        !           387:                /*
        !           388:                 * Lower case Hex digit.
        !           389:                 */
        !           390:                else if ( (base == 16) && ('a' <= *s) && (*s <= 'f') ) {
        !           391:                        valu *= base;
        !           392:                        valu += *s - ('a' - 10);
        !           393:                }
        !           394: 
        !           395:                /*
        !           396:                 * Not a digit.
        !           397:                 */
        !           398:                else
        !           399:                        break;
        !           400:        }
        !           401: 
        !           402:        if ( sign < 0 )
        !           403:                valu = -valu;
        !           404: 
        !           405:        return valu;
        !           406: }
        !           407: 
        !           408: /*
        !           409:  * Print out an usage message.
        !           410:  */
        !           411: usage()
        !           412: {
        !           413:        printf(helpmessage);
        !           414:        exit(1);
        !           415: }
        !           416: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.