bfa93af41ce3baeb16ed2e3f2a5d8c0f9237ca2a
[openwrt/staging/chunkeey.git] /
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
5
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.
10
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>
20 ---
21 arch/mips/bcm47xx/nvram.c | 60 ++++++++++++++++++++++++++++++++-----------
22 include/linux/bcm47xx_nvram.h | 15 +++++++++++
23 2 files changed, 60 insertions(+), 15 deletions(-)
24
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)
30 return -ENXIO;
31
32 found:
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);
38 -
39 src = (u32 *)header;
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");
47 + header->len = size;
48 + }
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;
53 + }
54 + /* proceed reading data after header */
55 + for (; i < header->len; i += 4)
56 *dst++ = readl(src++);
57 nvram_buf[NVRAM_SPACE - 1] = '\0';
58
59 @@ -139,6 +144,7 @@ static int nvram_init(void)
60 #ifdef CONFIG_MTD
61 struct mtd_info *mtd;
62 struct nvram_header header;
63 + struct nvram_header *pheader;
64 size_t bytes_read;
65 int err;
66
67 @@ -147,20 +153,21 @@ static int nvram_init(void)
68 return -ENODEV;
69
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;
74 -
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",
81 - header.len, len);
82 + header.len, NVRAM_SPACE);
83 + header.len = NVRAM_SPACE - 1;
84 }
85
86 - err = mtd_read(mtd, 0, len, &bytes_read, dst);
87 + err = mtd_read(mtd, 0, header.len, &bytes_read,
88 + (u8 *)nvram_buf);
89 if (err)
90 return err;
91
92 + pheader = (struct nvram_header *)nvram_buf;
93 + pheader->len = header.len;
94 return 0;
95 }
96 #endif
97 @@ -219,3 +226,26 @@ int bcm47xx_nvram_gpio_pin(const char *name)
98 return -ENOENT;
99 }
100 EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);
101 +
102 +char *bcm47xx_nvram_get_contents(size_t *nvram_size)
103 +{
104 + int err;
105 + char *nvram;
106 + struct nvram_header *header;
107 +
108 + if (!nvram_buf[0]) {
109 + err = nvram_init();
110 + if (err)
111 + return NULL;
112 + }
113 +
114 + header = (struct nvram_header *)nvram_buf;
115 + *nvram_size = header->len - sizeof(struct nvram_header);
116 + nvram = vmalloc(*nvram_size);
117 + if (!nvram)
118 + return NULL;
119 + memcpy(nvram, &nvram_buf[sizeof(struct nvram_header)], *nvram_size);
120 +
121 + return nvram;
122 +}
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
128 @@ -10,11 +10,17 @@
129
130 #include <linux/types.h>
131 #include <linux/kernel.h>
132 +#include <linux/vmalloc.h>
133
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)
140 +{
141 + vfree(nvram);
142 +};
143 #else
144 static inline int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
145 {
146 @@ -29,6 +35,15 @@ static inline int bcm47xx_nvram_gpio_pin(const char *name)
147 {
148 return -ENOTSUPP;
149 };
150 +
151 +static inline char *bcm47xx_nvram_get_contents(size_t *val_len)
152 +{
153 + return NULL;
154 +};
155 +
156 +static inline void bcm47xx_nvram_release_contents(char *nvram)
157 +{
158 +};
159 #endif
160
161 #endif /* __BCM47XX_NVRAM_H */
162 --
163 1.8.4.5
164