beef1cf6758bc886e6455c873931edc115b2d1b1
[openwrt/staging/jogo.git] /
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
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 This avoids duplicating code doing casting and checking for NVRAM magic.
10
11 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
12 Signed-off-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
13 ---
14 drivers/firmware/broadcom/bcm47xx_nvram.c | 30 ++++++++++++++---------
15 1 file changed, 18 insertions(+), 12 deletions(-)
16
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};
24
25 +/**
26 + * bcm47xx_nvram_is_valid - check for a valid NVRAM at specified memory
27 + */
28 +static bool bcm47xx_nvram_is_valid(void __iomem *nvram)
29 +{
30 + return ((struct nvram_header *)nvram)->magic == NVRAM_MAGIC;
31 +}
32 +
33 static u32 find_nvram_size(void __iomem *end)
34 {
35 - struct nvram_header __iomem *header;
36 int i;
37
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];
43 }
44
45 @@ -55,6 +61,7 @@ static int bcm47xx_nvram_find_and_copy(void __iomem *flash_start, size_t res_siz
46 {
47 struct nvram_header __iomem *header;
48 size_t flash_size;
49 + size_t offset;
50 u32 size;
51
52 if (nvram_len) {
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);
56 if (size) {
57 - header = (struct nvram_header *)(flash_start + flash_size - size);
58 + offset = flash_size - size;
59 goto found;
60 }
61 flash_size <<= 1;
62 }
63
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) {
67 - size = NVRAM_SPACE;
68 +
69 + offset = 4096;
70 + if (bcm47xx_nvram_is_valid(flash_start + offset))
71 goto found;
72 - }
73
74 - header = (struct nvram_header *)(flash_start + 1024);
75 - if (header->magic == NVRAM_MAGIC) {
76 - size = NVRAM_SPACE;
77 + offset = 1024;
78 + if (bcm47xx_nvram_is_valid(flash_start + offset))
79 goto found;
80 - }
81
82 pr_err("no nvram found\n");
83 return -ENXIO;
84
85 found:
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");
92 nvram_len = size;
93 --
94 2.26.2
95