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.

Signed-off-by: Michael C. Pratt <[email protected]>
---
 tools/env/fw_env.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
index 8810b0e94dc..7dd90b16b46 100644
--- a/tools/env/fw_env.c
+++ b/tools/env/fw_env.c
@@ -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;
        }
 
-- 
2.30.2


Reply via email to