Annotation of embedtools/src/imgupd.c, revision 1.1.2.2

1.1.2.1   misho       1: #include "global.h"
                      2: 
                      3: 
                      4: char imgName[PATH_MAX], imgFile[PATH_MAX];
                      5: off_t imgSize, fSize, iSize;
                      6: int Verbose;
                      7: extern char compiled[], compiledby[], compilehost[];
                      8: 
                      9: static void
                     10: Usage()
                     11: {
                     12: 
                     13:        printf( "IMGUPD is tool for management of images\n"
                     14:                "=== %s === %s@%s ===\n\n"
                     15:                "  Syntax: imgupd [options] <image_file>\n\n"
                     16:                "\t-v\t\tVerbose ...\n"
                     17:                "\t-s <size>\tStorage size\n"
                     18:                "\t-f <devfile>\tStorage file name\n"
                     19:                "\n", compiled, compiledby, compilehost);
                     20: }
                     21: 
                     22: static int
                     23: EmptyStore(int img)
                     24: {
                     25:        register int i;
                     26:        u_char buf[IMGBLOCK_SIZE];
                     27:        ssize_t wlen;
                     28: 
1.1.2.2 ! misho      29:        VERB(1) printf("Erase store %s\n", imgName);
        !            30: 
1.1.2.1   misho      31:        memset(buf, 0, sizeof buf);
                     32:        for (i = 0; i < howmany(imgSize, IMGBLOCK_SIZE); i++)
                     33:                if ((wlen = write(img, buf, sizeof buf)) == -1 || 
                     34:                                wlen != sizeof buf) {
1.1.2.2 ! misho      35:                        EERROR(EIO, "Error at chunk %d init %d bytes, should be %u\n", 
1.1.2.1   misho      36:                                        i, wlen, sizeof buf);
                     37:                        return -1;
                     38:                } else
                     39:                        VERB(1) printf("+Written chunk #%d\n", i);
                     40: 
                     41:        iSize = lseek(img, 0, SEEK_END);
                     42:        return iSize;
                     43: }
                     44: 
1.1.2.2 ! misho      45: static int
        !            46: FillStore(int img, int fd)
        !            47: {
        !            48:        register int i;
        !            49:        u_char buf[IMGBLOCK_SIZE];
        !            50:        ssize_t rlen, wlen;
        !            51: 
        !            52:        VERB(1) printf("Fill store %s from image file %s\n", imgName, imgFile);
        !            53: 
        !            54:        for (i = 0; i < howmany(imgSize, IMGBLOCK_SIZE); i++) {
        !            55:                memset(buf, 0, sizeof buf);
        !            56:                rlen = read(fd, buf, sizeof buf);
        !            57:                if (rlen == -1) {
        !            58:                        ESYSERR(0);
        !            59:                        return -1;
        !            60:                } else if (!rlen)
        !            61:                        break;
        !            62:                else
        !            63:                        VERB(1) printf("+Readed %d bytes for chunk #%d\n", rlen, i);
        !            64: 
        !            65:                wlen = write(img, buf, rlen);
        !            66:                if (wlen == -1) {
        !            67:                        ESYSERR(0);
        !            68:                        return -1;
        !            69:                } else if (!wlen || wlen != rlen) {
        !            70:                        EERROR(EIO, "Readed %d bytes are not equal to written %d bytes\n", 
        !            71:                                        rlen, wlen);
        !            72:                } else
        !            73:                        VERB(1) printf("+Written %d bytes at chunk #%d\n", wlen, i);
        !            74:        }
        !            75: 
        !            76:        return 0;
        !            77: }
        !            78: 
1.1.2.1   misho      79: int
                     80: main(int argc, char **argv)
                     81: {
                     82:        char ch;
                     83:        int fd, img;
                     84: 
                     85:        while ((ch = getopt(argc, argv, "hvs:f:")) != -1)
                     86:                switch (ch) {
                     87:                        case 'f':
                     88:                                strlcpy(imgName, optarg, sizeof imgName);
                     89:                                break;
                     90:                        case 's':
                     91:                                imgSize = strtoll(optarg, NULL, 0);
                     92:                                if (!imgSize) {
                     93:                                        Usage();
                     94:                                        return 1;
                     95:                                }
                     96:                                break;
                     97:                        case 'v':
                     98:                                Verbose++;
                     99:                                break;
                    100:                        case 'h':
                    101:                        default:
                    102:                                Usage();
                    103:                                return 1;
                    104:                }
                    105:        argc -= optind;
                    106:        argv += optind;
                    107:        if (!argc || !*imgName || !imgSize) {
                    108:                Usage();
                    109:                return 1;
                    110:        } else
                    111:                strlcpy(imgFile, *argv, sizeof imgFile);
                    112: 
                    113:        VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", imgSize, imgName, imgFile);
                    114: 
                    115:        /* open image file */
                    116:        fd = open(imgFile, O_RDONLY);
                    117:        if (fd == -1) {
                    118:                ESYSERR(0);
                    119:                return 2;
                    120:        } else
                    121:                fSize = lseek(fd, 0, SEEK_END);
                    122:        if (fSize == -1 || fSize > imgSize) {
                    123:                close(fd);
                    124:                printf("Error:: file size %llu is greater from storage size %llu\n", 
                    125:                                fSize, imgSize);
                    126:                return 2;
                    127:        } else
                    128:                lseek(fd, 0, SEEK_SET);
                    129: 
                    130:        /* open storage device */
                    131:        img = open(imgName, O_RDWR | O_CREAT, 0644);
                    132:        if (img == -1) {
                    133:                ESYSERR(0);
                    134:                close(fd);
                    135:                return 3;
                    136:        }
                    137:        if (EmptyStore(img) == -1 || iSize < imgSize) {
                    138:                close(fd);
                    139:                close(img);
                    140:                unlink(imgName);
                    141:                printf("Error:: current storage size %llu is smaller from storage size %llu\n", 
                    142:                                iSize, imgSize);
                    143:                return 3;
                    144:        } else
                    145:                lseek(img, 0, SEEK_SET);
                    146: 
1.1.2.2 ! misho     147:        if (FillStore(img, fd) == -1) {
        !           148:                close(fd);
        !           149:                close(img);
        !           150:                unlink(imgName);
        !           151:                return 4;
        !           152:        }
        !           153: 
1.1.2.1   misho     154:        close(img);
                    155:        close(fd);
                    156:        return 0;
                    157: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>