summaryrefslogtreecommitdiff
path: root/avr/env.c
diff options
context:
space:
mode:
Diffstat (limited to 'avr/env.c')
-rw-r--r--avr/env.c696
1 files changed, 696 insertions, 0 deletions
diff --git a/avr/env.c b/avr/env.c
new file mode 100644
index 0000000..017053c
--- /dev/null
+++ b/avr/env.c
@@ -0,0 +1,696 @@
+#include "common.h"
+#include <string.h>
+#include <stdlib.h>
+#include <avr/eeprom.h>
+
+#include "config.h"
+#include "debug.h"
+#include "xmalloc.h"
+#include "crc.h"
+#include "command.h"
+
+#include "env.h"
+
+
+#define ENV_SIZE (CONFIG_ENV_SIZE - sizeof(uint16_t) -1)
+#define ACTIVE_FLAG 1
+#define OBSOLETE_FLAG 0
+
+#define ENVLIST_DELETE (1<<0)
+
+
+/*
+ * Default Environment
+ */
+
+#define DELIM "\0"
+
+const FLASH char default_env[] = {
+ "bootdelay=" "3" DELIM
+ "bootcmd=" "reset; loadf; go ${startaddr}" DELIM
+ "baudrate=" "115200" DELIM
+ "startaddr=" "0" DELIM
+ DELIM
+};
+
+/* EEPROM storage */
+typedef struct environment_s {
+ uint16_t crc; /* CRC16 over data bytes */
+ uint8_t flags; /* active/obsolete flags */
+ char data[ENV_SIZE]; /* Environment data */
+} env_t;
+
+
+/* */
+typedef struct env_item_s {
+ char * envvar;
+} 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)
+{
+ unsigned int off = CONFIG_ENV_OFFSET;
+ char ret;
+
+ 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
+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;
+
+ if (ep1 == NULL)
+ return - strcmp_P(ep2->envvar, comp_key);
+ else
+ return strcmp(ep1->envvar, ep2->envvar);
+}
+
+
+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 {
+
+ env_item_t e;
+ e.envvar = (char *) name;
+
+ return bsearch(&e, env_list, entrycount,
+ sizeof(env_item_t), comp_env_items);
+ }
+#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
+env_item_t *envlist_enter(env_item_t *e)
+{
+ const size_t size = sizeof(env_item_t);
+ env_item_t *ep;
+
+ ep = bsearch(e, env_list, entrycount,
+ size, comp_env_items);
+
+ if (ep == NULL) {
+ if (entrycount < CONFIG_ENVVAR_MAX) {
+
+ 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 env_item_delete(env_item_t *ep)
+{
+ 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 envlist_delete(const MEMX char *name)
+{
+ env_item_t *ep = envlist_search(name);
+
+ if (ep != NULL)
+ return env_item_delete(ep);
+
+ return 1;
+}
+
+
+
+static
+int envlist_import(uint8_t flags)
+{
+ uint16_t index;
+
+ 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]);
+
+ 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;
+ }
+
+ np = (char *) xmalloc(len+1);
+ if (np == NULL) {
+ printf_P(PSTR("## Can't malloc %d bytes\n"), len+1);
+ return 1;
+ }
+ 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;
+}
+
+
+static
+uint16_t env_crc(uint16_t data_offset)
+{
+ uint16_t crc;
+ uint16_t i;
+ char c, c0;
+
+ 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 crc ;
+}
+
+
+/**
+ * return valid env
+ */
+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))
+ );
+
+ 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
+ }
+
+ return rc;
+}
+
+
+int env_init(void)
+{
+ env_valid = env_check_valid();
+ if (env_valid == 0) {
+ printf_P(PSTR("*** Warning - bad CRC, "
+ "using default environment\n\n"));
+ }
+
+ envlist_import(ENVLIST_DELETE);
+ return 0;
+}
+
+
+char *getenv(const MEMX char *name)
+{
+ env_item_t *ep;
+ char *ret = NULL;
+
+ ep = envlist_search(name);
+ if (ep != NULL)
+ ret = ep->envvar + strlen(ep->envvar) +1;
+ return ret;
+}
+
+static
+int env_item_save(env_item_t *ep, uint16_t offset, int space_left)
+{
+ int nlen, vlen;
+ char *var = ep->envvar;
+
+ nlen = strlen(var);
+ if (nlen == 0)
+ return 0;
+ vlen = strlen(var + nlen + 1);
+ if (vlen == 0)
+ return 0;
+ 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
+int saveenv(void)
+{
+ unsigned int off = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
+ unsigned int off_red = CONFIG_ENV_OFFSET;
+ unsigned int pos;
+ int len, left;
+ uint16_t crc;
+ int rc = 0;
+
+ if (env_valid == 2) {
+ off = CONFIG_ENV_OFFSET;
+ off_red = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
+ }
+
+ eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags), 0xff);
+
+ pos = off + offsetof(env_t, data);
+ left = ENV_SIZE - 1;
+ for (int i = 0 ; i < entrycount; i++) {
+ len = env_item_save(&env_list[i], pos, left);
+ if (len == 0) {
+ return 1;
+ }
+ pos += len;
+ left -= len;
+ }
+ /* terminate list */
+ 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),
+ ACTIVE_FLAG);
+
+ if (rc == 0) {
+ eeprom_update_byte((uint8_t *) off_red + offsetof(env_t, flags),
+ OBSOLETE_FLAG);
+ env_valid = (env_valid == 2) ? 1 : 2;
+ }
+
+ return rc;
+}
+
+
+static
+int env_item_print(env_item_t *ep)
+{
+ int len;
+ char *ev = ep->envvar;
+
+ len = printf_P(PSTR("%s="), ev);
+ len += printf_P(PSTR("%s\n"), ev + len);
+
+ return len;
+}
+
+
+/*
+ * Command interface: print one or all environment variables
+ *
+ * Returns -1 in case of error, or length of printed string
+ */
+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);
+
+ } else { /* print whole list */
+ len = 0;
+ for (int i = 0 ; i < entrycount; i++) {
+ len += env_item_print(&env_list[i]);
+ }
+ }
+ return len;
+}
+
+
+/**
+ * Set or delete environment variable
+ *
+ * Set a new environment variable,
+ * or replace or delete an existing one.
+ *
+ * @param flag (not used)
+ * @param argc
+ * @param argv[1] Environment variable to set or delete
+ * if no more args
+ * @param argv[2] ... Value to set it to
+ *
+ * @return 0 if ok, 1 on error
+ */
+static
+command_ret_t _do_env_set(int flag, int argc, char * const argv[])
+{
+ int i, len;
+ char *name, *value, *valp, *p;
+ env_item_t e, *ep;
+
+ (void) flag;
+ debug("Initial value for argc=%d\n", argc);
+
+ name = argv[1];
+ value = argv[2];
+
+ if (strchr(name, '=')) {
+ printf_P(PSTR("## Error: illegal character '='"
+ "in variable name \"%s\"\n"), name);
+ return CMD_RET_FAILURE;
+ }
+ 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;
+ }
+/*
+ env_id++;
+*/
+ /* Delete only ? */
+ if (argc < 3 || argv[2] == NULL) {
+ int rc = envlist_delete(name);
+ return rc ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
+ }
+
+ /*
+ * Insert / replace new value
+ */
+ for (i = 2, len += 1; i < argc; ++i)
+ len += strlen(argv[i]) + 1;
+
+ value = xmalloc(len);
+ if (value == NULL) {
+ printf_P(PSTR("## Can't malloc %d bytes\n"), len);
+ return CMD_RET_FAILURE;
+ }
+ strcpy(value, name);
+ valp = value + strlen(name) + 1;
+ for (i = 2, p = valp; i < argc; ++i) {
+ char *v = argv[i];
+
+ while ((*p++ = *v++) != '\0')
+ ;
+ *(p - 1) = ' ';
+ }
+ if (p != valp)
+ *--p = '\0';
+
+ e.envvar = value;
+ ep = envlist_enter(&e);
+ if (!ep) {
+ printf_P(PSTR("## Error inserting \"%s\" variable.\n"),
+ name);
+ return CMD_RET_FAILURE;
+ }
+
+ return CMD_RET_SUCCESS;
+}
+
+/**
+ * Set an environment variable
+ *
+ * @param varname Environment variable to set
+ * @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 };
+ int argc = 3;
+
+ if (varvalue == NULL || varvalue[0] == '\0')
+ --argc;
+
+ return (int) _do_env_set(0, argc, (char * const *)argv);
+}
+
+/**
+ * Set an environment variable to an integer value
+ *
+ * @param name Environment variable to set
+ * @param value Value to set it to
+ * @param radix Base
+ * @return 0 if ok, 1 on error
+ */
+static
+int setenv_intval(const MEMX char *name, unsigned long value, int radix)
+{
+ char buf[11];
+
+ ultoa(value, buf, radix);
+
+ return setenv(name, buf);
+}
+
+/**
+ * Set an environment variable to a decimal integer value
+ *
+ * @param name Environment variable to set
+ * @param value Value to set it to
+ * @return 0 if ok, 1 on error
+ */
+int setenv_ulong(const MEMX char *name, unsigned long value)
+{
+ return setenv_intval(name, value, 10);
+}
+
+
+/**
+ * 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);
+}
+
+
+/**
+ * Decode the integer value of an environment variable and return it.
+ *
+ * @param name Name of environemnt variable
+ * @param base Number base to use (normally 10, or 16 for hex)
+ * @param default_val Default value to return if the variable is not
+ * found
+ * @return the decoded value, or default_val if not found
+ */
+unsigned long getenv_ulong(const MEMX char *name, int base, unsigned long default_val)
+{
+ unsigned long value;
+ char *vp, *endp;
+
+ env_item_t *ep = envlist_search(name);
+
+ if (ep != NULL ) {
+ 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;
+
+ if (argc < 2)
+ return CMD_RET_USAGE;
+
+ return _do_env_set(flag, argc, 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;
+
+ printf_P(PSTR("Saving Environment ...\n"));
+ return saveenv() ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
+}
+
+
+#if defined(CONFIG_AUTO_COMPLETE)
+int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
+{
+ ENTRY *match;
+ int found, idx;
+
+ idx = 0;
+ found = 0;
+ cmdv[0] = NULL;
+
+ while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
+ int vallen = strlen(match->key) + 1;
+
+ if (found >= maxv - 2 || bufsz < vallen)
+ break;
+
+ cmdv[found++] = buf;
+ memcpy(buf, match->key, vallen);
+ buf += vallen;
+ bufsz -= vallen;
+ }
+
+ qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
+
+ if (idx)
+ cmdv[found++] = "...";
+
+ cmdv[found] = NULL;
+ return found;
+}
+#endif