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 --- a/arch/mips/bcm47xx/nvram.c
26 +++ b/arch/mips/bcm47xx/nvram.c
27 @@ -94,17 +94,22 @@ static int nvram_find_and_copy(void __io
31 - if (header->len > size)
32 - pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n");
33 - if (header->len > NVRAM_SPACE)
34 - pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
35 - header->len, NVRAM_SPACE - 1);
38 dst = (u32 *)nvram_buf;
39 for (i = 0; i < sizeof(struct nvram_header); i += 4)
40 *dst++ = __raw_readl(src++);
41 - for (; i < header->len && i < NVRAM_SPACE && i < size; i += 4)
42 + header = (struct nvram_header *)nvram_buf;
43 + if (header->len > size) {
44 + pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");
47 + if (header->len >= NVRAM_SPACE) {
48 + pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
49 + header->len, NVRAM_SPACE - 1);
50 + header->len = NVRAM_SPACE - 1;
52 + /* proceed reading data after header */
53 + for (; i < header->len; i += 4)
54 *dst++ = readl(src++);
55 nvram_buf[NVRAM_SPACE - 1] = '\0';
57 @@ -139,6 +144,7 @@ static int nvram_init(void)
60 struct nvram_header header;
61 + struct nvram_header *pheader;
65 @@ -147,20 +153,21 @@ static int nvram_init(void)
68 err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header);
69 - if (!err && header.magic == NVRAM_MAGIC) {
70 - u8 *dst = (uint8_t *)nvram_buf;
71 - size_t len = header.len;
73 - if (len >= NVRAM_SPACE) {
74 - len = NVRAM_SPACE - 1;
75 + if (!err && header.magic == NVRAM_MAGIC &&
76 + header.len > sizeof(header)) {
77 + if (header.len >= NVRAM_SPACE) {
78 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
80 + header.len, NVRAM_SPACE);
81 + header.len = NVRAM_SPACE - 1;
84 - err = mtd_read(mtd, 0, len, &bytes_read, dst);
85 + err = mtd_read(mtd, 0, header.len, &bytes_read,
90 + pheader = (struct nvram_header *)nvram_buf;
91 + pheader->len = header.len;
95 @@ -219,3 +226,26 @@ int bcm47xx_nvram_gpio_pin(const char *n
98 EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);
100 +char *bcm47xx_nvram_get_contents(size_t *nvram_size)
104 + struct nvram_header *header;
106 + if (!nvram_buf[0]) {
107 + err = nvram_init();
112 + header = (struct nvram_header *)nvram_buf;
113 + *nvram_size = header->len - sizeof(struct nvram_header);
114 + nvram = vmalloc(*nvram_size);
117 + memcpy(nvram, &nvram_buf[sizeof(struct nvram_header)], *nvram_size);
121 +EXPORT_SYMBOL(bcm47xx_nvram_get_contents);
122 --- a/include/linux/bcm47xx_nvram.h
123 +++ b/include/linux/bcm47xx_nvram.h
126 #include <linux/types.h>
127 #include <linux/kernel.h>
128 +#include <linux/vmalloc.h>
130 #ifdef CONFIG_BCM47XX
131 int bcm47xx_nvram_init_from_mem(u32 base, u32 lim);
132 int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len);
133 int bcm47xx_nvram_gpio_pin(const char *name);
134 +char *bcm47xx_nvram_get_contents(size_t *val_len);
135 +static inline void bcm47xx_nvram_release_contents(char *nvram)
140 static inline int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
142 @@ -29,6 +35,15 @@ static inline int bcm47xx_nvram_gpio_pin
147 +static inline char *bcm47xx_nvram_get_contents(size_t *val_len)
152 +static inline void bcm47xx_nvram_release_contents(char *nvram)
157 #endif /* __BCM47XX_NVRAM_H */