diff mbox series

fw_env: use env-size property for nvmem layout device detection

Message ID 20260902083839.22154-1-mcpratt@pm.me
State Superseded
Delegated to: Tom Rini
Headers show
Series fw_env: use env-size property for nvmem layout device detection | expand

Commit Message

Michael C. Pratt Sept. 2, 2026, 8:38 a.m. UTC
Since Linux commit 5b2f8c133d98 ("nvmem: layouts: u-boot-env:
add optional "env-size" property"), there is support for providing the size
of the u-boot environment used by the bootloader to the layout NVMEM driver
in order to calculate the CRC if the range is less than the partition size
in a similar fashion that it would be provided in a fw_env.config file.

The value is stored in binary in the same endianness as the system,
so no endianness translation is necessary, simply copy to a new variable.

Reuse the buffer that holds the path for opening the compatible sysfs file
for parsing this env-size property, and possibly more in the future,
as soon as the correct device is identified with the compatible property.

Signed-off-by: Michael C. Pratt <mcpratt@pm.me>
---
 tools/env/fw_env.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
diff mbox series

Patch

diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
index 8810b0e94dc..c6ac91cd80c 100644
--- a/tools/env/fw_env.c
+++ b/tools/env/fw_env.c
@@ -1714,6 +1714,7 @@  static int find_nvmem_device(void)
 	char comp[256];
 	char buf[32];
 	int bytes;
+	ulong layout_size = 0;
 	DIR *dir;
 
 	dir = opendir(path);
@@ -1775,10 +1776,14 @@  next:
 		fclose(fp);
 	}
 
+	/* Remember the nvmem device path for parsing other properties. */
+	bytes = snprintf(comp, sizeof(comp), "%s/%s", path, dent->d_name);
 	closedir(dir);
 
 	if (nvmem) {
 		struct stat s;
+		FILE *fp;
+		size_t size;
 
 		stat(nvmem, &s);
 
@@ -1786,6 +1791,20 @@  next:
 		DEVOFFSET(0) = 0;
 		ENVSIZE(0) = s.st_size;
 
+		if (bytes > 0 && bytes != sizeof(comp)) {
+			strcat(comp, "/of_node/nvmem-layout/env-size");
+			fp = fopen(comp, "rb");
+			if (fp) {
+				fstat(fileno(fp), &s);
+				size = fread(&layout_size, s.st_size, 1, fp);
+				if (size == 1)
+					ENVSIZE(0) = layout_size;
+				fclose(fp);
+			}
+
+			comp[bytes] = '\0';
+		}
+
 		return 0;
 	}