From 1da3acc4b7215e76d459905c3e74675ffa679ce0 Mon Sep 17 00:00:00 2001 From: Leo C Date: Sat, 16 Aug 2014 21:27:31 +0200 Subject: env.c cleanup --- avr/env.c | 654 +++++++++++++++++++++++--------------------------------------- avr/env.h | 1 - 2 files changed, 240 insertions(+), 415 deletions(-) diff --git a/avr/env.c b/avr/env.c index e16dc84..7cf33ac 100644 --- a/avr/env.c +++ b/avr/env.c @@ -16,17 +16,15 @@ #define ENV_SIZE (CONFIG_ENV_SIZE - sizeof(uint16_t) -1) #define ACTIVE_FLAG 1 #define OBSOLETE_FLAG 0 - - -#define DELIM "\0" - - #define ENV_GET_VAL (1<<0) /* * Default Environment */ + +#define DELIM "\0" + const FLASH char default_env[] = { "bootdelay=" "3" DELIM "bootcmd=" "reset; loadf; go $(startaddr)" DELIM @@ -35,9 +33,7 @@ const FLASH char default_env[] = { DELIM }; - - - +/* EEPROM storage */ typedef struct environment_s { uint16_t crc; /* CRC16 over data bytes */ uint8_t flags; /* active/obsolete flags */ @@ -45,7 +41,7 @@ typedef struct environment_s { } env_t; - +/* */ typedef struct env_item_s { #define EF_N_EEP (1<<7) /* Variable name is in EEPROM */ #define EF_V_EEP (1<<6) /* Variable value is in EEPROM */ @@ -61,12 +57,14 @@ typedef struct env_item_s { } val; } env_item_t; + static uint8_t env_valid; static env_item_t env_list[CONFIG_ENVVAR_MAX]; static int entrycount; -static char env_get_char(uint16_t index) +static +char env_get_char(uint16_t index) { unsigned int off = CONFIG_ENV_OFFSET; @@ -78,7 +76,8 @@ static char env_get_char(uint16_t index) } -uint16_t ee_name_get(char *buf, env_item_t *ep) +static +uint16_t varname_get(char *buf, env_item_t *ep) { int i = 0; char c; @@ -103,7 +102,8 @@ uint16_t ee_name_get(char *buf, env_item_t *ep) } -uint16_t ee_val_get(char *buf, uint16_t index, int len) +static +uint16_t varval_get(char *buf, uint16_t index, int len) { int i = 0; char c; @@ -126,7 +126,7 @@ int comp_env_key_item(const void *key, const void *item) char buf[CONFIG_SYS_ENV_NAMELEN+1]; env_item_t *ep = (env_item_t *) item; - ee_name_get(buf, ep); + varname_get(buf, ep); return strcmp((char *) key, buf); } @@ -140,270 +140,183 @@ int comp_env_items(const void *m1, const void *m2) env_item_t *ep1 = (env_item_t *) m1; env_item_t *ep2 = (env_item_t *) m2; - ee_name_get(b1, ep1); - ee_name_get(b2, ep2); + varname_get(b1, ep1); + varname_get(b2, ep2); return strcmp(b1, b2); } -#if 0 -env_item_t * ee_entry_get(const char *s, env_item_t *ep, uint_fast8_t flag) + +static +int envlist_import(void) { char name[CONFIG_SYS_ENV_NAMELEN+1]; uint16_t idx = 0; int nlen; + env_item_t e; -//printf_P(PSTR("*** ee_entry_get: >>>>>>>>> ENTER <<<<<<<<<\n")); - - - while ((nlen = ee_name_get(name, idx)) != 0 && idx < CONFIG_ENV_SIZE) { - -//printf_P(PSTR("** idx: %d, name: '%s', s: '%s', nlen: %d, cpres:%d\n"), -// idx, name, s, nlen, strncmp(name, s, nlen)); + e.flags = EF_N_EEP | EF_V_EEP; + e.name.eep = idx; + while ((nlen = varname_get(name, &e)) != 0 && idx < ENV_SIZE) { - if (strncmp(name, s, nlen) == 0) - break; - - idx += nlen + 1; - while (env_get_char(idx++) != 0) - ; - } + if (entrycount <= CONFIG_ENVVAR_MAX) { + e.val.eep = idx + nlen + 1; - if (nlen) { - char *vp; - ep->flags = EF_N_EEP; - ep->name.eep = idx; - if (flag & ENV_GET_VAL) { - vp = xmalloc(CONFIG_SYS_CBSIZE); - nlen = ee_val_get(vp, idx + nlen + 1, CONFIG_SYS_CBSIZE); - ep->val = xrealloc(vp, nlen + 1); - } else - ep->val = NULL; - + env_list[entrycount++] = e; -//printf_P(PSTR("*** ee_entry_get: >>>>>> LEAVE 0x%.4x <<<<<\n"), -// (unsigned int) ep); - return ep; + idx += nlen + 1; + while (env_get_char(idx++) != 0 && idx < ENV_SIZE) + ; + e.name.eep = idx; + } else { + debug("** Too many environment variables!\n"); + break; + } } + qsort(env_list, entrycount, sizeof(env_item_t), comp_env_items); -//printf_P(PSTR("*** ee_entry_get: >>>>>> LEAVE <<<<<\n")); - return NULL; -} -#endif + return 0; +} -#if 0 -static char p_env_name_buf[CONFIG_SYS_ENV_NAMELEN+1]; -static char *dbg_p_env_name(env_item_t *p) +static +int set_default_env(void) { - if (p->flags & EF_N_EEP) { - if (ee_name_get(p_env_name_buf, p) != 0) - return p_env_name_buf; - else - return ""; + char buf[56]; + uint16_t eep = CONFIG_ENV_OFFSET + offsetof(env_t, data); + unsigned int len = ENV_SIZE; + unsigned int i, src = 0; + char c = 0xff, c0 = c; + + if (env_valid == 1) { + eep = CONFIG_ENV_OFFSET + offsetof(env_t, data) + CONFIG_ENV_SIZE; } - return ""; -} -#endif -int env_item_print(env_item_t *ep) -{ - char buf[CONFIG_SYS_ENV_NAMELEN+1]; - int len; - env_item_t e = *ep; - - ee_name_get(buf, ep); - len = printf_P(PSTR("%s="), buf); - - if (e.val.ram != NULL) { - while (1) { - char c; - if (e.flags & EF_V_EEP) - c = env_get_char(e.val.eep++); - else - c = *e.val.ram++; - - if (c != '\0') { - putchar(c); - len++; - } else - break; + while (len) { + memcpy_P(buf, default_env+src, sizeof(buf)); + for (i=0; i < (len < sizeof(buf) ? len : sizeof(buf)) && + !(c == 0 && c0 == 0); + i++) { + c0 = c; c = buf[i]; } + eeprom_update_block(buf, (char *) eep, i); + if (c == 0 && c0 == 0) + len = 0; + if (len > sizeof(buf)) + len -= sizeof(buf); + src += sizeof(buf); + eep += sizeof(buf); } - putchar('\n'); - len ++; - return len; -} + return 0; +} -int env_item_save(env_item_t *ep, uint16_t offset, int space_left) +static +uint16_t env_crc(uint16_t data_offset) { - char buf[CONFIG_SYS_ENV_NAMELEN+1]; - int len; - env_item_t e = *ep; - -debug("-- env_item_save(%04x, %04x, %d)\n", - ep, offset, space_left); - - len = ee_name_get(buf, ep); - if (len == 0) - return 0; - buf[len++] = '='; - space_left -= len; + uint16_t crc; + uint16_t i; + char c, c0; -if ((unsigned) len < sizeof(buf)) - buf[len] = '\0'; /* terminate for debugging */ -debug(" len: %d, buf: '%s', space_left: %d\n", len, buf, space_left); - - if (space_left <= 0) - return 0; - - eeprom_update_block(buf, (uint8_t *) offset, len); - offset += len; - - if (e.val.ram != NULL) { - char c; - do { - if (e.flags & EF_V_EEP) - c = env_get_char(e.val.eep++); - else - c = *e.val.ram++; - - eeprom_update_byte((uint8_t *) offset, c); - offset++; - space_left--; - len++; - } while ((c != '\0') && space_left ); + crc = 0xffff; + c = 0xff; + for (i = 0; !(c == 0 && c0 == 0) && i < ENV_SIZE; i++) + { + c0 = c; + c = eeprom_read_byte((const uint8_t *) data_offset + i); + crc = crc16(crc, c); } - return len; -} + return crc ; +} -/* - * Update - * +/** + * return valid env */ -int env_item_update(env_item_t *ep) -{ - char buf[CONFIG_SYS_ENV_NAMELEN+1]; - uint_fast8_t len; - char c; - unsigned pos = 0; - - /* get name from old loc. (eeprom or ram */ - len = ee_name_get(buf, ep); - buf[len++] = '='; - -debug("-- env_item_update(%04x)\n", ep); -if (len < sizeof(buf)) - buf[len] = '\0'; /* terminate for debugging */ -debug(" len: %d, buf: '%s'\n", len, buf); - - /* search this name in new eeprom env */ - /* TODO: eliminate this ugly hack */ - uint8_t save_env_valid = env_valid; - env_valid = (env_valid == 2) ? 1 : 2; - -debug(" len: %d, buf: '%s', env_valid: %d\n", len, buf, env_valid); - - while ((c = env_get_char(pos)) != '\0' && pos < (ENV_SIZE - len)) { - uint_fast8_t i = 0; - -debug(" while: c: %02x, pos: %d\n ", c, pos); - - while (c == buf[i] && i <= len) { - -debug("%02x ", c); - - ++i; - c = env_get_char(pos + i); - } - -debug("\n c: %02x, i: %d, pos: %d\n", c, i, pos); - - if (i == len) { - if ((ep->flags & EF_N_EEP) == 0) - free(ep->name.ram); - ep->name.eep = pos; - if ((ep->flags & EF_V_EEP) == 0) - free(ep->val.ram); - ep->val.eep = pos + i; - ep->flags &= ~EF_DIRTY; - ep->flags |= EF_N_EEP | EF_V_EEP; - - /* TODO: */ - env_valid = save_env_valid; - return 0; - } - pos += i + 1; - while (((c = env_get_char(pos++)) != '\0') && pos < (ENV_SIZE - len)) - ; +static +int env_check_valid(void) +{ + const uint16_t offset[2] = {CONFIG_ENV_OFFSET, + CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE}; + uint_fast8_t flags[2], crc_ok[2]; + int rc; + + /* read FLAGS */ + flags[0] = eeprom_read_byte ((uint8_t *) offset[0] + + offsetof(env_t, flags)); + flags[1] = eeprom_read_byte ((uint8_t *) offset[1] + + offsetof(env_t, flags)); + + /* check CRC */ + crc_ok[0] = ( + eeprom_read_word((uint16_t *) offset[0] + + offsetof(env_t, crc)) + == env_crc(offset[0] + offsetof(env_t, data)) + ); + crc_ok[1] = ( + eeprom_read_word((uint16_t *) offset[1] + + offsetof(env_t, crc)) + == env_crc(offset[1] + offsetof(env_t, data)) + ); -debug("\n c: %02x, i: %d, pos: %d\n", c, i, pos); + if (!crc_ok[0] && !crc_ok[1]) { + rc = 0; + } else if (crc_ok[0] && !crc_ok[1]) { + rc = 1; + } else if (!crc_ok[0] && crc_ok[1]) { + rc = 2; + } else { + /* both ok - check serial */ +#if 1 + if (flags[1] == ACTIVE_FLAG && flags[0] != ACTIVE_FLAG) + rc = 2; + else if (flags[1] == OBSOLETE_FLAG && flags[0] == 0xFF) + rc = 2; + else + rc = 1; +#else + if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG) + rc = 1; + else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG) + rc = 2; + else if (flags[0] == 0xFF && flags[1] == 0) + rc = 2; + else if (flags[1] == 0xFF && flags[0] == 0) + rc = 1; + else /* flags are equal - almost impossible */ + rc = 1; +#endif } - /* TODO: */ - env_valid = save_env_valid; - /* name not found */ - return -1; -} + return rc; +} -int envlist_import(void) +int env_init(void) { - char name[CONFIG_SYS_ENV_NAMELEN+1]; - uint16_t idx = 0; - int nlen; - env_item_t e; - - e.flags = EF_N_EEP | EF_V_EEP; - e.name.eep = idx; - while ((nlen = ee_name_get(name, &e)) != 0 && idx < ENV_SIZE) { - - if (entrycount <= CONFIG_ENVVAR_MAX) { - e.val.eep = idx + nlen + 1; - - env_list[entrycount++] = e; - - idx += nlen + 1; - while (env_get_char(idx++) != 0 && idx < ENV_SIZE) - ; - e.name.eep = idx; - } else { - debug("** Too many environment variables!\n"); - break; - } + env_valid = env_check_valid(); + if (env_valid == 0) { + printf_P(PSTR("*** Warning - bad CRC, " + "using default environment\n\n")); + set_default_env(); } - qsort(env_list, entrycount, sizeof(env_item_t), comp_env_items); + entrycount = 0; + envlist_import(); + return 0; +} - return 0; -} -static env_item_t *envlist_search(const char *name) +static +env_item_t *envlist_search(const char *name) { return bsearch(name, env_list, entrycount, sizeof(env_item_t), comp_env_key_item); } -#if 0 -env_item_t *envlist_insert(const char *key, env_item_t *e) -{ - const size_t size = sizeof(env_item_t); - - if (entrycount < CONFIG_ENVVAR_MAX) { - env_list[entrycount++] = *e; - qsort(env_list, entrycount, size, comp_env_items); - - return bsearch(key, env_list, entrycount, - size, comp_env_key_item); - - } else - return NULL; -} -#endif +static env_item_t *envlist_enter(env_item_t *e) { char *key = e->name.ram; @@ -450,28 +363,7 @@ env_item_t *envlist_enter(env_item_t *e) } -static env_item_t *envlist_get(const char *name, uint_fast8_t flag) -{ - env_item_t *ep; - - ep = envlist_search(name); - - if (ep != NULL && (flag & ENV_GET_VAL)) { - if (ep->flags & EF_V_EEP) { - char *vp; - uint_fast8_t len; - /* TODO: function that gets len of val, - to get rid of xrealloc */ - vp = xmalloc(CONFIG_SYS_CBSIZE); - len = ee_val_get(vp, ep->val.eep, CONFIG_SYS_CBSIZE); - ep->val.ram = xrealloc(vp, len + 1); - ep->flags &= ~EF_V_EEP; - } - } - - return ep; -} - +static int env_item_delete(env_item_t *ep) { if (entrycount == 0) @@ -488,7 +380,8 @@ int env_item_delete(env_item_t *ep) return 0; } -static int envlist_delete(const char *name) +static +int envlist_delete(const char *name) { env_item_t *ep = bsearch(name, env_list, entrycount, sizeof(env_item_t), comp_env_key_item); @@ -496,60 +389,83 @@ static int envlist_delete(const char *name) if (ep != NULL) return env_item_delete(ep); -#if 0 - dump_ram((uint8_t *) &env_list[0], entrycount * sizeof(env_item_t), - "=== env_list:"); - dump_heap(); - debug("** entrycount: %d\n", entrycount); - for (int i=0; iflags & EF_V_EEP) { + char *vp; + uint_fast8_t len; + /* TODO: function that gets len of val, + to get rid of xrealloc */ + vp = xmalloc(CONFIG_SYS_CBSIZE); + len = varval_get(vp, ep->val.eep, CONFIG_SYS_CBSIZE); + ep->val.ram = xrealloc(vp, len + 1); + ep->flags &= ~EF_V_EEP; + } + } + + return ep; +} + + char *getenv(const char *name) { env_item_t *ep; char *ret = NULL; - debug("** getenv: %s\n", name); - ep = envlist_get(name, ENV_GET_VAL); if (ep != NULL) ret = ep->val.ram; -#if 0 - dump_ram((uint8_t *) &env_list[0], entrycount * sizeof(env_item_t), - "=== env_list:"); - dump_heap(); - debug("** entrycount: %d\n", entrycount); - for (int i=0; i sizeof(buf)) - len -= sizeof(buf); - src += sizeof(buf); - eep += sizeof(buf); - } - -#if 0 - dump_eep(0, 128); -#endif - return 0; -} - - -int env_check(uint16_t offset) +static +int env_item_print(env_item_t *ep) { - uint16_t crc, stored_crc; - - /* read old CRC */ - stored_crc = eeprom_read_word( - (const uint16_t *) offset + offsetof(env_t, crc)); - crc = env_crc(offset + offsetof(env_t, data)); - - debug_cond((crc != stored_crc), - "** crc eep: 0x%.4x, crc new: 0x%.4x\n", stored_crc, crc); + char buf[CONFIG_SYS_ENV_NAMELEN+1]; + int len; + env_item_t e = *ep; - return crc == stored_crc; -} - -int _env_init(void) -{ - unsigned int off_env[2]; - uint8_t flags[2], crc_ok[2]; - - off_env[0] = CONFIG_ENV_OFFSET; - off_env[1] = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; - - for (uint_fast8_t i = 0; i < 2; i++) { - /* read FLAGS */ - flags[i] = eeprom_read_byte ((uint8_t *) off_env[i] + - offsetof(env_t, flags)); - - /* check CRC */ - crc_ok[i] = ( - eeprom_read_word((uint16_t *) off_env[i] + - offsetof(env_t, crc)) - == env_crc(off_env[i] + offsetof(env_t, data)) - ); - } - - if (!crc_ok[0] && !crc_ok[1]) { - env_valid = 0; -debug("crc1: %02x, crc2: %02x, flag1 %02x, flag2 %02x, env_valid: %d\n", - crc_ok[0], crc_ok[1], flags[0], flags[1], env_valid); - return 0; - - } else if (crc_ok[0] && !crc_ok[1]) { - env_valid = 1; - } else if (!crc_ok[0] && crc_ok[1]) { - env_valid = 2; - } else { - /* both ok - check serial */ - if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG) - env_valid = 1; - else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG) - env_valid = 2; - else if (flags[0] == 0xFF && flags[1] == 0) - env_valid = 2; - else if (flags[1] == 0xFF && flags[0] == 0) - env_valid = 1; - else /* flags are equal - almost impossible */ - env_valid = 1; - } + varname_get(buf, ep); + len = printf_P(PSTR("%s="), buf); -debug("crc1: %02x, crc2: %02x, flag1 %02x, flag2 %02x, env_valid: %d\n", - crc_ok[0], crc_ok[1], flags[0], flags[1], env_valid); - - return 0; -} - - -int env_init(void) -{ - _env_init(); - if (env_valid == 0) { - printf_P(PSTR("*** Warning - bad CRC, " - "using default environment\n\n")); - set_default_env(); + if (e.val.ram != NULL) { + while (1) { + char c; + if (e.flags & EF_V_EEP) + c = env_get_char(e.val.eep++); + else + c = *e.val.ram++; + + if (c != '\0') { + putchar(c); + len++; + } else + break; + } } - entrycount = 0; - envlist_import(); - return 0; + putchar('\n'); + len ++; + + return len; } @@ -728,7 +551,8 @@ int env_init(void) * * Returns -1 in case of error, or length of printed string */ -static int env_print(char *name) +static +int env_print(char *name) { int len = -1; @@ -747,6 +571,7 @@ static int env_print(char *name) return len; } +static int env_print_ramsize(void) { int size = 0; @@ -770,8 +595,8 @@ int env_print_ramsize(void) return size; } -int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) + +int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { int i; int rcode = 0; @@ -816,7 +641,8 @@ int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc, * * @return 0 if ok, 1 on error */ -static int _do_env_set(int flag, int argc, char * const argv[]) +static +int _do_env_set(int flag, int argc, char * const argv[]) { int i, len; char *name, *value, *s; @@ -888,6 +714,7 @@ static int _do_env_set(int flag, int argc, char * const argv[]) * @param varvalue Value to set it to * @return 0 if ok, 1 on error */ +static int setenv(const char *varname, const char *varvalue) { const char * const argv[3] = { NULL, varname, varvalue }; @@ -970,7 +797,6 @@ int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) (void) cmdtp; (void) flag; (void) argc; (void) argv; printf_P(PSTR("Saving Environment ...\n")); - return saveenv() ? 1 : 0; } diff --git a/avr/env.h b/avr/env.h index 12cd7b3..9d55273 100644 --- a/avr/env.h +++ b/avr/env.h @@ -1,7 +1,6 @@ #ifndef ENV_H #define ENV_H -int set_default_env(void); int env_init(void); char *getenv(const char *name); -- cgit v1.2.3