[PATCH 1/2] environment: Support env from file in a file-system via device tree

Trent Piepho tpiepho at kymetacorp.com
Fri Dec 11 14:32:59 PST 2015


Current barebox,environment node only allows specifying a raw device or
partition to load an environment from.  Some boards, like OMAP and
SoCFPGA, instead want to use a file located in a FAT filesystem.
Extend the device tree bindings with a new property 'file-path' that
will trigger this behavior.

This allows any board using this driver to get the env from a file or
from a raw device, instead of each machine type being either raw
device only or file only.

Signed-off-by: Trent Piepho <tpiepho at kymetacorp.com>
---
 .../bindings/barebox/barebox,environment.rst       | 15 ++++++-
 drivers/of/Kconfig                                 |  7 ++++
 drivers/of/barebox.c                               | 47 +++++++++++++++++++++-
 3 files changed, 66 insertions(+), 3 deletions(-)

diff --git a/Documentation/devicetree/bindings/barebox/barebox,environment.rst b/Documentation/devicetree/bindings/barebox/barebox,environment.rst
index d5e52ea..12b103b 100644
--- a/Documentation/devicetree/bindings/barebox/barebox,environment.rst
+++ b/Documentation/devicetree/bindings/barebox/barebox,environment.rst
@@ -6,7 +6,10 @@ This driver provides an environment for barebox from the devicetree.
 Required properties:
 
 * ``compatible``: should be ``barebox,environment``
-* ``device-path``: path to the environment
+* ``device-path``: path to the device environment is on
+
+Optional properties:
+* ``file-path``: path to a file in the device named by device-path
 
 The device-path is a multistring property. The first string should contain
 a nodepath to the node containing the physical device of the environment or
@@ -19,9 +22,19 @@ the path to the environment. Supported values for <type>:
   be the label for MTD partitions, the number for DOS
   partitions (beginning with 0) or the name for GPT partitions.
 
+The file-path is the name of a file located in a FAT filesystem on the
+device named in device-path.  This filesystem will be mounted and the
+environment loaded from the file's location in the directory tree.
+
 Example::
 
   environment at 0 {
   	compatible = "barebox,environment";
   	device-path = &flash, "partname:barebox-environment";
   };
+
+  environment at 1 {
+  	compatible = "barebox,environment";
+  	device-path = &mmc, "partname:1";
+  	file-path = "barebox.env";
+  };
diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig
index 90475cf..d0a62bd 100644
--- a/drivers/of/Kconfig
+++ b/drivers/of/Kconfig
@@ -43,3 +43,10 @@ config OF_BAREBOX_DRIVERS
 	  support for this feature. This currently allows to configure the
 	  environment path from devicetree and to partition devices. See
 	  Documentation/devicetree/bindings/barebox/ for more information.
+
+config OF_BAREBOX_ENV_IN_FS
+	depends on OF_BAREBOX_DRIVERS
+	bool "Allow environment to come from file"
+	help
+	  Allow the devie tree configuration of the barebox environment path
+	  to specify a file in filesystem, which will be mounted.
diff --git a/drivers/of/barebox.c b/drivers/of/barebox.c
index 1b3078e..125feef 100644
--- a/drivers/of/barebox.c
+++ b/drivers/of/barebox.c
@@ -24,7 +24,46 @@
 #include <malloc.h>
 #include <partition.h>
 #include <envfs.h>
-#include <linux/mtd/mtd.h>
+#include <fs.h>
+
+#define ENV_MNT_DIR "/boot"	/* If env on filesystem, where to mount */
+
+/* If dev describes a file on a fs, mount the fs and change devpath to
+ * point to the file's path.  Otherwise leave devpath alone.  Does
+ * nothing in env in a file support isn't enabled.  */
+static int environment_check_mount(struct device_d *dev, char **devpath)
+{
+	const char *filepath;
+	int ret;
+
+	if (!IS_ENABLED(CONFIG_OF_BAREBOX_ENV_IN_FS))
+		return 0;
+
+	ret = of_property_read_string(dev->device_node, "file-path", &filepath);
+	if (ret == -EINVAL) {
+		/* No file-path so just use device-path */
+		return 0;
+	} else if (ret) {
+		/* file-path property exists, but has error */
+		dev_err(dev, "Problem with file-path property\n");
+		return ret;
+	}
+
+	/* Get device env is on and mount it */
+	mkdir(ENV_MNT_DIR, 0777);
+	ret = mount(*devpath, "fat", ENV_MNT_DIR, NULL);
+	if (ret) {
+		dev_err(dev, "Failed to load environment: mount %s failed (%d)\n",
+			*devpath, ret);
+		return ret;
+	}
+
+	/* Set env to be in a file on the now mounted device */
+	dev_dbg(dev, "Loading default env from %s on device %s\n",
+		filepath, *devpath);
+	*devpath = asprintf("%s/%s", ENV_MNT_DIR, filepath);
+	return 0;
+}
 
 static int environment_probe(struct device_d *dev)
 {
@@ -35,8 +74,12 @@ static int environment_probe(struct device_d *dev)
 	if (ret)
 		return ret;
 
-	dev_info(dev, "setting default environment path to %s\n", path);
+	/* Do we need to mount a fs and find env there? */
+	ret = environment_check_mount(dev, &path);
+	if (ret)
+		return ret;
 
+	dev_dbg(dev, "Setting default environment path to %s\n", path);
 	default_environment_path_set(path);
 
 	return 0;
-- 
1.8.3.1




More information about the barebox mailing list