public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Stefan Roese <sr@denx.de>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 4/6] env: Extract getenv_f() into separate source file
Date: Thu, 23 Aug 2012 10:12:43 +0200	[thread overview]
Message-ID: <1345709565-28862-5-git-send-email-sr@denx.de> (raw)
In-Reply-To: <1345709565-28862-1-git-send-email-sr@denx.de>

By extracting getenv_f and envmatch() from cmd_nvedit.c into a
separate file, getenv_f() can be included easily into the SPL
binary. With this, SPL boards can now use getenv_f() to read
environment variables (e.g. to detect if the OS or U-Boot shall
be executed).

In the approach this is done for env stored in NOR flash, as this
will be used by an upcoming MPC5200 board port.

Signed-off-by: Stefan Roese <sr@denx.de>
---
 common/Makefile       |  4 +++
 common/cmd_nvedit.c   | 58 ------------------------------------
 common/env_getenv_f.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 58 deletions(-)
 create mode 100644 common/env_getenv_f.c

diff --git a/common/Makefile b/common/Makefile
index 3d62775..2cd539a 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -50,6 +50,7 @@ XCOBJS-$(CONFIG_ENV_IS_EMBEDDED) += env_embedded.o
 COBJS-$(CONFIG_ENV_IS_IN_EEPROM) += env_embedded.o
 XCOBJS-$(CONFIG_ENV_IS_IN_FLASH) += env_embedded.o
 COBJS-$(CONFIG_ENV_IS_IN_NVRAM) += env_embedded.o
+COBJS-y += env_getenv_f.o
 COBJS-$(CONFIG_ENV_IS_IN_FLASH) += env_flash.o
 COBJS-$(CONFIG_ENV_IS_IN_MMC) += env_mmc.o
 COBJS-$(CONFIG_ENV_IS_IN_FAT) += env_fat.o
@@ -187,6 +188,9 @@ COBJS-$(CONFIG_USB_KEYBOARD) += usb_kbd.o
 endif
 
 ifdef CONFIG_SPL_BUILD
+COBJS-y += env_common.o
+COBJS-y += env_getenv_f.o
+COBJS-$(CONFIG_ENV_IS_IN_FLASH) += env_flash.o
 COBJS-$(CONFIG_SPL_YMODEM_SUPPORT) += xyzModem.o
 endif
 COBJS-y += console.o
diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index fd05e72..f766fd5 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -520,44 +520,6 @@ char *getenv(const char *name)
 	return NULL;
 }
 
