From: Javier Viguera <javier.viguera@digi.com>
Date: Tue, 10 Jan 2017 19:34:26 +0100
Subject: [PATCH 2/4] Implement U-Boot environment access functions

Signed-off-by: Javier Viguera <javier.viguera@digi.com>
Signed-off-by: Jose Diaz de Grenu <Jose.DiazdeGrenu@digi.com>
Signed-off-by: Gonzalo Ruiz <Gonzalo.Ruiz@digi.com>
---
 tools/env/Makefile   |  2 +-
 tools/env/ubootenv.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/env/ubootenv.h | 46 ++++++++++++++++++++++++++++++++++++
 3 files changed, 113 insertions(+), 1 deletion(-)
 create mode 100644 tools/env/ubootenv.c
 create mode 100644 tools/env/ubootenv.h

diff --git a/tools/env/Makefile b/tools/env/Makefile
index 33e41f5..486e76c 100644
--- a/tools/env/Makefile
+++ b/tools/env/Makefile
@@ -23,7 +23,7 @@ hostprogs-y := fw_printenv

 lib-y += fw_env.o \
 	crc32.o ctype.o linux_string.o \
-	env_attr.o env_flags.o ../../lib/md5.o
+	env_attr.o env_flags.o ../../lib/md5.o ubootenv.o

 fw_printenv-objs := fw_env_main.o $(lib-y)

diff --git a/tools/env/ubootenv.c b/tools/env/ubootenv.c
new file mode 100644
index 0000000..eb9ad5f
--- /dev/null
+++ b/tools/env/ubootenv.c
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2017, Digi International Inc.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, you can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Description: U-Boot environment get/set wrappers
+ *
+ */
+
+#include <stdio.h>
+
+#include "fw_env.h"
+
+/*
+ * Function:    uboot_getenv
+ * Description: get U-Boot's environment variable
+ */
+int uboot_getenv(char *name, const char **value)
+{
+	int ret = 0;
+
+	ret = fw_env_open(NULL);
+	if (ret)
+		goto err;
+
+	*value = fw_getenv(name);
+
+err:
+	return ret ? -1 : 0;
+}
+
+/*
+ * Function:    uboot_setenv
+ * Description: set U-Boot's environment variable
+ */
+int uboot_setenv(char *name, char *value)
+{
+	int ret = 0;
+
+	ret = fw_env_open(NULL);
+	if (ret)
+		goto err;
+
+	ret = fw_env_write(name, value);
+	if (ret)
+		goto err;
+
+	ret = fw_env_flush(NULL);
+	if (ret)
+		goto err;
+
+	ret = fw_env_close(NULL);
+
+err:
+	return ret ? -1 : 0;
+}
diff --git a/tools/env/ubootenv.h b/tools/env/ubootenv.h
new file mode 100644
index 0000000..d4043db
--- /dev/null
+++ b/tools/env/ubootenv.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2017, Digi International Inc.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, you can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Description: U-Boot environment get/set wrappers
+ *
+ */
+
+#ifndef UBOOTENV_H
+#define UBOOTENV_H
+
+/*
+ * Get U-Boot's environment variable.
+ *
+ * Params:
+ *   'name'  (input)   Name of the environment variable
+ *   'value' (output)  Pointer to the variable's value
+ *                     (NULL if not found)
+ *
+ * Return: 0 on sucess, -1 on failure
+ */
+int uboot_getenv(char *name, const char **value);
+
+/*
+ * Set U-Boot's environment variable.
+ *
+ * Params:
+ *   'name'  (input)  Name of the environment variable
+ *   'value' (input)  Value of the environment variable
+ *
+ * Return: 0 on sucess, -1 on failure
+ */
+int uboot_setenv(char *name, char *value);
+
+#endif /* UBOOTENV_H */
