Annotation of embedtools/src/ub_env.c, revision 1.1.2.13

1.1.2.1   misho       1: #include "global.h"
                      2: #include "ub_env.h"
                      3: 
                      4: 
1.1.2.4   misho       5: env_t *env;
                      6: 
1.1.2.3   misho       7: static int
1.1.2.5   misho       8: ub_flash_io(const char *csSec, int mode)
1.1.2.3   misho       9: {
1.1.2.7   misho      10:        int f, l, rlen;
1.1.2.5   misho      11:        const char *str;
1.1.2.12  misho      12:        size_t siz, esiz;
1.1.2.7   misho      13:        ait_val_t v;
1.1.2.12  misho      14:        u_int crc;
1.1.2.3   misho      15: 
                     16:        FTRACE(4);
                     17: 
1.1.2.12  misho      18:        str = cfg_getAttribute(&cfg, csSec, "drive_size");
1.1.2.6   misho      19:        siz = strtol(str, NULL, 0);
                     20:        if (!siz)
                     21:                return -1;
1.1.2.12  misho      22:        str = cfg_getAttribute(&cfg, csSec, "env_size");
                     23:        if (!str)
                     24:                esiz = siz - sizeof env->env_crc;
                     25:        else
                     26:                esiz = strtol(str, NULL, 0);
                     27:        str = cfg_getAttribute(&cfg, csSec, "drive_name");
1.1.2.5   misho      28:        if (!str) {
                     29:                printf("Error:: drive not found!\n");
                     30:                return -1;
                     31:        }
                     32: 
1.1.2.7   misho      33:        cfg_loadAttribute(&cfg, "ube", "lock", &v, UBE_LOCK);
1.1.2.8   misho      34:        l = open(AIT_GET_STR(&v), O_CREAT | O_EXCL | O_WRONLY, 0644);
1.1.2.7   misho      35:        if (l == -1) {
1.1.2.8   misho      36:                printf("Error:: Locked u-boot-env map %s\n", AIT_GET_STR(&v));
1.1.2.7   misho      37:                AIT_FREE_VAL(&v);
                     38:                return -1;
                     39:        }
                     40: 
1.1.2.5   misho      41:        f = open(str, mode);
1.1.2.3   misho      42:        if (f == -1) {
1.1.2.5   misho      43:                printf("Error:: Can't access u-boot-env device %s\n", str);
1.1.2.7   misho      44:                close(l);
                     45:                unlink(AIT_GET_STR(&v));
                     46:                AIT_FREE_VAL(&v);
1.1.2.3   misho      47:                return -1;
                     48:        }
                     49: 
                     50:        if (mode & O_RDWR) {
1.1.2.13! misho      51:                env->env_crc = crc32(0, (u_char*) env->env_data, esiz);
1.1.2.6   misho      52:                rlen = write(f, env, siz);
                     53:                if (rlen != siz)
                     54:                        printf("Error:: written %d bytes != %d\n", rlen, siz);
                     55:                else
                     56:                        VERB(3) printf("Written %d bytes\n", rlen);
                     57:                lseek(f, 0, SEEK_SET);
1.1.2.3   misho      58:        }
                     59: 
1.1.2.6   misho      60:        rlen = read(f, env, siz);
                     61:        if (rlen != siz)
                     62:                printf("Error:: readed %d bytes != %d\n", rlen, siz);
                     63:        else
                     64:                VERB(3) printf("Readed %d bytes\n", rlen);
                     65: 
1.1.2.12  misho      66:        crc = crc32(0, (u_char*) env->env_data, esiz);
                     67:        if (crc != env->env_crc)
1.1.2.13! misho      68:                VERB(1) printf("Warning:: Bad CRC, Flash crc32 0x%x != 0x%x\n", 
        !            69:                                env->env_crc, crc);
1.1.2.12  misho      70: 
1.1.2.3   misho      71:        close(f);
1.1.2.7   misho      72:        close(l);
                     73:        unlink(AIT_GET_STR(&v));
                     74:        AIT_FREE_VAL(&v);
                     75:        return 0;
1.1.2.3   misho      76: }
                     77: 
1.1.2.10  misho      78: static inline const char *
                     79: ub_envmatch(const char *csName, const char *e)
                     80: {
                     81:        while (*csName == *e++)
                     82:                if (*csName++ == '=')
                     83:                        return e;
                     84:        if (!*csName && *(e - 1) == '=')
                     85:                return e;
                     86: 
                     87:        return NULL;
                     88: }
                     89: 
1.1.2.4   misho      90: int
                     91: ub_load(const char *csSec)
                     92: {
                     93:        const char *str;
                     94:        size_t siz;
                     95: 
1.1.2.5   misho      96:        FTRACE(4);
                     97: 
1.1.2.12  misho      98:        str = cfg_getAttribute(&cfg, csSec, "drive_size");
1.1.2.4   misho      99:        siz = strtol(str, NULL, 0);
                    100:        if (!siz)
                    101:                return -1;
                    102: 
                    103:        env = e_malloc(siz);
                    104:        if (!env) {
                    105:                ELIBERR(elwix);
                    106:                return -1;
                    107:        }
                    108: 
1.1.2.7   misho     109:        return ub_flash_io(csSec, O_RDONLY);
1.1.2.4   misho     110: }
                    111: 
                    112: void
                    113: ub_unload()
                    114: {
1.1.2.5   misho     115:        FTRACE(4);
                    116: 
1.1.2.4   misho     117:        if (env)
                    118:                e_free(env);
                    119: }
                    120: 
