Annotation of embedtools/src/upd.c, revision 1.1.2.6

1.1.2.1   misho       1: #include "global.h"
                      2: #include "upd.h"
                      3: 
                      4: 
1.1.2.3   misho       5: static inline int ChkImg(const char *csImg, char *psDir)
1.1.2.1   misho       6: {
1.1.2.3   misho       7:        int res = 0;
1.1.2.2   misho       8: 
1.1.2.3   misho       9:        getcwd(psDir, MAXPATHLEN);
1.1.2.2   misho      10:        if (access(csImg, R_OK) == -1) {
                     11:                printf("Error:: Unable to find new image %s #%d - %s\n", 
                     12:                                csImg, errno, strerror(errno));
1.1.2.3   misho      13:                res = -1;
1.1.2.2   misho      14:        } else {
1.1.2.3   misho      15:                strlcat(psDir, "/", MAXPATHLEN);
                     16:                strlcat(psDir, csImg, MAXPATHLEN);
1.1.2.2   misho      17:        }
                     18: 
1.1.2.3   misho      19:        return res;
                     20: }
                     21: 
                     22: // -------------------------------
                     23: 
                     24: int Activate(const char *csImg)
                     25: {
                     26:        char szDir[MAXPATHLEN];
                     27: 
                     28:        if (ChkImg(csImg, szDir) == -1)
                     29:                return -1;
                     30: 
                     31:        VERB(3) printf("Activate procedure for %s\n", szDir);
                     32: 
1.1.2.2   misho      33:        unlink(FIRMWARE_IMG);
                     34:        if (symlink(szDir, FIRMWARE_IMG) == -1) {
                     35:                printf("Error:: Unable to activate new image %s #%d - %s\n", 
                     36:                                csImg, errno, strerror(errno));
                     37:                return -2;
                     38:        }
                     39: 
                     40:        syslog(LOG_NOTICE, "Activate new image %s", csImg);
                     41:        VERB(1) printf("Activate new image %s\n", csImg);
1.1.2.1   misho      42:        return 0;
                     43: }
                     44: 
1.1.2.4   misho      45: int Install(const char *csImg, const char *psDir)
1.1.2.1   misho      46: {
1.1.2.4   misho      47:        int src, dst, len;
                     48:        u_char buf[BUFSIZ];
                     49:        char szDir[MAXPATHLEN], szFile[MAXPATHLEN];
                     50:        struct stat ss, ds;
1.1.2.1   misho      51: 
1.1.2.4   misho      52:        strlcpy(szFile, psDir, MAXPATHLEN);
                     53:        strlcat(szFile, "/", MAXPATHLEN);
                     54:        strlcat(szFile, csImg, MAXPATHLEN);
                     55:        if (access(szFile, R_OK) == -1) {
                     56:                printf("Error:: Unable to find new image %s #%d - %s\n", 
                     57:                                csImg, errno, strerror(errno));
                     58:                return -1;
                     59:        } else {
                     60:                memset(&ss, 0, sizeof ss);
                     61:                if (stat(szFile, &ss) == -1) {
                     62:                        printf("Error:: Unable to find new image %s #%d - %s\n", 
                     63:                                        csImg, errno, strerror(errno));
                     64:                        return -1;
                     65:                }
                     66:        }
                     67: 
                     68:        getcwd(szDir, MAXPATHLEN);
                     69:        VERB(3) printf("Install procedure from %s to %s\n", szFile, szDir);
                     70:        strlcat(szDir, "/", MAXPATHLEN);
                     71:        strlcat(szDir, csImg, MAXPATHLEN);
                     72:        memset(&ds, 0, sizeof ds);
                     73:        if (stat(szDir, &ds) == -1 && ENOENT != errno) {
                     74:                printf("Error:: Unable to stat target #%d - %s\n", 
                     75:                                errno, strerror(errno));
                     76:                return -1;
                     77:        }
                     78: 
                     79:        if (ss.st_dev == ds.st_dev && ss.st_ino == ds.st_ino) {
                     80:                printf("Error:: Unable to install into self ...\n");
                     81:                return -1;
                     82:        }
                     83: 
                     84:        dst = open(szDir, O_WRONLY | O_CREAT | O_TRUNC, 0644);
                     85:        if (dst == -1) {
                     86:                printf("Error:: in create image %s #%d - %s\n", 
                     87:                                szDir, errno, strerror(errno));
                     88:                return -1;
                     89:        }
                     90:        src = open(szFile, O_RDONLY);
                     91:        if (src == -1) {
                     92:                printf("Error:: in open image %s #%d - %s\n", 
                     93:                                szFile, errno, strerror(errno));
                     94:                close(dst);
                     95:                unlink(szDir);
                     96:                return -1;
                     97:        }
                     98: 
                     99:        while ((len = read(src, buf, BUFSIZ)) > 0)
                    100:                if (write(dst, buf, len) == -1) {
                    101:                        printf("Error:: in write image #%d - %s\n", 
                    102:                                        errno, strerror(errno));
                    103:                        close(src);
                    104:                        close(dst);
                    105:                        unlink(szDir);
                    106: 
                    107:                        len = -1;
                    108:                        break;
                    109:                }
                    110: 
                    111:        close(src);
                    112:        close(dst);
                    113: 
                    114:        if (!len) {
                    115:                syslog(LOG_NOTICE, "Install image %s to %s", csImg, szDir);
                    116:                VERB(1) printf("Install image %s to %s\n", csImg, szDir);
                    117:        }
                    118:        return len;
1.1.2.1   misho     119: }
                    120: 