-/*
- * Look up variable from environment for restricted C runtime env.
- */
-int getenv_f(const char *name, char *buf, unsigned len)
-{
-	int i, nxt;
-
-	for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
-		int val, n;
-
-		for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
-			if (nxt >= CONFIG_ENV_SIZE)
-				return -1;
-		}
-
-		val = envmatch((uchar *)name, i);
-		if (val < 0)
-			continue;
-
-		/* found; copy out */
-		for (n = 0; n < len; ++n, ++buf) {
-			*buf = env_get_char(val++);
-			if (*buf == '\0')
-				return n;
-		}
-
-		if (n)
-			*--buf = '\0';
-
-		printf("env_buf [%d bytes] too small for value of \"%s\"\n",
-			len, name);
-
-		return n;
-	}
-
-	return -1;
-}
-
 /**
  * Decode the integer value of an environment variable and return it.
  *
@@ -593,26 +555,6 @@ U_BOOT_CMD(
 );
 #endif
 
-
-/*
- * Match a name / name=value pair
- *
- * s1 is either a simple 'name', or a 'name=value' pair.
- * i2 is the environment index for a 'name2=value2' pair.
- * If the names match, return the index for the value2, else -1.
- */
-int envmatch(uchar *s1, int i2)
-{
-	while (*s1 == env_get_char(i2++))
-		if (*s1++ == '=')
-			return i2;
-
-	if (*s1 == '\0' && env_get_char(i2-1) == '=')
-		return i2;
-
-	return -1;
-}
-
 static int do_env_default(cmd_tbl_t *cmdtp, int flag,
 			  int argc, char * const argv[])
 {
diff --git a/common/env_getenv_f.c b/common/env_getenv_f.c
new file mode 100644
index 0000000..7dfbfe0
--- /dev/null
+++ b/common/env_getenv_f.c
@@ -0,0 +1,82 @@
+/*
+ * (C) Copyright 2000-2010
+ * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
+ *
+ * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
+ * Andreas Heppel <aheppel@sysgo.de>
+ *
+ * Copyright 2011 Freescale Semiconductor, Inc.
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
+ * GNU General Public License for more details.
+ */
+
+#include <common.h>
+#include <environment.h>
+
+/*
+ * Match a name / name=value pair
+ *
+ * s1 is either a simple 'name', or a 'name=value' pair.
+ * i2 is the environment index for a 'name2=value2' pair.
+ * If the names match, return the index for the value2, else -1.
+ */
+int envmatch(uchar *s1, int i2)
+{
+	while (*s1 == env_get_char(i2++))
+		if (*s1++ == '=')
+			return i2;
+
+	if (*s1 == '\0' && env_get_char(i2-1) == '=')
+		return i2;
+
+	return -1;
+}
+
+/*
+ * Look up variable from environment for restricted C runtime env.
+ */
+int getenv_f(const char *name, char *buf, unsigned len)
+{
+	int i, nxt;
+
+	for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
+		int val, n;
+
+		for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
+			if (nxt >= CONFIG_ENV_SIZE)
+				return -1;
+		}
+
+		val = envmatch((uchar *)name, i);
+		if (val < 0)
+			continue;
+
+		/* found; copy out */
+		for (n = 0; n < len; ++n, ++buf) {
+			*buf = env_get_char(val++);
+			if (*buf == '\0')
+				return n;
+		}
+
+		if (n)
+			*--buf = '\0';
+
+		printf("env_buf [%d bytes] too small for value of \"%s\"\n",
+			len, name);
+
+		return n;
+	}
+
+	return -1;
+}
-- 
1.7.12

  parent reply	other threads:[~2012-08-23  8:12 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-23  8:12 [U-Boot] [PATCH 0/6] SPL: Port SPL framework to powerpc Stefan Roese
2012-08-23  8:12 ` [U-Boot] [PATCH 1/6] SPL: Add NOR flash booting support Stefan Roese
2012-08-23 15:07   ` Tom Rini
2012-08-23 15:19     ` Stefan Roese
2012-08-23  8:12 ` [U-Boot] [PATCH 2/6] powerpc: Extract EPAPR_MAGIC constants into processor.h Stefan Roese
2012-08-23  8:12 ` [U-Boot] [PATCH 3/6] SPL: Port SPL framework to powerpc Stefan Roese
2012-08-23 17:10   ` Tom Rini
2012-08-23 18:16     ` Stefan Roese
2012-08-23 19:31       ` Tom Rini
2012-08-24  8:17         ` Stefan Roese
2012-08-24 10:17           ` Heiko Schocher
2012-08-24 10:56             ` Stefan Roese
2012-08-24 11:13               ` Stefan Roese
2012-08-24 11:49                 ` Daniel Schwierzeck
2012-08-24 14:11                   ` Stefan Roese
2012-08-24 15:29                     ` Daniel Schwierzeck
2012-08-24 16:06                     ` Stefan Roese
2012-08-24 16:42                       ` Daniel Schwierzeck
2012-08-24 17:24                         ` Stefan Roese
2012-08-24 22:13                           ` Daniel Schwierzeck
2012-08-24 19:15                 ` Tom Rini
2012-08-25  8:48                   ` Stefan Roese
2012-08-23 21:39   ` Tom Rini
2012-08-24  7:01     ` Stefan Roese
2012-08-24 15:55       ` Tom Rini
2012-08-24 16:07         ` Stefan Roese
2012-08-24 16:19           ` Tom Rini
2012-08-24 17:21             ` Stefan Roese
2012-08-23 21:52   ` Tom Rini
2012-08-24  7:03     ` Stefan Roese
2012-08-23  8:12 ` Stefan Roese [this message]
2012-08-23  8:12 ` [U-Boot] [PATCH 5/6] mpc5200: Add SPL support Stefan Roese
2012-08-23  8:12 ` [U-Boot] [PATCH 6/6] mpc5200: Add a3m071 board support Stefan Roese
2012-08-23 21:53 ` [U-Boot] [PATCH 0/6] SPL: Port SPL framework to powerpc Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1345709565-28862-5-git-send-email-sr@denx.de \
    --to=sr@denx.de \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox