1 From 2536295c2aeafc769215a6b2883126fa94c90b9a Mon Sep 17 00:00:00 2001
2 From: Hante Meuleman <meuleman@broadcom.com>
3 Date: Thu, 21 May 2015 15:27:23 +0200
4 Subject: [PATCH] mips: bcm47xx: allow retrieval of complete nvram contents
6 Host platforms such as routers supported by OpenWrt can
7 support NVRAM reading directly from internal NVRAM store.
8 The brcmfmac for one requires the complete nvram contents
9 to select what needs to be sent to wireless device.
11 Signed-off-by: Arend van Spriel <arend@broadcom.com>
12 Signed-off-by: Hante Meuleman <meuleman@broadcom.com>
13 Reviewed-by: Arend Van Spriel <arend@broadcom.com>
14 Reviewed-by: Franky (Zhenhui) Lin <frankyl@broadcom.com>
15 Reviewed-by: Pieter-Paul Giesberts <pieterpg@broadcom.com>
16 Reviewed-by: Daniel (Deognyoun) Kim <dekim@broadcom.com>
17 Cc: linux-mips@linux-mips.org
18 Patchwork: https://patchwork.linux-mips.org/patch/10093/
19 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
21 arch/mips/bcm47xx/nvram.c | 60 ++++++++++++++++++++++++++++++++-----------
22 include/linux/bcm47xx_nvram.h | 15 +++++++++++
23 2 files changed, 60 insertions(+), 15 deletions(-)
25 diff --git a/arch/mips/bcm47xx/nvram.c b/arch/mips/bcm47xx/nvram.c
26 index 95d028c..2ed762e 100644
27 --- a/arch/mips/bcm47xx/nvram.c
28 +++ b/arch/mips/bcm47xx/nvram.c
29 @@ -94,17 +94,22 @@ static int nvram_find_and_copy(void __iomem *iobase, u32 lim)
33 - if (header->len > size)
34 - pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n");
35 - if (header->len > NVRAM_SPACE)
36 - pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
37 - header->len, NVRAM_SPACE - 1);
40 dst = (u32 *)nvram_buf;
41 for (i = 0; i < sizeof(struct nvram_header); i += 4)
42 *dst++ = __raw_readl(src++);
43 - for (; i < header->len && i < NVRAM_SPACE && i < size; i += 4)
44 + header = (struct nvram_header *)nvram_buf;
45 + if (header->len > size) {
46 + pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");
49 + if (header->len >= NVRAM_SPACE) {
50 + pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
51 + header->len, NVRAM_SPACE - 1);
52 + header->len = NVRAM_SPACE - 1;
54 + /* proceed reading data after header */
55 + for (; i < header->len; i += 4)
56 *dst++ = readl(src++);
57 nvram_buf[NVRAM_SPACE - 1] = '\0';
59 @@ -139,6 +144,7 @@ static int nvram_init(void)
62 struct nvram_header header;
63 + struct nvram_header *pheader;
67 @@ -147,20 +153,21 @@ static int nvram_init(void)
70 err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header);
71 - if (!err && header.magic == NVRAM_MAGIC) {
72 - u8 *dst = (uint8_t *)nvram_buf;
73 - size_t len = header.len;
75 - if (len >= NVRAM_SPACE) {
76 - len = NVRAM_SPACE - 1;
77 + if (!err && header.magic == NVRAM_MAGIC &&
78 + header.len > sizeof(header)) {
79 + if (header.len >= NVRAM_SPACE) {
80 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
82 + header.len, NVRAM_SPACE);
83 + header.len = NVRAM_SPACE - 1;
86 - err = mtd_read(mtd, 0, len, &bytes_read, dst);
87 + err = mtd_read(mtd, 0, header.len, &bytes_read,
92 + pheader = (struct nvram_header *)nvram_buf;
93 + pheader->len = header.len;
97 @@ -219,3 +226,26 @@ int bcm47xx_nvram_gpio_pin(const char *name)
100 EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);
102 +char *bcm47xx_nvram_get_contents(size_t *nvram_size)
106 + struct nvram_header *header;
108 + if (!nvram_buf[0]) {
109 + err = nvram_init();
114 + header = (struct nvram_header *)nvram_buf;
115 + *nvram_size = header->len - sizeof(struct nvram_header);
116 + nvram = vmalloc(*nvram_size);
119 + memcpy(nvram, &nvram_buf[sizeof(struct nvram_header)], *nvram_size);
123 +EXPORT_SYMBOL(bcm47xx_nvram_get_contents);
124 diff --git a/include/linux/bcm47xx_nvram.h b/include/linux/bcm47xx_nvram.h
125 index b12b07e..c73927c 100644
126 --- a/include/linux/bcm47xx_nvram.h
127 +++ b/include/linux/bcm47xx_nvram.h
130 #include <linux/types.h>
131 #include <linux/kernel.h>
132 +#include <linux/vmalloc.h>
134 #ifdef CONFIG_BCM47XX
135 int bcm47xx_nvram_init_from_mem(u32 base, u32 lim);
136 int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len);
137 int bcm47xx_nvram_gpio_pin(const char *name);
138 +char *bcm47xx_nvram_get_contents(size_t *val_len);
139 +static inline void bcm47xx_nvram_release_contents(char *nvram)
144 static inline int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
146 @@ -29,6 +35,15 @@ static inline int bcm47xx_nvram_gpio_pin(const char *name)
151 +static inline char *bcm47xx_nvram_get_contents(size_t *val_len)
156 +static inline void bcm47xx_nvram_release_contents(char *nvram)
161 #endif /* __BCM47XX_NVRAM_H */