1.1.2.3   misho     121: int Rollback(const char *csImg)
1.1.2.1   misho     122: {
1.1.2.4   misho     123:        int src, dst, len;
                    124:        u_char buf[BUFSIZ];
                    125:        char szDir[MAXPATHLEN], szFile[MAXPATHLEN];
                    126:        struct stat ss, ds;
1.1.2.3   misho     127: 
1.1.2.4   misho     128:        getcwd(szFile, MAXPATHLEN);
                    129:        strlcat(szFile, "/", MAXPATHLEN);
                    130:        strlcat(szFile, FIRMWARE_BAK, MAXPATHLEN);
                    131:        if (access(szFile, R_OK) == -1) {
                    132:                printf("Error:: Unable to find backup image #%d - %s\n", 
                    133:                                errno, strerror(errno));
                    134:                return -1;
                    135:        } else {
                    136:                memset(&ss, 0, sizeof ss);
                    137:                if (stat(szFile, &ss) == -1) {
                    138:                        printf("Error:: Unable to find backup image #%d - %s\n", 
                    139:                                        errno, strerror(errno));
                    140:                        return -1;
                    141:                }
                    142:        }
                    143: 
                    144:        getcwd(szDir, MAXPATHLEN);
                    145:        strlcat(szDir, "/", MAXPATHLEN);
                    146:        strlcat(szDir, csImg, MAXPATHLEN);
                    147:        VERB(3) printf("Rollback procedure for image %s from backup!\n", csImg);
                    148:        memset(&ds, 0, sizeof ds);
                    149:        if (stat(szDir, &ds) == -1 && ENOENT != errno) {
                    150:                printf("Error:: Unable to stat target #%d - %s\n", 
                    151:                                errno, strerror(errno));
                    152:                return -1;
                    153:        }
                    154: 
                    155:        if (ss.st_dev == ds.st_dev && ss.st_ino == ds.st_ino) {
                    156:                printf("Error:: Unable to rollback into self ...\n");
                    157:                return -1;
                    158:        }
                    159: 
                    160:        src = open(szFile, O_RDONLY);
                    161:        if (src == -1) {
                    162:                printf("Error:: in open backup %s #%d - %s\n", 
                    163:                                szFile, errno, strerror(errno));
1.1.2.3   misho     164:                return -1;
1.1.2.4   misho     165:        }
                    166:        dst = open(szDir, O_WRONLY | O_CREAT | O_TRUNC, 0644);
                    167:        if (dst == -1) {
                    168:                printf("Error:: in create image %s #%d - %s\n", 
                    169:                                szDir, errno, strerror(errno));
                    170:                close(src);
                    171:                return -1;
                    172:        }
1.1.2.3   misho     173: 
1.1.2.4   misho     174:        while ((len = read(src, buf, BUFSIZ)) > 0)
                    175:                if (write(dst, buf, len) == -1) {
                    176:                        printf("Error:: in write image #%d - %s\n", 
                    177:                                        errno, strerror(errno));
                    178:                        close(dst);
                    179:                        close(src);
                    180:                        unlink(szDir);
                    181: 
                    182:                        len = -1;
                    183:                        break;
                    184:                }
                    185: 
                    186:        close(dst);
                    187:        close(src);
                    188: 
                    189:        if (!len) {
                    190:                syslog(LOG_NOTICE, "Rollback image %s to %s", csImg, szDir);
                    191:                VERB(1) printf("Rollback image %s to %s\n", csImg, szDir);
                    192:        }
                    193:        return len;
1.1.2.1   misho     194: 
                    195:        return 0;
                    196: }
                    197: 
