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

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: 
                      7: 
1.1.2.3   misho       8: static int
1.1.2.5   misho       9: ub_flash_io(const char *csSec, int mode)
1.1.2.3   misho      10: {
1.1.2.7   misho      11:        int f, l, rlen;
1.1.2.5   misho      12:        const char *str;
1.1.2.6   misho      13:        size_t siz;
1.1.2.7   misho      14:        ait_val_t v;
1.1.2.3   misho      15: 
                     16:        FTRACE(4);
                     17: 
1.1.2.6   misho      18:        str = cfg_getAttribute(&cfg, csSec, "size");
                     19:        siz = strtol(str, NULL, 0);
                     20:        if (!siz)
                     21:                return -1;
1.1.2.5   misho      22:        str = cfg_getAttribute(&cfg, csSec, "drive");
                     23:        if (!str) {
                     24:                printf("Error:: drive not found!\n");
                     25:                return -1;
                     26:        }
                     27: 
1.1.2.7   misho      28:        cfg_loadAttribute(&cfg, "ube", "lock", &v, UBE_LOCK);
1.1.2.8   misho      29:        l = open(AIT_GET_STR(&v), O_CREAT | O_EXCL | O_WRONLY, 0644);
1.1.2.7   misho      30:        if (l == -1) {
1.1.2.8   misho      31:                printf("Error:: Locked u-boot-env map %s\n", AIT_GET_STR(&v));
1.1.2.7   misho      32:                AIT_FREE_VAL(&v);
                     33:                return -1;
                     34:        }
                     35: 
1.1.2.5   misho      36:        f = open(str, mode);
1.1.2.3   misho      37:        if (f == -1) {
1.1.2.5   misho      38:                printf("Error:: Can't access u-boot-env device %s\n", str);
1.1.2.7   misho      39:                close(l);
                     40:                unlink(AIT_GET_STR(&v));
                     41:                AIT_FREE_VAL(&v);
1.1.2.3   misho      42:                return -1;
                     43:        }
                     44: 
                     45:        if (mode & O_RDWR) {
1.1.2.6   misho      46:                rlen = write(f, env, siz);
                     47:                if (rlen != siz)
                     48:                        printf("Error:: written %d bytes != %d\n", rlen, siz);
                     49:                else
                     50:                        VERB(3) printf("Written %d bytes\n", rlen);
                     51:                lseek(f, 0, SEEK_SET);
1.1.2.3   misho      52:        }
                     53: 
1.1.2.6   misho      54:        rlen = read(f, env, siz);
                     55:        if (rlen != siz)
                     56:                printf("Error:: readed %d bytes != %d\n", rlen, siz);
                     57:        else
                     58:                VERB(3) printf("Readed %d bytes\n", rlen);
                     59: 
1.1.2.3   misho      60:        close(f);
1.1.2.7   misho      61:        close(l);
                     62:        unlink(AIT_GET_STR(&v));
                     63:        AIT_FREE_VAL(&v);
                     64:        return 0;
1.1.2.3   misho      65: }
                     66: 
1.1.2.10! misho      67: static inline const char *
        !            68: ub_envmatch(const char *csName, const char *e)
        !            69: {
        !            70:        const char *str = NULL;
        !            71: 
        !            72:        while (*csName == *e++)
        !            73:                if (*csName++ == '=')
        !            74:                        return e;
        !            75:        if (!*csName && *(e - 1) == '=')
        !            76:                return e;
        !            77: 
        !            78:        return NULL;
        !            79: }
        !            80: 
1.1.2.4   misho      81: int
                     82: ub_load(const char *csSec)
                     83: {
                     84:        const char *str;
                     85:        size_t siz;
                     86: 
1.1.2.5   misho      87:        FTRACE(4);
                     88: 
1.1.2.4   misho      89:        str = cfg_getAttribute(&cfg, csSec, "size");
                     90:        siz = strtol(str, NULL, 0);
                     91:        if (!siz)
                     92:                return -1;
                     93: 
                     94:        env = e_malloc(siz);
                     95:        if (!env) {
                     96:                ELIBERR(elwix);
                     97:                return -1;
                     98:        }
                     99: 
1.1.2.7   misho     100:        return ub_flash_io(csSec, O_RDONLY);
1.1.2.4   misho     101: }
                    102: 
                    103: void
                    104: ub_unload()
                    105: {
1.1.2.5   misho     106:        FTRACE(4);
                    107: 
1.1.2.4   misho     108:        if (env)
                    109:                e_free(env);
                    110: }
                    111: 
1.1.2.2   misho     112: const char*
1.1.2.5   misho     113: ub_getenv(const char *csSec, const char *csName)
1.1.2.2   misho     114: {
1.1.2.10! misho     115:        char *e, *nxt;
        !           116:        size_t dlen;
1.1.2.2   misho     117:        const char *str = NULL;
                    118: 
1.1.2.10! misho     119: 
1.1.2.3   misho     120:        FTRACE(3);
                    121: 
1.1.2.10! misho     122:        str = cfg_getAttribute(&cfg, csSec, "size");
        !           123:        dlen = strtol(str, NULL, 0);
        !           124:        if (!dlen)
        !           125:                return NULL;
        !           126:        else
        !           127:                dlen--;
        !           128: 
        !           129:        for (e = env->env_data; *e; e = nxt + 1) {
        !           130:                for (nxt = e; *nxt; nxt++)
        !           131:                        if (nxt >= env->env_data + dlen) {
        !           132:                                printf("Error:: environment not terminated\n");
        !           133:                                return NULL;
        !           134:                        }
        !           135: 
        !           136:                str = ub_envmatch(csName, e);
        !           137:                if (str)
        !           138:                        break;
        !           139:        }
        !           140: 
1.1.2.2   misho     141:        return str;
                    142: }
                    143: 
                    144: int
1.1.2.5   misho     145: ub_setenv(const char *csSec, const char *csName, const char *csValue)
1.1.2.2   misho     146: {
1.1.2.3   misho     147:        FTRACE(3);
                    148: 
1.1.2.2   misho     149:        return 0;
                    150: }
1.1.2.9   misho     151: 
                    152: int
                    153: ub_env(const char *csSec)
                    154: {
                    155:        char *e, *nxt;
                    156:        size_t dlen;
                    157:        const char *str;
                    158: 
                    159:        FTRACE(3);
                    160: 
                    161:        str = cfg_getAttribute(&cfg, csSec, "size");
                    162:        dlen = strtol(str, NULL, 0);
                    163:        if (!dlen)
                    164:                return -1;
                    165:        else
                    166:                dlen--;
                    167: 
                    168:        for (e = env->env_data; *e; e = nxt + 1) {
                    169:                for (nxt = e; *nxt; nxt++)
                    170:                        if (nxt >= env->env_data + dlen) {
                    171:                                printf("Error:: environment not terminated\n");
                    172:                                return -1;
                    173:                        }
                    174: 
                    175:                printf("%s\n", e);
                    176:        }
                    177: 
                    178:        return 0;
                    179: }

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