diff mbox series

fw_env: add fallback to Linux's NVMEM based access

Message ID 20220616185903.14426-1-zajec5@gmail.com
State New
Headers show
Series fw_env: add fallback to Linux's NVMEM based access | expand

Commit Message

Rafał Miłecki June 16, 2022, 6:59 p.m. UTC
From: Rafał Miłecki <rafal@milecki.pl>

A new DT binding for describing environment data block has been added in
Linux's commit 5db1c2dbc04c ("dt-bindings: nvmem: add U-Boot environment
variables binding"). Once we get a proper Linux NVMEM driver it'll be
possible to use Linux's binary interface for user-space as documented
in the:
https://www.kernel.org/doc/html/latest/driver-api/nvmem.html

This commits makes fw_env fallback to looking for a compatible NVMEM
device in case config file isn't present. In a long term this may make
config files redundant and avoid code (info) duplication.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
 tools/env/fw_env.c | 70 ++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 67 insertions(+), 3 deletions(-)

Comments

Tom Rini July 8, 2022, 4:39 p.m. UTC | #1
On Thu, Jun 16, 2022 at 08:59:03PM +0200, Rafał Miłecki wrote:

> From: Rafał Miłecki <rafal@milecki.pl>
> 
> A new DT binding for describing environment data block has been added in
> Linux's commit 5db1c2dbc04c ("dt-bindings: nvmem: add U-Boot environment
> variables binding"). Once we get a proper Linux NVMEM driver it'll be
> possible to use Linux's binary interface for user-space as documented
> in the:
> https://www.kernel.org/doc/html/latest/driver-api/nvmem.html
> 
> This commits makes fw_env fallback to looking for a compatible NVMEM
> device in case config file isn't present. In a long term this may make
> config files redundant and avoid code (info) duplication.
> 
> Signed-off-by: Rafał Miłecki <rafal@milecki.pl>

Applied to u-boot/next, thanks!
Arınç ÜNAL May 9, 2023, 5:59 a.m. UTC | #2
On 16.06.2022 21:59, Rafał Miłecki wrote:
> From: Rafał Miłecki <rafal@milecki.pl>
> 
> A new DT binding for describing environment data block has been added in
> Linux's commit 5db1c2dbc04c ("dt-bindings: nvmem: add U-Boot environment
> variables binding"). Once we get a proper Linux NVMEM driver it'll be
> possible to use Linux's binary interface for user-space as documented
> in the:
> https://www.kernel.org/doc/html/latest/driver-api/nvmem.html
> 
> This commits makes fw_env fallback to looking for a compatible NVMEM
> device in case config file isn't present. In a long term this may make
> config files redundant and avoid code (info) duplication.
> 
> Signed-off-by: Rafał Miłecki <rafal@milecki.pl>

I see the Linux NVMEM driver [0] does not implement @reg_write. Does 
this mean only fw_printenv would work for this method?

[0] 
https://github.com/torvalds/linux/blob/c49f1a8af6bcf6d18576bca898f8083ca4b129e1/drivers/nvmem/u-boot-env.c

Arınç
diff mbox series

Patch

diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
index 31afef6f3b..be77bbf054 100644
--- a/tools/env/fw_env.c
+++ b/tools/env/fw_env.c
@@ -1713,6 +1713,67 @@  static int check_device_config(int dev)
 	return rc;
 }
 
+static int find_nvmem_device(void)
+{
+	const char *path = "/sys/bus/nvmem/devices";
+	struct dirent *dent;
+	char *nvmem = NULL;
+	char comp[256];
+	char buf[32];
+	int bytes;
+	DIR *dir;
+
+	dir = opendir(path);
+	if (!dir) {
+		return -EIO;
+	}
+
+	while (!nvmem && (dent = readdir(dir))) {
+		FILE *fp;
+
+		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) {
+			continue;
+		}
+
+		bytes = snprintf(comp, sizeof(comp), "%s/%s/of_node/compatible", path, dent->d_name);
+		if (bytes < 0 || bytes == sizeof(comp)) {
+			continue;
+		}
+
+		fp = fopen(comp, "r");
+		if (!fp) {
+			continue;
+		}
+
+		fread(buf, sizeof(buf), 1, fp);
+
+		if (!strcmp(buf, "u-boot,env")) {
+			bytes = asprintf(&nvmem, "%s/%s/nvmem", path, dent->d_name);
+			if (bytes < 0) {
+				nvmem = NULL;
+			}
+		}
+
+		fclose(fp);
+	}
+
+	closedir(dir);
+
+	if (nvmem) {
+		struct stat s;
+
+		stat(nvmem, &s);
+
+		DEVNAME(0) = nvmem;
+		DEVOFFSET(0) = 0;
+		ENVSIZE(0) = s.st_size;
+
+		return 0;
+	}
+
+	return -ENOENT;
+}
+
 static int parse_config(struct env_opts *opts)
 {
 	int rc;
@@ -1723,9 +1784,12 @@  static int parse_config(struct env_opts *opts)
 #if defined(CONFIG_FILE)
 	/* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
 	if (get_config(opts->config_file)) {
-		fprintf(stderr, "Cannot parse config file '%s': %m\n",
-			opts->config_file);
-		return -1;
+		if (find_nvmem_device()) {
+			fprintf(stderr, "Cannot parse config file '%s': %m\n",
+				opts->config_file);
+			fprintf(stderr, "Failed to find NVMEM device\n");
+			return -1;
+		}
 	}
 #else
 	DEVNAME(0) = DEVICE1_NAME;