1.1.2.3   misho     198: int tFTP(const char *csImg, const char *psDir)
1.1.2.1   misho     199: {
1.1.2.5   misho     200:        int src, dst, len;
                    201:        u_char buf[BUFSIZ];
                    202:        char szDir[MAXPATHLEN], szFile[MAXPATHLEN];
                    203:        struct stat ss, ds;
1.1.2.1   misho     204: 
1.1.2.3   misho     205:        if (ChkImg(csImg, szDir) == -1)
                    206:                return -1;
1.1.2.5   misho     207:        else {
                    208:                memset(&ss, 0, sizeof ss);
                    209:                if (stat(szDir, &ss) == -1) {
                    210:                        printf("Error:: Unable to find image %s #%d - %s\n", 
                    211:                                        szDir, errno, strerror(errno));
                    212:                        return -1;
                    213:                }
                    214:        }
1.1.2.3   misho     215: 
                    216:        VERB(3) printf("tFTP procedure for %s to %s\n", szDir, psDir);
1.1.2.5   misho     217:        strlcpy(szFile, psDir, MAXPATHLEN);
                    218:        strlcat(szFile, "/", MAXPATHLEN);
                    219:        strlcat(szFile, csImg, MAXPATHLEN);
                    220:        memset(&ds, 0, sizeof ds);
                    221:        if (stat(szFile, &ds) == -1 && ENOENT != errno) {
                    222:                printf("Error:: Unable to stat target %s #%d - %s\n", 
                    223:                                szFile, errno, strerror(errno));
                    224:                return -1;
                    225:        }
                    226: 
                    227:        if (ss.st_dev == ds.st_dev && ss.st_ino == ds.st_ino) {
                    228:                printf("Error:: Unable to copy into self ...\n");
                    229:                return -1;
                    230:        }
                    231: 
                    232:        dst = open(szFile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
                    233:        if (dst == -1) {
                    234:                printf("Error:: in create backup %s #%d - %s\n", 
                    235:                                szFile, errno, strerror(errno));
                    236:                return -1;
                    237:        }
                    238:        src = open(szDir, O_RDONLY);
                    239:        if (src == -1) {
                    240:                printf("Error:: in open image %s #%d - %s\n", 
                    241:                                szDir, errno, strerror(errno));
                    242:                close(dst);
                    243:                unlink(szFile);
                    244:                return -1;
                    245:        }
                    246: 
                    247:        while ((len = read(src, buf, BUFSIZ)) > 0)
                    248:                if (write(dst, buf, len) == -1) {
                    249:                        printf("Error:: in write backup #%d - %s\n", 
                    250:                                        errno, strerror(errno));
                    251:                        close(src);
                    252:                        close(dst);
                    253:                        unlink(szFile);
1.1.2.3   misho     254: 
1.1.2.5   misho     255:                        len = -1;
                    256:                        break;
                    257:                }
1.1.2.3   misho     258: 
1.1.2.5   misho     259:        close(src);
                    260:        close(dst);
                    261: 
                    262:        if (!len) {
1.1.2.3   misho     263:                syslog(LOG_NOTICE, "Export tFTP image %s to %s", csImg, psDir);
                    264:                VERB(1) printf("Export tFTP image %s to %s\n", csImg, psDir);
                    265:        }
1.1.2.5   misho     266:        return len;
1.1.2.1   misho     267: }
                    268: 
1.1.2.5   misho     269: int Backup(const char *csImg)
1.1.2.1   misho     270: {
1.1.2.3   misho     271:        int src, dst, len;
                    272:        u_char buf[BUFSIZ];
                    273:        char szDir[MAXPATHLEN], szFile[MAXPATHLEN];
1.1.2.1   misho     274: 
1.1.2.3   misho     275:        if (ChkImg(csImg, szDir) == -1)
                    276:                return -1;
                    277: 
1.1.2.5   misho     278:        getcwd(szFile, MAXPATHLEN);
1.1.2.3   misho     279:        strlcat(szFile, "/", MAXPATHLEN);
                    280:        strlcat(szFile, FIRMWARE_BAK, MAXPATHLEN);
1.1.2.5   misho     281:        VERB(3) printf("Backup procedure for %s\n", szDir);
1.1.2.3   misho     282: 
                    283:        dst = open(szFile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
                    284:        if (dst == -1) {
                    285:                printf("Error:: in create backup %s #%d - %s\n", 
                    286:                                szFile, errno, strerror(errno));
                    287:                return -1;
                    288:        }
                    289:        src = open(szDir, O_RDONLY);
                    290:        if (src == -1) {
                    291:                printf("Error:: in open image %s #%d - %s\n", 
                    292:                                szDir, errno, strerror(errno));
                    293:                close(dst);
                    294:                unlink(szFile);
                    295:                return -1;
                    296:        }
                    297: 
                    298:        while ((len = read(src, buf, BUFSIZ)) > 0)
                    299:                if (write(dst, buf, len) == -1) {
                    300:                        printf("Error:: in write backup #%d - %s\n", 
                    301:                                        errno, strerror(errno));
                    302:                        close(src);
                    303:                        close(dst);
                    304:                        unlink(szFile);
1.1.2.4   misho     305: 
                    306:                        len = -1;
                    307:                        break;
1.1.2.3   misho     308:                }
                    309: 
                    310:        close(src);
                    311:        close(dst);
                    312: 
1.1.2.5   misho     313:        if (!len) {
1.1.2.3   misho     314:                syslog(LOG_NOTICE, "Backup image %s", csImg);
                    315:                VERB(1) printf("Backup image %s\n", csImg);
                    316:        }
1.1.2.4   misho     317:        return len;
1.1.2.1   misho     318: }
                    319: 
1.1.2.3   misho     320: int Clean(const char *csImg)
1.1.2.1   misho     321: {
1.1.2.3   misho     322:        char szDir[MAXPATHLEN], szFile[MAXPATHLEN];
                    323: 
                    324:        if (ChkImg(csImg, szDir) == -1)
                    325:                return -1;
                    326: 
                    327:        getcwd(szFile, MAXPATHLEN);
                    328:        strlcat(szFile, "/", MAXPATHLEN);
                    329:        strlcat(szFile, FIRMWARE_BAK, MAXPATHLEN);
                    330: 
                    331:        VERB(3) printf("Clean procedure for %s\n", szDir);
                    332: 
                    333:        if (unlink(szFile) == -1) {
                    334:                printf("Error:: in clean backup #%d - %s\n", errno, strerror(errno));
                    335:                return -1;
                    336:        }
1.1.2.1   misho     337: 
1.1.2.3   misho     338:        syslog(LOG_NOTICE, "Clean backup for image %s", csImg);
                    339:        VERB(1) printf("Clean backup for image %s\n", csImg);
1.1.2.1   misho     340:        return 0;
                    341: }
1.1.2.6 ! misho     342: 
        !           343: int Daemonize(struct sockaddr_in sin, const char *csTFTP)
        !           344: {
        !           345:        VERB(3) printf("Daemonize procedure for %s:%d to %s\n", 
        !           346:                        inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), csTFTP);
        !           347: 
        !           348:        return 0;
        !           349: }

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