105c65142a8e2b9bd610b000f678ebac78a2a32f
[openwrt/staging/robimarko.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 --- 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
28 return -ENXIO;
29
30 found:
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);
36 -
37 src = (u32 *)header;
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");
45 + header->len = size;
46 + }
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;
51 + }
52 + /* proceed reading data after header */
53 + for (; i < header->len; i += 4)
54 *dst++ = readl(src++);
55 nvram_buf[NVRAM_SPACE - 1] = '\0';
56
57 @@ -139,6 +144,7 @@ static int nvram_init(void)
58 #ifdef CONFIG_MTD
59 struct mtd_info *mtd;
60 struct nvram_header header;
61 + struct nvram_header *pheader;
62 size_t bytes_read;
63 int err;
64
65 @@ -147,20 +153,21 @@ static int nvram_init(void)
66 return -ENODEV;
67
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;
72 -
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",
79 - header.len, len);
80 + header.len, NVRAM_SPACE);
81 + header.len = NVRAM_SPACE - 1;
82 }
83
84 - err = mtd_read(mtd, 0, len, &bytes_read, dst);
85 + err = mtd_read(mtd, 0, header.len, &bytes_read,
86 + (u8 *)nvram_buf);
87 if (err)
88 return err;
89
90 + pheader = (struct nvram_header *)nvram_buf;
91 + pheader->len = header.len;
92 return 0;
93 }
94 #endif
95 @@ -219,3 +226,26 @@ int bcm47xx_nvram_gpio_pin(const char *n
96 return -ENOENT;
97 }
98 EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);
99 +
100 +char *bcm47xx_nvram_get_contents(size_t *nvram_size)
101 +{
102 + int err;
103 + char *nvram;
104 + struct nvram_header *header;
105 +
106 + if (!nvram_buf[0]) {
107 + err = nvram_init();
108 + if (err)
109 + return NULL;
110 + }
111 +
112 + header = (struct nvram_header *)nvram_buf;
113 + *nvram_size = header->len - sizeof(struct nvram_header);
114 + nvram = vmalloc(*nvram_size);
115 + if (!nvram)
116 + return NULL;
117 + memcpy(nvram, &nvram_buf[sizeof(struct nvram_header)], *nvram_size);
118 +
119 + return nvram;
120 +}
121 +EXPORT_SYMBOL(bcm47xx_nvram_get_contents);
122 --- a/include/linux/bcm47xx_nvram.h
123 +++ b/include/linux/bcm47xx_nvram.h
124 @@ -10,11 +10,17 @@
125
126 #include <linux/types.h>
127 #include <linux/kernel.h>
128 +#include <linux/vmalloc.h>
129
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)
136 +{
137 + vfree(nvram);
138 +};
139 #else
140 static inline int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
141 {
142 @@ -29,6 +35,15 @@ static inline int bcm47xx_nvram_gpio_pin
143 {
144 return -ENOTSUPP;
145 };
146 +
147 +static inline char *bcm47xx_nvram_get_contents(size_t *val_len)
148 +{
149 + return NULL;
150 +};
151 +
152 +static inline void bcm47xx_nvram_release_contents(char *nvram)
153 +{
154 +};
155 #endif
156
157 #endif /* __BCM47XX_NVRAM_H */