]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/env.c
Add command loadi (load intel hex file)
[z180-stamp.git] / avr / env.c
index 602d845c673c6ddf4b567a4cdc0be48b942fa54d..7be7d03501f102a7fbed6367262272859d3f8ead 100644 (file)
--- a/avr/env.c
+++ b/avr/env.c
@@ -1,7 +1,12 @@
+/*
+ * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
 #include "common.h"
 #include <string.h>
 #include <stdlib.h>
-
 #include <avr/eeprom.h>
 
 #include "config.h"
@@ -9,7 +14,6 @@
 #include "xmalloc.h"
 #include "crc.h"
 #include "command.h"
-
 #include "env.h"
 
 
@@ -28,7 +32,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
@@ -75,35 +79,43 @@ char env_get_char(uint16_t index)
 }
 
 
+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;
 
-       return strcmp(ep1->envvar, ep2->envvar);
+       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)
 {
-       env_item_t e;
-
-       e.envvar = (char *) name;
-
 #ifdef __MEMX
-       char buf[CONFIG_SYS_ENV_NAMELEN+1];
-
        if (__builtin_avr_flash_segment(name) != -1) {
-               char *p = buf;
-               while ((*p++ = *name++) != '\0')
-                       ;
-               e.envvar = buf;
+               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);
        }
-#endif /* __MEMX */
+#else
+       env_item_t e;
+       e.envvar = (char *) name;
 
        return bsearch(&e, env_list, entrycount,
                                sizeof(env_item_t), comp_env_items);
+#endif /* __MEMX */
 }
 
 
@@ -149,14 +161,9 @@ int env_item_delete(env_item_t *ep)
 }
 
 static
-int envlist_delete(const char *name)
+int envlist_delete(const MEMX char *name)
 {
-       env_item_t e;
-
-       e.envvar = (char *) name;
-
-       env_item_t *ep = bsearch(&e, env_list, entrycount,
-                               sizeof(env_item_t), comp_env_items);
+       env_item_t *ep = envlist_search(name);
 
        if (ep != NULL)
                return env_item_delete(ep);
@@ -423,35 +430,6 @@ int env_print(const MEMX char *name)
 }
 
 
-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;
-}
-
-
 /**
  * Set or delete environment variable
  *
@@ -552,37 +530,47 @@ int setenv(const char *varname, const char *varvalue)
        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 MEMX 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);
 }
 
 
@@ -613,6 +601,35 @@ unsigned long getenv_ulong(const MEMX char *name, int base, unsigned long defaul
 }
 
 
+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;