1 From 1e37bf84afacd5ba17b7a13a18ca2bc78aff05c0 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Fri, 15 Dec 2023 11:13:58 +0000
4 Subject: [PATCH] nvmem: brcm_nvram: store a copy of NVRAM content
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 This driver uses MMIO access for reading NVRAM from a flash device.
10 Underneath there is a flash controller that reads data and provides
13 Using MMIO interface affects controller configuration and may break real
14 controller driver. It was reported by multiple users of devices with
17 Modify driver to read & cache NVRAM content during init and use that
18 copy to provide NVMEM data when requested. On NAND flashes due to their
19 alignment NVRAM partitions can be quite big (1 MiB and more) while
20 actual NVRAM content stays quite small (usually 16 to 32 KiB). To avoid
21 allocating so much memory check for actual data length.
23 Link: https://lore.kernel.org/linux-mtd/CACna6rwf3_9QVjYcM+847biTX=K0EoWXuXcSMkJO1Vy_5vmVqA@mail.gmail.com/
24 Fixes: 3fef9ed0627a ("nvmem: brcm_nvram: new driver exposing Broadcom's NVRAM")
25 Cc: <Stable@vger.kernel.org>
26 Cc: Arınç ÜNAL <arinc.unal@arinc9.com>
27 Cc: Florian Fainelli <florian.fainelli@broadcom.com>
28 Cc: Scott Branden <scott.branden@broadcom.com>
29 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
30 Acked-by: Arınç ÜNAL <arinc.unal@arinc9.com>
31 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
32 Link: https://lore.kernel.org/r/20231215111358.316727-3-srinivas.kandagatla@linaro.org
33 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
35 drivers/nvmem/brcm_nvram.c | 134 ++++++++++++++++++++++++++-----------
36 1 file changed, 94 insertions(+), 40 deletions(-)
38 --- a/drivers/nvmem/brcm_nvram.c
39 +++ b/drivers/nvmem/brcm_nvram.c
42 #define NVRAM_MAGIC "FLSH"
45 + * struct brcm_nvram - driver state internal struct
47 + * @dev: NVMEM device pointer
48 + * @nvmem_size: Size of the whole space available for NVRAM
49 + * @data: NVRAM data copy stored to avoid poking underlaying flash controller
50 + * @data_len: NVRAM data size
51 + * @padding_byte: Padding value used to fill remaining space
52 + * @cells: Array of discovered NVMEM cells
53 + * @ncells: Number of elements in cells
61 + uint8_t padding_byte;
62 struct nvmem_cell_info *cells;
65 @@ -36,10 +50,47 @@ static int brcm_nvram_read(void *context
68 struct brcm_nvram *priv = context;
72 + if (offset + bytes > priv->data_len)
73 + to_copy = max_t(ssize_t, (ssize_t)priv->data_len - offset, 0);
77 + memcpy(val, priv->data + offset, to_copy);
79 + memset((uint8_t *)val + to_copy, priv->padding_byte, bytes - to_copy);
84 +static int brcm_nvram_copy_data(struct brcm_nvram *priv, struct platform_device *pdev)
86 + struct resource *res;
89 + base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
91 + return PTR_ERR(base);
93 + priv->nvmem_size = resource_size(res);
95 + priv->padding_byte = readb(base + priv->nvmem_size - 1);
96 + for (priv->data_len = priv->nvmem_size;
99 + if (readb(base + priv->data_len - 1) != priv->padding_byte)
102 + WARN(priv->data_len > SZ_128K, "Unexpected (big) NVRAM size: %zu B\n", priv->data_len);
105 - *dst++ = readb(priv->base + offset++);
106 + priv->data = devm_kzalloc(priv->dev, priv->data_len, GFP_KERNEL);
110 + memcpy_fromio(priv->data, base, priv->data_len);
112 + bcm47xx_nvram_init_from_iomem(base, priv->data_len);
116 @@ -67,8 +118,13 @@ static int brcm_nvram_add_cells(struct b
119 struct device *dev = priv->dev;
120 - char *var, *value, *eq;
126 + tmp = priv->data[len - 1];
127 + priv->data[len - 1] = '\0';
130 for (var = data + sizeof(struct brcm_nvram_header);
131 @@ -78,67 +134,68 @@ static int brcm_nvram_add_cells(struct b
134 priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
137 + if (!priv->cells) {
142 for (var = data + sizeof(struct brcm_nvram_header), idx = 0;
143 var < (char *)data + len && *var;
144 var = value + strlen(value) + 1, idx++) {
147 eq = strchr(var, '=');
151 + name = devm_kstrdup(dev, var, GFP_KERNEL);
159 - priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
160 - if (!priv->cells[idx].name)
162 + priv->cells[idx].name = name;
163 priv->cells[idx].offset = value - (char *)data;
164 priv->cells[idx].bytes = strlen(value);
165 priv->cells[idx].np = of_get_child_by_name(dev->of_node, priv->cells[idx].name);
166 - if (!strcmp(var, "et0macaddr") ||
167 - !strcmp(var, "et1macaddr") ||
168 - !strcmp(var, "et2macaddr")) {
169 + if (!strcmp(name, "et0macaddr") ||
170 + !strcmp(name, "et1macaddr") ||
171 + !strcmp(name, "et2macaddr")) {
172 priv->cells[idx].raw_len = strlen(value);
173 priv->cells[idx].bytes = ETH_ALEN;
174 priv->cells[idx].read_post_process = brcm_nvram_read_post_process_macaddr;
180 + priv->data[len - 1] = tmp;
184 static int brcm_nvram_parse(struct brcm_nvram *priv)
186 + struct brcm_nvram_header *header = (struct brcm_nvram_header *)priv->data;
187 struct device *dev = priv->dev;
188 - struct brcm_nvram_header header;
193 - memcpy_fromio(&header, priv->base, sizeof(header));
195 - if (memcmp(header.magic, NVRAM_MAGIC, 4)) {
196 + if (memcmp(header->magic, NVRAM_MAGIC, 4)) {
197 dev_err(dev, "Invalid NVRAM magic\n");
201 - len = le32_to_cpu(header.len);
203 - data = kzalloc(len, GFP_KERNEL);
207 - memcpy_fromio(data, priv->base, len);
208 - data[len - 1] = '\0';
210 - err = brcm_nvram_add_cells(priv, data, len);
212 - dev_err(dev, "Failed to add cells: %d\n", err);
214 + len = le32_to_cpu(header->len);
215 + if (len > priv->nvmem_size) {
216 + dev_err(dev, "NVRAM length (%zd) exceeds mapped size (%zd)\n", len,
222 + err = brcm_nvram_add_cells(priv, priv->data, len);
224 + dev_err(dev, "Failed to add cells: %d\n", err);
228 @@ -150,7 +207,6 @@ static int brcm_nvram_probe(struct platf
229 .reg_read = brcm_nvram_read,
231 struct device *dev = &pdev->dev;
232 - struct resource *res;
233 struct brcm_nvram *priv;
236 @@ -159,21 +215,19 @@ static int brcm_nvram_probe(struct platf
240 - priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
241 - if (IS_ERR(priv->base))
242 - return PTR_ERR(priv->base);
243 + err = brcm_nvram_copy_data(priv, pdev);
247 err = brcm_nvram_parse(priv);
251 - bcm47xx_nvram_init_from_iomem(priv->base, resource_size(res));
254 config.cells = priv->cells;
255 config.ncells = priv->ncells;
257 - config.size = resource_size(res);
258 + config.size = priv->nvmem_size;
260 return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));