WindCharger: Adds uboot default env after booting.

Addresses b/21073550

Change-Id: Ib4e716527ba4a6c79631bcb22e7a4486e7b1dc29
diff --git a/common/env_common.c b/common/env_common.c
index a188fa3..2b11e73 100755
--- a/common/env_common.c
+++ b/common/env_common.c
@@ -303,3 +303,54 @@
 	return found;
 }
 #endif
+
+int env_restore_defaults(void)
+{
+	int i;
+	const uchar delim = '=';
+	const int default_env_size = sizeof(default_environment) - 1;
+	char *s;
+	uchar *key = NULL;
+	uchar *val = NULL;
+	uchar *tmp_null = NULL;
+	for (i = 0; i < default_env_size; ++i) {
+		if (key == NULL) {
+			key = default_environment + i;
+		}
+		/* If delim is encountered, we know to check for key/val. */
+		if (default_environment[i] == delim) {
+			/* Ignore empty keys. */
+			if (key == (default_environment + i)) {
+				return -1;
+			}
+			/*
+			 * Does in-place changing of null terminator to avoid
+			 * extra buffers.
+			 */
+			tmp_null = default_environment + i;
+			val = tmp_null + 1;
+			*tmp_null = '\0';
+			s = getenv(key);
+			if (!s) {
+				setenv(key, val);
+			}
+			*tmp_null = delim;
+			/*
+			 * Advances pointer until we encounter the end of the
+			 * 'val' string.
+			 */
+			for ( ; i < default_env_size; ++i) {
+				if (default_environment[i] == '\0') {
+					break;
+				}
+			}
+			key = NULL;
+		}
+	}
+	if (key != NULL && *key != '\0') {
+		printf("%s: incomplete parsing while restoring env.\n", __FILE__);
+		printf("\tPlease check default_environment var\n");
+		return -1;
+	}
+	return 0;
+}
diff --git a/common/main.c b/common/main.c
index 46b2564..80e16bb 100755
--- a/common/main.c
+++ b/common/main.c
@@ -29,6 +29,9 @@
 #include <watchdog.h>
 #include <command.h>
 
+#define XMK_STR(x)	#x
+#define MK_STR(x)	XMK_STR(x)
+
 #ifdef CONFIG_MODEM_SUPPORT
 #include <malloc.h>		/* for free() prototype */
 #endif
@@ -405,24 +408,20 @@
 	}
 	else
 #endif /* CONFIG_BOOTCOUNT_LIMIT */
-		s = getenv ("bootcmd");
-       if (!s) {
-#ifdef CONFIG_ROOTFS_FLASH
-           /* XXX if rootfs is in flash, expect uImage to be in flash */
-#ifdef CONFIG_AR7100
-           setenv ("bootcmd", "bootm 0xbf200000");
-#else
-           setenv ("bootcmd", "bootm 0xbf450000");
-#endif /* CONFIG_AR7100 */
-#else
-           setenv ("bootcmd", "tftpboot 0x8022c090 uImage; bootm 0x8022c090");
-#endif
-       }
 
 #ifdef CONFIG_DUALIMAGE_SUPPORT
 		findbdr(0);
 #endif
-		s = getenv ("bootcmd");
+
+	if (env_restore_defaults()) {
+		printf("Warning: Unable to completely restore default env.\n");
+	}
+
+	s = getenv ("bootcmd");
+	if (!s) {
+		setenv("bootcmd", CONFIG_BOOTCOMMAND);
+		s = CONFIG_BOOTCOMMAND;
+	}
 
 //	debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
 
diff --git a/include/common.h b/include/common.h
index a39a27c..2d7aa6c 100755
--- a/include/common.h
+++ b/include/common.h
@@ -223,6 +223,9 @@
 const char *get_uboot_name(const char *sysvar_name);
 void delete_uboot_vars(void);
 
+/* common/env_common.c */
+int env_restore_defaults(void);
+
 /* common/cmd_nvedit.c */
 int	env_init     (void);
 void	env_relocate (void);