[OpenWrt-Devel] [PATCH] bcm53xx: Add functions to get/release copies of nvram contents.

Hante Meuleman meuleman at broadcom.com
Tue May 12 09:10:31 EDT 2015


Added two functions to the bcm47xx_nvram module to get and release copies 
of the complete nvram contents. This can be used by for example brcmfmac 
to get complete nvram contents which will after some parsing be sent to 
device.

Signed-off-by: Hante Meuleman <meuleman at broadcom.com>
---
diff --git a/target/linux/bcm53xx/files/drivers/firmware/broadcom/bcm47xx_nvram.c b/target/linux/bcm53xx/files/drivers/firmware/broadcom/bcm47xx_nvram.c
index d0055a4..96d2308 100644
--- a/target/linux/bcm53xx/files/drivers/firmware/broadcom/bcm47xx_nvram.c
+++ b/target/linux/bcm53xx/files/drivers/firmware/broadcom/bcm47xx_nvram.c
@@ -18,6 +18,7 @@
 #include <linux/string.h>
 #include <linux/mtd/mtd.h>
 #include <linux/bcm47xx_nvram.h>
+#include <linux/vmalloc.h>
 
 #define NVRAM_MAGIC                    0x48534C46      /* 'FLSH' */
 #define NVRAM_SPACE                    0x10000
@@ -35,6 +36,7 @@ struct nvram_header {
 };
 
 static char nvram_buf[NVRAM_SPACE];
+static size_t nvram_len;
 static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
 
 static u32 find_nvram_size(void __iomem *end)
@@ -147,19 +149,19 @@ static int nvram_init(void)
 
        err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header);
        if (!err && header.magic == NVRAM_MAGIC) {
-               u8 *dst = (uint8_t *)nvram_buf;
-               size_t len = header.len;
-
-               if (header.len > NVRAM_SPACE) {
+               nvram_len = header.len;
+               if (nvram_len > NVRAM_SPACE - 2) {
                        pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
                                header.len, NVRAM_SPACE);
-                       len = NVRAM_SPACE;
+                       nvram_len = NVRAM_SPACE - 2;
                }
 
-               err = mtd_read(mtd, 0, len, &bytes_read, dst);
+               err = mtd_read(mtd, 0, nvram_len, &bytes_read,
+                              (u8 *)nvram_buf);
                if (err)
                        return err;
-
+               nvram_buf[nvram_len] = 0;
+               nvram_buf[nvram_len + 1] = 0;
                return 0;
        }
 #endif
@@ -183,9 +185,7 @@ int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
 
        /* Look for name=value and return value */
        var = &nvram_buf[sizeof(struct nvram_header)];
-       end = nvram_buf + sizeof(nvram_buf) - 2;
-       end[0] = '\0';
-       end[1] = '\0';
+       end = nvram_buf + nvram_len;
        for (; *var; var = value + strlen(value) + 1) {
                data_left = end - var;
 
@@ -222,4 +222,32 @@ int bcm47xx_nvram_gpio_pin(const char *name)
 }
 EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);
 
+char *bcm47xx_nvram_get_contents(size_t *nvram_size)
+{
+       int err;
+       char *nvram;
+
+       if (!nvram_buf[0]) {
+               err = nvram_init();
+               if (err)
+                       return NULL;
+       }
+
+       *nvram_size = nvram_len - sizeof(struct nvram_header);
+       nvram = vmalloc(*nvram_size);
+       if (nvram == NULL)
+               return NULL;
+       memcpy(nvram, &nvram_buf[sizeof(struct nvram_header)], *nvram_size);
+
+       return nvram;
+}
+EXPORT_SYMBOL(bcm47xx_nvram_get_contents);
+
+void bcm47xx_nvram_release_contents(char *nvram)
+{
+       vfree(nvram);
+}
+EXPORT_SYMBOL(bcm47xx_nvram_release_contents);
+
+
 MODULE_LICENSE("GPLv2");
diff --git a/target/linux/bcm53xx/files/include/linux/bcm47xx_nvram.h b/target/linux/bcm53xx/files/include/linux/bcm47xx_nvram.h
index 0e52a92..2f3f48f 100644
--- a/target/linux/bcm53xx/files/include/linux/bcm47xx_nvram.h
+++ b/target/linux/bcm53xx/files/include/linux/bcm47xx_nvram.h
@@ -15,6 +15,8 @@
 int bcm47xx_nvram_init_from_mem(u32 base, u32 lim);
 int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len);
 int bcm47xx_nvram_gpio_pin(const char *name);
+char *bcm47xx_nvram_get_contents(size_t *val_len);
+void bcm47xx_nvram_release_contents(char *nvram);
 #else
 static inline int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
 {
@@ -29,6 +31,13 @@ static inline int bcm47xx_nvram_gpio_pin(const char *name)
 {
        return -ENOTSUPP;
 };
+static inline char *bcm47xx_nvram_get_contents(size_t *val_len)
+{
+       return NULL;
+};
+static inline void bcm47xx_nvram_release_contents(char *nvram)
+{
+};
 #endif
 
 #endif /* __BCM47XX_NVRAM_H */
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list