Skip to contentSkip to conversation

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

PATCH In review Michael C. Pratt 2 participants 2 messages Sep 3 – Sep 5

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 fw_env: use env-size property for nvmem layout device detection

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.

Devicetree binary cells are always stored in big-endian, so endian translation is now necessary for this case.

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.

---
 tools/env/fw_env.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
tools/env/fw_env.c +21 -0
index 8810b0e94dc..7dd90b16b46 100644
@@ -30,6 +30,7 @@
 #include <u-boot/crc.h>
 #include <unistd.h>
 #include <dirent.h>
+#include <asm/byteorder.h>
 
 #ifdef MTD_OLD
 # include <stdint.h>
@@ -1714,6 +1715,7 @@ static int find_nvmem_device(void)
 	char comp[256];
 	char buf[32];
 	int bytes;
+	ulong layout_size = 0;
 	DIR *dir;
 
 	dir = opendir(path);
@@ -1771,6 +1773,9 @@ static int find_nvmem_device(void)
 			}
 		}
 
+		/* Remember the nvmem device path for parsing other properties. */
+		bytes = snprintf(comp, sizeof(comp), "%s/%s", path, dent->d_name);
+
 next:
 		fclose(fp);
 	}
@@ -1779,6 +1784,8 @@ next:
 
 	if (nvmem) {
 		struct stat s;
+		FILE *fp;
+		size_t size;
 
 		stat(nvmem, &s);
 
@@ -1786,6 +1793,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) = __be32_to_cpu(layout_size);
+				fclose(fp);
+			}
+
+			comp[bytes] = '\0';
+		}
+
 		return 0;
 	}
 
Signature · 1 line
2.30.2
#1 lore raw
Quoted 10 lines · Michael C. Pratt

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.

Devicetree binary cells are always stored in big-endian, so endian translation is now necessary for this case.

[...]

Applied to u-boot/main, thanks!

[1/1] fw_env: use env-size property for nvmem layout device detection

      commit: 7bf077ec5733a230bbce34d8ce324f92d8c46cc1
Signature · 1 line
Tom