X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/blobdiff_plain/35e9ec0c03a150e3fe6a3350549573e4c02be67b..7f552300815ccadd45ebb3e7f0ae72a3b2e0c4e5:/avr/env.c diff --git a/avr/env.c b/avr/env.c index 7981116..017053c 100644 --- a/avr/env.c +++ b/avr/env.c @@ -1,7 +1,6 @@ #include "common.h" #include #include - #include #include "config.h" @@ -16,7 +15,8 @@ #define ENV_SIZE (CONFIG_ENV_SIZE - sizeof(uint16_t) -1) #define ACTIVE_FLAG 1 #define OBSOLETE_FLAG 0 -#define ENV_GET_VAL (1<<0) + +#define ENVLIST_DELETE (1<<0) /* @@ -27,7 +27,7 @@ const FLASH char default_env[] = { "bootdelay=" "3" DELIM - "bootcmd=" "reset; loadf; go $(startaddr)" DELIM + "bootcmd=" "reset; loadf; go ${startaddr}" DELIM "baudrate=" "115200" DELIM "startaddr=" "0" DELIM DELIM @@ -43,18 +43,7 @@ typedef struct environment_s { /* */ 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 */ -#define EF_DIRTY (1<<0) /* Variable is new or value changed */ - uint8_t flags; - union { - uint16_t eep; - char *ram; - } name; - union { - uint16_t eep; - char *ram; - } val; + char * envvar; } env_item_t; @@ -67,160 +56,158 @@ static char env_get_char(uint16_t index) { unsigned int off = CONFIG_ENV_OFFSET; + char ret; - if (env_valid == 2) - off = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; - - return (char) eeprom_read_byte((const uint8_t *)off + index + + switch (env_valid) { + case 2: + off += CONFIG_ENV_SIZE; + case 1: + ret = (char) eeprom_read_byte((const uint8_t *)off + index + offsetof(env_t, data)); + break; + + default: + ret = default_env[index]; + } + + return ret; } +static const FLASH char *comp_key; + static -uint16_t varname_get(char *buf, env_item_t *ep) +int comp_env_items(const void *m1, const void *m2) { - int i = 0; - char c; - - if (ep->flags & EF_N_EEP) { + env_item_t *ep1 = (env_item_t *) m1; + env_item_t *ep2 = (env_item_t *) m2; - while ((c = env_get_char(ep->name.eep + i)) != '=' && - c != '\0' && i < CONFIG_SYS_ENV_NAMELEN) { + if (ep1 == NULL) + return - strcmp_P(ep2->envvar, comp_key); + else + return strcmp(ep1->envvar, ep2->envvar); +} - buf[i] = c; - i++; - } - if (i > 0 && c != '=') { - debug("** ee_name: '%s' not '=' terminated!\n", buf); - } + +env_item_t *envlist_search(const MEMX char *name) +{ +#ifdef __MEMX + if (__builtin_avr_flash_segment(name) != -1) { + comp_key = name; + return bsearch(0, env_list, entrycount, + sizeof(env_item_t), comp_env_items); } else { - strncpy(buf, ep->name.ram, CONFIG_SYS_ENV_NAMELEN); - i = strnlen(buf, CONFIG_SYS_ENV_NAMELEN); + + env_item_t e; + e.envvar = (char *) name; + + return bsearch(&e, env_list, entrycount, + sizeof(env_item_t), comp_env_items); } - buf[i] = '\0'; - return i; +#else + env_item_t e; + e.envvar = (char *) name; + + return bsearch(&e, env_list, entrycount, + sizeof(env_item_t), comp_env_items); +#endif /* __MEMX */ } static -uint16_t varval_getlen(uint16_t index) +env_item_t *envlist_enter(env_item_t *e) { - int i = 0; - char c; + const size_t size = sizeof(env_item_t); + env_item_t *ep; - while ((c = env_get_char (index + i)) != '\0') { - i++; - }; + ep = bsearch(e, env_list, entrycount, + size, comp_env_items); - return i; -} + if (ep == NULL) { + if (entrycount < CONFIG_ENVVAR_MAX) { -static -uint16_t varval_get(char *buf, uint16_t index, int len) -{ - int i = 0; - char c; - - while ((c = env_get_char (index + i)) != '\0' && i < len) { - buf[i] = c; - i++; - }; - - buf[i] = '\0'; - - /* TODO: len check */ - - return i; + env_list[entrycount++] = *e; + qsort(env_list, entrycount, size, comp_env_items); + ep = bsearch(e, env_list, entrycount, + size, comp_env_items); + } + } else { + free(ep->envvar); + ep->envvar = e->envvar; + } + + return ep; } + static -int comp_env_key_item(const void *key, const void *item) +int env_item_delete(env_item_t *ep) { - char buf[CONFIG_SYS_ENV_NAMELEN+1]; - env_item_t *ep = (env_item_t *) item; - - varname_get(buf, ep); - - return strcmp((char *) key, buf); + if (entrycount == 0) + return -1; + + free(ep->envvar); + + entrycount--; + size_t size = sizeof(env_item_t); + memmove(ep, ep + 1, (env_list - ep)*size + entrycount*size); + + return 0; } static -int comp_env_items(const void *m1, const void *m2) +int envlist_delete(const MEMX char *name) { - char b1[CONFIG_SYS_ENV_NAMELEN+1]; - char b2[CONFIG_SYS_ENV_NAMELEN+1]; - - env_item_t *ep1 = (env_item_t *) m1; - env_item_t *ep2 = (env_item_t *) m2; - - varname_get(b1, ep1); - varname_get(b2, ep2); - - return strcmp(b1, b2); + env_item_t *ep = envlist_search(name); + + if (ep != NULL) + return env_item_delete(ep); + + return 1; } + static -int envlist_import(void) +int envlist_import(uint8_t flags) { - 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 = varname_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; - } - } - qsort(env_list, entrycount, sizeof(env_item_t), comp_env_items); + uint16_t index; - return 0; -} + int len; + env_item_t e; + char *np, c; + uint_fast8_t ef; + if ((flags & ENVLIST_DELETE) != 0) + while (entrycount != 0) + env_item_delete(&env_list[entrycount-1]); -static -int set_default_env(void) -{ - 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; - } + for (index = 0; env_get_char(index) != '\0'; ) { + for (len = 0; env_get_char(index + len) != '\0'; ++len) { + if ((index + len) >= ENV_SIZE) + return -1; + } - 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]; + np = (char *) xmalloc(len+1); + if (np == NULL) { + printf_P(PSTR("## Can't malloc %d bytes\n"), len+1); + return 1; } - 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); + e.envvar = np; + ef = 0; + while ((c = env_get_char(index++)) != '\0') { + if (!ef && (c == '=')) { + *np = '\0'; + ef = 1; + } else + *np = c; + np++; + } + *np = '\0'; + envlist_enter(&e); } + return 0; } @@ -228,7 +215,7 @@ int set_default_env(void) static uint16_t env_crc(uint16_t data_offset) { - uint16_t crc; + uint16_t crc; uint16_t i; char c, c0; @@ -236,7 +223,7 @@ uint16_t env_crc(uint16_t data_offset) c = 0xff; for (i = 0; !(c == 0 && c0 == 0) && i < ENV_SIZE; i++) { - c0 = c; + c0 = c; c = eeprom_read_byte((const uint8_t *) data_offset + i); crc = crc16(crc, c); } @@ -302,7 +289,7 @@ int env_check_valid(void) rc = 1; #endif } - + return rc; } @@ -313,173 +300,46 @@ int env_init(void) if (env_valid == 0) { printf_P(PSTR("*** Warning - bad CRC, " "using default environment\n\n")); - set_default_env(); - } - entrycount = 0; - envlist_import(); - return 0; -} - - -static -env_item_t *envlist_search(const char *name) -{ - return bsearch(name, env_list, entrycount, - sizeof(env_item_t), comp_env_key_item); -} - - -static -env_item_t *envlist_enter(env_item_t *e) -{ - char *key = e->name.ram; - const size_t size = sizeof(env_item_t); - env_item_t *ep; - - ep = bsearch(key, env_list, entrycount, - size, comp_env_key_item); - - if (ep == NULL) { - if (entrycount < CONFIG_ENVVAR_MAX) { - - env_list[entrycount++] = *e; - qsort(env_list, entrycount, size, comp_env_items); - ep = bsearch(key, env_list, entrycount, - size, comp_env_key_item); - } - } else { - if ((ep->flags & EF_V_EEP) == 0) { - free(ep->val.ram); - } - ep->val.ram = e->val.ram; - } - - if (ep != NULL) { - ep->flags |= EF_DIRTY; - ep->flags &= ~EF_V_EEP; - - if ((ep->flags & EF_N_EEP) == 0) { - int nlen = strnlen(key, CONFIG_SYS_ENV_NAMELEN); - char *name = xmalloc(nlen + 1); - if (name == NULL) { - printf_P(PSTR("## Can't malloc %d bytes\n"), - nlen + 1); - return NULL; - } - strcpy(name, key); - name[nlen] = '\0'; - ep->name.ram = name; - } } - return ep; -} - - -static -int env_item_delete(env_item_t *ep) -{ - if (entrycount == 0) - return -1; - if ((ep->flags & EF_V_EEP) == 0) - free(ep->val.ram); - if ((ep->flags & EF_N_EEP) == 0) - free(ep->name.ram); - - entrycount--; - size_t size = sizeof(env_item_t); - memmove(ep, ep + 1, (env_list - ep)*size + entrycount*size); - - return 0; -} - -static -int envlist_delete(const char *name) -{ - env_item_t *ep = bsearch(name, env_list, entrycount, - sizeof(env_item_t), comp_env_key_item); - - if (ep != NULL) - return env_item_delete(ep); - - return 1; -} - - -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; - int len; - len = varval_getlen(ep->val.eep); - if (len > CONFIG_SYS_CBSIZE) - len = CONFIG_SYS_CBSIZE; - vp = xmalloc(len + 1); - if (vp) { - varval_get(vp, ep->val.eep, len + 1); - ep->val.ram = vp; - ep->flags &= ~EF_V_EEP; - } else - printf_P(PSTR("Out of memory!\n")); - } - } - - return ep; + envlist_import(ENVLIST_DELETE); + return 0; } -char *getenv(const char *name) +char *getenv(const MEMX char *name) { env_item_t *ep; char *ret = NULL; - ep = envlist_get(name, ENV_GET_VAL); + ep = envlist_search(name); if (ep != NULL) - ret = ep->val.ram; + ret = ep->envvar + strlen(ep->envvar) +1; return ret; } static int env_item_save(env_item_t *ep, uint16_t offset, int space_left) { - char buf[CONFIG_SYS_ENV_NAMELEN+1]; - int len; - env_item_t e = *ep; + int nlen, vlen; + char *var = ep->envvar; - len = varname_get(buf, ep); - if (len == 0) + nlen = strlen(var); + if (nlen == 0) return 0; - buf[len++] = '='; - space_left -= len; - - if (space_left <= 0) + vlen = strlen(var + nlen + 1); + if (vlen == 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 ); - } - return len; -} + if (nlen + vlen + 2 > space_left) + return 0; + + eeprom_update_block(var, (uint8_t *) offset, nlen); + offset += nlen; + eeprom_update_byte((uint8_t *) offset++, '='); + eeprom_update_block(var + nlen +1, (uint8_t *) offset, vlen+1); + + return nlen + vlen + 2; +} static @@ -497,7 +357,7 @@ int saveenv(void) off_red = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; } - eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags), 0xff); + eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags), 0xff); pos = off + offsetof(env_t, data); left = ENV_SIZE - 1; @@ -513,20 +373,15 @@ int saveenv(void) eeprom_update_byte((uint8_t *) pos, 0); crc = env_crc(off + offsetof(env_t, data)); eeprom_update_word((uint16_t *) off + offsetof(env_t, crc), crc); - eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags), + eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags), ACTIVE_FLAG); if (rc == 0) { - while (entrycount != 0) { - env_item_delete(&env_list[entrycount-1]); - } - eeprom_update_byte((uint8_t *) off_red + offsetof(env_t, flags), + eeprom_update_byte((uint8_t *) off_red + offsetof(env_t, flags), OBSOLETE_FLAG); env_valid = (env_valid == 2) ? 1 : 2; - - envlist_import(); } - + return rc; } @@ -534,33 +389,14 @@ int saveenv(void) static int env_item_print(env_item_t *ep) { - char buf[CONFIG_SYS_ENV_NAMELEN+1]; int len; - env_item_t e = *ep; - - varname_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; - } - } - putchar('\n'); - len ++; + char *ev = ep->envvar; + + len = printf_P(PSTR("%s="), ev); + len += printf_P(PSTR("%s\n"), ev + len); return len; -} +} /* @@ -568,13 +404,13 @@ int env_item_print(env_item_t *ep) * * Returns -1 in case of error, or length of printed string */ -static -int env_print(char *name) +static +int env_print(const MEMX char *name) { int len = -1; if (name) { /* print a single name */ - + env_item_t *ep = envlist_search(name); if (ep != NULL) len = env_item_print(ep); @@ -588,60 +424,6 @@ int env_print(char *name) return len; } -static -int env_print_ramsize(void) -{ - int size = 0; - uint8_t name_cnt = 0; - uint8_t val_cnt = 0; - - for (int i = 0 ; i < entrycount; i++) { - if ((env_list[i].flags & EF_N_EEP) == 0 && - (env_list[i].name.ram != NULL)) { - name_cnt++; - size += strlen(env_list[i].name.ram) + 3; - } - if ((env_list[i].flags & EF_V_EEP) == 0 && - (env_list[i].val.ram != NULL)) { - val_cnt++; - size += strlen(env_list[i].val.ram) + 3; - } - } - printf_P(PSTR("%d bytes RAM used for %u names and %u values\n"), - size, name_cnt, val_cnt); - return size; -} - - -command_ret_t do_env_print(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) -{ - command_ret_t rc = CMD_RET_SUCCESS; - - (void) cmdtp; (void) flag; - - if (argc == 1) { - /* print all env vars */ - int size = env_print(NULL); - if (size < 0) - return CMD_RET_FAILURE; - printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"), - size, ENV_SIZE); - env_print_ramsize(); - return CMD_RET_SUCCESS; - } - - /* print selected env vars */ - for (int i = 1; i < argc; ++i) { - int rc = env_print(argv[i]); - if (rc < 0) { - printf_P(PSTR("## Error: \"%s\" not defined\n"), argv[i]); - rc = CMD_RET_FAILURE; - } - } - - return rc; -} - /** * Set or delete environment variable @@ -661,7 +443,7 @@ static command_ret_t _do_env_set(int flag, int argc, char * const argv[]) { int i, len; - char *name, *value, *s; + char *name, *value, *valp, *p; env_item_t e, *ep; (void) flag; @@ -675,7 +457,8 @@ command_ret_t _do_env_set(int flag, int argc, char * const argv[]) "in variable name \"%s\"\n"), name); return CMD_RET_FAILURE; } - if (strlen(name) > CONFIG_SYS_ENV_NAMELEN) { + len = strlen(name); + if (len > CONFIG_SYS_ENV_NAMELEN) { printf_P(PSTR("## Error: Variable name \"%s\" too long. " "(max %d characters)\n"), name, CONFIG_SYS_ENV_NAMELEN); return CMD_RET_FAILURE; @@ -692,7 +475,7 @@ command_ret_t _do_env_set(int flag, int argc, char * const argv[]) /* * Insert / replace new value */ - for (i = 2, len = 0; i < argc; ++i) + for (i = 2, len += 1; i < argc; ++i) len += strlen(argv[i]) + 1; value = xmalloc(len); @@ -700,19 +483,19 @@ command_ret_t _do_env_set(int flag, int argc, char * const argv[]) printf_P(PSTR("## Can't malloc %d bytes\n"), len); return CMD_RET_FAILURE; } - for (i = 2, s = value; i < argc; ++i) { + strcpy(value, name); + valp = value + strlen(name) + 1; + for (i = 2, p = valp; i < argc; ++i) { char *v = argv[i]; - while ((*s++ = *v++) != '\0') + while ((*p++ = *v++) != '\0') ; - *(s - 1) = ' '; + *(p - 1) = ' '; } - if (s != value) - *--s = '\0'; + if (p != valp) + *--p = '\0'; - e.flags = EF_DIRTY; - e.name.ram = name; - e.val.ram = value; + e.envvar = value; ep = envlist_enter(&e); if (!ep) { printf_P(PSTR("## Error inserting \"%s\" variable.\n"), @@ -738,41 +521,51 @@ int setenv(const char *varname, const char *varvalue) if (varvalue == NULL || varvalue[0] == '\0') --argc; - + return (int) _do_env_set(0, argc, (char * const *)argv); } -#if 0 /** * Set an environment variable to an integer value * - * @param varname Environment variable to set - * @param value Value to set it to + * @param name Environment variable to set + * @param value Value to set it to + * @param radix Base * @return 0 if ok, 1 on error */ -int setenv_ulong(const char *varname, unsigned long value) +static +int setenv_intval(const MEMX char *name, unsigned long value, int radix) { - /* TODO: this should be unsigned */ - char *str = simple_itoa(value); + char buf[11]; - return setenv(varname, str); -} -#endif + ultoa(value, buf, radix); + return setenv(name, buf); +} /** - * Set an environment variable to an value in hex + * Set an environment variable to a decimal integer value * - * @param varname Environment variable to set - * @param value Value to set it to + * @param name Environment variable to set + * @param value Value to set it to * @return 0 if ok, 1 on error */ -int setenv_hex(const char *varname, unsigned long value) +int setenv_ulong(const MEMX char *name, unsigned long value) { - char str[sizeof(unsigned long) *2 + 1]; + return setenv_intval(name, value, 10); +} - sprintf_P(str, PSTR("%lx"), value); - return setenv(varname, str); + +/** + * Set an environment variable to a value in hex + * + * @param name Environment variable to set + * @param value Value to set it to + * @return 0 if ok, 1 on error + */ +int setenv_hex(const MEMX char *name, unsigned long value) +{ + return setenv_intval(name, value, 16); } @@ -785,31 +578,53 @@ int setenv_hex(const char *varname, unsigned long value) * found * @return the decoded value, or default_val if not found */ -unsigned long getenv_ulong(const char *name, int base, unsigned long default_val) +unsigned long getenv_ulong(const MEMX char *name, int base, unsigned long default_val) { - char buf[16]; unsigned long value; char *vp, *endp; env_item_t *ep = envlist_search(name); if (ep != NULL ) { - if (ep->flags & EF_V_EEP) { - varval_get(buf, ep->val.eep, sizeof(buf)); - vp = buf; - } else - vp = ep->val.ram; - if (vp != NULL) { - value = strtoul(vp, &endp, base); - if (endp != vp) - return value; - } + vp = ep->envvar + strlen(ep->envvar) +1; + value = strtoul(vp, &endp, base); + if (endp != vp) + return value; } return default_val; } +command_ret_t do_env_print(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + command_ret_t rc = CMD_RET_SUCCESS; + + (void) cmdtp; (void) flag; + + if (argc == 1) { + /* print all env vars */ + int size = env_print(NULL); + if (size < 0) + return CMD_RET_FAILURE; + printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"), + size, ENV_SIZE); + return CMD_RET_SUCCESS; + } + + /* print selected env vars */ + for (int i = 1; i < argc; ++i) { + int rc = env_print(argv[i]); + if (rc < 0) { + printf_P(PSTR("## Error: \"%s\" not defined\n"), argv[i]); + rc = CMD_RET_FAILURE; + } + } + + return rc; +} + + command_ret_t do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { (void) cmdtp; @@ -821,6 +636,24 @@ command_ret_t do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv } +command_ret_t do_env_default(cmd_tbl_t *cmdtp, int flag, + int argc, char * const argv[]) +{ + (void) cmdtp; (void) flag; (void) argc; (void) argv; + + uint8_t tmp = env_valid; + env_valid = 0; + + /* Reset the whole environment */ + printf_P(PSTR("## Resetting to default environment\n")); + envlist_import(ENVLIST_DELETE); + + env_valid = tmp; + + return CMD_RET_SUCCESS; +} + + command_ret_t do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { (void) cmdtp; (void) flag; (void) argc; (void) argv; @@ -829,7 +662,8 @@ command_ret_t do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const arg return saveenv() ? CMD_RET_FAILURE : CMD_RET_SUCCESS; } -#if defined(CONFIG_AUTO_COMPLETE) + +#if defined(CONFIG_AUTO_COMPLETE) int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf) { ENTRY *match;