1 From 0a24b51a3264a3f942a75025ea5ff6133c8989b0 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Mon, 8 Mar 2021 10:03:17 +0100
4 Subject: [PATCH] firmware: bcm47xx_nvram: add helper checking for NVRAM
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 This avoids duplicating code doing casting and checking for NVRAM magic.
11 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
12 Signed-off-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
14 drivers/firmware/broadcom/bcm47xx_nvram.c | 30 ++++++++++++++---------
15 1 file changed, 18 insertions(+), 12 deletions(-)
17 diff --git a/drivers/firmware/broadcom/bcm47xx_nvram.c b/drivers/firmware/broadcom/bcm47xx_nvram.c
18 index b04007adc79f..99f3ec180be6 100644
19 --- a/drivers/firmware/broadcom/bcm47xx_nvram.c
20 +++ b/drivers/firmware/broadcom/bcm47xx_nvram.c
21 @@ -34,14 +34,20 @@ static char nvram_buf[NVRAM_SPACE];
22 static size_t nvram_len;
23 static const u32 nvram_sizes[] = {0x6000, 0x8000, 0xF000, 0x10000};
26 + * bcm47xx_nvram_is_valid - check for a valid NVRAM at specified memory
28 +static bool bcm47xx_nvram_is_valid(void __iomem *nvram)
30 + return ((struct nvram_header *)nvram)->magic == NVRAM_MAGIC;
33 static u32 find_nvram_size(void __iomem *end)
35 - struct nvram_header __iomem *header;
38 for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
39 - header = (struct nvram_header *)(end - nvram_sizes[i]);
40 - if (header->magic == NVRAM_MAGIC)
41 + if (bcm47xx_nvram_is_valid(end - nvram_sizes[i]))
42 return nvram_sizes[i];
45 @@ -55,6 +61,7 @@ static int bcm47xx_nvram_find_and_copy(void __iomem *flash_start, size_t res_siz
47 struct nvram_header __iomem *header;
53 @@ -68,31 +75,30 @@ static int bcm47xx_nvram_find_and_copy(void __iomem *flash_start, size_t res_siz
54 /* Windowed flash access */
55 size = find_nvram_size(flash_start + flash_size);
57 - header = (struct nvram_header *)(flash_start + flash_size - size);
58 + offset = flash_size - size;
64 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
65 - header = (struct nvram_header *)(flash_start + 4096);
66 - if (header->magic == NVRAM_MAGIC) {
70 + if (bcm47xx_nvram_is_valid(flash_start + offset))
74 - header = (struct nvram_header *)(flash_start + 1024);
75 - if (header->magic == NVRAM_MAGIC) {
78 + if (bcm47xx_nvram_is_valid(flash_start + offset))
82 pr_err("no nvram found\n");
86 + header = (struct nvram_header *)(flash_start + offset);
87 __ioread32_copy(nvram_buf, header, sizeof(*header) / 4);
88 nvram_len = ((struct nvram_header *)(nvram_buf))->len;
89 + size = res_size - offset;
90 if (nvram_len > size) {
91 pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");