-#include "common.h"
-#include <string.h>
-#include <stdlib.h>
+/*
+ * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ */
+#include "env.h"
#include <avr/eeprom.h>
#include "config.h"
#include "debug.h"
-#include "xmalloc.h"
#include "crc.h"
-#include "command.h"
-
-#include "env.h"
-
-
-
-#define DELIM "\0"
-#define ENV_SIZE CONFIG_ENV_SIZE-2
+#include "getopt-min.h"
+#define DEBUG_ENV 0 /* set to 1 to debug */
-#define ENV_GET_VAL (1<<0)
+#define debug_env(fmt, args...) \
+ debug_cond(DEBUG_ENV, fmt, ##args)
/*
- * Debugging
- *
- * TODO: move elsewhere
+ * Default Environment
*/
-#include <ctype.h>
-
-static void print_blanks(uint_fast8_t count)
-{
- while(count--)
- putchar(' ');
-}
-
-
-void dump_ram(const uint8_t *startaddr, int len, char *title)
-{
- uint8_t llen = 16;
- uint8_t pre = (size_t) startaddr % 16;
- const uint8_t *addr = (uint8_t *) ((size_t) startaddr & ~0x0f);
- len += pre;
- uint8_t i;
-
- if (title && *title)
- printf_P(PSTR("%s\n"),title);
-
- while (len) {
- if (len < 16)
- llen = len;
-
- printf_P(PSTR(" %.4x:"), (size_t) addr);
- print_blanks(3 * pre);
- for (i = pre; i < llen; i++)
- printf_P(PSTR(" %.2x"), addr[i]);
- print_blanks(3 * (16 - i + 1) + pre);
- for (i = pre; i < llen; i++)
- printf_P(PSTR("%c"), isprint(addr[i]) ? addr[i] : '.');
- putchar('\n');
-
- pre = 0;
- addr += 16;
- len -= llen;
- }
-}
-
-void dump_heap(void)
-{
- extern unsigned int __brkval;
-
- dump_ram((uint8_t *) __malloc_heap_start,
- __brkval - (unsigned int) __malloc_heap_start,
- "=== Heap:");
-}
-
-/* TODO: combine with dump_ram() */
-void dump_eep(const uint8_t *addr, int len)
-{
- int i;
- uint8_t buf[16];
-
- printf_P(PSTR("eeprom dump:"));
- while (len) {
- printf_P(PSTR("\n 0x%.4x:"), (unsigned int) addr);
- for (i = 0; i<16; i++)
- buf[i] = eeprom_read_byte(addr + i);
- for (i = 0; i<16; i++)
- printf_P(PSTR(" %.2x"), buf[i]);
- printf_P(PSTR(" "));
- for (i = 0; i<16; i++)
- printf_P(PSTR("%c"), isprint(buf[i]) ? buf[i] : '.');
-
- addr += 16;
- len -= len > 16 ? 16 : len;
- }
- putchar('\n');
-}
+#define DELIM "\0"
const FLASH char default_env[] = {
- "bootdelay=" "3" DELIM
- "bootcmd=" "echo hallo" DELIM
- "baudrate=" "115200" DELIM
+ ENV_BAUDRATE "=" stringify(CONFIG_BAUDRATE) DELIM
+ ENV_BOOTDELAY "=" stringify(CONFIG_BOOTDELAY) DELIM
+ ENV_BOOTCMD "=" "pin ${pins};loadcpm3;go ${startaddress}" DELIM
+ ENV_CPM3_SYSFILE "=" CONFIG_CPM3_SYSFILE DELIM
+ ENV_PINALIAS "=" "0:PG5,1:PG4,2:PB4,3:PB5,4:PB6,5:PB7,"
+ "6:PG3,7:PG2,8:PG1,9:PG0,10:PE7" DELIM
+ "pins" "=" "2,8 low 9 high 3 2" DELIM
DELIM
};
+#define ENV_SIZE (CONFIG_ENV_SIZE - sizeof(uint16_t) - sizeof(uint8_t))
+
+#define ENVLIST_DELETE (1<<0)
+
+
+/* EEPROM storage */
typedef struct environment_s {
uint16_t crc; /* CRC16 over data bytes */
+ uint8_t flags; /* active/obsolete flags */
+#define ACTIVE_FLAG 1
+#define OBSOLETE_FLAG 0
char data[ENV_SIZE]; /* Environment data */
} env_t;
+static uint8_t env_valid;
+static char env_list[ENV_SIZE];
-typedef struct env_item_s {
-#define EF_N_EEP (1<<7)
-#define EF_V_EEP (1<<6)
-#define EF_DIRTY (1<<0)
- uint8_t flags;
- union {
- uint16_t eep;
- char *ram;
- } name;
- union {
- uint16_t eep;
- char *ram;
- } val;
-} env_item_t;
+static
+char env_get_char(uint_fast16_t index)
+{
+ unsigned int off = CONFIG_ENV_OFFSET;
+ char ret;
+
+ switch (env_valid) {
+ case 2:
+ off += CONFIG_ENV_SIZE;
+ /* FALL TROUGH */
+ case 1:
+ ret = (char) eeprom_read_byte((const uint8_t *)off + index +
+ offsetof(env_t, data));
+ break;
+ default:
+ ret = default_env[index];
+ }
-static env_item_t env_list[CONFIG_ENVVAR_MAX];
-static int entrycount;
+ return ret;
+}
+static void envlist_clear(void)
+{
+ memset(env_list, 0, sizeof env_list);
+}
-static char env_get_char(uint16_t index)
+static
+int env_item_delete(char *ep)
{
- unsigned int off = CONFIG_ENV_OFFSET;
+ char *next = ep + strlen(ep) + 1;
+ char *end = next;
+ while (*end++ != 0)
+ while (*end++ != 0);
- return (char) eeprom_read_byte((const uint8_t *)off + index +
- offsetof(env_t, data));
-}
+ memmove(ep, next, end - next);
+ return 0;
+}
-uint16_t ee_name_get(char *buf, env_item_t *ep)
+static int envcmp(const char *s1, const char *s2)
{
- int i = 0;
- char c;
-
- if (ep->flags & EF_N_EEP) {
+ const unsigned char *u1 = (const unsigned char *)s1;
+ const unsigned char *u2 = (const unsigned char *)s2;
+ unsigned char c1, c2;
- while ((c = env_get_char(ep->name.eep + i)) != '=' &&
- c != '\0' && i < CONFIG_SYS_ENV_NAMELEN) {
+ do {
+ c1 = *u1++; c2 = *u2++;
+ if (c1 == '=') c1 = '\0';
+ if (c2 == '=') c2 = '\0';
+ } while (c1 != 0 && c1 - c2 == 0);
- buf[i] = c;
- i++;
- }
- if (i > 0 && c != '=') {
- debug("** ee_name: '%s' not '=' terminated!\n", buf);
- }
- } else {
- strncpy(buf, ep->name.ram, CONFIG_SYS_ENV_NAMELEN);
- i = strnlen(buf, CONFIG_SYS_ENV_NAMELEN);
- }
- buf[i] = '\0';
- return i;
+ return c1 - c2;
}
-
-uint16_t ee_val_get(char *buf, uint16_t index, int len)
+static char *envlist_search(const MEMX char *name)
{
- int i = 0;
+ char key[CONFIG_SYS_ENV_NAMELEN + 1];
+ const MEMX char *np = name;
+ int keylen = 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;
-}
-static
-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);
-
- return strcmp((char *) key, buf);
+ while (keylen <= CONFIG_SYS_ENV_NAMELEN && (c = *np++) != '\0')
+ key[keylen++] = c;
+ key[keylen] = '\0';
+
+ for (char *lp = env_list; *lp != 0; ++lp) {
+
+ if (envcmp(lp, key) == 0)
+ return lp;
+
+ while (*lp != 0)
+ ++lp;
+ }
+
+ return NULL;
}
-static
-int comp_env_items(const void *m1, const void *m2)
+static char *env_item_insert_name(char *pos, const char *name, int len)
{
- 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;
-
- ee_name_get(b1, ep1);
- ee_name_get(b2, ep2);
-
- return strcmp(b1, b2);
+ char *dstp = pos + len + 1;
+ char *end = pos;
+ while (*end++ != 0)
+ while (*end++ != 0);
+
+ if (end + len >= env_list + ENV_SIZE)
+ return NULL;
+
+ memmove(dstp, pos, end - pos);
+ strcpy(pos, name);
+ memset(pos + strlen(name), '=', dstp - pos - strlen(name));
+ *(dstp - 1) = '\0';
+
+ return pos;
}
-#if 0
-env_item_t * ee_entry_get(const char *s, env_item_t *ep, uint_fast8_t flag)
+
+static char *envlist_alloc(const char *name, int len)
{
- char name[CONFIG_SYS_ENV_NAMELEN+1];
- uint16_t idx = 0;
- int nlen;
-
-//printf_P(PSTR("*** ee_entry_get: >>>>>>>>> ENTER <<<<<<<<<\n"));
+ char *lp;
+ int rc;
- 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));
-
- if (strncmp(name, s, nlen) == 0)
+ for (lp = env_list; *lp != 0; ++lp) {
+
+ rc = envcmp(lp, name);
+ if (rc >= 0)
break;
-
- idx += nlen + 1;
- while (env_get_char(idx++) != 0)
- ;
- }
-
- 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;
-
-
-//printf_P(PSTR("*** ee_entry_get: >>>>>> LEAVE 0x%.4x <<<<<\n"),
-// (unsigned int) ep);
- return ep;
+
+ /* rc < 0 : check next place */
+ while (*lp != 0)
+ ++lp;
}
-//printf_P(PSTR("*** ee_entry_get: >>>>>> LEAVE <NULL> <<<<<\n"));
- return NULL;
-}
-#endif
+ /* rc == 0 : entry found, replace */
+ if (rc == 0)
+ env_item_delete(lp);
-#if 0
-static char p_env_name_buf[CONFIG_SYS_ENV_NAMELEN+1];
+ /* rc > 0 : entry not in list, insert */
+ /* *lp == 0 : entry not in list, insert */
-static char *dbg_p_env_name(env_item_t *p)
+ lp = env_item_insert_name(lp, name, len);
+
+ return lp;
+}
+
+#if 0
+static char *env_item_insert(char *pos, const char *envstr)
{
- if (p->flags & EF_N_EEP) {
- if (ee_name_get(p_env_name_buf, p) != 0)
- return p_env_name_buf;
- else
- return "<NULL>";
- }
- return "<NO EEP_NAME>";
+ char *dstp = pos + strlen(envstr) + 1;
+ char *end = pos;
+ while (*end++ != 0)
+ while (*end++ != 0);
+
+ if (end > env_list + ENV_SIZE)
+ return NULL;
+
+ memmove(dstp, pos, end - pos);
+ strcpy(pos, envstr);
+
+ return pos;
}
-#endif
-int env_item_print(env_item_t *ep)
+static char *envlist_enter(const char *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;
- }
+ char *lp;
+ int rc;
+
+ for (lp = env_list; *lp != 0; ++lp) {
+
+ rc = envcmp(lp, ep);
+ if (rc >= 0)
+ break;
+
+ /* rc < 0 : check next place */
+ while (*lp != 0)
+ ++lp;
}
- putchar('\n');
- len ++;
- return len;
-}
+ /* rc == 0 : entry found, replace */
+ if (rc == 0)
+ env_item_delete(lp);
+ /* rc > 0 : entry not in list, insert */
+ /* *lp == 0 : entry not in list, insert */
-static env_item_t *envlist_search(const char *name)
-{
- return bsearch(name, env_list, entrycount,
- sizeof(env_item_t), comp_env_key_item);
+ lp = env_item_insert(lp, ep);
+
+ return lp;
}
-
+#endif
-env_item_t *envlist_insert(const char *key, env_item_t *e)
+static
+int envlist_delete(const MEMX char *name)
{
- const size_t size = sizeof(env_item_t);
+ char *ep = envlist_search(name);
- if (entrycount < CONFIG_ENVVAR_MAX) {
- env_list[entrycount++] = *e;
- qsort(env_list, entrycount, size, comp_env_items);
+ if (ep != NULL)
+ return env_item_delete(ep);
- return bsearch(key, env_list, entrycount,
- size, comp_env_key_item);
-
- } else
- return NULL;
-}
-
-
-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;
+ return 1;
+}
+
+static
+int envlist_import(uint8_t flags)
+{
+ if ((flags & ENVLIST_DELETE) != 0)
+ envlist_clear();
+
+ for (uint_fast16_t index = 0; env_get_char(index) != '\0'; ) {
+ char name[CONFIG_SYS_ENV_NAMELEN+1];
+ uint_fast8_t nlen = 0;
+ size_t len;
+ char ch;
+ for (len = 0; (ch = env_get_char(index + len)) != '\0'; ++len) {
+ if ((index + len) >= ENV_SIZE)
+ return -1;
+ if (nlen == 0) {
+ if (ch == '=') {
+ nlen = len;
+ name[nlen] = '\0';
+ } else {
+ if (len > CONFIG_SYS_ENV_NAMELEN)
+ return -1;
+ name[len] = ch;
+ }
}
- strcpy(name, key);
- name[nlen] = '\0';
- ep->name.ram = name;
}
- }
- return ep;
-}
+ char *ep = envlist_alloc(name, len);
+ if (ep == NULL) {
+ printf_P(PSTR("## Error inserting \"%s\" variable.\n"), name);
+ return 1;
+ }
+ index += nlen;
+ char *p = ep + nlen;
+ while ((*p++ = env_get_char(index++)) != '\0')
+ ;
+ }
+ return 0;
+}
-static env_item_t *envlist_get(const char *name, uint_fast8_t flag)
+
+static
+uint16_t env_crc(uint16_t data_offset)
{
- env_item_t *ep;
-
- ep = envlist_search(name);
+ uint16_t crc;
+ uint16_t i;
+ char c, c0;
- if (ep != NULL && (flag & ENV_GET_VAL)) {
- if (ep->flags & EF_V_EEP) {
- char *vp;
- uint_fast8_t len;
- 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;
+ 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 ;
}
-static int envlist_delete(const char *name)
+/**
+ * return valid env
+ */
+static
+int env_check_valid(void)
{
- size_t size = sizeof(env_item_t);
- env_item_t *ep = bsearch(name, env_list, entrycount,
- sizeof(env_item_t), comp_env_key_item);
- int rc = 0;
-
- if (ep != NULL) {
-
- if ((ep->flags & EF_V_EEP) == 0)
- free(ep->val.ram);
- if ((ep->flags & EF_N_EEP) == 0)
- free(ep->name.ram);
-
- entrycount--;
-printf_P(PSTR("*** envlist_delete memmove: 0x%.4x, 0x%.4x, %d\n"),
- (unsigned) ep, (unsigned) ep + size,
- (env_list - ep)*size + entrycount*size);
- memmove(ep, ep + 1, (env_list - ep)*size + entrycount*size);
-
+ 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
- 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<entrycount; i++) {
- printf_P(PSTR("** env var [%d] "), i);
- env_item_print(&env_list[i]);
- }
+ 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;
}
-
-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 1
- 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<entrycount; i++) {
- printf_P(PSTR("** env var [%d] "), i);
- env_item_print(&env_list[i]);
+int env_init(void)
+{
+ env_valid = env_check_valid();
+ if (env_valid == 0) {
+ printf_P(PSTR("*** Warning - bad CRC, "
+ "using default environment\n\n"));
}
-#endif
- return ret;
+ envlist_import(ENVLIST_DELETE);
+ return 0;
}
-int saveenv(void)
-{
- int rc = 0;
+char *getenv_str(const MEMX char *name)
+{
+ char *ep = envlist_search(name);
+ if (ep != NULL)
+ return strchr(ep, '=') + 1;
+ else
+ return NULL;
+}
-// rc = env_export(&env_new);
- if (rc)
- return rc;
+static
+int env_item_save(char *ep, uint16_t offset, int space_left)
+{
+ int len;
+ len = strlen(ep);
+ if (len == 0)
+ return 0;
+ len += 1;
+ if (len > space_left)
+ return 0;
-// rc = eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
-// off, (uchar *)&env_new, CONFIG_ENV_SIZE);
+ eeprom_update_block(ep, (uint8_t *) offset, len);
- return rc;
+ return len;
}
-/*-----------------------------------------------------------------------------*/
-
-
-int set_default_env(void)
+static
+int saveenv(void)
{
- char buf[64];
- uint16_t crc = 0xffff;
- uint16_t eep = CONFIG_ENV_OFFSET + offsetof(env_t, data);
- unsigned int len = CONFIG_ENV_SIZE - offsetof(env_t, data);
- unsigned int i, src = 0;
- char c = 0xff, c0 = c;
-
-printf_P(PSTR("\n\n** set_default_env()\n"));
-
- 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];
- crc = crc16(crc, c);
- }
-
- eeprom_update_block(buf, (char *) eep, i);
-/**/ printf_P(PSTR("eeprom_update_block: eep: 0x%.4x, i:%d\n"),
- (unsigned int) eep, i);
-/**/ dump_ram((uint8_t *) buf, i, "=== buf:");
-/**/ dump_eep((const uint8_t *) eep, i);
-
- if (c == 0 && c0 == 0)
- len = 0;
- if (len > sizeof(buf))
- len -= sizeof(buf);
- src += sizeof(buf);
- eep += sizeof(buf);
+ 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;
}
-
-printf_P(PSTR("** crc adr: 0x%.4x, crc: 0x%.4x\n"),
- (unsigned int) (uint16_t *) CONFIG_ENV_OFFSET + offsetof(env_t,crc), crc);
-
- eeprom_update_word((uint16_t *) CONFIG_ENV_OFFSET + offsetof(env_t,crc), crc);
-
-/**/ dump_eep(0, 128);
- return 0;
-}
+ eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags), 0xff);
-int env_check(void)
-{
- uint16_t crc, stored_crc;
- uint16_t i;
- char c, c0;
+ pos = off + offsetof(env_t, data);
+ left = ENV_SIZE - 1;
- debug("\n\n** env_check()\n");
-
+ char *ep = env_list;
+ while (*ep) {
+ len = env_item_save(ep, pos, left);
+ if (len == 0) {
+ return 1;
+ }
+ pos += len;
+ left -= len;
+ while (*ep++);
+ }
- /* read old CRC */
- stored_crc = eeprom_read_word((const uint16_t *) CONFIG_ENV_OFFSET +
- offsetof(env_t, crc));
- crc = 0xffff;
- c = 0xff;
- for (i = offsetof(env_t, data);
- !(c == 0 && c0 == 0) && i < CONFIG_ENV_SIZE;
- i++)
- {
- c0 = c;
- c = eeprom_read_byte((const uint8_t *) CONFIG_ENV_OFFSET + i);
- crc = crc16(crc, c);
+ /* 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;
}
- debug("** crc eep: 0x%.4x, crc new: 0x%.4x\n", stored_crc, crc);
-
- return crc == stored_crc;
+
+ return rc;
}
-int env_init(void)
+
+static
+int env_item_print(char *ep, bool mode)
{
- 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;
- }
+ int len = strlen(ep) + 1;
+
+ if (mode) {
+ my_puts_P(PSTR("setenv "));
+ char ch;
+ while ((ch = *ep++) != '=')
+ putchar(ch);
+ printf_P(PSTR(" '%s'\n"), ep);
+ } else {
+ puts(ep);
}
- qsort(env_list, entrycount, sizeof(env_item_t), comp_env_items);
- return 0;
+ return len;
}
*
* Returns -1 in case of error, or length of printed string
*/
-static int env_print(char *name)
+static
+int env_print(const char *name, bool mode)
{
int len = -1;
+ char *ep;
+
+ if (name != NULL) { /* print a single name */
- if (name) { /* print a single name */
-
- env_item_t *ep = envlist_search(name);
+ ep = envlist_search(name);
if (ep != NULL)
- len = env_item_print(ep);
+ len = env_item_print(ep, mode);
} else { /* print whole list */
len = 0;
- for (int i = 0 ; i < entrycount; i++) {
- len += env_item_print(&env_list[i]);
+ ep = env_list;
+ while (*ep) {
+ len += env_item_print(ep, mode);
+ while (*ep++);
}
}
return len;
}
-
-int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
- char * const argv[])
-{
- int i;
- int rcode = 0;
-
- (void) cmdtp; (void) flag;
-
- if (argc == 1) {
- /* print all env vars */
- rcode = env_print(NULL);
- if (rcode < 0)
- return 1;
- printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"),
- rcode, ENV_SIZE);
- return 0;
- }
-
- /* print selected env vars */
- for (i = 1; i < argc; ++i) {
- int rc = env_print(argv[i]);
- if (rc < 0) {
- printf_P(PSTR("## Error: \"%s\" not defined\n"), argv[i]);
- ++rcode;
- }
- }
-
- return rcode;
-}
-
-
-/*
+/**
+ * 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 int _do_env_set(int flag, int argc, char * const argv[])
+static
+command_ret_t _do_env_set(uint_fast8_t flag UNUSED, int argc, char * const argv[])
{
- int i, len;
- char *name, *value, *s;
- env_item_t e, *ep;
-
- (void) flag;
- debug("Initial value for argc=%d\n", argc);
-
- name = argv[1];
- value = argv[2];
+ char *name = argv[1];
if (strchr(name, '=')) {
printf_P(PSTR("## Error: illegal character '='"
"in variable name \"%s\"\n"), name);
- return 1;
+ return CMD_RET_FAILURE;
}
- if (strlen(name) > CONFIG_SYS_ENV_NAMELEN) {
+ size_t 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 1;
+ return CMD_RET_FAILURE;
}
-/*
- env_id++;
-*/
+
/* Delete only ? */
- if (argc < 3 || argv[2] == NULL) {
+ if (argc < 3) {
int rc = envlist_delete(name);
- return !rc;
+ return rc ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
}
- /*
- * Insert / replace new value
- */
- for (i = 2, len = 0; i < argc; ++i)
+ /* Insert / replace new value */
+
+ for (int_fast8_t i = 2; 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 1;
+ char *pos = envlist_alloc(name, len);
+ if (pos == NULL) {
+ printf_P(PSTR("## Error inserting \"%s\" variable.\n"), name);
+ return CMD_RET_FAILURE;
}
- for (i = 2, s = value; i < argc; ++i) {
- char *v = argv[i];
- while ((*s++ = *v++) != '\0')
- ;
- *(s - 1) = ' ';
- }
- if (s != value)
- *--s = '\0';
-
- e.flags = EF_DIRTY;
- e.name.ram = name;
- e.val.ram = value;
- ep = envlist_enter(&e);
- if (!ep) {
- printf_P(PSTR("## Error inserting \"%s\" variable.\n"),
- name);
- return 1;
+ char *vp = pos + strlen(name) + 1;
+ for (int_fast8_t i = 2; i < argc; ++i) {
+ char *ap = argv[i];
+ while ((*vp++ = *ap++) != '\0');
+ //if (i < argc-1)
+ *(vp-1) = ' ';
}
+ *(vp-1) = '\0';
- return 0;
+ return CMD_RET_SUCCESS;
}
-int setenv(const char *varname, const char *varvalue)
+/**
+ * Set an environment variable
+ *
+ * @param varname Environment variable to set
+ * @param varvalue Value to set it to
+ * @return 0 if ok, 1 on error
+ */
+
+int setenv(const MEMX char *varname, const char *varvalue)
{
+ int rc;
+
+#ifdef __MEMX
+ char *tmpname;
+ if (__builtin_avr_flash_segment(varname) != -1) {
+ tmpname = malloc(strlen_P(varname)+1);
+ if (tmpname == NULL) {
+ printf_P(PSTR("setenv: Out of Memory!\n"));
+ return 1;
+ }
+ strcpy_P(tmpname, varname);
+ } else
+ tmpname = (char *) varname;
+
+ const char * const argv[3] = { NULL, tmpname, varvalue };
+#else
const char * const argv[3] = { NULL, varname, varvalue };
+#endif
+
int argc = 3;
if (varvalue == NULL || varvalue[0] == '\0')
--argc;
-
- return _do_env_set(0, argc, (char * const *)argv);
+
+ rc = (int) _do_env_set(0, argc, (char * const *)argv);
+
+#ifdef __MEMX
+ if (__builtin_avr_flash_segment(varname) != -1)
+ free(tmpname);
+#endif
+ return rc;
}
-#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);
}
-unsigned long getenv_hex(const char *varname, unsigned long default_val)
+
+/**
+ * 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)
{
- const char *s;
unsigned long value;
- char *endp;
+ char *vp, *endp;
+
+ vp = getenv_str(name);
+
+ if (vp != NULL ) {
+ value = strtoul(vp, &endp, base);
+ if (endp != vp)
+ return value;
+ }
+
+ return default_val;
+}
+
+
+/*
+ * Read an environment variable as a boolean
+ */
+bool getenv_yesno(const MEMX char *name)
+{
+ char *s = getenv_str(name);
+
+ if (s == NULL)
+ return false;
- s = getenv(varname);
- if (s)
- value = strtoul(s, &endp, 16);
- if (!s || endp == s)
- return default_val;
+ return strchr_P(PSTR("1yYtT"), *s) != NULL;
- return value;
+/*
+ return *s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T' ?
+ 1 : 0;
+*/
}
-int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+command_ret_t do_env_print(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED,
+ int argc, char * const argv[])
{
- (void) cmdtp;
+ bool mode = 0;
+ command_ret_t rc = CMD_RET_SUCCESS;
+
+ int opt;
+ while ((opt = getopt(argc, argv, PSTR("s"))) != -1) {
+ switch (opt) {
+ case 's':
+ mode = 1;
+ break;
+ default: /* '?' */
+ return CMD_RET_USAGE;
+ }
+ }
+
+ if (optind == argc) {
+ /* print all env vars */
+ int size = env_print(NULL, mode);
+ if (size < 0)
+ return CMD_RET_FAILURE;
+ if (mode == 0)
+ printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"),
+ size, ENV_SIZE - 1);
+ return CMD_RET_SUCCESS;
+ }
+
+ /* print selected env vars */
+ while (optind < argc) {
+ int len = env_print(argv[optind], mode);
+ if (len < 0) {
+ rc = CMD_RET_FAILURE;
+ }
+ optind++;
+ }
+
+ return rc;
+}
+
+command_ret_t do_env_set(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag, int argc, char * const argv[])
+{
if (argc < 2)
return CMD_RET_USAGE;
}
-#if defined(CONFIG_AUTO_COMPLETE)
+command_ret_t do_env_default(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED,
+ int argc UNUSED, char * const argv[] UNUSED)
+{
+ 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 UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
+{
+ 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;
}
#endif
+cmd_tbl_t cmd_tbl_env[] = {
+CMD_TBL_ITEM_COMPLETE(
+ printenv, CONFIG_SYS_MAXARGS, 1|CTBL_SUBCMDAUTO, do_env_print,
+ "print environment variables",
+ "[-s] [name ...]\n"
+ " Print value of environment variable(s) 'name'\n"
+ " If no names are given, print values of all environment variables\n"
+ " -s Print in setenv form",
+ var_complete
+),
+CMD_TBL_ITEM_COMPLETE(
+ setenv, CONFIG_SYS_MAXARGS, CTBL_SUBCMDAUTO, do_env_set,
+ "set environment variables",
+ "name value ...\n"
+ " - set environment variable 'name' to 'value ...'\n"
+ "setenv name\n"
+ " - delete environment variable 'name'",
+ var_complete
+),
+CMD_TBL_ITEM(
+ saveenv, 1, CTBL_SUBCMDAUTO, do_env_save,
+ "save environment variables to persistent storage",
+ ""
+),
+CMD_TBL_ITEM(
+ defaultenv, 1, CTBL_SUBCMDAUTO, do_env_default,
+ "set all environment variables to their default values",
+ ""
+),
+
+CMD_TBL_ITEM_COMPLETE(
+ print, CONFIG_SYS_MAXARGS, 1, do_env_print,
+ "print environment variables",
+ "[-s] [name ...]\n"
+ " Print value of environment variable(s) 'name'\n"
+ " If no names are given, print values of all environment variables\n"
+ " -s Print in setenv form",
+ var_complete
+),
+CMD_TBL_ITEM_COMPLETE(
+ set, CONFIG_SYS_MAXARGS, 0, do_env_set,
+ "set environment variables",
+ "name value ...\n"
+ " - set environment variable 'name' to 'value ...'\n"
+ "setenv name\n"
+ " - delete environment variable 'name'",
+ var_complete
+),
+CMD_TBL_ITEM(
+ save, 1, 0, do_env_save,
+ "save environment variables to persistent storage",
+ ""
+),
+CMD_TBL_ITEM(
+ default, 1, 0, do_env_default,
+ "set all environment variables to their default values",
+ ""
+),
+
+CMD_TBL_ITEM(
+ help, CONFIG_SYS_MAXARGS, CTBL_RPT, do_help,
+ "Print sub command description/usage",
+ "\n"
+ " - print brief description of all sub commands\n"
+ "env help command ...\n"
+ " - print detailed usage of sub cmd 'command'"
+),
+
+/* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
+ {FSTR("?"), CONFIG_SYS_MAXARGS, 1, do_help,
+ NULL,
+#ifdef CONFIG_SYS_LONGHELP
+ FSTR(""),
+#endif /* CONFIG_SYS_LONGHELP */
+ NULL,
+#ifdef CONFIG_AUTO_COMPLETE
+ NULL,
+#endif
+},
+/* Mark end of table */
+CMD_TBL_END(cmd_tbl_env)
+};
+
+command_ret_t do_env(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
+{
+ return CMD_RET_USAGE;
+}