1.1.2.2   misho     121: const char*
1.1.2.5   misho     122: ub_getenv(const char *csSec, const char *csName)
1.1.2.2   misho     123: {
1.1.2.10  misho     124:        char *e, *nxt;
                    125:        size_t dlen;
1.1.2.2   misho     126:        const char *str = NULL;
                    127: 
1.1.2.3   misho     128:        FTRACE(3);
                    129: 
1.1.2.12  misho     130:        str = cfg_getAttribute(&cfg, csSec, "env_size");
                    131:        if (!str) {
                    132:                str = cfg_getAttribute(&cfg, csSec, "drive_size");
                    133:                if (!str)
1.1.2.13! misho     134:                        return NULL;
1.1.2.12  misho     135:        }
1.1.2.10  misho     136:        dlen = strtol(str, NULL, 0);
                    137:        if (!dlen)
                    138:                return NULL;
                    139:        else
                    140:                dlen--;
                    141: 
                    142:        for (e = env->env_data; *e; e = nxt + 1) {
                    143:                for (nxt = e; *nxt; nxt++)
                    144:                        if (nxt >= env->env_data + dlen) {
                    145:                                printf("Error:: environment not terminated\n");
                    146:                                return NULL;
                    147:                        }
                    148: 
                    149:                str = ub_envmatch(csName, e);
                    150:                if (str)
                    151:                        break;
                    152:        }
                    153: 
1.1.2.2   misho     154:        return str;
                    155: }
                    156: 
                    157: int
1.1.2.5   misho     158: ub_setenv(const char *csSec, const char *csName, const char *csValue)
1.1.2.2   misho     159: {
1.1.2.11  misho     160:        char *e, *nxt;
                    161:        size_t dlen, len;
                    162:        const char *str, *old = NULL;
                    163: 
1.1.2.3   misho     164:        FTRACE(3);
                    165: 
1.1.2.12  misho     166:        str = cfg_getAttribute(&cfg, csSec, "env_size");
                    167:        if (!str) {
                    168:                str = cfg_getAttribute(&cfg, csSec, "drive_size");
                    169:                if (!str)
                    170:                        return -1;
                    171:        }
1.1.2.11  misho     172:        dlen = strtol(str, NULL, 0);
                    173:        if (!dlen)
                    174:                return -1;
                    175:        else
                    176:                dlen--;
                    177: 
                    178:        for (e = env->env_data; *e; e = nxt + 1) {
                    179:                for (nxt = e; *nxt; nxt++)
                    180:                        if (nxt >= env->env_data + dlen) {
                    181:                                printf("Error:: environment not terminated\n");
                    182:                                return -1;
                    183:                        }
                    184: 
                    185:                old = ub_envmatch(csName, e);
                    186:                if (old)
                    187:                        break;
                    188:        }
                    189: 
                    190:        /* Delete any existing definition */
                    191:        if (old) {
                    192:                if (!*++nxt)
                    193:                        *e = 0;
                    194:                else
                    195:                        while (42) {
                    196:                                *e = *nxt++;
                    197:                                if (!*e && !*nxt)
                    198:                                        break;
                    199:                                e++;
                    200:                        }
                    201:                *++e = 0;
                    202:        }
                    203: 
                    204:        if (csValue) {
                    205:                /* Append new definition at the end */
                    206:                for (e = env->env_data; *e || *(e + 1); e++);
                    207:                if (e > env->env_data)
                    208:                        e++;
                    209:                /* "name" + "=" + "val" +"\0\0"  > u-boot-env size */
                    210:                len = strlen(csName) + 2; /* add '=' for first arg, ' ' for all others */
                    211:                len += strlen(csValue) + 1;
                    212:                if (len > env->env_data + dlen - e) {
                    213:                        printf("Error:: Environment overflow!\n");
                    214:                        return -1;
                    215:                }
                    216: 
                    217:                /* av pair */
                    218:                while ((*e = *csName++))
                    219:                        e++;
                    220:                *e = '=';
                    221:                while ((*++e = *csValue++));
                    222: 
                    223:                /* end is marked with double '\0' */
                    224:                *++e = 0;
                    225:        }
                    226: 
                    227:        if (ub_flash_io(csSec, O_RDWR)) {
                    228:                printf("Error:: Can't write environment to flash!\n");
                    229:                return -1;
                    230:        }
                    231: 
1.1.2.2   misho     232:        return 0;
                    233: }
1.1.2.9   misho     234: 
                    235: int
                    236: ub_env(const char *csSec)
                    237: {
                    238:        char *e, *nxt;
                    239:        size_t dlen;
                    240:        const char *str;
                    241: 
                    242:        FTRACE(3);
                    243: 
1.1.2.12  misho     244:        str = cfg_getAttribute(&cfg, csSec, "env_size");
                    245:        if (!str) {
                    246:                str = cfg_getAttribute(&cfg, csSec, "drive_size");
                    247:                if (!str)
                    248:                        return -1;
                    249:        }
1.1.2.9   misho     250:        dlen = strtol(str, NULL, 0);
                    251:        if (!dlen)
                    252:                return -1;
                    253:        else
                    254:                dlen--;
                    255: 
                    256:        for (e = env->env_data; *e; e = nxt + 1) {
                    257:                for (nxt = e; *nxt; nxt++)
                    258:                        if (nxt >= env->env_data + dlen) {
                    259:                                printf("Error:: environment not terminated\n");
                    260:                                return -1;
                    261:                        }
                    262: 
                    263:                printf("%s\n", e);
                    264:        }
                    265: 
                    266:        return 0;
                    267: }

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